docs: explain where the defaults for these printing parameters comes from
[Samba.git] / source3 / lib / netapi / share.c
blob37b23221be3d6c29652627a50d3e48cc3c72cd6f
1 /*
2 * Unix SMB/CIFS implementation.
3 * NetApi Share Support
4 * Copyright (C) Guenther Deschner 2008
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.
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.
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/>.
20 #include "includes.h"
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 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
27 #include "librpc/gen_ndr/ndr_security.h"
29 /****************************************************************
30 ****************************************************************/
32 static NTSTATUS map_srvsvc_share_info_to_SHARE_INFO_buffer(TALLOC_CTX *mem_ctx,
33 uint32_t level,
34 union srvsvc_NetShareInfo *info,
35 uint8_t **buffer,
36 uint32_t *num_shares)
38 struct SHARE_INFO_0 i0;
39 struct SHARE_INFO_1 i1;
40 struct SHARE_INFO_2 i2;
41 struct SHARE_INFO_501 i501;
42 struct SHARE_INFO_1005 i1005;
44 struct srvsvc_NetShareInfo0 *s0;
45 struct srvsvc_NetShareInfo1 *s1;
46 struct srvsvc_NetShareInfo2 *s2;
47 struct srvsvc_NetShareInfo501 *s501;
48 struct srvsvc_NetShareInfo1005 *s1005;
50 if (!buffer) {
51 return NT_STATUS_INVALID_PARAMETER;
54 switch (level) {
55 case 0:
56 s0 = info->info0;
58 i0.shi0_netname = talloc_strdup(mem_ctx, s0->name);
60 ADD_TO_ARRAY(mem_ctx, struct SHARE_INFO_0, i0,
61 (struct SHARE_INFO_0 **)buffer,
62 num_shares);
63 break;
65 case 1:
66 s1 = info->info1;
68 i1.shi1_netname = talloc_strdup(mem_ctx, s1->name);
69 i1.shi1_type = s1->type;
70 i1.shi1_remark = talloc_strdup(mem_ctx, s1->comment);
72 ADD_TO_ARRAY(mem_ctx, struct SHARE_INFO_1, i1,
73 (struct SHARE_INFO_1 **)buffer,
74 num_shares);
75 break;
77 case 2:
78 s2 = info->info2;
80 i2.shi2_netname = talloc_strdup(mem_ctx, s2->name);
81 i2.shi2_type = s2->type;
82 i2.shi2_remark = talloc_strdup(mem_ctx, s2->comment);
83 i2.shi2_permissions = s2->permissions;
84 i2.shi2_max_uses = s2->max_users;
85 i2.shi2_current_uses = s2->current_users;
86 i2.shi2_path = talloc_strdup(mem_ctx, s2->path);
87 i2.shi2_passwd = talloc_strdup(mem_ctx, s2->password);
89 ADD_TO_ARRAY(mem_ctx, struct SHARE_INFO_2, i2,
90 (struct SHARE_INFO_2 **)buffer,
91 num_shares);
92 break;
94 case 501:
95 s501 = info->info501;
97 i501.shi501_netname = talloc_strdup(mem_ctx, s501->name);
98 i501.shi501_type = s501->type;
99 i501.shi501_remark = talloc_strdup(mem_ctx, s501->comment);
100 i501.shi501_flags = s501->csc_policy;
102 ADD_TO_ARRAY(mem_ctx, struct SHARE_INFO_501, i501,
103 (struct SHARE_INFO_501 **)buffer,
104 num_shares);
105 break;
107 case 1005:
108 s1005 = info->info1005;
110 i1005.shi1005_flags = s1005->dfs_flags;
112 ADD_TO_ARRAY(mem_ctx, struct SHARE_INFO_1005, i1005,
113 (struct SHARE_INFO_1005 **)buffer,
114 num_shares);
115 break;
117 default:
118 return NT_STATUS_INVALID_PARAMETER;
121 return NT_STATUS_OK;
124 /****************************************************************
125 ****************************************************************/
127 static NTSTATUS map_SHARE_INFO_buffer_to_srvsvc_share_info(TALLOC_CTX *mem_ctx,
128 uint8_t *buffer,
129 uint32_t level,
130 union srvsvc_NetShareInfo *info)
132 struct SHARE_INFO_2 *i2 = NULL;
133 struct SHARE_INFO_502 *i502 = NULL;
134 struct SHARE_INFO_1004 *i1004 = NULL;
135 struct srvsvc_NetShareInfo2 *s2 = NULL;
136 struct srvsvc_NetShareInfo502 *s502 = NULL;
137 struct srvsvc_NetShareInfo1004 *s1004 = NULL;
139 if (!buffer) {
140 return NT_STATUS_INVALID_PARAMETER;
143 switch (level) {
144 case 2:
145 i2 = (struct SHARE_INFO_2 *)buffer;
147 s2 = talloc(mem_ctx, struct srvsvc_NetShareInfo2);
148 NT_STATUS_HAVE_NO_MEMORY(s2);
150 s2->name = i2->shi2_netname;
151 s2->type = i2->shi2_type;
152 s2->comment = i2->shi2_remark;
153 s2->permissions = i2->shi2_permissions;
154 s2->max_users = i2->shi2_max_uses;
155 s2->current_users = i2->shi2_current_uses;
156 s2->path = i2->shi2_path;
157 s2->password = i2->shi2_passwd;
159 info->info2 = s2;
161 break;
163 case 502:
164 i502 = (struct SHARE_INFO_502 *)buffer;
166 s502 = talloc(mem_ctx, struct srvsvc_NetShareInfo502);
167 NT_STATUS_HAVE_NO_MEMORY(s502);
169 s502->name = i502->shi502_netname;
170 s502->type = i502->shi502_type;
171 s502->comment = i502->shi502_remark;
172 s502->permissions = i502->shi502_permissions;
173 s502->max_users = i502->shi502_max_uses;
174 s502->current_users = i502->shi502_current_uses;
175 s502->path = i502->shi502_path;
176 s502->password = i502->shi502_passwd;
177 s502->sd_buf.sd_size =
178 ndr_size_security_descriptor(i502->shi502_security_descriptor, 0);
179 s502->sd_buf.sd = i502->shi502_security_descriptor;
181 info->info502 = s502;
183 break;
185 case 1004:
186 i1004 = (struct SHARE_INFO_1004 *)buffer;
188 s1004 = talloc(mem_ctx, struct srvsvc_NetShareInfo1004);
189 NT_STATUS_HAVE_NO_MEMORY(s1004);
191 s1004->comment = i1004->shi1004_remark;
193 info->info1004 = s1004;
195 break;
196 default:
197 return NT_STATUS_INVALID_PARAMETER;
200 return NT_STATUS_OK;
203 /****************************************************************
204 ****************************************************************/
206 WERROR NetShareAdd_r(struct libnetapi_ctx *ctx,
207 struct NetShareAdd *r)
209 WERROR werr;
210 NTSTATUS status;
211 union srvsvc_NetShareInfo info;
212 struct dcerpc_binding_handle *b;
214 if (!r->in.buffer) {
215 return WERR_INVALID_PARAM;
218 switch (r->in.level) {
219 case 2:
220 case 502:
221 break;
222 case 503:
223 return WERR_NOT_SUPPORTED;
224 default:
225 return WERR_UNKNOWN_LEVEL;
228 werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
229 &ndr_table_srvsvc,
230 &b);
231 if (!W_ERROR_IS_OK(werr)) {
232 goto done;
235 status = map_SHARE_INFO_buffer_to_srvsvc_share_info(ctx,
236 r->in.buffer,
237 r->in.level,
238 &info);
239 if (!NT_STATUS_IS_OK(status)) {
240 werr = ntstatus_to_werror(status);
241 goto done;
244 status = dcerpc_srvsvc_NetShareAdd(b, talloc_tos(),
245 r->in.server_name,
246 r->in.level,
247 &info,
248 r->out.parm_err,
249 &werr);
250 if (!NT_STATUS_IS_OK(status)) {
251 werr = ntstatus_to_werror(status);
252 goto done;
255 if (!W_ERROR_IS_OK(werr)) {
256 goto done;
259 done:
260 return werr;
263 /****************************************************************
264 ****************************************************************/
266 WERROR NetShareAdd_l(struct libnetapi_ctx *ctx,
267 struct NetShareAdd *r)
269 LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareAdd);
272 /****************************************************************
273 ****************************************************************/
275 WERROR NetShareDel_r(struct libnetapi_ctx *ctx,
276 struct NetShareDel *r)
278 WERROR werr;
279 NTSTATUS status;
280 struct dcerpc_binding_handle *b;
282 if (!r->in.net_name) {
283 return WERR_INVALID_PARAM;
286 werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
287 &ndr_table_srvsvc,
288 &b);
289 if (!W_ERROR_IS_OK(werr)) {
290 goto done;
293 status = dcerpc_srvsvc_NetShareDel(b, talloc_tos(),
294 r->in.server_name,
295 r->in.net_name,
296 r->in.reserved,
297 &werr);
298 if (!NT_STATUS_IS_OK(status)) {
299 werr = ntstatus_to_werror(status);
300 goto done;
303 done:
304 return werr;
307 /****************************************************************
308 ****************************************************************/
310 WERROR NetShareDel_l(struct libnetapi_ctx *ctx,
311 struct NetShareDel *r)
313 LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareDel);
316 /****************************************************************
317 ****************************************************************/
319 WERROR NetShareEnum_r(struct libnetapi_ctx *ctx,
320 struct NetShareEnum *r)
322 WERROR werr;
323 NTSTATUS status;
324 struct srvsvc_NetShareInfoCtr info_ctr;
325 struct srvsvc_NetShareCtr0 ctr0;
326 struct srvsvc_NetShareCtr1 ctr1;
327 struct srvsvc_NetShareCtr2 ctr2;
328 uint32_t i;
329 struct dcerpc_binding_handle *b;
331 if (!r->out.buffer) {
332 return WERR_INVALID_PARAM;
335 switch (r->in.level) {
336 case 0:
337 case 1:
338 case 2:
339 break;
340 case 502:
341 case 503:
342 return WERR_NOT_SUPPORTED;
343 default:
344 return WERR_UNKNOWN_LEVEL;
347 ZERO_STRUCT(info_ctr);
349 werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
350 &ndr_table_srvsvc,
351 &b);
352 if (!W_ERROR_IS_OK(werr)) {
353 goto done;
356 info_ctr.level = r->in.level;
357 switch (r->in.level) {
358 case 0:
359 ZERO_STRUCT(ctr0);
360 info_ctr.ctr.ctr0 = &ctr0;
361 break;
362 case 1:
363 ZERO_STRUCT(ctr1);
364 info_ctr.ctr.ctr1 = &ctr1;
365 break;
366 case 2:
367 ZERO_STRUCT(ctr2);
368 info_ctr.ctr.ctr2 = &ctr2;
369 break;
372 status = dcerpc_srvsvc_NetShareEnumAll(b, talloc_tos(),
373 r->in.server_name,
374 &info_ctr,
375 r->in.prefmaxlen,
376 r->out.total_entries,
377 r->out.resume_handle,
378 &werr);
379 if (!NT_STATUS_IS_OK(status)) {
380 werr = ntstatus_to_werror(status);
381 goto done;
384 if (!W_ERROR_IS_OK(werr) && !W_ERROR_EQUAL(werr, WERR_MORE_DATA)) {
385 goto done;
388 for (i=0; i < info_ctr.ctr.ctr1->count; i++) {
389 union srvsvc_NetShareInfo _i;
390 switch (r->in.level) {
391 case 0:
392 _i.info0 = &info_ctr.ctr.ctr0->array[i];
393 break;
394 case 1:
395 _i.info1 = &info_ctr.ctr.ctr1->array[i];
396 break;
397 case 2:
398 _i.info2 = &info_ctr.ctr.ctr2->array[i];
399 break;
402 status = map_srvsvc_share_info_to_SHARE_INFO_buffer(ctx,
403 r->in.level,
404 &_i,
405 r->out.buffer,
406 r->out.entries_read);
407 if (!NT_STATUS_IS_OK(status)) {
408 werr = ntstatus_to_werror(status);
409 goto done;
413 done:
414 return werr;
417 /****************************************************************
418 ****************************************************************/
420 WERROR NetShareEnum_l(struct libnetapi_ctx *ctx,
421 struct NetShareEnum *r)
423 LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareEnum);
426 /****************************************************************
427 ****************************************************************/
429 WERROR NetShareGetInfo_r(struct libnetapi_ctx *ctx,
430 struct NetShareGetInfo *r)
432 WERROR werr;
433 NTSTATUS status;
434 union srvsvc_NetShareInfo info;
435 uint32_t num_entries = 0;
436 struct dcerpc_binding_handle *b;
438 if (!r->in.net_name || !r->out.buffer) {
439 return WERR_INVALID_PARAM;
442 switch (r->in.level) {
443 case 0:
444 case 1:
445 case 2:
446 case 501:
447 case 1005:
448 break;
449 case 502:
450 case 503:
451 return WERR_NOT_SUPPORTED;
452 default:
453 return WERR_UNKNOWN_LEVEL;
456 werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
457 &ndr_table_srvsvc,
458 &b);
459 if (!W_ERROR_IS_OK(werr)) {
460 goto done;
463 status = dcerpc_srvsvc_NetShareGetInfo(b, talloc_tos(),
464 r->in.server_name,
465 r->in.net_name,
466 r->in.level,
467 &info,
468 &werr);
469 if (!NT_STATUS_IS_OK(status)) {
470 werr = ntstatus_to_werror(status);
471 goto done;
474 if (!W_ERROR_IS_OK(werr)) {
475 goto done;
478 status = map_srvsvc_share_info_to_SHARE_INFO_buffer(ctx,
479 r->in.level,
480 &info,
481 r->out.buffer,
482 &num_entries);
483 if (!NT_STATUS_IS_OK(status)) {
484 werr = ntstatus_to_werror(status);
487 done:
488 return werr;
491 /****************************************************************
492 ****************************************************************/
494 WERROR NetShareGetInfo_l(struct libnetapi_ctx *ctx,
495 struct NetShareGetInfo *r)
497 LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareGetInfo);
500 /****************************************************************
501 ****************************************************************/
503 WERROR NetShareSetInfo_r(struct libnetapi_ctx *ctx,
504 struct NetShareSetInfo *r)
506 WERROR werr;
507 NTSTATUS status;
508 union srvsvc_NetShareInfo info;
509 struct dcerpc_binding_handle *b;
511 if (!r->in.buffer) {
512 return WERR_INVALID_PARAM;
515 switch (r->in.level) {
516 case 2:
517 case 1004:
518 break;
519 case 1:
520 case 502:
521 case 503:
522 case 1005:
523 case 1006:
524 case 1501:
525 return WERR_NOT_SUPPORTED;
526 default:
527 return WERR_UNKNOWN_LEVEL;
530 werr = libnetapi_get_binding_handle(ctx, r->in.server_name,
531 &ndr_table_srvsvc,
532 &b);
533 if (!W_ERROR_IS_OK(werr)) {
534 goto done;
537 status = map_SHARE_INFO_buffer_to_srvsvc_share_info(ctx,
538 r->in.buffer,
539 r->in.level,
540 &info);
541 if (!NT_STATUS_IS_OK(status)) {
542 werr = ntstatus_to_werror(status);
543 goto done;
546 status = dcerpc_srvsvc_NetShareSetInfo(b, talloc_tos(),
547 r->in.server_name,
548 r->in.net_name,
549 r->in.level,
550 &info,
551 r->out.parm_err,
552 &werr);
553 if (!NT_STATUS_IS_OK(status)) {
554 werr = ntstatus_to_werror(status);
555 goto done;
558 if (!W_ERROR_IS_OK(werr)) {
559 goto done;
562 done:
563 return werr;
566 /****************************************************************
567 ****************************************************************/
569 WERROR NetShareSetInfo_l(struct libnetapi_ctx *ctx,
570 struct NetShareSetInfo *r)
572 LIBNETAPI_REDIRECT_TO_LOCALHOST(ctx, r, NetShareSetInfo);