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 const char *indent
= "";
154 if (servicename
!= NULL
) {
155 d_printf("[%s]\n", servicename
);
158 for (idx
= 0; idx
< num_params
; idx
++) {
159 d_printf("%s%s = %s\n", indent
, param_names
[idx
],
166 if (smbconf_share_exists(conf_ctx
, servicename
)) {
167 werr
= smbconf_delete_share(conf_ctx
, servicename
);
168 if (!W_ERROR_IS_OK(werr
)) {
172 werr
= smbconf_create_share(conf_ctx
, servicename
);
173 if (!W_ERROR_IS_OK(werr
)) {
177 for (idx
= 0; idx
< num_params
; idx
++) {
178 if (strequal(param_names
[idx
], "include")) {
179 includes
= TALLOC_REALLOC_ARRAY(mem_ctx
,
183 if (includes
== NULL
) {
187 includes
[num_includes
] = talloc_strdup(includes
,
189 if (includes
[num_includes
] == NULL
) {
195 werr
= smbconf_set_parameter(conf_ctx
,
199 if (!W_ERROR_IS_OK(werr
)) {
205 werr
= smbconf_set_includes(conf_ctx
, servicename
, num_includes
,
206 (const char **)includes
);
209 TALLOC_FREE(mem_ctx
);
214 /**********************************************************************
216 * the main conf functions
218 **********************************************************************/
220 static int net_conf_list(struct smbconf_ctx
*conf_ctx
,
221 int argc
, const char **argv
)
223 WERROR werr
= WERR_OK
;
228 uint32_t *num_params
;
230 char ***param_values
;
231 uint32_t share_count
, param_count
;
233 mem_ctx
= talloc_stackframe();
236 net_conf_list_usage(argc
, argv
);
240 werr
= smbconf_get_config(conf_ctx
, mem_ctx
, &num_shares
, &share_names
,
241 &num_params
, ¶m_names
, ¶m_values
);
242 if (!W_ERROR_IS_OK(werr
)) {
243 d_fprintf(stderr
, "Error getting config: %s\n",
248 for (share_count
= 0; share_count
< num_shares
; share_count
++) {
249 const char *indent
= "";
250 if (share_names
[share_count
] != NULL
) {
251 d_printf("[%s]\n", share_names
[share_count
]);
254 for (param_count
= 0; param_count
< num_params
[share_count
];
257 d_printf("%s%s = %s\n",
259 param_names
[share_count
][param_count
],
260 param_values
[share_count
][param_count
]);
268 TALLOC_FREE(mem_ctx
);
272 static int net_conf_import(struct smbconf_ctx
*conf_ctx
,
273 int argc
, const char **argv
)
276 const char *filename
= NULL
;
277 const char *servicename
= NULL
;
278 char *conf_source
= NULL
;
280 struct smbconf_ctx
*txt_ctx
;
283 mem_ctx
= talloc_stackframe();
288 net_conf_import_usage(argc
, argv
);
291 servicename
= talloc_strdup_lower(mem_ctx
, argv
[1]);
292 if (servicename
== NULL
) {
293 d_printf("error: out of memory!\n");
301 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
304 conf_source
= talloc_asprintf(mem_ctx
, "file:%s", filename
);
305 if (conf_source
== NULL
) {
306 d_printf("error: out of memory!\n");
310 werr
= smbconf_init(mem_ctx
, &txt_ctx
, conf_source
);
311 if (!W_ERROR_IS_OK(werr
)) {
312 d_printf("error loading file '%s': %s\n", filename
,
318 d_printf("\nTEST MODE - "
319 "would import the following configuration:\n\n");
322 if (servicename
!= NULL
) {
323 char **param_names
, **param_values
;
326 werr
= smbconf_get_share(txt_ctx
, mem_ctx
,
331 if (!W_ERROR_IS_OK(werr
)) {
334 werr
= import_process_service(conf_ctx
,
337 (const char **)param_names
,
338 (const char **)param_values
);
339 if (!W_ERROR_IS_OK(werr
)) {
343 char **share_names
, ***param_names
, ***param_values
;
344 uint32_t num_shares
, *num_params
, sidx
;
346 werr
= smbconf_get_config(txt_ctx
, mem_ctx
,
352 if (!W_ERROR_IS_OK(werr
)) {
356 werr
= smbconf_drop(conf_ctx
);
357 if (!W_ERROR_IS_OK(werr
)) {
361 for (sidx
= 0; sidx
< num_shares
; sidx
++) {
362 werr
= import_process_service(conf_ctx
,
365 (const char **)param_names
[sidx
],
366 (const char **)param_values
[sidx
]);
367 if (!W_ERROR_IS_OK(werr
)) {
376 TALLOC_FREE(mem_ctx
);
380 static int net_conf_listshares(struct smbconf_ctx
*conf_ctx
,
381 int argc
, const char **argv
)
383 WERROR werr
= WERR_OK
;
385 uint32_t count
, num_shares
= 0;
386 char **share_names
= NULL
;
389 mem_ctx
= talloc_stackframe();
392 net_conf_listshares_usage(argc
, argv
);
396 werr
= smbconf_get_share_names(conf_ctx
, mem_ctx
, &num_shares
,
398 if (!W_ERROR_IS_OK(werr
)) {
402 for (count
= 0; count
< num_shares
; count
++)
404 d_printf("%s\n", share_names
[count
]);
410 TALLOC_FREE(mem_ctx
);
414 static int net_conf_drop(struct smbconf_ctx
*conf_ctx
,
415 int argc
, const char **argv
)
421 net_conf_drop_usage(argc
, argv
);
425 werr
= smbconf_drop(conf_ctx
);
426 if (!W_ERROR_IS_OK(werr
)) {
427 d_fprintf(stderr
, "Error deleting configuration: %s\n",
438 static int net_conf_showshare(struct smbconf_ctx
*conf_ctx
,
439 int argc
, const char **argv
)
442 WERROR werr
= WERR_OK
;
443 const char *sharename
= NULL
;
450 mem_ctx
= talloc_stackframe();
453 net_conf_showshare_usage(argc
, argv
);
457 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
458 if (sharename
== NULL
) {
459 d_printf("error: out of memory!\n");
463 werr
= smbconf_get_share(conf_ctx
, mem_ctx
, sharename
, &num_params
,
464 ¶m_names
, ¶m_values
);
465 if (!W_ERROR_IS_OK(werr
)) {
466 d_printf("error getting share parameters: %s\n",
471 d_printf("[%s]\n", sharename
);
473 for (count
= 0; count
< num_params
; count
++) {
474 d_printf("\t%s = %s\n", param_names
[count
],
475 param_values
[count
]);
481 TALLOC_FREE(mem_ctx
);
486 * Add a share, with a couple of standard parameters, partly optional.
488 * This is a high level utility function of the net conf utility,
489 * not a direct frontend to the smbconf API.
491 static int net_conf_addshare(struct smbconf_ctx
*conf_ctx
,
492 int argc
, const char **argv
)
495 WERROR werr
= WERR_OK
;
496 char *sharename
= NULL
;
497 const char *path
= NULL
;
498 const char *comment
= NULL
;
499 const char *guest_ok
= "no";
500 const char *writeable
= "no";
501 SMB_STRUCT_STAT sbuf
;
502 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
508 net_conf_addshare_usage(argc
, argv
);
513 if (!strnequal(argv
[3], "guest_ok=", 9)) {
514 net_conf_addshare_usage(argc
, argv
);
517 switch (argv
[3][9]) {
527 net_conf_addshare_usage(argc
, argv
);
531 if (!strnequal(argv
[2], "writeable=", 10)) {
532 net_conf_addshare_usage(argc
, argv
);
535 switch (argv
[2][10]) {
545 net_conf_addshare_usage(argc
, argv
);
550 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
551 if (sharename
== NULL
) {
552 d_printf("error: out of memory!\n");
563 /* validate share name */
565 if (!validate_net_name(sharename
, INVALID_SHARENAME_CHARS
,
568 d_fprintf(stderr
, "ERROR: share name %s contains "
569 "invalid characters (any of %s)\n",
570 sharename
, INVALID_SHARENAME_CHARS
);
574 if (getpwnam(sharename
)) {
575 d_fprintf(stderr
, "ERROR: share name %s is already a valid "
576 "system user name.\n", sharename
);
580 if (strequal(sharename
, GLOBAL_NAME
)) {
582 "ERROR: 'global' is not a valid share name.\n");
586 if (smbconf_share_exists(conf_ctx
, sharename
)) {
587 d_fprintf(stderr
, "ERROR: share %s already exists.\n",
594 if (path
[0] != '/') {
596 "Error: path '%s' is not an absolute path.\n",
601 if (sys_stat(path
, &sbuf
) != 0) {
603 "ERROR: cannot stat path '%s' to ensure "
604 "this is a directory.\n"
606 path
, strerror(errno
));
610 if (!S_ISDIR(sbuf
.st_mode
)) {
612 "ERROR: path '%s' is not a directory.\n",
621 werr
= smbconf_create_share(conf_ctx
, sharename
);
622 if (!W_ERROR_IS_OK(werr
)) {
623 d_fprintf(stderr
, "Error creating share %s: %s\n",
624 sharename
, dos_errstr(werr
));
629 * fill the share with parameters
632 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "path", path
);
633 if (!W_ERROR_IS_OK(werr
)) {
634 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
635 "path", dos_errstr(werr
));
639 if (comment
!= NULL
) {
640 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "comment",
642 if (!W_ERROR_IS_OK(werr
)) {
643 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
644 "comment", dos_errstr(werr
));
649 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "guest ok", guest_ok
);
650 if (!W_ERROR_IS_OK(werr
)) {
651 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
652 "'guest ok'", dos_errstr(werr
));
656 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "writeable",
658 if (!W_ERROR_IS_OK(werr
)) {
659 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
660 "writeable", dos_errstr(werr
));
667 TALLOC_FREE(mem_ctx
);
671 static int net_conf_delshare(struct smbconf_ctx
*conf_ctx
,
672 int argc
, const char **argv
)
675 const char *sharename
= NULL
;
676 WERROR werr
= WERR_OK
;
677 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
680 net_conf_delshare_usage(argc
, argv
);
683 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
684 if (sharename
== NULL
) {
685 d_printf("error: out of memory!\n");
689 werr
= smbconf_delete_share(conf_ctx
, sharename
);
690 if (!W_ERROR_IS_OK(werr
)) {
691 d_fprintf(stderr
, "Error deleting share %s: %s\n",
692 sharename
, dos_errstr(werr
));
698 TALLOC_FREE(mem_ctx
);
702 static int net_conf_setparm(struct smbconf_ctx
*conf_ctx
,
703 int argc
, const char **argv
)
706 WERROR werr
= WERR_OK
;
707 char *service
= NULL
;
709 const char *value_str
= NULL
;
710 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
713 net_conf_setparm_usage(argc
, argv
);
716 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
717 if (service
== NULL
) {
718 d_printf("error: out of memory!\n");
721 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
723 d_printf("error: out of memory!\n");
728 if (!smbconf_share_exists(conf_ctx
, service
)) {
729 werr
= smbconf_create_share(conf_ctx
, service
);
730 if (!W_ERROR_IS_OK(werr
)) {
731 d_fprintf(stderr
, "Error creating share '%s': %s\n",
732 service
, dos_errstr(werr
));
737 werr
= smbconf_set_parameter(conf_ctx
, service
, param
, value_str
);
739 if (!W_ERROR_IS_OK(werr
)) {
740 d_fprintf(stderr
, "Error setting value '%s': %s\n",
741 param
, dos_errstr(werr
));
748 TALLOC_FREE(mem_ctx
);
752 static int net_conf_getparm(struct smbconf_ctx
*conf_ctx
,
753 int argc
, const char **argv
)
756 WERROR werr
= WERR_OK
;
757 char *service
= NULL
;
762 mem_ctx
= talloc_stackframe();
765 net_conf_getparm_usage(argc
, argv
);
768 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
769 if (service
== NULL
) {
770 d_printf("error: out of memory!\n");
773 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
775 d_printf("error: out of memory!\n");
779 werr
= smbconf_get_parameter(conf_ctx
, mem_ctx
, service
, param
, &valstr
);
781 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
783 "Error: given service '%s' does not exist.\n",
786 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
788 "Error: given parameter '%s' is not set.\n",
791 } else if (!W_ERROR_IS_OK(werr
)) {
792 d_fprintf(stderr
, "Error getting value '%s': %s.\n",
793 param
, dos_errstr(werr
));
797 d_printf("%s\n", valstr
);
801 TALLOC_FREE(mem_ctx
);
805 static int net_conf_delparm(struct smbconf_ctx
*conf_ctx
,
806 int argc
, const char **argv
)
809 WERROR werr
= WERR_OK
;
810 char *service
= NULL
;
812 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
815 net_conf_delparm_usage(argc
, argv
);
818 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
819 if (service
== NULL
) {
820 d_printf("error: out of memory!\n");
823 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
825 d_printf("error: out of memory!\n");
829 werr
= smbconf_delete_parameter(conf_ctx
, service
, param
);
831 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
833 "Error: given service '%s' does not exist.\n",
836 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
838 "Error: given parameter '%s' is not set.\n",
841 } else if (!W_ERROR_IS_OK(werr
)) {
842 d_fprintf(stderr
, "Error deleting value '%s': %s.\n",
843 param
, dos_errstr(werr
));
850 TALLOC_FREE(mem_ctx
);
854 static int net_conf_getincludes(struct smbconf_ctx
*conf_ctx
,
855 int argc
, const char **argv
)
858 uint32_t num_includes
;
861 char **includes
= NULL
;
863 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
866 net_conf_getincludes_usage(argc
, argv
);
870 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
871 if (service
== NULL
) {
872 d_printf("error: out of memory!\n");
876 werr
= smbconf_get_includes(conf_ctx
, mem_ctx
, service
,
877 &num_includes
, &includes
);
878 if (!W_ERROR_IS_OK(werr
)) {
879 d_printf("error getting includes: %s\n", dos_errstr(werr
));
883 for (count
= 0; count
< num_includes
; count
++) {
884 d_printf("include = %s\n", includes
[count
]);
890 TALLOC_FREE(mem_ctx
);
894 static int net_conf_setincludes(struct smbconf_ctx
*conf_ctx
,
895 int argc
, const char **argv
)
899 uint32_t num_includes
;
900 const char **includes
;
902 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
905 net_conf_setincludes_usage(argc
, argv
);
909 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
910 if (service
== NULL
) {
911 d_printf("error: out of memory!\n");
915 num_includes
= argc
- 1;
916 if (num_includes
== 0) {
922 werr
= smbconf_set_includes(conf_ctx
, service
, num_includes
, includes
);
923 if (!W_ERROR_IS_OK(werr
)) {
924 d_printf("error setting includes: %s\n", dos_errstr(werr
));
931 TALLOC_FREE(mem_ctx
);
935 static int net_conf_delincludes(struct smbconf_ctx
*conf_ctx
,
936 int argc
, const char **argv
)
941 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
944 net_conf_delincludes_usage(argc
, argv
);
948 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
949 if (service
== NULL
) {
950 d_printf("error: out of memory!\n");
954 werr
= smbconf_delete_includes(conf_ctx
, service
);
955 if (!W_ERROR_IS_OK(werr
)) {
956 d_printf("error deleting includes: %s\n", dos_errstr(werr
));
963 TALLOC_FREE(mem_ctx
);
968 /**********************************************************************
970 * Wrapper and net_conf_run_function mechanism.
972 **********************************************************************/
975 * Wrapper function to call the main conf functions.
976 * The wrapper calls handles opening and closing of the
979 static int net_conf_wrap_function(int (*fn
)(struct smbconf_ctx
*,
981 int argc
, const char **argv
)
984 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
985 struct smbconf_ctx
*conf_ctx
;
988 werr
= smbconf_init(mem_ctx
, &conf_ctx
, "registry:");
990 if (!W_ERROR_IS_OK(werr
)) {
994 ret
= fn(conf_ctx
, argc
, argv
);
996 smbconf_shutdown(conf_ctx
);
1002 * We need a functable struct of our own, because the
1003 * functions are called through a wrapper that handles
1004 * the opening and closing of the configuration, and so on.
1006 struct conf_functable
{
1007 const char *funcname
;
1008 int (*fn
)(struct smbconf_ctx
*ctx
, int argc
, const char **argv
);
1009 const char *helptext
;
1013 * This imitates net_run_function2 but calls the main functions
1014 * through the wrapper net_conf_wrap_function().
1016 static int net_conf_run_function(int argc
, const char **argv
,
1018 struct conf_functable
*table
)
1023 for (i
=0; table
[i
].funcname
; i
++) {
1024 if (StrCaseCmp(argv
[0], table
[i
].funcname
) == 0)
1025 return net_conf_wrap_function(table
[i
].fn
,
1031 for (i
=0; table
[i
].funcname
; i
++) {
1032 d_printf("%s %-15s %s\n", whoami
, table
[i
].funcname
,
1040 * Entry-point for all the CONF functions.
1043 int net_conf(int argc
, const char **argv
)
1046 struct conf_functable func_table
[] = {
1047 {"list", net_conf_list
,
1048 "Dump the complete configuration in smb.conf like format."},
1049 {"import", net_conf_import
,
1050 "Import configuration from file in smb.conf format."},
1051 {"listshares", net_conf_listshares
,
1052 "List the share names."},
1053 {"drop", net_conf_drop
,
1054 "Delete the complete configuration."},
1055 {"showshare", net_conf_showshare
,
1056 "Show the definition of a share."},
1057 {"addshare", net_conf_addshare
,
1058 "Create a new share."},
1059 {"delshare", net_conf_delshare
,
1061 {"setparm", net_conf_setparm
,
1062 "Store a parameter."},
1063 {"getparm", net_conf_getparm
,
1064 "Retrieve the value of a parameter."},
1065 {"delparm", net_conf_delparm
,
1066 "Delete a parameter."},
1067 {"getincludes", net_conf_getincludes
,
1068 "Show the includes of a share definition."},
1069 {"setincludes", net_conf_setincludes
,
1070 "Set includes for a share."},
1071 {"delincludes", net_conf_delincludes
,
1072 "Delete includes from a share definition."},
1076 ret
= net_conf_run_function(argc
, argv
, "net conf", func_table
);