ab1b0f3df7632a7c111bda868a66b140f7bca2ff
[sfrench/samba-autobuild/.git] / source3 / utils / net_conf.c
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
6  *
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.
11  *
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.
16  *
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/>.
19  */
20
21 /*
22  * This is an interface to Samba's configuration as made available
23  * by the libsmbconf interface (source/lib/smbconf/smbconf.c).
24  *
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.
28  */
29
30 #include "includes.h"
31 #include "utils/net.h"
32
33 /**********************************************************************
34  *
35  * usage functions
36  *
37  **********************************************************************/
38
39 static int net_conf_list_usage(struct net_context *c, int argc,
40                                const char **argv)
41 {
42         d_printf("USAGE: net conf list\n");
43         return -1;
44 }
45
46 static int net_conf_import_usage(struct net_context *c, int argc,
47                                  const char**argv)
48 {
49         d_printf("USAGE: net conf import [--test|-T] <filename> "
50                  "[<servicename>]\n"
51                  "\t[--test|-T]    testmode - do not act, just print "
52                         "what would be done\n"
53                  "\t<servicename>  only import service <servicename>, "
54                         "ignore the rest\n");
55         return -1;
56 }
57
58 static int net_conf_listshares_usage(struct net_context *c, int argc,
59                                      const char **argv)
60 {
61         d_printf("USAGE: net conf listshares\n");
62         return -1;
63 }
64
65 static int net_conf_drop_usage(struct net_context *c, int argc,
66                                const char **argv)
67 {
68         d_printf("USAGE: net conf drop\n");
69         return -1;
70 }
71
72 static int net_conf_showshare_usage(struct net_context *c, int argc,
73                                     const char **argv)
74 {
75         d_printf("USAGE: net conf showshare <sharename>\n");
76         return -1;
77 }
78
79 static int net_conf_addshare_usage(struct net_context *c, int argc,
80                                    const char **argv)
81 {
82         d_printf("USAGE: net conf addshare <sharename> <path> "
83                  "[writeable={y|N} [guest_ok={y|N} [<comment>]]\n"
84                  "\t<sharename>      the new share name.\n"
85                  "\t<path>           the path on the filesystem to export.\n"
86                  "\twriteable={y|N}  set \"writeable to \"yes\" or "
87                  "\"no\" (default) on this share.\n"
88                  "\tguest_ok={y|N}   set \"guest ok\" to \"yes\" or "
89                  "\"no\" (default)   on this share.\n"
90                  "\t<comment>        optional comment for the new share.\n");
91         return -1;
92 }
93
94 static int net_conf_delshare_usage(struct net_context *c, int argc,
95                                    const char **argv)
96 {
97         d_printf("USAGE: net conf delshare <sharename>\n");
98         return -1;
99 }
100
101 static int net_conf_setparm_usage(struct net_context *c, int argc,
102                                   const char **argv)
103 {
104         d_printf("USAGE: net conf setparm <section> <param> <value>\n");
105         return -1;
106 }
107
108 static int net_conf_getparm_usage(struct net_context *c, int argc,
109                                   const char **argv)
110 {
111         d_printf("USAGE: net conf getparm <section> <param>\n");
112         return -1;
113 }
114
115 static int net_conf_delparm_usage(struct net_context *c, int argc,
116                                   const char **argv)
117 {
118         d_printf("USAGE: net conf delparm <section> <param>\n");
119         return -1;
120 }
121
122 static int net_conf_getincludes_usage(struct net_context *c, int argc,
123                                       const char **argv)
124 {
125         d_printf("USAGE: net conf getincludes <section>\n");
126         return -1;
127 }
128
129 static int net_conf_setincludes_usage(struct net_context *c, int argc,
130                                       const char **argv)
131 {
132         d_printf("USAGE: net conf setincludes <section> [<filename>]*\n");
133         return -1;
134 }
135
136 static int net_conf_delincludes_usage(struct net_context *c, int argc,
137                                       const char **argv)
138 {
139         d_printf("USAGE: net conf delincludes <section>\n");
140         return -1;
141 }
142
143
144 /**********************************************************************
145  *
146  * Helper functions
147  *
148  **********************************************************************/
149
150 /**
151  * This functions process a service previously loaded with libsmbconf.
152  */
153 static WERROR import_process_service(struct net_context *c,
154                                      struct smbconf_ctx *conf_ctx,
155                                      struct smbconf_service *service)
156 {
157         uint32_t idx;
158         WERROR werr = WERR_OK;
159         uint32_t num_includes = 0;
160         char **includes = NULL;
161         TALLOC_CTX *mem_ctx = talloc_stackframe();
162
163         if (c->opt_testmode) {
164                 const char *indent = "";
165                 if (service->name != NULL) {
166                         d_printf("[%s]\n", service->name);
167                         indent = "\t";
168                 }
169                 for (idx = 0; idx < service->num_params; idx++) {
170                         d_printf("%s%s = %s\n", indent,
171                                  service->param_names[idx],
172                                  service->param_values[idx]);
173                 }
174                 d_printf("\n");
175                 goto done;
176         }
177
178         if (smbconf_share_exists(conf_ctx, service->name)) {
179                 werr = smbconf_delete_share(conf_ctx, service->name);
180                 if (!W_ERROR_IS_OK(werr)) {
181                         goto done;
182                 }
183         }
184         werr = smbconf_create_share(conf_ctx, service->name);
185         if (!W_ERROR_IS_OK(werr)) {
186                 goto done;
187         }
188
189         for (idx = 0; idx < service->num_params; idx ++) {
190                 if (strequal(service->param_names[idx], "include")) {
191                         includes = TALLOC_REALLOC_ARRAY(mem_ctx,
192                                                         includes,
193                                                         char *,
194                                                         num_includes+1);
195                         if (includes == NULL) {
196                                 werr = WERR_NOMEM;
197                                 goto done;
198                         }
199                         includes[num_includes] = talloc_strdup(includes,
200                                                 service->param_values[idx]);
201                         if (includes[num_includes] == NULL) {
202                                 werr = WERR_NOMEM;
203                                 goto done;
204                         }
205                         num_includes++;
206                 } else {
207                         werr = smbconf_set_parameter(conf_ctx,
208                                                      service->name,
209                                                      service->param_names[idx],
210                                                      service->param_values[idx]);
211                         if (!W_ERROR_IS_OK(werr)) {
212                                 goto done;
213                         }
214                 }
215         }
216
217         werr = smbconf_set_includes(conf_ctx, service->name, num_includes,
218                                     (const char **)includes);
219
220 done:
221         TALLOC_FREE(mem_ctx);
222         return werr;
223 }
224
225
226 /**********************************************************************
227  *
228  * the main conf functions
229  *
230  **********************************************************************/
231
232 static int net_conf_list(struct net_context *c, struct smbconf_ctx *conf_ctx,
233                          int argc, const char **argv)
234 {
235         WERROR werr = WERR_OK;
236         int ret = -1;
237         TALLOC_CTX *mem_ctx;
238         uint32_t num_shares;
239         uint32_t share_count, param_count;
240         struct smbconf_service **shares = NULL;
241
242         mem_ctx = talloc_stackframe();
243
244         if (argc != 0 || c->display_usage) {
245                 net_conf_list_usage(c, argc, argv);
246                 goto done;
247         }
248
249         werr = smbconf_get_config(conf_ctx, mem_ctx, &num_shares, &shares);
250         if (!W_ERROR_IS_OK(werr)) {
251                 d_fprintf(stderr, "Error getting config: %s\n",
252                           dos_errstr(werr));
253                 goto done;
254         }
255
256         for (share_count = 0; share_count < num_shares; share_count++) {
257                 const char *indent = "";
258                 if (shares[share_count]->name != NULL) {
259                         d_printf("[%s]\n", shares[share_count]->name);
260                         indent = "\t";
261                 }
262                 for (param_count = 0;
263                      param_count < shares[share_count]->num_params;
264                      param_count++)
265                 {
266                         d_printf("%s%s = %s\n",
267                                  indent,
268                                  shares[share_count]->param_names[param_count],
269                                  shares[share_count]->param_values[param_count]);
270                 }
271                 d_printf("\n");
272         }
273
274         ret = 0;
275
276 done:
277         TALLOC_FREE(mem_ctx);
278         return ret;
279 }
280
281 static int net_conf_import(struct net_context *c, struct smbconf_ctx *conf_ctx,
282                            int argc, const char **argv)
283 {
284         int ret = -1;
285         const char *filename = NULL;
286         const char *servicename = NULL;
287         char *conf_source = NULL;
288         TALLOC_CTX *mem_ctx;
289         struct smbconf_ctx *txt_ctx;
290         WERROR werr;
291
292         if (c->display_usage)
293                 return net_conf_import_usage(c, argc, argv);
294
295         mem_ctx = talloc_stackframe();
296
297         switch (argc) {
298                 case 0:
299                 default:
300                         net_conf_import_usage(c, argc, argv);
301                         goto done;
302                 case 2:
303                         servicename = talloc_strdup_lower(mem_ctx, argv[1]);
304                         if (servicename == NULL) {
305                                 d_printf("error: out of memory!\n");
306                                 goto done;
307                         }
308                 case 1:
309                         filename = argv[0];
310                         break;
311         }
312
313         DEBUG(3,("net_conf_import: reading configuration from file %s.\n",
314                 filename));
315
316         conf_source = talloc_asprintf(mem_ctx, "file:%s", filename);
317         if (conf_source == NULL) {
318                 d_printf("error: out of memory!\n");
319                 goto done;
320         }
321
322         werr = smbconf_init(mem_ctx, &txt_ctx, conf_source);
323         if (!W_ERROR_IS_OK(werr)) {
324                 d_printf("error loading file '%s': %s\n", filename,
325                          dos_errstr(werr));
326                 goto done;
327         }
328
329         if (c->opt_testmode) {
330                 d_printf("\nTEST MODE - "
331                          "would import the following configuration:\n\n");
332         }
333
334         if (servicename != NULL) {
335                 struct smbconf_service *service = NULL;
336
337                 werr = smbconf_get_share(txt_ctx, mem_ctx,
338                                          servicename,
339                                          &service);
340                 if (!W_ERROR_IS_OK(werr)) {
341                         goto done;
342                 }
343                 werr = import_process_service(c, conf_ctx, service);
344                 if (!W_ERROR_IS_OK(werr)) {
345                         goto done;
346                 }
347         } else {
348                 struct smbconf_service **services = NULL;
349                 uint32_t num_shares, sidx;
350
351                 werr = smbconf_get_config(txt_ctx, mem_ctx,
352                                           &num_shares,
353                                           &services);
354                 if (!W_ERROR_IS_OK(werr)) {
355                         goto done;
356                 }
357                 if (!c->opt_testmode) {
358                         werr = smbconf_drop(conf_ctx);
359                         if (!W_ERROR_IS_OK(werr)) {
360                                 goto done;
361                         }
362                 }
363                 for (sidx = 0; sidx < num_shares; sidx++) {
364                         werr = import_process_service(c, conf_ctx,
365                                                       services[sidx]);
366                         if (!W_ERROR_IS_OK(werr)) {
367                                 goto done;
368                         }
369                 }
370         }
371
372         ret = 0;
373
374 done:
375         TALLOC_FREE(mem_ctx);
376         return ret;
377 }
378
379 static int net_conf_listshares(struct net_context *c,
380                                struct smbconf_ctx *conf_ctx, int argc,
381                                const char **argv)
382 {
383         WERROR werr = WERR_OK;
384         int ret = -1;
385         uint32_t count, num_shares = 0;
386         char **share_names = NULL;
387         TALLOC_CTX *mem_ctx;
388
389         mem_ctx = talloc_stackframe();
390
391         if (argc != 0 || c->display_usage) {
392                 net_conf_listshares_usage(c, argc, argv);
393                 goto done;
394         }
395
396         werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
397                                        &share_names);
398         if (!W_ERROR_IS_OK(werr)) {
399                 goto done;
400         }
401
402         for (count = 0; count < num_shares; count++)
403         {
404                 d_printf("%s\n", share_names[count]);
405         }
406
407         ret = 0;
408
409 done:
410         TALLOC_FREE(mem_ctx);
411         return ret;
412 }
413
414 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
415                          int argc, const char **argv)
416 {
417         int ret = -1;
418         WERROR werr;
419
420         if (argc != 0 || c->display_usage) {
421                 net_conf_drop_usage(c, argc, argv);
422                 goto done;
423         }
424
425         werr = smbconf_drop(conf_ctx);
426         if (!W_ERROR_IS_OK(werr)) {
427                 d_fprintf(stderr, "Error deleting configuration: %s\n",
428                           dos_errstr(werr));
429                 goto done;
430         }
431
432         ret = 0;
433
434 done:
435         return ret;
436 }
437
438 static int net_conf_showshare(struct net_context *c,
439                               struct smbconf_ctx *conf_ctx, int argc,
440                               const char **argv)
441 {
442         int ret = -1;
443         WERROR werr = WERR_OK;
444         const char *sharename = NULL;
445         TALLOC_CTX *mem_ctx;
446         uint32_t count;
447         struct smbconf_service *service = NULL;
448
449         mem_ctx = talloc_stackframe();
450
451         if (argc != 1 || c->display_usage) {
452                 net_conf_showshare_usage(c, argc, argv);
453                 goto done;
454         }
455
456         sharename = talloc_strdup_lower(mem_ctx, argv[0]);
457         if (sharename == NULL) {
458                 d_printf("error: out of memory!\n");
459                 goto done;
460         }
461
462         werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
463         if (!W_ERROR_IS_OK(werr)) {
464                 d_printf("error getting share parameters: %s\n",
465                          dos_errstr(werr));
466                 goto done;
467         }
468
469         d_printf("[%s]\n", sharename);
470
471         for (count = 0; count < service->num_params; count++) {
472                 d_printf("\t%s = %s\n", service->param_names[count],
473                          service->param_values[count]);
474         }
475
476         ret = 0;
477
478 done:
479         TALLOC_FREE(mem_ctx);
480         return ret;
481 }
482
483 /**
484  * Add a share, with a couple of standard parameters, partly optional.
485  *
486  * This is a high level utility function of the net conf utility,
487  * not a direct frontend to the smbconf API.
488  */
489 static int net_conf_addshare(struct net_context *c,
490                              struct smbconf_ctx *conf_ctx, int argc,
491                              const char **argv)
492 {
493         int ret = -1;
494         WERROR werr = WERR_OK;
495         char *sharename = NULL;
496         const char *path = NULL;
497         const char *comment = NULL;
498         const char *guest_ok = "no";
499         const char *writeable = "no";
500         SMB_STRUCT_STAT sbuf;
501         TALLOC_CTX *mem_ctx = talloc_stackframe();
502
503         if (c->display_usage) {
504                 net_conf_addshare_usage(c, argc, argv);
505                 ret = 0;
506                 goto done;
507         }
508
509         switch (argc) {
510                 case 0:
511                 case 1:
512                 default:
513                         net_conf_addshare_usage(c, argc, argv);
514                         goto done;
515                 case 5:
516                         comment = argv[4];
517                 case 4:
518                         if (!strnequal(argv[3], "guest_ok=", 9)) {
519                                 net_conf_addshare_usage(c, argc, argv);
520                                 goto done;
521                         }
522                         switch (argv[3][9]) {
523                                 case 'y':
524                                 case 'Y':
525                                         guest_ok = "yes";
526                                         break;
527                                 case 'n':
528                                 case 'N':
529                                         guest_ok = "no";
530                                         break;
531                                 default:
532                                         net_conf_addshare_usage(c, argc, argv);
533                                         goto done;
534                         }
535                 case 3:
536                         if (!strnequal(argv[2], "writeable=", 10)) {
537                                 net_conf_addshare_usage(c, argc, argv);
538                                 goto done;
539                         }
540                         switch (argv[2][10]) {
541                                 case 'y':
542                                 case 'Y':
543                                         writeable = "yes";
544                                         break;
545                                 case 'n':
546                                 case 'N':
547                                         writeable = "no";
548                                         break;
549                                 default:
550                                         net_conf_addshare_usage(c, argc, argv);
551                                         goto done;
552                         }
553                 case 2:
554                         path = argv[1];
555                         sharename = talloc_strdup_lower(mem_ctx, argv[0]);
556                         if (sharename == NULL) {
557                                 d_printf("error: out of memory!\n");
558                                 goto done;
559                         }
560
561                         break;
562         }
563
564         /*
565          * validate arguments
566          */
567
568         /* validate share name */
569
570         if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
571                                strlen(sharename)))
572         {
573                 d_fprintf(stderr, "ERROR: share name %s contains "
574                         "invalid characters (any of %s)\n",
575                         sharename, INVALID_SHARENAME_CHARS);
576                 goto done;
577         }
578
579         if (getpwnam(sharename)) {
580                 d_fprintf(stderr, "ERROR: share name %s is already a valid "
581                           "system user name.\n", sharename);
582                 goto done;
583         }
584
585         if (strequal(sharename, GLOBAL_NAME)) {
586                 d_fprintf(stderr,
587                           "ERROR: 'global' is not a valid share name.\n");
588                 goto done;
589         }
590
591         if (smbconf_share_exists(conf_ctx, sharename)) {
592                 d_fprintf(stderr, "ERROR: share %s already exists.\n",
593                           sharename);
594                 goto done;
595         }
596
597         /* validate path */
598
599         if (path[0] != '/') {
600                 d_fprintf(stderr,
601                           "Error: path '%s' is not an absolute path.\n",
602                           path);
603                 goto done;
604         }
605
606         if (sys_stat(path, &sbuf) != 0) {
607                 d_fprintf(stderr,
608                           "ERROR: cannot stat path '%s' to ensure "
609                           "this is a directory.\n"
610                           "Error was '%s'.\n",
611                           path, strerror(errno));
612                 goto done;
613         }
614
615         if (!S_ISDIR(sbuf.st_mode)) {
616                 d_fprintf(stderr,
617                           "ERROR: path '%s' is not a directory.\n",
618                           path);
619                 goto done;
620         }
621
622         /*
623          * create the share
624          */
625
626         werr = smbconf_create_share(conf_ctx, sharename);
627         if (!W_ERROR_IS_OK(werr)) {
628                 d_fprintf(stderr, "Error creating share %s: %s\n",
629                           sharename, dos_errstr(werr));
630                 goto done;
631         }
632
633         /*
634          * fill the share with parameters
635          */
636
637         werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
638         if (!W_ERROR_IS_OK(werr)) {
639                 d_fprintf(stderr, "Error setting parameter %s: %s\n",
640                           "path", dos_errstr(werr));
641                 goto done;
642         }
643
644         if (comment != NULL) {
645                 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
646                                              comment);
647                 if (!W_ERROR_IS_OK(werr)) {
648                         d_fprintf(stderr, "Error setting parameter %s: %s\n",
649                                   "comment", dos_errstr(werr));
650                         goto done;
651                 }
652         }
653
654         werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
655         if (!W_ERROR_IS_OK(werr)) {
656                 d_fprintf(stderr, "Error setting parameter %s: %s\n",
657                           "'guest ok'", dos_errstr(werr));
658                 goto done;
659         }
660
661         werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
662                                      writeable);
663         if (!W_ERROR_IS_OK(werr)) {
664                 d_fprintf(stderr, "Error setting parameter %s: %s\n",
665                           "writeable", dos_errstr(werr));
666                 goto done;
667         }
668
669         ret = 0;
670
671 done:
672         TALLOC_FREE(mem_ctx);
673         return ret;
674 }
675
676 static int net_conf_delshare(struct net_context *c,
677                              struct smbconf_ctx *conf_ctx, int argc,
678                              const char **argv)
679 {
680         int ret = -1;
681         const char *sharename = NULL;
682         WERROR werr = WERR_OK;
683         TALLOC_CTX *mem_ctx = talloc_stackframe();
684
685         if (argc != 1 || c->display_usage) {
686                 net_conf_delshare_usage(c, argc, argv);
687                 goto done;
688         }
689         sharename = talloc_strdup_lower(mem_ctx, argv[0]);
690         if (sharename == NULL) {
691                 d_printf("error: out of memory!\n");
692                 goto done;
693         }
694
695         werr = smbconf_delete_share(conf_ctx, sharename);
696         if (!W_ERROR_IS_OK(werr)) {
697                 d_fprintf(stderr, "Error deleting share %s: %s\n",
698                           sharename, dos_errstr(werr));
699                 goto done;
700         }
701
702         ret = 0;
703 done:
704         TALLOC_FREE(mem_ctx);
705         return ret;
706 }
707
708 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
709                             int argc, const char **argv)
710 {
711         int ret = -1;
712         WERROR werr = WERR_OK;
713         char *service = NULL;
714         char *param = NULL;
715         const char *value_str = NULL;
716         TALLOC_CTX *mem_ctx = talloc_stackframe();
717
718         if (argc != 3 || c->display_usage) {
719                 net_conf_setparm_usage(c, argc, argv);
720                 goto done;
721         }
722         service = talloc_strdup_lower(mem_ctx, argv[0]);
723         if (service == NULL) {
724                 d_printf("error: out of memory!\n");
725                 goto done;
726         }
727         param = talloc_strdup_lower(mem_ctx, argv[1]);
728         if (param == NULL) {
729                 d_printf("error: out of memory!\n");
730                 goto done;
731         }
732         value_str = argv[2];
733
734         if (!smbconf_share_exists(conf_ctx, service)) {
735                 werr = smbconf_create_share(conf_ctx, service);
736                 if (!W_ERROR_IS_OK(werr)) {
737                         d_fprintf(stderr, "Error creating share '%s': %s\n",
738                                   service, dos_errstr(werr));
739                         goto done;
740                 }
741         }
742
743         werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
744
745         if (!W_ERROR_IS_OK(werr)) {
746                 d_fprintf(stderr, "Error setting value '%s': %s\n",
747                           param, dos_errstr(werr));
748                 goto done;
749         }
750
751         ret = 0;
752
753 done:
754         TALLOC_FREE(mem_ctx);
755         return ret;
756 }
757
758 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
759                             int argc, const char **argv)
760 {
761         int ret = -1;
762         WERROR werr = WERR_OK;
763         char *service = NULL;
764         char *param = NULL;
765         char *valstr = NULL;
766         TALLOC_CTX *mem_ctx;
767
768         mem_ctx = talloc_stackframe();
769
770         if (argc != 2 || c->display_usage) {
771                 net_conf_getparm_usage(c, argc, argv);
772                 goto done;
773         }
774         service = talloc_strdup_lower(mem_ctx, argv[0]);
775         if (service == NULL) {
776                 d_printf("error: out of memory!\n");
777                 goto done;
778         }
779         param = talloc_strdup_lower(mem_ctx, argv[1]);
780         if (param == NULL) {
781                 d_printf("error: out of memory!\n");
782                 goto done;
783         }
784
785         werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
786
787         if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
788                 d_fprintf(stderr,
789                           "Error: given service '%s' does not exist.\n",
790                           service);
791                 goto done;
792         } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
793                 d_fprintf(stderr,
794                           "Error: given parameter '%s' is not set.\n",
795                           param);
796                 goto done;
797         } else if (!W_ERROR_IS_OK(werr)) {
798                 d_fprintf(stderr, "Error getting value '%s': %s.\n",
799                           param, dos_errstr(werr));
800                 goto done;
801         }
802
803         d_printf("%s\n", valstr);
804
805         ret = 0;
806 done:
807         TALLOC_FREE(mem_ctx);
808         return ret;
809 }
810
811 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
812                             int argc, const char **argv)
813 {
814         int ret = -1;
815         WERROR werr = WERR_OK;
816         char *service = NULL;
817         char *param = NULL;
818         TALLOC_CTX *mem_ctx = talloc_stackframe();
819
820         if (argc != 2 || c->display_usage) {
821                 net_conf_delparm_usage(c, argc, argv);
822                 goto done;
823         }
824         service = talloc_strdup_lower(mem_ctx, argv[0]);
825         if (service == NULL) {
826                 d_printf("error: out of memory!\n");
827                 goto done;
828         }
829         param = talloc_strdup_lower(mem_ctx, argv[1]);
830         if (param == NULL) {
831                 d_printf("error: out of memory!\n");
832                 goto done;
833         }
834
835         werr = smbconf_delete_parameter(conf_ctx, service, param);
836
837         if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
838                 d_fprintf(stderr,
839                           "Error: given service '%s' does not exist.\n",
840                           service);
841                 goto done;
842         } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
843                 d_fprintf(stderr,
844                           "Error: given parameter '%s' is not set.\n",
845                           param);
846                 goto done;
847         } else if (!W_ERROR_IS_OK(werr)) {
848                 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
849                           param, dos_errstr(werr));
850                 goto done;
851         }
852
853         ret = 0;
854
855 done:
856         TALLOC_FREE(mem_ctx);
857         return ret;
858 }
859
860 static int net_conf_getincludes(struct net_context *c,
861                                 struct smbconf_ctx *conf_ctx,
862                                 int argc, const char **argv)
863 {
864         WERROR werr;
865         uint32_t num_includes;
866         uint32_t count;
867         char *service;
868         char **includes = NULL;
869         int ret = -1;
870         TALLOC_CTX *mem_ctx = talloc_stackframe();
871
872         if (argc != 1 || c->display_usage) {
873                 net_conf_getincludes_usage(c, argc, argv);
874                 goto done;
875         }
876
877         service = talloc_strdup_lower(mem_ctx, argv[0]);
878         if (service == NULL) {
879                 d_printf("error: out of memory!\n");
880                 goto done;
881         }
882
883         werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
884                                     &num_includes, &includes);
885         if (!W_ERROR_IS_OK(werr)) {
886                 d_printf("error getting includes: %s\n", dos_errstr(werr));
887                 goto done;
888         }
889
890         for (count = 0; count < num_includes; count++) {
891                 d_printf("include = %s\n", includes[count]);
892         }
893
894         ret = 0;
895
896 done:
897         TALLOC_FREE(mem_ctx);
898         return ret;
899 }
900
901 static int net_conf_setincludes(struct net_context *c,
902                                 struct smbconf_ctx *conf_ctx,
903                                 int argc, const char **argv)
904 {
905         WERROR werr;
906         char *service;
907         uint32_t num_includes;
908         const char **includes;
909         int ret = -1;
910         TALLOC_CTX *mem_ctx = talloc_stackframe();
911
912         if (argc < 1 || c->display_usage) {
913                 net_conf_setincludes_usage(c, argc, argv);
914                 goto done;
915         }
916
917         service = talloc_strdup_lower(mem_ctx, argv[0]);
918         if (service == NULL) {
919                 d_printf("error: out of memory!\n");
920                 goto done;
921         }
922
923         num_includes = argc - 1;
924         if (num_includes == 0) {
925                 includes = NULL;
926         } else {
927                 includes = argv + 1;
928         }
929
930         werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
931         if (!W_ERROR_IS_OK(werr)) {
932                 d_printf("error setting includes: %s\n", dos_errstr(werr));
933                 goto done;
934         }
935
936         ret = 0;
937
938 done:
939         TALLOC_FREE(mem_ctx);
940         return ret;
941 }
942
943 static int net_conf_delincludes(struct net_context *c,
944                                 struct smbconf_ctx *conf_ctx,
945                                 int argc, const char **argv)
946 {
947         WERROR werr;
948         char *service;
949         int ret = -1;
950         TALLOC_CTX *mem_ctx = talloc_stackframe();
951
952         if (argc != 1 || c->display_usage) {
953                 net_conf_delincludes_usage(c, argc, argv);
954                 goto done;
955         }
956
957         service = talloc_strdup_lower(mem_ctx, argv[0]);
958         if (service == NULL) {
959                 d_printf("error: out of memory!\n");
960                 goto done;
961         }
962
963         werr = smbconf_delete_includes(conf_ctx, service);
964         if (!W_ERROR_IS_OK(werr)) {
965                 d_printf("error deleting includes: %s\n", dos_errstr(werr));
966                 goto done;
967         }
968
969         ret = 0;
970
971 done:
972         TALLOC_FREE(mem_ctx);
973         return ret;
974 }
975
976
977 /**********************************************************************
978  *
979  * Wrapper and net_conf_run_function mechanism.
980  *
981  **********************************************************************/
982
983 /**
984  * Wrapper function to call the main conf functions.
985  * The wrapper calls handles opening and closing of the
986  * configuration.
987  */
988 static int net_conf_wrap_function(struct net_context *c,
989                                   int (*fn)(struct net_context *,
990                                             struct smbconf_ctx *,
991                                             int, const char **),
992                                   int argc, const char **argv)
993 {
994         WERROR werr;
995         TALLOC_CTX *mem_ctx = talloc_stackframe();
996         struct smbconf_ctx *conf_ctx;
997         int ret = -1;
998
999         werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1000
1001         if (!W_ERROR_IS_OK(werr)) {
1002                 return -1;
1003         }
1004
1005         ret = fn(c, conf_ctx, argc, argv);
1006
1007         smbconf_shutdown(conf_ctx);
1008
1009         return ret;
1010 }
1011
1012 /*
1013  * We need a functable struct of our own, because the
1014  * functions are called through a wrapper that handles
1015  * the opening and closing of the configuration, and so on.
1016  */
1017 struct conf_functable {
1018         const char *funcname;
1019         int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1020                   const char **argv);
1021         int valid_transports;
1022         const char *description;
1023         const char *usage;
1024 };
1025
1026 /**
1027  * This imitates net_run_function but calls the main functions
1028  * through the wrapper net_conf_wrap_function().
1029  */
1030 static int net_conf_run_function(struct net_context *c, int argc,
1031                                  const char **argv, const char *whoami,
1032                                  struct conf_functable *table)
1033 {
1034         int i;
1035
1036         if (argc != 0) {
1037                 for (i=0; table[i].funcname; i++) {
1038                         if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1039                                 return net_conf_wrap_function(c, table[i].fn,
1040                                                               argc-1,
1041                                                               argv+1);
1042                 }
1043         }
1044
1045         d_printf("Usage:\n");
1046         for (i=0; table[i].funcname; i++) {
1047                 if (c->display_usage == false)
1048                         d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1049                                  table[i].description);
1050                 else
1051                         d_printf("%s\n", table[i].usage);
1052         }
1053
1054         return c->display_usage?0:-1;
1055 }
1056
1057 /*
1058  * Entry-point for all the CONF functions.
1059  */
1060
1061 int net_conf(struct net_context *c, int argc, const char **argv)
1062 {
1063         int ret = -1;
1064         struct conf_functable func_table[] = {
1065                 {
1066                         "list",
1067                         net_conf_list,
1068                         NET_TRANSPORT_LOCAL,
1069                         "Dump the complete configuration in smb.conf like "
1070                         "format.",
1071                         "net conf list\n"
1072                         "    Dump the complete configuration in smb.conf like "
1073                         "format."
1074
1075                 },
1076                 {
1077                         "import",
1078                         net_conf_import,
1079                         NET_TRANSPORT_LOCAL,
1080                         "Import configuration from file in smb.conf format.",
1081                         "net conf import\n"
1082                         "    Import configuration from file in smb.conf format."
1083                 },
1084                 {
1085                         "listshares",
1086                         net_conf_listshares,
1087                         NET_TRANSPORT_LOCAL,
1088                         "List the share names.",
1089                         "net conf listshares\n"
1090                         "    List the share names."
1091                 },
1092                 {
1093                         "drop",
1094                         net_conf_drop,
1095                         NET_TRANSPORT_LOCAL,
1096                         "Delete the complete configuration.",
1097                         "net conf drop\n"
1098                         "    Delete the complete configuration."
1099                 },
1100                 {
1101                         "showshare",
1102                         net_conf_showshare,
1103                         NET_TRANSPORT_LOCAL,
1104                         "Show the definition of a share.",
1105                         "net conf showshare\n"
1106                         "    Show the definition of a share."
1107                 },
1108                 {
1109                         "addshare",
1110                         net_conf_addshare,
1111                         NET_TRANSPORT_LOCAL,
1112                         "Create a new share.",
1113                         "net conf addshare\n"
1114                         "    Create a new share."
1115                 },
1116                 {
1117                         "delshare",
1118                         net_conf_delshare,
1119                         NET_TRANSPORT_LOCAL,
1120                         "Delete a share.",
1121                         "net conf delshare\n"
1122                         "    Delete a share."
1123                 },
1124                 {
1125                         "setparm",
1126                         net_conf_setparm,
1127                         NET_TRANSPORT_LOCAL,
1128                         "Store a parameter.",
1129                         "net conf setparm\n"
1130                         "    Store a parameter."
1131                 },
1132                 {
1133                         "getparm",
1134                         net_conf_getparm,
1135                         NET_TRANSPORT_LOCAL,
1136                         "Retrieve the value of a parameter.",
1137                         "net conf getparm\n"
1138                         "    Retrieve the value of a parameter."
1139                 },
1140                 {
1141                         "delparm",
1142                         net_conf_delparm,
1143                         NET_TRANSPORT_LOCAL,
1144                         "Delete a parameter.",
1145                         "net conf delparm\n"
1146                         "    Delete a parameter."
1147                 },
1148                 {
1149                         "getincludes",
1150                         net_conf_getincludes,
1151                         NET_TRANSPORT_LOCAL,
1152                         "Show the includes of a share definition.",
1153                         "net conf getincludes\n"
1154                         "    Show the includes of a share definition."
1155                 },
1156                 {
1157                         "setincludes",
1158                         net_conf_setincludes,
1159                         NET_TRANSPORT_LOCAL,
1160                         "Set includes for a share.",
1161                         "net conf setincludes\n"
1162                         "    Set includes for a share."
1163                 },
1164                 {
1165                         "delincludes",
1166                         net_conf_delincludes,
1167                         NET_TRANSPORT_LOCAL,
1168                         "Delete includes from a share definition.",
1169                         "net conf setincludes\n"
1170                         "    Delete includes from a share definition."
1171                 },
1172                 {NULL, NULL, 0, NULL, NULL}
1173         };
1174
1175         ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1176
1177         return ret;
1178 }
1179