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 WERROR list_values(TALLOC_CTX *ctx, struct registry_key *key)
115 WERROR werr = WERR_OK;
117 struct registry_value *valvalue = NULL;
118 char *valname = NULL;
121 W_ERROR_IS_OK(werr = reg_enumvalue(ctx, key, idx, &valname,
125 d_printf("\t%s = %s\n", valname,
126 libnet_smbconf_format_registry_value(ctx, valvalue));
128 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
129 d_fprintf(stderr, "Error enumerating values: %s\n",
139 static char *parm_valstr(TALLOC_CTX *ctx, struct parm_struct *parm,
140 struct share_params *share)
144 void *ptr = parm->ptr;
146 if (parm->p_class == P_LOCAL && share->service >= 0) {
147 ptr = lp_local_ptr(share->service, ptr);
150 switch (parm->type) {
152 valstr = talloc_asprintf(ctx, "%c", *(char *)ptr);
156 valstr = talloc_asprintf(ctx, "%s", *(char **)ptr);
159 valstr = talloc_asprintf(ctx, "%s", BOOLSTR(*(bool *)ptr));
162 valstr = talloc_asprintf(ctx, "%s", BOOLSTR(!*(bool *)ptr));
165 for (i = 0; parm->enum_list[i].name; i++) {
166 if (*(int *)ptr == parm->enum_list[i].value)
168 valstr = talloc_asprintf(ctx, "%s",
169 parm->enum_list[i].name);
175 char *o = octal_string(*(int *)ptr);
176 valstr = talloc_move(ctx, &o);
180 valstr = talloc_strdup(ctx, "");
181 if ((char ***)ptr && *(char ***)ptr) {
182 char **list = *(char ***)ptr;
183 for (; *list; list++) {
184 /* surround strings with whitespace
185 * in double quotes */
186 if (strchr_m(*list, ' '))
188 valstr = talloc_asprintf_append(
191 ((*(list+1))?", ":""));
193 valstr = talloc_asprintf_append(
194 valstr, "%s%s", *list,
195 ((*(list+1))?", ":""));
201 valstr = talloc_asprintf(ctx, "%d", *(int *)ptr);
206 valstr = talloc_asprintf(ctx, "<type unimplemented>\n");
213 static int import_process_service(TALLOC_CTX *ctx,
214 struct share_params *share)
217 struct parm_struct *parm;
219 const char *servicename;
220 struct registry_key *key;
223 TALLOC_CTX *tmp_ctx = NULL;
225 tmp_ctx = talloc_new(ctx);
226 if (tmp_ctx == NULL) {
231 servicename = (share->service == GLOBAL_SECTION_SNUM)?
232 GLOBAL_NAME : lp_servicename(share->service);
235 d_printf("[%s]\n", servicename);
237 if (libnet_smbconf_key_exists(servicename)) {
238 werr = libnet_smbconf_delshare(servicename);
239 if (!W_ERROR_IS_OK(werr)) {
243 werr = libnet_smbconf_reg_createkey_internal(tmp_ctx, servicename, &key);
244 if (!W_ERROR_IS_OK(werr)) {
249 while ((parm = lp_next_parameter(share->service, &pnum, 0)))
251 if ((share->service < 0 && parm->p_class == P_LOCAL)
252 && !(parm->flags & FLAG_GLOBAL))
255 valstr = parm_valstr(tmp_ctx, parm, share);
257 if (parm->type != P_SEP) {
259 d_printf("\t%s = %s\n", parm->label, valstr);
261 werr = libnet_smbconf_reg_setvalue_internal(key,
262 parm->label, valstr);
263 if (!W_ERROR_IS_OK(werr)) {
277 TALLOC_FREE(tmp_ctx);
281 /* return true iff there are nondefault globals */
282 static bool globals_exist(void)
285 struct parm_struct *parm;
287 while ((parm = lp_next_parameter(GLOBAL_SECTION_SNUM, &i, 0)) != NULL) {
288 if (parm->type != P_SEP) {
299 static int net_conf_list(int argc, const char **argv)
301 WERROR werr = WERR_OK;
304 struct registry_key *base_key = NULL;
305 struct registry_key *sub_key = NULL;
307 char *subkey_name = NULL;
309 ctx = talloc_init("list");
312 net_conf_list_usage(argc, argv);
316 werr = libnet_smbconf_reg_open_basepath(ctx, REG_KEY_READ, &base_key);
317 if (!W_ERROR_IS_OK(werr)) {
321 if (libnet_smbconf_key_exists(GLOBAL_NAME)) {
322 werr = reg_openkey(ctx, base_key, GLOBAL_NAME,
323 REG_KEY_READ, &sub_key);
324 if (!W_ERROR_IS_OK(werr)) {
325 d_fprintf(stderr, "Error opening subkey '%s' : %s\n",
326 subkey_name, dos_errstr(werr));
329 d_printf("[%s]\n", GLOBAL_NAME);
330 if (!W_ERROR_IS_OK(list_values(ctx, sub_key))) {
337 W_ERROR_IS_OK(werr = reg_enumkey(ctx, base_key, idx_key,
338 &subkey_name, NULL));
341 if (strequal(subkey_name, GLOBAL_NAME)) {
344 d_printf("[%s]\n", subkey_name);
346 werr = reg_openkey(ctx, base_key, subkey_name,
347 REG_KEY_READ, &sub_key);
348 if (!W_ERROR_IS_OK(werr)) {
350 "Error opening subkey '%s': %s\n",
351 subkey_name, dos_errstr(werr));
354 if (!W_ERROR_IS_OK(list_values(ctx, sub_key))) {
359 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
360 d_fprintf(stderr, "Error enumerating subkeys: %s\n",
372 static int net_conf_import(int argc, const char **argv)
375 const char *filename = NULL;
376 const char *servicename = NULL;
377 bool service_found = false;
379 struct share_iterator *shares;
380 struct share_params *share;
381 struct share_params global_share = { GLOBAL_SECTION_SNUM };
383 ctx = talloc_init("net_conf_import");
388 net_conf_import_usage(argc, argv);
391 servicename = argv[1];
397 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
400 if (!lp_load(filename,
401 false, /* global_only */
402 true, /* save_defaults */
404 true)) /* initialize_globals */
406 d_fprintf(stderr, "Error parsing configuration file.\n");
411 d_printf("\nTEST MODE - "
412 "would import the following configuration:\n\n");
415 if (((servicename == NULL) && globals_exist()) ||
416 strequal(servicename, GLOBAL_NAME))
418 service_found = true;
419 if (import_process_service(ctx, &global_share) != 0) {
424 if (service_found && (servicename != NULL)) {
429 if (!(shares = share_list_all(ctx))) {
430 d_fprintf(stderr, "Could not list shares...\n");
433 while ((share = next_share(shares)) != NULL) {
434 if ((servicename == NULL)
435 || strequal(servicename, lp_servicename(share->service)))
437 service_found = true;
438 if (import_process_service(ctx, share)!= 0) {
444 if ((servicename != NULL) && !service_found) {
445 d_printf("Share %s not found in file %s\n",
446 servicename, filename);
458 static int net_conf_listshares(int argc, const char **argv)
460 WERROR werr = WERR_OK;
462 uint32_t count, num_shares = 0;
463 char **share_names = NULL;
466 ctx = talloc_init("listshares");
469 net_conf_listshares_usage(argc, argv);
473 werr = libnet_smbconf_get_share_names(ctx, &num_shares, &share_names);
474 if (!W_ERROR_IS_OK(werr)) {
478 for (count = 0; count < num_shares; count++)
480 d_printf("%s\n", share_names[count]);
490 static int net_conf_drop(int argc, const char **argv)
496 net_conf_drop_usage(argc, argv);
500 werr = libnet_smbconf_drop();
501 if (!W_ERROR_IS_OK(werr)) {
502 d_fprintf(stderr, "Error deleting configuration: %s\n",
513 static int net_conf_showshare(int argc, const char **argv)
516 WERROR werr = WERR_OK;
517 struct registry_key *key = NULL;
518 const char *sharename = NULL;
525 ctx = talloc_init("showshare");
528 net_conf_showshare_usage(argc, argv);
534 werr = libnet_smbconf_getshare(ctx, sharename, &num_params,
535 ¶m_names, ¶m_values);
536 if (!W_ERROR_IS_OK(werr)) {
537 d_printf("error getting share parameters: %s\n",
542 d_printf("[%s]\n", sharename);
544 for (count = 0; count < num_params; count++) {
545 d_printf("\t%s = %s\n", param_names[count],
546 param_values[count]);
556 static int net_conf_addshare(int argc, const char **argv)
559 WERROR werr = WERR_OK;
560 struct registry_key *newkey = NULL;
561 char *sharename = NULL;
562 const char *path = NULL;
563 const char *comment = NULL;
564 const char *guest_ok = "no";
565 const char *writeable = "no";
566 SMB_STRUCT_STAT sbuf;
572 net_conf_addshare_usage(argc, argv);
577 if (!strnequal(argv[3], "guest_ok=", 9)) {
578 net_conf_addshare_usage(argc, argv);
581 switch (argv[3][9]) {
591 net_conf_addshare_usage(argc, argv);
595 if (!strnequal(argv[2], "writeable=", 10)) {
596 net_conf_addshare_usage(argc, argv);
599 switch (argv[2][10]) {
609 net_conf_addshare_usage(argc, argv);
615 sharename = strdup_lower(argv[0]);
623 /* validate share name */
625 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
628 d_fprintf(stderr, "ERROR: share name %s contains "
629 "invalid characters (any of %s)\n",
630 sharename, INVALID_SHARENAME_CHARS);
634 if (getpwnam(sharename)) {
635 d_fprintf(stderr, "ERROR: share name %s is already a valid "
636 "system user name.\n", sharename);
640 if (strequal(sharename, GLOBAL_NAME)) {
642 "ERROR: 'global' is not a valid share name.\n");
648 if (path[0] != '/') {
650 "Error: path '%s' is not an absolute path.\n",
655 if (sys_stat(path, &sbuf) != 0) {
657 "ERROR: cannot stat path '%s' to ensure "
658 "this is a directory.\n"
660 path, strerror(errno));
664 if (!S_ISDIR(sbuf.st_mode)) {
666 "ERROR: path '%s' is not a directory.\n",
675 werr = libnet_smbconf_reg_createkey_internal(NULL, argv[0], &newkey);
676 if (!W_ERROR_IS_OK(werr)) {
680 /* add config params as values */
682 werr = libnet_smbconf_reg_setvalue_internal(newkey, "path", path);
683 if (!W_ERROR_IS_OK(werr))
686 if (comment != NULL) {
687 werr = libnet_smbconf_reg_setvalue_internal(newkey, "comment",
689 if (!W_ERROR_IS_OK(werr))
693 werr = libnet_smbconf_reg_setvalue_internal(newkey, "guest ok",
695 if (!W_ERROR_IS_OK(werr))
698 werr = libnet_smbconf_reg_setvalue_internal(newkey, "writeable",
700 if (!W_ERROR_IS_OK(werr))
707 SAFE_FREE(sharename);
711 static int net_conf_delshare(int argc, const char **argv)
714 const char *sharename = NULL;
715 WERROR werr = WERR_OK;
718 net_conf_delshare_usage(argc, argv);
723 werr = libnet_smbconf_delshare(sharename);
724 if (!W_ERROR_IS_OK(werr)) {
725 d_fprintf(stderr, "Error deleting share %s: %s\n",
726 sharename, dos_errstr(werr));
735 static int net_conf_setparm(int argc, const char **argv)
738 WERROR werr = WERR_OK;
739 char *service = NULL;
741 const char *value_str = NULL;
744 net_conf_setparm_usage(argc, argv);
747 service = strdup_lower(argv[0]);
748 param = strdup_lower(argv[1]);
751 werr = libnet_smbconf_setparm(service, param, value_str);
753 if (!W_ERROR_IS_OK(werr)) {
754 d_fprintf(stderr, "Error setting value '%s': %s\n",
755 param, dos_errstr(werr));
767 static int net_conf_getparm(int argc, const char **argv)
770 WERROR werr = WERR_OK;
771 char *service = NULL;
776 ctx = talloc_init("getparm");
779 net_conf_getparm_usage(argc, argv);
782 service = strdup_lower(argv[0]);
783 param = strdup_lower(argv[1]);
785 werr = libnet_smbconf_getparm(ctx, service, param, &valstr);
787 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
789 "Error: given service '%s' does not exist.\n",
792 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
794 "Error: given parameter '%s' is not set.\n",
797 } else if (!W_ERROR_IS_OK(werr)) {
798 d_fprintf(stderr, "Error getting value '%s': %s.\n",
799 param, dos_errstr(werr));
803 d_printf("%s\n", valstr);
813 static int net_conf_delparm(int argc, const char **argv)
816 WERROR werr = WERR_OK;
817 char *service = NULL;
821 net_conf_delparm_usage(argc, argv);
824 service = strdup_lower(argv[0]);
825 param = strdup_lower(argv[1]);
827 werr = libnet_smbconf_delparm(service, param);
829 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
831 "Error: given service '%s' does not exist.\n",
834 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
836 "Error: given parameter '%s' is not set.\n",
839 } else if (!W_ERROR_IS_OK(werr)) {
840 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
841 param, dos_errstr(werr));
854 * Entry-point for all the CONF functions.
857 int net_conf(int argc, const char **argv)
860 struct functable2 func[] = {
861 {"list", net_conf_list,
862 "Dump the complete configuration in smb.conf like format."},
863 {"import", net_conf_import,
864 "Import configuration from file in smb.conf format."},
865 {"listshares", net_conf_listshares,
866 "List the registry shares."},
867 {"drop", net_conf_drop,
868 "Delete the complete configuration from registry."},
869 {"showshare", net_conf_showshare,
870 "Show the definition of a registry share."},
871 {"addshare", net_conf_addshare,
872 "Create a new registry share."},
873 {"delshare", net_conf_delshare,
874 "Delete a registry share."},
875 {"setparm", net_conf_setparm,
876 "Store a parameter."},
877 {"getparm", net_conf_getparm,
878 "Retrieve the value of a parameter."},
879 {"delparm", net_conf_delparm,
880 "Delete a parameter."},
884 if (!registry_init_regdb()) {
885 d_fprintf(stderr, "Error initializing the registry!\n");
889 ret = net_run_function2(argc, argv, "net conf", func);