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");
113 /**********************************************************************
117 **********************************************************************/
120 * This functions process a service previously loaded with libsmbconf.
122 static WERROR import_process_service(struct smbconf_ctx *conf_ctx,
123 const char *servicename,
124 const uint32_t num_params,
125 const char **param_names,
126 const char **param_values)
129 WERROR werr = WERR_OK;
132 d_printf("[%s]\n", servicename);
134 werr = smbconf_delete_share(conf_ctx, servicename);
135 if (!W_ERROR_IS_OK(werr)) {
140 for (idx = 0; idx < num_params; idx ++) {
142 d_printf("\t%s = %s\n", param_names[idx],
145 werr = smbconf_set_parameter(conf_ctx,
149 if (!W_ERROR_IS_OK(werr)) {
160 /**********************************************************************
162 * the main conf functions
164 **********************************************************************/
166 static int net_conf_list(struct smbconf_ctx *conf_ctx,
167 int argc, const char **argv)
169 WERROR werr = WERR_OK;
174 uint32_t *num_params;
176 char ***param_values;
177 uint32_t share_count, param_count;
179 mem_ctx = talloc_stackframe();
182 net_conf_list_usage(argc, argv);
186 werr = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &share_names,
187 &num_params, ¶m_names, ¶m_values);
188 if (!W_ERROR_IS_OK(werr)) {
189 d_fprintf(stderr, "Error getting config: %s\n",
194 for (share_count = 0; share_count < num_shares; share_count++) {
195 d_printf("[%s]\n", share_names[share_count]);
196 for (param_count = 0; param_count < num_params[share_count];
199 d_printf("\t%s = %s\n",
200 param_names[share_count][param_count],
201 param_values[share_count][param_count]);
209 TALLOC_FREE(mem_ctx);
213 static int net_conf_import(struct smbconf_ctx *conf_ctx,
214 int argc, const char **argv)
217 const char *filename = NULL;
218 const char *servicename = NULL;
220 struct smbconf_ctx *txt_ctx;
223 mem_ctx = talloc_stackframe();
228 net_conf_import_usage(argc, argv);
231 servicename = argv[1];
237 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
240 werr = smbconf_init_txt_simple(mem_ctx, &txt_ctx, filename);
241 if (!W_ERROR_IS_OK(werr)) {
246 d_printf("\nTEST MODE - "
247 "would import the following configuration:\n\n");
250 if (servicename != NULL) {
251 char **param_names, **param_values;
254 werr = smbconf_get_share(txt_ctx, mem_ctx,
259 if (!W_ERROR_IS_OK(werr)) {
262 werr = import_process_service(conf_ctx,
265 (const char **)param_names,
266 (const char **)param_values);
267 if (!W_ERROR_IS_OK(werr)) {
271 char **share_names, ***param_names, ***param_values;
272 uint32_t num_shares, *num_params, sidx;
274 werr = smbconf_get_config(txt_ctx, mem_ctx,
275 &num_shares, &share_names,
276 &num_params, ¶m_names, ¶m_values);
277 if (!W_ERROR_IS_OK(werr)) {
280 for (sidx = 0; sidx < num_shares; sidx++) {
281 werr = import_process_service(conf_ctx,
284 (const char **)param_names[sidx],
285 (const char **)param_values[sidx]);
286 if (!W_ERROR_IS_OK(werr)) {
295 TALLOC_FREE(mem_ctx);
299 static int net_conf_listshares(struct smbconf_ctx *conf_ctx,
300 int argc, const char **argv)
302 WERROR werr = WERR_OK;
304 uint32_t count, num_shares = 0;
305 char **share_names = NULL;
308 mem_ctx = talloc_stackframe();
311 net_conf_listshares_usage(argc, argv);
315 werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
317 if (!W_ERROR_IS_OK(werr)) {
321 for (count = 0; count < num_shares; count++)
323 d_printf("%s\n", share_names[count]);
329 TALLOC_FREE(mem_ctx);
333 static int net_conf_drop(struct smbconf_ctx *conf_ctx,
334 int argc, const char **argv)
340 net_conf_drop_usage(argc, argv);
344 werr = smbconf_drop(conf_ctx);
345 if (!W_ERROR_IS_OK(werr)) {
346 d_fprintf(stderr, "Error deleting configuration: %s\n",
357 static int net_conf_showshare(struct smbconf_ctx *conf_ctx,
358 int argc, const char **argv)
361 WERROR werr = WERR_OK;
362 const char *sharename = NULL;
369 mem_ctx = talloc_stackframe();
372 net_conf_showshare_usage(argc, argv);
378 werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &num_params,
379 ¶m_names, ¶m_values);
380 if (!W_ERROR_IS_OK(werr)) {
381 d_printf("error getting share parameters: %s\n",
386 d_printf("[%s]\n", sharename);
388 for (count = 0; count < num_params; count++) {
389 d_printf("\t%s = %s\n", param_names[count],
390 param_values[count]);
396 TALLOC_FREE(mem_ctx);
401 * Add a share, with a couple of standard parameters, partly optional.
403 * This is a high level utility function of the net conf utility,
404 * not a direct frontend to the smbconf API.
406 static int net_conf_addshare(struct smbconf_ctx *conf_ctx,
407 int argc, const char **argv)
410 WERROR werr = WERR_OK;
411 char *sharename = NULL;
412 const char *path = NULL;
413 const char *comment = NULL;
414 const char *guest_ok = "no";
415 const char *writeable = "no";
416 SMB_STRUCT_STAT sbuf;
422 net_conf_addshare_usage(argc, argv);
427 if (!strnequal(argv[3], "guest_ok=", 9)) {
428 net_conf_addshare_usage(argc, argv);
431 switch (argv[3][9]) {
441 net_conf_addshare_usage(argc, argv);
445 if (!strnequal(argv[2], "writeable=", 10)) {
446 net_conf_addshare_usage(argc, argv);
449 switch (argv[2][10]) {
459 net_conf_addshare_usage(argc, argv);
464 sharename = strdup_lower(argv[0]);
472 /* validate share name */
474 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
477 d_fprintf(stderr, "ERROR: share name %s contains "
478 "invalid characters (any of %s)\n",
479 sharename, INVALID_SHARENAME_CHARS);
483 if (getpwnam(sharename)) {
484 d_fprintf(stderr, "ERROR: share name %s is already a valid "
485 "system user name.\n", sharename);
489 if (strequal(sharename, GLOBAL_NAME)) {
491 "ERROR: 'global' is not a valid share name.\n");
495 if (smbconf_share_exists(conf_ctx, sharename)) {
496 d_fprintf(stderr, "ERROR: share %s already exists.\n",
503 if (path[0] != '/') {
505 "Error: path '%s' is not an absolute path.\n",
510 if (sys_stat(path, &sbuf) != 0) {
512 "ERROR: cannot stat path '%s' to ensure "
513 "this is a directory.\n"
515 path, strerror(errno));
519 if (!S_ISDIR(sbuf.st_mode)) {
521 "ERROR: path '%s' is not a directory.\n",
530 werr = smbconf_create_share(conf_ctx, sharename);
531 if (!W_ERROR_IS_OK(werr)) {
532 d_fprintf(stderr, "Error creating share %s: %s\n",
533 sharename, dos_errstr(werr));
538 * fill the share with parameters
541 werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
542 if (!W_ERROR_IS_OK(werr)) {
543 d_fprintf(stderr, "Error setting parameter %s: %s\n",
544 "path", dos_errstr(werr));
548 if (comment != NULL) {
549 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
551 if (!W_ERROR_IS_OK(werr)) {
552 d_fprintf(stderr, "Error setting parameter %s: %s\n",
553 "comment", dos_errstr(werr));
558 werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
559 if (!W_ERROR_IS_OK(werr)) {
560 d_fprintf(stderr, "Error setting parameter %s: %s\n",
561 "'guest ok'", dos_errstr(werr));
565 werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
567 if (!W_ERROR_IS_OK(werr)) {
568 d_fprintf(stderr, "Error setting parameter %s: %s\n",
569 "writeable", dos_errstr(werr));
576 SAFE_FREE(sharename);
580 static int net_conf_delshare(struct smbconf_ctx *conf_ctx,
581 int argc, const char **argv)
584 const char *sharename = NULL;
585 WERROR werr = WERR_OK;
588 net_conf_delshare_usage(argc, argv);
593 werr = smbconf_delete_share(conf_ctx, sharename);
594 if (!W_ERROR_IS_OK(werr)) {
595 d_fprintf(stderr, "Error deleting share %s: %s\n",
596 sharename, dos_errstr(werr));
605 static int net_conf_setparm(struct smbconf_ctx *conf_ctx,
606 int argc, const char **argv)
609 WERROR werr = WERR_OK;
610 char *service = NULL;
612 const char *value_str = NULL;
615 net_conf_setparm_usage(argc, argv);
618 service = strdup_lower(argv[0]);
619 param = strdup_lower(argv[1]);
622 if (!smbconf_share_exists(conf_ctx, service)) {
623 werr = smbconf_create_share(conf_ctx, service);
624 if (!W_ERROR_IS_OK(werr)) {
625 d_fprintf(stderr, "Error creating share '%s': %s\n",
626 service, dos_errstr(werr));
631 werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
633 if (!W_ERROR_IS_OK(werr)) {
634 d_fprintf(stderr, "Error setting value '%s': %s\n",
635 param, dos_errstr(werr));
647 static int net_conf_getparm(struct smbconf_ctx *conf_ctx,
648 int argc, const char **argv)
651 WERROR werr = WERR_OK;
652 char *service = NULL;
657 mem_ctx = talloc_stackframe();
660 net_conf_getparm_usage(argc, argv);
663 service = strdup_lower(argv[0]);
664 param = strdup_lower(argv[1]);
666 werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
668 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
670 "Error: given service '%s' does not exist.\n",
673 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
675 "Error: given parameter '%s' is not set.\n",
678 } else if (!W_ERROR_IS_OK(werr)) {
679 d_fprintf(stderr, "Error getting value '%s': %s.\n",
680 param, dos_errstr(werr));
684 d_printf("%s\n", valstr);
690 TALLOC_FREE(mem_ctx);
694 static int net_conf_delparm(struct smbconf_ctx *conf_ctx,
695 int argc, const char **argv)
698 WERROR werr = WERR_OK;
699 char *service = NULL;
703 net_conf_delparm_usage(argc, argv);
706 service = strdup_lower(argv[0]);
707 param = strdup_lower(argv[1]);
709 werr = smbconf_delete_parameter(conf_ctx, service, param);
711 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
713 "Error: given service '%s' does not exist.\n",
716 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
718 "Error: given parameter '%s' is not set.\n",
721 } else if (!W_ERROR_IS_OK(werr)) {
722 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
723 param, dos_errstr(werr));
736 /**********************************************************************
738 * Wrapper and net_conf_run_function mechanism.
740 **********************************************************************/
743 * Wrapper function to call the main conf functions.
744 * The wrapper calls handles opening and closing of the
747 static int net_conf_wrap_function(int (*fn)(struct smbconf_ctx *,
749 int argc, const char **argv)
752 TALLOC_CTX *mem_ctx = talloc_stackframe();
753 struct smbconf_ctx *conf_ctx;
756 werr = smbconf_init_reg(mem_ctx, &conf_ctx, NULL);
758 if (!W_ERROR_IS_OK(werr)) {
762 ret = fn(conf_ctx, argc, argv);
764 smbconf_shutdown(conf_ctx);
770 * We need a functable struct of our own, because the
771 * functions are called through a wrapper that handles
772 * the opening and closing of the configuration, and so on.
774 struct conf_functable {
775 const char *funcname;
776 int (*fn)(struct smbconf_ctx *ctx, int argc, const char **argv);
777 const char *helptext;
781 * This imitates net_run_function2 but calls the main functions
782 * through the wrapper net_conf_wrap_function().
784 static int net_conf_run_function(int argc, const char **argv,
786 struct conf_functable *table)
791 for (i=0; table[i].funcname; i++) {
792 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
793 return net_conf_wrap_function(table[i].fn,
799 for (i=0; table[i].funcname; i++) {
800 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
808 * Entry-point for all the CONF functions.
811 int net_conf(int argc, const char **argv)
814 struct conf_functable func_table[] = {
815 {"list", net_conf_list,
816 "Dump the complete configuration in smb.conf like format."},
817 {"import", net_conf_import,
818 "Import configuration from file in smb.conf format."},
819 {"listshares", net_conf_listshares,
820 "List the share names."},
821 {"drop", net_conf_drop,
822 "Delete the complete configuration."},
823 {"showshare", net_conf_showshare,
824 "Show the definition of a share."},
825 {"addshare", net_conf_addshare,
826 "Create a new share."},
827 {"delshare", net_conf_delshare,
829 {"setparm", net_conf_setparm,
830 "Store a parameter."},
831 {"getparm", net_conf_getparm,
832 "Retrieve the value of a parameter."},
833 {"delparm", net_conf_delparm,
834 "Delete a parameter."},
838 ret = net_conf_run_function(argc, argv, "net conf", func_table);