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 libsmbconf interface (source/lib/smbconf/smbconf.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"
33 /**********************************************************************
37 **********************************************************************/
39 static int net_conf_list_usage(int argc
, const char **argv
)
41 d_printf("USAGE: net conf list\n");
45 static int net_conf_import_usage(int argc
, const char**argv
)
47 d_printf("USAGE: net conf import [--test|-T] <filename> "
49 "\t[--test|-T] testmode - do not act, just print "
50 "what would be done\n"
51 "\t<servicename> only import service <servicename>, "
56 static int net_conf_listshares_usage(int argc
, const char **argv
)
58 d_printf("USAGE: net conf listshares\n");
62 static int net_conf_drop_usage(int argc
, const char **argv
)
64 d_printf("USAGE: net conf drop\n");
68 static int net_conf_showshare_usage(int argc
, const char **argv
)
70 d_printf("USAGE: net conf showshare <sharename>\n");
74 static int net_conf_addshare_usage(int argc
, const char **argv
)
76 d_printf("USAGE: net conf addshare <sharename> <path> "
77 "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
78 "\t<sharename> the new share name.\n"
79 "\t<path> the path on the filesystem to export.\n"
80 "\twriteable={y|N} set \"writeable to \"yes\" or "
81 "\"no\" (default) on this share.\n"
82 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
83 "\"no\" (default) on this share.\n"
84 "\t<comment> optional comment for the new share.\n");
88 static int net_conf_delshare_usage(int argc
, const char **argv
)
90 d_printf("USAGE: net conf delshare <sharename>\n");
94 static int net_conf_setparm_usage(int argc
, const char **argv
)
96 d_printf("USAGE: net conf setparm <section> <param> <value>\n");
100 static int net_conf_getparm_usage(int argc
, const char **argv
)
102 d_printf("USAGE: net conf getparm <section> <param>\n");
106 static int net_conf_delparm_usage(int argc
, const char **argv
)
108 d_printf("USAGE: net conf delparm <section> <param>\n");
112 static int net_conf_getincludes_usage(int argc
, const char **argv
)
114 d_printf("USAGE: net conf getincludes <section>\n");
118 static int net_conf_setincludes_usage(int argc
, const char **argv
)
120 d_printf("USAGE: net conf setincludes <section> [<filename>]*\n");
124 static int net_conf_delincludes_usage(int argc
, const char **argv
)
126 d_printf("USAGE: net conf delincludes <section>\n");
131 /**********************************************************************
135 **********************************************************************/
138 * This functions process a service previously loaded with libsmbconf.
140 static WERROR
import_process_service(struct smbconf_ctx
*conf_ctx
,
141 const char *servicename
,
142 const uint32_t num_params
,
143 const char **param_names
,
144 const char **param_values
)
147 WERROR werr
= WERR_OK
;
148 uint32_t num_includes
= 0;
149 char **includes
= NULL
;
150 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
153 if (servicename
!= NULL
) {
154 d_printf("[%s]\n", servicename
);
155 for (idx
= 0; idx
< num_params
; idx
++) {
156 d_printf("\t%s = %s\n", param_names
[idx
],
161 for (idx
= 0; idx
< num_params
; idx
++) {
162 d_printf("%s = %s\n", param_names
[idx
],
170 if (smbconf_share_exists(conf_ctx
, servicename
)) {
171 werr
= smbconf_delete_share(conf_ctx
, servicename
);
172 if (!W_ERROR_IS_OK(werr
)) {
176 werr
= smbconf_create_share(conf_ctx
, servicename
);
177 if (!W_ERROR_IS_OK(werr
)) {
181 for (idx
= 0; idx
< num_params
; idx
++) {
182 if (strequal(param_names
[idx
], "include")) {
183 includes
= TALLOC_REALLOC_ARRAY(mem_ctx
,
187 if (includes
== NULL
) {
191 includes
[num_includes
] = talloc_strdup(includes
,
193 if (includes
[num_includes
] == NULL
) {
199 werr
= smbconf_set_parameter(conf_ctx
,
203 if (!W_ERROR_IS_OK(werr
)) {
209 werr
= smbconf_set_includes(conf_ctx
, servicename
, num_includes
,
210 (const char **)includes
);
213 TALLOC_FREE(mem_ctx
);
218 /**********************************************************************
220 * the main conf functions
222 **********************************************************************/
224 static int net_conf_list(struct smbconf_ctx
*conf_ctx
,
225 int argc
, const char **argv
)
227 WERROR werr
= WERR_OK
;
232 uint32_t *num_params
;
234 char ***param_values
;
235 uint32_t share_count
, param_count
;
237 mem_ctx
= talloc_stackframe();
240 net_conf_list_usage(argc
, argv
);
244 werr
= smbconf_get_config(conf_ctx
, mem_ctx
, &num_shares
, &share_names
,
245 &num_params
, ¶m_names
, ¶m_values
);
246 if (!W_ERROR_IS_OK(werr
)) {
247 d_fprintf(stderr
, "Error getting config: %s\n",
252 for (share_count
= 0; share_count
< num_shares
; share_count
++) {
253 d_printf("[%s]\n", share_names
[share_count
]);
254 for (param_count
= 0; param_count
< num_params
[share_count
];
257 d_printf("\t%s = %s\n",
258 param_names
[share_count
][param_count
],
259 param_values
[share_count
][param_count
]);
267 TALLOC_FREE(mem_ctx
);
271 static int net_conf_import(struct smbconf_ctx
*conf_ctx
,
272 int argc
, const char **argv
)
275 const char *filename
= NULL
;
276 const char *servicename
= NULL
;
277 char *conf_source
= NULL
;
279 struct smbconf_ctx
*txt_ctx
;
282 mem_ctx
= talloc_stackframe();
287 net_conf_import_usage(argc
, argv
);
290 servicename
= talloc_strdup_lower(mem_ctx
, argv
[1]);
291 if (servicename
== NULL
) {
292 d_printf("error: out of memory!\n");
300 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
303 conf_source
= talloc_asprintf(mem_ctx
, "file:%s", filename
);
304 if (conf_source
== NULL
) {
305 d_printf("error: out of memory!\n");
309 werr
= smbconf_init(mem_ctx
, &txt_ctx
, conf_source
);
310 if (!W_ERROR_IS_OK(werr
)) {
311 d_printf("error loading file '%s': %s\n", filename
,
317 d_printf("\nTEST MODE - "
318 "would import the following configuration:\n\n");
321 if (servicename
!= NULL
) {
322 char **param_names
, **param_values
;
325 werr
= smbconf_get_share(txt_ctx
, mem_ctx
,
330 if (!W_ERROR_IS_OK(werr
)) {
333 werr
= import_process_service(conf_ctx
,
336 (const char **)param_names
,
337 (const char **)param_values
);
338 if (!W_ERROR_IS_OK(werr
)) {
342 char **share_names
, ***param_names
, ***param_values
;
343 uint32_t num_shares
, *num_params
, sidx
;
345 werr
= smbconf_get_config(txt_ctx
, mem_ctx
,
351 if (!W_ERROR_IS_OK(werr
)) {
355 werr
= smbconf_drop(conf_ctx
);
356 if (!W_ERROR_IS_OK(werr
)) {
360 for (sidx
= 0; sidx
< num_shares
; sidx
++) {
361 werr
= import_process_service(conf_ctx
,
364 (const char **)param_names
[sidx
],
365 (const char **)param_values
[sidx
]);
366 if (!W_ERROR_IS_OK(werr
)) {
375 TALLOC_FREE(mem_ctx
);
379 static int net_conf_listshares(struct smbconf_ctx
*conf_ctx
,
380 int argc
, const char **argv
)
382 WERROR werr
= WERR_OK
;
384 uint32_t count
, num_shares
= 0;
385 char **share_names
= NULL
;
388 mem_ctx
= talloc_stackframe();
391 net_conf_listshares_usage(argc
, argv
);
395 werr
= smbconf_get_share_names(conf_ctx
, mem_ctx
, &num_shares
,
397 if (!W_ERROR_IS_OK(werr
)) {
401 for (count
= 0; count
< num_shares
; count
++)
403 d_printf("%s\n", share_names
[count
]);
409 TALLOC_FREE(mem_ctx
);
413 static int net_conf_drop(struct smbconf_ctx
*conf_ctx
,
414 int argc
, const char **argv
)
420 net_conf_drop_usage(argc
, argv
);
424 werr
= smbconf_drop(conf_ctx
);
425 if (!W_ERROR_IS_OK(werr
)) {
426 d_fprintf(stderr
, "Error deleting configuration: %s\n",
437 static int net_conf_showshare(struct smbconf_ctx
*conf_ctx
,
438 int argc
, const char **argv
)
441 WERROR werr
= WERR_OK
;
442 const char *sharename
= NULL
;
449 mem_ctx
= talloc_stackframe();
452 net_conf_showshare_usage(argc
, argv
);
456 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
457 if (sharename
== NULL
) {
458 d_printf("error: out of memory!\n");
462 werr
= smbconf_get_share(conf_ctx
, mem_ctx
, sharename
, &num_params
,
463 ¶m_names
, ¶m_values
);
464 if (!W_ERROR_IS_OK(werr
)) {
465 d_printf("error getting share parameters: %s\n",
470 d_printf("[%s]\n", sharename
);
472 for (count
= 0; count
< num_params
; count
++) {
473 d_printf("\t%s = %s\n", param_names
[count
],
474 param_values
[count
]);
480 TALLOC_FREE(mem_ctx
);
485 * Add a share, with a couple of standard parameters, partly optional.
487 * This is a high level utility function of the net conf utility,
488 * not a direct frontend to the smbconf API.
490 static int net_conf_addshare(struct smbconf_ctx
*conf_ctx
,
491 int argc
, const char **argv
)
494 WERROR werr
= WERR_OK
;
495 char *sharename
= NULL
;
496 const char *path
= NULL
;
497 const char *comment
= NULL
;
498 const char *guest_ok
= "no";
499 const char *writeable
= "no";
500 SMB_STRUCT_STAT sbuf
;
501 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
507 net_conf_addshare_usage(argc
, argv
);
512 if (!strnequal(argv
[3], "guest_ok=", 9)) {
513 net_conf_addshare_usage(argc
, argv
);
516 switch (argv
[3][9]) {
526 net_conf_addshare_usage(argc
, argv
);
530 if (!strnequal(argv
[2], "writeable=", 10)) {
531 net_conf_addshare_usage(argc
, argv
);
534 switch (argv
[2][10]) {
544 net_conf_addshare_usage(argc
, argv
);
549 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
550 if (sharename
== NULL
) {
551 d_printf("error: out of memory!\n");
562 /* validate share name */
564 if (!validate_net_name(sharename
, INVALID_SHARENAME_CHARS
,
567 d_fprintf(stderr
, "ERROR: share name %s contains "
568 "invalid characters (any of %s)\n",
569 sharename
, INVALID_SHARENAME_CHARS
);
573 if (getpwnam(sharename
)) {
574 d_fprintf(stderr
, "ERROR: share name %s is already a valid "
575 "system user name.\n", sharename
);
579 if (strequal(sharename
, GLOBAL_NAME
)) {
581 "ERROR: 'global' is not a valid share name.\n");
585 if (smbconf_share_exists(conf_ctx
, sharename
)) {
586 d_fprintf(stderr
, "ERROR: share %s already exists.\n",
593 if (path
[0] != '/') {
595 "Error: path '%s' is not an absolute path.\n",
600 if (sys_stat(path
, &sbuf
) != 0) {
602 "ERROR: cannot stat path '%s' to ensure "
603 "this is a directory.\n"
605 path
, strerror(errno
));
609 if (!S_ISDIR(sbuf
.st_mode
)) {
611 "ERROR: path '%s' is not a directory.\n",
620 werr
= smbconf_create_share(conf_ctx
, sharename
);
621 if (!W_ERROR_IS_OK(werr
)) {
622 d_fprintf(stderr
, "Error creating share %s: %s\n",
623 sharename
, dos_errstr(werr
));
628 * fill the share with parameters
631 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "path", path
);
632 if (!W_ERROR_IS_OK(werr
)) {
633 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
634 "path", dos_errstr(werr
));
638 if (comment
!= NULL
) {
639 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "comment",
641 if (!W_ERROR_IS_OK(werr
)) {
642 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
643 "comment", dos_errstr(werr
));
648 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "guest ok", guest_ok
);
649 if (!W_ERROR_IS_OK(werr
)) {
650 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
651 "'guest ok'", dos_errstr(werr
));
655 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "writeable",
657 if (!W_ERROR_IS_OK(werr
)) {
658 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
659 "writeable", dos_errstr(werr
));
666 TALLOC_FREE(mem_ctx
);
670 static int net_conf_delshare(struct smbconf_ctx
*conf_ctx
,
671 int argc
, const char **argv
)
674 const char *sharename
= NULL
;
675 WERROR werr
= WERR_OK
;
676 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
679 net_conf_delshare_usage(argc
, argv
);
682 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
683 if (sharename
== NULL
) {
684 d_printf("error: out of memory!\n");
688 werr
= smbconf_delete_share(conf_ctx
, sharename
);
689 if (!W_ERROR_IS_OK(werr
)) {
690 d_fprintf(stderr
, "Error deleting share %s: %s\n",
691 sharename
, dos_errstr(werr
));
697 TALLOC_FREE(mem_ctx
);
701 static int net_conf_setparm(struct smbconf_ctx
*conf_ctx
,
702 int argc
, const char **argv
)
705 WERROR werr
= WERR_OK
;
706 char *service
= NULL
;
708 const char *value_str
= NULL
;
709 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
712 net_conf_setparm_usage(argc
, argv
);
715 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
716 if (service
== NULL
) {
717 d_printf("error: out of memory!\n");
720 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
722 d_printf("error: out of memory!\n");
727 if (!smbconf_share_exists(conf_ctx
, service
)) {
728 werr
= smbconf_create_share(conf_ctx
, service
);
729 if (!W_ERROR_IS_OK(werr
)) {
730 d_fprintf(stderr
, "Error creating share '%s': %s\n",
731 service
, dos_errstr(werr
));
736 werr
= smbconf_set_parameter(conf_ctx
, service
, param
, value_str
);
738 if (!W_ERROR_IS_OK(werr
)) {
739 d_fprintf(stderr
, "Error setting value '%s': %s\n",
740 param
, dos_errstr(werr
));
747 TALLOC_FREE(mem_ctx
);
751 static int net_conf_getparm(struct smbconf_ctx
*conf_ctx
,
752 int argc
, const char **argv
)
755 WERROR werr
= WERR_OK
;
756 char *service
= NULL
;
761 mem_ctx
= talloc_stackframe();
764 net_conf_getparm_usage(argc
, argv
);
767 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
768 if (service
== NULL
) {
769 d_printf("error: out of memory!\n");
772 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
774 d_printf("error: out of memory!\n");
778 werr
= smbconf_get_parameter(conf_ctx
, mem_ctx
, service
, param
, &valstr
);
780 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
782 "Error: given service '%s' does not exist.\n",
785 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
787 "Error: given parameter '%s' is not set.\n",
790 } else if (!W_ERROR_IS_OK(werr
)) {
791 d_fprintf(stderr
, "Error getting value '%s': %s.\n",
792 param
, dos_errstr(werr
));
796 d_printf("%s\n", valstr
);
800 TALLOC_FREE(mem_ctx
);
804 static int net_conf_delparm(struct smbconf_ctx
*conf_ctx
,
805 int argc
, const char **argv
)
808 WERROR werr
= WERR_OK
;
809 char *service
= NULL
;
811 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
814 net_conf_delparm_usage(argc
, argv
);
817 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
818 if (service
== NULL
) {
819 d_printf("error: out of memory!\n");
822 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
824 d_printf("error: out of memory!\n");
828 werr
= smbconf_delete_parameter(conf_ctx
, service
, param
);
830 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
832 "Error: given service '%s' does not exist.\n",
835 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
837 "Error: given parameter '%s' is not set.\n",
840 } else if (!W_ERROR_IS_OK(werr
)) {
841 d_fprintf(stderr
, "Error deleting value '%s': %s.\n",
842 param
, dos_errstr(werr
));
849 TALLOC_FREE(mem_ctx
);
853 static int net_conf_getincludes(struct smbconf_ctx
*conf_ctx
,
854 int argc
, const char **argv
)
857 uint32_t num_includes
;
860 char **includes
= NULL
;
862 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
865 net_conf_getincludes_usage(argc
, argv
);
869 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
870 if (service
== NULL
) {
871 d_printf("error: out of memory!\n");
875 werr
= smbconf_get_includes(conf_ctx
, mem_ctx
, service
,
876 &num_includes
, &includes
);
877 if (!W_ERROR_IS_OK(werr
)) {
878 d_printf("error getting includes: %s\n", dos_errstr(werr
));
882 for (count
= 0; count
< num_includes
; count
++) {
883 d_printf("include = %s\n", includes
[count
]);
889 TALLOC_FREE(mem_ctx
);
893 static int net_conf_setincludes(struct smbconf_ctx
*conf_ctx
,
894 int argc
, const char **argv
)
898 uint32_t num_includes
;
899 const char **includes
;
901 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
904 net_conf_setincludes_usage(argc
, argv
);
908 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
909 if (service
== NULL
) {
910 d_printf("error: out of memory!\n");
914 num_includes
= argc
- 1;
915 if (num_includes
== 0) {
921 werr
= smbconf_set_includes(conf_ctx
, service
, num_includes
, includes
);
922 if (!W_ERROR_IS_OK(werr
)) {
923 d_printf("error setting includes: %s\n", dos_errstr(werr
));
930 TALLOC_FREE(mem_ctx
);
934 static int net_conf_delincludes(struct smbconf_ctx
*conf_ctx
,
935 int argc
, const char **argv
)
940 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
943 net_conf_delincludes_usage(argc
, argv
);
947 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
948 if (service
== NULL
) {
949 d_printf("error: out of memory!\n");
953 werr
= smbconf_delete_includes(conf_ctx
, service
);
954 if (!W_ERROR_IS_OK(werr
)) {
955 d_printf("error deleting includes: %s\n", dos_errstr(werr
));
962 TALLOC_FREE(mem_ctx
);
967 /**********************************************************************
969 * Wrapper and net_conf_run_function mechanism.
971 **********************************************************************/
974 * Wrapper function to call the main conf functions.
975 * The wrapper calls handles opening and closing of the
978 static int net_conf_wrap_function(int (*fn
)(struct smbconf_ctx
*,
980 int argc
, const char **argv
)
983 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
984 struct smbconf_ctx
*conf_ctx
;
987 werr
= smbconf_init(mem_ctx
, &conf_ctx
, "registry:");
989 if (!W_ERROR_IS_OK(werr
)) {
993 ret
= fn(conf_ctx
, argc
, argv
);
995 smbconf_shutdown(conf_ctx
);
1001 * We need a functable struct of our own, because the
1002 * functions are called through a wrapper that handles
1003 * the opening and closing of the configuration, and so on.
1005 struct conf_functable
{
1006 const char *funcname
;
1007 int (*fn
)(struct smbconf_ctx
*ctx
, int argc
, const char **argv
);
1008 const char *helptext
;
1012 * This imitates net_run_function2 but calls the main functions
1013 * through the wrapper net_conf_wrap_function().
1015 static int net_conf_run_function(int argc
, const char **argv
,
1017 struct conf_functable
*table
)
1022 for (i
=0; table
[i
].funcname
; i
++) {
1023 if (StrCaseCmp(argv
[0], table
[i
].funcname
) == 0)
1024 return net_conf_wrap_function(table
[i
].fn
,
1030 for (i
=0; table
[i
].funcname
; i
++) {
1031 d_printf("%s %-15s %s\n", whoami
, table
[i
].funcname
,
1039 * Entry-point for all the CONF functions.
1042 int net_conf(int argc
, const char **argv
)
1045 struct conf_functable func_table
[] = {
1046 {"list", net_conf_list
,
1047 "Dump the complete configuration in smb.conf like format."},
1048 {"import", net_conf_import
,
1049 "Import configuration from file in smb.conf format."},
1050 {"listshares", net_conf_listshares
,
1051 "List the share names."},
1052 {"drop", net_conf_drop
,
1053 "Delete the complete configuration."},
1054 {"showshare", net_conf_showshare
,
1055 "Show the definition of a share."},
1056 {"addshare", net_conf_addshare
,
1057 "Create a new share."},
1058 {"delshare", net_conf_delshare
,
1060 {"setparm", net_conf_setparm
,
1061 "Store a parameter."},
1062 {"getparm", net_conf_getparm
,
1063 "Retrieve the value of a parameter."},
1064 {"delparm", net_conf_delparm
,
1065 "Delete a parameter."},
1066 {"getincludes", net_conf_getincludes
,
1067 "Show the includes of a share definition."},
1068 {"setincludes", net_conf_setincludes
,
1069 "Set includes for a share."},
1070 {"delincludes", net_conf_delincludes
,
1071 "Delete includes from a share definition."},
1075 ret
= net_conf_run_function(argc
, argv
, "net conf", func_table
);