smbXsrv_session: Remove a "can't happen" NULL check
[Samba.git] / source3 / utils / net_conf.c
blobc16f240634c4e80f47df525fc120300c96ef70af
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 bool ok = false;
653 if (strequal(sharename, HOMES_NAME) && path[0] == '\0') {
654 /* The homes share can be an empty path. */
655 ok = true;
657 if (!ok) {
658 d_fprintf(stderr,
659 _("Error: path '%s' is not an absolute path.\n"),
660 path);
661 goto done;
666 * start a transaction
669 err = smbconf_transaction_start(conf_ctx);
670 if (!SBC_ERROR_IS_OK(err)) {
671 d_printf("error starting transaction: %s\n",
672 sbcErrorString(err));
673 goto done;
677 * create the share
680 err = smbconf_create_share(conf_ctx, sharename);
681 if (!SBC_ERROR_IS_OK(err)) {
682 d_fprintf(stderr, _("Error creating share %s: %s\n"),
683 sharename, sbcErrorString(err));
684 goto cancel;
688 * fill the share with parameters
691 err = smbconf_set_parameter(conf_ctx, sharename, "path", path);
692 if (!SBC_ERROR_IS_OK(err)) {
693 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
694 "path", sbcErrorString(err));
695 goto cancel;
698 if (comment != NULL) {
699 err = smbconf_set_parameter(conf_ctx, sharename, "comment",
700 comment);
701 if (!SBC_ERROR_IS_OK(err)) {
702 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
703 "comment", sbcErrorString(err));
704 goto cancel;
708 err = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
709 if (!SBC_ERROR_IS_OK(err)) {
710 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
711 "'guest ok'", sbcErrorString(err));
712 goto cancel;
715 err = smbconf_set_parameter(conf_ctx, sharename, "writeable",
716 writeable);
717 if (!SBC_ERROR_IS_OK(err)) {
718 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
719 "writeable", sbcErrorString(err));
720 goto cancel;
724 * commit the whole thing
727 err = smbconf_transaction_commit(conf_ctx);
728 if (!SBC_ERROR_IS_OK(err)) {
729 d_printf("error committing transaction: %s\n",
730 sbcErrorString(err));
731 } else {
732 ret = 0;
735 goto done;
737 cancel:
738 err = smbconf_transaction_cancel(conf_ctx);
739 if (!SBC_ERROR_IS_OK(err)) {
740 d_printf("error cancelling transaction: %s\n",
741 sbcErrorString(err));
744 done:
745 TALLOC_FREE(mem_ctx);
746 return ret;
749 static int net_conf_delshare(struct net_context *c,
750 struct smbconf_ctx *conf_ctx, int argc,
751 const char **argv)
753 int ret = -1;
754 const char *sharename = NULL;
755 sbcErr err;
756 NTSTATUS status;
757 TALLOC_CTX *mem_ctx = talloc_stackframe();
759 if (argc != 1 || c->display_usage) {
760 net_conf_delshare_usage(c, argc, argv);
761 goto done;
763 sharename = talloc_strdup(mem_ctx, argv[0]);
764 if (sharename == NULL) {
765 d_printf(_("error: out of memory!\n"));
766 goto done;
769 err = smbconf_delete_share(conf_ctx, sharename);
770 if (!SBC_ERROR_IS_OK(err)) {
771 d_fprintf(stderr, _("Error deleting share %s: %s\n"),
772 sharename, sbcErrorString(err));
773 goto done;
776 status = delete_share_security(sharename);
777 if (!NT_STATUS_IS_OK(status) &&
778 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
779 d_fprintf(stderr, _("deleting share acl failed for %s: %s\n"),
780 sharename, nt_errstr(status));
781 goto done;
784 ret = 0;
785 done:
786 TALLOC_FREE(mem_ctx);
787 return ret;
790 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
791 int argc, const char **argv)
793 int ret = -1;
794 sbcErr err;
795 char *service = NULL;
796 char *param = NULL;
797 const char *value_str = NULL;
798 TALLOC_CTX *mem_ctx = talloc_stackframe();
800 if (argc != 3 || c->display_usage) {
801 net_conf_setparm_usage(c, argc, argv);
802 goto done;
805 * NULL service name means "dangling parameters" to libsmbconf.
806 * We use the empty string from the command line for this purpose.
808 if (strlen(argv[0]) != 0) {
809 service = talloc_strdup(mem_ctx, argv[0]);
810 if (service == NULL) {
811 d_printf(_("error: out of memory!\n"));
812 goto done;
815 param = strlower_talloc(mem_ctx, argv[1]);
816 if (param == NULL) {
817 d_printf(_("error: out of memory!\n"));
818 goto done;
820 value_str = argv[2];
822 if (!net_conf_param_valid(service,param, value_str)) {
823 goto done;
826 err = smbconf_transaction_start(conf_ctx);
827 if (!SBC_ERROR_IS_OK(err)) {
828 d_printf(_("error starting transaction: %s\n"),
829 sbcErrorString(err));
830 goto done;
833 if (!smbconf_share_exists(conf_ctx, service)) {
834 err = smbconf_create_share(conf_ctx, service);
835 if (!SBC_ERROR_IS_OK(err)) {
836 d_fprintf(stderr, _("Error creating share '%s': %s\n"),
837 service, sbcErrorString(err));
838 goto cancel;
842 err = smbconf_set_parameter(conf_ctx, service, param, value_str);
843 if (!SBC_ERROR_IS_OK(err)) {
844 d_fprintf(stderr, _("Error setting value '%s': %s\n"),
845 param, sbcErrorString(err));
846 goto cancel;
849 err = smbconf_transaction_commit(conf_ctx);
850 if (!SBC_ERROR_IS_OK(err)) {
851 d_printf(_("error committing transaction: %s\n"),
852 sbcErrorString(err));
853 } else {
854 ret = 0;
857 goto done;
859 cancel:
860 err = smbconf_transaction_cancel(conf_ctx);
861 if (!SBC_ERROR_IS_OK(err)) {
862 d_printf(_("error cancelling transaction: %s\n"),
863 sbcErrorString(err));
866 done:
867 TALLOC_FREE(mem_ctx);
868 return ret;
871 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
872 int argc, const char **argv)
874 int ret = -1;
875 sbcErr err;
876 char *service = NULL;
877 char *param = NULL;
878 char *valstr = NULL;
879 TALLOC_CTX *mem_ctx;
881 mem_ctx = talloc_stackframe();
883 if (argc != 2 || c->display_usage) {
884 net_conf_getparm_usage(c, argc, argv);
885 goto done;
888 * NULL service name means "dangling parameters" to libsmbconf.
889 * We use the empty string from the command line for this purpose.
891 if (strlen(argv[0]) != 0) {
892 service = talloc_strdup(mem_ctx, argv[0]);
893 if (service == NULL) {
894 d_printf(_("error: out of memory!\n"));
895 goto done;
898 param = strlower_talloc(mem_ctx, argv[1]);
899 if (param == NULL) {
900 d_printf(_("error: out of memory!\n"));
901 goto done;
904 err = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
905 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
906 d_fprintf(stderr,
907 _("Error: given service '%s' does not exist.\n"),
908 service);
909 goto done;
910 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
911 d_fprintf(stderr,
912 _("Error: given parameter '%s' is not set.\n"),
913 param);
914 goto done;
915 } else if (!SBC_ERROR_IS_OK(err)) {
916 d_fprintf(stderr, _("Error getting value '%s': %s.\n"),
917 param, sbcErrorString(err));
918 goto done;
921 d_printf("%s\n", valstr);
923 ret = 0;
924 done:
925 TALLOC_FREE(mem_ctx);
926 return ret;
929 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
930 int argc, const char **argv)
932 int ret = -1;
933 sbcErr err;
934 char *service = NULL;
935 char *param = NULL;
936 TALLOC_CTX *mem_ctx = talloc_stackframe();
938 if (argc != 2 || c->display_usage) {
939 net_conf_delparm_usage(c, argc, argv);
940 goto done;
943 * NULL service name means "dangling parameters" to libsmbconf.
944 * We use the empty string from the command line for this purpose.
946 if (strlen(argv[0]) != 0) {
947 service = talloc_strdup(mem_ctx, argv[0]);
948 if (service == NULL) {
949 d_printf(_("error: out of memory!\n"));
950 goto done;
953 param = strlower_talloc(mem_ctx, argv[1]);
954 if (param == NULL) {
955 d_printf("error: out of memory!\n");
956 goto done;
959 err = smbconf_delete_parameter(conf_ctx, service, param);
960 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
961 d_fprintf(stderr,
962 _("Error: given service '%s' does not exist.\n"),
963 service);
964 goto done;
965 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
966 d_fprintf(stderr,
967 _("Error: given parameter '%s' is not set.\n"),
968 param);
969 goto done;
970 } else if (!SBC_ERROR_IS_OK(err)) {
971 d_fprintf(stderr, _("Error deleting value '%s': %s.\n"),
972 param, sbcErrorString(err));
973 goto done;
976 ret = 0;
978 done:
979 TALLOC_FREE(mem_ctx);
980 return ret;
983 static int net_conf_getincludes(struct net_context *c,
984 struct smbconf_ctx *conf_ctx,
985 int argc, const char **argv)
987 sbcErr err;
988 uint32_t num_includes;
989 uint32_t count;
990 char *service;
991 char **includes = NULL;
992 int ret = -1;
993 TALLOC_CTX *mem_ctx = talloc_stackframe();
995 if (argc != 1 || c->display_usage) {
996 net_conf_getincludes_usage(c, argc, argv);
997 goto done;
1000 service = talloc_strdup(mem_ctx, argv[0]);
1001 if (service == NULL) {
1002 d_printf(_("error: out of memory!\n"));
1003 goto done;
1006 err = smbconf_get_includes(conf_ctx, mem_ctx, service,
1007 &num_includes, &includes);
1008 if (!SBC_ERROR_IS_OK(err)) {
1009 d_printf(_("error getting includes: %s\n"), sbcErrorString(err));
1010 goto done;
1013 for (count = 0; count < num_includes; count++) {
1014 d_printf("include = %s\n", includes[count]);
1017 ret = 0;
1019 done:
1020 TALLOC_FREE(mem_ctx);
1021 return ret;
1024 static int net_conf_setincludes(struct net_context *c,
1025 struct smbconf_ctx *conf_ctx,
1026 int argc, const char **argv)
1028 sbcErr err;
1029 char *service;
1030 uint32_t num_includes;
1031 const char **includes;
1032 int ret = -1;
1033 TALLOC_CTX *mem_ctx = talloc_stackframe();
1035 if (argc < 1 || c->display_usage) {
1036 net_conf_setincludes_usage(c, argc, argv);
1037 goto done;
1040 service = talloc_strdup(mem_ctx, argv[0]);
1041 if (service == NULL) {
1042 d_printf(_("error: out of memory!\n"));
1043 goto done;
1046 num_includes = argc - 1;
1047 if (num_includes == 0) {
1048 includes = NULL;
1049 } else {
1050 includes = argv + 1;
1053 err = smbconf_set_includes(conf_ctx, service, num_includes, includes);
1054 if (!SBC_ERROR_IS_OK(err)) {
1055 d_printf(_("error setting includes: %s\n"), sbcErrorString(err));
1056 goto done;
1059 ret = 0;
1061 done:
1062 TALLOC_FREE(mem_ctx);
1063 return ret;
1066 static int net_conf_delincludes(struct net_context *c,
1067 struct smbconf_ctx *conf_ctx,
1068 int argc, const char **argv)
1070 sbcErr err;
1071 char *service;
1072 int ret = -1;
1073 TALLOC_CTX *mem_ctx = talloc_stackframe();
1075 if (argc != 1 || c->display_usage) {
1076 net_conf_delincludes_usage(c, argc, argv);
1077 goto done;
1080 service = talloc_strdup(mem_ctx, argv[0]);
1081 if (service == NULL) {
1082 d_printf(_("error: out of memory!\n"));
1083 goto done;
1086 err = smbconf_delete_includes(conf_ctx, service);
1087 if (!SBC_ERROR_IS_OK(err)) {
1088 d_printf(_("error deleting includes: %s\n"), sbcErrorString(err));
1089 goto done;
1092 ret = 0;
1094 done:
1095 TALLOC_FREE(mem_ctx);
1096 return ret;
1100 /**********************************************************************
1102 * Wrapper and net_conf_run_function mechanism.
1104 **********************************************************************/
1107 * Wrapper function to call the main conf functions.
1108 * The wrapper calls handles opening and closing of the
1109 * configuration.
1111 static int net_conf_wrap_function(struct net_context *c,
1112 int (*fn)(struct net_context *,
1113 struct smbconf_ctx *,
1114 int, const char **),
1115 int argc, const char **argv)
1117 sbcErr err;
1118 TALLOC_CTX *mem_ctx = talloc_stackframe();
1119 struct smbconf_ctx *conf_ctx;
1120 int ret = -1;
1122 err = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1123 if (!SBC_ERROR_IS_OK(err)) {
1124 talloc_free(mem_ctx);
1125 return -1;
1128 ret = fn(c, conf_ctx, argc, argv);
1130 smbconf_shutdown(conf_ctx);
1132 talloc_free(mem_ctx);
1133 return ret;
1137 * We need a functable struct of our own, because the
1138 * functions are called through a wrapper that handles
1139 * the opening and closing of the configuration, and so on.
1141 struct conf_functable {
1142 const char *funcname;
1143 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1144 const char **argv);
1145 int valid_transports;
1146 const char *description;
1147 const char *usage;
1151 * This imitates net_run_function but calls the main functions
1152 * through the wrapper net_conf_wrap_function().
1154 static int net_conf_run_function(struct net_context *c, int argc,
1155 const char **argv, const char *whoami,
1156 struct conf_functable *table)
1158 int i;
1160 if (argc != 0) {
1161 for (i=0; table[i].funcname; i++) {
1162 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
1163 return net_conf_wrap_function(c, table[i].fn,
1164 argc-1,
1165 argv+1);
1169 d_printf(_("Usage:\n"));
1170 for (i=0; table[i].funcname; i++) {
1171 if (c->display_usage == false)
1172 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1173 table[i].description);
1174 else
1175 d_printf("%s\n", table[i].usage);
1178 return c->display_usage?0:-1;
1182 * Entry-point for all the CONF functions.
1185 int net_conf(struct net_context *c, int argc, const char **argv)
1187 int ret = -1;
1188 struct conf_functable func_table[] = {
1190 "list",
1191 net_conf_list,
1192 NET_TRANSPORT_LOCAL,
1193 N_("Dump the complete configuration in smb.conf like "
1194 "format."),
1195 N_("net conf list\n"
1196 " Dump the complete configuration in smb.conf "
1197 "like format.")
1201 "import",
1202 net_conf_import,
1203 NET_TRANSPORT_LOCAL,
1204 N_("Import configuration from file in smb.conf "
1205 "format."),
1206 N_("net conf import\n"
1207 " Import configuration from file in smb.conf "
1208 "format.")
1211 "listshares",
1212 net_conf_listshares,
1213 NET_TRANSPORT_LOCAL,
1214 N_("List the share names."),
1215 N_("net conf listshares\n"
1216 " List the share names.")
1219 "drop",
1220 net_conf_drop,
1221 NET_TRANSPORT_LOCAL,
1222 N_("Delete the complete configuration."),
1223 N_("net conf drop\n"
1224 " Delete the complete configuration.")
1227 "showshare",
1228 net_conf_showshare,
1229 NET_TRANSPORT_LOCAL,
1230 N_("Show the definition of a share."),
1231 N_("net conf showshare\n"
1232 " Show the definition of a share.")
1235 "addshare",
1236 net_conf_addshare,
1237 NET_TRANSPORT_LOCAL,
1238 N_("Create a new share."),
1239 N_("net conf addshare\n"
1240 " Create a new share.")
1243 "delshare",
1244 net_conf_delshare,
1245 NET_TRANSPORT_LOCAL,
1246 N_("Delete a share."),
1247 N_("net conf delshare\n"
1248 " Delete a share.")
1251 "setparm",
1252 net_conf_setparm,
1253 NET_TRANSPORT_LOCAL,
1254 N_("Store a parameter."),
1255 N_("net conf setparm\n"
1256 " Store a parameter.")
1259 "getparm",
1260 net_conf_getparm,
1261 NET_TRANSPORT_LOCAL,
1262 N_("Retrieve the value of a parameter."),
1263 N_("net conf getparm\n"
1264 " Retrieve the value of a parameter.")
1267 "delparm",
1268 net_conf_delparm,
1269 NET_TRANSPORT_LOCAL,
1270 N_("Delete a parameter."),
1271 N_("net conf delparm\n"
1272 " Delete a parameter.")
1275 "getincludes",
1276 net_conf_getincludes,
1277 NET_TRANSPORT_LOCAL,
1278 N_("Show the includes of a share definition."),
1279 N_("net conf getincludes\n"
1280 " Show the includes of a share definition.")
1283 "setincludes",
1284 net_conf_setincludes,
1285 NET_TRANSPORT_LOCAL,
1286 N_("Set includes for a share."),
1287 N_("net conf setincludes\n"
1288 " Set includes for a share.")
1291 "delincludes",
1292 net_conf_delincludes,
1293 NET_TRANSPORT_LOCAL,
1294 N_("Delete includes from a share definition."),
1295 N_("net conf delincludes\n"
1296 " Delete includes from a share definition.")
1298 {NULL, NULL, 0, NULL, NULL}
1301 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1303 return ret;