libsmbconf: add delete_includes mehtod to the api (and backend implementations)
[Samba.git] / source / lib / smbconf / smbconf_reg.c
blobb262959afc62f347c85843ffcea11ad8e4041a95
1 /*
2 * Unix SMB/CIFS implementation.
3 * libsmbconf - Samba configuration library, registry backend
4 * Copyright (C) Michael Adam 2008
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.
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.
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/>.
20 #include "includes.h"
21 #include "smbconf_private.h"
23 #define INCLUDES_VALNAME "includes"
25 struct reg_private_data {
26 NT_USER_TOKEN *token;
27 bool open; /* did _we_ open the registry? */
30 /**********************************************************************
32 * helper functions
34 **********************************************************************/
36 /**
37 * a convenience helper to cast the private data structure
39 static struct reg_private_data *rpd(struct smbconf_ctx *ctx)
41 return (struct reg_private_data *)(ctx->data);
45 * check whether a given value name is forbidden in registry (smbconf)
47 static bool smbconf_reg_valname_forbidden(const char *valname)
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
57 const char **forbidden = NULL;
59 for (forbidden = forbidden_valnames; *forbidden != NULL; forbidden++) {
60 if (strwicmp(valname, *forbidden) == 0) {
61 return true;
64 return false;
67 static bool smbconf_reg_valname_valid(const char *valname)
69 return (lp_parameter_is_valid(valname) &&
70 !smbconf_reg_valname_forbidden(valname));
73 /**
74 * Open a registry key specified by "path"
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)
82 WERROR werr = WERR_OK;
84 if (ctx == NULL) {
85 DEBUG(1, ("Error: configuration is not open!\n"));
86 werr = WERR_INVALID_PARAM;
87 goto done;
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;
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;
103 if (path == NULL) {
104 DEBUG(1, ("Error: NULL path string given\n"));
105 werr = WERR_INVALID_PARAM;
106 goto done;
109 werr = reg_open_path(mem_ctx, path, desired_access, rpd(ctx)->token,
110 key);
112 if (!W_ERROR_IS_OK(werr)) {
113 DEBUG(1, ("Error opening registry path '%s': %s\n",
114 path, dos_errstr(werr)));
117 done:
118 return werr;
122 * Open a subkey of the base key (i.e a service)
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)
130 WERROR werr = WERR_OK;
131 char *path = NULL;
133 if (servicename == NULL) {
134 DEBUG(3, ("Error: NULL servicename given.\n"));
135 werr = WERR_INVALID_PARAM;
136 goto done;
139 path = talloc_asprintf(mem_ctx, "%s\\%s", ctx->path, servicename);
140 if (path == NULL) {
141 werr = WERR_NOMEM;
142 goto done;
145 werr = smbconf_reg_open_path(mem_ctx, ctx, path, desired_access, key);
147 done:
148 TALLOC_FREE(path);
149 return werr;
153 * open the base key
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)
160 return smbconf_reg_open_path(mem_ctx, ctx, ctx->path, desired_access,
161 key);
165 * check if a value exists in a given registry key
167 static bool smbconf_value_exists(struct registry_key *key, const char *param)
169 bool ret = false;
170 WERROR werr = WERR_OK;
171 TALLOC_CTX *ctx = talloc_stackframe();
172 struct registry_value *value = NULL;
174 werr = reg_queryvalue(ctx, key, param, &value);
175 if (W_ERROR_IS_OK(werr)) {
176 ret = true;
179 TALLOC_FREE(ctx);
180 return ret;
184 * create a subkey of the base key (i.e. a service...)
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)
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;
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;
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;
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;
216 if (!W_ERROR_IS_OK(werr)) {
217 DEBUG(5, ("Error creating key %s: %s\n",
218 subkeyname, dos_errstr(werr)));
221 done:
222 TALLOC_FREE(create_ctx);
223 return werr;
227 * add a value to a key.
229 static WERROR smbconf_reg_set_value(struct registry_key *key,
230 const char *valname,
231 const char *valstr)
233 struct registry_value val;
234 WERROR werr = WERR_OK;
235 char *subkeyname;
236 const char *canon_valname;
237 const char *canon_valstr;
239 if (!lp_canonicalize_parameter_with_value(valname, valstr,
240 &canon_valname,
241 &canon_valstr))
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));
250 werr = WERR_INVALID_PARAM;
251 goto done;
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;
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;
268 subkeyname++;
269 if (!strequal(subkeyname, GLOBAL_NAME) &&
270 lp_parameter_is_global(valname))
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;
279 ZERO_STRUCT(val);
281 val.type = REG_SZ;
282 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
283 val.v.sz.len = strlen(canon_valstr) + 1;
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)));
292 done:
293 return werr;
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)
301 WERROR werr;
302 struct registry_value *value;
303 uint32_t count;
304 TALLOC_CTX *tmp_ctx = talloc_stackframe();
306 if (strings == NULL) {
307 werr = WERR_INVALID_PARAM;
308 goto done;
311 value = TALLOC_ZERO_P(tmp_ctx, struct registry_value);
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;
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;
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)));
336 done:
337 TALLOC_FREE(tmp_ctx);
338 return werr;
342 * format a registry_value into a string.
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.
348 static char *smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
349 struct registry_value *value)
351 char *result = NULL;
353 /* alternatively, create a new talloc context? */
354 if (mem_ctx == NULL) {
355 return result;
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;
376 break;
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;
386 return result;
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)
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();
400 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
401 /* no includes */
402 goto done;
405 werr = reg_queryvalue(tmp_ctx, key, INCLUDES_VALNAME, &value);
406 if (!W_ERROR_IS_OK(werr)) {
407 goto done;
410 if (value->type != REG_MULTI_SZ) {
411 /* wront type -- ignore */
412 goto done;
415 for (count = 0; count < value->v.multi_sz.num_strings; count++)
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;
426 if (count > 0) {
427 *includes = talloc_move(mem_ctx, &tmp_includes);
428 if (*includes == NULL) {
429 werr = WERR_NOMEM;
430 goto done;
432 *num_includes = count;
433 } else {
434 *num_includes = 0;
435 *includes = NULL;
438 done:
439 TALLOC_FREE(tmp_ctx);
440 return werr;
444 * Get the values of a key as a list of value names
445 * and a list of value strings (ordered)
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)
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;
464 if ((num_values == NULL) || (value_names == NULL) ||
465 (value_strings == NULL))
467 werr = WERR_INVALID_PARAM;
468 goto done;
471 tmp_ctx = talloc_stackframe();
472 if (tmp_ctx == NULL) {
473 werr = WERR_NOMEM;
474 goto done;
477 for (count = 0;
478 werr = reg_enumvalue(tmp_ctx, key, count, &valname, &valvalue),
479 W_ERROR_IS_OK(werr);
480 count++)
482 char *valstring;
484 if (!smbconf_reg_valname_valid(valname)) {
485 continue;
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;
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;
501 tmp_num_values++;
503 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
504 goto done;
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;
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;
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;
527 tmp_num_values++;
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;
539 done:
540 TALLOC_FREE(tmp_ctx);
541 return werr;
544 /**********************************************************************
546 * smbconf operations: registry implementations
548 **********************************************************************/
551 * initialize the registry smbconf backend
553 static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
555 WERROR werr = WERR_OK;
557 if (path == NULL) {
558 path = KEY_SMBCONF;
560 ctx->path = talloc_strdup(ctx, path);
561 if (ctx->path == NULL) {
562 werr = WERR_NOMEM;
563 goto done;
566 ctx->data = TALLOC_ZERO_P(ctx, struct reg_private_data);
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;
574 rpd(ctx)->open = false;
576 if (!registry_init_smbconf()) {
577 werr = WERR_REG_IO_FAILURE;
578 goto done;
581 done:
582 return werr;
585 static int smbconf_reg_shutdown(struct smbconf_ctx *ctx)
587 return ctx->ops->close_conf(ctx);
590 static WERROR smbconf_reg_open(struct smbconf_ctx *ctx)
592 WERROR werr;
594 if (rpd(ctx)->open) {
595 return WERR_OK;
598 werr = regdb_open();
599 if (W_ERROR_IS_OK(werr)) {
600 rpd(ctx)->open = true;
602 return werr;
605 static int smbconf_reg_close(struct smbconf_ctx *ctx)
607 int ret;
609 if (!rpd(ctx)->open) {
610 return 0;
613 ret = regdb_close();
614 if (ret == 0) {
615 rpd(ctx)->open = false;
617 return ret;
621 * Get the change sequence number of the given service/parameter.
622 * service and parameter strings may be NULL.
624 static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
625 struct smbconf_csn *csn,
626 const char *service, const char *param)
628 if (csn == NULL) {
629 return;
632 if (!W_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
633 return;
636 csn->csn = (uint64_t)regdb_get_seqnum();
640 * Drop the whole configuration (restarting empty) - registry version
642 static WERROR smbconf_reg_drop(struct smbconf_ctx *ctx)
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;
651 path = talloc_strdup(mem_ctx, ctx->path);
652 if (path == NULL) {
653 werr = WERR_NOMEM;
654 goto done;
656 p = strrchr(path, '\\');
657 *p = '\0';
658 werr = smbconf_reg_open_path(mem_ctx, ctx, path, REG_KEY_WRITE,
659 &parent_key);
661 if (!W_ERROR_IS_OK(werr)) {
662 goto done;
665 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
667 if (!W_ERROR_IS_OK(werr)) {
668 goto done;
671 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
672 &new_key, &action);
674 done:
675 TALLOC_FREE(mem_ctx);
676 return werr;
680 * get the list of share names defined in the configuration.
681 * registry version.
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)
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;
696 if ((num_shares == NULL) || (share_names == NULL)) {
697 werr = WERR_INVALID_PARAM;
698 goto done;
701 tmp_ctx = talloc_stackframe();
702 if (tmp_ctx == NULL) {
703 werr = WERR_NOMEM;
704 goto done;
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;
714 added_count++;
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;
723 for (count = 0;
724 werr = reg_enumkey(tmp_ctx, key, count, &subkey_name, NULL),
725 W_ERROR_IS_OK(werr);
726 count++)
728 if (strequal(subkey_name, GLOBAL_NAME)) {
729 continue;
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;
739 added_count++;
741 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
742 goto done;
744 werr = WERR_OK;
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;
753 done:
754 TALLOC_FREE(tmp_ctx);
755 return werr;
759 * check if a share/service of a given name exists - registry version
761 static bool smbconf_reg_share_exists(struct smbconf_ctx *ctx,
762 const char *servicename)
764 bool ret = false;
765 WERROR werr = WERR_OK;
766 TALLOC_CTX *mem_ctx = talloc_stackframe();
767 struct registry_key *key = NULL;
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;
775 TALLOC_FREE(mem_ctx);
776 return ret;
780 * Add a service if it does not already exist - registry version
782 static WERROR smbconf_reg_create_share(struct smbconf_ctx *ctx,
783 const char *servicename)
785 WERROR werr;
786 TALLOC_CTX *mem_ctx = talloc_stackframe();
787 struct registry_key *key = NULL;
789 werr = smbconf_reg_create_service_key(mem_ctx, ctx, servicename, &key);
791 TALLOC_FREE(mem_ctx);
792 return werr;
796 * get a definition of a share (service) from configuration.
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)
804 WERROR werr = WERR_OK;
805 struct registry_key *key = NULL;
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;
813 werr = smbconf_reg_get_values(mem_ctx, key, num_params,
814 param_names, param_values);
816 done:
817 TALLOC_FREE(key);
818 return werr;
822 * delete a service from configuration
824 static WERROR smbconf_reg_delete_share(struct smbconf_ctx *ctx,
825 const char *servicename)
827 WERROR werr = WERR_OK;
828 struct registry_key *key = NULL;
829 TALLOC_CTX *mem_ctx = talloc_stackframe();
831 werr = smbconf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE, &key);
832 if (!W_ERROR_IS_OK(werr)) {
833 goto done;
836 werr = reg_deletekey_recursive(key, key, servicename);
838 done:
839 TALLOC_FREE(mem_ctx);
840 return werr;
844 * set a configuration parameter to the value provided.
846 static WERROR smbconf_reg_set_parameter(struct smbconf_ctx *ctx,
847 const char *service,
848 const char *param,
849 const char *valstr)
851 WERROR werr;
852 struct registry_key *key = NULL;
853 TALLOC_CTX *mem_ctx = talloc_stackframe();
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;
861 werr = smbconf_reg_set_value(key, param, valstr);
863 done:
864 TALLOC_FREE(mem_ctx);
865 return werr;
869 * get the value of a configuration parameter as a string
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)
877 WERROR werr = WERR_OK;
878 struct registry_key *key = NULL;
879 struct registry_value *value = NULL;
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;
887 if (!smbconf_reg_valname_valid(param)) {
888 werr = WERR_INVALID_PARAM;
889 goto done;
892 if (!smbconf_value_exists(key, param)) {
893 werr = WERR_INVALID_PARAM;
894 goto done;
897 werr = reg_queryvalue(mem_ctx, key, param, &value);
898 if (!W_ERROR_IS_OK(werr)) {
899 goto done;
902 *valstr = smbconf_format_registry_value(mem_ctx, value);
904 if (*valstr == NULL) {
905 werr = WERR_NOMEM;
908 done:
909 TALLOC_FREE(key);
910 TALLOC_FREE(value);
911 return werr;
915 * delete a parameter from configuration
917 static WERROR smbconf_reg_delete_parameter(struct smbconf_ctx *ctx,
918 const char *service,
919 const char *param)
921 struct registry_key *key = NULL;
922 WERROR werr = WERR_OK;
923 TALLOC_CTX *mem_ctx = talloc_stackframe();
925 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
926 REG_KEY_ALL, &key);
927 if (!W_ERROR_IS_OK(werr)) {
928 goto done;
931 if (!smbconf_reg_valname_valid(param)) {
932 werr = WERR_INVALID_PARAM;
933 goto done;
936 if (!smbconf_value_exists(key, param)) {
937 werr = WERR_INVALID_PARAM;
938 goto done;
941 werr = reg_deletevalue(key, param);
943 done:
944 TALLOC_FREE(mem_ctx);
945 return werr;
948 static WERROR smbconf_reg_get_includes(struct smbconf_ctx *ctx,
949 TALLOC_CTX *mem_ctx,
950 const char *service,
951 uint32_t *num_includes,
952 char ***includes)
954 WERROR werr;
955 struct registry_key *key = NULL;
956 TALLOC_CTX *tmp_ctx = talloc_stackframe();
958 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
959 REG_KEY_READ, &key);
960 if (!W_ERROR_IS_OK(werr)) {
961 goto done;
964 werr = smbconf_reg_get_includes_internal(mem_ctx, key, num_includes,
965 includes);
967 done:
968 TALLOC_FREE(tmp_ctx);
969 return werr;
972 static WERROR smbconf_reg_set_includes(struct smbconf_ctx *ctx,
973 const char *service,
974 uint32_t num_includes,
975 const char **includes)
977 WERROR werr = WERR_OK;
978 struct registry_key *key = NULL;
979 TALLOC_CTX *tmp_ctx = talloc_stackframe();
981 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
982 REG_KEY_ALL, &key);
983 if (!W_ERROR_IS_OK(werr)) {
984 goto done;
987 if (num_includes == 0) {
988 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
989 goto done;
991 werr = reg_deletevalue(key, INCLUDES_VALNAME);
992 } else {
993 werr = smbconf_reg_set_multi_sz_value(key, INCLUDES_VALNAME,
994 num_includes, includes);
997 done:
998 TALLOC_FREE(tmp_ctx);
999 return werr;
1002 static WERROR smbconf_reg_delete_includes(struct smbconf_ctx *ctx,
1003 const char *service)
1005 WERROR werr = WERR_OK;
1006 struct registry_key *key = NULL;
1007 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1009 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1010 REG_KEY_ALL, &key);
1011 if (!W_ERROR_IS_OK(werr)) {
1012 goto done;
1015 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
1016 goto done;
1019 werr = reg_deletevalue(key, INCLUDES_VALNAME);
1022 done:
1023 TALLOC_FREE(tmp_ctx);
1024 return werr;
1027 struct smbconf_ops smbconf_ops_reg = {
1028 .init = smbconf_reg_init,
1029 .shutdown = smbconf_reg_shutdown,
1030 .open_conf = smbconf_reg_open,
1031 .close_conf = smbconf_reg_close,
1032 .get_csn = smbconf_reg_get_csn,
1033 .drop = smbconf_reg_drop,
1034 .get_share_names = smbconf_reg_get_share_names,
1035 .share_exists = smbconf_reg_share_exists,
1036 .create_share = smbconf_reg_create_share,
1037 .get_share = smbconf_reg_get_share,
1038 .delete_share = smbconf_reg_delete_share,
1039 .set_parameter = smbconf_reg_set_parameter,
1040 .get_parameter = smbconf_reg_get_parameter,
1041 .delete_parameter = smbconf_reg_delete_parameter,
1042 .get_includes = smbconf_reg_get_includes,
1043 .set_includes = smbconf_reg_set_includes,
1044 .delete_includes = smbconf_reg_delete_includes,
1049 * initialize the smbconf registry backend
1050 * the only function that is exported from this module
1052 WERROR smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
1053 const char *path)
1055 return smbconf_init(mem_ctx, conf_ctx, path, &smbconf_ops_reg);