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/>.
21 #include "smbconf_private.h"
23 #define INCLUDES_VALNAME "includes"
25 struct reg_private_data {
27 bool open; /* did _we_ open the registry? */
30 /**********************************************************************
34 **********************************************************************/
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 bool registry_smbconf_valname_forbidden(const char *valname)
49 /* hard code the list of forbidden names here for now */
50 const char *forbidden_valnames[] = {
56 const char **forbidden = NULL;
58 for (forbidden = forbidden_valnames; *forbidden != NULL; forbidden++) {
59 if (strwicmp(valname, *forbidden) == 0) {
67 * Open a registry key specified by "path"
69 static WERROR smbconf_reg_open_path(TALLOC_CTX *mem_ctx,
70 struct smbconf_ctx *ctx,
72 uint32 desired_access,
73 struct registry_key **key)
75 WERROR werr = WERR_OK;
78 DEBUG(1, ("Error: configuration is not open!\n"));
79 werr = WERR_INVALID_PARAM;
83 if (rpd(ctx)->token == NULL) {
84 DEBUG(1, ("Error: token missing from smbconf_ctx. "
85 "was smbconf_init() called?\n"));
86 werr = WERR_INVALID_PARAM;
90 werr = ctx->ops->open_conf(ctx);
91 if (!W_ERROR_IS_OK(werr)) {
92 DEBUG(1, ("Error opening the registry.\n"));
97 DEBUG(1, ("Error: NULL path string given\n"));
98 werr = WERR_INVALID_PARAM;
102 werr = reg_open_path(mem_ctx, path, desired_access, rpd(ctx)->token,
105 if (!W_ERROR_IS_OK(werr)) {
106 DEBUG(1, ("Error opening registry path '%s': %s\n",
107 path, dos_errstr(werr)));
115 * Open a subkey of the base key (i.e a service)
117 static WERROR smbconf_reg_open_service_key(TALLOC_CTX *mem_ctx,
118 struct smbconf_ctx *ctx,
119 const char *servicename,
120 uint32 desired_access,
121 struct registry_key **key)
123 WERROR werr = WERR_OK;
126 if (servicename == NULL) {
127 DEBUG(3, ("Error: NULL servicename given.\n"));
128 werr = WERR_INVALID_PARAM;
132 path = talloc_asprintf(mem_ctx, "%s\\%s", ctx->path, servicename);
138 werr = smbconf_reg_open_path(mem_ctx, ctx, path, desired_access, key);
148 static WERROR smbconf_reg_open_base_key(TALLOC_CTX *mem_ctx,
149 struct smbconf_ctx *ctx,
150 uint32 desired_access,
151 struct registry_key **key)
153 return smbconf_reg_open_path(mem_ctx, ctx, ctx->path, desired_access,
158 * check if a value exists in a given registry key
160 static bool smbconf_value_exists(struct registry_key *key, const char *param)
163 WERROR werr = WERR_OK;
164 TALLOC_CTX *ctx = talloc_stackframe();
165 struct registry_value *value = NULL;
167 werr = reg_queryvalue(ctx, key, param, &value);
168 if (W_ERROR_IS_OK(werr)) {
177 * create a subkey of the base key (i.e. a service...)
179 static WERROR smbconf_reg_create_service_key(TALLOC_CTX *mem_ctx,
180 struct smbconf_ctx *ctx,
181 const char * subkeyname,
182 struct registry_key **newkey)
184 WERROR werr = WERR_OK;
185 struct registry_key *create_parent = NULL;
186 TALLOC_CTX *create_ctx;
187 enum winreg_CreateAction action = REG_ACTION_NONE;
189 /* create a new talloc ctx for creation. it will hold
190 * the intermediate parent key (SMBCONF) for creation
191 * and will be destroyed when leaving this function... */
192 if (!(create_ctx = talloc_stackframe())) {
197 werr = smbconf_reg_open_base_key(create_ctx, ctx, REG_KEY_WRITE,
199 if (!W_ERROR_IS_OK(werr)) {
203 werr = reg_createkey(mem_ctx, create_parent, subkeyname,
204 REG_KEY_WRITE, newkey, &action);
205 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
206 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
207 werr = WERR_ALREADY_EXISTS;
209 if (!W_ERROR_IS_OK(werr)) {
210 DEBUG(5, ("Error creating key %s: %s\n",
211 subkeyname, dos_errstr(werr)));
215 TALLOC_FREE(create_ctx);
220 * add a value to a key.
222 static WERROR smbconf_reg_set_value(struct registry_key *key,
226 struct registry_value val;
227 WERROR werr = WERR_OK;
229 const char *canon_valname;
230 const char *canon_valstr;
232 if (!lp_canonicalize_parameter_with_value(valname, valstr,
236 if (canon_valname == NULL) {
237 DEBUG(5, ("invalid parameter '%s' given\n",
240 DEBUG(5, ("invalid value '%s' given for "
241 "parameter '%s'\n", valstr, valname));
243 werr = WERR_INVALID_PARAM;
247 if (registry_smbconf_valname_forbidden(canon_valname)) {
248 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
250 werr = WERR_INVALID_PARAM;
254 subkeyname = strrchr_m(key->key->name, '\\');
255 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
256 DEBUG(5, ("Invalid registry key '%s' given as "
257 "smbconf section.\n", key->key->name));
258 werr = WERR_INVALID_PARAM;
262 if (!strequal(subkeyname, GLOBAL_NAME) &&
263 lp_parameter_is_global(valname))
265 DEBUG(5, ("Global paramter '%s' not allowed in "
266 "service definition ('%s').\n", canon_valname,
268 werr = WERR_INVALID_PARAM;
275 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
276 val.v.sz.len = strlen(canon_valstr) + 1;
278 werr = reg_setvalue(key, canon_valname, &val);
279 if (!W_ERROR_IS_OK(werr)) {
280 DEBUG(5, ("Error adding value '%s' to "
282 canon_valname, key->key->name, dos_errstr(werr)));
289 static WERROR smbconf_reg_set_multi_sz_value(struct registry_key *key,
291 const uint32_t num_strings,
292 const char **strings)
295 struct registry_value *value;
297 TALLOC_CTX *tmp_ctx = talloc_stackframe();
299 if (strings == NULL) {
300 werr = WERR_INVALID_PARAM;
304 value = TALLOC_ZERO_P(tmp_ctx, struct registry_value);
306 value->type = REG_MULTI_SZ;
307 value->v.multi_sz.num_strings = num_strings;
308 value->v.multi_sz.strings = TALLOC_ARRAY(tmp_ctx, char *, num_strings);
309 if (value->v.multi_sz.strings == NULL) {
313 for (count = 0; count < num_strings; count++) {
314 value->v.multi_sz.strings[count] =
315 talloc_strdup(value->v.multi_sz.strings,
317 if (value->v.multi_sz.strings[count] == NULL) {
323 werr = reg_setvalue(key, valname, value);
324 if (!W_ERROR_IS_OK(werr)) {
325 DEBUG(5, ("Error adding value '%s' to key '%s': %s\n",
326 valname, key->key->name, dos_errstr(werr)));
330 TALLOC_FREE(tmp_ctx);
335 * format a registry_value into a string.
337 * This is intended to be used for smbconf registry values,
338 * which are ar stored as REG_SZ values, so the incomplete
339 * handling should be ok.
341 static char *smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
342 struct registry_value *value)
346 /* alternatively, create a new talloc context? */
347 if (mem_ctx == NULL) {
351 switch (value->type) {
353 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
357 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
361 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
362 result = talloc_asprintf(mem_ctx, "%s\"%s\" ",
363 result ? result : "" ,
364 value->v.multi_sz.strings[j]);
365 if (result == NULL) {
372 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
373 (int)value->v.binary.length);
376 result = talloc_asprintf(mem_ctx, "<unprintable>");
383 * Get the values of a key as a list of value names
384 * and a list of value strings (ordered)
386 static WERROR smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
387 struct registry_key *key,
388 uint32_t *num_values,
390 char ***value_strings)
392 TALLOC_CTX *tmp_ctx = NULL;
393 WERROR werr = WERR_OK;
395 struct registry_value *valvalue = NULL;
396 char *valname = NULL;
397 char **tmp_valnames = NULL;
398 char **tmp_valstrings = NULL;
400 if ((num_values == NULL) || (value_names == NULL) ||
401 (value_strings == NULL))
403 werr = WERR_INVALID_PARAM;
407 tmp_ctx = talloc_stackframe();
408 if (tmp_ctx == NULL) {
414 werr = reg_enumvalue(tmp_ctx, key, count, &valname, &valvalue),
420 werr = smbconf_add_string_to_array(tmp_ctx,
423 if (!W_ERROR_IS_OK(werr)) {
427 valstring = smbconf_format_registry_value(tmp_ctx, valvalue);
428 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
430 if (!W_ERROR_IS_OK(werr)) {
434 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
442 *value_names = talloc_move(mem_ctx, &tmp_valnames);
443 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
446 *value_strings = NULL;
450 TALLOC_FREE(tmp_ctx);
454 /**********************************************************************
456 * smbconf operations: registry implementations
458 **********************************************************************/
461 * initialize the registry smbconf backend
463 static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
465 WERROR werr = WERR_OK;
470 ctx->path = talloc_strdup(ctx, path);
471 if (ctx->path == NULL) {
476 ctx->data = TALLOC_ZERO_P(ctx, struct reg_private_data);
478 werr = ntstatus_to_werror(registry_create_admin_token(ctx,
479 &(rpd(ctx)->token)));
480 if (!W_ERROR_IS_OK(werr)) {
481 DEBUG(1, ("Error creating admin token\n"));
484 rpd(ctx)->open = false;
486 if (!registry_init_smbconf()) {
487 werr = WERR_REG_IO_FAILURE;
495 static int smbconf_reg_shutdown(struct smbconf_ctx *ctx)
497 return ctx->ops->close_conf(ctx);
500 static WERROR smbconf_reg_open(struct smbconf_ctx *ctx)
504 if (rpd(ctx)->open) {
509 if (W_ERROR_IS_OK(werr)) {
510 rpd(ctx)->open = true;
515 static int smbconf_reg_close(struct smbconf_ctx *ctx)
519 if (!rpd(ctx)->open) {
525 rpd(ctx)->open = false;
531 * Get the change sequence number of the given service/parameter.
532 * service and parameter strings may be NULL.
534 static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
535 struct smbconf_csn *csn,
536 const char *service, const char *param)
542 if (!W_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
546 csn->csn = (uint64_t)regdb_get_seqnum();
550 * Drop the whole configuration (restarting empty) - registry version
552 static WERROR smbconf_reg_drop(struct smbconf_ctx *ctx)
555 WERROR werr = WERR_OK;
556 struct registry_key *parent_key = NULL;
557 struct registry_key *new_key = NULL;
558 TALLOC_CTX* mem_ctx = talloc_stackframe();
559 enum winreg_CreateAction action;
561 path = talloc_strdup(mem_ctx, ctx->path);
566 p = strrchr(path, '\\');
568 werr = smbconf_reg_open_path(mem_ctx, ctx, path, REG_KEY_WRITE,
571 if (!W_ERROR_IS_OK(werr)) {
575 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
577 if (!W_ERROR_IS_OK(werr)) {
581 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
585 TALLOC_FREE(mem_ctx);
590 * get the list of share names defined in the configuration.
593 static WERROR smbconf_reg_get_share_names(struct smbconf_ctx *ctx,
595 uint32_t *num_shares,
599 uint32_t added_count = 0;
600 TALLOC_CTX *tmp_ctx = NULL;
601 WERROR werr = WERR_OK;
602 struct registry_key *key = NULL;
603 char *subkey_name = NULL;
604 char **tmp_share_names = NULL;
606 if ((num_shares == NULL) || (share_names == NULL)) {
607 werr = WERR_INVALID_PARAM;
611 tmp_ctx = talloc_stackframe();
612 if (tmp_ctx == NULL) {
617 /* make sure "global" is always listed first */
618 if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
619 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
621 if (!W_ERROR_IS_OK(werr)) {
627 werr = smbconf_reg_open_base_key(tmp_ctx, ctx,
628 SEC_RIGHTS_ENUM_SUBKEYS, &key);
629 if (!W_ERROR_IS_OK(werr)) {
634 werr = reg_enumkey(tmp_ctx, key, count, &subkey_name, NULL),
638 if (strequal(subkey_name, GLOBAL_NAME)) {
642 werr = smbconf_add_string_to_array(tmp_ctx,
646 if (!W_ERROR_IS_OK(werr)) {
651 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
656 *num_shares = added_count;
657 if (added_count > 0) {
658 *share_names = talloc_move(mem_ctx, &tmp_share_names);
664 TALLOC_FREE(tmp_ctx);
669 * check if a share/service of a given name exists - registry version
671 static bool smbconf_reg_share_exists(struct smbconf_ctx *ctx,
672 const char *servicename)
675 WERROR werr = WERR_OK;
676 TALLOC_CTX *mem_ctx = talloc_stackframe();
677 struct registry_key *key = NULL;
679 werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
681 if (W_ERROR_IS_OK(werr)) {
685 TALLOC_FREE(mem_ctx);
690 * Add a service if it does not already exist - registry version
692 static WERROR smbconf_reg_create_share(struct smbconf_ctx *ctx,
693 const char *servicename)
696 TALLOC_CTX *mem_ctx = talloc_stackframe();
697 struct registry_key *key = NULL;
699 werr = smbconf_reg_create_service_key(mem_ctx, ctx, servicename, &key);
701 TALLOC_FREE(mem_ctx);
706 * get a definition of a share (service) from configuration.
708 static WERROR smbconf_reg_get_share(struct smbconf_ctx *ctx,
710 const char *servicename,
711 uint32_t *num_params,
712 char ***param_names, char ***param_values)
714 WERROR werr = WERR_OK;
715 struct registry_key *key = NULL;
717 werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
719 if (!W_ERROR_IS_OK(werr)) {
723 werr = smbconf_reg_get_values(mem_ctx, key, num_params,
724 param_names, param_values);
732 * delete a service from configuration
734 static WERROR smbconf_reg_delete_share(struct smbconf_ctx *ctx,
735 const char *servicename)
737 WERROR werr = WERR_OK;
738 struct registry_key *key = NULL;
739 TALLOC_CTX *mem_ctx = talloc_stackframe();
741 werr = smbconf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE, &key);
742 if (!W_ERROR_IS_OK(werr)) {
746 werr = reg_deletekey_recursive(key, key, servicename);
749 TALLOC_FREE(mem_ctx);
754 * set a configuration parameter to the value provided.
756 static WERROR smbconf_reg_set_parameter(struct smbconf_ctx *ctx,
762 struct registry_key *key = NULL;
763 TALLOC_CTX *mem_ctx = talloc_stackframe();
765 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
766 REG_KEY_WRITE, &key);
767 if (!W_ERROR_IS_OK(werr)) {
771 werr = smbconf_reg_set_value(key, param, valstr);
774 TALLOC_FREE(mem_ctx);
779 * get the value of a configuration parameter as a string
781 static WERROR smbconf_reg_get_parameter(struct smbconf_ctx *ctx,
787 WERROR werr = WERR_OK;
788 struct registry_key *key = NULL;
789 struct registry_value *value = NULL;
791 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
793 if (!W_ERROR_IS_OK(werr)) {
797 if (!smbconf_value_exists(key, param)) {
798 werr = WERR_INVALID_PARAM;
802 werr = reg_queryvalue(mem_ctx, key, param, &value);
803 if (!W_ERROR_IS_OK(werr)) {
807 *valstr = smbconf_format_registry_value(mem_ctx, value);
809 if (*valstr == NULL) {
820 * delete a parameter from configuration
822 static WERROR smbconf_reg_delete_parameter(struct smbconf_ctx *ctx,
826 struct registry_key *key = NULL;
827 WERROR werr = WERR_OK;
828 TALLOC_CTX *mem_ctx = talloc_stackframe();
830 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
832 if (!W_ERROR_IS_OK(werr)) {
836 if (!smbconf_value_exists(key, param)) {
837 werr = WERR_INVALID_PARAM;
841 werr = reg_deletevalue(key, param);
844 TALLOC_FREE(mem_ctx);
848 static WERROR smbconf_reg_get_includes(struct smbconf_ctx *ctx,
851 uint32_t *num_includes,
856 struct registry_key *key = NULL;
857 struct registry_value *value = NULL;
858 char **tmp_includes = NULL;
859 TALLOC_CTX *tmp_ctx = talloc_stackframe();
861 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
863 if (!W_ERROR_IS_OK(werr)) {
867 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
872 werr = reg_queryvalue(tmp_ctx, key, INCLUDES_VALNAME, &value);
873 if (!W_ERROR_IS_OK(werr)) {
877 if (value->type != REG_MULTI_SZ) {
878 /* wront type -- ignore */
882 for (count = 0; count < value->v.multi_sz.num_strings; count++)
884 werr = smbconf_add_string_to_array(tmp_ctx,
887 value->v.multi_sz.strings[count]);
888 if (!W_ERROR_IS_OK(werr)) {
894 *includes = talloc_move(mem_ctx, &tmp_includes);
895 if (*includes == NULL) {
899 *num_includes = count;
906 TALLOC_FREE(tmp_ctx);
910 static WERROR smbconf_reg_set_includes(struct smbconf_ctx *ctx,
912 uint32_t num_includes,
913 const char **includes)
915 WERROR werr = WERR_OK;
916 struct registry_key *key = NULL;
917 TALLOC_CTX *tmp_ctx = talloc_stackframe();
919 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
921 if (!W_ERROR_IS_OK(werr)) {
925 werr = smbconf_reg_set_multi_sz_value(key, INCLUDES_VALNAME,
926 num_includes, includes);
929 TALLOC_FREE(tmp_ctx);
934 struct smbconf_ops smbconf_ops_reg = {
935 .init = smbconf_reg_init,
936 .shutdown = smbconf_reg_shutdown,
937 .open_conf = smbconf_reg_open,
938 .close_conf = smbconf_reg_close,
939 .get_csn = smbconf_reg_get_csn,
940 .drop = smbconf_reg_drop,
941 .get_share_names = smbconf_reg_get_share_names,
942 .share_exists = smbconf_reg_share_exists,
943 .create_share = smbconf_reg_create_share,
944 .get_share = smbconf_reg_get_share,
945 .delete_share = smbconf_reg_delete_share,
946 .set_parameter = smbconf_reg_set_parameter,
947 .get_parameter = smbconf_reg_get_parameter,
948 .delete_parameter = smbconf_reg_delete_parameter,
949 .get_includes = smbconf_reg_get_includes,
950 .set_includes = smbconf_reg_set_includes,
955 * initialize the smbconf registry backend
956 * the only function that is exported from this module
958 WERROR smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
961 return smbconf_init(mem_ctx, conf_ctx, path, &smbconf_ops_reg);