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