Fix bug 7005 - mangle method = hash truncates files with dot '. ' character
[Samba.git] / source / utils / net_conf.c
blobf6e3712bb8f1839f9def08e542958d20f605dddc
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 dos_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 dos_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 dos_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: %s\n",
401 win_errstr(werr));
402 goto done;
404 werr = smbconf_transaction_start(conf_ctx);
405 if (!W_ERROR_IS_OK(werr)) {
406 d_printf("error starting transaction: %s\n",
407 win_errstr(werr));
408 goto done;
413 werr = smbconf_transaction_commit(conf_ctx);
414 if (!W_ERROR_IS_OK(werr)) {
415 d_printf("error committing transaction: %s\n",
416 win_errstr(werr));
417 } else {
418 ret = 0;
421 goto done;
423 cancel:
424 werr = smbconf_transaction_cancel(conf_ctx);
425 if (!W_ERROR_IS_OK(werr)) {
426 d_printf("error cancelling transaction: %s\n",
427 win_errstr(werr));
430 done:
431 TALLOC_FREE(mem_ctx);
432 return ret;
435 static int net_conf_listshares(struct net_context *c,
436 struct smbconf_ctx *conf_ctx, int argc,
437 const char **argv)
439 WERROR werr = WERR_OK;
440 int ret = -1;
441 uint32_t count, num_shares = 0;
442 char **share_names = NULL;
443 TALLOC_CTX *mem_ctx;
445 mem_ctx = talloc_stackframe();
447 if (argc != 0 || c->display_usage) {
448 net_conf_listshares_usage(c, argc, argv);
449 goto done;
452 werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
453 &share_names);
454 if (!W_ERROR_IS_OK(werr)) {
455 goto done;
458 for (count = 0; count < num_shares; count++)
460 d_printf("%s\n", share_names[count]);
463 ret = 0;
465 done:
466 TALLOC_FREE(mem_ctx);
467 return ret;
470 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
471 int argc, const char **argv)
473 int ret = -1;
474 WERROR werr;
476 if (argc != 0 || c->display_usage) {
477 net_conf_drop_usage(c, argc, argv);
478 goto done;
481 werr = smbconf_drop(conf_ctx);
482 if (!W_ERROR_IS_OK(werr)) {
483 d_fprintf(stderr, "Error deleting configuration: %s\n",
484 dos_errstr(werr));
485 goto done;
488 ret = 0;
490 done:
491 return ret;
494 static int net_conf_showshare(struct net_context *c,
495 struct smbconf_ctx *conf_ctx, int argc,
496 const char **argv)
498 int ret = -1;
499 WERROR werr = WERR_OK;
500 const char *sharename = NULL;
501 TALLOC_CTX *mem_ctx;
502 uint32_t count;
503 struct smbconf_service *service = NULL;
505 mem_ctx = talloc_stackframe();
507 if (argc != 1 || c->display_usage) {
508 net_conf_showshare_usage(c, argc, argv);
509 goto done;
512 sharename = talloc_strdup(mem_ctx, argv[0]);
513 if (sharename == NULL) {
514 d_printf("error: out of memory!\n");
515 goto done;
518 werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
519 if (!W_ERROR_IS_OK(werr)) {
520 d_printf("error getting share parameters: %s\n",
521 dos_errstr(werr));
522 goto done;
525 d_printf("[%s]\n", service->name);
527 for (count = 0; count < service->num_params; count++) {
528 d_printf("\t%s = %s\n", service->param_names[count],
529 service->param_values[count]);
532 ret = 0;
534 done:
535 TALLOC_FREE(mem_ctx);
536 return ret;
540 * Add a share, with a couple of standard parameters, partly optional.
542 * This is a high level utility function of the net conf utility,
543 * not a direct frontend to the smbconf API.
545 static int net_conf_addshare(struct net_context *c,
546 struct smbconf_ctx *conf_ctx, int argc,
547 const char **argv)
549 int ret = -1;
550 WERROR werr = WERR_OK;
551 char *sharename = NULL;
552 const char *path = NULL;
553 const char *comment = NULL;
554 const char *guest_ok = "no";
555 const char *writeable = "no";
556 SMB_STRUCT_STAT sbuf;
557 TALLOC_CTX *mem_ctx = talloc_stackframe();
559 if (c->display_usage) {
560 net_conf_addshare_usage(c, argc, argv);
561 ret = 0;
562 goto done;
565 switch (argc) {
566 case 0:
567 case 1:
568 default:
569 net_conf_addshare_usage(c, argc, argv);
570 goto done;
571 case 5:
572 comment = argv[4];
573 case 4:
574 if (!strnequal(argv[3], "guest_ok=", 9)) {
575 net_conf_addshare_usage(c, argc, argv);
576 goto done;
578 switch (argv[3][9]) {
579 case 'y':
580 case 'Y':
581 guest_ok = "yes";
582 break;
583 case 'n':
584 case 'N':
585 guest_ok = "no";
586 break;
587 default:
588 net_conf_addshare_usage(c, argc, argv);
589 goto done;
591 case 3:
592 if (!strnequal(argv[2], "writeable=", 10)) {
593 net_conf_addshare_usage(c, argc, argv);
594 goto done;
596 switch (argv[2][10]) {
597 case 'y':
598 case 'Y':
599 writeable = "yes";
600 break;
601 case 'n':
602 case 'N':
603 writeable = "no";
604 break;
605 default:
606 net_conf_addshare_usage(c, argc, argv);
607 goto done;
609 case 2:
610 path = argv[1];
611 sharename = talloc_strdup(mem_ctx, argv[0]);
612 if (sharename == NULL) {
613 d_printf("error: out of memory!\n");
614 goto done;
617 break;
621 * validate arguments
624 /* validate share name */
626 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
627 strlen(sharename)))
629 d_fprintf(stderr, "ERROR: share name %s contains "
630 "invalid characters (any of %s)\n",
631 sharename, INVALID_SHARENAME_CHARS);
632 goto done;
635 if (strequal(sharename, GLOBAL_NAME)) {
636 d_fprintf(stderr,
637 "ERROR: 'global' is not a valid share name.\n");
638 goto done;
641 if (smbconf_share_exists(conf_ctx, sharename)) {
642 d_fprintf(stderr, "ERROR: share %s already exists.\n",
643 sharename);
644 goto done;
647 /* validate path */
649 if (path[0] != '/') {
650 d_fprintf(stderr,
651 "Error: path '%s' is not an absolute path.\n",
652 path);
653 goto done;
656 if (sys_stat(path, &sbuf) != 0) {
657 d_fprintf(stderr,
658 "ERROR: cannot stat path '%s' to ensure "
659 "this is a directory.\n"
660 "Error was '%s'.\n",
661 path, strerror(errno));
662 goto done;
665 if (!S_ISDIR(sbuf.st_mode)) {
666 d_fprintf(stderr,
667 "ERROR: path '%s' is not a directory.\n",
668 path);
669 goto done;
673 * create the share
676 werr = smbconf_create_share(conf_ctx, sharename);
677 if (!W_ERROR_IS_OK(werr)) {
678 d_fprintf(stderr, "Error creating share %s: %s\n",
679 sharename, dos_errstr(werr));
680 goto done;
684 * fill the share with parameters
687 werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
688 if (!W_ERROR_IS_OK(werr)) {
689 d_fprintf(stderr, "Error setting parameter %s: %s\n",
690 "path", dos_errstr(werr));
691 goto done;
694 if (comment != NULL) {
695 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
696 comment);
697 if (!W_ERROR_IS_OK(werr)) {
698 d_fprintf(stderr, "Error setting parameter %s: %s\n",
699 "comment", dos_errstr(werr));
700 goto done;
704 werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
705 if (!W_ERROR_IS_OK(werr)) {
706 d_fprintf(stderr, "Error setting parameter %s: %s\n",
707 "'guest ok'", dos_errstr(werr));
708 goto done;
711 werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
712 writeable);
713 if (!W_ERROR_IS_OK(werr)) {
714 d_fprintf(stderr, "Error setting parameter %s: %s\n",
715 "writeable", dos_errstr(werr));
716 goto done;
719 ret = 0;
721 done:
722 TALLOC_FREE(mem_ctx);
723 return ret;
726 static int net_conf_delshare(struct net_context *c,
727 struct smbconf_ctx *conf_ctx, int argc,
728 const char **argv)
730 int ret = -1;
731 const char *sharename = NULL;
732 WERROR werr = WERR_OK;
733 TALLOC_CTX *mem_ctx = talloc_stackframe();
735 if (argc != 1 || c->display_usage) {
736 net_conf_delshare_usage(c, argc, argv);
737 goto done;
739 sharename = talloc_strdup(mem_ctx, argv[0]);
740 if (sharename == NULL) {
741 d_printf("error: out of memory!\n");
742 goto done;
745 werr = smbconf_delete_share(conf_ctx, sharename);
746 if (!W_ERROR_IS_OK(werr)) {
747 d_fprintf(stderr, "Error deleting share %s: %s\n",
748 sharename, dos_errstr(werr));
749 goto done;
752 ret = 0;
753 done:
754 TALLOC_FREE(mem_ctx);
755 return ret;
758 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
759 int argc, const char **argv)
761 int ret = -1;
762 WERROR werr = WERR_OK;
763 char *service = NULL;
764 char *param = NULL;
765 const char *value_str = NULL;
766 TALLOC_CTX *mem_ctx = talloc_stackframe();
768 if (argc != 3 || c->display_usage) {
769 net_conf_setparm_usage(c, argc, argv);
770 goto done;
772 service = talloc_strdup(mem_ctx, argv[0]);
773 if (service == NULL) {
774 d_printf("error: out of memory!\n");
775 goto done;
777 param = talloc_strdup_lower(mem_ctx, argv[1]);
778 if (param == NULL) {
779 d_printf("error: out of memory!\n");
780 goto done;
782 value_str = argv[2];
784 if (!smbconf_share_exists(conf_ctx, service)) {
785 werr = smbconf_create_share(conf_ctx, service);
786 if (!W_ERROR_IS_OK(werr)) {
787 d_fprintf(stderr, "Error creating share '%s': %s\n",
788 service, dos_errstr(werr));
789 goto done;
793 werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
795 if (!W_ERROR_IS_OK(werr)) {
796 d_fprintf(stderr, "Error setting value '%s': %s\n",
797 param, dos_errstr(werr));
798 goto done;
801 ret = 0;
803 done:
804 TALLOC_FREE(mem_ctx);
805 return ret;
808 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
809 int argc, const char **argv)
811 int ret = -1;
812 WERROR werr = WERR_OK;
813 char *service = NULL;
814 char *param = NULL;
815 char *valstr = NULL;
816 TALLOC_CTX *mem_ctx;
818 mem_ctx = talloc_stackframe();
820 if (argc != 2 || c->display_usage) {
821 net_conf_getparm_usage(c, argc, argv);
822 goto done;
824 service = talloc_strdup(mem_ctx, argv[0]);
825 if (service == NULL) {
826 d_printf("error: out of memory!\n");
827 goto done;
829 param = talloc_strdup_lower(mem_ctx, argv[1]);
830 if (param == NULL) {
831 d_printf("error: out of memory!\n");
832 goto done;
835 werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
837 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
838 d_fprintf(stderr,
839 "Error: given service '%s' does not exist.\n",
840 service);
841 goto done;
842 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
843 d_fprintf(stderr,
844 "Error: given parameter '%s' is not set.\n",
845 param);
846 goto done;
847 } else if (!W_ERROR_IS_OK(werr)) {
848 d_fprintf(stderr, "Error getting value '%s': %s.\n",
849 param, dos_errstr(werr));
850 goto done;
853 d_printf("%s\n", valstr);
855 ret = 0;
856 done:
857 TALLOC_FREE(mem_ctx);
858 return ret;
861 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
862 int argc, const char **argv)
864 int ret = -1;
865 WERROR werr = WERR_OK;
866 char *service = NULL;
867 char *param = NULL;
868 TALLOC_CTX *mem_ctx = talloc_stackframe();
870 if (argc != 2 || c->display_usage) {
871 net_conf_delparm_usage(c, argc, argv);
872 goto done;
874 service = talloc_strdup(mem_ctx, argv[0]);
875 if (service == NULL) {
876 d_printf("error: out of memory!\n");
877 goto done;
879 param = talloc_strdup_lower(mem_ctx, argv[1]);
880 if (param == NULL) {
881 d_printf("error: out of memory!\n");
882 goto done;
885 werr = smbconf_delete_parameter(conf_ctx, service, param);
887 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
888 d_fprintf(stderr,
889 "Error: given service '%s' does not exist.\n",
890 service);
891 goto done;
892 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
893 d_fprintf(stderr,
894 "Error: given parameter '%s' is not set.\n",
895 param);
896 goto done;
897 } else if (!W_ERROR_IS_OK(werr)) {
898 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
899 param, dos_errstr(werr));
900 goto done;
903 ret = 0;
905 done:
906 TALLOC_FREE(mem_ctx);
907 return ret;
910 static int net_conf_getincludes(struct net_context *c,
911 struct smbconf_ctx *conf_ctx,
912 int argc, const char **argv)
914 WERROR werr;
915 uint32_t num_includes;
916 uint32_t count;
917 char *service;
918 char **includes = NULL;
919 int ret = -1;
920 TALLOC_CTX *mem_ctx = talloc_stackframe();
922 if (argc != 1 || c->display_usage) {
923 net_conf_getincludes_usage(c, argc, argv);
924 goto done;
927 service = talloc_strdup(mem_ctx, argv[0]);
928 if (service == NULL) {
929 d_printf("error: out of memory!\n");
930 goto done;
933 werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
934 &num_includes, &includes);
935 if (!W_ERROR_IS_OK(werr)) {
936 d_printf("error getting includes: %s\n", dos_errstr(werr));
937 goto done;
940 for (count = 0; count < num_includes; count++) {
941 d_printf("include = %s\n", includes[count]);
944 ret = 0;
946 done:
947 TALLOC_FREE(mem_ctx);
948 return ret;
951 static int net_conf_setincludes(struct net_context *c,
952 struct smbconf_ctx *conf_ctx,
953 int argc, const char **argv)
955 WERROR werr;
956 char *service;
957 uint32_t num_includes;
958 const char **includes;
959 int ret = -1;
960 TALLOC_CTX *mem_ctx = talloc_stackframe();
962 if (argc < 1 || c->display_usage) {
963 net_conf_setincludes_usage(c, argc, argv);
964 goto done;
967 service = talloc_strdup(mem_ctx, argv[0]);
968 if (service == NULL) {
969 d_printf("error: out of memory!\n");
970 goto done;
973 num_includes = argc - 1;
974 if (num_includes == 0) {
975 includes = NULL;
976 } else {
977 includes = argv + 1;
980 werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
981 if (!W_ERROR_IS_OK(werr)) {
982 d_printf("error setting includes: %s\n", dos_errstr(werr));
983 goto done;
986 ret = 0;
988 done:
989 TALLOC_FREE(mem_ctx);
990 return ret;
993 static int net_conf_delincludes(struct net_context *c,
994 struct smbconf_ctx *conf_ctx,
995 int argc, const char **argv)
997 WERROR werr;
998 char *service;
999 int ret = -1;
1000 TALLOC_CTX *mem_ctx = talloc_stackframe();
1002 if (argc != 1 || c->display_usage) {
1003 net_conf_delincludes_usage(c, argc, argv);
1004 goto done;
1007 service = talloc_strdup(mem_ctx, argv[0]);
1008 if (service == NULL) {
1009 d_printf("error: out of memory!\n");
1010 goto done;
1013 werr = smbconf_delete_includes(conf_ctx, service);
1014 if (!W_ERROR_IS_OK(werr)) {
1015 d_printf("error deleting includes: %s\n", dos_errstr(werr));
1016 goto done;
1019 ret = 0;
1021 done:
1022 TALLOC_FREE(mem_ctx);
1023 return ret;
1027 /**********************************************************************
1029 * Wrapper and net_conf_run_function mechanism.
1031 **********************************************************************/
1034 * Wrapper function to call the main conf functions.
1035 * The wrapper calls handles opening and closing of the
1036 * configuration.
1038 static int net_conf_wrap_function(struct net_context *c,
1039 int (*fn)(struct net_context *,
1040 struct smbconf_ctx *,
1041 int, const char **),
1042 int argc, const char **argv)
1044 WERROR werr;
1045 TALLOC_CTX *mem_ctx = talloc_stackframe();
1046 struct smbconf_ctx *conf_ctx;
1047 int ret = -1;
1049 werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1051 if (!W_ERROR_IS_OK(werr)) {
1052 return -1;
1055 ret = fn(c, conf_ctx, argc, argv);
1057 smbconf_shutdown(conf_ctx);
1059 return ret;
1063 * We need a functable struct of our own, because the
1064 * functions are called through a wrapper that handles
1065 * the opening and closing of the configuration, and so on.
1067 struct conf_functable {
1068 const char *funcname;
1069 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1070 const char **argv);
1071 int valid_transports;
1072 const char *description;
1073 const char *usage;
1077 * This imitates net_run_function but calls the main functions
1078 * through the wrapper net_conf_wrap_function().
1080 static int net_conf_run_function(struct net_context *c, int argc,
1081 const char **argv, const char *whoami,
1082 struct conf_functable *table)
1084 int i;
1086 if (argc != 0) {
1087 for (i=0; table[i].funcname; i++) {
1088 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1089 return net_conf_wrap_function(c, table[i].fn,
1090 argc-1,
1091 argv+1);
1095 d_printf("Usage:\n");
1096 for (i=0; table[i].funcname; i++) {
1097 if (c->display_usage == false)
1098 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1099 table[i].description);
1100 else
1101 d_printf("%s\n", table[i].usage);
1104 return c->display_usage?0:-1;
1108 * Entry-point for all the CONF functions.
1111 int net_conf(struct net_context *c, int argc, const char **argv)
1113 int ret = -1;
1114 struct conf_functable func_table[] = {
1116 "list",
1117 net_conf_list,
1118 NET_TRANSPORT_LOCAL,
1119 "Dump the complete configuration in smb.conf like "
1120 "format.",
1121 "net conf list\n"
1122 " Dump the complete configuration in smb.conf like "
1123 "format."
1127 "import",
1128 net_conf_import,
1129 NET_TRANSPORT_LOCAL,
1130 "Import configuration from file in smb.conf format.",
1131 "net conf import\n"
1132 " Import configuration from file in smb.conf format."
1135 "listshares",
1136 net_conf_listshares,
1137 NET_TRANSPORT_LOCAL,
1138 "List the share names.",
1139 "net conf listshares\n"
1140 " List the share names."
1143 "drop",
1144 net_conf_drop,
1145 NET_TRANSPORT_LOCAL,
1146 "Delete the complete configuration.",
1147 "net conf drop\n"
1148 " Delete the complete configuration."
1151 "showshare",
1152 net_conf_showshare,
1153 NET_TRANSPORT_LOCAL,
1154 "Show the definition of a share.",
1155 "net conf showshare\n"
1156 " Show the definition of a share."
1159 "addshare",
1160 net_conf_addshare,
1161 NET_TRANSPORT_LOCAL,
1162 "Create a new share.",
1163 "net conf addshare\n"
1164 " Create a new share."
1167 "delshare",
1168 net_conf_delshare,
1169 NET_TRANSPORT_LOCAL,
1170 "Delete a share.",
1171 "net conf delshare\n"
1172 " Delete a share."
1175 "setparm",
1176 net_conf_setparm,
1177 NET_TRANSPORT_LOCAL,
1178 "Store a parameter.",
1179 "net conf setparm\n"
1180 " Store a parameter."
1183 "getparm",
1184 net_conf_getparm,
1185 NET_TRANSPORT_LOCAL,
1186 "Retrieve the value of a parameter.",
1187 "net conf getparm\n"
1188 " Retrieve the value of a parameter."
1191 "delparm",
1192 net_conf_delparm,
1193 NET_TRANSPORT_LOCAL,
1194 "Delete a parameter.",
1195 "net conf delparm\n"
1196 " Delete a parameter."
1199 "getincludes",
1200 net_conf_getincludes,
1201 NET_TRANSPORT_LOCAL,
1202 "Show the includes of a share definition.",
1203 "net conf getincludes\n"
1204 " Show the includes of a share definition."
1207 "setincludes",
1208 net_conf_setincludes,
1209 NET_TRANSPORT_LOCAL,
1210 "Set includes for a share.",
1211 "net conf setincludes\n"
1212 " Set includes for a share."
1215 "delincludes",
1216 net_conf_delincludes,
1217 NET_TRANSPORT_LOCAL,
1218 "Delete includes from a share definition.",
1219 "net conf setincludes\n"
1220 " Delete includes from a share definition."
1222 {NULL, NULL, 0, NULL, NULL}
1225 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1227 return ret;