2 * Unix SMB/CIFS implementation.
3 * libnet smbconf registry Support
4 * Copyright (C) Michael Adam 2007
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_smbconf_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);
58 * Open a subkey of KEY_SMBCONF (i.e a service)
60 static WERROR libnet_smbconf_reg_open_path(TALLOC_CTX *ctx,
61 const char *subkeyname,
62 uint32 desired_access,
63 struct registry_key **key)
65 WERROR werr = WERR_OK;
69 if (!(token = registry_create_admin_token(ctx))) {
70 DEBUG(1, ("Error creating admin token\n"));
74 if (subkeyname == NULL) {
75 path = talloc_strdup(ctx, KEY_SMBCONF);
77 path = talloc_asprintf(ctx, "%s\\%s", KEY_SMBCONF, subkeyname);
80 werr = reg_open_path(ctx, path, desired_access,
83 if (!W_ERROR_IS_OK(werr)) {
84 DEBUG(1, ("Error opening registry path '%s': %s\n",
85 path, dos_errstr(werr)));
94 * open the base key KEY_SMBCONF
96 static WERROR libnet_smbconf_reg_open_basepath(TALLOC_CTX *ctx,
97 uint32 desired_access,
98 struct registry_key **key)
100 return libnet_smbconf_reg_open_path(ctx, NULL, desired_access, key);
103 static bool libnet_smbconf_value_exists(struct registry_key *key,
107 WERROR werr = WERR_OK;
108 TALLOC_CTX *ctx = talloc_stackframe();
109 struct registry_value *value = NULL;
111 werr = reg_queryvalue(ctx, key, param, &value);
112 if (W_ERROR_IS_OK(werr)) {
121 * create a subkey of KEY_SMBCONF
123 WERROR libnet_smbconf_reg_createkey_internal(TALLOC_CTX *ctx,
124 const char * subkeyname,
125 struct registry_key **newkey)
127 WERROR werr = WERR_OK;
128 struct registry_key *create_parent = NULL;
129 TALLOC_CTX *create_ctx;
130 enum winreg_CreateAction action = REG_ACTION_NONE;
132 /* create a new talloc ctx for creation. it will hold
133 * the intermediate parent key (SMBCONF) for creation
134 * and will be destroyed when leaving this function... */
135 if (!(create_ctx = talloc_new(ctx))) {
140 werr = libnet_smbconf_reg_open_basepath(create_ctx, REG_KEY_WRITE,
142 if (!W_ERROR_IS_OK(werr)) {
146 werr = reg_createkey(ctx, create_parent, subkeyname,
147 REG_KEY_WRITE, newkey, &action);
148 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
149 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
150 werr = WERR_ALREADY_EXISTS;
152 if (!W_ERROR_IS_OK(werr)) {
153 DEBUG(5, ("Error creating key %s: %s\n",
154 subkeyname, dos_errstr(werr)));
158 TALLOC_FREE(create_ctx);
163 * add a value to a key.
165 WERROR libnet_smbconf_reg_setvalue_internal(struct registry_key *key,
169 struct registry_value val;
170 WERROR werr = WERR_OK;
172 const char *canon_valname;
173 const char *canon_valstr;
175 if (!lp_canonicalize_parameter_with_value(valname, valstr,
179 if (canon_valname == NULL) {
180 DEBUG(5, ("invalid parameter '%s' given\n",
183 DEBUG(5, ("invalid value '%s' given for "
184 "parameter '%s'\n", valstr, valname));
186 werr = WERR_INVALID_PARAM;
193 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
194 val.v.sz.len = strlen(canon_valstr) + 1;
196 if (registry_smbconf_valname_forbidden(canon_valname)) {
197 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
199 werr = WERR_INVALID_PARAM;
203 subkeyname = strrchr_m(key->key->name, '\\');
204 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
205 DEBUG(5, ("Invalid registry key '%s' given as "
206 "smbconf section.\n", key->key->name));
207 werr = WERR_INVALID_PARAM;
211 if (!strequal(subkeyname, GLOBAL_NAME) &&
212 lp_parameter_is_global(valname))
214 DEBUG(5, ("Global paramter '%s' not allowed in "
215 "service definition ('%s').\n", canon_valname,
217 werr = WERR_INVALID_PARAM;
221 werr = reg_setvalue(key, canon_valname, &val);
222 if (!W_ERROR_IS_OK(werr)) {
223 DEBUG(5, ("Error adding value '%s' to "
225 canon_valname, key->key->name, dos_errstr(werr)));
233 * format a registry_value into a string.
235 * This is intended to be used for smbconf registry values,
236 * which are ar stored as REG_SZ values, so the incomplete
237 * handling should be ok.
239 static char *libnet_smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
240 struct registry_value *value)
244 /* alternatively, create a new talloc context? */
245 if (mem_ctx == NULL) {
249 switch (value->type) {
251 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
255 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
259 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
260 result = talloc_asprintf(mem_ctx, "\"%s\" ",
261 value->v.multi_sz.strings[j]);
266 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
267 (int)value->v.binary.length);
270 result = talloc_asprintf(mem_ctx, "<unprintable>");
277 * Get the values of a key as a list of value names
278 * and a list of value strings (ordered)
280 static WERROR libnet_smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
281 struct registry_key *key,
282 uint32_t *num_values,
284 char ***value_strings)
286 TALLOC_CTX *tmp_ctx = NULL;
287 WERROR werr = WERR_OK;
289 struct registry_value *valvalue = NULL;
290 char *valname = NULL;
291 char **tmp_valnames = NULL;
292 char **tmp_valstrings = NULL;
294 if ((num_values == NULL) || (value_names == NULL) ||
295 (value_strings == NULL))
297 werr = WERR_INVALID_PARAM;
301 tmp_ctx = talloc_new(mem_ctx);
302 if (tmp_ctx == NULL) {
308 W_ERROR_IS_OK(werr = reg_enumvalue(tmp_ctx, key, count, &valname,
314 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
317 if (!W_ERROR_IS_OK(werr)) {
321 valstring = libnet_smbconf_format_registry_value(tmp_ctx,
323 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
327 if (!W_ERROR_IS_OK(werr)) {
331 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
339 *value_names = talloc_move(mem_ctx, &tmp_valnames);
340 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
343 *value_strings = NULL;
347 TALLOC_FREE(tmp_ctx);
351 /**********************************************************************
353 * The actual net conf api functions, that are exported.
355 **********************************************************************/
358 * Drop the whole configuration (restarting empty).
360 WERROR libnet_smbconf_drop(void)
363 WERROR werr = WERR_OK;
364 NT_USER_TOKEN *token;
365 struct registry_key *parent_key = NULL;
366 struct registry_key *new_key = NULL;
367 TALLOC_CTX* mem_ctx = talloc_stackframe();
368 enum winreg_CreateAction action;
370 if (!(token = registry_create_admin_token(mem_ctx))) {
371 /* what is the appropriate error code here? */
372 werr = WERR_CAN_NOT_COMPLETE;
376 path = talloc_strdup(mem_ctx, KEY_SMBCONF);
381 p = strrchr(path, '\\');
383 werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token, &parent_key);
385 if (!W_ERROR_IS_OK(werr)) {
389 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
391 if (!W_ERROR_IS_OK(werr)) {
395 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
399 TALLOC_FREE(mem_ctx);
404 * Get the whole configuration as lists of strings with counts:
406 * num_shares : number of shares
407 * share_names : list of length num_shares of share names
408 * num_params : list of length num_shares of parameter counts for each share
409 * param_names : list of lists of parameter names for each share
410 * param_values : list of lists of parameter values for each share
412 WERROR libnet_smbconf_get_config(TALLOC_CTX *mem_ctx, uint32_t *num_shares,
413 char ***share_names, uint32_t **num_params,
414 char ****param_names, char ****param_values)
416 WERROR werr = WERR_OK;
417 TALLOC_CTX *tmp_ctx = NULL;
418 uint32_t tmp_num_shares;
419 char **tmp_share_names;
420 uint32_t *tmp_num_params;
421 char ***tmp_param_names;
422 char ***tmp_param_values;
425 if ((num_shares == NULL) || (share_names == NULL) ||
426 (num_params == NULL) || (param_names == NULL) ||
427 (param_values == NULL))
429 werr = WERR_INVALID_PARAM;
433 tmp_ctx = talloc_new(mem_ctx);
434 if (tmp_ctx == NULL) {
439 werr = libnet_smbconf_get_share_names(tmp_ctx, &tmp_num_shares,
441 if (!W_ERROR_IS_OK(werr)) {
445 tmp_num_params = TALLOC_ARRAY(tmp_ctx, uint32_t, tmp_num_shares);
446 tmp_param_names = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
447 tmp_param_values = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
449 if ((tmp_num_params == NULL) || (tmp_param_names == NULL) ||
450 (tmp_param_values == NULL))
456 for (count = 0; count < tmp_num_shares; count++) {
457 werr = libnet_smbconf_getshare(mem_ctx, tmp_share_names[count],
458 &tmp_num_params[count],
459 &tmp_param_names[count],
460 &tmp_param_values[count]);
461 if (!W_ERROR_IS_OK(werr)) {
468 *num_shares = tmp_num_shares;
469 if (tmp_num_shares > 0) {
470 *share_names = talloc_move(mem_ctx, &tmp_share_names);
471 *num_params = talloc_move(mem_ctx, &tmp_num_params);
472 *param_names = talloc_move(mem_ctx, &tmp_param_names);
473 *param_values = talloc_move(mem_ctx, &tmp_param_values);
478 *param_values = NULL;
482 TALLOC_FREE(tmp_ctx);
488 * get the list of share names defined in the configuration.
490 WERROR libnet_smbconf_get_share_names(TALLOC_CTX *mem_ctx, uint32_t *num_shares,
494 uint32_t added_count = 0;
495 TALLOC_CTX *tmp_ctx = NULL;
496 WERROR werr = WERR_OK;
497 struct registry_key *key = NULL;
498 char *subkey_name = NULL;
499 char **tmp_share_names = NULL;
501 if ((num_shares == NULL) || (share_names == NULL)) {
502 werr = WERR_INVALID_PARAM;
506 tmp_ctx = talloc_new(mem_ctx);
507 if (tmp_ctx == NULL) {
512 /* make sure "global" is always listed first */
513 if (libnet_smbconf_share_exists(GLOBAL_NAME)) {
514 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
517 if (!W_ERROR_IS_OK(werr)) {
523 werr = libnet_smbconf_reg_open_basepath(tmp_ctx,
524 SEC_RIGHTS_ENUM_SUBKEYS,
526 if (!W_ERROR_IS_OK(werr)) {
531 W_ERROR_IS_OK(werr = reg_enumkey(tmp_ctx, key, count,
532 &subkey_name, NULL));
535 if (strequal(subkey_name, GLOBAL_NAME)) {
539 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
543 if (!W_ERROR_IS_OK(werr)) {
548 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
553 *num_shares = added_count;
554 if (added_count > 0) {
555 *share_names = talloc_move(mem_ctx, &tmp_share_names);
561 TALLOC_FREE(tmp_ctx);
566 * check if a share/service of a given name exists
568 bool libnet_smbconf_share_exists(const char *subkeyname)
571 WERROR werr = WERR_OK;
572 TALLOC_CTX *mem_ctx = talloc_stackframe();
573 struct registry_key *key = NULL;
575 werr = libnet_smbconf_reg_open_path(mem_ctx, subkeyname, REG_KEY_READ,
577 if (W_ERROR_IS_OK(werr)) {
581 TALLOC_FREE(mem_ctx);
586 * get a definition of a share (service) from configuration.
588 WERROR libnet_smbconf_getshare(TALLOC_CTX *mem_ctx, const char *servicename,
589 uint32_t *num_params, char ***param_names,
590 char ***param_values)
592 WERROR werr = WERR_OK;
593 struct registry_key *key = NULL;
595 werr = libnet_smbconf_reg_open_path(mem_ctx, servicename, REG_KEY_READ,
597 if (!W_ERROR_IS_OK(werr)) {
601 werr = libnet_smbconf_reg_get_values(mem_ctx, key, num_params,
602 param_names, param_values);
610 * delete a service from configuration
612 WERROR libnet_smbconf_delshare(const char *servicename)
614 WERROR werr = WERR_OK;
615 struct registry_key *key = NULL;
616 TALLOC_CTX *ctx = talloc_stackframe();
618 werr = libnet_smbconf_reg_open_basepath(ctx, REG_KEY_WRITE, &key);
619 if (!W_ERROR_IS_OK(werr)) {
623 werr = reg_deletekey_recursive(key, key, servicename);
631 * set a configuration parameter to the value provided.
633 WERROR libnet_smbconf_setparm(const char *service,
638 struct registry_key *key = NULL;
639 TALLOC_CTX *mem_ctx = talloc_stackframe();
641 if (!libnet_smbconf_share_exists(service)) {
642 werr = libnet_smbconf_reg_createkey_internal(mem_ctx, service,
645 werr = libnet_smbconf_reg_open_path(mem_ctx, service,
646 REG_KEY_WRITE, &key);
648 if (!W_ERROR_IS_OK(werr)) {
652 werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
655 TALLOC_FREE(mem_ctx);
660 * get the value of a configuration parameter as a string
662 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
667 WERROR werr = WERR_OK;
668 struct registry_key *key = NULL;
669 struct registry_value *value = NULL;
671 if (valstr == NULL) {
672 werr = WERR_INVALID_PARAM;
676 if (!libnet_smbconf_share_exists(service)) {
677 werr = WERR_NO_SUCH_SERVICE;
681 werr = libnet_smbconf_reg_open_path(mem_ctx, service, REG_KEY_READ,
683 if (!W_ERROR_IS_OK(werr)) {
687 if (!libnet_smbconf_value_exists(key, param)) {
688 werr = WERR_INVALID_PARAM;
692 werr = reg_queryvalue(mem_ctx, key, param, &value);
693 if (!W_ERROR_IS_OK(werr)) {
697 *valstr = libnet_smbconf_format_registry_value(mem_ctx, value);
699 if (*valstr == NULL) {
710 * delete a parameter from configuration
712 WERROR libnet_smbconf_delparm(const char *service,
715 struct registry_key *key = NULL;
716 WERROR werr = WERR_OK;
717 TALLOC_CTX *mem_ctx = talloc_stackframe();
719 if (!libnet_smbconf_share_exists(service)) {
720 return WERR_NO_SUCH_SERVICE;
723 werr = libnet_smbconf_reg_open_path(mem_ctx, service, REG_KEY_ALL, &key);
724 if (!W_ERROR_IS_OK(werr)) {
728 if (!libnet_smbconf_value_exists(key, param)) {
729 werr = WERR_INVALID_PARAM;
733 werr = reg_deletevalue(key, param);
736 TALLOC_FREE(mem_ctx);
741 /**********************************************************************
743 * Convenience functions that are also exported.
745 **********************************************************************/
747 WERROR libnet_smbconf_set_global_param(const char *param,
750 return libnet_smbconf_setparm(GLOBAL_NAME, param, val);