s3:net conf: don't store share names as lower case, but as given.
[Samba/ekacnet.git] / source3 / utils / net_conf.c
blobb65202ee692afeb45ff835e90005a40dffd8b3fb
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;
343 werr = import_process_service(c, conf_ctx, service);
344 if (!W_ERROR_IS_OK(werr)) {
345 goto cancel;
347 } else {
348 struct smbconf_service **services = NULL;
349 uint32_t num_shares, sidx;
351 werr = smbconf_get_config(txt_ctx, mem_ctx,
352 &num_shares,
353 &services);
354 if (!W_ERROR_IS_OK(werr)) {
355 goto cancel;
357 if (!c->opt_testmode) {
358 werr = smbconf_drop(conf_ctx);
359 if (!W_ERROR_IS_OK(werr)) {
360 goto cancel;
365 * Wrap the importing of shares into a transaction,
366 * but only 100 at a time, in order to serve memory.
367 * The allocated memory accumulates across the actions
368 * within the transaction, and for me, some 1500
369 * imported shares, the MAX_TALLOC_SIZE of 256 MB
370 * was exceeded.
372 werr = smbconf_transaction_start(conf_ctx);
373 if (!W_ERROR_IS_OK(werr)) {
374 d_printf("error starting transaction: %s\n",
375 win_errstr(werr));
376 goto done;
379 for (sidx = 0; sidx < num_shares; sidx++) {
380 werr = import_process_service(c, conf_ctx,
381 services[sidx]);
382 if (!W_ERROR_IS_OK(werr)) {
383 goto cancel;
386 if (sidx % 100) {
387 continue;
390 werr = smbconf_transaction_commit(conf_ctx);
391 if (!W_ERROR_IS_OK(werr)) {
392 d_printf("error committing transaction: %s\n",
393 win_errstr(werr));
394 goto done;
396 werr = smbconf_transaction_start(conf_ctx);
397 if (!W_ERROR_IS_OK(werr)) {
398 d_printf("error starting transaction: %s\n",
399 win_errstr(werr));
400 goto done;
405 werr = smbconf_transaction_commit(conf_ctx);
406 if (!W_ERROR_IS_OK(werr)) {
407 d_printf("error committing transaction: %s\n",
408 win_errstr(werr));
409 } else {
410 ret = 0;
413 goto done;
415 cancel:
416 werr = smbconf_transaction_cancel(conf_ctx);
417 if (!W_ERROR_IS_OK(werr)) {
418 d_printf("error cancelling transaction: %s\n",
419 win_errstr(werr));
422 done:
423 TALLOC_FREE(mem_ctx);
424 return ret;
427 static int net_conf_listshares(struct net_context *c,
428 struct smbconf_ctx *conf_ctx, int argc,
429 const char **argv)
431 WERROR werr = WERR_OK;
432 int ret = -1;
433 uint32_t count, num_shares = 0;
434 char **share_names = NULL;
435 TALLOC_CTX *mem_ctx;
437 mem_ctx = talloc_stackframe();
439 if (argc != 0 || c->display_usage) {
440 net_conf_listshares_usage(c, argc, argv);
441 goto done;
444 werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
445 &share_names);
446 if (!W_ERROR_IS_OK(werr)) {
447 goto done;
450 for (count = 0; count < num_shares; count++)
452 d_printf("%s\n", share_names[count]);
455 ret = 0;
457 done:
458 TALLOC_FREE(mem_ctx);
459 return ret;
462 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
463 int argc, const char **argv)
465 int ret = -1;
466 WERROR werr;
468 if (argc != 0 || c->display_usage) {
469 net_conf_drop_usage(c, argc, argv);
470 goto done;
473 werr = smbconf_drop(conf_ctx);
474 if (!W_ERROR_IS_OK(werr)) {
475 d_fprintf(stderr, "Error deleting configuration: %s\n",
476 win_errstr(werr));
477 goto done;
480 ret = 0;
482 done:
483 return ret;
486 static int net_conf_showshare(struct net_context *c,
487 struct smbconf_ctx *conf_ctx, int argc,
488 const char **argv)
490 int ret = -1;
491 WERROR werr = WERR_OK;
492 const char *sharename = NULL;
493 TALLOC_CTX *mem_ctx;
494 uint32_t count;
495 struct smbconf_service *service = NULL;
497 mem_ctx = talloc_stackframe();
499 if (argc != 1 || c->display_usage) {
500 net_conf_showshare_usage(c, argc, argv);
501 goto done;
504 sharename = talloc_strdup(mem_ctx, argv[0]);
505 if (sharename == NULL) {
506 d_printf("error: out of memory!\n");
507 goto done;
510 werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
511 if (!W_ERROR_IS_OK(werr)) {
512 d_printf("error getting share parameters: %s\n",
513 win_errstr(werr));
514 goto done;
517 d_printf("[%s]\n", service->name);
519 for (count = 0; count < service->num_params; count++) {
520 d_printf("\t%s = %s\n", service->param_names[count],
521 service->param_values[count]);
524 ret = 0;
526 done:
527 TALLOC_FREE(mem_ctx);
528 return ret;
532 * Add a share, with a couple of standard parameters, partly optional.
534 * This is a high level utility function of the net conf utility,
535 * not a direct frontend to the smbconf API.
537 static int net_conf_addshare(struct net_context *c,
538 struct smbconf_ctx *conf_ctx, int argc,
539 const char **argv)
541 int ret = -1;
542 WERROR werr = WERR_OK;
543 char *sharename = NULL;
544 const char *path = NULL;
545 const char *comment = NULL;
546 const char *guest_ok = "no";
547 const char *writeable = "no";
548 SMB_STRUCT_STAT sbuf;
549 TALLOC_CTX *mem_ctx = talloc_stackframe();
551 if (c->display_usage) {
552 net_conf_addshare_usage(c, argc, argv);
553 ret = 0;
554 goto done;
557 switch (argc) {
558 case 0:
559 case 1:
560 default:
561 net_conf_addshare_usage(c, argc, argv);
562 goto done;
563 case 5:
564 comment = argv[4];
565 case 4:
566 if (!strnequal(argv[3], "guest_ok=", 9)) {
567 net_conf_addshare_usage(c, argc, argv);
568 goto done;
570 switch (argv[3][9]) {
571 case 'y':
572 case 'Y':
573 guest_ok = "yes";
574 break;
575 case 'n':
576 case 'N':
577 guest_ok = "no";
578 break;
579 default:
580 net_conf_addshare_usage(c, argc, argv);
581 goto done;
583 case 3:
584 if (!strnequal(argv[2], "writeable=", 10)) {
585 net_conf_addshare_usage(c, argc, argv);
586 goto done;
588 switch (argv[2][10]) {
589 case 'y':
590 case 'Y':
591 writeable = "yes";
592 break;
593 case 'n':
594 case 'N':
595 writeable = "no";
596 break;
597 default:
598 net_conf_addshare_usage(c, argc, argv);
599 goto done;
601 case 2:
602 path = argv[1];
603 sharename = talloc_strdup(mem_ctx, argv[0]);
604 if (sharename == NULL) {
605 d_printf("error: out of memory!\n");
606 goto done;
609 break;
613 * validate arguments
616 /* validate share name */
618 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
619 strlen(sharename)))
621 d_fprintf(stderr, "ERROR: share name %s contains "
622 "invalid characters (any of %s)\n",
623 sharename, INVALID_SHARENAME_CHARS);
624 goto done;
627 if (strequal(sharename, GLOBAL_NAME)) {
628 d_fprintf(stderr,
629 "ERROR: 'global' is not a valid share name.\n");
630 goto done;
633 if (smbconf_share_exists(conf_ctx, sharename)) {
634 d_fprintf(stderr, "ERROR: share %s already exists.\n",
635 sharename);
636 goto done;
639 /* validate path */
641 if (path[0] != '/') {
642 d_fprintf(stderr,
643 "Error: path '%s' is not an absolute path.\n",
644 path);
645 goto done;
648 if (sys_stat(path, &sbuf) != 0) {
649 d_fprintf(stderr,
650 "ERROR: cannot stat path '%s' to ensure "
651 "this is a directory.\n"
652 "Error was '%s'.\n",
653 path, strerror(errno));
654 goto done;
657 if (!S_ISDIR(sbuf.st_mode)) {
658 d_fprintf(stderr,
659 "ERROR: path '%s' is not a directory.\n",
660 path);
661 goto done;
665 * create the share
668 werr = smbconf_create_share(conf_ctx, sharename);
669 if (!W_ERROR_IS_OK(werr)) {
670 d_fprintf(stderr, "Error creating share %s: %s\n",
671 sharename, win_errstr(werr));
672 goto done;
676 * fill the share with parameters
679 werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
680 if (!W_ERROR_IS_OK(werr)) {
681 d_fprintf(stderr, "Error setting parameter %s: %s\n",
682 "path", win_errstr(werr));
683 goto done;
686 if (comment != NULL) {
687 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
688 comment);
689 if (!W_ERROR_IS_OK(werr)) {
690 d_fprintf(stderr, "Error setting parameter %s: %s\n",
691 "comment", win_errstr(werr));
692 goto done;
696 werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
697 if (!W_ERROR_IS_OK(werr)) {
698 d_fprintf(stderr, "Error setting parameter %s: %s\n",
699 "'guest ok'", win_errstr(werr));
700 goto done;
703 werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
704 writeable);
705 if (!W_ERROR_IS_OK(werr)) {
706 d_fprintf(stderr, "Error setting parameter %s: %s\n",
707 "writeable", win_errstr(werr));
708 goto done;
711 ret = 0;
713 done:
714 TALLOC_FREE(mem_ctx);
715 return ret;
718 static int net_conf_delshare(struct net_context *c,
719 struct smbconf_ctx *conf_ctx, int argc,
720 const char **argv)
722 int ret = -1;
723 const char *sharename = NULL;
724 WERROR werr = WERR_OK;
725 TALLOC_CTX *mem_ctx = talloc_stackframe();
727 if (argc != 1 || c->display_usage) {
728 net_conf_delshare_usage(c, argc, argv);
729 goto done;
731 sharename = talloc_strdup(mem_ctx, argv[0]);
732 if (sharename == NULL) {
733 d_printf("error: out of memory!\n");
734 goto done;
737 werr = smbconf_delete_share(conf_ctx, sharename);
738 if (!W_ERROR_IS_OK(werr)) {
739 d_fprintf(stderr, "Error deleting share %s: %s\n",
740 sharename, win_errstr(werr));
741 goto done;
744 ret = 0;
745 done:
746 TALLOC_FREE(mem_ctx);
747 return ret;
750 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
751 int argc, const char **argv)
753 int ret = -1;
754 WERROR werr = WERR_OK;
755 char *service = NULL;
756 char *param = NULL;
757 const char *value_str = NULL;
758 TALLOC_CTX *mem_ctx = talloc_stackframe();
760 if (argc != 3 || c->display_usage) {
761 net_conf_setparm_usage(c, argc, argv);
762 goto done;
764 service = talloc_strdup(mem_ctx, argv[0]);
765 if (service == NULL) {
766 d_printf("error: out of memory!\n");
767 goto done;
769 param = talloc_strdup_lower(mem_ctx, argv[1]);
770 if (param == NULL) {
771 d_printf("error: out of memory!\n");
772 goto done;
774 value_str = argv[2];
776 if (!smbconf_share_exists(conf_ctx, service)) {
777 werr = smbconf_create_share(conf_ctx, service);
778 if (!W_ERROR_IS_OK(werr)) {
779 d_fprintf(stderr, "Error creating share '%s': %s\n",
780 service, win_errstr(werr));
781 goto done;
785 werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
787 if (!W_ERROR_IS_OK(werr)) {
788 d_fprintf(stderr, "Error setting value '%s': %s\n",
789 param, win_errstr(werr));
790 goto done;
793 ret = 0;
795 done:
796 TALLOC_FREE(mem_ctx);
797 return ret;
800 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
801 int argc, const char **argv)
803 int ret = -1;
804 WERROR werr = WERR_OK;
805 char *service = NULL;
806 char *param = NULL;
807 char *valstr = NULL;
808 TALLOC_CTX *mem_ctx;
810 mem_ctx = talloc_stackframe();
812 if (argc != 2 || c->display_usage) {
813 net_conf_getparm_usage(c, argc, argv);
814 goto done;
816 service = talloc_strdup(mem_ctx, argv[0]);
817 if (service == NULL) {
818 d_printf("error: out of memory!\n");
819 goto done;
821 param = talloc_strdup_lower(mem_ctx, argv[1]);
822 if (param == NULL) {
823 d_printf("error: out of memory!\n");
824 goto done;
827 werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
829 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
830 d_fprintf(stderr,
831 "Error: given service '%s' does not exist.\n",
832 service);
833 goto done;
834 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
835 d_fprintf(stderr,
836 "Error: given parameter '%s' is not set.\n",
837 param);
838 goto done;
839 } else if (!W_ERROR_IS_OK(werr)) {
840 d_fprintf(stderr, "Error getting value '%s': %s.\n",
841 param, win_errstr(werr));
842 goto done;
845 d_printf("%s\n", valstr);
847 ret = 0;
848 done:
849 TALLOC_FREE(mem_ctx);
850 return ret;
853 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
854 int argc, const char **argv)
856 int ret = -1;
857 WERROR werr = WERR_OK;
858 char *service = NULL;
859 char *param = NULL;
860 TALLOC_CTX *mem_ctx = talloc_stackframe();
862 if (argc != 2 || c->display_usage) {
863 net_conf_delparm_usage(c, argc, argv);
864 goto done;
866 service = talloc_strdup(mem_ctx, argv[0]);
867 if (service == NULL) {
868 d_printf("error: out of memory!\n");
869 goto done;
871 param = talloc_strdup_lower(mem_ctx, argv[1]);
872 if (param == NULL) {
873 d_printf("error: out of memory!\n");
874 goto done;
877 werr = smbconf_delete_parameter(conf_ctx, service, param);
879 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
880 d_fprintf(stderr,
881 "Error: given service '%s' does not exist.\n",
882 service);
883 goto done;
884 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
885 d_fprintf(stderr,
886 "Error: given parameter '%s' is not set.\n",
887 param);
888 goto done;
889 } else if (!W_ERROR_IS_OK(werr)) {
890 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
891 param, win_errstr(werr));
892 goto done;
895 ret = 0;
897 done:
898 TALLOC_FREE(mem_ctx);
899 return ret;
902 static int net_conf_getincludes(struct net_context *c,
903 struct smbconf_ctx *conf_ctx,
904 int argc, const char **argv)
906 WERROR werr;
907 uint32_t num_includes;
908 uint32_t count;
909 char *service;
910 char **includes = NULL;
911 int ret = -1;
912 TALLOC_CTX *mem_ctx = talloc_stackframe();
914 if (argc != 1 || c->display_usage) {
915 net_conf_getincludes_usage(c, argc, argv);
916 goto done;
919 service = talloc_strdup(mem_ctx, argv[0]);
920 if (service == NULL) {
921 d_printf("error: out of memory!\n");
922 goto done;
925 werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
926 &num_includes, &includes);
927 if (!W_ERROR_IS_OK(werr)) {
928 d_printf("error getting includes: %s\n", win_errstr(werr));
929 goto done;
932 for (count = 0; count < num_includes; count++) {
933 d_printf("include = %s\n", includes[count]);
936 ret = 0;
938 done:
939 TALLOC_FREE(mem_ctx);
940 return ret;
943 static int net_conf_setincludes(struct net_context *c,
944 struct smbconf_ctx *conf_ctx,
945 int argc, const char **argv)
947 WERROR werr;
948 char *service;
949 uint32_t num_includes;
950 const char **includes;
951 int ret = -1;
952 TALLOC_CTX *mem_ctx = talloc_stackframe();
954 if (argc < 1 || c->display_usage) {
955 net_conf_setincludes_usage(c, argc, argv);
956 goto done;
959 service = talloc_strdup(mem_ctx, argv[0]);
960 if (service == NULL) {
961 d_printf("error: out of memory!\n");
962 goto done;
965 num_includes = argc - 1;
966 if (num_includes == 0) {
967 includes = NULL;
968 } else {
969 includes = argv + 1;
972 werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
973 if (!W_ERROR_IS_OK(werr)) {
974 d_printf("error setting includes: %s\n", win_errstr(werr));
975 goto done;
978 ret = 0;
980 done:
981 TALLOC_FREE(mem_ctx);
982 return ret;
985 static int net_conf_delincludes(struct net_context *c,
986 struct smbconf_ctx *conf_ctx,
987 int argc, const char **argv)
989 WERROR werr;
990 char *service;
991 int ret = -1;
992 TALLOC_CTX *mem_ctx = talloc_stackframe();
994 if (argc != 1 || c->display_usage) {
995 net_conf_delincludes_usage(c, argc, argv);
996 goto done;
999 service = talloc_strdup(mem_ctx, argv[0]);
1000 if (service == NULL) {
1001 d_printf("error: out of memory!\n");
1002 goto done;
1005 werr = smbconf_delete_includes(conf_ctx, service);
1006 if (!W_ERROR_IS_OK(werr)) {
1007 d_printf("error deleting includes: %s\n", win_errstr(werr));
1008 goto done;
1011 ret = 0;
1013 done:
1014 TALLOC_FREE(mem_ctx);
1015 return ret;
1019 /**********************************************************************
1021 * Wrapper and net_conf_run_function mechanism.
1023 **********************************************************************/
1026 * Wrapper function to call the main conf functions.
1027 * The wrapper calls handles opening and closing of the
1028 * configuration.
1030 static int net_conf_wrap_function(struct net_context *c,
1031 int (*fn)(struct net_context *,
1032 struct smbconf_ctx *,
1033 int, const char **),
1034 int argc, const char **argv)
1036 WERROR werr;
1037 TALLOC_CTX *mem_ctx = talloc_stackframe();
1038 struct smbconf_ctx *conf_ctx;
1039 int ret = -1;
1041 werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1043 if (!W_ERROR_IS_OK(werr)) {
1044 return -1;
1047 ret = fn(c, conf_ctx, argc, argv);
1049 smbconf_shutdown(conf_ctx);
1051 return ret;
1055 * We need a functable struct of our own, because the
1056 * functions are called through a wrapper that handles
1057 * the opening and closing of the configuration, and so on.
1059 struct conf_functable {
1060 const char *funcname;
1061 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1062 const char **argv);
1063 int valid_transports;
1064 const char *description;
1065 const char *usage;
1069 * This imitates net_run_function but calls the main functions
1070 * through the wrapper net_conf_wrap_function().
1072 static int net_conf_run_function(struct net_context *c, int argc,
1073 const char **argv, const char *whoami,
1074 struct conf_functable *table)
1076 int i;
1078 if (argc != 0) {
1079 for (i=0; table[i].funcname; i++) {
1080 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1081 return net_conf_wrap_function(c, table[i].fn,
1082 argc-1,
1083 argv+1);
1087 d_printf("Usage:\n");
1088 for (i=0; table[i].funcname; i++) {
1089 if (c->display_usage == false)
1090 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1091 table[i].description);
1092 else
1093 d_printf("%s\n", table[i].usage);
1096 return c->display_usage?0:-1;
1100 * Entry-point for all the CONF functions.
1103 int net_conf(struct net_context *c, int argc, const char **argv)
1105 int ret = -1;
1106 struct conf_functable func_table[] = {
1108 "list",
1109 net_conf_list,
1110 NET_TRANSPORT_LOCAL,
1111 "Dump the complete configuration in smb.conf like "
1112 "format.",
1113 "net conf list\n"
1114 " Dump the complete configuration in smb.conf like "
1115 "format."
1119 "import",
1120 net_conf_import,
1121 NET_TRANSPORT_LOCAL,
1122 "Import configuration from file in smb.conf format.",
1123 "net conf import\n"
1124 " Import configuration from file in smb.conf format."
1127 "listshares",
1128 net_conf_listshares,
1129 NET_TRANSPORT_LOCAL,
1130 "List the share names.",
1131 "net conf listshares\n"
1132 " List the share names."
1135 "drop",
1136 net_conf_drop,
1137 NET_TRANSPORT_LOCAL,
1138 "Delete the complete configuration.",
1139 "net conf drop\n"
1140 " Delete the complete configuration."
1143 "showshare",
1144 net_conf_showshare,
1145 NET_TRANSPORT_LOCAL,
1146 "Show the definition of a share.",
1147 "net conf showshare\n"
1148 " Show the definition of a share."
1151 "addshare",
1152 net_conf_addshare,
1153 NET_TRANSPORT_LOCAL,
1154 "Create a new share.",
1155 "net conf addshare\n"
1156 " Create a new share."
1159 "delshare",
1160 net_conf_delshare,
1161 NET_TRANSPORT_LOCAL,
1162 "Delete a share.",
1163 "net conf delshare\n"
1164 " Delete a share."
1167 "setparm",
1168 net_conf_setparm,
1169 NET_TRANSPORT_LOCAL,
1170 "Store a parameter.",
1171 "net conf setparm\n"
1172 " Store a parameter."
1175 "getparm",
1176 net_conf_getparm,
1177 NET_TRANSPORT_LOCAL,
1178 "Retrieve the value of a parameter.",
1179 "net conf getparm\n"
1180 " Retrieve the value of a parameter."
1183 "delparm",
1184 net_conf_delparm,
1185 NET_TRANSPORT_LOCAL,
1186 "Delete a parameter.",
1187 "net conf delparm\n"
1188 " Delete a parameter."
1191 "getincludes",
1192 net_conf_getincludes,
1193 NET_TRANSPORT_LOCAL,
1194 "Show the includes of a share definition.",
1195 "net conf getincludes\n"
1196 " Show the includes of a share definition."
1199 "setincludes",
1200 net_conf_setincludes,
1201 NET_TRANSPORT_LOCAL,
1202 "Set includes for a share.",
1203 "net conf setincludes\n"
1204 " Set includes for a share."
1207 "delincludes",
1208 net_conf_delincludes,
1209 NET_TRANSPORT_LOCAL,
1210 "Delete includes from a share definition.",
1211 "net conf setincludes\n"
1212 " Delete includes from a share definition."
1214 {NULL, NULL, 0, NULL, NULL}
1217 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1219 return ret;