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