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 "libnet/libnet.h"
24 /**********************************************************************
26 * Helper functions (mostly registry related)
27 * TODO: These should be eventually static.
29 **********************************************************************/
32 * add a string to a talloced array of strings.
34 static WERROR libnet_conf_add_string_to_array(TALLOC_CTX *mem_ctx,
39 char **new_array = NULL;
41 if ((array == NULL) || (string == NULL)) {
42 return WERR_INVALID_PARAM;
45 new_array = TALLOC_REALLOC_ARRAY(mem_ctx, *array, char *, count + 1);
46 if (new_array == NULL) {
50 new_array[count] = talloc_strdup(new_array, string);
57 static WERROR libnet_conf_reg_initialize(struct libnet_conf_ctx *ctx)
59 WERROR werr = WERR_OK;
61 if (!registry_init_regdb()) {
62 werr = WERR_REG_IO_FAILURE;
66 werr = ntstatus_to_werror(registry_create_admin_token(ctx,
68 if (!W_ERROR_IS_OK(werr)) {
69 DEBUG(1, ("Error creating admin token\n"));
78 * Open a registry key specified by "path"
80 static WERROR libnet_conf_reg_open_path(TALLOC_CTX *mem_ctx,
81 struct libnet_conf_ctx *ctx,
83 uint32 desired_access,
84 struct registry_key **key)
86 WERROR werr = WERR_OK;
89 DEBUG(1, ("Error: configuration is not open!\n"));
90 werr = WERR_INVALID_PARAM;
94 if (ctx->token == NULL) {
95 DEBUG(1, ("Error: token missing from libnet_conf_ctx. "
96 "was libnet_conf_open() called?\n"));
97 werr = WERR_INVALID_PARAM;
102 DEBUG(1, ("Error: NULL path string given\n"));
103 werr = WERR_INVALID_PARAM;
107 werr = reg_open_path(mem_ctx, path, desired_access, ctx->token, key);
109 if (!W_ERROR_IS_OK(werr)) {
110 DEBUG(1, ("Error opening registry path '%s': %s\n",
111 path, dos_errstr(werr)));
119 * Open a subkey of KEY_SMBCONF (i.e a service)
121 static WERROR libnet_conf_reg_open_service_key(TALLOC_CTX *mem_ctx,
122 struct libnet_conf_ctx *ctx,
123 const char *servicename,
124 uint32 desired_access,
125 struct registry_key **key)
127 WERROR werr = WERR_OK;
130 if (servicename == NULL) {
131 DEBUG(3, ("Error: NULL servicename given.\n"));
132 werr = WERR_INVALID_PARAM;
136 path = talloc_asprintf(mem_ctx, "%s\\%s", KEY_SMBCONF, servicename);
138 werr = libnet_conf_reg_open_path(mem_ctx, ctx, path, desired_access,
147 * open the base key KEY_SMBCONF
149 static WERROR libnet_conf_reg_open_base_key(TALLOC_CTX *mem_ctx,
150 struct libnet_conf_ctx *ctx,
151 uint32 desired_access,
152 struct registry_key **key)
154 return libnet_conf_reg_open_path(mem_ctx, ctx, KEY_SMBCONF,
155 desired_access, key);
159 * check if a value exists in a given registry key
161 static bool libnet_conf_value_exists(struct registry_key *key,
165 WERROR werr = WERR_OK;
166 TALLOC_CTX *ctx = talloc_stackframe();
167 struct registry_value *value = NULL;
169 werr = reg_queryvalue(ctx, key, param, &value);
170 if (W_ERROR_IS_OK(werr)) {
179 * create a subkey of KEY_SMBCONF
181 static WERROR libnet_conf_reg_create_service_key(TALLOC_CTX *mem_ctx,
182 struct libnet_conf_ctx *ctx,
183 const char * subkeyname,
184 struct registry_key **newkey)
186 WERROR werr = WERR_OK;
187 struct registry_key *create_parent = NULL;
188 TALLOC_CTX *create_ctx;
189 enum winreg_CreateAction action = REG_ACTION_NONE;
191 /* create a new talloc ctx for creation. it will hold
192 * the intermediate parent key (SMBCONF) for creation
193 * and will be destroyed when leaving this function... */
194 if (!(create_ctx = talloc_new(mem_ctx))) {
199 werr = libnet_conf_reg_open_base_key(create_ctx, ctx, REG_KEY_WRITE,
201 if (!W_ERROR_IS_OK(werr)) {
205 werr = reg_createkey(mem_ctx, create_parent, subkeyname,
206 REG_KEY_WRITE, newkey, &action);
207 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
208 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
209 werr = WERR_ALREADY_EXISTS;
211 if (!W_ERROR_IS_OK(werr)) {
212 DEBUG(5, ("Error creating key %s: %s\n",
213 subkeyname, dos_errstr(werr)));
217 TALLOC_FREE(create_ctx);
222 * add a value to a key.
224 static WERROR libnet_conf_reg_set_value(struct registry_key *key,
228 struct registry_value val;
229 WERROR werr = WERR_OK;
231 const char *canon_valname;
232 const char *canon_valstr;
234 if (!lp_canonicalize_parameter_with_value(valname, valstr,
238 if (canon_valname == NULL) {
239 DEBUG(5, ("invalid parameter '%s' given\n",
242 DEBUG(5, ("invalid value '%s' given for "
243 "parameter '%s'\n", valstr, valname));
245 werr = WERR_INVALID_PARAM;
252 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
253 val.v.sz.len = strlen(canon_valstr) + 1;
255 if (registry_smbconf_valname_forbidden(canon_valname)) {
256 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
258 werr = WERR_INVALID_PARAM;
262 subkeyname = strrchr_m(key->key->name, '\\');
263 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
264 DEBUG(5, ("Invalid registry key '%s' given as "
265 "smbconf section.\n", key->key->name));
266 werr = WERR_INVALID_PARAM;
270 if (!strequal(subkeyname, GLOBAL_NAME) &&
271 lp_parameter_is_global(valname))
273 DEBUG(5, ("Global paramter '%s' not allowed in "
274 "service definition ('%s').\n", canon_valname,
276 werr = WERR_INVALID_PARAM;
280 werr = reg_setvalue(key, canon_valname, &val);
281 if (!W_ERROR_IS_OK(werr)) {
282 DEBUG(5, ("Error adding value '%s' to "
284 canon_valname, key->key->name, dos_errstr(werr)));
292 * format a registry_value into a string.
294 * This is intended to be used for smbconf registry values,
295 * which are ar stored as REG_SZ values, so the incomplete
296 * handling should be ok.
298 static char *libnet_conf_format_registry_value(TALLOC_CTX *mem_ctx,
299 struct registry_value *value)
303 /* alternatively, create a new talloc context? */
304 if (mem_ctx == NULL) {
308 switch (value->type) {
310 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
314 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
318 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
319 result = talloc_asprintf(mem_ctx, "\"%s\" ",
320 value->v.multi_sz.strings[j]);
325 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
326 (int)value->v.binary.length);
329 result = talloc_asprintf(mem_ctx, "<unprintable>");
336 * Get the values of a key as a list of value names
337 * and a list of value strings (ordered)
339 static WERROR libnet_conf_reg_get_values(TALLOC_CTX *mem_ctx,
340 struct registry_key *key,
341 uint32_t *num_values,
343 char ***value_strings)
345 TALLOC_CTX *tmp_ctx = NULL;
346 WERROR werr = WERR_OK;
348 struct registry_value *valvalue = NULL;
349 char *valname = NULL;
350 char **tmp_valnames = NULL;
351 char **tmp_valstrings = NULL;
353 if ((num_values == NULL) || (value_names == NULL) ||
354 (value_strings == NULL))
356 werr = WERR_INVALID_PARAM;
360 tmp_ctx = talloc_new(mem_ctx);
361 if (tmp_ctx == NULL) {
367 W_ERROR_IS_OK(werr = reg_enumvalue(tmp_ctx, key, count, &valname,
373 werr = libnet_conf_add_string_to_array(tmp_ctx,
376 if (!W_ERROR_IS_OK(werr)) {
380 valstring = libnet_conf_format_registry_value(tmp_ctx,
382 werr = libnet_conf_add_string_to_array(tmp_ctx,
386 if (!W_ERROR_IS_OK(werr)) {
390 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
398 *value_names = talloc_move(mem_ctx, &tmp_valnames);
399 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
402 *value_strings = NULL;
406 TALLOC_FREE(tmp_ctx);
410 static int libnet_conf_destroy_ctx(struct libnet_conf_ctx *ctx)
412 return regdb_close();
415 /**********************************************************************
417 * The actual net conf api functions, that are exported.
419 **********************************************************************/
422 * Open the configuration.
424 * This should be the first function in a sequence of calls to libnet_conf
427 * Upon success, this creates and returns the conf context
428 * that should be passed around in subsequent calls to the other
429 * libnet_conf functions.
431 * After the work with the configuration is completed, libnet_conf_close()
434 WERROR libnet_conf_open(TALLOC_CTX *mem_ctx, struct libnet_conf_ctx **conf_ctx)
436 WERROR werr = WERR_OK;
437 struct libnet_conf_ctx *ctx;
439 if (conf_ctx == NULL) {
440 return WERR_INVALID_PARAM;
443 ctx = TALLOC_ZERO_P(mem_ctx, struct libnet_conf_ctx);
448 werr = libnet_conf_reg_initialize(ctx);
449 if (!W_ERROR_IS_OK(werr)) {
453 talloc_set_destructor(ctx, libnet_conf_destroy_ctx);
464 * Close the configuration.
466 void libnet_conf_close(struct libnet_conf_ctx *ctx)
468 /* this also closes the registry (by destructor): */
473 * Drop the whole configuration (restarting empty).
475 WERROR libnet_conf_drop(struct libnet_conf_ctx *ctx)
478 WERROR werr = WERR_OK;
479 struct registry_key *parent_key = NULL;
480 struct registry_key *new_key = NULL;
481 TALLOC_CTX* mem_ctx = talloc_stackframe();
482 enum winreg_CreateAction action;
484 path = talloc_strdup(mem_ctx, KEY_SMBCONF);
489 p = strrchr(path, '\\');
491 werr = libnet_conf_reg_open_path(mem_ctx, ctx, path, REG_KEY_WRITE,
494 if (!W_ERROR_IS_OK(werr)) {
498 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
500 if (!W_ERROR_IS_OK(werr)) {
504 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
508 TALLOC_FREE(mem_ctx);
513 * Get the whole configuration as lists of strings with counts:
515 * num_shares : number of shares
516 * share_names : list of length num_shares of share names
517 * num_params : list of length num_shares of parameter counts for each share
518 * param_names : list of lists of parameter names for each share
519 * param_values : list of lists of parameter values for each share
521 WERROR libnet_conf_get_config(TALLOC_CTX *mem_ctx,
522 struct libnet_conf_ctx *ctx, uint32_t *num_shares,
523 char ***share_names, uint32_t **num_params,
524 char ****param_names, char ****param_values)
526 WERROR werr = WERR_OK;
527 TALLOC_CTX *tmp_ctx = NULL;
528 uint32_t tmp_num_shares;
529 char **tmp_share_names;
530 uint32_t *tmp_num_params;
531 char ***tmp_param_names;
532 char ***tmp_param_values;
535 if ((num_shares == NULL) || (share_names == NULL) ||
536 (num_params == NULL) || (param_names == NULL) ||
537 (param_values == NULL))
539 werr = WERR_INVALID_PARAM;
543 tmp_ctx = talloc_new(mem_ctx);
544 if (tmp_ctx == NULL) {
549 werr = libnet_conf_get_share_names(tmp_ctx, ctx, &tmp_num_shares,
551 if (!W_ERROR_IS_OK(werr)) {
555 tmp_num_params = TALLOC_ARRAY(tmp_ctx, uint32_t, tmp_num_shares);
556 tmp_param_names = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
557 tmp_param_values = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
559 if ((tmp_num_params == NULL) || (tmp_param_names == NULL) ||
560 (tmp_param_values == NULL))
566 for (count = 0; count < tmp_num_shares; count++) {
567 werr = libnet_conf_get_share(mem_ctx, ctx,
568 tmp_share_names[count],
569 &tmp_num_params[count],
570 &tmp_param_names[count],
571 &tmp_param_values[count]);
572 if (!W_ERROR_IS_OK(werr)) {
579 *num_shares = tmp_num_shares;
580 if (tmp_num_shares > 0) {
581 *share_names = talloc_move(mem_ctx, &tmp_share_names);
582 *num_params = talloc_move(mem_ctx, &tmp_num_params);
583 *param_names = talloc_move(mem_ctx, &tmp_param_names);
584 *param_values = talloc_move(mem_ctx, &tmp_param_values);
589 *param_values = NULL;
593 TALLOC_FREE(tmp_ctx);
598 * get the list of share names defined in the configuration.
600 WERROR libnet_conf_get_share_names(TALLOC_CTX *mem_ctx,
601 struct libnet_conf_ctx *ctx,
602 uint32_t *num_shares,
606 uint32_t added_count = 0;
607 TALLOC_CTX *tmp_ctx = NULL;
608 WERROR werr = WERR_OK;
609 struct registry_key *key = NULL;
610 char *subkey_name = NULL;
611 char **tmp_share_names = NULL;
613 if ((num_shares == NULL) || (share_names == NULL)) {
614 werr = WERR_INVALID_PARAM;
618 tmp_ctx = talloc_new(mem_ctx);
619 if (tmp_ctx == NULL) {
624 /* make sure "global" is always listed first */
625 if (libnet_conf_share_exists(ctx, GLOBAL_NAME)) {
626 werr = libnet_conf_add_string_to_array(tmp_ctx,
629 if (!W_ERROR_IS_OK(werr)) {
635 werr = libnet_conf_reg_open_base_key(tmp_ctx, ctx,
636 SEC_RIGHTS_ENUM_SUBKEYS, &key);
637 if (!W_ERROR_IS_OK(werr)) {
642 W_ERROR_IS_OK(werr = reg_enumkey(tmp_ctx, key, count,
643 &subkey_name, NULL));
646 if (strequal(subkey_name, GLOBAL_NAME)) {
650 werr = libnet_conf_add_string_to_array(tmp_ctx,
654 if (!W_ERROR_IS_OK(werr)) {
659 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
664 *num_shares = added_count;
665 if (added_count > 0) {
666 *share_names = talloc_move(mem_ctx, &tmp_share_names);
672 TALLOC_FREE(tmp_ctx);
677 * check if a share/service of a given name exists
679 bool libnet_conf_share_exists(struct libnet_conf_ctx *ctx,
680 const char *servicename)
683 WERROR werr = WERR_OK;
684 TALLOC_CTX *mem_ctx = talloc_stackframe();
685 struct registry_key *key = NULL;
687 werr = libnet_conf_reg_open_service_key(mem_ctx, ctx, servicename,
689 if (W_ERROR_IS_OK(werr)) {
693 TALLOC_FREE(mem_ctx);
698 * Add a service if it does not already exist.
700 WERROR libnet_conf_create_share(struct libnet_conf_ctx *ctx,
701 const char *servicename)
704 TALLOC_CTX *mem_ctx = talloc_stackframe();
705 struct registry_key *key = NULL;
707 if (libnet_conf_share_exists(ctx, servicename)) {
708 werr = WERR_ALREADY_EXISTS;
712 werr = libnet_conf_reg_create_service_key(mem_ctx, ctx, servicename,
716 TALLOC_FREE(mem_ctx);
721 * get a definition of a share (service) from configuration.
723 WERROR libnet_conf_get_share(TALLOC_CTX *mem_ctx, struct libnet_conf_ctx *ctx,
724 const char *servicename, uint32_t *num_params,
725 char ***param_names, char ***param_values)
727 WERROR werr = WERR_OK;
728 struct registry_key *key = NULL;
730 werr = libnet_conf_reg_open_service_key(mem_ctx, ctx, servicename,
732 if (!W_ERROR_IS_OK(werr)) {
736 werr = libnet_conf_reg_get_values(mem_ctx, key, num_params,
737 param_names, param_values);
745 * delete a service from configuration
747 WERROR libnet_conf_delete_share(struct libnet_conf_ctx *ctx,
748 const char *servicename)
750 WERROR werr = WERR_OK;
751 struct registry_key *key = NULL;
752 TALLOC_CTX *mem_ctx = talloc_stackframe();
754 werr = libnet_conf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE, &key);
755 if (!W_ERROR_IS_OK(werr)) {
759 werr = reg_deletekey_recursive(key, key, servicename);
762 TALLOC_FREE(mem_ctx);
767 * set a configuration parameter to the value provided.
769 WERROR libnet_conf_set_parameter(struct libnet_conf_ctx *ctx,
775 struct registry_key *key = NULL;
776 TALLOC_CTX *mem_ctx = talloc_stackframe();
778 if (!libnet_conf_share_exists(ctx, service)) {
779 werr = WERR_NO_SUCH_SERVICE;
783 werr = libnet_conf_reg_open_service_key(mem_ctx, ctx, service,
784 REG_KEY_WRITE, &key);
785 if (!W_ERROR_IS_OK(werr)) {
789 werr = libnet_conf_reg_set_value(key, param, valstr);
792 TALLOC_FREE(mem_ctx);
797 * Set a global parameter
798 * (i.e. a parameter in the [global] service).
800 * This also creates [global] when it does not exist.
802 WERROR libnet_conf_set_global_parameter(struct libnet_conf_ctx *ctx,
803 const char *param, const char *val)
807 if (!libnet_conf_share_exists(ctx, GLOBAL_NAME)) {
808 werr = libnet_conf_create_share(ctx, GLOBAL_NAME);
809 if (!W_ERROR_IS_OK(werr)) {
813 werr = libnet_conf_set_parameter(ctx, GLOBAL_NAME, param, val);
820 * get the value of a configuration parameter as a string
822 WERROR libnet_conf_get_parameter(TALLOC_CTX *mem_ctx,
823 struct libnet_conf_ctx *ctx,
828 WERROR werr = WERR_OK;
829 struct registry_key *key = NULL;
830 struct registry_value *value = NULL;
832 if (valstr == NULL) {
833 werr = WERR_INVALID_PARAM;
837 if (!libnet_conf_share_exists(ctx, service)) {
838 werr = WERR_NO_SUCH_SERVICE;
842 werr = libnet_conf_reg_open_service_key(mem_ctx, ctx, service,
844 if (!W_ERROR_IS_OK(werr)) {
848 if (!libnet_conf_value_exists(key, param)) {
849 werr = WERR_INVALID_PARAM;
853 werr = reg_queryvalue(mem_ctx, key, param, &value);
854 if (!W_ERROR_IS_OK(werr)) {
858 *valstr = libnet_conf_format_registry_value(mem_ctx, value);
860 if (*valstr == NULL) {
871 * Get the value of a global parameter.
873 * Create [global] if it does not exist.
875 WERROR libnet_conf_get_global_parameter(TALLOC_CTX *mem_ctx,
876 struct libnet_conf_ctx *ctx,
882 if (!libnet_conf_share_exists(ctx, GLOBAL_NAME)) {
883 werr = libnet_conf_create_share(ctx, GLOBAL_NAME);
884 if (!W_ERROR_IS_OK(werr)) {
888 werr = libnet_conf_get_parameter(mem_ctx, ctx, GLOBAL_NAME, param,
896 * delete a parameter from configuration
898 WERROR libnet_conf_delete_parameter(struct libnet_conf_ctx *ctx,
899 const char *service, const char *param)
901 struct registry_key *key = NULL;
902 WERROR werr = WERR_OK;
903 TALLOC_CTX *mem_ctx = talloc_stackframe();
905 if (!libnet_conf_share_exists(ctx, service)) {
906 return WERR_NO_SUCH_SERVICE;
909 werr = libnet_conf_reg_open_service_key(mem_ctx, ctx, service,
912 if (!W_ERROR_IS_OK(werr)) {
916 if (!libnet_conf_value_exists(key, param)) {
917 werr = WERR_INVALID_PARAM;
921 werr = reg_deletevalue(key, param);
924 TALLOC_FREE(mem_ctx);
929 * Delete a global parameter.
931 * Create [global] if it does not exist.
933 WERROR libnet_conf_delete_global_parameter(struct libnet_conf_ctx *ctx,
938 if (!libnet_conf_share_exists(ctx, GLOBAL_NAME)) {
939 werr = libnet_conf_create_share(ctx, GLOBAL_NAME);
940 if (!W_ERROR_IS_OK(werr)) {
944 werr = libnet_conf_delete_parameter(ctx, GLOBAL_NAME, param);