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