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 * Open a registry key specified by "path"
47 static WERROR
smbconf_reg_open_path(TALLOC_CTX
*mem_ctx
,
48 struct smbconf_ctx
*ctx
,
50 uint32 desired_access
,
51 struct registry_key
**key
)
53 WERROR werr
= WERR_OK
;
56 DEBUG(1, ("Error: configuration is not open!\n"));
57 werr
= WERR_INVALID_PARAM
;
61 if (rpd(ctx
)->token
== NULL
) {
62 DEBUG(1, ("Error: token missing from smbconf_ctx. "
63 "was smbconf_init() called?\n"));
64 werr
= WERR_INVALID_PARAM
;
68 werr
= ctx
->ops
->open_conf(ctx
);
69 if (!W_ERROR_IS_OK(werr
)) {
70 DEBUG(1, ("Error opening the registry.\n"));
75 DEBUG(1, ("Error: NULL path string given\n"));
76 werr
= WERR_INVALID_PARAM
;
80 werr
= reg_open_path(mem_ctx
, path
, desired_access
, rpd(ctx
)->token
,
83 if (!W_ERROR_IS_OK(werr
)) {
84 DEBUG(1, ("Error opening registry path '%s': %s\n",
85 path
, dos_errstr(werr
)));
93 * Open a subkey of the base key (i.e a service)
95 static WERROR
smbconf_reg_open_service_key(TALLOC_CTX
*mem_ctx
,
96 struct smbconf_ctx
*ctx
,
97 const char *servicename
,
98 uint32 desired_access
,
99 struct registry_key
**key
)
101 WERROR werr
= WERR_OK
;
104 if (servicename
== NULL
) {
105 DEBUG(3, ("Error: NULL servicename given.\n"));
106 werr
= WERR_INVALID_PARAM
;
110 path
= talloc_asprintf(mem_ctx
, "%s\\%s", ctx
->path
, servicename
);
116 werr
= smbconf_reg_open_path(mem_ctx
, ctx
, path
, desired_access
, key
);
126 static WERROR
smbconf_reg_open_base_key(TALLOC_CTX
*mem_ctx
,
127 struct smbconf_ctx
*ctx
,
128 uint32 desired_access
,
129 struct registry_key
**key
)
131 return smbconf_reg_open_path(mem_ctx
, ctx
, ctx
->path
, desired_access
,
136 * check if a value exists in a given registry key
138 static bool smbconf_value_exists(struct registry_key
*key
, const char *param
)
141 WERROR werr
= WERR_OK
;
142 TALLOC_CTX
*ctx
= talloc_stackframe();
143 struct registry_value
*value
= NULL
;
145 werr
= reg_queryvalue(ctx
, key
, param
, &value
);
146 if (W_ERROR_IS_OK(werr
)) {
155 * create a subkey of the base key (i.e. a service...)
157 static WERROR
smbconf_reg_create_service_key(TALLOC_CTX
*mem_ctx
,
158 struct smbconf_ctx
*ctx
,
159 const char * subkeyname
,
160 struct registry_key
**newkey
)
162 WERROR werr
= WERR_OK
;
163 struct registry_key
*create_parent
= NULL
;
164 TALLOC_CTX
*create_ctx
;
165 enum winreg_CreateAction action
= REG_ACTION_NONE
;
167 /* create a new talloc ctx for creation. it will hold
168 * the intermediate parent key (SMBCONF) for creation
169 * and will be destroyed when leaving this function... */
170 if (!(create_ctx
= talloc_stackframe())) {
175 werr
= smbconf_reg_open_base_key(create_ctx
, ctx
, REG_KEY_WRITE
,
177 if (!W_ERROR_IS_OK(werr
)) {
181 werr
= reg_createkey(mem_ctx
, create_parent
, subkeyname
,
182 REG_KEY_WRITE
, newkey
, &action
);
183 if (W_ERROR_IS_OK(werr
) && (action
!= REG_CREATED_NEW_KEY
)) {
184 DEBUG(10, ("Key '%s' already exists.\n", subkeyname
));
185 werr
= WERR_ALREADY_EXISTS
;
187 if (!W_ERROR_IS_OK(werr
)) {
188 DEBUG(5, ("Error creating key %s: %s\n",
189 subkeyname
, dos_errstr(werr
)));
193 TALLOC_FREE(create_ctx
);
198 * add a value to a key.
200 static WERROR
smbconf_reg_set_value(struct registry_key
*key
,
204 struct registry_value val
;
205 WERROR werr
= WERR_OK
;
207 const char *canon_valname
;
208 const char *canon_valstr
;
210 if (!lp_canonicalize_parameter_with_value(valname
, valstr
,
214 if (canon_valname
== NULL
) {
215 DEBUG(5, ("invalid parameter '%s' given\n",
218 DEBUG(5, ("invalid value '%s' given for "
219 "parameter '%s'\n", valstr
, valname
));
221 werr
= WERR_INVALID_PARAM
;
225 if (registry_smbconf_valname_forbidden(canon_valname
)) {
226 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
228 werr
= WERR_INVALID_PARAM
;
232 subkeyname
= strrchr_m(key
->key
->name
, '\\');
233 if ((subkeyname
== NULL
) || (*(subkeyname
+1) == '\0')) {
234 DEBUG(5, ("Invalid registry key '%s' given as "
235 "smbconf section.\n", key
->key
->name
));
236 werr
= WERR_INVALID_PARAM
;
240 if (!strequal(subkeyname
, GLOBAL_NAME
) &&
241 lp_parameter_is_global(valname
))
243 DEBUG(5, ("Global paramter '%s' not allowed in "
244 "service definition ('%s').\n", canon_valname
,
246 werr
= WERR_INVALID_PARAM
;
253 val
.v
.sz
.str
= CONST_DISCARD(char *, canon_valstr
);
254 val
.v
.sz
.len
= strlen(canon_valstr
) + 1;
256 werr
= reg_setvalue(key
, canon_valname
, &val
);
257 if (!W_ERROR_IS_OK(werr
)) {
258 DEBUG(5, ("Error adding value '%s' to "
260 canon_valname
, key
->key
->name
, dos_errstr(werr
)));
267 static WERROR
smbconf_reg_set_multi_sz_value(struct registry_key
*key
,
269 const uint32_t num_strings
,
270 const char **strings
)
273 struct registry_value
*value
;
275 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
277 if (strings
== NULL
) {
278 werr
= WERR_INVALID_PARAM
;
282 value
= TALLOC_ZERO_P(tmp_ctx
, struct registry_value
);
284 value
->type
= REG_MULTI_SZ
;
285 value
->v
.multi_sz
.num_strings
= num_strings
;
286 value
->v
.multi_sz
.strings
= TALLOC_ARRAY(tmp_ctx
, char *, num_strings
);
287 if (value
->v
.multi_sz
.strings
== NULL
) {
291 for (count
= 0; count
< num_strings
; count
++) {
292 value
->v
.multi_sz
.strings
[count
] =
293 talloc_strdup(value
->v
.multi_sz
.strings
,
295 if (value
->v
.multi_sz
.strings
[count
] == NULL
) {
301 werr
= reg_setvalue(key
, valname
, value
);
302 if (!W_ERROR_IS_OK(werr
)) {
303 DEBUG(5, ("Error adding value '%s' to key '%s': %s\n",
304 valname
, key
->key
->name
, dos_errstr(werr
)));
308 TALLOC_FREE(tmp_ctx
);
313 * format a registry_value into a string.
315 * This is intended to be used for smbconf registry values,
316 * which are ar stored as REG_SZ values, so the incomplete
317 * handling should be ok.
319 static char *smbconf_format_registry_value(TALLOC_CTX
*mem_ctx
,
320 struct registry_value
*value
)
324 /* alternatively, create a new talloc context? */
325 if (mem_ctx
== NULL
) {
329 switch (value
->type
) {
331 result
= talloc_asprintf(mem_ctx
, "%d", value
->v
.dword
);
335 result
= talloc_asprintf(mem_ctx
, "%s", value
->v
.sz
.str
);
339 for (j
= 0; j
< value
->v
.multi_sz
.num_strings
; j
++) {
340 result
= talloc_asprintf(mem_ctx
, "%s \"%s\" ",
342 value
->v
.multi_sz
.strings
[j
]);
343 if (result
== NULL
) {
350 result
= talloc_asprintf(mem_ctx
, "binary (%d bytes)",
351 (int)value
->v
.binary
.length
);
354 result
= talloc_asprintf(mem_ctx
, "<unprintable>");
361 * Get the values of a key as a list of value names
362 * and a list of value strings (ordered)
364 static WERROR
smbconf_reg_get_values(TALLOC_CTX
*mem_ctx
,
365 struct registry_key
*key
,
366 uint32_t *num_values
,
368 char ***value_strings
)
370 TALLOC_CTX
*tmp_ctx
= NULL
;
371 WERROR werr
= WERR_OK
;
373 struct registry_value
*valvalue
= NULL
;
374 char *valname
= NULL
;
375 char **tmp_valnames
= NULL
;
376 char **tmp_valstrings
= NULL
;
378 if ((num_values
== NULL
) || (value_names
== NULL
) ||
379 (value_strings
== NULL
))
381 werr
= WERR_INVALID_PARAM
;
385 tmp_ctx
= talloc_stackframe();
386 if (tmp_ctx
== NULL
) {
392 werr
= reg_enumvalue(tmp_ctx
, key
, count
, &valname
, &valvalue
),
398 werr
= smbconf_add_string_to_array(tmp_ctx
,
401 if (!W_ERROR_IS_OK(werr
)) {
405 valstring
= smbconf_format_registry_value(tmp_ctx
, valvalue
);
406 werr
= smbconf_add_string_to_array(tmp_ctx
, &tmp_valstrings
,
408 if (!W_ERROR_IS_OK(werr
)) {
412 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
420 *value_names
= talloc_move(mem_ctx
, &tmp_valnames
);
421 *value_strings
= talloc_move(mem_ctx
, &tmp_valstrings
);
424 *value_strings
= NULL
;
428 TALLOC_FREE(tmp_ctx
);
432 /**********************************************************************
434 * smbconf operations: registry implementations
436 **********************************************************************/
439 * initialize the registry smbconf backend
441 static WERROR
smbconf_reg_init(struct smbconf_ctx
*ctx
, const char *path
)
443 WERROR werr
= WERR_OK
;
448 ctx
->path
= talloc_strdup(ctx
, path
);
449 if (ctx
->path
== NULL
) {
454 ctx
->data
= TALLOC_ZERO_P(ctx
, struct reg_private_data
);
456 werr
= ntstatus_to_werror(registry_create_admin_token(ctx
,
457 &(rpd(ctx
)->token
)));
458 if (!W_ERROR_IS_OK(werr
)) {
459 DEBUG(1, ("Error creating admin token\n"));
462 rpd(ctx
)->open
= false;
464 if (!registry_init_smbconf()) {
465 werr
= WERR_REG_IO_FAILURE
;
473 static int smbconf_reg_shutdown(struct smbconf_ctx
*ctx
)
475 return ctx
->ops
->close_conf(ctx
);
478 static WERROR
smbconf_reg_open(struct smbconf_ctx
*ctx
)
482 if (rpd(ctx
)->open
) {
487 if (W_ERROR_IS_OK(werr
)) {
488 rpd(ctx
)->open
= true;
493 static int smbconf_reg_close(struct smbconf_ctx
*ctx
)
497 if (!rpd(ctx
)->open
) {
503 rpd(ctx
)->open
= false;
509 * Get the change sequence number of the given service/parameter.
510 * service and parameter strings may be NULL.
512 static void smbconf_reg_get_csn(struct smbconf_ctx
*ctx
,
513 struct smbconf_csn
*csn
,
514 const char *service
, const char *param
)
520 if (!W_ERROR_IS_OK(ctx
->ops
->open_conf(ctx
))) {
524 csn
->csn
= (uint64_t)regdb_get_seqnum();
528 * Drop the whole configuration (restarting empty) - registry version
530 static WERROR
smbconf_reg_drop(struct smbconf_ctx
*ctx
)
533 WERROR werr
= WERR_OK
;
534 struct registry_key
*parent_key
= NULL
;
535 struct registry_key
*new_key
= NULL
;
536 TALLOC_CTX
* mem_ctx
= talloc_stackframe();
537 enum winreg_CreateAction action
;
539 path
= talloc_strdup(mem_ctx
, ctx
->path
);
544 p
= strrchr(path
, '\\');
546 werr
= smbconf_reg_open_path(mem_ctx
, ctx
, path
, REG_KEY_WRITE
,
549 if (!W_ERROR_IS_OK(werr
)) {
553 werr
= reg_deletekey_recursive(mem_ctx
, parent_key
, p
+1);
555 if (!W_ERROR_IS_OK(werr
)) {
559 werr
= reg_createkey(mem_ctx
, parent_key
, p
+1, REG_KEY_WRITE
,
563 TALLOC_FREE(mem_ctx
);
568 * get the list of share names defined in the configuration.
571 static WERROR
smbconf_reg_get_share_names(struct smbconf_ctx
*ctx
,
573 uint32_t *num_shares
,
577 uint32_t added_count
= 0;
578 TALLOC_CTX
*tmp_ctx
= NULL
;
579 WERROR werr
= WERR_OK
;
580 struct registry_key
*key
= NULL
;
581 char *subkey_name
= NULL
;
582 char **tmp_share_names
= NULL
;
584 if ((num_shares
== NULL
) || (share_names
== NULL
)) {
585 werr
= WERR_INVALID_PARAM
;
589 tmp_ctx
= talloc_stackframe();
590 if (tmp_ctx
== NULL
) {
595 /* make sure "global" is always listed first */
596 if (smbconf_share_exists(ctx
, GLOBAL_NAME
)) {
597 werr
= smbconf_add_string_to_array(tmp_ctx
, &tmp_share_names
,
599 if (!W_ERROR_IS_OK(werr
)) {
605 werr
= smbconf_reg_open_base_key(tmp_ctx
, ctx
,
606 SEC_RIGHTS_ENUM_SUBKEYS
, &key
);
607 if (!W_ERROR_IS_OK(werr
)) {
612 werr
= reg_enumkey(tmp_ctx
, key
, count
, &subkey_name
, NULL
),
616 if (strequal(subkey_name
, GLOBAL_NAME
)) {
620 werr
= smbconf_add_string_to_array(tmp_ctx
,
624 if (!W_ERROR_IS_OK(werr
)) {
629 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
634 *num_shares
= added_count
;
635 if (added_count
> 0) {
636 *share_names
= talloc_move(mem_ctx
, &tmp_share_names
);
642 TALLOC_FREE(tmp_ctx
);
647 * check if a share/service of a given name exists - registry version
649 static bool smbconf_reg_share_exists(struct smbconf_ctx
*ctx
,
650 const char *servicename
)
653 WERROR werr
= WERR_OK
;
654 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
655 struct registry_key
*key
= NULL
;
657 werr
= smbconf_reg_open_service_key(mem_ctx
, ctx
, servicename
,
659 if (W_ERROR_IS_OK(werr
)) {
663 TALLOC_FREE(mem_ctx
);
668 * Add a service if it does not already exist - registry version
670 static WERROR
smbconf_reg_create_share(struct smbconf_ctx
*ctx
,
671 const char *servicename
)
674 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
675 struct registry_key
*key
= NULL
;
677 werr
= smbconf_reg_create_service_key(mem_ctx
, ctx
, servicename
, &key
);
679 TALLOC_FREE(mem_ctx
);
684 * get a definition of a share (service) from configuration.
686 static WERROR
smbconf_reg_get_share(struct smbconf_ctx
*ctx
,
688 const char *servicename
,
689 uint32_t *num_params
,
690 char ***param_names
, char ***param_values
)
692 WERROR werr
= WERR_OK
;
693 struct registry_key
*key
= NULL
;
695 werr
= smbconf_reg_open_service_key(mem_ctx
, ctx
, servicename
,
697 if (!W_ERROR_IS_OK(werr
)) {
701 werr
= smbconf_reg_get_values(mem_ctx
, key
, num_params
,
702 param_names
, param_values
);
710 * delete a service from configuration
712 static WERROR
smbconf_reg_delete_share(struct smbconf_ctx
*ctx
,
713 const char *servicename
)
715 WERROR werr
= WERR_OK
;
716 struct registry_key
*key
= NULL
;
717 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
719 werr
= smbconf_reg_open_base_key(mem_ctx
, ctx
, REG_KEY_WRITE
, &key
);
720 if (!W_ERROR_IS_OK(werr
)) {
724 werr
= reg_deletekey_recursive(key
, key
, servicename
);
727 TALLOC_FREE(mem_ctx
);
732 * set a configuration parameter to the value provided.
734 static WERROR
smbconf_reg_set_parameter(struct smbconf_ctx
*ctx
,
740 struct registry_key
*key
= NULL
;
741 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
743 werr
= smbconf_reg_open_service_key(mem_ctx
, ctx
, service
,
744 REG_KEY_WRITE
, &key
);
745 if (!W_ERROR_IS_OK(werr
)) {
749 werr
= smbconf_reg_set_value(key
, param
, valstr
);
752 TALLOC_FREE(mem_ctx
);
757 * get the value of a configuration parameter as a string
759 static WERROR
smbconf_reg_get_parameter(struct smbconf_ctx
*ctx
,
765 WERROR werr
= WERR_OK
;
766 struct registry_key
*key
= NULL
;
767 struct registry_value
*value
= NULL
;
769 werr
= smbconf_reg_open_service_key(mem_ctx
, ctx
, service
,
771 if (!W_ERROR_IS_OK(werr
)) {
775 if (!smbconf_value_exists(key
, param
)) {
776 werr
= WERR_INVALID_PARAM
;
780 werr
= reg_queryvalue(mem_ctx
, key
, param
, &value
);
781 if (!W_ERROR_IS_OK(werr
)) {
785 *valstr
= smbconf_format_registry_value(mem_ctx
, value
);
787 if (*valstr
== NULL
) {
798 * delete a parameter from configuration
800 static WERROR
smbconf_reg_delete_parameter(struct smbconf_ctx
*ctx
,
804 struct registry_key
*key
= NULL
;
805 WERROR werr
= WERR_OK
;
806 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
808 werr
= smbconf_reg_open_service_key(mem_ctx
, ctx
, service
,
810 if (!W_ERROR_IS_OK(werr
)) {
814 if (!smbconf_value_exists(key
, param
)) {
815 werr
= WERR_INVALID_PARAM
;
819 werr
= reg_deletevalue(key
, param
);
822 TALLOC_FREE(mem_ctx
);
826 static WERROR
smbconf_reg_get_includes(struct smbconf_ctx
*ctx
,
829 uint32_t *num_includes
,
834 struct registry_key
*key
= NULL
;
835 struct registry_value
*value
= NULL
;
836 char **tmp_includes
= NULL
;
837 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
839 werr
= smbconf_reg_open_service_key(tmp_ctx
, ctx
, service
,
841 if (!W_ERROR_IS_OK(werr
)) {
845 if (!smbconf_value_exists(key
, INCLUDES_VALNAME
)) {
850 werr
= reg_queryvalue(tmp_ctx
, key
, INCLUDES_VALNAME
, &value
);
851 if (!W_ERROR_IS_OK(werr
)) {
855 if (value
->type
!= REG_MULTI_SZ
) {
856 /* wront type -- ignore */
860 for (count
= 0; count
< value
->v
.multi_sz
.num_strings
; count
++)
862 werr
= smbconf_add_string_to_array(tmp_ctx
,
865 value
->v
.multi_sz
.strings
[count
]);
866 if (!W_ERROR_IS_OK(werr
)) {
872 *includes
= talloc_move(mem_ctx
, &tmp_includes
);
873 if (*includes
== NULL
) {
877 *num_includes
= count
;
884 TALLOC_FREE(tmp_ctx
);
888 static WERROR
smbconf_reg_set_includes(struct smbconf_ctx
*ctx
,
890 uint32_t num_includes
,
891 const char **includes
)
893 WERROR werr
= WERR_OK
;
894 struct registry_key
*key
= NULL
;
895 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
897 werr
= smbconf_reg_open_service_key(tmp_ctx
, ctx
, service
,
899 if (!W_ERROR_IS_OK(werr
)) {
903 werr
= smbconf_reg_set_multi_sz_value(key
, INCLUDES_VALNAME
,
904 num_includes
, includes
);
907 TALLOC_FREE(tmp_ctx
);
912 struct smbconf_ops smbconf_ops_reg
= {
913 .init
= smbconf_reg_init
,
914 .shutdown
= smbconf_reg_shutdown
,
915 .open_conf
= smbconf_reg_open
,
916 .close_conf
= smbconf_reg_close
,
917 .get_csn
= smbconf_reg_get_csn
,
918 .drop
= smbconf_reg_drop
,
919 .get_share_names
= smbconf_reg_get_share_names
,
920 .share_exists
= smbconf_reg_share_exists
,
921 .create_share
= smbconf_reg_create_share
,
922 .get_share
= smbconf_reg_get_share
,
923 .delete_share
= smbconf_reg_delete_share
,
924 .set_parameter
= smbconf_reg_set_parameter
,
925 .get_parameter
= smbconf_reg_get_parameter
,
926 .delete_parameter
= smbconf_reg_delete_parameter
,
927 .get_includes
= smbconf_reg_get_includes
,
928 .set_includes
= smbconf_reg_set_includes
,
933 * initialize the smbconf registry backend
934 * the only function that is exported from this module
936 WERROR
smbconf_init_reg(TALLOC_CTX
*mem_ctx
, struct smbconf_ctx
**conf_ctx
,
939 return smbconf_init(mem_ctx
, conf_ctx
, path
, &smbconf_ops_reg
);