2 * Unix SMB/CIFS implementation.
3 * libnet smbconf registry Support
4 * Copyright (C) Michael Adam 2007-2008
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_conf_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 registry key specified by "path"
60 static WERROR libnet_conf_reg_open_path(TALLOC_CTX *mem_ctx,
62 uint32 desired_access,
63 struct registry_key **key)
65 WERROR werr = WERR_OK;
67 TALLOC_CTX *tmp_ctx = NULL;
70 DEBUG(1, ("Error: NULL path string given\n"));
71 werr = WERR_INVALID_PARAM;
75 tmp_ctx = talloc_new(mem_ctx);
76 if (tmp_ctx == NULL) {
81 token = registry_create_admin_token(tmp_ctx);
83 DEBUG(1, ("Error creating admin token\n"));
84 /* what is the appropriate error code here? */
85 werr = WERR_CAN_NOT_COMPLETE;
89 werr = reg_open_path(mem_ctx, path, desired_access, token, key);
91 if (!W_ERROR_IS_OK(werr)) {
92 DEBUG(1, ("Error opening registry path '%s': %s\n",
93 path, dos_errstr(werr)));
102 * Open a subkey of KEY_SMBCONF (i.e a service)
104 static WERROR libnet_conf_reg_open_service_key(TALLOC_CTX *ctx,
105 const char *servicename,
106 uint32 desired_access,
107 struct registry_key **key)
109 WERROR werr = WERR_OK;
112 if (servicename == NULL) {
113 DEBUG(3, ("Error: NULL servicename given.\n"));
114 werr = WERR_INVALID_PARAM;
118 path = talloc_asprintf(ctx, "%s\\%s", KEY_SMBCONF, servicename);
120 werr = libnet_conf_reg_open_path(ctx, path, desired_access, key);
128 * open the base key KEY_SMBCONF
130 static WERROR libnet_conf_reg_open_base_key(TALLOC_CTX *ctx,
131 uint32 desired_access,
132 struct registry_key **key)
134 return libnet_conf_reg_open_path(ctx, KEY_SMBCONF, desired_access, key);
137 static bool libnet_conf_value_exists(struct registry_key *key,
141 WERROR werr = WERR_OK;
142 TALLOC_CTX *ctx = talloc_stackframe();
143 struct registry_value *value = NULL;
145 werr = reg_queryvalue(ctx, key, param, &value);
146 if (W_ERROR_IS_OK(werr)) {
155 * create a subkey of KEY_SMBCONF
157 static WERROR libnet_conf_reg_create_service_key(TALLOC_CTX *ctx,
158 const char * subkeyname,
159 struct registry_key **newkey)
161 WERROR werr = WERR_OK;
162 struct registry_key *create_parent = NULL;
163 TALLOC_CTX *create_ctx;
164 enum winreg_CreateAction action = REG_ACTION_NONE;
166 /* create a new talloc ctx for creation. it will hold
167 * the intermediate parent key (SMBCONF) for creation
168 * and will be destroyed when leaving this function... */
169 if (!(create_ctx = talloc_new(ctx))) {
174 werr = libnet_conf_reg_open_base_key(create_ctx, REG_KEY_WRITE,
176 if (!W_ERROR_IS_OK(werr)) {
180 werr = reg_createkey(ctx, create_parent, subkeyname,
181 REG_KEY_WRITE, newkey, &action);
182 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
183 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
184 werr = WERR_ALREADY_EXISTS;
186 if (!W_ERROR_IS_OK(werr)) {
187 DEBUG(5, ("Error creating key %s: %s\n",
188 subkeyname, dos_errstr(werr)));
192 TALLOC_FREE(create_ctx);
197 * add a value to a key.
199 static WERROR libnet_conf_reg_set_value(struct registry_key *key,
203 struct registry_value val;
204 WERROR werr = WERR_OK;
206 const char *canon_valname;
207 const char *canon_valstr;
209 if (!lp_canonicalize_parameter_with_value(valname, valstr,
213 if (canon_valname == NULL) {
214 DEBUG(5, ("invalid parameter '%s' given\n",
217 DEBUG(5, ("invalid value '%s' given for "
218 "parameter '%s'\n", valstr, valname));
220 werr = WERR_INVALID_PARAM;
227 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
228 val.v.sz.len = strlen(canon_valstr) + 1;
230 if (registry_smbconf_valname_forbidden(canon_valname)) {
231 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
233 werr = WERR_INVALID_PARAM;
237 subkeyname = strrchr_m(key->key->name, '\\');
238 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
239 DEBUG(5, ("Invalid registry key '%s' given as "
240 "smbconf section.\n", key->key->name));
241 werr = WERR_INVALID_PARAM;
245 if (!strequal(subkeyname, GLOBAL_NAME) &&
246 lp_parameter_is_global(valname))
248 DEBUG(5, ("Global paramter '%s' not allowed in "
249 "service definition ('%s').\n", canon_valname,
251 werr = WERR_INVALID_PARAM;
255 werr = reg_setvalue(key, canon_valname, &val);
256 if (!W_ERROR_IS_OK(werr)) {
257 DEBUG(5, ("Error adding value '%s' to "
259 canon_valname, key->key->name, dos_errstr(werr)));
267 * format a registry_value into a string.
269 * This is intended to be used for smbconf registry values,
270 * which are ar stored as REG_SZ values, so the incomplete
271 * handling should be ok.
273 static char *libnet_conf_format_registry_value(TALLOC_CTX *mem_ctx,
274 struct registry_value *value)
278 /* alternatively, create a new talloc context? */
279 if (mem_ctx == NULL) {
283 switch (value->type) {
285 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
289 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
293 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
294 result = talloc_asprintf(mem_ctx, "\"%s\" ",
295 value->v.multi_sz.strings[j]);
300 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
301 (int)value->v.binary.length);
304 result = talloc_asprintf(mem_ctx, "<unprintable>");
311 * Get the values of a key as a list of value names
312 * and a list of value strings (ordered)
314 static WERROR libnet_conf_reg_get_values(TALLOC_CTX *mem_ctx,
315 struct registry_key *key,
316 uint32_t *num_values,
318 char ***value_strings)
320 TALLOC_CTX *tmp_ctx = NULL;
321 WERROR werr = WERR_OK;
323 struct registry_value *valvalue = NULL;
324 char *valname = NULL;
325 char **tmp_valnames = NULL;
326 char **tmp_valstrings = NULL;
328 if ((num_values == NULL) || (value_names == NULL) ||
329 (value_strings == NULL))
331 werr = WERR_INVALID_PARAM;
335 tmp_ctx = talloc_new(mem_ctx);
336 if (tmp_ctx == NULL) {
342 W_ERROR_IS_OK(werr = reg_enumvalue(tmp_ctx, key, count, &valname,
348 werr = libnet_conf_add_string_to_array(tmp_ctx,
351 if (!W_ERROR_IS_OK(werr)) {
355 valstring = libnet_conf_format_registry_value(tmp_ctx,
357 werr = libnet_conf_add_string_to_array(tmp_ctx,
361 if (!W_ERROR_IS_OK(werr)) {
365 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
373 *value_names = talloc_move(mem_ctx, &tmp_valnames);
374 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
377 *value_strings = NULL;
381 TALLOC_FREE(tmp_ctx);
385 /**********************************************************************
387 * The actual net conf api functions, that are exported.
389 **********************************************************************/
392 * Drop the whole configuration (restarting empty).
394 WERROR libnet_conf_drop(void)
397 WERROR werr = WERR_OK;
398 struct registry_key *parent_key = NULL;
399 struct registry_key *new_key = NULL;
400 TALLOC_CTX* mem_ctx = talloc_stackframe();
401 enum winreg_CreateAction action;
403 path = talloc_strdup(mem_ctx, KEY_SMBCONF);
408 p = strrchr(path, '\\');
410 werr = libnet_conf_reg_open_path(mem_ctx, path, REG_KEY_WRITE,
413 if (!W_ERROR_IS_OK(werr)) {
417 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
419 if (!W_ERROR_IS_OK(werr)) {
423 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
427 TALLOC_FREE(mem_ctx);
432 * Get the whole configuration as lists of strings with counts:
434 * num_shares : number of shares
435 * share_names : list of length num_shares of share names
436 * num_params : list of length num_shares of parameter counts for each share
437 * param_names : list of lists of parameter names for each share
438 * param_values : list of lists of parameter values for each share
440 WERROR libnet_conf_get_config(TALLOC_CTX *mem_ctx, uint32_t *num_shares,
441 char ***share_names, uint32_t **num_params,
442 char ****param_names, char ****param_values)
444 WERROR werr = WERR_OK;
445 TALLOC_CTX *tmp_ctx = NULL;
446 uint32_t tmp_num_shares;
447 char **tmp_share_names;
448 uint32_t *tmp_num_params;
449 char ***tmp_param_names;
450 char ***tmp_param_values;
453 if ((num_shares == NULL) || (share_names == NULL) ||
454 (num_params == NULL) || (param_names == NULL) ||
455 (param_values == NULL))
457 werr = WERR_INVALID_PARAM;
461 tmp_ctx = talloc_new(mem_ctx);
462 if (tmp_ctx == NULL) {
467 werr = libnet_conf_get_share_names(tmp_ctx, &tmp_num_shares,
469 if (!W_ERROR_IS_OK(werr)) {
473 tmp_num_params = TALLOC_ARRAY(tmp_ctx, uint32_t, tmp_num_shares);
474 tmp_param_names = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
475 tmp_param_values = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
477 if ((tmp_num_params == NULL) || (tmp_param_names == NULL) ||
478 (tmp_param_values == NULL))
484 for (count = 0; count < tmp_num_shares; count++) {
485 werr = libnet_smbconf_getshare(mem_ctx, tmp_share_names[count],
486 &tmp_num_params[count],
487 &tmp_param_names[count],
488 &tmp_param_values[count]);
489 if (!W_ERROR_IS_OK(werr)) {
496 *num_shares = tmp_num_shares;
497 if (tmp_num_shares > 0) {
498 *share_names = talloc_move(mem_ctx, &tmp_share_names);
499 *num_params = talloc_move(mem_ctx, &tmp_num_params);
500 *param_names = talloc_move(mem_ctx, &tmp_param_names);
501 *param_values = talloc_move(mem_ctx, &tmp_param_values);
506 *param_values = NULL;
510 TALLOC_FREE(tmp_ctx);
516 * get the list of share names defined in the configuration.
518 WERROR libnet_conf_get_share_names(TALLOC_CTX *mem_ctx, uint32_t *num_shares,
522 uint32_t added_count = 0;
523 TALLOC_CTX *tmp_ctx = NULL;
524 WERROR werr = WERR_OK;
525 struct registry_key *key = NULL;
526 char *subkey_name = NULL;
527 char **tmp_share_names = NULL;
529 if ((num_shares == NULL) || (share_names == NULL)) {
530 werr = WERR_INVALID_PARAM;
534 tmp_ctx = talloc_new(mem_ctx);
535 if (tmp_ctx == NULL) {
540 /* make sure "global" is always listed first */
541 if (libnet_smbconf_share_exists(GLOBAL_NAME)) {
542 werr = libnet_conf_add_string_to_array(tmp_ctx,
545 if (!W_ERROR_IS_OK(werr)) {
551 werr = libnet_conf_reg_open_base_key(tmp_ctx, SEC_RIGHTS_ENUM_SUBKEYS,
553 if (!W_ERROR_IS_OK(werr)) {
558 W_ERROR_IS_OK(werr = reg_enumkey(tmp_ctx, key, count,
559 &subkey_name, NULL));
562 if (strequal(subkey_name, GLOBAL_NAME)) {
566 werr = libnet_conf_add_string_to_array(tmp_ctx,
570 if (!W_ERROR_IS_OK(werr)) {
575 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
580 *num_shares = added_count;
581 if (added_count > 0) {
582 *share_names = talloc_move(mem_ctx, &tmp_share_names);
588 TALLOC_FREE(tmp_ctx);
593 * check if a share/service of a given name exists
595 bool libnet_smbconf_share_exists(const char *servicename)
598 WERROR werr = WERR_OK;
599 TALLOC_CTX *mem_ctx = talloc_stackframe();
600 struct registry_key *key = NULL;
602 werr = libnet_conf_reg_open_service_key(mem_ctx, servicename,
604 if (W_ERROR_IS_OK(werr)) {
608 TALLOC_FREE(mem_ctx);
613 * Add a service if it does not already exist.
615 WERROR libnet_smbconf_create_share(const char *servicename)
618 TALLOC_CTX *mem_ctx = talloc_stackframe();
619 struct registry_key *key = NULL;
621 if (libnet_smbconf_share_exists(servicename)) {
622 werr = WERR_ALREADY_EXISTS;
626 werr = libnet_conf_reg_create_service_key(mem_ctx, servicename, &key);
629 TALLOC_FREE(mem_ctx);
634 * get a definition of a share (service) from configuration.
636 WERROR libnet_smbconf_getshare(TALLOC_CTX *mem_ctx, const char *servicename,
637 uint32_t *num_params, char ***param_names,
638 char ***param_values)
640 WERROR werr = WERR_OK;
641 struct registry_key *key = NULL;
643 werr = libnet_conf_reg_open_service_key(mem_ctx, servicename,
645 if (!W_ERROR_IS_OK(werr)) {
649 werr = libnet_conf_reg_get_values(mem_ctx, key, num_params,
650 param_names, param_values);
658 * delete a service from configuration
660 WERROR libnet_smbconf_delshare(const char *servicename)
662 WERROR werr = WERR_OK;
663 struct registry_key *key = NULL;
664 TALLOC_CTX *ctx = talloc_stackframe();
666 werr = libnet_conf_reg_open_base_key(ctx, REG_KEY_WRITE, &key);
667 if (!W_ERROR_IS_OK(werr)) {
671 werr = reg_deletekey_recursive(key, key, servicename);
679 * set a configuration parameter to the value provided.
681 WERROR libnet_smbconf_setparm(const char *service,
686 struct registry_key *key = NULL;
687 TALLOC_CTX *mem_ctx = talloc_stackframe();
689 if (!libnet_smbconf_share_exists(service)) {
690 werr = WERR_NO_SUCH_SERVICE;
694 werr = libnet_conf_reg_open_service_key(mem_ctx, service, REG_KEY_WRITE,
696 if (!W_ERROR_IS_OK(werr)) {
700 werr = libnet_conf_reg_set_value(key, param, valstr);
703 TALLOC_FREE(mem_ctx);
708 * get the value of a configuration parameter as a string
710 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
715 WERROR werr = WERR_OK;
716 struct registry_key *key = NULL;
717 struct registry_value *value = NULL;
719 if (valstr == NULL) {
720 werr = WERR_INVALID_PARAM;
724 if (!libnet_smbconf_share_exists(service)) {
725 werr = WERR_NO_SUCH_SERVICE;
729 werr = libnet_conf_reg_open_service_key(mem_ctx, service, REG_KEY_READ,
731 if (!W_ERROR_IS_OK(werr)) {
735 if (!libnet_conf_value_exists(key, param)) {
736 werr = WERR_INVALID_PARAM;
740 werr = reg_queryvalue(mem_ctx, key, param, &value);
741 if (!W_ERROR_IS_OK(werr)) {
745 *valstr = libnet_conf_format_registry_value(mem_ctx, value);
747 if (*valstr == NULL) {
758 * delete a parameter from configuration
760 WERROR libnet_smbconf_delparm(const char *service,
763 struct registry_key *key = NULL;
764 WERROR werr = WERR_OK;
765 TALLOC_CTX *mem_ctx = talloc_stackframe();
767 if (!libnet_smbconf_share_exists(service)) {
768 return WERR_NO_SUCH_SERVICE;
771 werr = libnet_conf_reg_open_service_key(mem_ctx, service, REG_KEY_ALL,
773 if (!W_ERROR_IS_OK(werr)) {
777 if (!libnet_conf_value_exists(key, param)) {
778 werr = WERR_INVALID_PARAM;
782 werr = reg_deletevalue(key, param);
785 TALLOC_FREE(mem_ctx);
790 /**********************************************************************
792 * Convenience functions that are also exported.
794 **********************************************************************/
796 WERROR libnet_smbconf_set_global_param(const char *param,
799 return libnet_smbconf_setparm(GLOBAL_NAME, param, val);