2 * Unix SMB/CIFS implementation.
3 * libnet smbconf registry Support
4 * Copyright (C) Michael Adam 2007-2008
5 * Copyright (C) Guenther Deschner 2007
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "smbconf_private.h"
24 /**********************************************************************
26 * Helper functions (mostly registry related)
28 **********************************************************************/
31 * add a string to a talloced array of strings.
33 static WERROR
smbconf_add_string_to_array(TALLOC_CTX
*mem_ctx
,
38 char **new_array
= NULL
;
40 if ((array
== NULL
) || (string
== NULL
)) {
41 return WERR_INVALID_PARAM
;
44 new_array
= TALLOC_REALLOC_ARRAY(mem_ctx
, *array
, char *, count
+ 1);
45 if (new_array
== NULL
) {
49 new_array
[count
] = talloc_strdup(new_array
, string
);
50 if (new_array
[count
] == NULL
) {
51 TALLOC_FREE(new_array
);
60 static WERROR
smbconf_reg_initialize(struct smbconf_ctx
*ctx
)
62 WERROR werr
= WERR_OK
;
64 if (!registry_init_smbconf()) {
65 werr
= WERR_REG_IO_FAILURE
;
69 werr
= ntstatus_to_werror(registry_create_admin_token(ctx
,
71 if (!W_ERROR_IS_OK(werr
)) {
72 DEBUG(1, ("Error creating admin token\n"));
81 * Open a registry key specified by "path"
83 static WERROR
smbconf_reg_open_path(TALLOC_CTX
*mem_ctx
,
84 struct smbconf_ctx
*ctx
,
86 uint32 desired_access
,
87 struct registry_key
**key
)
89 WERROR werr
= WERR_OK
;
92 DEBUG(1, ("Error: configuration is not open!\n"));
93 werr
= WERR_INVALID_PARAM
;
97 if (ctx
->token
== NULL
) {
98 DEBUG(1, ("Error: token missing from smbconf_ctx. "
99 "was smbconf_open() called?\n"));
100 werr
= WERR_INVALID_PARAM
;
105 DEBUG(1, ("Error: NULL path string given\n"));
106 werr
= WERR_INVALID_PARAM
;
110 werr
= reg_open_path(mem_ctx
, path
, desired_access
, ctx
->token
, key
);
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 KEY_SMBCONF (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", KEY_SMBCONF
, servicename
);
145 werr
= smbconf_reg_open_path(mem_ctx
, ctx
, path
, desired_access
, key
);
153 * open the base key KEY_SMBCONF
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
, KEY_SMBCONF
, 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 KEY_SMBCONF
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
;
257 val
.v
.sz
.str
= CONST_DISCARD(char *, canon_valstr
);
258 val
.v
.sz
.len
= strlen(canon_valstr
) + 1;
260 if (registry_smbconf_valname_forbidden(canon_valname
)) {
261 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
263 werr
= WERR_INVALID_PARAM
;
267 subkeyname
= strrchr_m(key
->key
->name
, '\\');
268 if ((subkeyname
== NULL
) || (*(subkeyname
+1) == '\0')) {
269 DEBUG(5, ("Invalid registry key '%s' given as "
270 "smbconf section.\n", key
->key
->name
));
271 werr
= WERR_INVALID_PARAM
;
275 if (!strequal(subkeyname
, GLOBAL_NAME
) &&
276 lp_parameter_is_global(valname
))
278 DEBUG(5, ("Global paramter '%s' not allowed in "
279 "service definition ('%s').\n", canon_valname
,
281 werr
= WERR_INVALID_PARAM
;
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
)));
297 * format a registry_value into a string.
299 * This is intended to be used for smbconf registry values,
300 * which are ar stored as REG_SZ values, so the incomplete
301 * handling should be ok.
303 static char *smbconf_format_registry_value(TALLOC_CTX
*mem_ctx
,
304 struct registry_value
*value
)
308 /* alternatively, create a new talloc context? */
309 if (mem_ctx
== NULL
) {
313 switch (value
->type
) {
315 result
= talloc_asprintf(mem_ctx
, "%d", value
->v
.dword
);
319 result
= talloc_asprintf(mem_ctx
, "%s", value
->v
.sz
.str
);
323 for (j
= 0; j
< value
->v
.multi_sz
.num_strings
; j
++) {
324 result
= talloc_asprintf(mem_ctx
, "%s \"%s\" ",
326 value
->v
.multi_sz
.strings
[j
]);
327 if (result
== NULL
) {
334 result
= talloc_asprintf(mem_ctx
, "binary (%d bytes)",
335 (int)value
->v
.binary
.length
);
338 result
= talloc_asprintf(mem_ctx
, "<unprintable>");
345 * Get the values of a key as a list of value names
346 * and a list of value strings (ordered)
348 static WERROR
smbconf_reg_get_values(TALLOC_CTX
*mem_ctx
,
349 struct registry_key
*key
,
350 uint32_t *num_values
,
352 char ***value_strings
)
354 TALLOC_CTX
*tmp_ctx
= NULL
;
355 WERROR werr
= WERR_OK
;
357 struct registry_value
*valvalue
= NULL
;
358 char *valname
= NULL
;
359 char **tmp_valnames
= NULL
;
360 char **tmp_valstrings
= NULL
;
362 if ((num_values
== NULL
) || (value_names
== NULL
) ||
363 (value_strings
== NULL
))
365 werr
= WERR_INVALID_PARAM
;
369 tmp_ctx
= talloc_stackframe();
370 if (tmp_ctx
== NULL
) {
376 W_ERROR_IS_OK(werr
= reg_enumvalue(tmp_ctx
, key
, count
, &valname
,
382 werr
= smbconf_add_string_to_array(tmp_ctx
,
385 if (!W_ERROR_IS_OK(werr
)) {
389 valstring
= smbconf_format_registry_value(tmp_ctx
, valvalue
);
390 werr
= smbconf_add_string_to_array(tmp_ctx
, &tmp_valstrings
,
392 if (!W_ERROR_IS_OK(werr
)) {
396 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
404 *value_names
= talloc_move(mem_ctx
, &tmp_valnames
);
405 *value_strings
= talloc_move(mem_ctx
, &tmp_valstrings
);
408 *value_strings
= NULL
;
412 TALLOC_FREE(tmp_ctx
);
416 static int smbconf_destroy_ctx(struct smbconf_ctx
*ctx
)
418 return regdb_close();
422 * Get the change sequence number of the given service/parameter.
423 * service and parameter strings may be NULL.
425 static void smbconf_reg_get_csn(struct smbconf_ctx
*ctx
,
426 struct smbconf_csn
*csn
,
427 const char *service
, const char *param
)
432 csn
->csn
= (uint64_t)regdb_get_seqnum();
435 static WERROR
smbconf_global_check(struct smbconf_ctx
*ctx
)
437 if (!smbconf_share_exists(ctx
, GLOBAL_NAME
)) {
438 return smbconf_create_share(ctx
, GLOBAL_NAME
);
443 /**********************************************************************
445 * The actual net conf api functions, that are exported.
447 **********************************************************************/
450 * Open the configuration.
452 * This should be the first function in a sequence of calls to smbconf
455 * Upon success, this creates and returns the conf context
456 * that should be passed around in subsequent calls to the other
459 * After the work with the configuration is completed, smbconf_close()
462 WERROR
smbconf_open(TALLOC_CTX
*mem_ctx
, struct smbconf_ctx
**conf_ctx
)
464 WERROR werr
= WERR_OK
;
465 struct smbconf_ctx
*ctx
;
467 if (conf_ctx
== NULL
) {
468 return WERR_INVALID_PARAM
;
471 ctx
= TALLOC_ZERO_P(mem_ctx
, struct smbconf_ctx
);
476 werr
= smbconf_reg_initialize(ctx
);
477 if (!W_ERROR_IS_OK(werr
)) {
481 talloc_set_destructor(ctx
, smbconf_destroy_ctx
);
492 * Close the configuration.
494 void smbconf_close(struct smbconf_ctx
*ctx
)
496 /* this also closes the registry (by destructor): */
501 * Detect changes in the configuration.
502 * The given csn struct is filled with the current csn.
503 * smbconf_changed() can also be used for initial retrieval
506 bool smbconf_changed(struct smbconf_ctx
*ctx
, struct smbconf_csn
*csn
,
507 const char *service
, const char *param
)
509 struct smbconf_csn old_csn
;
517 smbconf_reg_get_csn(ctx
, csn
, service
, param
);
518 return (csn
->csn
!= old_csn
.csn
);
522 * Drop the whole configuration (restarting empty).
524 WERROR
smbconf_drop(struct smbconf_ctx
*ctx
)
527 WERROR werr
= WERR_OK
;
528 struct registry_key
*parent_key
= NULL
;
529 struct registry_key
*new_key
= NULL
;
530 TALLOC_CTX
* mem_ctx
= talloc_stackframe();
531 enum winreg_CreateAction action
;
533 path
= talloc_strdup(mem_ctx
, KEY_SMBCONF
);
538 p
= strrchr(path
, '\\');
540 werr
= smbconf_reg_open_path(mem_ctx
, ctx
, path
, REG_KEY_WRITE
,
543 if (!W_ERROR_IS_OK(werr
)) {
547 werr
= reg_deletekey_recursive(mem_ctx
, parent_key
, p
+1);
549 if (!W_ERROR_IS_OK(werr
)) {
553 werr
= reg_createkey(mem_ctx
, parent_key
, p
+1, REG_KEY_WRITE
,
557 TALLOC_FREE(mem_ctx
);
562 * Get the whole configuration as lists of strings with counts:
564 * num_shares : number of shares
565 * share_names : list of length num_shares of share names
566 * num_params : list of length num_shares of parameter counts for each share
567 * param_names : list of lists of parameter names for each share
568 * param_values : list of lists of parameter values for each share
570 WERROR
smbconf_get_config(struct smbconf_ctx
*ctx
,
572 uint32_t *num_shares
,
573 char ***share_names
, uint32_t **num_params
,
574 char ****param_names
, char ****param_values
)
576 WERROR werr
= WERR_OK
;
577 TALLOC_CTX
*tmp_ctx
= NULL
;
578 uint32_t tmp_num_shares
;
579 char **tmp_share_names
;
580 uint32_t *tmp_num_params
;
581 char ***tmp_param_names
;
582 char ***tmp_param_values
;
585 if ((num_shares
== NULL
) || (share_names
== NULL
) ||
586 (num_params
== NULL
) || (param_names
== NULL
) ||
587 (param_values
== NULL
))
589 werr
= WERR_INVALID_PARAM
;
593 tmp_ctx
= talloc_stackframe();
594 if (tmp_ctx
== NULL
) {
599 werr
= smbconf_get_share_names(ctx
, tmp_ctx
, &tmp_num_shares
,
601 if (!W_ERROR_IS_OK(werr
)) {
605 tmp_num_params
= TALLOC_ARRAY(tmp_ctx
, uint32_t, tmp_num_shares
);
606 tmp_param_names
= TALLOC_ARRAY(tmp_ctx
, char **, tmp_num_shares
);
607 tmp_param_values
= TALLOC_ARRAY(tmp_ctx
, char **, tmp_num_shares
);
609 if ((tmp_num_params
== NULL
) || (tmp_param_names
== NULL
) ||
610 (tmp_param_values
== NULL
))
616 for (count
= 0; count
< tmp_num_shares
; count
++) {
617 werr
= smbconf_get_share(ctx
, mem_ctx
,
618 tmp_share_names
[count
],
619 &tmp_num_params
[count
],
620 &tmp_param_names
[count
],
621 &tmp_param_values
[count
]);
622 if (!W_ERROR_IS_OK(werr
)) {
629 *num_shares
= tmp_num_shares
;
630 if (tmp_num_shares
> 0) {
631 *share_names
= talloc_move(mem_ctx
, &tmp_share_names
);
632 *num_params
= talloc_move(mem_ctx
, &tmp_num_params
);
633 *param_names
= talloc_move(mem_ctx
, &tmp_param_names
);
634 *param_values
= talloc_move(mem_ctx
, &tmp_param_values
);
639 *param_values
= NULL
;
643 TALLOC_FREE(tmp_ctx
);
648 * get the list of share names defined in the configuration.
650 WERROR
smbconf_get_share_names(struct smbconf_ctx
*ctx
,
652 uint32_t *num_shares
,
656 uint32_t added_count
= 0;
657 TALLOC_CTX
*tmp_ctx
= NULL
;
658 WERROR werr
= WERR_OK
;
659 struct registry_key
*key
= NULL
;
660 char *subkey_name
= NULL
;
661 char **tmp_share_names
= NULL
;
663 if ((num_shares
== NULL
) || (share_names
== NULL
)) {
664 werr
= WERR_INVALID_PARAM
;
668 tmp_ctx
= talloc_stackframe();
669 if (tmp_ctx
== NULL
) {
674 /* make sure "global" is always listed first */
675 if (smbconf_share_exists(ctx
, GLOBAL_NAME
)) {
676 werr
= smbconf_add_string_to_array(tmp_ctx
, &tmp_share_names
,
678 if (!W_ERROR_IS_OK(werr
)) {
684 werr
= smbconf_reg_open_base_key(tmp_ctx
, ctx
,
685 SEC_RIGHTS_ENUM_SUBKEYS
, &key
);
686 if (!W_ERROR_IS_OK(werr
)) {
691 W_ERROR_IS_OK(werr
= reg_enumkey(tmp_ctx
, key
, count
,
692 &subkey_name
, NULL
));
695 if (strequal(subkey_name
, GLOBAL_NAME
)) {
699 werr
= smbconf_add_string_to_array(tmp_ctx
,
703 if (!W_ERROR_IS_OK(werr
)) {
708 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS
, werr
)) {
713 *num_shares
= added_count
;
714 if (added_count
> 0) {
715 *share_names
= talloc_move(mem_ctx
, &tmp_share_names
);
721 TALLOC_FREE(tmp_ctx
);
726 * check if a share/service of a given name exists
728 bool smbconf_share_exists(struct smbconf_ctx
*ctx
,
729 const char *servicename
)
732 WERROR werr
= WERR_OK
;
733 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
734 struct registry_key
*key
= NULL
;
736 werr
= smbconf_reg_open_service_key(mem_ctx
, ctx
, servicename
,
738 if (W_ERROR_IS_OK(werr
)) {
742 TALLOC_FREE(mem_ctx
);
747 * Add a service if it does not already exist.
749 WERROR
smbconf_create_share(struct smbconf_ctx
*ctx
,
750 const char *servicename
)
753 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
754 struct registry_key
*key
= NULL
;
756 if (smbconf_share_exists(ctx
, servicename
)) {
757 werr
= WERR_ALREADY_EXISTS
;
761 werr
= smbconf_reg_create_service_key(mem_ctx
, ctx
, servicename
, &key
);
764 TALLOC_FREE(mem_ctx
);
769 * get a definition of a share (service) from configuration.
771 WERROR
smbconf_get_share(struct smbconf_ctx
*ctx
,
773 const char *servicename
, uint32_t *num_params
,
774 char ***param_names
, char ***param_values
)
776 WERROR werr
= WERR_OK
;
777 struct registry_key
*key
= NULL
;
779 werr
= smbconf_reg_open_service_key(mem_ctx
, ctx
, servicename
,
781 if (!W_ERROR_IS_OK(werr
)) {
785 werr
= smbconf_reg_get_values(mem_ctx
, key
, num_params
,
786 param_names
, param_values
);
794 * delete a service from configuration
796 WERROR
smbconf_delete_share(struct smbconf_ctx
*ctx
, const char *servicename
)
798 WERROR werr
= WERR_OK
;
799 struct registry_key
*key
= NULL
;
800 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
802 werr
= smbconf_reg_open_base_key(mem_ctx
, ctx
, REG_KEY_WRITE
, &key
);
803 if (!W_ERROR_IS_OK(werr
)) {
807 werr
= reg_deletekey_recursive(key
, key
, servicename
);
810 TALLOC_FREE(mem_ctx
);
815 * set a configuration parameter to the value provided.
817 WERROR
smbconf_set_parameter(struct smbconf_ctx
*ctx
,
823 struct registry_key
*key
= NULL
;
824 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
826 if (!smbconf_share_exists(ctx
, service
)) {
827 werr
= WERR_NO_SUCH_SERVICE
;
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 * Set a global parameter
846 * (i.e. a parameter in the [global] service).
848 * This also creates [global] when it does not exist.
850 WERROR
smbconf_set_global_parameter(struct smbconf_ctx
*ctx
,
851 const char *param
, const char *val
)
855 werr
= smbconf_global_check(ctx
);
856 if (W_ERROR_IS_OK(werr
)) {
857 werr
= smbconf_set_parameter(ctx
, GLOBAL_NAME
, param
, val
);
864 * get the value of a configuration parameter as a string
866 WERROR
smbconf_get_parameter(struct smbconf_ctx
*ctx
,
872 WERROR werr
= WERR_OK
;
873 struct registry_key
*key
= NULL
;
874 struct registry_value
*value
= NULL
;
876 if (valstr
== NULL
) {
877 werr
= WERR_INVALID_PARAM
;
881 if (!smbconf_share_exists(ctx
, service
)) {
882 werr
= WERR_NO_SUCH_SERVICE
;
886 werr
= smbconf_reg_open_service_key(mem_ctx
, ctx
, service
,
888 if (!W_ERROR_IS_OK(werr
)) {
892 if (!smbconf_value_exists(key
, param
)) {
893 werr
= WERR_INVALID_PARAM
;
897 werr
= reg_queryvalue(mem_ctx
, key
, param
, &value
);
898 if (!W_ERROR_IS_OK(werr
)) {
902 *valstr
= smbconf_format_registry_value(mem_ctx
, value
);
904 if (*valstr
== NULL
) {
915 * Get the value of a global parameter.
917 * Create [global] if it does not exist.
919 WERROR
smbconf_get_global_parameter(struct smbconf_ctx
*ctx
,
926 werr
= smbconf_global_check(ctx
);
927 if (W_ERROR_IS_OK(werr
)) {
928 werr
= smbconf_get_parameter(ctx
, mem_ctx
, GLOBAL_NAME
, param
,
936 * delete a parameter from configuration
938 WERROR
smbconf_delete_parameter(struct smbconf_ctx
*ctx
,
939 const char *service
, const char *param
)
941 struct registry_key
*key
= NULL
;
942 WERROR werr
= WERR_OK
;
943 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
945 if (!smbconf_share_exists(ctx
, service
)) {
946 return WERR_NO_SUCH_SERVICE
;
949 werr
= smbconf_reg_open_service_key(mem_ctx
, ctx
, service
,
951 if (!W_ERROR_IS_OK(werr
)) {
955 if (!smbconf_value_exists(key
, param
)) {
956 werr
= WERR_INVALID_PARAM
;
960 werr
= reg_deletevalue(key
, param
);
963 TALLOC_FREE(mem_ctx
);
968 * Delete a global parameter.
970 * Create [global] if it does not exist.
972 WERROR
smbconf_delete_global_parameter(struct smbconf_ctx
*ctx
,
977 werr
= smbconf_global_check(ctx
);
978 if (W_ERROR_IS_OK(werr
)) {
979 werr
= smbconf_delete_parameter(ctx
, GLOBAL_NAME
, param
);