smbstatus: add json items to traverse_struct
[Samba.git] / source3 / utils / net_rpc_conf.c
bloba68d3f70607890b35161eaefdb072174091acf68
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_FILE_NOT_FOUND)){
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_NOT_ENOUGH_MEMORY;
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_NOT_ENOUGH_MEMORY;
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, num_subkeys, i, param_cnt = 0;
422 const char **val_names;
423 const char **subkeys = NULL;
424 enum winreg_Type *types;
425 DATA_BLOB *data;
426 struct winreg_String key = { 0, };
427 const char **multi_s = NULL;
428 const char *s = NULL;
429 struct smbconf_service tmp_share;
431 ZERO_STRUCT(tmp_share);
434 * Determine correct upper/lowercase.
436 status = dcerpc_winreg_enum_keys(frame,
438 parent_hnd,
439 &num_subkeys,
440 &subkeys,
441 &result);
442 if (!(NT_STATUS_IS_OK(status))) {
443 d_fprintf(stderr, _("Failed to enumerate shares: %s\n"),
444 nt_errstr(status));
445 goto error;
447 if (!(W_ERROR_IS_OK(result))) {
448 d_fprintf(stderr, _("Failed to enumerate shares: %s\n"),
449 win_errstr(result));
450 goto error;
453 for (i = 0; i < num_subkeys; i++) {
454 if (!strequal(share_name, subkeys[i])) {
455 continue;
458 key.name = subkeys[i];
461 if (key.name == NULL) {
462 d_fprintf(stderr, _("Could not find share.\n"));
463 goto error;
466 status = dcerpc_winreg_OpenKey(b, frame, parent_hnd, key, 0,
467 REG_KEY_READ, &child_hnd, &result);
469 if (!(NT_STATUS_IS_OK(status))) {
470 d_fprintf(stderr, _("Failed to open subkey: %s\n"),
471 nt_errstr(status));
472 goto error;
474 if (!(W_ERROR_IS_OK(result))) {
475 d_fprintf(stderr, _("Failed to open subkey: %s\n"),
476 win_errstr(result));
477 goto error;
479 /* get all the info from the share key */
480 status = dcerpc_winreg_enumvals(frame,
482 &child_hnd,
483 &num_vals,
484 &val_names,
485 &types,
486 &data,
487 &result);
489 if (!(NT_STATUS_IS_OK(status))) {
490 d_fprintf(stderr, _("Failed to enumerate values: %s\n"),
491 nt_errstr(status));
492 goto error;
494 if (!(W_ERROR_IS_OK(result))) {
495 d_fprintf(stderr, _("Failed to enumerate values: %s\n"),
496 win_errstr(result));
497 goto error;
499 /* check for includes */
500 for (i = 0; i < num_vals; i++) {
501 if (strcmp(val_names[i], "includes") == 0){
502 if (!pull_reg_multi_sz(frame,
503 &data[i],
504 &multi_s))
506 result = WERR_NOT_ENOUGH_MEMORY;
507 d_fprintf(stderr,
508 _("Failed to enumerate values: %s\n"),
509 win_errstr(result));
510 goto error;
512 includes_idx = i;
515 /* count the number of includes */
516 includes_cnt = 0;
517 if (includes_idx != -1) {
518 for (includes_cnt = 0;
519 multi_s[includes_cnt] != NULL;
520 includes_cnt ++);
522 /* place the name of the share in the smbconf_service struct */
523 tmp_share.name = talloc_strdup(frame, key.name);
524 if (tmp_share.name == NULL) {
525 result = WERR_NOT_ENOUGH_MEMORY;
526 d_fprintf(stderr, _("Failed to create share: %s\n"),
527 win_errstr(result));
528 goto error;
530 /* place the number of parameters in the smbconf_service struct */
531 tmp_share.num_params = num_vals;
532 if (includes_idx != -1) {
533 tmp_share.num_params = num_vals + includes_cnt - 1;
535 /* allocate memory for the param_names and param_values lists */
536 tmp_share.param_names = talloc_zero_array(frame, char *, tmp_share.num_params);
537 if (tmp_share.param_names == NULL) {
538 result = WERR_NOT_ENOUGH_MEMORY;
539 d_fprintf(stderr, _("Failed to create share: %s\n"),
540 win_errstr(result));
541 goto error;
543 tmp_share.param_values = talloc_zero_array(frame, char *, tmp_share.num_params);
544 if (tmp_share.param_values == NULL) {
545 result = WERR_NOT_ENOUGH_MEMORY;
546 d_fprintf(stderr, _("Failed to create share: %s\n"),
547 win_errstr(result));
548 goto error;
550 /* place all params except includes */
551 for (i = 0; i < num_vals; i++) {
552 if (strcmp(val_names[i], "includes") != 0) {
553 if (!pull_reg_sz(frame, &data[i], &s)) {
554 result = WERR_NOT_ENOUGH_MEMORY;
555 d_fprintf(stderr,
556 _("Failed to enumerate values: %s\n"),
557 win_errstr(result));
558 goto error;
560 /* place param_names */
561 tmp_share.param_names[param_cnt] = talloc_strdup(frame, val_names[i]);
562 if (tmp_share.param_names[param_cnt] == NULL) {
563 result = WERR_NOT_ENOUGH_MEMORY;
564 d_fprintf(stderr, _("Failed to create share: %s\n"),
565 win_errstr(result));
566 goto error;
569 /* place param_values */
570 tmp_share.param_values[param_cnt++] = talloc_strdup(frame, s);
571 if (tmp_share.param_values[param_cnt - 1] == NULL) {
572 result = WERR_NOT_ENOUGH_MEMORY;
573 d_fprintf(stderr, _("Failed to create share: %s\n"),
574 win_errstr(result));
575 goto error;
579 /* place the includes last */
580 for (i = 0; i < includes_cnt; i++) {
581 tmp_share.param_names[param_cnt] = talloc_strdup(frame, "include");
582 if (tmp_share.param_names[param_cnt] == NULL) {
583 result = WERR_NOT_ENOUGH_MEMORY;
584 d_fprintf(stderr, _("Failed to create share: %s\n"),
585 win_errstr(result));
586 goto error;
589 tmp_share.param_values[param_cnt++] = talloc_strdup(frame, multi_s[i]);
590 if (tmp_share.param_values[param_cnt - 1] == NULL) {
591 result = WERR_NOT_ENOUGH_MEMORY;
592 d_fprintf(stderr, _("Failed to create share: %s\n"),
593 win_errstr(result));
594 goto error;
598 /* move everything to the main memory ctx */
599 for (i = 0; i < param_cnt; i++) {
600 tmp_share.param_names[i] = talloc_move(mem_ctx, &tmp_share.param_names[i]);
601 tmp_share.param_values[i] = talloc_move(mem_ctx, &tmp_share.param_values[i]);
604 tmp_share.name = talloc_move(mem_ctx, &tmp_share.name);
605 tmp_share.param_names = talloc_move(mem_ctx, &tmp_share.param_names);
606 tmp_share.param_values = talloc_move(mem_ctx, &tmp_share.param_values);
607 /* out parameter */
608 *share = tmp_share;
609 error:
610 /* close child */
611 dcerpc_winreg_CloseKey(b, frame, &child_hnd, &_werr);
612 *werr = result;
613 TALLOC_FREE(frame);
614 return status;
618 * The function prints the shares held as smbconf_service structs
619 * in a smbconf file format.
621 static int rpc_conf_print_shares(uint32_t num_shares,
622 struct smbconf_service *shares)
625 uint32_t share_count, param_count;
626 const char *indent = "\t";
628 if (num_shares == 0) {
629 return 0;
632 for (share_count = 0; share_count < num_shares; share_count++) {
633 d_printf("\n");
634 if (shares[share_count].name != NULL) {
635 d_printf("[%s]\n", shares[share_count].name);
638 for (param_count = 0;
639 param_count < shares[share_count].num_params;
640 param_count++)
642 d_printf("%s%s = %s\n",
643 indent,
644 shares[share_count].param_names[param_count],
645 shares[share_count].param_values[param_count]);
648 d_printf("\n");
650 return 0;
655 * The function openes the registry key
656 * HKLM/Software/Samba/smbconf with the give access_mask
658 static NTSTATUS rpc_conf_open_conf(TALLOC_CTX *mem_ctx,
659 struct dcerpc_binding_handle *b,
660 uint32_t access_mask,
661 struct policy_handle *hive_hnd,
662 struct policy_handle *key_hnd,
663 WERROR *werr)
665 TALLOC_CTX *frame = talloc_stackframe();
666 NTSTATUS status = NT_STATUS_OK;
667 WERROR result = WERR_OK;
668 WERROR _werr;
669 struct policy_handle tmp_hive_hnd, tmp_key_hnd;
670 struct winreg_String key;
672 ZERO_STRUCT(key);
674 status = dcerpc_winreg_OpenHKLM(b, frame, NULL,
675 access_mask, &tmp_hive_hnd, &result);
678 * print no error messages if it is a read only open
679 * and key does not exist
680 * error still gets returned
683 if (access_mask == REG_KEY_READ &&
684 W_ERROR_EQUAL(result, WERR_FILE_NOT_FOUND))
686 goto error;
689 if (!(NT_STATUS_IS_OK(status))) {
690 d_fprintf(stderr, _("Failed to open hive: %s\n"),
691 nt_errstr(status));
692 goto error;
694 if (!W_ERROR_IS_OK(result)) {
695 d_fprintf(stderr, _("Failed to open hive: %s\n"),
696 win_errstr(result));
697 goto error;
700 key.name = confpath;
701 status = dcerpc_winreg_OpenKey(b, frame, &tmp_hive_hnd, key, 0,
702 access_mask, &tmp_key_hnd, &result);
705 * print no error messages if it is a read only open
706 * and key does not exist
707 * error still gets returned
710 if (access_mask == REG_KEY_READ &&
711 W_ERROR_EQUAL(result, WERR_FILE_NOT_FOUND))
713 goto error;
716 if (!(NT_STATUS_IS_OK(status))) {
717 d_fprintf(stderr, _("Failed to open smbconf key: %s\n"),
718 nt_errstr(status));
719 dcerpc_winreg_CloseKey(b, frame, &tmp_hive_hnd, &_werr);
720 goto error;
722 if (!(W_ERROR_IS_OK(result))) {
723 d_fprintf(stderr, _("Failed to open smbconf key: %s\n"),
724 win_errstr(result));
725 dcerpc_winreg_CloseKey(b, frame, &tmp_hive_hnd, &_werr);
726 goto error;
729 *hive_hnd = tmp_hive_hnd;
730 *key_hnd = tmp_key_hnd;
732 error:
733 TALLOC_FREE(frame);
734 *werr = result;
736 return status;
739 /**********************************************************
741 * internal functions that provide the functionality
742 * net rpc conf
744 **********************************************************/
746 static NTSTATUS rpc_conf_listshares_internal(struct net_context *c,
747 const struct dom_sid *domain_sid,
748 const char *domain_name,
749 struct cli_state *cli,
750 struct rpc_pipe_client *pipe_hnd,
751 TALLOC_CTX *mem_ctx,
752 int argc,
753 const char **argv )
756 TALLOC_CTX *frame = talloc_stackframe();
757 NTSTATUS status = NT_STATUS_OK;
758 WERROR werr = WERR_OK;
759 WERROR _werr;
761 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
763 /* key info */
764 struct policy_handle hive_hnd, key_hnd;
765 uint32_t num_subkeys;
766 uint32_t i;
767 const char **subkeys = NULL;
770 ZERO_STRUCT(hive_hnd);
771 ZERO_STRUCT(key_hnd);
774 if (argc != 0 || c->display_usage) {
775 rpc_conf_listshares_usage(c, argc, argv);
776 status = NT_STATUS_INVALID_PARAMETER;
777 goto error;
781 status = rpc_conf_open_conf(frame,
783 REG_KEY_READ,
784 &hive_hnd,
785 &key_hnd,
786 &werr);
788 if (!(NT_STATUS_IS_OK(status))) {
789 goto error;
792 if (!(W_ERROR_IS_OK(werr))) {
793 goto error;
796 status = dcerpc_winreg_enum_keys(frame,
798 &key_hnd,
799 &num_subkeys,
800 &subkeys,
801 &werr);
803 if (!(NT_STATUS_IS_OK(status))) {
804 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
805 nt_errstr(status));
806 goto error;
809 if (!(W_ERROR_IS_OK(werr))) {
810 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
811 win_errstr(werr));
812 goto error;
815 for (i = 0; i < num_subkeys; i++) {
816 d_printf("%s\n", subkeys[i]);
819 error:
820 if (!(W_ERROR_IS_OK(werr))) {
821 status = werror_to_ntstatus(werr);
824 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
825 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
827 TALLOC_FREE(frame);
828 return status;;
831 static NTSTATUS rpc_conf_delshare_internal(struct net_context *c,
832 const struct dom_sid *domain_sid,
833 const char *domain_name,
834 struct cli_state *cli,
835 struct rpc_pipe_client *pipe_hnd,
836 TALLOC_CTX *mem_ctx,
837 int argc,
838 const char **argv )
841 TALLOC_CTX *frame = talloc_stackframe();
842 NTSTATUS status = NT_STATUS_OK;
843 WERROR werr = WERR_OK;
844 WERROR _werr;
846 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
848 /* key info */
849 struct policy_handle hive_hnd, key_hnd;
851 ZERO_STRUCT(hive_hnd);
852 ZERO_STRUCT(key_hnd);
855 if (argc != 1 || c->display_usage) {
856 rpc_conf_delshare_usage(c, argc, argv);
857 status = NT_STATUS_INVALID_PARAMETER;
858 goto error;
861 status = rpc_conf_open_conf(frame,
863 REG_KEY_ALL,
864 &hive_hnd,
865 &key_hnd,
866 &werr);
868 if (!(NT_STATUS_IS_OK(status))) {
869 goto error;
872 if (!(W_ERROR_IS_OK(werr))) {
873 goto error;
876 status = dcerpc_winreg_delete_subkeys_recursive(frame,
878 &key_hnd,
879 REG_KEY_ALL,
880 argv[0],
881 &werr);
883 if (!NT_STATUS_IS_OK(status)) {
884 d_fprintf(stderr,
885 "winreg_delete_subkeys: Could not delete key %s: %s\n",
886 argv[0], nt_errstr(status));
887 goto error;
890 if (W_ERROR_EQUAL(werr, WERR_FILE_NOT_FOUND)){
891 d_fprintf(stderr, _("ERROR: Key does not exist\n"));
895 if (!W_ERROR_IS_OK(werr)) {
896 d_fprintf(stderr,
897 "winreg_delete_subkeys: Could not delete key %s: %s\n",
898 argv[0], win_errstr(werr));
899 goto error;
902 error:
903 if (!(W_ERROR_IS_OK(werr))) {
904 status = werror_to_ntstatus(werr);
907 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
908 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
910 TALLOC_FREE(frame);
912 return status;
915 static NTSTATUS rpc_conf_list_internal(struct net_context *c,
916 const struct dom_sid *domain_sid,
917 const char *domain_name,
918 struct cli_state *cli,
919 struct rpc_pipe_client *pipe_hnd,
920 TALLOC_CTX *mem_ctx,
921 int argc,
922 const char **argv )
925 TALLOC_CTX *frame = talloc_stackframe();
926 NTSTATUS status = NT_STATUS_OK;
927 WERROR werr = WERR_OK;
928 WERROR _werr;
930 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
932 /* key info */
933 struct policy_handle hive_hnd, key_hnd;
934 uint32_t num_subkeys;
935 uint32_t i;
936 struct smbconf_service *shares;
937 const char **subkeys = NULL;
940 ZERO_STRUCT(hive_hnd);
941 ZERO_STRUCT(key_hnd);
944 if (argc != 0 || c->display_usage) {
945 rpc_conf_list_usage(c, argc, argv);
946 status = NT_STATUS_INVALID_PARAMETER;
947 goto error;
950 status = rpc_conf_open_conf(frame,
952 REG_KEY_READ,
953 &hive_hnd,
954 &key_hnd,
955 &werr);
957 if (!(NT_STATUS_IS_OK(status))) {
958 goto error;
961 if (!(W_ERROR_IS_OK(werr))) {
962 goto error;
965 status = dcerpc_winreg_enum_keys(frame,
967 &key_hnd,
968 &num_subkeys,
969 &subkeys,
970 &werr);
972 if (!(NT_STATUS_IS_OK(status))) {
973 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
974 nt_errstr(status));
975 goto error;
978 if (!(W_ERROR_IS_OK(werr))) {
979 d_fprintf(stderr, _("Failed to enumerate keys: %s\n"),
980 win_errstr(werr));
981 goto error;
984 if (num_subkeys == 0) {
985 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
986 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
987 TALLOC_FREE(frame);
988 return NT_STATUS_OK;
991 /* get info from each subkey */
992 shares = talloc_zero_array(frame, struct smbconf_service, num_subkeys);
993 if (shares == NULL) {
994 werr = WERR_NOT_ENOUGH_MEMORY;
995 d_fprintf(stderr, _("Failed to create shares: %s\n"),
996 win_errstr(werr));
997 goto error;
1001 for (i = 0; i < num_subkeys; i++) {
1002 /* get each share and place it in the shares array */
1003 status = rpc_conf_get_share(frame,
1005 &key_hnd,
1006 subkeys[i],
1007 &shares[i],
1008 &werr);
1009 if (!(NT_STATUS_IS_OK(status))) {
1010 goto error;
1012 if (!(W_ERROR_IS_OK(werr))) {
1013 goto error;
1017 /* print the shares array */
1018 rpc_conf_print_shares(num_subkeys, shares);
1020 error:
1021 if (!(W_ERROR_IS_OK(werr))) {
1022 status = werror_to_ntstatus(werr);
1025 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1026 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1028 TALLOC_FREE(frame);
1029 return status;
1033 static NTSTATUS rpc_conf_drop_internal(struct net_context *c,
1034 const struct dom_sid *domain_sid,
1035 const char *domain_name,
1036 struct cli_state *cli,
1037 struct rpc_pipe_client *pipe_hnd,
1038 TALLOC_CTX *mem_ctx,
1039 int argc,
1040 const char **argv )
1042 TALLOC_CTX *frame = talloc_stackframe();
1043 NTSTATUS status = NT_STATUS_OK;
1044 WERROR werr = WERR_OK;
1045 WERROR _werr;
1047 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1049 /* key info */
1050 struct policy_handle hive_hnd, key_hnd;
1051 const char *keyname = confpath;
1052 struct winreg_String wkey, wkeyclass;
1053 enum winreg_CreateAction action = REG_ACTION_NONE;
1056 ZERO_STRUCT(hive_hnd);
1057 ZERO_STRUCT(key_hnd);
1060 if (argc != 0 || c->display_usage) {
1061 rpc_conf_drop_usage(c, argc, argv);
1062 status = NT_STATUS_INVALID_PARAMETER;
1063 goto error;
1066 status = rpc_conf_open_conf(frame,
1068 REG_KEY_ALL,
1069 &hive_hnd,
1070 &key_hnd,
1071 &werr);
1073 if (!(NT_STATUS_IS_OK(status))) {
1074 goto error;
1077 if (!(W_ERROR_IS_OK(werr))) {
1078 goto error;
1081 status = dcerpc_winreg_delete_subkeys_recursive(frame,
1083 &hive_hnd,
1084 REG_KEY_ALL,
1085 keyname,
1086 &werr);
1088 if (!NT_STATUS_IS_OK(status)) {
1089 d_printf("winreg_delete_subkeys: Could not delete key %s: %s\n",
1090 keyname, nt_errstr(status));
1091 goto error;
1094 if (!W_ERROR_IS_OK(werr)) {
1095 d_printf("winreg_delete_subkeys: Could not delete key %s: %s\n",
1096 keyname, win_errstr(werr));
1097 goto error;
1100 ZERO_STRUCT(wkey);
1101 wkey.name = keyname;
1102 ZERO_STRUCT(wkeyclass);
1103 wkeyclass.name = "";
1104 action = REG_ACTION_NONE;
1106 status = dcerpc_winreg_CreateKey(b,
1107 frame,
1108 &hive_hnd,
1109 wkey,
1110 wkeyclass,
1112 REG_KEY_ALL,
1113 NULL,
1114 &key_hnd,
1115 &action,
1116 &werr);
1118 if (!NT_STATUS_IS_OK(status)) {
1119 d_printf("winreg_CreateKey: Could not create smbconf key\n");
1120 goto error;
1123 if (!W_ERROR_IS_OK(werr)) {
1124 d_printf("winreg_CreateKey: Could not create smbconf key\n");
1125 goto error;
1129 error:
1130 if (!(W_ERROR_IS_OK(werr))) {
1131 status = werror_to_ntstatus(werr);
1134 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1135 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1137 TALLOC_FREE(frame);
1138 return status;
1141 static NTSTATUS rpc_conf_import_internal(struct net_context *c,
1142 const struct dom_sid *domain_sid,
1143 const char *domain_name,
1144 struct cli_state *cli,
1145 struct rpc_pipe_client *pipe_hnd,
1146 TALLOC_CTX *mem_ctx,
1147 int argc,
1148 const char **argv )
1151 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1153 struct policy_handle hive_hnd, key_hnd;
1155 const char *filename = NULL;
1156 const char *servicename = NULL;
1157 char *conf_source = NULL;
1158 TALLOC_CTX *frame;
1159 struct smbconf_ctx *txt_ctx;
1160 struct smbconf_service *service = NULL;
1161 struct smbconf_service **services = NULL;
1162 uint32_t num_shares, i;
1163 sbcErr err = SBC_ERR_UNKNOWN_FAILURE;
1165 WERROR werr = WERR_OK;
1166 NTSTATUS status = NT_STATUS_OK;
1168 ZERO_STRUCT(hive_hnd);
1169 ZERO_STRUCT(key_hnd);
1171 frame = talloc_stackframe();
1173 if (c->display_usage) {
1174 rpc_conf_import_usage(c, argc, argv);
1175 status = NT_STATUS_INVALID_PARAMETER;
1176 goto error;
1179 switch (argc) {
1180 case 0:
1181 default:
1182 rpc_conf_import_usage(c, argc, argv);
1183 status = NT_STATUS_INVALID_PARAMETER;
1184 goto error;
1185 case 2:
1186 servicename = talloc_strdup(frame, argv[1]);
1187 if (servicename == NULL) {
1188 d_printf(_("error: out of memory!\n"));
1189 goto error;
1192 FALL_THROUGH;
1193 case 1:
1194 filename = argv[0];
1195 break;
1198 DEBUG(3,("rpc_conf_import: reading configuration from file %s.\n",
1199 filename));
1201 conf_source = talloc_asprintf(frame, "file:%s", filename);
1202 if (conf_source == NULL) {
1203 d_fprintf(stderr, _("error: out of memory!\n"));
1204 goto error;
1207 err = smbconf_init(frame, &txt_ctx, conf_source);
1208 if (!SBC_ERROR_IS_OK(err)) {
1209 d_fprintf(stderr, _("error loading file '%s': %s\n"), filename,
1210 sbcErrorString(err));
1211 goto error;
1214 if (c->opt_testmode) {
1215 d_printf(_("\nTEST MODE - "
1216 "would import the following configuration:\n\n"));
1219 if (servicename != NULL) {
1220 err = smbconf_get_share(txt_ctx, frame,
1221 servicename,
1222 &service);
1223 if (!SBC_ERROR_IS_OK(err)) {
1224 goto error;
1227 num_shares = 1;
1229 } else {
1231 err = smbconf_get_config(txt_ctx, frame,
1232 &num_shares,
1233 &services);
1234 if (!SBC_ERROR_IS_OK(err)) {
1235 goto error;
1239 if (c->opt_testmode) {
1240 if (servicename != NULL) {
1241 rpc_conf_print_shares(1, service);
1243 for (i = 0; i < num_shares; i++) {
1244 rpc_conf_print_shares(1, services[i]);
1246 goto error;
1249 status = rpc_conf_drop_internal(c,
1250 domain_sid,
1251 domain_name,
1252 cli,
1253 pipe_hnd,
1254 frame,
1256 NULL );
1258 if (!(NT_STATUS_IS_OK(status))) {
1259 goto error;
1262 status = rpc_conf_open_conf(frame,
1264 REG_KEY_READ,
1265 &hive_hnd,
1266 &key_hnd,
1267 &werr);
1269 if (!(NT_STATUS_IS_OK(status))) {
1270 goto error;
1273 if (!(W_ERROR_IS_OK(werr))) {
1274 goto error;
1277 if (servicename != NULL) {
1278 status = rpc_conf_set_share(frame,
1280 &key_hnd,
1281 service,
1282 &werr);
1284 if (!(NT_STATUS_IS_OK(status))) {
1285 goto error;
1288 if (!(W_ERROR_IS_OK(werr))) {
1289 goto error;
1292 } else {
1294 for (i = 0; i < num_shares; i++) {
1295 status = rpc_conf_set_share(frame,
1297 &key_hnd,
1298 services[i],
1299 &werr);
1301 if (!(NT_STATUS_IS_OK(status))) {
1302 goto error;
1305 if (!(W_ERROR_IS_OK(werr))) {
1306 goto error;
1312 error:
1313 if (!SBC_ERROR_IS_OK(err)) {
1314 d_fprintf(stderr, "ERROR: %s\n", sbcErrorString(err));
1317 if (!(W_ERROR_IS_OK(werr))) {
1318 status = werror_to_ntstatus(werr);
1320 TALLOC_FREE(frame);
1321 return status;
1324 static NTSTATUS rpc_conf_showshare_internal(struct net_context *c,
1325 const struct dom_sid *domain_sid,
1326 const char *domain_name,
1327 struct cli_state *cli,
1328 struct rpc_pipe_client *pipe_hnd,
1329 TALLOC_CTX *mem_ctx,
1330 int argc,
1331 const char **argv )
1333 TALLOC_CTX *frame = talloc_stackframe();
1334 NTSTATUS status = NT_STATUS_OK;
1335 WERROR werr = WERR_OK;
1336 WERROR _werr;
1338 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1340 /* key info */
1341 struct policy_handle hive_hnd, key_hnd;
1342 struct smbconf_service *service = NULL;
1343 const char *sharename = NULL;
1346 ZERO_STRUCT(hive_hnd);
1347 ZERO_STRUCT(key_hnd);
1350 if (argc != 1 || c->display_usage) {
1351 rpc_conf_showshare_usage(c, argc, argv);
1352 status = NT_STATUS_INVALID_PARAMETER;
1353 goto error;
1356 status = rpc_conf_open_conf(frame,
1358 REG_KEY_READ,
1359 &hive_hnd,
1360 &key_hnd,
1361 &werr);
1363 if (!(NT_STATUS_IS_OK(status))) {
1364 goto error;
1367 if (!(W_ERROR_IS_OK(werr))) {
1368 goto error;
1371 sharename = talloc_strdup(frame, argv[0]);
1372 if (sharename == NULL) {
1373 werr = WERR_NOT_ENOUGH_MEMORY;
1374 d_fprintf(stderr, _("Failed to create share: %s\n"),
1375 win_errstr(werr));
1376 goto error;
1379 service = talloc(frame, struct smbconf_service);
1380 if (service == NULL) {
1381 werr = WERR_NOT_ENOUGH_MEMORY;
1382 d_fprintf(stderr, _("Failed to create share: %s\n"),
1383 win_errstr(werr));
1384 goto error;
1387 status = rpc_conf_get_share(frame,
1389 &key_hnd,
1390 sharename,
1391 service,
1392 &werr);
1394 if (!(NT_STATUS_IS_OK(status))) {
1395 goto error;
1397 if (!(W_ERROR_IS_OK(werr))) {
1398 goto error;
1401 rpc_conf_print_shares(1, service);
1403 error:
1404 if (!(W_ERROR_IS_OK(werr))) {
1405 status = werror_to_ntstatus(werr);
1408 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1409 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1411 TALLOC_FREE(frame);
1412 return status;
1415 static NTSTATUS rpc_conf_addshare_internal(struct net_context *c,
1416 const struct dom_sid *domain_sid,
1417 const char *domain_name,
1418 struct cli_state *cli,
1419 struct rpc_pipe_client *pipe_hnd,
1420 TALLOC_CTX *mem_ctx,
1421 int argc,
1422 const char **argv )
1424 TALLOC_CTX *frame = talloc_stackframe();
1425 NTSTATUS status = NT_STATUS_OK;
1426 WERROR werr = WERR_OK;
1427 WERROR _werr;
1429 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1431 /* key info */
1432 struct policy_handle hive_hnd, key_hnd, share_hnd;
1433 char *sharename = NULL;
1434 const char *path = NULL;
1435 const char *comment = NULL;
1436 const char *guest_ok = "no";
1437 const char *read_only = "yes";
1438 struct winreg_String key, keyclass;
1439 enum winreg_CreateAction action = 0;
1442 ZERO_STRUCT(hive_hnd);
1443 ZERO_STRUCT(key_hnd);
1444 ZERO_STRUCT(share_hnd);
1446 ZERO_STRUCT(key);
1447 ZERO_STRUCT(keyclass);
1449 if (c->display_usage) {
1450 rpc_conf_addshare_usage(c, argc, argv);
1451 status = NT_STATUS_INVALID_PARAMETER;
1452 goto error;
1455 switch (argc) {
1456 case 0:
1457 case 1:
1458 default:
1459 rpc_conf_addshare_usage(c, argc, argv);
1460 status = NT_STATUS_INVALID_PARAMETER;
1461 goto error;
1462 case 5:
1463 comment = argv[4];
1465 FALL_THROUGH;
1466 case 4:
1467 if (!strnequal(argv[3], "guest_ok=", 9)) {
1468 rpc_conf_addshare_usage(c, argc, argv);
1469 status = NT_STATUS_INVALID_PARAMETER;
1470 goto error;
1472 switch (argv[3][9]) {
1473 case 'y':
1474 case 'Y':
1475 guest_ok = "yes";
1476 break;
1477 case 'n':
1478 case 'N':
1479 guest_ok = "no";
1480 break;
1481 default:
1482 rpc_conf_addshare_usage(c, argc, argv);
1483 status = NT_STATUS_INVALID_PARAMETER;
1484 goto error;
1487 FALL_THROUGH;
1488 case 3:
1489 if (!strnequal(argv[2], "writeable=", 10)) {
1490 rpc_conf_addshare_usage(c, argc, argv);
1491 status = NT_STATUS_INVALID_PARAMETER;
1492 goto error;
1494 switch (argv[2][10]) {
1495 case 'y':
1496 case 'Y':
1497 read_only = "no";
1498 break;
1499 case 'n':
1500 case 'N':
1501 read_only = "yes";
1502 break;
1503 default:
1504 rpc_conf_addshare_usage(c, argc, argv);
1505 status = NT_STATUS_INVALID_PARAMETER;
1506 goto error;
1509 FALL_THROUGH;
1510 case 2:
1511 path = argv[1];
1512 sharename = talloc_strdup(frame, argv[0]);
1513 if (sharename == NULL) {
1514 d_printf(_("error: out of memory!\n"));
1515 goto error;
1518 break;
1521 status = rpc_conf_open_conf(frame,
1523 REG_KEY_READ,
1524 &hive_hnd,
1525 &key_hnd,
1526 &werr);
1528 if (!(NT_STATUS_IS_OK(status))) {
1529 goto error;
1532 if (!(W_ERROR_IS_OK(werr))) {
1533 goto error;
1536 key.name = argv[0];
1537 keyclass.name = "";
1539 status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
1540 0, REG_KEY_READ, NULL, &share_hnd,
1541 &action, &werr);
1543 if (!(NT_STATUS_IS_OK(status))) {
1544 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1545 argv[0], nt_errstr(status));
1546 goto error;
1549 if (!W_ERROR_IS_OK(werr)) {
1550 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1551 argv[0], win_errstr(werr));
1552 goto error;
1555 switch (action) {
1556 case REG_ACTION_NONE:
1557 werr = WERR_CREATE_FAILED;
1558 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1559 argv[0], win_errstr(werr));
1560 goto error;
1561 case REG_CREATED_NEW_KEY:
1562 DEBUG(5, ("net rpc conf setincludes:"
1563 "createkey created %s\n", argv[0]));
1564 break;
1565 case REG_OPENED_EXISTING_KEY:
1566 d_fprintf(stderr, _("ERROR: Share '%s' already exists\n"), argv[0]);
1567 status = NT_STATUS_INVALID_PARAMETER;
1568 goto error;
1571 /* set the path parameter */
1572 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1573 "path", path, &werr);
1575 if (!(NT_STATUS_IS_OK(status))) {
1576 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1577 " with value %s\n %s\n",
1578 "path", path, nt_errstr(status));
1579 goto error;
1582 if (!(W_ERROR_IS_OK(werr))) {
1583 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1584 " with value %s\n %s\n",
1585 "path", path, win_errstr(werr));
1586 goto error;
1589 /* set the writeable parameter */
1590 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1591 "read only", read_only, &werr);
1593 if (!(NT_STATUS_IS_OK(status))) {
1594 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1595 " with value %s\n %s\n",
1596 "read only", read_only, nt_errstr(status));
1597 goto error;
1600 if (!(W_ERROR_IS_OK(werr))) {
1601 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1602 " with value %s\n %s\n",
1603 "read only", read_only, win_errstr(werr));
1604 goto error;
1607 /* set the guest ok parameter */
1608 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1609 "guest ok", guest_ok, &werr);
1611 if (!(NT_STATUS_IS_OK(status))) {
1612 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1613 " with value %s\n %s\n",
1614 "guest ok", guest_ok, nt_errstr(status));
1615 goto error;
1618 if (!(W_ERROR_IS_OK(werr))) {
1619 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1620 " with value %s\n %s\n",
1621 "guest ok", guest_ok, win_errstr(werr));
1622 goto error;
1625 if (argc == 5) {
1626 /* set the comment parameter */
1627 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1628 "comment", comment, &werr);
1630 if (!(NT_STATUS_IS_OK(status))) {
1631 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1632 " with value %s\n %s\n",
1633 "comment", comment, nt_errstr(status));
1634 goto error;
1637 if (!(W_ERROR_IS_OK(werr))) {
1638 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1639 " with value %s\n %s\n",
1640 "comment", comment, win_errstr(werr));
1641 goto error;
1644 error:
1645 if (!(W_ERROR_IS_OK(werr))) {
1646 status = werror_to_ntstatus(werr);
1649 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1650 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1651 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
1653 TALLOC_FREE(frame);
1654 return status;
1657 static NTSTATUS rpc_conf_getparm_internal(struct net_context *c,
1658 const struct dom_sid *domain_sid,
1659 const char *domain_name,
1660 struct cli_state *cli,
1661 struct rpc_pipe_client *pipe_hnd,
1662 TALLOC_CTX *mem_ctx,
1663 int argc,
1664 const char **argv )
1666 TALLOC_CTX *frame = talloc_stackframe();
1667 NTSTATUS status = NT_STATUS_OK;
1668 WERROR werr = WERR_OK;
1669 WERROR _werr;
1671 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1673 /* key info */
1674 struct policy_handle hive_hnd, key_hnd;
1675 struct smbconf_service *service = NULL;
1677 bool param_is_set = false;
1678 uint32_t param_count;
1680 ZERO_STRUCT(hive_hnd);
1681 ZERO_STRUCT(key_hnd);
1684 if (argc != 2 || c->display_usage) {
1685 rpc_conf_getparm_usage(c, argc, argv);
1686 status = NT_STATUS_INVALID_PARAMETER;
1687 goto error;
1690 status = rpc_conf_open_conf(frame,
1692 REG_KEY_READ,
1693 &hive_hnd,
1694 &key_hnd,
1695 &werr);
1697 if (!(NT_STATUS_IS_OK(status))) {
1698 goto error;
1701 if (!(W_ERROR_IS_OK(werr))) {
1702 goto error;
1706 service = talloc(frame, struct smbconf_service);
1708 status = rpc_conf_get_share(frame,
1710 &key_hnd,
1711 argv[0],
1712 service,
1713 &werr);
1715 if (!(NT_STATUS_IS_OK(status))) {
1716 goto error;
1719 if (W_ERROR_EQUAL(werr, WERR_FILE_NOT_FOUND)) {
1720 d_fprintf(stderr, _("ERROR: Share %s does not exist\n"),
1721 argv[0]);
1722 goto error;
1725 if (!(W_ERROR_IS_OK(werr))) {
1726 goto error;
1729 for (param_count = 0;
1730 param_count < service->num_params;
1731 param_count++)
1733 /* should includes also be printed? */
1734 if (strcmp(service->param_names[param_count], argv[1]) == 0) {
1735 d_printf(_("%s\n"),
1736 service->param_values[param_count]);
1737 param_is_set = true;
1741 if (!param_is_set) {
1742 d_fprintf(stderr, _("ERROR: Given parameter '%s' has not been set\n"),
1743 argv[1]);
1744 werr = WERR_FILE_NOT_FOUND;
1745 goto error;
1748 error:
1750 if (!(W_ERROR_IS_OK(werr))) {
1751 status = werror_to_ntstatus(werr);
1754 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1755 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1757 TALLOC_FREE(frame);
1758 return status;
1762 static NTSTATUS rpc_conf_setparm_internal(struct net_context *c,
1763 const struct dom_sid *domain_sid,
1764 const char *domain_name,
1765 struct cli_state *cli,
1766 struct rpc_pipe_client *pipe_hnd,
1767 TALLOC_CTX *mem_ctx,
1768 int argc,
1769 const char **argv )
1771 TALLOC_CTX *frame = talloc_stackframe();
1772 NTSTATUS status = NT_STATUS_OK;
1773 WERROR werr = WERR_OK;
1774 WERROR _werr;
1776 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1778 /* key info */
1779 struct policy_handle hive_hnd, key_hnd, share_hnd;
1781 struct winreg_String key, keyclass;
1782 enum winreg_CreateAction action = 0;
1784 const char *service_name, *param_name, *valstr;
1786 ZERO_STRUCT(hive_hnd);
1787 ZERO_STRUCT(key_hnd);
1788 ZERO_STRUCT(share_hnd);
1790 ZERO_STRUCT(key);
1791 ZERO_STRUCT(keyclass);
1793 if (argc != 3 || c->display_usage) {
1794 rpc_conf_setparm_usage(c, argc, argv);
1795 status = NT_STATUS_INVALID_PARAMETER;
1796 goto error;
1799 status = rpc_conf_open_conf(frame,
1801 REG_KEY_READ,
1802 &hive_hnd,
1803 &key_hnd,
1804 &werr);
1806 if (!(NT_STATUS_IS_OK(status))) {
1807 goto error;
1810 if (!(W_ERROR_IS_OK(werr))) {
1811 goto error;
1814 service_name = argv[0];
1815 param_name = argv[1];
1816 valstr = argv[2];
1818 key.name = service_name;
1819 keyclass.name = "";
1821 status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
1822 0, REG_KEY_READ, NULL, &share_hnd,
1823 &action, &werr);
1825 if (!(NT_STATUS_IS_OK(status))) {
1826 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1827 service_name, nt_errstr(status));
1828 goto error;
1831 if (!W_ERROR_IS_OK(werr)) {
1832 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1833 service_name, win_errstr(werr));
1834 goto error;
1837 switch (action) {
1838 case REG_ACTION_NONE:
1839 werr = WERR_CREATE_FAILED;
1840 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
1841 service_name, win_errstr(werr));
1842 goto error;
1843 case REG_CREATED_NEW_KEY:
1844 DEBUG(5, ("net rpc conf setparm:"
1845 "createkey created %s\n", service_name));
1846 break;
1847 case REG_OPENED_EXISTING_KEY:
1848 DEBUG(5, ("net rpc conf setparm:"
1849 "createkey opened existing %s\n",
1850 service_name));
1852 /* delete posibly existing value */
1853 status = rpc_conf_del_value(frame,
1855 &key_hnd,
1856 service_name,
1857 param_name,
1858 &werr);
1860 if (!(NT_STATUS_IS_OK(status))) {
1861 goto error;
1864 if (!(W_ERROR_IS_OK(werr))) {
1865 goto error;
1868 break;
1872 * check if parameter is valid for writing
1875 if (!net_conf_param_valid(service_name, param_name, valstr)) {
1876 werr = WERR_INVALID_PARAMETER;
1877 goto error;
1880 /* set the parameter */
1881 status = dcerpc_winreg_set_sz(frame, b, &share_hnd,
1882 param_name, valstr, &werr);
1884 if (!(NT_STATUS_IS_OK(status))) {
1885 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1886 " with value %s\n %s\n",
1887 param_name, valstr, nt_errstr(status));
1888 goto error;
1891 if (!(W_ERROR_IS_OK(werr))) {
1892 d_fprintf(stderr, "ERROR: Could not set parameter '%s'"
1893 " with value %s\n %s\n",
1894 param_name, valstr, win_errstr(werr));
1895 goto error;
1898 error:
1900 if (!(W_ERROR_IS_OK(werr))) {
1901 status = werror_to_ntstatus(werr);
1904 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1905 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1906 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
1908 TALLOC_FREE(frame);
1909 return status;
1912 static NTSTATUS rpc_conf_delparm_internal(struct net_context *c,
1913 const struct dom_sid *domain_sid,
1914 const char *domain_name,
1915 struct cli_state *cli,
1916 struct rpc_pipe_client *pipe_hnd,
1917 TALLOC_CTX *mem_ctx,
1918 int argc,
1919 const char **argv )
1921 TALLOC_CTX *frame = talloc_stackframe();
1922 NTSTATUS status = NT_STATUS_OK;
1923 WERROR werr = WERR_OK;
1924 WERROR _werr;
1926 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1928 /* key info */
1929 struct policy_handle hive_hnd, key_hnd;
1932 ZERO_STRUCT(hive_hnd);
1933 ZERO_STRUCT(key_hnd);
1936 if (argc != 2 || c->display_usage) {
1937 rpc_conf_delparm_usage(c, argc, argv);
1938 status = NT_STATUS_INVALID_PARAMETER;
1939 goto error;
1942 status = rpc_conf_open_conf(frame,
1944 REG_KEY_READ,
1945 &hive_hnd,
1946 &key_hnd,
1947 &werr);
1949 if (!(NT_STATUS_IS_OK(status))) {
1950 goto error;
1953 if (!(W_ERROR_IS_OK(werr))) {
1954 goto error;
1957 status = rpc_conf_del_value(frame,
1959 &key_hnd,
1960 argv[0],
1961 argv[1],
1962 &werr);
1964 error:
1966 if (!(W_ERROR_IS_OK(werr))) {
1967 status = werror_to_ntstatus(werr);
1970 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
1971 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
1973 TALLOC_FREE(frame);
1974 return status;
1978 static NTSTATUS rpc_conf_getincludes_internal(struct net_context *c,
1979 const struct dom_sid *domain_sid,
1980 const char *domain_name,
1981 struct cli_state *cli,
1982 struct rpc_pipe_client *pipe_hnd,
1983 TALLOC_CTX *mem_ctx,
1984 int argc,
1985 const char **argv )
1987 TALLOC_CTX *frame = talloc_stackframe();
1988 NTSTATUS status = NT_STATUS_OK;
1989 WERROR werr = WERR_OK;
1990 WERROR _werr;
1992 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
1994 /* key info */
1995 struct policy_handle hive_hnd, key_hnd;
1996 struct smbconf_service *service = NULL;
1998 uint32_t param_count;
2001 ZERO_STRUCT(hive_hnd);
2002 ZERO_STRUCT(key_hnd);
2005 if (argc != 1 || c->display_usage) {
2006 rpc_conf_getincludes_usage(c, argc, argv);
2007 status = NT_STATUS_INVALID_PARAMETER;
2008 goto error;
2011 status = rpc_conf_open_conf(frame,
2013 REG_KEY_READ,
2014 &hive_hnd,
2015 &key_hnd,
2016 &werr);
2018 if (!(NT_STATUS_IS_OK(status))) {
2019 goto error;
2022 if (!(W_ERROR_IS_OK(werr))) {
2023 goto error;
2026 service = talloc(frame, struct smbconf_service);
2028 status = rpc_conf_get_share(frame,
2030 &key_hnd,
2031 argv[0],
2032 service,
2033 &werr);
2035 if (!(NT_STATUS_IS_OK(status))) {
2036 goto error;
2039 if (!(W_ERROR_IS_OK(werr))) {
2040 goto error;
2043 for (param_count = 0;
2044 param_count < service->num_params;
2045 param_count++)
2047 if (strcmp(service->param_names[param_count], "include") == 0) {
2048 d_printf(_("%s = %s\n"),
2049 service->param_names[param_count],
2050 service->param_values[param_count]);
2054 error:
2056 if (!(W_ERROR_IS_OK(werr))) {
2057 status = werror_to_ntstatus(werr);
2060 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
2061 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
2063 TALLOC_FREE(frame);
2064 return status;
2068 static NTSTATUS rpc_conf_setincludes_internal(struct net_context *c,
2069 const struct dom_sid *domain_sid,
2070 const char *domain_name,
2071 struct cli_state *cli,
2072 struct rpc_pipe_client *pipe_hnd,
2073 TALLOC_CTX *mem_ctx,
2074 int argc,
2075 const char **argv )
2077 TALLOC_CTX *frame = talloc_stackframe();
2078 NTSTATUS status = NT_STATUS_OK;
2079 WERROR werr = WERR_OK;
2080 WERROR _werr;
2082 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2084 /* key info */
2085 struct policy_handle hive_hnd, key_hnd, share_hnd;
2087 struct winreg_String key, keyclass;
2088 enum winreg_CreateAction action = 0;
2090 ZERO_STRUCT(hive_hnd);
2091 ZERO_STRUCT(key_hnd);
2092 ZERO_STRUCT(share_hnd);
2094 ZERO_STRUCT(key);
2095 ZERO_STRUCT(keyclass);
2097 if (argc < 1 || c->display_usage) {
2098 rpc_conf_setincludes_usage(c, argc, argv);
2099 status = NT_STATUS_INVALID_PARAMETER;
2100 goto error;
2103 status = rpc_conf_open_conf(frame,
2105 REG_KEY_READ,
2106 &hive_hnd,
2107 &key_hnd,
2108 &werr);
2110 if (!(NT_STATUS_IS_OK(status))) {
2111 goto error;
2114 if (!(W_ERROR_IS_OK(werr))) {
2115 goto error;
2118 key.name = argv[0];
2119 keyclass.name = "";
2121 status = dcerpc_winreg_CreateKey(b, frame, &key_hnd, key, keyclass,
2122 0, REG_KEY_READ, NULL, &share_hnd,
2123 &action, &werr);
2125 if (!(NT_STATUS_IS_OK(status))) {
2126 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
2127 argv[0], nt_errstr(status));
2128 goto error;
2131 if (!W_ERROR_IS_OK(werr)) {
2132 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
2133 argv[0], win_errstr(werr));
2134 goto error;
2137 switch (action) {
2138 case REG_ACTION_NONE:
2139 /* Is there any other way to treat this? */
2140 werr = WERR_CREATE_FAILED;
2141 d_fprintf(stderr, _("ERROR: Could not create share key '%s'\n%s\n"),
2142 argv[0], win_errstr(werr));
2143 goto error;
2144 case REG_CREATED_NEW_KEY:
2145 DEBUG(5, ("net rpc conf setincludes:"
2146 "createkey created %s\n", argv[0]));
2147 break;
2148 case REG_OPENED_EXISTING_KEY:
2149 DEBUG(5, ("net rpc conf setincludes:"
2150 "createkey opened existing %s\n", argv[0]));
2152 /* delete posibly existing value */
2153 status = rpc_conf_del_value(frame,
2155 &key_hnd,
2156 argv[0],
2157 "includes",
2158 &werr);
2160 if (!(NT_STATUS_IS_OK(status))) {
2161 goto error;
2164 if (!(W_ERROR_IS_OK(werr))) {
2165 goto error;
2167 break;
2170 /* set the 'includes' values */
2171 status = dcerpc_winreg_set_multi_sz(frame, b, &share_hnd,
2172 "includes", argv + 1, &werr);
2173 if (!(NT_STATUS_IS_OK(status))) {
2174 d_fprintf(stderr, "ERROR: Could not set includes\n %s\n",
2175 nt_errstr(status));
2176 goto error;
2179 if (!(W_ERROR_IS_OK(werr))) {
2180 d_fprintf(stderr, "ERROR: Could not set includes\n %s\n",
2181 win_errstr(werr));
2182 goto error;
2185 error:
2187 if (!(W_ERROR_IS_OK(werr))) {
2188 status = werror_to_ntstatus(werr);
2191 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
2192 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
2193 dcerpc_winreg_CloseKey(b, frame, &share_hnd, &_werr);
2195 TALLOC_FREE(frame);
2196 return status;
2199 static NTSTATUS rpc_conf_delincludes_internal(struct net_context *c,
2200 const struct dom_sid *domain_sid,
2201 const char *domain_name,
2202 struct cli_state *cli,
2203 struct rpc_pipe_client *pipe_hnd,
2204 TALLOC_CTX *mem_ctx,
2205 int argc,
2206 const char **argv )
2208 TALLOC_CTX *frame = talloc_stackframe();
2209 NTSTATUS status = NT_STATUS_OK;
2210 WERROR werr = WERR_OK;
2211 WERROR _werr;
2213 struct dcerpc_binding_handle *b = pipe_hnd->binding_handle;
2215 /* key info */
2216 struct policy_handle hive_hnd, key_hnd;
2219 ZERO_STRUCT(hive_hnd);
2220 ZERO_STRUCT(key_hnd);
2223 if (argc != 1 || c->display_usage) {
2224 rpc_conf_delincludes_usage(c, argc, argv);
2225 status = NT_STATUS_INVALID_PARAMETER;
2226 goto error;
2229 status = rpc_conf_open_conf(frame,
2231 REG_KEY_READ,
2232 &hive_hnd,
2233 &key_hnd,
2234 &werr);
2236 if (!(NT_STATUS_IS_OK(status))) {
2237 goto error;
2240 if (!(W_ERROR_IS_OK(werr))) {
2241 goto error;
2244 status = rpc_conf_del_value(frame,
2246 &key_hnd,
2247 argv[0],
2248 "includes",
2249 &werr);
2251 error:
2253 if (!(W_ERROR_IS_OK(werr))) {
2254 status = werror_to_ntstatus(werr);
2257 dcerpc_winreg_CloseKey(b, frame, &hive_hnd, &_werr);
2258 dcerpc_winreg_CloseKey(b, frame, &key_hnd, &_werr);
2260 TALLOC_FREE(frame);
2261 return status;
2265 /**********************************************************
2267 * Functions that run the rpc commands for net rpc conf modules
2269 **********************************************************/
2271 static int rpc_conf_drop(struct net_context *c, int argc,
2272 const char **argv)
2274 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2275 rpc_conf_drop_internal, argc, argv );
2279 static int rpc_conf_showshare(struct net_context *c, int argc,
2280 const char **argv)
2282 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2283 rpc_conf_showshare_internal, argc, argv );
2286 static int rpc_conf_addshare(struct net_context *c, int argc,
2287 const char **argv)
2289 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2290 rpc_conf_addshare_internal, argc, argv );
2293 static int rpc_conf_listshares(struct net_context *c, int argc,
2294 const char **argv)
2296 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2297 rpc_conf_listshares_internal, argc, argv );
2300 static int rpc_conf_list(struct net_context *c, int argc,
2301 const char **argv)
2303 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2304 rpc_conf_list_internal, argc, argv );
2307 static int rpc_conf_import(struct net_context *c, int argc,
2308 const char **argv)
2310 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2311 rpc_conf_import_internal, argc, argv );
2313 static int rpc_conf_delshare(struct net_context *c, int argc,
2314 const char **argv)
2316 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2317 rpc_conf_delshare_internal, argc, argv );
2320 static int rpc_conf_getparm(struct net_context *c, int argc,
2321 const char **argv)
2323 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2324 rpc_conf_getparm_internal, argc, argv );
2327 static int rpc_conf_setparm(struct net_context *c, int argc,
2328 const char **argv)
2330 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2331 rpc_conf_setparm_internal, argc, argv );
2333 static int rpc_conf_delparm(struct net_context *c, int argc,
2334 const char **argv)
2336 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2337 rpc_conf_delparm_internal, argc, argv );
2340 static int rpc_conf_getincludes(struct net_context *c, int argc,
2341 const char **argv)
2343 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2344 rpc_conf_getincludes_internal, argc, argv );
2347 static int rpc_conf_setincludes(struct net_context *c, int argc,
2348 const char **argv)
2350 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2351 rpc_conf_setincludes_internal, argc, argv );
2354 static int rpc_conf_delincludes(struct net_context *c, int argc,
2355 const char **argv)
2357 return run_rpc_command(c, NULL, &ndr_table_winreg, 0,
2358 rpc_conf_delincludes_internal, argc, argv );
2361 /* function calls */
2362 int net_rpc_conf(struct net_context *c, int argc,
2363 const char **argv)
2365 struct functable func_table[] = {
2367 "list",
2368 rpc_conf_list,
2369 NET_TRANSPORT_RPC,
2370 N_("Dump the complete remote configuration in smb.conf like "
2371 "format."),
2372 N_("net rpc conf list\n"
2373 " Dump the complete remote configuration in smb.conf "
2374 "like format.")
2378 "import",
2379 rpc_conf_import,
2380 NET_TRANSPORT_RPC,
2381 N_("Import configuration from file in smb.conf "
2382 "format."),
2383 N_("net rpc conf import\n"
2384 " Import configuration from file in smb.conf "
2385 "format.")
2388 "listshares",
2389 rpc_conf_listshares,
2390 NET_TRANSPORT_RPC,
2391 N_("List the remote share names."),
2392 N_("net rpc conf list\n"
2393 " List the remote share names.")
2397 "drop",
2398 rpc_conf_drop,
2399 NET_TRANSPORT_RPC,
2400 N_("Delete the complete remote configuration."),
2401 N_("net rpc conf drop\n"
2402 " Delete the complete remote configuration.")
2406 "showshare",
2407 rpc_conf_showshare,
2408 NET_TRANSPORT_RPC,
2409 N_("Show the definition of a remote share."),
2410 N_("net rpc conf showshare\n"
2411 " Show the definition of a remote share.")
2415 "addshare",
2416 rpc_conf_addshare,
2417 NET_TRANSPORT_RPC,
2418 N_("Create a new remote share."),
2419 N_("net rpc conf addshare\n"
2420 " Create a new remote share.")
2423 "delshare",
2424 rpc_conf_delshare,
2425 NET_TRANSPORT_RPC,
2426 N_("Delete a remote share."),
2427 N_("net rpc conf delshare\n"
2428 " Delete a remote share.")
2431 "getparm",
2432 rpc_conf_getparm,
2433 NET_TRANSPORT_RPC,
2434 N_("Retrieve the value of a parameter."),
2435 N_("net rpc conf getparm\n"
2436 " Retrieve the value of a parameter.")
2439 "setparm",
2440 rpc_conf_setparm,
2441 NET_TRANSPORT_RPC,
2442 N_("Store a parameter."),
2443 N_("net rpc conf setparm\n"
2444 " Store a parameter.")
2447 "delparm",
2448 rpc_conf_delparm,
2449 NET_TRANSPORT_RPC,
2450 N_("Delete a parameter."),
2451 N_("net rpc conf delparm\n"
2452 " Delete a parameter.")
2455 "getincludes",
2456 rpc_conf_getincludes,
2457 NET_TRANSPORT_RPC,
2458 N_("Show the includes of a share definition."),
2459 N_("net rpc conf getincludes\n"
2460 " Show the includes of a share definition.")
2463 "setincludes",
2464 rpc_conf_setincludes,
2465 NET_TRANSPORT_RPC,
2466 N_("Set includes for a share."),
2467 N_("net rpc conf setincludes\n"
2468 " Set includes for a share.")
2471 "delincludes",
2472 rpc_conf_delincludes,
2473 NET_TRANSPORT_RPC,
2474 N_("Delete includes from a share definition."),
2475 N_("net rpc conf delincludes\n"
2476 " Delete includes from a share definition.")
2478 {NULL, NULL, 0, NULL, NULL}
2481 return net_run_function(c, argc, argv, "net rpc conf", func_table);