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