Increase level of debug msg when a regkey is not found
[ira/wip.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(5, ("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                 path = talloc_strdup(mem_ctx, ctx->path);
135         } else {
136                 path = talloc_asprintf(mem_ctx, "%s\\%s", ctx->path,
137                                        servicename);
138         }
139         if (path == NULL) {
140                 werr = WERR_NOMEM;
141                 goto done;
142         }
143
144         werr = smbconf_reg_open_path(mem_ctx, ctx, path, desired_access, key);
145
146 done:
147         TALLOC_FREE(path);
148         return werr;
149 }
150
151 /**
152  * open the base key
153  */
154 static WERROR smbconf_reg_open_base_key(TALLOC_CTX *mem_ctx,
155                                         struct smbconf_ctx *ctx,
156                                         uint32 desired_access,
157                                         struct registry_key **key)
158 {
159         return smbconf_reg_open_path(mem_ctx, ctx, ctx->path, desired_access,
160                                      key);
161 }
162
163 /**
164  * check if a value exists in a given registry key
165  */
166 static bool smbconf_value_exists(struct registry_key *key, const char *param)
167 {
168         bool ret = false;
169         WERROR werr = WERR_OK;
170         TALLOC_CTX *ctx = talloc_stackframe();
171         struct registry_value *value = NULL;
172
173         werr = reg_queryvalue(ctx, key, param, &value);
174         if (W_ERROR_IS_OK(werr)) {
175                 ret = true;
176         }
177
178         TALLOC_FREE(ctx);
179         return ret;
180 }
181
182 /**
183  * create a subkey of the base key (i.e. a service...)
184  */
185 static WERROR smbconf_reg_create_service_key(TALLOC_CTX *mem_ctx,
186                                              struct smbconf_ctx *ctx,
187                                              const char * subkeyname,
188                                              struct registry_key **newkey)
189 {
190         WERROR werr = WERR_OK;
191         struct registry_key *create_parent = NULL;
192         TALLOC_CTX *create_ctx;
193         enum winreg_CreateAction action = REG_ACTION_NONE;
194
195         /* create a new talloc ctx for creation. it will hold
196          * the intermediate parent key (SMBCONF) for creation
197          * and will be destroyed when leaving this function... */
198         create_ctx = talloc_stackframe();
199
200         werr = smbconf_reg_open_base_key(create_ctx, ctx, REG_KEY_WRITE,
201                                          &create_parent);
202         if (!W_ERROR_IS_OK(werr)) {
203                 goto done;
204         }
205
206         werr = reg_createkey(mem_ctx, create_parent, subkeyname,
207                              REG_KEY_WRITE, newkey, &action);
208         if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
209                 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
210                 werr = WERR_ALREADY_EXISTS;
211         }
212         if (!W_ERROR_IS_OK(werr)) {
213                 DEBUG(5, ("Error creating key %s: %s\n",
214                          subkeyname, dos_errstr(werr)));
215         }
216
217 done:
218         TALLOC_FREE(create_ctx);
219         return werr;
220 }
221
222 /**
223  * add a value to a key.
224  */
225 static WERROR smbconf_reg_set_value(struct registry_key *key,
226                                     const char *valname,
227                                     const char *valstr)
228 {
229         struct registry_value val;
230         WERROR werr = WERR_OK;
231         char *subkeyname;
232         const char *canon_valname;
233         const char *canon_valstr;
234
235         if (!lp_canonicalize_parameter_with_value(valname, valstr,
236                                                   &canon_valname,
237                                                   &canon_valstr))
238         {
239                 if (canon_valname == NULL) {
240                         DEBUG(5, ("invalid parameter '%s' given\n",
241                                   valname));
242                 } else {
243                         DEBUG(5, ("invalid value '%s' given for "
244                                   "parameter '%s'\n", valstr, valname));
245                 }
246                 werr = WERR_INVALID_PARAM;
247                 goto done;
248         }
249
250         if (smbconf_reg_valname_forbidden(canon_valname)) {
251                 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
252                           canon_valname));
253                 werr = WERR_INVALID_PARAM;
254                 goto done;
255         }
256
257         subkeyname = strrchr_m(key->key->name, '\\');
258         if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
259                 DEBUG(5, ("Invalid registry key '%s' given as "
260                           "smbconf section.\n", key->key->name));
261                 werr = WERR_INVALID_PARAM;
262                 goto done;
263         }
264         subkeyname++;
265         if (!strequal(subkeyname, GLOBAL_NAME) &&
266             lp_parameter_is_global(valname))
267         {
268                 DEBUG(5, ("Global paramter '%s' not allowed in "
269                           "service definition ('%s').\n", canon_valname,
270                           subkeyname));
271                 werr = WERR_INVALID_PARAM;
272                 goto done;
273         }
274
275         ZERO_STRUCT(val);
276
277         val.type = REG_SZ;
278         val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
279         val.v.sz.len = strlen(canon_valstr) + 1;
280
281         werr = reg_setvalue(key, canon_valname, &val);
282         if (!W_ERROR_IS_OK(werr)) {
283                 DEBUG(5, ("Error adding value '%s' to "
284                           "key '%s': %s\n",
285                           canon_valname, key->key->name, dos_errstr(werr)));
286         }
287
288 done:
289         return werr;
290 }
291
292 static WERROR smbconf_reg_set_multi_sz_value(struct registry_key *key,
293                                              const char *valname,
294                                              const uint32_t num_strings,
295                                              const char **strings)
296 {
297         WERROR werr;
298         struct registry_value *value;
299         uint32_t count;
300         TALLOC_CTX *tmp_ctx = talloc_stackframe();
301
302         if (strings == NULL) {
303                 werr = WERR_INVALID_PARAM;
304                 goto done;
305         }
306
307         value = TALLOC_ZERO_P(tmp_ctx, struct registry_value);
308
309         value->type = REG_MULTI_SZ;
310         value->v.multi_sz.num_strings = num_strings;
311         value->v.multi_sz.strings = TALLOC_ARRAY(tmp_ctx, char *, num_strings);
312         if (value->v.multi_sz.strings == NULL) {
313                 werr = WERR_NOMEM;
314                 goto done;
315         }
316         for (count = 0; count < num_strings; count++) {
317                 value->v.multi_sz.strings[count] =
318                         talloc_strdup(value->v.multi_sz.strings,
319                                       strings[count]);
320                 if (value->v.multi_sz.strings[count] == NULL) {
321                         werr = WERR_NOMEM;
322                         goto done;
323                 }
324         }
325
326         werr = reg_setvalue(key, valname, value);
327         if (!W_ERROR_IS_OK(werr)) {
328                 DEBUG(5, ("Error adding value '%s' to key '%s': %s\n",
329                           valname, key->key->name, dos_errstr(werr)));
330         }
331
332 done:
333         TALLOC_FREE(tmp_ctx);
334         return werr;
335 }
336
337 /**
338  * format a registry_value into a string.
339  *
340  * This is intended to be used for smbconf registry values,
341  * which are ar stored as REG_SZ values, so the incomplete
342  * handling should be ok.
343  */
344 static char *smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
345                                            struct registry_value *value)
346 {
347         char *result = NULL;
348
349         /* alternatively, create a new talloc context? */
350         if (mem_ctx == NULL) {
351                 return result;
352         }
353
354         switch (value->type) {
355         case REG_DWORD:
356                 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
357                 break;
358         case REG_SZ:
359         case REG_EXPAND_SZ:
360                 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
361                 break;
362         case REG_MULTI_SZ: {
363                 uint32 j;
364                 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
365                         result = talloc_asprintf(mem_ctx, "%s\"%s\" ",
366                                                  result ? result : "" ,
367                                                  value->v.multi_sz.strings[j]);
368                         if (result == NULL) {
369                                 break;
370                         }
371                 }
372                 break;
373         }
374         case REG_BINARY:
375                 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
376                                          (int)value->v.binary.length);
377                 break;
378         default:
379                 result = talloc_asprintf(mem_ctx, "<unprintable>");
380                 break;
381         }
382         return result;
383 }
384
385 static WERROR smbconf_reg_get_includes_internal(TALLOC_CTX *mem_ctx,
386                                                 struct registry_key *key,
387                                                 uint32_t *num_includes,
388                                                 char ***includes)
389 {
390         WERROR werr;
391         uint32_t count;
392         struct registry_value *value = NULL;
393         char **tmp_includes = NULL;
394         TALLOC_CTX *tmp_ctx = talloc_stackframe();
395
396         if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
397                 /* no includes */
398                 *num_includes = 0;
399                 *includes = NULL;
400                 werr = WERR_OK;
401                 goto done;
402         }
403
404         werr = reg_queryvalue(tmp_ctx, key, INCLUDES_VALNAME, &value);
405         if (!W_ERROR_IS_OK(werr)) {
406                 goto done;
407         }
408
409         if (value->type != REG_MULTI_SZ) {
410                 /* wront type -- ignore */
411                 goto done;
412         }
413
414         for (count = 0; count < value->v.multi_sz.num_strings; count++)
415         {
416                 werr = smbconf_add_string_to_array(tmp_ctx,
417                                         &tmp_includes,
418                                         count,
419                                         value->v.multi_sz.strings[count]);
420                 if (!W_ERROR_IS_OK(werr)) {
421                         goto done;
422                 }
423         }
424
425         if (count > 0) {
426                 *includes = talloc_move(mem_ctx, &tmp_includes);
427                 if (*includes == NULL) {
428                         werr = WERR_NOMEM;
429                         goto done;
430                 }
431                 *num_includes = count;
432         } else {
433                 *num_includes = 0;
434                 *includes = NULL;
435         }
436
437 done:
438         TALLOC_FREE(tmp_ctx);
439         return werr;
440 }
441
442 /**
443  * Get the values of a key as a list of value names
444  * and a list of value strings (ordered)
445  */
446 static WERROR smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
447                                      struct registry_key *key,
448                                      uint32_t *num_values,
449                                      char ***value_names,
450                                      char ***value_strings)
451 {
452         TALLOC_CTX *tmp_ctx = NULL;
453         WERROR werr = WERR_OK;
454         uint32_t count;
455         struct registry_value *valvalue = NULL;
456         char *valname = NULL;
457         uint32_t tmp_num_values = 0;
458         char **tmp_valnames = NULL;
459         char **tmp_valstrings = NULL;
460         uint32_t num_includes = 0;
461         char **includes = NULL;
462
463         if ((num_values == NULL) || (value_names == NULL) ||
464             (value_strings == NULL))
465         {
466                 werr = WERR_INVALID_PARAM;
467                 goto done;
468         }
469
470         tmp_ctx = talloc_stackframe();
471
472         for (count = 0;
473              werr = reg_enumvalue(tmp_ctx, key, count, &valname, &valvalue),
474              W_ERROR_IS_OK(werr);
475              count++)
476         {
477                 char *valstring;
478
479                 if (!smbconf_reg_valname_valid(valname)) {
480                         continue;
481                 }
482
483                 werr = smbconf_add_string_to_array(tmp_ctx,
484                                                    &tmp_valnames,
485                                                    tmp_num_values, valname);
486                 if (!W_ERROR_IS_OK(werr)) {
487                         goto done;
488                 }
489
490                 valstring = smbconf_format_registry_value(tmp_ctx, valvalue);
491                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
492                                                    tmp_num_values, valstring);
493                 if (!W_ERROR_IS_OK(werr)) {
494                         goto done;
495                 }
496                 tmp_num_values++;
497         }
498         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
499                 goto done;
500         }
501
502         /* now add the includes at the end */
503         werr = smbconf_reg_get_includes_internal(tmp_ctx, key, &num_includes,
504                                                  &includes);
505         if (!W_ERROR_IS_OK(werr)) {
506                 goto done;
507         }
508         for (count = 0; count < num_includes; count++) {
509                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valnames,
510                                                    tmp_num_values, "include");
511                 if (!W_ERROR_IS_OK(werr)) {
512                         goto done;
513                 }
514
515                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
516                                                    tmp_num_values,
517                                                    includes[count]);
518                 if (!W_ERROR_IS_OK(werr)) {
519                         goto done;
520                 }
521
522                 tmp_num_values++;
523         }
524
525         *num_values = tmp_num_values;
526         if (tmp_num_values > 0) {
527                 *value_names = talloc_move(mem_ctx, &tmp_valnames);
528                 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
529         } else {
530                 *value_names = NULL;
531                 *value_strings = NULL;
532         }
533
534 done:
535         TALLOC_FREE(tmp_ctx);
536         return werr;
537 }
538
539 static bool smbconf_reg_key_has_values(struct registry_key *key)
540 {
541         WERROR werr;
542         uint32_t num_subkeys;
543         uint32_t max_subkeylen;
544         uint32_t max_subkeysize;
545         uint32_t num_values;
546         uint32_t max_valnamelen;
547         uint32_t max_valbufsize;
548         uint32_t secdescsize;
549         NTTIME last_changed_time;
550
551         werr = reg_queryinfokey(key, &num_subkeys, &max_subkeylen,
552                                 &max_subkeysize, &num_values, &max_valnamelen,
553                                 &max_valbufsize, &secdescsize,
554                                 &last_changed_time);
555         if (!W_ERROR_IS_OK(werr)) {
556                 return false;
557         }
558
559         return (num_values != 0);
560 }
561
562 /**
563  * delete all values from a key
564  */
565 static WERROR smbconf_reg_delete_values(struct registry_key *key)
566 {
567         WERROR werr;
568         char *valname;
569         struct registry_value *valvalue;
570         uint32_t count;
571         TALLOC_CTX *mem_ctx = talloc_stackframe();
572
573         for (count = 0;
574              werr = reg_enumvalue(mem_ctx, key, count, &valname, &valvalue),
575              W_ERROR_IS_OK(werr);
576              count++)
577         {
578                 werr = reg_deletevalue(key, valname);
579                 if (!W_ERROR_IS_OK(werr)) {
580                         goto done;
581                 }
582         }
583         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
584                 DEBUG(1, ("smbconf_reg_delete_values: "
585                           "Error enumerating values of %s: %s\n",
586                           key->key->name,
587                           dos_errstr(werr)));
588                 goto done;
589         }
590
591         werr = WERR_OK;
592
593 done:
594         TALLOC_FREE(mem_ctx);
595         return werr;
596 }
597
598 /**********************************************************************
599  *
600  * smbconf operations: registry implementations
601  *
602  **********************************************************************/
603
604 /**
605  * initialize the registry smbconf backend
606  */
607 static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
608 {
609         WERROR werr = WERR_OK;
610
611         if (path == NULL) {
612                 path = KEY_SMBCONF;
613         }
614         ctx->path = talloc_strdup(ctx, path);
615         if (ctx->path == NULL) {
616                 werr = WERR_NOMEM;
617                 goto done;
618         }
619
620         ctx->data = TALLOC_ZERO_P(ctx, struct reg_private_data);
621
622         werr = ntstatus_to_werror(registry_create_admin_token(ctx,
623                                                         &(rpd(ctx)->token)));
624         if (!W_ERROR_IS_OK(werr)) {
625                 DEBUG(1, ("Error creating admin token\n"));
626                 goto done;
627         }
628         rpd(ctx)->open = false;
629
630         werr = registry_init_smbconf(path);
631         if (!W_ERROR_IS_OK(werr)) {
632                 goto done;
633         }
634
635 done:
636         return werr;
637 }
638
639 static int smbconf_reg_shutdown(struct smbconf_ctx *ctx)
640 {
641         return ctx->ops->close_conf(ctx);
642 }
643
644 static WERROR smbconf_reg_open(struct smbconf_ctx *ctx)
645 {
646         WERROR werr;
647
648         if (rpd(ctx)->open) {
649                 return WERR_OK;
650         }
651
652         werr = regdb_open();
653         if (W_ERROR_IS_OK(werr)) {
654                 rpd(ctx)->open = true;
655         }
656         return werr;
657 }
658
659 static int smbconf_reg_close(struct smbconf_ctx *ctx)
660 {
661         int ret;
662
663         if (!rpd(ctx)->open) {
664                 return 0;
665         }
666
667         ret = regdb_close();
668         if (ret == 0) {
669                 rpd(ctx)->open = false;
670         }
671         return ret;
672 }
673
674 /**
675  * Get the change sequence number of the given service/parameter.
676  * service and parameter strings may be NULL.
677  */
678 static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
679                                 struct smbconf_csn *csn,
680                                 const char *service, const char *param)
681 {
682         if (csn == NULL) {
683                 return;
684         }
685
686         if (!W_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
687                 return;
688         }
689
690         csn->csn = (uint64_t)regdb_get_seqnum();
691 }
692
693 /**
694  * Drop the whole configuration (restarting empty) - registry version
695  */
696 static WERROR smbconf_reg_drop(struct smbconf_ctx *ctx)
697 {
698         char *path, *p;
699         WERROR werr = WERR_OK;
700         struct registry_key *parent_key = NULL;
701         struct registry_key *new_key = NULL;
702         TALLOC_CTX* mem_ctx = talloc_stackframe();
703         enum winreg_CreateAction action;
704
705         path = talloc_strdup(mem_ctx, ctx->path);
706         if (path == NULL) {
707                 werr = WERR_NOMEM;
708                 goto done;
709         }
710         p = strrchr(path, '\\');
711         *p = '\0';
712         werr = smbconf_reg_open_path(mem_ctx, ctx, path, REG_KEY_WRITE,
713                                      &parent_key);
714
715         if (!W_ERROR_IS_OK(werr)) {
716                 goto done;
717         }
718
719         werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
720
721         if (!W_ERROR_IS_OK(werr)) {
722                 goto done;
723         }
724
725         werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
726                              &new_key, &action);
727
728 done:
729         TALLOC_FREE(mem_ctx);
730         return werr;
731 }
732
733 /**
734  * get the list of share names defined in the configuration.
735  * registry version.
736  */
737 static WERROR smbconf_reg_get_share_names(struct smbconf_ctx *ctx,
738                                           TALLOC_CTX *mem_ctx,
739                                           uint32_t *num_shares,
740                                           char ***share_names)
741 {
742         uint32_t count;
743         uint32_t added_count = 0;
744         TALLOC_CTX *tmp_ctx = NULL;
745         WERROR werr = WERR_OK;
746         struct registry_key *key = NULL;
747         char *subkey_name = NULL;
748         char **tmp_share_names = NULL;
749
750         if ((num_shares == NULL) || (share_names == NULL)) {
751                 werr = WERR_INVALID_PARAM;
752                 goto done;
753         }
754
755         tmp_ctx = talloc_stackframe();
756
757         /* if there are values in the base key, return NULL as share name */
758         werr = smbconf_reg_open_base_key(tmp_ctx, ctx,
759                                          SEC_RIGHTS_ENUM_SUBKEYS, &key);
760         if (!W_ERROR_IS_OK(werr)) {
761                 goto done;
762         }
763
764         if (smbconf_reg_key_has_values(key)) {
765                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
766                                                    0, NULL);
767                 if (!W_ERROR_IS_OK(werr)) {
768                         goto done;
769                 }
770                 added_count++;
771         }
772
773         /* make sure "global" is always listed first */
774         if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
775                 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
776                                                    added_count, GLOBAL_NAME);
777                 if (!W_ERROR_IS_OK(werr)) {
778                         goto done;
779                 }
780                 added_count++;
781         }
782
783         for (count = 0;
784              werr = reg_enumkey(tmp_ctx, key, count, &subkey_name, NULL),
785              W_ERROR_IS_OK(werr);
786              count++)
787         {
788                 if (strequal(subkey_name, GLOBAL_NAME)) {
789                         continue;
790                 }
791
792                 werr = smbconf_add_string_to_array(tmp_ctx,
793                                                    &tmp_share_names,
794                                                    added_count,
795                                                    subkey_name);
796                 if (!W_ERROR_IS_OK(werr)) {
797                         goto done;
798                 }
799                 added_count++;
800         }
801         if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
802                 goto done;
803         }
804         werr = WERR_OK;
805
806         *num_shares = added_count;
807         if (added_count > 0) {
808                 *share_names = talloc_move(mem_ctx, &tmp_share_names);
809         } else {
810                 *share_names = NULL;
811         }
812
813 done:
814         TALLOC_FREE(tmp_ctx);
815         return werr;
816 }
817
818 /**
819  * check if a share/service of a given name exists - registry version
820  */
821 static bool smbconf_reg_share_exists(struct smbconf_ctx *ctx,
822                                      const char *servicename)
823 {
824         bool ret = false;
825         WERROR werr = WERR_OK;
826         TALLOC_CTX *mem_ctx = talloc_stackframe();
827         struct registry_key *key = NULL;
828
829         werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
830                                             REG_KEY_READ, &key);
831         if (W_ERROR_IS_OK(werr)) {
832                 ret = true;
833         }
834
835         TALLOC_FREE(mem_ctx);
836         return ret;
837 }
838
839 /**
840  * Add a service if it does not already exist - registry version
841  */
842 static WERROR smbconf_reg_create_share(struct smbconf_ctx *ctx,
843                                        const char *servicename)
844 {
845         WERROR werr;
846         TALLOC_CTX *mem_ctx = talloc_stackframe();
847         struct registry_key *key = NULL;
848
849         if (servicename == NULL) {
850                 werr = smbconf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE,
851                                                  &key);
852         } else {
853                 werr = smbconf_reg_create_service_key(mem_ctx, ctx,
854                                                       servicename, &key);
855         }
856
857         TALLOC_FREE(mem_ctx);
858         return werr;
859 }
860
861 /**
862  * get a definition of a share (service) from configuration.
863  */
864 static WERROR smbconf_reg_get_share(struct smbconf_ctx *ctx,
865                                     TALLOC_CTX *mem_ctx,
866                                     const char *servicename,
867                                     struct smbconf_service **service)
868 {
869         WERROR werr = WERR_OK;
870         struct registry_key *key = NULL;
871         struct smbconf_service *tmp_service = NULL;
872         TALLOC_CTX *tmp_ctx = talloc_stackframe();
873
874         werr = smbconf_reg_open_service_key(tmp_ctx, ctx, servicename,
875                                             REG_KEY_READ, &key);
876         if (!W_ERROR_IS_OK(werr)) {
877                 goto done;
878         }
879
880         tmp_service = TALLOC_ZERO_P(tmp_ctx, struct smbconf_service);
881         if (tmp_service == NULL) {
882                 werr =  WERR_NOMEM;
883                 goto done;
884         }
885
886         if (servicename != NULL) {
887                 tmp_service->name = talloc_strdup(tmp_service, servicename);
888                 if (tmp_service->name == NULL) {
889                         werr = WERR_NOMEM;
890                         goto done;
891                 }
892         }
893
894         werr = smbconf_reg_get_values(tmp_service, key,
895                                       &(tmp_service->num_params),
896                                       &(tmp_service->param_names),
897                                       &(tmp_service->param_values));
898
899         if (W_ERROR_IS_OK(werr)) {
900                 *service = talloc_move(mem_ctx, &tmp_service);
901         }
902
903 done:
904         TALLOC_FREE(tmp_ctx);
905         return werr;
906 }
907
908 /**
909  * delete a service from configuration
910  */
911 static WERROR smbconf_reg_delete_share(struct smbconf_ctx *ctx,
912                                        const char *servicename)
913 {
914         WERROR werr = WERR_OK;
915         struct registry_key *key = NULL;
916         TALLOC_CTX *mem_ctx = talloc_stackframe();
917
918         werr = smbconf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE, &key);
919         if (!W_ERROR_IS_OK(werr)) {
920                 goto done;
921         }
922
923         if (servicename != NULL) {
924                 werr = reg_deletekey_recursive(key, key, servicename);
925         } else {
926                 werr = smbconf_reg_delete_values(key);
927         }
928
929 done:
930         TALLOC_FREE(mem_ctx);
931         return werr;
932 }
933
934 /**
935  * set a configuration parameter to the value provided.
936  */
937 static WERROR smbconf_reg_set_parameter(struct smbconf_ctx *ctx,
938                                         const char *service,
939                                         const char *param,
940                                         const char *valstr)
941 {
942         WERROR werr;
943         struct registry_key *key = NULL;
944         TALLOC_CTX *mem_ctx = talloc_stackframe();
945
946         werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
947                                             REG_KEY_WRITE, &key);
948         if (!W_ERROR_IS_OK(werr)) {
949                 goto done;
950         }
951
952         werr = smbconf_reg_set_value(key, param, valstr);
953
954 done:
955         TALLOC_FREE(mem_ctx);
956         return werr;
957 }
958
959 /**
960  * get the value of a configuration parameter as a string
961  */
962 static WERROR smbconf_reg_get_parameter(struct smbconf_ctx *ctx,
963                                         TALLOC_CTX *mem_ctx,
964                                         const char *service,
965                                         const char *param,
966                                         char **valstr)
967 {
968         WERROR werr = WERR_OK;
969         struct registry_key *key = NULL;
970         struct registry_value *value = NULL;
971
972         werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
973                                             REG_KEY_READ, &key);
974         if (!W_ERROR_IS_OK(werr)) {
975                 goto done;
976         }
977
978         if (!smbconf_reg_valname_valid(param)) {
979                 werr = WERR_INVALID_PARAM;
980                 goto done;
981         }
982
983         if (!smbconf_value_exists(key, param)) {
984                 werr = WERR_INVALID_PARAM;
985                 goto done;
986         }
987
988         werr = reg_queryvalue(mem_ctx, key, param, &value);
989         if (!W_ERROR_IS_OK(werr)) {
990                 goto done;
991         }
992
993         *valstr = smbconf_format_registry_value(mem_ctx, value);
994
995         if (*valstr == NULL) {
996                 werr = WERR_NOMEM;
997         }
998
999 done:
1000         TALLOC_FREE(key);
1001         TALLOC_FREE(value);
1002         return werr;
1003 }
1004
1005 /**
1006  * delete a parameter from configuration
1007  */
1008 static WERROR smbconf_reg_delete_parameter(struct smbconf_ctx *ctx,
1009                                            const char *service,
1010                                            const char *param)
1011 {
1012         struct registry_key *key = NULL;
1013         WERROR werr = WERR_OK;
1014         TALLOC_CTX *mem_ctx = talloc_stackframe();
1015
1016         werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
1017                                             REG_KEY_ALL, &key);
1018         if (!W_ERROR_IS_OK(werr)) {
1019                 goto done;
1020         }
1021
1022         if (!smbconf_reg_valname_valid(param)) {
1023                 werr = WERR_INVALID_PARAM;
1024                 goto done;
1025         }
1026
1027         if (!smbconf_value_exists(key, param)) {
1028                 werr = WERR_INVALID_PARAM;
1029                 goto done;
1030         }
1031
1032         werr = reg_deletevalue(key, param);
1033
1034 done:
1035         TALLOC_FREE(mem_ctx);
1036         return werr;
1037 }
1038
1039 static WERROR smbconf_reg_get_includes(struct smbconf_ctx *ctx,
1040                                        TALLOC_CTX *mem_ctx,
1041                                        const char *service,
1042                                        uint32_t *num_includes,
1043                                        char ***includes)
1044 {
1045         WERROR werr;
1046         struct registry_key *key = NULL;
1047         TALLOC_CTX *tmp_ctx = talloc_stackframe();
1048
1049         werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1050                                             REG_KEY_READ, &key);
1051         if (!W_ERROR_IS_OK(werr)) {
1052                 goto done;
1053         }
1054
1055         werr = smbconf_reg_get_includes_internal(mem_ctx, key, num_includes,
1056                                                  includes);
1057
1058 done:
1059         TALLOC_FREE(tmp_ctx);
1060         return werr;
1061 }
1062
1063 static WERROR smbconf_reg_set_includes(struct smbconf_ctx *ctx,
1064                                        const char *service,
1065                                        uint32_t num_includes,
1066                                        const char **includes)
1067 {
1068         WERROR werr = WERR_OK;
1069         struct registry_key *key = NULL;
1070         TALLOC_CTX *tmp_ctx = talloc_stackframe();
1071
1072         werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1073                                             REG_KEY_ALL, &key);
1074         if (!W_ERROR_IS_OK(werr)) {
1075                 goto done;
1076         }
1077
1078         if (num_includes == 0) {
1079                 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
1080                         goto done;
1081                 }
1082                 werr = reg_deletevalue(key, INCLUDES_VALNAME);
1083         } else {
1084                 werr = smbconf_reg_set_multi_sz_value(key, INCLUDES_VALNAME,
1085                                                       num_includes, includes);
1086         }
1087
1088 done:
1089         TALLOC_FREE(tmp_ctx);
1090         return werr;
1091 }
1092
1093 static WERROR smbconf_reg_delete_includes(struct smbconf_ctx *ctx,
1094                                           const char *service)
1095 {
1096         WERROR werr = WERR_OK;
1097         struct registry_key *key = NULL;
1098         TALLOC_CTX *tmp_ctx = talloc_stackframe();
1099
1100         werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1101                                             REG_KEY_ALL, &key);
1102         if (!W_ERROR_IS_OK(werr)) {
1103                 goto done;
1104         }
1105
1106         if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
1107                 goto done;
1108         }
1109
1110         werr = reg_deletevalue(key, INCLUDES_VALNAME);
1111
1112
1113 done:
1114         TALLOC_FREE(tmp_ctx);
1115         return werr;
1116 }
1117
1118 struct smbconf_ops smbconf_ops_reg = {
1119         .init                   = smbconf_reg_init,
1120         .shutdown               = smbconf_reg_shutdown,
1121         .open_conf              = smbconf_reg_open,
1122         .close_conf             = smbconf_reg_close,
1123         .get_csn                = smbconf_reg_get_csn,
1124         .drop                   = smbconf_reg_drop,
1125         .get_share_names        = smbconf_reg_get_share_names,
1126         .share_exists           = smbconf_reg_share_exists,
1127         .create_share           = smbconf_reg_create_share,
1128         .get_share              = smbconf_reg_get_share,
1129         .delete_share           = smbconf_reg_delete_share,
1130         .set_parameter          = smbconf_reg_set_parameter,
1131         .get_parameter          = smbconf_reg_get_parameter,
1132         .delete_parameter       = smbconf_reg_delete_parameter,
1133         .get_includes           = smbconf_reg_get_includes,
1134         .set_includes           = smbconf_reg_set_includes,
1135         .delete_includes        = smbconf_reg_delete_includes,
1136 };
1137
1138
1139 /**
1140  * initialize the smbconf registry backend
1141  * the only function that is exported from this module
1142  */
1143 WERROR smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
1144                         const char *path)
1145 {
1146         return smbconf_init_internal(mem_ctx, conf_ctx, path, &smbconf_ops_reg);
1147 }