net conf: fix output of out-of-share parameters in test mode import
[Samba.git] / source / utils / net_conf.c
blob7d1658ba94a0196f3f5b1d42768fe3ea808fa642
1 /*
2 * Samba Unix/Linux SMB client library
3 * Distributed SMB/CIFS Server Management Utility
4 * Local configuration interface
5 * Copyright (C) Michael Adam 2007-2008
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 as made available
23 * by the libsmbconf interface (source/lib/smbconf/smbconf.c).
25 * This currently supports local interaction with the configuration
26 * stored in the registry. But other backends and remote access via
27 * rpc might get implemented in the future.
30 #include "includes.h"
31 #include "utils/net.h"
33 /**********************************************************************
35 * usage functions
37 **********************************************************************/
39 static int net_conf_list_usage(int argc, const char **argv)
41 d_printf("USAGE: net conf list\n");
42 return -1;
45 static int net_conf_import_usage(int argc, const char**argv)
47 d_printf("USAGE: net conf import [--test|-T] <filename> "
48 "[<servicename>]\n"
49 "\t[--test|-T] testmode - do not act, just print "
50 "what would be done\n"
51 "\t<servicename> only import service <servicename>, "
52 "ignore the rest\n");
53 return -1;
56 static int net_conf_listshares_usage(int argc, const char **argv)
58 d_printf("USAGE: net conf listshares\n");
59 return -1;
62 static int net_conf_drop_usage(int argc, const char **argv)
64 d_printf("USAGE: net conf drop\n");
65 return -1;
68 static int net_conf_showshare_usage(int argc, const char **argv)
70 d_printf("USAGE: net conf showshare <sharename>\n");
71 return -1;
74 static int net_conf_addshare_usage(int argc, const char **argv)
76 d_printf("USAGE: net conf addshare <sharename> <path> "
77 "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
78 "\t<sharename> the new share name.\n"
79 "\t<path> the path on the filesystem to export.\n"
80 "\twriteable={y|N} set \"writeable to \"yes\" or "
81 "\"no\" (default) on this share.\n"
82 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
83 "\"no\" (default) on this share.\n"
84 "\t<comment> optional comment for the new share.\n");
85 return -1;
88 static int net_conf_delshare_usage(int argc, const char **argv)
90 d_printf("USAGE: net conf delshare <sharename>\n");
91 return -1;
94 static int net_conf_setparm_usage(int argc, const char **argv)
96 d_printf("USAGE: net conf setparm <section> <param> <value>\n");
97 return -1;
100 static int net_conf_getparm_usage(int argc, const char **argv)
102 d_printf("USAGE: net conf getparm <section> <param>\n");
103 return -1;
106 static int net_conf_delparm_usage(int argc, const char **argv)
108 d_printf("USAGE: net conf delparm <section> <param>\n");
109 return -1;
112 static int net_conf_getincludes_usage(int argc, const char **argv)
114 d_printf("USAGE: net conf getincludes <section>\n");
115 return -1;
118 static int net_conf_setincludes_usage(int argc, const char **argv)
120 d_printf("USAGE: net conf setincludes <section> [<filename>]*\n");
121 return -1;
124 static int net_conf_delincludes_usage(int argc, const char **argv)
126 d_printf("USAGE: net conf delincludes <section>\n");
127 return -1;
131 /**********************************************************************
133 * Helper functions
135 **********************************************************************/
138 * This functions process a service previously loaded with libsmbconf.
140 static WERROR import_process_service(struct smbconf_ctx *conf_ctx,
141 const char *servicename,
142 const uint32_t num_params,
143 const char **param_names,
144 const char **param_values)
146 uint32_t idx;
147 WERROR werr = WERR_OK;
148 uint32_t num_includes = 0;
149 char **includes = NULL;
150 TALLOC_CTX *mem_ctx = talloc_stackframe();
152 if (opt_testmode) {
153 if (servicename != NULL) {
154 d_printf("[%s]\n", servicename);
155 for (idx = 0; idx < num_params; idx++) {
156 d_printf("\t%s = %s\n", param_names[idx],
157 param_values[idx]);
160 else {
161 for (idx = 0; idx < num_params; idx++) {
162 d_printf("%s = %s\n", param_names[idx],
163 param_values[idx]);
166 d_printf("\n");
167 goto done;
170 if (smbconf_share_exists(conf_ctx, servicename)) {
171 werr = smbconf_delete_share(conf_ctx, servicename);
172 if (!W_ERROR_IS_OK(werr)) {
173 goto done;
176 werr = smbconf_create_share(conf_ctx, servicename);
177 if (!W_ERROR_IS_OK(werr)) {
178 goto done;
181 for (idx = 0; idx < num_params; idx ++) {
182 if (strequal(param_names[idx], "include")) {
183 includes = TALLOC_REALLOC_ARRAY(mem_ctx,
184 includes,
185 char *,
186 num_includes+1);
187 if (includes == NULL) {
188 werr = WERR_NOMEM;
189 goto done;
191 includes[num_includes] = talloc_strdup(includes,
192 param_values[idx]);
193 if (includes[num_includes] == NULL) {
194 werr = WERR_NOMEM;
195 goto done;
197 num_includes++;
198 } else {
199 werr = smbconf_set_parameter(conf_ctx,
200 servicename,
201 param_names[idx],
202 param_values[idx]);
203 if (!W_ERROR_IS_OK(werr)) {
204 goto done;
209 werr = smbconf_set_includes(conf_ctx, servicename, num_includes,
210 (const char **)includes);
212 done:
213 TALLOC_FREE(mem_ctx);
214 return werr;
218 /**********************************************************************
220 * the main conf functions
222 **********************************************************************/
224 static int net_conf_list(struct smbconf_ctx *conf_ctx,
225 int argc, const char **argv)
227 WERROR werr = WERR_OK;
228 int ret = -1;
229 TALLOC_CTX *mem_ctx;
230 uint32_t num_shares;
231 char **share_names;
232 uint32_t *num_params;
233 char ***param_names;
234 char ***param_values;
235 uint32_t share_count, param_count;
237 mem_ctx = talloc_stackframe();
239 if (argc != 0) {
240 net_conf_list_usage(argc, argv);
241 goto done;
244 werr = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &share_names,
245 &num_params, &param_names, &param_values);
246 if (!W_ERROR_IS_OK(werr)) {
247 d_fprintf(stderr, "Error getting config: %s\n",
248 dos_errstr(werr));
249 goto done;
252 for (share_count = 0; share_count < num_shares; share_count++) {
253 d_printf("[%s]\n", share_names[share_count]);
254 for (param_count = 0; param_count < num_params[share_count];
255 param_count++)
257 d_printf("\t%s = %s\n",
258 param_names[share_count][param_count],
259 param_values[share_count][param_count]);
261 d_printf("\n");
264 ret = 0;
266 done:
267 TALLOC_FREE(mem_ctx);
268 return ret;
271 static int net_conf_import(struct smbconf_ctx *conf_ctx,
272 int argc, const char **argv)
274 int ret = -1;
275 const char *filename = NULL;
276 const char *servicename = NULL;
277 char *conf_source = NULL;
278 TALLOC_CTX *mem_ctx;
279 struct smbconf_ctx *txt_ctx;
280 WERROR werr;
282 mem_ctx = talloc_stackframe();
284 switch (argc) {
285 case 0:
286 default:
287 net_conf_import_usage(argc, argv);
288 goto done;
289 case 2:
290 servicename = talloc_strdup_lower(mem_ctx, argv[1]);
291 if (servicename == NULL) {
292 d_printf("error: out of memory!\n");
293 goto done;
295 case 1:
296 filename = argv[0];
297 break;
300 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
301 filename));
303 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
304 if (conf_source == NULL) {
305 d_printf("error: out of memory!\n");
306 goto done;
309 werr = smbconf_init(mem_ctx, &txt_ctx, conf_source);
310 if (!W_ERROR_IS_OK(werr)) {
311 d_printf("error loading file '%s': %s\n", filename,
312 dos_errstr(werr));
313 goto done;
316 if (opt_testmode) {
317 d_printf("\nTEST MODE - "
318 "would import the following configuration:\n\n");
321 if (servicename != NULL) {
322 char **param_names, **param_values;
323 uint32_t num_params;
325 werr = smbconf_get_share(txt_ctx, mem_ctx,
326 servicename,
327 &num_params,
328 &param_names,
329 &param_values);
330 if (!W_ERROR_IS_OK(werr)) {
331 goto done;
333 werr = import_process_service(conf_ctx,
334 servicename,
335 num_params,
336 (const char **)param_names,
337 (const char **)param_values);
338 if (!W_ERROR_IS_OK(werr)) {
339 goto done;
341 } else {
342 char **share_names, ***param_names, ***param_values;
343 uint32_t num_shares, *num_params, sidx;
345 werr = smbconf_get_config(txt_ctx, mem_ctx,
346 &num_shares,
347 &share_names,
348 &num_params,
349 &param_names,
350 &param_values);
351 if (!W_ERROR_IS_OK(werr)) {
352 goto done;
354 if (!opt_testmode) {
355 werr = smbconf_drop(conf_ctx);
356 if (!W_ERROR_IS_OK(werr)) {
357 goto done;
360 for (sidx = 0; sidx < num_shares; sidx++) {
361 werr = import_process_service(conf_ctx,
362 share_names[sidx],
363 num_params[sidx],
364 (const char **)param_names[sidx],
365 (const char **)param_values[sidx]);
366 if (!W_ERROR_IS_OK(werr)) {
367 goto done;
372 ret = 0;
374 done:
375 TALLOC_FREE(mem_ctx);
376 return ret;
379 static int net_conf_listshares(struct smbconf_ctx *conf_ctx,
380 int argc, const char **argv)
382 WERROR werr = WERR_OK;
383 int ret = -1;
384 uint32_t count, num_shares = 0;
385 char **share_names = NULL;
386 TALLOC_CTX *mem_ctx;
388 mem_ctx = talloc_stackframe();
390 if (argc != 0) {
391 net_conf_listshares_usage(argc, argv);
392 goto done;
395 werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
396 &share_names);
397 if (!W_ERROR_IS_OK(werr)) {
398 goto done;
401 for (count = 0; count < num_shares; count++)
403 d_printf("%s\n", share_names[count]);
406 ret = 0;
408 done:
409 TALLOC_FREE(mem_ctx);
410 return ret;
413 static int net_conf_drop(struct smbconf_ctx *conf_ctx,
414 int argc, const char **argv)
416 int ret = -1;
417 WERROR werr;
419 if (argc != 0) {
420 net_conf_drop_usage(argc, argv);
421 goto done;
424 werr = smbconf_drop(conf_ctx);
425 if (!W_ERROR_IS_OK(werr)) {
426 d_fprintf(stderr, "Error deleting configuration: %s\n",
427 dos_errstr(werr));
428 goto done;
431 ret = 0;
433 done:
434 return ret;
437 static int net_conf_showshare(struct smbconf_ctx *conf_ctx,
438 int argc, const char **argv)
440 int ret = -1;
441 WERROR werr = WERR_OK;
442 const char *sharename = NULL;
443 TALLOC_CTX *mem_ctx;
444 uint32_t num_params;
445 uint32_t count;
446 char **param_names;
447 char **param_values;
449 mem_ctx = talloc_stackframe();
451 if (argc != 1) {
452 net_conf_showshare_usage(argc, argv);
453 goto done;
456 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
457 if (sharename == NULL) {
458 d_printf("error: out of memory!\n");
459 goto done;
462 werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &num_params,
463 &param_names, &param_values);
464 if (!W_ERROR_IS_OK(werr)) {
465 d_printf("error getting share parameters: %s\n",
466 dos_errstr(werr));
467 goto done;
470 d_printf("[%s]\n", sharename);
472 for (count = 0; count < num_params; count++) {
473 d_printf("\t%s = %s\n", param_names[count],
474 param_values[count]);
477 ret = 0;
479 done:
480 TALLOC_FREE(mem_ctx);
481 return ret;
485 * Add a share, with a couple of standard parameters, partly optional.
487 * This is a high level utility function of the net conf utility,
488 * not a direct frontend to the smbconf API.
490 static int net_conf_addshare(struct smbconf_ctx *conf_ctx,
491 int argc, const char **argv)
493 int ret = -1;
494 WERROR werr = WERR_OK;
495 char *sharename = NULL;
496 const char *path = NULL;
497 const char *comment = NULL;
498 const char *guest_ok = "no";
499 const char *writeable = "no";
500 SMB_STRUCT_STAT sbuf;
501 TALLOC_CTX *mem_ctx = talloc_stackframe();
503 switch (argc) {
504 case 0:
505 case 1:
506 default:
507 net_conf_addshare_usage(argc, argv);
508 goto done;
509 case 5:
510 comment = argv[4];
511 case 4:
512 if (!strnequal(argv[3], "guest_ok=", 9)) {
513 net_conf_addshare_usage(argc, argv);
514 goto done;
516 switch (argv[3][9]) {
517 case 'y':
518 case 'Y':
519 guest_ok = "yes";
520 break;
521 case 'n':
522 case 'N':
523 guest_ok = "no";
524 break;
525 default:
526 net_conf_addshare_usage(argc, argv);
527 goto done;
529 case 3:
530 if (!strnequal(argv[2], "writeable=", 10)) {
531 net_conf_addshare_usage(argc, argv);
532 goto done;
534 switch (argv[2][10]) {
535 case 'y':
536 case 'Y':
537 writeable = "yes";
538 break;
539 case 'n':
540 case 'N':
541 writeable = "no";
542 break;
543 default:
544 net_conf_addshare_usage(argc, argv);
545 goto done;
547 case 2:
548 path = argv[1];
549 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
550 if (sharename == NULL) {
551 d_printf("error: out of memory!\n");
552 goto done;
555 break;
559 * validate arguments
562 /* validate share name */
564 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
565 strlen(sharename)))
567 d_fprintf(stderr, "ERROR: share name %s contains "
568 "invalid characters (any of %s)\n",
569 sharename, INVALID_SHARENAME_CHARS);
570 goto done;
573 if (getpwnam(sharename)) {
574 d_fprintf(stderr, "ERROR: share name %s is already a valid "
575 "system user name.\n", sharename);
576 goto done;
579 if (strequal(sharename, GLOBAL_NAME)) {
580 d_fprintf(stderr,
581 "ERROR: 'global' is not a valid share name.\n");
582 goto done;
585 if (smbconf_share_exists(conf_ctx, sharename)) {
586 d_fprintf(stderr, "ERROR: share %s already exists.\n",
587 sharename);
588 goto done;
591 /* validate path */
593 if (path[0] != '/') {
594 d_fprintf(stderr,
595 "Error: path '%s' is not an absolute path.\n",
596 path);
597 goto done;
600 if (sys_stat(path, &sbuf) != 0) {
601 d_fprintf(stderr,
602 "ERROR: cannot stat path '%s' to ensure "
603 "this is a directory.\n"
604 "Error was '%s'.\n",
605 path, strerror(errno));
606 goto done;
609 if (!S_ISDIR(sbuf.st_mode)) {
610 d_fprintf(stderr,
611 "ERROR: path '%s' is not a directory.\n",
612 path);
613 goto done;
617 * create the share
620 werr = smbconf_create_share(conf_ctx, sharename);
621 if (!W_ERROR_IS_OK(werr)) {
622 d_fprintf(stderr, "Error creating share %s: %s\n",
623 sharename, dos_errstr(werr));
624 goto done;
628 * fill the share with parameters
631 werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
632 if (!W_ERROR_IS_OK(werr)) {
633 d_fprintf(stderr, "Error setting parameter %s: %s\n",
634 "path", dos_errstr(werr));
635 goto done;
638 if (comment != NULL) {
639 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
640 comment);
641 if (!W_ERROR_IS_OK(werr)) {
642 d_fprintf(stderr, "Error setting parameter %s: %s\n",
643 "comment", dos_errstr(werr));
644 goto done;
648 werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
649 if (!W_ERROR_IS_OK(werr)) {
650 d_fprintf(stderr, "Error setting parameter %s: %s\n",
651 "'guest ok'", dos_errstr(werr));
652 goto done;
655 werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
656 writeable);
657 if (!W_ERROR_IS_OK(werr)) {
658 d_fprintf(stderr, "Error setting parameter %s: %s\n",
659 "writeable", dos_errstr(werr));
660 goto done;
663 ret = 0;
665 done:
666 TALLOC_FREE(mem_ctx);
667 return ret;
670 static int net_conf_delshare(struct smbconf_ctx *conf_ctx,
671 int argc, const char **argv)
673 int ret = -1;
674 const char *sharename = NULL;
675 WERROR werr = WERR_OK;
676 TALLOC_CTX *mem_ctx = talloc_stackframe();
678 if (argc != 1) {
679 net_conf_delshare_usage(argc, argv);
680 goto done;
682 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
683 if (sharename == NULL) {
684 d_printf("error: out of memory!\n");
685 goto done;
688 werr = smbconf_delete_share(conf_ctx, sharename);
689 if (!W_ERROR_IS_OK(werr)) {
690 d_fprintf(stderr, "Error deleting share %s: %s\n",
691 sharename, dos_errstr(werr));
692 goto done;
695 ret = 0;
696 done:
697 TALLOC_FREE(mem_ctx);
698 return ret;
701 static int net_conf_setparm(struct smbconf_ctx *conf_ctx,
702 int argc, const char **argv)
704 int ret = -1;
705 WERROR werr = WERR_OK;
706 char *service = NULL;
707 char *param = NULL;
708 const char *value_str = NULL;
709 TALLOC_CTX *mem_ctx = talloc_stackframe();
711 if (argc != 3) {
712 net_conf_setparm_usage(argc, argv);
713 goto done;
715 service = talloc_strdup_lower(mem_ctx, argv[0]);
716 if (service == NULL) {
717 d_printf("error: out of memory!\n");
718 goto done;
720 param = talloc_strdup_lower(mem_ctx, argv[1]);
721 if (param == NULL) {
722 d_printf("error: out of memory!\n");
723 goto done;
725 value_str = argv[2];
727 if (!smbconf_share_exists(conf_ctx, service)) {
728 werr = smbconf_create_share(conf_ctx, service);
729 if (!W_ERROR_IS_OK(werr)) {
730 d_fprintf(stderr, "Error creating share '%s': %s\n",
731 service, dos_errstr(werr));
732 goto done;
736 werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
738 if (!W_ERROR_IS_OK(werr)) {
739 d_fprintf(stderr, "Error setting value '%s': %s\n",
740 param, dos_errstr(werr));
741 goto done;
744 ret = 0;
746 done:
747 TALLOC_FREE(mem_ctx);
748 return ret;
751 static int net_conf_getparm(struct smbconf_ctx *conf_ctx,
752 int argc, const char **argv)
754 int ret = -1;
755 WERROR werr = WERR_OK;
756 char *service = NULL;
757 char *param = NULL;
758 char *valstr = NULL;
759 TALLOC_CTX *mem_ctx;
761 mem_ctx = talloc_stackframe();
763 if (argc != 2) {
764 net_conf_getparm_usage(argc, argv);
765 goto done;
767 service = talloc_strdup_lower(mem_ctx, argv[0]);
768 if (service == NULL) {
769 d_printf("error: out of memory!\n");
770 goto done;
772 param = talloc_strdup_lower(mem_ctx, argv[1]);
773 if (param == NULL) {
774 d_printf("error: out of memory!\n");
775 goto done;
778 werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
780 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
781 d_fprintf(stderr,
782 "Error: given service '%s' does not exist.\n",
783 service);
784 goto done;
785 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
786 d_fprintf(stderr,
787 "Error: given parameter '%s' is not set.\n",
788 param);
789 goto done;
790 } else if (!W_ERROR_IS_OK(werr)) {
791 d_fprintf(stderr, "Error getting value '%s': %s.\n",
792 param, dos_errstr(werr));
793 goto done;
796 d_printf("%s\n", valstr);
798 ret = 0;
799 done:
800 TALLOC_FREE(mem_ctx);
801 return ret;
804 static int net_conf_delparm(struct smbconf_ctx *conf_ctx,
805 int argc, const char **argv)
807 int ret = -1;
808 WERROR werr = WERR_OK;
809 char *service = NULL;
810 char *param = NULL;
811 TALLOC_CTX *mem_ctx = talloc_stackframe();
813 if (argc != 2) {
814 net_conf_delparm_usage(argc, argv);
815 goto done;
817 service = talloc_strdup_lower(mem_ctx, argv[0]);
818 if (service == NULL) {
819 d_printf("error: out of memory!\n");
820 goto done;
822 param = talloc_strdup_lower(mem_ctx, argv[1]);
823 if (param == NULL) {
824 d_printf("error: out of memory!\n");
825 goto done;
828 werr = smbconf_delete_parameter(conf_ctx, service, param);
830 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
831 d_fprintf(stderr,
832 "Error: given service '%s' does not exist.\n",
833 service);
834 goto done;
835 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
836 d_fprintf(stderr,
837 "Error: given parameter '%s' is not set.\n",
838 param);
839 goto done;
840 } else if (!W_ERROR_IS_OK(werr)) {
841 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
842 param, dos_errstr(werr));
843 goto done;
846 ret = 0;
848 done:
849 TALLOC_FREE(mem_ctx);
850 return ret;
853 static int net_conf_getincludes(struct smbconf_ctx *conf_ctx,
854 int argc, const char **argv)
856 WERROR werr;
857 uint32_t num_includes;
858 uint32_t count;
859 char *service;
860 char **includes = NULL;
861 int ret = -1;
862 TALLOC_CTX *mem_ctx = talloc_stackframe();
864 if (argc != 1) {
865 net_conf_getincludes_usage(argc, argv);
866 goto done;
869 service = talloc_strdup_lower(mem_ctx, argv[0]);
870 if (service == NULL) {
871 d_printf("error: out of memory!\n");
872 goto done;
875 werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
876 &num_includes, &includes);
877 if (!W_ERROR_IS_OK(werr)) {
878 d_printf("error getting includes: %s\n", dos_errstr(werr));
879 goto done;
882 for (count = 0; count < num_includes; count++) {
883 d_printf("include = %s\n", includes[count]);
886 ret = 0;
888 done:
889 TALLOC_FREE(mem_ctx);
890 return ret;
893 static int net_conf_setincludes(struct smbconf_ctx *conf_ctx,
894 int argc, const char **argv)
896 WERROR werr;
897 char *service;
898 uint32_t num_includes;
899 const char **includes;
900 int ret = -1;
901 TALLOC_CTX *mem_ctx = talloc_stackframe();
903 if (argc < 1) {
904 net_conf_setincludes_usage(argc, argv);
905 goto done;
908 service = talloc_strdup_lower(mem_ctx, argv[0]);
909 if (service == NULL) {
910 d_printf("error: out of memory!\n");
911 goto done;
914 num_includes = argc - 1;
915 if (num_includes == 0) {
916 includes = NULL;
917 } else {
918 includes = argv + 1;
921 werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
922 if (!W_ERROR_IS_OK(werr)) {
923 d_printf("error setting includes: %s\n", dos_errstr(werr));
924 goto done;
927 ret = 0;
929 done:
930 TALLOC_FREE(mem_ctx);
931 return ret;
934 static int net_conf_delincludes(struct smbconf_ctx *conf_ctx,
935 int argc, const char **argv)
937 WERROR werr;
938 char *service;
939 int ret = -1;
940 TALLOC_CTX *mem_ctx = talloc_stackframe();
942 if (argc != 1) {
943 net_conf_delincludes_usage(argc, argv);
944 goto done;
947 service = talloc_strdup_lower(mem_ctx, argv[0]);
948 if (service == NULL) {
949 d_printf("error: out of memory!\n");
950 goto done;
953 werr = smbconf_delete_includes(conf_ctx, service);
954 if (!W_ERROR_IS_OK(werr)) {
955 d_printf("error deleting includes: %s\n", dos_errstr(werr));
956 goto done;
959 ret = 0;
961 done:
962 TALLOC_FREE(mem_ctx);
963 return ret;
967 /**********************************************************************
969 * Wrapper and net_conf_run_function mechanism.
971 **********************************************************************/
974 * Wrapper function to call the main conf functions.
975 * The wrapper calls handles opening and closing of the
976 * configuration.
978 static int net_conf_wrap_function(int (*fn)(struct smbconf_ctx *,
979 int, const char **),
980 int argc, const char **argv)
982 WERROR werr;
983 TALLOC_CTX *mem_ctx = talloc_stackframe();
984 struct smbconf_ctx *conf_ctx;
985 int ret = -1;
987 werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
989 if (!W_ERROR_IS_OK(werr)) {
990 return -1;
993 ret = fn(conf_ctx, argc, argv);
995 smbconf_shutdown(conf_ctx);
997 return ret;
1001 * We need a functable struct of our own, because the
1002 * functions are called through a wrapper that handles
1003 * the opening and closing of the configuration, and so on.
1005 struct conf_functable {
1006 const char *funcname;
1007 int (*fn)(struct smbconf_ctx *ctx, int argc, const char **argv);
1008 const char *helptext;
1012 * This imitates net_run_function2 but calls the main functions
1013 * through the wrapper net_conf_wrap_function().
1015 static int net_conf_run_function(int argc, const char **argv,
1016 const char *whoami,
1017 struct conf_functable *table)
1019 int i;
1021 if (argc != 0) {
1022 for (i=0; table[i].funcname; i++) {
1023 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1024 return net_conf_wrap_function(table[i].fn,
1025 argc-1,
1026 argv+1);
1030 for (i=0; table[i].funcname; i++) {
1031 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1032 table[i].helptext);
1035 return -1;
1039 * Entry-point for all the CONF functions.
1042 int net_conf(int argc, const char **argv)
1044 int ret = -1;
1045 struct conf_functable func_table[] = {
1046 {"list", net_conf_list,
1047 "Dump the complete configuration in smb.conf like format."},
1048 {"import", net_conf_import,
1049 "Import configuration from file in smb.conf format."},
1050 {"listshares", net_conf_listshares,
1051 "List the share names."},
1052 {"drop", net_conf_drop,
1053 "Delete the complete configuration."},
1054 {"showshare", net_conf_showshare,
1055 "Show the definition of a share."},
1056 {"addshare", net_conf_addshare,
1057 "Create a new share."},
1058 {"delshare", net_conf_delshare,
1059 "Delete a share."},
1060 {"setparm", net_conf_setparm,
1061 "Store a parameter."},
1062 {"getparm", net_conf_getparm,
1063 "Retrieve the value of a parameter."},
1064 {"delparm", net_conf_delparm,
1065 "Delete a parameter."},
1066 {"getincludes", net_conf_getincludes,
1067 "Show the includes of a share definition."},
1068 {"setincludes", net_conf_setincludes,
1069 "Set includes for a share."},
1070 {"delincludes", net_conf_delincludes,
1071 "Delete includes from a share definition."},
1072 {NULL, NULL, NULL}
1075 ret = net_conf_run_function(argc, argv, "net conf", func_table);
1077 return ret;