s3:registry: extract the reg_backend_db prototypes into their own header.
[Samba/ekacnet.git] / source3 / lib / smbconf / smbconf_reg.c
blobf6602676e21f10be7057b93e9c05d914bb635e0c
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 "lib/smbconf/smbconf_private.h"
22 #include "registry.h"
23 #include "registry/reg_backend_db.h"
24 #include "lib/smbconf/smbconf_init.h"
25 #include "lib/smbconf/smbconf_reg.h"
27 #define INCLUDES_VALNAME "includes"
29 struct reg_private_data {
30 struct registry_key *base_key;
31 bool open; /* did _we_ open the registry? */
34 /**********************************************************************
36 * helper functions
38 **********************************************************************/
40 /**
41 * a convenience helper to cast the private data structure
43 static struct reg_private_data *rpd(struct smbconf_ctx *ctx)
45 return (struct reg_private_data *)(ctx->data);
49 * check whether a given value name is forbidden in registry (smbconf)
51 static bool smbconf_reg_valname_forbidden(const char *valname)
53 /* hard code the list of forbidden names here for now */
54 const char *forbidden_valnames[] = {
55 "lock directory",
56 "lock dir",
57 "config backend",
58 "include",
59 "includes", /* this has a special meaning internally */
60 NULL
62 const char **forbidden = NULL;
64 for (forbidden = forbidden_valnames; *forbidden != NULL; forbidden++) {
65 if (strwicmp(valname, *forbidden) == 0) {
66 return true;
69 return false;
72 static bool smbconf_reg_valname_valid(const char *valname)
74 return (!smbconf_reg_valname_forbidden(valname) &&
75 lp_parameter_is_valid(valname));
78 /**
79 * Open a subkey of the base key (i.e a service)
81 static WERROR smbconf_reg_open_service_key(TALLOC_CTX *mem_ctx,
82 struct smbconf_ctx *ctx,
83 const char *servicename,
84 uint32 desired_access,
85 struct registry_key **key)
87 WERROR werr;
89 if (servicename == NULL) {
90 *key = rpd(ctx)->base_key;
91 return WERR_OK;
93 werr = reg_openkey(mem_ctx, rpd(ctx)->base_key, servicename,
94 desired_access, key);
96 if (W_ERROR_EQUAL(werr, WERR_BADFILE)) {
97 werr = WERR_NO_SUCH_SERVICE;
100 return werr;
104 * check if a value exists in a given registry key
106 static bool smbconf_value_exists(struct registry_key *key, const char *param)
108 bool ret = false;
109 WERROR werr = WERR_OK;
110 TALLOC_CTX *ctx = talloc_stackframe();
111 struct registry_value *value = NULL;
113 werr = reg_queryvalue(ctx, key, param, &value);
114 if (W_ERROR_IS_OK(werr)) {
115 ret = true;
118 talloc_free(ctx);
119 return ret;
123 * create a subkey of the base key (i.e. a service...)
125 static WERROR smbconf_reg_create_service_key(TALLOC_CTX *mem_ctx,
126 struct smbconf_ctx *ctx,
127 const char * subkeyname,
128 struct registry_key **newkey)
130 WERROR werr = WERR_OK;
131 TALLOC_CTX *create_ctx;
132 enum winreg_CreateAction action = REG_ACTION_NONE;
134 /* create a new talloc ctx for creation. it will hold
135 * the intermediate parent key (SMBCONF) for creation
136 * and will be destroyed when leaving this function... */
137 create_ctx = talloc_stackframe();
139 werr = reg_createkey(mem_ctx, rpd(ctx)->base_key, subkeyname,
140 REG_KEY_WRITE, newkey, &action);
141 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
142 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
143 werr = WERR_FILE_EXISTS;
145 if (!W_ERROR_IS_OK(werr)) {
146 DEBUG(5, ("Error creating key %s: %s\n",
147 subkeyname, win_errstr(werr)));
150 talloc_free(create_ctx);
151 return werr;
155 * add a value to a key.
157 static WERROR smbconf_reg_set_value(struct registry_key *key,
158 const char *valname,
159 const char *valstr)
161 struct registry_value val;
162 WERROR werr = WERR_OK;
163 char *subkeyname;
164 const char *canon_valname;
165 const char *canon_valstr;
167 if (!lp_canonicalize_parameter_with_value(valname, valstr,
168 &canon_valname,
169 &canon_valstr))
171 if (canon_valname == NULL) {
172 DEBUG(5, ("invalid parameter '%s' given\n",
173 valname));
174 } else {
175 DEBUG(5, ("invalid value '%s' given for "
176 "parameter '%s'\n", valstr, valname));
178 werr = WERR_INVALID_PARAM;
179 goto done;
182 if (smbconf_reg_valname_forbidden(canon_valname)) {
183 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
184 canon_valname));
185 werr = WERR_INVALID_PARAM;
186 goto done;
189 subkeyname = strrchr_m(key->key->name, '\\');
190 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
191 DEBUG(5, ("Invalid registry key '%s' given as "
192 "smbconf section.\n", key->key->name));
193 werr = WERR_INVALID_PARAM;
194 goto done;
196 subkeyname++;
197 if (!strequal(subkeyname, GLOBAL_NAME) &&
198 lp_parameter_is_global(valname))
200 DEBUG(5, ("Global parameter '%s' not allowed in "
201 "service definition ('%s').\n", canon_valname,
202 subkeyname));
203 werr = WERR_INVALID_PARAM;
204 goto done;
207 ZERO_STRUCT(val);
209 val.type = REG_SZ;
210 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
211 val.v.sz.len = strlen(canon_valstr) + 1;
213 werr = reg_setvalue(key, canon_valname, &val);
214 if (!W_ERROR_IS_OK(werr)) {
215 DEBUG(5, ("Error adding value '%s' to "
216 "key '%s': %s\n",
217 canon_valname, key->key->name, win_errstr(werr)));
220 done:
221 return werr;
224 static WERROR smbconf_reg_set_multi_sz_value(struct registry_key *key,
225 const char *valname,
226 const uint32_t num_strings,
227 const char **strings)
229 WERROR werr;
230 struct registry_value *value;
231 uint32_t count;
232 TALLOC_CTX *tmp_ctx = talloc_stackframe();
234 if (strings == NULL) {
235 werr = WERR_INVALID_PARAM;
236 goto done;
239 value = TALLOC_ZERO_P(tmp_ctx, struct registry_value);
241 value->type = REG_MULTI_SZ;
242 value->v.multi_sz.num_strings = num_strings;
243 value->v.multi_sz.strings = TALLOC_ARRAY(tmp_ctx, char *, num_strings);
244 if (value->v.multi_sz.strings == NULL) {
245 werr = WERR_NOMEM;
246 goto done;
248 for (count = 0; count < num_strings; count++) {
249 value->v.multi_sz.strings[count] =
250 talloc_strdup(value->v.multi_sz.strings,
251 strings[count]);
252 if (value->v.multi_sz.strings[count] == NULL) {
253 werr = WERR_NOMEM;
254 goto done;
258 werr = reg_setvalue(key, valname, value);
259 if (!W_ERROR_IS_OK(werr)) {
260 DEBUG(5, ("Error adding value '%s' to key '%s': %s\n",
261 valname, key->key->name, win_errstr(werr)));
264 done:
265 talloc_free(tmp_ctx);
266 return werr;
270 * format a registry_value into a string.
272 * This is intended to be used for smbconf registry values,
273 * which are ar stored as REG_SZ values, so the incomplete
274 * handling should be ok.
276 static char *smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
277 struct registry_value *value)
279 char *result = NULL;
281 /* alternatively, create a new talloc context? */
282 if (mem_ctx == NULL) {
283 return result;
286 switch (value->type) {
287 case REG_DWORD:
288 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
289 break;
290 case REG_SZ:
291 case REG_EXPAND_SZ:
292 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
293 break;
294 case REG_MULTI_SZ: {
295 uint32 j;
296 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
297 result = talloc_asprintf(mem_ctx, "%s\"%s\" ",
298 result ? result : "" ,
299 value->v.multi_sz.strings[j]);
300 if (result == NULL) {
301 break;
304 break;
306 case REG_BINARY:
307 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
308 (int)value->v.binary.length);
309 break;
310 default:
311 result = talloc_asprintf(mem_ctx, "<unprintable>");
312 break;
314 return result;
317 static WERROR smbconf_reg_get_includes_internal(TALLOC_CTX *mem_ctx,
318 struct registry_key *key,
319 uint32_t *num_includes,
320 char ***includes)
322 WERROR werr;
323 uint32_t count;
324 struct registry_value *value = NULL;
325 char **tmp_includes = NULL;
326 TALLOC_CTX *tmp_ctx = talloc_stackframe();
328 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
329 /* no includes */
330 *num_includes = 0;
331 *includes = NULL;
332 werr = WERR_OK;
333 goto done;
336 werr = reg_queryvalue(tmp_ctx, key, INCLUDES_VALNAME, &value);
337 if (!W_ERROR_IS_OK(werr)) {
338 goto done;
341 if (value->type != REG_MULTI_SZ) {
342 /* wrong type -- ignore */
343 goto done;
346 for (count = 0; count < value->v.multi_sz.num_strings; count++)
348 werr = smbconf_add_string_to_array(tmp_ctx,
349 &tmp_includes,
350 count,
351 value->v.multi_sz.strings[count]);
352 if (!W_ERROR_IS_OK(werr)) {
353 goto done;
357 if (count > 0) {
358 *includes = talloc_move(mem_ctx, &tmp_includes);
359 if (*includes == NULL) {
360 werr = WERR_NOMEM;
361 goto done;
363 *num_includes = count;
364 } else {
365 *num_includes = 0;
366 *includes = NULL;
369 done:
370 talloc_free(tmp_ctx);
371 return werr;
375 * Get the values of a key as a list of value names
376 * and a list of value strings (ordered)
378 static WERROR smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
379 struct registry_key *key,
380 uint32_t *num_values,
381 char ***value_names,
382 char ***value_strings)
384 TALLOC_CTX *tmp_ctx = NULL;
385 WERROR werr = WERR_OK;
386 uint32_t count;
387 struct registry_value *valvalue = NULL;
388 char *valname = NULL;
389 uint32_t tmp_num_values = 0;
390 char **tmp_valnames = NULL;
391 char **tmp_valstrings = NULL;
392 uint32_t num_includes = 0;
393 char **includes = NULL;
395 if ((num_values == NULL) || (value_names == NULL) ||
396 (value_strings == NULL))
398 werr = WERR_INVALID_PARAM;
399 goto done;
402 tmp_ctx = talloc_stackframe();
404 for (count = 0;
405 werr = reg_enumvalue(tmp_ctx, key, count, &valname, &valvalue),
406 W_ERROR_IS_OK(werr);
407 count++)
409 char *valstring;
411 if (!smbconf_reg_valname_valid(valname)) {
412 continue;
415 werr = smbconf_add_string_to_array(tmp_ctx,
416 &tmp_valnames,
417 tmp_num_values, valname);
418 if (!W_ERROR_IS_OK(werr)) {
419 goto done;
422 valstring = smbconf_format_registry_value(tmp_ctx, valvalue);
423 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
424 tmp_num_values, valstring);
425 if (!W_ERROR_IS_OK(werr)) {
426 goto done;
428 tmp_num_values++;
430 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
431 goto done;
434 /* now add the includes at the end */
435 werr = smbconf_reg_get_includes_internal(tmp_ctx, key, &num_includes,
436 &includes);
437 if (!W_ERROR_IS_OK(werr)) {
438 goto done;
440 for (count = 0; count < num_includes; count++) {
441 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valnames,
442 tmp_num_values, "include");
443 if (!W_ERROR_IS_OK(werr)) {
444 goto done;
447 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
448 tmp_num_values,
449 includes[count]);
450 if (!W_ERROR_IS_OK(werr)) {
451 goto done;
454 tmp_num_values++;
457 *num_values = tmp_num_values;
458 if (tmp_num_values > 0) {
459 *value_names = talloc_move(mem_ctx, &tmp_valnames);
460 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
461 } else {
462 *value_names = NULL;
463 *value_strings = NULL;
466 done:
467 talloc_free(tmp_ctx);
468 return werr;
471 static bool smbconf_reg_key_has_values(struct registry_key *key)
473 WERROR werr;
474 uint32_t num_subkeys;
475 uint32_t max_subkeylen;
476 uint32_t max_subkeysize;
477 uint32_t num_values;
478 uint32_t max_valnamelen;
479 uint32_t max_valbufsize;
480 uint32_t secdescsize;
481 NTTIME last_changed_time;
483 werr = reg_queryinfokey(key, &num_subkeys, &max_subkeylen,
484 &max_subkeysize, &num_values, &max_valnamelen,
485 &max_valbufsize, &secdescsize,
486 &last_changed_time);
487 if (!W_ERROR_IS_OK(werr)) {
488 return false;
491 return (num_values != 0);
495 * delete all values from a key
497 static WERROR smbconf_reg_delete_values(struct registry_key *key)
499 WERROR werr;
500 char *valname;
501 struct registry_value *valvalue;
502 uint32_t count;
503 TALLOC_CTX *mem_ctx = talloc_stackframe();
505 for (count = 0;
506 werr = reg_enumvalue(mem_ctx, key, count, &valname, &valvalue),
507 W_ERROR_IS_OK(werr);
508 count++)
510 werr = reg_deletevalue(key, valname);
511 if (!W_ERROR_IS_OK(werr)) {
512 goto done;
515 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
516 DEBUG(1, ("smbconf_reg_delete_values: "
517 "Error enumerating values of %s: %s\n",
518 key->key->name,
519 win_errstr(werr)));
520 goto done;
523 werr = WERR_OK;
525 done:
526 talloc_free(mem_ctx);
527 return werr;
530 /**********************************************************************
532 * smbconf operations: registry implementations
534 **********************************************************************/
537 * initialize the registry smbconf backend
539 static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
541 WERROR werr = WERR_OK;
542 struct nt_user_token *token;
544 if (path == NULL) {
545 path = KEY_SMBCONF;
547 ctx->path = talloc_strdup(ctx, path);
548 if (ctx->path == NULL) {
549 werr = WERR_NOMEM;
550 goto done;
553 ctx->data = TALLOC_ZERO_P(ctx, struct reg_private_data);
555 werr = ntstatus_to_werror(registry_create_admin_token(ctx, &token));
556 if (!W_ERROR_IS_OK(werr)) {
557 DEBUG(1, ("Error creating admin token\n"));
558 goto done;
560 rpd(ctx)->open = false;
562 werr = registry_init_smbconf(path);
563 if (!W_ERROR_IS_OK(werr)) {
564 goto done;
567 werr = ctx->ops->open_conf(ctx);
568 if (!W_ERROR_IS_OK(werr)) {
569 DEBUG(1, ("Error opening the registry.\n"));
570 goto done;
573 werr = reg_open_path(ctx, ctx->path,
574 KEY_ENUMERATE_SUB_KEYS | REG_KEY_WRITE,
575 token, &rpd(ctx)->base_key);
576 if (!W_ERROR_IS_OK(werr)) {
577 goto done;
580 done:
581 return werr;
584 static int smbconf_reg_shutdown(struct smbconf_ctx *ctx)
586 return ctx->ops->close_conf(ctx);
589 static bool smbconf_reg_requires_messaging(struct smbconf_ctx *ctx)
591 #ifdef CLUSTER_SUPPORT
592 if (lp_clustering() && lp_parm_bool(-1, "ctdb", "registry.tdb", true)) {
593 return true;
595 #endif
596 return false;
599 static bool smbconf_reg_is_writeable(struct smbconf_ctx *ctx)
602 * The backend has write support.
604 * TODO: add access checks whether the concrete
605 * config source is really writeable by the calling user.
607 return true;
610 static WERROR smbconf_reg_open(struct smbconf_ctx *ctx)
612 WERROR werr;
614 if (rpd(ctx)->open) {
615 return WERR_OK;
618 werr = regdb_open();
619 if (W_ERROR_IS_OK(werr)) {
620 rpd(ctx)->open = true;
622 return werr;
625 static int smbconf_reg_close(struct smbconf_ctx *ctx)
627 int ret;
629 if (!rpd(ctx)->open) {
630 return 0;
633 ret = regdb_close();
634 if (ret == 0) {
635 rpd(ctx)->open = false;
637 return ret;
641 * Get the change sequence number of the given service/parameter.
642 * service and parameter strings may be NULL.
644 static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
645 struct smbconf_csn *csn,
646 const char *service, const char *param)
648 if (csn == NULL) {
649 return;
652 if (!W_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
653 return;
656 csn->csn = (uint64_t)regdb_get_seqnum();
660 * Drop the whole configuration (restarting empty) - registry version
662 static WERROR smbconf_reg_drop(struct smbconf_ctx *ctx)
664 char *path, *p;
665 WERROR werr = WERR_OK;
666 struct registry_key *parent_key = NULL;
667 struct registry_key *new_key = NULL;
668 TALLOC_CTX* mem_ctx = talloc_stackframe();
669 enum winreg_CreateAction action;
670 struct nt_user_token *token;
672 werr = ntstatus_to_werror(registry_create_admin_token(ctx, &token));
673 if (!W_ERROR_IS_OK(werr)) {
674 DEBUG(1, ("Error creating admin token\n"));
675 goto done;
678 path = talloc_strdup(mem_ctx, ctx->path);
679 if (path == NULL) {
680 werr = WERR_NOMEM;
681 goto done;
683 p = strrchr(path, '\\');
684 *p = '\0';
685 werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token,
686 &parent_key);
688 if (!W_ERROR_IS_OK(werr)) {
689 goto done;
692 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
694 if (!W_ERROR_IS_OK(werr)) {
695 goto done;
698 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
699 &new_key, &action);
701 done:
702 talloc_free(mem_ctx);
703 return werr;
707 * get the list of share names defined in the configuration.
708 * registry version.
710 static WERROR smbconf_reg_get_share_names(struct smbconf_ctx *ctx,
711 TALLOC_CTX *mem_ctx,
712 uint32_t *num_shares,
713 char ***share_names)
715 uint32_t count;
716 uint32_t added_count = 0;
717 TALLOC_CTX *tmp_ctx = NULL;
718 WERROR werr = WERR_OK;
719 char *subkey_name = NULL;
720 char **tmp_share_names = NULL;
722 if ((num_shares == NULL) || (share_names == NULL)) {
723 werr = WERR_INVALID_PARAM;
724 goto done;
727 tmp_ctx = talloc_stackframe();
729 /* if there are values in the base key, return NULL as share name */
731 if (smbconf_reg_key_has_values(rpd(ctx)->base_key)) {
732 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
733 0, NULL);
734 if (!W_ERROR_IS_OK(werr)) {
735 goto done;
737 added_count++;
740 /* make sure "global" is always listed first */
741 if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
742 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
743 added_count, GLOBAL_NAME);
744 if (!W_ERROR_IS_OK(werr)) {
745 goto done;
747 added_count++;
750 for (count = 0;
751 werr = reg_enumkey(tmp_ctx, rpd(ctx)->base_key, count,
752 &subkey_name, NULL),
753 W_ERROR_IS_OK(werr);
754 count++)
756 if (strequal(subkey_name, GLOBAL_NAME)) {
757 continue;
760 werr = smbconf_add_string_to_array(tmp_ctx,
761 &tmp_share_names,
762 added_count,
763 subkey_name);
764 if (!W_ERROR_IS_OK(werr)) {
765 goto done;
767 added_count++;
769 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
770 goto done;
772 werr = WERR_OK;
774 *num_shares = added_count;
775 if (added_count > 0) {
776 *share_names = talloc_move(mem_ctx, &tmp_share_names);
777 } else {
778 *share_names = NULL;
781 done:
782 talloc_free(tmp_ctx);
783 return werr;
787 * check if a share/service of a given name exists - registry version
789 static bool smbconf_reg_share_exists(struct smbconf_ctx *ctx,
790 const char *servicename)
792 bool ret = false;
793 WERROR werr = WERR_OK;
794 TALLOC_CTX *mem_ctx = talloc_stackframe();
795 struct registry_key *key = NULL;
797 werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
798 REG_KEY_READ, &key);
799 if (W_ERROR_IS_OK(werr)) {
800 ret = true;
803 talloc_free(mem_ctx);
804 return ret;
808 * Add a service if it does not already exist - registry version
810 static WERROR smbconf_reg_create_share(struct smbconf_ctx *ctx,
811 const char *servicename)
813 WERROR werr;
814 struct registry_key *key = NULL;
816 if (servicename == NULL) {
817 return WERR_OK;
820 werr = smbconf_reg_create_service_key(talloc_tos(), ctx,
821 servicename, &key);
823 talloc_free(key);
824 return werr;
828 * get a definition of a share (service) from configuration.
830 static WERROR smbconf_reg_get_share(struct smbconf_ctx *ctx,
831 TALLOC_CTX *mem_ctx,
832 const char *servicename,
833 struct smbconf_service **service)
835 WERROR werr = WERR_OK;
836 struct registry_key *key = NULL;
837 struct smbconf_service *tmp_service = NULL;
838 TALLOC_CTX *tmp_ctx = talloc_stackframe();
840 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, servicename,
841 REG_KEY_READ, &key);
842 if (!W_ERROR_IS_OK(werr)) {
843 goto done;
846 tmp_service = TALLOC_ZERO_P(tmp_ctx, struct smbconf_service);
847 if (tmp_service == NULL) {
848 werr = WERR_NOMEM;
849 goto done;
852 if (servicename != NULL) {
853 tmp_service->name = talloc_strdup(tmp_service, servicename);
854 if (tmp_service->name == NULL) {
855 werr = WERR_NOMEM;
856 goto done;
860 werr = smbconf_reg_get_values(tmp_service, key,
861 &(tmp_service->num_params),
862 &(tmp_service->param_names),
863 &(tmp_service->param_values));
865 if (W_ERROR_IS_OK(werr)) {
866 *service = talloc_move(mem_ctx, &tmp_service);
869 done:
870 talloc_free(tmp_ctx);
871 return werr;
875 * delete a service from configuration
877 static WERROR smbconf_reg_delete_share(struct smbconf_ctx *ctx,
878 const char *servicename)
880 WERROR werr = WERR_OK;
881 TALLOC_CTX *mem_ctx = talloc_stackframe();
883 if (servicename != NULL) {
884 werr = reg_deletekey_recursive(mem_ctx, rpd(ctx)->base_key,
885 servicename);
886 } else {
887 werr = smbconf_reg_delete_values(rpd(ctx)->base_key);
890 talloc_free(mem_ctx);
891 return werr;
895 * set a configuration parameter to the value provided.
897 static WERROR smbconf_reg_set_parameter(struct smbconf_ctx *ctx,
898 const char *service,
899 const char *param,
900 const char *valstr)
902 WERROR werr;
903 struct registry_key *key = NULL;
904 TALLOC_CTX *mem_ctx = talloc_stackframe();
906 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
907 REG_KEY_WRITE, &key);
908 if (!W_ERROR_IS_OK(werr)) {
909 goto done;
912 werr = smbconf_reg_set_value(key, param, valstr);
914 done:
915 talloc_free(mem_ctx);
916 return werr;
920 * get the value of a configuration parameter as a string
922 static WERROR smbconf_reg_get_parameter(struct smbconf_ctx *ctx,
923 TALLOC_CTX *mem_ctx,
924 const char *service,
925 const char *param,
926 char **valstr)
928 WERROR werr = WERR_OK;
929 struct registry_key *key = NULL;
930 struct registry_value *value = NULL;
932 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
933 REG_KEY_READ, &key);
934 if (!W_ERROR_IS_OK(werr)) {
935 goto done;
938 if (!smbconf_reg_valname_valid(param)) {
939 werr = WERR_INVALID_PARAM;
940 goto done;
943 if (!smbconf_value_exists(key, param)) {
944 werr = WERR_INVALID_PARAM;
945 goto done;
948 werr = reg_queryvalue(mem_ctx, key, param, &value);
949 if (!W_ERROR_IS_OK(werr)) {
950 goto done;
953 *valstr = smbconf_format_registry_value(mem_ctx, value);
955 if (*valstr == NULL) {
956 werr = WERR_NOMEM;
959 done:
960 talloc_free(key);
961 talloc_free(value);
962 return werr;
966 * delete a parameter from configuration
968 static WERROR smbconf_reg_delete_parameter(struct smbconf_ctx *ctx,
969 const char *service,
970 const char *param)
972 struct registry_key *key = NULL;
973 WERROR werr = WERR_OK;
974 TALLOC_CTX *mem_ctx = talloc_stackframe();
976 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
977 REG_KEY_ALL, &key);
978 if (!W_ERROR_IS_OK(werr)) {
979 goto done;
982 if (!smbconf_reg_valname_valid(param)) {
983 werr = WERR_INVALID_PARAM;
984 goto done;
987 if (!smbconf_value_exists(key, param)) {
988 werr = WERR_INVALID_PARAM;
989 goto done;
992 werr = reg_deletevalue(key, param);
994 done:
995 talloc_free(mem_ctx);
996 return werr;
999 static WERROR smbconf_reg_get_includes(struct smbconf_ctx *ctx,
1000 TALLOC_CTX *mem_ctx,
1001 const char *service,
1002 uint32_t *num_includes,
1003 char ***includes)
1005 WERROR werr;
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_READ, &key);
1011 if (!W_ERROR_IS_OK(werr)) {
1012 goto done;
1015 werr = smbconf_reg_get_includes_internal(mem_ctx, key, num_includes,
1016 includes);
1018 done:
1019 talloc_free(tmp_ctx);
1020 return werr;
1023 static WERROR smbconf_reg_set_includes(struct smbconf_ctx *ctx,
1024 const char *service,
1025 uint32_t num_includes,
1026 const char **includes)
1028 WERROR werr = WERR_OK;
1029 struct registry_key *key = NULL;
1030 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1032 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1033 REG_KEY_ALL, &key);
1034 if (!W_ERROR_IS_OK(werr)) {
1035 goto done;
1038 if (num_includes == 0) {
1039 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
1040 goto done;
1042 werr = reg_deletevalue(key, INCLUDES_VALNAME);
1043 } else {
1044 werr = smbconf_reg_set_multi_sz_value(key, INCLUDES_VALNAME,
1045 num_includes, includes);
1048 done:
1049 talloc_free(tmp_ctx);
1050 return werr;
1053 static WERROR smbconf_reg_delete_includes(struct smbconf_ctx *ctx,
1054 const char *service)
1056 WERROR werr = WERR_OK;
1057 struct registry_key *key = NULL;
1058 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1060 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1061 REG_KEY_ALL, &key);
1062 if (!W_ERROR_IS_OK(werr)) {
1063 goto done;
1066 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
1067 goto done;
1070 werr = reg_deletevalue(key, INCLUDES_VALNAME);
1073 done:
1074 talloc_free(tmp_ctx);
1075 return werr;
1078 static WERROR smbconf_reg_transaction_start(struct smbconf_ctx *ctx)
1080 return regdb_transaction_start();
1083 static WERROR smbconf_reg_transaction_commit(struct smbconf_ctx *ctx)
1085 return regdb_transaction_commit();
1088 static WERROR smbconf_reg_transaction_cancel(struct smbconf_ctx *ctx)
1090 return regdb_transaction_cancel();
1093 struct smbconf_ops smbconf_ops_reg = {
1094 .init = smbconf_reg_init,
1095 .shutdown = smbconf_reg_shutdown,
1096 .requires_messaging = smbconf_reg_requires_messaging,
1097 .is_writeable = smbconf_reg_is_writeable,
1098 .open_conf = smbconf_reg_open,
1099 .close_conf = smbconf_reg_close,
1100 .get_csn = smbconf_reg_get_csn,
1101 .drop = smbconf_reg_drop,
1102 .get_share_names = smbconf_reg_get_share_names,
1103 .share_exists = smbconf_reg_share_exists,
1104 .create_share = smbconf_reg_create_share,
1105 .get_share = smbconf_reg_get_share,
1106 .delete_share = smbconf_reg_delete_share,
1107 .set_parameter = smbconf_reg_set_parameter,
1108 .get_parameter = smbconf_reg_get_parameter,
1109 .delete_parameter = smbconf_reg_delete_parameter,
1110 .get_includes = smbconf_reg_get_includes,
1111 .set_includes = smbconf_reg_set_includes,
1112 .delete_includes = smbconf_reg_delete_includes,
1113 .transaction_start = smbconf_reg_transaction_start,
1114 .transaction_commit = smbconf_reg_transaction_commit,
1115 .transaction_cancel = smbconf_reg_transaction_cancel,
1120 * initialize the smbconf registry backend
1121 * the only function that is exported from this module
1123 WERROR smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
1124 const char *path)
1126 return smbconf_init_internal(mem_ctx, conf_ctx, path, &smbconf_ops_reg);