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)
33 WERROR libnet_smbconf_reg_open_path(TALLOC_CTX *ctx,
34 const char *subkeyname,
35 uint32 desired_access,
36 struct registry_key **key)
38 WERROR werr = WERR_OK;
42 if (!(token = registry_create_admin_token(ctx))) {
43 DEBUG(1, ("Error creating admin token\n"));
47 if (subkeyname == NULL) {
48 path = talloc_strdup(ctx, KEY_SMBCONF);
50 path = talloc_asprintf(ctx, "%s\\%s", KEY_SMBCONF, subkeyname);
53 werr = reg_open_path(ctx, path, desired_access,
56 if (!W_ERROR_IS_OK(werr)) {
57 DEBUG(1, ("Error opening registry path '%s': %s\n",
58 path, dos_errstr(werr)));
67 * open the base key KEY_SMBCONF
69 WERROR libnet_smbconf_reg_open_basepath(TALLOC_CTX *ctx, uint32 desired_access,
70 struct registry_key **key)
72 return libnet_smbconf_reg_open_path(ctx, NULL, desired_access, key);
76 * check if a subkey of KEY_SMBCONF of a given name exists
78 bool libnet_smbconf_key_exists(const char *subkeyname)
81 WERROR werr = WERR_OK;
82 TALLOC_CTX *mem_ctx = talloc_stackframe();
83 struct registry_key *key = NULL;
85 werr = libnet_smbconf_reg_open_path(mem_ctx, subkeyname, REG_KEY_READ,
87 if (W_ERROR_IS_OK(werr)) {
95 static bool libnet_smbconf_value_exists(struct registry_key *key,
99 WERROR werr = WERR_OK;
100 TALLOC_CTX *ctx = talloc_stackframe();
101 struct registry_value *value = NULL;
103 werr = reg_queryvalue(ctx, key, param, &value);
104 if (W_ERROR_IS_OK(werr)) {
113 * create a subkey of KEY_SMBCONF
115 WERROR libnet_smbconf_reg_createkey_internal(TALLOC_CTX *ctx,
116 const char * subkeyname,
117 struct registry_key **newkey)
119 WERROR werr = WERR_OK;
120 struct registry_key *create_parent = NULL;
121 TALLOC_CTX *create_ctx;
122 enum winreg_CreateAction action = REG_ACTION_NONE;
124 /* create a new talloc ctx for creation. it will hold
125 * the intermediate parent key (SMBCONF) for creation
126 * and will be destroyed when leaving this function... */
127 if (!(create_ctx = talloc_new(ctx))) {
132 werr = libnet_smbconf_reg_open_basepath(create_ctx, REG_KEY_WRITE,
134 if (!W_ERROR_IS_OK(werr)) {
138 werr = reg_createkey(ctx, create_parent, subkeyname,
139 REG_KEY_WRITE, newkey, &action);
140 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
141 d_fprintf(stderr, "Key '%s' already exists.\n", subkeyname);
142 werr = WERR_ALREADY_EXISTS;
144 if (!W_ERROR_IS_OK(werr)) {
145 d_fprintf(stderr, "Error creating key %s: %s\n",
146 subkeyname, dos_errstr(werr));
150 TALLOC_FREE(create_ctx);
155 * add a value to a key.
157 WERROR libnet_smbconf_reg_setvalue_internal(struct registry_key *key,
161 struct registry_value val;
162 WERROR werr = WERR_OK;
164 const char *canon_valname;
165 const char *canon_valstr;
167 if (!lp_canonicalize_parameter_with_value(valname, valstr,
171 if (canon_valname == NULL) {
172 d_fprintf(stderr, "invalid parameter '%s' given\n",
175 d_fprintf(stderr, "invalid value '%s' given for "
176 "parameter '%s'\n", valstr, valname);
178 werr = WERR_INVALID_PARAM;
185 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
186 val.v.sz.len = strlen(canon_valstr) + 1;
188 if (registry_smbconf_valname_forbidden(canon_valname)) {
189 d_fprintf(stderr, "Parameter '%s' not allowed in registry.\n",
191 werr = WERR_INVALID_PARAM;
195 subkeyname = strrchr_m(key->key->name, '\\');
196 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
197 d_fprintf(stderr, "Invalid registry key '%s' given as "
198 "smbconf section.\n", key->key->name);
199 werr = WERR_INVALID_PARAM;
203 if (!strequal(subkeyname, GLOBAL_NAME) &&
204 lp_parameter_is_global(valname))
206 d_fprintf(stderr, "Global paramter '%s' not allowed in "
207 "service definition ('%s').\n", canon_valname,
209 werr = WERR_INVALID_PARAM;
213 werr = reg_setvalue(key, canon_valname, &val);
214 if (!W_ERROR_IS_OK(werr)) {
216 "Error adding value '%s' to "
218 canon_valname, key->key->name, dos_errstr(werr));
226 * format a registry_value into a string.
228 * This is intended to be used for smbconf registry values,
229 * which are ar stored as REG_SZ values, so the incomplete
230 * handling should be ok.
232 char *libnet_smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
233 struct registry_value *value)
237 /* alternatively, create a new talloc context? */
238 if (mem_ctx == NULL) {
242 switch (value->type) {
244 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
248 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
252 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
253 result = talloc_asprintf(mem_ctx, "\"%s\" ",
254 value->v.multi_sz.strings[j]);
259 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
260 (int)value->v.binary.length);
263 result = talloc_asprintf(mem_ctx, "<unprintable>");
270 /**********************************************************************
272 * The actual net conf api functions, that are exported.
274 **********************************************************************/
277 * Drop the whole configuration (restarting empty).
279 WERROR libnet_smbconf_drop(void)
282 WERROR werr = WERR_OK;
283 NT_USER_TOKEN *token;
284 struct registry_key *parent_key = NULL;
285 struct registry_key *new_key = NULL;
286 TALLOC_CTX* mem_ctx = talloc_stackframe();
287 enum winreg_CreateAction action;
289 if (!(token = registry_create_admin_token(mem_ctx))) {
290 /* what is the appropriate error code here? */
291 werr = WERR_CAN_NOT_COMPLETE;
295 path = talloc_strdup(mem_ctx, KEY_SMBCONF);
300 p = strrchr(path, '\\');
302 werr = reg_open_path(mem_ctx, path, REG_KEY_WRITE, token, &parent_key);
304 if (!W_ERROR_IS_OK(werr)) {
308 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
310 if (!W_ERROR_IS_OK(werr)) {
314 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
318 TALLOC_FREE(mem_ctx);
323 * delete a service from configuration
325 WERROR libnet_smbconf_delshare(const char *servicename)
327 WERROR werr = WERR_OK;
328 struct registry_key *key = NULL;
329 TALLOC_CTX *ctx = talloc_stackframe();
331 werr = libnet_smbconf_reg_open_basepath(ctx, REG_KEY_WRITE, &key);
332 if (!W_ERROR_IS_OK(werr)) {
336 werr = reg_deletekey_recursive(key, key, servicename);
343 WERROR libnet_smbconf_setparm(const char *service,
348 struct registry_key *key = NULL;
349 TALLOC_CTX *mem_ctx = talloc_stackframe();
351 if (!libnet_smbconf_key_exists(service)) {
352 werr = libnet_smbconf_reg_createkey_internal(mem_ctx, service,
355 werr = libnet_smbconf_reg_open_path(mem_ctx, service,
356 REG_KEY_WRITE, &key);
358 if (!W_ERROR_IS_OK(werr)) {
362 werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
365 TALLOC_FREE(mem_ctx);
369 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
374 WERROR werr = WERR_OK;
375 struct registry_key *key = NULL;
376 struct registry_value *value = NULL;
378 if (valstr == NULL) {
379 werr = WERR_INVALID_PARAM;
383 if (!libnet_smbconf_key_exists(service)) {
384 werr = WERR_NO_SUCH_SERVICE;
388 werr = libnet_smbconf_reg_open_path(mem_ctx, service, REG_KEY_READ,
390 if (!W_ERROR_IS_OK(werr)) {
394 if (!libnet_smbconf_value_exists(key, param)) {
395 werr = WERR_INVALID_PARAM;
399 werr = reg_queryvalue(mem_ctx, key, param, &value);
400 if (!W_ERROR_IS_OK(werr)) {
404 *valstr = libnet_smbconf_format_registry_value(mem_ctx, value);
406 if (*valstr == NULL) {
416 WERROR libnet_smbconf_delparm(const char *service,
419 struct registry_key *key = NULL;
420 WERROR werr = WERR_OK;
421 TALLOC_CTX *mem_ctx = talloc_stackframe();
423 if (!libnet_smbconf_key_exists(service)) {
424 return WERR_NO_SUCH_SERVICE;
427 werr = libnet_smbconf_reg_open_path(mem_ctx, service, REG_KEY_ALL, &key);
428 if (!W_ERROR_IS_OK(werr)) {
432 if (!libnet_smbconf_value_exists(key, param)) {
433 werr = WERR_INVALID_PARAM;
437 werr = reg_deletevalue(key, param);
440 TALLOC_FREE(mem_ctx);
445 /**********************************************************************
447 * Convenience functions that are also exported.
449 **********************************************************************/
451 WERROR libnet_smbconf_set_global_param(const char *param,
454 return libnet_smbconf_setparm(GLOBAL_NAME, param, val);