docs: Add very basic samba manpage.
[Samba/gebeck_regimport.git] / source3 / utils / net_rpc_conf.c
blob8c5717c519dec516a87f81115e8924655bd00cd1
1 /*
2 * Samba Unix/Linux SMB client library
3 * Distributed SMB/CIFS Server Management Utility
4 * Local configuration interface
5 * Copyright (C) Vicentiu Ciorbaru 2011
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * This is an interface to Samba's configuration.
24 * This tool supports local as well as remote interaction via rpc
25 * with the configuration stored in the registry.
29 #include "includes.h"
30 #include "utils/net.h"
31 #include "rpc_client/cli_pipe.h"
32 #include "../librpc/gen_ndr/ndr_samr_c.h"
33 #include "rpc_client/init_samr.h"
34 #include "../librpc/gen_ndr/ndr_winreg_c.h"
35 #include "../libcli/registry/util_reg.h"
36 #include "rpc_client/cli_winreg.h"
37 #include "lib/smbconf/smbconf.h"
38 #include "lib/smbconf/smbconf_init.h"
39 #include "lib/smbconf/smbconf_reg.h"
40 #include "lib/param/loadparm.h"
44 /* internal functions */
45 /**********************************************************
47 * usage functions
49 **********************************************************/
50 const char confpath[100] = "Software\\Samba\\smbconf";
52 static int rpc_conf_list_usage(struct net_context *c, int argc,
53 const char **argv)
55 d_printf("%s net rpc conf list\n", _("Usage:"));
56 return -1;
59 static int rpc_conf_listshares_usage(struct net_context *c, int argc,
60 const char **argv)
62 d_printf("%s net rpc conf listshares\n", _("Usage:"));
63 return -1;
66 static int rpc_conf_delshare_usage(struct net_context *c, int argc,
67 const char **argv)
69 d_printf("%s\n%s",
70 _("Usage:"),
71 _("net rpc conf delshare <sharename>\n"));
72 return -1;
75 static int rpc_conf_addshare_usage(struct net_context *c, int argc,
76 const char **argv)
78 d_printf("%s\n%s",
79 _("Usage:"),
80 _(" net rpc conf addshare <sharename> <path> "
81 "[writeable={y|N} [guest_ok={y|N} [<comment>]]]\n"
82 "\t<sharename> the new share name.\n"
83 "\t<path> the path on the filesystem to export.\n"
84 "\twriteable={y|N} set \"writeable to \"yes\" or "
85 "\"no\" (default) on this share.\n"
86 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
87 "\"no\" (default) on this share.\n"
88 "\t<comment> optional comment for the new share.\n"));
89 return -1;
93 static int rpc_conf_import_usage(struct net_context *c, int argc,
94 const char**argv)
96 d_printf("%s\n%s",
97 _("Usage:"),
98 _(" net rpc conf import [--test|-T] <filename> "
99 "[<servicename>]\n"
100 "\t[--test|-T] testmode - do not act, just print "
101 "what would be done\n"
102 "\t<servicename> only import service <servicename>, "
103 "ignore the rest\n"));
104 return -1;
107 static int rpc_conf_showshare_usage(struct net_context *c, int argc,
108 const char **argv)
110 d_printf("%s\n%s",
111 _("Usage:"),
112 _("net rpc conf showshare <sharename>\n"));
113 return -1;
116 static int rpc_conf_drop_usage(struct net_context *c, int argc,
117 const char **argv)
119 d_printf("%s\nnet rpc conf drop\n", _("Usage:"));
120 return -1;
123 static int rpc_conf_getparm_usage(struct net_context *c, int argc,
124 const char **argv)
126 d_printf("%s\nnet rpc conf getparm <sharename> <parameter>\n",
127 _("Usage:"));
128 return -1;
131 static int rpc_conf_setparm_usage(struct net_context *c, int argc,
132 const char **argv)
134 d_printf("%s\n%s",
135 _("Usage:"),
136 _(" net rpc conf setparm <section> <param> <value>\n"));
137 return -1;
140 static int rpc_conf_delparm_usage(struct net_context *c, int argc,
141 const char **argv)
143 d_printf("%s\nnet rpc conf delparm <sharename> <parameter>\n",
144 _("Usage:"));
145 return -1;
148 static int rpc_conf_getincludes_usage(struct net_context *c, int argc,
149 const char **argv)
151 d_printf("%s\nnet rpc conf getincludes <sharename>\n",
152 _("Usage:"));
153 return -1;
156 static int rpc_conf_setincludes_usage(struct net_context *c, int argc,
157 const char **argv)
159 d_printf("%s\nnet rpc conf setincludes <sharename> [<filename>]*\n",
160 _("Usage:"));
161 return -1;
164 static int rpc_conf_delincludes_usage(struct net_context *c, int argc,
165 const char **argv)
167 d_printf("%s\nnet rpc conf delincludes <sharename>\n",
168 _("Usage:"));
169 return -1;
172 /**********************************************************
174 * helper functions
176 **********************************************************/
178 static bool rpc_conf_reg_valname_forbidden(const char * valname)
180 const char *forbidden_valnames[] = {
181 "lock directory",
182 "lock dir",
183 "config backend",
184 "include",
185 "includes", /* this has a special meaning internally */
186 NULL
188 const char **forbidden = NULL;
190 for (forbidden = forbidden_valnames; *forbidden != NULL; forbidden++) {
191 if (strwicmp(valname, *forbidden) == 0) {
192 return true;
195 return false;
200 * The function deletes a registry value with the name 'value' from the share
201 * with the name 'share_name'. 'parent_hnd' is the handle for the smbconf key.
203 static NTSTATUS rpc_conf_del_value(TALLOC_CTX *mem_ctx,
204 struct dcerpc_binding_handle *b,
205 struct policy_handle *parent_hnd,
206 const char *share_name,
207 const char *value,
208 WERROR *werr)
211 TALLOC_CTX *frame = talloc_stackframe();
212 NTSTATUS status = NT_STATUS_OK;
213 WERROR result = WERR_OK;
214 WERROR _werr;
216 struct winreg_String keyname, valuename;
217 struct policy_handle child_hnd;
219 ZERO_STRUCT(child_hnd);
220 ZERO_STRUCT(keyname);
221 ZERO_STRUCT(valuename);
223 keyname.name = share_name;
224 valuename.name = value;
226 status = dcerpc_winreg_OpenKey(b, frame, parent_hnd, keyname, 0,
227 REG_KEY_WRITE, &child_hnd, &result);
229 if (!(NT_STATUS_IS_OK(status))) {
230 d_fprintf(stderr, _("Failed to open key '%s': %s\n"),
231 keyname.name, nt_errstr(status));
232 goto error;
235 if (!(W_ERROR_IS_OK(result))) {
236 d_fprintf(stderr, _("Failed to open key '%s': %s\n"),
237 keyname.name, win_errstr(result));
238 goto error;
241 status = dcerpc_winreg_DeleteValue(b,
242 frame,
243 &child_hnd,
244 valuename,
245 &result);
247 if (!(NT_STATUS_IS_OK(status))) {
248 d_fprintf(stderr, _("Failed to delete value %s\n"),
249 nt_errstr(status));
250 goto error;
253 if (!(W_ERROR_IS_OK(result))) {
254 if (W_ERROR_EQUAL(result, WERR_BADFILE)){
255 result = WERR_OK;
256 goto error;
259 d_fprintf(stderr, _("Failed to delete value %s\n"),
260 win_errstr(result));
261 goto error;
264 error:
265 *werr = result;
267 dcerpc_winreg_CloseKey(b, frame, &child_hnd, &_werr);
269 TALLOC_FREE(frame);
270 return status;;
275 * The function sets a share in the registry with the parameters
276 * held in the smbconf_service struct
278 static NTSTATUS rpc_conf_set_share(TALLOC_CTX *mem_ctx,
279 struct dcerpc_binding_handle *b,
280 struct policy_handle *parent_hnd,
281 struct smbconf_service *service,
282 WERROR *werr)
284 TALLOC_CTX *frame = talloc_stackframe();
286 NTSTATUS status = NT_STATUS_OK;
287 WERROR result = WERR_OK;
288 WERROR _werr;
289 enum winreg_CreateAction action;
290 uint32_t i, j;
292 const char **includes;
294 struct winreg_String wkey, wkeyclass;
295 struct policy_handle share_hnd;
297 ZERO_STRUCT(share_hnd);
298 ZERO_STRUCT(wkey);
299 ZERO_STRUCT(wkeyclass);
301 wkey.name = service->name;
302 wkeyclass.name = "";
303 action = REG_ACTION_NONE;
305 status = dcerpc_winreg_CreateKey(b,
306 frame,
307 parent_hnd,
308 wkey,
309 wkeyclass,
311 REG_KEY_ALL,
312 NULL,
313 &share_hnd,
314 &action,
315 &result);
317 if (!NT_STATUS_IS_OK(status)) {
318 d_printf("winreg_CreateKey: Could not create smbconf key\n");
319 goto error;
322 if (!W_ERROR_IS_OK(result)) {
323 d_printf("winreg_CreateKey: Could not create smbconf key\n");
324 goto error;
327 for (i = 0; i < service->num_params; i++) {
328 if (strequal(service->param_names[i], "include") == 0)
331 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
332 service->param_names[i],
333 service->param_values[i],
334 &result);
336 if (!(NT_STATUS_IS_OK(status))) {
337 d_fprintf(stderr,
338 "ERROR: Share: '%s'\n"
339 "Could not set parameter '%s'"
340 " with value %s\n %s\n",
341 service->name,
342 service->param_names[i],
343 service->param_values[i],
344 nt_errstr(status));
345 goto error;
348 if (!(W_ERROR_IS_OK(result))) {
349 d_fprintf(stderr,
350 "ERROR: Share: '%s'\n"
351 "Could not set parameter '%s'"
352 " with value %s\n %s\n",
353 service->name,
354 service->param_names[i],
355 service->param_values[i],
356 win_errstr(result));
357 goto error;
359 } else {
361 includes = talloc_zero_array(frame,
362 const char *,
363 service->num_params + 1);
364 if (includes == NULL) {
365 result = WERR_NOMEM;
366 d_fprintf(stderr, "ERROR: out of memory\n");
367 goto error;
370 for (j = i; j < service->num_params; j++) {
372 includes[j - i] = talloc_strdup(
373 frame,
374 service->param_values[j]);
376 if (includes[j-i] == NULL) {
377 result = WERR_NOMEM;
378 d_fprintf(stderr, "ERROR: out of memory\n");
379 goto error;
383 status = dcerpc_winreg_set_multi_sz(frame, b, &share_hnd,
384 "includes",
385 includes,
386 &result);
388 if (!(NT_STATUS_IS_OK(status))) {
389 d_fprintf(stderr, "ERROR: Share: '%s'\n"
390 "Could not set includes\n %s\n",
391 service->name,
392 nt_errstr(status));
393 goto error;
396 if (!(W_ERROR_IS_OK(result))) {
397 d_fprintf(stderr, "ERROR: Share: '%s'\n"
398 "Could not set includes\n %s\n",
399 service->name,
400 win_errstr(result));
401 goto error;
404 i = service->num_params;
408 error:
409 /* in case of error, should it delete the created key? */
410 if (!(W_ERROR_IS_OK(result))) {
411 status = werror_to_ntstatus(result);
415 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
417 TALLOC_FREE(frame);
418 return status;
423 * The function opens the registry database and retrieves
424 * as a smbconf_service struct the share with the name
425 * 'share_name'
427 static NTSTATUS rpc_conf_get_share(TALLOC_CTX *mem_ctx,
428 struct dcerpc_binding_handle *b,
429 struct policy_handle *parent_hnd,
430 const char *share_name,
431 struct smbconf_service *share,
432 WERROR *werr)
434 TALLOC_CTX *frame = talloc_stackframe();
436 NTSTATUS status = NT_STATUS_OK;
437 WERROR result = WERR_OK;
438 WERROR _werr;
439 struct policy_handle child_hnd;
440 int32_t includes_cnt, includes_idx = -1;
441 uint32_t num_vals, i, param_cnt = 0;
442 const char **val_names;
443 enum winreg_Type *types;
444 DATA_BLOB *data;
445 struct winreg_String key = { 0, };
446 const char **multi_s = NULL;
447 const char *s = NULL;
448 struct smbconf_service tmp_share;
450 ZERO_STRUCT(tmp_share);
452 key.name = share_name;
453 status = dcerpc_winreg_OpenKey(b, frame, parent_hnd, key, 0,
454 REG_KEY_READ, &child_hnd, &result);
456 if (!(NT_STATUS_IS_OK(status))) {
457 d_fprintf(stderr, _("Failed to open subkey: %s\n"),
458 nt_errstr(status));
459 goto error;
461 if (!(W_ERROR_IS_OK(result))) {
462 d_fprintf(stderr, _("Failed to open subkey: %s\n"),
463 win_errstr(result));
464 goto error;
466 /* get all the info from the share key */
467 status = dcerpc_winreg_enumvals(frame,
469 &child_hnd,
470 &num_vals,
471 &val_names,
472 &types,
473 &data,
474 &result);
476 if (!(NT_STATUS_IS_OK(status))) {
477 d_fprintf(stderr, _("Failed to enumerate values: %s\n"),
478 nt_errstr(status));
479 goto error;
481 if (!(W_ERROR_IS_OK(result))) {
482 d_fprintf(stderr, _("Failed to enumerate values: %s\n"),
483 win_errstr(result));
484 goto error;
486 /* check for includes */
487 for (i = 0; i < num_vals; i++) {
488 if (strcmp(val_names[i], "includes") == 0){
489 if (!pull_reg_multi_sz(frame,
490 &data[i],
491 &multi_s))
493 result = WERR_NOMEM;
494 d_fprintf(stderr,
495 _("Failed to enumerate values: %s\n"),
496 win_errstr(result));
497 goto error;
499 includes_idx = i;
502 /* count the number of includes */
503 includes_cnt = 0;
504 if (includes_idx != -1) {
505 for (includes_cnt = 0;
506 multi_s[includes_cnt] != NULL;
507 includes_cnt ++);
509 /* place the name of the share in the smbconf_service struct */
510 tmp_share.name = talloc_strdup(frame, share_name);
511 if (tmp_share.name == NULL) {
512 result = WERR_NOMEM;
513 d_fprintf(stderr, _("Failed to create share: %s\n"),
514 win_errstr(result));
515 goto error;
517 /* place the number of parameters in the smbconf_service struct */
518 tmp_share.num_params = num_vals;
519 if (includes_idx != -1) {
520 tmp_share.num_params = num_vals + includes_cnt - 1;
522 /* allocate memory for the param_names and param_values lists */
523 tmp_share.param_names = talloc_zero_array(frame, char *, tmp_share.num_params);
524 if (tmp_share.param_names == NULL) {
525 result = WERR_NOMEM;
526 d_fprintf(stderr, _("Failed to create share: %s\n"),
527 win_errstr(result));
528 goto error;
530 tmp_share.param_values = talloc_zero_array(frame, char *, tmp_share.num_params);
531 if (tmp_share.param_values == NULL) {
532 result = WERR_NOMEM;
533 d_fprintf(stderr, _("Failed to create share: %s\n"),
534 win_errstr(result));
535 goto error;
537 /* place all params except includes */
538 for (i = 0; i < num_vals; i++) {
539 if (strcmp(val_names[i], "includes") != 0) {
540 if (!pull_reg_sz(frame, &data[i], &s)) {
541 result = WERR_NOMEM;
542 d_fprintf(stderr,
543 _("Failed to enumerate values: %s\n"),
544 win_errstr(result));
545 goto error;
547 /* place param_names */
548 tmp_share.param_names[param_cnt] = talloc_strdup(frame, val_names[i]);
549 if (tmp_share.param_names[param_cnt] == NULL) {
550 result = WERR_NOMEM;
551 d_fprintf(stderr, _("Failed to create share: %s\n"),
552 win_errstr(result));
553 goto error;
556 /* place param_values */
557 tmp_share.param_values[param_cnt++] = talloc_strdup(frame, s);
558 if (tmp_share.param_values[param_cnt - 1] == NULL) {
559 result = WERR_NOMEM;
560 d_fprintf(stderr, _("Failed to create share: %s\n"),
561 win_errstr(result));
562 goto error;
566 /* place the includes last */
567 for (i = 0; i < includes_cnt; i++) {
568 tmp_share.param_names[param_cnt] = talloc_strdup(frame, "include");
569 if (tmp_share.param_names[param_cnt] == NULL) {
570 result = WERR_NOMEM;
571 d_fprintf(stderr, _("Failed to create share: %s\n"),
572 win_errstr(result));
573 goto error;
576 tmp_share.param_values[param_cnt++] = talloc_strdup(frame, multi_s[i]);
577 if (tmp_share.param_values[param_cnt - 1] == NULL) {
578 result = WERR_NOMEM;
579 d_fprintf(stderr, _("Failed to create share: %s\n"),
580 win_errstr(result));
581 goto error;
585 /* move everything to the main memory ctx */
586 for (i = 0; i < param_cnt; i++) {
587 tmp_share.param_names[i] = talloc_move(mem_ctx, &tmp_share.param_names[i]);
588 tmp_share.param_values[i] = talloc_move(mem_ctx, &tmp_share.param_values[i]);
591 tmp_share.name = talloc_move(mem_ctx, &tmp_share.name);
592 tmp_share.param_names = talloc_move(mem_ctx, &tmp_share.param_names);
593 tmp_share.param_values = talloc_move(mem_ctx, &tmp_share.param_values);
594 /* out parameter */
595 *share = tmp_share;
596 error:
597 /* close child */
598 dcerpc_winreg_CloseKey(b, frame, &child_hnd, &_werr);
599 *werr = result;
600 TALLOC_FREE(frame);
601 return status;
605 * The function prints the shares held as smbconf_service structs
606 * in a smbconf file format.
608 static int rpc_conf_print_shares(uint32_t num_shares,
609 struct smbconf_service *shares)
612 uint32_t share_count, param_count;
613 const char *indent = "\t";
615 if (num_shares == 0) {
616 return 0;
619 for (share_count = 0; share_count < num_shares; share_count++) {
620 d_printf("\n");
621 if (shares[share_count].name != NULL) {
622 d_printf("[%s]\n", shares[share_count].name);
625 for (param_count = 0;
626 param_count < shares[share_count].num_params;
627 param_count++)
629 d_printf("%s%s = %s\n",
630 indent,
631 shares[share_count].param_names[param_count],
632 shares[share_count].param_values[param_count]);
635 d_printf("\n");
637 return 0;
642 * The function openes the registry key
643 * HKLM/Software/Samba/smbconf with the give access_mask
645 static NTSTATUS rpc_conf_open_conf(TALLOC_CTX *mem_ctx,
646 struct dcerpc_binding_handle *b,
647 uint32_t access_mask,
648 struct policy_handle *hive_hnd,
649 struct policy_handle *key_hnd,
650 WERROR *werr)
652 TALLOC_CTX *frame = talloc_stackframe();
653 NTSTATUS status = NT_STATUS_OK;
654 WERROR result = WERR_OK;
655 WERROR _werr;
656 struct policy_handle tmp_hive_hnd, tmp_key_hnd;
657 struct winreg_String key;
659 ZERO_STRUCT(key);
661 status = dcerpc_winreg_OpenHKLM(b, frame, NULL,
662 access_mask, &tmp_hive_hnd, &result);
665 * print no error messages if it is a read only open
666 * and key does not exist
667 * error still gets returned
670 if (access_mask == REG_KEY_READ &&
671 W_ERROR_EQUAL(result, WERR_BADFILE))
673 goto error;
676 if (!(NT_STATUS_IS_OK(status))) {
677 d_fprintf(stderr, _("Failed to open hive: %s\n"),
678 nt_errstr(status));
679 goto error;
681 if (!W_ERROR_IS_OK(result)) {
682 d_fprintf(stderr, _("Failed to open hive: %s\n"),
683 win_errstr(result));
684 goto error;
687 key.name = confpath;
688 status = dcerpc_winreg_OpenKey(b, frame, &tmp_hive_hnd, key, 0,
689 access_mask, &tmp_key_hnd, &result);
692 * print no error messages if it is a read only open
693 * and key does not exist
694 * error still gets returned
697 if (access_mask == REG_KEY_READ &&
698 W_ERROR_EQUAL(result, WERR_BADFILE))
700 goto error;
703 if (!(NT_STATUS_IS_OK(status))) {
704 d_fprintf(stderr, _("Failed to open smbconf key: %s\n"),
705 nt_errstr(status));
706 dcerpc_winreg_CloseKey(b, frame, &tmp_hive_hnd, &_werr);
707 goto error;
709 if (!(W_ERROR_IS_OK(result))) {
710 d_fprintf(stderr, _("Failed to open smbconf key: %s\n"),
711 win_errstr(result));
712 dcerpc_winreg_CloseKey(b, frame, &tmp_hive_hnd, &_werr);
713 goto error;
716 *hive_hnd = tmp_hive_hnd;
717 *key_hnd = tmp_key_hnd;
719 error:
720 TALLOC_FREE(frame);
721 *werr = result;
723 return status;
726 /**********************************************************
728 * internal functions that provide the functionality
729 * net rpc conf
731 **********************************************************/
733 static NTSTATUS rpc_conf_listshares_internal(struct net_context *c,
734 const struct dom_sid *domain_sid,
735 const char *domain_name,
736 struct cli_state *cli,
737 struct rpc_pipe_client *pipe_hnd,
738 TALLOC_CTX *mem_ctx,
739 int argc,
740 const char **argv )
743 TALLOC_CTX *frame = talloc_stackframe();
744 NTSTATUS status = NT_STATUS_OK;
745 WERROR werr = WERR_OK;
746 WERROR _werr;
748 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
750 /* key info */
751 struct policy_handle hive_hnd, key_hnd;
752 uint32_t num_subkeys;
753 uint32_t i;
754 const char **subkeys = NULL;
757 ZERO_STRUCT(hive_hnd);
758 ZERO_STRUCT(key_hnd);
761 if (argc != 0 || c->display_usage) {
762 rpc_conf_listshares_usage(c, argc, argv);
763 status = NT_STATUS_INVALID_PARAMETER;
764 goto error;
768 status = rpc_conf_open_conf(frame,
770 REG_KEY_READ,
771 &hive_hnd,
772 &key_hnd,
773 &werr);
775 if (!(NT_STATUS_IS_OK(status))) {
776 goto error;
779 if (!(W_ERROR_IS_OK(werr))) {
780 goto error;
783 status = dcerpc_winreg_enum_keys(frame,
785 &key_hnd,
786 &num_subkeys,
787 &subkeys,
788 &werr);
790 if (!(NT_STATUS_IS_OK(status))) {
791 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
792 nt_errstr(status));
793 goto error;
796 if (!(W_ERROR_IS_OK(werr))) {
797 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
798 win_errstr(werr));
799 goto error;
802 for (i = 0; i < num_subkeys; i++) {
803 d_printf("%s\n", subkeys[i]);
806 error:
807 if (!(W_ERROR_IS_OK(werr))) {
808 status = werror_to_ntstatus(werr);
811 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
812 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
814 TALLOC_FREE(frame);
815 return status;;
818 static NTSTATUS rpc_conf_delshare_internal(struct net_context *c,
819 const struct dom_sid *domain_sid,
820 const char *domain_name,
821 struct cli_state *cli,
822 struct rpc_pipe_client *pipe_hnd,
823 TALLOC_CTX *mem_ctx,
824 int argc,
825 const char **argv )
828 TALLOC_CTX *frame = talloc_stackframe();
829 NTSTATUS status = NT_STATUS_OK;
830 WERROR werr = WERR_OK;
831 WERROR _werr;
833 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
835 /* key info */
836 struct policy_handle hive_hnd, key_hnd;
838 ZERO_STRUCT(hive_hnd);
839 ZERO_STRUCT(key_hnd);
842 if (argc != 1 || c->display_usage) {
843 rpc_conf_delshare_usage(c, argc, argv);
844 status = NT_STATUS_INVALID_PARAMETER;
845 goto error;
848 status = rpc_conf_open_conf(frame,
850 REG_KEY_ALL,
851 &hive_hnd,
852 &key_hnd,
853 &werr);
855 if (!(NT_STATUS_IS_OK(status))) {
856 goto error;
859 if (!(W_ERROR_IS_OK(werr))) {
860 goto error;
863 status = dcerpc_winreg_delete_subkeys_recursive(frame,
865 &key_hnd,
866 REG_KEY_ALL,
867 argv[0],
868 &werr);
870 if (!NT_STATUS_IS_OK(status)) {
871 d_fprintf(stderr,
872 "winreg_delete_subkeys: Could not delete key %s: %s\n",
873 argv[0], nt_errstr(status));
874 goto error;
877 if (W_ERROR_EQUAL(werr, WERR_BADFILE)){
878 d_fprintf(stderr, _("ERROR: Key does not exist\n"));
882 if (!W_ERROR_IS_OK(werr)) {
883 d_fprintf(stderr,
884 "winreg_delete_subkeys: Could not delete key %s: %s\n",
885 argv[0], win_errstr(werr));
886 goto error;
889 error:
890 if (!(W_ERROR_IS_OK(werr))) {
891 status = werror_to_ntstatus(werr);
894 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
895 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
897 TALLOC_FREE(frame);
899 return status;
902 static NTSTATUS rpc_conf_list_internal(struct net_context *c,
903 const struct dom_sid *domain_sid,
904 const char *domain_name,
905 struct cli_state *cli,
906 struct rpc_pipe_client *pipe_hnd,
907 TALLOC_CTX *mem_ctx,
908 int argc,
909 const char **argv )
912 TALLOC_CTX *frame = talloc_stackframe();
913 NTSTATUS status = NT_STATUS_OK;
914 WERROR werr = WERR_OK;
915 WERROR _werr;
917 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
919 /* key info */
920 struct policy_handle hive_hnd, key_hnd;
921 uint32_t num_subkeys;
922 uint32_t i;
923 struct smbconf_service *shares;
924 const char **subkeys = NULL;
927 ZERO_STRUCT(hive_hnd);
928 ZERO_STRUCT(key_hnd);
931 if (argc != 0 || c->display_usage) {
932 rpc_conf_list_usage(c, argc, argv);
933 status = NT_STATUS_INVALID_PARAMETER;
934 goto error;
937 status = rpc_conf_open_conf(frame,
939 REG_KEY_READ,
940 &hive_hnd,
941 &key_hnd,
942 &werr);
944 if (!(NT_STATUS_IS_OK(status))) {
945 goto error;
948 if (!(W_ERROR_IS_OK(werr))) {
949 goto error;
952 status = dcerpc_winreg_enum_keys(frame,
954 &key_hnd,
955 &num_subkeys,
956 &subkeys,
957 &werr);
959 if (!(NT_STATUS_IS_OK(status))) {
960 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
961 nt_errstr(status));
962 goto error;
965 if (!(W_ERROR_IS_OK(werr))) {
966 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
967 win_errstr(werr));
968 goto error;
971 if (num_subkeys == 0) {
972 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
973 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
974 TALLOC_FREE(frame);
975 return NT_STATUS_OK;
978 /* get info from each subkey */
979 shares = talloc_zero_array(frame, struct smbconf_service, num_subkeys);
980 if (shares == NULL) {
981 werr = WERR_NOMEM;
982 d_fprintf(stderr, _("Failed to create shares: %s\n"),
983 win_errstr(werr));
984 goto error;
988 for (i = 0; i < num_subkeys; i++) {
989 /* get each share and place it in the shares array */
990 status = rpc_conf_get_share(frame,
992 &key_hnd,
993 subkeys[i],
994 &shares[i],
995 &werr);
996 if (!(NT_STATUS_IS_OK(status))) {
997 goto error;
999 if (!(W_ERROR_IS_OK(werr))) {
1000 goto error;
1004 /* print the shares array */
1005 rpc_conf_print_shares(num_subkeys, shares);
1007 error:
1008 if (!(W_ERROR_IS_OK(werr))) {
1009 status = werror_to_ntstatus(werr);
1012 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1013 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1015 TALLOC_FREE(frame);
1016 return status;
1020 static NTSTATUS rpc_conf_drop_internal(struct net_context *c,
1021 const struct dom_sid *domain_sid,
1022 const char *domain_name,
1023 struct cli_state *cli,
1024 struct rpc_pipe_client *pipe_hnd,
1025 TALLOC_CTX *mem_ctx,
1026 int argc,
1027 const char **argv )
1029 TALLOC_CTX *frame = talloc_stackframe();
1030 NTSTATUS status = NT_STATUS_OK;
1031 WERROR werr = WERR_OK;
1032 WERROR _werr;
1034 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1036 /* key info */
1037 struct policy_handle hive_hnd, key_hnd;
1038 const char *keyname = confpath;
1039 struct winreg_String wkey, wkeyclass;
1040 enum winreg_CreateAction action = REG_ACTION_NONE;
1043 ZERO_STRUCT(hive_hnd);
1044 ZERO_STRUCT(key_hnd);
1047 if (argc != 0 || c->display_usage) {
1048 rpc_conf_drop_usage(c, argc, argv);
1049 status = NT_STATUS_INVALID_PARAMETER;
1050 goto error;
1053 status = rpc_conf_open_conf(frame,
1055 REG_KEY_ALL,
1056 &hive_hnd,
1057 &key_hnd,
1058 &werr);
1060 if (!(NT_STATUS_IS_OK(status))) {
1061 goto error;
1064 if (!(W_ERROR_IS_OK(werr))) {
1065 goto error;
1068 status = dcerpc_winreg_delete_subkeys_recursive(frame,
1070 &hive_hnd,
1071 REG_KEY_ALL,
1072 keyname,
1073 &werr);
1075 if (!NT_STATUS_IS_OK(status)) {
1076 d_printf("winreg_delete_subkeys: Could not delete key %s: %s\n",
1077 keyname, nt_errstr(status));
1078 goto error;
1081 if (!W_ERROR_IS_OK(werr)) {
1082 d_printf("winreg_delete_subkeys: Could not delete key %s: %s\n",
1083 keyname, win_errstr(werr));
1084 goto error;
1087 ZERO_STRUCT(wkey);
1088 wkey.name = keyname;
1089 ZERO_STRUCT(wkeyclass);
1090 wkeyclass.name = "";
1091 action = REG_ACTION_NONE;
1093 status = dcerpc_winreg_CreateKey(b,
1094 frame,
1095 &hive_hnd,
1096 wkey,
1097 wkeyclass,
1099 REG_KEY_ALL,
1100 NULL,
1101 &key_hnd,
1102 &action,
1103 &werr);
1105 if (!NT_STATUS_IS_OK(status)) {
1106 d_printf("winreg_CreateKey: Could not create smbconf key\n");
1107 goto error;
1110 if (!W_ERROR_IS_OK(werr)) {
1111 d_printf("winreg_CreateKey: Could not create smbconf key\n");
1112 goto error;
1116 error:
1117 if (!(W_ERROR_IS_OK(werr))) {
1118 status = werror_to_ntstatus(werr);
1121 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1122 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1124 TALLOC_FREE(frame);
1125 return status;
1128 static NTSTATUS rpc_conf_import_internal(struct net_context *c,
1129 const struct dom_sid *domain_sid,
1130 const char *domain_name,
1131 struct cli_state *cli,
1132 struct rpc_pipe_client *pipe_hnd,
1133 TALLOC_CTX *mem_ctx,
1134 int argc,
1135 const char **argv )
1138 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1140 struct policy_handle hive_hnd, key_hnd;
1142 const char *filename = NULL;
1143 const char *servicename = NULL;
1144 char *conf_source = NULL;
1145 TALLOC_CTX *frame;
1146 struct smbconf_ctx *txt_ctx;
1147 struct smbconf_service *service = NULL;
1148 struct smbconf_service **services = NULL;
1149 uint32_t num_shares, i;
1150 sbcErr err = SBC_ERR_UNKNOWN_FAILURE;
1152 WERROR werr = WERR_OK;
1153 NTSTATUS status = NT_STATUS_OK;
1155 ZERO_STRUCT(hive_hnd);
1156 ZERO_STRUCT(key_hnd);
1158 frame = talloc_stackframe();
1160 if (c->display_usage) {
1161 rpc_conf_import_usage(c, argc, argv);
1162 status = NT_STATUS_INVALID_PARAMETER;
1163 goto error;
1166 switch (argc) {
1167 case 0:
1168 default:
1169 rpc_conf_import_usage(c, argc, argv);
1170 status = NT_STATUS_INVALID_PARAMETER;
1171 goto error;
1172 case 2:
1173 servicename = talloc_strdup(frame, argv[1]);
1174 if (servicename == NULL) {
1175 d_printf(_("error: out of memory!\n"));
1176 goto error;
1178 case 1:
1179 filename = argv[0];
1180 break;
1183 DEBUG(3,("rpc_conf_import: reading configuration from file %s.\n",
1184 filename));
1186 conf_source = talloc_asprintf(frame, "file:%s", filename);
1187 if (conf_source == NULL) {
1188 d_fprintf(stderr, _("error: out of memory!\n"));
1189 goto error;
1192 err = smbconf_init(frame, &txt_ctx, conf_source);
1193 if (!SBC_ERROR_IS_OK(err)) {
1194 d_fprintf(stderr, _("error loading file '%s': %s\n"), filename,
1195 sbcErrorString(err));
1196 goto error;
1199 if (c->opt_testmode) {
1200 d_printf(_("\nTEST MODE - "
1201 "would import the following configuration:\n\n"));
1204 if (servicename != NULL) {
1205 err = smbconf_get_share(txt_ctx, frame,
1206 servicename,
1207 &service);
1208 if (!SBC_ERROR_IS_OK(err)) {
1209 goto error;
1212 num_shares = 1;
1214 } else {
1216 err = smbconf_get_config(txt_ctx, frame,
1217 &num_shares,
1218 &services);
1219 if (!SBC_ERROR_IS_OK(err)) {
1220 goto error;
1224 if (c->opt_testmode) {
1225 if (servicename != NULL) {
1226 rpc_conf_print_shares(1, service);
1228 for (i = 0; i < num_shares; i++) {
1229 rpc_conf_print_shares(1, services[i]);
1231 goto error;
1234 status = rpc_conf_drop_internal(c,
1235 domain_sid,
1236 domain_name,
1237 cli,
1238 pipe_hnd,
1239 frame,
1241 NULL );
1243 if (!(NT_STATUS_IS_OK(status))) {
1244 goto error;
1247 status = rpc_conf_open_conf(frame,
1249 REG_KEY_READ,
1250 &hive_hnd,
1251 &key_hnd,
1252 &werr);
1254 if (!(NT_STATUS_IS_OK(status))) {
1255 goto error;
1258 if (!(W_ERROR_IS_OK(werr))) {
1259 goto error;
1262 if (servicename != NULL) {
1263 status = rpc_conf_set_share(frame,
1265 &key_hnd,
1266 service,
1267 &werr);
1269 if (!(NT_STATUS_IS_OK(status))) {
1270 goto error;
1273 if (!(W_ERROR_IS_OK(werr))) {
1274 goto error;
1277 } else {
1279 for (i = 0; i < num_shares; i++) {
1280 status = rpc_conf_set_share(frame,
1282 &key_hnd,
1283 services[i],
1284 &werr);
1286 if (!(NT_STATUS_IS_OK(status))) {
1287 goto error;
1290 if (!(W_ERROR_IS_OK(werr))) {
1291 goto error;
1297 error:
1298 if (!SBC_ERROR_IS_OK(err)) {
1299 d_fprintf(stderr, "ERROR: %s\n", sbcErrorString(err));
1302 if (!(W_ERROR_IS_OK(werr))) {
1303 status = werror_to_ntstatus(werr);
1305 TALLOC_FREE(frame);
1306 return status;
1309 static NTSTATUS rpc_conf_showshare_internal(struct net_context *c,
1310 const struct dom_sid *domain_sid,
1311 const char *domain_name,
1312 struct cli_state *cli,
1313 struct rpc_pipe_client *pipe_hnd,
1314 TALLOC_CTX *mem_ctx,
1315 int argc,
1316 const char **argv )
1318 TALLOC_CTX *frame = talloc_stackframe();
1319 NTSTATUS status = NT_STATUS_OK;
1320 WERROR werr = WERR_OK;
1321 WERROR _werr;
1323 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1325 /* key info */
1326 struct policy_handle hive_hnd, key_hnd;
1327 struct smbconf_service *service = NULL;
1328 const char *sharename = NULL;
1331 ZERO_STRUCT(hive_hnd);
1332 ZERO_STRUCT(key_hnd);
1335 if (argc != 1 || c->display_usage) {
1336 rpc_conf_showshare_usage(c, argc, argv);
1337 status = NT_STATUS_INVALID_PARAMETER;
1338 goto error;
1341 status = rpc_conf_open_conf(frame,
1343 REG_KEY_READ,
1344 &hive_hnd,
1345 &key_hnd,
1346 &werr);
1348 if (!(NT_STATUS_IS_OK(status))) {
1349 goto error;
1352 if (!(W_ERROR_IS_OK(werr))) {
1353 goto error;
1356 sharename = talloc_strdup(frame, argv[0]);
1357 if (sharename == NULL) {
1358 werr = WERR_NOMEM;
1359 d_fprintf(stderr, _("Failed to create share: %s\n"),
1360 win_errstr(werr));
1361 goto error;
1364 service = talloc(frame, struct smbconf_service);
1365 if (service == NULL) {
1366 werr = WERR_NOMEM;
1367 d_fprintf(stderr, _("Failed to create share: %s\n"),
1368 win_errstr(werr));
1369 goto error;
1372 status = rpc_conf_get_share(frame,
1374 &key_hnd,
1375 sharename,
1376 service,
1377 &werr);
1379 if (!(NT_STATUS_IS_OK(status))) {
1380 goto error;
1382 if (!(W_ERROR_IS_OK(werr))) {
1383 goto error;
1386 rpc_conf_print_shares(1, service);
1388 error:
1389 if (!(W_ERROR_IS_OK(werr))) {
1390 status = werror_to_ntstatus(werr);
1393 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1394 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1396 TALLOC_FREE(frame);
1397 return status;
1400 static NTSTATUS rpc_conf_addshare_internal(struct net_context *c,
1401 const struct dom_sid *domain_sid,
1402 const char *domain_name,
1403 struct cli_state *cli,
1404 struct rpc_pipe_client *pipe_hnd,
1405 TALLOC_CTX *mem_ctx,
1406 int argc,
1407 const char **argv )
1409 TALLOC_CTX *frame = talloc_stackframe();
1410 NTSTATUS status = NT_STATUS_OK;
1411 WERROR werr = WERR_OK;
1412 WERROR _werr;
1414 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1416 /* key info */
1417 struct policy_handle hive_hnd, key_hnd, share_hnd;
1418 char *sharename = NULL;
1419 const char *path = NULL;
1420 const char *comment = NULL;
1421 const char *guest_ok = "no";
1422 const char *read_only = "yes";
1423 struct winreg_String key, keyclass;
1424 enum winreg_CreateAction action = 0;
1427 ZERO_STRUCT(hive_hnd);
1428 ZERO_STRUCT(key_hnd);
1429 ZERO_STRUCT(share_hnd);
1431 ZERO_STRUCT(key);
1432 ZERO_STRUCT(keyclass);
1434 if (c->display_usage) {
1435 rpc_conf_addshare_usage(c, argc, argv);
1436 status = NT_STATUS_INVALID_PARAMETER;
1437 goto error;
1440 switch (argc) {
1441 case 0:
1442 case 1:
1443 default:
1444 rpc_conf_addshare_usage(c, argc, argv);
1445 status = NT_STATUS_INVALID_PARAMETER;
1446 goto error;
1447 case 5:
1448 comment = argv[4];
1449 case 4:
1450 if (!strnequal(argv[3], "guest_ok=", 9)) {
1451 rpc_conf_addshare_usage(c, argc, argv);
1452 status = NT_STATUS_INVALID_PARAMETER;
1453 goto error;
1455 switch (argv[3][9]) {
1456 case 'y':
1457 case 'Y':
1458 guest_ok = "yes";
1459 break;
1460 case 'n':
1461 case 'N':
1462 guest_ok = "no";
1463 break;
1464 default:
1465 rpc_conf_addshare_usage(c, argc, argv);
1466 status = NT_STATUS_INVALID_PARAMETER;
1467 goto error;
1469 case 3:
1470 if (!strnequal(argv[2], "writeable=", 10)) {
1471 rpc_conf_addshare_usage(c, argc, argv);
1472 status = NT_STATUS_INVALID_PARAMETER;
1473 goto error;
1475 switch (argv[2][10]) {
1476 case 'y':
1477 case 'Y':
1478 read_only = "no";
1479 break;
1480 case 'n':
1481 case 'N':
1482 read_only = "yes";
1483 break;
1484 default:
1485 rpc_conf_addshare_usage(c, argc, argv);
1486 status = NT_STATUS_INVALID_PARAMETER;
1487 goto error;
1489 case 2:
1490 path = argv[1];
1491 sharename = talloc_strdup(frame, argv[0]);
1492 if (sharename == NULL) {
1493 d_printf(_("error: out of memory!\n"));
1494 goto error;
1497 break;
1500 status = rpc_conf_open_conf(frame,
1502 REG_KEY_READ,
1503 &hive_hnd,
1504 &key_hnd,
1505 &werr);
1507 if (!(NT_STATUS_IS_OK(status))) {
1508 goto error;
1511 if (!(W_ERROR_IS_OK(werr))) {
1512 goto error;
1515 key.name = argv[0];
1516 keyclass.name = "";
1518 status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
1519 0, REG_KEY_READ, NULL, &share_hnd,
1520 &action, &werr);
1522 if (!(NT_STATUS_IS_OK(status))) {
1523 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1524 argv[0], nt_errstr(status));
1525 goto error;
1528 if (!W_ERROR_IS_OK(werr)) {
1529 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1530 argv[0], win_errstr(werr));
1531 goto error;
1534 switch (action) {
1535 case REG_ACTION_NONE:
1536 werr = WERR_CREATE_FAILED;
1537 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1538 argv[0], win_errstr(werr));
1539 goto error;
1540 case REG_CREATED_NEW_KEY:
1541 DEBUG(5, ("net rpc conf setincludes:"
1542 "createkey created %s\n", argv[0]));
1543 break;
1544 case REG_OPENED_EXISTING_KEY:
1545 d_fprintf(stderr, _("ERROR: Share '%s' already exists\n"), argv[0]);
1546 status = NT_STATUS_INVALID_PARAMETER;
1547 goto error;
1550 /* set the path parameter */
1551 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1552 "path", path, &werr);
1554 if (!(NT_STATUS_IS_OK(status))) {
1555 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1556 " with value %s\n %s\n",
1557 "path", path, nt_errstr(status));
1558 goto error;
1561 if (!(W_ERROR_IS_OK(werr))) {
1562 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1563 " with value %s\n %s\n",
1564 "path", path, win_errstr(werr));
1565 goto error;
1568 /* set the writeable parameter */
1569 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1570 "read only", read_only, &werr);
1572 if (!(NT_STATUS_IS_OK(status))) {
1573 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1574 " with value %s\n %s\n",
1575 "read only", read_only, nt_errstr(status));
1576 goto error;
1579 if (!(W_ERROR_IS_OK(werr))) {
1580 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1581 " with value %s\n %s\n",
1582 "read only", read_only, win_errstr(werr));
1583 goto error;
1586 /* set the guest ok parameter */
1587 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1588 "guest ok", guest_ok, &werr);
1590 if (!(NT_STATUS_IS_OK(status))) {
1591 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1592 " with value %s\n %s\n",
1593 "guest ok", guest_ok, nt_errstr(status));
1594 goto error;
1597 if (!(W_ERROR_IS_OK(werr))) {
1598 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1599 " with value %s\n %s\n",
1600 "guest ok", guest_ok, win_errstr(werr));
1601 goto error;
1604 if (argc == 5) {
1605 /* set the comment parameter */
1606 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1607 "comment", comment, &werr);
1609 if (!(NT_STATUS_IS_OK(status))) {
1610 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1611 " with value %s\n %s\n",
1612 "comment", comment, nt_errstr(status));
1613 goto error;
1616 if (!(W_ERROR_IS_OK(werr))) {
1617 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1618 " with value %s\n %s\n",
1619 "comment", comment, win_errstr(werr));
1620 goto error;
1623 error:
1624 if (!(W_ERROR_IS_OK(werr))) {
1625 status = werror_to_ntstatus(werr);
1628 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1629 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1630 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
1632 TALLOC_FREE(frame);
1633 return status;
1636 static NTSTATUS rpc_conf_getparm_internal(struct net_context *c,
1637 const struct dom_sid *domain_sid,
1638 const char *domain_name,
1639 struct cli_state *cli,
1640 struct rpc_pipe_client *pipe_hnd,
1641 TALLOC_CTX *mem_ctx,
1642 int argc,
1643 const char **argv )
1645 TALLOC_CTX *frame = talloc_stackframe();
1646 NTSTATUS status = NT_STATUS_OK;
1647 WERROR werr = WERR_OK;
1648 WERROR _werr;
1650 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1652 /* key info */
1653 struct policy_handle hive_hnd, key_hnd;
1654 struct smbconf_service *service = NULL;
1656 bool param_is_set = false;
1657 uint32_t param_count;
1659 ZERO_STRUCT(hive_hnd);
1660 ZERO_STRUCT(key_hnd);
1663 if (argc != 2 || c->display_usage) {
1664 rpc_conf_getparm_usage(c, argc, argv);
1665 status = NT_STATUS_INVALID_PARAMETER;
1666 goto error;
1669 status = rpc_conf_open_conf(frame,
1671 REG_KEY_READ,
1672 &hive_hnd,
1673 &key_hnd,
1674 &werr);
1676 if (!(NT_STATUS_IS_OK(status))) {
1677 goto error;
1680 if (!(W_ERROR_IS_OK(werr))) {
1681 goto error;
1685 service = talloc(frame, struct smbconf_service);
1687 status = rpc_conf_get_share(frame,
1689 &key_hnd,
1690 argv[0],
1691 service,
1692 &werr);
1694 if (!(NT_STATUS_IS_OK(status))) {
1695 goto error;
1698 if (W_ERROR_EQUAL(werr, WERR_BADFILE)) {
1699 d_fprintf(stderr, _("ERROR: Share %s does not exist\n"),
1700 argv[0]);
1701 goto error;
1704 if (!(W_ERROR_IS_OK(werr))) {
1705 goto error;
1708 for (param_count = 0;
1709 param_count < service->num_params;
1710 param_count++)
1712 /* should includes also be printed? */
1713 if (strcmp(service->param_names[param_count], argv[1]) == 0) {
1714 d_printf(_("%s\n"),
1715 service->param_values[param_count]);
1716 param_is_set = true;
1720 if (!param_is_set) {
1721 d_fprintf(stderr, _("ERROR: Given parameter '%s' has not been set\n"),
1722 argv[1]);
1723 werr = WERR_BADFILE;
1724 goto error;
1727 error:
1729 if (!(W_ERROR_IS_OK(werr))) {
1730 status = werror_to_ntstatus(werr);
1733 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1734 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1736 TALLOC_FREE(frame);
1737 return status;
1741 static NTSTATUS rpc_conf_setparm_internal(struct net_context *c,
1742 const struct dom_sid *domain_sid,
1743 const char *domain_name,
1744 struct cli_state *cli,
1745 struct rpc_pipe_client *pipe_hnd,
1746 TALLOC_CTX *mem_ctx,
1747 int argc,
1748 const char **argv )
1750 TALLOC_CTX *frame = talloc_stackframe();
1751 NTSTATUS status = NT_STATUS_OK;
1752 WERROR werr = WERR_OK;
1753 WERROR _werr;
1755 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1757 /* key info */
1758 struct policy_handle hive_hnd, key_hnd, share_hnd;
1760 struct winreg_String key, keyclass;
1761 enum winreg_CreateAction action = 0;
1763 const char *canon_valname;
1764 const char *canon_valstr;
1766 ZERO_STRUCT(hive_hnd);
1767 ZERO_STRUCT(key_hnd);
1768 ZERO_STRUCT(share_hnd);
1770 ZERO_STRUCT(key);
1771 ZERO_STRUCT(keyclass);
1773 if (argc != 3 || c->display_usage) {
1774 rpc_conf_setparm_usage(c, argc, argv);
1775 status = NT_STATUS_INVALID_PARAMETER;
1776 goto error;
1779 status = rpc_conf_open_conf(frame,
1781 REG_KEY_READ,
1782 &hive_hnd,
1783 &key_hnd,
1784 &werr);
1786 if (!(NT_STATUS_IS_OK(status))) {
1787 goto error;
1790 if (!(W_ERROR_IS_OK(werr))) {
1791 goto error;
1794 key.name = argv[0];
1795 keyclass.name = "";
1797 status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
1798 0, REG_KEY_READ, NULL, &share_hnd,
1799 &action, &werr);
1801 if (!(NT_STATUS_IS_OK(status))) {
1802 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1803 argv[0], nt_errstr(status));
1804 goto error;
1807 if (!W_ERROR_IS_OK(werr)) {
1808 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1809 argv[0], win_errstr(werr));
1810 goto error;
1813 switch (action) {
1814 case REG_ACTION_NONE:
1815 werr = WERR_CREATE_FAILED;
1816 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1817 argv[0], win_errstr(werr));
1818 goto error;
1819 case REG_CREATED_NEW_KEY:
1820 DEBUG(5, ("net rpc conf setparm:"
1821 "createkey created %s\n", argv[0]));
1822 break;
1823 case REG_OPENED_EXISTING_KEY:
1824 DEBUG(5, ("net rpc conf setparm:"
1825 "createkey opened existing %s\n", argv[0]));
1827 /* delete posibly existing value */
1828 status = rpc_conf_del_value(frame,
1830 &key_hnd,
1831 argv[0],
1832 argv[1],
1833 &werr);
1835 if (!(NT_STATUS_IS_OK(status))) {
1836 goto error;
1839 if (!(W_ERROR_IS_OK(werr))) {
1840 goto error;
1843 break;
1846 /* check if parameter is valid for writing */
1847 if (!lp_canonicalize_parameter_with_value(argv[1], argv[2],
1848 &canon_valname,
1849 &canon_valstr))
1851 if (canon_valname == NULL) {
1852 d_fprintf(stderr, "invalid parameter '%s' given\n",
1853 argv[1]);
1854 } else {
1855 d_fprintf(stderr, "invalid value '%s' given for "
1856 "parameter '%s'\n", argv[1], argv[2]);
1858 werr = WERR_INVALID_PARAM;
1859 goto error;
1862 if (rpc_conf_reg_valname_forbidden(canon_valname)) {
1863 d_fprintf(stderr, "Parameter '%s' not allowed in registry.\n",
1864 canon_valname);
1865 werr = WERR_INVALID_PARAM;
1866 goto error;
1869 if (!strequal(argv[0], "global") &&
1870 lp_parameter_is_global(argv[1]))
1872 d_fprintf(stderr, "Global parameter '%s' not allowed in "
1873 "service definition ('%s').\n", canon_valname,
1874 argv[0]);
1875 werr = WERR_INVALID_PARAM;
1876 goto error;
1879 /* set the parameter */
1880 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1881 argv[1], argv[2], &werr);
1883 if (!(NT_STATUS_IS_OK(status))) {
1884 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1885 " with value %s\n %s\n",
1886 argv[1], argv[2], nt_errstr(status));
1887 goto error;
1890 if (!(W_ERROR_IS_OK(werr))) {
1891 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1892 " with value %s\n %s\n",
1893 argv[1], argv[2], win_errstr(werr));
1894 goto error;
1897 error:
1899 if (!(W_ERROR_IS_OK(werr))) {
1900 status = werror_to_ntstatus(werr);
1903 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1904 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1905 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
1907 TALLOC_FREE(frame);
1908 return status;
1911 static NTSTATUS rpc_conf_delparm_internal(struct net_context *c,
1912 const struct dom_sid *domain_sid,
1913 const char *domain_name,
1914 struct cli_state *cli,
1915 struct rpc_pipe_client *pipe_hnd,
1916 TALLOC_CTX *mem_ctx,
1917 int argc,
1918 const char **argv )
1920 TALLOC_CTX *frame = talloc_stackframe();
1921 NTSTATUS status = NT_STATUS_OK;
1922 WERROR werr = WERR_OK;
1923 WERROR _werr;
1925 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1927 /* key info */
1928 struct policy_handle hive_hnd, key_hnd;
1931 ZERO_STRUCT(hive_hnd);
1932 ZERO_STRUCT(key_hnd);
1935 if (argc != 2 || c->display_usage) {
1936 rpc_conf_delparm_usage(c, argc, argv);
1937 status = NT_STATUS_INVALID_PARAMETER;
1938 goto error;
1941 status = rpc_conf_open_conf(frame,
1943 REG_KEY_READ,
1944 &hive_hnd,
1945 &key_hnd,
1946 &werr);
1948 if (!(NT_STATUS_IS_OK(status))) {
1949 goto error;
1952 if (!(W_ERROR_IS_OK(werr))) {
1953 goto error;
1956 status = rpc_conf_del_value(frame,
1958 &key_hnd,
1959 argv[0],
1960 argv[1],
1961 &werr);
1963 error:
1965 if (!(W_ERROR_IS_OK(werr))) {
1966 status = werror_to_ntstatus(werr);
1969 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1970 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1972 TALLOC_FREE(frame);
1973 return status;
1977 static NTSTATUS rpc_conf_getincludes_internal(struct net_context *c,
1978 const struct dom_sid *domain_sid,
1979 const char *domain_name,
1980 struct cli_state *cli,
1981 struct rpc_pipe_client *pipe_hnd,
1982 TALLOC_CTX *mem_ctx,
1983 int argc,
1984 const char **argv )
1986 TALLOC_CTX *frame = talloc_stackframe();
1987 NTSTATUS status = NT_STATUS_OK;
1988 WERROR werr = WERR_OK;
1989 WERROR _werr;
1991 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1993 /* key info */
1994 struct policy_handle hive_hnd, key_hnd;
1995 struct smbconf_service *service = NULL;
1997 uint32_t param_count;
2000 ZERO_STRUCT(hive_hnd);
2001 ZERO_STRUCT(key_hnd);
2004 if (argc != 1 || c->display_usage) {
2005 rpc_conf_getincludes_usage(c, argc, argv);
2006 status = NT_STATUS_INVALID_PARAMETER;
2007 goto error;
2010 status = rpc_conf_open_conf(frame,
2012 REG_KEY_READ,
2013 &hive_hnd,
2014 &key_hnd,
2015 &werr);
2017 if (!(NT_STATUS_IS_OK(status))) {
2018 goto error;
2021 if (!(W_ERROR_IS_OK(werr))) {
2022 goto error;
2025 service = talloc(frame, struct smbconf_service);
2027 status = rpc_conf_get_share(frame,
2029 &key_hnd,
2030 argv[0],
2031 service,
2032 &werr);
2034 if (!(NT_STATUS_IS_OK(status))) {
2035 goto error;
2038 if (!(W_ERROR_IS_OK(werr))) {
2039 goto error;
2042 for (param_count = 0;
2043 param_count < service->num_params;
2044 param_count++)
2046 if (strcmp(service->param_names[param_count], "include") == 0) {
2047 d_printf(_("%s = %s\n"),
2048 service->param_names[param_count],
2049 service->param_values[param_count]);
2053 error:
2055 if (!(W_ERROR_IS_OK(werr))) {
2056 status = werror_to_ntstatus(werr);
2059 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
2060 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
2062 TALLOC_FREE(frame);
2063 return status;
2067 static NTSTATUS rpc_conf_setincludes_internal(struct net_context *c,
2068 const struct dom_sid *domain_sid,
2069 const char *domain_name,
2070 struct cli_state *cli,
2071 struct rpc_pipe_client *pipe_hnd,
2072 TALLOC_CTX *mem_ctx,
2073 int argc,
2074 const char **argv )
2076 TALLOC_CTX *frame = talloc_stackframe();
2077 NTSTATUS status = NT_STATUS_OK;
2078 WERROR werr = WERR_OK;
2079 WERROR _werr;
2081 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2083 /* key info */
2084 struct policy_handle hive_hnd, key_hnd, share_hnd;
2086 struct winreg_String key, keyclass;
2087 enum winreg_CreateAction action = 0;
2089 ZERO_STRUCT(hive_hnd);
2090 ZERO_STRUCT(key_hnd);
2091 ZERO_STRUCT(share_hnd);
2093 ZERO_STRUCT(key);
2094 ZERO_STRUCT(keyclass);
2096 if (argc < 1 || c->display_usage) {
2097 rpc_conf_setincludes_usage(c, argc, argv);
2098 status = NT_STATUS_INVALID_PARAMETER;
2099 goto error;
2102 status = rpc_conf_open_conf(frame,
2104 REG_KEY_READ,
2105 &hive_hnd,
2106 &key_hnd,
2107 &werr);
2109 if (!(NT_STATUS_IS_OK(status))) {
2110 goto error;
2113 if (!(W_ERROR_IS_OK(werr))) {
2114 goto error;
2117 key.name = argv[0];
2118 keyclass.name = "";
2120 status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
2121 0, REG_KEY_READ, NULL, &share_hnd,
2122 &action, &werr);
2124 if (!(NT_STATUS_IS_OK(status))) {
2125 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
2126 argv[0], nt_errstr(status));
2127 goto error;
2130 if (!W_ERROR_IS_OK(werr)) {
2131 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
2132 argv[0], win_errstr(werr));
2133 goto error;
2136 switch (action) {
2137 case REG_ACTION_NONE:
2138 /* Is there any other way to treat this? */
2139 werr = WERR_CREATE_FAILED;
2140 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
2141 argv[0], win_errstr(werr));
2142 goto error;
2143 case REG_CREATED_NEW_KEY:
2144 DEBUG(5, ("net rpc conf setincludes:"
2145 "createkey created %s\n", argv[0]));
2146 break;
2147 case REG_OPENED_EXISTING_KEY:
2148 DEBUG(5, ("net rpc conf setincludes:"
2149 "createkey opened existing %s\n", argv[0]));
2151 /* delete posibly existing value */
2152 status = rpc_conf_del_value(frame,
2154 &key_hnd,
2155 argv[0],
2156 "includes",
2157 &werr);
2159 if (!(NT_STATUS_IS_OK(status))) {
2160 goto error;
2163 if (!(W_ERROR_IS_OK(werr))) {
2164 goto error;
2166 break;
2169 /* set the 'includes' values */
2170 status = dcerpc_winreg_set_multi_sz(frame, b, &share_hnd,
2171 "includes", argv + 1, &werr);
2172 if (!(NT_STATUS_IS_OK(status))) {
2173 d_fprintf(stderr, "ERROR: Could not set includes\n %s\n",
2174 nt_errstr(status));
2175 goto error;
2178 if (!(W_ERROR_IS_OK(werr))) {
2179 d_fprintf(stderr, "ERROR: Could not set includes\n %s\n",
2180 win_errstr(werr));
2181 goto error;
2184 error:
2186 if (!(W_ERROR_IS_OK(werr))) {
2187 status = werror_to_ntstatus(werr);
2190 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
2191 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
2192 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
2194 TALLOC_FREE(frame);
2195 return status;
2198 static NTSTATUS rpc_conf_delincludes_internal(struct net_context *c,
2199 const struct dom_sid *domain_sid,
2200 const char *domain_name,
2201 struct cli_state *cli,
2202 struct rpc_pipe_client *pipe_hnd,
2203 TALLOC_CTX *mem_ctx,
2204 int argc,
2205 const char **argv )
2207 TALLOC_CTX *frame = talloc_stackframe();
2208 NTSTATUS status = NT_STATUS_OK;
2209 WERROR werr = WERR_OK;
2210 WERROR _werr;
2212 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2214 /* key info */
2215 struct policy_handle hive_hnd, key_hnd;
2218 ZERO_STRUCT(hive_hnd);
2219 ZERO_STRUCT(key_hnd);
2222 if (argc != 1 || c->display_usage) {
2223 rpc_conf_delincludes_usage(c, argc, argv);
2224 status = NT_STATUS_INVALID_PARAMETER;
2225 goto error;
2228 status = rpc_conf_open_conf(frame,
2230 REG_KEY_READ,
2231 &hive_hnd,
2232 &key_hnd,
2233 &werr);
2235 if (!(NT_STATUS_IS_OK(status))) {
2236 goto error;
2239 if (!(W_ERROR_IS_OK(werr))) {
2240 goto error;
2243 status = rpc_conf_del_value(frame,
2245 &key_hnd,
2246 argv[0],
2247 "includes",
2248 &werr);
2250 error:
2252 if (!(W_ERROR_IS_OK(werr))) {
2253 status = werror_to_ntstatus(werr);
2256 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
2257 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
2259 TALLOC_FREE(frame);
2260 return status;
2264 /**********************************************************
2266 * Functions that run the rpc commands for net rpc conf modules
2268 **********************************************************/
2270 static int rpc_conf_drop(struct net_context *c, int argc,
2271 const char **argv)
2273 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2274 rpc_conf_drop_internal, argc, argv );
2278 static int rpc_conf_showshare(struct net_context *c, int argc,
2279 const char **argv)
2281 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2282 rpc_conf_showshare_internal, argc, argv );
2285 static int rpc_conf_addshare(struct net_context *c, int argc,
2286 const char **argv)
2288 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2289 rpc_conf_addshare_internal, argc, argv );
2292 static int rpc_conf_listshares(struct net_context *c, int argc,
2293 const char **argv)
2295 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2296 rpc_conf_listshares_internal, argc, argv );
2299 static int rpc_conf_list(struct net_context *c, int argc,
2300 const char **argv)
2302 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2303 rpc_conf_list_internal, argc, argv );
2306 static int rpc_conf_import(struct net_context *c, int argc,
2307 const char **argv)
2309 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2310 rpc_conf_import_internal, argc, argv );
2312 static int rpc_conf_delshare(struct net_context *c, int argc,
2313 const char **argv)
2315 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2316 rpc_conf_delshare_internal, argc, argv );
2319 static int rpc_conf_getparm(struct net_context *c, int argc,
2320 const char **argv)
2322 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2323 rpc_conf_getparm_internal, argc, argv );
2326 static int rpc_conf_setparm(struct net_context *c, int argc,
2327 const char **argv)
2329 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2330 rpc_conf_setparm_internal, argc, argv );
2332 static int rpc_conf_delparm(struct net_context *c, int argc,
2333 const char **argv)
2335 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2336 rpc_conf_delparm_internal, argc, argv );
2339 static int rpc_conf_getincludes(struct net_context *c, int argc,
2340 const char **argv)
2342 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2343 rpc_conf_getincludes_internal, argc, argv );
2346 static int rpc_conf_setincludes(struct net_context *c, int argc,
2347 const char **argv)
2349 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2350 rpc_conf_setincludes_internal, argc, argv );
2353 static int rpc_conf_delincludes(struct net_context *c, int argc,
2354 const char **argv)
2356 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2357 rpc_conf_delincludes_internal, argc, argv );
2360 /* function calls */
2361 int net_rpc_conf(struct net_context *c, int argc,
2362 const char **argv)
2364 struct functable func_table[] = {
2366 "list",
2367 rpc_conf_list,
2368 NET_TRANSPORT_RPC,
2369 N_("Dump the complete remote configuration in smb.conf like "
2370 "format."),
2371 N_("net rpc conf list\n"
2372 " Dump the complete remote configuration in smb.conf "
2373 "like format.")
2377 "import",
2378 rpc_conf_import,
2379 NET_TRANSPORT_RPC,
2380 N_("Import configuration from file in smb.conf "
2381 "format."),
2382 N_("net rpc conf import\n"
2383 " Import configuration from file in smb.conf "
2384 "format.")
2387 "listshares",
2388 rpc_conf_listshares,
2389 NET_TRANSPORT_RPC,
2390 N_("List the remote share names."),
2391 N_("net rpc conf list\n"
2392 " List the remote share names.")
2396 "drop",
2397 rpc_conf_drop,
2398 NET_TRANSPORT_RPC,
2399 N_("Delete the complete remote configuration."),
2400 N_("net rpc conf drop\n"
2401 " Delete the complete remote configuration.")
2405 "showshare",
2406 rpc_conf_showshare,
2407 NET_TRANSPORT_RPC,
2408 N_("Show the definition of a remote share."),
2409 N_("net rpc conf showshare\n"
2410 " Show the definition of a remote share.")
2414 "addshare",
2415 rpc_conf_addshare,
2416 NET_TRANSPORT_RPC,
2417 N_("Create a new remote share."),
2418 N_("net rpc conf addshare\n"
2419 " Create a new remote share.")
2422 "delshare",
2423 rpc_conf_delshare,
2424 NET_TRANSPORT_RPC,
2425 N_("Delete a remote share."),
2426 N_("net rpc conf delshare\n"
2427 " Delete a remote share.")
2430 "getparm",
2431 rpc_conf_getparm,
2432 NET_TRANSPORT_RPC,
2433 N_("Retrieve the value of a parameter."),
2434 N_("net rpc conf getparm\n"
2435 " Retrieve the value of a parameter.")
2438 "setparm",
2439 rpc_conf_setparm,
2440 NET_TRANSPORT_RPC,
2441 N_("Store a parameter."),
2442 N_("net rpc conf setparm\n"
2443 " Store a parameter.")
2446 "delparm",
2447 rpc_conf_delparm,
2448 NET_TRANSPORT_RPC,
2449 N_("Delete a parameter."),
2450 N_("net rpc conf delparm\n"
2451 " Delete a parameter.")
2454 "getincludes",
2455 rpc_conf_getincludes,
2456 NET_TRANSPORT_RPC,
2457 N_("Show the includes of a share definition."),
2458 N_("net rpc conf getincludes\n"
2459 " Show the includes of a share definition.")
2462 "setincludes",
2463 rpc_conf_setincludes,
2464 NET_TRANSPORT_RPC,
2465 N_("Set includes for a share."),
2466 N_("net rpc conf setincludes\n"
2467 " Set includes for a share.")
2470 "delincludes",
2471 rpc_conf_delincludes,
2472 NET_TRANSPORT_RPC,
2473 N_("Delete includes from a share definition."),
2474 N_("net rpc conf delincludes\n"
2475 " Delete includes from a share definition.")
2477 {NULL, NULL, 0, NULL, NULL}
2480 return net_run_function(c, argc, argv, "net rpc conf", func_table);