2 * Samba Unix/Linux SMB client library
3 * Distributed SMB/CIFS Server Management Utility
4 * Local configuration interface
5 * Copyright (C) Michael Adam 2007
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 configuration stored inside the
23 * samba registry. In the future there might be support for other
24 * configuration backends as well.
28 #include "utils/net.h"
29 #include "libnet/libnet.h"
35 static int net_conf_list_usage(int argc, const char **argv)
37 d_printf("USAGE: net conf list\n");
41 static int net_conf_import_usage(int argc, const char**argv)
43 d_printf("USAGE: net conf import [--test|-T] <filename> "
45 "\t[--test|-T] testmode - do not act, just print "
46 "what would be done\n"
47 "\t<servicename> only import service <servicename>, "
52 static int net_conf_listshares_usage(int argc, const char **argv)
54 d_printf("USAGE: net conf listshares\n");
58 static int net_conf_drop_usage(int argc, const char **argv)
60 d_printf("USAGE: net conf drop\n");
64 static int net_conf_showshare_usage(int argc, const char **argv)
66 d_printf("USAGE: net conf showshare <sharename>\n");
70 static int net_conf_addshare_usage(int argc, const char **argv)
72 d_printf("USAGE: net conf addshare <sharename> <path> "
73 "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
74 "\t<sharename> the new share name.\n"
75 "\t<path> the path on the filesystem to export.\n"
76 "\twriteable={y|N} set \"writeable to \"yes\" or "
77 "\"no\" (default) on this share.\n"
78 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
79 "\"no\" (default) on this share.\n"
80 "\t<comment> optional comment for the new share.\n");
84 static int net_conf_delshare_usage(int argc, const char **argv)
86 d_printf("USAGE: net conf delshare <sharename>\n");
90 static int net_conf_setparm_usage(int argc, const char **argv)
92 d_printf("USAGE: net conf setparm <section> <param> <value>\n");
96 static int net_conf_getparm_usage(int argc, const char **argv)
98 d_printf("USAGE: net conf getparm <section> <param>\n");
102 static int net_conf_delparm_usage(int argc, const char **argv)
104 d_printf("USAGE: net conf delparm <section> <param>\n");
113 static char *parm_valstr(TALLOC_CTX *ctx, struct parm_struct *parm,
114 struct share_params *share)
118 void *ptr = parm->ptr;
120 if (parm->p_class == P_LOCAL && share->service >= 0) {
121 ptr = lp_local_ptr(share->service, ptr);
124 switch (parm->type) {
126 valstr = talloc_asprintf(ctx, "%c", *(char *)ptr);
130 valstr = talloc_asprintf(ctx, "%s", *(char **)ptr);
133 valstr = talloc_asprintf(ctx, "%s", BOOLSTR(*(bool *)ptr));
136 valstr = talloc_asprintf(ctx, "%s", BOOLSTR(!*(bool *)ptr));
139 for (i = 0; parm->enum_list[i].name; i++) {
140 if (*(int *)ptr == parm->enum_list[i].value)
142 valstr = talloc_asprintf(ctx, "%s",
143 parm->enum_list[i].name);
149 char *o = octal_string(*(int *)ptr);
150 valstr = talloc_move(ctx, &o);
154 valstr = talloc_strdup(ctx, "");
155 if ((char ***)ptr && *(char ***)ptr) {
156 char **list = *(char ***)ptr;
157 for (; *list; list++) {
158 /* surround strings with whitespace
159 * in double quotes */
160 if (strchr_m(*list, ' '))
162 valstr = talloc_asprintf_append(
165 ((*(list+1))?", ":""));
167 valstr = talloc_asprintf_append(
168 valstr, "%s%s", *list,
169 ((*(list+1))?", ":""));
175 valstr = talloc_asprintf(ctx, "%d", *(int *)ptr);
180 valstr = talloc_asprintf(ctx, "<type unimplemented>\n");
187 static int import_process_service(TALLOC_CTX *ctx,
188 struct share_params *share)
191 struct parm_struct *parm;
193 const char *servicename;
196 TALLOC_CTX *tmp_ctx = NULL;
198 tmp_ctx = talloc_new(ctx);
199 if (tmp_ctx == NULL) {
204 servicename = (share->service == GLOBAL_SECTION_SNUM)?
205 GLOBAL_NAME : lp_servicename(share->service);
208 d_printf("[%s]\n", servicename);
210 if (libnet_smbconf_share_exists(servicename)) {
211 werr = libnet_smbconf_delshare(servicename);
212 if (!W_ERROR_IS_OK(werr)) {
218 while ((parm = lp_next_parameter(share->service, &pnum, 0)))
220 if ((share->service < 0) && (parm->p_class == P_LOCAL)
221 && !(parm->flags & FLAG_GLOBAL))
226 valstr = parm_valstr(tmp_ctx, parm, share);
228 if (parm->type != P_SEP) {
230 d_printf("\t%s = %s\n", parm->label, valstr);
232 werr = libnet_smbconf_setparm(servicename,
235 if (!W_ERROR_IS_OK(werr)) {
237 "Error setting parameter '%s'"
238 ": %s\n", parm->label,
253 TALLOC_FREE(tmp_ctx);
257 /* return true iff there are nondefault globals */
258 static bool globals_exist(void)
261 struct parm_struct *parm;
263 while ((parm = lp_next_parameter(GLOBAL_SECTION_SNUM, &i, 0)) != NULL) {
264 if (parm->type != P_SEP) {
275 static int net_conf_list(int argc, const char **argv)
277 WERROR werr = WERR_OK;
282 uint32_t *num_params;
284 char ***param_values;
285 uint32_t share_count, param_count;
287 ctx = talloc_init("list");
290 net_conf_list_usage(argc, argv);
294 werr = libnet_smbconf_get_config(ctx, &num_shares, &share_names,
295 &num_params, ¶m_names,
297 if (!W_ERROR_IS_OK(werr)) {
298 d_fprintf(stderr, "Error getting config: %s\n",
303 for (share_count = 0; share_count < num_shares; share_count++) {
304 d_printf("[%s]\n", share_names[share_count]);
305 for (param_count = 0; param_count < num_params[share_count];
308 d_printf("\t%s = %s\n",
309 param_names[share_count][param_count],
310 param_values[share_count][param_count]);
322 static int net_conf_import(int argc, const char **argv)
325 const char *filename = NULL;
326 const char *servicename = NULL;
327 bool service_found = false;
329 struct share_iterator *shares;
330 struct share_params *share;
331 struct share_params global_share = { GLOBAL_SECTION_SNUM };
333 ctx = talloc_init("net_conf_import");
338 net_conf_import_usage(argc, argv);
341 servicename = argv[1];
347 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
350 if (!lp_load(filename,
351 false, /* global_only */
352 true, /* save_defaults */
354 true)) /* initialize_globals */
356 d_fprintf(stderr, "Error parsing configuration file.\n");
361 d_printf("\nTEST MODE - "
362 "would import the following configuration:\n\n");
365 if (((servicename == NULL) && globals_exist()) ||
366 strequal(servicename, GLOBAL_NAME))
368 service_found = true;
369 if (import_process_service(ctx, &global_share) != 0) {
374 if (service_found && (servicename != NULL)) {
379 if (!(shares = share_list_all(ctx))) {
380 d_fprintf(stderr, "Could not list shares...\n");
383 while ((share = next_share(shares)) != NULL) {
384 if ((servicename == NULL)
385 || strequal(servicename, lp_servicename(share->service)))
387 service_found = true;
388 if (import_process_service(ctx, share)!= 0) {
394 if ((servicename != NULL) && !service_found) {
395 d_printf("Share %s not found in file %s\n",
396 servicename, filename);
408 static int net_conf_listshares(int argc, const char **argv)
410 WERROR werr = WERR_OK;
412 uint32_t count, num_shares = 0;
413 char **share_names = NULL;
416 ctx = talloc_init("listshares");
419 net_conf_listshares_usage(argc, argv);
423 werr = libnet_smbconf_get_share_names(ctx, &num_shares, &share_names);
424 if (!W_ERROR_IS_OK(werr)) {
428 for (count = 0; count < num_shares; count++)
430 d_printf("%s\n", share_names[count]);
440 static int net_conf_drop(int argc, const char **argv)
446 net_conf_drop_usage(argc, argv);
450 werr = libnet_smbconf_drop();
451 if (!W_ERROR_IS_OK(werr)) {
452 d_fprintf(stderr, "Error deleting configuration: %s\n",
463 static int net_conf_showshare(int argc, const char **argv)
466 WERROR werr = WERR_OK;
467 const char *sharename = NULL;
474 ctx = talloc_init("showshare");
477 net_conf_showshare_usage(argc, argv);
483 werr = libnet_smbconf_getshare(ctx, sharename, &num_params,
484 ¶m_names, ¶m_values);
485 if (!W_ERROR_IS_OK(werr)) {
486 d_printf("error getting share parameters: %s\n",
491 d_printf("[%s]\n", sharename);
493 for (count = 0; count < num_params; count++) {
494 d_printf("\t%s = %s\n", param_names[count],
495 param_values[count]);
506 * Add a share, with a couple of standard parameters, partly optional.
508 * This is a high level utility function of the net conf utility,
509 * not a direct frontend to the libnet_conf API.
511 static int net_conf_addshare(int argc, const char **argv)
514 WERROR werr = WERR_OK;
515 char *sharename = NULL;
516 const char *path = NULL;
517 const char *comment = NULL;
518 const char *guest_ok = "no";
519 const char *writeable = "no";
520 SMB_STRUCT_STAT sbuf;
526 net_conf_addshare_usage(argc, argv);
531 if (!strnequal(argv[3], "guest_ok=", 9)) {
532 net_conf_addshare_usage(argc, argv);
535 switch (argv[3][9]) {
545 net_conf_addshare_usage(argc, argv);
549 if (!strnequal(argv[2], "writeable=", 10)) {
550 net_conf_addshare_usage(argc, argv);
553 switch (argv[2][10]) {
563 net_conf_addshare_usage(argc, argv);
568 sharename = strdup_lower(argv[0]);
576 /* validate share name */
578 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
581 d_fprintf(stderr, "ERROR: share name %s contains "
582 "invalid characters (any of %s)\n",
583 sharename, INVALID_SHARENAME_CHARS);
587 if (getpwnam(sharename)) {
588 d_fprintf(stderr, "ERROR: share name %s is already a valid "
589 "system user name.\n", sharename);
593 if (strequal(sharename, GLOBAL_NAME)) {
595 "ERROR: 'global' is not a valid share name.\n");
599 if (libnet_smbconf_share_exists(sharename)) {
600 d_fprintf(stderr, "ERROR: share %s already exists.\n",
607 if (path[0] != '/') {
609 "Error: path '%s' is not an absolute path.\n",
614 if (sys_stat(path, &sbuf) != 0) {
616 "ERROR: cannot stat path '%s' to ensure "
617 "this is a directory.\n"
619 path, strerror(errno));
623 if (!S_ISDIR(sbuf.st_mode)) {
625 "ERROR: path '%s' is not a directory.\n",
631 * create the share by adding the parameters
634 werr = libnet_smbconf_setparm(sharename, "path", path);
635 if (!W_ERROR_IS_OK(werr)) {
636 d_fprintf(stderr, "Error setting parameter %s: %s\n",
637 "path", dos_errstr(werr));
641 if (comment != NULL) {
642 werr = libnet_smbconf_setparm(sharename, "comment", comment);
643 if (!W_ERROR_IS_OK(werr)) {
644 d_fprintf(stderr, "Error setting parameter %s: %s\n",
645 "comment", dos_errstr(werr));
650 werr = libnet_smbconf_setparm(sharename, "guest ok", guest_ok);
651 if (!W_ERROR_IS_OK(werr)) {
652 d_fprintf(stderr, "Error setting parameter %s: %s\n",
653 "'guest ok'", dos_errstr(werr));
657 werr = libnet_smbconf_setparm(sharename, "writeable", writeable);
658 if (!W_ERROR_IS_OK(werr)) {
659 d_fprintf(stderr, "Error setting parameter %s: %s\n",
660 "writeable", dos_errstr(werr));
667 SAFE_FREE(sharename);
671 static int net_conf_delshare(int argc, const char **argv)
674 const char *sharename = NULL;
675 WERROR werr = WERR_OK;
678 net_conf_delshare_usage(argc, argv);
683 werr = libnet_smbconf_delshare(sharename);
684 if (!W_ERROR_IS_OK(werr)) {
685 d_fprintf(stderr, "Error deleting share %s: %s\n",
686 sharename, dos_errstr(werr));
695 static int net_conf_setparm(int argc, const char **argv)
698 WERROR werr = WERR_OK;
699 char *service = NULL;
701 const char *value_str = NULL;
704 net_conf_setparm_usage(argc, argv);
707 service = strdup_lower(argv[0]);
708 param = strdup_lower(argv[1]);
711 werr = libnet_smbconf_setparm(service, param, value_str);
713 if (!W_ERROR_IS_OK(werr)) {
714 d_fprintf(stderr, "Error setting value '%s': %s\n",
715 param, dos_errstr(werr));
727 static int net_conf_getparm(int argc, const char **argv)
730 WERROR werr = WERR_OK;
731 char *service = NULL;
736 ctx = talloc_init("getparm");
739 net_conf_getparm_usage(argc, argv);
742 service = strdup_lower(argv[0]);
743 param = strdup_lower(argv[1]);
745 werr = libnet_smbconf_getparm(ctx, service, param, &valstr);
747 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
749 "Error: given service '%s' does not exist.\n",
752 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
754 "Error: given parameter '%s' is not set.\n",
757 } else if (!W_ERROR_IS_OK(werr)) {
758 d_fprintf(stderr, "Error getting value '%s': %s.\n",
759 param, dos_errstr(werr));
763 d_printf("%s\n", valstr);
773 static int net_conf_delparm(int argc, const char **argv)
776 WERROR werr = WERR_OK;
777 char *service = NULL;
781 net_conf_delparm_usage(argc, argv);
784 service = strdup_lower(argv[0]);
785 param = strdup_lower(argv[1]);
787 werr = libnet_smbconf_delparm(service, param);
789 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
791 "Error: given service '%s' does not exist.\n",
794 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
796 "Error: given parameter '%s' is not set.\n",
799 } else if (!W_ERROR_IS_OK(werr)) {
800 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
801 param, dos_errstr(werr));
814 * Entry-point for all the CONF functions.
817 int net_conf(int argc, const char **argv)
820 struct functable2 func[] = {
821 {"list", net_conf_list,
822 "Dump the complete configuration in smb.conf like format."},
823 {"import", net_conf_import,
824 "Import configuration from file in smb.conf format."},
825 {"listshares", net_conf_listshares,
826 "List the registry shares."},
827 {"drop", net_conf_drop,
828 "Delete the complete configuration from registry."},
829 {"showshare", net_conf_showshare,
830 "Show the definition of a registry share."},
831 {"addshare", net_conf_addshare,
832 "Create a new registry share."},
833 {"delshare", net_conf_delshare,
834 "Delete a registry share."},
835 {"setparm", net_conf_setparm,
836 "Store a parameter."},
837 {"getparm", net_conf_getparm,
838 "Retrieve the value of a parameter."},
839 {"delparm", net_conf_delparm,
840 "Delete a parameter."},
844 if (!registry_init_regdb()) {
845 d_fprintf(stderr, "Error initializing the registry!\n");
849 ret = net_run_function2(argc, argv, "net conf", func);