torture: fix whitespace/tab mixup in internal_torture_run_test()
[Samba.git] / source3 / utils / net_conf.c
blobe8481b4d5f975e8332c0b6cd366a4d2eb6c0bbd8
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 "system/filesys.h"
32 #include "utils/net.h"
33 #include "utils/net_conf_util.h"
34 #include "lib/smbconf/smbconf.h"
35 #include "lib/smbconf/smbconf_init.h"
36 #include "lib/smbconf/smbconf_reg.h"
37 #include "lib/param/loadparm.h"
39 /**********************************************************************
41 * usage functions
43 **********************************************************************/
45 static int net_conf_list_usage(struct net_context *c, int argc,
46 const char **argv)
48 d_printf("%s net conf list\n", _("Usage:"));
49 return -1;
52 static int net_conf_import_usage(struct net_context *c, int argc,
53 const char**argv)
55 d_printf("%s\n%s",
56 _("Usage:"),
57 _(" net conf import [--test|-T] <filename> "
58 "[<servicename>]\n"
59 "\t[--test|-T] testmode - do not act, just print "
60 "what would be done\n"
61 "\t<servicename> only import service <servicename>, "
62 "ignore the rest\n"));
63 return -1;
66 static int net_conf_listshares_usage(struct net_context *c, int argc,
67 const char **argv)
69 d_printf("%s\nnet conf listshares\n", _("Usage:"));
70 return -1;
73 static int net_conf_drop_usage(struct net_context *c, int argc,
74 const char **argv)
76 d_printf("%s\nnet conf drop\n", _("Usage:"));
77 return -1;
80 static int net_conf_showshare_usage(struct net_context *c, int argc,
81 const char **argv)
83 d_printf("%s\n%s",
84 _("Usage:"),
85 _("net conf showshare <sharename>\n"));
86 return -1;
89 static int net_conf_addshare_usage(struct net_context *c, int argc,
90 const char **argv)
92 d_printf("%s\n%s",
93 _("Usage:"),
94 _(" net conf addshare <sharename> <path> "
95 "[writeable={y|N} [guest_ok={y|N} [<comment>]]]\n"
96 "\t<sharename> the new share name.\n"
97 "\t<path> the path on the filesystem to export.\n"
98 "\twriteable={y|N} set \"writeable to \"yes\" or "
99 "\"no\" (default) on this share.\n"
100 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
101 "\"no\" (default) on this share.\n"
102 "\t<comment> optional comment for the new share.\n"));
103 return -1;
106 static int net_conf_delshare_usage(struct net_context *c, int argc,
107 const char **argv)
109 d_printf("%s\n%s",
110 _("Usage:"),
111 _("net conf delshare <sharename>\n"));
112 return -1;
115 static int net_conf_setparm_usage(struct net_context *c, int argc,
116 const char **argv)
118 d_printf("%s\n%s",
119 _("Usage:"),
120 _(" net conf setparm <section> <param> <value>\n"));
121 return -1;
124 static int net_conf_getparm_usage(struct net_context *c, int argc,
125 const char **argv)
127 d_printf("%s\n%s",
128 _("Usage:"),
129 _(" net conf getparm <section> <param>\n"));
130 return -1;
133 static int net_conf_delparm_usage(struct net_context *c, int argc,
134 const char **argv)
136 d_printf("%s\n%s",
137 _("Usage:"),
138 _(" net conf delparm <section> <param>\n"));
139 return -1;
142 static int net_conf_getincludes_usage(struct net_context *c, int argc,
143 const char **argv)
145 d_printf("%s\n%s",
146 _("Usage:"),
147 _(" net conf getincludes <section>\n"));
148 return -1;
151 static int net_conf_setincludes_usage(struct net_context *c, int argc,
152 const char **argv)
154 d_printf("%s\n%s",
155 _("Usage:"),
156 _(" net conf setincludes <section> [<filename>]*\n"));
157 return -1;
160 static int net_conf_delincludes_usage(struct net_context *c, int argc,
161 const char **argv)
163 d_printf("%s\n%s",
164 _("Usage:"),
165 _(" net conf delincludes <section>\n"));
166 return -1;
170 /**********************************************************************
172 * Helper functions
174 **********************************************************************/
177 * This functions process a service previously loaded with libsmbconf.
179 static sbcErr import_process_service(struct net_context *c,
180 struct smbconf_ctx *conf_ctx,
181 struct smbconf_service *service)
183 sbcErr err = SBC_ERR_OK;
185 if (c->opt_testmode) {
186 uint32_t idx;
187 const char *indent = "";
188 if (service->name != NULL) {
189 d_printf("[%s]\n", service->name);
190 indent = "\t";
192 for (idx = 0; idx < service->num_params; idx++) {
193 d_printf("%s%s = %s\n", indent,
194 service->param_names[idx],
195 service->param_values[idx]);
197 d_printf("\n");
198 goto done;
201 if (smbconf_share_exists(conf_ctx, service->name)) {
202 err = smbconf_delete_share(conf_ctx, service->name);
203 if (!SBC_ERROR_IS_OK(err)) {
204 goto done;
208 err = smbconf_create_set_share(conf_ctx, service);
210 done:
211 return err;
215 /**********************************************************************
217 * the main conf functions
219 **********************************************************************/
221 static int net_conf_list(struct net_context *c, struct smbconf_ctx *conf_ctx,
222 int argc, const char **argv)
224 sbcErr err;
225 int ret = -1;
226 TALLOC_CTX *mem_ctx;
227 uint32_t num_shares;
228 uint32_t share_count, param_count;
229 struct smbconf_service **shares = NULL;
231 mem_ctx = talloc_stackframe();
233 if (argc != 0 || c->display_usage) {
234 net_conf_list_usage(c, argc, argv);
235 goto done;
238 err = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &shares);
239 if (!SBC_ERROR_IS_OK(err)) {
240 d_fprintf(stderr, _("Error getting config: %s\n"),
241 sbcErrorString(err));
242 goto done;
245 for (share_count = 0; share_count < num_shares; share_count++) {
246 const char *indent = "";
247 if (shares[share_count]->name != NULL) {
248 d_printf("[%s]\n", shares[share_count]->name);
249 indent = "\t";
251 for (param_count = 0;
252 param_count < shares[share_count]->num_params;
253 param_count++)
255 d_printf("%s%s = %s\n",
256 indent,
257 shares[share_count]->param_names[param_count],
258 shares[share_count]->param_values[param_count]);
260 d_printf("\n");
263 ret = 0;
265 done:
266 TALLOC_FREE(mem_ctx);
267 return ret;
270 static int net_conf_import(struct net_context *c, struct smbconf_ctx *conf_ctx,
271 int argc, const char **argv)
273 int ret = -1;
274 const char *filename = NULL;
275 const char *servicename = NULL;
276 char *conf_source = NULL;
277 TALLOC_CTX *mem_ctx;
278 struct smbconf_ctx *txt_ctx;
279 sbcErr err;
281 if (c->display_usage)
282 return net_conf_import_usage(c, argc, argv);
284 mem_ctx = talloc_stackframe();
286 switch (argc) {
287 case 0:
288 default:
289 net_conf_import_usage(c, argc, argv);
290 goto done;
291 case 2:
292 servicename = talloc_strdup(mem_ctx, argv[1]);
293 if (servicename == NULL) {
294 d_printf(_("error: out of memory!\n"));
295 goto done;
297 case 1:
298 filename = argv[0];
299 break;
302 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
303 filename));
305 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
306 if (conf_source == NULL) {
307 d_printf(_("error: out of memory!\n"));
308 goto done;
311 err = smbconf_init(mem_ctx, &txt_ctx, conf_source);
312 if (!SBC_ERROR_IS_OK(err)) {
313 d_printf(_("error loading file '%s': %s\n"), filename,
314 sbcErrorString(err));
315 goto done;
318 if (c->opt_testmode) {
319 d_printf(_("\nTEST MODE - "
320 "would import the following configuration:\n\n"));
323 if (servicename != NULL) {
324 struct smbconf_service *service = NULL;
326 err = smbconf_get_share(txt_ctx, mem_ctx,
327 servicename,
328 &service);
329 if (!SBC_ERROR_IS_OK(err)) {
330 goto cancel;
333 err = smbconf_transaction_start(conf_ctx);
334 if (!SBC_ERROR_IS_OK(err)) {
335 d_printf(_("error starting transaction: %s\n"),
336 sbcErrorString(err));
337 goto done;
340 err = import_process_service(c, conf_ctx, service);
341 if (!SBC_ERROR_IS_OK(err)) {
342 goto cancel;
344 } else {
345 struct smbconf_service **services = NULL;
346 uint32_t num_shares, sidx;
348 err = smbconf_get_config(txt_ctx, mem_ctx,
349 &num_shares,
350 &services);
351 if (!SBC_ERROR_IS_OK(err)) {
352 goto cancel;
354 if (!c->opt_testmode) {
355 if (!SBC_ERROR_IS_OK(smbconf_drop(conf_ctx))) {
356 goto cancel;
361 * Wrap the importing of shares into a transaction,
362 * but only 100 at a time, in order to save memory.
363 * The allocated memory accumulates across the actions
364 * within the transaction, and for me, some 1500
365 * imported shares, the MAX_TALLOC_SIZE of 256 MB
366 * was exceeded.
368 err = smbconf_transaction_start(conf_ctx);
369 if (!SBC_ERROR_IS_OK(err)) {
370 d_printf(_("error starting transaction: %s\n"),
371 sbcErrorString(err));
372 goto done;
375 for (sidx = 0; sidx < num_shares; sidx++) {
376 err = import_process_service(c, conf_ctx,
377 services[sidx]);
378 if (!SBC_ERROR_IS_OK(err)) {
379 goto cancel;
382 if (sidx % 100) {
383 continue;
386 err = smbconf_transaction_commit(conf_ctx);
387 if (!SBC_ERROR_IS_OK(err)) {
388 d_printf(_("error committing transaction: "
389 "%s\n"),
390 sbcErrorString(err));
391 goto done;
393 err = smbconf_transaction_start(conf_ctx);
394 if (!SBC_ERROR_IS_OK(err)) {
395 d_printf(_("error starting transaction: %s\n"),
396 sbcErrorString(err));
397 goto done;
402 err = smbconf_transaction_commit(conf_ctx);
403 if (!SBC_ERROR_IS_OK(err)) {
404 d_printf(_("error committing transaction: %s\n"),
405 sbcErrorString(err));
406 } else {
407 ret = 0;
410 goto done;
412 cancel:
413 err = smbconf_transaction_cancel(conf_ctx);
414 if (!SBC_ERROR_IS_OK(err)) {
415 d_printf(_("error cancelling transaction: %s\n"),
416 sbcErrorString(err));
419 done:
420 TALLOC_FREE(mem_ctx);
421 return ret;
424 static int net_conf_listshares(struct net_context *c,
425 struct smbconf_ctx *conf_ctx, int argc,
426 const char **argv)
428 sbcErr err;
429 int ret = -1;
430 uint32_t count, num_shares = 0;
431 char **share_names = NULL;
432 TALLOC_CTX *mem_ctx;
434 mem_ctx = talloc_stackframe();
436 if (argc != 0 || c->display_usage) {
437 net_conf_listshares_usage(c, argc, argv);
438 goto done;
441 err = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
442 &share_names);
443 if (!SBC_ERROR_IS_OK(err)) {
444 goto done;
447 for (count = 0; count < num_shares; count++)
449 d_printf("%s\n", share_names[count]);
452 ret = 0;
454 done:
455 TALLOC_FREE(mem_ctx);
456 return ret;
459 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
460 int argc, const char **argv)
462 int ret = -1;
463 sbcErr err;
465 if (argc != 0 || c->display_usage) {
466 net_conf_drop_usage(c, argc, argv);
467 goto done;
470 err = smbconf_drop(conf_ctx);
471 if (!SBC_ERROR_IS_OK(err)) {
472 d_fprintf(stderr, _("Error deleting configuration: %s\n"),
473 sbcErrorString(err));
474 goto done;
477 ret = 0;
479 done:
480 return ret;
483 static int net_conf_showshare(struct net_context *c,
484 struct smbconf_ctx *conf_ctx, int argc,
485 const char **argv)
487 int ret = -1;
488 sbcErr err;
489 const char *sharename = NULL;
490 TALLOC_CTX *mem_ctx;
491 uint32_t count;
492 struct smbconf_service *service = NULL;
494 mem_ctx = talloc_stackframe();
496 if (argc != 1 || c->display_usage) {
497 net_conf_showshare_usage(c, argc, argv);
498 goto done;
501 sharename = talloc_strdup(mem_ctx, argv[0]);
502 if (sharename == NULL) {
503 d_printf("error: out of memory!\n");
504 goto done;
507 err = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
508 if (!SBC_ERROR_IS_OK(err)) {
509 d_printf(_("error getting share parameters: %s\n"),
510 sbcErrorString(err));
511 goto done;
514 d_printf("[%s]\n", service->name);
516 for (count = 0; count < service->num_params; count++) {
517 d_printf("\t%s = %s\n", service->param_names[count],
518 service->param_values[count]);
521 ret = 0;
523 done:
524 TALLOC_FREE(mem_ctx);
525 return ret;
529 * Add a share, with a couple of standard parameters, partly optional.
531 * This is a high level utility function of the net conf utility,
532 * not a direct frontend to the smbconf API.
534 static int net_conf_addshare(struct net_context *c,
535 struct smbconf_ctx *conf_ctx, int argc,
536 const char **argv)
538 int ret = -1;
539 sbcErr err;
540 char *sharename = NULL;
541 const char *path = NULL;
542 const char *comment = NULL;
543 const char *guest_ok = "no";
544 const char *writeable = "no";
545 TALLOC_CTX *mem_ctx = talloc_stackframe();
547 if (c->display_usage) {
548 net_conf_addshare_usage(c, argc, argv);
549 ret = 0;
550 goto done;
553 switch (argc) {
554 case 0:
555 case 1:
556 default:
557 net_conf_addshare_usage(c, argc, argv);
558 goto done;
559 case 5:
560 comment = argv[4];
561 case 4:
562 if (!strnequal(argv[3], "guest_ok=", 9)) {
563 net_conf_addshare_usage(c, argc, argv);
564 goto done;
566 switch (argv[3][9]) {
567 case 'y':
568 case 'Y':
569 guest_ok = "yes";
570 break;
571 case 'n':
572 case 'N':
573 guest_ok = "no";
574 break;
575 default:
576 net_conf_addshare_usage(c, argc, argv);
577 goto done;
579 case 3:
580 if (!strnequal(argv[2], "writeable=", 10)) {
581 net_conf_addshare_usage(c, argc, argv);
582 goto done;
584 switch (argv[2][10]) {
585 case 'y':
586 case 'Y':
587 writeable = "yes";
588 break;
589 case 'n':
590 case 'N':
591 writeable = "no";
592 break;
593 default:
594 net_conf_addshare_usage(c, argc, argv);
595 goto done;
597 case 2:
598 path = argv[1];
599 sharename = talloc_strdup(mem_ctx, argv[0]);
600 if (sharename == NULL) {
601 d_printf(_("error: out of memory!\n"));
602 goto done;
605 break;
609 * validate arguments
612 /* validate share name */
614 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
615 strlen(sharename)))
617 d_fprintf(stderr, _("ERROR: share name %s contains "
618 "invalid characters (any of %s)\n"),
619 sharename, INVALID_SHARENAME_CHARS);
620 goto done;
623 if (strequal(sharename, GLOBAL_NAME)) {
624 d_fprintf(stderr,
625 _("ERROR: 'global' is not a valid share name.\n"));
626 goto done;
629 if (smbconf_share_exists(conf_ctx, sharename)) {
630 d_fprintf(stderr, _("ERROR: share %s already exists.\n"),
631 sharename);
632 goto done;
635 /* validate path */
637 if (path[0] != '/') {
638 d_fprintf(stderr,
639 _("Error: path '%s' is not an absolute path.\n"),
640 path);
641 goto done;
645 * start a transaction
648 err = smbconf_transaction_start(conf_ctx);
649 if (!SBC_ERROR_IS_OK(err)) {
650 d_printf("error starting transaction: %s\n",
651 sbcErrorString(err));
652 goto done;
656 * create the share
659 err = smbconf_create_share(conf_ctx, sharename);
660 if (!SBC_ERROR_IS_OK(err)) {
661 d_fprintf(stderr, _("Error creating share %s: %s\n"),
662 sharename, sbcErrorString(err));
663 goto cancel;
667 * fill the share with parameters
670 err = smbconf_set_parameter(conf_ctx, sharename, "path", path);
671 if (!SBC_ERROR_IS_OK(err)) {
672 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
673 "path", sbcErrorString(err));
674 goto cancel;
677 if (comment != NULL) {
678 err = smbconf_set_parameter(conf_ctx, sharename, "comment",
679 comment);
680 if (!SBC_ERROR_IS_OK(err)) {
681 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
682 "comment", sbcErrorString(err));
683 goto cancel;
687 err = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
688 if (!SBC_ERROR_IS_OK(err)) {
689 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
690 "'guest ok'", sbcErrorString(err));
691 goto cancel;
694 err = smbconf_set_parameter(conf_ctx, sharename, "writeable",
695 writeable);
696 if (!SBC_ERROR_IS_OK(err)) {
697 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
698 "writeable", sbcErrorString(err));
699 goto cancel;
703 * commit the whole thing
706 err = smbconf_transaction_commit(conf_ctx);
707 if (!SBC_ERROR_IS_OK(err)) {
708 d_printf("error committing transaction: %s\n",
709 sbcErrorString(err));
710 } else {
711 ret = 0;
714 goto done;
716 cancel:
717 err = smbconf_transaction_cancel(conf_ctx);
718 if (!SBC_ERROR_IS_OK(err)) {
719 d_printf("error cancelling transaction: %s\n",
720 sbcErrorString(err));
723 done:
724 TALLOC_FREE(mem_ctx);
725 return ret;
728 static int net_conf_delshare(struct net_context *c,
729 struct smbconf_ctx *conf_ctx, int argc,
730 const char **argv)
732 int ret = -1;
733 const char *sharename = NULL;
734 sbcErr err;
735 TALLOC_CTX *mem_ctx = talloc_stackframe();
737 if (argc != 1 || c->display_usage) {
738 net_conf_delshare_usage(c, argc, argv);
739 goto done;
741 sharename = talloc_strdup(mem_ctx, argv[0]);
742 if (sharename == NULL) {
743 d_printf(_("error: out of memory!\n"));
744 goto done;
747 err = smbconf_delete_share(conf_ctx, sharename);
748 if (!SBC_ERROR_IS_OK(err)) {
749 d_fprintf(stderr, _("Error deleting share %s: %s\n"),
750 sharename, sbcErrorString(err));
751 goto done;
754 ret = 0;
755 done:
756 TALLOC_FREE(mem_ctx);
757 return ret;
760 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
761 int argc, const char **argv)
763 int ret = -1;
764 sbcErr err;
765 char *service = NULL;
766 char *param = NULL;
767 const char *value_str = NULL;
768 TALLOC_CTX *mem_ctx = talloc_stackframe();
770 if (argc != 3 || c->display_usage) {
771 net_conf_setparm_usage(c, argc, argv);
772 goto done;
775 * NULL service name means "dangling parameters" to libsmbconf.
776 * We use the empty string from the command line for this purpose.
778 if (strlen(argv[0]) != 0) {
779 service = talloc_strdup(mem_ctx, argv[0]);
780 if (service == NULL) {
781 d_printf(_("error: out of memory!\n"));
782 goto done;
785 param = strlower_talloc(mem_ctx, argv[1]);
786 if (param == NULL) {
787 d_printf(_("error: out of memory!\n"));
788 goto done;
790 value_str = argv[2];
792 if (!net_conf_param_valid(service,param, value_str)) {
793 goto done;
796 err = smbconf_transaction_start(conf_ctx);
797 if (!SBC_ERROR_IS_OK(err)) {
798 d_printf(_("error starting transaction: %s\n"),
799 sbcErrorString(err));
800 goto done;
803 if (!smbconf_share_exists(conf_ctx, service)) {
804 err = smbconf_create_share(conf_ctx, service);
805 if (!SBC_ERROR_IS_OK(err)) {
806 d_fprintf(stderr, _("Error creating share '%s': %s\n"),
807 service, sbcErrorString(err));
808 goto cancel;
812 err = smbconf_set_parameter(conf_ctx, service, param, value_str);
813 if (!SBC_ERROR_IS_OK(err)) {
814 d_fprintf(stderr, _("Error setting value '%s': %s\n"),
815 param, sbcErrorString(err));
816 goto cancel;
819 err = smbconf_transaction_commit(conf_ctx);
820 if (!SBC_ERROR_IS_OK(err)) {
821 d_printf(_("error committing transaction: %s\n"),
822 sbcErrorString(err));
823 } else {
824 ret = 0;
827 goto done;
829 cancel:
830 err = smbconf_transaction_cancel(conf_ctx);
831 if (!SBC_ERROR_IS_OK(err)) {
832 d_printf(_("error cancelling transaction: %s\n"),
833 sbcErrorString(err));
836 done:
837 TALLOC_FREE(mem_ctx);
838 return ret;
841 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
842 int argc, const char **argv)
844 int ret = -1;
845 sbcErr err;
846 char *service = NULL;
847 char *param = NULL;
848 char *valstr = NULL;
849 TALLOC_CTX *mem_ctx;
851 mem_ctx = talloc_stackframe();
853 if (argc != 2 || c->display_usage) {
854 net_conf_getparm_usage(c, argc, argv);
855 goto done;
858 * NULL service name means "dangling parameters" to libsmbconf.
859 * We use the empty string from the command line for this purpose.
861 if (strlen(argv[0]) != 0) {
862 service = talloc_strdup(mem_ctx, argv[0]);
863 if (service == NULL) {
864 d_printf(_("error: out of memory!\n"));
865 goto done;
868 param = strlower_talloc(mem_ctx, argv[1]);
869 if (param == NULL) {
870 d_printf(_("error: out of memory!\n"));
871 goto done;
874 err = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
875 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
876 d_fprintf(stderr,
877 _("Error: given service '%s' does not exist.\n"),
878 service);
879 goto done;
880 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
881 d_fprintf(stderr,
882 _("Error: given parameter '%s' is not set.\n"),
883 param);
884 goto done;
885 } else if (!SBC_ERROR_IS_OK(err)) {
886 d_fprintf(stderr, _("Error getting value '%s': %s.\n"),
887 param, sbcErrorString(err));
888 goto done;
891 d_printf("%s\n", valstr);
893 ret = 0;
894 done:
895 TALLOC_FREE(mem_ctx);
896 return ret;
899 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
900 int argc, const char **argv)
902 int ret = -1;
903 sbcErr err;
904 char *service = NULL;
905 char *param = NULL;
906 TALLOC_CTX *mem_ctx = talloc_stackframe();
908 if (argc != 2 || c->display_usage) {
909 net_conf_delparm_usage(c, argc, argv);
910 goto done;
913 * NULL service name means "dangling parameters" to libsmbconf.
914 * We use the empty string from the command line for this purpose.
916 if (strlen(argv[0]) != 0) {
917 service = talloc_strdup(mem_ctx, argv[0]);
918 if (service == NULL) {
919 d_printf(_("error: out of memory!\n"));
920 goto done;
923 param = strlower_talloc(mem_ctx, argv[1]);
924 if (param == NULL) {
925 d_printf("error: out of memory!\n");
926 goto done;
929 err = smbconf_delete_parameter(conf_ctx, service, param);
930 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
931 d_fprintf(stderr,
932 _("Error: given service '%s' does not exist.\n"),
933 service);
934 goto done;
935 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
936 d_fprintf(stderr,
937 _("Error: given parameter '%s' is not set.\n"),
938 param);
939 goto done;
940 } else if (!SBC_ERROR_IS_OK(err)) {
941 d_fprintf(stderr, _("Error deleting value '%s': %s.\n"),
942 param, sbcErrorString(err));
943 goto done;
946 ret = 0;
948 done:
949 TALLOC_FREE(mem_ctx);
950 return ret;
953 static int net_conf_getincludes(struct net_context *c,
954 struct smbconf_ctx *conf_ctx,
955 int argc, const char **argv)
957 sbcErr err;
958 uint32_t num_includes;
959 uint32_t count;
960 char *service;
961 char **includes = NULL;
962 int ret = -1;
963 TALLOC_CTX *mem_ctx = talloc_stackframe();
965 if (argc != 1 || c->display_usage) {
966 net_conf_getincludes_usage(c, argc, argv);
967 goto done;
970 service = talloc_strdup(mem_ctx, argv[0]);
971 if (service == NULL) {
972 d_printf(_("error: out of memory!\n"));
973 goto done;
976 err = smbconf_get_includes(conf_ctx, mem_ctx, service,
977 &num_includes, &includes);
978 if (!SBC_ERROR_IS_OK(err)) {
979 d_printf(_("error getting includes: %s\n"), sbcErrorString(err));
980 goto done;
983 for (count = 0; count < num_includes; count++) {
984 d_printf("include = %s\n", includes[count]);
987 ret = 0;
989 done:
990 TALLOC_FREE(mem_ctx);
991 return ret;
994 static int net_conf_setincludes(struct net_context *c,
995 struct smbconf_ctx *conf_ctx,
996 int argc, const char **argv)
998 sbcErr err;
999 char *service;
1000 uint32_t num_includes;
1001 const char **includes;
1002 int ret = -1;
1003 TALLOC_CTX *mem_ctx = talloc_stackframe();
1005 if (argc < 1 || c->display_usage) {
1006 net_conf_setincludes_usage(c, argc, argv);
1007 goto done;
1010 service = talloc_strdup(mem_ctx, argv[0]);
1011 if (service == NULL) {
1012 d_printf(_("error: out of memory!\n"));
1013 goto done;
1016 num_includes = argc - 1;
1017 if (num_includes == 0) {
1018 includes = NULL;
1019 } else {
1020 includes = argv + 1;
1023 err = smbconf_set_includes(conf_ctx, service, num_includes, includes);
1024 if (!SBC_ERROR_IS_OK(err)) {
1025 d_printf(_("error setting includes: %s\n"), sbcErrorString(err));
1026 goto done;
1029 ret = 0;
1031 done:
1032 TALLOC_FREE(mem_ctx);
1033 return ret;
1036 static int net_conf_delincludes(struct net_context *c,
1037 struct smbconf_ctx *conf_ctx,
1038 int argc, const char **argv)
1040 sbcErr err;
1041 char *service;
1042 int ret = -1;
1043 TALLOC_CTX *mem_ctx = talloc_stackframe();
1045 if (argc != 1 || c->display_usage) {
1046 net_conf_delincludes_usage(c, argc, argv);
1047 goto done;
1050 service = talloc_strdup(mem_ctx, argv[0]);
1051 if (service == NULL) {
1052 d_printf(_("error: out of memory!\n"));
1053 goto done;
1056 err = smbconf_delete_includes(conf_ctx, service);
1057 if (!SBC_ERROR_IS_OK(err)) {
1058 d_printf(_("error deleting includes: %s\n"), sbcErrorString(err));
1059 goto done;
1062 ret = 0;
1064 done:
1065 TALLOC_FREE(mem_ctx);
1066 return ret;
1070 /**********************************************************************
1072 * Wrapper and net_conf_run_function mechanism.
1074 **********************************************************************/
1077 * Wrapper function to call the main conf functions.
1078 * The wrapper calls handles opening and closing of the
1079 * configuration.
1081 static int net_conf_wrap_function(struct net_context *c,
1082 int (*fn)(struct net_context *,
1083 struct smbconf_ctx *,
1084 int, const char **),
1085 int argc, const char **argv)
1087 sbcErr err;
1088 TALLOC_CTX *mem_ctx = talloc_stackframe();
1089 struct smbconf_ctx *conf_ctx;
1090 int ret = -1;
1092 err = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1093 if (!SBC_ERROR_IS_OK(err)) {
1094 talloc_free(mem_ctx);
1095 return -1;
1098 ret = fn(c, conf_ctx, argc, argv);
1100 smbconf_shutdown(conf_ctx);
1102 talloc_free(mem_ctx);
1103 return ret;
1107 * We need a functable struct of our own, because the
1108 * functions are called through a wrapper that handles
1109 * the opening and closing of the configuration, and so on.
1111 struct conf_functable {
1112 const char *funcname;
1113 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1114 const char **argv);
1115 int valid_transports;
1116 const char *description;
1117 const char *usage;
1121 * This imitates net_run_function but calls the main functions
1122 * through the wrapper net_conf_wrap_function().
1124 static int net_conf_run_function(struct net_context *c, int argc,
1125 const char **argv, const char *whoami,
1126 struct conf_functable *table)
1128 int i;
1130 if (argc != 0) {
1131 for (i=0; table[i].funcname; i++) {
1132 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
1133 return net_conf_wrap_function(c, table[i].fn,
1134 argc-1,
1135 argv+1);
1139 d_printf(_("Usage:\n"));
1140 for (i=0; table[i].funcname; i++) {
1141 if (c->display_usage == false)
1142 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1143 table[i].description);
1144 else
1145 d_printf("%s\n", table[i].usage);
1148 return c->display_usage?0:-1;
1152 * Entry-point for all the CONF functions.
1155 int net_conf(struct net_context *c, int argc, const char **argv)
1157 int ret = -1;
1158 struct conf_functable func_table[] = {
1160 "list",
1161 net_conf_list,
1162 NET_TRANSPORT_LOCAL,
1163 N_("Dump the complete configuration in smb.conf like "
1164 "format."),
1165 N_("net conf list\n"
1166 " Dump the complete configuration in smb.conf "
1167 "like format.")
1171 "import",
1172 net_conf_import,
1173 NET_TRANSPORT_LOCAL,
1174 N_("Import configuration from file in smb.conf "
1175 "format."),
1176 N_("net conf import\n"
1177 " Import configuration from file in smb.conf "
1178 "format.")
1181 "listshares",
1182 net_conf_listshares,
1183 NET_TRANSPORT_LOCAL,
1184 N_("List the share names."),
1185 N_("net conf listshares\n"
1186 " List the share names.")
1189 "drop",
1190 net_conf_drop,
1191 NET_TRANSPORT_LOCAL,
1192 N_("Delete the complete configuration."),
1193 N_("net conf drop\n"
1194 " Delete the complete configuration.")
1197 "showshare",
1198 net_conf_showshare,
1199 NET_TRANSPORT_LOCAL,
1200 N_("Show the definition of a share."),
1201 N_("net conf showshare\n"
1202 " Show the definition of a share.")
1205 "addshare",
1206 net_conf_addshare,
1207 NET_TRANSPORT_LOCAL,
1208 N_("Create a new share."),
1209 N_("net conf addshare\n"
1210 " Create a new share.")
1213 "delshare",
1214 net_conf_delshare,
1215 NET_TRANSPORT_LOCAL,
1216 N_("Delete a share."),
1217 N_("net conf delshare\n"
1218 " Delete a share.")
1221 "setparm",
1222 net_conf_setparm,
1223 NET_TRANSPORT_LOCAL,
1224 N_("Store a parameter."),
1225 N_("net conf setparm\n"
1226 " Store a parameter.")
1229 "getparm",
1230 net_conf_getparm,
1231 NET_TRANSPORT_LOCAL,
1232 N_("Retrieve the value of a parameter."),
1233 N_("net conf getparm\n"
1234 " Retrieve the value of a parameter.")
1237 "delparm",
1238 net_conf_delparm,
1239 NET_TRANSPORT_LOCAL,
1240 N_("Delete a parameter."),
1241 N_("net conf delparm\n"
1242 " Delete a parameter.")
1245 "getincludes",
1246 net_conf_getincludes,
1247 NET_TRANSPORT_LOCAL,
1248 N_("Show the includes of a share definition."),
1249 N_("net conf getincludes\n"
1250 " Show the includes of a share definition.")
1253 "setincludes",
1254 net_conf_setincludes,
1255 NET_TRANSPORT_LOCAL,
1256 N_("Set includes for a share."),
1257 N_("net conf setincludes\n"
1258 " Set includes for a share.")
1261 "delincludes",
1262 net_conf_delincludes,
1263 NET_TRANSPORT_LOCAL,
1264 N_("Delete includes from a share definition."),
1265 N_("net conf delincludes\n"
1266 " Delete includes from a share definition.")
1268 {NULL, NULL, 0, NULL, NULL}
1271 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1273 return ret;