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