f6b6d8d46bbcda08524939174b73823dfae1b2e4
[samba.git] / source3 / lib / smbconf / smbconf_reg.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *  libsmbconf - Samba configuration library, registry backend
4  *  Copyright (C) Michael Adam 2008
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "includes.h"
21 #include "lib/smbconf/smbconf_private.h"
22 #include "registry.h"
23 #include "registry/reg_api.h"
24 #include "registry/reg_backend_db.h"
25 #include "registry/reg_util_token.h"
26 #include "registry/reg_api_util.h"
27 #include "registry/reg_init_smbconf.h"
28 #include "lib/smbconf/smbconf_init.h"
29 #include "lib/smbconf/smbconf_reg.h"
30 #include "../libcli/registry/util_reg.h"
31
32 #define INCLUDES_VALNAME "includes"
33
34 struct reg_private_data {
35         struct registry_key *base_key;
36         bool open;              /* did _we_ open the registry? */
37 };
38
39 /**********************************************************************
40  *
41  * helper functions
42  *
43  **********************************************************************/
44
45 /**
46  * a convenience helper to cast the private data structure
47  */
48 static struct reg_private_data *rpd(struct smbconf_ctx *ctx)
49 {
50         return (struct reg_private_data *)(ctx->data);
51 }
52
53 /*
54  * check whether a given value name is forbidden in registry (smbconf)
55  */
56 static bool smbconf_reg_valname_forbidden(const char *valname)
57 {
58         /* hard code the list of forbidden names here for now */
59         const char *forbidden_valnames[] = {
60                 "lock directory",
61                 "lock dir",
62                 "config backend",
63                 "include",
64                 "includes", /* this has a special meaning internally */
65                 NULL
66         };
67         const char **forbidden = NULL;
68
69         for (forbidden = forbidden_valnames; *forbidden != NULL; forbidden++) {
70                 if (strwicmp(valname, *forbidden) == 0) {
71                         return true;
72                 }
73         }
74         return false;
75 }
76
77 static bool smbconf_reg_valname_valid(const char *valname)
78 {
79         return (!smbconf_reg_valname_forbidden(valname) &&
80                 lp_parameter_is_valid(valname));
81 }
82
83 /**
84  * Open a subkey of the base key (i.e a service)
85  */
86 static WERROR smbconf_reg_open_service_key(TALLOC_CTX *mem_ctx,
87                                            struct smbconf_ctx *ctx,
88                                            const char *servicename,
89                                            uint32 desired_access,
90                                            struct registry_key **key)
91 {
92         WERROR werr;
93
94         if (servicename == NULL) {
95                 *key = rpd(ctx)->base_key;
96                 return WERR_OK;
97         }
98         werr = reg_openkey(mem_ctx, rpd(ctx)->base_key, servicename,
99                            desired_access, key);
100
101         if (W_ERROR_EQUAL(werr, WERR_BADFILE)) {
102                 werr = WERR_NO_SUCH_SERVICE;
103         }
104
105         return werr;
106 }
107
108 /**
109  * check if a value exists in a given registry key
110  */
111 static bool smbconf_value_exists(struct registry_key *key, const char *param)
112 {
113         bool ret = false;
114         WERROR werr = WERR_OK;
115         TALLOC_CTX *ctx = talloc_stackframe();
116         struct registry_value *value = NULL;
117
118         werr = reg_queryvalue(ctx, key, param, &value);
119         if (W_ERROR_IS_OK(werr)) {
120                 ret = true;
121         }
122
123         talloc_free(ctx);
124         return ret;
125 }
126
127 /**
128  * create a subkey of the base key (i.e. a service...)
129  */
130 static WERROR smbconf_reg_create_service_key(TALLOC_CTX *mem_ctx,
131                                              struct smbconf_ctx *ctx,
132                                              const char * subkeyname,
133                                              struct registry_key **newkey)
134 {
135         WERROR werr = WERR_OK;
136         TALLOC_CTX *create_ctx;
137         enum winreg_CreateAction action = REG_ACTION_NONE;
138
139         /* create a new talloc ctx for creation. it will hold
140          * the intermediate parent key (SMBCONF) for creation
141          * and will be destroyed when leaving this function... */
142         create_ctx = talloc_stackframe();
143
144         werr = reg_createkey(mem_ctx, rpd(ctx)->base_key, subkeyname,
145                              REG_KEY_WRITE, newkey, &action);
146         if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
147                 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
148                 werr = WERR_FILE_EXISTS;
149         }
150         if (!W_ERROR_IS_OK(werr)) {
151                 DEBUG(5, ("Error creating key %s: %s\n",
152                          subkeyname, win_errstr(werr)));
153         }
154
155         talloc_free(create_ctx);
156         return werr;
157 }
158
159 /**
160  * add a value to a key.
161  */
162 static WERROR smbconf_reg_set_value(struct registry_key *key,
163                                     const char *valname,
164                                     const char *valstr)
165 {
166         struct registry_value val;
167         WERROR werr = WERR_OK;
168         char *subkeyname;
169         const char *canon_valname;
170         const char *canon_valstr;
171
172         if (!lp_canonicalize_parameter_with_value(valname, valstr,
173                                                   &canon_valname,
174                                                   &canon_valstr))
175         {
176                 if (canon_valname == NULL) {
177                         DEBUG(5, ("invalid parameter '%s' given\n",
178                                   valname));
179                 } else {
180                         DEBUG(5, ("invalid value '%s' given for "
181                                   "parameter '%s'\n", valstr, valname));
182                 }
183                 werr = WERR_INVALID_PARAM;
184                 goto done;
185         }
186
187         if (smbconf_reg_valname_forbidden(canon_valname)) {
188                 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
189                           canon_valname));
190                 werr = WERR_INVALID_PARAM;
191                 goto done;
192         }
193
194         subkeyname = strrchr_m(key->key->name, '\\');
195         if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
196                 DEBUG(5, ("Invalid registry key '%s' given as "
197                           "smbconf section.\n", key->key->name));
198                 werr = WERR_INVALID_PARAM;
199                 goto done;
200         }
201         subkeyname++;
202         if (!strequal(subkeyname, GLOBAL_NAME) &&
203             lp_parameter_is_global(valname))
204         {
205                 DEBUG(5, ("Global parameter '%s' not allowed in "
206                           "service definition ('%s').\n", canon_valname,
207                           subkeyname));
208                 werr = WERR_INVALID_PARAM;
209                 goto done;
210         }
211
212         ZERO_STRUCT(val);
213
214         val.type = REG_SZ;
215         if (!push_reg_sz(talloc_tos(), &val.data, canon_valstr)) {
216                 werr = WERR_NOMEM;
217                 goto done;
218         }
219
220         werr = reg_setvalue(key, canon_valname, &val);
221         if (!W_ERROR_IS_OK(werr)) {
222                 DEBUG(5, ("Error adding value '%s' to "
223                           "key '%s': %s\n",
224                           canon_valname, key->key->name, win_errstr(werr)));
225         }
226
227 done:
228         return werr;
229 }
230
231 static WERROR smbconf_reg_set_multi_sz_value(struct registry_key *key,
232                                              const char *valname,
233                                              const uint32_t num_strings,
234                                              const char **strings)
235 {
236         WERROR werr;
237         struct registry_value *value;
238         uint32_t count;
239         TALLOC_CTX *tmp_ctx = talloc_stackframe();
240         const char **array;
241
242         if (strings == NULL) {
243                 werr = WERR_INVALID_PARAM;
244                 goto done;
245         }
246
247         array = talloc_zero_array(tmp_ctx, const char *, num_strings + 1);
248         if (array == NULL) {
249                 werr = WERR_NOMEM;
250                 goto done;
251         }
252
253         value = TALLOC_ZERO_P(tmp_ctx, struct registry_value);
254         if (value == NULL) {
255                 werr = WERR_NOMEM;
256                 goto done;
257         }
258
259         value->type = REG_MULTI_SZ;
260
261         for (count = 0; count < num_strings; count++) {
262                 array[count] = talloc_strdup(value, strings[count]);
263                 if (array[count] == NULL) {
264                         werr = WERR_NOMEM;
265                         goto done;
266                 }
267         }
268
269         if (!push_reg_multi_sz(value, &value->data, array)) {
270                 werr = WERR_NOMEM;
271                 goto done;
272         }
273
274         werr = reg_setvalue(key, valname, value);
275         if (!W_ERROR_IS_OK(werr)) {
276                 DEBUG(5, ("Error adding value '%s' to key '%s': %s\n",
277                           valname, key->key->name, win_errstr(werr)));
278         }
279
280 done:
281         talloc_free(tmp_ctx);
282         return werr;
283 }
284
285 /**
286  * format a registry_value into a string.
287  *
288  * This is intended to be used for smbconf registry values,
289  * which are ar stored as REG_SZ values, so the incomplete
290  * handling should be ok.
291  */
292 static char *smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
293                                            struct registry_value *value)
294 {
295         char *result = NULL;
296
297         /* alternatively, create a new talloc context? */
298         if (mem_ctx == NULL) {
299                 return result;
300         }
301
302         switch (value->type) {
303         case REG_DWORD:
304                 if (value->data.length >= 4) {
305                         uint32_t v = IVAL(value->data.data, 0);
306                         result = talloc_asprintf(mem_ctx, "%d", v);
307                 }
308                 break;
309         case REG_SZ:
310         case REG_EXPAND_SZ: {
311                 const char *s;
312                 if (!pull_reg_sz(mem_ctx, &value->data, &s)) {
313                         break;
314                 }
315                 result = talloc_strdup(mem_ctx, s);
316                 break;
317         }
318         case REG_MULTI_SZ: {
319                 uint32 j;
320                 const char **a = NULL;
321                 if (!pull_reg_multi_sz(mem_ctx, &value->data, &a)) {
322                         break;
323                 }
324                 for (j = 0; a[j] != NULL; j++) {
325                         result = talloc_asprintf(mem_ctx, "%s\"%s\" ",
326                                                  result ? result : "" ,
327                                                  a[j]);
328                         if (result == NULL) {
329                                 break;
330                         }
331                 }
332                 break;
333         }
334         case REG_BINARY:
335                 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
336                                          (int)value->data.length);
337                 break;
338         default:
339                 result = talloc_asprintf(mem_ctx, "<unprintable>");
340                 break;
341         }
342         return result;
343 }
344
345 static WERROR smbconf_reg_get_includes_internal(TALLOC_CTX *mem_ctx,
346                                                 struct registry_key *key,
347                                                 uint32_t *num_includes,
348                                                 char ***includes)
349 {
350         WERROR werr;
351         sbcErr err;
352         uint32_t count;
353         struct registry_value *value = NULL;
354         char **tmp_includes = NULL;
355         const char **array = NULL;
356         TALLOC_CTX *tmp_ctx = talloc_stackframe();
357
358         if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
359                 /* no includes */
360                 *num_includes = 0;
361                 *includes = NULL;
362                 werr = WERR_OK;
363                 goto done;
364         }
365
366         werr = reg_queryvalue(tmp_ctx, key, INCLUDES_VALNAME, &value);
367         if (!W_ERROR_IS_OK(werr)) {
368                 goto done;
369         }
370
371         if (value->type != REG_MULTI_SZ) {
372                 /* wrong type -- ignore */
373                 goto done;
374         }
375
376         if (!pull_reg_multi_sz(tmp_ctx, &value->data, &array)) {
377                 werr = WERR_NOMEM;
378                 goto done;
379         }
380
381         for (count = 0; array[count] != NULL; count++) {
382                 err = smbconf_add_string_to_array(tmp_ctx,
383                                         &tmp_includes,
384                                         count,
385                                         array[count]);
386                 if (!SBC_ERROR_IS_OK(err)) {
387                         werr = WERR_NOMEM;
388                         goto done;
389                 }
390         }
391
392         if (count > 0) {
393                 *includes = talloc_move(mem_ctx, &tmp_includes);
394                 if (*includes == NULL) {
395                         werr = WERR_NOMEM;
396                         goto done;
397                 }
398                 *num_includes = count;
399         } else {
400                 *num_includes = 0;
401                 *includes = NULL;
402         }
403
404 done:
405         talloc_free(tmp_ctx);
406         return werr;
407 }
408
409 /**
410  * Get the values of a key as a list of value names
411  * and a list of value strings (ordered)
412  */
413 static WERROR smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
414                                      struct registry_key *key,
415                                      uint32_t *num_values,
416                                      char ***value_names,
417                                      char ***value_strings)
418 {
419         TALLOC_CTX *tmp_ctx = NULL;
420         WERROR werr = WERR_OK;
421         sbcErr err;
422         uint32_t count;
423         struct registry_value *valvalue = NULL;
424         char *valname = NULL;
425         uint32_t tmp_num_values = 0;
426         char **tmp_valnames = NULL;
427         char **tmp_valstrings = NULL;
428         uint32_t num_includes = 0;
429         char **includes = NULL;
430
431         if ((num_values == NULL) || (value_names == NULL) ||
432             (value_strings == NULL))
433         {
434                 werr = WERR_INVALID_PARAM;
435                 goto done;
436         }
437
438         tmp_ctx = talloc_stackframe();
439
440         for (count = 0;
441              werr = reg_enumvalue(tmp_ctx, key, count, &valname, &valvalue),
442              W_ERROR_IS_OK(werr);
443              count++)
444         {
445                 char *valstring;
446
447                 if (!smbconf_reg_valname_valid(valname)) {
448                         continue;
449                 }
450
451                 err = smbconf_add_string_to_array(tmp_ctx,
452                                                   &tmp_valnames,
453                                                   tmp_num_values, valname);
454                 if (!SBC_ERROR_IS_OK(err)) {
455                         werr = WERR_NOMEM;
456                         goto done;
457                 }
458
459                 valstring = smbconf_format_registry_value(tmp_ctx, valvalue);
460                 err = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
461                                                   tmp_num_values, valstring);
462                 if (!SBC_ERROR_IS_OK(err)) {
463                         werr = WERR_NOMEM;
464                         goto done;
465                 }
466                 tmp_num_values++;
467         }
468         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
469                 goto done;
470         }
471
472         /* now add the includes at the end */
473         werr = smbconf_reg_get_includes_internal(tmp_ctx, key, &num_includes,
474                                                  &includes);
475         if (!W_ERROR_IS_OK(werr)) {
476                 goto done;
477         }
478         for (count = 0; count < num_includes; count++) {
479                 err = smbconf_add_string_to_array(tmp_ctx, &tmp_valnames,
480                                                   tmp_num_values, "include");
481                 if (!SBC_ERROR_IS_OK(err)) {
482                         werr = WERR_NOMEM;
483                         goto done;
484                 }
485
486                 err = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
487                                                   tmp_num_values,
488                                                   includes[count]);
489                 if (!SBC_ERROR_IS_OK(err)) {
490                         werr = WERR_NOMEM;
491                         goto done;
492                 }
493
494                 tmp_num_values++;
495         }
496
497         *num_values = tmp_num_values;
498         if (tmp_num_values > 0) {
499                 *value_names = talloc_move(mem_ctx, &tmp_valnames);
500                 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
501         } else {
502                 *value_names = NULL;
503                 *value_strings = NULL;
504         }
505
506 done:
507         talloc_free(tmp_ctx);
508         return werr;
509 }
510
511 static bool smbconf_reg_key_has_values(struct registry_key *key)
512 {
513         WERROR werr;
514         uint32_t num_subkeys;
515         uint32_t max_subkeylen;
516         uint32_t max_subkeysize;
517         uint32_t num_values;
518         uint32_t max_valnamelen;
519         uint32_t max_valbufsize;
520         uint32_t secdescsize;
521         NTTIME last_changed_time;
522
523         werr = reg_queryinfokey(key, &num_subkeys, &max_subkeylen,
524                                 &max_subkeysize, &num_values, &max_valnamelen,
525                                 &max_valbufsize, &secdescsize,
526                                 &last_changed_time);
527         if (!W_ERROR_IS_OK(werr)) {
528                 return false;
529         }
530
531         return (num_values != 0);
532 }
533
534 /**
535  * delete all values from a key
536  */
537 static WERROR smbconf_reg_delete_values(struct registry_key *key)
538 {
539         WERROR werr;
540         char *valname;
541         struct registry_value *valvalue;
542         uint32_t count;
543         TALLOC_CTX *mem_ctx = talloc_stackframe();
544
545         for (count = 0;
546              werr = reg_enumvalue(mem_ctx, key, count, &valname, &valvalue),
547              W_ERROR_IS_OK(werr);
548              count++)
549         {
550                 werr = reg_deletevalue(key, valname);
551                 if (!W_ERROR_IS_OK(werr)) {
552                         goto done;
553                 }
554         }
555         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
556                 DEBUG(1, ("smbconf_reg_delete_values: "
557                           "Error enumerating values of %s: %s\n",
558                           key->key->name,
559                           win_errstr(werr)));
560                 goto done;
561         }
562
563         werr = WERR_OK;
564
565 done:
566         talloc_free(mem_ctx);
567         return werr;
568 }
569
570 /**********************************************************************
571  *
572  * smbconf operations: registry implementations
573  *
574  **********************************************************************/
575
576 /**
577  * initialize the registry smbconf backend
578  */
579 static sbcErr smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
580 {
581         WERROR werr = WERR_OK;
582         sbcErr err;
583         struct security_token *token;
584
585         if (path == NULL) {
586                 path = KEY_SMBCONF;
587         }
588         ctx->path = talloc_strdup(ctx, path);
589         if (ctx->path == NULL) {
590                 werr = WERR_NOMEM;
591                 goto done;
592         }
593
594         ctx->data = TALLOC_ZERO_P(ctx, struct reg_private_data);
595
596         werr = ntstatus_to_werror(registry_create_admin_token(ctx, &token));
597         if (!W_ERROR_IS_OK(werr)) {
598                 DEBUG(1, ("Error creating admin token\n"));
599                 err = SBC_ERR_UNKNOWN_FAILURE;
600                 goto done;
601         }
602         rpd(ctx)->open = false;
603
604         werr = registry_init_smbconf(path);
605         if (!W_ERROR_IS_OK(werr)) {
606                 err = SBC_ERR_BADFILE;
607                 goto done;
608         }
609
610         err = ctx->ops->open_conf(ctx);
611         if (!SBC_ERROR_IS_OK(err)) {
612                 DEBUG(1, ("Error opening the registry.\n"));
613                 goto done;
614         }
615
616         werr = reg_open_path(ctx, ctx->path,
617                              KEY_ENUMERATE_SUB_KEYS | REG_KEY_WRITE,
618                              token, &rpd(ctx)->base_key);
619         if (!W_ERROR_IS_OK(werr)) {
620                 err = SBC_ERR_UNKNOWN_FAILURE;
621                 goto done;
622         }
623
624 done:
625         return err;
626 }
627
628 static int smbconf_reg_shutdown(struct smbconf_ctx *ctx)
629 {
630         return ctx->ops->close_conf(ctx);
631 }
632
633 static bool smbconf_reg_requires_messaging(struct smbconf_ctx *ctx)
634 {
635 #ifdef CLUSTER_SUPPORT
636         if (lp_clustering() && lp_parm_bool(-1, "ctdb", "registry.tdb", true)) {
637                 return true;
638         }
639 #endif
640         return false;
641 }
642
643 static bool smbconf_reg_is_writeable(struct smbconf_ctx *ctx)
644 {
645         /*
646          * The backend has write support.
647          *
648          *  TODO: add access checks whether the concrete
649          *  config source is really writeable by the calling user.
650          */
651         return true;
652 }
653
654 static sbcErr smbconf_reg_open(struct smbconf_ctx *ctx)
655 {
656         WERROR werr;
657
658         if (rpd(ctx)->open) {
659                 return SBC_ERR_OK;
660         }
661
662         werr = regdb_open();
663         if (!W_ERROR_IS_OK(werr)) {
664                 return SBC_ERR_BADFILE;
665         }
666
667         rpd(ctx)->open = true;
668         return SBC_ERR_OK;
669 }
670
671 static int smbconf_reg_close(struct smbconf_ctx *ctx)
672 {
673         int ret;
674
675         if (!rpd(ctx)->open) {
676                 return 0;
677         }
678
679         ret = regdb_close();
680         if (ret == 0) {
681                 rpd(ctx)->open = false;
682         }
683         return ret;
684 }
685
686 /**
687  * Get the change sequence number of the given service/parameter.
688  * service and parameter strings may be NULL.
689  */
690 static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
691                                 struct smbconf_csn *csn,
692                                 const char *service, const char *param)
693 {
694         if (csn == NULL) {
695                 return;
696         }
697
698         if (!SBC_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
699                 return;
700         }
701
702         csn->csn = (uint64_t)regdb_get_seqnum();
703 }
704
705 /**
706  * Drop the whole configuration (restarting empty) - registry version
707  */
708 static sbcErr smbconf_reg_drop(struct smbconf_ctx *ctx)
709 {
710         char *path, *p;
711         WERROR werr = WERR_OK;
712         sbcErr err = SBC_ERR_OK;
713         struct registry_key *parent_key = NULL;
714         struct registry_key *new_key = NULL;
715         TALLOC_CTX* mem_ctx = talloc_stackframe();
716         enum winreg_CreateAction action;
717         struct security_token *token;
718
719         werr = ntstatus_to_werror(registry_create_admin_token(ctx, &token));
720         if (!W_ERROR_IS_OK(werr)) {
721                 DEBUG(1, ("Error creating admin token\n"));
722                 err = SBC_ERR_UNKNOWN_FAILURE;
723                 goto done;
724         }
725
726         path = talloc_strdup(mem_ctx, ctx->path);
727         if (path == NULL) {
728                 err = SBC_ERR_NOMEM;
729                 goto done;
730         }
731         p = strrchr(path, '\\');
732         if (p == NULL) {
733                 err = SBC_ERR_INVALID_PARAM;
734                 goto done;
735         }
736         *p = '\0';
737         werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token,
738                              &parent_key);
739         if (!W_ERROR_IS_OK(werr)) {
740                 err = SBC_ERR_IO_FAILURE;
741                 goto done;
742         }
743
744         werr = reg_deletekey_recursive(parent_key, p+1);
745         if (!W_ERROR_IS_OK(werr)) {
746                 err = SBC_ERR_IO_FAILURE;
747                 goto done;
748         }
749
750         werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
751                              &new_key, &action);
752         if (!W_ERROR_IS_OK(werr)) {
753                 err = SBC_ERR_IO_FAILURE;
754                 goto done;
755         }
756
757 done:
758         talloc_free(mem_ctx);
759         return err;
760 }
761
762 /**
763  * get the list of share names defined in the configuration.
764  * registry version.
765  */
766 static sbcErr smbconf_reg_get_share_names(struct smbconf_ctx *ctx,
767                                           TALLOC_CTX *mem_ctx,
768                                           uint32_t *num_shares,
769                                           char ***share_names)
770 {
771         uint32_t count;
772         uint32_t added_count = 0;
773         TALLOC_CTX *tmp_ctx = NULL;
774         WERROR werr;
775         sbcErr err = SBC_ERR_OK;
776         char *subkey_name = NULL;
777         char **tmp_share_names = NULL;
778
779         if ((num_shares == NULL) || (share_names == NULL)) {
780                 return SBC_ERR_INVALID_PARAM;
781         }
782
783         tmp_ctx = talloc_stackframe();
784
785         /* if there are values in the base key, return NULL as share name */
786
787         if (smbconf_reg_key_has_values(rpd(ctx)->base_key)) {
788                 err = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
789                                                    0, NULL);
790                 if (!SBC_ERROR_IS_OK(err)) {
791                         goto done;
792                 }
793                 added_count++;
794         }
795
796         /* make sure "global" is always listed first */
797         if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
798                 err = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
799                                                   added_count, GLOBAL_NAME);
800                 if (!SBC_ERROR_IS_OK(err)) {
801                         goto done;
802                 }
803                 added_count++;
804         }
805
806         for (count = 0;
807              werr = reg_enumkey(tmp_ctx, rpd(ctx)->base_key, count,
808                                 &subkey_name, NULL),
809              W_ERROR_IS_OK(werr);
810              count++)
811         {
812                 if (strequal(subkey_name, GLOBAL_NAME)) {
813                         continue;
814                 }
815
816                 err = smbconf_add_string_to_array(tmp_ctx,
817                                                    &tmp_share_names,
818                                                    added_count,
819                                                    subkey_name);
820                 if (!SBC_ERROR_IS_OK(err)) {
821                         goto done;
822                 }
823                 added_count++;
824         }
825         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
826                 err = SBC_ERR_NO_MORE_ITEMS;
827                 goto done;
828         }
829         err = SBC_ERR_OK;
830
831         *num_shares = added_count;
832         if (added_count > 0) {
833                 *share_names = talloc_move(mem_ctx, &tmp_share_names);
834         } else {
835                 *share_names = NULL;
836         }
837
838 done:
839         talloc_free(tmp_ctx);
840         return err;
841 }
842
843 /**
844  * check if a share/service of a given name exists - registry version
845  */
846 static bool smbconf_reg_share_exists(struct smbconf_ctx *ctx,
847                                      const char *servicename)
848 {
849         bool ret = false;
850         WERROR werr = WERR_OK;
851         TALLOC_CTX *mem_ctx = talloc_stackframe();
852         struct registry_key *key = NULL;
853
854         werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
855                                             REG_KEY_READ, &key);
856         if (W_ERROR_IS_OK(werr)) {
857                 ret = true;
858         }
859
860         talloc_free(mem_ctx);
861         return ret;
862 }
863
864 /**
865  * Add a service if it does not already exist - registry version
866  */
867 static WERROR smbconf_reg_create_share(struct smbconf_ctx *ctx,
868                                        const char *servicename)
869 {
870         WERROR werr;
871         struct registry_key *key = NULL;
872
873         if (servicename == NULL) {
874                 return WERR_OK;
875         }
876
877         werr = smbconf_reg_create_service_key(talloc_tos(), ctx,
878                                               servicename, &key);
879
880         talloc_free(key);
881         return werr;
882 }
883
884 /**
885  * get a definition of a share (service) from configuration.
886  */
887 static WERROR smbconf_reg_get_share(struct smbconf_ctx *ctx,
888                                     TALLOC_CTX *mem_ctx,
889                                     const char *servicename,
890                                     struct smbconf_service **service)
891 {
892         WERROR werr = WERR_OK;
893         struct registry_key *key = NULL;
894         struct smbconf_service *tmp_service = NULL;
895         TALLOC_CTX *tmp_ctx = talloc_stackframe();
896
897         werr = smbconf_reg_open_service_key(tmp_ctx, ctx, servicename,
898                                             REG_KEY_READ, &key);
899         if (!W_ERROR_IS_OK(werr)) {
900                 goto done;
901         }
902
903         tmp_service = TALLOC_ZERO_P(tmp_ctx, struct smbconf_service);
904         if (tmp_service == NULL) {
905                 werr =  WERR_NOMEM;
906                 goto done;
907         }
908
909         if (servicename != NULL) {
910                 tmp_service->name = talloc_strdup(tmp_service, servicename);
911                 if (tmp_service->name == NULL) {
912                         werr = WERR_NOMEM;
913                         goto done;
914                 }
915         }
916
917         werr = smbconf_reg_get_values(tmp_service, key,
918                                       &(tmp_service->num_params),
919                                       &(tmp_service->param_names),
920                                       &(tmp_service->param_values));
921
922         if (W_ERROR_IS_OK(werr)) {
923                 *service = talloc_move(mem_ctx, &tmp_service);
924         }
925
926 done:
927         talloc_free(tmp_ctx);
928         return werr;
929 }
930
931 /**
932  * delete a service from configuration
933  */
934 static WERROR smbconf_reg_delete_share(struct smbconf_ctx *ctx,
935                                        const char *servicename)
936 {
937         WERROR werr = WERR_OK;
938         TALLOC_CTX *mem_ctx = talloc_stackframe();
939
940         if (servicename != NULL) {
941                 werr = reg_deletekey_recursive(rpd(ctx)->base_key, servicename);
942         } else {
943                 werr = smbconf_reg_delete_values(rpd(ctx)->base_key);
944         }
945
946         talloc_free(mem_ctx);
947         return werr;
948 }
949
950 /**
951  * set a configuration parameter to the value provided.
952  */
953 static WERROR smbconf_reg_set_parameter(struct smbconf_ctx *ctx,
954                                         const char *service,
955                                         const char *param,
956                                         const char *valstr)
957 {
958         WERROR werr;
959         struct registry_key *key = NULL;
960         TALLOC_CTX *mem_ctx = talloc_stackframe();
961
962         werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
963                                             REG_KEY_WRITE, &key);
964         if (!W_ERROR_IS_OK(werr)) {
965                 goto done;
966         }
967
968         werr = smbconf_reg_set_value(key, param, valstr);
969
970 done:
971         talloc_free(mem_ctx);
972         return werr;
973 }
974
975 /**
976  * get the value of a configuration parameter as a string
977  */
978 static WERROR smbconf_reg_get_parameter(struct smbconf_ctx *ctx,
979                                         TALLOC_CTX *mem_ctx,
980                                         const char *service,
981                                         const char *param,
982                                         char **valstr)
983 {
984         WERROR werr = WERR_OK;
985         struct registry_key *key = NULL;
986         struct registry_value *value = NULL;
987
988         werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
989                                             REG_KEY_READ, &key);
990         if (!W_ERROR_IS_OK(werr)) {
991                 goto done;
992         }
993
994         if (!smbconf_reg_valname_valid(param)) {
995                 werr = WERR_INVALID_PARAM;
996                 goto done;
997         }
998
999         if (!smbconf_value_exists(key, param)) {
1000                 werr = WERR_INVALID_PARAM;
1001                 goto done;
1002         }
1003
1004         werr = reg_queryvalue(mem_ctx, key, param, &value);
1005         if (!W_ERROR_IS_OK(werr)) {
1006                 goto done;
1007         }
1008
1009         *valstr = smbconf_format_registry_value(mem_ctx, value);
1010
1011         if (*valstr == NULL) {
1012                 werr = WERR_NOMEM;
1013         }
1014
1015 done:
1016         talloc_free(key);
1017         talloc_free(value);
1018         return werr;
1019 }
1020
1021 /**
1022  * delete a parameter from configuration
1023  */
1024 static WERROR smbconf_reg_delete_parameter(struct smbconf_ctx *ctx,
1025                                            const char *service,
1026                                            const char *param)
1027 {
1028         struct registry_key *key = NULL;
1029         WERROR werr = WERR_OK;
1030         TALLOC_CTX *mem_ctx = talloc_stackframe();
1031
1032         werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
1033                                             REG_KEY_ALL, &key);
1034         if (!W_ERROR_IS_OK(werr)) {
1035                 goto done;
1036         }
1037
1038         if (!smbconf_reg_valname_valid(param)) {
1039                 werr = WERR_INVALID_PARAM;
1040                 goto done;
1041         }
1042
1043         if (!smbconf_value_exists(key, param)) {
1044                 werr = WERR_INVALID_PARAM;
1045                 goto done;
1046         }
1047
1048         werr = reg_deletevalue(key, param);
1049
1050 done:
1051         talloc_free(mem_ctx);
1052         return werr;
1053 }
1054
1055 static WERROR smbconf_reg_get_includes(struct smbconf_ctx *ctx,
1056                                        TALLOC_CTX *mem_ctx,
1057                                        const char *service,
1058                                        uint32_t *num_includes,
1059                                        char ***includes)
1060 {
1061         WERROR werr;
1062         struct registry_key *key = NULL;
1063         TALLOC_CTX *tmp_ctx = talloc_stackframe();
1064
1065         werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1066                                             REG_KEY_READ, &key);
1067         if (!W_ERROR_IS_OK(werr)) {
1068                 goto done;
1069         }
1070
1071         werr = smbconf_reg_get_includes_internal(mem_ctx, key, num_includes,
1072                                                  includes);
1073
1074 done:
1075         talloc_free(tmp_ctx);
1076         return werr;
1077 }
1078
1079 static WERROR smbconf_reg_set_includes(struct smbconf_ctx *ctx,
1080                                        const char *service,
1081                                        uint32_t num_includes,
1082                                        const char **includes)
1083 {
1084         WERROR werr = WERR_OK;
1085         struct registry_key *key = NULL;
1086         TALLOC_CTX *tmp_ctx = talloc_stackframe();
1087
1088         werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1089                                             REG_KEY_ALL, &key);
1090         if (!W_ERROR_IS_OK(werr)) {
1091                 goto done;
1092         }
1093
1094         if (num_includes == 0) {
1095                 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
1096                         goto done;
1097                 }
1098                 werr = reg_deletevalue(key, INCLUDES_VALNAME);
1099         } else {
1100                 werr = smbconf_reg_set_multi_sz_value(key, INCLUDES_VALNAME,
1101                                                       num_includes, includes);
1102         }
1103
1104 done:
1105         talloc_free(tmp_ctx);
1106         return werr;
1107 }
1108
1109 static WERROR smbconf_reg_delete_includes(struct smbconf_ctx *ctx,
1110                                           const char *service)
1111 {
1112         WERROR werr = WERR_OK;
1113         struct registry_key *key = NULL;
1114         TALLOC_CTX *tmp_ctx = talloc_stackframe();
1115
1116         werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1117                                             REG_KEY_ALL, &key);
1118         if (!W_ERROR_IS_OK(werr)) {
1119                 goto done;
1120         }
1121
1122         if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
1123                 goto done;
1124         }
1125
1126         werr = reg_deletevalue(key, INCLUDES_VALNAME);
1127
1128
1129 done:
1130         talloc_free(tmp_ctx);
1131         return werr;
1132 }
1133
1134 static WERROR smbconf_reg_transaction_start(struct smbconf_ctx *ctx)
1135 {
1136         return regdb_transaction_start();
1137 }
1138
1139 static WERROR smbconf_reg_transaction_commit(struct smbconf_ctx *ctx)
1140 {
1141         return regdb_transaction_commit();
1142 }
1143
1144 static WERROR smbconf_reg_transaction_cancel(struct smbconf_ctx *ctx)
1145 {
1146         return regdb_transaction_cancel();
1147 }
1148
1149 struct smbconf_ops smbconf_ops_reg = {
1150         .init                   = smbconf_reg_init,
1151         .shutdown               = smbconf_reg_shutdown,
1152         .requires_messaging     = smbconf_reg_requires_messaging,
1153         .is_writeable           = smbconf_reg_is_writeable,
1154         .open_conf              = smbconf_reg_open,
1155         .close_conf             = smbconf_reg_close,
1156         .get_csn                = smbconf_reg_get_csn,
1157         .drop                   = smbconf_reg_drop,
1158         .get_share_names        = smbconf_reg_get_share_names,
1159         .share_exists           = smbconf_reg_share_exists,
1160         .create_share           = smbconf_reg_create_share,
1161         .get_share              = smbconf_reg_get_share,
1162         .delete_share           = smbconf_reg_delete_share,
1163         .set_parameter          = smbconf_reg_set_parameter,
1164         .get_parameter          = smbconf_reg_get_parameter,
1165         .delete_parameter       = smbconf_reg_delete_parameter,
1166         .get_includes           = smbconf_reg_get_includes,
1167         .set_includes           = smbconf_reg_set_includes,
1168         .delete_includes        = smbconf_reg_delete_includes,
1169         .transaction_start      = smbconf_reg_transaction_start,
1170         .transaction_commit     = smbconf_reg_transaction_commit,
1171         .transaction_cancel     = smbconf_reg_transaction_cancel,
1172 };
1173
1174
1175 /**
1176  * initialize the smbconf registry backend
1177  * the only function that is exported from this module
1178  */
1179 sbcErr smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
1180                         const char *path)
1181 {
1182         return smbconf_init_internal(mem_ctx, conf_ctx, path, &smbconf_ops_reg);
1183 }