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"
25 * yuck - static variable to keep track of the registry initialization.
27 static bool registry_initialized = false;
29 /**********************************************************************
31 * Helper functions (mostly registry related)
32 * TODO: These should be eventually static.
34 **********************************************************************/
37 * add a string to a talloced array of strings.
39 static WERROR libnet_conf_add_string_to_array(TALLOC_CTX *mem_ctx,
44 char **new_array = NULL;
46 if ((array == NULL) || (string == NULL)) {
47 return WERR_INVALID_PARAM;
50 new_array = TALLOC_REALLOC_ARRAY(mem_ctx, *array, char *, count + 1);
51 if (new_array == NULL) {
55 new_array[count] = talloc_strdup(new_array, string);
62 static WERROR libnet_conf_reg_initialize(void)
64 WERROR werr = WERR_OK;
66 if (registry_initialized) {
70 if (!registry_init_regdb()) {
71 werr = WERR_REG_IO_FAILURE;
75 registry_initialized = true;
82 * Open a registry key specified by "path"
84 static WERROR libnet_conf_reg_open_path(TALLOC_CTX *mem_ctx,
86 uint32 desired_access,
87 struct registry_key **key)
89 WERROR werr = WERR_OK;
90 NT_USER_TOKEN *token = NULL;
91 TALLOC_CTX *tmp_ctx = NULL;
94 DEBUG(1, ("Error: NULL path string given\n"));
95 werr = WERR_INVALID_PARAM;
99 tmp_ctx = talloc_new(mem_ctx);
100 if (tmp_ctx == NULL) {
105 werr = libnet_conf_reg_initialize();
106 if (!W_ERROR_IS_OK(werr)) {
107 DEBUG(1, ("Error initializing registry: %s\n",
112 werr = ntstatus_to_werror(registry_create_admin_token(tmp_ctx, &token));
113 if (W_ERROR_IS_OK(werr)) {
114 DEBUG(1, ("Error creating admin token\n"));
118 werr = reg_open_path(mem_ctx, path, desired_access, token, key);
120 if (!W_ERROR_IS_OK(werr)) {
121 DEBUG(1, ("Error opening registry path '%s': %s\n",
122 path, dos_errstr(werr)));
126 TALLOC_FREE(tmp_ctx);
131 * Open a subkey of KEY_SMBCONF (i.e a service)
133 static WERROR libnet_conf_reg_open_service_key(TALLOC_CTX *ctx,
134 const char *servicename,
135 uint32 desired_access,
136 struct registry_key **key)
138 WERROR werr = WERR_OK;
141 if (servicename == NULL) {
142 DEBUG(3, ("Error: NULL servicename given.\n"));
143 werr = WERR_INVALID_PARAM;
147 path = talloc_asprintf(ctx, "%s\\%s", KEY_SMBCONF, servicename);
149 werr = libnet_conf_reg_open_path(ctx, path, desired_access, key);
157 * open the base key KEY_SMBCONF
159 static WERROR libnet_conf_reg_open_base_key(TALLOC_CTX *ctx,
160 uint32 desired_access,
161 struct registry_key **key)
163 return libnet_conf_reg_open_path(ctx, KEY_SMBCONF, desired_access, key);
167 * check if a value exists in a given registry key
169 static bool libnet_conf_value_exists(struct registry_key *key,
173 WERROR werr = WERR_OK;
174 TALLOC_CTX *ctx = talloc_stackframe();
175 struct registry_value *value = NULL;
177 werr = reg_queryvalue(ctx, key, param, &value);
178 if (W_ERROR_IS_OK(werr)) {
187 * create a subkey of KEY_SMBCONF
189 static WERROR libnet_conf_reg_create_service_key(TALLOC_CTX *ctx,
190 const char * subkeyname,
191 struct registry_key **newkey)
193 WERROR werr = WERR_OK;
194 struct registry_key *create_parent = NULL;
195 TALLOC_CTX *create_ctx;
196 enum winreg_CreateAction action = REG_ACTION_NONE;
198 /* create a new talloc ctx for creation. it will hold
199 * the intermediate parent key (SMBCONF) for creation
200 * and will be destroyed when leaving this function... */
201 if (!(create_ctx = talloc_new(ctx))) {
206 werr = libnet_conf_reg_open_base_key(create_ctx, REG_KEY_WRITE,
208 if (!W_ERROR_IS_OK(werr)) {
212 werr = reg_createkey(ctx, create_parent, subkeyname,
213 REG_KEY_WRITE, newkey, &action);
214 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
215 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
216 werr = WERR_ALREADY_EXISTS;
218 if (!W_ERROR_IS_OK(werr)) {
219 DEBUG(5, ("Error creating key %s: %s\n",
220 subkeyname, dos_errstr(werr)));
224 TALLOC_FREE(create_ctx);
229 * add a value to a key.
231 static WERROR libnet_conf_reg_set_value(struct registry_key *key,
235 struct registry_value val;
236 WERROR werr = WERR_OK;
238 const char *canon_valname;
239 const char *canon_valstr;
241 if (!lp_canonicalize_parameter_with_value(valname, valstr,
245 if (canon_valname == NULL) {
246 DEBUG(5, ("invalid parameter '%s' given\n",
249 DEBUG(5, ("invalid value '%s' given for "
250 "parameter '%s'\n", valstr, valname));
252 werr = WERR_INVALID_PARAM;
259 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
260 val.v.sz.len = strlen(canon_valstr) + 1;
262 if (registry_smbconf_valname_forbidden(canon_valname)) {
263 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
265 werr = WERR_INVALID_PARAM;
269 subkeyname = strrchr_m(key->key->name, '\\');
270 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
271 DEBUG(5, ("Invalid registry key '%s' given as "
272 "smbconf section.\n", key->key->name));
273 werr = WERR_INVALID_PARAM;
277 if (!strequal(subkeyname, GLOBAL_NAME) &&
278 lp_parameter_is_global(valname))
280 DEBUG(5, ("Global paramter '%s' not allowed in "
281 "service definition ('%s').\n", canon_valname,
283 werr = WERR_INVALID_PARAM;
287 werr = reg_setvalue(key, canon_valname, &val);
288 if (!W_ERROR_IS_OK(werr)) {
289 DEBUG(5, ("Error adding value '%s' to "
291 canon_valname, key->key->name, dos_errstr(werr)));
299 * format a registry_value into a string.
301 * This is intended to be used for smbconf registry values,
302 * which are ar stored as REG_SZ values, so the incomplete
303 * handling should be ok.
305 static char *libnet_conf_format_registry_value(TALLOC_CTX *mem_ctx,
306 struct registry_value *value)
310 /* alternatively, create a new talloc context? */
311 if (mem_ctx == NULL) {
315 switch (value->type) {
317 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
321 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
325 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
326 result = talloc_asprintf(mem_ctx, "\"%s\" ",
327 value->v.multi_sz.strings[j]);
332 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
333 (int)value->v.binary.length);
336 result = talloc_asprintf(mem_ctx, "<unprintable>");
343 * Get the values of a key as a list of value names
344 * and a list of value strings (ordered)
346 static WERROR libnet_conf_reg_get_values(TALLOC_CTX *mem_ctx,
347 struct registry_key *key,
348 uint32_t *num_values,
350 char ***value_strings)
352 TALLOC_CTX *tmp_ctx = NULL;
353 WERROR werr = WERR_OK;
355 struct registry_value *valvalue = NULL;
356 char *valname = NULL;
357 char **tmp_valnames = NULL;
358 char **tmp_valstrings = NULL;
360 if ((num_values == NULL) || (value_names == NULL) ||
361 (value_strings == NULL))
363 werr = WERR_INVALID_PARAM;
367 tmp_ctx = talloc_new(mem_ctx);
368 if (tmp_ctx == NULL) {
374 W_ERROR_IS_OK(werr = reg_enumvalue(tmp_ctx, key, count, &valname,
380 werr = libnet_conf_add_string_to_array(tmp_ctx,
383 if (!W_ERROR_IS_OK(werr)) {
387 valstring = libnet_conf_format_registry_value(tmp_ctx,
389 werr = libnet_conf_add_string_to_array(tmp_ctx,
393 if (!W_ERROR_IS_OK(werr)) {
397 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
405 *value_names = talloc_move(mem_ctx, &tmp_valnames);
406 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
409 *value_strings = NULL;
413 TALLOC_FREE(tmp_ctx);
417 /**********************************************************************
419 * The actual net conf api functions, that are exported.
421 **********************************************************************/
424 * Drop the whole configuration (restarting empty).
426 WERROR libnet_conf_drop(void)
429 WERROR werr = WERR_OK;
430 struct registry_key *parent_key = NULL;
431 struct registry_key *new_key = NULL;
432 TALLOC_CTX* mem_ctx = talloc_stackframe();
433 enum winreg_CreateAction action;
435 path = talloc_strdup(mem_ctx, KEY_SMBCONF);
440 p = strrchr(path, '\\');
442 werr = libnet_conf_reg_open_path(mem_ctx, path, REG_KEY_WRITE,
445 if (!W_ERROR_IS_OK(werr)) {
449 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
451 if (!W_ERROR_IS_OK(werr)) {
455 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
459 TALLOC_FREE(mem_ctx);
464 * Get the whole configuration as lists of strings with counts:
466 * num_shares : number of shares
467 * share_names : list of length num_shares of share names
468 * num_params : list of length num_shares of parameter counts for each share
469 * param_names : list of lists of parameter names for each share
470 * param_values : list of lists of parameter values for each share
472 WERROR libnet_conf_get_config(TALLOC_CTX *mem_ctx, uint32_t *num_shares,
473 char ***share_names, uint32_t **num_params,
474 char ****param_names, char ****param_values)
476 WERROR werr = WERR_OK;
477 TALLOC_CTX *tmp_ctx = NULL;
478 uint32_t tmp_num_shares;
479 char **tmp_share_names;
480 uint32_t *tmp_num_params;
481 char ***tmp_param_names;
482 char ***tmp_param_values;
485 if ((num_shares == NULL) || (share_names == NULL) ||
486 (num_params == NULL) || (param_names == NULL) ||
487 (param_values == NULL))
489 werr = WERR_INVALID_PARAM;
493 tmp_ctx = talloc_new(mem_ctx);
494 if (tmp_ctx == NULL) {
499 werr = libnet_conf_get_share_names(tmp_ctx, &tmp_num_shares,
501 if (!W_ERROR_IS_OK(werr)) {
505 tmp_num_params = TALLOC_ARRAY(tmp_ctx, uint32_t, tmp_num_shares);
506 tmp_param_names = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
507 tmp_param_values = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
509 if ((tmp_num_params == NULL) || (tmp_param_names == NULL) ||
510 (tmp_param_values == NULL))
516 for (count = 0; count < tmp_num_shares; count++) {
517 werr = libnet_conf_get_share(mem_ctx, tmp_share_names[count],
518 &tmp_num_params[count],
519 &tmp_param_names[count],
520 &tmp_param_values[count]);
521 if (!W_ERROR_IS_OK(werr)) {
528 *num_shares = tmp_num_shares;
529 if (tmp_num_shares > 0) {
530 *share_names = talloc_move(mem_ctx, &tmp_share_names);
531 *num_params = talloc_move(mem_ctx, &tmp_num_params);
532 *param_names = talloc_move(mem_ctx, &tmp_param_names);
533 *param_values = talloc_move(mem_ctx, &tmp_param_values);
538 *param_values = NULL;
542 TALLOC_FREE(tmp_ctx);
548 * get the list of share names defined in the configuration.
550 WERROR libnet_conf_get_share_names(TALLOC_CTX *mem_ctx, uint32_t *num_shares,
554 uint32_t added_count = 0;
555 TALLOC_CTX *tmp_ctx = NULL;
556 WERROR werr = WERR_OK;
557 struct registry_key *key = NULL;
558 char *subkey_name = NULL;
559 char **tmp_share_names = NULL;
561 if ((num_shares == NULL) || (share_names == NULL)) {
562 werr = WERR_INVALID_PARAM;
566 tmp_ctx = talloc_new(mem_ctx);
567 if (tmp_ctx == NULL) {
572 /* make sure "global" is always listed first */
573 if (libnet_conf_share_exists(GLOBAL_NAME)) {
574 werr = libnet_conf_add_string_to_array(tmp_ctx,
577 if (!W_ERROR_IS_OK(werr)) {
583 werr = libnet_conf_reg_open_base_key(tmp_ctx, SEC_RIGHTS_ENUM_SUBKEYS,
585 if (!W_ERROR_IS_OK(werr)) {
590 W_ERROR_IS_OK(werr = reg_enumkey(tmp_ctx, key, count,
591 &subkey_name, NULL));
594 if (strequal(subkey_name, GLOBAL_NAME)) {
598 werr = libnet_conf_add_string_to_array(tmp_ctx,
602 if (!W_ERROR_IS_OK(werr)) {
607 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
612 *num_shares = added_count;
613 if (added_count > 0) {
614 *share_names = talloc_move(mem_ctx, &tmp_share_names);
620 TALLOC_FREE(tmp_ctx);
625 * check if a share/service of a given name exists
627 bool libnet_conf_share_exists(const char *servicename)
630 WERROR werr = WERR_OK;
631 TALLOC_CTX *mem_ctx = talloc_stackframe();
632 struct registry_key *key = NULL;
634 werr = libnet_conf_reg_open_service_key(mem_ctx, servicename,
636 if (W_ERROR_IS_OK(werr)) {
640 TALLOC_FREE(mem_ctx);
645 * Add a service if it does not already exist.
647 WERROR libnet_conf_create_share(const char *servicename)
650 TALLOC_CTX *mem_ctx = talloc_stackframe();
651 struct registry_key *key = NULL;
653 if (libnet_conf_share_exists(servicename)) {
654 werr = WERR_ALREADY_EXISTS;
658 werr = libnet_conf_reg_create_service_key(mem_ctx, servicename, &key);
661 TALLOC_FREE(mem_ctx);
666 * get a definition of a share (service) from configuration.
668 WERROR libnet_conf_get_share(TALLOC_CTX *mem_ctx, const char *servicename,
669 uint32_t *num_params, char ***param_names,
670 char ***param_values)
672 WERROR werr = WERR_OK;
673 struct registry_key *key = NULL;
675 werr = libnet_conf_reg_open_service_key(mem_ctx, servicename,
677 if (!W_ERROR_IS_OK(werr)) {
681 werr = libnet_conf_reg_get_values(mem_ctx, key, num_params,
682 param_names, param_values);
690 * delete a service from configuration
692 WERROR libnet_conf_delete_share(const char *servicename)
694 WERROR werr = WERR_OK;
695 struct registry_key *key = NULL;
696 TALLOC_CTX *ctx = talloc_stackframe();
698 werr = libnet_conf_reg_open_base_key(ctx, REG_KEY_WRITE, &key);
699 if (!W_ERROR_IS_OK(werr)) {
703 werr = reg_deletekey_recursive(key, key, servicename);
711 * set a configuration parameter to the value provided.
713 WERROR libnet_conf_set_parameter(const char *service,
718 struct registry_key *key = NULL;
719 TALLOC_CTX *mem_ctx = talloc_stackframe();
721 if (!libnet_conf_share_exists(service)) {
722 werr = WERR_NO_SUCH_SERVICE;
726 werr = libnet_conf_reg_open_service_key(mem_ctx, service, REG_KEY_WRITE,
728 if (!W_ERROR_IS_OK(werr)) {
732 werr = libnet_conf_reg_set_value(key, param, valstr);
735 TALLOC_FREE(mem_ctx);
740 * get the value of a configuration parameter as a string
742 WERROR libnet_conf_get_parameter(TALLOC_CTX *mem_ctx,
747 WERROR werr = WERR_OK;
748 struct registry_key *key = NULL;
749 struct registry_value *value = NULL;
751 if (valstr == NULL) {
752 werr = WERR_INVALID_PARAM;
756 if (!libnet_conf_share_exists(service)) {
757 werr = WERR_NO_SUCH_SERVICE;
761 werr = libnet_conf_reg_open_service_key(mem_ctx, service, REG_KEY_READ,
763 if (!W_ERROR_IS_OK(werr)) {
767 if (!libnet_conf_value_exists(key, param)) {
768 werr = WERR_INVALID_PARAM;
772 werr = reg_queryvalue(mem_ctx, key, param, &value);
773 if (!W_ERROR_IS_OK(werr)) {
777 *valstr = libnet_conf_format_registry_value(mem_ctx, value);
779 if (*valstr == NULL) {
790 * delete a parameter from configuration
792 WERROR libnet_conf_delete_parameter(const char *service, const char *param)
794 struct registry_key *key = NULL;
795 WERROR werr = WERR_OK;
796 TALLOC_CTX *mem_ctx = talloc_stackframe();
798 if (!libnet_conf_share_exists(service)) {
799 return WERR_NO_SUCH_SERVICE;
802 werr = libnet_conf_reg_open_service_key(mem_ctx, service, REG_KEY_ALL,
804 if (!W_ERROR_IS_OK(werr)) {
808 if (!libnet_conf_value_exists(key, param)) {
809 werr = WERR_INVALID_PARAM;
813 werr = reg_deletevalue(key, param);
816 TALLOC_FREE(mem_ctx);
821 /**********************************************************************
823 * Convenience functions that are also exported.
825 **********************************************************************/
827 WERROR libnet_conf_set_global_parameter(const char *param, const char *val)
829 return libnet_conf_set_parameter(GLOBAL_NAME, param, val);