netapi: implement NetShareGetInfo_r.
[kamenim/samba.git] / source3 / lib / netapi / share.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  NetApi Share Support
4  *  Copyright (C) Guenther Deschner 2008
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
22 #include "librpc/gen_ndr/libnetapi.h"
23 #include "lib/netapi/netapi.h"
24 #include "lib/netapi/netapi_private.h"
25 #include "lib/netapi/libnetapi.h"
26
27 /****************************************************************
28 ****************************************************************/
29
30 static NTSTATUS map_srvsvc_share_info_to_SHARE_INFO_buffer(TALLOC_CTX *mem_ctx,
31                                                            uint32_t level,
32                                                            union srvsvc_NetShareInfo *info,
33                                                            uint8_t **buffer,
34                                                            uint32_t *num_shares)
35 {
36         struct SHARE_INFO_0 i0;
37         struct SHARE_INFO_1 i1;
38         struct SHARE_INFO_2 i2;
39         struct SHARE_INFO_501 i501;
40         struct SHARE_INFO_1005 i1005;
41
42         struct srvsvc_NetShareInfo0 *s0;
43         struct srvsvc_NetShareInfo1 *s1;
44         struct srvsvc_NetShareInfo2 *s2;
45         struct srvsvc_NetShareInfo501 *s501;
46         struct srvsvc_NetShareInfo1005 *s1005;
47
48         if (!buffer) {
49                 return NT_STATUS_INVALID_PARAMETER;
50         }
51
52         switch (level) {
53                 case 0:
54                         s0 = info->info0;
55
56                         i0.shi0_netname         = talloc_strdup(mem_ctx, s0->name);
57
58                         ADD_TO_ARRAY(mem_ctx, struct SHARE_INFO_0, i0,
59                                      (struct SHARE_INFO_0 **)buffer,
60                                      num_shares);
61                         break;
62
63                 case 1:
64                         s1 = info->info1;
65
66                         i1.shi1_netname         = talloc_strdup(mem_ctx, s1->name);
67                         i1.shi1_type            = s1->type;
68                         i1.shi1_remark          = talloc_strdup(mem_ctx, s1->comment);
69
70                         ADD_TO_ARRAY(mem_ctx, struct SHARE_INFO_1, i1,
71                                      (struct SHARE_INFO_1 **)buffer,
72                                      num_shares);
73                         break;
74
75                 case 2:
76                         s2 = info->info2;
77
78                         i2.shi2_netname         = talloc_strdup(mem_ctx, s2->name);
79                         i2.shi2_type            = s2->type;
80                         i2.shi2_remark          = talloc_strdup(mem_ctx, s2->comment);
81                         i2.shi2_permissions     = s2->permissions;
82                         i2.shi2_max_uses        = s2->max_users;
83                         i2.shi2_current_uses    = s2->current_users;
84                         i2.shi2_path            = talloc_strdup(mem_ctx, s2->path);
85                         i2.shi2_passwd          = talloc_strdup(mem_ctx, s2->password);
86
87                         ADD_TO_ARRAY(mem_ctx, struct SHARE_INFO_2, i2,
88                                      (struct SHARE_INFO_2 **)buffer,
89                                      num_shares);
90                         break;
91
92                 case 501:
93                         s501 = info->info501;
94
95                         i501.shi501_netname             = talloc_strdup(mem_ctx, s501->name);
96                         i501.shi501_type                = s501->type;
97                         i501.shi501_remark              = talloc_strdup(mem_ctx, s501->comment);
98                         i501.shi501_flags               = s501->csc_policy;
99
100                         ADD_TO_ARRAY(mem_ctx, struct SHARE_INFO_501, i501,
101                                      (struct SHARE_INFO_501 **)buffer,
102                                      num_shares);
103                         break;
104
105                 case 1005:
106                         s1005 = info->info1005;
107
108                         i1005.shi1005_flags             = s1005->dfs_flags;
109
110                         ADD_TO_ARRAY(mem_ctx, struct SHARE_INFO_1005, i1005,
111                                      (struct SHARE_INFO_1005 **)buffer,
112                                      num_shares);
113                         break;
114
115                 default:
116                         return NT_STATUS_INVALID_PARAMETER;
117         }
118
119         return NT_STATUS_OK;
120 }
121
122 /****************************************************************
123 ****************************************************************/
124
125 static NTSTATUS map_SHARE_INFO_buffer_to_srvsvc_share_info(TALLOC_CTX *mem_ctx,
126                                                            uint8_t *buffer,
127                                                            uint32_t level,
128                                                            union srvsvc_NetShareInfo *info)
129 {
130         struct SHARE_INFO_2 *i2 = NULL;
131         struct srvsvc_NetShareInfo2 *s2 = NULL;
132
133         if (!buffer) {
134                 return NT_STATUS_INVALID_PARAMETER;
135         }
136
137         switch (level) {
138                 case 2:
139                         i2 = (struct SHARE_INFO_2 *)buffer;
140
141                         s2 = TALLOC_P(mem_ctx, struct srvsvc_NetShareInfo2);
142                         NT_STATUS_HAVE_NO_MEMORY(s2);
143
144                         s2->name                = i2->shi2_netname;
145                         s2->type                = i2->shi2_type;
146                         s2->comment             = i2->shi2_remark;
147                         s2->permissions         = i2->shi2_permissions;
148                         s2->max_users           = i2->shi2_max_uses;
149                         s2->current_users       = i2->shi2_current_uses;
150                         s2->path                = i2->shi2_path;
151                         s2->password            = i2->shi2_passwd;
152
153                         info->info2 = s2;
154
155                         break;
156                 default:
157                         return NT_STATUS_INVALID_PARAMETER;
158         }
159
160         return NT_STATUS_OK;
161 }
162
163 /****************************************************************
164 ****************************************************************/
165
166 WERROR NetShareAdd_r(struct libnetapi_ctx *ctx,
167                      struct NetShareAdd *r)
168 {
169         WERROR werr;
170         NTSTATUS status;
171         struct cli_state *cli = NULL;
172         struct rpc_pipe_client *pipe_cli = NULL;
173         union srvsvc_NetShareInfo info;
174
175         if (!r->in.buffer) {
176                 return WERR_INVALID_PARAM;
177         }
178
179         switch (r->in.level) {
180                 case 2:
181                         break;
182                 case 502:
183                 case 503:
184                         return WERR_NOT_SUPPORTED;
185                 default:
186                         return WERR_UNKNOWN_LEVEL;
187         }
188
189         werr = libnetapi_open_pipe(ctx, r->in.server_name,
190                                    &ndr_table_srvsvc.syntax_id,
191                                    &cli,
192                                    &pipe_cli);
193         if (!W_ERROR_IS_OK(werr)) {
194                 goto done;
195         }
196
197         status = map_SHARE_INFO_buffer_to_srvsvc_share_info(ctx,
198                                                             r->in.buffer,
199                                                             r->in.level,
200                                                             &info);
201         if (!NT_STATUS_IS_OK(status)) {
202                 werr = ntstatus_to_werror(status);
203                 goto done;
204         }
205
206         status = rpccli_srvsvc_NetShareAdd(pipe_cli, ctx,
207                                            r->in.server_name,
208                                            r->in.level,
209                                            &info,
210                                            r->out.parm_err,
211                                            &werr);
212         if (!W_ERROR_IS_OK(werr)) {
213                 goto done;
214         }
215
216  done:
217         if (!cli) {
218                 return werr;
219         }
220
221         return werr;
222 }
223
224 /****************************************************************
225 ****************************************************************/
226
227 WERROR NetShareAdd_l(struct libnetapi_ctx *ctx,
228                      struct NetShareAdd *r)
229 {
230         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareAdd);
231 }
232
233 /****************************************************************
234 ****************************************************************/
235
236 WERROR NetShareDel_r(struct libnetapi_ctx *ctx,
237                      struct NetShareDel *r)
238 {
239         WERROR werr;
240         NTSTATUS status;
241         struct cli_state *cli = NULL;
242         struct rpc_pipe_client *pipe_cli = NULL;
243
244         if (!r->in.net_name) {
245                 return WERR_INVALID_PARAM;
246         }
247
248         werr = libnetapi_open_pipe(ctx, r->in.server_name,
249                                    &ndr_table_srvsvc.syntax_id,
250                                    &cli,
251                                    &pipe_cli);
252         if (!W_ERROR_IS_OK(werr)) {
253                 goto done;
254         }
255
256         status = rpccli_srvsvc_NetShareDel(pipe_cli, ctx,
257                                            r->in.server_name,
258                                            r->in.net_name,
259                                            r->in.reserved,
260                                            &werr);
261         if (!W_ERROR_IS_OK(werr)) {
262                 goto done;
263         }
264
265  done:
266         if (!cli) {
267                 return werr;
268         }
269
270         return werr;
271 }
272
273 /****************************************************************
274 ****************************************************************/
275
276 WERROR NetShareDel_l(struct libnetapi_ctx *ctx,
277                      struct NetShareDel *r)
278 {
279         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareDel);
280 }
281
282 /****************************************************************
283 ****************************************************************/
284
285 WERROR NetShareEnum_r(struct libnetapi_ctx *ctx,
286                       struct NetShareEnum *r)
287 {
288         return WERR_NOT_SUPPORTED;
289 }
290
291 /****************************************************************
292 ****************************************************************/
293
294 WERROR NetShareEnum_l(struct libnetapi_ctx *ctx,
295                       struct NetShareEnum *r)
296 {
297         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareEnum);
298 }
299
300 /****************************************************************
301 ****************************************************************/
302
303 WERROR NetShareGetInfo_r(struct libnetapi_ctx *ctx,
304                          struct NetShareGetInfo *r)
305 {
306         WERROR werr;
307         NTSTATUS status;
308         struct cli_state *cli = NULL;
309         struct rpc_pipe_client *pipe_cli = NULL;
310         union srvsvc_NetShareInfo info;
311         uint32_t num_entries = 0;
312
313         if (!r->in.net_name) {
314                 return WERR_INVALID_PARAM;
315         }
316
317         switch (r->in.level) {
318                 case 0:
319                 case 1:
320                 case 2:
321                 case 501:
322                 case 1005:
323                         break;
324                 case 502:
325                 case 503:
326                         return WERR_NOT_SUPPORTED;
327                 default:
328                         return WERR_UNKNOWN_LEVEL;
329         }
330
331         werr = libnetapi_open_pipe(ctx, r->in.server_name,
332                                    &ndr_table_srvsvc.syntax_id,
333                                    &cli,
334                                    &pipe_cli);
335         if (!W_ERROR_IS_OK(werr)) {
336                 goto done;
337         }
338
339         status = rpccli_srvsvc_NetShareGetInfo(pipe_cli, ctx,
340                                                r->in.server_name,
341                                                r->in.net_name,
342                                                r->in.level,
343                                                &info,
344                                                &werr);
345
346         if (!W_ERROR_IS_OK(werr)) {
347                 goto done;
348         }
349
350         status = map_srvsvc_share_info_to_SHARE_INFO_buffer(ctx,
351                                                             r->in.level,
352                                                             &info,
353                                                             r->out.buffer,
354                                                             &num_entries);
355         if (!NT_STATUS_IS_OK(status)) {
356                 werr = ntstatus_to_werror(status);
357         }
358
359  done:
360         if (!cli) {
361                 return werr;
362         }
363
364         return werr;
365 }
366
367 /****************************************************************
368 ****************************************************************/
369
370 WERROR NetShareGetInfo_l(struct libnetapi_ctx *ctx,
371                          struct NetShareGetInfo *r)
372 {
373         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareGetInfo);
374 }