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 the Samba's configuration as made available
23 * by the libnet_conf interface (source/libnet/libnet_conf.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"
32 #include "libnet/libnet.h"
38 static int net_conf_list_usage(int argc, const char **argv)
40 d_printf("USAGE: net conf list\n");
44 static int net_conf_import_usage(int argc, const char**argv)
46 d_printf("USAGE: net conf import [--test|-T] <filename> "
48 "\t[--test|-T] testmode - do not act, just print "
49 "what would be done\n"
50 "\t<servicename> only import service <servicename>, "
55 static int net_conf_listshares_usage(int argc, const char **argv)
57 d_printf("USAGE: net conf listshares\n");
61 static int net_conf_drop_usage(int argc, const char **argv)
63 d_printf("USAGE: net conf drop\n");
67 static int net_conf_showshare_usage(int argc, const char **argv)
69 d_printf("USAGE: net conf showshare <sharename>\n");
73 static int net_conf_addshare_usage(int argc, const char **argv)
75 d_printf("USAGE: net conf addshare <sharename> <path> "
76 "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
77 "\t<sharename> the new share name.\n"
78 "\t<path> the path on the filesystem to export.\n"
79 "\twriteable={y|N} set \"writeable to \"yes\" or "
80 "\"no\" (default) on this share.\n"
81 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
82 "\"no\" (default) on this share.\n"
83 "\t<comment> optional comment for the new share.\n");
87 static int net_conf_delshare_usage(int argc, const char **argv)
89 d_printf("USAGE: net conf delshare <sharename>\n");
93 static int net_conf_setparm_usage(int argc, const char **argv)
95 d_printf("USAGE: net conf setparm <section> <param> <value>\n");
99 static int net_conf_getparm_usage(int argc, const char **argv)
101 d_printf("USAGE: net conf getparm <section> <param>\n");
105 static int net_conf_delparm_usage(int argc, const char **argv)
107 d_printf("USAGE: net conf delparm <section> <param>\n");
116 static char *parm_valstr(TALLOC_CTX *ctx, struct parm_struct *parm,
117 struct share_params *share)
121 void *ptr = parm->ptr;
123 if (parm->p_class == P_LOCAL && share->service >= 0) {
124 ptr = lp_local_ptr(share->service, ptr);
127 switch (parm->type) {
129 valstr = talloc_asprintf(ctx, "%c", *(char *)ptr);
133 valstr = talloc_asprintf(ctx, "%s", *(char **)ptr);
136 valstr = talloc_asprintf(ctx, "%s", BOOLSTR(*(bool *)ptr));
139 valstr = talloc_asprintf(ctx, "%s", BOOLSTR(!*(bool *)ptr));
142 for (i = 0; parm->enum_list[i].name; i++) {
143 if (*(int *)ptr == parm->enum_list[i].value)
145 valstr = talloc_asprintf(ctx, "%s",
146 parm->enum_list[i].name);
152 char *o = octal_string(*(int *)ptr);
153 valstr = talloc_move(ctx, &o);
157 valstr = talloc_strdup(ctx, "");
158 if ((char ***)ptr && *(char ***)ptr) {
159 char **list = *(char ***)ptr;
160 for (; *list; list++) {
161 /* surround strings with whitespace
162 * in double quotes */
163 if (strchr_m(*list, ' '))
165 valstr = talloc_asprintf_append(
168 ((*(list+1))?", ":""));
170 valstr = talloc_asprintf_append(
171 valstr, "%s%s", *list,
172 ((*(list+1))?", ":""));
178 valstr = talloc_asprintf(ctx, "%d", *(int *)ptr);
183 valstr = talloc_asprintf(ctx, "<type unimplemented>\n");
190 static int import_process_service(TALLOC_CTX *ctx,
191 struct share_params *share)
194 struct parm_struct *parm;
196 const char *servicename;
199 TALLOC_CTX *tmp_ctx = NULL;
201 tmp_ctx = talloc_new(ctx);
202 if (tmp_ctx == NULL) {
207 servicename = (share->service == GLOBAL_SECTION_SNUM)?
208 GLOBAL_NAME : lp_servicename(share->service);
211 d_printf("[%s]\n", servicename);
213 if (libnet_conf_share_exists(servicename)) {
214 werr = libnet_conf_delete_share(servicename);
215 if (!W_ERROR_IS_OK(werr)) {
221 while ((parm = lp_next_parameter(share->service, &pnum, 0)))
223 if ((share->service < 0) && (parm->p_class == P_LOCAL)
224 && !(parm->flags & FLAG_GLOBAL))
229 valstr = parm_valstr(tmp_ctx, parm, share);
231 if (parm->type != P_SEP) {
233 d_printf("\t%s = %s\n", parm->label, valstr);
235 werr = libnet_conf_set_parameter(servicename,
238 if (!W_ERROR_IS_OK(werr)) {
240 "Error setting parameter '%s'"
241 ": %s\n", parm->label,
256 TALLOC_FREE(tmp_ctx);
260 /* return true iff there are nondefault globals */
261 static bool globals_exist(void)
264 struct parm_struct *parm;
266 while ((parm = lp_next_parameter(GLOBAL_SECTION_SNUM, &i, 0)) != NULL) {
267 if (parm->type != P_SEP) {
278 static int net_conf_list(int argc, const char **argv)
280 WERROR werr = WERR_OK;
285 uint32_t *num_params;
287 char ***param_values;
288 uint32_t share_count, param_count;
290 ctx = talloc_init("list");
293 net_conf_list_usage(argc, argv);
297 werr = libnet_conf_get_config(ctx, &num_shares, &share_names,
298 &num_params, ¶m_names,
300 if (!W_ERROR_IS_OK(werr)) {
301 d_fprintf(stderr, "Error getting config: %s\n",
306 for (share_count = 0; share_count < num_shares; share_count++) {
307 d_printf("[%s]\n", share_names[share_count]);
308 for (param_count = 0; param_count < num_params[share_count];
311 d_printf("\t%s = %s\n",
312 param_names[share_count][param_count],
313 param_values[share_count][param_count]);
325 static int net_conf_import(int argc, const char **argv)
328 const char *filename = NULL;
329 const char *servicename = NULL;
330 bool service_found = false;
332 struct share_iterator *shares;
333 struct share_params *share;
334 struct share_params global_share = { GLOBAL_SECTION_SNUM };
336 ctx = talloc_init("net_conf_import");
341 net_conf_import_usage(argc, argv);
344 servicename = argv[1];
350 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
353 if (!lp_load(filename,
354 false, /* global_only */
355 true, /* save_defaults */
357 true)) /* initialize_globals */
359 d_fprintf(stderr, "Error parsing configuration file.\n");
364 d_printf("\nTEST MODE - "
365 "would import the following configuration:\n\n");
368 if (((servicename == NULL) && globals_exist()) ||
369 strequal(servicename, GLOBAL_NAME))
371 service_found = true;
372 if (import_process_service(ctx, &global_share) != 0) {
377 if (service_found && (servicename != NULL)) {
382 if (!(shares = share_list_all(ctx))) {
383 d_fprintf(stderr, "Could not list shares...\n");
386 while ((share = next_share(shares)) != NULL) {
387 if ((servicename == NULL)
388 || strequal(servicename, lp_servicename(share->service)))
390 service_found = true;
391 if (import_process_service(ctx, share)!= 0) {
397 if ((servicename != NULL) && !service_found) {
398 d_printf("Share %s not found in file %s\n",
399 servicename, filename);
411 static int net_conf_listshares(int argc, const char **argv)
413 WERROR werr = WERR_OK;
415 uint32_t count, num_shares = 0;
416 char **share_names = NULL;
419 ctx = talloc_init("listshares");
422 net_conf_listshares_usage(argc, argv);
426 werr = libnet_conf_get_share_names(ctx, &num_shares, &share_names);
427 if (!W_ERROR_IS_OK(werr)) {
431 for (count = 0; count < num_shares; count++)
433 d_printf("%s\n", share_names[count]);
443 static int net_conf_drop(int argc, const char **argv)
449 net_conf_drop_usage(argc, argv);
453 werr = libnet_conf_drop();
454 if (!W_ERROR_IS_OK(werr)) {
455 d_fprintf(stderr, "Error deleting configuration: %s\n",
466 static int net_conf_showshare(int argc, const char **argv)
469 WERROR werr = WERR_OK;
470 const char *sharename = NULL;
477 ctx = talloc_init("showshare");
480 net_conf_showshare_usage(argc, argv);
486 werr = libnet_conf_get_share(ctx, sharename, &num_params,
487 ¶m_names, ¶m_values);
488 if (!W_ERROR_IS_OK(werr)) {
489 d_printf("error getting share parameters: %s\n",
494 d_printf("[%s]\n", sharename);
496 for (count = 0; count < num_params; count++) {
497 d_printf("\t%s = %s\n", param_names[count],
498 param_values[count]);
509 * Add a share, with a couple of standard parameters, partly optional.
511 * This is a high level utility function of the net conf utility,
512 * not a direct frontend to the libnet_conf API.
514 static int net_conf_addshare(int argc, const char **argv)
517 WERROR werr = WERR_OK;
518 char *sharename = NULL;
519 const char *path = NULL;
520 const char *comment = NULL;
521 const char *guest_ok = "no";
522 const char *writeable = "no";
523 SMB_STRUCT_STAT sbuf;
529 net_conf_addshare_usage(argc, argv);
534 if (!strnequal(argv[3], "guest_ok=", 9)) {
535 net_conf_addshare_usage(argc, argv);
538 switch (argv[3][9]) {
548 net_conf_addshare_usage(argc, argv);
552 if (!strnequal(argv[2], "writeable=", 10)) {
553 net_conf_addshare_usage(argc, argv);
556 switch (argv[2][10]) {
566 net_conf_addshare_usage(argc, argv);
571 sharename = strdup_lower(argv[0]);
579 /* validate share name */
581 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
584 d_fprintf(stderr, "ERROR: share name %s contains "
585 "invalid characters (any of %s)\n",
586 sharename, INVALID_SHARENAME_CHARS);
590 if (getpwnam(sharename)) {
591 d_fprintf(stderr, "ERROR: share name %s is already a valid "
592 "system user name.\n", sharename);
596 if (strequal(sharename, GLOBAL_NAME)) {
598 "ERROR: 'global' is not a valid share name.\n");
602 if (libnet_conf_share_exists(sharename)) {
603 d_fprintf(stderr, "ERROR: share %s already exists.\n",
610 if (path[0] != '/') {
612 "Error: path '%s' is not an absolute path.\n",
617 if (sys_stat(path, &sbuf) != 0) {
619 "ERROR: cannot stat path '%s' to ensure "
620 "this is a directory.\n"
622 path, strerror(errno));
626 if (!S_ISDIR(sbuf.st_mode)) {
628 "ERROR: path '%s' is not a directory.\n",
637 werr = libnet_conf_create_share(sharename);
638 if (!W_ERROR_IS_OK(werr)) {
639 d_fprintf(stderr, "Error creating share %s: %s\n",
640 sharename, dos_errstr(werr));
645 * fill the share with parameters
648 werr = libnet_conf_set_parameter(sharename, "path", path);
649 if (!W_ERROR_IS_OK(werr)) {
650 d_fprintf(stderr, "Error setting parameter %s: %s\n",
651 "path", dos_errstr(werr));
655 if (comment != NULL) {
656 werr = libnet_conf_set_parameter(sharename, "comment", comment);
657 if (!W_ERROR_IS_OK(werr)) {
658 d_fprintf(stderr, "Error setting parameter %s: %s\n",
659 "comment", dos_errstr(werr));
664 werr = libnet_conf_set_parameter(sharename, "guest ok", guest_ok);
665 if (!W_ERROR_IS_OK(werr)) {
666 d_fprintf(stderr, "Error setting parameter %s: %s\n",
667 "'guest ok'", dos_errstr(werr));
671 werr = libnet_conf_set_parameter(sharename, "writeable", writeable);
672 if (!W_ERROR_IS_OK(werr)) {
673 d_fprintf(stderr, "Error setting parameter %s: %s\n",
674 "writeable", dos_errstr(werr));
681 SAFE_FREE(sharename);
685 static int net_conf_delshare(int argc, const char **argv)
688 const char *sharename = NULL;
689 WERROR werr = WERR_OK;
692 net_conf_delshare_usage(argc, argv);
697 werr = libnet_conf_delete_share(sharename);
698 if (!W_ERROR_IS_OK(werr)) {
699 d_fprintf(stderr, "Error deleting share %s: %s\n",
700 sharename, dos_errstr(werr));
709 static int net_conf_setparm(int argc, const char **argv)
712 WERROR werr = WERR_OK;
713 char *service = NULL;
715 const char *value_str = NULL;
718 net_conf_setparm_usage(argc, argv);
721 service = strdup_lower(argv[0]);
722 param = strdup_lower(argv[1]);
725 if (!libnet_conf_share_exists(service)) {
726 werr = libnet_conf_create_share(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 = libnet_conf_set_parameter(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));
750 static int net_conf_getparm(int argc, const char **argv)
753 WERROR werr = WERR_OK;
754 char *service = NULL;
759 ctx = talloc_init("getparm");
762 net_conf_getparm_usage(argc, argv);
765 service = strdup_lower(argv[0]);
766 param = strdup_lower(argv[1]);
768 werr = libnet_conf_get_parameter(ctx, service, param, &valstr);
770 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
772 "Error: given service '%s' does not exist.\n",
775 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
777 "Error: given parameter '%s' is not set.\n",
780 } else if (!W_ERROR_IS_OK(werr)) {
781 d_fprintf(stderr, "Error getting value '%s': %s.\n",
782 param, dos_errstr(werr));
786 d_printf("%s\n", valstr);
796 static int net_conf_delparm(int argc, const char **argv)
799 WERROR werr = WERR_OK;
800 char *service = NULL;
804 net_conf_delparm_usage(argc, argv);
807 service = strdup_lower(argv[0]);
808 param = strdup_lower(argv[1]);
810 werr = libnet_conf_delete_parameter(service, param);
812 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
814 "Error: given service '%s' does not exist.\n",
817 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
819 "Error: given parameter '%s' is not set.\n",
822 } else if (!W_ERROR_IS_OK(werr)) {
823 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
824 param, dos_errstr(werr));
837 * Entry-point for all the CONF functions.
840 int net_conf(int argc, const char **argv)
843 struct functable2 func[] = {
844 {"list", net_conf_list,
845 "Dump the complete configuration in smb.conf like format."},
846 {"import", net_conf_import,
847 "Import configuration from file in smb.conf format."},
848 {"listshares", net_conf_listshares,
849 "List the share names."},
850 {"drop", net_conf_drop,
851 "Delete the complete configuration."},
852 {"showshare", net_conf_showshare,
853 "Show the definition of a share."},
854 {"addshare", net_conf_addshare,
855 "Create a new share."},
856 {"delshare", net_conf_delshare,
858 {"setparm", net_conf_setparm,
859 "Store a parameter."},
860 {"getparm", net_conf_getparm,
861 "Retrieve the value of a parameter."},
862 {"delparm", net_conf_delparm,
863 "Delete a parameter."},
867 ret = net_run_function2(argc, argv, "net conf", func);