configure: make use of LIBREPLACE_NETWORK_LIBS instead of redoing the tests
[Samba.git] / source / utils / net_conf.c
bloba922e135833083def4b28ab4fa8eedccda1d1d56
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(struct net_context *c, int argc,
40 const char **argv)
42 d_printf("USAGE: net conf list\n");
43 return -1;
46 static int net_conf_import_usage(struct net_context *c, int argc,
47 const char**argv)
49 d_printf("USAGE: net conf import [--test|-T] <filename> "
50 "[<servicename>]\n"
51 "\t[--test|-T] testmode - do not act, just print "
52 "what would be done\n"
53 "\t<servicename> only import service <servicename>, "
54 "ignore the rest\n");
55 return -1;
58 static int net_conf_listshares_usage(struct net_context *c, int argc,
59 const char **argv)
61 d_printf("USAGE: net conf listshares\n");
62 return -1;
65 static int net_conf_drop_usage(struct net_context *c, int argc,
66 const char **argv)
68 d_printf("USAGE: net conf drop\n");
69 return -1;
72 static int net_conf_showshare_usage(struct net_context *c, int argc,
73 const char **argv)
75 d_printf("USAGE: net conf showshare <sharename>\n");
76 return -1;
79 static int net_conf_addshare_usage(struct net_context *c, int argc,
80 const char **argv)
82 d_printf("USAGE: net conf addshare <sharename> <path> "
83 "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
84 "\t<sharename> the new share name.\n"
85 "\t<path> the path on the filesystem to export.\n"
86 "\twriteable={y|N} set \"writeable to \"yes\" or "
87 "\"no\" (default) on this share.\n"
88 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
89 "\"no\" (default) on this share.\n"
90 "\t<comment> optional comment for the new share.\n");
91 return -1;
94 static int net_conf_delshare_usage(struct net_context *c, int argc,
95 const char **argv)
97 d_printf("USAGE: net conf delshare <sharename>\n");
98 return -1;
101 static int net_conf_setparm_usage(struct net_context *c, int argc,
102 const char **argv)
104 d_printf("USAGE: net conf setparm <section> <param> <value>\n");
105 return -1;
108 static int net_conf_getparm_usage(struct net_context *c, int argc,
109 const char **argv)
111 d_printf("USAGE: net conf getparm <section> <param>\n");
112 return -1;
115 static int net_conf_delparm_usage(struct net_context *c, int argc,
116 const char **argv)
118 d_printf("USAGE: net conf delparm <section> <param>\n");
119 return -1;
122 static int net_conf_getincludes_usage(struct net_context *c, int argc,
123 const char **argv)
125 d_printf("USAGE: net conf getincludes <section>\n");
126 return -1;
129 static int net_conf_setincludes_usage(struct net_context *c, int argc,
130 const char **argv)
132 d_printf("USAGE: net conf setincludes <section> [<filename>]*\n");
133 return -1;
136 static int net_conf_delincludes_usage(struct net_context *c, int argc,
137 const char **argv)
139 d_printf("USAGE: net conf delincludes <section>\n");
140 return -1;
144 /**********************************************************************
146 * Helper functions
148 **********************************************************************/
151 * This functions process a service previously loaded with libsmbconf.
153 static WERROR import_process_service(struct net_context *c,
154 struct smbconf_ctx *conf_ctx,
155 struct smbconf_service *service)
157 uint32_t idx;
158 WERROR werr = WERR_OK;
159 uint32_t num_includes = 0;
160 char **includes = NULL;
161 TALLOC_CTX *mem_ctx = talloc_stackframe();
163 if (c->opt_testmode) {
164 const char *indent = "";
165 if (service->name != NULL) {
166 d_printf("[%s]\n", service->name);
167 indent = "\t";
169 for (idx = 0; idx < service->num_params; idx++) {
170 d_printf("%s%s = %s\n", indent,
171 service->param_names[idx],
172 service->param_values[idx]);
174 d_printf("\n");
175 goto done;
178 if (smbconf_share_exists(conf_ctx, service->name)) {
179 werr = smbconf_delete_share(conf_ctx, service->name);
180 if (!W_ERROR_IS_OK(werr)) {
181 goto done;
184 werr = smbconf_create_share(conf_ctx, service->name);
185 if (!W_ERROR_IS_OK(werr)) {
186 goto done;
189 for (idx = 0; idx < service->num_params; idx ++) {
190 if (strequal(service->param_names[idx], "include")) {
191 includes = TALLOC_REALLOC_ARRAY(mem_ctx,
192 includes,
193 char *,
194 num_includes+1);
195 if (includes == NULL) {
196 werr = WERR_NOMEM;
197 goto done;
199 includes[num_includes] = talloc_strdup(includes,
200 service->param_values[idx]);
201 if (includes[num_includes] == NULL) {
202 werr = WERR_NOMEM;
203 goto done;
205 num_includes++;
206 } else {
207 werr = smbconf_set_parameter(conf_ctx,
208 service->name,
209 service->param_names[idx],
210 service->param_values[idx]);
211 if (!W_ERROR_IS_OK(werr)) {
212 goto done;
217 werr = smbconf_set_includes(conf_ctx, service->name, num_includes,
218 (const char **)includes);
220 done:
221 TALLOC_FREE(mem_ctx);
222 return werr;
226 /**********************************************************************
228 * the main conf functions
230 **********************************************************************/
232 static int net_conf_list(struct net_context *c, struct smbconf_ctx *conf_ctx,
233 int argc, const char **argv)
235 WERROR werr = WERR_OK;
236 int ret = -1;
237 TALLOC_CTX *mem_ctx;
238 uint32_t num_shares;
239 uint32_t share_count, param_count;
240 struct smbconf_service **shares = NULL;
242 mem_ctx = talloc_stackframe();
244 if (argc != 0) {
245 net_conf_list_usage(c, argc, argv);
246 goto done;
249 werr = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &shares);
250 if (!W_ERROR_IS_OK(werr)) {
251 d_fprintf(stderr, "Error getting config: %s\n",
252 dos_errstr(werr));
253 goto done;
256 for (share_count = 0; share_count < num_shares; share_count++) {
257 const char *indent = "";
258 if (shares[share_count]->name != NULL) {
259 d_printf("[%s]\n", shares[share_count]->name);
260 indent = "\t";
262 for (param_count = 0;
263 param_count < shares[share_count]->num_params;
264 param_count++)
266 d_printf("%s%s = %s\n",
267 indent,
268 shares[share_count]->param_names[param_count],
269 shares[share_count]->param_values[param_count]);
271 d_printf("\n");
274 ret = 0;
276 done:
277 TALLOC_FREE(mem_ctx);
278 return ret;
281 static int net_conf_import(struct net_context *c, struct smbconf_ctx *conf_ctx,
282 int argc, const char **argv)
284 int ret = -1;
285 const char *filename = NULL;
286 const char *servicename = NULL;
287 char *conf_source = NULL;
288 TALLOC_CTX *mem_ctx;
289 struct smbconf_ctx *txt_ctx;
290 WERROR werr;
292 mem_ctx = talloc_stackframe();
294 switch (argc) {
295 case 0:
296 default:
297 net_conf_import_usage(c, argc, argv);
298 goto done;
299 case 2:
300 servicename = talloc_strdup_lower(mem_ctx, argv[1]);
301 if (servicename == NULL) {
302 d_printf("error: out of memory!\n");
303 goto done;
305 case 1:
306 filename = argv[0];
307 break;
310 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
311 filename));
313 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
314 if (conf_source == NULL) {
315 d_printf("error: out of memory!\n");
316 goto done;
319 werr = smbconf_init(mem_ctx, &txt_ctx, conf_source);
320 if (!W_ERROR_IS_OK(werr)) {
321 d_printf("error loading file '%s': %s\n", filename,
322 dos_errstr(werr));
323 goto done;
326 if (c->opt_testmode) {
327 d_printf("\nTEST MODE - "
328 "would import the following configuration:\n\n");
331 if (servicename != NULL) {
332 struct smbconf_service *service = NULL;
334 werr = smbconf_get_share(txt_ctx, mem_ctx,
335 servicename,
336 &service);
337 if (!W_ERROR_IS_OK(werr)) {
338 goto done;
340 werr = import_process_service(c, conf_ctx, service);
341 if (!W_ERROR_IS_OK(werr)) {
342 goto done;
344 } else {
345 struct smbconf_service **services = NULL;
346 uint32_t num_shares, sidx;
348 werr = smbconf_get_config(txt_ctx, mem_ctx,
349 &num_shares,
350 &services);
351 if (!W_ERROR_IS_OK(werr)) {
352 goto done;
354 if (!c->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(c, conf_ctx,
362 services[sidx]);
363 if (!W_ERROR_IS_OK(werr)) {
364 goto done;
369 ret = 0;
371 done:
372 TALLOC_FREE(mem_ctx);
373 return ret;
376 static int net_conf_listshares(struct net_context *c,
377 struct smbconf_ctx *conf_ctx, int argc,
378 const char **argv)
380 WERROR werr = WERR_OK;
381 int ret = -1;
382 uint32_t count, num_shares = 0;
383 char **share_names = NULL;
384 TALLOC_CTX *mem_ctx;
386 mem_ctx = talloc_stackframe();
388 if (argc != 0) {
389 net_conf_listshares_usage(c, argc, argv);
390 goto done;
393 werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
394 &share_names);
395 if (!W_ERROR_IS_OK(werr)) {
396 goto done;
399 for (count = 0; count < num_shares; count++)
401 d_printf("%s\n", share_names[count]);
404 ret = 0;
406 done:
407 TALLOC_FREE(mem_ctx);
408 return ret;
411 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
412 int argc, const char **argv)
414 int ret = -1;
415 WERROR werr;
417 if (argc != 0) {
418 net_conf_drop_usage(c, argc, argv);
419 goto done;
422 werr = smbconf_drop(conf_ctx);
423 if (!W_ERROR_IS_OK(werr)) {
424 d_fprintf(stderr, "Error deleting configuration: %s\n",
425 dos_errstr(werr));
426 goto done;
429 ret = 0;
431 done:
432 return ret;
435 static int net_conf_showshare(struct net_context *c,
436 struct smbconf_ctx *conf_ctx, int argc,
437 const char **argv)
439 int ret = -1;
440 WERROR werr = WERR_OK;
441 const char *sharename = NULL;
442 TALLOC_CTX *mem_ctx;
443 uint32_t count;
444 struct smbconf_service *service = NULL;
446 mem_ctx = talloc_stackframe();
448 if (argc != 1) {
449 net_conf_showshare_usage(c, argc, argv);
450 goto done;
453 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
454 if (sharename == NULL) {
455 d_printf("error: out of memory!\n");
456 goto done;
459 werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
460 if (!W_ERROR_IS_OK(werr)) {
461 d_printf("error getting share parameters: %s\n",
462 dos_errstr(werr));
463 goto done;
466 d_printf("[%s]\n", sharename);
468 for (count = 0; count < service->num_params; count++) {
469 d_printf("\t%s = %s\n", service->param_names[count],
470 service->param_values[count]);
473 ret = 0;
475 done:
476 TALLOC_FREE(mem_ctx);
477 return ret;
481 * Add a share, with a couple of standard parameters, partly optional.
483 * This is a high level utility function of the net conf utility,
484 * not a direct frontend to the smbconf API.
486 static int net_conf_addshare(struct net_context *c,
487 struct smbconf_ctx *conf_ctx, int argc,
488 const char **argv)
490 int ret = -1;
491 WERROR werr = WERR_OK;
492 char *sharename = NULL;
493 const char *path = NULL;
494 const char *comment = NULL;
495 const char *guest_ok = "no";
496 const char *writeable = "no";
497 SMB_STRUCT_STAT sbuf;
498 TALLOC_CTX *mem_ctx = talloc_stackframe();
500 switch (argc) {
501 case 0:
502 case 1:
503 default:
504 net_conf_addshare_usage(c, argc, argv);
505 goto done;
506 case 5:
507 comment = argv[4];
508 case 4:
509 if (!strnequal(argv[3], "guest_ok=", 9)) {
510 net_conf_addshare_usage(c, argc, argv);
511 goto done;
513 switch (argv[3][9]) {
514 case 'y':
515 case 'Y':
516 guest_ok = "yes";
517 break;
518 case 'n':
519 case 'N':
520 guest_ok = "no";
521 break;
522 default:
523 net_conf_addshare_usage(c, argc, argv);
524 goto done;
526 case 3:
527 if (!strnequal(argv[2], "writeable=", 10)) {
528 net_conf_addshare_usage(c, argc, argv);
529 goto done;
531 switch (argv[2][10]) {
532 case 'y':
533 case 'Y':
534 writeable = "yes";
535 break;
536 case 'n':
537 case 'N':
538 writeable = "no";
539 break;
540 default:
541 net_conf_addshare_usage(c, argc, argv);
542 goto done;
544 case 2:
545 path = argv[1];
546 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
547 if (sharename == NULL) {
548 d_printf("error: out of memory!\n");
549 goto done;
552 break;
556 * validate arguments
559 /* validate share name */
561 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
562 strlen(sharename)))
564 d_fprintf(stderr, "ERROR: share name %s contains "
565 "invalid characters (any of %s)\n",
566 sharename, INVALID_SHARENAME_CHARS);
567 goto done;
570 if (getpwnam(sharename)) {
571 d_fprintf(stderr, "ERROR: share name %s is already a valid "
572 "system user name.\n", sharename);
573 goto done;
576 if (strequal(sharename, GLOBAL_NAME)) {
577 d_fprintf(stderr,
578 "ERROR: 'global' is not a valid share name.\n");
579 goto done;
582 if (smbconf_share_exists(conf_ctx, sharename)) {
583 d_fprintf(stderr, "ERROR: share %s already exists.\n",
584 sharename);
585 goto done;
588 /* validate path */
590 if (path[0] != '/') {
591 d_fprintf(stderr,
592 "Error: path '%s' is not an absolute path.\n",
593 path);
594 goto done;
597 if (sys_stat(path, &sbuf) != 0) {
598 d_fprintf(stderr,
599 "ERROR: cannot stat path '%s' to ensure "
600 "this is a directory.\n"
601 "Error was '%s'.\n",
602 path, strerror(errno));
603 goto done;
606 if (!S_ISDIR(sbuf.st_mode)) {
607 d_fprintf(stderr,
608 "ERROR: path '%s' is not a directory.\n",
609 path);
610 goto done;
614 * create the share
617 werr = smbconf_create_share(conf_ctx, sharename);
618 if (!W_ERROR_IS_OK(werr)) {
619 d_fprintf(stderr, "Error creating share %s: %s\n",
620 sharename, dos_errstr(werr));
621 goto done;
625 * fill the share with parameters
628 werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
629 if (!W_ERROR_IS_OK(werr)) {
630 d_fprintf(stderr, "Error setting parameter %s: %s\n",
631 "path", dos_errstr(werr));
632 goto done;
635 if (comment != NULL) {
636 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
637 comment);
638 if (!W_ERROR_IS_OK(werr)) {
639 d_fprintf(stderr, "Error setting parameter %s: %s\n",
640 "comment", dos_errstr(werr));
641 goto done;
645 werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
646 if (!W_ERROR_IS_OK(werr)) {
647 d_fprintf(stderr, "Error setting parameter %s: %s\n",
648 "'guest ok'", dos_errstr(werr));
649 goto done;
652 werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
653 writeable);
654 if (!W_ERROR_IS_OK(werr)) {
655 d_fprintf(stderr, "Error setting parameter %s: %s\n",
656 "writeable", dos_errstr(werr));
657 goto done;
660 ret = 0;
662 done:
663 TALLOC_FREE(mem_ctx);
664 return ret;
667 static int net_conf_delshare(struct net_context *c,
668 struct smbconf_ctx *conf_ctx, int argc,
669 const char **argv)
671 int ret = -1;
672 const char *sharename = NULL;
673 WERROR werr = WERR_OK;
674 TALLOC_CTX *mem_ctx = talloc_stackframe();
676 if (argc != 1) {
677 net_conf_delshare_usage(c, argc, argv);
678 goto done;
680 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
681 if (sharename == NULL) {
682 d_printf("error: out of memory!\n");
683 goto done;
686 werr = smbconf_delete_share(conf_ctx, sharename);
687 if (!W_ERROR_IS_OK(werr)) {
688 d_fprintf(stderr, "Error deleting share %s: %s\n",
689 sharename, dos_errstr(werr));
690 goto done;
693 ret = 0;
694 done:
695 TALLOC_FREE(mem_ctx);
696 return ret;
699 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
700 int argc, const char **argv)
702 int ret = -1;
703 WERROR werr = WERR_OK;
704 char *service = NULL;
705 char *param = NULL;
706 const char *value_str = NULL;
707 TALLOC_CTX *mem_ctx = talloc_stackframe();
709 if (argc != 3) {
710 net_conf_setparm_usage(c, argc, argv);
711 goto done;
713 service = talloc_strdup_lower(mem_ctx, argv[0]);
714 if (service == NULL) {
715 d_printf("error: out of memory!\n");
716 goto done;
718 param = talloc_strdup_lower(mem_ctx, argv[1]);
719 if (param == NULL) {
720 d_printf("error: out of memory!\n");
721 goto done;
723 value_str = argv[2];
725 if (!smbconf_share_exists(conf_ctx, service)) {
726 werr = smbconf_create_share(conf_ctx, service);
727 if (!W_ERROR_IS_OK(werr)) {
728 d_fprintf(stderr, "Error creating share '%s': %s\n",
729 service, dos_errstr(werr));
730 goto done;
734 werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
736 if (!W_ERROR_IS_OK(werr)) {
737 d_fprintf(stderr, "Error setting value '%s': %s\n",
738 param, dos_errstr(werr));
739 goto done;
742 ret = 0;
744 done:
745 TALLOC_FREE(mem_ctx);
746 return ret;
749 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
750 int argc, const char **argv)
752 int ret = -1;
753 WERROR werr = WERR_OK;
754 char *service = NULL;
755 char *param = NULL;
756 char *valstr = NULL;
757 TALLOC_CTX *mem_ctx;
759 mem_ctx = talloc_stackframe();
761 if (argc != 2) {
762 net_conf_getparm_usage(c, argc, argv);
763 goto done;
765 service = talloc_strdup_lower(mem_ctx, argv[0]);
766 if (service == NULL) {
767 d_printf("error: out of memory!\n");
768 goto done;
770 param = talloc_strdup_lower(mem_ctx, argv[1]);
771 if (param == NULL) {
772 d_printf("error: out of memory!\n");
773 goto done;
776 werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
778 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
779 d_fprintf(stderr,
780 "Error: given service '%s' does not exist.\n",
781 service);
782 goto done;
783 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
784 d_fprintf(stderr,
785 "Error: given parameter '%s' is not set.\n",
786 param);
787 goto done;
788 } else if (!W_ERROR_IS_OK(werr)) {
789 d_fprintf(stderr, "Error getting value '%s': %s.\n",
790 param, dos_errstr(werr));
791 goto done;
794 d_printf("%s\n", valstr);
796 ret = 0;
797 done:
798 TALLOC_FREE(mem_ctx);
799 return ret;
802 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
803 int argc, const char **argv)
805 int ret = -1;
806 WERROR werr = WERR_OK;
807 char *service = NULL;
808 char *param = NULL;
809 TALLOC_CTX *mem_ctx = talloc_stackframe();
811 if (argc != 2) {
812 net_conf_delparm_usage(c, argc, argv);
813 goto done;
815 service = talloc_strdup_lower(mem_ctx, argv[0]);
816 if (service == NULL) {
817 d_printf("error: out of memory!\n");
818 goto done;
820 param = talloc_strdup_lower(mem_ctx, argv[1]);
821 if (param == NULL) {
822 d_printf("error: out of memory!\n");
823 goto done;
826 werr = smbconf_delete_parameter(conf_ctx, service, param);
828 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
829 d_fprintf(stderr,
830 "Error: given service '%s' does not exist.\n",
831 service);
832 goto done;
833 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
834 d_fprintf(stderr,
835 "Error: given parameter '%s' is not set.\n",
836 param);
837 goto done;
838 } else if (!W_ERROR_IS_OK(werr)) {
839 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
840 param, dos_errstr(werr));
841 goto done;
844 ret = 0;
846 done:
847 TALLOC_FREE(mem_ctx);
848 return ret;
851 static int net_conf_getincludes(struct net_context *c,
852 struct smbconf_ctx *conf_ctx,
853 int argc, const char **argv)
855 WERROR werr;
856 uint32_t num_includes;
857 uint32_t count;
858 char *service;
859 char **includes = NULL;
860 int ret = -1;
861 TALLOC_CTX *mem_ctx = talloc_stackframe();
863 if (argc != 1) {
864 net_conf_getincludes_usage(c, argc, argv);
865 goto done;
868 service = talloc_strdup_lower(mem_ctx, argv[0]);
869 if (service == NULL) {
870 d_printf("error: out of memory!\n");
871 goto done;
874 werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
875 &num_includes, &includes);
876 if (!W_ERROR_IS_OK(werr)) {
877 d_printf("error getting includes: %s\n", dos_errstr(werr));
878 goto done;
881 for (count = 0; count < num_includes; count++) {
882 d_printf("include = %s\n", includes[count]);
885 ret = 0;
887 done:
888 TALLOC_FREE(mem_ctx);
889 return ret;
892 static int net_conf_setincludes(struct net_context *c,
893 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(c, 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 net_context *c,
935 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(c, 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(struct net_context *c,
980 int (*fn)(struct net_context *,
981 struct smbconf_ctx *,
982 int, const char **),
983 int argc, const char **argv)
985 WERROR werr;
986 TALLOC_CTX *mem_ctx = talloc_stackframe();
987 struct smbconf_ctx *conf_ctx;
988 int ret = -1;
990 werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
992 if (!W_ERROR_IS_OK(werr)) {
993 return -1;
996 ret = fn(c, conf_ctx, argc, argv);
998 smbconf_shutdown(conf_ctx);
1000 return ret;
1004 * We need a functable struct of our own, because the
1005 * functions are called through a wrapper that handles
1006 * the opening and closing of the configuration, and so on.
1008 struct conf_functable {
1009 const char *funcname;
1010 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1011 const char **argv);
1012 const char *helptext;
1016 * This imitates net_run_function2 but calls the main functions
1017 * through the wrapper net_conf_wrap_function().
1019 static int net_conf_run_function(struct net_context *c, int argc,
1020 const char **argv, const char *whoami,
1021 struct conf_functable *table)
1023 int i;
1025 if (argc != 0) {
1026 for (i=0; table[i].funcname; i++) {
1027 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1028 return net_conf_wrap_function(c, table[i].fn,
1029 argc-1,
1030 argv+1);
1034 for (i=0; table[i].funcname; i++) {
1035 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1036 table[i].helptext);
1039 return -1;
1043 * Entry-point for all the CONF functions.
1046 int net_conf(struct net_context *c, int argc, const char **argv)
1048 int ret = -1;
1049 struct conf_functable func_table[] = {
1050 {"list", net_conf_list,
1051 "Dump the complete configuration in smb.conf like format."},
1052 {"import", net_conf_import,
1053 "Import configuration from file in smb.conf format."},
1054 {"listshares", net_conf_listshares,
1055 "List the share names."},
1056 {"drop", net_conf_drop,
1057 "Delete the complete configuration."},
1058 {"showshare", net_conf_showshare,
1059 "Show the definition of a share."},
1060 {"addshare", net_conf_addshare,
1061 "Create a new share."},
1062 {"delshare", net_conf_delshare,
1063 "Delete a share."},
1064 {"setparm", net_conf_setparm,
1065 "Store a parameter."},
1066 {"getparm", net_conf_getparm,
1067 "Retrieve the value of a parameter."},
1068 {"delparm", net_conf_delparm,
1069 "Delete a parameter."},
1070 {"getincludes", net_conf_getincludes,
1071 "Show the includes of a share definition."},
1072 {"setincludes", net_conf_setincludes,
1073 "Set includes for a share."},
1074 {"delincludes", net_conf_delincludes,
1075 "Delete includes from a share definition."},
1076 {NULL, NULL, NULL}
1079 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1081 return ret;