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 libnet_conf interface (source/libnet/libnet_conf.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"
32 #include "libnet/libnet.h"
38 static int net_conf_list_usage(int argc, const char **argv)
40 d_printf("USAGE: net conf list\n");
44 static int net_conf_import_usage(int argc, const char**argv)
46 d_printf("USAGE: net conf import [--test|-T] <filename> "
48 "\t[--test|-T] testmode - do not act, just print "
49 "what would be done\n"
50 "\t<servicename> only import service <servicename>, "
55 static int net_conf_listshares_usage(int argc, const char **argv)
57 d_printf("USAGE: net conf listshares\n");
61 static int net_conf_drop_usage(int argc, const char **argv)
63 d_printf("USAGE: net conf drop\n");
67 static int net_conf_showshare_usage(int argc, const char **argv)
69 d_printf("USAGE: net conf showshare <sharename>\n");
73 static int net_conf_addshare_usage(int argc, const char **argv)
75 d_printf("USAGE: net conf addshare <sharename> <path> "
76 "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
77 "\t<sharename> the new share name.\n"
78 "\t<path> the path on the filesystem to export.\n"
79 "\twriteable={y|N} set \"writeable to \"yes\" or "
80 "\"no\" (default) on this share.\n"
81 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
82 "\"no\" (default) on this share.\n"
83 "\t<comment> optional comment for the new share.\n");
87 static int net_conf_delshare_usage(int argc, const char **argv)
89 d_printf("USAGE: net conf delshare <sharename>\n");
93 static int net_conf_setparm_usage(int argc, const char **argv)
95 d_printf("USAGE: net conf setparm <section> <param> <value>\n");
99 static int net_conf_getparm_usage(int argc, const char **argv)
101 d_printf("USAGE: net conf getparm <section> <param>\n");
105 static int net_conf_delparm_usage(int argc, const char **argv)
107 d_printf("USAGE: net conf delparm <section> <param>\n");
116 static char *parm_valstr(TALLOC_CTX *ctx, struct parm_struct *parm,
117 struct share_params *share)
121 void *ptr = parm->ptr;
123 if (parm->p_class == P_LOCAL && share->service >= 0) {
124 ptr = lp_local_ptr(share->service, ptr);
127 switch (parm->type) {
129 valstr = talloc_asprintf(ctx, "%c", *(char *)ptr);
133 valstr = talloc_asprintf(ctx, "%s", *(char **)ptr);
136 valstr = talloc_asprintf(ctx, "%s", BOOLSTR(*(bool *)ptr));
139 valstr = talloc_asprintf(ctx, "%s", BOOLSTR(!*(bool *)ptr));
142 for (i = 0; parm->enum_list[i].name; i++) {
143 if (*(int *)ptr == parm->enum_list[i].value)
145 valstr = talloc_asprintf(ctx, "%s",
146 parm->enum_list[i].name);
152 char *o = octal_string(*(int *)ptr);
153 valstr = talloc_move(ctx, &o);
157 valstr = talloc_strdup(ctx, "");
158 if ((char ***)ptr && *(char ***)ptr) {
159 char **list = *(char ***)ptr;
160 for (; *list; list++) {
161 /* surround strings with whitespace
162 * in double quotes */
163 if (strchr_m(*list, ' '))
165 valstr = talloc_asprintf_append(
168 ((*(list+1))?", ":""));
170 valstr = talloc_asprintf_append(
171 valstr, "%s%s", *list,
172 ((*(list+1))?", ":""));
178 valstr = talloc_asprintf(ctx, "%d", *(int *)ptr);
183 valstr = talloc_asprintf(ctx, "<type unimplemented>\n");
190 static int import_process_service(TALLOC_CTX *ctx,
191 struct libnet_conf_ctx *conf_ctx,
192 struct share_params *share)
195 struct parm_struct *parm;
197 const char *servicename;
200 TALLOC_CTX *tmp_ctx = NULL;
202 tmp_ctx = talloc_new(ctx);
203 if (tmp_ctx == NULL) {
208 servicename = (share->service == GLOBAL_SECTION_SNUM)?
209 GLOBAL_NAME : lp_servicename(share->service);
212 d_printf("[%s]\n", servicename);
214 if (libnet_conf_share_exists(conf_ctx, servicename)) {
215 werr = libnet_conf_delete_share(conf_ctx, servicename);
216 if (!W_ERROR_IS_OK(werr)) {
222 while ((parm = lp_next_parameter(share->service, &pnum, 0)))
224 if ((share->service < 0) && (parm->p_class == P_LOCAL)
225 && !(parm->flags & FLAG_GLOBAL))
230 valstr = parm_valstr(tmp_ctx, parm, share);
232 if (parm->type != P_SEP) {
234 d_printf("\t%s = %s\n", parm->label, valstr);
236 werr = libnet_conf_set_parameter(conf_ctx,
240 if (!W_ERROR_IS_OK(werr)) {
242 "Error setting parameter '%s'"
243 ": %s\n", parm->label,
258 TALLOC_FREE(tmp_ctx);
262 /* return true iff there are nondefault globals */
263 static bool globals_exist(void)
266 struct parm_struct *parm;
268 while ((parm = lp_next_parameter(GLOBAL_SECTION_SNUM, &i, 0)) != NULL) {
269 if (parm->type != P_SEP) {
280 static int net_conf_list(struct libnet_conf_ctx *conf_ctx,
281 int argc, const char **argv)
283 WERROR werr = WERR_OK;
288 uint32_t *num_params;
290 char ***param_values;
291 uint32_t share_count, param_count;
293 ctx = talloc_init("list");
296 net_conf_list_usage(argc, argv);
300 werr = libnet_conf_get_config(ctx, conf_ctx, &num_shares, &share_names,
301 &num_params, ¶m_names, ¶m_values);
302 if (!W_ERROR_IS_OK(werr)) {
303 d_fprintf(stderr, "Error getting config: %s\n",
308 for (share_count = 0; share_count < num_shares; share_count++) {
309 d_printf("[%s]\n", share_names[share_count]);
310 for (param_count = 0; param_count < num_params[share_count];
313 d_printf("\t%s = %s\n",
314 param_names[share_count][param_count],
315 param_values[share_count][param_count]);
327 static int net_conf_import(struct libnet_conf_ctx *conf_ctx,
328 int argc, const char **argv)
331 const char *filename = NULL;
332 const char *servicename = NULL;
333 bool service_found = false;
335 struct share_iterator *shares;
336 struct share_params *share;
337 struct share_params global_share = { GLOBAL_SECTION_SNUM };
339 ctx = talloc_init("net_conf_import");
344 net_conf_import_usage(argc, argv);
347 servicename = argv[1];
353 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
356 if (!lp_load(filename,
357 false, /* global_only */
358 true, /* save_defaults */
360 true)) /* initialize_globals */
362 d_fprintf(stderr, "Error parsing configuration file.\n");
367 d_printf("\nTEST MODE - "
368 "would import the following configuration:\n\n");
371 if (((servicename == NULL) && globals_exist()) ||
372 strequal(servicename, GLOBAL_NAME))
374 service_found = true;
375 if (import_process_service(ctx, conf_ctx, &global_share) != 0) {
380 if (service_found && (servicename != NULL)) {
385 if (!(shares = share_list_all(ctx))) {
386 d_fprintf(stderr, "Could not list shares...\n");
389 while ((share = next_share(shares)) != NULL) {
390 if ((servicename == NULL)
391 || strequal(servicename, lp_servicename(share->service)))
393 service_found = true;
394 if (import_process_service(ctx, conf_ctx, share)!= 0) {
400 if ((servicename != NULL) && !service_found) {
401 d_printf("Share %s not found in file %s\n",
402 servicename, filename);
414 static int net_conf_listshares(struct libnet_conf_ctx *conf_ctx,
415 int argc, const char **argv)
417 WERROR werr = WERR_OK;
419 uint32_t count, num_shares = 0;
420 char **share_names = NULL;
423 ctx = talloc_init("listshares");
426 net_conf_listshares_usage(argc, argv);
430 werr = libnet_conf_get_share_names(ctx, conf_ctx, &num_shares,
432 if (!W_ERROR_IS_OK(werr)) {
436 for (count = 0; count < num_shares; count++)
438 d_printf("%s\n", share_names[count]);
448 static int net_conf_drop(struct libnet_conf_ctx *conf_ctx,
449 int argc, const char **argv)
455 net_conf_drop_usage(argc, argv);
459 werr = libnet_conf_drop(conf_ctx);
460 if (!W_ERROR_IS_OK(werr)) {
461 d_fprintf(stderr, "Error deleting configuration: %s\n",
472 static int net_conf_showshare(struct libnet_conf_ctx *conf_ctx,
473 int argc, const char **argv)
476 WERROR werr = WERR_OK;
477 const char *sharename = NULL;
484 ctx = talloc_init("showshare");
487 net_conf_showshare_usage(argc, argv);
493 werr = libnet_conf_get_share(ctx, conf_ctx, sharename, &num_params,
494 ¶m_names, ¶m_values);
495 if (!W_ERROR_IS_OK(werr)) {
496 d_printf("error getting share parameters: %s\n",
501 d_printf("[%s]\n", sharename);
503 for (count = 0; count < num_params; count++) {
504 d_printf("\t%s = %s\n", param_names[count],
505 param_values[count]);
516 * Add a share, with a couple of standard parameters, partly optional.
518 * This is a high level utility function of the net conf utility,
519 * not a direct frontend to the libnet_conf API.
521 static int net_conf_addshare(struct libnet_conf_ctx *conf_ctx,
522 int argc, const char **argv)
525 WERROR werr = WERR_OK;
526 char *sharename = NULL;
527 const char *path = NULL;
528 const char *comment = NULL;
529 const char *guest_ok = "no";
530 const char *writeable = "no";
531 SMB_STRUCT_STAT sbuf;
537 net_conf_addshare_usage(argc, argv);
542 if (!strnequal(argv[3], "guest_ok=", 9)) {
543 net_conf_addshare_usage(argc, argv);
546 switch (argv[3][9]) {
556 net_conf_addshare_usage(argc, argv);
560 if (!strnequal(argv[2], "writeable=", 10)) {
561 net_conf_addshare_usage(argc, argv);
564 switch (argv[2][10]) {
574 net_conf_addshare_usage(argc, argv);
579 sharename = strdup_lower(argv[0]);
587 /* validate share name */
589 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
592 d_fprintf(stderr, "ERROR: share name %s contains "
593 "invalid characters (any of %s)\n",
594 sharename, INVALID_SHARENAME_CHARS);
598 if (getpwnam(sharename)) {
599 d_fprintf(stderr, "ERROR: share name %s is already a valid "
600 "system user name.\n", sharename);
604 if (strequal(sharename, GLOBAL_NAME)) {
606 "ERROR: 'global' is not a valid share name.\n");
610 if (libnet_conf_share_exists(conf_ctx, sharename)) {
611 d_fprintf(stderr, "ERROR: share %s already exists.\n",
618 if (path[0] != '/') {
620 "Error: path '%s' is not an absolute path.\n",
625 if (sys_stat(path, &sbuf) != 0) {
627 "ERROR: cannot stat path '%s' to ensure "
628 "this is a directory.\n"
630 path, strerror(errno));
634 if (!S_ISDIR(sbuf.st_mode)) {
636 "ERROR: path '%s' is not a directory.\n",
645 werr = libnet_conf_create_share(conf_ctx, sharename);
646 if (!W_ERROR_IS_OK(werr)) {
647 d_fprintf(stderr, "Error creating share %s: %s\n",
648 sharename, dos_errstr(werr));
653 * fill the share with parameters
656 werr = libnet_conf_set_parameter(conf_ctx, sharename, "path", path);
657 if (!W_ERROR_IS_OK(werr)) {
658 d_fprintf(stderr, "Error setting parameter %s: %s\n",
659 "path", dos_errstr(werr));
663 if (comment != NULL) {
664 werr = libnet_conf_set_parameter(conf_ctx, sharename, "comment",
666 if (!W_ERROR_IS_OK(werr)) {
667 d_fprintf(stderr, "Error setting parameter %s: %s\n",
668 "comment", dos_errstr(werr));
673 werr = libnet_conf_set_parameter(conf_ctx, sharename, "guest ok",
675 if (!W_ERROR_IS_OK(werr)) {
676 d_fprintf(stderr, "Error setting parameter %s: %s\n",
677 "'guest ok'", dos_errstr(werr));
681 werr = libnet_conf_set_parameter(conf_ctx, sharename, "writeable",
683 if (!W_ERROR_IS_OK(werr)) {
684 d_fprintf(stderr, "Error setting parameter %s: %s\n",
685 "writeable", dos_errstr(werr));
692 SAFE_FREE(sharename);
696 static int net_conf_delshare(struct libnet_conf_ctx *conf_ctx,
697 int argc, const char **argv)
700 const char *sharename = NULL;
701 WERROR werr = WERR_OK;
704 net_conf_delshare_usage(argc, argv);
709 werr = libnet_conf_delete_share(conf_ctx, sharename);
710 if (!W_ERROR_IS_OK(werr)) {
711 d_fprintf(stderr, "Error deleting share %s: %s\n",
712 sharename, dos_errstr(werr));
721 static int net_conf_setparm(struct libnet_conf_ctx *conf_ctx,
722 int argc, const char **argv)
725 WERROR werr = WERR_OK;
726 char *service = NULL;
728 const char *value_str = NULL;
731 net_conf_setparm_usage(argc, argv);
734 service = strdup_lower(argv[0]);
735 param = strdup_lower(argv[1]);
738 if (!libnet_conf_share_exists(conf_ctx, service)) {
739 werr = libnet_conf_create_share(conf_ctx, service);
740 if (!W_ERROR_IS_OK(werr)) {
741 d_fprintf(stderr, "Error creating share '%s': %s\n",
742 service, dos_errstr(werr));
747 werr = libnet_conf_set_parameter(conf_ctx, service, param, value_str);
749 if (!W_ERROR_IS_OK(werr)) {
750 d_fprintf(stderr, "Error setting value '%s': %s\n",
751 param, dos_errstr(werr));
763 static int net_conf_getparm(struct libnet_conf_ctx *conf_ctx,
764 int argc, const char **argv)
767 WERROR werr = WERR_OK;
768 char *service = NULL;
773 ctx = talloc_init("getparm");
776 net_conf_getparm_usage(argc, argv);
779 service = strdup_lower(argv[0]);
780 param = strdup_lower(argv[1]);
782 werr = libnet_conf_get_parameter(ctx, conf_ctx, service, param, &valstr);
784 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
786 "Error: given service '%s' does not exist.\n",
789 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
791 "Error: given parameter '%s' is not set.\n",
794 } else if (!W_ERROR_IS_OK(werr)) {
795 d_fprintf(stderr, "Error getting value '%s': %s.\n",
796 param, dos_errstr(werr));
800 d_printf("%s\n", valstr);
810 static int net_conf_delparm(struct libnet_conf_ctx *conf_ctx,
811 int argc, const char **argv)
814 WERROR werr = WERR_OK;
815 char *service = NULL;
819 net_conf_delparm_usage(argc, argv);
822 service = strdup_lower(argv[0]);
823 param = strdup_lower(argv[1]);
825 werr = libnet_conf_delete_parameter(conf_ctx, service, param);
827 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
829 "Error: given service '%s' does not exist.\n",
832 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
834 "Error: given parameter '%s' is not set.\n",
837 } else if (!W_ERROR_IS_OK(werr)) {
838 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
839 param, dos_errstr(werr));
851 static int net_conf_wrap_function(int (*fn)(struct libnet_conf_ctx *,
853 int argc, const char **argv)
856 TALLOC_CTX *mem_ctx = talloc_stackframe();
857 struct libnet_conf_ctx *conf_ctx;
860 werr = libnet_conf_open(mem_ctx, &conf_ctx);
862 if (!W_ERROR_IS_OK(werr)) {
866 ret = fn(conf_ctx, argc, argv);
868 libnet_conf_close(conf_ctx);
874 * We need a functable struct of our own, because the
875 * functions are called through a wrapper that handles
876 * the opening and closing of the configuration, and so on.
878 struct conf_functable {
879 const char *funcname;
880 int (*fn)(struct libnet_conf_ctx *ctx, int argc, const char **argv);
881 const char *helptext;
884 static int net_conf_run_function(int argc, const char **argv,
886 struct conf_functable *table)
891 for (i=0; table[i].funcname; i++) {
892 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
893 return net_conf_wrap_function(table[i].fn,
899 for (i=0; table[i].funcname; i++) {
900 d_printf("%s %-15s %s\n", whoami, table[i].funcname,
908 * Entry-point for all the CONF functions.
911 int net_conf(int argc, const char **argv)
914 struct conf_functable func_table[] = {
915 {"list", net_conf_list,
916 "Dump the complete configuration in smb.conf like format."},
917 {"import", net_conf_import,
918 "Import configuration from file in smb.conf format."},
919 {"listshares", net_conf_listshares,
920 "List the share names."},
921 {"drop", net_conf_drop,
922 "Delete the complete configuration."},
923 {"showshare", net_conf_showshare,
924 "Show the definition of a share."},
925 {"addshare", net_conf_addshare,
926 "Create a new share."},
927 {"delshare", net_conf_delshare,
929 {"setparm", net_conf_setparm,
930 "Store a parameter."},
931 {"getparm", net_conf_getparm,
932 "Retrieve the value of a parameter."},
933 {"delparm", net_conf_delparm,
934 "Delete a parameter."},
938 ret = net_conf_run_function(argc, argv, "net conf", func_table);