libnetapi_open_pipe does not need to return cli_state
[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 SHARE_INFO_1004 *i1004 = NULL;
132         struct srvsvc_NetShareInfo2 *s2 = NULL;
133         struct srvsvc_NetShareInfo1004 *s1004 = NULL;
134
135         if (!buffer) {
136                 return NT_STATUS_INVALID_PARAMETER;
137         }
138
139         switch (level) {
140                 case 2:
141                         i2 = (struct SHARE_INFO_2 *)buffer;
142
143                         s2 = TALLOC_P(mem_ctx, struct srvsvc_NetShareInfo2);
144                         NT_STATUS_HAVE_NO_MEMORY(s2);
145
146                         s2->name                = i2->shi2_netname;
147                         s2->type                = i2->shi2_type;
148                         s2->comment             = i2->shi2_remark;
149                         s2->permissions         = i2->shi2_permissions;
150                         s2->max_users           = i2->shi2_max_uses;
151                         s2->current_users       = i2->shi2_current_uses;
152                         s2->path                = i2->shi2_path;
153                         s2->password            = i2->shi2_passwd;
154
155                         info->info2 = s2;
156
157                         break;
158                 case 1004:
159                         i1004 = (struct SHARE_INFO_1004 *)buffer;
160
161                         s1004 = TALLOC_P(mem_ctx, struct srvsvc_NetShareInfo1004);
162                         NT_STATUS_HAVE_NO_MEMORY(s1004);
163
164                         s1004->comment          = i1004->shi1004_remark;
165
166                         info->info1004 = s1004;
167
168                         break;
169                 default:
170                         return NT_STATUS_INVALID_PARAMETER;
171         }
172
173         return NT_STATUS_OK;
174 }
175
176 /****************************************************************
177 ****************************************************************/
178
179 WERROR NetShareAdd_r(struct libnetapi_ctx *ctx,
180                      struct NetShareAdd *r)
181 {
182         WERROR werr;
183         NTSTATUS status;
184         struct rpc_pipe_client *pipe_cli = NULL;
185         union srvsvc_NetShareInfo info;
186
187         if (!r->in.buffer) {
188                 return WERR_INVALID_PARAM;
189         }
190
191         switch (r->in.level) {
192                 case 2:
193                         break;
194                 case 502:
195                 case 503:
196                         return WERR_NOT_SUPPORTED;
197                 default:
198                         return WERR_UNKNOWN_LEVEL;
199         }
200
201         werr = libnetapi_open_pipe(ctx, r->in.server_name,
202                                    &ndr_table_srvsvc.syntax_id,
203                                    &pipe_cli);
204         if (!W_ERROR_IS_OK(werr)) {
205                 goto done;
206         }
207
208         status = map_SHARE_INFO_buffer_to_srvsvc_share_info(ctx,
209                                                             r->in.buffer,
210                                                             r->in.level,
211                                                             &info);
212         if (!NT_STATUS_IS_OK(status)) {
213                 werr = ntstatus_to_werror(status);
214                 goto done;
215         }
216
217         status = rpccli_srvsvc_NetShareAdd(pipe_cli, ctx,
218                                            r->in.server_name,
219                                            r->in.level,
220                                            &info,
221                                            r->out.parm_err,
222                                            &werr);
223         if (!W_ERROR_IS_OK(werr)) {
224                 goto done;
225         }
226
227  done:
228         return werr;
229 }
230
231 /****************************************************************
232 ****************************************************************/
233
234 WERROR NetShareAdd_l(struct libnetapi_ctx *ctx,
235                      struct NetShareAdd *r)
236 {
237         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareAdd);
238 }
239
240 /****************************************************************
241 ****************************************************************/
242
243 WERROR NetShareDel_r(struct libnetapi_ctx *ctx,
244                      struct NetShareDel *r)
245 {
246         WERROR werr;
247         NTSTATUS status;
248         struct rpc_pipe_client *pipe_cli = NULL;
249
250         if (!r->in.net_name) {
251                 return WERR_INVALID_PARAM;
252         }
253
254         werr = libnetapi_open_pipe(ctx, r->in.server_name,
255                                    &ndr_table_srvsvc.syntax_id,
256                                    &pipe_cli);
257         if (!W_ERROR_IS_OK(werr)) {
258                 goto done;
259         }
260
261         status = rpccli_srvsvc_NetShareDel(pipe_cli, ctx,
262                                            r->in.server_name,
263                                            r->in.net_name,
264                                            r->in.reserved,
265                                            &werr);
266         if (!NT_STATUS_IS_OK(status)) {
267                 werr = ntstatus_to_werror(status);
268                 goto done;
269         }
270
271  done:
272         return werr;
273 }
274
275 /****************************************************************
276 ****************************************************************/
277
278 WERROR NetShareDel_l(struct libnetapi_ctx *ctx,
279                      struct NetShareDel *r)
280 {
281         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareDel);
282 }
283
284 /****************************************************************
285 ****************************************************************/
286
287 WERROR NetShareEnum_r(struct libnetapi_ctx *ctx,
288                       struct NetShareEnum *r)
289 {
290         WERROR werr;
291         NTSTATUS status;
292         struct rpc_pipe_client *pipe_cli = NULL;
293         struct srvsvc_NetShareInfoCtr info_ctr;
294         struct srvsvc_NetShareCtr0 ctr0;
295         struct srvsvc_NetShareCtr1 ctr1;
296         struct srvsvc_NetShareCtr2 ctr2;
297         uint32_t i;
298
299         if (!r->out.buffer) {
300                 return WERR_INVALID_PARAM;
301         }
302
303         switch (r->in.level) {
304                 case 0:
305                 case 1:
306                 case 2:
307                         break;
308                 case 502:
309                 case 503:
310                         return WERR_NOT_SUPPORTED;
311                 default:
312                         return WERR_UNKNOWN_LEVEL;
313         }
314
315         ZERO_STRUCT(info_ctr);
316
317         werr = libnetapi_open_pipe(ctx, r->in.server_name,
318                                    &ndr_table_srvsvc.syntax_id,
319                                    &pipe_cli);
320         if (!W_ERROR_IS_OK(werr)) {
321                 goto done;
322         }
323
324         info_ctr.level = r->in.level;
325         switch (r->in.level) {
326                 case 0:
327                         ZERO_STRUCT(ctr0);
328                         info_ctr.ctr.ctr0 = &ctr0;
329                         break;
330                 case 1:
331                         ZERO_STRUCT(ctr1);
332                         info_ctr.ctr.ctr1 = &ctr1;
333                         break;
334                 case 2:
335                         ZERO_STRUCT(ctr2);
336                         info_ctr.ctr.ctr2 = &ctr2;
337                         break;
338         }
339
340         status = rpccli_srvsvc_NetShareEnumAll(pipe_cli, ctx,
341                                                r->in.server_name,
342                                                &info_ctr,
343                                                r->in.prefmaxlen,
344                                                r->out.total_entries,
345                                                r->out.resume_handle,
346                                                &werr);
347         if (NT_STATUS_IS_ERR(status)) {
348                 goto done;
349         }
350
351         for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
352                 union srvsvc_NetShareInfo _i;
353                 switch (r->in.level) {
354                         case 0:
355                                 _i.info0 = &info_ctr.ctr.ctr0->array[i];
356                                 break;
357                         case 1:
358                                 _i.info1 = &info_ctr.ctr.ctr1->array[i];
359                                 break;
360                         case 2:
361                                 _i.info2 = &info_ctr.ctr.ctr2->array[i];
362                                 break;
363                 }
364
365                 status = map_srvsvc_share_info_to_SHARE_INFO_buffer(ctx,
366                                                                     r->in.level,
367                                                                     &_i,
368                                                                     r->out.buffer,
369                                                                     r->out.entries_read);
370                 if (!NT_STATUS_IS_OK(status)) {
371                         werr = ntstatus_to_werror(status);
372                 }
373         }
374
375  done:
376         return werr;
377 }
378
379 /****************************************************************
380 ****************************************************************/
381
382 WERROR NetShareEnum_l(struct libnetapi_ctx *ctx,
383                       struct NetShareEnum *r)
384 {
385         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareEnum);
386 }
387
388 /****************************************************************
389 ****************************************************************/
390
391 WERROR NetShareGetInfo_r(struct libnetapi_ctx *ctx,
392                          struct NetShareGetInfo *r)
393 {
394         WERROR werr;
395         NTSTATUS status;
396         struct rpc_pipe_client *pipe_cli = NULL;
397         union srvsvc_NetShareInfo info;
398         uint32_t num_entries = 0;
399
400         if (!r->in.net_name || !r->out.buffer) {
401                 return WERR_INVALID_PARAM;
402         }
403
404         switch (r->in.level) {
405                 case 0:
406                 case 1:
407                 case 2:
408                 case 501:
409                 case 1005:
410                         break;
411                 case 502:
412                 case 503:
413                         return WERR_NOT_SUPPORTED;
414                 default:
415                         return WERR_UNKNOWN_LEVEL;
416         }
417
418         werr = libnetapi_open_pipe(ctx, r->in.server_name,
419                                    &ndr_table_srvsvc.syntax_id,
420                                    &pipe_cli);
421         if (!W_ERROR_IS_OK(werr)) {
422                 goto done;
423         }
424
425         status = rpccli_srvsvc_NetShareGetInfo(pipe_cli, ctx,
426                                                r->in.server_name,
427                                                r->in.net_name,
428                                                r->in.level,
429                                                &info,
430                                                &werr);
431
432         if (!W_ERROR_IS_OK(werr)) {
433                 goto done;
434         }
435
436         status = map_srvsvc_share_info_to_SHARE_INFO_buffer(ctx,
437                                                             r->in.level,
438                                                             &info,
439                                                             r->out.buffer,
440                                                             &num_entries);
441         if (!NT_STATUS_IS_OK(status)) {
442                 werr = ntstatus_to_werror(status);
443         }
444
445  done:
446         return werr;
447 }
448
449 /****************************************************************
450 ****************************************************************/
451
452 WERROR NetShareGetInfo_l(struct libnetapi_ctx *ctx,
453                          struct NetShareGetInfo *r)
454 {
455         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareGetInfo);
456 }
457
458 /****************************************************************
459 ****************************************************************/
460
461 WERROR NetShareSetInfo_r(struct libnetapi_ctx *ctx,
462                          struct NetShareSetInfo *r)
463 {
464         WERROR werr;
465         NTSTATUS status;
466         struct rpc_pipe_client *pipe_cli = NULL;
467         union srvsvc_NetShareInfo info;
468
469         if (!r->in.buffer) {
470                 return WERR_INVALID_PARAM;
471         }
472
473         switch (r->in.level) {
474                 case 2:
475                 case 1004:
476                         break;
477                 case 1:
478                 case 502:
479                 case 503:
480                 case 1005:
481                 case 1006:
482                 case 1501:
483                         return WERR_NOT_SUPPORTED;
484                 default:
485                         return WERR_UNKNOWN_LEVEL;
486         }
487
488         werr = libnetapi_open_pipe(ctx, r->in.server_name,
489                                    &ndr_table_srvsvc.syntax_id,
490                                    &pipe_cli);
491         if (!W_ERROR_IS_OK(werr)) {
492                 goto done;
493         }
494
495         status = map_SHARE_INFO_buffer_to_srvsvc_share_info(ctx,
496                                                             r->in.buffer,
497                                                             r->in.level,
498                                                             &info);
499         if (!NT_STATUS_IS_OK(status)) {
500                 werr = ntstatus_to_werror(status);
501                 goto done;
502         }
503
504         status = rpccli_srvsvc_NetShareSetInfo(pipe_cli, ctx,
505                                                r->in.server_name,
506                                                r->in.net_name,
507                                                r->in.level,
508                                                &info,
509                                                r->out.parm_err,
510                                                &werr);
511         if (!W_ERROR_IS_OK(werr)) {
512                 goto done;
513         }
514
515  done:
516         return werr;
517 }
518
519 /****************************************************************
520 ****************************************************************/
521
522 WERROR NetShareSetInfo_l(struct libnetapi_ctx *ctx,
523                          struct NetShareSetInfo *r)
524 {
525         LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareSetInfo);
526 }