LCOV - code coverage report
Current view: top level - source4/libnet - libnet_share.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 73 110 66.4 %
Date: 2024-04-21 15:09:00 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* 
       2             :    Unix SMB/CIFS implementation.
       3             :    
       4             :    Copyright (C) Grégory LEOCADIE <gleocadie@idealx.com>
       5             :    
       6             :    This program is free software; you can redistribute it and/or modify
       7             :    it under the terms of the GNU General Public License as published by
       8             :    the Free Software Foundation; either version 3 of the License, or
       9             :    (at your option) any later version.
      10             :    
      11             :    This program is distributed in the hope that it will be useful,
      12             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :    GNU General Public License for more details.
      15             :    
      16             :    You should have received a copy of the GNU General Public License
      17             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      18             : */
      19             : 
      20             : #include "includes.h"
      21             : #include "libnet/libnet.h"
      22             : #include "librpc/gen_ndr/ndr_srvsvc_c.h"
      23             : 
      24             : 
      25           5 : NTSTATUS libnet_ListShares(struct libnet_context *ctx, 
      26             :                            TALLOC_CTX *mem_ctx, struct libnet_ListShares *r)
      27             : {
      28           5 :         NTSTATUS status;
      29           5 :         struct libnet_RpcConnect c;
      30           5 :         struct srvsvc_NetShareEnumAll s;
      31           5 :         struct srvsvc_NetShareInfoCtr info_ctr;
      32           5 :         uint32_t resume_handle = 0;
      33           5 :         uint32_t totalentries = 0;
      34           5 :         struct srvsvc_NetShareCtr0 ctr0;
      35           5 :         struct srvsvc_NetShareCtr1 ctr1;
      36           5 :         struct srvsvc_NetShareCtr2 ctr2;
      37           5 :         struct srvsvc_NetShareCtr501 ctr501;
      38           5 :         struct srvsvc_NetShareCtr502 ctr502;
      39             : 
      40           5 :         ZERO_STRUCT(c);
      41             : 
      42           5 :         c.level               = LIBNET_RPC_CONNECT_SERVER;
      43           5 :         c.in.name             = r->in.server_name;
      44           5 :         c.in.dcerpc_iface     = &ndr_table_srvsvc;
      45             : 
      46           5 :         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", c.in.name);
      47             : 
      48           5 :         status = libnet_RpcConnect(ctx, mem_ctx, &c);
      49           5 :         if (!NT_STATUS_IS_OK(status)) {
      50           0 :                 r->out.error_string = talloc_asprintf(mem_ctx,
      51             :                                                       "Connection to SRVSVC pipe of server %s "
      52             :                                                       "failed: %s",
      53             :                                                       r->in.server_name,
      54             :                                                       nt_errstr(status));
      55           0 :                 return status;
      56             :         }
      57             : 
      58           5 :         info_ctr.level = r->in.level;
      59           5 :         switch (info_ctr.level) {
      60           1 :         case 0:
      61           1 :                 info_ctr.ctr.ctr0 = &ctr0;
      62           1 :                 ZERO_STRUCT(ctr0);
      63           1 :                 break;
      64           1 :         case 1:
      65           1 :                 info_ctr.ctr.ctr1 = &ctr1;
      66           1 :                 ZERO_STRUCT(ctr1);
      67           1 :                 break;
      68           1 :         case 2:
      69           1 :                 info_ctr.ctr.ctr2 = &ctr2;
      70           1 :                 ZERO_STRUCT(ctr2);
      71           1 :                 break;
      72           1 :         case 501:
      73           1 :                 info_ctr.ctr.ctr501 = &ctr501;
      74           1 :                 ZERO_STRUCT(ctr501);
      75           1 :                 break;
      76           1 :         case 502:
      77           1 :                 info_ctr.ctr.ctr502 = &ctr502;
      78           1 :                 ZERO_STRUCT(ctr502);
      79           1 :                 break;
      80           0 :         default:
      81           0 :                 r->out.error_string = talloc_asprintf(mem_ctx,
      82             :                                                       "libnet_ListShares: Invalid info level requested: %d",
      83             :                                                       info_ctr.level);
      84           0 :                 return NT_STATUS_INVALID_PARAMETER;
      85             :         }
      86           5 :         s.in.max_buffer = ~0;
      87           5 :         s.in.resume_handle = &resume_handle;
      88           5 :         s.in.info_ctr = &info_ctr;
      89           5 :         s.out.info_ctr = &info_ctr;
      90           5 :         s.out.totalentries = &totalentries;
      91             : 
      92           5 :         status = dcerpc_srvsvc_NetShareEnumAll_r(c.out.dcerpc_pipe->binding_handle, mem_ctx, &s);
      93             :         
      94           5 :         if (!NT_STATUS_IS_OK(status)) {
      95           0 :                 r->out.error_string = talloc_asprintf(mem_ctx,
      96             :                                                       "srvsvc_NetShareEnumAll on server '%s' failed"
      97             :                                                       ": %s",
      98             :                                                       r->in.server_name, nt_errstr(status));
      99           0 :                 goto disconnect;
     100             :         }
     101             : 
     102           5 :         if (!W_ERROR_IS_OK(s.out.result) && !W_ERROR_EQUAL(s.out.result, WERR_MORE_DATA)) {
     103           0 :                 r->out.error_string = talloc_asprintf(mem_ctx,
     104             :                                                       "srvsvc_NetShareEnumAll on server '%s' failed: %s",
     105             :                                                       r->in.server_name, win_errstr(s.out.result));
     106           0 :                 goto disconnect;
     107             :         }
     108             : 
     109           5 :         r->out.ctr = s.out.info_ctr->ctr;
     110             : 
     111           5 : disconnect:
     112           5 :         talloc_free(c.out.dcerpc_pipe);
     113             : 
     114           5 :         return status;  
     115             : }
     116             : 
     117             : 
     118           0 : NTSTATUS libnet_AddShare(struct libnet_context *ctx,
     119             :                          TALLOC_CTX *mem_ctx, struct libnet_AddShare *r)
     120             : {
     121           0 :         NTSTATUS status;
     122           0 :         struct libnet_RpcConnect c;
     123           0 :         struct srvsvc_NetShareAdd s;
     124           0 :         union srvsvc_NetShareInfo info;
     125             : 
     126           0 :         ZERO_STRUCT(c);
     127             : 
     128           0 :         c.level              = LIBNET_RPC_CONNECT_SERVER;
     129           0 :         c.in.name            = r->in.server_name;
     130           0 :         c.in.dcerpc_iface    = &ndr_table_srvsvc;
     131             : 
     132           0 :         status = libnet_RpcConnect(ctx, mem_ctx, &c);
     133           0 :         if (!NT_STATUS_IS_OK(status)) {
     134           0 :                 r->out.error_string = talloc_asprintf(mem_ctx,
     135             :                                                       "Connection to SRVSVC pipe of server %s "
     136             :                                                       "failed: %s",
     137             :                                                       r->in.server_name, nt_errstr(status));
     138           0 :                 return status;
     139             :         }
     140             : 
     141           0 :         info.info2              = &r->in.share;
     142             : 
     143           0 :         s.in.level              = 2;
     144           0 :         s.in.info               = &info;
     145           0 :         s.in.server_unc         = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
     146             :  
     147           0 :         status = dcerpc_srvsvc_NetShareAdd_r(c.out.dcerpc_pipe->binding_handle, mem_ctx, &s);
     148             : 
     149           0 :         if (!NT_STATUS_IS_OK(status)) {
     150           0 :                 r->out.error_string = talloc_asprintf(mem_ctx,
     151             :                                                       "srvsvc_NetShareAdd '%s' on server '%s' failed"
     152             :                                                       ": %s",
     153             :                                                       r->in.share.name, r->in.server_name, 
     154             :                                                       nt_errstr(status));
     155           0 :         } else if (!W_ERROR_IS_OK(s.out.result)) {
     156           0 :                 r->out.error_string = talloc_asprintf(mem_ctx,
     157             :                                                       "srvsvc_NetShareAdd '%s' on server '%s' failed"
     158             :                                                       ": %s",
     159             :                                                       r->in.share.name, r->in.server_name, 
     160             :                                                       win_errstr(s.out.result));
     161           0 :                 status = werror_to_ntstatus(s.out.result);
     162             :         }
     163             : 
     164           0 :         talloc_free(c.out.dcerpc_pipe);
     165             :         
     166           0 :         return status;
     167             : }
     168             : 
     169             : 
     170           1 : NTSTATUS libnet_DelShare(struct libnet_context *ctx,
     171             :                          TALLOC_CTX *mem_ctx, struct libnet_DelShare *r)
     172             : {
     173           1 :         NTSTATUS status;
     174           1 :         struct libnet_RpcConnect c;
     175           1 :         struct srvsvc_NetShareDel s;
     176             : 
     177           1 :         ZERO_STRUCT(c);
     178           1 :         ZERO_STRUCT(s);
     179             : 
     180           1 :         c.level               = LIBNET_RPC_CONNECT_SERVER;
     181           1 :         c.in.name             = r->in.server_name;
     182           1 :         c.in.dcerpc_iface     = &ndr_table_srvsvc;
     183             : 
     184           1 :         status = libnet_RpcConnect(ctx, mem_ctx, &c);
     185           1 :         if (!NT_STATUS_IS_OK(status)) {
     186           0 :                 r->out.error_string = talloc_asprintf(mem_ctx,
     187             :                                                       "Connection to SRVSVC pipe of server %s "
     188             :                                                       "failed: %s",
     189             :                                                       r->in.server_name, nt_errstr(status));
     190           0 :                 return status;
     191             :         } 
     192             :                 
     193           1 :         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
     194           1 :         s.in.share_name = r->in.share_name;
     195             : 
     196           1 :         status = dcerpc_srvsvc_NetShareDel_r(c.out.dcerpc_pipe->binding_handle, mem_ctx, &s);
     197           1 :         if (!NT_STATUS_IS_OK(status)) {
     198           0 :                 r->out.error_string = talloc_asprintf(mem_ctx,
     199             :                                                       "srvsvc_NetShareDel '%s' on server '%s' failed"
     200             :                                                       ": %s",
     201             :                                                       r->in.share_name, r->in.server_name, 
     202             :                                                       nt_errstr(status));
     203           1 :         } else if (!W_ERROR_IS_OK(s.out.result)) {
     204           1 :                 r->out.error_string = talloc_asprintf(mem_ctx,
     205             :                                                       "srvsvc_NetShareDel '%s' on server '%s' failed"
     206             :                                                       ": %s",
     207             :                                                       r->in.share_name, r->in.server_name, 
     208             :                                                       win_errstr(s.out.result));
     209           1 :                 status = werror_to_ntstatus(s.out.result);
     210             :         }
     211             : 
     212           1 :         talloc_free(c.out.dcerpc_pipe);
     213             : 
     214           1 :         return status;
     215             : }

Generated by: LCOV version 1.14