check_parent_exists() can change errno. Ensure we preserve it across calls.
[Samba.git] / source3 / utils / net_conf.c
blob04164045d0f2084d8228c6a520300598fbfc6013
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"
37 /**********************************************************************
39 * usage functions
41 **********************************************************************/
43 static int net_conf_list_usage(struct net_context *c, int argc,
44 const char **argv)
46 d_printf("%s net conf list\n", _("Usage:"));
47 return -1;
50 static int net_conf_import_usage(struct net_context *c, int argc,
51 const char**argv)
53 d_printf("%s\n%s",
54 _("Usage:"),
55 _(" net conf import [--test|-T] <filename> "
56 "[<servicename>]\n"
57 "\t[--test|-T] testmode - do not act, just print "
58 "what would be done\n"
59 "\t<servicename> only import service <servicename>, "
60 "ignore the rest\n"));
61 return -1;
64 static int net_conf_listshares_usage(struct net_context *c, int argc,
65 const char **argv)
67 d_printf("%s\nnet conf listshares\n", _("Usage:"));
68 return -1;
71 static int net_conf_drop_usage(struct net_context *c, int argc,
72 const char **argv)
74 d_printf("%s\nnet conf drop\n", _("Usage:"));
75 return -1;
78 static int net_conf_showshare_usage(struct net_context *c, int argc,
79 const char **argv)
81 d_printf("%s\n%s",
82 _("Usage:"),
83 _("net conf showshare <sharename>\n"));
84 return -1;
87 static int net_conf_addshare_usage(struct net_context *c, int argc,
88 const char **argv)
90 d_printf("%s\n%s",
91 _("Usage:"),
92 _(" net conf addshare <sharename> <path> "
93 "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
94 "\t<sharename> the new share name.\n"
95 "\t<path> the path on the filesystem to export.\n"
96 "\twriteable={y|N} set \"writeable to \"yes\" or "
97 "\"no\" (default) on this share.\n"
98 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
99 "\"no\" (default) on this share.\n"
100 "\t<comment> optional comment for the new share.\n"));
101 return -1;
104 static int net_conf_delshare_usage(struct net_context *c, int argc,
105 const char **argv)
107 d_printf("%s\n%s",
108 _("Usage:"),
109 _("net conf delshare <sharename>\n"));
110 return -1;
113 static int net_conf_setparm_usage(struct net_context *c, int argc,
114 const char **argv)
116 d_printf("%s\n%s",
117 _("Usage:"),
118 _(" net conf setparm <section> <param> <value>\n"));
119 return -1;
122 static int net_conf_getparm_usage(struct net_context *c, int argc,
123 const char **argv)
125 d_printf("%s\n%s",
126 _("Usage:"),
127 _(" net conf getparm <section> <param>\n"));
128 return -1;
131 static int net_conf_delparm_usage(struct net_context *c, int argc,
132 const char **argv)
134 d_printf("%s\n%s",
135 _("Usage:"),
136 _(" net conf delparm <section> <param>\n"));
137 return -1;
140 static int net_conf_getincludes_usage(struct net_context *c, int argc,
141 const char **argv)
143 d_printf("%s\n%s",
144 _("Usage:"),
145 _(" net conf getincludes <section>\n"));
146 return -1;
149 static int net_conf_setincludes_usage(struct net_context *c, int argc,
150 const char **argv)
152 d_printf("%s\n%s",
153 _("Usage:"),
154 _(" net conf setincludes <section> [<filename>]*\n"));
155 return -1;
158 static int net_conf_delincludes_usage(struct net_context *c, int argc,
159 const char **argv)
161 d_printf("%s\n%s",
162 _("Usage:"),
163 _(" net conf delincludes <section>\n"));
164 return -1;
168 /**********************************************************************
170 * Helper functions
172 **********************************************************************/
175 * This functions process a service previously loaded with libsmbconf.
177 static sbcErr import_process_service(struct net_context *c,
178 struct smbconf_ctx *conf_ctx,
179 struct smbconf_service *service)
181 uint32_t idx;
182 sbcErr err = SBC_ERR_OK;
183 uint32_t num_includes = 0;
184 char **includes = NULL;
185 TALLOC_CTX *mem_ctx = talloc_stackframe();
187 if (c->opt_testmode) {
188 const char *indent = "";
189 if (service->name != NULL) {
190 d_printf("[%s]\n", service->name);
191 indent = "\t";
193 for (idx = 0; idx < service->num_params; idx++) {
194 d_printf("%s%s = %s\n", indent,
195 service->param_names[idx],
196 service->param_values[idx]);
198 d_printf("\n");
199 goto done;
202 if (smbconf_share_exists(conf_ctx, service->name)) {
203 err = smbconf_delete_share(conf_ctx, service->name);
204 if (!SBC_ERROR_IS_OK(err)) {
205 goto done;
208 err = smbconf_create_share(conf_ctx, service->name);
209 if (!SBC_ERROR_IS_OK(err)) {
210 goto done;
213 for (idx = 0; idx < service->num_params; idx ++) {
214 if (strequal(service->param_names[idx], "include")) {
215 includes = TALLOC_REALLOC_ARRAY(mem_ctx,
216 includes,
217 char *,
218 num_includes+1);
219 if (includes == NULL) {
220 err = SBC_ERR_NOMEM;
221 goto done;
223 includes[num_includes] = talloc_strdup(includes,
224 service->param_values[idx]);
225 if (includes[num_includes] == NULL) {
226 err = SBC_ERR_NOMEM;
227 goto done;
229 num_includes++;
230 } else {
231 err = smbconf_set_parameter(conf_ctx,
232 service->name,
233 service->param_names[idx],
234 service->param_values[idx]);
235 if (!SBC_ERROR_IS_OK(err)) {
236 d_fprintf(stderr,
237 _("Error in section [%s], parameter \"%s\": %s\n"),
238 service->name, service->param_names[idx],
239 sbcErrorString(err));
240 goto done;
245 err = smbconf_set_includes(conf_ctx, service->name, num_includes,
246 (const char **)includes);
247 if (!SBC_ERROR_IS_OK(err)) {
248 goto done;
251 err = SBC_ERR_OK;
252 done:
253 TALLOC_FREE(mem_ctx);
254 return err;
258 /**********************************************************************
260 * the main conf functions
262 **********************************************************************/
264 static int net_conf_list(struct net_context *c, struct smbconf_ctx *conf_ctx,
265 int argc, const char **argv)
267 sbcErr err;
268 int ret = -1;
269 TALLOC_CTX *mem_ctx;
270 uint32_t num_shares;
271 uint32_t share_count, param_count;
272 struct smbconf_service **shares = NULL;
274 mem_ctx = talloc_stackframe();
276 if (argc != 0 || c->display_usage) {
277 net_conf_list_usage(c, argc, argv);
278 goto done;
281 err = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &shares);
282 if (!SBC_ERROR_IS_OK(err)) {
283 d_fprintf(stderr, _("Error getting config: %s\n"),
284 sbcErrorString(err));
285 goto done;
288 for (share_count = 0; share_count < num_shares; share_count++) {
289 const char *indent = "";
290 if (shares[share_count]->name != NULL) {
291 d_printf("[%s]\n", shares[share_count]->name);
292 indent = "\t";
294 for (param_count = 0;
295 param_count < shares[share_count]->num_params;
296 param_count++)
298 d_printf("%s%s = %s\n",
299 indent,
300 shares[share_count]->param_names[param_count],
301 shares[share_count]->param_values[param_count]);
303 d_printf("\n");
306 ret = 0;
308 done:
309 TALLOC_FREE(mem_ctx);
310 return ret;
313 static int net_conf_import(struct net_context *c, struct smbconf_ctx *conf_ctx,
314 int argc, const char **argv)
316 int ret = -1;
317 const char *filename = NULL;
318 const char *servicename = NULL;
319 char *conf_source = NULL;
320 TALLOC_CTX *mem_ctx;
321 struct smbconf_ctx *txt_ctx;
322 sbcErr err;
324 if (c->display_usage)
325 return net_conf_import_usage(c, argc, argv);
327 mem_ctx = talloc_stackframe();
329 switch (argc) {
330 case 0:
331 default:
332 net_conf_import_usage(c, argc, argv);
333 goto done;
334 case 2:
335 servicename = talloc_strdup(mem_ctx, argv[1]);
336 if (servicename == NULL) {
337 d_printf(_("error: out of memory!\n"));
338 goto done;
340 case 1:
341 filename = argv[0];
342 break;
345 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
346 filename));
348 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
349 if (conf_source == NULL) {
350 d_printf(_("error: out of memory!\n"));
351 goto done;
354 err = smbconf_init(mem_ctx, &txt_ctx, conf_source);
355 if (!SBC_ERROR_IS_OK(err)) {
356 d_printf(_("error loading file '%s': %s\n"), filename,
357 sbcErrorString(err));
358 goto done;
361 if (c->opt_testmode) {
362 d_printf(_("\nTEST MODE - "
363 "would import the following configuration:\n\n"));
366 if (servicename != NULL) {
367 struct smbconf_service *service = NULL;
369 err = smbconf_get_share(txt_ctx, mem_ctx,
370 servicename,
371 &service);
372 if (!SBC_ERROR_IS_OK(err)) {
373 goto cancel;
376 err = smbconf_transaction_start(conf_ctx);
377 if (!SBC_ERROR_IS_OK(err)) {
378 d_printf(_("error starting transaction: %s\n"),
379 sbcErrorString(err));
380 goto done;
383 err = import_process_service(c, conf_ctx, service);
384 if (!SBC_ERROR_IS_OK(err)) {
385 goto cancel;
387 } else {
388 struct smbconf_service **services = NULL;
389 uint32_t num_shares, sidx;
391 err = smbconf_get_config(txt_ctx, mem_ctx,
392 &num_shares,
393 &services);
394 if (!SBC_ERROR_IS_OK(err)) {
395 goto cancel;
397 if (!c->opt_testmode) {
398 if (!SBC_ERROR_IS_OK(smbconf_drop(conf_ctx))) {
399 goto cancel;
404 * Wrap the importing of shares into a transaction,
405 * but only 100 at a time, in order to save memory.
406 * The allocated memory accumulates across the actions
407 * within the transaction, and for me, some 1500
408 * imported shares, the MAX_TALLOC_SIZE of 256 MB
409 * was exceeded.
411 err = smbconf_transaction_start(conf_ctx);
412 if (!SBC_ERROR_IS_OK(err)) {
413 d_printf(_("error starting transaction: %s\n"),
414 sbcErrorString(err));
415 goto done;
418 for (sidx = 0; sidx < num_shares; sidx++) {
419 err = import_process_service(c, conf_ctx,
420 services[sidx]);
421 if (!SBC_ERROR_IS_OK(err)) {
422 goto cancel;
425 if (sidx % 100) {
426 continue;
429 err = smbconf_transaction_commit(conf_ctx);
430 if (!SBC_ERROR_IS_OK(err)) {
431 d_printf(_("error committing transaction: "
432 "%s\n"),
433 sbcErrorString(err));
434 goto done;
436 err = smbconf_transaction_start(conf_ctx);
437 if (!SBC_ERROR_IS_OK(err)) {
438 d_printf(_("error starting transaction: %s\n"),
439 sbcErrorString(err));
440 goto done;
445 err = smbconf_transaction_commit(conf_ctx);
446 if (!SBC_ERROR_IS_OK(err)) {
447 d_printf(_("error committing transaction: %s\n"),
448 sbcErrorString(err));
449 } else {
450 ret = 0;
453 goto done;
455 cancel:
456 err = smbconf_transaction_cancel(conf_ctx);
457 if (!SBC_ERROR_IS_OK(err)) {
458 d_printf(_("error cancelling transaction: %s\n"),
459 sbcErrorString(err));
462 done:
463 TALLOC_FREE(mem_ctx);
464 return ret;
467 static int net_conf_listshares(struct net_context *c,
468 struct smbconf_ctx *conf_ctx, int argc,
469 const char **argv)
471 sbcErr err;
472 int ret = -1;
473 uint32_t count, num_shares = 0;
474 char **share_names = NULL;
475 TALLOC_CTX *mem_ctx;
477 mem_ctx = talloc_stackframe();
479 if (argc != 0 || c->display_usage) {
480 net_conf_listshares_usage(c, argc, argv);
481 goto done;
484 err = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
485 &share_names);
486 if (!SBC_ERROR_IS_OK(err)) {
487 goto done;
490 for (count = 0; count < num_shares; count++)
492 d_printf("%s\n", share_names[count]);
495 ret = 0;
497 done:
498 TALLOC_FREE(mem_ctx);
499 return ret;
502 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
503 int argc, const char **argv)
505 int ret = -1;
506 sbcErr err;
508 if (argc != 0 || c->display_usage) {
509 net_conf_drop_usage(c, argc, argv);
510 goto done;
513 err = smbconf_drop(conf_ctx);
514 if (!SBC_ERROR_IS_OK(err)) {
515 d_fprintf(stderr, _("Error deleting configuration: %s\n"),
516 sbcErrorString(err));
517 goto done;
520 ret = 0;
522 done:
523 return ret;
526 static int net_conf_showshare(struct net_context *c,
527 struct smbconf_ctx *conf_ctx, int argc,
528 const char **argv)
530 int ret = -1;
531 sbcErr err;
532 const char *sharename = NULL;
533 TALLOC_CTX *mem_ctx;
534 uint32_t count;
535 struct smbconf_service *service = NULL;
537 mem_ctx = talloc_stackframe();
539 if (argc != 1 || c->display_usage) {
540 net_conf_showshare_usage(c, argc, argv);
541 goto done;
544 sharename = talloc_strdup(mem_ctx, argv[0]);
545 if (sharename == NULL) {
546 d_printf("error: out of memory!\n");
547 goto done;
550 err = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
551 if (!SBC_ERROR_IS_OK(err)) {
552 d_printf(_("error getting share parameters: %s\n"),
553 sbcErrorString(err));
554 goto done;
557 d_printf("[%s]\n", service->name);
559 for (count = 0; count < service->num_params; count++) {
560 d_printf("\t%s = %s\n", service->param_names[count],
561 service->param_values[count]);
564 ret = 0;
566 done:
567 TALLOC_FREE(mem_ctx);
568 return ret;
572 * Add a share, with a couple of standard parameters, partly optional.
574 * This is a high level utility function of the net conf utility,
575 * not a direct frontend to the smbconf API.
577 static int net_conf_addshare(struct net_context *c,
578 struct smbconf_ctx *conf_ctx, int argc,
579 const char **argv)
581 int ret = -1;
582 sbcErr err;
583 char *sharename = NULL;
584 const char *path = NULL;
585 const char *comment = NULL;
586 const char *guest_ok = "no";
587 const char *writeable = "no";
588 TALLOC_CTX *mem_ctx = talloc_stackframe();
590 if (c->display_usage) {
591 net_conf_addshare_usage(c, argc, argv);
592 ret = 0;
593 goto done;
596 switch (argc) {
597 case 0:
598 case 1:
599 default:
600 net_conf_addshare_usage(c, argc, argv);
601 goto done;
602 case 5:
603 comment = argv[4];
604 case 4:
605 if (!strnequal(argv[3], "guest_ok=", 9)) {
606 net_conf_addshare_usage(c, argc, argv);
607 goto done;
609 switch (argv[3][9]) {
610 case 'y':
611 case 'Y':
612 guest_ok = "yes";
613 break;
614 case 'n':
615 case 'N':
616 guest_ok = "no";
617 break;
618 default:
619 net_conf_addshare_usage(c, argc, argv);
620 goto done;
622 case 3:
623 if (!strnequal(argv[2], "writeable=", 10)) {
624 net_conf_addshare_usage(c, argc, argv);
625 goto done;
627 switch (argv[2][10]) {
628 case 'y':
629 case 'Y':
630 writeable = "yes";
631 break;
632 case 'n':
633 case 'N':
634 writeable = "no";
635 break;
636 default:
637 net_conf_addshare_usage(c, argc, argv);
638 goto done;
640 case 2:
641 path = argv[1];
642 sharename = talloc_strdup(mem_ctx, argv[0]);
643 if (sharename == NULL) {
644 d_printf(_("error: out of memory!\n"));
645 goto done;
648 break;
652 * validate arguments
655 /* validate share name */
657 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
658 strlen(sharename)))
660 d_fprintf(stderr, _("ERROR: share name %s contains "
661 "invalid characters (any of %s)\n"),
662 sharename, INVALID_SHARENAME_CHARS);
663 goto done;
666 if (strequal(sharename, GLOBAL_NAME)) {
667 d_fprintf(stderr,
668 _("ERROR: 'global' is not a valid share name.\n"));
669 goto done;
672 if (smbconf_share_exists(conf_ctx, sharename)) {
673 d_fprintf(stderr, _("ERROR: share %s already exists.\n"),
674 sharename);
675 goto done;
678 /* validate path */
680 if (path[0] != '/') {
681 d_fprintf(stderr,
682 _("Error: path '%s' is not an absolute path.\n"),
683 path);
684 goto done;
688 * start a transaction
691 err = smbconf_transaction_start(conf_ctx);
692 if (!SBC_ERROR_IS_OK(err)) {
693 d_printf("error starting transaction: %s\n",
694 sbcErrorString(err));
695 goto done;
699 * create the share
702 err = smbconf_create_share(conf_ctx, sharename);
703 if (!SBC_ERROR_IS_OK(err)) {
704 d_fprintf(stderr, _("Error creating share %s: %s\n"),
705 sharename, sbcErrorString(err));
706 goto cancel;
710 * fill the share with parameters
713 err = smbconf_set_parameter(conf_ctx, sharename, "path", path);
714 if (!SBC_ERROR_IS_OK(err)) {
715 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
716 "path", sbcErrorString(err));
717 goto cancel;
720 if (comment != NULL) {
721 err = smbconf_set_parameter(conf_ctx, sharename, "comment",
722 comment);
723 if (!SBC_ERROR_IS_OK(err)) {
724 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
725 "comment", sbcErrorString(err));
726 goto cancel;
730 err = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
731 if (!SBC_ERROR_IS_OK(err)) {
732 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
733 "'guest ok'", sbcErrorString(err));
734 goto cancel;
737 err = smbconf_set_parameter(conf_ctx, sharename, "writeable",
738 writeable);
739 if (!SBC_ERROR_IS_OK(err)) {
740 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
741 "writeable", sbcErrorString(err));
742 goto cancel;
746 * commit the whole thing
749 err = smbconf_transaction_commit(conf_ctx);
750 if (!SBC_ERROR_IS_OK(err)) {
751 d_printf("error committing transaction: %s\n",
752 sbcErrorString(err));
753 } else {
754 ret = 0;
757 goto done;
759 cancel:
760 err = smbconf_transaction_cancel(conf_ctx);
761 if (!SBC_ERROR_IS_OK(err)) {
762 d_printf("error cancelling transaction: %s\n",
763 sbcErrorString(err));
766 done:
767 TALLOC_FREE(mem_ctx);
768 return ret;
771 static int net_conf_delshare(struct net_context *c,
772 struct smbconf_ctx *conf_ctx, int argc,
773 const char **argv)
775 int ret = -1;
776 const char *sharename = NULL;
777 sbcErr err;
778 TALLOC_CTX *mem_ctx = talloc_stackframe();
780 if (argc != 1 || c->display_usage) {
781 net_conf_delshare_usage(c, argc, argv);
782 goto done;
784 sharename = talloc_strdup(mem_ctx, argv[0]);
785 if (sharename == NULL) {
786 d_printf(_("error: out of memory!\n"));
787 goto done;
790 err = smbconf_delete_share(conf_ctx, sharename);
791 if (!SBC_ERROR_IS_OK(err)) {
792 d_fprintf(stderr, _("Error deleting share %s: %s\n"),
793 sharename, sbcErrorString(err));
794 goto done;
797 ret = 0;
798 done:
799 TALLOC_FREE(mem_ctx);
800 return ret;
803 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
804 int argc, const char **argv)
806 int ret = -1;
807 sbcErr err;
808 char *service = NULL;
809 char *param = NULL;
810 const char *value_str = NULL;
811 TALLOC_CTX *mem_ctx = talloc_stackframe();
813 if (argc != 3 || c->display_usage) {
814 net_conf_setparm_usage(c, argc, argv);
815 goto done;
818 * NULL service name means "dangling parameters" to libsmbconf.
819 * We use the empty string from the command line for this purpose.
821 if (strlen(argv[0]) != 0) {
822 service = talloc_strdup(mem_ctx, argv[0]);
823 if (service == NULL) {
824 d_printf(_("error: out of memory!\n"));
825 goto done;
828 param = strlower_talloc(mem_ctx, argv[1]);
829 if (param == NULL) {
830 d_printf(_("error: out of memory!\n"));
831 goto done;
833 value_str = argv[2];
835 err = smbconf_transaction_start(conf_ctx);
836 if (!SBC_ERROR_IS_OK(err)) {
837 d_printf(_("error starting transaction: %s\n"),
838 sbcErrorString(err));
839 goto done;
842 if (!smbconf_share_exists(conf_ctx, service)) {
843 err = smbconf_create_share(conf_ctx, service);
844 if (!SBC_ERROR_IS_OK(err)) {
845 d_fprintf(stderr, _("Error creating share '%s': %s\n"),
846 service, sbcErrorString(err));
847 goto cancel;
851 err = smbconf_set_parameter(conf_ctx, service, param, value_str);
852 if (!SBC_ERROR_IS_OK(err)) {
853 d_fprintf(stderr, _("Error setting value '%s': %s\n"),
854 param, sbcErrorString(err));
855 goto cancel;
858 err = smbconf_transaction_commit(conf_ctx);
859 if (!SBC_ERROR_IS_OK(err)) {
860 d_printf(_("error committing transaction: %s\n"),
861 sbcErrorString(err));
862 } else {
863 ret = 0;
866 goto done;
868 cancel:
869 err = smbconf_transaction_cancel(conf_ctx);
870 if (!SBC_ERROR_IS_OK(err)) {
871 d_printf(_("error cancelling transaction: %s\n"),
872 sbcErrorString(err));
875 done:
876 TALLOC_FREE(mem_ctx);
877 return ret;
880 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
881 int argc, const char **argv)
883 int ret = -1;
884 sbcErr err;
885 char *service = NULL;
886 char *param = NULL;
887 char *valstr = NULL;
888 TALLOC_CTX *mem_ctx;
890 mem_ctx = talloc_stackframe();
892 if (argc != 2 || c->display_usage) {
893 net_conf_getparm_usage(c, argc, argv);
894 goto done;
897 * NULL service name means "dangling parameters" to libsmbconf.
898 * We use the empty string from the command line for this purpose.
900 if (strlen(argv[0]) != 0) {
901 service = talloc_strdup(mem_ctx, argv[0]);
902 if (service == NULL) {
903 d_printf(_("error: out of memory!\n"));
904 goto done;
907 param = strlower_talloc(mem_ctx, argv[1]);
908 if (param == NULL) {
909 d_printf(_("error: out of memory!\n"));
910 goto done;
913 err = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
914 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
915 d_fprintf(stderr,
916 _("Error: given service '%s' does not exist.\n"),
917 service);
918 goto done;
919 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
920 d_fprintf(stderr,
921 _("Error: given parameter '%s' is not set.\n"),
922 param);
923 goto done;
924 } else if (!SBC_ERROR_IS_OK(err)) {
925 d_fprintf(stderr, _("Error getting value '%s': %s.\n"),
926 param, sbcErrorString(err));
927 goto done;
930 d_printf("%s\n", valstr);
932 ret = 0;
933 done:
934 TALLOC_FREE(mem_ctx);
935 return ret;
938 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
939 int argc, const char **argv)
941 int ret = -1;
942 sbcErr err;
943 char *service = NULL;
944 char *param = NULL;
945 TALLOC_CTX *mem_ctx = talloc_stackframe();
947 if (argc != 2 || c->display_usage) {
948 net_conf_delparm_usage(c, argc, argv);
949 goto done;
952 * NULL service name means "dangling parameters" to libsmbconf.
953 * We use the empty string from the command line for this purpose.
955 if (strlen(argv[0]) != 0) {
956 service = talloc_strdup(mem_ctx, argv[0]);
957 if (service == NULL) {
958 d_printf(_("error: out of memory!\n"));
959 goto done;
962 param = strlower_talloc(mem_ctx, argv[1]);
963 if (param == NULL) {
964 d_printf("error: out of memory!\n");
965 goto done;
968 err = smbconf_delete_parameter(conf_ctx, service, param);
969 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
970 d_fprintf(stderr,
971 _("Error: given service '%s' does not exist.\n"),
972 service);
973 goto done;
974 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
975 d_fprintf(stderr,
976 _("Error: given parameter '%s' is not set.\n"),
977 param);
978 goto done;
979 } else if (!SBC_ERROR_IS_OK(err)) {
980 d_fprintf(stderr, _("Error deleting value '%s': %s.\n"),
981 param, sbcErrorString(err));
982 goto done;
985 ret = 0;
987 done:
988 TALLOC_FREE(mem_ctx);
989 return ret;
992 static int net_conf_getincludes(struct net_context *c,
993 struct smbconf_ctx *conf_ctx,
994 int argc, const char **argv)
996 sbcErr err;
997 uint32_t num_includes;
998 uint32_t count;
999 char *service;
1000 char **includes = NULL;
1001 int ret = -1;
1002 TALLOC_CTX *mem_ctx = talloc_stackframe();
1004 if (argc != 1 || c->display_usage) {
1005 net_conf_getincludes_usage(c, argc, argv);
1006 goto done;
1009 service = talloc_strdup(mem_ctx, argv[0]);
1010 if (service == NULL) {
1011 d_printf(_("error: out of memory!\n"));
1012 goto done;
1015 err = smbconf_get_includes(conf_ctx, mem_ctx, service,
1016 &num_includes, &includes);
1017 if (!SBC_ERROR_IS_OK(err)) {
1018 d_printf(_("error getting includes: %s\n"), sbcErrorString(err));
1019 goto done;
1022 for (count = 0; count < num_includes; count++) {
1023 d_printf("include = %s\n", includes[count]);
1026 ret = 0;
1028 done:
1029 TALLOC_FREE(mem_ctx);
1030 return ret;
1033 static int net_conf_setincludes(struct net_context *c,
1034 struct smbconf_ctx *conf_ctx,
1035 int argc, const char **argv)
1037 sbcErr err;
1038 char *service;
1039 uint32_t num_includes;
1040 const char **includes;
1041 int ret = -1;
1042 TALLOC_CTX *mem_ctx = talloc_stackframe();
1044 if (argc < 1 || c->display_usage) {
1045 net_conf_setincludes_usage(c, argc, argv);
1046 goto done;
1049 service = talloc_strdup(mem_ctx, argv[0]);
1050 if (service == NULL) {
1051 d_printf(_("error: out of memory!\n"));
1052 goto done;
1055 num_includes = argc - 1;
1056 if (num_includes == 0) {
1057 includes = NULL;
1058 } else {
1059 includes = argv + 1;
1062 err = smbconf_set_includes(conf_ctx, service, num_includes, includes);
1063 if (!SBC_ERROR_IS_OK(err)) {
1064 d_printf(_("error setting includes: %s\n"), sbcErrorString(err));
1065 goto done;
1068 ret = 0;
1070 done:
1071 TALLOC_FREE(mem_ctx);
1072 return ret;
1075 static int net_conf_delincludes(struct net_context *c,
1076 struct smbconf_ctx *conf_ctx,
1077 int argc, const char **argv)
1079 sbcErr err;
1080 char *service;
1081 int ret = -1;
1082 TALLOC_CTX *mem_ctx = talloc_stackframe();
1084 if (argc != 1 || c->display_usage) {
1085 net_conf_delincludes_usage(c, argc, argv);
1086 goto done;
1089 service = talloc_strdup(mem_ctx, argv[0]);
1090 if (service == NULL) {
1091 d_printf(_("error: out of memory!\n"));
1092 goto done;
1095 err = smbconf_delete_includes(conf_ctx, service);
1096 if (!SBC_ERROR_IS_OK(err)) {
1097 d_printf(_("error deleting includes: %s\n"), sbcErrorString(err));
1098 goto done;
1101 ret = 0;
1103 done:
1104 TALLOC_FREE(mem_ctx);
1105 return ret;
1109 /**********************************************************************
1111 * Wrapper and net_conf_run_function mechanism.
1113 **********************************************************************/
1116 * Wrapper function to call the main conf functions.
1117 * The wrapper calls handles opening and closing of the
1118 * configuration.
1120 static int net_conf_wrap_function(struct net_context *c,
1121 int (*fn)(struct net_context *,
1122 struct smbconf_ctx *,
1123 int, const char **),
1124 int argc, const char **argv)
1126 sbcErr err;
1127 TALLOC_CTX *mem_ctx = talloc_stackframe();
1128 struct smbconf_ctx *conf_ctx;
1129 int ret = -1;
1131 err = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1132 if (!SBC_ERROR_IS_OK(err)) {
1133 return -1;
1136 ret = fn(c, conf_ctx, argc, argv);
1138 smbconf_shutdown(conf_ctx);
1140 return ret;
1144 * We need a functable struct of our own, because the
1145 * functions are called through a wrapper that handles
1146 * the opening and closing of the configuration, and so on.
1148 struct conf_functable {
1149 const char *funcname;
1150 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1151 const char **argv);
1152 int valid_transports;
1153 const char *description;
1154 const char *usage;
1158 * This imitates net_run_function but calls the main functions
1159 * through the wrapper net_conf_wrap_function().
1161 static int net_conf_run_function(struct net_context *c, int argc,
1162 const char **argv, const char *whoami,
1163 struct conf_functable *table)
1165 int i;
1167 if (argc != 0) {
1168 for (i=0; table[i].funcname; i++) {
1169 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1170 return net_conf_wrap_function(c, table[i].fn,
1171 argc-1,
1172 argv+1);
1176 d_printf(_("Usage:\n"));
1177 for (i=0; table[i].funcname; i++) {
1178 if (c->display_usage == false)
1179 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1180 table[i].description);
1181 else
1182 d_printf("%s\n", table[i].usage);
1185 return c->display_usage?0:-1;
1189 * Entry-point for all the CONF functions.
1192 int net_conf(struct net_context *c, int argc, const char **argv)
1194 int ret = -1;
1195 struct conf_functable func_table[] = {
1197 "list",
1198 net_conf_list,
1199 NET_TRANSPORT_LOCAL,
1200 N_("Dump the complete configuration in smb.conf like "
1201 "format."),
1202 N_("net conf list\n"
1203 " Dump the complete configuration in smb.conf "
1204 "like format.")
1208 "import",
1209 net_conf_import,
1210 NET_TRANSPORT_LOCAL,
1211 N_("Import configuration from file in smb.conf "
1212 "format."),
1213 N_("net conf import\n"
1214 " Import configuration from file in smb.conf "
1215 "format.")
1218 "listshares",
1219 net_conf_listshares,
1220 NET_TRANSPORT_LOCAL,
1221 N_("List the share names."),
1222 N_("net conf listshares\n"
1223 " List the share names.")
1226 "drop",
1227 net_conf_drop,
1228 NET_TRANSPORT_LOCAL,
1229 N_("Delete the complete configuration."),
1230 N_("net conf drop\n"
1231 " Delete the complete configuration.")
1234 "showshare",
1235 net_conf_showshare,
1236 NET_TRANSPORT_LOCAL,
1237 N_("Show the definition of a share."),
1238 N_("net conf showshare\n"
1239 " Show the definition of a share.")
1242 "addshare",
1243 net_conf_addshare,
1244 NET_TRANSPORT_LOCAL,
1245 N_("Create a new share."),
1246 N_("net conf addshare\n"
1247 " Create a new share.")
1250 "delshare",
1251 net_conf_delshare,
1252 NET_TRANSPORT_LOCAL,
1253 N_("Delete a share."),
1254 N_("net conf delshare\n"
1255 " Delete a share.")
1258 "setparm",
1259 net_conf_setparm,
1260 NET_TRANSPORT_LOCAL,
1261 N_("Store a parameter."),
1262 N_("net conf setparm\n"
1263 " Store a parameter.")
1266 "getparm",
1267 net_conf_getparm,
1268 NET_TRANSPORT_LOCAL,
1269 N_("Retrieve the value of a parameter."),
1270 N_("net conf getparm\n"
1271 " Retrieve the value of a parameter.")
1274 "delparm",
1275 net_conf_delparm,
1276 NET_TRANSPORT_LOCAL,
1277 N_("Delete a parameter."),
1278 N_("net conf delparm\n"
1279 " Delete a parameter.")
1282 "getincludes",
1283 net_conf_getincludes,
1284 NET_TRANSPORT_LOCAL,
1285 N_("Show the includes of a share definition."),
1286 N_("net conf getincludes\n"
1287 " Show the includes of a share definition.")
1290 "setincludes",
1291 net_conf_setincludes,
1292 NET_TRANSPORT_LOCAL,
1293 N_("Set includes for a share."),
1294 N_("net conf setincludes\n"
1295 " Set includes for a share.")
1298 "delincludes",
1299 net_conf_delincludes,
1300 NET_TRANSPORT_LOCAL,
1301 N_("Delete includes from a share definition."),
1302 N_("net conf setincludes\n"
1303 " Delete includes from a share definition.")
1305 {NULL, NULL, 0, NULL, NULL}
1308 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1310 return ret;