s3:net: wrap net conf import into one big transaction
[Samba/ekacnet.git] / source / utils / net_conf.c
blob5847dc71974918c0c475cba63a21f9c083e3a0a3
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 || c->display_usage) {
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 if (c->display_usage)
293 return net_conf_import_usage(c, argc, argv);
295 mem_ctx = talloc_stackframe();
297 switch (argc) {
298 case 0:
299 default:
300 net_conf_import_usage(c, argc, argv);
301 goto done;
302 case 2:
303 servicename = talloc_strdup_lower(mem_ctx, argv[1]);
304 if (servicename == NULL) {
305 d_printf("error: out of memory!\n");
306 goto done;
308 case 1:
309 filename = argv[0];
310 break;
313 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
314 filename));
316 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
317 if (conf_source == NULL) {
318 d_printf("error: out of memory!\n");
319 goto done;
322 werr = smbconf_init(mem_ctx, &txt_ctx, conf_source);
323 if (!W_ERROR_IS_OK(werr)) {
324 d_printf("error loading file '%s': %s\n", filename,
325 dos_errstr(werr));
326 goto done;
329 if (c->opt_testmode) {
330 d_printf("\nTEST MODE - "
331 "would import the following configuration:\n\n");
334 werr = smbconf_transaction_start(conf_ctx);
335 if (!W_ERROR_IS_OK(werr)) {
336 d_printf("error starting transaction: %s\n", win_errstr(werr));
337 goto done;
340 if (servicename != NULL) {
341 struct smbconf_service *service = NULL;
343 werr = smbconf_get_share(txt_ctx, mem_ctx,
344 servicename,
345 &service);
346 if (!W_ERROR_IS_OK(werr)) {
347 goto cancel;
349 werr = import_process_service(c, conf_ctx, service);
350 if (!W_ERROR_IS_OK(werr)) {
351 goto cancel;
353 } else {
354 struct smbconf_service **services = NULL;
355 uint32_t num_shares, sidx;
357 werr = smbconf_get_config(txt_ctx, mem_ctx,
358 &num_shares,
359 &services);
360 if (!W_ERROR_IS_OK(werr)) {
361 goto cancel;
363 if (!c->opt_testmode) {
364 werr = smbconf_drop(conf_ctx);
365 if (!W_ERROR_IS_OK(werr)) {
366 goto cancel;
369 for (sidx = 0; sidx < num_shares; sidx++) {
370 werr = import_process_service(c, conf_ctx,
371 services[sidx]);
372 if (!W_ERROR_IS_OK(werr)) {
373 goto cancel;
378 werr = smbconf_transaction_commit(conf_ctx);
379 if (!W_ERROR_IS_OK(werr)) {
380 d_printf("error committing transaction: %s\n",
381 win_errstr(werr));
382 } else {
383 ret = 0;
386 goto done;
388 cancel:
389 werr = smbconf_transaction_cancel(conf_ctx);
390 if (!W_ERROR_IS_OK(werr)) {
391 d_printf("error cancelling transaction: %s\n",
392 win_errstr(werr));
395 done:
396 TALLOC_FREE(mem_ctx);
397 return ret;
400 static int net_conf_listshares(struct net_context *c,
401 struct smbconf_ctx *conf_ctx, int argc,
402 const char **argv)
404 WERROR werr = WERR_OK;
405 int ret = -1;
406 uint32_t count, num_shares = 0;
407 char **share_names = NULL;
408 TALLOC_CTX *mem_ctx;
410 mem_ctx = talloc_stackframe();
412 if (argc != 0 || c->display_usage) {
413 net_conf_listshares_usage(c, argc, argv);
414 goto done;
417 werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
418 &share_names);
419 if (!W_ERROR_IS_OK(werr)) {
420 goto done;
423 for (count = 0; count < num_shares; count++)
425 d_printf("%s\n", share_names[count]);
428 ret = 0;
430 done:
431 TALLOC_FREE(mem_ctx);
432 return ret;
435 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
436 int argc, const char **argv)
438 int ret = -1;
439 WERROR werr;
441 if (argc != 0 || c->display_usage) {
442 net_conf_drop_usage(c, argc, argv);
443 goto done;
446 werr = smbconf_drop(conf_ctx);
447 if (!W_ERROR_IS_OK(werr)) {
448 d_fprintf(stderr, "Error deleting configuration: %s\n",
449 dos_errstr(werr));
450 goto done;
453 ret = 0;
455 done:
456 return ret;
459 static int net_conf_showshare(struct net_context *c,
460 struct smbconf_ctx *conf_ctx, int argc,
461 const char **argv)
463 int ret = -1;
464 WERROR werr = WERR_OK;
465 const char *sharename = NULL;
466 TALLOC_CTX *mem_ctx;
467 uint32_t count;
468 struct smbconf_service *service = NULL;
470 mem_ctx = talloc_stackframe();
472 if (argc != 1 || c->display_usage) {
473 net_conf_showshare_usage(c, argc, argv);
474 goto done;
477 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
478 if (sharename == NULL) {
479 d_printf("error: out of memory!\n");
480 goto done;
483 werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
484 if (!W_ERROR_IS_OK(werr)) {
485 d_printf("error getting share parameters: %s\n",
486 dos_errstr(werr));
487 goto done;
490 d_printf("[%s]\n", sharename);
492 for (count = 0; count < service->num_params; count++) {
493 d_printf("\t%s = %s\n", service->param_names[count],
494 service->param_values[count]);
497 ret = 0;
499 done:
500 TALLOC_FREE(mem_ctx);
501 return ret;
505 * Add a share, with a couple of standard parameters, partly optional.
507 * This is a high level utility function of the net conf utility,
508 * not a direct frontend to the smbconf API.
510 static int net_conf_addshare(struct net_context *c,
511 struct smbconf_ctx *conf_ctx, int argc,
512 const char **argv)
514 int ret = -1;
515 WERROR werr = WERR_OK;
516 char *sharename = NULL;
517 const char *path = NULL;
518 const char *comment = NULL;
519 const char *guest_ok = "no";
520 const char *writeable = "no";
521 SMB_STRUCT_STAT sbuf;
522 TALLOC_CTX *mem_ctx = talloc_stackframe();
524 if (c->display_usage) {
525 net_conf_addshare_usage(c, argc, argv);
526 ret = 0;
527 goto done;
530 switch (argc) {
531 case 0:
532 case 1:
533 default:
534 net_conf_addshare_usage(c, argc, argv);
535 goto done;
536 case 5:
537 comment = argv[4];
538 case 4:
539 if (!strnequal(argv[3], "guest_ok=", 9)) {
540 net_conf_addshare_usage(c, argc, argv);
541 goto done;
543 switch (argv[3][9]) {
544 case 'y':
545 case 'Y':
546 guest_ok = "yes";
547 break;
548 case 'n':
549 case 'N':
550 guest_ok = "no";
551 break;
552 default:
553 net_conf_addshare_usage(c, argc, argv);
554 goto done;
556 case 3:
557 if (!strnequal(argv[2], "writeable=", 10)) {
558 net_conf_addshare_usage(c, argc, argv);
559 goto done;
561 switch (argv[2][10]) {
562 case 'y':
563 case 'Y':
564 writeable = "yes";
565 break;
566 case 'n':
567 case 'N':
568 writeable = "no";
569 break;
570 default:
571 net_conf_addshare_usage(c, argc, argv);
572 goto done;
574 case 2:
575 path = argv[1];
576 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
577 if (sharename == NULL) {
578 d_printf("error: out of memory!\n");
579 goto done;
582 break;
586 * validate arguments
589 /* validate share name */
591 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
592 strlen(sharename)))
594 d_fprintf(stderr, "ERROR: share name %s contains "
595 "invalid characters (any of %s)\n",
596 sharename, INVALID_SHARENAME_CHARS);
597 goto done;
600 if (strequal(sharename, GLOBAL_NAME)) {
601 d_fprintf(stderr,
602 "ERROR: 'global' is not a valid share name.\n");
603 goto done;
606 if (smbconf_share_exists(conf_ctx, sharename)) {
607 d_fprintf(stderr, "ERROR: share %s already exists.\n",
608 sharename);
609 goto done;
612 /* validate path */
614 if (path[0] != '/') {
615 d_fprintf(stderr,
616 "Error: path '%s' is not an absolute path.\n",
617 path);
618 goto done;
621 if (sys_stat(path, &sbuf) != 0) {
622 d_fprintf(stderr,
623 "ERROR: cannot stat path '%s' to ensure "
624 "this is a directory.\n"
625 "Error was '%s'.\n",
626 path, strerror(errno));
627 goto done;
630 if (!S_ISDIR(sbuf.st_mode)) {
631 d_fprintf(stderr,
632 "ERROR: path '%s' is not a directory.\n",
633 path);
634 goto done;
638 * create the share
641 werr = smbconf_create_share(conf_ctx, sharename);
642 if (!W_ERROR_IS_OK(werr)) {
643 d_fprintf(stderr, "Error creating share %s: %s\n",
644 sharename, dos_errstr(werr));
645 goto done;
649 * fill the share with parameters
652 werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
653 if (!W_ERROR_IS_OK(werr)) {
654 d_fprintf(stderr, "Error setting parameter %s: %s\n",
655 "path", dos_errstr(werr));
656 goto done;
659 if (comment != NULL) {
660 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
661 comment);
662 if (!W_ERROR_IS_OK(werr)) {
663 d_fprintf(stderr, "Error setting parameter %s: %s\n",
664 "comment", dos_errstr(werr));
665 goto done;
669 werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
670 if (!W_ERROR_IS_OK(werr)) {
671 d_fprintf(stderr, "Error setting parameter %s: %s\n",
672 "'guest ok'", dos_errstr(werr));
673 goto done;
676 werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
677 writeable);
678 if (!W_ERROR_IS_OK(werr)) {
679 d_fprintf(stderr, "Error setting parameter %s: %s\n",
680 "writeable", dos_errstr(werr));
681 goto done;
684 ret = 0;
686 done:
687 TALLOC_FREE(mem_ctx);
688 return ret;
691 static int net_conf_delshare(struct net_context *c,
692 struct smbconf_ctx *conf_ctx, int argc,
693 const char **argv)
695 int ret = -1;
696 const char *sharename = NULL;
697 WERROR werr = WERR_OK;
698 TALLOC_CTX *mem_ctx = talloc_stackframe();
700 if (argc != 1 || c->display_usage) {
701 net_conf_delshare_usage(c, argc, argv);
702 goto done;
704 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
705 if (sharename == NULL) {
706 d_printf("error: out of memory!\n");
707 goto done;
710 werr = smbconf_delete_share(conf_ctx, sharename);
711 if (!W_ERROR_IS_OK(werr)) {
712 d_fprintf(stderr, "Error deleting share %s: %s\n",
713 sharename, dos_errstr(werr));
714 goto done;
717 ret = 0;
718 done:
719 TALLOC_FREE(mem_ctx);
720 return ret;
723 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
724 int argc, const char **argv)
726 int ret = -1;
727 WERROR werr = WERR_OK;
728 char *service = NULL;
729 char *param = NULL;
730 const char *value_str = NULL;
731 TALLOC_CTX *mem_ctx = talloc_stackframe();
733 if (argc != 3 || c->display_usage) {
734 net_conf_setparm_usage(c, argc, argv);
735 goto done;
737 service = talloc_strdup_lower(mem_ctx, argv[0]);
738 if (service == NULL) {
739 d_printf("error: out of memory!\n");
740 goto done;
742 param = talloc_strdup_lower(mem_ctx, argv[1]);
743 if (param == NULL) {
744 d_printf("error: out of memory!\n");
745 goto done;
747 value_str = argv[2];
749 if (!smbconf_share_exists(conf_ctx, service)) {
750 werr = smbconf_create_share(conf_ctx, service);
751 if (!W_ERROR_IS_OK(werr)) {
752 d_fprintf(stderr, "Error creating share '%s': %s\n",
753 service, dos_errstr(werr));
754 goto done;
758 werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
760 if (!W_ERROR_IS_OK(werr)) {
761 d_fprintf(stderr, "Error setting value '%s': %s\n",
762 param, dos_errstr(werr));
763 goto done;
766 ret = 0;
768 done:
769 TALLOC_FREE(mem_ctx);
770 return ret;
773 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
774 int argc, const char **argv)
776 int ret = -1;
777 WERROR werr = WERR_OK;
778 char *service = NULL;
779 char *param = NULL;
780 char *valstr = NULL;
781 TALLOC_CTX *mem_ctx;
783 mem_ctx = talloc_stackframe();
785 if (argc != 2 || c->display_usage) {
786 net_conf_getparm_usage(c, argc, argv);
787 goto done;
789 service = talloc_strdup_lower(mem_ctx, argv[0]);
790 if (service == NULL) {
791 d_printf("error: out of memory!\n");
792 goto done;
794 param = talloc_strdup_lower(mem_ctx, argv[1]);
795 if (param == NULL) {
796 d_printf("error: out of memory!\n");
797 goto done;
800 werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
802 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
803 d_fprintf(stderr,
804 "Error: given service '%s' does not exist.\n",
805 service);
806 goto done;
807 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
808 d_fprintf(stderr,
809 "Error: given parameter '%s' is not set.\n",
810 param);
811 goto done;
812 } else if (!W_ERROR_IS_OK(werr)) {
813 d_fprintf(stderr, "Error getting value '%s': %s.\n",
814 param, dos_errstr(werr));
815 goto done;
818 d_printf("%s\n", valstr);
820 ret = 0;
821 done:
822 TALLOC_FREE(mem_ctx);
823 return ret;
826 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
827 int argc, const char **argv)
829 int ret = -1;
830 WERROR werr = WERR_OK;
831 char *service = NULL;
832 char *param = NULL;
833 TALLOC_CTX *mem_ctx = talloc_stackframe();
835 if (argc != 2 || c->display_usage) {
836 net_conf_delparm_usage(c, argc, argv);
837 goto done;
839 service = talloc_strdup_lower(mem_ctx, argv[0]);
840 if (service == NULL) {
841 d_printf("error: out of memory!\n");
842 goto done;
844 param = talloc_strdup_lower(mem_ctx, argv[1]);
845 if (param == NULL) {
846 d_printf("error: out of memory!\n");
847 goto done;
850 werr = smbconf_delete_parameter(conf_ctx, service, param);
852 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
853 d_fprintf(stderr,
854 "Error: given service '%s' does not exist.\n",
855 service);
856 goto done;
857 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
858 d_fprintf(stderr,
859 "Error: given parameter '%s' is not set.\n",
860 param);
861 goto done;
862 } else if (!W_ERROR_IS_OK(werr)) {
863 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
864 param, dos_errstr(werr));
865 goto done;
868 ret = 0;
870 done:
871 TALLOC_FREE(mem_ctx);
872 return ret;
875 static int net_conf_getincludes(struct net_context *c,
876 struct smbconf_ctx *conf_ctx,
877 int argc, const char **argv)
879 WERROR werr;
880 uint32_t num_includes;
881 uint32_t count;
882 char *service;
883 char **includes = NULL;
884 int ret = -1;
885 TALLOC_CTX *mem_ctx = talloc_stackframe();
887 if (argc != 1 || c->display_usage) {
888 net_conf_getincludes_usage(c, argc, argv);
889 goto done;
892 service = talloc_strdup_lower(mem_ctx, argv[0]);
893 if (service == NULL) {
894 d_printf("error: out of memory!\n");
895 goto done;
898 werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
899 &num_includes, &includes);
900 if (!W_ERROR_IS_OK(werr)) {
901 d_printf("error getting includes: %s\n", dos_errstr(werr));
902 goto done;
905 for (count = 0; count < num_includes; count++) {
906 d_printf("include = %s\n", includes[count]);
909 ret = 0;
911 done:
912 TALLOC_FREE(mem_ctx);
913 return ret;
916 static int net_conf_setincludes(struct net_context *c,
917 struct smbconf_ctx *conf_ctx,
918 int argc, const char **argv)
920 WERROR werr;
921 char *service;
922 uint32_t num_includes;
923 const char **includes;
924 int ret = -1;
925 TALLOC_CTX *mem_ctx = talloc_stackframe();
927 if (argc < 1 || c->display_usage) {
928 net_conf_setincludes_usage(c, argc, argv);
929 goto done;
932 service = talloc_strdup_lower(mem_ctx, argv[0]);
933 if (service == NULL) {
934 d_printf("error: out of memory!\n");
935 goto done;
938 num_includes = argc - 1;
939 if (num_includes == 0) {
940 includes = NULL;
941 } else {
942 includes = argv + 1;
945 werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
946 if (!W_ERROR_IS_OK(werr)) {
947 d_printf("error setting includes: %s\n", dos_errstr(werr));
948 goto done;
951 ret = 0;
953 done:
954 TALLOC_FREE(mem_ctx);
955 return ret;
958 static int net_conf_delincludes(struct net_context *c,
959 struct smbconf_ctx *conf_ctx,
960 int argc, const char **argv)
962 WERROR werr;
963 char *service;
964 int ret = -1;
965 TALLOC_CTX *mem_ctx = talloc_stackframe();
967 if (argc != 1 || c->display_usage) {
968 net_conf_delincludes_usage(c, argc, argv);
969 goto done;
972 service = talloc_strdup_lower(mem_ctx, argv[0]);
973 if (service == NULL) {
974 d_printf("error: out of memory!\n");
975 goto done;
978 werr = smbconf_delete_includes(conf_ctx, service);
979 if (!W_ERROR_IS_OK(werr)) {
980 d_printf("error deleting includes: %s\n", dos_errstr(werr));
981 goto done;
984 ret = 0;
986 done:
987 TALLOC_FREE(mem_ctx);
988 return ret;
992 /**********************************************************************
994 * Wrapper and net_conf_run_function mechanism.
996 **********************************************************************/
999 * Wrapper function to call the main conf functions.
1000 * The wrapper calls handles opening and closing of the
1001 * configuration.
1003 static int net_conf_wrap_function(struct net_context *c,
1004 int (*fn)(struct net_context *,
1005 struct smbconf_ctx *,
1006 int, const char **),
1007 int argc, const char **argv)
1009 WERROR werr;
1010 TALLOC_CTX *mem_ctx = talloc_stackframe();
1011 struct smbconf_ctx *conf_ctx;
1012 int ret = -1;
1014 werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1016 if (!W_ERROR_IS_OK(werr)) {
1017 return -1;
1020 ret = fn(c, conf_ctx, argc, argv);
1022 smbconf_shutdown(conf_ctx);
1024 return ret;
1028 * We need a functable struct of our own, because the
1029 * functions are called through a wrapper that handles
1030 * the opening and closing of the configuration, and so on.
1032 struct conf_functable {
1033 const char *funcname;
1034 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1035 const char **argv);
1036 int valid_transports;
1037 const char *description;
1038 const char *usage;
1042 * This imitates net_run_function but calls the main functions
1043 * through the wrapper net_conf_wrap_function().
1045 static int net_conf_run_function(struct net_context *c, int argc,
1046 const char **argv, const char *whoami,
1047 struct conf_functable *table)
1049 int i;
1051 if (argc != 0) {
1052 for (i=0; table[i].funcname; i++) {
1053 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1054 return net_conf_wrap_function(c, table[i].fn,
1055 argc-1,
1056 argv+1);
1060 d_printf("Usage:\n");
1061 for (i=0; table[i].funcname; i++) {
1062 if (c->display_usage == false)
1063 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1064 table[i].description);
1065 else
1066 d_printf("%s\n", table[i].usage);
1069 return c->display_usage?0:-1;
1073 * Entry-point for all the CONF functions.
1076 int net_conf(struct net_context *c, int argc, const char **argv)
1078 int ret = -1;
1079 struct conf_functable func_table[] = {
1081 "list",
1082 net_conf_list,
1083 NET_TRANSPORT_LOCAL,
1084 "Dump the complete configuration in smb.conf like "
1085 "format.",
1086 "net conf list\n"
1087 " Dump the complete configuration in smb.conf like "
1088 "format."
1092 "import",
1093 net_conf_import,
1094 NET_TRANSPORT_LOCAL,
1095 "Import configuration from file in smb.conf format.",
1096 "net conf import\n"
1097 " Import configuration from file in smb.conf format."
1100 "listshares",
1101 net_conf_listshares,
1102 NET_TRANSPORT_LOCAL,
1103 "List the share names.",
1104 "net conf listshares\n"
1105 " List the share names."
1108 "drop",
1109 net_conf_drop,
1110 NET_TRANSPORT_LOCAL,
1111 "Delete the complete configuration.",
1112 "net conf drop\n"
1113 " Delete the complete configuration."
1116 "showshare",
1117 net_conf_showshare,
1118 NET_TRANSPORT_LOCAL,
1119 "Show the definition of a share.",
1120 "net conf showshare\n"
1121 " Show the definition of a share."
1124 "addshare",
1125 net_conf_addshare,
1126 NET_TRANSPORT_LOCAL,
1127 "Create a new share.",
1128 "net conf addshare\n"
1129 " Create a new share."
1132 "delshare",
1133 net_conf_delshare,
1134 NET_TRANSPORT_LOCAL,
1135 "Delete a share.",
1136 "net conf delshare\n"
1137 " Delete a share."
1140 "setparm",
1141 net_conf_setparm,
1142 NET_TRANSPORT_LOCAL,
1143 "Store a parameter.",
1144 "net conf setparm\n"
1145 " Store a parameter."
1148 "getparm",
1149 net_conf_getparm,
1150 NET_TRANSPORT_LOCAL,
1151 "Retrieve the value of a parameter.",
1152 "net conf getparm\n"
1153 " Retrieve the value of a parameter."
1156 "delparm",
1157 net_conf_delparm,
1158 NET_TRANSPORT_LOCAL,
1159 "Delete a parameter.",
1160 "net conf delparm\n"
1161 " Delete a parameter."
1164 "getincludes",
1165 net_conf_getincludes,
1166 NET_TRANSPORT_LOCAL,
1167 "Show the includes of a share definition.",
1168 "net conf getincludes\n"
1169 " Show the includes of a share definition."
1172 "setincludes",
1173 net_conf_setincludes,
1174 NET_TRANSPORT_LOCAL,
1175 "Set includes for a share.",
1176 "net conf setincludes\n"
1177 " Set includes for a share."
1180 "delincludes",
1181 net_conf_delincludes,
1182 NET_TRANSPORT_LOCAL,
1183 "Delete includes from a share definition.",
1184 "net conf setincludes\n"
1185 " Delete includes from a share definition."
1187 {NULL, NULL, 0, NULL, NULL}
1190 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1192 return ret;