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/>.
23 /**********************************************************************
25 * Helper functions (mostly registry related)
26 * TODO: These should be eventually static.
28 **********************************************************************/
31 * Open a subkey of KEY_SMBCONF (i.e a service)
32 * - variant without error output (q = quiet)-
34 static WERROR libnet_smbconf_open_path_q(TALLOC_CTX *ctx,
35 const char *subkeyname,
36 uint32 desired_access,
37 struct registry_key **key)
39 WERROR werr = WERR_OK;
43 if (!(token = registry_create_admin_token(ctx))) {
44 DEBUG(1, ("Error creating admin token\n"));
48 if (subkeyname == NULL) {
49 path = talloc_strdup(ctx, KEY_SMBCONF);
51 path = talloc_asprintf(ctx, "%s\\%s", KEY_SMBCONF, subkeyname);
54 werr = reg_open_path(ctx, path, desired_access,
63 * check if a subkey of KEY_SMBCONF of a given name exists
65 bool libnet_smbconf_key_exists(TALLOC_CTX *ctx, const char *subkeyname)
68 WERROR werr = WERR_OK;
70 struct registry_key *key;
72 if (!(mem_ctx = talloc_new(ctx))) {
73 d_fprintf(stderr, "ERROR: Out of memory...!\n");
77 werr = libnet_smbconf_open_path_q(mem_ctx, subkeyname, REG_KEY_READ, &key);
78 if (W_ERROR_IS_OK(werr)) {
87 static bool libnet_smbconf_value_exists(TALLOC_CTX *ctx,
88 struct registry_key *key,
92 WERROR werr = WERR_OK;
93 struct registry_value *value = NULL;
95 werr = reg_queryvalue(ctx, key, param, &value);
96 if (W_ERROR_IS_OK(werr)) {
105 * Open a subkey of KEY_SMBCONF (i.e a service)
106 * - variant with error output -
108 WERROR libnet_smbconf_open_path(TALLOC_CTX *ctx, const char *subkeyname,
109 uint32 desired_access,
110 struct registry_key **key)
112 WERROR werr = WERR_OK;
114 werr = libnet_smbconf_open_path_q(ctx, subkeyname, desired_access, key);
115 if (!W_ERROR_IS_OK(werr)) {
116 d_fprintf(stderr, "Error opening registry path '%s\\%s': %s\n",
118 (subkeyname == NULL) ? "" : subkeyname,
126 * open the base key KEY_SMBCONF
128 WERROR libnet_smbconf_open_basepath(TALLOC_CTX *ctx, uint32 desired_access,
129 struct registry_key **key)
131 return libnet_smbconf_open_path(ctx, NULL, desired_access, key);
135 * create a subkey of KEY_SMBCONF
137 WERROR libnet_smbconf_reg_createkey_internal(TALLOC_CTX *ctx,
138 const char * subkeyname,
139 struct registry_key **newkey)
141 WERROR werr = WERR_OK;
142 struct registry_key *create_parent = NULL;
143 TALLOC_CTX *create_ctx;
144 enum winreg_CreateAction action = REG_ACTION_NONE;
146 /* create a new talloc ctx for creation. it will hold
147 * the intermediate parent key (SMBCONF) for creation
148 * and will be destroyed when leaving this function... */
149 if (!(create_ctx = talloc_new(ctx))) {
154 werr = libnet_smbconf_open_basepath(create_ctx, REG_KEY_WRITE, &create_parent);
155 if (!W_ERROR_IS_OK(werr)) {
159 werr = reg_createkey(ctx, create_parent, subkeyname,
160 REG_KEY_WRITE, newkey, &action);
161 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
162 d_fprintf(stderr, "Key '%s' already exists.\n", subkeyname);
163 werr = WERR_ALREADY_EXISTS;
165 if (!W_ERROR_IS_OK(werr)) {
166 d_fprintf(stderr, "Error creating key %s: %s\n",
167 subkeyname, dos_errstr(werr));
171 TALLOC_FREE(create_ctx);
176 * add a value to a key.
178 WERROR libnet_smbconf_reg_setvalue_internal(struct registry_key *key,
182 struct registry_value val;
183 WERROR werr = WERR_OK;
185 const char *canon_valname;
186 const char *canon_valstr;
188 if (!lp_canonicalize_parameter_with_value(valname, valstr,
192 if (canon_valname == NULL) {
193 d_fprintf(stderr, "invalid parameter '%s' given\n",
196 d_fprintf(stderr, "invalid value '%s' given for "
197 "parameter '%s'\n", valstr, valname);
199 werr = WERR_INVALID_PARAM;
206 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
207 val.v.sz.len = strlen(canon_valstr) + 1;
209 if (registry_smbconf_valname_forbidden(canon_valname)) {
210 d_fprintf(stderr, "Parameter '%s' not allowed in registry.\n",
212 werr = WERR_INVALID_PARAM;
216 subkeyname = strrchr_m(key->key->name, '\\');
217 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
218 d_fprintf(stderr, "Invalid registry key '%s' given as "
219 "smbconf section.\n", key->key->name);
220 werr = WERR_INVALID_PARAM;
224 if (!strequal(subkeyname, GLOBAL_NAME) &&
225 lp_parameter_is_global(valname))
227 d_fprintf(stderr, "Global paramter '%s' not allowed in "
228 "service definition ('%s').\n", canon_valname,
230 werr = WERR_INVALID_PARAM;
234 werr = reg_setvalue(key, canon_valname, &val);
235 if (!W_ERROR_IS_OK(werr)) {
237 "Error adding value '%s' to "
239 canon_valname, key->key->name, dos_errstr(werr));
246 /**********************************************************************
248 * The actual net conf api functions, that are exported.
250 **********************************************************************/
252 WERROR libnet_smbconf_setparm(TALLOC_CTX *mem_ctx,
258 struct registry_key *key = NULL;
260 if (!libnet_smbconf_key_exists(mem_ctx, service)) {
261 werr = libnet_smbconf_reg_createkey_internal(mem_ctx, service,
264 werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_WRITE,
267 W_ERROR_NOT_OK_RETURN(werr);
269 werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
274 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
277 struct registry_value **value)
280 struct registry_key *key = NULL;
282 if (!libnet_smbconf_key_exists(mem_ctx, service)) {
283 werr = WERR_NO_SUCH_SERVICE;
287 werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_READ, &key);
288 if (!W_ERROR_IS_OK(werr)) {
292 if (!libnet_smbconf_value_exists(mem_ctx, key, param)) {
293 werr = WERR_INVALID_PARAM;
297 werr = reg_queryvalue(mem_ctx, key, param, value);
304 WERROR libnet_smbconf_delparm(TALLOC_CTX *mem_ctx,
308 struct registry_key *key = NULL;
309 WERROR werr = WERR_OK;
311 if (!libnet_smbconf_key_exists(mem_ctx, service)) {
312 return WERR_NO_SUCH_SERVICE;
315 werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_ALL, &key);
316 W_ERROR_NOT_OK_RETURN(werr);
318 if (!libnet_smbconf_value_exists(mem_ctx, key, param)) {
319 return WERR_INVALID_PARAM;
322 werr = reg_deletevalue(key, param);
328 /**********************************************************************
330 * Convenience functions, that are also exportet.
332 **********************************************************************/
334 WERROR libnet_smbconf_set_global_param(TALLOC_CTX *mem_ctx,
338 return libnet_smbconf_setparm(mem_ctx, GLOBAL_NAME, param, val);