r14402: Generate seperate headers for RPC client functions.
[samba.git] / source4 / libnet / libnet_share.c
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "libnet/libnet.h"
23 #include "librpc/gen_ndr/ndr_srvsvc_c.h"
24
25
26 NTSTATUS libnet_ListShares(struct libnet_context *ctx, 
27                            TALLOC_CTX *mem_ctx, struct libnet_ListShares *r)
28 {
29         NTSTATUS status;
30         struct libnet_RpcConnect c;
31         struct srvsvc_NetShareEnumAll s;
32         uint32_t resume_handle = 0;
33         struct srvsvc_NetShareCtr0 ctr0;
34         struct srvsvc_NetShareCtr1 ctr1;
35         struct srvsvc_NetShareCtr2 ctr2;
36         struct srvsvc_NetShareCtr501 ctr501;
37         struct srvsvc_NetShareCtr502 ctr502;
38
39         c.level               = LIBNET_RPC_CONNECT_SERVER;
40         c.in.name             = r->in.server_name;
41         c.in.dcerpc_iface     = &dcerpc_table_srvsvc;
42
43         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", c.in.name);
44
45         status = libnet_RpcConnect(ctx, mem_ctx, &c);
46         if (!NT_STATUS_IS_OK(status)) {
47                 r->out.error_string = talloc_asprintf(mem_ctx,
48                                                       "Connection to SRVSVC pipe of server %s "
49                                                       "failed: %s\n",
50                                                       r->in.server_name,
51                                                       nt_errstr(status));
52                 return status;
53         }
54
55         s.in.level = r->in.level;
56         switch (s.in.level) {
57         case 0:
58                 s.in.ctr.ctr0 = &ctr0;
59                 ZERO_STRUCT(ctr0);
60                 break;
61         case 1:
62                 s.in.ctr.ctr1 = &ctr1;
63                 ZERO_STRUCT(ctr1);
64                 break;
65         case 2:
66                 s.in.ctr.ctr2 = &ctr2;
67                 ZERO_STRUCT(ctr2);
68                 break;
69         case 501:
70                 s.in.ctr.ctr501 = &ctr501;
71                 ZERO_STRUCT(ctr501);
72                 break;
73         case 502:
74                 s.in.ctr.ctr502 = &ctr502;
75                 ZERO_STRUCT(ctr502);
76                 break;
77         default:
78                 r->out.error_string = talloc_asprintf(mem_ctx,
79                                                       "libnet_ListShares: Invalid info level requested: %d\n",
80                                                       s.in.level);
81                 return NT_STATUS_INVALID_PARAMETER;
82         }
83         s.in.max_buffer = ~0;
84         s.in.resume_handle = &resume_handle;
85
86
87         status = dcerpc_srvsvc_NetShareEnumAll(c.out.dcerpc_pipe, mem_ctx, &s);
88         
89         if (!NT_STATUS_IS_OK(status)) {
90                 r->out.error_string = talloc_asprintf(mem_ctx,
91                                                       "srvsvc_NetShareEnumAll on server '%s' failed"
92                                                       ": %s\n",
93                                                       r->in.server_name, nt_errstr(status));
94                 goto disconnect;
95         }
96
97         if (!W_ERROR_IS_OK(s.out.result) && !W_ERROR_EQUAL(s.out.result, WERR_MORE_DATA)) {
98                 r->out.error_string = talloc_asprintf(mem_ctx,
99                                                       "srvsvc_NetShareEnumAll on server '%s' failed: %s",
100                                                       r->in.server_name, win_errstr(s.out.result));
101                 goto disconnect;
102         }
103
104         r->out.ctr = s.out.ctr;
105
106 disconnect:
107         talloc_free(c.out.dcerpc_pipe);
108
109         return status;  
110 }
111
112
113 NTSTATUS libnet_AddShare(struct libnet_context *ctx,
114                          TALLOC_CTX *mem_ctx, struct libnet_AddShare *r)
115 {
116         NTSTATUS status;
117         struct libnet_RpcConnect c;
118         struct srvsvc_NetShareAdd s;
119
120         c.level              = LIBNET_RPC_CONNECT_SERVER;
121         c.in.name            = r->in.server_name;
122         c.in.dcerpc_iface    = &dcerpc_table_srvsvc;
123
124         status = libnet_RpcConnect(ctx, mem_ctx, &c);
125         if (!NT_STATUS_IS_OK(status)) {
126                 r->out.error_string = talloc_asprintf(mem_ctx,
127                                                       "Connection to SRVSVC pipe of server %s "
128                                                       "failed: %s\n",
129                                                       r->in.server_name, nt_errstr(status));
130                 return status;
131         }
132
133         s.in.level              = r->in.level;
134         s.in.info.info2         = &r->in.share;
135         s.in.server_unc         = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
136  
137         status = dcerpc_srvsvc_NetShareAdd(c.out.dcerpc_pipe, mem_ctx,&s);      
138
139         if (!NT_STATUS_IS_OK(status)) {
140                 r->out.error_string = talloc_asprintf(mem_ctx,
141                                                       "srvsvc_NetShareAdd on server '%s' failed"
142                                                       ": %s\n",
143                                                       r->in.server_name, nt_errstr(status));
144         }
145
146         talloc_free(c.out.dcerpc_pipe);
147         
148         return status;
149 }
150
151
152 NTSTATUS libnet_DelShare(struct libnet_context *ctx,
153                          TALLOC_CTX *mem_ctx, struct libnet_DelShare *r)
154 {
155         NTSTATUS status;
156         struct libnet_RpcConnect c;
157         struct srvsvc_NetShareDel s;
158
159         c.level               = LIBNET_RPC_CONNECT_SERVER;
160         c.in.name             = r->in.server_name;
161         c.in.dcerpc_iface     = &dcerpc_table_srvsvc;
162
163         status = libnet_RpcConnect(ctx, mem_ctx, &c);
164         if (!NT_STATUS_IS_OK(status)) {
165                 r->out.error_string = talloc_asprintf(mem_ctx,
166                                                       "Connection to SRVSVC pipe of server %s "
167                                                       "failed: %s\n",
168                                                       r->in.server_name, nt_errstr(status));
169                 return status;
170         } 
171                 
172         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
173         s.in.share_name = r->in.share_name;
174
175         status = dcerpc_srvsvc_NetShareDel(c.out.dcerpc_pipe, mem_ctx, &s);
176         if (!NT_STATUS_IS_OK(status)) {
177                 r->out.error_string = talloc_asprintf(mem_ctx,
178                                                       "srvsvc_NetShareDel on server '%s' failed"
179                                                       ": %s\n",
180                                                       r->in.server_name, nt_errstr(status));
181         }
182
183         talloc_free(c.out.dcerpc_pipe);
184
185         return status;
186 }