s4-srvsvc: merge srvsvc_NetShareEnumAll from s3 idl.
[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 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 NTSTATUS libnet_ListShares(struct libnet_context *ctx, 
26                            TALLOC_CTX *mem_ctx, struct libnet_ListShares *r)
27 {
28         NTSTATUS status;
29         struct libnet_RpcConnect c;
30         struct srvsvc_NetShareEnumAll s;
31         struct srvsvc_NetShareInfoCtr info_ctr;
32         uint32_t resume_handle = 0;
33         uint32_t totalentries = 0;
34         struct srvsvc_NetShareCtr0 ctr0;
35         struct srvsvc_NetShareCtr1 ctr1;
36         struct srvsvc_NetShareCtr2 ctr2;
37         struct srvsvc_NetShareCtr501 ctr501;
38         struct srvsvc_NetShareCtr502 ctr502;
39
40         c.level               = LIBNET_RPC_CONNECT_SERVER;
41         c.in.name             = r->in.server_name;
42         c.in.dcerpc_iface     = &ndr_table_srvsvc;
43
44         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", c.in.name);
45
46         status = libnet_RpcConnect(ctx, mem_ctx, &c);
47         if (!NT_STATUS_IS_OK(status)) {
48                 r->out.error_string = talloc_asprintf(mem_ctx,
49                                                       "Connection to SRVSVC pipe of server %s "
50                                                       "failed: %s",
51                                                       r->in.server_name,
52                                                       nt_errstr(status));
53                 return status;
54         }
55
56         info_ctr.level = r->in.level;
57         switch (info_ctr.level) {
58         case 0:
59                 info_ctr.ctr.ctr0 = &ctr0;
60                 ZERO_STRUCT(ctr0);
61                 break;
62         case 1:
63                 info_ctr.ctr.ctr1 = &ctr1;
64                 ZERO_STRUCT(ctr1);
65                 break;
66         case 2:
67                 info_ctr.ctr.ctr2 = &ctr2;
68                 ZERO_STRUCT(ctr2);
69                 break;
70         case 501:
71                 info_ctr.ctr.ctr501 = &ctr501;
72                 ZERO_STRUCT(ctr501);
73                 break;
74         case 502:
75                 info_ctr.ctr.ctr502 = &ctr502;
76                 ZERO_STRUCT(ctr502);
77                 break;
78         default:
79                 r->out.error_string = talloc_asprintf(mem_ctx,
80                                                       "libnet_ListShares: Invalid info level requested: %d",
81                                                       info_ctr.level);
82                 return NT_STATUS_INVALID_PARAMETER;
83         }
84         s.in.max_buffer = ~0;
85         s.in.resume_handle = &resume_handle;
86         s.in.info_ctr = &info_ctr;
87         s.out.info_ctr = &info_ctr;
88         s.out.totalentries = &totalentries;
89
90         status = dcerpc_srvsvc_NetShareEnumAll(c.out.dcerpc_pipe, mem_ctx, &s);
91         
92         if (!NT_STATUS_IS_OK(status)) {
93                 r->out.error_string = talloc_asprintf(mem_ctx,
94                                                       "srvsvc_NetShareEnumAll on server '%s' failed"
95                                                       ": %s",
96                                                       r->in.server_name, nt_errstr(status));
97                 goto disconnect;
98         }
99
100         if (!W_ERROR_IS_OK(s.out.result) && !W_ERROR_EQUAL(s.out.result, WERR_MORE_DATA)) {
101                 r->out.error_string = talloc_asprintf(mem_ctx,
102                                                       "srvsvc_NetShareEnumAll on server '%s' failed: %s",
103                                                       r->in.server_name, win_errstr(s.out.result));
104                 goto disconnect;
105         }
106
107         r->out.ctr = s.out.info_ctr->ctr;
108
109 disconnect:
110         talloc_free(c.out.dcerpc_pipe);
111
112         return status;  
113 }
114
115
116 NTSTATUS libnet_AddShare(struct libnet_context *ctx,
117                          TALLOC_CTX *mem_ctx, struct libnet_AddShare *r)
118 {
119         NTSTATUS status;
120         struct libnet_RpcConnect c;
121         struct srvsvc_NetShareAdd s;
122         union srvsvc_NetShareInfo info;
123
124         c.level              = LIBNET_RPC_CONNECT_SERVER;
125         c.in.name            = r->in.server_name;
126         c.in.dcerpc_iface    = &ndr_table_srvsvc;
127
128         status = libnet_RpcConnect(ctx, mem_ctx, &c);
129         if (!NT_STATUS_IS_OK(status)) {
130                 r->out.error_string = talloc_asprintf(mem_ctx,
131                                                       "Connection to SRVSVC pipe of server %s "
132                                                       "failed: %s",
133                                                       r->in.server_name, nt_errstr(status));
134                 return status;
135         }
136
137         info.info2              = &r->in.share;
138
139         s.in.level              = 2;
140         s.in.info               = &info;
141         s.in.server_unc         = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
142  
143         status = dcerpc_srvsvc_NetShareAdd(c.out.dcerpc_pipe, mem_ctx, &s);     
144
145         if (!NT_STATUS_IS_OK(status)) {
146                 r->out.error_string = talloc_asprintf(mem_ctx,
147                                                       "srvsvc_NetShareAdd '%s' on server '%s' failed"
148                                                       ": %s",
149                                                       r->in.share.name, r->in.server_name, 
150                                                       nt_errstr(status));
151         } else if (!W_ERROR_IS_OK(s.out.result)) {
152                 r->out.error_string = talloc_asprintf(mem_ctx,
153                                                       "srvsvc_NetShareAdd '%s' on server '%s' failed"
154                                                       ": %s",
155                                                       r->in.share.name, r->in.server_name, 
156                                                       win_errstr(s.out.result));
157                 status = werror_to_ntstatus(s.out.result);
158         }
159
160         talloc_free(c.out.dcerpc_pipe);
161         
162         return status;
163 }
164
165
166 NTSTATUS libnet_DelShare(struct libnet_context *ctx,
167                          TALLOC_CTX *mem_ctx, struct libnet_DelShare *r)
168 {
169         NTSTATUS status;
170         struct libnet_RpcConnect c;
171         struct srvsvc_NetShareDel s;
172
173         c.level               = LIBNET_RPC_CONNECT_SERVER;
174         c.in.name             = r->in.server_name;
175         c.in.dcerpc_iface     = &ndr_table_srvsvc;
176
177         status = libnet_RpcConnect(ctx, mem_ctx, &c);
178         if (!NT_STATUS_IS_OK(status)) {
179                 r->out.error_string = talloc_asprintf(mem_ctx,
180                                                       "Connection to SRVSVC pipe of server %s "
181                                                       "failed: %s",
182                                                       r->in.server_name, nt_errstr(status));
183                 return status;
184         } 
185                 
186         s.in.server_unc = talloc_asprintf(mem_ctx, "\\\\%s", r->in.server_name);
187         s.in.share_name = r->in.share_name;
188
189         status = dcerpc_srvsvc_NetShareDel(c.out.dcerpc_pipe, mem_ctx, &s);
190         if (!NT_STATUS_IS_OK(status)) {
191                 r->out.error_string = talloc_asprintf(mem_ctx,
192                                                       "srvsvc_NetShareDel '%s' on server '%s' failed"
193                                                       ": %s",
194                                                       r->in.share_name, r->in.server_name, 
195                                                       nt_errstr(status));
196         } else if (!W_ERROR_IS_OK(s.out.result)) {
197                 r->out.error_string = talloc_asprintf(mem_ctx,
198                                                       "srvsvc_NetShareDel '%s' on server '%s' failed"
199                                                       ": %s",
200                                                       r->in.share_name, r->in.server_name, 
201                                                       win_errstr(s.out.result));
202                 status = werror_to_ntstatus(s.out.result);
203         }
204
205         talloc_free(c.out.dcerpc_pipe);
206
207         return status;
208 }