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