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 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"
34 /**********************************************************************
38 **********************************************************************/
40 static int net_conf_list_usage(int argc
, const char **argv
)
42 d_printf("USAGE: net conf list\n");
46 static int net_conf_import_usage(int argc
, const char**argv
)
48 d_printf("USAGE: net conf import [--test|-T] <filename> "
50 "\t[--test|-T] testmode - do not act, just print "
51 "what would be done\n"
52 "\t<servicename> only import service <servicename>, "
57 static int net_conf_listshares_usage(int argc
, const char **argv
)
59 d_printf("USAGE: net conf listshares\n");
63 static int net_conf_drop_usage(int argc
, const char **argv
)
65 d_printf("USAGE: net conf drop\n");
69 static int net_conf_showshare_usage(int argc
, const char **argv
)
71 d_printf("USAGE: net conf showshare <sharename>\n");
75 static int net_conf_addshare_usage(int argc
, const char **argv
)
77 d_printf("USAGE: net conf addshare <sharename> <path> "
78 "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
79 "\t<sharename> the new share name.\n"
80 "\t<path> the path on the filesystem to export.\n"
81 "\twriteable={y|N} set \"writeable to \"yes\" or "
82 "\"no\" (default) on this share.\n"
83 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
84 "\"no\" (default) on this share.\n"
85 "\t<comment> optional comment for the new share.\n");
89 static int net_conf_delshare_usage(int argc
, const char **argv
)
91 d_printf("USAGE: net conf delshare <sharename>\n");
95 static int net_conf_setparm_usage(int argc
, const char **argv
)
97 d_printf("USAGE: net conf setparm <section> <param> <value>\n");
101 static int net_conf_getparm_usage(int argc
, const char **argv
)
103 d_printf("USAGE: net conf getparm <section> <param>\n");
107 static int net_conf_delparm_usage(int argc
, const char **argv
)
109 d_printf("USAGE: net conf delparm <section> <param>\n");
114 /**********************************************************************
118 **********************************************************************/
121 * This formats an in-memory smbconf parameter to a string.
122 * The result string is allocated with talloc.
124 static char *parm_valstr(TALLOC_CTX
*ctx
, struct parm_struct
*parm
,
125 struct share_params
*share
)
129 void *ptr
= parm
->ptr
;
131 if (parm
->p_class
== P_LOCAL
&& share
->service
>= 0) {
132 ptr
= lp_local_ptr(share
->service
, ptr
);
135 switch (parm
->type
) {
137 valstr
= talloc_asprintf(ctx
, "%c", *(char *)ptr
);
141 valstr
= talloc_asprintf(ctx
, "%s", *(char **)ptr
);
144 valstr
= talloc_asprintf(ctx
, "%s", BOOLSTR(*(bool *)ptr
));
147 valstr
= talloc_asprintf(ctx
, "%s", BOOLSTR(!*(bool *)ptr
));
150 for (i
= 0; parm
->enum_list
[i
].name
; i
++) {
151 if (*(int *)ptr
== parm
->enum_list
[i
].value
)
153 valstr
= talloc_asprintf(ctx
, "%s",
154 parm
->enum_list
[i
].name
);
160 char *o
= octal_string(*(int *)ptr
);
161 valstr
= talloc_move(ctx
, &o
);
165 valstr
= talloc_strdup(ctx
, "");
166 if ((char ***)ptr
&& *(char ***)ptr
) {
167 char **list
= *(char ***)ptr
;
168 for (; *list
; list
++) {
169 /* surround strings with whitespace
170 * in double quotes */
171 if (strchr_m(*list
, ' '))
173 valstr
= talloc_asprintf_append(
176 ((*(list
+1))?", ":""));
178 valstr
= talloc_asprintf_append(
179 valstr
, "%s%s", *list
,
180 ((*(list
+1))?", ":""));
186 valstr
= talloc_asprintf(ctx
, "%d", *(int *)ptr
);
191 valstr
= talloc_asprintf(ctx
, "<type unimplemented>\n");
199 * This functions imports a configuration that has previously
200 * been loaded with lp_load() to registry.
202 static int import_process_service(TALLOC_CTX
*ctx
,
203 struct libnet_conf_ctx
*conf_ctx
,
204 struct share_params
*share
)
207 struct parm_struct
*parm
;
209 const char *servicename
;
212 TALLOC_CTX
*tmp_ctx
= NULL
;
214 tmp_ctx
= talloc_new(ctx
);
215 if (tmp_ctx
== NULL
) {
220 servicename
= (share
->service
== GLOBAL_SECTION_SNUM
)?
221 GLOBAL_NAME
: lp_servicename(share
->service
);
224 d_printf("[%s]\n", servicename
);
226 if (libnet_conf_share_exists(conf_ctx
, servicename
)) {
227 werr
= libnet_conf_delete_share(conf_ctx
, servicename
);
228 if (!W_ERROR_IS_OK(werr
)) {
232 werr
= libnet_conf_create_share(conf_ctx
, servicename
);
233 if (!W_ERROR_IS_OK(werr
)) {
238 while ((parm
= lp_next_parameter(share
->service
, &pnum
, 0)))
240 if ((share
->service
< 0) && (parm
->p_class
== P_LOCAL
)
241 && !(parm
->flags
& FLAG_GLOBAL
))
246 valstr
= parm_valstr(tmp_ctx
, parm
, share
);
248 if (parm
->type
!= P_SEP
) {
250 d_printf("\t%s = %s\n", parm
->label
, valstr
);
252 werr
= libnet_conf_set_parameter(conf_ctx
,
256 if (!W_ERROR_IS_OK(werr
)) {
258 "Error setting parameter '%s'"
259 ": %s\n", parm
->label
,
274 TALLOC_FREE(tmp_ctx
);
279 * Return true iff there are nondefault globals in the
280 * currently loaded configuration.
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
) {
296 /**********************************************************************
298 * the main conf functions
300 **********************************************************************/
302 static int net_conf_list(struct libnet_conf_ctx
*conf_ctx
,
303 int argc
, const char **argv
)
305 WERROR werr
= WERR_OK
;
310 uint32_t *num_params
;
312 char ***param_values
;
313 uint32_t share_count
, param_count
;
315 ctx
= talloc_init("list");
318 net_conf_list_usage(argc
, argv
);
322 werr
= libnet_conf_get_config(ctx
, conf_ctx
, &num_shares
, &share_names
,
323 &num_params
, ¶m_names
, ¶m_values
);
324 if (!W_ERROR_IS_OK(werr
)) {
325 d_fprintf(stderr
, "Error getting config: %s\n",
330 for (share_count
= 0; share_count
< num_shares
; share_count
++) {
331 d_printf("[%s]\n", share_names
[share_count
]);
332 for (param_count
= 0; param_count
< num_params
[share_count
];
335 d_printf("\t%s = %s\n",
336 param_names
[share_count
][param_count
],
337 param_values
[share_count
][param_count
]);
349 static int net_conf_import(struct libnet_conf_ctx
*conf_ctx
,
350 int argc
, const char **argv
)
353 const char *filename
= NULL
;
354 const char *servicename
= NULL
;
355 bool service_found
= false;
357 struct share_iterator
*shares
;
358 struct share_params
*share
;
359 struct share_params global_share
= { GLOBAL_SECTION_SNUM
};
361 ctx
= talloc_init("net_conf_import");
366 net_conf_import_usage(argc
, argv
);
369 servicename
= argv
[1];
375 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
378 if (!lp_load(filename
,
379 false, /* global_only */
380 true, /* save_defaults */
382 true)) /* initialize_globals */
384 d_fprintf(stderr
, "Error parsing configuration file.\n");
389 d_printf("\nTEST MODE - "
390 "would import the following configuration:\n\n");
393 if (((servicename
== NULL
) && globals_exist()) ||
394 strequal(servicename
, GLOBAL_NAME
))
396 service_found
= true;
397 if (import_process_service(ctx
, conf_ctx
, &global_share
) != 0) {
402 if (service_found
&& (servicename
!= NULL
)) {
407 if (!(shares
= share_list_all(ctx
))) {
408 d_fprintf(stderr
, "Could not list shares...\n");
411 while ((share
= next_share(shares
)) != NULL
) {
412 if ((servicename
== NULL
)
413 || strequal(servicename
, lp_servicename(share
->service
)))
415 service_found
= true;
416 if (import_process_service(ctx
, conf_ctx
, share
)!= 0) {
422 if ((servicename
!= NULL
) && !service_found
) {
423 d_printf("Share %s not found in file %s\n",
424 servicename
, filename
);
436 static int net_conf_listshares(struct libnet_conf_ctx
*conf_ctx
,
437 int argc
, const char **argv
)
439 WERROR werr
= WERR_OK
;
441 uint32_t count
, num_shares
= 0;
442 char **share_names
= NULL
;
445 ctx
= talloc_init("listshares");
448 net_conf_listshares_usage(argc
, argv
);
452 werr
= libnet_conf_get_share_names(ctx
, conf_ctx
, &num_shares
,
454 if (!W_ERROR_IS_OK(werr
)) {
458 for (count
= 0; count
< num_shares
; count
++)
460 d_printf("%s\n", share_names
[count
]);
470 static int net_conf_drop(struct libnet_conf_ctx
*conf_ctx
,
471 int argc
, const char **argv
)
477 net_conf_drop_usage(argc
, argv
);
481 werr
= libnet_conf_drop(conf_ctx
);
482 if (!W_ERROR_IS_OK(werr
)) {
483 d_fprintf(stderr
, "Error deleting configuration: %s\n",
494 static int net_conf_showshare(struct libnet_conf_ctx
*conf_ctx
,
495 int argc
, const char **argv
)
498 WERROR werr
= WERR_OK
;
499 const char *sharename
= NULL
;
506 ctx
= talloc_init("showshare");
509 net_conf_showshare_usage(argc
, argv
);
515 werr
= libnet_conf_get_share(ctx
, conf_ctx
, sharename
, &num_params
,
516 ¶m_names
, ¶m_values
);
517 if (!W_ERROR_IS_OK(werr
)) {
518 d_printf("error getting share parameters: %s\n",
523 d_printf("[%s]\n", sharename
);
525 for (count
= 0; count
< num_params
; count
++) {
526 d_printf("\t%s = %s\n", param_names
[count
],
527 param_values
[count
]);
538 * Add a share, with a couple of standard parameters, partly optional.
540 * This is a high level utility function of the net conf utility,
541 * not a direct frontend to the libnet_conf API.
543 static int net_conf_addshare(struct libnet_conf_ctx
*conf_ctx
,
544 int argc
, const char **argv
)
547 WERROR werr
= WERR_OK
;
548 char *sharename
= NULL
;
549 const char *path
= NULL
;
550 const char *comment
= NULL
;
551 const char *guest_ok
= "no";
552 const char *writeable
= "no";
553 SMB_STRUCT_STAT sbuf
;
559 net_conf_addshare_usage(argc
, argv
);
564 if (!strnequal(argv
[3], "guest_ok=", 9)) {
565 net_conf_addshare_usage(argc
, argv
);
568 switch (argv
[3][9]) {
578 net_conf_addshare_usage(argc
, argv
);
582 if (!strnequal(argv
[2], "writeable=", 10)) {
583 net_conf_addshare_usage(argc
, argv
);
586 switch (argv
[2][10]) {
596 net_conf_addshare_usage(argc
, argv
);
601 sharename
= strdup_lower(argv
[0]);
609 /* validate share name */
611 if (!validate_net_name(sharename
, INVALID_SHARENAME_CHARS
,
614 d_fprintf(stderr
, "ERROR: share name %s contains "
615 "invalid characters (any of %s)\n",
616 sharename
, INVALID_SHARENAME_CHARS
);
620 if (getpwnam(sharename
)) {
621 d_fprintf(stderr
, "ERROR: share name %s is already a valid "
622 "system user name.\n", sharename
);
626 if (strequal(sharename
, GLOBAL_NAME
)) {
628 "ERROR: 'global' is not a valid share name.\n");
632 if (libnet_conf_share_exists(conf_ctx
, sharename
)) {
633 d_fprintf(stderr
, "ERROR: share %s already exists.\n",
640 if (path
[0] != '/') {
642 "Error: path '%s' is not an absolute path.\n",
647 if (sys_stat(path
, &sbuf
) != 0) {
649 "ERROR: cannot stat path '%s' to ensure "
650 "this is a directory.\n"
652 path
, strerror(errno
));
656 if (!S_ISDIR(sbuf
.st_mode
)) {
658 "ERROR: path '%s' is not a directory.\n",
667 werr
= libnet_conf_create_share(conf_ctx
, sharename
);
668 if (!W_ERROR_IS_OK(werr
)) {
669 d_fprintf(stderr
, "Error creating share %s: %s\n",
670 sharename
, dos_errstr(werr
));
675 * fill the share with parameters
678 werr
= libnet_conf_set_parameter(conf_ctx
, sharename
, "path", path
);
679 if (!W_ERROR_IS_OK(werr
)) {
680 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
681 "path", dos_errstr(werr
));
685 if (comment
!= NULL
) {
686 werr
= libnet_conf_set_parameter(conf_ctx
, sharename
, "comment",
688 if (!W_ERROR_IS_OK(werr
)) {
689 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
690 "comment", dos_errstr(werr
));
695 werr
= libnet_conf_set_parameter(conf_ctx
, sharename
, "guest ok",
697 if (!W_ERROR_IS_OK(werr
)) {
698 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
699 "'guest ok'", dos_errstr(werr
));
703 werr
= libnet_conf_set_parameter(conf_ctx
, sharename
, "writeable",
705 if (!W_ERROR_IS_OK(werr
)) {
706 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
707 "writeable", dos_errstr(werr
));
714 SAFE_FREE(sharename
);
718 static int net_conf_delshare(struct libnet_conf_ctx
*conf_ctx
,
719 int argc
, const char **argv
)
722 const char *sharename
= NULL
;
723 WERROR werr
= WERR_OK
;
726 net_conf_delshare_usage(argc
, argv
);
731 werr
= libnet_conf_delete_share(conf_ctx
, sharename
);
732 if (!W_ERROR_IS_OK(werr
)) {
733 d_fprintf(stderr
, "Error deleting share %s: %s\n",
734 sharename
, dos_errstr(werr
));
743 static int net_conf_setparm(struct libnet_conf_ctx
*conf_ctx
,
744 int argc
, const char **argv
)
747 WERROR werr
= WERR_OK
;
748 char *service
= NULL
;
750 const char *value_str
= NULL
;
753 net_conf_setparm_usage(argc
, argv
);
756 service
= strdup_lower(argv
[0]);
757 param
= strdup_lower(argv
[1]);
760 if (!libnet_conf_share_exists(conf_ctx
, service
)) {
761 werr
= libnet_conf_create_share(conf_ctx
, service
);
762 if (!W_ERROR_IS_OK(werr
)) {
763 d_fprintf(stderr
, "Error creating share '%s': %s\n",
764 service
, dos_errstr(werr
));
769 werr
= libnet_conf_set_parameter(conf_ctx
, service
, param
, value_str
);
771 if (!W_ERROR_IS_OK(werr
)) {
772 d_fprintf(stderr
, "Error setting value '%s': %s\n",
773 param
, dos_errstr(werr
));
785 static int net_conf_getparm(struct libnet_conf_ctx
*conf_ctx
,
786 int argc
, const char **argv
)
789 WERROR werr
= WERR_OK
;
790 char *service
= NULL
;
795 ctx
= talloc_init("getparm");
798 net_conf_getparm_usage(argc
, argv
);
801 service
= strdup_lower(argv
[0]);
802 param
= strdup_lower(argv
[1]);
804 werr
= libnet_conf_get_parameter(ctx
, conf_ctx
, service
, param
, &valstr
);
806 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
808 "Error: given service '%s' does not exist.\n",
811 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
813 "Error: given parameter '%s' is not set.\n",
816 } else if (!W_ERROR_IS_OK(werr
)) {
817 d_fprintf(stderr
, "Error getting value '%s': %s.\n",
818 param
, dos_errstr(werr
));
822 d_printf("%s\n", valstr
);
832 static int net_conf_delparm(struct libnet_conf_ctx
*conf_ctx
,
833 int argc
, const char **argv
)
836 WERROR werr
= WERR_OK
;
837 char *service
= NULL
;
841 net_conf_delparm_usage(argc
, argv
);
844 service
= strdup_lower(argv
[0]);
845 param
= strdup_lower(argv
[1]);
847 werr
= libnet_conf_delete_parameter(conf_ctx
, service
, param
);
849 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
851 "Error: given service '%s' does not exist.\n",
854 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
856 "Error: given parameter '%s' is not set.\n",
859 } else if (!W_ERROR_IS_OK(werr
)) {
860 d_fprintf(stderr
, "Error deleting value '%s': %s.\n",
861 param
, dos_errstr(werr
));
874 /**********************************************************************
876 * Wrapper and net_conf_run_function mechanism.
878 **********************************************************************/
881 * Wrapper function to call the main conf functions.
882 * The wrapper calls handles opening and closing of the
885 static int net_conf_wrap_function(int (*fn
)(struct libnet_conf_ctx
*,
887 int argc
, const char **argv
)
890 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
891 struct libnet_conf_ctx
*conf_ctx
;
894 werr
= libnet_conf_open(mem_ctx
, &conf_ctx
);
896 if (!W_ERROR_IS_OK(werr
)) {
900 ret
= fn(conf_ctx
, argc
, argv
);
902 libnet_conf_close(conf_ctx
);
908 * We need a functable struct of our own, because the
909 * functions are called through a wrapper that handles
910 * the opening and closing of the configuration, and so on.
912 struct conf_functable
{
913 const char *funcname
;
914 int (*fn
)(struct libnet_conf_ctx
*ctx
, int argc
, const char **argv
);
915 const char *helptext
;
919 * This imitates net_run_function2 but calls the main functions
920 * through the wrapper net_conf_wrap_function().
922 static int net_conf_run_function(int argc
, const char **argv
,
924 struct conf_functable
*table
)
929 for (i
=0; table
[i
].funcname
; i
++) {
930 if (StrCaseCmp(argv
[0], table
[i
].funcname
) == 0)
931 return net_conf_wrap_function(table
[i
].fn
,
937 for (i
=0; table
[i
].funcname
; i
++) {
938 d_printf("%s %-15s %s\n", whoami
, table
[i
].funcname
,
946 * Entry-point for all the CONF functions.
949 int net_conf(int argc
, const char **argv
)
952 struct conf_functable func_table
[] = {
953 {"list", net_conf_list
,
954 "Dump the complete configuration in smb.conf like format."},
955 {"import", net_conf_import
,
956 "Import configuration from file in smb.conf format."},
957 {"listshares", net_conf_listshares
,
958 "List the share names."},
959 {"drop", net_conf_drop
,
960 "Delete the complete configuration."},
961 {"showshare", net_conf_showshare
,
962 "Show the definition of a share."},
963 {"addshare", net_conf_addshare
,
964 "Create a new share."},
965 {"delshare", net_conf_delshare
,
967 {"setparm", net_conf_setparm
,
968 "Store a parameter."},
969 {"getparm", net_conf_getparm
,
970 "Retrieve the value of a parameter."},
971 {"delparm", net_conf_delparm
,
972 "Delete a parameter."},
976 ret
= net_conf_run_function(argc
, argv
, "net conf", func_table
);