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 struct smbconf_service
*service
)
144 WERROR werr
= WERR_OK
;
145 uint32_t num_includes
= 0;
146 char **includes
= NULL
;
147 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
150 const char *indent
= "";
151 if (service
->name
!= NULL
) {
152 d_printf("[%s]\n", service
->name
);
155 for (idx
= 0; idx
< service
->num_params
; idx
++) {
156 d_printf("%s%s = %s\n", indent
,
157 service
->param_names
[idx
],
158 service
->param_values
[idx
]);
164 if (smbconf_share_exists(conf_ctx
, service
->name
)) {
165 werr
= smbconf_delete_share(conf_ctx
, service
->name
);
166 if (!W_ERROR_IS_OK(werr
)) {
170 werr
= smbconf_create_share(conf_ctx
, service
->name
);
171 if (!W_ERROR_IS_OK(werr
)) {
175 for (idx
= 0; idx
< service
->num_params
; idx
++) {
176 if (strequal(service
->param_names
[idx
], "include")) {
177 includes
= TALLOC_REALLOC_ARRAY(mem_ctx
,
181 if (includes
== NULL
) {
185 includes
[num_includes
] = talloc_strdup(includes
,
186 service
->param_values
[idx
]);
187 if (includes
[num_includes
] == NULL
) {
193 werr
= smbconf_set_parameter(conf_ctx
,
195 service
->param_names
[idx
],
196 service
->param_values
[idx
]);
197 if (!W_ERROR_IS_OK(werr
)) {
203 werr
= smbconf_set_includes(conf_ctx
, service
->name
, num_includes
,
204 (const char **)includes
);
207 TALLOC_FREE(mem_ctx
);
212 /**********************************************************************
214 * the main conf functions
216 **********************************************************************/
218 static int net_conf_list(struct smbconf_ctx
*conf_ctx
,
219 int argc
, const char **argv
)
221 WERROR werr
= WERR_OK
;
225 uint32_t share_count
, param_count
;
226 struct smbconf_service
**shares
= NULL
;
228 mem_ctx
= talloc_stackframe();
231 net_conf_list_usage(argc
, argv
);
235 werr
= smbconf_get_config(conf_ctx
, mem_ctx
, &num_shares
, &shares
);
236 if (!W_ERROR_IS_OK(werr
)) {
237 d_fprintf(stderr
, "Error getting config: %s\n",
242 for (share_count
= 0; share_count
< num_shares
; share_count
++) {
243 const char *indent
= "";
244 if (shares
[share_count
]->name
!= NULL
) {
245 d_printf("[%s]\n", shares
[share_count
]->name
);
248 for (param_count
= 0;
249 param_count
< shares
[share_count
]->num_params
;
252 d_printf("%s%s = %s\n",
254 shares
[share_count
]->param_names
[param_count
],
255 shares
[share_count
]->param_values
[param_count
]);
263 TALLOC_FREE(mem_ctx
);
267 static int net_conf_import(struct smbconf_ctx
*conf_ctx
,
268 int argc
, const char **argv
)
271 const char *filename
= NULL
;
272 const char *servicename
= NULL
;
273 char *conf_source
= NULL
;
275 struct smbconf_ctx
*txt_ctx
;
278 mem_ctx
= talloc_stackframe();
283 net_conf_import_usage(argc
, argv
);
286 servicename
= talloc_strdup_lower(mem_ctx
, argv
[1]);
287 if (servicename
== NULL
) {
288 d_printf("error: out of memory!\n");
296 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
299 conf_source
= talloc_asprintf(mem_ctx
, "file:%s", filename
);
300 if (conf_source
== NULL
) {
301 d_printf("error: out of memory!\n");
305 werr
= smbconf_init(mem_ctx
, &txt_ctx
, conf_source
);
306 if (!W_ERROR_IS_OK(werr
)) {
307 d_printf("error loading file '%s': %s\n", filename
,
313 d_printf("\nTEST MODE - "
314 "would import the following configuration:\n\n");
317 if (servicename
!= NULL
) {
318 struct smbconf_service
*service
= NULL
;
320 werr
= smbconf_get_share(txt_ctx
, mem_ctx
,
323 if (!W_ERROR_IS_OK(werr
)) {
326 werr
= import_process_service(conf_ctx
, service
);
327 if (!W_ERROR_IS_OK(werr
)) {
331 struct smbconf_service
**services
= NULL
;
332 uint32_t num_shares
, sidx
;
334 werr
= smbconf_get_config(txt_ctx
, mem_ctx
,
337 if (!W_ERROR_IS_OK(werr
)) {
341 werr
= smbconf_drop(conf_ctx
);
342 if (!W_ERROR_IS_OK(werr
)) {
346 for (sidx
= 0; sidx
< num_shares
; sidx
++) {
347 werr
= import_process_service(conf_ctx
, services
[sidx
]);
348 if (!W_ERROR_IS_OK(werr
)) {
357 TALLOC_FREE(mem_ctx
);
361 static int net_conf_listshares(struct smbconf_ctx
*conf_ctx
,
362 int argc
, const char **argv
)
364 WERROR werr
= WERR_OK
;
366 uint32_t count
, num_shares
= 0;
367 char **share_names
= NULL
;
370 mem_ctx
= talloc_stackframe();
373 net_conf_listshares_usage(argc
, argv
);
377 werr
= smbconf_get_share_names(conf_ctx
, mem_ctx
, &num_shares
,
379 if (!W_ERROR_IS_OK(werr
)) {
383 for (count
= 0; count
< num_shares
; count
++)
385 d_printf("%s\n", share_names
[count
]);
391 TALLOC_FREE(mem_ctx
);
395 static int net_conf_drop(struct smbconf_ctx
*conf_ctx
,
396 int argc
, const char **argv
)
402 net_conf_drop_usage(argc
, argv
);
406 werr
= smbconf_drop(conf_ctx
);
407 if (!W_ERROR_IS_OK(werr
)) {
408 d_fprintf(stderr
, "Error deleting configuration: %s\n",
419 static int net_conf_showshare(struct smbconf_ctx
*conf_ctx
,
420 int argc
, const char **argv
)
423 WERROR werr
= WERR_OK
;
424 const char *sharename
= NULL
;
427 struct smbconf_service
*service
= NULL
;
429 mem_ctx
= talloc_stackframe();
432 net_conf_showshare_usage(argc
, argv
);
436 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
437 if (sharename
== NULL
) {
438 d_printf("error: out of memory!\n");
442 werr
= smbconf_get_share(conf_ctx
, mem_ctx
, sharename
, &service
);
443 if (!W_ERROR_IS_OK(werr
)) {
444 d_printf("error getting share parameters: %s\n",
449 d_printf("[%s]\n", sharename
);
451 for (count
= 0; count
< service
->num_params
; count
++) {
452 d_printf("\t%s = %s\n", service
->param_names
[count
],
453 service
->param_values
[count
]);
459 TALLOC_FREE(mem_ctx
);
464 * Add a share, with a couple of standard parameters, partly optional.
466 * This is a high level utility function of the net conf utility,
467 * not a direct frontend to the smbconf API.
469 static int net_conf_addshare(struct smbconf_ctx
*conf_ctx
,
470 int argc
, const char **argv
)
473 WERROR werr
= WERR_OK
;
474 char *sharename
= NULL
;
475 const char *path
= NULL
;
476 const char *comment
= NULL
;
477 const char *guest_ok
= "no";
478 const char *writeable
= "no";
479 SMB_STRUCT_STAT sbuf
;
480 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
486 net_conf_addshare_usage(argc
, argv
);
491 if (!strnequal(argv
[3], "guest_ok=", 9)) {
492 net_conf_addshare_usage(argc
, argv
);
495 switch (argv
[3][9]) {
505 net_conf_addshare_usage(argc
, argv
);
509 if (!strnequal(argv
[2], "writeable=", 10)) {
510 net_conf_addshare_usage(argc
, argv
);
513 switch (argv
[2][10]) {
523 net_conf_addshare_usage(argc
, argv
);
528 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
529 if (sharename
== NULL
) {
530 d_printf("error: out of memory!\n");
541 /* validate share name */
543 if (!validate_net_name(sharename
, INVALID_SHARENAME_CHARS
,
546 d_fprintf(stderr
, "ERROR: share name %s contains "
547 "invalid characters (any of %s)\n",
548 sharename
, INVALID_SHARENAME_CHARS
);
552 if (getpwnam(sharename
)) {
553 d_fprintf(stderr
, "ERROR: share name %s is already a valid "
554 "system user name.\n", sharename
);
558 if (strequal(sharename
, GLOBAL_NAME
)) {
560 "ERROR: 'global' is not a valid share name.\n");
564 if (smbconf_share_exists(conf_ctx
, sharename
)) {
565 d_fprintf(stderr
, "ERROR: share %s already exists.\n",
572 if (path
[0] != '/') {
574 "Error: path '%s' is not an absolute path.\n",
579 if (sys_stat(path
, &sbuf
) != 0) {
581 "ERROR: cannot stat path '%s' to ensure "
582 "this is a directory.\n"
584 path
, strerror(errno
));
588 if (!S_ISDIR(sbuf
.st_mode
)) {
590 "ERROR: path '%s' is not a directory.\n",
599 werr
= smbconf_create_share(conf_ctx
, sharename
);
600 if (!W_ERROR_IS_OK(werr
)) {
601 d_fprintf(stderr
, "Error creating share %s: %s\n",
602 sharename
, dos_errstr(werr
));
607 * fill the share with parameters
610 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "path", path
);
611 if (!W_ERROR_IS_OK(werr
)) {
612 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
613 "path", dos_errstr(werr
));
617 if (comment
!= NULL
) {
618 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "comment",
620 if (!W_ERROR_IS_OK(werr
)) {
621 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
622 "comment", dos_errstr(werr
));
627 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "guest ok", guest_ok
);
628 if (!W_ERROR_IS_OK(werr
)) {
629 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
630 "'guest ok'", dos_errstr(werr
));
634 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "writeable",
636 if (!W_ERROR_IS_OK(werr
)) {
637 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
638 "writeable", dos_errstr(werr
));
645 TALLOC_FREE(mem_ctx
);
649 static int net_conf_delshare(struct smbconf_ctx
*conf_ctx
,
650 int argc
, const char **argv
)
653 const char *sharename
= NULL
;
654 WERROR werr
= WERR_OK
;
655 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
658 net_conf_delshare_usage(argc
, argv
);
661 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
662 if (sharename
== NULL
) {
663 d_printf("error: out of memory!\n");
667 werr
= smbconf_delete_share(conf_ctx
, sharename
);
668 if (!W_ERROR_IS_OK(werr
)) {
669 d_fprintf(stderr
, "Error deleting share %s: %s\n",
670 sharename
, dos_errstr(werr
));
676 TALLOC_FREE(mem_ctx
);
680 static int net_conf_setparm(struct smbconf_ctx
*conf_ctx
,
681 int argc
, const char **argv
)
684 WERROR werr
= WERR_OK
;
685 char *service
= NULL
;
687 const char *value_str
= NULL
;
688 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
691 net_conf_setparm_usage(argc
, argv
);
694 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
695 if (service
== NULL
) {
696 d_printf("error: out of memory!\n");
699 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
701 d_printf("error: out of memory!\n");
706 if (!smbconf_share_exists(conf_ctx
, service
)) {
707 werr
= smbconf_create_share(conf_ctx
, service
);
708 if (!W_ERROR_IS_OK(werr
)) {
709 d_fprintf(stderr
, "Error creating share '%s': %s\n",
710 service
, dos_errstr(werr
));
715 werr
= smbconf_set_parameter(conf_ctx
, service
, param
, value_str
);
717 if (!W_ERROR_IS_OK(werr
)) {
718 d_fprintf(stderr
, "Error setting value '%s': %s\n",
719 param
, dos_errstr(werr
));
726 TALLOC_FREE(mem_ctx
);
730 static int net_conf_getparm(struct smbconf_ctx
*conf_ctx
,
731 int argc
, const char **argv
)
734 WERROR werr
= WERR_OK
;
735 char *service
= NULL
;
740 mem_ctx
= talloc_stackframe();
743 net_conf_getparm_usage(argc
, argv
);
746 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
747 if (service
== NULL
) {
748 d_printf("error: out of memory!\n");
751 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
753 d_printf("error: out of memory!\n");
757 werr
= smbconf_get_parameter(conf_ctx
, mem_ctx
, service
, param
, &valstr
);
759 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
761 "Error: given service '%s' does not exist.\n",
764 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
766 "Error: given parameter '%s' is not set.\n",
769 } else if (!W_ERROR_IS_OK(werr
)) {
770 d_fprintf(stderr
, "Error getting value '%s': %s.\n",
771 param
, dos_errstr(werr
));
775 d_printf("%s\n", valstr
);
779 TALLOC_FREE(mem_ctx
);
783 static int net_conf_delparm(struct smbconf_ctx
*conf_ctx
,
784 int argc
, const char **argv
)
787 WERROR werr
= WERR_OK
;
788 char *service
= NULL
;
790 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
793 net_conf_delparm_usage(argc
, argv
);
796 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
797 if (service
== NULL
) {
798 d_printf("error: out of memory!\n");
801 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
803 d_printf("error: out of memory!\n");
807 werr
= smbconf_delete_parameter(conf_ctx
, service
, param
);
809 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
811 "Error: given service '%s' does not exist.\n",
814 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
816 "Error: given parameter '%s' is not set.\n",
819 } else if (!W_ERROR_IS_OK(werr
)) {
820 d_fprintf(stderr
, "Error deleting value '%s': %s.\n",
821 param
, dos_errstr(werr
));
828 TALLOC_FREE(mem_ctx
);
832 static int net_conf_getincludes(struct smbconf_ctx
*conf_ctx
,
833 int argc
, const char **argv
)
836 uint32_t num_includes
;
839 char **includes
= NULL
;
841 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
844 net_conf_getincludes_usage(argc
, argv
);
848 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
849 if (service
== NULL
) {
850 d_printf("error: out of memory!\n");
854 werr
= smbconf_get_includes(conf_ctx
, mem_ctx
, service
,
855 &num_includes
, &includes
);
856 if (!W_ERROR_IS_OK(werr
)) {
857 d_printf("error getting includes: %s\n", dos_errstr(werr
));
861 for (count
= 0; count
< num_includes
; count
++) {
862 d_printf("include = %s\n", includes
[count
]);
868 TALLOC_FREE(mem_ctx
);
872 static int net_conf_setincludes(struct smbconf_ctx
*conf_ctx
,
873 int argc
, const char **argv
)
877 uint32_t num_includes
;
878 const char **includes
;
880 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
883 net_conf_setincludes_usage(argc
, argv
);
887 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
888 if (service
== NULL
) {
889 d_printf("error: out of memory!\n");
893 num_includes
= argc
- 1;
894 if (num_includes
== 0) {
900 werr
= smbconf_set_includes(conf_ctx
, service
, num_includes
, includes
);
901 if (!W_ERROR_IS_OK(werr
)) {
902 d_printf("error setting includes: %s\n", dos_errstr(werr
));
909 TALLOC_FREE(mem_ctx
);
913 static int net_conf_delincludes(struct smbconf_ctx
*conf_ctx
,
914 int argc
, const char **argv
)
919 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
922 net_conf_delincludes_usage(argc
, argv
);
926 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
927 if (service
== NULL
) {
928 d_printf("error: out of memory!\n");
932 werr
= smbconf_delete_includes(conf_ctx
, service
);
933 if (!W_ERROR_IS_OK(werr
)) {
934 d_printf("error deleting includes: %s\n", dos_errstr(werr
));
941 TALLOC_FREE(mem_ctx
);
946 /**********************************************************************
948 * Wrapper and net_conf_run_function mechanism.
950 **********************************************************************/
953 * Wrapper function to call the main conf functions.
954 * The wrapper calls handles opening and closing of the
957 static int net_conf_wrap_function(int (*fn
)(struct smbconf_ctx
*,
959 int argc
, const char **argv
)
962 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
963 struct smbconf_ctx
*conf_ctx
;
966 werr
= smbconf_init(mem_ctx
, &conf_ctx
, "registry:");
968 if (!W_ERROR_IS_OK(werr
)) {
972 ret
= fn(conf_ctx
, argc
, argv
);
974 smbconf_shutdown(conf_ctx
);
980 * We need a functable struct of our own, because the
981 * functions are called through a wrapper that handles
982 * the opening and closing of the configuration, and so on.
984 struct conf_functable
{
985 const char *funcname
;
986 int (*fn
)(struct smbconf_ctx
*ctx
, int argc
, const char **argv
);
987 const char *helptext
;
991 * This imitates net_run_function2 but calls the main functions
992 * through the wrapper net_conf_wrap_function().
994 static int net_conf_run_function(int argc
, const char **argv
,
996 struct conf_functable
*table
)
1001 for (i
=0; table
[i
].funcname
; i
++) {
1002 if (StrCaseCmp(argv
[0], table
[i
].funcname
) == 0)
1003 return net_conf_wrap_function(table
[i
].fn
,
1009 for (i
=0; table
[i
].funcname
; i
++) {
1010 d_printf("%s %-15s %s\n", whoami
, table
[i
].funcname
,
1018 * Entry-point for all the CONF functions.
1021 int net_conf(int argc
, const char **argv
)
1024 struct conf_functable func_table
[] = {
1025 {"list", net_conf_list
,
1026 "Dump the complete configuration in smb.conf like format."},
1027 {"import", net_conf_import
,
1028 "Import configuration from file in smb.conf format."},
1029 {"listshares", net_conf_listshares
,
1030 "List the share names."},
1031 {"drop", net_conf_drop
,
1032 "Delete the complete configuration."},
1033 {"showshare", net_conf_showshare
,
1034 "Show the definition of a share."},
1035 {"addshare", net_conf_addshare
,
1036 "Create a new share."},
1037 {"delshare", net_conf_delshare
,
1039 {"setparm", net_conf_setparm
,
1040 "Store a parameter."},
1041 {"getparm", net_conf_getparm
,
1042 "Retrieve the value of a parameter."},
1043 {"delparm", net_conf_delparm
,
1044 "Delete a parameter."},
1045 {"getincludes", net_conf_getincludes
,
1046 "Show the includes of a share definition."},
1047 {"setincludes", net_conf_setincludes
,
1048 "Set includes for a share."},
1049 {"delincludes", net_conf_delincludes
,
1050 "Delete includes from a share definition."},
1054 ret
= net_conf_run_function(argc
, argv
, "net conf", func_table
);