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_smbconf_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 subkey of KEY_SMBCONF (i.e a service)
60 static WERROR libnet_smbconf_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_smbconf_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_smbconf_reg_open_path(ctx, path, desired_access, key);
128 * open the base key KEY_SMBCONF
130 static WERROR libnet_smbconf_reg_open_basekey(TALLOC_CTX *ctx,
131 uint32 desired_access,
132 struct registry_key **key)
134 return libnet_smbconf_reg_open_path(ctx, KEY_SMBCONF, desired_access,
138 static bool libnet_smbconf_value_exists(struct registry_key *key,
142 WERROR werr = WERR_OK;
143 TALLOC_CTX *ctx = talloc_stackframe();
144 struct registry_value *value = NULL;
146 werr = reg_queryvalue(ctx, key, param, &value);
147 if (W_ERROR_IS_OK(werr)) {
156 * create a subkey of KEY_SMBCONF
158 static WERROR libnet_smbconf_reg_create_service_key(TALLOC_CTX *ctx,
159 const char * subkeyname,
160 struct registry_key **newkey)
162 WERROR werr = WERR_OK;
163 struct registry_key *create_parent = NULL;
164 TALLOC_CTX *create_ctx;
165 enum winreg_CreateAction action = REG_ACTION_NONE;
167 /* create a new talloc ctx for creation. it will hold
168 * the intermediate parent key (SMBCONF) for creation
169 * and will be destroyed when leaving this function... */
170 if (!(create_ctx = talloc_new(ctx))) {
175 werr = libnet_smbconf_reg_open_basekey(create_ctx, REG_KEY_WRITE,
177 if (!W_ERROR_IS_OK(werr)) {
181 werr = reg_createkey(ctx, create_parent, subkeyname,
182 REG_KEY_WRITE, newkey, &action);
183 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
184 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
185 werr = WERR_ALREADY_EXISTS;
187 if (!W_ERROR_IS_OK(werr)) {
188 DEBUG(5, ("Error creating key %s: %s\n",
189 subkeyname, dos_errstr(werr)));
193 TALLOC_FREE(create_ctx);
198 * add a value to a key.
200 static WERROR libnet_smbconf_reg_setvalue_internal(struct registry_key *key,
204 struct registry_value val;
205 WERROR werr = WERR_OK;
207 const char *canon_valname;
208 const char *canon_valstr;
210 if (!lp_canonicalize_parameter_with_value(valname, valstr,
214 if (canon_valname == NULL) {
215 DEBUG(5, ("invalid parameter '%s' given\n",
218 DEBUG(5, ("invalid value '%s' given for "
219 "parameter '%s'\n", valstr, valname));
221 werr = WERR_INVALID_PARAM;
228 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
229 val.v.sz.len = strlen(canon_valstr) + 1;
231 if (registry_smbconf_valname_forbidden(canon_valname)) {
232 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
234 werr = WERR_INVALID_PARAM;
238 subkeyname = strrchr_m(key->key->name, '\\');
239 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
240 DEBUG(5, ("Invalid registry key '%s' given as "
241 "smbconf section.\n", key->key->name));
242 werr = WERR_INVALID_PARAM;
246 if (!strequal(subkeyname, GLOBAL_NAME) &&
247 lp_parameter_is_global(valname))
249 DEBUG(5, ("Global paramter '%s' not allowed in "
250 "service definition ('%s').\n", canon_valname,
252 werr = WERR_INVALID_PARAM;
256 werr = reg_setvalue(key, canon_valname, &val);
257 if (!W_ERROR_IS_OK(werr)) {
258 DEBUG(5, ("Error adding value '%s' to "
260 canon_valname, key->key->name, dos_errstr(werr)));
268 * format a registry_value into a string.
270 * This is intended to be used for smbconf registry values,
271 * which are ar stored as REG_SZ values, so the incomplete
272 * handling should be ok.
274 static char *libnet_smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
275 struct registry_value *value)
279 /* alternatively, create a new talloc context? */
280 if (mem_ctx == NULL) {
284 switch (value->type) {
286 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
290 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
294 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
295 result = talloc_asprintf(mem_ctx, "\"%s\" ",
296 value->v.multi_sz.strings[j]);
301 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
302 (int)value->v.binary.length);
305 result = talloc_asprintf(mem_ctx, "<unprintable>");
312 * Get the values of a key as a list of value names
313 * and a list of value strings (ordered)
315 static WERROR libnet_smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
316 struct registry_key *key,
317 uint32_t *num_values,
319 char ***value_strings)
321 TALLOC_CTX *tmp_ctx = NULL;
322 WERROR werr = WERR_OK;
324 struct registry_value *valvalue = NULL;
325 char *valname = NULL;
326 char **tmp_valnames = NULL;
327 char **tmp_valstrings = NULL;
329 if ((num_values == NULL) || (value_names == NULL) ||
330 (value_strings == NULL))
332 werr = WERR_INVALID_PARAM;
336 tmp_ctx = talloc_new(mem_ctx);
337 if (tmp_ctx == NULL) {
343 W_ERROR_IS_OK(werr = reg_enumvalue(tmp_ctx, key, count, &valname,
349 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
352 if (!W_ERROR_IS_OK(werr)) {
356 valstring = libnet_smbconf_format_registry_value(tmp_ctx,
358 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
362 if (!W_ERROR_IS_OK(werr)) {
366 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
374 *value_names = talloc_move(mem_ctx, &tmp_valnames);
375 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
378 *value_strings = NULL;
382 TALLOC_FREE(tmp_ctx);
386 /**********************************************************************
388 * The actual net conf api functions, that are exported.
390 **********************************************************************/
393 * Drop the whole configuration (restarting empty).
395 WERROR libnet_smbconf_drop(void)
398 WERROR werr = WERR_OK;
399 struct registry_key *parent_key = NULL;
400 struct registry_key *new_key = NULL;
401 TALLOC_CTX* mem_ctx = talloc_stackframe();
402 enum winreg_CreateAction action;
404 path = talloc_strdup(mem_ctx, KEY_SMBCONF);
409 p = strrchr(path, '\\');
411 werr = libnet_smbconf_reg_open_path(mem_ctx, path, REG_KEY_WRITE,
414 if (!W_ERROR_IS_OK(werr)) {
418 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
420 if (!W_ERROR_IS_OK(werr)) {
424 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
428 TALLOC_FREE(mem_ctx);
433 * Get the whole configuration as lists of strings with counts:
435 * num_shares : number of shares
436 * share_names : list of length num_shares of share names
437 * num_params : list of length num_shares of parameter counts for each share
438 * param_names : list of lists of parameter names for each share
439 * param_values : list of lists of parameter values for each share
441 WERROR libnet_smbconf_get_config(TALLOC_CTX *mem_ctx, uint32_t *num_shares,
442 char ***share_names, uint32_t **num_params,
443 char ****param_names, char ****param_values)
445 WERROR werr = WERR_OK;
446 TALLOC_CTX *tmp_ctx = NULL;
447 uint32_t tmp_num_shares;
448 char **tmp_share_names;
449 uint32_t *tmp_num_params;
450 char ***tmp_param_names;
451 char ***tmp_param_values;
454 if ((num_shares == NULL) || (share_names == NULL) ||
455 (num_params == NULL) || (param_names == NULL) ||
456 (param_values == NULL))
458 werr = WERR_INVALID_PARAM;
462 tmp_ctx = talloc_new(mem_ctx);
463 if (tmp_ctx == NULL) {
468 werr = libnet_smbconf_get_share_names(tmp_ctx, &tmp_num_shares,
470 if (!W_ERROR_IS_OK(werr)) {
474 tmp_num_params = TALLOC_ARRAY(tmp_ctx, uint32_t, tmp_num_shares);
475 tmp_param_names = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
476 tmp_param_values = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
478 if ((tmp_num_params == NULL) || (tmp_param_names == NULL) ||
479 (tmp_param_values == NULL))
485 for (count = 0; count < tmp_num_shares; count++) {
486 werr = libnet_smbconf_getshare(mem_ctx, tmp_share_names[count],
487 &tmp_num_params[count],
488 &tmp_param_names[count],
489 &tmp_param_values[count]);
490 if (!W_ERROR_IS_OK(werr)) {
497 *num_shares = tmp_num_shares;
498 if (tmp_num_shares > 0) {
499 *share_names = talloc_move(mem_ctx, &tmp_share_names);
500 *num_params = talloc_move(mem_ctx, &tmp_num_params);
501 *param_names = talloc_move(mem_ctx, &tmp_param_names);
502 *param_values = talloc_move(mem_ctx, &tmp_param_values);
507 *param_values = NULL;
511 TALLOC_FREE(tmp_ctx);
517 * get the list of share names defined in the configuration.
519 WERROR libnet_smbconf_get_share_names(TALLOC_CTX *mem_ctx, uint32_t *num_shares,
523 uint32_t added_count = 0;
524 TALLOC_CTX *tmp_ctx = NULL;
525 WERROR werr = WERR_OK;
526 struct registry_key *key = NULL;
527 char *subkey_name = NULL;
528 char **tmp_share_names = NULL;
530 if ((num_shares == NULL) || (share_names == NULL)) {
531 werr = WERR_INVALID_PARAM;
535 tmp_ctx = talloc_new(mem_ctx);
536 if (tmp_ctx == NULL) {
541 /* make sure "global" is always listed first */
542 if (libnet_smbconf_share_exists(GLOBAL_NAME)) {
543 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
546 if (!W_ERROR_IS_OK(werr)) {
552 werr = libnet_smbconf_reg_open_basekey(tmp_ctx,
553 SEC_RIGHTS_ENUM_SUBKEYS,
555 if (!W_ERROR_IS_OK(werr)) {
560 W_ERROR_IS_OK(werr = reg_enumkey(tmp_ctx, key, count,
561 &subkey_name, NULL));
564 if (strequal(subkey_name, GLOBAL_NAME)) {
568 werr = libnet_smbconf_add_string_to_array(tmp_ctx,
572 if (!W_ERROR_IS_OK(werr)) {
577 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
582 *num_shares = added_count;
583 if (added_count > 0) {
584 *share_names = talloc_move(mem_ctx, &tmp_share_names);
590 TALLOC_FREE(tmp_ctx);
595 * check if a share/service of a given name exists
597 bool libnet_smbconf_share_exists(const char *servicename)
600 WERROR werr = WERR_OK;
601 TALLOC_CTX *mem_ctx = talloc_stackframe();
602 struct registry_key *key = NULL;
604 werr = libnet_smbconf_reg_open_service_key(mem_ctx, servicename,
606 if (W_ERROR_IS_OK(werr)) {
610 TALLOC_FREE(mem_ctx);
615 * Add a service if it does not already exist.
617 WERROR libnet_smbconf_create_share(const char *servicename)
620 TALLOC_CTX *mem_ctx = talloc_stackframe();
621 struct registry_key *key = NULL;
623 if (libnet_smbconf_share_exists(servicename)) {
624 werr = WERR_ALREADY_EXISTS;
628 werr = libnet_smbconf_reg_create_service_key(mem_ctx, servicename, &key);
631 TALLOC_FREE(mem_ctx);
636 * get a definition of a share (service) from configuration.
638 WERROR libnet_smbconf_getshare(TALLOC_CTX *mem_ctx, const char *servicename,
639 uint32_t *num_params, char ***param_names,
640 char ***param_values)
642 WERROR werr = WERR_OK;
643 struct registry_key *key = NULL;
645 werr = libnet_smbconf_reg_open_service_key(mem_ctx, servicename,
647 if (!W_ERROR_IS_OK(werr)) {
651 werr = libnet_smbconf_reg_get_values(mem_ctx, key, num_params,
652 param_names, param_values);
660 * delete a service from configuration
662 WERROR libnet_smbconf_delshare(const char *servicename)
664 WERROR werr = WERR_OK;
665 struct registry_key *key = NULL;
666 TALLOC_CTX *ctx = talloc_stackframe();
668 werr = libnet_smbconf_reg_open_basekey(ctx, REG_KEY_WRITE, &key);
669 if (!W_ERROR_IS_OK(werr)) {
673 werr = reg_deletekey_recursive(key, key, servicename);
681 * set a configuration parameter to the value provided.
683 WERROR libnet_smbconf_setparm(const char *service,
688 struct registry_key *key = NULL;
689 TALLOC_CTX *mem_ctx = talloc_stackframe();
691 if (!libnet_smbconf_share_exists(service)) {
692 werr = WERR_NO_SUCH_SERVICE;
696 werr = libnet_smbconf_reg_open_service_key(mem_ctx, service,
697 REG_KEY_WRITE, &key);
698 if (!W_ERROR_IS_OK(werr)) {
702 werr = libnet_smbconf_reg_setvalue_internal(key, param, valstr);
705 TALLOC_FREE(mem_ctx);
710 * get the value of a configuration parameter as a string
712 WERROR libnet_smbconf_getparm(TALLOC_CTX *mem_ctx,
717 WERROR werr = WERR_OK;
718 struct registry_key *key = NULL;
719 struct registry_value *value = NULL;
721 if (valstr == NULL) {
722 werr = WERR_INVALID_PARAM;
726 if (!libnet_smbconf_share_exists(service)) {
727 werr = WERR_NO_SUCH_SERVICE;
731 werr = libnet_smbconf_reg_open_service_key(mem_ctx, service,
733 if (!W_ERROR_IS_OK(werr)) {
737 if (!libnet_smbconf_value_exists(key, param)) {
738 werr = WERR_INVALID_PARAM;
742 werr = reg_queryvalue(mem_ctx, key, param, &value);
743 if (!W_ERROR_IS_OK(werr)) {
747 *valstr = libnet_smbconf_format_registry_value(mem_ctx, value);
749 if (*valstr == NULL) {
760 * delete a parameter from configuration
762 WERROR libnet_smbconf_delparm(const char *service,
765 struct registry_key *key = NULL;
766 WERROR werr = WERR_OK;
767 TALLOC_CTX *mem_ctx = talloc_stackframe();
769 if (!libnet_smbconf_share_exists(service)) {
770 return WERR_NO_SUCH_SERVICE;
773 werr = libnet_smbconf_reg_open_service_key(mem_ctx, service,
775 if (!W_ERROR_IS_OK(werr)) {
779 if (!libnet_smbconf_value_exists(key, param)) {
780 werr = WERR_INVALID_PARAM;
784 werr = reg_deletevalue(key, param);
787 TALLOC_FREE(mem_ctx);
792 /**********************************************************************
794 * Convenience functions that are also exported.
796 **********************************************************************/
798 WERROR libnet_smbconf_set_global_param(const char *param,
801 return libnet_smbconf_setparm(GLOBAL_NAME, param, val);