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(const char *subkeyname)
68 WERROR werr = WERR_OK;
69 TALLOC_CTX *mem_ctx = talloc_stackframe();
70 struct registry_key *key = NULL;
72 werr = libnet_smbconf_open_path_q(mem_ctx, subkeyname, REG_KEY_READ, &key);
73 if (W_ERROR_IS_OK(werr)) {
81 static bool libnet_smbconf_value_exists(struct registry_key *key,
85 WERROR werr = WERR_OK;
86 TALLOC_CTX *ctx = talloc_stackframe();
87 struct registry_value *value = NULL;
89 werr = reg_queryvalue(ctx, key, param, &value);
90 if (W_ERROR_IS_OK(werr)) {
99 * Open a subkey of KEY_SMBCONF (i.e a service)
100 * - variant with error output -
102 WERROR libnet_smbconf_open_path(TALLOC_CTX *ctx, const char *subkeyname,
103 uint32 desired_access,
104 struct registry_key **key)
106 WERROR werr = WERR_OK;
108 werr = libnet_smbconf_open_path_q(ctx, subkeyname, desired_access, key);
109 if (!W_ERROR_IS_OK(werr)) {
110 d_fprintf(stderr, "Error opening registry path '%s\\%s': %s\n",
112 (subkeyname == NULL) ? "" : subkeyname,
120 * open the base key KEY_SMBCONF
122 WERROR libnet_smbconf_open_basepath(TALLOC_CTX *ctx, uint32 desired_access,
123 struct registry_key **key)
125 return libnet_smbconf_open_path(ctx, NULL, desired_access, key);
129 * create a subkey of KEY_SMBCONF
131 WERROR libnet_smbconf_reg_createkey_internal(TALLOC_CTX *ctx,
132 const char * subkeyname,
133 struct registry_key **newkey)
135 WERROR werr = WERR_OK;
136 struct registry_key *create_parent = NULL;
137 TALLOC_CTX *create_ctx;
138 enum winreg_CreateAction action = REG_ACTION_NONE;
140 /* create a new talloc ctx for creation. it will hold
141 * the intermediate parent key (SMBCONF) for creation
142 * and will be destroyed when leaving this function... */
143 if (!(create_ctx = talloc_new(ctx))) {
148 werr = libnet_smbconf_open_basepath(create_ctx, REG_KEY_WRITE, &create_parent);
149 if (!W_ERROR_IS_OK(werr)) {
153 werr = reg_createkey(ctx, create_parent, subkeyname,
154 REG_KEY_WRITE, newkey, &action);
155 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
156 d_fprintf(stderr, "Key '%s' already exists.\n", subkeyname);
157 werr = WERR_ALREADY_EXISTS;
159 if (!W_ERROR_IS_OK(werr)) {
160 d_fprintf(stderr, "Error creating key %s: %s\n",
161 subkeyname, dos_errstr(werr));
165 TALLOC_FREE(create_ctx);
170 * add a value to a key.
172 WERROR libnet_smbconf_reg_setvalue_internal(struct registry_key *key,
176 struct registry_value val;
177 WERROR werr = WERR_OK;
179 const char *canon_valname;
180 const char *canon_valstr;
182 if (!lp_canonicalize_parameter_with_value(valname, valstr,
186 if (canon_valname == NULL) {
187 d_fprintf(stderr, "invalid parameter '%s' given\n",
190 d_fprintf(stderr, "invalid value '%s' given for "
191 "parameter '%s'\n", valstr, valname);
193 werr = WERR_INVALID_PARAM;
200 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
201 val.v.sz.len = strlen(canon_valstr) + 1;
203 if (registry_smbconf_valname_forbidden(canon_valname)) {
204 d_fprintf(stderr, "Parameter '%s' not allowed in registry.\n",
206 werr = WERR_INVALID_PARAM;
210 subkeyname = strrchr_m(key->key->name, '\\');
211 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
212 d_fprintf(stderr, "Invalid registry key '%s' given as "
213 "smbconf section.\n", key->key->name);
214 werr = WERR_INVALID_PARAM;
218 if (!strequal(subkeyname, GLOBAL_NAME) &&
219 lp_parameter_is_global(valname))
221 d_fprintf(stderr, "Global paramter '%s' not allowed in "
222 "service definition ('%s').\n", canon_valname,
224 werr = WERR_INVALID_PARAM;
228 werr = reg_setvalue(key, canon_valname, &val);
229 if (!W_ERROR_IS_OK(werr)) {
231 "Error adding value '%s' to "
233 canon_valname, key->key->name, dos_errstr(werr));
240 /**********************************************************************
242 * The actual net conf api functions, that are exported.
244 **********************************************************************/
246 WERROR libnet_smbconf_drop(void)
249 WERROR werr = WERR_OK;
250 NT_USER_TOKEN *token;
251 struct registry_key *parent_key = NULL;
252 struct registry_key *new_key = NULL;
253 TALLOC_CTX* mem_ctx = talloc_stackframe();
254 enum winreg_CreateAction action;
256 if (!(token = registry_create_admin_token(mem_ctx))) {
257 /* what is the appropriate error code here? */
258 werr = WERR_CAN_NOT_COMPLETE;
262 path = talloc_strdup(mem_ctx, KEY_SMBCONF);
267 p = strrchr(path, '\\');
269 werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token, &parent_key);
271 if (!W_ERROR_IS_OK(werr)) {
275 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
277 if (!W_ERROR_IS_OK(werr)) {
281 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
285 TALLOC_FREE(mem_ctx);
290 * delete a service from configuration
292 WERROR libnet_smbconf_delshare(const char *servicename)
294 WERROR werr = WERR_OK;
295 struct registry_key *key = NULL;
296 TALLOC_CTX *ctx = talloc_stackframe();
298 werr = libnet_smbconf_open_basepath(ctx, REG_KEY_WRITE, &key);
299 if (!W_ERROR_IS_OK(werr)) {
303 werr = reg_deletekey_recursive(key, key, servicename);
310 WERROR libnet_smbconf_setparm(TALLOC_CTX *mem_ctx,
316 struct registry_key *key = NULL;
318 if (!libnet_smbconf_key_exists(service)) {
319 werr = libnet_smbconf_reg_createkey_internal(mem_ctx, service,
322 werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_WRITE,
325 if (!W_ERROR_IS_OK(werr)) {
329 werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
336 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
339 struct registry_value **value)
342 struct registry_key *key = NULL;
344 if (!libnet_smbconf_key_exists(service)) {
345 werr = WERR_NO_SUCH_SERVICE;
349 werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_READ, &key);
350 if (!W_ERROR_IS_OK(werr)) {
354 if (!libnet_smbconf_value_exists(key, param)) {
355 werr = WERR_INVALID_PARAM;
359 werr = reg_queryvalue(mem_ctx, key, param, value);
366 WERROR libnet_smbconf_delparm(TALLOC_CTX *mem_ctx,
370 struct registry_key *key = NULL;
371 WERROR werr = WERR_OK;
373 if (!libnet_smbconf_key_exists(service)) {
374 return WERR_NO_SUCH_SERVICE;
377 werr = libnet_smbconf_open_path(mem_ctx, service, REG_KEY_ALL, &key);
378 if (!W_ERROR_IS_OK(werr)) {
382 if (!libnet_smbconf_value_exists(key, param)) {
383 werr = WERR_INVALID_PARAM;
387 werr = reg_deletevalue(key, param);
395 /**********************************************************************
397 * Convenience functions, that are also exportet.
399 **********************************************************************/
401 WERROR libnet_smbconf_set_global_param(TALLOC_CTX *mem_ctx,
405 return libnet_smbconf_setparm(mem_ctx, GLOBAL_NAME, param, val);