net: Add libnetapi context to net's impressive list of globals.
[Samba.git] / source / utils / net_conf.c
blob4fffcf8a8c25403bcad33c9064c2a0b62c699bc0
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 const char *indent = "";
154 if (servicename != NULL) {
155 d_printf("[%s]\n", servicename);
156 indent = "\t";
158 for (idx = 0; idx < num_params; idx++) {
159 d_printf("%s%s = %s\n", indent, param_names[idx],
160 param_values[idx]);
162 d_printf("\n");
163 goto done;
166 if (smbconf_share_exists(conf_ctx, servicename)) {
167 werr = smbconf_delete_share(conf_ctx, servicename);
168 if (!W_ERROR_IS_OK(werr)) {
169 goto done;
172 werr = smbconf_create_share(conf_ctx, servicename);
173 if (!W_ERROR_IS_OK(werr)) {
174 goto done;
177 for (idx = 0; idx < num_params; idx ++) {
178 if (strequal(param_names[idx], "include")) {
179 includes = TALLOC_REALLOC_ARRAY(mem_ctx,
180 includes,
181 char *,
182 num_includes+1);
183 if (includes == NULL) {
184 werr = WERR_NOMEM;
185 goto done;
187 includes[num_includes] = talloc_strdup(includes,
188 param_values[idx]);
189 if (includes[num_includes] == NULL) {
190 werr = WERR_NOMEM;
191 goto done;
193 num_includes++;
194 } else {
195 werr = smbconf_set_parameter(conf_ctx,
196 servicename,
197 param_names[idx],
198 param_values[idx]);
199 if (!W_ERROR_IS_OK(werr)) {
200 goto done;
205 werr = smbconf_set_includes(conf_ctx, servicename, num_includes,
206 (const char **)includes);
208 done:
209 TALLOC_FREE(mem_ctx);
210 return werr;
214 /**********************************************************************
216 * the main conf functions
218 **********************************************************************/
220 static int net_conf_list(struct smbconf_ctx *conf_ctx,
221 int argc, const char **argv)
223 WERROR werr = WERR_OK;
224 int ret = -1;
225 TALLOC_CTX *mem_ctx;
226 uint32_t num_shares;
227 char **share_names;
228 uint32_t *num_params;
229 char ***param_names;
230 char ***param_values;
231 uint32_t share_count, param_count;
233 mem_ctx = talloc_stackframe();
235 if (argc != 0) {
236 net_conf_list_usage(argc, argv);
237 goto done;
240 werr = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &share_names,
241 &num_params, &param_names, &param_values);
242 if (!W_ERROR_IS_OK(werr)) {
243 d_fprintf(stderr, "Error getting config: %s\n",
244 dos_errstr(werr));
245 goto done;
248 for (share_count = 0; share_count < num_shares; share_count++) {
249 const char *indent = "";
250 if (share_names[share_count] != NULL) {
251 d_printf("[%s]\n", share_names[share_count]);
252 indent = "\t";
254 for (param_count = 0; param_count < num_params[share_count];
255 param_count++)
257 d_printf("%s%s = %s\n",
258 indent,
259 param_names[share_count][param_count],
260 param_values[share_count][param_count]);
262 d_printf("\n");
265 ret = 0;
267 done:
268 TALLOC_FREE(mem_ctx);
269 return ret;
272 static int net_conf_import(struct smbconf_ctx *conf_ctx,
273 int argc, const char **argv)
275 int ret = -1;
276 const char *filename = NULL;
277 const char *servicename = NULL;
278 char *conf_source = NULL;
279 TALLOC_CTX *mem_ctx;
280 struct smbconf_ctx *txt_ctx;
281 WERROR werr;
283 mem_ctx = talloc_stackframe();
285 switch (argc) {
286 case 0:
287 default:
288 net_conf_import_usage(argc, argv);
289 goto done;
290 case 2:
291 servicename = talloc_strdup_lower(mem_ctx, argv[1]);
292 if (servicename == NULL) {
293 d_printf("error: out of memory!\n");
294 goto done;
296 case 1:
297 filename = argv[0];
298 break;
301 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
302 filename));
304 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
305 if (conf_source == NULL) {
306 d_printf("error: out of memory!\n");
307 goto done;
310 werr = smbconf_init(mem_ctx, &txt_ctx, conf_source);
311 if (!W_ERROR_IS_OK(werr)) {
312 d_printf("error loading file '%s': %s\n", filename,
313 dos_errstr(werr));
314 goto done;
317 if (opt_testmode) {
318 d_printf("\nTEST MODE - "
319 "would import the following configuration:\n\n");
322 if (servicename != NULL) {
323 char **param_names, **param_values;
324 uint32_t num_params;
326 werr = smbconf_get_share(txt_ctx, mem_ctx,
327 servicename,
328 &num_params,
329 &param_names,
330 &param_values);
331 if (!W_ERROR_IS_OK(werr)) {
332 goto done;
334 werr = import_process_service(conf_ctx,
335 servicename,
336 num_params,
337 (const char **)param_names,
338 (const char **)param_values);
339 if (!W_ERROR_IS_OK(werr)) {
340 goto done;
342 } else {
343 char **share_names, ***param_names, ***param_values;
344 uint32_t num_shares, *num_params, sidx;
346 werr = smbconf_get_config(txt_ctx, mem_ctx,
347 &num_shares,
348 &share_names,
349 &num_params,
350 &param_names,
351 &param_values);
352 if (!W_ERROR_IS_OK(werr)) {
353 goto done;
355 if (!opt_testmode) {
356 werr = smbconf_drop(conf_ctx);
357 if (!W_ERROR_IS_OK(werr)) {
358 goto done;
361 for (sidx = 0; sidx < num_shares; sidx++) {
362 werr = import_process_service(conf_ctx,
363 share_names[sidx],
364 num_params[sidx],
365 (const char **)param_names[sidx],
366 (const char **)param_values[sidx]);
367 if (!W_ERROR_IS_OK(werr)) {
368 goto done;
373 ret = 0;
375 done:
376 TALLOC_FREE(mem_ctx);
377 return ret;
380 static int net_conf_listshares(struct smbconf_ctx *conf_ctx,
381 int argc, const char **argv)
383 WERROR werr = WERR_OK;
384 int ret = -1;
385 uint32_t count, num_shares = 0;
386 char **share_names = NULL;
387 TALLOC_CTX *mem_ctx;
389 mem_ctx = talloc_stackframe();
391 if (argc != 0) {
392 net_conf_listshares_usage(argc, argv);
393 goto done;
396 werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
397 &share_names);
398 if (!W_ERROR_IS_OK(werr)) {
399 goto done;
402 for (count = 0; count < num_shares; count++)
404 d_printf("%s\n", share_names[count]);
407 ret = 0;
409 done:
410 TALLOC_FREE(mem_ctx);
411 return ret;
414 static int net_conf_drop(struct smbconf_ctx *conf_ctx,
415 int argc, const char **argv)
417 int ret = -1;
418 WERROR werr;
420 if (argc != 0) {
421 net_conf_drop_usage(argc, argv);
422 goto done;
425 werr = smbconf_drop(conf_ctx);
426 if (!W_ERROR_IS_OK(werr)) {
427 d_fprintf(stderr, "Error deleting configuration: %s\n",
428 dos_errstr(werr));
429 goto done;
432 ret = 0;
434 done:
435 return ret;
438 static int net_conf_showshare(struct smbconf_ctx *conf_ctx,
439 int argc, const char **argv)
441 int ret = -1;
442 WERROR werr = WERR_OK;
443 const char *sharename = NULL;
444 TALLOC_CTX *mem_ctx;
445 uint32_t num_params;
446 uint32_t count;
447 char **param_names;
448 char **param_values;
450 mem_ctx = talloc_stackframe();
452 if (argc != 1) {
453 net_conf_showshare_usage(argc, argv);
454 goto done;
457 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
458 if (sharename == NULL) {
459 d_printf("error: out of memory!\n");
460 goto done;
463 werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &num_params,
464 &param_names, &param_values);
465 if (!W_ERROR_IS_OK(werr)) {
466 d_printf("error getting share parameters: %s\n",
467 dos_errstr(werr));
468 goto done;
471 d_printf("[%s]\n", sharename);
473 for (count = 0; count < num_params; count++) {
474 d_printf("\t%s = %s\n", param_names[count],
475 param_values[count]);
478 ret = 0;
480 done:
481 TALLOC_FREE(mem_ctx);
482 return ret;
486 * Add a share, with a couple of standard parameters, partly optional.
488 * This is a high level utility function of the net conf utility,
489 * not a direct frontend to the smbconf API.
491 static int net_conf_addshare(struct smbconf_ctx *conf_ctx,
492 int argc, const char **argv)
494 int ret = -1;
495 WERROR werr = WERR_OK;
496 char *sharename = NULL;
497 const char *path = NULL;
498 const char *comment = NULL;
499 const char *guest_ok = "no";
500 const char *writeable = "no";
501 SMB_STRUCT_STAT sbuf;
502 TALLOC_CTX *mem_ctx = talloc_stackframe();
504 switch (argc) {
505 case 0:
506 case 1:
507 default:
508 net_conf_addshare_usage(argc, argv);
509 goto done;
510 case 5:
511 comment = argv[4];
512 case 4:
513 if (!strnequal(argv[3], "guest_ok=", 9)) {
514 net_conf_addshare_usage(argc, argv);
515 goto done;
517 switch (argv[3][9]) {
518 case 'y':
519 case 'Y':
520 guest_ok = "yes";
521 break;
522 case 'n':
523 case 'N':
524 guest_ok = "no";
525 break;
526 default:
527 net_conf_addshare_usage(argc, argv);
528 goto done;
530 case 3:
531 if (!strnequal(argv[2], "writeable=", 10)) {
532 net_conf_addshare_usage(argc, argv);
533 goto done;
535 switch (argv[2][10]) {
536 case 'y':
537 case 'Y':
538 writeable = "yes";
539 break;
540 case 'n':
541 case 'N':
542 writeable = "no";
543 break;
544 default:
545 net_conf_addshare_usage(argc, argv);
546 goto done;
548 case 2:
549 path = argv[1];
550 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
551 if (sharename == NULL) {
552 d_printf("error: out of memory!\n");
553 goto done;
556 break;
560 * validate arguments
563 /* validate share name */
565 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
566 strlen(sharename)))
568 d_fprintf(stderr, "ERROR: share name %s contains "
569 "invalid characters (any of %s)\n",
570 sharename, INVALID_SHARENAME_CHARS);
571 goto done;
574 if (getpwnam(sharename)) {
575 d_fprintf(stderr, "ERROR: share name %s is already a valid "
576 "system user name.\n", sharename);
577 goto done;
580 if (strequal(sharename, GLOBAL_NAME)) {
581 d_fprintf(stderr,
582 "ERROR: 'global' is not a valid share name.\n");
583 goto done;
586 if (smbconf_share_exists(conf_ctx, sharename)) {
587 d_fprintf(stderr, "ERROR: share %s already exists.\n",
588 sharename);
589 goto done;
592 /* validate path */
594 if (path[0] != '/') {
595 d_fprintf(stderr,
596 "Error: path '%s' is not an absolute path.\n",
597 path);
598 goto done;
601 if (sys_stat(path, &sbuf) != 0) {
602 d_fprintf(stderr,
603 "ERROR: cannot stat path '%s' to ensure "
604 "this is a directory.\n"
605 "Error was '%s'.\n",
606 path, strerror(errno));
607 goto done;
610 if (!S_ISDIR(sbuf.st_mode)) {
611 d_fprintf(stderr,
612 "ERROR: path '%s' is not a directory.\n",
613 path);
614 goto done;
618 * create the share
621 werr = smbconf_create_share(conf_ctx, sharename);
622 if (!W_ERROR_IS_OK(werr)) {
623 d_fprintf(stderr, "Error creating share %s: %s\n",
624 sharename, dos_errstr(werr));
625 goto done;
629 * fill the share with parameters
632 werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
633 if (!W_ERROR_IS_OK(werr)) {
634 d_fprintf(stderr, "Error setting parameter %s: %s\n",
635 "path", dos_errstr(werr));
636 goto done;
639 if (comment != NULL) {
640 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
641 comment);
642 if (!W_ERROR_IS_OK(werr)) {
643 d_fprintf(stderr, "Error setting parameter %s: %s\n",
644 "comment", dos_errstr(werr));
645 goto done;
649 werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
650 if (!W_ERROR_IS_OK(werr)) {
651 d_fprintf(stderr, "Error setting parameter %s: %s\n",
652 "'guest ok'", dos_errstr(werr));
653 goto done;
656 werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
657 writeable);
658 if (!W_ERROR_IS_OK(werr)) {
659 d_fprintf(stderr, "Error setting parameter %s: %s\n",
660 "writeable", dos_errstr(werr));
661 goto done;
664 ret = 0;
666 done:
667 TALLOC_FREE(mem_ctx);
668 return ret;
671 static int net_conf_delshare(struct smbconf_ctx *conf_ctx,
672 int argc, const char **argv)
674 int ret = -1;
675 const char *sharename = NULL;
676 WERROR werr = WERR_OK;
677 TALLOC_CTX *mem_ctx = talloc_stackframe();
679 if (argc != 1) {
680 net_conf_delshare_usage(argc, argv);
681 goto done;
683 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
684 if (sharename == NULL) {
685 d_printf("error: out of memory!\n");
686 goto done;
689 werr = smbconf_delete_share(conf_ctx, sharename);
690 if (!W_ERROR_IS_OK(werr)) {
691 d_fprintf(stderr, "Error deleting share %s: %s\n",
692 sharename, dos_errstr(werr));
693 goto done;
696 ret = 0;
697 done:
698 TALLOC_FREE(mem_ctx);
699 return ret;
702 static int net_conf_setparm(struct smbconf_ctx *conf_ctx,
703 int argc, const char **argv)
705 int ret = -1;
706 WERROR werr = WERR_OK;
707 char *service = NULL;
708 char *param = NULL;
709 const char *value_str = NULL;
710 TALLOC_CTX *mem_ctx = talloc_stackframe();
712 if (argc != 3) {
713 net_conf_setparm_usage(argc, argv);
714 goto done;
716 service = talloc_strdup_lower(mem_ctx, argv[0]);
717 if (service == NULL) {
718 d_printf("error: out of memory!\n");
719 goto done;
721 param = talloc_strdup_lower(mem_ctx, argv[1]);
722 if (param == NULL) {
723 d_printf("error: out of memory!\n");
724 goto done;
726 value_str = argv[2];
728 if (!smbconf_share_exists(conf_ctx, service)) {
729 werr = smbconf_create_share(conf_ctx, service);
730 if (!W_ERROR_IS_OK(werr)) {
731 d_fprintf(stderr, "Error creating share '%s': %s\n",
732 service, dos_errstr(werr));
733 goto done;
737 werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
739 if (!W_ERROR_IS_OK(werr)) {
740 d_fprintf(stderr, "Error setting value '%s': %s\n",
741 param, dos_errstr(werr));
742 goto done;
745 ret = 0;
747 done:
748 TALLOC_FREE(mem_ctx);
749 return ret;
752 static int net_conf_getparm(struct smbconf_ctx *conf_ctx,
753 int argc, const char **argv)
755 int ret = -1;
756 WERROR werr = WERR_OK;
757 char *service = NULL;
758 char *param = NULL;
759 char *valstr = NULL;
760 TALLOC_CTX *mem_ctx;
762 mem_ctx = talloc_stackframe();
764 if (argc != 2) {
765 net_conf_getparm_usage(argc, argv);
766 goto done;
768 service = talloc_strdup_lower(mem_ctx, argv[0]);
769 if (service == NULL) {
770 d_printf("error: out of memory!\n");
771 goto done;
773 param = talloc_strdup_lower(mem_ctx, argv[1]);
774 if (param == NULL) {
775 d_printf("error: out of memory!\n");
776 goto done;
779 werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
781 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
782 d_fprintf(stderr,
783 "Error: given service '%s' does not exist.\n",
784 service);
785 goto done;
786 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
787 d_fprintf(stderr,
788 "Error: given parameter '%s' is not set.\n",
789 param);
790 goto done;
791 } else if (!W_ERROR_IS_OK(werr)) {
792 d_fprintf(stderr, "Error getting value '%s': %s.\n",
793 param, dos_errstr(werr));
794 goto done;
797 d_printf("%s\n", valstr);
799 ret = 0;
800 done:
801 TALLOC_FREE(mem_ctx);
802 return ret;
805 static int net_conf_delparm(struct smbconf_ctx *conf_ctx,
806 int argc, const char **argv)
808 int ret = -1;
809 WERROR werr = WERR_OK;
810 char *service = NULL;
811 char *param = NULL;
812 TALLOC_CTX *mem_ctx = talloc_stackframe();
814 if (argc != 2) {
815 net_conf_delparm_usage(argc, argv);
816 goto done;
818 service = talloc_strdup_lower(mem_ctx, argv[0]);
819 if (service == NULL) {
820 d_printf("error: out of memory!\n");
821 goto done;
823 param = talloc_strdup_lower(mem_ctx, argv[1]);
824 if (param == NULL) {
825 d_printf("error: out of memory!\n");
826 goto done;
829 werr = smbconf_delete_parameter(conf_ctx, service, param);
831 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
832 d_fprintf(stderr,
833 "Error: given service '%s' does not exist.\n",
834 service);
835 goto done;
836 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
837 d_fprintf(stderr,
838 "Error: given parameter '%s' is not set.\n",
839 param);
840 goto done;
841 } else if (!W_ERROR_IS_OK(werr)) {
842 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
843 param, dos_errstr(werr));
844 goto done;
847 ret = 0;
849 done:
850 TALLOC_FREE(mem_ctx);
851 return ret;
854 static int net_conf_getincludes(struct smbconf_ctx *conf_ctx,
855 int argc, const char **argv)
857 WERROR werr;
858 uint32_t num_includes;
859 uint32_t count;
860 char *service;
861 char **includes = NULL;
862 int ret = -1;
863 TALLOC_CTX *mem_ctx = talloc_stackframe();
865 if (argc != 1) {
866 net_conf_getincludes_usage(argc, argv);
867 goto done;
870 service = talloc_strdup_lower(mem_ctx, argv[0]);
871 if (service == NULL) {
872 d_printf("error: out of memory!\n");
873 goto done;
876 werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
877 &num_includes, &includes);
878 if (!W_ERROR_IS_OK(werr)) {
879 d_printf("error getting includes: %s\n", dos_errstr(werr));
880 goto done;
883 for (count = 0; count < num_includes; count++) {
884 d_printf("include = %s\n", includes[count]);
887 ret = 0;
889 done:
890 TALLOC_FREE(mem_ctx);
891 return ret;
894 static int net_conf_setincludes(struct smbconf_ctx *conf_ctx,
895 int argc, const char **argv)
897 WERROR werr;
898 char *service;
899 uint32_t num_includes;
900 const char **includes;
901 int ret = -1;
902 TALLOC_CTX *mem_ctx = talloc_stackframe();
904 if (argc < 1) {
905 net_conf_setincludes_usage(argc, argv);
906 goto done;
909 service = talloc_strdup_lower(mem_ctx, argv[0]);
910 if (service == NULL) {
911 d_printf("error: out of memory!\n");
912 goto done;
915 num_includes = argc - 1;
916 if (num_includes == 0) {
917 includes = NULL;
918 } else {
919 includes = argv + 1;
922 werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
923 if (!W_ERROR_IS_OK(werr)) {
924 d_printf("error setting includes: %s\n", dos_errstr(werr));
925 goto done;
928 ret = 0;
930 done:
931 TALLOC_FREE(mem_ctx);
932 return ret;
935 static int net_conf_delincludes(struct smbconf_ctx *conf_ctx,
936 int argc, const char **argv)
938 WERROR werr;
939 char *service;
940 int ret = -1;
941 TALLOC_CTX *mem_ctx = talloc_stackframe();
943 if (argc != 1) {
944 net_conf_delincludes_usage(argc, argv);
945 goto done;
948 service = talloc_strdup_lower(mem_ctx, argv[0]);
949 if (service == NULL) {
950 d_printf("error: out of memory!\n");
951 goto done;
954 werr = smbconf_delete_includes(conf_ctx, service);
955 if (!W_ERROR_IS_OK(werr)) {
956 d_printf("error deleting includes: %s\n", dos_errstr(werr));
957 goto done;
960 ret = 0;
962 done:
963 TALLOC_FREE(mem_ctx);
964 return ret;
968 /**********************************************************************
970 * Wrapper and net_conf_run_function mechanism.
972 **********************************************************************/
975 * Wrapper function to call the main conf functions.
976 * The wrapper calls handles opening and closing of the
977 * configuration.
979 static int net_conf_wrap_function(int (*fn)(struct smbconf_ctx *,
980 int, const char **),
981 int argc, const char **argv)
983 WERROR werr;
984 TALLOC_CTX *mem_ctx = talloc_stackframe();
985 struct smbconf_ctx *conf_ctx;
986 int ret = -1;
988 werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
990 if (!W_ERROR_IS_OK(werr)) {
991 return -1;
994 ret = fn(conf_ctx, argc, argv);
996 smbconf_shutdown(conf_ctx);
998 return ret;
1002 * We need a functable struct of our own, because the
1003 * functions are called through a wrapper that handles
1004 * the opening and closing of the configuration, and so on.
1006 struct conf_functable {
1007 const char *funcname;
1008 int (*fn)(struct smbconf_ctx *ctx, int argc, const char **argv);
1009 const char *helptext;
1013 * This imitates net_run_function2 but calls the main functions
1014 * through the wrapper net_conf_wrap_function().
1016 static int net_conf_run_function(int argc, const char **argv,
1017 const char *whoami,
1018 struct conf_functable *table)
1020 int i;
1022 if (argc != 0) {
1023 for (i=0; table[i].funcname; i++) {
1024 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1025 return net_conf_wrap_function(table[i].fn,
1026 argc-1,
1027 argv+1);
1031 for (i=0; table[i].funcname; i++) {
1032 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1033 table[i].helptext);
1036 return -1;
1040 * Entry-point for all the CONF functions.
1043 int net_conf(int argc, const char **argv)
1045 int ret = -1;
1046 struct conf_functable func_table[] = {
1047 {"list", net_conf_list,
1048 "Dump the complete configuration in smb.conf like format."},
1049 {"import", net_conf_import,
1050 "Import configuration from file in smb.conf format."},
1051 {"listshares", net_conf_listshares,
1052 "List the share names."},
1053 {"drop", net_conf_drop,
1054 "Delete the complete configuration."},
1055 {"showshare", net_conf_showshare,
1056 "Show the definition of a share."},
1057 {"addshare", net_conf_addshare,
1058 "Create a new share."},
1059 {"delshare", net_conf_delshare,
1060 "Delete a share."},
1061 {"setparm", net_conf_setparm,
1062 "Store a parameter."},
1063 {"getparm", net_conf_getparm,
1064 "Retrieve the value of a parameter."},
1065 {"delparm", net_conf_delparm,
1066 "Delete a parameter."},
1067 {"getincludes", net_conf_getincludes,
1068 "Show the includes of a share definition."},
1069 {"setincludes", net_conf_setincludes,
1070 "Set includes for a share."},
1071 {"delincludes", net_conf_delincludes,
1072 "Delete includes from a share definition."},
1073 {NULL, NULL, NULL}
1076 ret = net_conf_run_function(argc, argv, "net conf", func_table);
1078 return ret;