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;
194 struct registry_key *key;
197 TALLOC_CTX *tmp_ctx = NULL;
199 tmp_ctx = talloc_new(ctx);
200 if (tmp_ctx == NULL) {
205 servicename = (share->service == GLOBAL_SECTION_SNUM)?
206 GLOBAL_NAME : lp_servicename(share->service);
209 d_printf("[%s]\n", servicename);
211 if (libnet_smbconf_share_exists(servicename)) {
212 werr = libnet_smbconf_delshare(servicename);
213 if (!W_ERROR_IS_OK(werr)) {
217 werr = libnet_smbconf_reg_createkey_internal(tmp_ctx, servicename, &key);
218 if (!W_ERROR_IS_OK(werr)) {
219 d_fprintf(stderr, "Error creating share %s: %s\n",
220 servicename, dos_errstr(werr));
225 while ((parm = lp_next_parameter(share->service, &pnum, 0)))
227 if ((share->service < 0 && parm->p_class == P_LOCAL)
228 && !(parm->flags & FLAG_GLOBAL))
231 valstr = parm_valstr(tmp_ctx, parm, share);
233 if (parm->type != P_SEP) {
235 d_printf("\t%s = %s\n", parm->label, valstr);
237 werr = libnet_smbconf_reg_setvalue_internal(key,
238 parm->label, valstr);
239 if (!W_ERROR_IS_OK(werr)) {
241 "Error setting parameter '%s'"
242 ": %s\n", parm->label,
257 TALLOC_FREE(tmp_ctx);
261 /* return true iff there are nondefault globals */
262 static bool globals_exist(void)
265 struct parm_struct *parm;
267 while ((parm = lp_next_parameter(GLOBAL_SECTION_SNUM, &i, 0)) != NULL) {
268 if (parm->type != P_SEP) {
279 static int net_conf_list(int argc, const char **argv)
281 WERROR werr = WERR_OK;
286 uint32_t *num_params;
288 char ***param_values;
289 uint32_t share_count, param_count;
291 ctx = talloc_init("list");
294 net_conf_list_usage(argc, argv);
298 werr = libnet_smbconf_get_config(ctx, &num_shares, &share_names,
299 &num_params, ¶m_names,
301 if (!W_ERROR_IS_OK(werr)) {
302 d_fprintf(stderr, "Error getting config: %s\n",
307 for (share_count = 0; share_count < num_shares; share_count++) {
308 d_printf("[%s]\n", share_names[share_count]);
309 for (param_count = 0; param_count < num_params[share_count];
312 d_printf("\t%s = %s\n",
313 param_names[share_count][param_count],
314 param_values[share_count][param_count]);
326 static int net_conf_import(int argc, const char **argv)
329 const char *filename = NULL;
330 const char *servicename = NULL;
331 bool service_found = false;
333 struct share_iterator *shares;
334 struct share_params *share;
335 struct share_params global_share = { GLOBAL_SECTION_SNUM };
337 ctx = talloc_init("net_conf_import");
342 net_conf_import_usage(argc, argv);
345 servicename = argv[1];
351 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
354 if (!lp_load(filename,
355 false, /* global_only */
356 true, /* save_defaults */
358 true)) /* initialize_globals */
360 d_fprintf(stderr, "Error parsing configuration file.\n");
365 d_printf("\nTEST MODE - "
366 "would import the following configuration:\n\n");
369 if (((servicename == NULL) && globals_exist()) ||
370 strequal(servicename, GLOBAL_NAME))
372 service_found = true;
373 if (import_process_service(ctx, &global_share) != 0) {
378 if (service_found && (servicename != NULL)) {
383 if (!(shares = share_list_all(ctx))) {
384 d_fprintf(stderr, "Could not list shares...\n");
387 while ((share = next_share(shares)) != NULL) {
388 if ((servicename == NULL)
389 || strequal(servicename, lp_servicename(share->service)))
391 service_found = true;
392 if (import_process_service(ctx, share)!= 0) {
398 if ((servicename != NULL) && !service_found) {
399 d_printf("Share %s not found in file %s\n",
400 servicename, filename);
412 static int net_conf_listshares(int argc, const char **argv)
414 WERROR werr = WERR_OK;
416 uint32_t count, num_shares = 0;
417 char **share_names = NULL;
420 ctx = talloc_init("listshares");
423 net_conf_listshares_usage(argc, argv);
427 werr = libnet_smbconf_get_share_names(ctx, &num_shares, &share_names);
428 if (!W_ERROR_IS_OK(werr)) {
432 for (count = 0; count < num_shares; count++)
434 d_printf("%s\n", share_names[count]);
444 static int net_conf_drop(int argc, const char **argv)
450 net_conf_drop_usage(argc, argv);
454 werr = libnet_smbconf_drop();
455 if (!W_ERROR_IS_OK(werr)) {
456 d_fprintf(stderr, "Error deleting configuration: %s\n",
467 static int net_conf_showshare(int argc, const char **argv)
470 WERROR werr = WERR_OK;
471 const char *sharename = NULL;
478 ctx = talloc_init("showshare");
481 net_conf_showshare_usage(argc, argv);
487 werr = libnet_smbconf_getshare(ctx, sharename, &num_params,
488 ¶m_names, ¶m_values);
489 if (!W_ERROR_IS_OK(werr)) {
490 d_printf("error getting share parameters: %s\n",
495 d_printf("[%s]\n", sharename);
497 for (count = 0; count < num_params; count++) {
498 d_printf("\t%s = %s\n", param_names[count],
499 param_values[count]);
509 static int net_conf_addshare(int argc, const char **argv)
512 WERROR werr = WERR_OK;
513 struct registry_key *newkey = NULL;
514 char *sharename = NULL;
515 const char *path = NULL;
516 const char *comment = NULL;
517 const char *guest_ok = "no";
518 const char *writeable = "no";
519 SMB_STRUCT_STAT sbuf;
525 net_conf_addshare_usage(argc, argv);
530 if (!strnequal(argv[3], "guest_ok=", 9)) {
531 net_conf_addshare_usage(argc, argv);
534 switch (argv[3][9]) {
544 net_conf_addshare_usage(argc, argv);
548 if (!strnequal(argv[2], "writeable=", 10)) {
549 net_conf_addshare_usage(argc, argv);
552 switch (argv[2][10]) {
562 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");
601 if (path[0] != '/') {
603 "Error: path '%s' is not an absolute path.\n",
608 if (sys_stat(path, &sbuf) != 0) {
610 "ERROR: cannot stat path '%s' to ensure "
611 "this is a directory.\n"
613 path, strerror(errno));
617 if (!S_ISDIR(sbuf.st_mode)) {
619 "ERROR: path '%s' is not a directory.\n",
628 werr = libnet_smbconf_reg_createkey_internal(NULL, argv[0], &newkey);
629 if (!W_ERROR_IS_OK(werr)) {
630 d_fprintf(stderr, "Error creating share %s: %s\n",
631 argv[0], dos_errstr(werr));
635 /* add config params as values */
637 werr = libnet_smbconf_reg_setvalue_internal(newkey, "path", path);
638 if (!W_ERROR_IS_OK(werr)) {
639 d_fprintf(stderr, "Error setting parameter %s: %s\n",
640 "path", dos_errstr(werr));
644 if (comment != NULL) {
645 werr = libnet_smbconf_reg_setvalue_internal(newkey, "comment",
647 if (!W_ERROR_IS_OK(werr)) {
648 d_fprintf(stderr, "Error setting parameter %s: %s\n",
649 "comment", dos_errstr(werr));
654 werr = libnet_smbconf_reg_setvalue_internal(newkey, "guest ok",
656 if (!W_ERROR_IS_OK(werr)) {
657 d_fprintf(stderr, "Error setting parameter %s: %s\n",
658 "'guest ok'", dos_errstr(werr));
662 werr = libnet_smbconf_reg_setvalue_internal(newkey, "writeable",
664 if (!W_ERROR_IS_OK(werr)) {
665 d_fprintf(stderr, "Error setting parameter %s: %s\n",
666 "writeable", dos_errstr(werr));
674 SAFE_FREE(sharename);
678 static int net_conf_delshare(int argc, const char **argv)
681 const char *sharename = NULL;
682 WERROR werr = WERR_OK;
685 net_conf_delshare_usage(argc, argv);
690 werr = libnet_smbconf_delshare(sharename);
691 if (!W_ERROR_IS_OK(werr)) {
692 d_fprintf(stderr, "Error deleting share %s: %s\n",
693 sharename, dos_errstr(werr));
702 static int net_conf_setparm(int argc, const char **argv)
705 WERROR werr = WERR_OK;
706 char *service = NULL;
708 const char *value_str = NULL;
711 net_conf_setparm_usage(argc, argv);
714 service = strdup_lower(argv[0]);
715 param = strdup_lower(argv[1]);
718 werr = libnet_smbconf_setparm(service, param, value_str);
720 if (!W_ERROR_IS_OK(werr)) {
721 d_fprintf(stderr, "Error setting value '%s': %s\n",
722 param, dos_errstr(werr));
734 static int net_conf_getparm(int argc, const char **argv)
737 WERROR werr = WERR_OK;
738 char *service = NULL;
743 ctx = talloc_init("getparm");
746 net_conf_getparm_usage(argc, argv);
749 service = strdup_lower(argv[0]);
750 param = strdup_lower(argv[1]);
752 werr = libnet_smbconf_getparm(ctx, service, param, &valstr);
754 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
756 "Error: given service '%s' does not exist.\n",
759 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
761 "Error: given parameter '%s' is not set.\n",
764 } else if (!W_ERROR_IS_OK(werr)) {
765 d_fprintf(stderr, "Error getting value '%s': %s.\n",
766 param, dos_errstr(werr));
770 d_printf("%s\n", valstr);
780 static int net_conf_delparm(int argc, const char **argv)
783 WERROR werr = WERR_OK;
784 char *service = NULL;
788 net_conf_delparm_usage(argc, argv);
791 service = strdup_lower(argv[0]);
792 param = strdup_lower(argv[1]);
794 werr = libnet_smbconf_delparm(service, param);
796 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
798 "Error: given service '%s' does not exist.\n",
801 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
803 "Error: given parameter '%s' is not set.\n",
806 } else if (!W_ERROR_IS_OK(werr)) {
807 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
808 param, dos_errstr(werr));
821 * Entry-point for all the CONF functions.
824 int net_conf(int argc, const char **argv)
827 struct functable2 func[] = {
828 {"list", net_conf_list,
829 "Dump the complete configuration in smb.conf like format."},
830 {"import", net_conf_import,
831 "Import configuration from file in smb.conf format."},
832 {"listshares", net_conf_listshares,
833 "List the registry shares."},
834 {"drop", net_conf_drop,
835 "Delete the complete configuration from registry."},
836 {"showshare", net_conf_showshare,
837 "Show the definition of a registry share."},
838 {"addshare", net_conf_addshare,
839 "Create a new registry share."},
840 {"delshare", net_conf_delshare,
841 "Delete a registry share."},
842 {"setparm", net_conf_setparm,
843 "Store a parameter."},
844 {"getparm", net_conf_getparm,
845 "Retrieve the value of a parameter."},
846 {"delparm", net_conf_delparm,
847 "Delete a parameter."},
851 if (!registry_init_regdb()) {
852 d_fprintf(stderr, "Error initializing the registry!\n");
856 ret = net_run_function2(argc, argv, "net conf", func);