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 d_printf("[%s]\n", servicename
);
154 for (idx
= 0; idx
< num_params
; idx
++) {
155 d_printf("\t%s = %s\n", param_names
[idx
],
162 if (smbconf_share_exists(conf_ctx
, servicename
)) {
163 werr
= smbconf_delete_share(conf_ctx
, servicename
);
164 if (!W_ERROR_IS_OK(werr
)) {
168 werr
= smbconf_create_share(conf_ctx
, servicename
);
169 if (!W_ERROR_IS_OK(werr
)) {
173 for (idx
= 0; idx
< num_params
; idx
++) {
174 if (strequal(param_names
[idx
], "include")) {
175 includes
= TALLOC_REALLOC_ARRAY(mem_ctx
,
179 if (includes
== NULL
) {
183 includes
[num_includes
] = talloc_strdup(includes
,
185 if (includes
[num_includes
] == NULL
) {
191 werr
= smbconf_set_parameter(conf_ctx
,
195 if (!W_ERROR_IS_OK(werr
)) {
201 werr
= smbconf_set_includes(conf_ctx
, servicename
, num_includes
,
202 (const char **)includes
);
205 TALLOC_FREE(mem_ctx
);
210 /**********************************************************************
212 * the main conf functions
214 **********************************************************************/
216 static int net_conf_list(struct smbconf_ctx
*conf_ctx
,
217 int argc
, const char **argv
)
219 WERROR werr
= WERR_OK
;
224 uint32_t *num_params
;
226 char ***param_values
;
227 uint32_t share_count
, param_count
;
229 mem_ctx
= talloc_stackframe();
232 net_conf_list_usage(argc
, argv
);
236 werr
= smbconf_get_config(conf_ctx
, mem_ctx
, &num_shares
, &share_names
,
237 &num_params
, ¶m_names
, ¶m_values
);
238 if (!W_ERROR_IS_OK(werr
)) {
239 d_fprintf(stderr
, "Error getting config: %s\n",
244 for (share_count
= 0; share_count
< num_shares
; share_count
++) {
245 d_printf("[%s]\n", share_names
[share_count
]);
246 for (param_count
= 0; param_count
< num_params
[share_count
];
249 d_printf("\t%s = %s\n",
250 param_names
[share_count
][param_count
],
251 param_values
[share_count
][param_count
]);
259 TALLOC_FREE(mem_ctx
);
263 static int net_conf_import(struct smbconf_ctx
*conf_ctx
,
264 int argc
, const char **argv
)
267 const char *filename
= NULL
;
268 const char *servicename
= NULL
;
269 char *conf_source
= NULL
;
271 struct smbconf_ctx
*txt_ctx
;
274 mem_ctx
= talloc_stackframe();
279 net_conf_import_usage(argc
, argv
);
282 servicename
= talloc_strdup_lower(mem_ctx
, argv
[1]);
283 if (servicename
== NULL
) {
284 d_printf("error: out of memory!\n");
292 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
295 conf_source
= talloc_asprintf(mem_ctx
, "file:%s", filename
);
296 if (conf_source
== NULL
) {
297 d_printf("error: out of memory!\n");
301 werr
= smbconf_init(mem_ctx
, &txt_ctx
, conf_source
);
302 if (!W_ERROR_IS_OK(werr
)) {
303 d_printf("error loading file '%s': %s\n", filename
,
309 d_printf("\nTEST MODE - "
310 "would import the following configuration:\n\n");
313 if (servicename
!= NULL
) {
314 char **param_names
, **param_values
;
317 werr
= smbconf_get_share(txt_ctx
, mem_ctx
,
322 if (!W_ERROR_IS_OK(werr
)) {
325 werr
= import_process_service(conf_ctx
,
328 (const char **)param_names
,
329 (const char **)param_values
);
330 if (!W_ERROR_IS_OK(werr
)) {
334 char **share_names
, ***param_names
, ***param_values
;
335 uint32_t num_shares
, *num_params
, sidx
;
337 werr
= smbconf_get_config(txt_ctx
, mem_ctx
,
343 if (!W_ERROR_IS_OK(werr
)) {
347 werr
= smbconf_drop(conf_ctx
);
348 if (!W_ERROR_IS_OK(werr
)) {
352 for (sidx
= 0; sidx
< num_shares
; sidx
++) {
353 werr
= import_process_service(conf_ctx
,
356 (const char **)param_names
[sidx
],
357 (const char **)param_values
[sidx
]);
358 if (!W_ERROR_IS_OK(werr
)) {
367 TALLOC_FREE(mem_ctx
);
371 static int net_conf_listshares(struct smbconf_ctx
*conf_ctx
,
372 int argc
, const char **argv
)
374 WERROR werr
= WERR_OK
;
376 uint32_t count
, num_shares
= 0;
377 char **share_names
= NULL
;
380 mem_ctx
= talloc_stackframe();
383 net_conf_listshares_usage(argc
, argv
);
387 werr
= smbconf_get_share_names(conf_ctx
, mem_ctx
, &num_shares
,
389 if (!W_ERROR_IS_OK(werr
)) {
393 for (count
= 0; count
< num_shares
; count
++)
395 d_printf("%s\n", share_names
[count
]);
401 TALLOC_FREE(mem_ctx
);
405 static int net_conf_drop(struct smbconf_ctx
*conf_ctx
,
406 int argc
, const char **argv
)
412 net_conf_drop_usage(argc
, argv
);
416 werr
= smbconf_drop(conf_ctx
);
417 if (!W_ERROR_IS_OK(werr
)) {
418 d_fprintf(stderr
, "Error deleting configuration: %s\n",
429 static int net_conf_showshare(struct smbconf_ctx
*conf_ctx
,
430 int argc
, const char **argv
)
433 WERROR werr
= WERR_OK
;
434 const char *sharename
= NULL
;
441 mem_ctx
= talloc_stackframe();
444 net_conf_showshare_usage(argc
, argv
);
448 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
449 if (sharename
== NULL
) {
450 d_printf("error: out of memory!\n");
454 werr
= smbconf_get_share(conf_ctx
, mem_ctx
, sharename
, &num_params
,
455 ¶m_names
, ¶m_values
);
456 if (!W_ERROR_IS_OK(werr
)) {
457 d_printf("error getting share parameters: %s\n",
462 d_printf("[%s]\n", sharename
);
464 for (count
= 0; count
< num_params
; count
++) {
465 d_printf("\t%s = %s\n", param_names
[count
],
466 param_values
[count
]);
472 TALLOC_FREE(mem_ctx
);
477 * Add a share, with a couple of standard parameters, partly optional.
479 * This is a high level utility function of the net conf utility,
480 * not a direct frontend to the smbconf API.
482 static int net_conf_addshare(struct smbconf_ctx
*conf_ctx
,
483 int argc
, const char **argv
)
486 WERROR werr
= WERR_OK
;
487 char *sharename
= NULL
;
488 const char *path
= NULL
;
489 const char *comment
= NULL
;
490 const char *guest_ok
= "no";
491 const char *writeable
= "no";
492 SMB_STRUCT_STAT sbuf
;
493 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
499 net_conf_addshare_usage(argc
, argv
);
504 if (!strnequal(argv
[3], "guest_ok=", 9)) {
505 net_conf_addshare_usage(argc
, argv
);
508 switch (argv
[3][9]) {
518 net_conf_addshare_usage(argc
, argv
);
522 if (!strnequal(argv
[2], "writeable=", 10)) {
523 net_conf_addshare_usage(argc
, argv
);
526 switch (argv
[2][10]) {
536 net_conf_addshare_usage(argc
, argv
);
541 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
542 if (sharename
== NULL
) {
543 d_printf("error: out of memory!\n");
554 /* validate share name */
556 if (!validate_net_name(sharename
, INVALID_SHARENAME_CHARS
,
559 d_fprintf(stderr
, "ERROR: share name %s contains "
560 "invalid characters (any of %s)\n",
561 sharename
, INVALID_SHARENAME_CHARS
);
565 if (getpwnam(sharename
)) {
566 d_fprintf(stderr
, "ERROR: share name %s is already a valid "
567 "system user name.\n", sharename
);
571 if (strequal(sharename
, GLOBAL_NAME
)) {
573 "ERROR: 'global' is not a valid share name.\n");
577 if (smbconf_share_exists(conf_ctx
, sharename
)) {
578 d_fprintf(stderr
, "ERROR: share %s already exists.\n",
585 if (path
[0] != '/') {
587 "Error: path '%s' is not an absolute path.\n",
592 if (sys_stat(path
, &sbuf
) != 0) {
594 "ERROR: cannot stat path '%s' to ensure "
595 "this is a directory.\n"
597 path
, strerror(errno
));
601 if (!S_ISDIR(sbuf
.st_mode
)) {
603 "ERROR: path '%s' is not a directory.\n",
612 werr
= smbconf_create_share(conf_ctx
, sharename
);
613 if (!W_ERROR_IS_OK(werr
)) {
614 d_fprintf(stderr
, "Error creating share %s: %s\n",
615 sharename
, dos_errstr(werr
));
620 * fill the share with parameters
623 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "path", path
);
624 if (!W_ERROR_IS_OK(werr
)) {
625 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
626 "path", dos_errstr(werr
));
630 if (comment
!= NULL
) {
631 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "comment",
633 if (!W_ERROR_IS_OK(werr
)) {
634 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
635 "comment", dos_errstr(werr
));
640 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "guest ok", guest_ok
);
641 if (!W_ERROR_IS_OK(werr
)) {
642 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
643 "'guest ok'", dos_errstr(werr
));
647 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "writeable",
649 if (!W_ERROR_IS_OK(werr
)) {
650 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
651 "writeable", dos_errstr(werr
));
658 TALLOC_FREE(mem_ctx
);
662 static int net_conf_delshare(struct smbconf_ctx
*conf_ctx
,
663 int argc
, const char **argv
)
666 const char *sharename
= NULL
;
667 WERROR werr
= WERR_OK
;
668 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
671 net_conf_delshare_usage(argc
, argv
);
674 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
675 if (sharename
== NULL
) {
676 d_printf("error: out of memory!\n");
680 werr
= smbconf_delete_share(conf_ctx
, sharename
);
681 if (!W_ERROR_IS_OK(werr
)) {
682 d_fprintf(stderr
, "Error deleting share %s: %s\n",
683 sharename
, dos_errstr(werr
));
689 TALLOC_FREE(mem_ctx
);
693 static int net_conf_setparm(struct smbconf_ctx
*conf_ctx
,
694 int argc
, const char **argv
)
697 WERROR werr
= WERR_OK
;
698 char *service
= NULL
;
700 const char *value_str
= NULL
;
701 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
704 net_conf_setparm_usage(argc
, argv
);
707 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
708 if (service
== NULL
) {
709 d_printf("error: out of memory!\n");
712 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
714 d_printf("error: out of memory!\n");
719 if (!smbconf_share_exists(conf_ctx
, service
)) {
720 werr
= smbconf_create_share(conf_ctx
, service
);
721 if (!W_ERROR_IS_OK(werr
)) {
722 d_fprintf(stderr
, "Error creating share '%s': %s\n",
723 service
, dos_errstr(werr
));
728 werr
= smbconf_set_parameter(conf_ctx
, service
, param
, value_str
);
730 if (!W_ERROR_IS_OK(werr
)) {
731 d_fprintf(stderr
, "Error setting value '%s': %s\n",
732 param
, dos_errstr(werr
));
739 TALLOC_FREE(mem_ctx
);
743 static int net_conf_getparm(struct smbconf_ctx
*conf_ctx
,
744 int argc
, const char **argv
)
747 WERROR werr
= WERR_OK
;
748 char *service
= NULL
;
753 mem_ctx
= talloc_stackframe();
756 net_conf_getparm_usage(argc
, argv
);
759 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
760 if (service
== NULL
) {
761 d_printf("error: out of memory!\n");
764 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
766 d_printf("error: out of memory!\n");
770 werr
= smbconf_get_parameter(conf_ctx
, mem_ctx
, service
, param
, &valstr
);
772 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
774 "Error: given service '%s' does not exist.\n",
777 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
779 "Error: given parameter '%s' is not set.\n",
782 } else if (!W_ERROR_IS_OK(werr
)) {
783 d_fprintf(stderr
, "Error getting value '%s': %s.\n",
784 param
, dos_errstr(werr
));
788 d_printf("%s\n", valstr
);
792 TALLOC_FREE(mem_ctx
);
796 static int net_conf_delparm(struct smbconf_ctx
*conf_ctx
,
797 int argc
, const char **argv
)
800 WERROR werr
= WERR_OK
;
801 char *service
= NULL
;
803 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
806 net_conf_delparm_usage(argc
, argv
);
809 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
810 if (service
== NULL
) {
811 d_printf("error: out of memory!\n");
814 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
816 d_printf("error: out of memory!\n");
820 werr
= smbconf_delete_parameter(conf_ctx
, service
, param
);
822 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
824 "Error: given service '%s' does not exist.\n",
827 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
829 "Error: given parameter '%s' is not set.\n",
832 } else if (!W_ERROR_IS_OK(werr
)) {
833 d_fprintf(stderr
, "Error deleting value '%s': %s.\n",
834 param
, dos_errstr(werr
));
841 TALLOC_FREE(mem_ctx
);
845 static int net_conf_getincludes(struct smbconf_ctx
*conf_ctx
,
846 int argc
, const char **argv
)
849 uint32_t num_includes
;
852 char **includes
= NULL
;
854 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
857 net_conf_getincludes_usage(argc
, argv
);
861 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
862 if (service
== NULL
) {
863 d_printf("error: out of memory!\n");
867 werr
= smbconf_get_includes(conf_ctx
, mem_ctx
, service
,
868 &num_includes
, &includes
);
869 if (!W_ERROR_IS_OK(werr
)) {
870 d_printf("error getting includes: %s\n", dos_errstr(werr
));
874 for (count
= 0; count
< num_includes
; count
++) {
875 d_printf("include = %s\n", includes
[count
]);
881 TALLOC_FREE(mem_ctx
);
885 static int net_conf_setincludes(struct smbconf_ctx
*conf_ctx
,
886 int argc
, const char **argv
)
890 uint32_t num_includes
;
891 const char **includes
;
893 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
896 net_conf_setincludes_usage(argc
, argv
);
900 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
901 if (service
== NULL
) {
902 d_printf("error: out of memory!\n");
906 num_includes
= argc
- 1;
907 if (num_includes
== 0) {
913 werr
= smbconf_set_includes(conf_ctx
, service
, num_includes
, includes
);
914 if (!W_ERROR_IS_OK(werr
)) {
915 d_printf("error setting includes: %s\n", dos_errstr(werr
));
922 TALLOC_FREE(mem_ctx
);
926 static int net_conf_delincludes(struct smbconf_ctx
*conf_ctx
,
927 int argc
, const char **argv
)
932 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
935 net_conf_delincludes_usage(argc
, argv
);
939 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
940 if (service
== NULL
) {
941 d_printf("error: out of memory!\n");
945 werr
= smbconf_delete_includes(conf_ctx
, service
);
946 if (!W_ERROR_IS_OK(werr
)) {
947 d_printf("error deleting includes: %s\n", dos_errstr(werr
));
954 TALLOC_FREE(mem_ctx
);
959 /**********************************************************************
961 * Wrapper and net_conf_run_function mechanism.
963 **********************************************************************/
966 * Wrapper function to call the main conf functions.
967 * The wrapper calls handles opening and closing of the
970 static int net_conf_wrap_function(int (*fn
)(struct smbconf_ctx
*,
972 int argc
, const char **argv
)
975 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
976 struct smbconf_ctx
*conf_ctx
;
979 werr
= smbconf_init(mem_ctx
, &conf_ctx
, "registry:");
981 if (!W_ERROR_IS_OK(werr
)) {
985 ret
= fn(conf_ctx
, argc
, argv
);
987 smbconf_shutdown(conf_ctx
);
993 * We need a functable struct of our own, because the
994 * functions are called through a wrapper that handles
995 * the opening and closing of the configuration, and so on.
997 struct conf_functable
{
998 const char *funcname
;
999 int (*fn
)(struct smbconf_ctx
*ctx
, int argc
, const char **argv
);
1000 const char *helptext
;
1004 * This imitates net_run_function2 but calls the main functions
1005 * through the wrapper net_conf_wrap_function().
1007 static int net_conf_run_function(int argc
, const char **argv
,
1009 struct conf_functable
*table
)
1014 for (i
=0; table
[i
].funcname
; i
++) {
1015 if (StrCaseCmp(argv
[0], table
[i
].funcname
) == 0)
1016 return net_conf_wrap_function(table
[i
].fn
,
1022 for (i
=0; table
[i
].funcname
; i
++) {
1023 d_printf("%s %-15s %s\n", whoami
, table
[i
].funcname
,
1031 * Entry-point for all the CONF functions.
1034 int net_conf(int argc
, const char **argv
)
1037 struct conf_functable func_table
[] = {
1038 {"list", net_conf_list
,
1039 "Dump the complete configuration in smb.conf like format."},
1040 {"import", net_conf_import
,
1041 "Import configuration from file in smb.conf format."},
1042 {"listshares", net_conf_listshares
,
1043 "List the share names."},
1044 {"drop", net_conf_drop
,
1045 "Delete the complete configuration."},
1046 {"showshare", net_conf_showshare
,
1047 "Show the definition of a share."},
1048 {"addshare", net_conf_addshare
,
1049 "Create a new share."},
1050 {"delshare", net_conf_delshare
,
1052 {"setparm", net_conf_setparm
,
1053 "Store a parameter."},
1054 {"getparm", net_conf_getparm
,
1055 "Retrieve the value of a parameter."},
1056 {"delparm", net_conf_delparm
,
1057 "Delete a parameter."},
1058 {"getincludes", net_conf_getincludes
,
1059 "Show the includes of a share definition."},
1060 {"setincludes", net_conf_setincludes
,
1061 "Set includes for a share."},
1062 {"delincludes", net_conf_delincludes
,
1063 "Delete includes from a share definition."},
1067 ret
= net_conf_run_function(argc
, argv
, "net conf", func_table
);