packaging(RHEL-CTDB): move the idmap and nss_info modules to the common package
[Samba/gbeck.git] / source3 / utils / net_conf.c
blob4b511f2faa0c3a7cd14aec28177d2ae640532ecb
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 "utils/net.h"
33 /**********************************************************************
35 * usage functions
37 **********************************************************************/
39 static int net_conf_list_usage(struct net_context *c, int argc,
40 const char **argv)
42 d_printf(_("USAGE: net conf list\n"));
43 return -1;
46 static int net_conf_import_usage(struct net_context *c, int argc,
47 const char**argv)
49 d_printf(_("USAGE: net conf import [--test|-T] <filename> "
50 "[<servicename>]\n"
51 "\t[--test|-T] testmode - do not act, just print "
52 "what would be done\n"
53 "\t<servicename> only import service <servicename>, "
54 "ignore the rest\n"));
55 return -1;
58 static int net_conf_listshares_usage(struct net_context *c, int argc,
59 const char **argv)
61 d_printf(_("USAGE: net conf listshares\n"));
62 return -1;
65 static int net_conf_drop_usage(struct net_context *c, int argc,
66 const char **argv)
68 d_printf(_("USAGE: net conf drop\n"));
69 return -1;
72 static int net_conf_showshare_usage(struct net_context *c, int argc,
73 const char **argv)
75 d_printf(_("USAGE: net conf showshare <sharename>\n"));
76 return -1;
79 static int net_conf_addshare_usage(struct net_context *c, int argc,
80 const char **argv)
82 d_printf(_("USAGE: net conf addshare <sharename> <path> "
83 "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
84 "\t<sharename> the new share name.\n"
85 "\t<path> the path on the filesystem to export.\n"
86 "\twriteable={y|N} set \"writeable to \"yes\" or "
87 "\"no\" (default) on this share.\n"
88 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
89 "\"no\" (default) on this share.\n"
90 "\t<comment> optional comment for the new share.\n"));
91 return -1;
94 static int net_conf_delshare_usage(struct net_context *c, int argc,
95 const char **argv)
97 d_printf(_("USAGE: net conf delshare <sharename>\n"));
98 return -1;
101 static int net_conf_setparm_usage(struct net_context *c, int argc,
102 const char **argv)
104 d_printf(_("USAGE: net conf setparm <section> <param> <value>\n"));
105 return -1;
108 static int net_conf_getparm_usage(struct net_context *c, int argc,
109 const char **argv)
111 d_printf(_("USAGE: net conf getparm <section> <param>\n"));
112 return -1;
115 static int net_conf_delparm_usage(struct net_context *c, int argc,
116 const char **argv)
118 d_printf(_("USAGE: net conf delparm <section> <param>\n"));
119 return -1;
122 static int net_conf_getincludes_usage(struct net_context *c, int argc,
123 const char **argv)
125 d_printf(_("USAGE: net conf getincludes <section>\n"));
126 return -1;
129 static int net_conf_setincludes_usage(struct net_context *c, int argc,
130 const char **argv)
132 d_printf(_("USAGE: net conf setincludes <section> [<filename>]*\n"));
133 return -1;
136 static int net_conf_delincludes_usage(struct net_context *c, int argc,
137 const char **argv)
139 d_printf(_("USAGE: net conf delincludes <section>\n"));
140 return -1;
144 /**********************************************************************
146 * Helper functions
148 **********************************************************************/
151 * This functions process a service previously loaded with libsmbconf.
153 static WERROR import_process_service(struct net_context *c,
154 struct smbconf_ctx *conf_ctx,
155 struct smbconf_service *service)
157 uint32_t idx;
158 WERROR werr = WERR_OK;
159 uint32_t num_includes = 0;
160 char **includes = NULL;
161 TALLOC_CTX *mem_ctx = talloc_stackframe();
163 if (c->opt_testmode) {
164 const char *indent = "";
165 if (service->name != NULL) {
166 d_printf("[%s]\n", service->name);
167 indent = "\t";
169 for (idx = 0; idx < service->num_params; idx++) {
170 d_printf("%s%s = %s\n", indent,
171 service->param_names[idx],
172 service->param_values[idx]);
174 d_printf("\n");
175 goto done;
178 if (smbconf_share_exists(conf_ctx, service->name)) {
179 werr = smbconf_delete_share(conf_ctx, service->name);
180 if (!W_ERROR_IS_OK(werr)) {
181 goto done;
184 werr = smbconf_create_share(conf_ctx, service->name);
185 if (!W_ERROR_IS_OK(werr)) {
186 goto done;
189 for (idx = 0; idx < service->num_params; idx ++) {
190 if (strequal(service->param_names[idx], "include")) {
191 includes = TALLOC_REALLOC_ARRAY(mem_ctx,
192 includes,
193 char *,
194 num_includes+1);
195 if (includes == NULL) {
196 werr = WERR_NOMEM;
197 goto done;
199 includes[num_includes] = talloc_strdup(includes,
200 service->param_values[idx]);
201 if (includes[num_includes] == NULL) {
202 werr = WERR_NOMEM;
203 goto done;
205 num_includes++;
206 } else {
207 werr = smbconf_set_parameter(conf_ctx,
208 service->name,
209 service->param_names[idx],
210 service->param_values[idx]);
211 if (!W_ERROR_IS_OK(werr)) {
212 goto done;
217 werr = smbconf_set_includes(conf_ctx, service->name, num_includes,
218 (const char **)includes);
220 done:
221 TALLOC_FREE(mem_ctx);
222 return werr;
226 /**********************************************************************
228 * the main conf functions
230 **********************************************************************/
232 static int net_conf_list(struct net_context *c, struct smbconf_ctx *conf_ctx,
233 int argc, const char **argv)
235 WERROR werr = WERR_OK;
236 int ret = -1;
237 TALLOC_CTX *mem_ctx;
238 uint32_t num_shares;
239 uint32_t share_count, param_count;
240 struct smbconf_service **shares = NULL;
242 mem_ctx = talloc_stackframe();
244 if (argc != 0 || c->display_usage) {
245 net_conf_list_usage(c, argc, argv);
246 goto done;
249 werr = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &shares);
250 if (!W_ERROR_IS_OK(werr)) {
251 d_fprintf(stderr, _("Error getting config: %s\n"),
252 win_errstr(werr));
253 goto done;
256 for (share_count = 0; share_count < num_shares; share_count++) {
257 const char *indent = "";
258 if (shares[share_count]->name != NULL) {
259 d_printf("[%s]\n", shares[share_count]->name);
260 indent = "\t";
262 for (param_count = 0;
263 param_count < shares[share_count]->num_params;
264 param_count++)
266 d_printf("%s%s = %s\n",
267 indent,
268 shares[share_count]->param_names[param_count],
269 shares[share_count]->param_values[param_count]);
271 d_printf("\n");
274 ret = 0;
276 done:
277 TALLOC_FREE(mem_ctx);
278 return ret;
281 static int net_conf_import(struct net_context *c, struct smbconf_ctx *conf_ctx,
282 int argc, const char **argv)
284 int ret = -1;
285 const char *filename = NULL;
286 const char *servicename = NULL;
287 char *conf_source = NULL;
288 TALLOC_CTX *mem_ctx;
289 struct smbconf_ctx *txt_ctx;
290 WERROR werr;
292 if (c->display_usage)
293 return net_conf_import_usage(c, argc, argv);
295 mem_ctx = talloc_stackframe();
297 switch (argc) {
298 case 0:
299 default:
300 net_conf_import_usage(c, argc, argv);
301 goto done;
302 case 2:
303 servicename = talloc_strdup(mem_ctx, argv[1]);
304 if (servicename == NULL) {
305 d_printf(_("error: out of memory!\n"));
306 goto done;
308 case 1:
309 filename = argv[0];
310 break;
313 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
314 filename));
316 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
317 if (conf_source == NULL) {
318 d_printf(_("error: out of memory!\n"));
319 goto done;
322 werr = smbconf_init(mem_ctx, &txt_ctx, conf_source);
323 if (!W_ERROR_IS_OK(werr)) {
324 d_printf(_("error loading file '%s': %s\n"), filename,
325 win_errstr(werr));
326 goto done;
329 if (c->opt_testmode) {
330 d_printf(_("\nTEST MODE - "
331 "would import the following configuration:\n\n"));
334 if (servicename != NULL) {
335 struct smbconf_service *service = NULL;
337 werr = smbconf_get_share(txt_ctx, mem_ctx,
338 servicename,
339 &service);
340 if (!W_ERROR_IS_OK(werr)) {
341 goto cancel;
344 werr = smbconf_transaction_start(conf_ctx);
345 if (!W_ERROR_IS_OK(werr)) {
346 d_printf(_("error starting transaction: %s\n"),
347 win_errstr(werr));
348 goto done;
351 werr = import_process_service(c, conf_ctx, service);
352 if (!W_ERROR_IS_OK(werr)) {
353 goto cancel;
355 } else {
356 struct smbconf_service **services = NULL;
357 uint32_t num_shares, sidx;
359 werr = smbconf_get_config(txt_ctx, mem_ctx,
360 &num_shares,
361 &services);
362 if (!W_ERROR_IS_OK(werr)) {
363 goto cancel;
365 if (!c->opt_testmode) {
366 werr = smbconf_drop(conf_ctx);
367 if (!W_ERROR_IS_OK(werr)) {
368 goto cancel;
373 * Wrap the importing of shares into a transaction,
374 * but only 100 at a time, in order to serve memory.
375 * The allocated memory accumulates across the actions
376 * within the transaction, and for me, some 1500
377 * imported shares, the MAX_TALLOC_SIZE of 256 MB
378 * was exceeded.
380 werr = smbconf_transaction_start(conf_ctx);
381 if (!W_ERROR_IS_OK(werr)) {
382 d_printf(_("error starting transaction: %s\n"),
383 win_errstr(werr));
384 goto done;
387 for (sidx = 0; sidx < num_shares; sidx++) {
388 werr = import_process_service(c, conf_ctx,
389 services[sidx]);
390 if (!W_ERROR_IS_OK(werr)) {
391 goto cancel;
394 if (sidx % 100) {
395 continue;
398 werr = smbconf_transaction_commit(conf_ctx);
399 if (!W_ERROR_IS_OK(werr)) {
400 d_printf(_("error committing transaction: "
401 "%s\n"),
402 win_errstr(werr));
403 goto done;
405 werr = smbconf_transaction_start(conf_ctx);
406 if (!W_ERROR_IS_OK(werr)) {
407 d_printf(_("error starting transaction: %s\n"),
408 win_errstr(werr));
409 goto done;
414 werr = smbconf_transaction_commit(conf_ctx);
415 if (!W_ERROR_IS_OK(werr)) {
416 d_printf(_("error committing transaction: %s\n"),
417 win_errstr(werr));
418 } else {
419 ret = 0;
422 goto done;
424 cancel:
425 werr = smbconf_transaction_cancel(conf_ctx);
426 if (!W_ERROR_IS_OK(werr)) {
427 d_printf(_("error cancelling transaction: %s\n"),
428 win_errstr(werr));
431 done:
432 TALLOC_FREE(mem_ctx);
433 return ret;
436 static int net_conf_listshares(struct net_context *c,
437 struct smbconf_ctx *conf_ctx, int argc,
438 const char **argv)
440 WERROR werr = WERR_OK;
441 int ret = -1;
442 uint32_t count, num_shares = 0;
443 char **share_names = NULL;
444 TALLOC_CTX *mem_ctx;
446 mem_ctx = talloc_stackframe();
448 if (argc != 0 || c->display_usage) {
449 net_conf_listshares_usage(c, argc, argv);
450 goto done;
453 werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
454 &share_names);
455 if (!W_ERROR_IS_OK(werr)) {
456 goto done;
459 for (count = 0; count < num_shares; count++)
461 d_printf("%s\n", share_names[count]);
464 ret = 0;
466 done:
467 TALLOC_FREE(mem_ctx);
468 return ret;
471 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
472 int argc, const char **argv)
474 int ret = -1;
475 WERROR werr;
477 if (argc != 0 || c->display_usage) {
478 net_conf_drop_usage(c, argc, argv);
479 goto done;
482 werr = smbconf_drop(conf_ctx);
483 if (!W_ERROR_IS_OK(werr)) {
484 d_fprintf(stderr, _("Error deleting configuration: %s\n"),
485 win_errstr(werr));
486 goto done;
489 ret = 0;
491 done:
492 return ret;
495 static int net_conf_showshare(struct net_context *c,
496 struct smbconf_ctx *conf_ctx, int argc,
497 const char **argv)
499 int ret = -1;
500 WERROR werr = WERR_OK;
501 const char *sharename = NULL;
502 TALLOC_CTX *mem_ctx;
503 uint32_t count;
504 struct smbconf_service *service = NULL;
506 mem_ctx = talloc_stackframe();
508 if (argc != 1 || c->display_usage) {
509 net_conf_showshare_usage(c, argc, argv);
510 goto done;
513 sharename = talloc_strdup(mem_ctx, argv[0]);
514 if (sharename == NULL) {
515 d_printf("error: out of memory!\n");
516 goto done;
519 werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
520 if (!W_ERROR_IS_OK(werr)) {
521 d_printf(_("error getting share parameters: %s\n"),
522 win_errstr(werr));
523 goto done;
526 d_printf("[%s]\n", service->name);
528 for (count = 0; count < service->num_params; count++) {
529 d_printf("\t%s = %s\n", service->param_names[count],
530 service->param_values[count]);
533 ret = 0;
535 done:
536 TALLOC_FREE(mem_ctx);
537 return ret;
541 * Add a share, with a couple of standard parameters, partly optional.
543 * This is a high level utility function of the net conf utility,
544 * not a direct frontend to the smbconf API.
546 static int net_conf_addshare(struct net_context *c,
547 struct smbconf_ctx *conf_ctx, int argc,
548 const char **argv)
550 int ret = -1;
551 WERROR werr = WERR_OK;
552 char *sharename = NULL;
553 const char *path = NULL;
554 const char *comment = NULL;
555 const char *guest_ok = "no";
556 const char *writeable = "no";
557 SMB_STRUCT_STAT sbuf;
558 TALLOC_CTX *mem_ctx = talloc_stackframe();
560 if (c->display_usage) {
561 net_conf_addshare_usage(c, argc, argv);
562 ret = 0;
563 goto done;
566 switch (argc) {
567 case 0:
568 case 1:
569 default:
570 net_conf_addshare_usage(c, argc, argv);
571 goto done;
572 case 5:
573 comment = argv[4];
574 case 4:
575 if (!strnequal(argv[3], "guest_ok=", 9)) {
576 net_conf_addshare_usage(c, argc, argv);
577 goto done;
579 switch (argv[3][9]) {
580 case 'y':
581 case 'Y':
582 guest_ok = "yes";
583 break;
584 case 'n':
585 case 'N':
586 guest_ok = "no";
587 break;
588 default:
589 net_conf_addshare_usage(c, argc, argv);
590 goto done;
592 case 3:
593 if (!strnequal(argv[2], "writeable=", 10)) {
594 net_conf_addshare_usage(c, argc, argv);
595 goto done;
597 switch (argv[2][10]) {
598 case 'y':
599 case 'Y':
600 writeable = "yes";
601 break;
602 case 'n':
603 case 'N':
604 writeable = "no";
605 break;
606 default:
607 net_conf_addshare_usage(c, argc, argv);
608 goto done;
610 case 2:
611 path = argv[1];
612 sharename = talloc_strdup(mem_ctx, argv[0]);
613 if (sharename == NULL) {
614 d_printf(_("error: out of memory!\n"));
615 goto done;
618 break;
622 * validate arguments
625 /* validate share name */
627 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
628 strlen(sharename)))
630 d_fprintf(stderr, _("ERROR: share name %s contains "
631 "invalid characters (any of %s)\n"),
632 sharename, INVALID_SHARENAME_CHARS);
633 goto done;
636 if (strequal(sharename, GLOBAL_NAME)) {
637 d_fprintf(stderr,
638 _("ERROR: 'global' is not a valid share name.\n"));
639 goto done;
642 if (smbconf_share_exists(conf_ctx, sharename)) {
643 d_fprintf(stderr, _("ERROR: share %s already exists.\n"),
644 sharename);
645 goto done;
648 /* validate path */
650 if (path[0] != '/') {
651 d_fprintf(stderr,
652 _("Error: path '%s' is not an absolute path.\n"),
653 path);
654 goto done;
657 if (sys_stat(path, &sbuf) != 0) {
658 d_fprintf(stderr,
659 _("ERROR: cannot stat path '%s' to ensure "
660 "this is a directory.\n"
661 "Error was '%s'.\n"),
662 path, strerror(errno));
663 goto done;
666 if (!S_ISDIR(sbuf.st_ex_mode)) {
667 d_fprintf(stderr,
668 _("ERROR: path '%s' is not a directory.\n"),
669 path);
670 goto done;
674 * create the share
677 werr = smbconf_create_share(conf_ctx, sharename);
678 if (!W_ERROR_IS_OK(werr)) {
679 d_fprintf(stderr, _("Error creating share %s: %s\n"),
680 sharename, win_errstr(werr));
681 goto done;
685 * fill the share with parameters
688 werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
689 if (!W_ERROR_IS_OK(werr)) {
690 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
691 "path", win_errstr(werr));
692 goto done;
695 if (comment != NULL) {
696 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
697 comment);
698 if (!W_ERROR_IS_OK(werr)) {
699 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
700 "comment", win_errstr(werr));
701 goto done;
705 werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
706 if (!W_ERROR_IS_OK(werr)) {
707 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
708 "'guest ok'", win_errstr(werr));
709 goto done;
712 werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
713 writeable);
714 if (!W_ERROR_IS_OK(werr)) {
715 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
716 "writeable", win_errstr(werr));
717 goto done;
720 ret = 0;
722 done:
723 TALLOC_FREE(mem_ctx);
724 return ret;
727 static int net_conf_delshare(struct net_context *c,
728 struct smbconf_ctx *conf_ctx, int argc,
729 const char **argv)
731 int ret = -1;
732 const char *sharename = NULL;
733 WERROR werr = WERR_OK;
734 TALLOC_CTX *mem_ctx = talloc_stackframe();
736 if (argc != 1 || c->display_usage) {
737 net_conf_delshare_usage(c, argc, argv);
738 goto done;
740 sharename = talloc_strdup(mem_ctx, argv[0]);
741 if (sharename == NULL) {
742 d_printf(_("error: out of memory!\n"));
743 goto done;
746 werr = smbconf_delete_share(conf_ctx, sharename);
747 if (!W_ERROR_IS_OK(werr)) {
748 d_fprintf(stderr, _("Error deleting share %s: %s\n"),
749 sharename, win_errstr(werr));
750 goto done;
753 ret = 0;
754 done:
755 TALLOC_FREE(mem_ctx);
756 return ret;
759 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
760 int argc, const char **argv)
762 int ret = -1;
763 WERROR werr = WERR_OK;
764 char *service = NULL;
765 char *param = NULL;
766 const char *value_str = NULL;
767 TALLOC_CTX *mem_ctx = talloc_stackframe();
769 if (argc != 3 || c->display_usage) {
770 net_conf_setparm_usage(c, argc, argv);
771 goto done;
774 * NULL service name means "dangling parameters" to libsmbconf.
775 * We use the empty string from the command line for this purpose.
777 if (strlen(argv[0]) != 0) {
778 service = talloc_strdup(mem_ctx, argv[0]);
779 if (service == NULL) {
780 d_printf(_("error: out of memory!\n"));
781 goto done;
784 param = strlower_talloc(mem_ctx, argv[1]);
785 if (param == NULL) {
786 d_printf(_("error: out of memory!\n"));
787 goto done;
789 value_str = argv[2];
791 werr = smbconf_transaction_start(conf_ctx);
792 if (!W_ERROR_IS_OK(werr)) {
793 d_printf(_("error starting transaction: %s\n"),
794 win_errstr(werr));
795 goto done;
798 if (!smbconf_share_exists(conf_ctx, service)) {
799 werr = smbconf_create_share(conf_ctx, service);
800 if (!W_ERROR_IS_OK(werr)) {
801 d_fprintf(stderr, _("Error creating share '%s': %s\n"),
802 service, win_errstr(werr));
803 goto cancel;
807 werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
809 if (!W_ERROR_IS_OK(werr)) {
810 d_fprintf(stderr, _("Error setting value '%s': %s\n"),
811 param, win_errstr(werr));
812 goto cancel;
815 werr = smbconf_transaction_commit(conf_ctx);
816 if (!W_ERROR_IS_OK(werr)) {
817 d_printf(_("error committing transaction: %s\n"),
818 win_errstr(werr));
819 } else {
820 ret = 0;
823 goto done;
825 cancel:
826 werr = smbconf_transaction_cancel(conf_ctx);
827 if (!W_ERROR_IS_OK(werr)) {
828 d_printf(_("error cancelling transaction: %s\n"),
829 win_errstr(werr));
832 done:
833 TALLOC_FREE(mem_ctx);
834 return ret;
837 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
838 int argc, const char **argv)
840 int ret = -1;
841 WERROR werr = WERR_OK;
842 char *service = NULL;
843 char *param = NULL;
844 char *valstr = NULL;
845 TALLOC_CTX *mem_ctx;
847 mem_ctx = talloc_stackframe();
849 if (argc != 2 || c->display_usage) {
850 net_conf_getparm_usage(c, argc, argv);
851 goto done;
854 * NULL service name means "dangling parameters" to libsmbconf.
855 * We use the empty string from the command line for this purpose.
857 if (strlen(argv[0]) != 0) {
858 service = talloc_strdup(mem_ctx, argv[0]);
859 if (service == NULL) {
860 d_printf(_("error: out of memory!\n"));
861 goto done;
864 param = strlower_talloc(mem_ctx, argv[1]);
865 if (param == NULL) {
866 d_printf(_("error: out of memory!\n"));
867 goto done;
870 werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
872 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
873 d_fprintf(stderr,
874 _("Error: given service '%s' does not exist.\n"),
875 service);
876 goto done;
877 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
878 d_fprintf(stderr,
879 _("Error: given parameter '%s' is not set.\n"),
880 param);
881 goto done;
882 } else if (!W_ERROR_IS_OK(werr)) {
883 d_fprintf(stderr, _("Error getting value '%s': %s.\n"),
884 param, win_errstr(werr));
885 goto done;
888 d_printf("%s\n", valstr);
890 ret = 0;
891 done:
892 TALLOC_FREE(mem_ctx);
893 return ret;
896 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
897 int argc, const char **argv)
899 int ret = -1;
900 WERROR werr = WERR_OK;
901 char *service = NULL;
902 char *param = NULL;
903 TALLOC_CTX *mem_ctx = talloc_stackframe();
905 if (argc != 2 || c->display_usage) {
906 net_conf_delparm_usage(c, argc, argv);
907 goto done;
910 * NULL service name means "dangling parameters" to libsmbconf.
911 * We use the empty string from the command line for this purpose.
913 if (strlen(argv[0]) != 0) {
914 service = talloc_strdup(mem_ctx, argv[0]);
915 if (service == NULL) {
916 d_printf(_("error: out of memory!\n"));
917 goto done;
920 param = strlower_talloc(mem_ctx, argv[1]);
921 if (param == NULL) {
922 d_printf("error: out of memory!\n");
923 goto done;
926 werr = smbconf_delete_parameter(conf_ctx, service, param);
928 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
929 d_fprintf(stderr,
930 _("Error: given service '%s' does not exist.\n"),
931 service);
932 goto done;
933 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
934 d_fprintf(stderr,
935 _("Error: given parameter '%s' is not set.\n"),
936 param);
937 goto done;
938 } else if (!W_ERROR_IS_OK(werr)) {
939 d_fprintf(stderr, _("Error deleting value '%s': %s.\n"),
940 param, win_errstr(werr));
941 goto done;
944 ret = 0;
946 done:
947 TALLOC_FREE(mem_ctx);
948 return ret;
951 static int net_conf_getincludes(struct net_context *c,
952 struct smbconf_ctx *conf_ctx,
953 int argc, const char **argv)
955 WERROR werr;
956 uint32_t num_includes;
957 uint32_t count;
958 char *service;
959 char **includes = NULL;
960 int ret = -1;
961 TALLOC_CTX *mem_ctx = talloc_stackframe();
963 if (argc != 1 || c->display_usage) {
964 net_conf_getincludes_usage(c, argc, argv);
965 goto done;
968 service = talloc_strdup(mem_ctx, argv[0]);
969 if (service == NULL) {
970 d_printf(_("error: out of memory!\n"));
971 goto done;
974 werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
975 &num_includes, &includes);
976 if (!W_ERROR_IS_OK(werr)) {
977 d_printf(_("error getting includes: %s\n"), win_errstr(werr));
978 goto done;
981 for (count = 0; count < num_includes; count++) {
982 d_printf("include = %s\n", includes[count]);
985 ret = 0;
987 done:
988 TALLOC_FREE(mem_ctx);
989 return ret;
992 static int net_conf_setincludes(struct net_context *c,
993 struct smbconf_ctx *conf_ctx,
994 int argc, const char **argv)
996 WERROR werr;
997 char *service;
998 uint32_t num_includes;
999 const char **includes;
1000 int ret = -1;
1001 TALLOC_CTX *mem_ctx = talloc_stackframe();
1003 if (argc < 1 || c->display_usage) {
1004 net_conf_setincludes_usage(c, argc, argv);
1005 goto done;
1008 service = talloc_strdup(mem_ctx, argv[0]);
1009 if (service == NULL) {
1010 d_printf(_("error: out of memory!\n"));
1011 goto done;
1014 num_includes = argc - 1;
1015 if (num_includes == 0) {
1016 includes = NULL;
1017 } else {
1018 includes = argv + 1;
1021 werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
1022 if (!W_ERROR_IS_OK(werr)) {
1023 d_printf(_("error setting includes: %s\n"), win_errstr(werr));
1024 goto done;
1027 ret = 0;
1029 done:
1030 TALLOC_FREE(mem_ctx);
1031 return ret;
1034 static int net_conf_delincludes(struct net_context *c,
1035 struct smbconf_ctx *conf_ctx,
1036 int argc, const char **argv)
1038 WERROR werr;
1039 char *service;
1040 int ret = -1;
1041 TALLOC_CTX *mem_ctx = talloc_stackframe();
1043 if (argc != 1 || c->display_usage) {
1044 net_conf_delincludes_usage(c, argc, argv);
1045 goto done;
1048 service = talloc_strdup(mem_ctx, argv[0]);
1049 if (service == NULL) {
1050 d_printf(_("error: out of memory!\n"));
1051 goto done;
1054 werr = smbconf_delete_includes(conf_ctx, service);
1055 if (!W_ERROR_IS_OK(werr)) {
1056 d_printf(_("error deleting includes: %s\n"), win_errstr(werr));
1057 goto done;
1060 ret = 0;
1062 done:
1063 TALLOC_FREE(mem_ctx);
1064 return ret;
1068 /**********************************************************************
1070 * Wrapper and net_conf_run_function mechanism.
1072 **********************************************************************/
1075 * Wrapper function to call the main conf functions.
1076 * The wrapper calls handles opening and closing of the
1077 * configuration.
1079 static int net_conf_wrap_function(struct net_context *c,
1080 int (*fn)(struct net_context *,
1081 struct smbconf_ctx *,
1082 int, const char **),
1083 int argc, const char **argv)
1085 WERROR werr;
1086 TALLOC_CTX *mem_ctx = talloc_stackframe();
1087 struct smbconf_ctx *conf_ctx;
1088 int ret = -1;
1090 werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1092 if (!W_ERROR_IS_OK(werr)) {
1093 return -1;
1096 ret = fn(c, conf_ctx, argc, argv);
1098 smbconf_shutdown(conf_ctx);
1100 return ret;
1104 * We need a functable struct of our own, because the
1105 * functions are called through a wrapper that handles
1106 * the opening and closing of the configuration, and so on.
1108 struct conf_functable {
1109 const char *funcname;
1110 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1111 const char **argv);
1112 int valid_transports;
1113 const char *description;
1114 const char *usage;
1118 * This imitates net_run_function but calls the main functions
1119 * through the wrapper net_conf_wrap_function().
1121 static int net_conf_run_function(struct net_context *c, int argc,
1122 const char **argv, const char *whoami,
1123 struct conf_functable *table)
1125 int i;
1127 if (argc != 0) {
1128 for (i=0; table[i].funcname; i++) {
1129 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1130 return net_conf_wrap_function(c, table[i].fn,
1131 argc-1,
1132 argv+1);
1136 d_printf(_("Usage:\n"));
1137 for (i=0; table[i].funcname; i++) {
1138 if (c->display_usage == false)
1139 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1140 table[i].description);
1141 else
1142 d_printf("%s\n", table[i].usage);
1145 return c->display_usage?0:-1;
1149 * Entry-point for all the CONF functions.
1152 int net_conf(struct net_context *c, int argc, const char **argv)
1154 int ret = -1;
1155 struct conf_functable func_table[] = {
1157 "list",
1158 net_conf_list,
1159 NET_TRANSPORT_LOCAL,
1160 N_("Dump the complete configuration in smb.conf like "
1161 "format."),
1162 N_("net conf list\n"
1163 " Dump the complete configuration in smb.conf "
1164 "like format.")
1168 "import",
1169 net_conf_import,
1170 NET_TRANSPORT_LOCAL,
1171 N_("Import configuration from file in smb.conf "
1172 "format."),
1173 N_("net conf import\n"
1174 " Import configuration from file in smb.conf "
1175 "format.")
1178 "listshares",
1179 net_conf_listshares,
1180 NET_TRANSPORT_LOCAL,
1181 N_("List the share names."),
1182 N_("net conf listshares\n"
1183 " List the share names.")
1186 "drop",
1187 net_conf_drop,
1188 NET_TRANSPORT_LOCAL,
1189 N_("Delete the complete configuration."),
1190 N_("net conf drop\n"
1191 " Delete the complete configuration.")
1194 "showshare",
1195 net_conf_showshare,
1196 NET_TRANSPORT_LOCAL,
1197 N_("Show the definition of a share."),
1198 N_("net conf showshare\n"
1199 " Show the definition of a share.")
1202 "addshare",
1203 net_conf_addshare,
1204 NET_TRANSPORT_LOCAL,
1205 N_("Create a new share."),
1206 N_("net conf addshare\n"
1207 " Create a new share.")
1210 "delshare",
1211 net_conf_delshare,
1212 NET_TRANSPORT_LOCAL,
1213 N_("Delete a share."),
1214 N_("net conf delshare\n"
1215 " Delete a share.")
1218 "setparm",
1219 net_conf_setparm,
1220 NET_TRANSPORT_LOCAL,
1221 N_("Store a parameter."),
1222 N_("net conf setparm\n"
1223 " Store a parameter.")
1226 "getparm",
1227 net_conf_getparm,
1228 NET_TRANSPORT_LOCAL,
1229 N_("Retrieve the value of a parameter."),
1230 N_("net conf getparm\n"
1231 " Retrieve the value of a parameter.")
1234 "delparm",
1235 net_conf_delparm,
1236 NET_TRANSPORT_LOCAL,
1237 N_("Delete a parameter."),
1238 N_("net conf delparm\n"
1239 " Delete a parameter.")
1242 "getincludes",
1243 net_conf_getincludes,
1244 NET_TRANSPORT_LOCAL,
1245 N_("Show the includes of a share definition."),
1246 N_("net conf getincludes\n"
1247 " Show the includes of a share definition.")
1250 "setincludes",
1251 net_conf_setincludes,
1252 NET_TRANSPORT_LOCAL,
1253 N_("Set includes for a share."),
1254 N_("net conf setincludes\n"
1255 " Set includes for a share.")
1258 "delincludes",
1259 net_conf_delincludes,
1260 NET_TRANSPORT_LOCAL,
1261 N_("Delete includes from a share definition."),
1262 N_("net conf setincludes\n"
1263 " Delete includes from a share definition.")
1265 {NULL, NULL, 0, NULL, NULL}
1268 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1270 return ret;