dea6209c78e712b7be6161596e4cf4e18d0878cd
[samba.git] / source / 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(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 cancel;
342                 }
343                 werr = import_process_service(c, conf_ctx, service);
344                 if (!W_ERROR_IS_OK(werr)) {
345                         goto cancel;
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 cancel;
356                 }
357                 if (!c->opt_testmode) {
358                         werr = smbconf_drop(conf_ctx);
359                         if (!W_ERROR_IS_OK(werr)) {
360                                 goto cancel;
361                         }
362                 }
363
364                 /*
365                  * Wrap the importing of shares into a transaction,
366                  * but only 100 at a time, in order to serve memory.
367                  * The allocated memory accumulates across the actions
368                  * within the transaction, and for me, some 1500
369                  * imported shares, the MAX_TALLOC_SIZE of 256 MB
370                  * was exceeded.
371                  */
372                 werr = smbconf_transaction_start(conf_ctx);
373                 if (!W_ERROR_IS_OK(werr)) {
374                         d_printf("error starting transaction: %s\n",
375                                  win_errstr(werr));
376                         goto done;
377                 }
378
379                 for (sidx = 0; sidx < num_shares; sidx++) {
380                         werr = import_process_service(c, conf_ctx,
381                                                       services[sidx]);
382                         if (!W_ERROR_IS_OK(werr)) {
383                                 goto cancel;
384                         }
385
386                         if (sidx % 100) {
387                                 continue;
388                         }
389
390                         werr = smbconf_transaction_commit(conf_ctx);
391                         if (!W_ERROR_IS_OK(werr)) {
392                                 d_printf("error committing transaction: %s\n",
393                                          win_errstr(werr));
394                                 goto done;
395                         }
396                         werr = smbconf_transaction_start(conf_ctx);
397                         if (!W_ERROR_IS_OK(werr)) {
398                                 d_printf("error starting transaction: %s\n",
399                                          win_errstr(werr));
400                                 goto done;
401                         }
402                 }
403         }
404
405         werr = smbconf_transaction_commit(conf_ctx);
406         if (!W_ERROR_IS_OK(werr)) {
407                 d_printf("error committing transaction: %s\n",
408                          win_errstr(werr));
409         } else {
410                 ret = 0;
411         }
412
413         goto done;
414
415 cancel:
416         werr = smbconf_transaction_cancel(conf_ctx);
417         if (!W_ERROR_IS_OK(werr)) {
418                 d_printf("error cancelling transaction: %s\n",
419                          win_errstr(werr));
420         }
421
422 done:
423         TALLOC_FREE(mem_ctx);
424         return ret;
425 }
426
427 static int net_conf_listshares(struct net_context *c,
428                                struct smbconf_ctx *conf_ctx, int argc,
429                                const char **argv)
430 {
431         WERROR werr = WERR_OK;
432         int ret = -1;
433         uint32_t count, num_shares = 0;
434         char **share_names = NULL;
435         TALLOC_CTX *mem_ctx;
436
437         mem_ctx = talloc_stackframe();
438
439         if (argc != 0 || c->display_usage) {
440                 net_conf_listshares_usage(c, argc, argv);
441                 goto done;
442         }
443
444         werr = smbconf_get_share_names(conf_ctx, mem_ctx, &num_shares,
445                                        &share_names);
446         if (!W_ERROR_IS_OK(werr)) {
447                 goto done;
448         }
449
450         for (count = 0; count < num_shares; count++)
451         {
452                 d_printf("%s\n", share_names[count]);
453         }
454
455         ret = 0;
456
457 done:
458         TALLOC_FREE(mem_ctx);
459         return ret;
460 }
461
462 static int net_conf_drop(struct net_context *c, struct smbconf_ctx *conf_ctx,
463                          int argc, const char **argv)
464 {
465         int ret = -1;
466         WERROR werr;
467
468         if (argc != 0 || c->display_usage) {
469                 net_conf_drop_usage(c, argc, argv);
470                 goto done;
471         }
472
473         werr = smbconf_drop(conf_ctx);
474         if (!W_ERROR_IS_OK(werr)) {
475                 d_fprintf(stderr, "Error deleting configuration: %s\n",
476                           dos_errstr(werr));
477                 goto done;
478         }
479
480         ret = 0;
481
482 done:
483         return ret;
484 }
485
486 static int net_conf_showshare(struct net_context *c,
487                               struct smbconf_ctx *conf_ctx, int argc,
488                               const char **argv)
489 {
490         int ret = -1;
491         WERROR werr = WERR_OK;
492         const char *sharename = NULL;
493         TALLOC_CTX *mem_ctx;
494         uint32_t count;
495         struct smbconf_service *service = NULL;
496
497         mem_ctx = talloc_stackframe();
498
499         if (argc != 1 || c->display_usage) {
500                 net_conf_showshare_usage(c, argc, argv);
501                 goto done;
502         }
503
504         sharename = talloc_strdup(mem_ctx, argv[0]);
505         if (sharename == NULL) {
506                 d_printf("error: out of memory!\n");
507                 goto done;
508         }
509
510         werr = smbconf_get_share(conf_ctx, mem_ctx, sharename, &service);
511         if (!W_ERROR_IS_OK(werr)) {
512                 d_printf("error getting share parameters: %s\n",
513                          dos_errstr(werr));
514                 goto done;
515         }
516
517         d_printf("[%s]\n", service->name);
518
519         for (count = 0; count < service->num_params; count++) {
520                 d_printf("\t%s = %s\n", service->param_names[count],
521                          service->param_values[count]);
522         }
523
524         ret = 0;
525
526 done:
527         TALLOC_FREE(mem_ctx);
528         return ret;
529 }
530
531 /**
532  * Add a share, with a couple of standard parameters, partly optional.
533  *
534  * This is a high level utility function of the net conf utility,
535  * not a direct frontend to the smbconf API.
536  */
537 static int net_conf_addshare(struct net_context *c,
538                              struct smbconf_ctx *conf_ctx, int argc,
539                              const char **argv)
540 {
541         int ret = -1;
542         WERROR werr = WERR_OK;
543         char *sharename = NULL;
544         const char *path = NULL;
545         const char *comment = NULL;
546         const char *guest_ok = "no";
547         const char *writeable = "no";
548         SMB_STRUCT_STAT sbuf;
549         TALLOC_CTX *mem_ctx = talloc_stackframe();
550
551         if (c->display_usage) {
552                 net_conf_addshare_usage(c, argc, argv);
553                 ret = 0;
554                 goto done;
555         }
556
557         switch (argc) {
558                 case 0:
559                 case 1:
560                 default:
561                         net_conf_addshare_usage(c, argc, argv);
562                         goto done;
563                 case 5:
564                         comment = argv[4];
565                 case 4:
566                         if (!strnequal(argv[3], "guest_ok=", 9)) {
567                                 net_conf_addshare_usage(c, argc, argv);
568                                 goto done;
569                         }
570                         switch (argv[3][9]) {
571                                 case 'y':
572                                 case 'Y':
573                                         guest_ok = "yes";
574                                         break;
575                                 case 'n':
576                                 case 'N':
577                                         guest_ok = "no";
578                                         break;
579                                 default:
580                                         net_conf_addshare_usage(c, argc, argv);
581                                         goto done;
582                         }
583                 case 3:
584                         if (!strnequal(argv[2], "writeable=", 10)) {
585                                 net_conf_addshare_usage(c, argc, argv);
586                                 goto done;
587                         }
588                         switch (argv[2][10]) {
589                                 case 'y':
590                                 case 'Y':
591                                         writeable = "yes";
592                                         break;
593                                 case 'n':
594                                 case 'N':
595                                         writeable = "no";
596                                         break;
597                                 default:
598                                         net_conf_addshare_usage(c, argc, argv);
599                                         goto done;
600                         }
601                 case 2:
602                         path = argv[1];
603                         sharename = talloc_strdup(mem_ctx, argv[0]);
604                         if (sharename == NULL) {
605                                 d_printf("error: out of memory!\n");
606                                 goto done;
607                         }
608
609                         break;
610         }
611
612         /*
613          * validate arguments
614          */
615
616         /* validate share name */
617
618         if (!validate_net_name(sharename, INVALID_SHARENAME_CHARS,
619                                strlen(sharename)))
620         {
621                 d_fprintf(stderr, "ERROR: share name %s contains "
622                         "invalid characters (any of %s)\n",
623                         sharename, INVALID_SHARENAME_CHARS);
624                 goto done;
625         }
626
627         if (strequal(sharename, GLOBAL_NAME)) {
628                 d_fprintf(stderr,
629                           "ERROR: 'global' is not a valid share name.\n");
630                 goto done;
631         }
632
633         if (smbconf_share_exists(conf_ctx, sharename)) {
634                 d_fprintf(stderr, "ERROR: share %s already exists.\n",
635                           sharename);
636                 goto done;
637         }
638
639         /* validate path */
640
641         if (path[0] != '/') {
642                 d_fprintf(stderr,
643                           "Error: path '%s' is not an absolute path.\n",
644                           path);
645                 goto done;
646         }
647
648         if (sys_stat(path, &sbuf) != 0) {
649                 d_fprintf(stderr,
650                           "ERROR: cannot stat path '%s' to ensure "
651                           "this is a directory.\n"
652                           "Error was '%s'.\n",
653                           path, strerror(errno));
654                 goto done;
655         }
656
657         if (!S_ISDIR(sbuf.st_mode)) {
658                 d_fprintf(stderr,
659                           "ERROR: path '%s' is not a directory.\n",
660                           path);
661                 goto done;
662         }
663
664         /*
665          * create the share
666          */
667
668         werr = smbconf_create_share(conf_ctx, sharename);
669         if (!W_ERROR_IS_OK(werr)) {
670                 d_fprintf(stderr, "Error creating share %s: %s\n",
671                           sharename, dos_errstr(werr));
672                 goto done;
673         }
674
675         /*
676          * fill the share with parameters
677          */
678
679         werr = smbconf_set_parameter(conf_ctx, sharename, "path", path);
680         if (!W_ERROR_IS_OK(werr)) {
681                 d_fprintf(stderr, "Error setting parameter %s: %s\n",
682                           "path", dos_errstr(werr));
683                 goto done;
684         }
685
686         if (comment != NULL) {
687                 werr = smbconf_set_parameter(conf_ctx, sharename, "comment",
688                                              comment);
689                 if (!W_ERROR_IS_OK(werr)) {
690                         d_fprintf(stderr, "Error setting parameter %s: %s\n",
691                                   "comment", dos_errstr(werr));
692                         goto done;
693                 }
694         }
695
696         werr = smbconf_set_parameter(conf_ctx, sharename, "guest ok", guest_ok);
697         if (!W_ERROR_IS_OK(werr)) {
698                 d_fprintf(stderr, "Error setting parameter %s: %s\n",
699                           "'guest ok'", dos_errstr(werr));
700                 goto done;
701         }
702
703         werr = smbconf_set_parameter(conf_ctx, sharename, "writeable",
704                                      writeable);
705         if (!W_ERROR_IS_OK(werr)) {
706                 d_fprintf(stderr, "Error setting parameter %s: %s\n",
707                           "writeable", dos_errstr(werr));
708                 goto done;
709         }
710
711         ret = 0;
712
713 done:
714         TALLOC_FREE(mem_ctx);
715         return ret;
716 }
717
718 static int net_conf_delshare(struct net_context *c,
719                              struct smbconf_ctx *conf_ctx, int argc,
720                              const char **argv)
721 {
722         int ret = -1;
723         const char *sharename = NULL;
724         WERROR werr = WERR_OK;
725         TALLOC_CTX *mem_ctx = talloc_stackframe();
726
727         if (argc != 1 || c->display_usage) {
728                 net_conf_delshare_usage(c, argc, argv);
729                 goto done;
730         }
731         sharename = talloc_strdup(mem_ctx, argv[0]);
732         if (sharename == NULL) {
733                 d_printf("error: out of memory!\n");
734                 goto done;
735         }
736
737         werr = smbconf_delete_share(conf_ctx, sharename);
738         if (!W_ERROR_IS_OK(werr)) {
739                 d_fprintf(stderr, "Error deleting share %s: %s\n",
740                           sharename, dos_errstr(werr));
741                 goto done;
742         }
743
744         ret = 0;
745 done:
746         TALLOC_FREE(mem_ctx);
747         return ret;
748 }
749
750 static int net_conf_setparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
751                             int argc, const char **argv)
752 {
753         int ret = -1;
754         WERROR werr = WERR_OK;
755         char *service = NULL;
756         char *param = NULL;
757         const char *value_str = NULL;
758         TALLOC_CTX *mem_ctx = talloc_stackframe();
759
760         if (argc != 3 || c->display_usage) {
761                 net_conf_setparm_usage(c, argc, argv);
762                 goto done;
763         }
764         service = talloc_strdup(mem_ctx, argv[0]);
765         if (service == NULL) {
766                 d_printf("error: out of memory!\n");
767                 goto done;
768         }
769         param = talloc_strdup_lower(mem_ctx, argv[1]);
770         if (param == NULL) {
771                 d_printf("error: out of memory!\n");
772                 goto done;
773         }
774         value_str = argv[2];
775
776         if (!smbconf_share_exists(conf_ctx, service)) {
777                 werr = smbconf_create_share(conf_ctx, service);
778                 if (!W_ERROR_IS_OK(werr)) {
779                         d_fprintf(stderr, "Error creating share '%s': %s\n",
780                                   service, dos_errstr(werr));
781                         goto done;
782                 }
783         }
784
785         werr = smbconf_set_parameter(conf_ctx, service, param, value_str);
786
787         if (!W_ERROR_IS_OK(werr)) {
788                 d_fprintf(stderr, "Error setting value '%s': %s\n",
789                           param, dos_errstr(werr));
790                 goto done;
791         }
792
793         ret = 0;
794
795 done:
796         TALLOC_FREE(mem_ctx);
797         return ret;
798 }
799
800 static int net_conf_getparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
801                             int argc, const char **argv)
802 {
803         int ret = -1;
804         WERROR werr = WERR_OK;
805         char *service = NULL;
806         char *param = NULL;
807         char *valstr = NULL;
808         TALLOC_CTX *mem_ctx;
809
810         mem_ctx = talloc_stackframe();
811
812         if (argc != 2 || c->display_usage) {
813                 net_conf_getparm_usage(c, argc, argv);
814                 goto done;
815         }
816         service = talloc_strdup(mem_ctx, argv[0]);
817         if (service == NULL) {
818                 d_printf("error: out of memory!\n");
819                 goto done;
820         }
821         param = talloc_strdup_lower(mem_ctx, argv[1]);
822         if (param == NULL) {
823                 d_printf("error: out of memory!\n");
824                 goto done;
825         }
826
827         werr = smbconf_get_parameter(conf_ctx, mem_ctx, service, param, &valstr);
828
829         if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
830                 d_fprintf(stderr,
831                           "Error: given service '%s' does not exist.\n",
832                           service);
833                 goto done;
834         } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
835                 d_fprintf(stderr,
836                           "Error: given parameter '%s' is not set.\n",
837                           param);
838                 goto done;
839         } else if (!W_ERROR_IS_OK(werr)) {
840                 d_fprintf(stderr, "Error getting value '%s': %s.\n",
841                           param, dos_errstr(werr));
842                 goto done;
843         }
844
845         d_printf("%s\n", valstr);
846
847         ret = 0;
848 done:
849         TALLOC_FREE(mem_ctx);
850         return ret;
851 }
852
853 static int net_conf_delparm(struct net_context *c, struct smbconf_ctx *conf_ctx,
854                             int argc, const char **argv)
855 {
856         int ret = -1;
857         WERROR werr = WERR_OK;
858         char *service = NULL;
859         char *param = NULL;
860         TALLOC_CTX *mem_ctx = talloc_stackframe();
861
862         if (argc != 2 || c->display_usage) {
863                 net_conf_delparm_usage(c, argc, argv);
864                 goto done;
865         }
866         service = talloc_strdup(mem_ctx, argv[0]);
867         if (service == NULL) {
868                 d_printf("error: out of memory!\n");
869                 goto done;
870         }
871         param = talloc_strdup_lower(mem_ctx, argv[1]);
872         if (param == NULL) {
873                 d_printf("error: out of memory!\n");
874                 goto done;
875         }
876
877         werr = smbconf_delete_parameter(conf_ctx, service, param);
878
879         if (W_ERROR_EQUAL(werr, WERR_NO_SUCH_SERVICE)) {
880                 d_fprintf(stderr,
881                           "Error: given service '%s' does not exist.\n",
882                           service);
883                 goto done;
884         } else if (W_ERROR_EQUAL(werr, WERR_INVALID_PARAM)) {
885                 d_fprintf(stderr,
886                           "Error: given parameter '%s' is not set.\n",
887                           param);
888                 goto done;
889         } else if (!W_ERROR_IS_OK(werr)) {
890                 d_fprintf(stderr, "Error deleting value '%s': %s.\n",
891                           param, dos_errstr(werr));
892                 goto done;
893         }
894
895         ret = 0;
896
897 done:
898         TALLOC_FREE(mem_ctx);
899         return ret;
900 }
901
902 static int net_conf_getincludes(struct net_context *c,
903                                 struct smbconf_ctx *conf_ctx,
904                                 int argc, const char **argv)
905 {
906         WERROR werr;
907         uint32_t num_includes;
908         uint32_t count;
909         char *service;
910         char **includes = NULL;
911         int ret = -1;
912         TALLOC_CTX *mem_ctx = talloc_stackframe();
913
914         if (argc != 1 || c->display_usage) {
915                 net_conf_getincludes_usage(c, argc, argv);
916                 goto done;
917         }
918
919         service = talloc_strdup(mem_ctx, argv[0]);
920         if (service == NULL) {
921                 d_printf("error: out of memory!\n");
922                 goto done;
923         }
924
925         werr = smbconf_get_includes(conf_ctx, mem_ctx, service,
926                                     &num_includes, &includes);
927         if (!W_ERROR_IS_OK(werr)) {
928                 d_printf("error getting includes: %s\n", dos_errstr(werr));
929                 goto done;
930         }
931
932         for (count = 0; count < num_includes; count++) {
933                 d_printf("include = %s\n", includes[count]);
934         }
935
936         ret = 0;
937
938 done:
939         TALLOC_FREE(mem_ctx);
940         return ret;
941 }
942
943 static int net_conf_setincludes(struct net_context *c,
944                                 struct smbconf_ctx *conf_ctx,
945                                 int argc, const char **argv)
946 {
947         WERROR werr;
948         char *service;
949         uint32_t num_includes;
950         const char **includes;
951         int ret = -1;
952         TALLOC_CTX *mem_ctx = talloc_stackframe();
953
954         if (argc < 1 || c->display_usage) {
955                 net_conf_setincludes_usage(c, argc, argv);
956                 goto done;
957         }
958
959         service = talloc_strdup(mem_ctx, argv[0]);
960         if (service == NULL) {
961                 d_printf("error: out of memory!\n");
962                 goto done;
963         }
964
965         num_includes = argc - 1;
966         if (num_includes == 0) {
967                 includes = NULL;
968         } else {
969                 includes = argv + 1;
970         }
971
972         werr = smbconf_set_includes(conf_ctx, service, num_includes, includes);
973         if (!W_ERROR_IS_OK(werr)) {
974                 d_printf("error setting includes: %s\n", dos_errstr(werr));
975                 goto done;
976         }
977
978         ret = 0;
979
980 done:
981         TALLOC_FREE(mem_ctx);
982         return ret;
983 }
984
985 static int net_conf_delincludes(struct net_context *c,
986                                 struct smbconf_ctx *conf_ctx,
987                                 int argc, const char **argv)
988 {
989         WERROR werr;
990         char *service;
991         int ret = -1;
992         TALLOC_CTX *mem_ctx = talloc_stackframe();
993
994         if (argc != 1 || c->display_usage) {
995                 net_conf_delincludes_usage(c, argc, argv);
996                 goto done;
997         }
998
999         service = talloc_strdup(mem_ctx, argv[0]);
1000         if (service == NULL) {
1001                 d_printf("error: out of memory!\n");
1002                 goto done;
1003         }
1004
1005         werr = smbconf_delete_includes(conf_ctx, service);
1006         if (!W_ERROR_IS_OK(werr)) {
1007                 d_printf("error deleting includes: %s\n", dos_errstr(werr));
1008                 goto done;
1009         }
1010
1011         ret = 0;
1012
1013 done:
1014         TALLOC_FREE(mem_ctx);
1015         return ret;
1016 }
1017
1018
1019 /**********************************************************************
1020  *
1021  * Wrapper and net_conf_run_function mechanism.
1022  *
1023  **********************************************************************/
1024
1025 /**
1026  * Wrapper function to call the main conf functions.
1027  * The wrapper calls handles opening and closing of the
1028  * configuration.
1029  */
1030 static int net_conf_wrap_function(struct net_context *c,
1031                                   int (*fn)(struct net_context *,
1032                                             struct smbconf_ctx *,
1033                                             int, const char **),
1034                                   int argc, const char **argv)
1035 {
1036         WERROR werr;
1037         TALLOC_CTX *mem_ctx = talloc_stackframe();
1038         struct smbconf_ctx *conf_ctx;
1039         int ret = -1;
1040
1041         werr = smbconf_init(mem_ctx, &conf_ctx, "registry:");
1042
1043         if (!W_ERROR_IS_OK(werr)) {
1044                 return -1;
1045         }
1046
1047         ret = fn(c, conf_ctx, argc, argv);
1048
1049         smbconf_shutdown(conf_ctx);
1050
1051         return ret;
1052 }
1053
1054 /*
1055  * We need a functable struct of our own, because the
1056  * functions are called through a wrapper that handles
1057  * the opening and closing of the configuration, and so on.
1058  */
1059 struct conf_functable {
1060         const char *funcname;
1061         int (*fn)(struct net_context *c, struct smbconf_ctx *ctx, int argc,
1062                   const char **argv);
1063         int valid_transports;
1064         const char *description;
1065         const char *usage;
1066 };
1067
1068 /**
1069  * This imitates net_run_function but calls the main functions
1070  * through the wrapper net_conf_wrap_function().
1071  */
1072 static int net_conf_run_function(struct net_context *c, int argc,
1073                                  const char **argv, const char *whoami,
1074                                  struct conf_functable *table)
1075 {
1076         int i;
1077
1078         if (argc != 0) {
1079                 for (i=0; table[i].funcname; i++) {
1080                         if (StrCaseCmp(argv[0], table[i].funcname) == 0)
1081                                 return net_conf_wrap_function(c, table[i].fn,
1082                                                               argc-1,
1083                                                               argv+1);
1084                 }
1085         }
1086
1087         d_printf("Usage:\n");
1088         for (i=0; table[i].funcname; i++) {
1089                 if (c->display_usage == false)
1090                         d_printf("%s %-15s %s\n", whoami, table[i].funcname,
1091                                  table[i].description);
1092                 else
1093                         d_printf("%s\n", table[i].usage);
1094         }
1095
1096         return c->display_usage?0:-1;
1097 }
1098
1099 /*
1100  * Entry-point for all the CONF functions.
1101  */
1102
1103 int net_conf(struct net_context *c, int argc, const char **argv)
1104 {
1105         int ret = -1;
1106         struct conf_functable func_table[] = {
1107                 {
1108                         "list",
1109                         net_conf_list,
1110                         NET_TRANSPORT_LOCAL,
1111                         "Dump the complete configuration in smb.conf like "
1112                         "format.",
1113                         "net conf list\n"
1114                         "    Dump the complete configuration in smb.conf like "
1115                         "format."
1116
1117                 },
1118                 {
1119                         "import",
1120                         net_conf_import,
1121                         NET_TRANSPORT_LOCAL,
1122                         "Import configuration from file in smb.conf format.",
1123                         "net conf import\n"
1124                         "    Import configuration from file in smb.conf format."
1125                 },
1126                 {
1127                         "listshares",
1128                         net_conf_listshares,
1129                         NET_TRANSPORT_LOCAL,
1130                         "List the share names.",
1131                         "net conf listshares\n"
1132                         "    List the share names."
1133                 },
1134                 {
1135                         "drop",
1136                         net_conf_drop,
1137                         NET_TRANSPORT_LOCAL,
1138                         "Delete the complete configuration.",
1139                         "net conf drop\n"
1140                         "    Delete the complete configuration."
1141                 },
1142                 {
1143                         "showshare",
1144                         net_conf_showshare,
1145                         NET_TRANSPORT_LOCAL,
1146                         "Show the definition of a share.",
1147                         "net conf showshare\n"
1148                         "    Show the definition of a share."
1149                 },
1150                 {
1151                         "addshare",
1152                         net_conf_addshare,
1153                         NET_TRANSPORT_LOCAL,
1154                         "Create a new share.",
1155                         "net conf addshare\n"
1156                         "    Create a new share."
1157                 },
1158                 {
1159                         "delshare",
1160                         net_conf_delshare,
1161                         NET_TRANSPORT_LOCAL,
1162                         "Delete a share.",
1163                         "net conf delshare\n"
1164                         "    Delete a share."
1165                 },
1166                 {
1167                         "setparm",
1168                         net_conf_setparm,
1169                         NET_TRANSPORT_LOCAL,
1170                         "Store a parameter.",
1171                         "net conf setparm\n"
1172                         "    Store a parameter."
1173                 },
1174                 {
1175                         "getparm",
1176                         net_conf_getparm,
1177                         NET_TRANSPORT_LOCAL,
1178                         "Retrieve the value of a parameter.",
1179                         "net conf getparm\n"
1180                         "    Retrieve the value of a parameter."
1181                 },
1182                 {
1183                         "delparm",
1184                         net_conf_delparm,
1185                         NET_TRANSPORT_LOCAL,
1186                         "Delete a parameter.",
1187                         "net conf delparm\n"
1188                         "    Delete a parameter."
1189                 },
1190                 {
1191                         "getincludes",
1192                         net_conf_getincludes,
1193                         NET_TRANSPORT_LOCAL,
1194                         "Show the includes of a share definition.",
1195                         "net conf getincludes\n"
1196                         "    Show the includes of a share definition."
1197                 },
1198                 {
1199                         "setincludes",
1200                         net_conf_setincludes,
1201                         NET_TRANSPORT_LOCAL,
1202                         "Set includes for a share.",
1203                         "net conf setincludes\n"
1204                         "    Set includes for a share."
1205                 },
1206                 {
1207                         "delincludes",
1208                         net_conf_delincludes,
1209                         NET_TRANSPORT_LOCAL,
1210                         "Delete includes from a share definition.",
1211                         "net conf setincludes\n"
1212                         "    Delete includes from a share definition."
1213                 },
1214                 {NULL, NULL, 0, NULL, NULL}
1215         };
1216
1217         ret = net_conf_run_function(c, argc, argv, "net conf", func_table);
1218
1219         return ret;
1220 }
1221