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.
31 #include "utils/net.h"
33 /**********************************************************************
37 **********************************************************************/
39 static int net_conf_list_usage(struct net_context *c, int argc,
42 d_printf("USAGE: net conf list\n");
46 static int net_conf_import_usage(struct net_context *c, int argc,
49 d_printf("USAGE: net conf import [--test|-T] <filename> "
51 "\t[--test|-T] testmode - do not act, just print "
52 "what would be done\n"
53 "\t<servicename> only import service <servicename>, "
58 static int net_conf_listshares_usage(struct net_context *c, int argc,
61 d_printf("USAGE: net conf listshares\n");
65 static int net_conf_drop_usage(struct net_context *c, int argc,
68 d_printf("USAGE: net conf drop\n");
72 static int net_conf_showshare_usage(struct net_context *c, int argc,
75 d_printf("USAGE: net conf showshare <sharename>\n");
79 static int net_conf_addshare_usage(struct net_context *c, int argc,
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");
94 static int net_conf_delshare_usage(struct net_context *c, int argc,
97 d_printf("USAGE: net conf delshare <sharename>\n");
101 static int net_conf_setparm_usage(struct net_context *c, int argc,
104 d_printf("USAGE: net conf setparm <section> <param> <value>\n");
108 static int net_conf_getparm_usage(struct net_context *c, int argc,
111 d_printf("USAGE: net conf getparm <section> <param>\n");
115 static int net_conf_delparm_usage(struct net_context *c, int argc,
118 d_printf("USAGE: net conf delparm <section> <param>\n");
122 static int net_conf_getincludes_usage(struct net_context *c, int argc,
125 d_printf("USAGE: net conf getincludes <section>\n");
129 static int net_conf_setincludes_usage(struct net_context *c, int argc,
132 d_printf("USAGE: net conf setincludes <section> [<filename>]*\n");
136 static int net_conf_delincludes_usage(struct net_context *c, int argc,
139 d_printf("USAGE: net conf delincludes <section>\n");
144 /**********************************************************************
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)
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);
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]);
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)) {
184 werr = smbconf_create_share(conf_ctx, service->name);
185 if (!W_ERROR_IS_OK(werr)) {
189 for (idx = 0; idx < service->num_params; idx ++) {
190 if (strequal(service->param_names[idx], "include")) {
191 includes = TALLOC_REALLOC_ARRAY(mem_ctx,
195 if (includes == NULL) {
199 includes[num_includes] = talloc_strdup(includes,
200 service->param_values[idx]);
201 if (includes[num_includes] == NULL) {
207 werr = smbconf_set_parameter(conf_ctx,
209 service->param_names[idx],
210 service->param_values[idx]);
211 if (!W_ERROR_IS_OK(werr)) {
217 werr = smbconf_set_includes(conf_ctx, service->name, num_includes,
218 (const char **)includes);
221 TALLOC_FREE(mem_ctx);
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;
239 uint32_t share_count, param_count;
240 struct smbconf_service **shares = NULL;
242 mem_ctx = talloc_stackframe();
245 net_conf_list_usage(c, argc, argv);
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",
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);
262 for (param_count = 0;
263 param_count < shares[share_count]->num_params;
266 d_printf("%s%s = %s\n",
268 shares[share_count]->param_names[param_count],
269 shares[share_count]->param_values[param_count]);
277 TALLOC_FREE(mem_ctx);
281 static int net_conf_import(struct net_context *c, struct smbconf_ctx *conf_ctx,
282 int argc, const char **argv)
285 const char *filename = NULL;
286 const char *servicename = NULL;
287 char *conf_source = NULL;
289 struct smbconf_ctx *txt_ctx;
292 mem_ctx = talloc_stackframe();
297 net_conf_import_usage(c, argc, argv);
300 servicename = talloc_strdup_lower(mem_ctx, argv[1]);
301 if (servicename == NULL) {
302 d_printf("error: out of memory!\n");
310 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
313 conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
314 if (conf_source == NULL) {
315 d_printf("error: out of memory!\n");
319 werr = smbconf_init(mem_ctx, &txt_ctx, conf_source);
320 if (!W_ERROR_IS_OK(werr)) {
321 d_printf("error loading file '%s': %s\n", filename,
326 if (c->opt_testmode) {
327 d_printf("\nTEST MODE - "
328 "would import the following configuration:\n\n");
331 if (servicename != NULL) {
332 struct smbconf_service *service = NULL;
334 werr = smbconf_get_share(txt_ctx, mem_ctx,
337 if (!W_ERROR_IS_OK(werr)) {
340 werr = import_process_service(c, conf_ctx, service);
341 if (!W_ERROR_IS_OK(werr)) {
345 struct smbconf_service **services = NULL;
346 uint32_t num_shares, sidx;
348 werr = smbconf_get_config(txt_ctx, mem_ctx,
351 if (!W_ERROR_IS_OK(werr)) {
354 if (!c->opt_testmode) {
355 werr = smbconf_drop(conf_ctx);
356 if (!W_ERROR_IS_OK(werr)) {
360 for (sidx = 0; sidx < num_shares; sidx++) {
361 werr = import_process_service(c, conf_ctx,
363 if (!W_ERROR_IS_OK(werr)) {
372 TALLOC_FREE(mem_ctx);
376 static int net_conf_listshares(struct net_context *c,
377 struct smbconf_ctx *conf_ctx, int argc,
380 WERROR werr = WERR_OK;
382 uint32_t count, num_shares = 0;
383 char **share_names = NULL;
386 mem_ctx = talloc_stackframe();
389 net_conf_listshares_usage(c, argc, argv);
393 werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
395 if (!W_ERROR_IS_OK(werr)) {
399 for (count = 0; count < num_shares; count++)
401 d_printf("%s\n", share_names[count]);
407 TALLOC_FREE(mem_ctx);
411 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
412 int argc, const char **argv)
418 net_conf_drop_usage(c, argc, argv);
422 werr = smbconf_drop(conf_ctx);
423 if (!W_ERROR_IS_OK(werr)) {
424 d_fprintf(stderr, "Error deleting configuration: %s\n",
435 static int net_conf_showshare(struct net_context *c,
436 struct smbconf_ctx *conf_ctx, int argc,
440 WERROR werr = WERR_OK;
441 const char *sharename = NULL;
444 struct smbconf_service *service = NULL;
446 mem_ctx = talloc_stackframe();
449 net_conf_showshare_usage(c, argc, argv);
453 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
454 if (sharename == NULL) {
455 d_printf("error: out of memory!\n");
459 werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
460 if (!W_ERROR_IS_OK(werr)) {
461 d_printf("error getting share parameters: %s\n",
466 d_printf("[%s]\n", sharename);
468 for (count = 0; count < service->num_params; count++) {
469 d_printf("\t%s = %s\n", service->param_names[count],
470 service->param_values[count]);
476 TALLOC_FREE(mem_ctx);
481 * Add a share, with a couple of standard parameters, partly optional.
483 * This is a high level utility function of the net conf utility,
484 * not a direct frontend to the smbconf API.
486 static int net_conf_addshare(struct net_context *c,
487 struct smbconf_ctx *conf_ctx, int argc,
491 WERROR werr = WERR_OK;
492 char *sharename = NULL;
493 const char *path = NULL;
494 const char *comment = NULL;
495 const char *guest_ok = "no";
496 const char *writeable = "no";
497 SMB_STRUCT_STAT sbuf;
498 TALLOC_CTX *mem_ctx = talloc_stackframe();
504 net_conf_addshare_usage(c, argc, argv);
509 if (!strnequal(argv[3], "guest_ok=", 9)) {
510 net_conf_addshare_usage(c, argc, argv);
513 switch (argv[3][9]) {
523 net_conf_addshare_usage(c, argc, argv);
527 if (!strnequal(argv[2], "writeable=", 10)) {
528 net_conf_addshare_usage(c, argc, argv);
531 switch (argv[2][10]) {
541 net_conf_addshare_usage(c, argc, argv);
546 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
547 if (sharename == NULL) {
548 d_printf("error: out of memory!\n");
559 /* validate share name */
561 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
564 d_fprintf(stderr, "ERROR: share name %s contains "
565 "invalid characters (any of %s)\n",
566 sharename, INVALID_SHARENAME_CHARS);
570 if (getpwnam(sharename)) {
571 d_fprintf(stderr, "ERROR: share name %s is already a valid "
572 "system user name.\n", sharename);
576 if (strequal(sharename, GLOBAL_NAME)) {
578 "ERROR: 'global' is not a valid share name.\n");
582 if (smbconf_share_exists(conf_ctx, sharename)) {
583 d_fprintf(stderr, "ERROR: share %s already exists.\n",
590 if (path[0] != '/') {
592 "Error: path '%s' is not an absolute path.\n",
597 if (sys_stat(path, &sbuf) != 0) {
599 "ERROR: cannot stat path '%s' to ensure "
600 "this is a directory.\n"
602 path, strerror(errno));
606 if (!S_ISDIR(sbuf.st_mode)) {
608 "ERROR: path '%s' is not a directory.\n",
617 werr = smbconf_create_share(conf_ctx, sharename);
618 if (!W_ERROR_IS_OK(werr)) {
619 d_fprintf(stderr, "Error creating share %s: %s\n",
620 sharename, dos_errstr(werr));
625 * fill the share with parameters
628 werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
629 if (!W_ERROR_IS_OK(werr)) {
630 d_fprintf(stderr, "Error setting parameter %s: %s\n",
631 "path", dos_errstr(werr));
635 if (comment != NULL) {
636 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
638 if (!W_ERROR_IS_OK(werr)) {
639 d_fprintf(stderr, "Error setting parameter %s: %s\n",
640 "comment", dos_errstr(werr));
645 werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
646 if (!W_ERROR_IS_OK(werr)) {
647 d_fprintf(stderr, "Error setting parameter %s: %s\n",
648 "'guest ok'", dos_errstr(werr));
652 werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
654 if (!W_ERROR_IS_OK(werr)) {
655 d_fprintf(stderr, "Error setting parameter %s: %s\n",
656 "writeable", dos_errstr(werr));
663 TALLOC_FREE(mem_ctx);
667 static int net_conf_delshare(struct net_context *c,
668 struct smbconf_ctx *conf_ctx, int argc,
672 const char *sharename = NULL;
673 WERROR werr = WERR_OK;
674 TALLOC_CTX *mem_ctx = talloc_stackframe();
677 net_conf_delshare_usage(c, argc, argv);
680 sharename = talloc_strdup_lower(mem_ctx, argv[0]);
681 if (sharename == NULL) {
682 d_printf("error: out of memory!\n");
686 werr = smbconf_delete_share(conf_ctx, sharename);
687 if (!W_ERROR_IS_OK(werr)) {
688 d_fprintf(stderr, "Error deleting share %s: %s\n",
689 sharename, dos_errstr(werr));
695 TALLOC_FREE(mem_ctx);
699 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
700 int argc, const char **argv)
703 WERROR werr = WERR_OK;
704 char *service = NULL;
706 const char *value_str = NULL;
707 TALLOC_CTX *mem_ctx = talloc_stackframe();
710 net_conf_setparm_usage(c, argc, argv);
713 service = talloc_strdup_lower(mem_ctx, argv[0]);
714 if (service == NULL) {
715 d_printf("error: out of memory!\n");
718 param = talloc_strdup_lower(mem_ctx, argv[1]);
720 d_printf("error: out of memory!\n");
725 if (!smbconf_share_exists(conf_ctx, service)) {
726 werr = smbconf_create_share(conf_ctx, service);
727 if (!W_ERROR_IS_OK(werr)) {
728 d_fprintf(stderr, "Error creating share '%s': %s\n",
729 service, dos_errstr(werr));
734 werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
736 if (!W_ERROR_IS_OK(werr)) {
737 d_fprintf(stderr, "Error setting value '%s': %s\n",
738 param, dos_errstr(werr));
745 TALLOC_FREE(mem_ctx);
749 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
750 int argc, const char **argv)
753 WERROR werr = WERR_OK;
754 char *service = NULL;
759 mem_ctx = talloc_stackframe();
762 net_conf_getparm_usage(c, argc, argv);
765 service = talloc_strdup_lower(mem_ctx, argv[0]);
766 if (service == NULL) {
767 d_printf("error: out of memory!\n");
770 param = talloc_strdup_lower(mem_ctx, argv[1]);
772 d_printf("error: out of memory!\n");
776 werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
778 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
780 "Error: given service '%s' does not exist.\n",
783 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
785 "Error: given parameter '%s' is not set.\n",
788 } else if (!W_ERROR_IS_OK(werr)) {
789 d_fprintf(stderr, "Error getting value '%s': %s.\n",
790 param, dos_errstr(werr));
794 d_printf("%s\n", valstr);
798 TALLOC_FREE(mem_ctx);
802 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
803 int argc, const char **argv)
806 WERROR werr = WERR_OK;
807 char *service = NULL;
809 TALLOC_CTX *mem_ctx = talloc_stackframe();
812 net_conf_delparm_usage(c, argc, argv);
815 service = talloc_strdup_lower(mem_ctx, argv[0]);
816 if (service == NULL) {
817 d_printf("error: out of memory!\n");
820 param = talloc_strdup_lower(mem_ctx, argv[1]);
822 d_printf("error: out of memory!\n");
826 werr = smbconf_delete_parameter(conf_ctx, service, param);
828 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
830 "Error: given service '%s' does not exist.\n",
833 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
835 "Error: given parameter '%s' is not set.\n",
838 } else if (!W_ERROR_IS_OK(werr)) {
839 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
840 param, dos_errstr(werr));
847 TALLOC_FREE(mem_ctx);
851 static int net_conf_getincludes(struct net_context *c,
852 struct smbconf_ctx *conf_ctx,
853 int argc, const char **argv)
856 uint32_t num_includes;
859 char **includes = NULL;
861 TALLOC_CTX *mem_ctx = talloc_stackframe();
864 net_conf_getincludes_usage(c, argc, argv);
868 service = talloc_strdup_lower(mem_ctx, argv[0]);
869 if (service == NULL) {
870 d_printf("error: out of memory!\n");
874 werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
875 &num_includes, &includes);
876 if (!W_ERROR_IS_OK(werr)) {
877 d_printf("error getting includes: %s\n", dos_errstr(werr));
881 for (count = 0; count < num_includes; count++) {
882 d_printf("include = %s\n", includes[count]);
888 TALLOC_FREE(mem_ctx);
892 static int net_conf_setincludes(struct net_context *c,
893 struct smbconf_ctx *conf_ctx,
894 int argc, const char **argv)
898 uint32_t num_includes;
899 const char **includes;
901 TALLOC_CTX *mem_ctx = talloc_stackframe();
904 net_conf_setincludes_usage(c, argc, argv);
908 service = talloc_strdup_lower(mem_ctx, argv[0]);
909 if (service == NULL) {
910 d_printf("error: out of memory!\n");
914 num_includes = argc - 1;
915 if (num_includes == 0) {
921 werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
922 if (!W_ERROR_IS_OK(werr)) {
923 d_printf("error setting includes: %s\n", dos_errstr(werr));
930 TALLOC_FREE(mem_ctx);
934 static int net_conf_delincludes(struct net_context *c,
935 struct smbconf_ctx *conf_ctx,
936 int argc, const char **argv)
941 TALLOC_CTX *mem_ctx = talloc_stackframe();
944 net_conf_delincludes_usage(c, argc, argv);
948 service = talloc_strdup_lower(mem_ctx, argv[0]);
949 if (service == NULL) {
950 d_printf("error: out of memory!\n");
954 werr = smbconf_delete_includes(conf_ctx, service);
955 if (!W_ERROR_IS_OK(werr)) {
956 d_printf("error deleting includes: %s\n", dos_errstr(werr));
963 TALLOC_FREE(mem_ctx);
968 /**********************************************************************
970 * Wrapper and net_conf_run_function mechanism.
972 **********************************************************************/
975 * Wrapper function to call the main conf functions.
976 * The wrapper calls handles opening and closing of the
979 static int net_conf_wrap_function(struct net_context *c,
980 int (*fn)(struct net_context *,
981 struct smbconf_ctx *,
983 int argc, const char **argv)
986 TALLOC_CTX *mem_ctx = talloc_stackframe();
987 struct smbconf_ctx *conf_ctx;
990 werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
992 if (!W_ERROR_IS_OK(werr)) {
996 ret = fn(c, conf_ctx, argc, argv);
998 smbconf_shutdown(conf_ctx);
1004 * We need a functable struct of our own, because the
1005 * functions are called through a wrapper that handles
1006 * the opening and closing of the configuration, and so on.
1008 struct conf_functable {
1009 const char *funcname;
1010 int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1012 const char *helptext;
1016 * This imitates net_run_function2 but calls the main functions
1017 * through the wrapper net_conf_wrap_function().
1019 static int net_conf_run_function(struct net_context *c, int argc,
1020 const char **argv, const char *whoami,
1021 struct conf_functable *table)
1026 for (i=0; table[i].funcname; i++) {
1027 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1028 return net_conf_wrap_function(c, table[i].fn,
1034 for (i=0; table[i].funcname; i++) {
1035 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1043 * Entry-point for all the CONF functions.
1046 int net_conf(struct net_context *c, int argc, const char **argv)
1049 struct conf_functable func_table[] = {
1050 {"list", net_conf_list,
1051 "Dump the complete configuration in smb.conf like format."},
1052 {"import", net_conf_import,
1053 "Import configuration from file in smb.conf format."},
1054 {"listshares", net_conf_listshares,
1055 "List the share names."},
1056 {"drop", net_conf_drop,
1057 "Delete the complete configuration."},
1058 {"showshare", net_conf_showshare,
1059 "Show the definition of a share."},
1060 {"addshare", net_conf_addshare,
1061 "Create a new share."},
1062 {"delshare", net_conf_delshare,
1064 {"setparm", net_conf_setparm,
1065 "Store a parameter."},
1066 {"getparm", net_conf_getparm,
1067 "Retrieve the value of a parameter."},
1068 {"delparm", net_conf_delparm,
1069 "Delete a parameter."},
1070 {"getincludes", net_conf_getincludes,
1071 "Show the includes of a share definition."},
1072 {"setincludes", net_conf_setincludes,
1073 "Set includes for a share."},
1074 {"delincludes", net_conf_delincludes,
1075 "Delete includes from a share definition."},
1079 ret = net_conf_run_function(c, argc, argv, "net conf", func_table);