libsmbconf: consider "include" a forbidden parameter in regisry config again.
[ddiss/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 "smbconf_private.h"
22
23 #define INCLUDES_VALNAME "includes"
24
25 struct reg_private_data {
26         NT_USER_TOKEN *token;
27         bool open;              /* did _we_ open the registry? */
28 };
29
30 /**********************************************************************
31  *
32  * helper functions
33  *
34  **********************************************************************/
35
36 /**
37  * a convenience helper to cast the private data structure
38  */
39 static struct reg_private_data *rpd(struct smbconf_ctx *ctx)
40 {
41         return (struct reg_private_data *)(ctx->data);
42 }
43
44 /*
45  * check whether a given value name is forbidden in registry (smbconf)
46  */
47 static bool smbconf_reg_valname_forbidden(const char *valname)
48 {
49         /* hard code the list of forbidden names here for now */
50         const char *forbidden_valnames[] = {
51                 "lock directory",
52                 "lock dir",
53                 "config backend",
54                 "include",
55                 NULL
56         };
57         const char **forbidden = NULL;
58
59         for (forbidden = forbidden_valnames; *forbidden != NULL; forbidden++) {
60                 if (strwicmp(valname, *forbidden) == 0) {
61                         return true;
62                 }
63         }
64         return false;
65 }
66
67 static bool smbconf_reg_valname_valid(const char *valname)
68 {
69         return (lp_parameter_is_valid(valname) &&
70                 !smbconf_reg_valname_forbidden(valname));
71 }
72
73 /**
74  * Open a registry key specified by "path"
75  */
76 static WERROR smbconf_reg_open_path(TALLOC_CTX *mem_ctx,
77                                     struct smbconf_ctx *ctx,
78                                     const char *path,
79                                     uint32 desired_access,
80                                     struct registry_key **key)
81 {
82         WERROR werr = WERR_OK;
83
84         if (ctx == NULL) {
85                 DEBUG(1, ("Error: configuration is not open!\n"));
86                 werr = WERR_INVALID_PARAM;
87                 goto done;
88         }
89
90         if (rpd(ctx)->token == NULL) {
91                 DEBUG(1, ("Error: token missing from smbconf_ctx. "
92                           "was smbconf_init() called?\n"));
93                 werr = WERR_INVALID_PARAM;
94                 goto done;
95         }
96
97         werr = ctx->ops->open_conf(ctx);
98         if (!W_ERROR_IS_OK(werr)) {
99                 DEBUG(1, ("Error opening the registry.\n"));
100                 goto done;
101         }
102
103         if (path == NULL) {
104                 DEBUG(1, ("Error: NULL path string given\n"));
105                 werr = WERR_INVALID_PARAM;
106                 goto done;
107         }
108
109         werr = reg_open_path(mem_ctx, path, desired_access, rpd(ctx)->token,
110                              key);
111
112         if (!W_ERROR_IS_OK(werr)) {
113                 DEBUG(1, ("Error opening registry path '%s': %s\n",
114                           path, dos_errstr(werr)));
115         }
116
117 done:
118         return werr;
119 }
120
121 /**
122  * Open a subkey of the base key (i.e a service)
123  */
124 static WERROR smbconf_reg_open_service_key(TALLOC_CTX *mem_ctx,
125                                            struct smbconf_ctx *ctx,
126                                            const char *servicename,
127                                            uint32 desired_access,
128                                            struct registry_key **key)
129 {
130         WERROR werr = WERR_OK;
131         char *path = NULL;
132
133         if (servicename == NULL) {
134                 DEBUG(3, ("Error: NULL servicename given.\n"));
135                 werr = WERR_INVALID_PARAM;
136                 goto done;
137         }
138
139         path = talloc_asprintf(mem_ctx, "%s\\%s", ctx->path, servicename);
140         if (path == NULL) {
141                 werr = WERR_NOMEM;
142                 goto done;
143         }
144
145         werr = smbconf_reg_open_path(mem_ctx, ctx, path, desired_access, key);
146
147 done:
148         TALLOC_FREE(path);
149         return werr;
150 }
151
152 /**
153  * open the base key
154  */
155 static WERROR smbconf_reg_open_base_key(TALLOC_CTX *mem_ctx,
156                                         struct smbconf_ctx *ctx,
157                                         uint32 desired_access,
158                                         struct registry_key **key)
159 {
160         return smbconf_reg_open_path(mem_ctx, ctx, ctx->path, desired_access,
161                                      key);
162 }
163
164 /**
165  * check if a value exists in a given registry key
166  */
167 static bool smbconf_value_exists(struct registry_key *key, const char *param)
168 {
169         bool ret = false;
170         WERROR werr = WERR_OK;
171         TALLOC_CTX *ctx = talloc_stackframe();
172         struct registry_value *value = NULL;
173
174         werr = reg_queryvalue(ctx, key, param, &value);
175         if (W_ERROR_IS_OK(werr)) {
176                 ret = true;
177         }
178
179         TALLOC_FREE(ctx);
180         return ret;
181 }
182
183 /**
184  * create a subkey of the base key (i.e. a service...)
185  */
186 static WERROR smbconf_reg_create_service_key(TALLOC_CTX *mem_ctx,
187                                              struct smbconf_ctx *ctx,
188                                              const char * subkeyname,
189                                              struct registry_key **newkey)
190 {
191         WERROR werr = WERR_OK;
192         struct registry_key *create_parent = NULL;
193         TALLOC_CTX *create_ctx;
194         enum winreg_CreateAction action = REG_ACTION_NONE;
195
196         /* create a new talloc ctx for creation. it will hold
197          * the intermediate parent key (SMBCONF) for creation
198          * and will be destroyed when leaving this function... */
199         if (!(create_ctx = talloc_stackframe())) {
200                 werr = WERR_NOMEM;
201                 goto done;
202         }
203
204         werr = smbconf_reg_open_base_key(create_ctx, ctx, REG_KEY_WRITE,
205                                          &create_parent);
206         if (!W_ERROR_IS_OK(werr)) {
207                 goto done;
208         }
209
210         werr = reg_createkey(mem_ctx, create_parent, subkeyname,
211                              REG_KEY_WRITE, newkey, &action);
212         if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
213                 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
214                 werr = WERR_ALREADY_EXISTS;
215         }
216         if (!W_ERROR_IS_OK(werr)) {
217                 DEBUG(5, ("Error creating key %s: %s\n",
218                          subkeyname, dos_errstr(werr)));
219         }
220
221 done:
222         TALLOC_FREE(create_ctx);
223         return werr;
224 }
225
226 /**
227  * add a value to a key.
228  */
229 static WERROR smbconf_reg_set_value(struct registry_key *key,
230                                     const char *valname,
231                                     const char *valstr)
232 {
233         struct registry_value val;
234         WERROR werr = WERR_OK;
235         char *subkeyname;
236         const char *canon_valname;
237         const char *canon_valstr;
238
239         if (!lp_canonicalize_parameter_with_value(valname, valstr,
240                                                   &canon_valname,
241                                                   &canon_valstr))
242         {
243                 if (canon_valname == NULL) {
244                         DEBUG(5, ("invalid parameter '%s' given\n",
245                                   valname));
246                 } else {
247                         DEBUG(5, ("invalid value '%s' given for "
248                                   "parameter '%s'\n", valstr, valname));
249                 }
250                 werr = WERR_INVALID_PARAM;
251                 goto done;
252         }
253
254         if (smbconf_reg_valname_forbidden(canon_valname)) {
255                 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
256                           canon_valname));
257                 werr = WERR_INVALID_PARAM;
258                 goto done;
259         }
260
261         subkeyname = strrchr_m(key->key->name, '\\');
262         if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
263                 DEBUG(5, ("Invalid registry key '%s' given as "
264                           "smbconf section.\n", key->key->name));
265                 werr = WERR_INVALID_PARAM;
266                 goto done;
267         }
268         subkeyname++;
269         if (!strequal(subkeyname, GLOBAL_NAME) &&
270             lp_parameter_is_global(valname))
271         {
272                 DEBUG(5, ("Global paramter '%s' not allowed in "
273                           "service definition ('%s').\n", canon_valname,
274                           subkeyname));
275                 werr = WERR_INVALID_PARAM;
276                 goto done;
277         }
278
279         ZERO_STRUCT(val);
280
281         val.type = REG_SZ;
282         val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
283         val.v.sz.len = strlen(canon_valstr) + 1;
284
285         werr = reg_setvalue(key, canon_valname, &val);
286         if (!W_ERROR_IS_OK(werr)) {
287                 DEBUG(5, ("Error adding value '%s' to "
288                           "key '%s': %s\n",
289                           canon_valname, key->key->name, dos_errstr(werr)));
290         }
291
292 done:
293         return werr;
294 }
295
296 static WERROR smbconf_reg_set_multi_sz_value(struct registry_key *key,
297                                              const char *valname,
298                                              const uint32_t num_strings,
299                                              const char **strings)
300 {
301         WERROR werr;
302         struct registry_value *value;
303         uint32_t count;
304         TALLOC_CTX *tmp_ctx = talloc_stackframe();
305
306         if (strings == NULL) {
307                 werr = WERR_INVALID_PARAM;
308                 goto done;
309         }
310
311         value = TALLOC_ZERO_P(tmp_ctx, struct registry_value);
312
313         value->type = REG_MULTI_SZ;
314         value->v.multi_sz.num_strings = num_strings;
315         value->v.multi_sz.strings = TALLOC_ARRAY(tmp_ctx, char *, num_strings);
316         if (value->v.multi_sz.strings == NULL) {
317                 werr = WERR_NOMEM;
318                 goto done;
319         }
320         for (count = 0; count < num_strings; count++) {
321                 value->v.multi_sz.strings[count] =
322                         talloc_strdup(value->v.multi_sz.strings,
323                                       strings[count]);
324                 if (value->v.multi_sz.strings[count] == NULL) {
325                         werr = WERR_NOMEM;
326                         goto done;
327                 }
328         }
329
330         werr = reg_setvalue(key, valname, value);
331         if (!W_ERROR_IS_OK(werr)) {
332                 DEBUG(5, ("Error adding value '%s' to key '%s': %s\n",
333                           valname, key->key->name, dos_errstr(werr)));
334         }
335
336 done:
337         TALLOC_FREE(tmp_ctx);
338         return werr;
339 }
340
341 /**
342  * format a registry_value into a string.
343  *
344  * This is intended to be used for smbconf registry values,
345  * which are ar stored as REG_SZ values, so the incomplete
346  * handling should be ok.
347  */
348 static char *smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
349                                            struct registry_value *value)
350 {
351         char *result = NULL;
352
353         /* alternatively, create a new talloc context? */
354         if (mem_ctx == NULL) {
355                 return result;
356         }
357
358         switch (value->type) {
359         case REG_DWORD:
360                 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
361                 break;
362         case REG_SZ:
363         case REG_EXPAND_SZ:
364                 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
365                 break;
366         case REG_MULTI_SZ: {
367                 uint32 j;
368                 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
369                         result = talloc_asprintf(mem_ctx, "%s\"%s\" ",
370                                                  result ? result : "" ,
371                                                  value->v.multi_sz.strings[j]);
372                         if (result == NULL) {
373                                 break;
374                         }
375                 }
376                 break;
377         }
378         case REG_BINARY:
379                 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
380                                          (int)value->v.binary.length);
381                 break;
382         default:
383                 result = talloc_asprintf(mem_ctx, "<unprintable>");
384                 break;
385         }
386         return result;
387 }
388
389 static WERROR smbconf_reg_get_includes_internal(TALLOC_CTX *mem_ctx,
390                                                 struct registry_key *key,
391                                                 uint32_t *num_includes,
392                                                 char ***includes)
393 {
394         WERROR werr;
395         uint32_t count;
396         struct registry_value *value = NULL;
397         char **tmp_includes = NULL;
398         TALLOC_CTX *tmp_ctx = talloc_stackframe();
399
400         if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
401                 /* no includes */
402                 goto done;
403         }
404
405         werr = reg_queryvalue(tmp_ctx, key, INCLUDES_VALNAME, &value);
406         if (!W_ERROR_IS_OK(werr)) {
407                 goto done;
408         }
409
410         if (value->type != REG_MULTI_SZ) {
411                 /* wront type -- ignore */
412                 goto done;
413         }
414
415         for (count = 0; count < value->v.multi_sz.num_strings; count++)
416         {
417                 werr = smbconf_add_string_to_array(tmp_ctx,
418                                         &tmp_includes,
419                                         count,
420                                         value->v.multi_sz.strings[count]);
421                 if (!W_ERROR_IS_OK(werr)) {
422                         goto done;
423                 }
424         }
425
426         if (count > 0) {
427                 *includes = talloc_move(mem_ctx, &tmp_includes);
428                 if (*includes == NULL) {
429                         werr = WERR_NOMEM;
430                         goto done;
431                 }
432                 *num_includes = count;
433         } else {
434                 *num_includes = 0;
435                 *includes = NULL;
436         }
437
438 done:
439         TALLOC_FREE(tmp_ctx);
440         return werr;
441 }
442
443 /**
444  * Get the values of a key as a list of value names
445  * and a list of value strings (ordered)
446  */
447 static WERROR smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
448                                      struct registry_key *key,
449                                      uint32_t *num_values,
450                                      char ***value_names,
451                                      char ***value_strings)
452 {
453         TALLOC_CTX *tmp_ctx = NULL;
454         WERROR werr = WERR_OK;
455         uint32_t count;
456         struct registry_value *valvalue = NULL;
457         char *valname = NULL;
458         uint32_t tmp_num_values = 0;
459         char **tmp_valnames = NULL;
460         char **tmp_valstrings = NULL;
461         uint32_t num_includes = 0;
462         char **includes = NULL;
463
464         if ((num_values == NULL) || (value_names == NULL) ||
465             (value_strings == NULL))
466         {
467                 werr = WERR_INVALID_PARAM;
468                 goto done;
469         }
470
471         tmp_ctx = talloc_stackframe();
472         if (tmp_ctx == NULL) {
473                 werr = WERR_NOMEM;
474                 goto done;
475         }
476
477         for (count = 0;
478              werr = reg_enumvalue(tmp_ctx, key, count, &valname, &valvalue),
479              W_ERROR_IS_OK(werr);
480              count++)
481         {
482                 char *valstring;
483
484                 if (!smbconf_reg_valname_valid(valname)) {
485                         continue;
486                 }
487
488                 werr = smbconf_add_string_to_array(tmp_ctx,
489                                                    &tmp_valnames,
490                                                    tmp_num_values, valname);
491                 if (!W_ERROR_IS_OK(werr)) {
492                         goto done;
493                 }
494
495                 valstring = smbconf_format_registry_value(tmp_ctx, valvalue);
496                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
497                                                    tmp_num_values, valstring);
498                 if (!W_ERROR_IS_OK(werr)) {
499                         goto done;
500                 }
501                 tmp_num_values++;
502         }
503         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
504                 goto done;
505         }
506
507         /* now add the includes at the end */
508         werr = smbconf_reg_get_includes_internal(tmp_ctx, key, &num_includes,
509                                                  &includes);
510         if (!W_ERROR_IS_OK(werr)) {
511                 goto done;
512         }
513         for (count = 0; count < num_includes; count++) {
514                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valnames,
515                                                    tmp_num_values, "include");
516                 if (!W_ERROR_IS_OK(werr)) {
517                         goto done;
518                 }
519
520                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
521                                                    tmp_num_values,
522                                                    includes[count]);
523                 if (!W_ERROR_IS_OK(werr)) {
524                         goto done;
525                 }
526
527                 tmp_num_values++;
528         }
529
530         *num_values = tmp_num_values;
531         if (tmp_num_values > 0) {
532                 *value_names = talloc_move(mem_ctx, &tmp_valnames);
533                 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
534         } else {
535                 *value_names = NULL;
536                 *value_strings = NULL;
537         }
538
539 done:
540         TALLOC_FREE(tmp_ctx);
541         return werr;
542 }
543
544 /**********************************************************************
545  *
546  * smbconf operations: registry implementations
547  *
548  **********************************************************************/
549
550 /**
551  * initialize the registry smbconf backend
552  */
553 static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
554 {
555         WERROR werr = WERR_OK;
556
557         if (path == NULL) {
558                 path = KEY_SMBCONF;
559         }
560         ctx->path = talloc_strdup(ctx, path);
561         if (ctx->path == NULL) {
562                 werr = WERR_NOMEM;
563                 goto done;
564         }
565
566         ctx->data = TALLOC_ZERO_P(ctx, struct reg_private_data);
567
568         werr = ntstatus_to_werror(registry_create_admin_token(ctx,
569                                                         &(rpd(ctx)->token)));
570         if (!W_ERROR_IS_OK(werr)) {
571                 DEBUG(1, ("Error creating admin token\n"));
572                 goto done;
573         }
574         rpd(ctx)->open = false;
575
576         if (!registry_init_smbconf()) {
577                 werr = WERR_REG_IO_FAILURE;
578                 goto done;
579         }
580
581 done:
582         return werr;
583 }
584
585 static int smbconf_reg_shutdown(struct smbconf_ctx *ctx)
586 {
587         return ctx->ops->close_conf(ctx);
588 }
589
590 static WERROR smbconf_reg_open(struct smbconf_ctx *ctx)
591 {
592         WERROR werr;
593
594         if (rpd(ctx)->open) {
595                 return WERR_OK;
596         }
597
598         werr = regdb_open();
599         if (W_ERROR_IS_OK(werr)) {
600                 rpd(ctx)->open = true;
601         }
602         return werr;
603 }
604
605 static int smbconf_reg_close(struct smbconf_ctx *ctx)
606 {
607         int ret;
608
609         if (!rpd(ctx)->open) {
610                 return 0;
611         }
612
613         ret = regdb_close();
614         if (ret == 0) {
615                 rpd(ctx)->open = false;
616         }
617         return ret;
618 }
619
620 /**
621  * Get the change sequence number of the given service/parameter.
622  * service and parameter strings may be NULL.
623  */
624 static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
625                                 struct smbconf_csn *csn,
626                                 const char *service, const char *param)
627 {
628         if (csn == NULL) {
629                 return;
630         }
631
632         if (!W_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
633                 return;
634         }
635
636         csn->csn = (uint64_t)regdb_get_seqnum();
637 }
638
639 /**
640  * Drop the whole configuration (restarting empty) - registry version
641  */
642 static WERROR smbconf_reg_drop(struct smbconf_ctx *ctx)
643 {
644         char *path, *p;
645         WERROR werr = WERR_OK;
646         struct registry_key *parent_key = NULL;
647         struct registry_key *new_key = NULL;
648         TALLOC_CTX* mem_ctx = talloc_stackframe();
649         enum winreg_CreateAction action;
650
651         path = talloc_strdup(mem_ctx, ctx->path);
652         if (path == NULL) {
653                 werr = WERR_NOMEM;
654                 goto done;
655         }
656         p = strrchr(path, '\\');
657         *p = '\0';
658         werr = smbconf_reg_open_path(mem_ctx, ctx, path, REG_KEY_WRITE,
659                                      &parent_key);
660
661         if (!W_ERROR_IS_OK(werr)) {
662                 goto done;
663         }
664
665         werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
666
667         if (!W_ERROR_IS_OK(werr)) {
668                 goto done;
669         }
670
671         werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
672                              &new_key, &action);
673
674 done:
675         TALLOC_FREE(mem_ctx);
676         return werr;
677 }
678
679 /**
680  * get the list of share names defined in the configuration.
681  * registry version.
682  */
683 static WERROR smbconf_reg_get_share_names(struct smbconf_ctx *ctx,
684                                           TALLOC_CTX *mem_ctx,
685                                           uint32_t *num_shares,
686                                           char ***share_names)
687 {
688         uint32_t count;
689         uint32_t added_count = 0;
690         TALLOC_CTX *tmp_ctx = NULL;
691         WERROR werr = WERR_OK;
692         struct registry_key *key = NULL;
693         char *subkey_name = NULL;
694         char **tmp_share_names = NULL;
695
696         if ((num_shares == NULL) || (share_names == NULL)) {
697                 werr = WERR_INVALID_PARAM;
698                 goto done;
699         }
700
701         tmp_ctx = talloc_stackframe();
702         if (tmp_ctx == NULL) {
703                 werr = WERR_NOMEM;
704                 goto done;
705         }
706
707         /* make sure "global" is always listed first */
708         if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
709                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
710                                                    0, GLOBAL_NAME);
711                 if (!W_ERROR_IS_OK(werr)) {
712                         goto done;
713                 }
714                 added_count++;
715         }
716
717         werr = smbconf_reg_open_base_key(tmp_ctx, ctx,
718                                          SEC_RIGHTS_ENUM_SUBKEYS, &key);
719         if (!W_ERROR_IS_OK(werr)) {
720                 goto done;
721         }
722
723         for (count = 0;
724              werr = reg_enumkey(tmp_ctx, key, count, &subkey_name, NULL),
725              W_ERROR_IS_OK(werr);
726              count++)
727         {
728                 if (strequal(subkey_name, GLOBAL_NAME)) {
729                         continue;
730                 }
731
732                 werr = smbconf_add_string_to_array(tmp_ctx,
733                                                    &tmp_share_names,
734                                                    added_count,
735                                                    subkey_name);
736                 if (!W_ERROR_IS_OK(werr)) {
737                         goto done;
738                 }
739                 added_count++;
740         }
741         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
742                 goto done;
743         }
744         werr = WERR_OK;
745
746         *num_shares = added_count;
747         if (added_count > 0) {
748                 *share_names = talloc_move(mem_ctx, &tmp_share_names);
749         } else {
750                 *share_names = NULL;
751         }
752
753 done:
754         TALLOC_FREE(tmp_ctx);
755         return werr;
756 }
757
758 /**
759  * check if a share/service of a given name exists - registry version
760  */
761 static bool smbconf_reg_share_exists(struct smbconf_ctx *ctx,
762                                      const char *servicename)
763 {
764         bool ret = false;
765         WERROR werr = WERR_OK;
766         TALLOC_CTX *mem_ctx = talloc_stackframe();
767         struct registry_key *key = NULL;
768
769         werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
770                                             REG_KEY_READ, &key);
771         if (W_ERROR_IS_OK(werr)) {
772                 ret = true;
773         }
774
775         TALLOC_FREE(mem_ctx);
776         return ret;
777 }
778
779 /**
780  * Add a service if it does not already exist - registry version
781  */
782 static WERROR smbconf_reg_create_share(struct smbconf_ctx *ctx,
783                                        const char *servicename)
784 {
785         WERROR werr;
786         TALLOC_CTX *mem_ctx = talloc_stackframe();
787         struct registry_key *key = NULL;
788
789         werr = smbconf_reg_create_service_key(mem_ctx, ctx, servicename, &key);
790
791         TALLOC_FREE(mem_ctx);
792         return werr;
793 }
794
795 /**
796  * get a definition of a share (service) from configuration.
797  */
798 static WERROR smbconf_reg_get_share(struct smbconf_ctx *ctx,
799                                     TALLOC_CTX *mem_ctx,
800                                     const char *servicename,
801                                     uint32_t *num_params,
802                                     char ***param_names, char ***param_values)
803 {
804         WERROR werr = WERR_OK;
805         struct registry_key *key = NULL;
806
807         werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
808                                             REG_KEY_READ, &key);
809         if (!W_ERROR_IS_OK(werr)) {
810                 goto done;
811         }
812
813         werr = smbconf_reg_get_values(mem_ctx, key, num_params,
814                                       param_names, param_values);
815
816 done:
817         TALLOC_FREE(key);
818         return werr;
819 }
820
821 /**
822  * delete a service from configuration
823  */
824 static WERROR smbconf_reg_delete_share(struct smbconf_ctx *ctx,
825                                        const char *servicename)
826 {
827         WERROR werr = WERR_OK;
828         struct registry_key *key = NULL;
829         TALLOC_CTX *mem_ctx = talloc_stackframe();
830
831         werr = smbconf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE, &key);
832         if (!W_ERROR_IS_OK(werr)) {
833                 goto done;
834         }
835
836         werr = reg_deletekey_recursive(key, key, servicename);
837
838 done:
839         TALLOC_FREE(mem_ctx);
840         return werr;
841 }
842
843 /**
844  * set a configuration parameter to the value provided.
845  */
846 static WERROR smbconf_reg_set_parameter(struct smbconf_ctx *ctx,
847                                         const char *service,
848                                         const char *param,
849                                         const char *valstr)
850 {
851         WERROR werr;
852         struct registry_key *key = NULL;
853         TALLOC_CTX *mem_ctx = talloc_stackframe();
854
855         werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
856                                             REG_KEY_WRITE, &key);
857         if (!W_ERROR_IS_OK(werr)) {
858                 goto done;
859         }
860
861         werr = smbconf_reg_set_value(key, param, valstr);
862
863 done:
864         TALLOC_FREE(mem_ctx);
865         return werr;
866 }
867
868 /**
869  * get the value of a configuration parameter as a string
870  */
871 static WERROR smbconf_reg_get_parameter(struct smbconf_ctx *ctx,
872                                         TALLOC_CTX *mem_ctx,
873                                         const char *service,
874                                         const char *param,
875                                         char **valstr)
876 {
877         WERROR werr = WERR_OK;
878         struct registry_key *key = NULL;
879         struct registry_value *value = NULL;
880
881         werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
882                                             REG_KEY_READ, &key);
883         if (!W_ERROR_IS_OK(werr)) {
884                 goto done;
885         }
886
887         if (!smbconf_value_exists(key, param)) {
888                 werr = WERR_INVALID_PARAM;
889                 goto done;
890         }
891
892         werr = reg_queryvalue(mem_ctx, key, param, &value);
893         if (!W_ERROR_IS_OK(werr)) {
894                 goto done;
895         }
896
897         *valstr = smbconf_format_registry_value(mem_ctx, value);
898
899         if (*valstr == NULL) {
900                 werr = WERR_NOMEM;
901         }
902
903 done:
904         TALLOC_FREE(key);
905         TALLOC_FREE(value);
906         return werr;
907 }
908
909 /**
910  * delete a parameter from configuration
911  */
912 static WERROR smbconf_reg_delete_parameter(struct smbconf_ctx *ctx,
913                                            const char *service,
914                                            const char *param)
915 {
916         struct registry_key *key = NULL;
917         WERROR werr = WERR_OK;
918         TALLOC_CTX *mem_ctx = talloc_stackframe();
919
920         werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
921                                             REG_KEY_ALL, &key);
922         if (!W_ERROR_IS_OK(werr)) {
923                 goto done;
924         }
925
926         if (!smbconf_value_exists(key, param)) {
927                 werr = WERR_INVALID_PARAM;
928                 goto done;
929         }
930
931         werr = reg_deletevalue(key, param);
932
933 done:
934         TALLOC_FREE(mem_ctx);
935         return werr;
936 }
937
938 static WERROR smbconf_reg_get_includes(struct smbconf_ctx *ctx,
939                                        TALLOC_CTX *mem_ctx,
940                                        const char *service,
941                                        uint32_t *num_includes,
942                                        char ***includes)
943 {
944         WERROR werr;
945         struct registry_key *key = NULL;
946         TALLOC_CTX *tmp_ctx = talloc_stackframe();
947
948         werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
949                                             REG_KEY_READ, &key);
950         if (!W_ERROR_IS_OK(werr)) {
951                 goto done;
952         }
953
954         werr = smbconf_reg_get_includes_internal(mem_ctx, key, num_includes,
955                                                  includes);
956
957 done:
958         TALLOC_FREE(tmp_ctx);
959         return werr;
960 }
961
962 static WERROR smbconf_reg_set_includes(struct smbconf_ctx *ctx,
963                                        const char *service,
964                                        uint32_t num_includes,
965                                        const char **includes)
966 {
967         WERROR werr = WERR_OK;
968         struct registry_key *key = NULL;
969         TALLOC_CTX *tmp_ctx = talloc_stackframe();
970
971         werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
972                                             REG_KEY_ALL, &key);
973         if (!W_ERROR_IS_OK(werr)) {
974                 goto done;
975         }
976
977         werr = smbconf_reg_set_multi_sz_value(key, INCLUDES_VALNAME,
978                                               num_includes, includes);
979
980 done:
981         TALLOC_FREE(tmp_ctx);
982         return werr;
983 }
984
985
986 struct smbconf_ops smbconf_ops_reg = {
987         .init                   = smbconf_reg_init,
988         .shutdown               = smbconf_reg_shutdown,
989         .open_conf              = smbconf_reg_open,
990         .close_conf             = smbconf_reg_close,
991         .get_csn                = smbconf_reg_get_csn,
992         .drop                   = smbconf_reg_drop,
993         .get_share_names        = smbconf_reg_get_share_names,
994         .share_exists           = smbconf_reg_share_exists,
995         .create_share           = smbconf_reg_create_share,
996         .get_share              = smbconf_reg_get_share,
997         .delete_share           = smbconf_reg_delete_share,
998         .set_parameter          = smbconf_reg_set_parameter,
999         .get_parameter          = smbconf_reg_get_parameter,
1000         .delete_parameter       = smbconf_reg_delete_parameter,
1001         .get_includes           = smbconf_reg_get_includes,
1002         .set_includes           = smbconf_reg_set_includes,
1003 };
1004
1005
1006 /**
1007  * initialize the smbconf registry backend
1008  * the only function that is exported from this module
1009  */
1010 WERROR smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
1011                         const char *path)
1012 {
1013         return smbconf_init(mem_ctx, conf_ctx, path, &smbconf_ops_reg);
1014 }