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