ctdb-tcp: Do not stop outbound connection in ctdb_tcp_node_connect()
[Samba.git] / source3 / utils / net_conf.c
blob267c4c802df869ed6a976931d3c8c0762fb594ac
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;
298 FALL_THROUGH;
299 case 1:
300 filename = argv[0];
301 break;
304 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
305 filename));
307 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
308 if (conf_source == NULL) {
309 d_printf(_("error: out of memory!\n"));
310 goto done;
313 err = smbconf_init(mem_ctx, &txt_ctx, conf_source);
314 if (!SBC_ERROR_IS_OK(err)) {
315 d_printf(_("error loading file '%s': %s\n"), filename,
316 sbcErrorString(err));
317 goto done;
320 if (c->opt_testmode) {
321 d_printf(_("\nTEST MODE - "
322 "would import the following configuration:\n\n"));
325 if (servicename != NULL) {
326 struct smbconf_service *service = NULL;
328 err = smbconf_get_share(txt_ctx, mem_ctx,
329 servicename,
330 &service);
331 if (!SBC_ERROR_IS_OK(err)) {
332 goto cancel;
335 err = smbconf_transaction_start(conf_ctx);
336 if (!SBC_ERROR_IS_OK(err)) {
337 d_printf(_("error starting transaction: %s\n"),
338 sbcErrorString(err));
339 goto done;
342 err = import_process_service(c, conf_ctx, service);
343 if (!SBC_ERROR_IS_OK(err)) {
344 d_printf(_("error importing service %s: %s\n"),
345 servicename, sbcErrorString(err));
346 goto cancel;
348 } else {
349 struct smbconf_service **services = NULL;
350 uint32_t num_shares, sidx;
352 err = smbconf_get_config(txt_ctx, mem_ctx,
353 &num_shares,
354 &services);
355 if (!SBC_ERROR_IS_OK(err)) {
356 goto cancel;
358 if (!c->opt_testmode) {
359 if (!SBC_ERROR_IS_OK(smbconf_drop(conf_ctx))) {
360 goto cancel;
365 * Wrap the importing of shares into a transaction,
366 * but only 100 at a time, in order to save memory.
367 * The allocated memory accumulates across the actions
368 * within the transaction, and for me, some 1500
369 * imported shares, the MAX_TALLOC_SIZE of 256 MB
370 * was exceeded.
372 err = smbconf_transaction_start(conf_ctx);
373 if (!SBC_ERROR_IS_OK(err)) {
374 d_printf(_("error starting transaction: %s\n"),
375 sbcErrorString(err));
376 goto done;
379 for (sidx = 0; sidx < num_shares; sidx++) {
380 err = import_process_service(c, conf_ctx,
381 services[sidx]);
382 if (!SBC_ERROR_IS_OK(err)) {
383 d_printf(_("error importing service %s: %s\n"),
384 services[sidx]->name,
385 sbcErrorString(err));
386 goto cancel;
389 if (sidx % 100) {
390 continue;
393 err = smbconf_transaction_commit(conf_ctx);
394 if (!SBC_ERROR_IS_OK(err)) {
395 d_printf(_("error committing transaction: "
396 "%s\n"),
397 sbcErrorString(err));
398 goto done;
400 err = smbconf_transaction_start(conf_ctx);
401 if (!SBC_ERROR_IS_OK(err)) {
402 d_printf(_("error starting transaction: %s\n"),
403 sbcErrorString(err));
404 goto done;
409 err = smbconf_transaction_commit(conf_ctx);
410 if (!SBC_ERROR_IS_OK(err)) {
411 d_printf(_("error committing transaction: %s\n"),
412 sbcErrorString(err));
413 } else {
414 ret = 0;
417 goto done;
419 cancel:
420 err = smbconf_transaction_cancel(conf_ctx);
421 if (!SBC_ERROR_IS_OK(err)) {
422 d_printf(_("error cancelling transaction: %s\n"),
423 sbcErrorString(err));
426 done:
427 TALLOC_FREE(mem_ctx);
428 return ret;
431 static int net_conf_listshares(struct net_context *c,
432 struct smbconf_ctx *conf_ctx, int argc,
433 const char **argv)
435 sbcErr err;
436 int ret = -1;
437 uint32_t count, num_shares = 0;
438 char **share_names = NULL;
439 TALLOC_CTX *mem_ctx;
441 mem_ctx = talloc_stackframe();
443 if (argc != 0 || c->display_usage) {
444 net_conf_listshares_usage(c, argc, argv);
445 goto done;
448 err = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
449 &share_names);
450 if (!SBC_ERROR_IS_OK(err)) {
451 goto done;
454 for (count = 0; count < num_shares; count++)
456 d_printf("%s\n", share_names[count]);
459 ret = 0;
461 done:
462 TALLOC_FREE(mem_ctx);
463 return ret;
466 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
467 int argc, const char **argv)
469 int ret = -1;
470 sbcErr err;
472 if (argc != 0 || c->display_usage) {
473 net_conf_drop_usage(c, argc, argv);
474 goto done;
477 err = smbconf_drop(conf_ctx);
478 if (!SBC_ERROR_IS_OK(err)) {
479 d_fprintf(stderr, _("Error deleting configuration: %s\n"),
480 sbcErrorString(err));
481 goto done;
484 ret = 0;
486 done:
487 return ret;
490 static int net_conf_showshare(struct net_context *c,
491 struct smbconf_ctx *conf_ctx, int argc,
492 const char **argv)
494 int ret = -1;
495 sbcErr err;
496 const char *sharename = NULL;
497 TALLOC_CTX *mem_ctx;
498 uint32_t count;
499 struct smbconf_service *service = NULL;
501 mem_ctx = talloc_stackframe();
503 if (argc != 1 || c->display_usage) {
504 net_conf_showshare_usage(c, argc, argv);
505 goto done;
508 sharename = talloc_strdup(mem_ctx, argv[0]);
509 if (sharename == NULL) {
510 d_printf("error: out of memory!\n");
511 goto done;
514 err = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
515 if (!SBC_ERROR_IS_OK(err)) {
516 d_printf(_("error getting share parameters: %s\n"),
517 sbcErrorString(err));
518 goto done;
521 d_printf("[%s]\n", service->name);
523 for (count = 0; count < service->num_params; count++) {
524 d_printf("\t%s = %s\n", service->param_names[count],
525 service->param_values[count]);
528 ret = 0;
530 done:
531 TALLOC_FREE(mem_ctx);
532 return ret;
536 * Add a share, with a couple of standard parameters, partly optional.
538 * This is a high level utility function of the net conf utility,
539 * not a direct frontend to the smbconf API.
541 static int net_conf_addshare(struct net_context *c,
542 struct smbconf_ctx *conf_ctx, int argc,
543 const char **argv)
545 int ret = -1;
546 sbcErr err;
547 char *sharename = NULL;
548 const char *path = NULL;
549 const char *comment = NULL;
550 const char *guest_ok = "no";
551 const char *writeable = "no";
552 TALLOC_CTX *mem_ctx = talloc_stackframe();
554 if (c->display_usage) {
555 net_conf_addshare_usage(c, argc, argv);
556 ret = 0;
557 goto done;
560 switch (argc) {
561 case 0:
562 case 1:
563 default:
564 net_conf_addshare_usage(c, argc, argv);
565 goto done;
566 case 5:
567 comment = argv[4];
569 FALL_THROUGH;
570 case 4:
571 if (!strnequal(argv[3], "guest_ok=", 9)) {
572 net_conf_addshare_usage(c, argc, argv);
573 goto done;
575 switch (argv[3][9]) {
576 case 'y':
577 case 'Y':
578 guest_ok = "yes";
579 break;
580 case 'n':
581 case 'N':
582 guest_ok = "no";
583 break;
584 default:
585 net_conf_addshare_usage(c, argc, argv);
586 goto done;
589 FALL_THROUGH;
590 case 3:
591 if (!strnequal(argv[2], "writeable=", 10)) {
592 net_conf_addshare_usage(c, argc, argv);
593 goto done;
595 switch (argv[2][10]) {
596 case 'y':
597 case 'Y':
598 writeable = "yes";
599 break;
600 case 'n':
601 case 'N':
602 writeable = "no";
603 break;
604 default:
605 net_conf_addshare_usage(c, argc, argv);
606 goto done;
609 FALL_THROUGH;
610 case 2:
611 path = argv[1];
612 sharename = talloc_strdup(mem_ctx, argv[0]);
613 if (sharename == NULL) {
614 d_printf(_("error: out of memory!\n"));
615 goto done;
618 break;
622 * validate arguments
625 /* validate share name */
627 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
628 strlen(sharename)))
630 d_fprintf(stderr, _("ERROR: share name %s contains "
631 "invalid characters (any of %s)\n"),
632 sharename, INVALID_SHARENAME_CHARS);
633 goto done;
636 if (strequal(sharename, GLOBAL_NAME)) {
637 d_fprintf(stderr,
638 _("ERROR: 'global' is not a valid share name.\n"));
639 goto done;
642 if (smbconf_share_exists(conf_ctx, sharename)) {
643 d_fprintf(stderr, _("ERROR: share %s already exists.\n"),
644 sharename);
645 goto done;
648 /* validate path */
650 if (path[0] != '/') {
651 d_fprintf(stderr,
652 _("Error: path '%s' is not an absolute path.\n"),
653 path);
654 goto done;
658 * start a transaction
661 err = smbconf_transaction_start(conf_ctx);
662 if (!SBC_ERROR_IS_OK(err)) {
663 d_printf("error starting transaction: %s\n",
664 sbcErrorString(err));
665 goto done;
669 * create the share
672 err = smbconf_create_share(conf_ctx, sharename);
673 if (!SBC_ERROR_IS_OK(err)) {
674 d_fprintf(stderr, _("Error creating share %s: %s\n"),
675 sharename, sbcErrorString(err));
676 goto cancel;
680 * fill the share with parameters
683 err = smbconf_set_parameter(conf_ctx, sharename, "path", path);
684 if (!SBC_ERROR_IS_OK(err)) {
685 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
686 "path", sbcErrorString(err));
687 goto cancel;
690 if (comment != NULL) {
691 err = smbconf_set_parameter(conf_ctx, sharename, "comment",
692 comment);
693 if (!SBC_ERROR_IS_OK(err)) {
694 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
695 "comment", sbcErrorString(err));
696 goto cancel;
700 err = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
701 if (!SBC_ERROR_IS_OK(err)) {
702 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
703 "'guest ok'", sbcErrorString(err));
704 goto cancel;
707 err = smbconf_set_parameter(conf_ctx, sharename, "writeable",
708 writeable);
709 if (!SBC_ERROR_IS_OK(err)) {
710 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
711 "writeable", sbcErrorString(err));
712 goto cancel;
716 * commit the whole thing
719 err = smbconf_transaction_commit(conf_ctx);
720 if (!SBC_ERROR_IS_OK(err)) {
721 d_printf("error committing transaction: %s\n",
722 sbcErrorString(err));
723 } else {
724 ret = 0;
727 goto done;
729 cancel:
730 err = smbconf_transaction_cancel(conf_ctx);
731 if (!SBC_ERROR_IS_OK(err)) {
732 d_printf("error cancelling transaction: %s\n",
733 sbcErrorString(err));
736 done:
737 TALLOC_FREE(mem_ctx);
738 return ret;
741 static int net_conf_delshare(struct net_context *c,
742 struct smbconf_ctx *conf_ctx, int argc,
743 const char **argv)
745 int ret = -1;
746 const char *sharename = NULL;
747 sbcErr err;
748 NTSTATUS status;
749 TALLOC_CTX *mem_ctx = talloc_stackframe();
751 if (argc != 1 || c->display_usage) {
752 net_conf_delshare_usage(c, argc, argv);
753 goto done;
755 sharename = talloc_strdup(mem_ctx, argv[0]);
756 if (sharename == NULL) {
757 d_printf(_("error: out of memory!\n"));
758 goto done;
761 err = smbconf_delete_share(conf_ctx, sharename);
762 if (!SBC_ERROR_IS_OK(err)) {
763 d_fprintf(stderr, _("Error deleting share %s: %s\n"),
764 sharename, sbcErrorString(err));
765 goto done;
768 status = delete_share_security(sharename);
769 if (!NT_STATUS_IS_OK(status) &&
770 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
771 d_fprintf(stderr, _("deleting share acl failed for %s: %s\n"),
772 sharename, nt_errstr(status));
773 goto done;
776 ret = 0;
777 done:
778 TALLOC_FREE(mem_ctx);
779 return ret;
782 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
783 int argc, const char **argv)
785 int ret = -1;
786 sbcErr err;
787 char *service = NULL;
788 char *param = NULL;
789 const char *value_str = NULL;
790 TALLOC_CTX *mem_ctx = talloc_stackframe();
792 if (argc != 3 || c->display_usage) {
793 net_conf_setparm_usage(c, argc, argv);
794 goto done;
797 * NULL service name means "dangling parameters" to libsmbconf.
798 * We use the empty string from the command line for this purpose.
800 if (strlen(argv[0]) != 0) {
801 service = talloc_strdup(mem_ctx, argv[0]);
802 if (service == NULL) {
803 d_printf(_("error: out of memory!\n"));
804 goto done;
807 param = strlower_talloc(mem_ctx, argv[1]);
808 if (param == NULL) {
809 d_printf(_("error: out of memory!\n"));
810 goto done;
812 value_str = argv[2];
814 if (!net_conf_param_valid(service,param, value_str)) {
815 goto done;
818 err = smbconf_transaction_start(conf_ctx);
819 if (!SBC_ERROR_IS_OK(err)) {
820 d_printf(_("error starting transaction: %s\n"),
821 sbcErrorString(err));
822 goto done;
825 if (!smbconf_share_exists(conf_ctx, service)) {
826 err = smbconf_create_share(conf_ctx, service);
827 if (!SBC_ERROR_IS_OK(err)) {
828 d_fprintf(stderr, _("Error creating share '%s': %s\n"),
829 service, sbcErrorString(err));
830 goto cancel;
834 err = smbconf_set_parameter(conf_ctx, service, param, value_str);
835 if (!SBC_ERROR_IS_OK(err)) {
836 d_fprintf(stderr, _("Error setting value '%s': %s\n"),
837 param, sbcErrorString(err));
838 goto cancel;
841 err = smbconf_transaction_commit(conf_ctx);
842 if (!SBC_ERROR_IS_OK(err)) {
843 d_printf(_("error committing transaction: %s\n"),
844 sbcErrorString(err));
845 } else {
846 ret = 0;
849 goto done;
851 cancel:
852 err = smbconf_transaction_cancel(conf_ctx);
853 if (!SBC_ERROR_IS_OK(err)) {
854 d_printf(_("error cancelling transaction: %s\n"),
855 sbcErrorString(err));
858 done:
859 TALLOC_FREE(mem_ctx);
860 return ret;
863 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
864 int argc, const char **argv)
866 int ret = -1;
867 sbcErr err;
868 char *service = NULL;
869 char *param = NULL;
870 char *valstr = NULL;
871 TALLOC_CTX *mem_ctx;
873 mem_ctx = talloc_stackframe();
875 if (argc != 2 || c->display_usage) {
876 net_conf_getparm_usage(c, argc, argv);
877 goto done;
880 * NULL service name means "dangling parameters" to libsmbconf.
881 * We use the empty string from the command line for this purpose.
883 if (strlen(argv[0]) != 0) {
884 service = talloc_strdup(mem_ctx, argv[0]);
885 if (service == NULL) {
886 d_printf(_("error: out of memory!\n"));
887 goto done;
890 param = strlower_talloc(mem_ctx, argv[1]);
891 if (param == NULL) {
892 d_printf(_("error: out of memory!\n"));
893 goto done;
896 err = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
897 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
898 d_fprintf(stderr,
899 _("Error: given service '%s' does not exist.\n"),
900 service);
901 goto done;
902 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
903 d_fprintf(stderr,
904 _("Error: given parameter '%s' is not set.\n"),
905 param);
906 goto done;
907 } else if (!SBC_ERROR_IS_OK(err)) {
908 d_fprintf(stderr, _("Error getting value '%s': %s.\n"),
909 param, sbcErrorString(err));
910 goto done;
913 d_printf("%s\n", valstr);
915 ret = 0;
916 done:
917 TALLOC_FREE(mem_ctx);
918 return ret;
921 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
922 int argc, const char **argv)
924 int ret = -1;
925 sbcErr err;
926 char *service = NULL;
927 char *param = NULL;
928 TALLOC_CTX *mem_ctx = talloc_stackframe();
930 if (argc != 2 || c->display_usage) {
931 net_conf_delparm_usage(c, argc, argv);
932 goto done;
935 * NULL service name means "dangling parameters" to libsmbconf.
936 * We use the empty string from the command line for this purpose.
938 if (strlen(argv[0]) != 0) {
939 service = talloc_strdup(mem_ctx, argv[0]);
940 if (service == NULL) {
941 d_printf(_("error: out of memory!\n"));
942 goto done;
945 param = strlower_talloc(mem_ctx, argv[1]);
946 if (param == NULL) {
947 d_printf("error: out of memory!\n");
948 goto done;
951 err = smbconf_delete_parameter(conf_ctx, service, param);
952 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
953 d_fprintf(stderr,
954 _("Error: given service '%s' does not exist.\n"),
955 service);
956 goto done;
957 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
958 d_fprintf(stderr,
959 _("Error: given parameter '%s' is not set.\n"),
960 param);
961 goto done;
962 } else if (!SBC_ERROR_IS_OK(err)) {
963 d_fprintf(stderr, _("Error deleting value '%s': %s.\n"),
964 param, sbcErrorString(err));
965 goto done;
968 ret = 0;
970 done:
971 TALLOC_FREE(mem_ctx);
972 return ret;
975 static int net_conf_getincludes(struct net_context *c,
976 struct smbconf_ctx *conf_ctx,
977 int argc, const char **argv)
979 sbcErr err;
980 uint32_t num_includes;
981 uint32_t count;
982 char *service;
983 char **includes = NULL;
984 int ret = -1;
985 TALLOC_CTX *mem_ctx = talloc_stackframe();
987 if (argc != 1 || c->display_usage) {
988 net_conf_getincludes_usage(c, argc, argv);
989 goto done;
992 service = talloc_strdup(mem_ctx, argv[0]);
993 if (service == NULL) {
994 d_printf(_("error: out of memory!\n"));
995 goto done;
998 err = smbconf_get_includes(conf_ctx, mem_ctx, service,
999 &num_includes, &includes);
1000 if (!SBC_ERROR_IS_OK(err)) {
1001 d_printf(_("error getting includes: %s\n"), sbcErrorString(err));
1002 goto done;
1005 for (count = 0; count < num_includes; count++) {
1006 d_printf("include = %s\n", includes[count]);
1009 ret = 0;
1011 done:
1012 TALLOC_FREE(mem_ctx);
1013 return ret;
1016 static int net_conf_setincludes(struct net_context *c,
1017 struct smbconf_ctx *conf_ctx,
1018 int argc, const char **argv)
1020 sbcErr err;
1021 char *service;
1022 uint32_t num_includes;
1023 const char **includes;
1024 int ret = -1;
1025 TALLOC_CTX *mem_ctx = talloc_stackframe();
1027 if (argc < 1 || c->display_usage) {
1028 net_conf_setincludes_usage(c, argc, argv);
1029 goto done;
1032 service = talloc_strdup(mem_ctx, argv[0]);
1033 if (service == NULL) {
1034 d_printf(_("error: out of memory!\n"));
1035 goto done;
1038 num_includes = argc - 1;
1039 if (num_includes == 0) {
1040 includes = NULL;
1041 } else {
1042 includes = argv + 1;
1045 err = smbconf_set_includes(conf_ctx, service, num_includes, includes);
1046 if (!SBC_ERROR_IS_OK(err)) {
1047 d_printf(_("error setting includes: %s\n"), sbcErrorString(err));
1048 goto done;
1051 ret = 0;
1053 done:
1054 TALLOC_FREE(mem_ctx);
1055 return ret;
1058 static int net_conf_delincludes(struct net_context *c,
1059 struct smbconf_ctx *conf_ctx,
1060 int argc, const char **argv)
1062 sbcErr err;
1063 char *service;
1064 int ret = -1;
1065 TALLOC_CTX *mem_ctx = talloc_stackframe();
1067 if (argc != 1 || c->display_usage) {
1068 net_conf_delincludes_usage(c, argc, argv);
1069 goto done;
1072 service = talloc_strdup(mem_ctx, argv[0]);
1073 if (service == NULL) {
1074 d_printf(_("error: out of memory!\n"));
1075 goto done;
1078 err = smbconf_delete_includes(conf_ctx, service);
1079 if (!SBC_ERROR_IS_OK(err)) {
1080 d_printf(_("error deleting includes: %s\n"), sbcErrorString(err));
1081 goto done;
1084 ret = 0;
1086 done:
1087 TALLOC_FREE(mem_ctx);
1088 return ret;
1092 /**********************************************************************
1094 * Wrapper and net_conf_run_function mechanism.
1096 **********************************************************************/
1099 * Wrapper function to call the main conf functions.
1100 * The wrapper calls handles opening and closing of the
1101 * configuration.
1103 static int net_conf_wrap_function(struct net_context *c,
1104 int (*fn)(struct net_context *,
1105 struct smbconf_ctx *,
1106 int, const char **),
1107 int argc, const char **argv)
1109 sbcErr err;
1110 TALLOC_CTX *mem_ctx = talloc_stackframe();
1111 struct smbconf_ctx *conf_ctx;
1112 int ret = -1;
1114 err = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1115 if (!SBC_ERROR_IS_OK(err)) {
1116 talloc_free(mem_ctx);
1117 return -1;
1120 ret = fn(c, conf_ctx, argc, argv);
1122 smbconf_shutdown(conf_ctx);
1124 talloc_free(mem_ctx);
1125 return ret;
1129 * We need a functable struct of our own, because the
1130 * functions are called through a wrapper that handles
1131 * the opening and closing of the configuration, and so on.
1133 struct conf_functable {
1134 const char *funcname;
1135 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1136 const char **argv);
1137 int valid_transports;
1138 const char *description;
1139 const char *usage;
1143 * This imitates net_run_function but calls the main functions
1144 * through the wrapper net_conf_wrap_function().
1146 static int net_conf_run_function(struct net_context *c, int argc,
1147 const char **argv, const char *whoami,
1148 struct conf_functable *table)
1150 int i;
1152 if (argc != 0) {
1153 for (i=0; table[i].funcname; i++) {
1154 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
1155 return net_conf_wrap_function(c, table[i].fn,
1156 argc-1,
1157 argv+1);
1161 d_printf(_("Usage:\n"));
1162 for (i=0; table[i].funcname; i++) {
1163 if (c->display_usage == false)
1164 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1165 table[i].description);
1166 else
1167 d_printf("%s\n", table[i].usage);
1170 return c->display_usage?0:-1;
1174 * Entry-point for all the CONF functions.
1177 int net_conf(struct net_context *c, int argc, const char **argv)
1179 int ret = -1;
1180 struct conf_functable func_table[] = {
1182 "list",
1183 net_conf_list,
1184 NET_TRANSPORT_LOCAL,
1185 N_("Dump the complete configuration in smb.conf like "
1186 "format."),
1187 N_("net conf list\n"
1188 " Dump the complete configuration in smb.conf "
1189 "like format.")
1193 "import",
1194 net_conf_import,
1195 NET_TRANSPORT_LOCAL,
1196 N_("Import configuration from file in smb.conf "
1197 "format."),
1198 N_("net conf import\n"
1199 " Import configuration from file in smb.conf "
1200 "format.")
1203 "listshares",
1204 net_conf_listshares,
1205 NET_TRANSPORT_LOCAL,
1206 N_("List the share names."),
1207 N_("net conf listshares\n"
1208 " List the share names.")
1211 "drop",
1212 net_conf_drop,
1213 NET_TRANSPORT_LOCAL,
1214 N_("Delete the complete configuration."),
1215 N_("net conf drop\n"
1216 " Delete the complete configuration.")
1219 "showshare",
1220 net_conf_showshare,
1221 NET_TRANSPORT_LOCAL,
1222 N_("Show the definition of a share."),
1223 N_("net conf showshare\n"
1224 " Show the definition of a share.")
1227 "addshare",
1228 net_conf_addshare,
1229 NET_TRANSPORT_LOCAL,
1230 N_("Create a new share."),
1231 N_("net conf addshare\n"
1232 " Create a new share.")
1235 "delshare",
1236 net_conf_delshare,
1237 NET_TRANSPORT_LOCAL,
1238 N_("Delete a share."),
1239 N_("net conf delshare\n"
1240 " Delete a share.")
1243 "setparm",
1244 net_conf_setparm,
1245 NET_TRANSPORT_LOCAL,
1246 N_("Store a parameter."),
1247 N_("net conf setparm\n"
1248 " Store a parameter.")
1251 "getparm",
1252 net_conf_getparm,
1253 NET_TRANSPORT_LOCAL,
1254 N_("Retrieve the value of a parameter."),
1255 N_("net conf getparm\n"
1256 " Retrieve the value of a parameter.")
1259 "delparm",
1260 net_conf_delparm,
1261 NET_TRANSPORT_LOCAL,
1262 N_("Delete a parameter."),
1263 N_("net conf delparm\n"
1264 " Delete a parameter.")
1267 "getincludes",
1268 net_conf_getincludes,
1269 NET_TRANSPORT_LOCAL,
1270 N_("Show the includes of a share definition."),
1271 N_("net conf getincludes\n"
1272 " Show the includes of a share definition.")
1275 "setincludes",
1276 net_conf_setincludes,
1277 NET_TRANSPORT_LOCAL,
1278 N_("Set includes for a share."),
1279 N_("net conf setincludes\n"
1280 " Set includes for a share.")
1283 "delincludes",
1284 net_conf_delincludes,
1285 NET_TRANSPORT_LOCAL,
1286 N_("Delete includes from a share definition."),
1287 N_("net conf delincludes\n"
1288 " Delete includes from a share definition.")
1290 {NULL, NULL, 0, NULL, NULL}
1293 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1295 return ret;