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) {
66 static bool smbconf_reg_valname_valid(const char *valname)
68 return (lp_parameter_is_valid(valname) &&
69 !smbconf_reg_valname_forbidden(valname));
73 * Open a registry key specified by "path"
75 static WERROR smbconf_reg_open_path(TALLOC_CTX *mem_ctx,
76 struct smbconf_ctx *ctx,
78 uint32 desired_access,
79 struct registry_key **key)
81 WERROR werr = WERR_OK;
84 DEBUG(1, ("Error: configuration is not open!\n"));
85 werr = WERR_INVALID_PARAM;
89 if (rpd(ctx)->token == NULL) {
90 DEBUG(1, ("Error: token missing from smbconf_ctx. "
91 "was smbconf_init() called?\n"));
92 werr = WERR_INVALID_PARAM;
96 werr = ctx->ops->open_conf(ctx);
97 if (!W_ERROR_IS_OK(werr)) {
98 DEBUG(1, ("Error opening the registry.\n"));
103 DEBUG(1, ("Error: NULL path string given\n"));
104 werr = WERR_INVALID_PARAM;
108 werr = reg_open_path(mem_ctx, path, desired_access, rpd(ctx)->token,
111 if (!W_ERROR_IS_OK(werr)) {
112 DEBUG(1, ("Error opening registry path '%s': %s\n",
113 path, dos_errstr(werr)));
121 * Open a subkey of the base key (i.e a service)
123 static WERROR smbconf_reg_open_service_key(TALLOC_CTX *mem_ctx,
124 struct smbconf_ctx *ctx,
125 const char *servicename,
126 uint32 desired_access,
127 struct registry_key **key)
129 WERROR werr = WERR_OK;
132 if (servicename == NULL) {
133 DEBUG(3, ("Error: NULL servicename given.\n"));
134 werr = WERR_INVALID_PARAM;
138 path = talloc_asprintf(mem_ctx, "%s\\%s", ctx->path, servicename);
144 werr = smbconf_reg_open_path(mem_ctx, ctx, path, desired_access, key);
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)
159 return smbconf_reg_open_path(mem_ctx, ctx, ctx->path, desired_access,
164 * check if a value exists in a given registry key
166 static bool smbconf_value_exists(struct registry_key *key, const char *param)
169 WERROR werr = WERR_OK;
170 TALLOC_CTX *ctx = talloc_stackframe();
171 struct registry_value *value = NULL;
173 werr = reg_queryvalue(ctx, key, param, &value);
174 if (W_ERROR_IS_OK(werr)) {
183 * create a subkey of the base key (i.e. a service...)
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)
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;
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 if (!(create_ctx = talloc_stackframe())) {
203 werr = smbconf_reg_open_base_key(create_ctx, ctx, REG_KEY_WRITE,
205 if (!W_ERROR_IS_OK(werr)) {
209 werr = reg_createkey(mem_ctx, create_parent, subkeyname,
210 REG_KEY_WRITE, newkey, &action);
211 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
212 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
213 werr = WERR_ALREADY_EXISTS;
215 if (!W_ERROR_IS_OK(werr)) {
216 DEBUG(5, ("Error creating key %s: %s\n",
217 subkeyname, dos_errstr(werr)));
221 TALLOC_FREE(create_ctx);
226 * add a value to a key.
228 static WERROR smbconf_reg_set_value(struct registry_key *key,
232 struct registry_value val;
233 WERROR werr = WERR_OK;
235 const char *canon_valname;
236 const char *canon_valstr;
238 if (!lp_canonicalize_parameter_with_value(valname, valstr,
242 if (canon_valname == NULL) {
243 DEBUG(5, ("invalid parameter '%s' given\n",
246 DEBUG(5, ("invalid value '%s' given for "
247 "parameter '%s'\n", valstr, valname));
249 werr = WERR_INVALID_PARAM;
253 if (smbconf_reg_valname_forbidden(canon_valname)) {
254 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
256 werr = WERR_INVALID_PARAM;
260 subkeyname = strrchr_m(key->key->name, '\\');
261 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
262 DEBUG(5, ("Invalid registry key '%s' given as "
263 "smbconf section.\n", key->key->name));
264 werr = WERR_INVALID_PARAM;
268 if (!strequal(subkeyname, GLOBAL_NAME) &&
269 lp_parameter_is_global(valname))
271 DEBUG(5, ("Global paramter '%s' not allowed in "
272 "service definition ('%s').\n", canon_valname,
274 werr = WERR_INVALID_PARAM;
281 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
282 val.v.sz.len = strlen(canon_valstr) + 1;
284 werr = reg_setvalue(key, canon_valname, &val);
285 if (!W_ERROR_IS_OK(werr)) {
286 DEBUG(5, ("Error adding value '%s' to "
288 canon_valname, key->key->name, dos_errstr(werr)));
295 static WERROR smbconf_reg_set_multi_sz_value(struct registry_key *key,
297 const uint32_t num_strings,
298 const char **strings)
301 struct registry_value *value;
303 TALLOC_CTX *tmp_ctx = talloc_stackframe();
305 if (strings == NULL) {
306 werr = WERR_INVALID_PARAM;
310 value = TALLOC_ZERO_P(tmp_ctx, struct registry_value);
312 value->type = REG_MULTI_SZ;
313 value->v.multi_sz.num_strings = num_strings;
314 value->v.multi_sz.strings = TALLOC_ARRAY(tmp_ctx, char *, num_strings);
315 if (value->v.multi_sz.strings == NULL) {
319 for (count = 0; count < num_strings; count++) {
320 value->v.multi_sz.strings[count] =
321 talloc_strdup(value->v.multi_sz.strings,
323 if (value->v.multi_sz.strings[count] == NULL) {
329 werr = reg_setvalue(key, valname, value);
330 if (!W_ERROR_IS_OK(werr)) {
331 DEBUG(5, ("Error adding value '%s' to key '%s': %s\n",
332 valname, key->key->name, dos_errstr(werr)));
336 TALLOC_FREE(tmp_ctx);
341 * format a registry_value into a string.
343 * This is intended to be used for smbconf registry values,
344 * which are ar stored as REG_SZ values, so the incomplete
345 * handling should be ok.
347 static char *smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
348 struct registry_value *value)
352 /* alternatively, create a new talloc context? */
353 if (mem_ctx == NULL) {
357 switch (value->type) {
359 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
363 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
367 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
368 result = talloc_asprintf(mem_ctx, "%s\"%s\" ",
369 result ? result : "" ,
370 value->v.multi_sz.strings[j]);
371 if (result == NULL) {
378 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
379 (int)value->v.binary.length);
382 result = talloc_asprintf(mem_ctx, "<unprintable>");
388 static WERROR smbconf_reg_get_includes_internal(TALLOC_CTX *mem_ctx,
389 struct registry_key *key,
390 uint32_t *num_includes,
395 struct registry_value *value = NULL;
396 char **tmp_includes = NULL;
397 TALLOC_CTX *tmp_ctx = talloc_stackframe();
399 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
404 werr = reg_queryvalue(tmp_ctx, key, INCLUDES_VALNAME, &value);
405 if (!W_ERROR_IS_OK(werr)) {
409 if (value->type != REG_MULTI_SZ) {
410 /* wront type -- ignore */
414 for (count = 0; count < value->v.multi_sz.num_strings; count++)
416 werr = smbconf_add_string_to_array(tmp_ctx,
419 value->v.multi_sz.strings[count]);
420 if (!W_ERROR_IS_OK(werr)) {
426 *includes = talloc_move(mem_ctx, &tmp_includes);
427 if (*includes == NULL) {
431 *num_includes = count;
438 TALLOC_FREE(tmp_ctx);
443 * Get the values of a key as a list of value names
444 * and a list of value strings (ordered)
446 static WERROR smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
447 struct registry_key *key,
448 uint32_t *num_values,
450 char ***value_strings)
452 TALLOC_CTX *tmp_ctx = NULL;
453 WERROR werr = WERR_OK;
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;
461 if ((num_values == NULL) || (value_names == NULL) ||
462 (value_strings == NULL))
464 werr = WERR_INVALID_PARAM;
468 tmp_ctx = talloc_stackframe();
469 if (tmp_ctx == NULL) {
475 werr = reg_enumvalue(tmp_ctx, key, count, &valname, &valvalue),
481 if (!smbconf_reg_valname_valid(valname)) {
485 werr = smbconf_add_string_to_array(tmp_ctx,
487 tmp_num_values, valname);
488 if (!W_ERROR_IS_OK(werr)) {
492 valstring = smbconf_format_registry_value(tmp_ctx, valvalue);
493 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
494 tmp_num_values, valstring);
495 if (!W_ERROR_IS_OK(werr)) {
500 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
506 *num_values = tmp_num_values;
507 if (tmp_num_values > 0) {
508 *value_names = talloc_move(mem_ctx, &tmp_valnames);
509 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
512 *value_strings = NULL;
516 TALLOC_FREE(tmp_ctx);
520 /**********************************************************************
522 * smbconf operations: registry implementations
524 **********************************************************************/
527 * initialize the registry smbconf backend
529 static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
531 WERROR werr = WERR_OK;
536 ctx->path = talloc_strdup(ctx, path);
537 if (ctx->path == NULL) {
542 ctx->data = TALLOC_ZERO_P(ctx, struct reg_private_data);
544 werr = ntstatus_to_werror(registry_create_admin_token(ctx,
545 &(rpd(ctx)->token)));
546 if (!W_ERROR_IS_OK(werr)) {
547 DEBUG(1, ("Error creating admin token\n"));
550 rpd(ctx)->open = false;
552 if (!registry_init_smbconf()) {
553 werr = WERR_REG_IO_FAILURE;
561 static int smbconf_reg_shutdown(struct smbconf_ctx *ctx)
563 return ctx->ops->close_conf(ctx);
566 static WERROR smbconf_reg_open(struct smbconf_ctx *ctx)
570 if (rpd(ctx)->open) {
575 if (W_ERROR_IS_OK(werr)) {
576 rpd(ctx)->open = true;
581 static int smbconf_reg_close(struct smbconf_ctx *ctx)
585 if (!rpd(ctx)->open) {
591 rpd(ctx)->open = false;
597 * Get the change sequence number of the given service/parameter.
598 * service and parameter strings may be NULL.
600 static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
601 struct smbconf_csn *csn,
602 const char *service, const char *param)
608 if (!W_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
612 csn->csn = (uint64_t)regdb_get_seqnum();
616 * Drop the whole configuration (restarting empty) - registry version
618 static WERROR smbconf_reg_drop(struct smbconf_ctx *ctx)
621 WERROR werr = WERR_OK;
622 struct registry_key *parent_key = NULL;
623 struct registry_key *new_key = NULL;
624 TALLOC_CTX* mem_ctx = talloc_stackframe();
625 enum winreg_CreateAction action;
627 path = talloc_strdup(mem_ctx, ctx->path);
632 p = strrchr(path, '\\');
634 werr = smbconf_reg_open_path(mem_ctx, ctx, path, REG_KEY_WRITE,
637 if (!W_ERROR_IS_OK(werr)) {
641 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
643 if (!W_ERROR_IS_OK(werr)) {
647 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
651 TALLOC_FREE(mem_ctx);
656 * get the list of share names defined in the configuration.
659 static WERROR smbconf_reg_get_share_names(struct smbconf_ctx *ctx,
661 uint32_t *num_shares,
665 uint32_t added_count = 0;
666 TALLOC_CTX *tmp_ctx = NULL;
667 WERROR werr = WERR_OK;
668 struct registry_key *key = NULL;
669 char *subkey_name = NULL;
670 char **tmp_share_names = NULL;
672 if ((num_shares == NULL) || (share_names == NULL)) {
673 werr = WERR_INVALID_PARAM;
677 tmp_ctx = talloc_stackframe();
678 if (tmp_ctx == NULL) {
683 /* make sure "global" is always listed first */
684 if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
685 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
687 if (!W_ERROR_IS_OK(werr)) {
693 werr = smbconf_reg_open_base_key(tmp_ctx, ctx,
694 SEC_RIGHTS_ENUM_SUBKEYS, &key);
695 if (!W_ERROR_IS_OK(werr)) {
700 werr = reg_enumkey(tmp_ctx, key, count, &subkey_name, NULL),
704 if (strequal(subkey_name, GLOBAL_NAME)) {
708 werr = smbconf_add_string_to_array(tmp_ctx,
712 if (!W_ERROR_IS_OK(werr)) {
717 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
722 *num_shares = added_count;
723 if (added_count > 0) {
724 *share_names = talloc_move(mem_ctx, &tmp_share_names);
730 TALLOC_FREE(tmp_ctx);
735 * check if a share/service of a given name exists - registry version
737 static bool smbconf_reg_share_exists(struct smbconf_ctx *ctx,
738 const char *servicename)
741 WERROR werr = WERR_OK;
742 TALLOC_CTX *mem_ctx = talloc_stackframe();
743 struct registry_key *key = NULL;
745 werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
747 if (W_ERROR_IS_OK(werr)) {
751 TALLOC_FREE(mem_ctx);
756 * Add a service if it does not already exist - registry version
758 static WERROR smbconf_reg_create_share(struct smbconf_ctx *ctx,
759 const char *servicename)
762 TALLOC_CTX *mem_ctx = talloc_stackframe();
763 struct registry_key *key = NULL;
765 werr = smbconf_reg_create_service_key(mem_ctx, ctx, servicename, &key);
767 TALLOC_FREE(mem_ctx);
772 * get a definition of a share (service) from configuration.
774 static WERROR smbconf_reg_get_share(struct smbconf_ctx *ctx,
776 const char *servicename,
777 uint32_t *num_params,
778 char ***param_names, char ***param_values)
780 WERROR werr = WERR_OK;
781 struct registry_key *key = NULL;
783 werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
785 if (!W_ERROR_IS_OK(werr)) {
789 werr = smbconf_reg_get_values(mem_ctx, key, num_params,
790 param_names, param_values);
798 * delete a service from configuration
800 static WERROR smbconf_reg_delete_share(struct smbconf_ctx *ctx,
801 const char *servicename)
803 WERROR werr = WERR_OK;
804 struct registry_key *key = NULL;
805 TALLOC_CTX *mem_ctx = talloc_stackframe();
807 werr = smbconf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE, &key);
808 if (!W_ERROR_IS_OK(werr)) {
812 werr = reg_deletekey_recursive(key, key, servicename);
815 TALLOC_FREE(mem_ctx);
820 * set a configuration parameter to the value provided.
822 static WERROR smbconf_reg_set_parameter(struct smbconf_ctx *ctx,
828 struct registry_key *key = NULL;
829 TALLOC_CTX *mem_ctx = talloc_stackframe();
831 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
832 REG_KEY_WRITE, &key);
833 if (!W_ERROR_IS_OK(werr)) {
837 werr = smbconf_reg_set_value(key, param, valstr);
840 TALLOC_FREE(mem_ctx);
845 * get the value of a configuration parameter as a string
847 static WERROR smbconf_reg_get_parameter(struct smbconf_ctx *ctx,
853 WERROR werr = WERR_OK;
854 struct registry_key *key = NULL;
855 struct registry_value *value = NULL;
857 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
859 if (!W_ERROR_IS_OK(werr)) {
863 if (!smbconf_value_exists(key, param)) {
864 werr = WERR_INVALID_PARAM;
868 werr = reg_queryvalue(mem_ctx, key, param, &value);
869 if (!W_ERROR_IS_OK(werr)) {
873 *valstr = smbconf_format_registry_value(mem_ctx, value);
875 if (*valstr == NULL) {
886 * delete a parameter from configuration
888 static WERROR smbconf_reg_delete_parameter(struct smbconf_ctx *ctx,
892 struct registry_key *key = NULL;
893 WERROR werr = WERR_OK;
894 TALLOC_CTX *mem_ctx = talloc_stackframe();
896 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
898 if (!W_ERROR_IS_OK(werr)) {
902 if (!smbconf_value_exists(key, param)) {
903 werr = WERR_INVALID_PARAM;
907 werr = reg_deletevalue(key, param);
910 TALLOC_FREE(mem_ctx);
914 static WERROR smbconf_reg_get_includes(struct smbconf_ctx *ctx,
917 uint32_t *num_includes,
921 struct registry_key *key = NULL;
922 TALLOC_CTX *tmp_ctx = talloc_stackframe();
924 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
926 if (!W_ERROR_IS_OK(werr)) {
930 werr = smbconf_reg_get_includes_internal(mem_ctx, key, num_includes,
934 TALLOC_FREE(tmp_ctx);
938 static WERROR smbconf_reg_set_includes(struct smbconf_ctx *ctx,
940 uint32_t num_includes,
941 const char **includes)
943 WERROR werr = WERR_OK;
944 struct registry_key *key = NULL;
945 TALLOC_CTX *tmp_ctx = talloc_stackframe();
947 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
949 if (!W_ERROR_IS_OK(werr)) {
953 werr = smbconf_reg_set_multi_sz_value(key, INCLUDES_VALNAME,
954 num_includes, includes);
957 TALLOC_FREE(tmp_ctx);
962 struct smbconf_ops smbconf_ops_reg = {
963 .init = smbconf_reg_init,
964 .shutdown = smbconf_reg_shutdown,
965 .open_conf = smbconf_reg_open,
966 .close_conf = smbconf_reg_close,
967 .get_csn = smbconf_reg_get_csn,
968 .drop = smbconf_reg_drop,
969 .get_share_names = smbconf_reg_get_share_names,
970 .share_exists = smbconf_reg_share_exists,
971 .create_share = smbconf_reg_create_share,
972 .get_share = smbconf_reg_get_share,
973 .delete_share = smbconf_reg_delete_share,
974 .set_parameter = smbconf_reg_set_parameter,
975 .get_parameter = smbconf_reg_get_parameter,
976 .delete_parameter = smbconf_reg_delete_parameter,
977 .get_includes = smbconf_reg_get_includes,
978 .set_includes = smbconf_reg_set_includes,
983 * initialize the smbconf registry backend
984 * the only function that is exported from this module
986 WERROR smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
989 return smbconf_init(mem_ctx, conf_ctx, path, &smbconf_ops_reg);