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/>.
24 * Open a subkey of KEY_SMBCONF (i.e a service)
25 * - variant without error output (q = quiet)-
27 static WERROR libnet_smbconf_open_path_q(TALLOC_CTX *ctx,
28 const char *subkeyname,
29 uint32 desired_access,
30 struct registry_key **key)
32 WERROR werr = WERR_OK;
36 if (!(token = registry_create_admin_token(ctx))) {
37 DEBUG(1, ("Error creating admin token\n"));
41 if (subkeyname == NULL) {
42 path = talloc_strdup(ctx, KEY_SMBCONF);
44 path = talloc_asprintf(ctx, "%s\\%s", KEY_SMBCONF, subkeyname);
47 werr = reg_open_path(ctx, path, desired_access,
56 * check if a subkey of KEY_SMBCONF of a given name exists
58 bool libnet_smbconf_key_exists(TALLOC_CTX *ctx, const char *subkeyname)
61 WERROR werr = WERR_OK;
63 struct registry_key *key;
65 if (!(mem_ctx = talloc_new(ctx))) {
66 d_fprintf(stderr, "ERROR: Out of memory...!\n");
70 werr = libnet_smbconf_open_path_q(mem_ctx, subkeyname, REG_KEY_READ, &key);
71 if (W_ERROR_IS_OK(werr)) {
81 * Open a subkey of KEY_SMBCONF (i.e a service)
82 * - variant with error output -
84 WERROR libnet_smbconf_open_path(TALLOC_CTX *ctx, const char *subkeyname,
85 uint32 desired_access,
86 struct registry_key **key)
88 WERROR werr = WERR_OK;
90 werr = libnet_smbconf_open_path_q(ctx, subkeyname, desired_access, key);
91 if (!W_ERROR_IS_OK(werr)) {
92 d_fprintf(stderr, "Error opening registry path '%s\\%s': %s\n",
94 (subkeyname == NULL) ? "" : subkeyname,
102 * open the base key KEY_SMBCONF
104 WERROR libnet_smbconf_open_basepath(TALLOC_CTX *ctx, uint32 desired_access,
105 struct registry_key **key)
107 return libnet_smbconf_open_path(ctx, NULL, desired_access, key);
111 * create a subkey of KEY_SMBCONF
113 WERROR libnet_reg_createkey_internal(TALLOC_CTX *ctx,
114 const char * subkeyname,
115 struct registry_key **newkey)
117 WERROR werr = WERR_OK;
118 struct registry_key *create_parent = NULL;
119 TALLOC_CTX *create_ctx;
120 enum winreg_CreateAction action = REG_ACTION_NONE;
122 /* create a new talloc ctx for creation. it will hold
123 * the intermediate parent key (SMBCONF) for creation
124 * and will be destroyed when leaving this function... */
125 if (!(create_ctx = talloc_new(ctx))) {
130 werr = libnet_smbconf_open_basepath(create_ctx, REG_KEY_WRITE, &create_parent);
131 if (!W_ERROR_IS_OK(werr)) {
135 werr = reg_createkey(ctx, create_parent, subkeyname,
136 REG_KEY_WRITE, newkey, &action);
137 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
138 d_fprintf(stderr, "Key '%s' already exists.\n", subkeyname);
139 werr = WERR_ALREADY_EXISTS;
141 if (!W_ERROR_IS_OK(werr)) {
142 d_fprintf(stderr, "Error creating key %s: %s\n",
143 subkeyname, dos_errstr(werr));
147 TALLOC_FREE(create_ctx);
153 * add a value to a key.
155 WERROR libnet_smbconf_reg_setvalue_internal(struct registry_key *key,
159 struct registry_value val;
160 WERROR werr = WERR_OK;
162 const char *canon_valname;
163 const char *canon_valstr;
165 if (!lp_canonicalize_parameter_with_value(valname, valstr,
169 if (canon_valname == NULL) {
170 d_fprintf(stderr, "invalid parameter '%s' given\n",
173 d_fprintf(stderr, "invalid value '%s' given for "
174 "parameter '%s'\n", valstr, valname);
176 werr = WERR_INVALID_PARAM;
183 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
184 val.v.sz.len = strlen(canon_valstr) + 1;
186 if (registry_smbconf_valname_forbidden(canon_valname)) {
187 d_fprintf(stderr, "Parameter '%s' not allowed in registry.\n",
189 werr = WERR_INVALID_PARAM;
193 subkeyname = strrchr_m(key->key->name, '\\');
194 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
195 d_fprintf(stderr, "Invalid registry key '%s' given as "
196 "smbconf section.\n", key->key->name);
197 werr = WERR_INVALID_PARAM;
201 if (!strequal(subkeyname, GLOBAL_NAME) &&
202 lp_parameter_is_global(valname))
204 d_fprintf(stderr, "Global paramter '%s' not allowed in "
205 "service definition ('%s').\n", canon_valname,
207 werr = WERR_INVALID_PARAM;
211 werr = reg_setvalue(key, canon_valname, &val);
212 if (!W_ERROR_IS_OK(werr)) {
214 "Error adding value '%s' to "
216 canon_valname, key->key->name, dos_errstr(werr));
223 static WERROR do_modify_val_config(struct registry_key *key,
224 const char *val_name,
225 const char *val_data)
227 struct registry_value val;
232 val.v.sz.str = CONST_DISCARD(char *, val_data);
233 val.v.sz.len = strlen(val_data) + 1;
235 return reg_setvalue(key, val_name, &val);
238 WERROR libnet_smbconf_set_global_param(TALLOC_CTX *mem_ctx,
243 struct registry_key *key = NULL;
245 if (!lp_include_registry_globals()) {
246 return WERR_NOT_SUPPORTED;
249 if (!registry_init_regdb()) {
250 return WERR_REG_IO_FAILURE;
253 if (!libnet_smbconf_key_exists(mem_ctx, GLOBAL_NAME)) {
254 werr = libnet_reg_createkey_internal(mem_ctx,
257 werr = libnet_smbconf_open_path(mem_ctx,
259 REG_KEY_WRITE, &key);
262 if (!W_ERROR_IS_OK(werr)) {
266 return do_modify_val_config(key, param, val);
269 static bool libnet_smbconf_value_exists(TALLOC_CTX *ctx,
270 struct registry_key *key,
274 WERROR werr = WERR_OK;
275 struct registry_value *value = NULL;
277 werr = reg_queryvalue(ctx, key, param, &value);
278 if (W_ERROR_IS_OK(werr)) {
286 WERROR libnet_smbconf_delparm(TALLOC_CTX *mem_ctx,
290 struct registry_key *key = NULL;
291 WERROR werr = WERR_OK;
293 if (!libnet_smbconf_key_exists(mem_ctx, service)) {
294 return WERR_NO_SUCH_SERVICE;
297 werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_READ, &key);
298 W_ERROR_NOT_OK_RETURN(werr);
300 if (!libnet_smbconf_value_exists(mem_ctx, key, param)) {
301 return WERR_INVALID_PARAM;
304 werr = reg_deletevalue(key, param);
305 W_ERROR_NOT_OK_RETURN(werr);