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 (strequal(sharename
, GLOBAL_NAME
)) {
554 "ERROR: 'global' is not a valid share name.\n");
558 if (smbconf_share_exists(conf_ctx
, sharename
)) {
559 d_fprintf(stderr
, "ERROR: share %s already exists.\n",
566 if (path
[0] != '/') {
568 "Error: path '%s' is not an absolute path.\n",
573 if (sys_stat(path
, &sbuf
) != 0) {
575 "ERROR: cannot stat path '%s' to ensure "
576 "this is a directory.\n"
578 path
, strerror(errno
));
582 if (!S_ISDIR(sbuf
.st_mode
)) {
584 "ERROR: path '%s' is not a directory.\n",
593 werr
= smbconf_create_share(conf_ctx
, sharename
);
594 if (!W_ERROR_IS_OK(werr
)) {
595 d_fprintf(stderr
, "Error creating share %s: %s\n",
596 sharename
, dos_errstr(werr
));
601 * fill the share with parameters
604 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "path", path
);
605 if (!W_ERROR_IS_OK(werr
)) {
606 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
607 "path", dos_errstr(werr
));
611 if (comment
!= NULL
) {
612 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "comment",
614 if (!W_ERROR_IS_OK(werr
)) {
615 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
616 "comment", dos_errstr(werr
));
621 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "guest ok", guest_ok
);
622 if (!W_ERROR_IS_OK(werr
)) {
623 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
624 "'guest ok'", dos_errstr(werr
));
628 werr
= smbconf_set_parameter(conf_ctx
, sharename
, "writeable",
630 if (!W_ERROR_IS_OK(werr
)) {
631 d_fprintf(stderr
, "Error setting parameter %s: %s\n",
632 "writeable", dos_errstr(werr
));
639 TALLOC_FREE(mem_ctx
);
643 static int net_conf_delshare(struct smbconf_ctx
*conf_ctx
,
644 int argc
, const char **argv
)
647 const char *sharename
= NULL
;
648 WERROR werr
= WERR_OK
;
649 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
652 net_conf_delshare_usage(argc
, argv
);
655 sharename
= talloc_strdup_lower(mem_ctx
, argv
[0]);
656 if (sharename
== NULL
) {
657 d_printf("error: out of memory!\n");
661 werr
= smbconf_delete_share(conf_ctx
, sharename
);
662 if (!W_ERROR_IS_OK(werr
)) {
663 d_fprintf(stderr
, "Error deleting share %s: %s\n",
664 sharename
, dos_errstr(werr
));
670 TALLOC_FREE(mem_ctx
);
674 static int net_conf_setparm(struct smbconf_ctx
*conf_ctx
,
675 int argc
, const char **argv
)
678 WERROR werr
= WERR_OK
;
679 char *service
= NULL
;
681 const char *value_str
= NULL
;
682 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
685 net_conf_setparm_usage(argc
, argv
);
688 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
689 if (service
== NULL
) {
690 d_printf("error: out of memory!\n");
693 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
695 d_printf("error: out of memory!\n");
700 if (!smbconf_share_exists(conf_ctx
, service
)) {
701 werr
= smbconf_create_share(conf_ctx
, service
);
702 if (!W_ERROR_IS_OK(werr
)) {
703 d_fprintf(stderr
, "Error creating share '%s': %s\n",
704 service
, dos_errstr(werr
));
709 werr
= smbconf_set_parameter(conf_ctx
, service
, param
, value_str
);
711 if (!W_ERROR_IS_OK(werr
)) {
712 d_fprintf(stderr
, "Error setting value '%s': %s\n",
713 param
, dos_errstr(werr
));
720 TALLOC_FREE(mem_ctx
);
724 static int net_conf_getparm(struct smbconf_ctx
*conf_ctx
,
725 int argc
, const char **argv
)
728 WERROR werr
= WERR_OK
;
729 char *service
= NULL
;
734 mem_ctx
= talloc_stackframe();
737 net_conf_getparm_usage(argc
, argv
);
740 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
741 if (service
== NULL
) {
742 d_printf("error: out of memory!\n");
745 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
747 d_printf("error: out of memory!\n");
751 werr
= smbconf_get_parameter(conf_ctx
, mem_ctx
, service
, param
, &valstr
);
753 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
755 "Error: given service '%s' does not exist.\n",
758 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
760 "Error: given parameter '%s' is not set.\n",
763 } else if (!W_ERROR_IS_OK(werr
)) {
764 d_fprintf(stderr
, "Error getting value '%s': %s.\n",
765 param
, dos_errstr(werr
));
769 d_printf("%s\n", valstr
);
773 TALLOC_FREE(mem_ctx
);
777 static int net_conf_delparm(struct smbconf_ctx
*conf_ctx
,
778 int argc
, const char **argv
)
781 WERROR werr
= WERR_OK
;
782 char *service
= NULL
;
784 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
787 net_conf_delparm_usage(argc
, argv
);
790 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
791 if (service
== NULL
) {
792 d_printf("error: out of memory!\n");
795 param
= talloc_strdup_lower(mem_ctx
, argv
[1]);
797 d_printf("error: out of memory!\n");
801 werr
= smbconf_delete_parameter(conf_ctx
, service
, param
);
803 if (W_ERROR_EQUAL(werr
, WERR_NO_SUCH_SERVICE
)) {
805 "Error: given service '%s' does not exist.\n",
808 } else if (W_ERROR_EQUAL(werr
, WERR_INVALID_PARAM
)) {
810 "Error: given parameter '%s' is not set.\n",
813 } else if (!W_ERROR_IS_OK(werr
)) {
814 d_fprintf(stderr
, "Error deleting value '%s': %s.\n",
815 param
, dos_errstr(werr
));
822 TALLOC_FREE(mem_ctx
);
826 static int net_conf_getincludes(struct smbconf_ctx
*conf_ctx
,
827 int argc
, const char **argv
)
830 uint32_t num_includes
;
833 char **includes
= NULL
;
835 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
838 net_conf_getincludes_usage(argc
, argv
);
842 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
843 if (service
== NULL
) {
844 d_printf("error: out of memory!\n");
848 werr
= smbconf_get_includes(conf_ctx
, mem_ctx
, service
,
849 &num_includes
, &includes
);
850 if (!W_ERROR_IS_OK(werr
)) {
851 d_printf("error getting includes: %s\n", dos_errstr(werr
));
855 for (count
= 0; count
< num_includes
; count
++) {
856 d_printf("include = %s\n", includes
[count
]);
862 TALLOC_FREE(mem_ctx
);
866 static int net_conf_setincludes(struct smbconf_ctx
*conf_ctx
,
867 int argc
, const char **argv
)
871 uint32_t num_includes
;
872 const char **includes
;
874 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
877 net_conf_setincludes_usage(argc
, argv
);
881 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
882 if (service
== NULL
) {
883 d_printf("error: out of memory!\n");
887 num_includes
= argc
- 1;
888 if (num_includes
== 0) {
894 werr
= smbconf_set_includes(conf_ctx
, service
, num_includes
, includes
);
895 if (!W_ERROR_IS_OK(werr
)) {
896 d_printf("error setting includes: %s\n", dos_errstr(werr
));
903 TALLOC_FREE(mem_ctx
);
907 static int net_conf_delincludes(struct smbconf_ctx
*conf_ctx
,
908 int argc
, const char **argv
)
913 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
916 net_conf_delincludes_usage(argc
, argv
);
920 service
= talloc_strdup_lower(mem_ctx
, argv
[0]);
921 if (service
== NULL
) {
922 d_printf("error: out of memory!\n");
926 werr
= smbconf_delete_includes(conf_ctx
, service
);
927 if (!W_ERROR_IS_OK(werr
)) {
928 d_printf("error deleting includes: %s\n", dos_errstr(werr
));
935 TALLOC_FREE(mem_ctx
);
940 /**********************************************************************
942 * Wrapper and net_conf_run_function mechanism.
944 **********************************************************************/
947 * Wrapper function to call the main conf functions.
948 * The wrapper calls handles opening and closing of the
951 static int net_conf_wrap_function(int (*fn
)(struct smbconf_ctx
*,
953 int argc
, const char **argv
)
956 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
957 struct smbconf_ctx
*conf_ctx
;
960 werr
= smbconf_init(mem_ctx
, &conf_ctx
, "registry:");
962 if (!W_ERROR_IS_OK(werr
)) {
966 ret
= fn(conf_ctx
, argc
, argv
);
968 smbconf_shutdown(conf_ctx
);
974 * We need a functable struct of our own, because the
975 * functions are called through a wrapper that handles
976 * the opening and closing of the configuration, and so on.
978 struct conf_functable
{
979 const char *funcname
;
980 int (*fn
)(struct smbconf_ctx
*ctx
, int argc
, const char **argv
);
981 const char *helptext
;
985 * This imitates net_run_function2 but calls the main functions
986 * through the wrapper net_conf_wrap_function().
988 static int net_conf_run_function(int argc
, const char **argv
,
990 struct conf_functable
*table
)
995 for (i
=0; table
[i
].funcname
; i
++) {
996 if (StrCaseCmp(argv
[0], table
[i
].funcname
) == 0)
997 return net_conf_wrap_function(table
[i
].fn
,
1003 for (i
=0; table
[i
].funcname
; i
++) {
1004 d_printf("%s %-15s %s\n", whoami
, table
[i
].funcname
,
1012 * Entry-point for all the CONF functions.
1015 int net_conf(int argc
, const char **argv
)
1018 struct conf_functable func_table
[] = {
1019 {"list", net_conf_list
,
1020 "Dump the complete configuration in smb.conf like format."},
1021 {"import", net_conf_import
,
1022 "Import configuration from file in smb.conf format."},
1023 {"listshares", net_conf_listshares
,
1024 "List the share names."},
1025 {"drop", net_conf_drop
,
1026 "Delete the complete configuration."},
1027 {"showshare", net_conf_showshare
,
1028 "Show the definition of a share."},
1029 {"addshare", net_conf_addshare
,
1030 "Create a new share."},
1031 {"delshare", net_conf_delshare
,
1033 {"setparm", net_conf_setparm
,
1034 "Store a parameter."},
1035 {"getparm", net_conf_getparm
,
1036 "Retrieve the value of a parameter."},
1037 {"delparm", net_conf_delparm
,
1038 "Delete a parameter."},
1039 {"getincludes", net_conf_getincludes
,
1040 "Show the includes of a share definition."},
1041 {"setincludes", net_conf_setincludes
,
1042 "Set includes for a share."},
1043 {"delincludes", net_conf_delincludes
,
1044 "Delete includes from a share definition."},
1048 ret
= net_conf_run_function(argc
, argv
, "net conf", func_table
);