messages_ctdb: Use message_hdr_[get/put]
[Samba.git] / source3 / utils / net_rpc_conf.c
blob5a64accdcf4785f7318f2285cdebd3992b38cbd9
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 "utils/net_conf_util.h"
32 #include "rpc_client/cli_pipe.h"
33 #include "../librpc/gen_ndr/ndr_samr_c.h"
34 #include "rpc_client/init_samr.h"
35 #include "../librpc/gen_ndr/ndr_winreg_c.h"
36 #include "../libcli/registry/util_reg.h"
37 #include "rpc_client/cli_winreg.h"
38 #include "lib/smbconf/smbconf.h"
39 #include "lib/smbconf/smbconf_init.h"
40 #include "lib/smbconf/smbconf_reg.h"
41 #include "lib/param/loadparm.h"
45 /* internal functions */
46 /**********************************************************
48 * usage functions
50 **********************************************************/
51 const char confpath[100] = "Software\\Samba\\smbconf";
53 static int rpc_conf_list_usage(struct net_context *c, int argc,
54 const char **argv)
56 d_printf("%s net rpc conf list\n", _("Usage:"));
57 return -1;
60 static int rpc_conf_listshares_usage(struct net_context *c, int argc,
61 const char **argv)
63 d_printf("%s net rpc conf listshares\n", _("Usage:"));
64 return -1;
67 static int rpc_conf_delshare_usage(struct net_context *c, int argc,
68 const char **argv)
70 d_printf("%s\n%s",
71 _("Usage:"),
72 _("net rpc conf delshare <sharename>\n"));
73 return -1;
76 static int rpc_conf_addshare_usage(struct net_context *c, int argc,
77 const char **argv)
79 d_printf("%s\n%s",
80 _("Usage:"),
81 _(" net rpc conf addshare <sharename> <path> "
82 "[writeable={y|N} [guest_ok={y|N} [<comment>]]]\n"
83 "\t<sharename> the new share name.\n"
84 "\t<path> the path on the filesystem to export.\n"
85 "\twriteable={y|N} set \"writeable to \"yes\" or "
86 "\"no\" (default) on this share.\n"
87 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
88 "\"no\" (default) on this share.\n"
89 "\t<comment> optional comment for the new share.\n"));
90 return -1;
94 static int rpc_conf_import_usage(struct net_context *c, int argc,
95 const char**argv)
97 d_printf("%s\n%s",
98 _("Usage:"),
99 _(" net rpc conf import [--test|-T] <filename> "
100 "[<servicename>]\n"
101 "\t[--test|-T] testmode - do not act, just print "
102 "what would be done\n"
103 "\t<servicename> only import service <servicename>, "
104 "ignore the rest\n"));
105 return -1;
108 static int rpc_conf_showshare_usage(struct net_context *c, int argc,
109 const char **argv)
111 d_printf("%s\n%s",
112 _("Usage:"),
113 _("net rpc conf showshare <sharename>\n"));
114 return -1;
117 static int rpc_conf_drop_usage(struct net_context *c, int argc,
118 const char **argv)
120 d_printf("%s\nnet rpc conf drop\n", _("Usage:"));
121 return -1;
124 static int rpc_conf_getparm_usage(struct net_context *c, int argc,
125 const char **argv)
127 d_printf("%s\nnet rpc conf getparm <sharename> <parameter>\n",
128 _("Usage:"));
129 return -1;
132 static int rpc_conf_setparm_usage(struct net_context *c, int argc,
133 const char **argv)
135 d_printf("%s\n%s",
136 _("Usage:"),
137 _(" net rpc conf setparm <section> <param> <value>\n"));
138 return -1;
141 static int rpc_conf_delparm_usage(struct net_context *c, int argc,
142 const char **argv)
144 d_printf("%s\nnet rpc conf delparm <sharename> <parameter>\n",
145 _("Usage:"));
146 return -1;
149 static int rpc_conf_getincludes_usage(struct net_context *c, int argc,
150 const char **argv)
152 d_printf("%s\nnet rpc conf getincludes <sharename>\n",
153 _("Usage:"));
154 return -1;
157 static int rpc_conf_setincludes_usage(struct net_context *c, int argc,
158 const char **argv)
160 d_printf("%s\nnet rpc conf setincludes <sharename> [<filename>]*\n",
161 _("Usage:"));
162 return -1;
165 static int rpc_conf_delincludes_usage(struct net_context *c, int argc,
166 const char **argv)
168 d_printf("%s\nnet rpc conf delincludes <sharename>\n",
169 _("Usage:"));
170 return -1;
173 /**********************************************************
175 * helper functions
177 **********************************************************/
180 * The function deletes a registry value with the name 'value' from the share
181 * with the name 'share_name'. 'parent_hnd' is the handle for the smbconf key.
183 static NTSTATUS rpc_conf_del_value(TALLOC_CTX *mem_ctx,
184 struct dcerpc_binding_handle *b,
185 struct policy_handle *parent_hnd,
186 const char *share_name,
187 const char *value,
188 WERROR *werr)
191 TALLOC_CTX *frame = talloc_stackframe();
192 NTSTATUS status = NT_STATUS_OK;
193 WERROR result = WERR_OK;
194 WERROR _werr;
196 struct winreg_String keyname, valuename;
197 struct policy_handle child_hnd;
199 ZERO_STRUCT(child_hnd);
200 ZERO_STRUCT(keyname);
201 ZERO_STRUCT(valuename);
203 keyname.name = share_name;
204 valuename.name = value;
206 status = dcerpc_winreg_OpenKey(b, frame, parent_hnd, keyname, 0,
207 REG_KEY_WRITE, &child_hnd, &result);
209 if (!(NT_STATUS_IS_OK(status))) {
210 d_fprintf(stderr, _("Failed to open key '%s': %s\n"),
211 keyname.name, nt_errstr(status));
212 goto error;
215 if (!(W_ERROR_IS_OK(result))) {
216 d_fprintf(stderr, _("Failed to open key '%s': %s\n"),
217 keyname.name, win_errstr(result));
218 goto error;
221 status = dcerpc_winreg_DeleteValue(b,
222 frame,
223 &child_hnd,
224 valuename,
225 &result);
227 if (!(NT_STATUS_IS_OK(status))) {
228 d_fprintf(stderr, _("Failed to delete value %s\n"),
229 nt_errstr(status));
230 goto error;
233 if (!(W_ERROR_IS_OK(result))) {
234 if (W_ERROR_EQUAL(result, WERR_BADFILE)){
235 result = WERR_OK;
236 goto error;
239 d_fprintf(stderr, _("Failed to delete value %s\n"),
240 win_errstr(result));
241 goto error;
244 error:
245 *werr = result;
247 dcerpc_winreg_CloseKey(b, frame, &child_hnd, &_werr);
249 TALLOC_FREE(frame);
250 return status;;
255 * The function sets a share in the registry with the parameters
256 * held in the smbconf_service struct
258 static NTSTATUS rpc_conf_set_share(TALLOC_CTX *mem_ctx,
259 struct dcerpc_binding_handle *b,
260 struct policy_handle *parent_hnd,
261 struct smbconf_service *service,
262 WERROR *werr)
264 TALLOC_CTX *frame = talloc_stackframe();
266 NTSTATUS status = NT_STATUS_OK;
267 WERROR result = WERR_OK;
268 WERROR _werr;
269 enum winreg_CreateAction action;
270 uint32_t i, j;
272 const char **includes;
274 struct winreg_String wkey, wkeyclass;
275 struct policy_handle share_hnd;
277 ZERO_STRUCT(share_hnd);
278 ZERO_STRUCT(wkey);
279 ZERO_STRUCT(wkeyclass);
281 wkey.name = service->name;
282 wkeyclass.name = "";
283 action = REG_ACTION_NONE;
285 status = dcerpc_winreg_CreateKey(b,
286 frame,
287 parent_hnd,
288 wkey,
289 wkeyclass,
291 REG_KEY_ALL,
292 NULL,
293 &share_hnd,
294 &action,
295 &result);
297 if (!NT_STATUS_IS_OK(status)) {
298 d_printf("winreg_CreateKey: Could not create smbconf key\n");
299 goto error;
302 if (!W_ERROR_IS_OK(result)) {
303 d_printf("winreg_CreateKey: Could not create smbconf key\n");
304 goto error;
307 for (i = 0; i < service->num_params; i++) {
308 if (strequal(service->param_names[i], "include") == 0)
311 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
312 service->param_names[i],
313 service->param_values[i],
314 &result);
316 if (!(NT_STATUS_IS_OK(status))) {
317 d_fprintf(stderr,
318 "ERROR: Share: '%s'\n"
319 "Could not set parameter '%s'"
320 " with value %s\n %s\n",
321 service->name,
322 service->param_names[i],
323 service->param_values[i],
324 nt_errstr(status));
325 goto error;
328 if (!(W_ERROR_IS_OK(result))) {
329 d_fprintf(stderr,
330 "ERROR: Share: '%s'\n"
331 "Could not set parameter '%s'"
332 " with value %s\n %s\n",
333 service->name,
334 service->param_names[i],
335 service->param_values[i],
336 win_errstr(result));
337 goto error;
339 } else {
341 includes = talloc_zero_array(frame,
342 const char *,
343 service->num_params + 1);
344 if (includes == NULL) {
345 result = WERR_NOMEM;
346 d_fprintf(stderr, "ERROR: out of memory\n");
347 goto error;
350 for (j = i; j < service->num_params; j++) {
352 includes[j - i] = talloc_strdup(
353 frame,
354 service->param_values[j]);
356 if (includes[j-i] == NULL) {
357 result = WERR_NOMEM;
358 d_fprintf(stderr, "ERROR: out of memory\n");
359 goto error;
363 status = dcerpc_winreg_set_multi_sz(frame, b, &share_hnd,
364 "includes",
365 includes,
366 &result);
368 if (!(NT_STATUS_IS_OK(status))) {
369 d_fprintf(stderr, "ERROR: Share: '%s'\n"
370 "Could not set includes\n %s\n",
371 service->name,
372 nt_errstr(status));
373 goto error;
376 if (!(W_ERROR_IS_OK(result))) {
377 d_fprintf(stderr, "ERROR: Share: '%s'\n"
378 "Could not set includes\n %s\n",
379 service->name,
380 win_errstr(result));
381 goto error;
384 i = service->num_params;
388 error:
389 /* in case of error, should it delete the created key? */
390 if (!(W_ERROR_IS_OK(result))) {
391 status = werror_to_ntstatus(result);
395 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
397 TALLOC_FREE(frame);
398 return status;
403 * The function opens the registry database and retrieves
404 * as a smbconf_service struct the share with the name
405 * 'share_name'
407 static NTSTATUS rpc_conf_get_share(TALLOC_CTX *mem_ctx,
408 struct dcerpc_binding_handle *b,
409 struct policy_handle *parent_hnd,
410 const char *share_name,
411 struct smbconf_service *share,
412 WERROR *werr)
414 TALLOC_CTX *frame = talloc_stackframe();
416 NTSTATUS status = NT_STATUS_OK;
417 WERROR result = WERR_OK;
418 WERROR _werr;
419 struct policy_handle child_hnd;
420 int32_t includes_cnt, includes_idx = -1;
421 uint32_t num_vals, i, param_cnt = 0;
422 const char **val_names;
423 enum winreg_Type *types;
424 DATA_BLOB *data;
425 struct winreg_String key = { 0, };
426 const char **multi_s = NULL;
427 const char *s = NULL;
428 struct smbconf_service tmp_share;
430 ZERO_STRUCT(tmp_share);
432 key.name = share_name;
433 status = dcerpc_winreg_OpenKey(b, frame, parent_hnd, key, 0,
434 REG_KEY_READ, &child_hnd, &result);
436 if (!(NT_STATUS_IS_OK(status))) {
437 d_fprintf(stderr, _("Failed to open subkey: %s\n"),
438 nt_errstr(status));
439 goto error;
441 if (!(W_ERROR_IS_OK(result))) {
442 d_fprintf(stderr, _("Failed to open subkey: %s\n"),
443 win_errstr(result));
444 goto error;
446 /* get all the info from the share key */
447 status = dcerpc_winreg_enumvals(frame,
449 &child_hnd,
450 &num_vals,
451 &val_names,
452 &types,
453 &data,
454 &result);
456 if (!(NT_STATUS_IS_OK(status))) {
457 d_fprintf(stderr, _("Failed to enumerate values: %s\n"),
458 nt_errstr(status));
459 goto error;
461 if (!(W_ERROR_IS_OK(result))) {
462 d_fprintf(stderr, _("Failed to enumerate values: %s\n"),
463 win_errstr(result));
464 goto error;
466 /* check for includes */
467 for (i = 0; i < num_vals; i++) {
468 if (strcmp(val_names[i], "includes") == 0){
469 if (!pull_reg_multi_sz(frame,
470 &data[i],
471 &multi_s))
473 result = WERR_NOMEM;
474 d_fprintf(stderr,
475 _("Failed to enumerate values: %s\n"),
476 win_errstr(result));
477 goto error;
479 includes_idx = i;
482 /* count the number of includes */
483 includes_cnt = 0;
484 if (includes_idx != -1) {
485 for (includes_cnt = 0;
486 multi_s[includes_cnt] != NULL;
487 includes_cnt ++);
489 /* place the name of the share in the smbconf_service struct */
490 tmp_share.name = talloc_strdup(frame, share_name);
491 if (tmp_share.name == NULL) {
492 result = WERR_NOMEM;
493 d_fprintf(stderr, _("Failed to create share: %s\n"),
494 win_errstr(result));
495 goto error;
497 /* place the number of parameters in the smbconf_service struct */
498 tmp_share.num_params = num_vals;
499 if (includes_idx != -1) {
500 tmp_share.num_params = num_vals + includes_cnt - 1;
502 /* allocate memory for the param_names and param_values lists */
503 tmp_share.param_names = talloc_zero_array(frame, char *, tmp_share.num_params);
504 if (tmp_share.param_names == NULL) {
505 result = WERR_NOMEM;
506 d_fprintf(stderr, _("Failed to create share: %s\n"),
507 win_errstr(result));
508 goto error;
510 tmp_share.param_values = talloc_zero_array(frame, char *, tmp_share.num_params);
511 if (tmp_share.param_values == NULL) {
512 result = WERR_NOMEM;
513 d_fprintf(stderr, _("Failed to create share: %s\n"),
514 win_errstr(result));
515 goto error;
517 /* place all params except includes */
518 for (i = 0; i < num_vals; i++) {
519 if (strcmp(val_names[i], "includes") != 0) {
520 if (!pull_reg_sz(frame, &data[i], &s)) {
521 result = WERR_NOMEM;
522 d_fprintf(stderr,
523 _("Failed to enumerate values: %s\n"),
524 win_errstr(result));
525 goto error;
527 /* place param_names */
528 tmp_share.param_names[param_cnt] = talloc_strdup(frame, val_names[i]);
529 if (tmp_share.param_names[param_cnt] == NULL) {
530 result = WERR_NOMEM;
531 d_fprintf(stderr, _("Failed to create share: %s\n"),
532 win_errstr(result));
533 goto error;
536 /* place param_values */
537 tmp_share.param_values[param_cnt++] = talloc_strdup(frame, s);
538 if (tmp_share.param_values[param_cnt - 1] == NULL) {
539 result = WERR_NOMEM;
540 d_fprintf(stderr, _("Failed to create share: %s\n"),
541 win_errstr(result));
542 goto error;
546 /* place the includes last */
547 for (i = 0; i < includes_cnt; i++) {
548 tmp_share.param_names[param_cnt] = talloc_strdup(frame, "include");
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 tmp_share.param_values[param_cnt++] = talloc_strdup(frame, multi_s[i]);
557 if (tmp_share.param_values[param_cnt - 1] == NULL) {
558 result = WERR_NOMEM;
559 d_fprintf(stderr, _("Failed to create share: %s\n"),
560 win_errstr(result));
561 goto error;
565 /* move everything to the main memory ctx */
566 for (i = 0; i < param_cnt; i++) {
567 tmp_share.param_names[i] = talloc_move(mem_ctx, &tmp_share.param_names[i]);
568 tmp_share.param_values[i] = talloc_move(mem_ctx, &tmp_share.param_values[i]);
571 tmp_share.name = talloc_move(mem_ctx, &tmp_share.name);
572 tmp_share.param_names = talloc_move(mem_ctx, &tmp_share.param_names);
573 tmp_share.param_values = talloc_move(mem_ctx, &tmp_share.param_values);
574 /* out parameter */
575 *share = tmp_share;
576 error:
577 /* close child */
578 dcerpc_winreg_CloseKey(b, frame, &child_hnd, &_werr);
579 *werr = result;
580 TALLOC_FREE(frame);
581 return status;
585 * The function prints the shares held as smbconf_service structs
586 * in a smbconf file format.
588 static int rpc_conf_print_shares(uint32_t num_shares,
589 struct smbconf_service *shares)
592 uint32_t share_count, param_count;
593 const char *indent = "\t";
595 if (num_shares == 0) {
596 return 0;
599 for (share_count = 0; share_count < num_shares; share_count++) {
600 d_printf("\n");
601 if (shares[share_count].name != NULL) {
602 d_printf("[%s]\n", shares[share_count].name);
605 for (param_count = 0;
606 param_count < shares[share_count].num_params;
607 param_count++)
609 d_printf("%s%s = %s\n",
610 indent,
611 shares[share_count].param_names[param_count],
612 shares[share_count].param_values[param_count]);
615 d_printf("\n");
617 return 0;
622 * The function openes the registry key
623 * HKLM/Software/Samba/smbconf with the give access_mask
625 static NTSTATUS rpc_conf_open_conf(TALLOC_CTX *mem_ctx,
626 struct dcerpc_binding_handle *b,
627 uint32_t access_mask,
628 struct policy_handle *hive_hnd,
629 struct policy_handle *key_hnd,
630 WERROR *werr)
632 TALLOC_CTX *frame = talloc_stackframe();
633 NTSTATUS status = NT_STATUS_OK;
634 WERROR result = WERR_OK;
635 WERROR _werr;
636 struct policy_handle tmp_hive_hnd, tmp_key_hnd;
637 struct winreg_String key;
639 ZERO_STRUCT(key);
641 status = dcerpc_winreg_OpenHKLM(b, frame, NULL,
642 access_mask, &tmp_hive_hnd, &result);
645 * print no error messages if it is a read only open
646 * and key does not exist
647 * error still gets returned
650 if (access_mask == REG_KEY_READ &&
651 W_ERROR_EQUAL(result, WERR_BADFILE))
653 goto error;
656 if (!(NT_STATUS_IS_OK(status))) {
657 d_fprintf(stderr, _("Failed to open hive: %s\n"),
658 nt_errstr(status));
659 goto error;
661 if (!W_ERROR_IS_OK(result)) {
662 d_fprintf(stderr, _("Failed to open hive: %s\n"),
663 win_errstr(result));
664 goto error;
667 key.name = confpath;
668 status = dcerpc_winreg_OpenKey(b, frame, &tmp_hive_hnd, key, 0,
669 access_mask, &tmp_key_hnd, &result);
672 * print no error messages if it is a read only open
673 * and key does not exist
674 * error still gets returned
677 if (access_mask == REG_KEY_READ &&
678 W_ERROR_EQUAL(result, WERR_BADFILE))
680 goto error;
683 if (!(NT_STATUS_IS_OK(status))) {
684 d_fprintf(stderr, _("Failed to open smbconf key: %s\n"),
685 nt_errstr(status));
686 dcerpc_winreg_CloseKey(b, frame, &tmp_hive_hnd, &_werr);
687 goto error;
689 if (!(W_ERROR_IS_OK(result))) {
690 d_fprintf(stderr, _("Failed to open smbconf key: %s\n"),
691 win_errstr(result));
692 dcerpc_winreg_CloseKey(b, frame, &tmp_hive_hnd, &_werr);
693 goto error;
696 *hive_hnd = tmp_hive_hnd;
697 *key_hnd = tmp_key_hnd;
699 error:
700 TALLOC_FREE(frame);
701 *werr = result;
703 return status;
706 /**********************************************************
708 * internal functions that provide the functionality
709 * net rpc conf
711 **********************************************************/
713 static NTSTATUS rpc_conf_listshares_internal(struct net_context *c,
714 const struct dom_sid *domain_sid,
715 const char *domain_name,
716 struct cli_state *cli,
717 struct rpc_pipe_client *pipe_hnd,
718 TALLOC_CTX *mem_ctx,
719 int argc,
720 const char **argv )
723 TALLOC_CTX *frame = talloc_stackframe();
724 NTSTATUS status = NT_STATUS_OK;
725 WERROR werr = WERR_OK;
726 WERROR _werr;
728 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
730 /* key info */
731 struct policy_handle hive_hnd, key_hnd;
732 uint32_t num_subkeys;
733 uint32_t i;
734 const char **subkeys = NULL;
737 ZERO_STRUCT(hive_hnd);
738 ZERO_STRUCT(key_hnd);
741 if (argc != 0 || c->display_usage) {
742 rpc_conf_listshares_usage(c, argc, argv);
743 status = NT_STATUS_INVALID_PARAMETER;
744 goto error;
748 status = rpc_conf_open_conf(frame,
750 REG_KEY_READ,
751 &hive_hnd,
752 &key_hnd,
753 &werr);
755 if (!(NT_STATUS_IS_OK(status))) {
756 goto error;
759 if (!(W_ERROR_IS_OK(werr))) {
760 goto error;
763 status = dcerpc_winreg_enum_keys(frame,
765 &key_hnd,
766 &num_subkeys,
767 &subkeys,
768 &werr);
770 if (!(NT_STATUS_IS_OK(status))) {
771 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
772 nt_errstr(status));
773 goto error;
776 if (!(W_ERROR_IS_OK(werr))) {
777 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
778 win_errstr(werr));
779 goto error;
782 for (i = 0; i < num_subkeys; i++) {
783 d_printf("%s\n", subkeys[i]);
786 error:
787 if (!(W_ERROR_IS_OK(werr))) {
788 status = werror_to_ntstatus(werr);
791 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
792 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
794 TALLOC_FREE(frame);
795 return status;;
798 static NTSTATUS rpc_conf_delshare_internal(struct net_context *c,
799 const struct dom_sid *domain_sid,
800 const char *domain_name,
801 struct cli_state *cli,
802 struct rpc_pipe_client *pipe_hnd,
803 TALLOC_CTX *mem_ctx,
804 int argc,
805 const char **argv )
808 TALLOC_CTX *frame = talloc_stackframe();
809 NTSTATUS status = NT_STATUS_OK;
810 WERROR werr = WERR_OK;
811 WERROR _werr;
813 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
815 /* key info */
816 struct policy_handle hive_hnd, key_hnd;
818 ZERO_STRUCT(hive_hnd);
819 ZERO_STRUCT(key_hnd);
822 if (argc != 1 || c->display_usage) {
823 rpc_conf_delshare_usage(c, argc, argv);
824 status = NT_STATUS_INVALID_PARAMETER;
825 goto error;
828 status = rpc_conf_open_conf(frame,
830 REG_KEY_ALL,
831 &hive_hnd,
832 &key_hnd,
833 &werr);
835 if (!(NT_STATUS_IS_OK(status))) {
836 goto error;
839 if (!(W_ERROR_IS_OK(werr))) {
840 goto error;
843 status = dcerpc_winreg_delete_subkeys_recursive(frame,
845 &key_hnd,
846 REG_KEY_ALL,
847 argv[0],
848 &werr);
850 if (!NT_STATUS_IS_OK(status)) {
851 d_fprintf(stderr,
852 "winreg_delete_subkeys: Could not delete key %s: %s\n",
853 argv[0], nt_errstr(status));
854 goto error;
857 if (W_ERROR_EQUAL(werr, WERR_BADFILE)){
858 d_fprintf(stderr, _("ERROR: Key does not exist\n"));
862 if (!W_ERROR_IS_OK(werr)) {
863 d_fprintf(stderr,
864 "winreg_delete_subkeys: Could not delete key %s: %s\n",
865 argv[0], win_errstr(werr));
866 goto error;
869 error:
870 if (!(W_ERROR_IS_OK(werr))) {
871 status = werror_to_ntstatus(werr);
874 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
875 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
877 TALLOC_FREE(frame);
879 return status;
882 static NTSTATUS rpc_conf_list_internal(struct net_context *c,
883 const struct dom_sid *domain_sid,
884 const char *domain_name,
885 struct cli_state *cli,
886 struct rpc_pipe_client *pipe_hnd,
887 TALLOC_CTX *mem_ctx,
888 int argc,
889 const char **argv )
892 TALLOC_CTX *frame = talloc_stackframe();
893 NTSTATUS status = NT_STATUS_OK;
894 WERROR werr = WERR_OK;
895 WERROR _werr;
897 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
899 /* key info */
900 struct policy_handle hive_hnd, key_hnd;
901 uint32_t num_subkeys;
902 uint32_t i;
903 struct smbconf_service *shares;
904 const char **subkeys = NULL;
907 ZERO_STRUCT(hive_hnd);
908 ZERO_STRUCT(key_hnd);
911 if (argc != 0 || c->display_usage) {
912 rpc_conf_list_usage(c, argc, argv);
913 status = NT_STATUS_INVALID_PARAMETER;
914 goto error;
917 status = rpc_conf_open_conf(frame,
919 REG_KEY_READ,
920 &hive_hnd,
921 &key_hnd,
922 &werr);
924 if (!(NT_STATUS_IS_OK(status))) {
925 goto error;
928 if (!(W_ERROR_IS_OK(werr))) {
929 goto error;
932 status = dcerpc_winreg_enum_keys(frame,
934 &key_hnd,
935 &num_subkeys,
936 &subkeys,
937 &werr);
939 if (!(NT_STATUS_IS_OK(status))) {
940 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
941 nt_errstr(status));
942 goto error;
945 if (!(W_ERROR_IS_OK(werr))) {
946 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
947 win_errstr(werr));
948 goto error;
951 if (num_subkeys == 0) {
952 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
953 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
954 TALLOC_FREE(frame);
955 return NT_STATUS_OK;
958 /* get info from each subkey */
959 shares = talloc_zero_array(frame, struct smbconf_service, num_subkeys);
960 if (shares == NULL) {
961 werr = WERR_NOMEM;
962 d_fprintf(stderr, _("Failed to create shares: %s\n"),
963 win_errstr(werr));
964 goto error;
968 for (i = 0; i < num_subkeys; i++) {
969 /* get each share and place it in the shares array */
970 status = rpc_conf_get_share(frame,
972 &key_hnd,
973 subkeys[i],
974 &shares[i],
975 &werr);
976 if (!(NT_STATUS_IS_OK(status))) {
977 goto error;
979 if (!(W_ERROR_IS_OK(werr))) {
980 goto error;
984 /* print the shares array */
985 rpc_conf_print_shares(num_subkeys, shares);
987 error:
988 if (!(W_ERROR_IS_OK(werr))) {
989 status = werror_to_ntstatus(werr);
992 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
993 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
995 TALLOC_FREE(frame);
996 return status;
1000 static NTSTATUS rpc_conf_drop_internal(struct net_context *c,
1001 const struct dom_sid *domain_sid,
1002 const char *domain_name,
1003 struct cli_state *cli,
1004 struct rpc_pipe_client *pipe_hnd,
1005 TALLOC_CTX *mem_ctx,
1006 int argc,
1007 const char **argv )
1009 TALLOC_CTX *frame = talloc_stackframe();
1010 NTSTATUS status = NT_STATUS_OK;
1011 WERROR werr = WERR_OK;
1012 WERROR _werr;
1014 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1016 /* key info */
1017 struct policy_handle hive_hnd, key_hnd;
1018 const char *keyname = confpath;
1019 struct winreg_String wkey, wkeyclass;
1020 enum winreg_CreateAction action = REG_ACTION_NONE;
1023 ZERO_STRUCT(hive_hnd);
1024 ZERO_STRUCT(key_hnd);
1027 if (argc != 0 || c->display_usage) {
1028 rpc_conf_drop_usage(c, argc, argv);
1029 status = NT_STATUS_INVALID_PARAMETER;
1030 goto error;
1033 status = rpc_conf_open_conf(frame,
1035 REG_KEY_ALL,
1036 &hive_hnd,
1037 &key_hnd,
1038 &werr);
1040 if (!(NT_STATUS_IS_OK(status))) {
1041 goto error;
1044 if (!(W_ERROR_IS_OK(werr))) {
1045 goto error;
1048 status = dcerpc_winreg_delete_subkeys_recursive(frame,
1050 &hive_hnd,
1051 REG_KEY_ALL,
1052 keyname,
1053 &werr);
1055 if (!NT_STATUS_IS_OK(status)) {
1056 d_printf("winreg_delete_subkeys: Could not delete key %s: %s\n",
1057 keyname, nt_errstr(status));
1058 goto error;
1061 if (!W_ERROR_IS_OK(werr)) {
1062 d_printf("winreg_delete_subkeys: Could not delete key %s: %s\n",
1063 keyname, win_errstr(werr));
1064 goto error;
1067 ZERO_STRUCT(wkey);
1068 wkey.name = keyname;
1069 ZERO_STRUCT(wkeyclass);
1070 wkeyclass.name = "";
1071 action = REG_ACTION_NONE;
1073 status = dcerpc_winreg_CreateKey(b,
1074 frame,
1075 &hive_hnd,
1076 wkey,
1077 wkeyclass,
1079 REG_KEY_ALL,
1080 NULL,
1081 &key_hnd,
1082 &action,
1083 &werr);
1085 if (!NT_STATUS_IS_OK(status)) {
1086 d_printf("winreg_CreateKey: Could not create smbconf key\n");
1087 goto error;
1090 if (!W_ERROR_IS_OK(werr)) {
1091 d_printf("winreg_CreateKey: Could not create smbconf key\n");
1092 goto error;
1096 error:
1097 if (!(W_ERROR_IS_OK(werr))) {
1098 status = werror_to_ntstatus(werr);
1101 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1102 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1104 TALLOC_FREE(frame);
1105 return status;
1108 static NTSTATUS rpc_conf_import_internal(struct net_context *c,
1109 const struct dom_sid *domain_sid,
1110 const char *domain_name,
1111 struct cli_state *cli,
1112 struct rpc_pipe_client *pipe_hnd,
1113 TALLOC_CTX *mem_ctx,
1114 int argc,
1115 const char **argv )
1118 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1120 struct policy_handle hive_hnd, key_hnd;
1122 const char *filename = NULL;
1123 const char *servicename = NULL;
1124 char *conf_source = NULL;
1125 TALLOC_CTX *frame;
1126 struct smbconf_ctx *txt_ctx;
1127 struct smbconf_service *service = NULL;
1128 struct smbconf_service **services = NULL;
1129 uint32_t num_shares, i;
1130 sbcErr err = SBC_ERR_UNKNOWN_FAILURE;
1132 WERROR werr = WERR_OK;
1133 NTSTATUS status = NT_STATUS_OK;
1135 ZERO_STRUCT(hive_hnd);
1136 ZERO_STRUCT(key_hnd);
1138 frame = talloc_stackframe();
1140 if (c->display_usage) {
1141 rpc_conf_import_usage(c, argc, argv);
1142 status = NT_STATUS_INVALID_PARAMETER;
1143 goto error;
1146 switch (argc) {
1147 case 0:
1148 default:
1149 rpc_conf_import_usage(c, argc, argv);
1150 status = NT_STATUS_INVALID_PARAMETER;
1151 goto error;
1152 case 2:
1153 servicename = talloc_strdup(frame, argv[1]);
1154 if (servicename == NULL) {
1155 d_printf(_("error: out of memory!\n"));
1156 goto error;
1158 case 1:
1159 filename = argv[0];
1160 break;
1163 DEBUG(3,("rpc_conf_import: reading configuration from file %s.\n",
1164 filename));
1166 conf_source = talloc_asprintf(frame, "file:%s", filename);
1167 if (conf_source == NULL) {
1168 d_fprintf(stderr, _("error: out of memory!\n"));
1169 goto error;
1172 err = smbconf_init(frame, &txt_ctx, conf_source);
1173 if (!SBC_ERROR_IS_OK(err)) {
1174 d_fprintf(stderr, _("error loading file '%s': %s\n"), filename,
1175 sbcErrorString(err));
1176 goto error;
1179 if (c->opt_testmode) {
1180 d_printf(_("\nTEST MODE - "
1181 "would import the following configuration:\n\n"));
1184 if (servicename != NULL) {
1185 err = smbconf_get_share(txt_ctx, frame,
1186 servicename,
1187 &service);
1188 if (!SBC_ERROR_IS_OK(err)) {
1189 goto error;
1192 num_shares = 1;
1194 } else {
1196 err = smbconf_get_config(txt_ctx, frame,
1197 &num_shares,
1198 &services);
1199 if (!SBC_ERROR_IS_OK(err)) {
1200 goto error;
1204 if (c->opt_testmode) {
1205 if (servicename != NULL) {
1206 rpc_conf_print_shares(1, service);
1208 for (i = 0; i < num_shares; i++) {
1209 rpc_conf_print_shares(1, services[i]);
1211 goto error;
1214 status = rpc_conf_drop_internal(c,
1215 domain_sid,
1216 domain_name,
1217 cli,
1218 pipe_hnd,
1219 frame,
1221 NULL );
1223 if (!(NT_STATUS_IS_OK(status))) {
1224 goto error;
1227 status = rpc_conf_open_conf(frame,
1229 REG_KEY_READ,
1230 &hive_hnd,
1231 &key_hnd,
1232 &werr);
1234 if (!(NT_STATUS_IS_OK(status))) {
1235 goto error;
1238 if (!(W_ERROR_IS_OK(werr))) {
1239 goto error;
1242 if (servicename != NULL) {
1243 status = rpc_conf_set_share(frame,
1245 &key_hnd,
1246 service,
1247 &werr);
1249 if (!(NT_STATUS_IS_OK(status))) {
1250 goto error;
1253 if (!(W_ERROR_IS_OK(werr))) {
1254 goto error;
1257 } else {
1259 for (i = 0; i < num_shares; i++) {
1260 status = rpc_conf_set_share(frame,
1262 &key_hnd,
1263 services[i],
1264 &werr);
1266 if (!(NT_STATUS_IS_OK(status))) {
1267 goto error;
1270 if (!(W_ERROR_IS_OK(werr))) {
1271 goto error;
1277 error:
1278 if (!SBC_ERROR_IS_OK(err)) {
1279 d_fprintf(stderr, "ERROR: %s\n", sbcErrorString(err));
1282 if (!(W_ERROR_IS_OK(werr))) {
1283 status = werror_to_ntstatus(werr);
1285 TALLOC_FREE(frame);
1286 return status;
1289 static NTSTATUS rpc_conf_showshare_internal(struct net_context *c,
1290 const struct dom_sid *domain_sid,
1291 const char *domain_name,
1292 struct cli_state *cli,
1293 struct rpc_pipe_client *pipe_hnd,
1294 TALLOC_CTX *mem_ctx,
1295 int argc,
1296 const char **argv )
1298 TALLOC_CTX *frame = talloc_stackframe();
1299 NTSTATUS status = NT_STATUS_OK;
1300 WERROR werr = WERR_OK;
1301 WERROR _werr;
1303 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1305 /* key info */
1306 struct policy_handle hive_hnd, key_hnd;
1307 struct smbconf_service *service = NULL;
1308 const char *sharename = NULL;
1311 ZERO_STRUCT(hive_hnd);
1312 ZERO_STRUCT(key_hnd);
1315 if (argc != 1 || c->display_usage) {
1316 rpc_conf_showshare_usage(c, argc, argv);
1317 status = NT_STATUS_INVALID_PARAMETER;
1318 goto error;
1321 status = rpc_conf_open_conf(frame,
1323 REG_KEY_READ,
1324 &hive_hnd,
1325 &key_hnd,
1326 &werr);
1328 if (!(NT_STATUS_IS_OK(status))) {
1329 goto error;
1332 if (!(W_ERROR_IS_OK(werr))) {
1333 goto error;
1336 sharename = talloc_strdup(frame, argv[0]);
1337 if (sharename == NULL) {
1338 werr = WERR_NOMEM;
1339 d_fprintf(stderr, _("Failed to create share: %s\n"),
1340 win_errstr(werr));
1341 goto error;
1344 service = talloc(frame, struct smbconf_service);
1345 if (service == NULL) {
1346 werr = WERR_NOMEM;
1347 d_fprintf(stderr, _("Failed to create share: %s\n"),
1348 win_errstr(werr));
1349 goto error;
1352 status = rpc_conf_get_share(frame,
1354 &key_hnd,
1355 sharename,
1356 service,
1357 &werr);
1359 if (!(NT_STATUS_IS_OK(status))) {
1360 goto error;
1362 if (!(W_ERROR_IS_OK(werr))) {
1363 goto error;
1366 rpc_conf_print_shares(1, service);
1368 error:
1369 if (!(W_ERROR_IS_OK(werr))) {
1370 status = werror_to_ntstatus(werr);
1373 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1374 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1376 TALLOC_FREE(frame);
1377 return status;
1380 static NTSTATUS rpc_conf_addshare_internal(struct net_context *c,
1381 const struct dom_sid *domain_sid,
1382 const char *domain_name,
1383 struct cli_state *cli,
1384 struct rpc_pipe_client *pipe_hnd,
1385 TALLOC_CTX *mem_ctx,
1386 int argc,
1387 const char **argv )
1389 TALLOC_CTX *frame = talloc_stackframe();
1390 NTSTATUS status = NT_STATUS_OK;
1391 WERROR werr = WERR_OK;
1392 WERROR _werr;
1394 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1396 /* key info */
1397 struct policy_handle hive_hnd, key_hnd, share_hnd;
1398 char *sharename = NULL;
1399 const char *path = NULL;
1400 const char *comment = NULL;
1401 const char *guest_ok = "no";
1402 const char *read_only = "yes";
1403 struct winreg_String key, keyclass;
1404 enum winreg_CreateAction action = 0;
1407 ZERO_STRUCT(hive_hnd);
1408 ZERO_STRUCT(key_hnd);
1409 ZERO_STRUCT(share_hnd);
1411 ZERO_STRUCT(key);
1412 ZERO_STRUCT(keyclass);
1414 if (c->display_usage) {
1415 rpc_conf_addshare_usage(c, argc, argv);
1416 status = NT_STATUS_INVALID_PARAMETER;
1417 goto error;
1420 switch (argc) {
1421 case 0:
1422 case 1:
1423 default:
1424 rpc_conf_addshare_usage(c, argc, argv);
1425 status = NT_STATUS_INVALID_PARAMETER;
1426 goto error;
1427 case 5:
1428 comment = argv[4];
1429 case 4:
1430 if (!strnequal(argv[3], "guest_ok=", 9)) {
1431 rpc_conf_addshare_usage(c, argc, argv);
1432 status = NT_STATUS_INVALID_PARAMETER;
1433 goto error;
1435 switch (argv[3][9]) {
1436 case 'y':
1437 case 'Y':
1438 guest_ok = "yes";
1439 break;
1440 case 'n':
1441 case 'N':
1442 guest_ok = "no";
1443 break;
1444 default:
1445 rpc_conf_addshare_usage(c, argc, argv);
1446 status = NT_STATUS_INVALID_PARAMETER;
1447 goto error;
1449 case 3:
1450 if (!strnequal(argv[2], "writeable=", 10)) {
1451 rpc_conf_addshare_usage(c, argc, argv);
1452 status = NT_STATUS_INVALID_PARAMETER;
1453 goto error;
1455 switch (argv[2][10]) {
1456 case 'y':
1457 case 'Y':
1458 read_only = "no";
1459 break;
1460 case 'n':
1461 case 'N':
1462 read_only = "yes";
1463 break;
1464 default:
1465 rpc_conf_addshare_usage(c, argc, argv);
1466 status = NT_STATUS_INVALID_PARAMETER;
1467 goto error;
1469 case 2:
1470 path = argv[1];
1471 sharename = talloc_strdup(frame, argv[0]);
1472 if (sharename == NULL) {
1473 d_printf(_("error: out of memory!\n"));
1474 goto error;
1477 break;
1480 status = rpc_conf_open_conf(frame,
1482 REG_KEY_READ,
1483 &hive_hnd,
1484 &key_hnd,
1485 &werr);
1487 if (!(NT_STATUS_IS_OK(status))) {
1488 goto error;
1491 if (!(W_ERROR_IS_OK(werr))) {
1492 goto error;
1495 key.name = argv[0];
1496 keyclass.name = "";
1498 status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
1499 0, REG_KEY_READ, NULL, &share_hnd,
1500 &action, &werr);
1502 if (!(NT_STATUS_IS_OK(status))) {
1503 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1504 argv[0], nt_errstr(status));
1505 goto error;
1508 if (!W_ERROR_IS_OK(werr)) {
1509 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1510 argv[0], win_errstr(werr));
1511 goto error;
1514 switch (action) {
1515 case REG_ACTION_NONE:
1516 werr = WERR_CREATE_FAILED;
1517 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1518 argv[0], win_errstr(werr));
1519 goto error;
1520 case REG_CREATED_NEW_KEY:
1521 DEBUG(5, ("net rpc conf setincludes:"
1522 "createkey created %s\n", argv[0]));
1523 break;
1524 case REG_OPENED_EXISTING_KEY:
1525 d_fprintf(stderr, _("ERROR: Share '%s' already exists\n"), argv[0]);
1526 status = NT_STATUS_INVALID_PARAMETER;
1527 goto error;
1530 /* set the path parameter */
1531 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1532 "path", path, &werr);
1534 if (!(NT_STATUS_IS_OK(status))) {
1535 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1536 " with value %s\n %s\n",
1537 "path", path, nt_errstr(status));
1538 goto error;
1541 if (!(W_ERROR_IS_OK(werr))) {
1542 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1543 " with value %s\n %s\n",
1544 "path", path, win_errstr(werr));
1545 goto error;
1548 /* set the writeable parameter */
1549 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1550 "read only", read_only, &werr);
1552 if (!(NT_STATUS_IS_OK(status))) {
1553 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1554 " with value %s\n %s\n",
1555 "read only", read_only, nt_errstr(status));
1556 goto error;
1559 if (!(W_ERROR_IS_OK(werr))) {
1560 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1561 " with value %s\n %s\n",
1562 "read only", read_only, win_errstr(werr));
1563 goto error;
1566 /* set the guest ok parameter */
1567 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1568 "guest ok", guest_ok, &werr);
1570 if (!(NT_STATUS_IS_OK(status))) {
1571 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1572 " with value %s\n %s\n",
1573 "guest ok", guest_ok, nt_errstr(status));
1574 goto error;
1577 if (!(W_ERROR_IS_OK(werr))) {
1578 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1579 " with value %s\n %s\n",
1580 "guest ok", guest_ok, win_errstr(werr));
1581 goto error;
1584 if (argc == 5) {
1585 /* set the comment parameter */
1586 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1587 "comment", comment, &werr);
1589 if (!(NT_STATUS_IS_OK(status))) {
1590 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1591 " with value %s\n %s\n",
1592 "comment", comment, nt_errstr(status));
1593 goto error;
1596 if (!(W_ERROR_IS_OK(werr))) {
1597 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1598 " with value %s\n %s\n",
1599 "comment", comment, win_errstr(werr));
1600 goto error;
1603 error:
1604 if (!(W_ERROR_IS_OK(werr))) {
1605 status = werror_to_ntstatus(werr);
1608 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1609 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1610 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
1612 TALLOC_FREE(frame);
1613 return status;
1616 static NTSTATUS rpc_conf_getparm_internal(struct net_context *c,
1617 const struct dom_sid *domain_sid,
1618 const char *domain_name,
1619 struct cli_state *cli,
1620 struct rpc_pipe_client *pipe_hnd,
1621 TALLOC_CTX *mem_ctx,
1622 int argc,
1623 const char **argv )
1625 TALLOC_CTX *frame = talloc_stackframe();
1626 NTSTATUS status = NT_STATUS_OK;
1627 WERROR werr = WERR_OK;
1628 WERROR _werr;
1630 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1632 /* key info */
1633 struct policy_handle hive_hnd, key_hnd;
1634 struct smbconf_service *service = NULL;
1636 bool param_is_set = false;
1637 uint32_t param_count;
1639 ZERO_STRUCT(hive_hnd);
1640 ZERO_STRUCT(key_hnd);
1643 if (argc != 2 || c->display_usage) {
1644 rpc_conf_getparm_usage(c, argc, argv);
1645 status = NT_STATUS_INVALID_PARAMETER;
1646 goto error;
1649 status = rpc_conf_open_conf(frame,
1651 REG_KEY_READ,
1652 &hive_hnd,
1653 &key_hnd,
1654 &werr);
1656 if (!(NT_STATUS_IS_OK(status))) {
1657 goto error;
1660 if (!(W_ERROR_IS_OK(werr))) {
1661 goto error;
1665 service = talloc(frame, struct smbconf_service);
1667 status = rpc_conf_get_share(frame,
1669 &key_hnd,
1670 argv[0],
1671 service,
1672 &werr);
1674 if (!(NT_STATUS_IS_OK(status))) {
1675 goto error;
1678 if (W_ERROR_EQUAL(werr, WERR_BADFILE)) {
1679 d_fprintf(stderr, _("ERROR: Share %s does not exist\n"),
1680 argv[0]);
1681 goto error;
1684 if (!(W_ERROR_IS_OK(werr))) {
1685 goto error;
1688 for (param_count = 0;
1689 param_count < service->num_params;
1690 param_count++)
1692 /* should includes also be printed? */
1693 if (strcmp(service->param_names[param_count], argv[1]) == 0) {
1694 d_printf(_("%s\n"),
1695 service->param_values[param_count]);
1696 param_is_set = true;
1700 if (!param_is_set) {
1701 d_fprintf(stderr, _("ERROR: Given parameter '%s' has not been set\n"),
1702 argv[1]);
1703 werr = WERR_BADFILE;
1704 goto error;
1707 error:
1709 if (!(W_ERROR_IS_OK(werr))) {
1710 status = werror_to_ntstatus(werr);
1713 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1714 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1716 TALLOC_FREE(frame);
1717 return status;
1721 static NTSTATUS rpc_conf_setparm_internal(struct net_context *c,
1722 const struct dom_sid *domain_sid,
1723 const char *domain_name,
1724 struct cli_state *cli,
1725 struct rpc_pipe_client *pipe_hnd,
1726 TALLOC_CTX *mem_ctx,
1727 int argc,
1728 const char **argv )
1730 TALLOC_CTX *frame = talloc_stackframe();
1731 NTSTATUS status = NT_STATUS_OK;
1732 WERROR werr = WERR_OK;
1733 WERROR _werr;
1735 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1737 /* key info */
1738 struct policy_handle hive_hnd, key_hnd, share_hnd;
1740 struct winreg_String key, keyclass;
1741 enum winreg_CreateAction action = 0;
1743 const char *service_name, *param_name, *valstr;
1745 ZERO_STRUCT(hive_hnd);
1746 ZERO_STRUCT(key_hnd);
1747 ZERO_STRUCT(share_hnd);
1749 ZERO_STRUCT(key);
1750 ZERO_STRUCT(keyclass);
1752 if (argc != 3 || c->display_usage) {
1753 rpc_conf_setparm_usage(c, argc, argv);
1754 status = NT_STATUS_INVALID_PARAMETER;
1755 goto error;
1758 status = rpc_conf_open_conf(frame,
1760 REG_KEY_READ,
1761 &hive_hnd,
1762 &key_hnd,
1763 &werr);
1765 if (!(NT_STATUS_IS_OK(status))) {
1766 goto error;
1769 if (!(W_ERROR_IS_OK(werr))) {
1770 goto error;
1773 service_name = argv[0];
1774 param_name = argv[1];
1775 valstr = argv[2];
1777 key.name = service_name;
1778 keyclass.name = "";
1780 status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
1781 0, REG_KEY_READ, NULL, &share_hnd,
1782 &action, &werr);
1784 if (!(NT_STATUS_IS_OK(status))) {
1785 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1786 service_name, nt_errstr(status));
1787 goto error;
1790 if (!W_ERROR_IS_OK(werr)) {
1791 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1792 service_name, win_errstr(werr));
1793 goto error;
1796 switch (action) {
1797 case REG_ACTION_NONE:
1798 werr = WERR_CREATE_FAILED;
1799 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1800 service_name, win_errstr(werr));
1801 goto error;
1802 case REG_CREATED_NEW_KEY:
1803 DEBUG(5, ("net rpc conf setparm:"
1804 "createkey created %s\n", service_name));
1805 break;
1806 case REG_OPENED_EXISTING_KEY:
1807 DEBUG(5, ("net rpc conf setparm:"
1808 "createkey opened existing %s\n",
1809 service_name));
1811 /* delete posibly existing value */
1812 status = rpc_conf_del_value(frame,
1814 &key_hnd,
1815 service_name,
1816 param_name,
1817 &werr);
1819 if (!(NT_STATUS_IS_OK(status))) {
1820 goto error;
1823 if (!(W_ERROR_IS_OK(werr))) {
1824 goto error;
1827 break;
1831 * check if parameter is valid for writing
1834 if (!net_conf_param_valid(service_name, param_name, valstr)) {
1835 werr = WERR_INVALID_PARAM;
1836 goto error;
1839 /* set the parameter */
1840 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1841 param_name, valstr, &werr);
1843 if (!(NT_STATUS_IS_OK(status))) {
1844 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1845 " with value %s\n %s\n",
1846 param_name, valstr, nt_errstr(status));
1847 goto error;
1850 if (!(W_ERROR_IS_OK(werr))) {
1851 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1852 " with value %s\n %s\n",
1853 param_name, valstr, win_errstr(werr));
1854 goto error;
1857 error:
1859 if (!(W_ERROR_IS_OK(werr))) {
1860 status = werror_to_ntstatus(werr);
1863 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1864 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1865 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
1867 TALLOC_FREE(frame);
1868 return status;
1871 static NTSTATUS rpc_conf_delparm_internal(struct net_context *c,
1872 const struct dom_sid *domain_sid,
1873 const char *domain_name,
1874 struct cli_state *cli,
1875 struct rpc_pipe_client *pipe_hnd,
1876 TALLOC_CTX *mem_ctx,
1877 int argc,
1878 const char **argv )
1880 TALLOC_CTX *frame = talloc_stackframe();
1881 NTSTATUS status = NT_STATUS_OK;
1882 WERROR werr = WERR_OK;
1883 WERROR _werr;
1885 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1887 /* key info */
1888 struct policy_handle hive_hnd, key_hnd;
1891 ZERO_STRUCT(hive_hnd);
1892 ZERO_STRUCT(key_hnd);
1895 if (argc != 2 || c->display_usage) {
1896 rpc_conf_delparm_usage(c, argc, argv);
1897 status = NT_STATUS_INVALID_PARAMETER;
1898 goto error;
1901 status = rpc_conf_open_conf(frame,
1903 REG_KEY_READ,
1904 &hive_hnd,
1905 &key_hnd,
1906 &werr);
1908 if (!(NT_STATUS_IS_OK(status))) {
1909 goto error;
1912 if (!(W_ERROR_IS_OK(werr))) {
1913 goto error;
1916 status = rpc_conf_del_value(frame,
1918 &key_hnd,
1919 argv[0],
1920 argv[1],
1921 &werr);
1923 error:
1925 if (!(W_ERROR_IS_OK(werr))) {
1926 status = werror_to_ntstatus(werr);
1929 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1930 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1932 TALLOC_FREE(frame);
1933 return status;
1937 static NTSTATUS rpc_conf_getincludes_internal(struct net_context *c,
1938 const struct dom_sid *domain_sid,
1939 const char *domain_name,
1940 struct cli_state *cli,
1941 struct rpc_pipe_client *pipe_hnd,
1942 TALLOC_CTX *mem_ctx,
1943 int argc,
1944 const char **argv )
1946 TALLOC_CTX *frame = talloc_stackframe();
1947 NTSTATUS status = NT_STATUS_OK;
1948 WERROR werr = WERR_OK;
1949 WERROR _werr;
1951 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1953 /* key info */
1954 struct policy_handle hive_hnd, key_hnd;
1955 struct smbconf_service *service = NULL;
1957 uint32_t param_count;
1960 ZERO_STRUCT(hive_hnd);
1961 ZERO_STRUCT(key_hnd);
1964 if (argc != 1 || c->display_usage) {
1965 rpc_conf_getincludes_usage(c, argc, argv);
1966 status = NT_STATUS_INVALID_PARAMETER;
1967 goto error;
1970 status = rpc_conf_open_conf(frame,
1972 REG_KEY_READ,
1973 &hive_hnd,
1974 &key_hnd,
1975 &werr);
1977 if (!(NT_STATUS_IS_OK(status))) {
1978 goto error;
1981 if (!(W_ERROR_IS_OK(werr))) {
1982 goto error;
1985 service = talloc(frame, struct smbconf_service);
1987 status = rpc_conf_get_share(frame,
1989 &key_hnd,
1990 argv[0],
1991 service,
1992 &werr);
1994 if (!(NT_STATUS_IS_OK(status))) {
1995 goto error;
1998 if (!(W_ERROR_IS_OK(werr))) {
1999 goto error;
2002 for (param_count = 0;
2003 param_count < service->num_params;
2004 param_count++)
2006 if (strcmp(service->param_names[param_count], "include") == 0) {
2007 d_printf(_("%s = %s\n"),
2008 service->param_names[param_count],
2009 service->param_values[param_count]);
2013 error:
2015 if (!(W_ERROR_IS_OK(werr))) {
2016 status = werror_to_ntstatus(werr);
2019 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
2020 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
2022 TALLOC_FREE(frame);
2023 return status;
2027 static NTSTATUS rpc_conf_setincludes_internal(struct net_context *c,
2028 const struct dom_sid *domain_sid,
2029 const char *domain_name,
2030 struct cli_state *cli,
2031 struct rpc_pipe_client *pipe_hnd,
2032 TALLOC_CTX *mem_ctx,
2033 int argc,
2034 const char **argv )
2036 TALLOC_CTX *frame = talloc_stackframe();
2037 NTSTATUS status = NT_STATUS_OK;
2038 WERROR werr = WERR_OK;
2039 WERROR _werr;
2041 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2043 /* key info */
2044 struct policy_handle hive_hnd, key_hnd, share_hnd;
2046 struct winreg_String key, keyclass;
2047 enum winreg_CreateAction action = 0;
2049 ZERO_STRUCT(hive_hnd);
2050 ZERO_STRUCT(key_hnd);
2051 ZERO_STRUCT(share_hnd);
2053 ZERO_STRUCT(key);
2054 ZERO_STRUCT(keyclass);
2056 if (argc < 1 || c->display_usage) {
2057 rpc_conf_setincludes_usage(c, argc, argv);
2058 status = NT_STATUS_INVALID_PARAMETER;
2059 goto error;
2062 status = rpc_conf_open_conf(frame,
2064 REG_KEY_READ,
2065 &hive_hnd,
2066 &key_hnd,
2067 &werr);
2069 if (!(NT_STATUS_IS_OK(status))) {
2070 goto error;
2073 if (!(W_ERROR_IS_OK(werr))) {
2074 goto error;
2077 key.name = argv[0];
2078 keyclass.name = "";
2080 status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
2081 0, REG_KEY_READ, NULL, &share_hnd,
2082 &action, &werr);
2084 if (!(NT_STATUS_IS_OK(status))) {
2085 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
2086 argv[0], nt_errstr(status));
2087 goto error;
2090 if (!W_ERROR_IS_OK(werr)) {
2091 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
2092 argv[0], win_errstr(werr));
2093 goto error;
2096 switch (action) {
2097 case REG_ACTION_NONE:
2098 /* Is there any other way to treat this? */
2099 werr = WERR_CREATE_FAILED;
2100 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
2101 argv[0], win_errstr(werr));
2102 goto error;
2103 case REG_CREATED_NEW_KEY:
2104 DEBUG(5, ("net rpc conf setincludes:"
2105 "createkey created %s\n", argv[0]));
2106 break;
2107 case REG_OPENED_EXISTING_KEY:
2108 DEBUG(5, ("net rpc conf setincludes:"
2109 "createkey opened existing %s\n", argv[0]));
2111 /* delete posibly existing value */
2112 status = rpc_conf_del_value(frame,
2114 &key_hnd,
2115 argv[0],
2116 "includes",
2117 &werr);
2119 if (!(NT_STATUS_IS_OK(status))) {
2120 goto error;
2123 if (!(W_ERROR_IS_OK(werr))) {
2124 goto error;
2126 break;
2129 /* set the 'includes' values */
2130 status = dcerpc_winreg_set_multi_sz(frame, b, &share_hnd,
2131 "includes", argv + 1, &werr);
2132 if (!(NT_STATUS_IS_OK(status))) {
2133 d_fprintf(stderr, "ERROR: Could not set includes\n %s\n",
2134 nt_errstr(status));
2135 goto error;
2138 if (!(W_ERROR_IS_OK(werr))) {
2139 d_fprintf(stderr, "ERROR: Could not set includes\n %s\n",
2140 win_errstr(werr));
2141 goto error;
2144 error:
2146 if (!(W_ERROR_IS_OK(werr))) {
2147 status = werror_to_ntstatus(werr);
2150 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
2151 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
2152 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
2154 TALLOC_FREE(frame);
2155 return status;
2158 static NTSTATUS rpc_conf_delincludes_internal(struct net_context *c,
2159 const struct dom_sid *domain_sid,
2160 const char *domain_name,
2161 struct cli_state *cli,
2162 struct rpc_pipe_client *pipe_hnd,
2163 TALLOC_CTX *mem_ctx,
2164 int argc,
2165 const char **argv )
2167 TALLOC_CTX *frame = talloc_stackframe();
2168 NTSTATUS status = NT_STATUS_OK;
2169 WERROR werr = WERR_OK;
2170 WERROR _werr;
2172 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2174 /* key info */
2175 struct policy_handle hive_hnd, key_hnd;
2178 ZERO_STRUCT(hive_hnd);
2179 ZERO_STRUCT(key_hnd);
2182 if (argc != 1 || c->display_usage) {
2183 rpc_conf_delincludes_usage(c, argc, argv);
2184 status = NT_STATUS_INVALID_PARAMETER;
2185 goto error;
2188 status = rpc_conf_open_conf(frame,
2190 REG_KEY_READ,
2191 &hive_hnd,
2192 &key_hnd,
2193 &werr);
2195 if (!(NT_STATUS_IS_OK(status))) {
2196 goto error;
2199 if (!(W_ERROR_IS_OK(werr))) {
2200 goto error;
2203 status = rpc_conf_del_value(frame,
2205 &key_hnd,
2206 argv[0],
2207 "includes",
2208 &werr);
2210 error:
2212 if (!(W_ERROR_IS_OK(werr))) {
2213 status = werror_to_ntstatus(werr);
2216 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
2217 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
2219 TALLOC_FREE(frame);
2220 return status;
2224 /**********************************************************
2226 * Functions that run the rpc commands for net rpc conf modules
2228 **********************************************************/
2230 static int rpc_conf_drop(struct net_context *c, int argc,
2231 const char **argv)
2233 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2234 rpc_conf_drop_internal, argc, argv );
2238 static int rpc_conf_showshare(struct net_context *c, int argc,
2239 const char **argv)
2241 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2242 rpc_conf_showshare_internal, argc, argv );
2245 static int rpc_conf_addshare(struct net_context *c, int argc,
2246 const char **argv)
2248 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2249 rpc_conf_addshare_internal, argc, argv );
2252 static int rpc_conf_listshares(struct net_context *c, int argc,
2253 const char **argv)
2255 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2256 rpc_conf_listshares_internal, argc, argv );
2259 static int rpc_conf_list(struct net_context *c, int argc,
2260 const char **argv)
2262 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2263 rpc_conf_list_internal, argc, argv );
2266 static int rpc_conf_import(struct net_context *c, int argc,
2267 const char **argv)
2269 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2270 rpc_conf_import_internal, argc, argv );
2272 static int rpc_conf_delshare(struct net_context *c, int argc,
2273 const char **argv)
2275 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2276 rpc_conf_delshare_internal, argc, argv );
2279 static int rpc_conf_getparm(struct net_context *c, int argc,
2280 const char **argv)
2282 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2283 rpc_conf_getparm_internal, argc, argv );
2286 static int rpc_conf_setparm(struct net_context *c, int argc,
2287 const char **argv)
2289 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2290 rpc_conf_setparm_internal, argc, argv );
2292 static int rpc_conf_delparm(struct net_context *c, int argc,
2293 const char **argv)
2295 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2296 rpc_conf_delparm_internal, argc, argv );
2299 static int rpc_conf_getincludes(struct net_context *c, int argc,
2300 const char **argv)
2302 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2303 rpc_conf_getincludes_internal, argc, argv );
2306 static int rpc_conf_setincludes(struct net_context *c, int argc,
2307 const char **argv)
2309 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2310 rpc_conf_setincludes_internal, argc, argv );
2313 static int rpc_conf_delincludes(struct net_context *c, int argc,
2314 const char **argv)
2316 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2317 rpc_conf_delincludes_internal, argc, argv );
2320 /* function calls */
2321 int net_rpc_conf(struct net_context *c, int argc,
2322 const char **argv)
2324 struct functable func_table[] = {
2326 "list",
2327 rpc_conf_list,
2328 NET_TRANSPORT_RPC,
2329 N_("Dump the complete remote configuration in smb.conf like "
2330 "format."),
2331 N_("net rpc conf list\n"
2332 " Dump the complete remote configuration in smb.conf "
2333 "like format.")
2337 "import",
2338 rpc_conf_import,
2339 NET_TRANSPORT_RPC,
2340 N_("Import configuration from file in smb.conf "
2341 "format."),
2342 N_("net rpc conf import\n"
2343 " Import configuration from file in smb.conf "
2344 "format.")
2347 "listshares",
2348 rpc_conf_listshares,
2349 NET_TRANSPORT_RPC,
2350 N_("List the remote share names."),
2351 N_("net rpc conf list\n"
2352 " List the remote share names.")
2356 "drop",
2357 rpc_conf_drop,
2358 NET_TRANSPORT_RPC,
2359 N_("Delete the complete remote configuration."),
2360 N_("net rpc conf drop\n"
2361 " Delete the complete remote configuration.")
2365 "showshare",
2366 rpc_conf_showshare,
2367 NET_TRANSPORT_RPC,
2368 N_("Show the definition of a remote share."),
2369 N_("net rpc conf showshare\n"
2370 " Show the definition of a remote share.")
2374 "addshare",
2375 rpc_conf_addshare,
2376 NET_TRANSPORT_RPC,
2377 N_("Create a new remote share."),
2378 N_("net rpc conf addshare\n"
2379 " Create a new remote share.")
2382 "delshare",
2383 rpc_conf_delshare,
2384 NET_TRANSPORT_RPC,
2385 N_("Delete a remote share."),
2386 N_("net rpc conf delshare\n"
2387 " Delete a remote share.")
2390 "getparm",
2391 rpc_conf_getparm,
2392 NET_TRANSPORT_RPC,
2393 N_("Retrieve the value of a parameter."),
2394 N_("net rpc conf getparm\n"
2395 " Retrieve the value of a parameter.")
2398 "setparm",
2399 rpc_conf_setparm,
2400 NET_TRANSPORT_RPC,
2401 N_("Store a parameter."),
2402 N_("net rpc conf setparm\n"
2403 " Store a parameter.")
2406 "delparm",
2407 rpc_conf_delparm,
2408 NET_TRANSPORT_RPC,
2409 N_("Delete a parameter."),
2410 N_("net rpc conf delparm\n"
2411 " Delete a parameter.")
2414 "getincludes",
2415 rpc_conf_getincludes,
2416 NET_TRANSPORT_RPC,
2417 N_("Show the includes of a share definition."),
2418 N_("net rpc conf getincludes\n"
2419 " Show the includes of a share definition.")
2422 "setincludes",
2423 rpc_conf_setincludes,
2424 NET_TRANSPORT_RPC,
2425 N_("Set includes for a share."),
2426 N_("net rpc conf setincludes\n"
2427 " Set includes for a share.")
2430 "delincludes",
2431 rpc_conf_delincludes,
2432 NET_TRANSPORT_RPC,
2433 N_("Delete includes from a share definition."),
2434 N_("net rpc conf delincludes\n"
2435 " Delete includes from a share definition.")
2437 {NULL, NULL, 0, NULL, NULL}
2440 return net_run_function(c, argc, argv, "net rpc conf", func_table);