wafsamba: add optional keep_underscore=True to SAMBA_LIBRARY()
[Samba.git] / source3 / utils / net_conf.c
blobe43cd1216446024c6827779e3290a2d40f5d0a0b
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 "lib/smbconf/smbconf.h"
34 #include "lib/smbconf/smbconf_init.h"
35 #include "lib/smbconf/smbconf_reg.h"
36 #include "lib/param/loadparm.h"
38 /**********************************************************************
40 * usage functions
42 **********************************************************************/
44 static int net_conf_list_usage(struct net_context *c, int argc,
45 const char **argv)
47 d_printf("%s net conf list\n", _("Usage:"));
48 return -1;
51 static int net_conf_import_usage(struct net_context *c, int argc,
52 const char**argv)
54 d_printf("%s\n%s",
55 _("Usage:"),
56 _(" net conf import [--test|-T] <filename> "
57 "[<servicename>]\n"
58 "\t[--test|-T] testmode - do not act, just print "
59 "what would be done\n"
60 "\t<servicename> only import service <servicename>, "
61 "ignore the rest\n"));
62 return -1;
65 static int net_conf_listshares_usage(struct net_context *c, int argc,
66 const char **argv)
68 d_printf("%s\nnet conf listshares\n", _("Usage:"));
69 return -1;
72 static int net_conf_drop_usage(struct net_context *c, int argc,
73 const char **argv)
75 d_printf("%s\nnet conf drop\n", _("Usage:"));
76 return -1;
79 static int net_conf_showshare_usage(struct net_context *c, int argc,
80 const char **argv)
82 d_printf("%s\n%s",
83 _("Usage:"),
84 _("net conf showshare <sharename>\n"));
85 return -1;
88 static int net_conf_addshare_usage(struct net_context *c, int argc,
89 const char **argv)
91 d_printf("%s\n%s",
92 _("Usage:"),
93 _(" net conf addshare <sharename> <path> "
94 "[writeable={y|N} [guest_ok={y|N} [<comment>]]]\n"
95 "\t<sharename> the new share name.\n"
96 "\t<path> the path on the filesystem to export.\n"
97 "\twriteable={y|N} set \"writeable to \"yes\" or "
98 "\"no\" (default) on this share.\n"
99 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
100 "\"no\" (default) on this share.\n"
101 "\t<comment> optional comment for the new share.\n"));
102 return -1;
105 static int net_conf_delshare_usage(struct net_context *c, int argc,
106 const char **argv)
108 d_printf("%s\n%s",
109 _("Usage:"),
110 _("net conf delshare <sharename>\n"));
111 return -1;
114 static int net_conf_setparm_usage(struct net_context *c, int argc,
115 const char **argv)
117 d_printf("%s\n%s",
118 _("Usage:"),
119 _(" net conf setparm <section> <param> <value>\n"));
120 return -1;
123 static int net_conf_getparm_usage(struct net_context *c, int argc,
124 const char **argv)
126 d_printf("%s\n%s",
127 _("Usage:"),
128 _(" net conf getparm <section> <param>\n"));
129 return -1;
132 static int net_conf_delparm_usage(struct net_context *c, int argc,
133 const char **argv)
135 d_printf("%s\n%s",
136 _("Usage:"),
137 _(" net conf delparm <section> <param>\n"));
138 return -1;
141 static int net_conf_getincludes_usage(struct net_context *c, int argc,
142 const char **argv)
144 d_printf("%s\n%s",
145 _("Usage:"),
146 _(" net conf getincludes <section>\n"));
147 return -1;
150 static int net_conf_setincludes_usage(struct net_context *c, int argc,
151 const char **argv)
153 d_printf("%s\n%s",
154 _("Usage:"),
155 _(" net conf setincludes <section> [<filename>]*\n"));
156 return -1;
159 static int net_conf_delincludes_usage(struct net_context *c, int argc,
160 const char **argv)
162 d_printf("%s\n%s",
163 _("Usage:"),
164 _(" net conf delincludes <section>\n"));
165 return -1;
169 /**********************************************************************
171 * Helper functions
173 **********************************************************************/
176 * This functions process a service previously loaded with libsmbconf.
178 static sbcErr import_process_service(struct net_context *c,
179 struct smbconf_ctx *conf_ctx,
180 struct smbconf_service *service)
182 sbcErr err = SBC_ERR_OK;
184 if (c->opt_testmode) {
185 uint32_t idx;
186 const char *indent = "";
187 if (service->name != NULL) {
188 d_printf("[%s]\n", service->name);
189 indent = "\t";
191 for (idx = 0; idx < service->num_params; idx++) {
192 d_printf("%s%s = %s\n", indent,
193 service->param_names[idx],
194 service->param_values[idx]);
196 d_printf("\n");
197 goto done;
200 if (smbconf_share_exists(conf_ctx, service->name)) {
201 err = smbconf_delete_share(conf_ctx, service->name);
202 if (!SBC_ERROR_IS_OK(err)) {
203 goto done;
207 err = smbconf_create_set_share(conf_ctx, service);
209 done:
210 return err;
214 /**********************************************************************
216 * the main conf functions
218 **********************************************************************/
220 static int net_conf_list(struct net_context *c, struct smbconf_ctx *conf_ctx,
221 int argc, const char **argv)
223 sbcErr err;
224 int ret = -1;
225 TALLOC_CTX *mem_ctx;
226 uint32_t num_shares;
227 uint32_t share_count, param_count;
228 struct smbconf_service **shares = NULL;
230 mem_ctx = talloc_stackframe();
232 if (argc != 0 || c->display_usage) {
233 net_conf_list_usage(c, argc, argv);
234 goto done;
237 err = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &shares);
238 if (!SBC_ERROR_IS_OK(err)) {
239 d_fprintf(stderr, _("Error getting config: %s\n"),
240 sbcErrorString(err));
241 goto done;
244 for (share_count = 0; share_count < num_shares; share_count++) {
245 const char *indent = "";
246 if (shares[share_count]->name != NULL) {
247 d_printf("[%s]\n", shares[share_count]->name);
248 indent = "\t";
250 for (param_count = 0;
251 param_count < shares[share_count]->num_params;
252 param_count++)
254 d_printf("%s%s = %s\n",
255 indent,
256 shares[share_count]->param_names[param_count],
257 shares[share_count]->param_values[param_count]);
259 d_printf("\n");
262 ret = 0;
264 done:
265 TALLOC_FREE(mem_ctx);
266 return ret;
269 static int net_conf_import(struct net_context *c, struct smbconf_ctx *conf_ctx,
270 int argc, const char **argv)
272 int ret = -1;
273 const char *filename = NULL;
274 const char *servicename = NULL;
275 char *conf_source = NULL;
276 TALLOC_CTX *mem_ctx;
277 struct smbconf_ctx *txt_ctx;
278 sbcErr err;
280 if (c->display_usage)
281 return net_conf_import_usage(c, argc, argv);
283 mem_ctx = talloc_stackframe();
285 switch (argc) {
286 case 0:
287 default:
288 net_conf_import_usage(c, argc, argv);
289 goto done;
290 case 2:
291 servicename = talloc_strdup(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 err = smbconf_init(mem_ctx, &txt_ctx, conf_source);
311 if (!SBC_ERROR_IS_OK(err)) {
312 d_printf(_("error loading file '%s': %s\n"), filename,
313 sbcErrorString(err));
314 goto done;
317 if (c->opt_testmode) {
318 d_printf(_("\nTEST MODE - "
319 "would import the following configuration:\n\n"));
322 if (servicename != NULL) {
323 struct smbconf_service *service = NULL;
325 err = smbconf_get_share(txt_ctx, mem_ctx,
326 servicename,
327 &service);
328 if (!SBC_ERROR_IS_OK(err)) {
329 goto cancel;
332 err = smbconf_transaction_start(conf_ctx);
333 if (!SBC_ERROR_IS_OK(err)) {
334 d_printf(_("error starting transaction: %s\n"),
335 sbcErrorString(err));
336 goto done;
339 err = import_process_service(c, conf_ctx, service);
340 if (!SBC_ERROR_IS_OK(err)) {
341 goto cancel;
343 } else {
344 struct smbconf_service **services = NULL;
345 uint32_t num_shares, sidx;
347 err = smbconf_get_config(txt_ctx, mem_ctx,
348 &num_shares,
349 &services);
350 if (!SBC_ERROR_IS_OK(err)) {
351 goto cancel;
353 if (!c->opt_testmode) {
354 if (!SBC_ERROR_IS_OK(smbconf_drop(conf_ctx))) {
355 goto cancel;
360 * Wrap the importing of shares into a transaction,
361 * but only 100 at a time, in order to save memory.
362 * The allocated memory accumulates across the actions
363 * within the transaction, and for me, some 1500
364 * imported shares, the MAX_TALLOC_SIZE of 256 MB
365 * was exceeded.
367 err = smbconf_transaction_start(conf_ctx);
368 if (!SBC_ERROR_IS_OK(err)) {
369 d_printf(_("error starting transaction: %s\n"),
370 sbcErrorString(err));
371 goto done;
374 for (sidx = 0; sidx < num_shares; sidx++) {
375 err = import_process_service(c, conf_ctx,
376 services[sidx]);
377 if (!SBC_ERROR_IS_OK(err)) {
378 goto cancel;
381 if (sidx % 100) {
382 continue;
385 err = smbconf_transaction_commit(conf_ctx);
386 if (!SBC_ERROR_IS_OK(err)) {
387 d_printf(_("error committing transaction: "
388 "%s\n"),
389 sbcErrorString(err));
390 goto done;
392 err = smbconf_transaction_start(conf_ctx);
393 if (!SBC_ERROR_IS_OK(err)) {
394 d_printf(_("error starting transaction: %s\n"),
395 sbcErrorString(err));
396 goto done;
401 err = smbconf_transaction_commit(conf_ctx);
402 if (!SBC_ERROR_IS_OK(err)) {
403 d_printf(_("error committing transaction: %s\n"),
404 sbcErrorString(err));
405 } else {
406 ret = 0;
409 goto done;
411 cancel:
412 err = smbconf_transaction_cancel(conf_ctx);
413 if (!SBC_ERROR_IS_OK(err)) {
414 d_printf(_("error cancelling transaction: %s\n"),
415 sbcErrorString(err));
418 done:
419 TALLOC_FREE(mem_ctx);
420 return ret;
423 static int net_conf_listshares(struct net_context *c,
424 struct smbconf_ctx *conf_ctx, int argc,
425 const char **argv)
427 sbcErr err;
428 int ret = -1;
429 uint32_t count, num_shares = 0;
430 char **share_names = NULL;
431 TALLOC_CTX *mem_ctx;
433 mem_ctx = talloc_stackframe();
435 if (argc != 0 || c->display_usage) {
436 net_conf_listshares_usage(c, argc, argv);
437 goto done;
440 err = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
441 &share_names);
442 if (!SBC_ERROR_IS_OK(err)) {
443 goto done;
446 for (count = 0; count < num_shares; count++)
448 d_printf("%s\n", share_names[count]);
451 ret = 0;
453 done:
454 TALLOC_FREE(mem_ctx);
455 return ret;
458 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
459 int argc, const char **argv)
461 int ret = -1;
462 sbcErr err;
464 if (argc != 0 || c->display_usage) {
465 net_conf_drop_usage(c, argc, argv);
466 goto done;
469 err = smbconf_drop(conf_ctx);
470 if (!SBC_ERROR_IS_OK(err)) {
471 d_fprintf(stderr, _("Error deleting configuration: %s\n"),
472 sbcErrorString(err));
473 goto done;
476 ret = 0;
478 done:
479 return ret;
482 static int net_conf_showshare(struct net_context *c,
483 struct smbconf_ctx *conf_ctx, int argc,
484 const char **argv)
486 int ret = -1;
487 sbcErr err;
488 const char *sharename = NULL;
489 TALLOC_CTX *mem_ctx;
490 uint32_t count;
491 struct smbconf_service *service = NULL;
493 mem_ctx = talloc_stackframe();
495 if (argc != 1 || c->display_usage) {
496 net_conf_showshare_usage(c, argc, argv);
497 goto done;
500 sharename = talloc_strdup(mem_ctx, argv[0]);
501 if (sharename == NULL) {
502 d_printf("error: out of memory!\n");
503 goto done;
506 err = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
507 if (!SBC_ERROR_IS_OK(err)) {
508 d_printf(_("error getting share parameters: %s\n"),
509 sbcErrorString(err));
510 goto done;
513 d_printf("[%s]\n", service->name);
515 for (count = 0; count < service->num_params; count++) {
516 d_printf("\t%s = %s\n", service->param_names[count],
517 service->param_values[count]);
520 ret = 0;
522 done:
523 TALLOC_FREE(mem_ctx);
524 return ret;
528 * Add a share, with a couple of standard parameters, partly optional.
530 * This is a high level utility function of the net conf utility,
531 * not a direct frontend to the smbconf API.
533 static int net_conf_addshare(struct net_context *c,
534 struct smbconf_ctx *conf_ctx, int argc,
535 const char **argv)
537 int ret = -1;
538 sbcErr err;
539 char *sharename = NULL;
540 const char *path = NULL;
541 const char *comment = NULL;
542 const char *guest_ok = "no";
543 const char *writeable = "no";
544 TALLOC_CTX *mem_ctx = talloc_stackframe();
546 if (c->display_usage) {
547 net_conf_addshare_usage(c, argc, argv);
548 ret = 0;
549 goto done;
552 switch (argc) {
553 case 0:
554 case 1:
555 default:
556 net_conf_addshare_usage(c, argc, argv);
557 goto done;
558 case 5:
559 comment = argv[4];
560 case 4:
561 if (!strnequal(argv[3], "guest_ok=", 9)) {
562 net_conf_addshare_usage(c, argc, argv);
563 goto done;
565 switch (argv[3][9]) {
566 case 'y':
567 case 'Y':
568 guest_ok = "yes";
569 break;
570 case 'n':
571 case 'N':
572 guest_ok = "no";
573 break;
574 default:
575 net_conf_addshare_usage(c, argc, argv);
576 goto done;
578 case 3:
579 if (!strnequal(argv[2], "writeable=", 10)) {
580 net_conf_addshare_usage(c, argc, argv);
581 goto done;
583 switch (argv[2][10]) {
584 case 'y':
585 case 'Y':
586 writeable = "yes";
587 break;
588 case 'n':
589 case 'N':
590 writeable = "no";
591 break;
592 default:
593 net_conf_addshare_usage(c, argc, argv);
594 goto done;
596 case 2:
597 path = argv[1];
598 sharename = talloc_strdup(mem_ctx, argv[0]);
599 if (sharename == NULL) {
600 d_printf(_("error: out of memory!\n"));
601 goto done;
604 break;
608 * validate arguments
611 /* validate share name */
613 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
614 strlen(sharename)))
616 d_fprintf(stderr, _("ERROR: share name %s contains "
617 "invalid characters (any of %s)\n"),
618 sharename, INVALID_SHARENAME_CHARS);
619 goto done;
622 if (strequal(sharename, GLOBAL_NAME)) {
623 d_fprintf(stderr,
624 _("ERROR: 'global' is not a valid share name.\n"));
625 goto done;
628 if (smbconf_share_exists(conf_ctx, sharename)) {
629 d_fprintf(stderr, _("ERROR: share %s already exists.\n"),
630 sharename);
631 goto done;
634 /* validate path */
636 if (path[0] != '/') {
637 d_fprintf(stderr,
638 _("Error: path '%s' is not an absolute path.\n"),
639 path);
640 goto done;
644 * start a transaction
647 err = smbconf_transaction_start(conf_ctx);
648 if (!SBC_ERROR_IS_OK(err)) {
649 d_printf("error starting transaction: %s\n",
650 sbcErrorString(err));
651 goto done;
655 * create the share
658 err = smbconf_create_share(conf_ctx, sharename);
659 if (!SBC_ERROR_IS_OK(err)) {
660 d_fprintf(stderr, _("Error creating share %s: %s\n"),
661 sharename, sbcErrorString(err));
662 goto cancel;
666 * fill the share with parameters
669 err = smbconf_set_parameter(conf_ctx, sharename, "path", path);
670 if (!SBC_ERROR_IS_OK(err)) {
671 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
672 "path", sbcErrorString(err));
673 goto cancel;
676 if (comment != NULL) {
677 err = smbconf_set_parameter(conf_ctx, sharename, "comment",
678 comment);
679 if (!SBC_ERROR_IS_OK(err)) {
680 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
681 "comment", sbcErrorString(err));
682 goto cancel;
686 err = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
687 if (!SBC_ERROR_IS_OK(err)) {
688 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
689 "'guest ok'", sbcErrorString(err));
690 goto cancel;
693 err = smbconf_set_parameter(conf_ctx, sharename, "writeable",
694 writeable);
695 if (!SBC_ERROR_IS_OK(err)) {
696 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
697 "writeable", sbcErrorString(err));
698 goto cancel;
702 * commit the whole thing
705 err = smbconf_transaction_commit(conf_ctx);
706 if (!SBC_ERROR_IS_OK(err)) {
707 d_printf("error committing transaction: %s\n",
708 sbcErrorString(err));
709 } else {
710 ret = 0;
713 goto done;
715 cancel:
716 err = smbconf_transaction_cancel(conf_ctx);
717 if (!SBC_ERROR_IS_OK(err)) {
718 d_printf("error cancelling transaction: %s\n",
719 sbcErrorString(err));
722 done:
723 TALLOC_FREE(mem_ctx);
724 return ret;
727 static int net_conf_delshare(struct net_context *c,
728 struct smbconf_ctx *conf_ctx, int argc,
729 const char **argv)
731 int ret = -1;
732 const char *sharename = NULL;
733 sbcErr err;
734 TALLOC_CTX *mem_ctx = talloc_stackframe();
736 if (argc != 1 || c->display_usage) {
737 net_conf_delshare_usage(c, argc, argv);
738 goto done;
740 sharename = talloc_strdup(mem_ctx, argv[0]);
741 if (sharename == NULL) {
742 d_printf(_("error: out of memory!\n"));
743 goto done;
746 err = smbconf_delete_share(conf_ctx, sharename);
747 if (!SBC_ERROR_IS_OK(err)) {
748 d_fprintf(stderr, _("Error deleting share %s: %s\n"),
749 sharename, sbcErrorString(err));
750 goto done;
753 ret = 0;
754 done:
755 TALLOC_FREE(mem_ctx);
756 return ret;
759 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
760 int argc, const char **argv)
762 int ret = -1;
763 sbcErr err;
764 char *service = NULL;
765 char *param = NULL;
766 const char *value_str = NULL;
767 TALLOC_CTX *mem_ctx = talloc_stackframe();
769 if (argc != 3 || c->display_usage) {
770 net_conf_setparm_usage(c, argc, argv);
771 goto done;
774 * NULL service name means "dangling parameters" to libsmbconf.
775 * We use the empty string from the command line for this purpose.
777 if (strlen(argv[0]) != 0) {
778 service = talloc_strdup(mem_ctx, argv[0]);
779 if (service == NULL) {
780 d_printf(_("error: out of memory!\n"));
781 goto done;
784 param = strlower_talloc(mem_ctx, argv[1]);
785 if (param == NULL) {
786 d_printf(_("error: out of memory!\n"));
787 goto done;
789 value_str = argv[2];
791 err = smbconf_transaction_start(conf_ctx);
792 if (!SBC_ERROR_IS_OK(err)) {
793 d_printf(_("error starting transaction: %s\n"),
794 sbcErrorString(err));
795 goto done;
798 if (!smbconf_share_exists(conf_ctx, service)) {
799 err = smbconf_create_share(conf_ctx, service);
800 if (!SBC_ERROR_IS_OK(err)) {
801 d_fprintf(stderr, _("Error creating share '%s': %s\n"),
802 service, sbcErrorString(err));
803 goto cancel;
807 err = smbconf_set_parameter(conf_ctx, service, param, value_str);
808 if (!SBC_ERROR_IS_OK(err)) {
809 d_fprintf(stderr, _("Error setting value '%s': %s\n"),
810 param, sbcErrorString(err));
811 goto cancel;
814 err = smbconf_transaction_commit(conf_ctx);
815 if (!SBC_ERROR_IS_OK(err)) {
816 d_printf(_("error committing transaction: %s\n"),
817 sbcErrorString(err));
818 } else {
819 ret = 0;
822 goto done;
824 cancel:
825 err = smbconf_transaction_cancel(conf_ctx);
826 if (!SBC_ERROR_IS_OK(err)) {
827 d_printf(_("error cancelling transaction: %s\n"),
828 sbcErrorString(err));
831 done:
832 TALLOC_FREE(mem_ctx);
833 return ret;
836 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
837 int argc, const char **argv)
839 int ret = -1;
840 sbcErr err;
841 char *service = NULL;
842 char *param = NULL;
843 char *valstr = NULL;
844 TALLOC_CTX *mem_ctx;
846 mem_ctx = talloc_stackframe();
848 if (argc != 2 || c->display_usage) {
849 net_conf_getparm_usage(c, argc, argv);
850 goto done;
853 * NULL service name means "dangling parameters" to libsmbconf.
854 * We use the empty string from the command line for this purpose.
856 if (strlen(argv[0]) != 0) {
857 service = talloc_strdup(mem_ctx, argv[0]);
858 if (service == NULL) {
859 d_printf(_("error: out of memory!\n"));
860 goto done;
863 param = strlower_talloc(mem_ctx, argv[1]);
864 if (param == NULL) {
865 d_printf(_("error: out of memory!\n"));
866 goto done;
869 err = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
870 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
871 d_fprintf(stderr,
872 _("Error: given service '%s' does not exist.\n"),
873 service);
874 goto done;
875 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
876 d_fprintf(stderr,
877 _("Error: given parameter '%s' is not set.\n"),
878 param);
879 goto done;
880 } else if (!SBC_ERROR_IS_OK(err)) {
881 d_fprintf(stderr, _("Error getting value '%s': %s.\n"),
882 param, sbcErrorString(err));
883 goto done;
886 d_printf("%s\n", valstr);
888 ret = 0;
889 done:
890 TALLOC_FREE(mem_ctx);
891 return ret;
894 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
895 int argc, const char **argv)
897 int ret = -1;
898 sbcErr err;
899 char *service = NULL;
900 char *param = NULL;
901 TALLOC_CTX *mem_ctx = talloc_stackframe();
903 if (argc != 2 || c->display_usage) {
904 net_conf_delparm_usage(c, argc, argv);
905 goto done;
908 * NULL service name means "dangling parameters" to libsmbconf.
909 * We use the empty string from the command line for this purpose.
911 if (strlen(argv[0]) != 0) {
912 service = talloc_strdup(mem_ctx, argv[0]);
913 if (service == NULL) {
914 d_printf(_("error: out of memory!\n"));
915 goto done;
918 param = strlower_talloc(mem_ctx, argv[1]);
919 if (param == NULL) {
920 d_printf("error: out of memory!\n");
921 goto done;
924 err = smbconf_delete_parameter(conf_ctx, service, param);
925 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
926 d_fprintf(stderr,
927 _("Error: given service '%s' does not exist.\n"),
928 service);
929 goto done;
930 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
931 d_fprintf(stderr,
932 _("Error: given parameter '%s' is not set.\n"),
933 param);
934 goto done;
935 } else if (!SBC_ERROR_IS_OK(err)) {
936 d_fprintf(stderr, _("Error deleting value '%s': %s.\n"),
937 param, sbcErrorString(err));
938 goto done;
941 ret = 0;
943 done:
944 TALLOC_FREE(mem_ctx);
945 return ret;
948 static int net_conf_getincludes(struct net_context *c,
949 struct smbconf_ctx *conf_ctx,
950 int argc, const char **argv)
952 sbcErr err;
953 uint32_t num_includes;
954 uint32_t count;
955 char *service;
956 char **includes = NULL;
957 int ret = -1;
958 TALLOC_CTX *mem_ctx = talloc_stackframe();
960 if (argc != 1 || c->display_usage) {
961 net_conf_getincludes_usage(c, argc, argv);
962 goto done;
965 service = talloc_strdup(mem_ctx, argv[0]);
966 if (service == NULL) {
967 d_printf(_("error: out of memory!\n"));
968 goto done;
971 err = smbconf_get_includes(conf_ctx, mem_ctx, service,
972 &num_includes, &includes);
973 if (!SBC_ERROR_IS_OK(err)) {
974 d_printf(_("error getting includes: %s\n"), sbcErrorString(err));
975 goto done;
978 for (count = 0; count < num_includes; count++) {
979 d_printf("include = %s\n", includes[count]);
982 ret = 0;
984 done:
985 TALLOC_FREE(mem_ctx);
986 return ret;
989 static int net_conf_setincludes(struct net_context *c,
990 struct smbconf_ctx *conf_ctx,
991 int argc, const char **argv)
993 sbcErr err;
994 char *service;
995 uint32_t num_includes;
996 const char **includes;
997 int ret = -1;
998 TALLOC_CTX *mem_ctx = talloc_stackframe();
1000 if (argc < 1 || c->display_usage) {
1001 net_conf_setincludes_usage(c, argc, argv);
1002 goto done;
1005 service = talloc_strdup(mem_ctx, argv[0]);
1006 if (service == NULL) {
1007 d_printf(_("error: out of memory!\n"));
1008 goto done;
1011 num_includes = argc - 1;
1012 if (num_includes == 0) {
1013 includes = NULL;
1014 } else {
1015 includes = argv + 1;
1018 err = smbconf_set_includes(conf_ctx, service, num_includes, includes);
1019 if (!SBC_ERROR_IS_OK(err)) {
1020 d_printf(_("error setting includes: %s\n"), sbcErrorString(err));
1021 goto done;
1024 ret = 0;
1026 done:
1027 TALLOC_FREE(mem_ctx);
1028 return ret;
1031 static int net_conf_delincludes(struct net_context *c,
1032 struct smbconf_ctx *conf_ctx,
1033 int argc, const char **argv)
1035 sbcErr err;
1036 char *service;
1037 int ret = -1;
1038 TALLOC_CTX *mem_ctx = talloc_stackframe();
1040 if (argc != 1 || c->display_usage) {
1041 net_conf_delincludes_usage(c, argc, argv);
1042 goto done;
1045 service = talloc_strdup(mem_ctx, argv[0]);
1046 if (service == NULL) {
1047 d_printf(_("error: out of memory!\n"));
1048 goto done;
1051 err = smbconf_delete_includes(conf_ctx, service);
1052 if (!SBC_ERROR_IS_OK(err)) {
1053 d_printf(_("error deleting includes: %s\n"), sbcErrorString(err));
1054 goto done;
1057 ret = 0;
1059 done:
1060 TALLOC_FREE(mem_ctx);
1061 return ret;
1065 /**********************************************************************
1067 * Wrapper and net_conf_run_function mechanism.
1069 **********************************************************************/
1072 * Wrapper function to call the main conf functions.
1073 * The wrapper calls handles opening and closing of the
1074 * configuration.
1076 static int net_conf_wrap_function(struct net_context *c,
1077 int (*fn)(struct net_context *,
1078 struct smbconf_ctx *,
1079 int, const char **),
1080 int argc, const char **argv)
1082 sbcErr err;
1083 TALLOC_CTX *mem_ctx = talloc_stackframe();
1084 struct smbconf_ctx *conf_ctx;
1085 int ret = -1;
1087 err = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1088 if (!SBC_ERROR_IS_OK(err)) {
1089 talloc_free(mem_ctx);
1090 return -1;
1093 ret = fn(c, conf_ctx, argc, argv);
1095 smbconf_shutdown(conf_ctx);
1097 talloc_free(mem_ctx);
1098 return ret;
1102 * We need a functable struct of our own, because the
1103 * functions are called through a wrapper that handles
1104 * the opening and closing of the configuration, and so on.
1106 struct conf_functable {
1107 const char *funcname;
1108 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1109 const char **argv);
1110 int valid_transports;
1111 const char *description;
1112 const char *usage;
1116 * This imitates net_run_function but calls the main functions
1117 * through the wrapper net_conf_wrap_function().
1119 static int net_conf_run_function(struct net_context *c, int argc,
1120 const char **argv, const char *whoami,
1121 struct conf_functable *table)
1123 int i;
1125 if (argc != 0) {
1126 for (i=0; table[i].funcname; i++) {
1127 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
1128 return net_conf_wrap_function(c, table[i].fn,
1129 argc-1,
1130 argv+1);
1134 d_printf(_("Usage:\n"));
1135 for (i=0; table[i].funcname; i++) {
1136 if (c->display_usage == false)
1137 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1138 table[i].description);
1139 else
1140 d_printf("%s\n", table[i].usage);
1143 return c->display_usage?0:-1;
1147 * Entry-point for all the CONF functions.
1150 int net_conf(struct net_context *c, int argc, const char **argv)
1152 int ret = -1;
1153 struct conf_functable func_table[] = {
1155 "list",
1156 net_conf_list,
1157 NET_TRANSPORT_LOCAL,
1158 N_("Dump the complete configuration in smb.conf like "
1159 "format."),
1160 N_("net conf list\n"
1161 " Dump the complete configuration in smb.conf "
1162 "like format.")
1166 "import",
1167 net_conf_import,
1168 NET_TRANSPORT_LOCAL,
1169 N_("Import configuration from file in smb.conf "
1170 "format."),
1171 N_("net conf import\n"
1172 " Import configuration from file in smb.conf "
1173 "format.")
1176 "listshares",
1177 net_conf_listshares,
1178 NET_TRANSPORT_LOCAL,
1179 N_("List the share names."),
1180 N_("net conf listshares\n"
1181 " List the share names.")
1184 "drop",
1185 net_conf_drop,
1186 NET_TRANSPORT_LOCAL,
1187 N_("Delete the complete configuration."),
1188 N_("net conf drop\n"
1189 " Delete the complete configuration.")
1192 "showshare",
1193 net_conf_showshare,
1194 NET_TRANSPORT_LOCAL,
1195 N_("Show the definition of a share."),
1196 N_("net conf showshare\n"
1197 " Show the definition of a share.")
1200 "addshare",
1201 net_conf_addshare,
1202 NET_TRANSPORT_LOCAL,
1203 N_("Create a new share."),
1204 N_("net conf addshare\n"
1205 " Create a new share.")
1208 "delshare",
1209 net_conf_delshare,
1210 NET_TRANSPORT_LOCAL,
1211 N_("Delete a share."),
1212 N_("net conf delshare\n"
1213 " Delete a share.")
1216 "setparm",
1217 net_conf_setparm,
1218 NET_TRANSPORT_LOCAL,
1219 N_("Store a parameter."),
1220 N_("net conf setparm\n"
1221 " Store a parameter.")
1224 "getparm",
1225 net_conf_getparm,
1226 NET_TRANSPORT_LOCAL,
1227 N_("Retrieve the value of a parameter."),
1228 N_("net conf getparm\n"
1229 " Retrieve the value of a parameter.")
1232 "delparm",
1233 net_conf_delparm,
1234 NET_TRANSPORT_LOCAL,
1235 N_("Delete a parameter."),
1236 N_("net conf delparm\n"
1237 " Delete a parameter.")
1240 "getincludes",
1241 net_conf_getincludes,
1242 NET_TRANSPORT_LOCAL,
1243 N_("Show the includes of a share definition."),
1244 N_("net conf getincludes\n"
1245 " Show the includes of a share definition.")
1248 "setincludes",
1249 net_conf_setincludes,
1250 NET_TRANSPORT_LOCAL,
1251 N_("Set includes for a share."),
1252 N_("net conf setincludes\n"
1253 " Set includes for a share.")
1256 "delincludes",
1257 net_conf_delincludes,
1258 NET_TRANSPORT_LOCAL,
1259 N_("Delete includes from a share definition."),
1260 N_("net conf delincludes\n"
1261 " Delete includes from a share definition.")
1263 {NULL, NULL, 0, NULL, NULL}
1266 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1268 return ret;