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[] = {
57 const char **forbidden = NULL;
59 for (forbidden = forbidden_valnames; *forbidden != NULL; forbidden++) {
60 if (strwicmp(valname, *forbidden) == 0) {
67 static bool smbconf_reg_valname_valid(const char *valname)
69 return (lp_parameter_is_valid(valname) &&
70 !smbconf_reg_valname_forbidden(valname));
74 * Open a registry key specified by "path"
76 static WERROR smbconf_reg_open_path(TALLOC_CTX *mem_ctx,
77 struct smbconf_ctx *ctx,
79 uint32 desired_access,
80 struct registry_key **key)
82 WERROR werr = WERR_OK;
85 DEBUG(1, ("Error: configuration is not open!\n"));
86 werr = WERR_INVALID_PARAM;
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;
97 werr = ctx->ops->open_conf(ctx);
98 if (!W_ERROR_IS_OK(werr)) {
99 DEBUG(1, ("Error opening the registry.\n"));
104 DEBUG(1, ("Error: NULL path string given\n"));
105 werr = WERR_INVALID_PARAM;
109 werr = reg_open_path(mem_ctx, path, desired_access, rpd(ctx)->token,
112 if (!W_ERROR_IS_OK(werr)) {
113 DEBUG(1, ("Error opening registry path '%s': %s\n",
114 path, dos_errstr(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;
133 if (servicename == NULL) {
134 DEBUG(3, ("Error: NULL servicename given.\n"));
135 werr = WERR_INVALID_PARAM;
139 path = talloc_asprintf(mem_ctx, "%s\\%s", ctx->path, servicename);
145 werr = smbconf_reg_open_path(mem_ctx, ctx, path, desired_access, 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,
165 * check if a value exists in a given registry key
167 static bool smbconf_value_exists(struct registry_key *key, const char *param)
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)) {
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())) {
204 werr = smbconf_reg_open_base_key(create_ctx, ctx, REG_KEY_WRITE,
206 if (!W_ERROR_IS_OK(werr)) {
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)));
222 TALLOC_FREE(create_ctx);
227 * add a value to a key.
229 static WERROR smbconf_reg_set_value(struct registry_key *key,
233 struct registry_value val;
234 WERROR werr = WERR_OK;
236 const char *canon_valname;
237 const char *canon_valstr;
239 if (!lp_canonicalize_parameter_with_value(valname, valstr,
243 if (canon_valname == NULL) {
244 DEBUG(5, ("invalid parameter '%s' given\n",
247 DEBUG(5, ("invalid value '%s' given for "
248 "parameter '%s'\n", valstr, valname));
250 werr = WERR_INVALID_PARAM;
254 if (smbconf_reg_valname_forbidden(canon_valname)) {
255 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
257 werr = WERR_INVALID_PARAM;
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;
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,
275 werr = WERR_INVALID_PARAM;
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 "
289 canon_valname, key->key->name, dos_errstr(werr)));
296 static WERROR smbconf_reg_set_multi_sz_value(struct registry_key *key,
298 const uint32_t num_strings,
299 const char **strings)
302 struct registry_value *value;
304 TALLOC_CTX *tmp_ctx = talloc_stackframe();
306 if (strings == NULL) {
307 werr = WERR_INVALID_PARAM;
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) {
320 for (count = 0; count < num_strings; count++) {
321 value->v.multi_sz.strings[count] =
322 talloc_strdup(value->v.multi_sz.strings,
324 if (value->v.multi_sz.strings[count] == NULL) {
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)));
337 TALLOC_FREE(tmp_ctx);
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)
353 /* alternatively, create a new talloc context? */
354 if (mem_ctx == NULL) {
358 switch (value->type) {
360 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
364 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
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) {
379 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
380 (int)value->v.binary.length);
383 result = talloc_asprintf(mem_ctx, "<unprintable>");
389 static WERROR smbconf_reg_get_includes_internal(TALLOC_CTX *mem_ctx,
390 struct registry_key *key,
391 uint32_t *num_includes,
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)) {
405 werr = reg_queryvalue(tmp_ctx, key, INCLUDES_VALNAME, &value);
406 if (!W_ERROR_IS_OK(werr)) {
410 if (value->type != REG_MULTI_SZ) {
411 /* wront type -- ignore */
415 for (count = 0; count < value->v.multi_sz.num_strings; count++)
417 werr = smbconf_add_string_to_array(tmp_ctx,
420 value->v.multi_sz.strings[count]);
421 if (!W_ERROR_IS_OK(werr)) {
427 *includes = talloc_move(mem_ctx, &tmp_includes);
428 if (*includes == NULL) {
432 *num_includes = count;
439 TALLOC_FREE(tmp_ctx);
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,
451 char ***value_strings)
453 TALLOC_CTX *tmp_ctx = NULL;
454 WERROR werr = WERR_OK;
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;
471 tmp_ctx = talloc_stackframe();
472 if (tmp_ctx == NULL) {
478 werr = reg_enumvalue(tmp_ctx, key, count, &valname, &valvalue),
484 if (!smbconf_reg_valname_valid(valname)) {
488 werr = smbconf_add_string_to_array(tmp_ctx,
490 tmp_num_values, valname);
491 if (!W_ERROR_IS_OK(werr)) {
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)) {
503 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
507 /* now add the includes at the end */
508 werr = smbconf_reg_get_includes_internal(tmp_ctx, key, &num_includes,
510 if (!W_ERROR_IS_OK(werr)) {
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)) {
520 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
523 if (!W_ERROR_IS_OK(werr)) {
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);
536 *value_strings = NULL;
540 TALLOC_FREE(tmp_ctx);
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;
560 ctx->path = talloc_strdup(ctx, path);
561 if (ctx->path == NULL) {
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"));
574 rpd(ctx)->open = false;
576 if (!registry_init_smbconf()) {
577 werr = WERR_REG_IO_FAILURE;
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)
594 if (rpd(ctx)->open) {
599 if (W_ERROR_IS_OK(werr)) {
600 rpd(ctx)->open = true;
605 static int smbconf_reg_close(struct smbconf_ctx *ctx)
609 if (!rpd(ctx)->open) {
615 rpd(ctx)->open = false;
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)
632 if (!W_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
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)
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);
656 p = strrchr(path, '\\');
658 werr = smbconf_reg_open_path(mem_ctx, ctx, path, REG_KEY_WRITE,
661 if (!W_ERROR_IS_OK(werr)) {
665 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
667 if (!W_ERROR_IS_OK(werr)) {
671 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
675 TALLOC_FREE(mem_ctx);
680 * get the list of share names defined in the configuration.
683 static WERROR smbconf_reg_get_share_names(struct smbconf_ctx *ctx,
685 uint32_t *num_shares,
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;
701 tmp_ctx = talloc_stackframe();
702 if (tmp_ctx == NULL) {
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,
711 if (!W_ERROR_IS_OK(werr)) {
717 werr = smbconf_reg_open_base_key(tmp_ctx, ctx,
718 SEC_RIGHTS_ENUM_SUBKEYS, &key);
719 if (!W_ERROR_IS_OK(werr)) {
724 werr = reg_enumkey(tmp_ctx, key, count, &subkey_name, NULL),
728 if (strequal(subkey_name, GLOBAL_NAME)) {
732 werr = smbconf_add_string_to_array(tmp_ctx,
736 if (!W_ERROR_IS_OK(werr)) {
741 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
746 *num_shares = added_count;
747 if (added_count > 0) {
748 *share_names = talloc_move(mem_ctx, &tmp_share_names);
754 TALLOC_FREE(tmp_ctx);
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)
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,
771 if (W_ERROR_IS_OK(werr)) {
775 TALLOC_FREE(mem_ctx);
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)
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);
796 * get a definition of a share (service) from configuration.
798 static WERROR smbconf_reg_get_share(struct smbconf_ctx *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,
809 if (!W_ERROR_IS_OK(werr)) {
813 werr = smbconf_reg_get_values(mem_ctx, key, num_params,
814 param_names, param_values);
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)) {
836 werr = reg_deletekey_recursive(key, key, servicename);
839 TALLOC_FREE(mem_ctx);
844 * set a configuration parameter to the value provided.
846 static WERROR smbconf_reg_set_parameter(struct smbconf_ctx *ctx,
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)) {
861 werr = smbconf_reg_set_value(key, param, valstr);
864 TALLOC_FREE(mem_ctx);
869 * get the value of a configuration parameter as a string
871 static WERROR smbconf_reg_get_parameter(struct smbconf_ctx *ctx,
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,
883 if (!W_ERROR_IS_OK(werr)) {
887 if (!smbconf_value_exists(key, param)) {
888 werr = WERR_INVALID_PARAM;
892 werr = reg_queryvalue(mem_ctx, key, param, &value);
893 if (!W_ERROR_IS_OK(werr)) {
897 *valstr = smbconf_format_registry_value(mem_ctx, value);
899 if (*valstr == NULL) {
910 * delete a parameter from configuration
912 static WERROR smbconf_reg_delete_parameter(struct smbconf_ctx *ctx,
916 struct registry_key *key = NULL;
917 WERROR werr = WERR_OK;
918 TALLOC_CTX *mem_ctx = talloc_stackframe();
920 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
922 if (!W_ERROR_IS_OK(werr)) {
926 if (!smbconf_value_exists(key, param)) {
927 werr = WERR_INVALID_PARAM;
931 werr = reg_deletevalue(key, param);
934 TALLOC_FREE(mem_ctx);
938 static WERROR smbconf_reg_get_includes(struct smbconf_ctx *ctx,
941 uint32_t *num_includes,
945 struct registry_key *key = NULL;
946 TALLOC_CTX *tmp_ctx = talloc_stackframe();
948 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
950 if (!W_ERROR_IS_OK(werr)) {
954 werr = smbconf_reg_get_includes_internal(mem_ctx, key, num_includes,
958 TALLOC_FREE(tmp_ctx);
962 static WERROR smbconf_reg_set_includes(struct smbconf_ctx *ctx,
964 uint32_t num_includes,
965 const char **includes)
967 WERROR werr = WERR_OK;
968 struct registry_key *key = NULL;
969 TALLOC_CTX *tmp_ctx = talloc_stackframe();
971 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
973 if (!W_ERROR_IS_OK(werr)) {
977 werr = smbconf_reg_set_multi_sz_value(key, INCLUDES_VALNAME,
978 num_includes, includes);
981 TALLOC_FREE(tmp_ctx);
986 struct smbconf_ops smbconf_ops_reg = {
987 .init = smbconf_reg_init,
988 .shutdown = smbconf_reg_shutdown,
989 .open_conf = smbconf_reg_open,
990 .close_conf = smbconf_reg_close,
991 .get_csn = smbconf_reg_get_csn,
992 .drop = smbconf_reg_drop,
993 .get_share_names = smbconf_reg_get_share_names,
994 .share_exists = smbconf_reg_share_exists,
995 .create_share = smbconf_reg_create_share,
996 .get_share = smbconf_reg_get_share,
997 .delete_share = smbconf_reg_delete_share,
998 .set_parameter = smbconf_reg_set_parameter,
999 .get_parameter = smbconf_reg_get_parameter,
1000 .delete_parameter = smbconf_reg_delete_parameter,
1001 .get_includes = smbconf_reg_get_includes,
1002 .set_includes = smbconf_reg_set_includes,
1007 * initialize the smbconf registry backend
1008 * the only function that is exported from this module
1010 WERROR smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
1013 return smbconf_init(mem_ctx, conf_ctx, path, &smbconf_ops_reg);