s3-lib Replace StrCaseCmp() with strcasecmp_m()
[Samba.git] / source3 / utils / net_conf.c
blob6c61dd7206105896bb4a7d2a11d202110d74a1a2
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 SMB_STRUCT_STAT sbuf;
589 TALLOC_CTX *mem_ctx = talloc_stackframe();
591 if (c->display_usage) {
592 net_conf_addshare_usage(c, argc, argv);
593 ret = 0;
594 goto done;
597 switch (argc) {
598 case 0:
599 case 1:
600 default:
601 net_conf_addshare_usage(c, argc, argv);
602 goto done;
603 case 5:
604 comment = argv[4];
605 case 4:
606 if (!strnequal(argv[3], "guest_ok=", 9)) {
607 net_conf_addshare_usage(c, argc, argv);
608 goto done;
610 switch (argv[3][9]) {
611 case 'y':
612 case 'Y':
613 guest_ok = "yes";
614 break;
615 case 'n':
616 case 'N':
617 guest_ok = "no";
618 break;
619 default:
620 net_conf_addshare_usage(c, argc, argv);
621 goto done;
623 case 3:
624 if (!strnequal(argv[2], "writeable=", 10)) {
625 net_conf_addshare_usage(c, argc, argv);
626 goto done;
628 switch (argv[2][10]) {
629 case 'y':
630 case 'Y':
631 writeable = "yes";
632 break;
633 case 'n':
634 case 'N':
635 writeable = "no";
636 break;
637 default:
638 net_conf_addshare_usage(c, argc, argv);
639 goto done;
641 case 2:
642 path = argv[1];
643 sharename = talloc_strdup(mem_ctx, argv[0]);
644 if (sharename == NULL) {
645 d_printf(_("error: out of memory!\n"));
646 goto done;
649 break;
653 * validate arguments
656 /* validate share name */
658 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
659 strlen(sharename)))
661 d_fprintf(stderr, _("ERROR: share name %s contains "
662 "invalid characters (any of %s)\n"),
663 sharename, INVALID_SHARENAME_CHARS);
664 goto done;
667 if (strequal(sharename, GLOBAL_NAME)) {
668 d_fprintf(stderr,
669 _("ERROR: 'global' is not a valid share name.\n"));
670 goto done;
673 if (smbconf_share_exists(conf_ctx, sharename)) {
674 d_fprintf(stderr, _("ERROR: share %s already exists.\n"),
675 sharename);
676 goto done;
679 /* validate path */
681 if (path[0] != '/') {
682 d_fprintf(stderr,
683 _("Error: path '%s' is not an absolute path.\n"),
684 path);
685 goto done;
688 if (sys_stat(path, &sbuf, false) != 0) {
689 d_fprintf(stderr,
690 _("ERROR: cannot stat path '%s' to ensure "
691 "this is a directory.\n"
692 "Error was '%s'.\n"),
693 path, strerror(errno));
694 goto done;
697 if (!S_ISDIR(sbuf.st_ex_mode)) {
698 d_fprintf(stderr,
699 _("ERROR: path '%s' is not a directory.\n"),
700 path);
701 goto done;
705 * start a transaction
708 err = smbconf_transaction_start(conf_ctx);
709 if (!SBC_ERROR_IS_OK(err)) {
710 d_printf("error starting transaction: %s\n",
711 sbcErrorString(err));
712 goto done;
716 * create the share
719 err = smbconf_create_share(conf_ctx, sharename);
720 if (!SBC_ERROR_IS_OK(err)) {
721 d_fprintf(stderr, _("Error creating share %s: %s\n"),
722 sharename, sbcErrorString(err));
723 goto cancel;
727 * fill the share with parameters
730 err = smbconf_set_parameter(conf_ctx, sharename, "path", path);
731 if (!SBC_ERROR_IS_OK(err)) {
732 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
733 "path", sbcErrorString(err));
734 goto cancel;
737 if (comment != NULL) {
738 err = smbconf_set_parameter(conf_ctx, sharename, "comment",
739 comment);
740 if (!SBC_ERROR_IS_OK(err)) {
741 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
742 "comment", sbcErrorString(err));
743 goto cancel;
747 err = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
748 if (!SBC_ERROR_IS_OK(err)) {
749 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
750 "'guest ok'", sbcErrorString(err));
751 goto cancel;
754 err = smbconf_set_parameter(conf_ctx, sharename, "writeable",
755 writeable);
756 if (!SBC_ERROR_IS_OK(err)) {
757 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
758 "writeable", sbcErrorString(err));
759 goto cancel;
763 * commit the whole thing
766 err = smbconf_transaction_commit(conf_ctx);
767 if (!SBC_ERROR_IS_OK(err)) {
768 d_printf("error committing transaction: %s\n",
769 sbcErrorString(err));
770 } else {
771 ret = 0;
774 goto done;
776 cancel:
777 err = smbconf_transaction_cancel(conf_ctx);
778 if (!SBC_ERROR_IS_OK(err)) {
779 d_printf("error cancelling transaction: %s\n",
780 sbcErrorString(err));
783 done:
784 TALLOC_FREE(mem_ctx);
785 return ret;
788 static int net_conf_delshare(struct net_context *c,
789 struct smbconf_ctx *conf_ctx, int argc,
790 const char **argv)
792 int ret = -1;
793 const char *sharename = NULL;
794 sbcErr err;
795 TALLOC_CTX *mem_ctx = talloc_stackframe();
797 if (argc != 1 || c->display_usage) {
798 net_conf_delshare_usage(c, argc, argv);
799 goto done;
801 sharename = talloc_strdup(mem_ctx, argv[0]);
802 if (sharename == NULL) {
803 d_printf(_("error: out of memory!\n"));
804 goto done;
807 err = smbconf_delete_share(conf_ctx, sharename);
808 if (!SBC_ERROR_IS_OK(err)) {
809 d_fprintf(stderr, _("Error deleting share %s: %s\n"),
810 sharename, sbcErrorString(err));
811 goto done;
814 ret = 0;
815 done:
816 TALLOC_FREE(mem_ctx);
817 return ret;
820 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
821 int argc, const char **argv)
823 int ret = -1;
824 sbcErr err;
825 char *service = NULL;
826 char *param = NULL;
827 const char *value_str = NULL;
828 TALLOC_CTX *mem_ctx = talloc_stackframe();
830 if (argc != 3 || c->display_usage) {
831 net_conf_setparm_usage(c, argc, argv);
832 goto done;
835 * NULL service name means "dangling parameters" to libsmbconf.
836 * We use the empty string from the command line for this purpose.
838 if (strlen(argv[0]) != 0) {
839 service = talloc_strdup(mem_ctx, argv[0]);
840 if (service == NULL) {
841 d_printf(_("error: out of memory!\n"));
842 goto done;
845 param = strlower_talloc(mem_ctx, argv[1]);
846 if (param == NULL) {
847 d_printf(_("error: out of memory!\n"));
848 goto done;
850 value_str = argv[2];
852 err = smbconf_transaction_start(conf_ctx);
853 if (!SBC_ERROR_IS_OK(err)) {
854 d_printf(_("error starting transaction: %s\n"),
855 sbcErrorString(err));
856 goto done;
859 if (!smbconf_share_exists(conf_ctx, service)) {
860 err = smbconf_create_share(conf_ctx, service);
861 if (!SBC_ERROR_IS_OK(err)) {
862 d_fprintf(stderr, _("Error creating share '%s': %s\n"),
863 service, sbcErrorString(err));
864 goto cancel;
868 err = smbconf_set_parameter(conf_ctx, service, param, value_str);
869 if (!SBC_ERROR_IS_OK(err)) {
870 d_fprintf(stderr, _("Error setting value '%s': %s\n"),
871 param, sbcErrorString(err));
872 goto cancel;
875 err = smbconf_transaction_commit(conf_ctx);
876 if (!SBC_ERROR_IS_OK(err)) {
877 d_printf(_("error committing transaction: %s\n"),
878 sbcErrorString(err));
879 } else {
880 ret = 0;
883 goto done;
885 cancel:
886 err = smbconf_transaction_cancel(conf_ctx);
887 if (!SBC_ERROR_IS_OK(err)) {
888 d_printf(_("error cancelling transaction: %s\n"),
889 sbcErrorString(err));
892 done:
893 TALLOC_FREE(mem_ctx);
894 return ret;
897 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
898 int argc, const char **argv)
900 int ret = -1;
901 sbcErr err;
902 char *service = NULL;
903 char *param = NULL;
904 char *valstr = NULL;
905 TALLOC_CTX *mem_ctx;
907 mem_ctx = talloc_stackframe();
909 if (argc != 2 || c->display_usage) {
910 net_conf_getparm_usage(c, argc, argv);
911 goto done;
914 * NULL service name means "dangling parameters" to libsmbconf.
915 * We use the empty string from the command line for this purpose.
917 if (strlen(argv[0]) != 0) {
918 service = talloc_strdup(mem_ctx, argv[0]);
919 if (service == NULL) {
920 d_printf(_("error: out of memory!\n"));
921 goto done;
924 param = strlower_talloc(mem_ctx, argv[1]);
925 if (param == NULL) {
926 d_printf(_("error: out of memory!\n"));
927 goto done;
930 err = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
931 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
932 d_fprintf(stderr,
933 _("Error: given service '%s' does not exist.\n"),
934 service);
935 goto done;
936 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
937 d_fprintf(stderr,
938 _("Error: given parameter '%s' is not set.\n"),
939 param);
940 goto done;
941 } else if (!SBC_ERROR_IS_OK(err)) {
942 d_fprintf(stderr, _("Error getting value '%s': %s.\n"),
943 param, sbcErrorString(err));
944 goto done;
947 d_printf("%s\n", valstr);
949 ret = 0;
950 done:
951 TALLOC_FREE(mem_ctx);
952 return ret;
955 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
956 int argc, const char **argv)
958 int ret = -1;
959 sbcErr err;
960 char *service = NULL;
961 char *param = NULL;
962 TALLOC_CTX *mem_ctx = talloc_stackframe();
964 if (argc != 2 || c->display_usage) {
965 net_conf_delparm_usage(c, argc, argv);
966 goto done;
969 * NULL service name means "dangling parameters" to libsmbconf.
970 * We use the empty string from the command line for this purpose.
972 if (strlen(argv[0]) != 0) {
973 service = talloc_strdup(mem_ctx, argv[0]);
974 if (service == NULL) {
975 d_printf(_("error: out of memory!\n"));
976 goto done;
979 param = strlower_talloc(mem_ctx, argv[1]);
980 if (param == NULL) {
981 d_printf("error: out of memory!\n");
982 goto done;
985 err = smbconf_delete_parameter(conf_ctx, service, param);
986 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
987 d_fprintf(stderr,
988 _("Error: given service '%s' does not exist.\n"),
989 service);
990 goto done;
991 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
992 d_fprintf(stderr,
993 _("Error: given parameter '%s' is not set.\n"),
994 param);
995 goto done;
996 } else if (!SBC_ERROR_IS_OK(err)) {
997 d_fprintf(stderr, _("Error deleting value '%s': %s.\n"),
998 param, sbcErrorString(err));
999 goto done;
1002 ret = 0;
1004 done:
1005 TALLOC_FREE(mem_ctx);
1006 return ret;
1009 static int net_conf_getincludes(struct net_context *c,
1010 struct smbconf_ctx *conf_ctx,
1011 int argc, const char **argv)
1013 sbcErr err;
1014 uint32_t num_includes;
1015 uint32_t count;
1016 char *service;
1017 char **includes = NULL;
1018 int ret = -1;
1019 TALLOC_CTX *mem_ctx = talloc_stackframe();
1021 if (argc != 1 || c->display_usage) {
1022 net_conf_getincludes_usage(c, argc, argv);
1023 goto done;
1026 service = talloc_strdup(mem_ctx, argv[0]);
1027 if (service == NULL) {
1028 d_printf(_("error: out of memory!\n"));
1029 goto done;
1032 err = smbconf_get_includes(conf_ctx, mem_ctx, service,
1033 &num_includes, &includes);
1034 if (!SBC_ERROR_IS_OK(err)) {
1035 d_printf(_("error getting includes: %s\n"), sbcErrorString(err));
1036 goto done;
1039 for (count = 0; count < num_includes; count++) {
1040 d_printf("include = %s\n", includes[count]);
1043 ret = 0;
1045 done:
1046 TALLOC_FREE(mem_ctx);
1047 return ret;
1050 static int net_conf_setincludes(struct net_context *c,
1051 struct smbconf_ctx *conf_ctx,
1052 int argc, const char **argv)
1054 sbcErr err;
1055 char *service;
1056 uint32_t num_includes;
1057 const char **includes;
1058 int ret = -1;
1059 TALLOC_CTX *mem_ctx = talloc_stackframe();
1061 if (argc < 1 || c->display_usage) {
1062 net_conf_setincludes_usage(c, argc, argv);
1063 goto done;
1066 service = talloc_strdup(mem_ctx, argv[0]);
1067 if (service == NULL) {
1068 d_printf(_("error: out of memory!\n"));
1069 goto done;
1072 num_includes = argc - 1;
1073 if (num_includes == 0) {
1074 includes = NULL;
1075 } else {
1076 includes = argv + 1;
1079 err = smbconf_set_includes(conf_ctx, service, num_includes, includes);
1080 if (!SBC_ERROR_IS_OK(err)) {
1081 d_printf(_("error setting includes: %s\n"), sbcErrorString(err));
1082 goto done;
1085 ret = 0;
1087 done:
1088 TALLOC_FREE(mem_ctx);
1089 return ret;
1092 static int net_conf_delincludes(struct net_context *c,
1093 struct smbconf_ctx *conf_ctx,
1094 int argc, const char **argv)
1096 sbcErr err;
1097 char *service;
1098 int ret = -1;
1099 TALLOC_CTX *mem_ctx = talloc_stackframe();
1101 if (argc != 1 || c->display_usage) {
1102 net_conf_delincludes_usage(c, argc, argv);
1103 goto done;
1106 service = talloc_strdup(mem_ctx, argv[0]);
1107 if (service == NULL) {
1108 d_printf(_("error: out of memory!\n"));
1109 goto done;
1112 err = smbconf_delete_includes(conf_ctx, service);
1113 if (!SBC_ERROR_IS_OK(err)) {
1114 d_printf(_("error deleting includes: %s\n"), sbcErrorString(err));
1115 goto done;
1118 ret = 0;
1120 done:
1121 TALLOC_FREE(mem_ctx);
1122 return ret;
1126 /**********************************************************************
1128 * Wrapper and net_conf_run_function mechanism.
1130 **********************************************************************/
1133 * Wrapper function to call the main conf functions.
1134 * The wrapper calls handles opening and closing of the
1135 * configuration.
1137 static int net_conf_wrap_function(struct net_context *c,
1138 int (*fn)(struct net_context *,
1139 struct smbconf_ctx *,
1140 int, const char **),
1141 int argc, const char **argv)
1143 sbcErr err;
1144 TALLOC_CTX *mem_ctx = talloc_stackframe();
1145 struct smbconf_ctx *conf_ctx;
1146 int ret = -1;
1148 err = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1149 if (!SBC_ERROR_IS_OK(err)) {
1150 return -1;
1153 ret = fn(c, conf_ctx, argc, argv);
1155 smbconf_shutdown(conf_ctx);
1157 return ret;
1161 * We need a functable struct of our own, because the
1162 * functions are called through a wrapper that handles
1163 * the opening and closing of the configuration, and so on.
1165 struct conf_functable {
1166 const char *funcname;
1167 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1168 const char **argv);
1169 int valid_transports;
1170 const char *description;
1171 const char *usage;
1175 * This imitates net_run_function but calls the main functions
1176 * through the wrapper net_conf_wrap_function().
1178 static int net_conf_run_function(struct net_context *c, int argc,
1179 const char **argv, const char *whoami,
1180 struct conf_functable *table)
1182 int i;
1184 if (argc != 0) {
1185 for (i=0; table[i].funcname; i++) {
1186 if (strcasecmp_m(argv[0], table[i].funcname) == 0)
1187 return net_conf_wrap_function(c, table[i].fn,
1188 argc-1,
1189 argv+1);
1193 d_printf(_("Usage:\n"));
1194 for (i=0; table[i].funcname; i++) {
1195 if (c->display_usage == false)
1196 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1197 table[i].description);
1198 else
1199 d_printf("%s\n", table[i].usage);
1202 return c->display_usage?0:-1;
1206 * Entry-point for all the CONF functions.
1209 int net_conf(struct net_context *c, int argc, const char **argv)
1211 int ret = -1;
1212 struct conf_functable func_table[] = {
1214 "list",
1215 net_conf_list,
1216 NET_TRANSPORT_LOCAL,
1217 N_("Dump the complete configuration in smb.conf like "
1218 "format."),
1219 N_("net conf list\n"
1220 " Dump the complete configuration in smb.conf "
1221 "like format.")
1225 "import",
1226 net_conf_import,
1227 NET_TRANSPORT_LOCAL,
1228 N_("Import configuration from file in smb.conf "
1229 "format."),
1230 N_("net conf import\n"
1231 " Import configuration from file in smb.conf "
1232 "format.")
1235 "listshares",
1236 net_conf_listshares,
1237 NET_TRANSPORT_LOCAL,
1238 N_("List the share names."),
1239 N_("net conf listshares\n"
1240 " List the share names.")
1243 "drop",
1244 net_conf_drop,
1245 NET_TRANSPORT_LOCAL,
1246 N_("Delete the complete configuration."),
1247 N_("net conf drop\n"
1248 " Delete the complete configuration.")
1251 "showshare",
1252 net_conf_showshare,
1253 NET_TRANSPORT_LOCAL,
1254 N_("Show the definition of a share."),
1255 N_("net conf showshare\n"
1256 " Show the definition of a share.")
1259 "addshare",
1260 net_conf_addshare,
1261 NET_TRANSPORT_LOCAL,
1262 N_("Create a new share."),
1263 N_("net conf addshare\n"
1264 " Create a new share.")
1267 "delshare",
1268 net_conf_delshare,
1269 NET_TRANSPORT_LOCAL,
1270 N_("Delete a share."),
1271 N_("net conf delshare\n"
1272 " Delete a share.")
1275 "setparm",
1276 net_conf_setparm,
1277 NET_TRANSPORT_LOCAL,
1278 N_("Store a parameter."),
1279 N_("net conf setparm\n"
1280 " Store a parameter.")
1283 "getparm",
1284 net_conf_getparm,
1285 NET_TRANSPORT_LOCAL,
1286 N_("Retrieve the value of a parameter."),
1287 N_("net conf getparm\n"
1288 " Retrieve the value of a parameter.")
1291 "delparm",
1292 net_conf_delparm,
1293 NET_TRANSPORT_LOCAL,
1294 N_("Delete a parameter."),
1295 N_("net conf delparm\n"
1296 " Delete a parameter.")
1299 "getincludes",
1300 net_conf_getincludes,
1301 NET_TRANSPORT_LOCAL,
1302 N_("Show the includes of a share definition."),
1303 N_("net conf getincludes\n"
1304 " Show the includes of a share definition.")
1307 "setincludes",
1308 net_conf_setincludes,
1309 NET_TRANSPORT_LOCAL,
1310 N_("Set includes for a share."),
1311 N_("net conf setincludes\n"
1312 " Set includes for a share.")
1315 "delincludes",
1316 net_conf_delincludes,
1317 NET_TRANSPORT_LOCAL,
1318 N_("Delete includes from a share definition."),
1319 N_("net conf setincludes\n"
1320 " Delete includes from a share definition.")
1322 {NULL, NULL, 0, NULL, NULL}
1325 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1327 return ret;