Rename libnet_smbconf_getshare() to libnet_conf_get_share().
[Samba/nascimento.git] / source / utils / net_conf.c
blob7859e0e615e3bcc7f039cbaa8e1965c154a98d65
1 /*
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 the configuration stored inside the
23 * samba registry. In the future there might be support for other
24 * configuration backends as well.
27 #include "includes.h"
28 #include "utils/net.h"
29 #include "libnet/libnet.h"
32 * usage functions
35 static int net_conf_list_usage(int argc, const char **argv)
37 d_printf("USAGE: net conf list\n");
38 return -1;
41 static int net_conf_import_usage(int argc, const char**argv)
43 d_printf("USAGE: net conf import [--test|-T] <filename> "
44 "[<servicename>]\n"
45 "\t[--test|-T] testmode - do not act, just print "
46 "what would be done\n"
47 "\t<servicename> only import service <servicename>, "
48 "ignore the rest\n");
49 return -1;
52 static int net_conf_listshares_usage(int argc, const char **argv)
54 d_printf("USAGE: net conf listshares\n");
55 return -1;
58 static int net_conf_drop_usage(int argc, const char **argv)
60 d_printf("USAGE: net conf drop\n");
61 return -1;
64 static int net_conf_showshare_usage(int argc, const char **argv)
66 d_printf("USAGE: net conf showshare <sharename>\n");
67 return -1;
70 static int net_conf_addshare_usage(int argc, const char **argv)
72 d_printf("USAGE: net conf addshare <sharename> <path> "
73 "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
74 "\t<sharename> the new share name.\n"
75 "\t<path> the path on the filesystem to export.\n"
76 "\twriteable={y|N} set \"writeable to \"yes\" or "
77 "\"no\" (default) on this share.\n"
78 "\tguest_ok={y|N} set \"guest ok\" to \"yes\" or "
79 "\"no\" (default) on this share.\n"
80 "\t<comment> optional comment for the new share.\n");
81 return -1;
84 static int net_conf_delshare_usage(int argc, const char **argv)
86 d_printf("USAGE: net conf delshare <sharename>\n");
87 return -1;
90 static int net_conf_setparm_usage(int argc, const char **argv)
92 d_printf("USAGE: net conf setparm <section> <param> <value>\n");
93 return -1;
96 static int net_conf_getparm_usage(int argc, const char **argv)
98 d_printf("USAGE: net conf getparm <section> <param>\n");
99 return -1;
102 static int net_conf_delparm_usage(int argc, const char **argv)
104 d_printf("USAGE: net conf delparm <section> <param>\n");
105 return -1;
110 * Helper functions
113 static char *parm_valstr(TALLOC_CTX *ctx, struct parm_struct *parm,
114 struct share_params *share)
116 char *valstr = NULL;
117 int i = 0;
118 void *ptr = parm->ptr;
120 if (parm->p_class == P_LOCAL && share->service >= 0) {
121 ptr = lp_local_ptr(share->service, ptr);
124 switch (parm->type) {
125 case P_CHAR:
126 valstr = talloc_asprintf(ctx, "%c", *(char *)ptr);
127 break;
128 case P_STRING:
129 case P_USTRING:
130 valstr = talloc_asprintf(ctx, "%s", *(char **)ptr);
131 break;
132 case P_BOOL:
133 valstr = talloc_asprintf(ctx, "%s", BOOLSTR(*(bool *)ptr));
134 break;
135 case P_BOOLREV:
136 valstr = talloc_asprintf(ctx, "%s", BOOLSTR(!*(bool *)ptr));
137 break;
138 case P_ENUM:
139 for (i = 0; parm->enum_list[i].name; i++) {
140 if (*(int *)ptr == parm->enum_list[i].value)
142 valstr = talloc_asprintf(ctx, "%s",
143 parm->enum_list[i].name);
144 break;
147 break;
148 case P_OCTAL: {
149 char *o = octal_string(*(int *)ptr);
150 valstr = talloc_move(ctx, &o);
151 break;
153 case P_LIST:
154 valstr = talloc_strdup(ctx, "");
155 if ((char ***)ptr && *(char ***)ptr) {
156 char **list = *(char ***)ptr;
157 for (; *list; list++) {
158 /* surround strings with whitespace
159 * in double quotes */
160 if (strchr_m(*list, ' '))
162 valstr = talloc_asprintf_append(
163 valstr, "\"%s\"%s",
164 *list,
165 ((*(list+1))?", ":""));
166 } else {
167 valstr = talloc_asprintf_append(
168 valstr, "%s%s", *list,
169 ((*(list+1))?", ":""));
173 break;
174 case P_INTEGER:
175 valstr = talloc_asprintf(ctx, "%d", *(int *)ptr);
176 break;
177 case P_SEP:
178 break;
179 default:
180 valstr = talloc_asprintf(ctx, "<type unimplemented>\n");
181 break;
184 return valstr;
187 static int import_process_service(TALLOC_CTX *ctx,
188 struct share_params *share)
190 int ret = -1;
191 struct parm_struct *parm;
192 int pnum = 0;
193 const char *servicename;
194 WERROR werr;
195 char *valstr = NULL;
196 TALLOC_CTX *tmp_ctx = NULL;
198 tmp_ctx = talloc_new(ctx);
199 if (tmp_ctx == NULL) {
200 werr = WERR_NOMEM;
201 goto done;
204 servicename = (share->service == GLOBAL_SECTION_SNUM)?
205 GLOBAL_NAME : lp_servicename(share->service);
207 if (opt_testmode) {
208 d_printf("[%s]\n", servicename);
209 } else {
210 if (libnet_conf_share_exists(servicename)) {
211 werr = libnet_smbconf_delshare(servicename);
212 if (!W_ERROR_IS_OK(werr)) {
213 goto done;
218 while ((parm = lp_next_parameter(share->service, &pnum, 0)))
220 if ((share->service < 0) && (parm->p_class == P_LOCAL)
221 && !(parm->flags & FLAG_GLOBAL))
223 continue;
226 valstr = parm_valstr(tmp_ctx, parm, share);
228 if (parm->type != P_SEP) {
229 if (opt_testmode) {
230 d_printf("\t%s = %s\n", parm->label, valstr);
231 } else {
232 werr = libnet_smbconf_setparm(servicename,
233 parm->label,
234 valstr);
235 if (!W_ERROR_IS_OK(werr)) {
236 d_fprintf(stderr,
237 "Error setting parameter '%s'"
238 ": %s\n", parm->label,
239 dos_errstr(werr));
240 goto done;
246 if (opt_testmode) {
247 d_printf("\n");
250 ret = 0;
252 done:
253 TALLOC_FREE(tmp_ctx);
254 return ret;
257 /* return true iff there are nondefault globals */
258 static bool globals_exist(void)
260 int i = 0;
261 struct parm_struct *parm;
263 while ((parm = lp_next_parameter(GLOBAL_SECTION_SNUM, &i, 0)) != NULL) {
264 if (parm->type != P_SEP) {
265 return true;
268 return false;
272 * the conf functions
275 static int net_conf_list(int argc, const char **argv)
277 WERROR werr = WERR_OK;
278 int ret = -1;
279 TALLOC_CTX *ctx;
280 uint32_t num_shares;
281 char **share_names;
282 uint32_t *num_params;
283 char ***param_names;
284 char ***param_values;
285 uint32_t share_count, param_count;
287 ctx = talloc_init("list");
289 if (argc != 0) {
290 net_conf_list_usage(argc, argv);
291 goto done;
294 werr = libnet_conf_get_config(ctx, &num_shares, &share_names,
295 &num_params, &param_names,
296 &param_values);
297 if (!W_ERROR_IS_OK(werr)) {
298 d_fprintf(stderr, "Error getting config: %s\n",
299 dos_errstr(werr));
300 goto done;
303 for (share_count = 0; share_count < num_shares; share_count++) {
304 d_printf("[%s]\n", share_names[share_count]);
305 for (param_count = 0; param_count < num_params[share_count];
306 param_count++)
308 d_printf("\t%s = %s\n",
309 param_names[share_count][param_count],
310 param_values[share_count][param_count]);
312 d_printf("\n");
315 ret = 0;
317 done:
318 TALLOC_FREE(ctx);
319 return ret;
322 static int net_conf_import(int argc, const char **argv)
324 int ret = -1;
325 const char *filename = NULL;
326 const char *servicename = NULL;
327 bool service_found = false;
328 TALLOC_CTX *ctx;
329 struct share_iterator *shares;
330 struct share_params *share;
331 struct share_params global_share = { GLOBAL_SECTION_SNUM };
333 ctx = talloc_init("net_conf_import");
335 switch (argc) {
336 case 0:
337 default:
338 net_conf_import_usage(argc, argv);
339 goto done;
340 case 2:
341 servicename = argv[1];
342 case 1:
343 filename = argv[0];
344 break;
347 DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
348 filename));
350 if (!lp_load(filename,
351 false, /* global_only */
352 true, /* save_defaults */
353 false, /* add_ipc */
354 true)) /* initialize_globals */
356 d_fprintf(stderr, "Error parsing configuration file.\n");
357 goto done;
360 if (opt_testmode) {
361 d_printf("\nTEST MODE - "
362 "would import the following configuration:\n\n");
365 if (((servicename == NULL) && globals_exist()) ||
366 strequal(servicename, GLOBAL_NAME))
368 service_found = true;
369 if (import_process_service(ctx, &global_share) != 0) {
370 goto done;
374 if (service_found && (servicename != NULL)) {
375 ret = 0;
376 goto done;
379 if (!(shares = share_list_all(ctx))) {
380 d_fprintf(stderr, "Could not list shares...\n");
381 goto done;
383 while ((share = next_share(shares)) != NULL) {
384 if ((servicename == NULL)
385 || strequal(servicename, lp_servicename(share->service)))
387 service_found = true;
388 if (import_process_service(ctx, share)!= 0) {
389 goto done;
394 if ((servicename != NULL) && !service_found) {
395 d_printf("Share %s not found in file %s\n",
396 servicename, filename);
397 goto done;
401 ret = 0;
403 done:
404 TALLOC_FREE(ctx);
405 return ret;
408 static int net_conf_listshares(int argc, const char **argv)
410 WERROR werr = WERR_OK;
411 int ret = -1;
412 uint32_t count, num_shares = 0;
413 char **share_names = NULL;
414 TALLOC_CTX *ctx;
416 ctx = talloc_init("listshares");
418 if (argc != 0) {
419 net_conf_listshares_usage(argc, argv);
420 goto done;
423 werr = libnet_conf_get_share_names(ctx, &num_shares, &share_names);
424 if (!W_ERROR_IS_OK(werr)) {
425 goto done;
428 for (count = 0; count < num_shares; count++)
430 d_printf("%s\n", share_names[count]);
433 ret = 0;
435 done:
436 TALLOC_FREE(ctx);
437 return ret;
440 static int net_conf_drop(int argc, const char **argv)
442 int ret = -1;
443 WERROR werr;
445 if (argc != 0) {
446 net_conf_drop_usage(argc, argv);
447 goto done;
450 werr = libnet_conf_drop();
451 if (!W_ERROR_IS_OK(werr)) {
452 d_fprintf(stderr, "Error deleting configuration: %s\n",
453 dos_errstr(werr));
454 goto done;
457 ret = 0;
459 done:
460 return ret;
463 static int net_conf_showshare(int argc, const char **argv)
465 int ret = -1;
466 WERROR werr = WERR_OK;
467 const char *sharename = NULL;
468 TALLOC_CTX *ctx;
469 uint32_t num_params;
470 uint32_t count;
471 char **param_names;
472 char **param_values;
474 ctx = talloc_init("showshare");
476 if (argc != 1) {
477 net_conf_showshare_usage(argc, argv);
478 goto done;
481 sharename = argv[0];
483 werr = libnet_conf_get_share(ctx, sharename, &num_params,
484 &param_names, &param_values);
485 if (!W_ERROR_IS_OK(werr)) {
486 d_printf("error getting share parameters: %s\n",
487 dos_errstr(werr));
488 goto done;
491 d_printf("[%s]\n", sharename);
493 for (count = 0; count < num_params; count++) {
494 d_printf("\t%s = %s\n", param_names[count],
495 param_values[count]);
498 ret = 0;
500 done:
501 TALLOC_FREE(ctx);
502 return ret;
506 * Add a share, with a couple of standard parameters, partly optional.
508 * This is a high level utility function of the net conf utility,
509 * not a direct frontend to the libnet_conf API.
511 static int net_conf_addshare(int argc, const char **argv)
513 int ret = -1;
514 WERROR werr = WERR_OK;
515 char *sharename = NULL;
516 const char *path = NULL;
517 const char *comment = NULL;
518 const char *guest_ok = "no";
519 const char *writeable = "no";
520 SMB_STRUCT_STAT sbuf;
522 switch (argc) {
523 case 0:
524 case 1:
525 default:
526 net_conf_addshare_usage(argc, argv);
527 goto done;
528 case 5:
529 comment = argv[4];
530 case 4:
531 if (!strnequal(argv[3], "guest_ok=", 9)) {
532 net_conf_addshare_usage(argc, argv);
533 goto done;
535 switch (argv[3][9]) {
536 case 'y':
537 case 'Y':
538 guest_ok = "yes";
539 break;
540 case 'n':
541 case 'N':
542 guest_ok = "no";
543 break;
544 default:
545 net_conf_addshare_usage(argc, argv);
546 goto done;
548 case 3:
549 if (!strnequal(argv[2], "writeable=", 10)) {
550 net_conf_addshare_usage(argc, argv);
551 goto done;
553 switch (argv[2][10]) {
554 case 'y':
555 case 'Y':
556 writeable = "yes";
557 break;
558 case 'n':
559 case 'N':
560 writeable = "no";
561 break;
562 default:
563 net_conf_addshare_usage(argc, argv);
564 goto done;
566 case 2:
567 path = argv[1];
568 sharename = strdup_lower(argv[0]);
569 break;
573 * validate arguments
576 /* validate share name */
578 if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
579 strlen(sharename)))
581 d_fprintf(stderr, "ERROR: share name %s contains "
582 "invalid characters (any of %s)\n",
583 sharename, INVALID_SHARENAME_CHARS);
584 goto done;
587 if (getpwnam(sharename)) {
588 d_fprintf(stderr, "ERROR: share name %s is already a valid "
589 "system user name.\n", sharename);
590 goto done;
593 if (strequal(sharename, GLOBAL_NAME)) {
594 d_fprintf(stderr,
595 "ERROR: 'global' is not a valid share name.\n");
596 goto done;
599 if (libnet_conf_share_exists(sharename)) {
600 d_fprintf(stderr, "ERROR: share %s already exists.\n",
601 sharename);
602 goto done;
605 /* validate path */
607 if (path[0] != '/') {
608 d_fprintf(stderr,
609 "Error: path '%s' is not an absolute path.\n",
610 path);
611 goto done;
614 if (sys_stat(path, &sbuf) != 0) {
615 d_fprintf(stderr,
616 "ERROR: cannot stat path '%s' to ensure "
617 "this is a directory.\n"
618 "Error was '%s'.\n",
619 path, strerror(errno));
620 goto done;
623 if (!S_ISDIR(sbuf.st_mode)) {
624 d_fprintf(stderr,
625 "ERROR: path '%s' is not a directory.\n",
626 path);
627 goto done;
631 * create the share
634 werr = libnet_conf_create_share(sharename);
635 if (!W_ERROR_IS_OK(werr)) {
636 d_fprintf(stderr, "Error creating share %s: %s\n",
637 sharename, dos_errstr(werr));
638 goto done;
642 * fill the share with parameters
645 werr = libnet_smbconf_setparm(sharename, "path", path);
646 if (!W_ERROR_IS_OK(werr)) {
647 d_fprintf(stderr, "Error setting parameter %s: %s\n",
648 "path", dos_errstr(werr));
649 goto done;
652 if (comment != NULL) {
653 werr = libnet_smbconf_setparm(sharename, "comment", comment);
654 if (!W_ERROR_IS_OK(werr)) {
655 d_fprintf(stderr, "Error setting parameter %s: %s\n",
656 "comment", dos_errstr(werr));
657 goto done;
661 werr = libnet_smbconf_setparm(sharename, "guest ok", guest_ok);
662 if (!W_ERROR_IS_OK(werr)) {
663 d_fprintf(stderr, "Error setting parameter %s: %s\n",
664 "'guest ok'", dos_errstr(werr));
665 goto done;
668 werr = libnet_smbconf_setparm(sharename, "writeable", writeable);
669 if (!W_ERROR_IS_OK(werr)) {
670 d_fprintf(stderr, "Error setting parameter %s: %s\n",
671 "writeable", dos_errstr(werr));
672 goto done;
675 ret = 0;
677 done:
678 SAFE_FREE(sharename);
679 return ret;
682 static int net_conf_delshare(int argc, const char **argv)
684 int ret = -1;
685 const char *sharename = NULL;
686 WERROR werr = WERR_OK;
688 if (argc != 1) {
689 net_conf_delshare_usage(argc, argv);
690 goto done;
692 sharename = argv[0];
694 werr = libnet_smbconf_delshare(sharename);
695 if (!W_ERROR_IS_OK(werr)) {
696 d_fprintf(stderr, "Error deleting share %s: %s\n",
697 sharename, dos_errstr(werr));
698 goto done;
701 ret = 0;
702 done:
703 return ret;
706 static int net_conf_setparm(int argc, const char **argv)
708 int ret = -1;
709 WERROR werr = WERR_OK;
710 char *service = NULL;
711 char *param = NULL;
712 const char *value_str = NULL;
714 if (argc != 3) {
715 net_conf_setparm_usage(argc, argv);
716 goto done;
718 service = strdup_lower(argv[0]);
719 param = strdup_lower(argv[1]);
720 value_str = argv[2];
722 werr = libnet_smbconf_setparm(service, param, value_str);
724 if (!W_ERROR_IS_OK(werr)) {
725 d_fprintf(stderr, "Error setting value '%s': %s\n",
726 param, dos_errstr(werr));
727 goto done;
730 ret = 0;
732 done:
733 SAFE_FREE(service);
734 SAFE_FREE(param);
735 return ret;
738 static int net_conf_getparm(int argc, const char **argv)
740 int ret = -1;
741 WERROR werr = WERR_OK;
742 char *service = NULL;
743 char *param = NULL;
744 char *valstr = NULL;
745 TALLOC_CTX *ctx;
747 ctx = talloc_init("getparm");
749 if (argc != 2) {
750 net_conf_getparm_usage(argc, argv);
751 goto done;
753 service = strdup_lower(argv[0]);
754 param = strdup_lower(argv[1]);
756 werr = libnet_smbconf_getparm(ctx, service, param, &valstr);
758 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
759 d_fprintf(stderr,
760 "Error: given service '%s' does not exist.\n",
761 service);
762 goto done;
763 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
764 d_fprintf(stderr,
765 "Error: given parameter '%s' is not set.\n",
766 param);
767 goto done;
768 } else if (!W_ERROR_IS_OK(werr)) {
769 d_fprintf(stderr, "Error getting value '%s': %s.\n",
770 param, dos_errstr(werr));
771 goto done;
774 d_printf("%s\n", valstr);
776 ret = 0;
777 done:
778 SAFE_FREE(service);
779 SAFE_FREE(param);
780 TALLOC_FREE(ctx);
781 return ret;
784 static int net_conf_delparm(int argc, const char **argv)
786 int ret = -1;
787 WERROR werr = WERR_OK;
788 char *service = NULL;
789 char *param = NULL;
791 if (argc != 2) {
792 net_conf_delparm_usage(argc, argv);
793 goto done;
795 service = strdup_lower(argv[0]);
796 param = strdup_lower(argv[1]);
798 werr = libnet_smbconf_delparm(service, param);
800 if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
801 d_fprintf(stderr,
802 "Error: given service '%s' does not exist.\n",
803 service);
804 goto done;
805 } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
806 d_fprintf(stderr,
807 "Error: given parameter '%s' is not set.\n",
808 param);
809 goto done;
810 } else if (!W_ERROR_IS_OK(werr)) {
811 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
812 param, dos_errstr(werr));
813 goto done;
816 ret = 0;
818 done:
819 SAFE_FREE(service);
820 SAFE_FREE(param);
821 return ret;
825 * Entry-point for all the CONF functions.
828 int net_conf(int argc, const char **argv)
830 int ret = -1;
831 struct functable2 func[] = {
832 {"list", net_conf_list,
833 "Dump the complete configuration in smb.conf like format."},
834 {"import", net_conf_import,
835 "Import configuration from file in smb.conf format."},
836 {"listshares", net_conf_listshares,
837 "List the registry shares."},
838 {"drop", net_conf_drop,
839 "Delete the complete configuration from registry."},
840 {"showshare", net_conf_showshare,
841 "Show the definition of a registry share."},
842 {"addshare", net_conf_addshare,
843 "Create a new registry share."},
844 {"delshare", net_conf_delshare,
845 "Delete a registry share."},
846 {"setparm", net_conf_setparm,
847 "Store a parameter."},
848 {"getparm", net_conf_getparm,
849 "Retrieve the value of a parameter."},
850 {"delparm", net_conf_delparm,
851 "Delete a parameter."},
852 {NULL, NULL, NULL}
855 if (!registry_init_regdb()) {
856 d_fprintf(stderr, "Error initializing the registry!\n");
857 goto done;
860 ret = net_run_function2(argc, argv, "net conf", func);
862 regdb_close();
864 done:
865 return ret;