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 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[] = {
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 (smbconf_reg_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>");
382 static WERROR smbconf_reg_get_includes_internal(TALLOC_CTX *mem_ctx,
383 struct registry_key *key,
384 uint32_t *num_includes,
389 struct registry_value *value = NULL;
390 char **tmp_includes = NULL;
391 TALLOC_CTX *tmp_ctx = talloc_stackframe();
393 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
398 werr = reg_queryvalue(tmp_ctx, key, INCLUDES_VALNAME, &value);
399 if (!W_ERROR_IS_OK(werr)) {
403 if (value->type != REG_MULTI_SZ) {
404 /* wront type -- ignore */
408 for (count = 0; count < value->v.multi_sz.num_strings; count++)
410 werr = smbconf_add_string_to_array(tmp_ctx,
413 value->v.multi_sz.strings[count]);
414 if (!W_ERROR_IS_OK(werr)) {
420 *includes = talloc_move(mem_ctx, &tmp_includes);
421 if (*includes == NULL) {
425 *num_includes = count;
432 TALLOC_FREE(tmp_ctx);
437 * Get the values of a key as a list of value names
438 * and a list of value strings (ordered)
440 static WERROR smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
441 struct registry_key *key,
442 uint32_t *num_values,
444 char ***value_strings)
446 TALLOC_CTX *tmp_ctx = NULL;
447 WERROR werr = WERR_OK;
449 struct registry_value *valvalue = NULL;
450 char *valname = NULL;
451 char **tmp_valnames = NULL;
452 char **tmp_valstrings = NULL;
454 if ((num_values == NULL) || (value_names == NULL) ||
455 (value_strings == NULL))
457 werr = WERR_INVALID_PARAM;
461 tmp_ctx = talloc_stackframe();
462 if (tmp_ctx == NULL) {
468 werr = reg_enumvalue(tmp_ctx, key, count, &valname, &valvalue),
474 werr = smbconf_add_string_to_array(tmp_ctx,
477 if (!W_ERROR_IS_OK(werr)) {
481 valstring = smbconf_format_registry_value(tmp_ctx, valvalue);
482 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
484 if (!W_ERROR_IS_OK(werr)) {
488 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
496 *value_names = talloc_move(mem_ctx, &tmp_valnames);
497 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
500 *value_strings = NULL;
504 TALLOC_FREE(tmp_ctx);
508 /**********************************************************************
510 * smbconf operations: registry implementations
512 **********************************************************************/
515 * initialize the registry smbconf backend
517 static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
519 WERROR werr = WERR_OK;
524 ctx->path = talloc_strdup(ctx, path);
525 if (ctx->path == NULL) {
530 ctx->data = TALLOC_ZERO_P(ctx, struct reg_private_data);
532 werr = ntstatus_to_werror(registry_create_admin_token(ctx,
533 &(rpd(ctx)->token)));
534 if (!W_ERROR_IS_OK(werr)) {
535 DEBUG(1, ("Error creating admin token\n"));
538 rpd(ctx)->open = false;
540 if (!registry_init_smbconf()) {
541 werr = WERR_REG_IO_FAILURE;
549 static int smbconf_reg_shutdown(struct smbconf_ctx *ctx)
551 return ctx->ops->close_conf(ctx);
554 static WERROR smbconf_reg_open(struct smbconf_ctx *ctx)
558 if (rpd(ctx)->open) {
563 if (W_ERROR_IS_OK(werr)) {
564 rpd(ctx)->open = true;
569 static int smbconf_reg_close(struct smbconf_ctx *ctx)
573 if (!rpd(ctx)->open) {
579 rpd(ctx)->open = false;
585 * Get the change sequence number of the given service/parameter.
586 * service and parameter strings may be NULL.
588 static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
589 struct smbconf_csn *csn,
590 const char *service, const char *param)
596 if (!W_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
600 csn->csn = (uint64_t)regdb_get_seqnum();
604 * Drop the whole configuration (restarting empty) - registry version
606 static WERROR smbconf_reg_drop(struct smbconf_ctx *ctx)
609 WERROR werr = WERR_OK;
610 struct registry_key *parent_key = NULL;
611 struct registry_key *new_key = NULL;
612 TALLOC_CTX* mem_ctx = talloc_stackframe();
613 enum winreg_CreateAction action;
615 path = talloc_strdup(mem_ctx, ctx->path);
620 p = strrchr(path, '\\');
622 werr = smbconf_reg_open_path(mem_ctx, ctx, path, REG_KEY_WRITE,
625 if (!W_ERROR_IS_OK(werr)) {
629 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
631 if (!W_ERROR_IS_OK(werr)) {
635 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
639 TALLOC_FREE(mem_ctx);
644 * get the list of share names defined in the configuration.
647 static WERROR smbconf_reg_get_share_names(struct smbconf_ctx *ctx,
649 uint32_t *num_shares,
653 uint32_t added_count = 0;
654 TALLOC_CTX *tmp_ctx = NULL;
655 WERROR werr = WERR_OK;
656 struct registry_key *key = NULL;
657 char *subkey_name = NULL;
658 char **tmp_share_names = NULL;
660 if ((num_shares == NULL) || (share_names == NULL)) {
661 werr = WERR_INVALID_PARAM;
665 tmp_ctx = talloc_stackframe();
666 if (tmp_ctx == NULL) {
671 /* make sure "global" is always listed first */
672 if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
673 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
675 if (!W_ERROR_IS_OK(werr)) {
681 werr = smbconf_reg_open_base_key(tmp_ctx, ctx,
682 SEC_RIGHTS_ENUM_SUBKEYS, &key);
683 if (!W_ERROR_IS_OK(werr)) {
688 werr = reg_enumkey(tmp_ctx, key, count, &subkey_name, NULL),
692 if (strequal(subkey_name, GLOBAL_NAME)) {
696 werr = smbconf_add_string_to_array(tmp_ctx,
700 if (!W_ERROR_IS_OK(werr)) {
705 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
710 *num_shares = added_count;
711 if (added_count > 0) {
712 *share_names = talloc_move(mem_ctx, &tmp_share_names);
718 TALLOC_FREE(tmp_ctx);
723 * check if a share/service of a given name exists - registry version
725 static bool smbconf_reg_share_exists(struct smbconf_ctx *ctx,
726 const char *servicename)
729 WERROR werr = WERR_OK;
730 TALLOC_CTX *mem_ctx = talloc_stackframe();
731 struct registry_key *key = NULL;
733 werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
735 if (W_ERROR_IS_OK(werr)) {
739 TALLOC_FREE(mem_ctx);
744 * Add a service if it does not already exist - registry version
746 static WERROR smbconf_reg_create_share(struct smbconf_ctx *ctx,
747 const char *servicename)
750 TALLOC_CTX *mem_ctx = talloc_stackframe();
751 struct registry_key *key = NULL;
753 werr = smbconf_reg_create_service_key(mem_ctx, ctx, servicename, &key);
755 TALLOC_FREE(mem_ctx);
760 * get a definition of a share (service) from configuration.
762 static WERROR smbconf_reg_get_share(struct smbconf_ctx *ctx,
764 const char *servicename,
765 uint32_t *num_params,
766 char ***param_names, char ***param_values)
768 WERROR werr = WERR_OK;
769 struct registry_key *key = NULL;
771 werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
773 if (!W_ERROR_IS_OK(werr)) {
777 werr = smbconf_reg_get_values(mem_ctx, key, num_params,
778 param_names, param_values);
786 * delete a service from configuration
788 static WERROR smbconf_reg_delete_share(struct smbconf_ctx *ctx,
789 const char *servicename)
791 WERROR werr = WERR_OK;
792 struct registry_key *key = NULL;
793 TALLOC_CTX *mem_ctx = talloc_stackframe();
795 werr = smbconf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE, &key);
796 if (!W_ERROR_IS_OK(werr)) {
800 werr = reg_deletekey_recursive(key, key, servicename);
803 TALLOC_FREE(mem_ctx);
808 * set a configuration parameter to the value provided.
810 static WERROR smbconf_reg_set_parameter(struct smbconf_ctx *ctx,
816 struct registry_key *key = NULL;
817 TALLOC_CTX *mem_ctx = talloc_stackframe();
819 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
820 REG_KEY_WRITE, &key);
821 if (!W_ERROR_IS_OK(werr)) {
825 werr = smbconf_reg_set_value(key, param, valstr);
828 TALLOC_FREE(mem_ctx);
833 * get the value of a configuration parameter as a string
835 static WERROR smbconf_reg_get_parameter(struct smbconf_ctx *ctx,
841 WERROR werr = WERR_OK;
842 struct registry_key *key = NULL;
843 struct registry_value *value = NULL;
845 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
847 if (!W_ERROR_IS_OK(werr)) {
851 if (!smbconf_value_exists(key, param)) {
852 werr = WERR_INVALID_PARAM;
856 werr = reg_queryvalue(mem_ctx, key, param, &value);
857 if (!W_ERROR_IS_OK(werr)) {
861 *valstr = smbconf_format_registry_value(mem_ctx, value);
863 if (*valstr == NULL) {
874 * delete a parameter from configuration
876 static WERROR smbconf_reg_delete_parameter(struct smbconf_ctx *ctx,
880 struct registry_key *key = NULL;
881 WERROR werr = WERR_OK;
882 TALLOC_CTX *mem_ctx = talloc_stackframe();
884 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
886 if (!W_ERROR_IS_OK(werr)) {
890 if (!smbconf_value_exists(key, param)) {
891 werr = WERR_INVALID_PARAM;
895 werr = reg_deletevalue(key, param);
898 TALLOC_FREE(mem_ctx);
902 static WERROR smbconf_reg_get_includes(struct smbconf_ctx *ctx,
905 uint32_t *num_includes,
909 struct registry_key *key = NULL;
910 TALLOC_CTX *tmp_ctx = talloc_stackframe();
912 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
914 if (!W_ERROR_IS_OK(werr)) {
918 werr = smbconf_reg_get_includes_internal(mem_ctx, key, num_includes,
922 TALLOC_FREE(tmp_ctx);
926 static WERROR smbconf_reg_set_includes(struct smbconf_ctx *ctx,
928 uint32_t num_includes,
929 const char **includes)
931 WERROR werr = WERR_OK;
932 struct registry_key *key = NULL;
933 TALLOC_CTX *tmp_ctx = talloc_stackframe();
935 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
937 if (!W_ERROR_IS_OK(werr)) {
941 werr = smbconf_reg_set_multi_sz_value(key, INCLUDES_VALNAME,
942 num_includes, includes);
945 TALLOC_FREE(tmp_ctx);
950 struct smbconf_ops smbconf_ops_reg = {
951 .init = smbconf_reg_init,
952 .shutdown = smbconf_reg_shutdown,
953 .open_conf = smbconf_reg_open,
954 .close_conf = smbconf_reg_close,
955 .get_csn = smbconf_reg_get_csn,
956 .drop = smbconf_reg_drop,
957 .get_share_names = smbconf_reg_get_share_names,
958 .share_exists = smbconf_reg_share_exists,
959 .create_share = smbconf_reg_create_share,
960 .get_share = smbconf_reg_get_share,
961 .delete_share = smbconf_reg_delete_share,
962 .set_parameter = smbconf_reg_set_parameter,
963 .get_parameter = smbconf_reg_get_parameter,
964 .delete_parameter = smbconf_reg_delete_parameter,
965 .get_includes = smbconf_reg_get_includes,
966 .set_includes = smbconf_reg_set_includes,
971 * initialize the smbconf registry backend
972 * the only function that is exported from this module
974 WERROR smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
977 return smbconf_init(mem_ctx, conf_ctx, path, &smbconf_ops_reg);