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