libsmbconf: Convert smbconf_create_share() to smbErr.
[Samba.git] / source3 / utils / net_conf.c
blob3b6005a2b9599ef45c96c4aded165ea3a92ef732
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 WERROR import_process_service(struct net_context *c,
178 struct smbconf_ctx *conf_ctx,
179 struct smbconf_service *service)
181 uint32_t idx;
182 WERROR werr = WERR_OK;
183 sbcErr err;
184 uint32_t num_includes = 0;
185 char **includes = NULL;
186 TALLOC_CTX *mem_ctx = talloc_stackframe();
188 if (c->opt_testmode) {
189 const char *indent = "";
190 if (service->name != NULL) {
191 d_printf("[%s]\n", service->name);
192 indent = "\t";
194 for (idx = 0; idx < service->num_params; idx++) {
195 d_printf("%s%s = %s\n", indent,
196 service->param_names[idx],
197 service->param_values[idx]);
199 d_printf("\n");
200 goto done;
203 if (smbconf_share_exists(conf_ctx, service->name)) {
204 werr = smbconf_delete_share(conf_ctx, service->name);
205 if (!W_ERROR_IS_OK(werr)) {
206 goto done;
209 err = smbconf_create_share(conf_ctx, service->name);
210 if (!SBC_ERROR_IS_OK(err)) {
211 werr = WERR_GENERAL_FAILURE;
212 goto done;
215 for (idx = 0; idx < service->num_params; idx ++) {
216 if (strequal(service->param_names[idx], "include")) {
217 includes = TALLOC_REALLOC_ARRAY(mem_ctx,
218 includes,
219 char *,
220 num_includes+1);
221 if (includes == NULL) {
222 werr = WERR_NOMEM;
223 goto done;
225 includes[num_includes] = talloc_strdup(includes,
226 service->param_values[idx]);
227 if (includes[num_includes] == NULL) {
228 werr = WERR_NOMEM;
229 goto done;
231 num_includes++;
232 } else {
233 werr = smbconf_set_parameter(conf_ctx,
234 service->name,
235 service->param_names[idx],
236 service->param_values[idx]);
237 if (!W_ERROR_IS_OK(werr)) {
238 d_fprintf(stderr,
239 _("Error in section [%s], parameter \"%s\": %s\n"),
240 service->name, service->param_names[idx],
241 win_errstr(werr));
242 goto done;
247 werr = smbconf_set_includes(conf_ctx, service->name, num_includes,
248 (const char **)includes);
250 done:
251 TALLOC_FREE(mem_ctx);
252 return werr;
256 /**********************************************************************
258 * the main conf functions
260 **********************************************************************/
262 static int net_conf_list(struct net_context *c, struct smbconf_ctx *conf_ctx,
263 int argc, const char **argv)
265 WERROR werr = WERR_OK;
266 int ret = -1;
267 TALLOC_CTX *mem_ctx;
268 uint32_t num_shares;
269 uint32_t share_count, param_count;
270 struct smbconf_service **shares = NULL;
272 mem_ctx = talloc_stackframe();
274 if (argc != 0 || c->display_usage) {
275 net_conf_list_usage(c, argc, argv);
276 goto done;
279 werr = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &shares);
280 if (!W_ERROR_IS_OK(werr)) {
281 d_fprintf(stderr, _("Error getting config: %s\n"),
282 win_errstr(werr));
283 goto done;
286 for (share_count = 0; share_count < num_shares; share_count++) {
287 const char *indent = "";
288 if (shares[share_count]->name != NULL) {
289 d_printf("[%s]\n", shares[share_count]->name);
290 indent = "\t";
292 for (param_count = 0;
293 param_count < shares[share_count]->num_params;
294 param_count++)
296 d_printf("%s%s = %s\n",
297 indent,
298 shares[share_count]->param_names[param_count],
299 shares[share_count]->param_values[param_count]);
301 d_printf("\n");
304 ret = 0;
306 done:
307 TALLOC_FREE(mem_ctx);
308 return ret;
311 static int net_conf_import(struct net_context *c, struct smbconf_ctx *conf_ctx,
312 int argc, const char **argv)
314 int ret = -1;
315 const char *filename = NULL;
316 const char *servicename = NULL;
317 char *conf_source = NULL;
318 TALLOC_CTX *mem_ctx;
319 struct smbconf_ctx *txt_ctx;
320 WERROR werr;
321 sbcErr err;
323 if (c->display_usage)
324 return net_conf_import_usage(c, argc, argv);
326 mem_ctx = talloc_stackframe();
328 switch (argc) {
329 case 0:
330 default:
331 net_conf_import_usage(c, argc, argv);
332 goto done;
333 case 2:
334 servicename = talloc_strdup(mem_ctx, argv[1]);
335 if (servicename == NULL) {
336 d_printf(_("error: out of memory!\n"));
337 goto done;
339 case 1:
340 filename = argv[0];
341 break;
344 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
345 filename));
347 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
348 if (conf_source == NULL) {
349 d_printf(_("error: out of memory!\n"));
350 goto done;
353 err = smbconf_init(mem_ctx, &txt_ctx, conf_source);
354 if (!SBC_ERROR_IS_OK(err)) {
355 d_printf(_("error loading file '%s': %s\n"), filename,
356 sbcErrorString(err));
357 werr = WERR_NO_SUCH_SERVICE;
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 werr = smbconf_get_share(txt_ctx, mem_ctx,
370 servicename,
371 &service);
372 if (!W_ERROR_IS_OK(werr)) {
373 goto cancel;
376 werr = smbconf_transaction_start(conf_ctx);
377 if (!W_ERROR_IS_OK(werr)) {
378 d_printf(_("error starting transaction: %s\n"),
379 win_errstr(werr));
380 goto done;
383 werr = import_process_service(c, conf_ctx, service);
384 if (!W_ERROR_IS_OK(werr)) {
385 goto cancel;
387 } else {
388 struct smbconf_service **services = NULL;
389 uint32_t num_shares, sidx;
391 werr = smbconf_get_config(txt_ctx, mem_ctx,
392 &num_shares,
393 &services);
394 if (!W_ERROR_IS_OK(werr)) {
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 werr = smbconf_transaction_start(conf_ctx);
412 if (!W_ERROR_IS_OK(werr)) {
413 d_printf(_("error starting transaction: %s\n"),
414 win_errstr(werr));
415 goto done;
418 for (sidx = 0; sidx < num_shares; sidx++) {
419 werr = import_process_service(c, conf_ctx,
420 services[sidx]);
421 if (!W_ERROR_IS_OK(werr)) {
422 goto cancel;
425 if (sidx % 100) {
426 continue;
429 werr = smbconf_transaction_commit(conf_ctx);
430 if (!W_ERROR_IS_OK(werr)) {
431 d_printf(_("error committing transaction: "
432 "%s\n"),
433 win_errstr(werr));
434 goto done;
436 werr = smbconf_transaction_start(conf_ctx);
437 if (!W_ERROR_IS_OK(werr)) {
438 d_printf(_("error starting transaction: %s\n"),
439 win_errstr(werr));
440 goto done;
445 werr = smbconf_transaction_commit(conf_ctx);
446 if (!W_ERROR_IS_OK(werr)) {
447 d_printf(_("error committing transaction: %s\n"),
448 win_errstr(werr));
449 } else {
450 ret = 0;
453 goto done;
455 cancel:
456 werr = smbconf_transaction_cancel(conf_ctx);
457 if (!W_ERROR_IS_OK(werr)) {
458 d_printf(_("error cancelling transaction: %s\n"),
459 win_errstr(werr));
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 WERROR werr = WERR_OK;
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 werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
551 if (!W_ERROR_IS_OK(werr)) {
552 d_printf(_("error getting share parameters: %s\n"),
553 win_errstr(werr));
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 WERROR werr = WERR_OK;
583 sbcErr err;
584 char *sharename = NULL;
585 const char *path = NULL;
586 const char *comment = NULL;
587 const char *guest_ok = "no";
588 const char *writeable = "no";
589 SMB_STRUCT_STAT sbuf;
590 TALLOC_CTX *mem_ctx = talloc_stackframe();
592 if (c->display_usage) {
593 net_conf_addshare_usage(c, argc, argv);
594 ret = 0;
595 goto done;
598 switch (argc) {
599 case 0:
600 case 1:
601 default:
602 net_conf_addshare_usage(c, argc, argv);
603 goto done;
604 case 5:
605 comment = argv[4];
606 case 4:
607 if (!strnequal(argv[3], "guest_ok=", 9)) {
608 net_conf_addshare_usage(c, argc, argv);
609 goto done;
611 switch (argv[3][9]) {
612 case 'y':
613 case 'Y':
614 guest_ok = "yes";
615 break;
616 case 'n':
617 case 'N':
618 guest_ok = "no";
619 break;
620 default:
621 net_conf_addshare_usage(c, argc, argv);
622 goto done;
624 case 3:
625 if (!strnequal(argv[2], "writeable=", 10)) {
626 net_conf_addshare_usage(c, argc, argv);
627 goto done;
629 switch (argv[2][10]) {
630 case 'y':
631 case 'Y':
632 writeable = "yes";
633 break;
634 case 'n':
635 case 'N':
636 writeable = "no";
637 break;
638 default:
639 net_conf_addshare_usage(c, argc, argv);
640 goto done;
642 case 2:
643 path = argv[1];
644 sharename = talloc_strdup(mem_ctx, argv[0]);
645 if (sharename == NULL) {
646 d_printf(_("error: out of memory!\n"));
647 goto done;
650 break;
654 * validate arguments
657 /* validate share name */
659 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
660 strlen(sharename)))
662 d_fprintf(stderr, _("ERROR: share name %s contains "
663 "invalid characters (any of %s)\n"),
664 sharename, INVALID_SHARENAME_CHARS);
665 goto done;
668 if (strequal(sharename, GLOBAL_NAME)) {
669 d_fprintf(stderr,
670 _("ERROR: 'global' is not a valid share name.\n"));
671 goto done;
674 if (smbconf_share_exists(conf_ctx, sharename)) {
675 d_fprintf(stderr, _("ERROR: share %s already exists.\n"),
676 sharename);
677 goto done;
680 /* validate path */
682 if (path[0] != '/') {
683 d_fprintf(stderr,
684 _("Error: path '%s' is not an absolute path.\n"),
685 path);
686 goto done;
689 if (sys_stat(path, &sbuf, false) != 0) {
690 d_fprintf(stderr,
691 _("ERROR: cannot stat path '%s' to ensure "
692 "this is a directory.\n"
693 "Error was '%s'.\n"),
694 path, strerror(errno));
695 goto done;
698 if (!S_ISDIR(sbuf.st_ex_mode)) {
699 d_fprintf(stderr,
700 _("ERROR: path '%s' is not a directory.\n"),
701 path);
702 goto done;
706 * start a transaction
709 werr = smbconf_transaction_start(conf_ctx);
710 if (!W_ERROR_IS_OK(werr)) {
711 d_printf("error starting transaction: %s\n",
712 win_errstr(werr));
713 goto done;
717 * create the share
720 err = smbconf_create_share(conf_ctx, sharename);
721 if (!SBC_ERROR_IS_OK(err)) {
722 d_fprintf(stderr, _("Error creating share %s: %s\n"),
723 sharename, sbcErrorString(err));
724 goto cancel;
728 * fill the share with parameters
731 werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
732 if (!W_ERROR_IS_OK(werr)) {
733 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
734 "path", win_errstr(werr));
735 goto cancel;
738 if (comment != NULL) {
739 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
740 comment);
741 if (!W_ERROR_IS_OK(werr)) {
742 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
743 "comment", win_errstr(werr));
744 goto cancel;
748 werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
749 if (!W_ERROR_IS_OK(werr)) {
750 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
751 "'guest ok'", win_errstr(werr));
752 goto cancel;
755 werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
756 writeable);
757 if (!W_ERROR_IS_OK(werr)) {
758 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
759 "writeable", win_errstr(werr));
760 goto cancel;
764 * commit the whole thing
767 werr = smbconf_transaction_commit(conf_ctx);
768 if (!W_ERROR_IS_OK(werr)) {
769 d_printf("error committing transaction: %s\n",
770 win_errstr(werr));
771 } else {
772 ret = 0;
775 goto done;
777 cancel:
778 werr = smbconf_transaction_cancel(conf_ctx);
779 if (!W_ERROR_IS_OK(werr)) {
780 d_printf("error cancelling transaction: %s\n",
781 win_errstr(werr));
784 done:
785 TALLOC_FREE(mem_ctx);
786 return ret;
789 static int net_conf_delshare(struct net_context *c,
790 struct smbconf_ctx *conf_ctx, int argc,
791 const char **argv)
793 int ret = -1;
794 const char *sharename = NULL;
795 WERROR werr = WERR_OK;
796 TALLOC_CTX *mem_ctx = talloc_stackframe();
798 if (argc != 1 || c->display_usage) {
799 net_conf_delshare_usage(c, argc, argv);
800 goto done;
802 sharename = talloc_strdup(mem_ctx, argv[0]);
803 if (sharename == NULL) {
804 d_printf(_("error: out of memory!\n"));
805 goto done;
808 werr = smbconf_delete_share(conf_ctx, sharename);
809 if (!W_ERROR_IS_OK(werr)) {
810 d_fprintf(stderr, _("Error deleting share %s: %s\n"),
811 sharename, win_errstr(werr));
812 goto done;
815 ret = 0;
816 done:
817 TALLOC_FREE(mem_ctx);
818 return ret;
821 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
822 int argc, const char **argv)
824 int ret = -1;
825 WERROR werr = WERR_OK;
826 sbcErr err;
827 char *service = NULL;
828 char *param = NULL;
829 const char *value_str = NULL;
830 TALLOC_CTX *mem_ctx = talloc_stackframe();
832 if (argc != 3 || c->display_usage) {
833 net_conf_setparm_usage(c, argc, argv);
834 goto done;
837 * NULL service name means "dangling parameters" to libsmbconf.
838 * We use the empty string from the command line for this purpose.
840 if (strlen(argv[0]) != 0) {
841 service = talloc_strdup(mem_ctx, argv[0]);
842 if (service == NULL) {
843 d_printf(_("error: out of memory!\n"));
844 goto done;
847 param = strlower_talloc(mem_ctx, argv[1]);
848 if (param == NULL) {
849 d_printf(_("error: out of memory!\n"));
850 goto done;
852 value_str = argv[2];
854 werr = smbconf_transaction_start(conf_ctx);
855 if (!W_ERROR_IS_OK(werr)) {
856 d_printf(_("error starting transaction: %s\n"),
857 win_errstr(werr));
858 goto done;
861 if (!smbconf_share_exists(conf_ctx, service)) {
862 err = smbconf_create_share(conf_ctx, service);
863 if (!SBC_ERROR_IS_OK(err)) {
864 d_fprintf(stderr, _("Error creating share '%s': %s\n"),
865 service, sbcErrorString(err));
866 goto cancel;
870 werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
872 if (!W_ERROR_IS_OK(werr)) {
873 d_fprintf(stderr, _("Error setting value '%s': %s\n"),
874 param, win_errstr(werr));
875 goto cancel;
878 werr = smbconf_transaction_commit(conf_ctx);
879 if (!W_ERROR_IS_OK(werr)) {
880 d_printf(_("error committing transaction: %s\n"),
881 win_errstr(werr));
882 } else {
883 ret = 0;
886 goto done;
888 cancel:
889 werr = smbconf_transaction_cancel(conf_ctx);
890 if (!W_ERROR_IS_OK(werr)) {
891 d_printf(_("error cancelling transaction: %s\n"),
892 win_errstr(werr));
895 done:
896 TALLOC_FREE(mem_ctx);
897 return ret;
900 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
901 int argc, const char **argv)
903 int ret = -1;
904 WERROR werr = WERR_OK;
905 char *service = NULL;
906 char *param = NULL;
907 char *valstr = NULL;
908 TALLOC_CTX *mem_ctx;
910 mem_ctx = talloc_stackframe();
912 if (argc != 2 || c->display_usage) {
913 net_conf_getparm_usage(c, argc, argv);
914 goto done;
917 * NULL service name means "dangling parameters" to libsmbconf.
918 * We use the empty string from the command line for this purpose.
920 if (strlen(argv[0]) != 0) {
921 service = talloc_strdup(mem_ctx, argv[0]);
922 if (service == NULL) {
923 d_printf(_("error: out of memory!\n"));
924 goto done;
927 param = strlower_talloc(mem_ctx, argv[1]);
928 if (param == NULL) {
929 d_printf(_("error: out of memory!\n"));
930 goto done;
933 werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
935 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
936 d_fprintf(stderr,
937 _("Error: given service '%s' does not exist.\n"),
938 service);
939 goto done;
940 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
941 d_fprintf(stderr,
942 _("Error: given parameter '%s' is not set.\n"),
943 param);
944 goto done;
945 } else if (!W_ERROR_IS_OK(werr)) {
946 d_fprintf(stderr, _("Error getting value '%s': %s.\n"),
947 param, win_errstr(werr));
948 goto done;
951 d_printf("%s\n", valstr);
953 ret = 0;
954 done:
955 TALLOC_FREE(mem_ctx);
956 return ret;
959 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
960 int argc, const char **argv)
962 int ret = -1;
963 WERROR werr = WERR_OK;
964 char *service = NULL;
965 char *param = NULL;
966 TALLOC_CTX *mem_ctx = talloc_stackframe();
968 if (argc != 2 || c->display_usage) {
969 net_conf_delparm_usage(c, argc, argv);
970 goto done;
973 * NULL service name means "dangling parameters" to libsmbconf.
974 * We use the empty string from the command line for this purpose.
976 if (strlen(argv[0]) != 0) {
977 service = talloc_strdup(mem_ctx, argv[0]);
978 if (service == NULL) {
979 d_printf(_("error: out of memory!\n"));
980 goto done;
983 param = strlower_talloc(mem_ctx, argv[1]);
984 if (param == NULL) {
985 d_printf("error: out of memory!\n");
986 goto done;
989 werr = smbconf_delete_parameter(conf_ctx, service, param);
991 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
992 d_fprintf(stderr,
993 _("Error: given service '%s' does not exist.\n"),
994 service);
995 goto done;
996 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
997 d_fprintf(stderr,
998 _("Error: given parameter '%s' is not set.\n"),
999 param);
1000 goto done;
1001 } else if (!W_ERROR_IS_OK(werr)) {
1002 d_fprintf(stderr, _("Error deleting value '%s': %s.\n"),
1003 param, win_errstr(werr));
1004 goto done;
1007 ret = 0;
1009 done:
1010 TALLOC_FREE(mem_ctx);
1011 return ret;
1014 static int net_conf_getincludes(struct net_context *c,
1015 struct smbconf_ctx *conf_ctx,
1016 int argc, const char **argv)
1018 WERROR werr;
1019 uint32_t num_includes;
1020 uint32_t count;
1021 char *service;
1022 char **includes = NULL;
1023 int ret = -1;
1024 TALLOC_CTX *mem_ctx = talloc_stackframe();
1026 if (argc != 1 || c->display_usage) {
1027 net_conf_getincludes_usage(c, argc, argv);
1028 goto done;
1031 service = talloc_strdup(mem_ctx, argv[0]);
1032 if (service == NULL) {
1033 d_printf(_("error: out of memory!\n"));
1034 goto done;
1037 werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
1038 &num_includes, &includes);
1039 if (!W_ERROR_IS_OK(werr)) {
1040 d_printf(_("error getting includes: %s\n"), win_errstr(werr));
1041 goto done;
1044 for (count = 0; count < num_includes; count++) {
1045 d_printf("include = %s\n", includes[count]);
1048 ret = 0;
1050 done:
1051 TALLOC_FREE(mem_ctx);
1052 return ret;
1055 static int net_conf_setincludes(struct net_context *c,
1056 struct smbconf_ctx *conf_ctx,
1057 int argc, const char **argv)
1059 WERROR werr;
1060 char *service;
1061 uint32_t num_includes;
1062 const char **includes;
1063 int ret = -1;
1064 TALLOC_CTX *mem_ctx = talloc_stackframe();
1066 if (argc < 1 || c->display_usage) {
1067 net_conf_setincludes_usage(c, argc, argv);
1068 goto done;
1071 service = talloc_strdup(mem_ctx, argv[0]);
1072 if (service == NULL) {
1073 d_printf(_("error: out of memory!\n"));
1074 goto done;
1077 num_includes = argc - 1;
1078 if (num_includes == 0) {
1079 includes = NULL;
1080 } else {
1081 includes = argv + 1;
1084 werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
1085 if (!W_ERROR_IS_OK(werr)) {
1086 d_printf(_("error setting includes: %s\n"), win_errstr(werr));
1087 goto done;
1090 ret = 0;
1092 done:
1093 TALLOC_FREE(mem_ctx);
1094 return ret;
1097 static int net_conf_delincludes(struct net_context *c,
1098 struct smbconf_ctx *conf_ctx,
1099 int argc, const char **argv)
1101 WERROR werr;
1102 char *service;
1103 int ret = -1;
1104 TALLOC_CTX *mem_ctx = talloc_stackframe();
1106 if (argc != 1 || c->display_usage) {
1107 net_conf_delincludes_usage(c, argc, argv);
1108 goto done;
1111 service = talloc_strdup(mem_ctx, argv[0]);
1112 if (service == NULL) {
1113 d_printf(_("error: out of memory!\n"));
1114 goto done;
1117 werr = smbconf_delete_includes(conf_ctx, service);
1118 if (!W_ERROR_IS_OK(werr)) {
1119 d_printf(_("error deleting includes: %s\n"), win_errstr(werr));
1120 goto done;
1123 ret = 0;
1125 done:
1126 TALLOC_FREE(mem_ctx);
1127 return ret;
1131 /**********************************************************************
1133 * Wrapper and net_conf_run_function mechanism.
1135 **********************************************************************/
1138 * Wrapper function to call the main conf functions.
1139 * The wrapper calls handles opening and closing of the
1140 * configuration.
1142 static int net_conf_wrap_function(struct net_context *c,
1143 int (*fn)(struct net_context *,
1144 struct smbconf_ctx *,
1145 int, const char **),
1146 int argc, const char **argv)
1148 sbcErr err;
1149 TALLOC_CTX *mem_ctx = talloc_stackframe();
1150 struct smbconf_ctx *conf_ctx;
1151 int ret = -1;
1153 err = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1154 if (!SBC_ERROR_IS_OK(err)) {
1155 return -1;
1158 ret = fn(c, conf_ctx, argc, argv);
1160 smbconf_shutdown(conf_ctx);
1162 return ret;
1166 * We need a functable struct of our own, because the
1167 * functions are called through a wrapper that handles
1168 * the opening and closing of the configuration, and so on.
1170 struct conf_functable {
1171 const char *funcname;
1172 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1173 const char **argv);
1174 int valid_transports;
1175 const char *description;
1176 const char *usage;
1180 * This imitates net_run_function but calls the main functions
1181 * through the wrapper net_conf_wrap_function().
1183 static int net_conf_run_function(struct net_context *c, int argc,
1184 const char **argv, const char *whoami,
1185 struct conf_functable *table)
1187 int i;
1189 if (argc != 0) {
1190 for (i=0; table[i].funcname; i++) {
1191 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1192 return net_conf_wrap_function(c, table[i].fn,
1193 argc-1,
1194 argv+1);
1198 d_printf(_("Usage:\n"));
1199 for (i=0; table[i].funcname; i++) {
1200 if (c->display_usage == false)
1201 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1202 table[i].description);
1203 else
1204 d_printf("%s\n", table[i].usage);
1207 return c->display_usage?0:-1;
1211 * Entry-point for all the CONF functions.
1214 int net_conf(struct net_context *c, int argc, const char **argv)
1216 int ret = -1;
1217 struct conf_functable func_table[] = {
1219 "list",
1220 net_conf_list,
1221 NET_TRANSPORT_LOCAL,
1222 N_("Dump the complete configuration in smb.conf like "
1223 "format."),
1224 N_("net conf list\n"
1225 " Dump the complete configuration in smb.conf "
1226 "like format.")
1230 "import",
1231 net_conf_import,
1232 NET_TRANSPORT_LOCAL,
1233 N_("Import configuration from file in smb.conf "
1234 "format."),
1235 N_("net conf import\n"
1236 " Import configuration from file in smb.conf "
1237 "format.")
1240 "listshares",
1241 net_conf_listshares,
1242 NET_TRANSPORT_LOCAL,
1243 N_("List the share names."),
1244 N_("net conf listshares\n"
1245 " List the share names.")
1248 "drop",
1249 net_conf_drop,
1250 NET_TRANSPORT_LOCAL,
1251 N_("Delete the complete configuration."),
1252 N_("net conf drop\n"
1253 " Delete the complete configuration.")
1256 "showshare",
1257 net_conf_showshare,
1258 NET_TRANSPORT_LOCAL,
1259 N_("Show the definition of a share."),
1260 N_("net conf showshare\n"
1261 " Show the definition of a share.")
1264 "addshare",
1265 net_conf_addshare,
1266 NET_TRANSPORT_LOCAL,
1267 N_("Create a new share."),
1268 N_("net conf addshare\n"
1269 " Create a new share.")
1272 "delshare",
1273 net_conf_delshare,
1274 NET_TRANSPORT_LOCAL,
1275 N_("Delete a share."),
1276 N_("net conf delshare\n"
1277 " Delete a share.")
1280 "setparm",
1281 net_conf_setparm,
1282 NET_TRANSPORT_LOCAL,
1283 N_("Store a parameter."),
1284 N_("net conf setparm\n"
1285 " Store a parameter.")
1288 "getparm",
1289 net_conf_getparm,
1290 NET_TRANSPORT_LOCAL,
1291 N_("Retrieve the value of a parameter."),
1292 N_("net conf getparm\n"
1293 " Retrieve the value of a parameter.")
1296 "delparm",
1297 net_conf_delparm,
1298 NET_TRANSPORT_LOCAL,
1299 N_("Delete a parameter."),
1300 N_("net conf delparm\n"
1301 " Delete a parameter.")
1304 "getincludes",
1305 net_conf_getincludes,
1306 NET_TRANSPORT_LOCAL,
1307 N_("Show the includes of a share definition."),
1308 N_("net conf getincludes\n"
1309 " Show the includes of a share definition.")
1312 "setincludes",
1313 net_conf_setincludes,
1314 NET_TRANSPORT_LOCAL,
1315 N_("Set includes for a share."),
1316 N_("net conf setincludes\n"
1317 " Set includes for a share.")
1320 "delincludes",
1321 net_conf_delincludes,
1322 NET_TRANSPORT_LOCAL,
1323 N_("Delete includes from a share definition."),
1324 N_("net conf setincludes\n"
1325 " Delete includes from a share definition.")
1327 {NULL, NULL, 0, NULL, NULL}
1330 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1332 return ret;