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 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_conf_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_conf_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_conf_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_conf_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_conf_get_share(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_conf_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",
634 werr
= libnet_conf_create_share(sharename
);
635 if (!W_ERROR_IS_OK(werr
)) {
636 d_fprintf(stderr
, "Error creating share %s: %s\n",
637 sharename
, dos_errstr(werr
));
642 * fill the share with parameters
645 werr
= libnet_smbconf_setparm(sharename
, "path", path
);
646 if (!W_ERROR_IS_OK(werr
)) {
647 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
648 "path", dos_errstr(werr
));
652 if (comment
!= NULL
) {
653 werr
= libnet_smbconf_setparm(sharename
, "comment", comment
);
654 if (!W_ERROR_IS_OK(werr
)) {
655 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
656 "comment", dos_errstr(werr
));
661 werr
= libnet_smbconf_setparm(sharename
, "guest ok", guest_ok
);
662 if (!W_ERROR_IS_OK(werr
)) {
663 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
664 "'guest ok'", dos_errstr(werr
));
668 werr
= libnet_smbconf_setparm(sharename
, "writeable", writeable
);
669 if (!W_ERROR_IS_OK(werr
)) {
670 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
671 "writeable", dos_errstr(werr
));
678 SAFE_FREE(sharename
);
682 static int net_conf_delshare(int argc
, const char **argv
)
685 const char *sharename
= NULL
;
686 WERROR werr
= WERR_OK
;
689 net_conf_delshare_usage(argc
, argv
);
694 werr
= libnet_smbconf_delshare(sharename
);
695 if (!W_ERROR_IS_OK(werr
)) {
696 d_fprintf(stderr
, "Error deleting share %s: %s\n",
697 sharename
, dos_errstr(werr
));
706 static int net_conf_setparm(int argc
, const char **argv
)
709 WERROR werr
= WERR_OK
;
710 char *service
= NULL
;
712 const char *value_str
= NULL
;
715 net_conf_setparm_usage(argc
, argv
);
718 service
= strdup_lower(argv
[0]);
719 param
= strdup_lower(argv
[1]);
722 werr
= libnet_smbconf_setparm(service
, param
, value_str
);
724 if (!W_ERROR_IS_OK(werr
)) {
725 d_fprintf(stderr
, "Error setting value '%s': %s\n",
726 param
, dos_errstr(werr
));
738 static int net_conf_getparm(int argc
, const char **argv
)
741 WERROR werr
= WERR_OK
;
742 char *service
= NULL
;
747 ctx
= talloc_init("getparm");
750 net_conf_getparm_usage(argc
, argv
);
753 service
= strdup_lower(argv
[0]);
754 param
= strdup_lower(argv
[1]);
756 werr
= libnet_smbconf_getparm(ctx
, service
, param
, &valstr
);
758 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
760 "Error: given service '%s' does not exist.\n",
763 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
765 "Error: given parameter '%s' is not set.\n",
768 } else if (!W_ERROR_IS_OK(werr
)) {
769 d_fprintf(stderr
, "Error getting value '%s': %s.\n",
770 param
, dos_errstr(werr
));
774 d_printf("%s\n", valstr
);
784 static int net_conf_delparm(int argc
, const char **argv
)
787 WERROR werr
= WERR_OK
;
788 char *service
= NULL
;
792 net_conf_delparm_usage(argc
, argv
);
795 service
= strdup_lower(argv
[0]);
796 param
= strdup_lower(argv
[1]);
798 werr
= libnet_smbconf_delparm(service
, param
);
800 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
802 "Error: given service '%s' does not exist.\n",
805 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
807 "Error: given parameter '%s' is not set.\n",
810 } else if (!W_ERROR_IS_OK(werr
)) {
811 d_fprintf(stderr
, "Error deleting value '%s': %s.\n",
812 param
, dos_errstr(werr
));
825 * Entry-point for all the CONF functions.
828 int net_conf(int argc
, const char **argv
)
831 struct functable2 func
[] = {
832 {"list", net_conf_list
,
833 "Dump the complete configuration in smb.conf like format."},
834 {"import", net_conf_import
,
835 "Import configuration from file in smb.conf format."},
836 {"listshares", net_conf_listshares
,
837 "List the registry shares."},
838 {"drop", net_conf_drop
,
839 "Delete the complete configuration from registry."},
840 {"showshare", net_conf_showshare
,
841 "Show the definition of a registry share."},
842 {"addshare", net_conf_addshare
,
843 "Create a new registry share."},
844 {"delshare", net_conf_delshare
,
845 "Delete a registry share."},
846 {"setparm", net_conf_setparm
,
847 "Store a parameter."},
848 {"getparm", net_conf_getparm
,
849 "Retrieve the value of a parameter."},
850 {"delparm", net_conf_delparm
,
851 "Delete a parameter."},
855 if (!registry_init_regdb()) {
856 d_fprintf(stderr
, "Error initializing the registry!\n");
860 ret
= net_run_function2(argc
, argv
, "net conf", func
);