libsmbconf: Convert smbconf_get_includes() to sbcErr.
[Samba.git] / source3 / utils / net_conf.c
blobd3c39fdb74d0514c28e2e5326c3e2a2f11fbd17a
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 err = smbconf_delete_share(conf_ctx, service->name);
205 if (!SBC_ERROR_IS_OK(err)) {
206 werr = WERR_GENERAL_FAILURE;
207 goto done;
210 err = smbconf_create_share(conf_ctx, service->name);
211 if (!SBC_ERROR_IS_OK(err)) {
212 werr = WERR_GENERAL_FAILURE;
213 goto done;
216 for (idx = 0; idx < service->num_params; idx ++) {
217 if (strequal(service->param_names[idx], "include")) {
218 includes = TALLOC_REALLOC_ARRAY(mem_ctx,
219 includes,
220 char *,
221 num_includes+1);
222 if (includes == NULL) {
223 werr = WERR_NOMEM;
224 goto done;
226 includes[num_includes] = talloc_strdup(includes,
227 service->param_values[idx]);
228 if (includes[num_includes] == NULL) {
229 werr = WERR_NOMEM;
230 goto done;
232 num_includes++;
233 } else {
234 err = smbconf_set_parameter(conf_ctx,
235 service->name,
236 service->param_names[idx],
237 service->param_values[idx]);
238 if (!SBC_ERROR_IS_OK(err)) {
239 d_fprintf(stderr,
240 _("Error in section [%s], parameter \"%s\": %s\n"),
241 service->name, service->param_names[idx],
242 sbcErrorString(err));
243 werr = WERR_NOMEM;
244 goto done;
249 werr = smbconf_set_includes(conf_ctx, service->name, num_includes,
250 (const char **)includes);
252 done:
253 TALLOC_FREE(mem_ctx);
254 return werr;
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 WERROR werr = WERR_OK;
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 werr = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &shares);
282 if (!W_ERROR_IS_OK(werr)) {
283 d_fprintf(stderr, _("Error getting config: %s\n"),
284 win_errstr(werr));
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 WERROR werr;
323 sbcErr err;
325 if (c->display_usage)
326 return net_conf_import_usage(c, argc, argv);
328 mem_ctx = talloc_stackframe();
330 switch (argc) {
331 case 0:
332 default:
333 net_conf_import_usage(c, argc, argv);
334 goto done;
335 case 2:
336 servicename = talloc_strdup(mem_ctx, argv[1]);
337 if (servicename == NULL) {
338 d_printf(_("error: out of memory!\n"));
339 goto done;
341 case 1:
342 filename = argv[0];
343 break;
346 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
347 filename));
349 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
350 if (conf_source == NULL) {
351 d_printf(_("error: out of memory!\n"));
352 goto done;
355 err = smbconf_init(mem_ctx, &txt_ctx, conf_source);
356 if (!SBC_ERROR_IS_OK(err)) {
357 d_printf(_("error loading file '%s': %s\n"), filename,
358 sbcErrorString(err));
359 werr = WERR_NO_SUCH_SERVICE;
360 goto done;
363 if (c->opt_testmode) {
364 d_printf(_("\nTEST MODE - "
365 "would import the following configuration:\n\n"));
368 if (servicename != NULL) {
369 struct smbconf_service *service = NULL;
371 err = smbconf_get_share(txt_ctx, mem_ctx,
372 servicename,
373 &service);
374 if (!SBC_ERROR_IS_OK(err)) {
375 goto cancel;
378 werr = smbconf_transaction_start(conf_ctx);
379 if (!W_ERROR_IS_OK(werr)) {
380 d_printf(_("error starting transaction: %s\n"),
381 win_errstr(werr));
382 goto done;
385 werr = import_process_service(c, conf_ctx, service);
386 if (!W_ERROR_IS_OK(werr)) {
387 goto cancel;
389 } else {
390 struct smbconf_service **services = NULL;
391 uint32_t num_shares, sidx;
393 werr = smbconf_get_config(txt_ctx, mem_ctx,
394 &num_shares,
395 &services);
396 if (!W_ERROR_IS_OK(werr)) {
397 goto cancel;
399 if (!c->opt_testmode) {
400 if (!SBC_ERROR_IS_OK(smbconf_drop(conf_ctx))) {
401 goto cancel;
406 * Wrap the importing of shares into a transaction,
407 * but only 100 at a time, in order to save memory.
408 * The allocated memory accumulates across the actions
409 * within the transaction, and for me, some 1500
410 * imported shares, the MAX_TALLOC_SIZE of 256 MB
411 * was exceeded.
413 werr = smbconf_transaction_start(conf_ctx);
414 if (!W_ERROR_IS_OK(werr)) {
415 d_printf(_("error starting transaction: %s\n"),
416 win_errstr(werr));
417 goto done;
420 for (sidx = 0; sidx < num_shares; sidx++) {
421 werr = import_process_service(c, conf_ctx,
422 services[sidx]);
423 if (!W_ERROR_IS_OK(werr)) {
424 goto cancel;
427 if (sidx % 100) {
428 continue;
431 werr = smbconf_transaction_commit(conf_ctx);
432 if (!W_ERROR_IS_OK(werr)) {
433 d_printf(_("error committing transaction: "
434 "%s\n"),
435 win_errstr(werr));
436 goto done;
438 werr = smbconf_transaction_start(conf_ctx);
439 if (!W_ERROR_IS_OK(werr)) {
440 d_printf(_("error starting transaction: %s\n"),
441 win_errstr(werr));
442 goto done;
447 werr = smbconf_transaction_commit(conf_ctx);
448 if (!W_ERROR_IS_OK(werr)) {
449 d_printf(_("error committing transaction: %s\n"),
450 win_errstr(werr));
451 } else {
452 ret = 0;
455 goto done;
457 cancel:
458 werr = smbconf_transaction_cancel(conf_ctx);
459 if (!W_ERROR_IS_OK(werr)) {
460 d_printf(_("error cancelling transaction: %s\n"),
461 win_errstr(werr));
464 done:
465 TALLOC_FREE(mem_ctx);
466 return ret;
469 static int net_conf_listshares(struct net_context *c,
470 struct smbconf_ctx *conf_ctx, int argc,
471 const char **argv)
473 sbcErr err;
474 int ret = -1;
475 uint32_t count, num_shares = 0;
476 char **share_names = NULL;
477 TALLOC_CTX *mem_ctx;
479 mem_ctx = talloc_stackframe();
481 if (argc != 0 || c->display_usage) {
482 net_conf_listshares_usage(c, argc, argv);
483 goto done;
486 err = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
487 &share_names);
488 if (!SBC_ERROR_IS_OK(err)) {
489 goto done;
492 for (count = 0; count < num_shares; count++)
494 d_printf("%s\n", share_names[count]);
497 ret = 0;
499 done:
500 TALLOC_FREE(mem_ctx);
501 return ret;
504 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
505 int argc, const char **argv)
507 int ret = -1;
508 sbcErr err;
510 if (argc != 0 || c->display_usage) {
511 net_conf_drop_usage(c, argc, argv);
512 goto done;
515 err = smbconf_drop(conf_ctx);
516 if (!SBC_ERROR_IS_OK(err)) {
517 d_fprintf(stderr, _("Error deleting configuration: %s\n"),
518 sbcErrorString(err));
519 goto done;
522 ret = 0;
524 done:
525 return ret;
528 static int net_conf_showshare(struct net_context *c,
529 struct smbconf_ctx *conf_ctx, int argc,
530 const char **argv)
532 int ret = -1;
533 sbcErr err;
534 const char *sharename = NULL;
535 TALLOC_CTX *mem_ctx;
536 uint32_t count;
537 struct smbconf_service *service = NULL;
539 mem_ctx = talloc_stackframe();
541 if (argc != 1 || c->display_usage) {
542 net_conf_showshare_usage(c, argc, argv);
543 goto done;
546 sharename = talloc_strdup(mem_ctx, argv[0]);
547 if (sharename == NULL) {
548 d_printf("error: out of memory!\n");
549 goto done;
552 err = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
553 if (!SBC_ERROR_IS_OK(err)) {
554 d_printf(_("error getting share parameters: %s\n"),
555 sbcErrorString(err));
556 goto done;
559 d_printf("[%s]\n", service->name);
561 for (count = 0; count < service->num_params; count++) {
562 d_printf("\t%s = %s\n", service->param_names[count],
563 service->param_values[count]);
566 ret = 0;
568 done:
569 TALLOC_FREE(mem_ctx);
570 return ret;
574 * Add a share, with a couple of standard parameters, partly optional.
576 * This is a high level utility function of the net conf utility,
577 * not a direct frontend to the smbconf API.
579 static int net_conf_addshare(struct net_context *c,
580 struct smbconf_ctx *conf_ctx, int argc,
581 const char **argv)
583 int ret = -1;
584 WERROR werr = WERR_OK;
585 sbcErr err;
586 char *sharename = NULL;
587 const char *path = NULL;
588 const char *comment = NULL;
589 const char *guest_ok = "no";
590 const char *writeable = "no";
591 SMB_STRUCT_STAT sbuf;
592 TALLOC_CTX *mem_ctx = talloc_stackframe();
594 if (c->display_usage) {
595 net_conf_addshare_usage(c, argc, argv);
596 ret = 0;
597 goto done;
600 switch (argc) {
601 case 0:
602 case 1:
603 default:
604 net_conf_addshare_usage(c, argc, argv);
605 goto done;
606 case 5:
607 comment = argv[4];
608 case 4:
609 if (!strnequal(argv[3], "guest_ok=", 9)) {
610 net_conf_addshare_usage(c, argc, argv);
611 goto done;
613 switch (argv[3][9]) {
614 case 'y':
615 case 'Y':
616 guest_ok = "yes";
617 break;
618 case 'n':
619 case 'N':
620 guest_ok = "no";
621 break;
622 default:
623 net_conf_addshare_usage(c, argc, argv);
624 goto done;
626 case 3:
627 if (!strnequal(argv[2], "writeable=", 10)) {
628 net_conf_addshare_usage(c, argc, argv);
629 goto done;
631 switch (argv[2][10]) {
632 case 'y':
633 case 'Y':
634 writeable = "yes";
635 break;
636 case 'n':
637 case 'N':
638 writeable = "no";
639 break;
640 default:
641 net_conf_addshare_usage(c, argc, argv);
642 goto done;
644 case 2:
645 path = argv[1];
646 sharename = talloc_strdup(mem_ctx, argv[0]);
647 if (sharename == NULL) {
648 d_printf(_("error: out of memory!\n"));
649 goto done;
652 break;
656 * validate arguments
659 /* validate share name */
661 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
662 strlen(sharename)))
664 d_fprintf(stderr, _("ERROR: share name %s contains "
665 "invalid characters (any of %s)\n"),
666 sharename, INVALID_SHARENAME_CHARS);
667 goto done;
670 if (strequal(sharename, GLOBAL_NAME)) {
671 d_fprintf(stderr,
672 _("ERROR: 'global' is not a valid share name.\n"));
673 goto done;
676 if (smbconf_share_exists(conf_ctx, sharename)) {
677 d_fprintf(stderr, _("ERROR: share %s already exists.\n"),
678 sharename);
679 goto done;
682 /* validate path */
684 if (path[0] != '/') {
685 d_fprintf(stderr,
686 _("Error: path '%s' is not an absolute path.\n"),
687 path);
688 goto done;
691 if (sys_stat(path, &sbuf, false) != 0) {
692 d_fprintf(stderr,
693 _("ERROR: cannot stat path '%s' to ensure "
694 "this is a directory.\n"
695 "Error was '%s'.\n"),
696 path, strerror(errno));
697 goto done;
700 if (!S_ISDIR(sbuf.st_ex_mode)) {
701 d_fprintf(stderr,
702 _("ERROR: path '%s' is not a directory.\n"),
703 path);
704 goto done;
708 * start a transaction
711 werr = smbconf_transaction_start(conf_ctx);
712 if (!W_ERROR_IS_OK(werr)) {
713 d_printf("error starting transaction: %s\n",
714 win_errstr(werr));
715 goto done;
719 * create the share
722 err = smbconf_create_share(conf_ctx, sharename);
723 if (!SBC_ERROR_IS_OK(err)) {
724 d_fprintf(stderr, _("Error creating share %s: %s\n"),
725 sharename, sbcErrorString(err));
726 goto cancel;
730 * fill the share with parameters
733 err = smbconf_set_parameter(conf_ctx, sharename, "path", path);
734 if (!SBC_ERROR_IS_OK(err)) {
735 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
736 "path", sbcErrorString(err));
737 goto cancel;
740 if (comment != NULL) {
741 err = smbconf_set_parameter(conf_ctx, sharename, "comment",
742 comment);
743 if (!SBC_ERROR_IS_OK(err)) {
744 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
745 "comment", sbcErrorString(err));
746 goto cancel;
750 err = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
751 if (!SBC_ERROR_IS_OK(err)) {
752 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
753 "'guest ok'", sbcErrorString(err));
754 goto cancel;
757 err = smbconf_set_parameter(conf_ctx, sharename, "writeable",
758 writeable);
759 if (!SBC_ERROR_IS_OK(err)) {
760 d_fprintf(stderr, _("Error setting parameter %s: %s\n"),
761 "writeable", sbcErrorString(err));
762 goto cancel;
766 * commit the whole thing
769 werr = smbconf_transaction_commit(conf_ctx);
770 if (!W_ERROR_IS_OK(werr)) {
771 d_printf("error committing transaction: %s\n",
772 win_errstr(werr));
773 } else {
774 ret = 0;
777 goto done;
779 cancel:
780 werr = smbconf_transaction_cancel(conf_ctx);
781 if (!W_ERROR_IS_OK(werr)) {
782 d_printf("error cancelling transaction: %s\n",
783 win_errstr(werr));
786 done:
787 TALLOC_FREE(mem_ctx);
788 return ret;
791 static int net_conf_delshare(struct net_context *c,
792 struct smbconf_ctx *conf_ctx, int argc,
793 const char **argv)
795 int ret = -1;
796 const char *sharename = NULL;
797 sbcErr err;
798 TALLOC_CTX *mem_ctx = talloc_stackframe();
800 if (argc != 1 || c->display_usage) {
801 net_conf_delshare_usage(c, argc, argv);
802 goto done;
804 sharename = talloc_strdup(mem_ctx, argv[0]);
805 if (sharename == NULL) {
806 d_printf(_("error: out of memory!\n"));
807 goto done;
810 err = smbconf_delete_share(conf_ctx, sharename);
811 if (!SBC_ERROR_IS_OK(err)) {
812 d_fprintf(stderr, _("Error deleting share %s: %s\n"),
813 sharename, sbcErrorString(err));
814 goto done;
817 ret = 0;
818 done:
819 TALLOC_FREE(mem_ctx);
820 return ret;
823 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
824 int argc, const char **argv)
826 int ret = -1;
827 WERROR werr = WERR_OK;
828 sbcErr err;
829 char *service = NULL;
830 char *param = NULL;
831 const char *value_str = NULL;
832 TALLOC_CTX *mem_ctx = talloc_stackframe();
834 if (argc != 3 || c->display_usage) {
835 net_conf_setparm_usage(c, argc, argv);
836 goto done;
839 * NULL service name means "dangling parameters" to libsmbconf.
840 * We use the empty string from the command line for this purpose.
842 if (strlen(argv[0]) != 0) {
843 service = talloc_strdup(mem_ctx, argv[0]);
844 if (service == NULL) {
845 d_printf(_("error: out of memory!\n"));
846 goto done;
849 param = strlower_talloc(mem_ctx, argv[1]);
850 if (param == NULL) {
851 d_printf(_("error: out of memory!\n"));
852 goto done;
854 value_str = argv[2];
856 werr = smbconf_transaction_start(conf_ctx);
857 if (!W_ERROR_IS_OK(werr)) {
858 d_printf(_("error starting transaction: %s\n"),
859 win_errstr(werr));
860 goto done;
863 if (!smbconf_share_exists(conf_ctx, service)) {
864 err = smbconf_create_share(conf_ctx, service);
865 if (!SBC_ERROR_IS_OK(err)) {
866 d_fprintf(stderr, _("Error creating share '%s': %s\n"),
867 service, sbcErrorString(err));
868 goto cancel;
872 err = smbconf_set_parameter(conf_ctx, service, param, value_str);
873 if (!SBC_ERROR_IS_OK(err)) {
874 d_fprintf(stderr, _("Error setting value '%s': %s\n"),
875 param, sbcErrorString(err));
876 goto cancel;
879 werr = smbconf_transaction_commit(conf_ctx);
880 if (!W_ERROR_IS_OK(werr)) {
881 d_printf(_("error committing transaction: %s\n"),
882 win_errstr(werr));
883 } else {
884 ret = 0;
887 goto done;
889 cancel:
890 werr = smbconf_transaction_cancel(conf_ctx);
891 if (!W_ERROR_IS_OK(werr)) {
892 d_printf(_("error cancelling transaction: %s\n"),
893 win_errstr(werr));
896 done:
897 TALLOC_FREE(mem_ctx);
898 return ret;
901 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
902 int argc, const char **argv)
904 int ret = -1;
905 sbcErr err;
906 char *service = NULL;
907 char *param = NULL;
908 char *valstr = NULL;
909 TALLOC_CTX *mem_ctx;
911 mem_ctx = talloc_stackframe();
913 if (argc != 2 || c->display_usage) {
914 net_conf_getparm_usage(c, argc, argv);
915 goto done;
918 * NULL service name means "dangling parameters" to libsmbconf.
919 * We use the empty string from the command line for this purpose.
921 if (strlen(argv[0]) != 0) {
922 service = talloc_strdup(mem_ctx, argv[0]);
923 if (service == NULL) {
924 d_printf(_("error: out of memory!\n"));
925 goto done;
928 param = strlower_talloc(mem_ctx, argv[1]);
929 if (param == NULL) {
930 d_printf(_("error: out of memory!\n"));
931 goto done;
934 err = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
935 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
936 d_fprintf(stderr,
937 _("Error: given service '%s' does not exist.\n"),
938 service);
939 goto done;
940 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
941 d_fprintf(stderr,
942 _("Error: given parameter '%s' is not set.\n"),
943 param);
944 goto done;
945 } else if (!SBC_ERROR_IS_OK(err)) {
946 d_fprintf(stderr, _("Error getting value '%s': %s.\n"),
947 param, sbcErrorString(err));
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 sbcErr err;
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 err = smbconf_delete_parameter(conf_ctx, service, param);
990 if (SBC_ERROR_EQUAL(err, SBC_ERR_NO_SUCH_SERVICE)) {
991 d_fprintf(stderr,
992 _("Error: given service '%s' does not exist.\n"),
993 service);
994 goto done;
995 } else if (SBC_ERROR_EQUAL(err, SBC_ERR_INVALID_PARAM)) {
996 d_fprintf(stderr,
997 _("Error: given parameter '%s' is not set.\n"),
998 param);
999 goto done;
1000 } else if (!SBC_ERROR_IS_OK(err)) {
1001 d_fprintf(stderr, _("Error deleting value '%s': %s.\n"),
1002 param, sbcErrorString(err));
1003 goto done;
1006 ret = 0;
1008 done:
1009 TALLOC_FREE(mem_ctx);
1010 return ret;
1013 static int net_conf_getincludes(struct net_context *c,
1014 struct smbconf_ctx *conf_ctx,
1015 int argc, const char **argv)
1017 sbcErr err;
1018 uint32_t num_includes;
1019 uint32_t count;
1020 char *service;
1021 char **includes = NULL;
1022 int ret = -1;
1023 TALLOC_CTX *mem_ctx = talloc_stackframe();
1025 if (argc != 1 || c->display_usage) {
1026 net_conf_getincludes_usage(c, argc, argv);
1027 goto done;
1030 service = talloc_strdup(mem_ctx, argv[0]);
1031 if (service == NULL) {
1032 d_printf(_("error: out of memory!\n"));
1033 goto done;
1036 err = smbconf_get_includes(conf_ctx, mem_ctx, service,
1037 &num_includes, &includes);
1038 if (!SBC_ERROR_IS_OK(err)) {
1039 d_printf(_("error getting includes: %s\n"), sbcErrorString(err));
1040 goto done;
1043 for (count = 0; count < num_includes; count++) {
1044 d_printf("include = %s\n", includes[count]);
1047 ret = 0;
1049 done:
1050 TALLOC_FREE(mem_ctx);
1051 return ret;
1054 static int net_conf_setincludes(struct net_context *c,
1055 struct smbconf_ctx *conf_ctx,
1056 int argc, const char **argv)
1058 WERROR werr;
1059 char *service;
1060 uint32_t num_includes;
1061 const char **includes;
1062 int ret = -1;
1063 TALLOC_CTX *mem_ctx = talloc_stackframe();
1065 if (argc < 1 || c->display_usage) {
1066 net_conf_setincludes_usage(c, argc, argv);
1067 goto done;
1070 service = talloc_strdup(mem_ctx, argv[0]);
1071 if (service == NULL) {
1072 d_printf(_("error: out of memory!\n"));
1073 goto done;
1076 num_includes = argc - 1;
1077 if (num_includes == 0) {
1078 includes = NULL;
1079 } else {
1080 includes = argv + 1;
1083 werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
1084 if (!W_ERROR_IS_OK(werr)) {
1085 d_printf(_("error setting includes: %s\n"), win_errstr(werr));
1086 goto done;
1089 ret = 0;
1091 done:
1092 TALLOC_FREE(mem_ctx);
1093 return ret;
1096 static int net_conf_delincludes(struct net_context *c,
1097 struct smbconf_ctx *conf_ctx,
1098 int argc, const char **argv)
1100 WERROR werr;
1101 char *service;
1102 int ret = -1;
1103 TALLOC_CTX *mem_ctx = talloc_stackframe();
1105 if (argc != 1 || c->display_usage) {
1106 net_conf_delincludes_usage(c, argc, argv);
1107 goto done;
1110 service = talloc_strdup(mem_ctx, argv[0]);
1111 if (service == NULL) {
1112 d_printf(_("error: out of memory!\n"));
1113 goto done;
1116 werr = smbconf_delete_includes(conf_ctx, service);
1117 if (!W_ERROR_IS_OK(werr)) {
1118 d_printf(_("error deleting includes: %s\n"), win_errstr(werr));
1119 goto done;
1122 ret = 0;
1124 done:
1125 TALLOC_FREE(mem_ctx);
1126 return ret;
1130 /**********************************************************************
1132 * Wrapper and net_conf_run_function mechanism.
1134 **********************************************************************/
1137 * Wrapper function to call the main conf functions.
1138 * The wrapper calls handles opening and closing of the
1139 * configuration.
1141 static int net_conf_wrap_function(struct net_context *c,
1142 int (*fn)(struct net_context *,
1143 struct smbconf_ctx *,
1144 int, const char **),
1145 int argc, const char **argv)
1147 sbcErr err;
1148 TALLOC_CTX *mem_ctx = talloc_stackframe();
1149 struct smbconf_ctx *conf_ctx;
1150 int ret = -1;
1152 err = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1153 if (!SBC_ERROR_IS_OK(err)) {
1154 return -1;
1157 ret = fn(c, conf_ctx, argc, argv);
1159 smbconf_shutdown(conf_ctx);
1161 return ret;
1165 * We need a functable struct of our own, because the
1166 * functions are called through a wrapper that handles
1167 * the opening and closing of the configuration, and so on.
1169 struct conf_functable {
1170 const char *funcname;
1171 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1172 const char **argv);
1173 int valid_transports;
1174 const char *description;
1175 const char *usage;
1179 * This imitates net_run_function but calls the main functions
1180 * through the wrapper net_conf_wrap_function().
1182 static int net_conf_run_function(struct net_context *c, int argc,
1183 const char **argv, const char *whoami,
1184 struct conf_functable *table)
1186 int i;
1188 if (argc != 0) {
1189 for (i=0; table[i].funcname; i++) {
1190 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1191 return net_conf_wrap_function(c, table[i].fn,
1192 argc-1,
1193 argv+1);
1197 d_printf(_("Usage:\n"));
1198 for (i=0; table[i].funcname; i++) {
1199 if (c->display_usage == false)
1200 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1201 table[i].description);
1202 else
1203 d_printf("%s\n", table[i].usage);
1206 return c->display_usage?0:-1;
1210 * Entry-point for all the CONF functions.
1213 int net_conf(struct net_context *c, int argc, const char **argv)
1215 int ret = -1;
1216 struct conf_functable func_table[] = {
1218 "list",
1219 net_conf_list,
1220 NET_TRANSPORT_LOCAL,
1221 N_("Dump the complete configuration in smb.conf like "
1222 "format."),
1223 N_("net conf list\n"
1224 " Dump the complete configuration in smb.conf "
1225 "like format.")
1229 "import",
1230 net_conf_import,
1231 NET_TRANSPORT_LOCAL,
1232 N_("Import configuration from file in smb.conf "
1233 "format."),
1234 N_("net conf import\n"
1235 " Import configuration from file in smb.conf "
1236 "format.")
1239 "listshares",
1240 net_conf_listshares,
1241 NET_TRANSPORT_LOCAL,
1242 N_("List the share names."),
1243 N_("net conf listshares\n"
1244 " List the share names.")
1247 "drop",
1248 net_conf_drop,
1249 NET_TRANSPORT_LOCAL,
1250 N_("Delete the complete configuration."),
1251 N_("net conf drop\n"
1252 " Delete the complete configuration.")
1255 "showshare",
1256 net_conf_showshare,
1257 NET_TRANSPORT_LOCAL,
1258 N_("Show the definition of a share."),
1259 N_("net conf showshare\n"
1260 " Show the definition of a share.")
1263 "addshare",
1264 net_conf_addshare,
1265 NET_TRANSPORT_LOCAL,
1266 N_("Create a new share."),
1267 N_("net conf addshare\n"
1268 " Create a new share.")
1271 "delshare",
1272 net_conf_delshare,
1273 NET_TRANSPORT_LOCAL,
1274 N_("Delete a share."),
1275 N_("net conf delshare\n"
1276 " Delete a share.")
1279 "setparm",
1280 net_conf_setparm,
1281 NET_TRANSPORT_LOCAL,
1282 N_("Store a parameter."),
1283 N_("net conf setparm\n"
1284 " Store a parameter.")
1287 "getparm",
1288 net_conf_getparm,
1289 NET_TRANSPORT_LOCAL,
1290 N_("Retrieve the value of a parameter."),
1291 N_("net conf getparm\n"
1292 " Retrieve the value of a parameter.")
1295 "delparm",
1296 net_conf_delparm,
1297 NET_TRANSPORT_LOCAL,
1298 N_("Delete a parameter."),
1299 N_("net conf delparm\n"
1300 " Delete a parameter.")
1303 "getincludes",
1304 net_conf_getincludes,
1305 NET_TRANSPORT_LOCAL,
1306 N_("Show the includes of a share definition."),
1307 N_("net conf getincludes\n"
1308 " Show the includes of a share definition.")
1311 "setincludes",
1312 net_conf_setincludes,
1313 NET_TRANSPORT_LOCAL,
1314 N_("Set includes for a share."),
1315 N_("net conf setincludes\n"
1316 " Set includes for a share.")
1319 "delincludes",
1320 net_conf_delincludes,
1321 NET_TRANSPORT_LOCAL,
1322 N_("Delete includes from a share definition."),
1323 N_("net conf setincludes\n"
1324 " Delete includes from a share definition.")
1326 {NULL, NULL, 0, NULL, NULL}
1329 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1331 return ret;