libsmbconf: refactor autocreation of [global] into a helper function.
[Samba.git] / source / lib / smbconf / smbconf.c
blobdd044eef818008e29d7866079765b29ef1b283fb
1 /*
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/>.
21 #include "includes.h"
22 #include "smbconf_private.h"
24 /**********************************************************************
26 * Helper functions (mostly registry related)
28 **********************************************************************/
30 /**
31 * add a string to a talloced array of strings.
33 static WERROR smbconf_add_string_to_array(TALLOC_CTX *mem_ctx,
34 char ***array,
35 uint32_t count,
36 const char *string)
38 char **new_array = NULL;
40 if ((array == NULL) || (string == NULL)) {
41 return WERR_INVALID_PARAM;
44 new_array = TALLOC_REALLOC_ARRAY(mem_ctx, *array, char *, count + 1);
45 if (new_array == NULL) {
46 return WERR_NOMEM;
49 new_array[count] = talloc_strdup(new_array, string);
50 if (new_array[count] == NULL) {
51 TALLOC_FREE(new_array);
52 return WERR_NOMEM;
55 *array = new_array;
57 return WERR_OK;
60 static WERROR smbconf_reg_initialize(struct smbconf_ctx *ctx)
62 WERROR werr = WERR_OK;
64 if (!registry_init_smbconf()) {
65 werr = WERR_REG_IO_FAILURE;
66 goto done;
69 werr = ntstatus_to_werror(registry_create_admin_token(ctx,
70 &(ctx->token)));
71 if (!W_ERROR_IS_OK(werr)) {
72 DEBUG(1, ("Error creating admin token\n"));
73 goto done;
76 done:
77 return werr;
80 /**
81 * Open a registry key specified by "path"
83 static WERROR smbconf_reg_open_path(TALLOC_CTX *mem_ctx,
84 struct smbconf_ctx *ctx,
85 const char *path,
86 uint32 desired_access,
87 struct registry_key **key)
89 WERROR werr = WERR_OK;
91 if (ctx == NULL) {
92 DEBUG(1, ("Error: configuration is not open!\n"));
93 werr = WERR_INVALID_PARAM;
94 goto done;
97 if (ctx->token == NULL) {
98 DEBUG(1, ("Error: token missing from smbconf_ctx. "
99 "was smbconf_open() called?\n"));
100 werr = WERR_INVALID_PARAM;
101 goto done;
104 if (path == NULL) {
105 DEBUG(1, ("Error: NULL path string given\n"));
106 werr = WERR_INVALID_PARAM;
107 goto done;
110 werr = reg_open_path(mem_ctx, path, desired_access, ctx->token, key);
112 if (!W_ERROR_IS_OK(werr)) {
113 DEBUG(1, ("Error opening registry path '%s': %s\n",
114 path, dos_errstr(werr)));
117 done:
118 return werr;
122 * Open a subkey of KEY_SMBCONF (i.e a service)
124 static WERROR smbconf_reg_open_service_key(TALLOC_CTX *mem_ctx,
125 struct smbconf_ctx *ctx,
126 const char *servicename,
127 uint32 desired_access,
128 struct registry_key **key)
130 WERROR werr = WERR_OK;
131 char *path = NULL;
133 if (servicename == NULL) {
134 DEBUG(3, ("Error: NULL servicename given.\n"));
135 werr = WERR_INVALID_PARAM;
136 goto done;
139 path = talloc_asprintf(mem_ctx, "%s\\%s", KEY_SMBCONF, servicename);
140 if (path == NULL) {
141 werr = WERR_NOMEM;
142 goto done;
145 werr = smbconf_reg_open_path(mem_ctx, ctx, path, desired_access, key);
147 done:
148 TALLOC_FREE(path);
149 return werr;
153 * open the base key KEY_SMBCONF
155 static WERROR smbconf_reg_open_base_key(TALLOC_CTX *mem_ctx,
156 struct smbconf_ctx *ctx,
157 uint32 desired_access,
158 struct registry_key **key)
160 return smbconf_reg_open_path(mem_ctx, ctx, KEY_SMBCONF, desired_access,
161 key);
165 * check if a value exists in a given registry key
167 static bool smbconf_value_exists(struct registry_key *key, const char *param)
169 bool ret = false;
170 WERROR werr = WERR_OK;
171 TALLOC_CTX *ctx = talloc_stackframe();
172 struct registry_value *value = NULL;
174 werr = reg_queryvalue(ctx, key, param, &value);
175 if (W_ERROR_IS_OK(werr)) {
176 ret = true;
179 TALLOC_FREE(ctx);
180 return ret;
184 * create a subkey of KEY_SMBCONF
186 static WERROR smbconf_reg_create_service_key(TALLOC_CTX *mem_ctx,
187 struct smbconf_ctx *ctx,
188 const char * subkeyname,
189 struct registry_key **newkey)
191 WERROR werr = WERR_OK;
192 struct registry_key *create_parent = NULL;
193 TALLOC_CTX *create_ctx;
194 enum winreg_CreateAction action = REG_ACTION_NONE;
196 /* create a new talloc ctx for creation. it will hold
197 * the intermediate parent key (SMBCONF) for creation
198 * and will be destroyed when leaving this function... */
199 if (!(create_ctx = talloc_stackframe())) {
200 werr = WERR_NOMEM;
201 goto done;
204 werr = smbconf_reg_open_base_key(create_ctx, ctx, REG_KEY_WRITE,
205 &create_parent);
206 if (!W_ERROR_IS_OK(werr)) {
207 goto done;
210 werr = reg_createkey(mem_ctx, create_parent, subkeyname,
211 REG_KEY_WRITE, newkey, &action);
212 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
213 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
214 werr = WERR_ALREADY_EXISTS;
216 if (!W_ERROR_IS_OK(werr)) {
217 DEBUG(5, ("Error creating key %s: %s\n",
218 subkeyname, dos_errstr(werr)));
221 done:
222 TALLOC_FREE(create_ctx);
223 return werr;
227 * add a value to a key.
229 static WERROR smbconf_reg_set_value(struct registry_key *key,
230 const char *valname,
231 const char *valstr)
233 struct registry_value val;
234 WERROR werr = WERR_OK;
235 char *subkeyname;
236 const char *canon_valname;
237 const char *canon_valstr;
239 if (!lp_canonicalize_parameter_with_value(valname, valstr,
240 &canon_valname,
241 &canon_valstr))
243 if (canon_valname == NULL) {
244 DEBUG(5, ("invalid parameter '%s' given\n",
245 valname));
246 } else {
247 DEBUG(5, ("invalid value '%s' given for "
248 "parameter '%s'\n", valstr, valname));
250 werr = WERR_INVALID_PARAM;
251 goto done;
254 ZERO_STRUCT(val);
256 val.type = REG_SZ;
257 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
258 val.v.sz.len = strlen(canon_valstr) + 1;
260 if (registry_smbconf_valname_forbidden(canon_valname)) {
261 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
262 canon_valname));
263 werr = WERR_INVALID_PARAM;
264 goto done;
267 subkeyname = strrchr_m(key->key->name, '\\');
268 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
269 DEBUG(5, ("Invalid registry key '%s' given as "
270 "smbconf section.\n", key->key->name));
271 werr = WERR_INVALID_PARAM;
272 goto done;
274 subkeyname++;
275 if (!strequal(subkeyname, GLOBAL_NAME) &&
276 lp_parameter_is_global(valname))
278 DEBUG(5, ("Global paramter '%s' not allowed in "
279 "service definition ('%s').\n", canon_valname,
280 subkeyname));
281 werr = WERR_INVALID_PARAM;
282 goto done;
285 werr = reg_setvalue(key, canon_valname, &val);
286 if (!W_ERROR_IS_OK(werr)) {
287 DEBUG(5, ("Error adding value '%s' to "
288 "key '%s': %s\n",
289 canon_valname, key->key->name, dos_errstr(werr)));
292 done:
293 return werr;
297 * format a registry_value into a string.
299 * This is intended to be used for smbconf registry values,
300 * which are ar stored as REG_SZ values, so the incomplete
301 * handling should be ok.
303 static char *smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
304 struct registry_value *value)
306 char *result = NULL;
308 /* alternatively, create a new talloc context? */
309 if (mem_ctx == NULL) {
310 return result;
313 switch (value->type) {
314 case REG_DWORD:
315 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
316 break;
317 case REG_SZ:
318 case REG_EXPAND_SZ:
319 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
320 break;
321 case REG_MULTI_SZ: {
322 uint32 j;
323 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
324 result = talloc_asprintf(mem_ctx, "%s \"%s\" ",
325 result,
326 value->v.multi_sz.strings[j]);
327 if (result == NULL) {
328 break;
331 break;
333 case REG_BINARY:
334 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
335 (int)value->v.binary.length);
336 break;
337 default:
338 result = talloc_asprintf(mem_ctx, "<unprintable>");
339 break;
341 return result;
345 * Get the values of a key as a list of value names
346 * and a list of value strings (ordered)
348 static WERROR smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
349 struct registry_key *key,
350 uint32_t *num_values,
351 char ***value_names,
352 char ***value_strings)
354 TALLOC_CTX *tmp_ctx = NULL;
355 WERROR werr = WERR_OK;
356 uint32_t count;
357 struct registry_value *valvalue = NULL;
358 char *valname = NULL;
359 char **tmp_valnames = NULL;
360 char **tmp_valstrings = NULL;
362 if ((num_values == NULL) || (value_names == NULL) ||
363 (value_strings == NULL))
365 werr = WERR_INVALID_PARAM;
366 goto done;
369 tmp_ctx = talloc_stackframe();
370 if (tmp_ctx == NULL) {
371 werr = WERR_NOMEM;
372 goto done;
375 for (count = 0;
376 W_ERROR_IS_OK(werr = reg_enumvalue(tmp_ctx, key, count, &valname,
377 &valvalue));
378 count++)
380 char *valstring;
382 werr = smbconf_add_string_to_array(tmp_ctx,
383 &tmp_valnames,
384 count, valname);
385 if (!W_ERROR_IS_OK(werr)) {
386 goto done;
389 valstring = smbconf_format_registry_value(tmp_ctx, valvalue);
390 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
391 count, valstring);
392 if (!W_ERROR_IS_OK(werr)) {
393 goto done;
396 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
397 goto done;
400 werr = WERR_OK;
402 *num_values = count;
403 if (count > 0) {
404 *value_names = talloc_move(mem_ctx, &tmp_valnames);
405 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
406 } else {
407 *value_names = NULL;
408 *value_strings = NULL;
411 done:
412 TALLOC_FREE(tmp_ctx);
413 return werr;
416 static int smbconf_destroy_ctx(struct smbconf_ctx *ctx)
418 return regdb_close();
422 * Get the change sequence number of the given service/parameter.
423 * service and parameter strings may be NULL.
425 static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
426 struct smbconf_csn *csn,
427 const char *service, const char *param)
429 if (csn == NULL) {
430 return;
432 csn->csn = (uint64_t)regdb_get_seqnum();
435 static WERROR smbconf_global_check(struct smbconf_ctx *ctx)
437 if (!smbconf_share_exists(ctx, GLOBAL_NAME)) {
438 return smbconf_create_share(ctx, GLOBAL_NAME);
440 return WERR_OK;
443 /**********************************************************************
445 * The actual net conf api functions, that are exported.
447 **********************************************************************/
450 * Open the configuration.
452 * This should be the first function in a sequence of calls to smbconf
453 * functions:
455 * Upon success, this creates and returns the conf context
456 * that should be passed around in subsequent calls to the other
457 * smbconf functions.
459 * After the work with the configuration is completed, smbconf_close()
460 * should be called.
462 WERROR smbconf_open(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx)
464 WERROR werr = WERR_OK;
465 struct smbconf_ctx *ctx;
467 if (conf_ctx == NULL) {
468 return WERR_INVALID_PARAM;
471 ctx = TALLOC_ZERO_P(mem_ctx, struct smbconf_ctx);
472 if (ctx == NULL) {
473 return WERR_NOMEM;
476 werr = smbconf_reg_initialize(ctx);
477 if (!W_ERROR_IS_OK(werr)) {
478 goto fail;
481 talloc_set_destructor(ctx, smbconf_destroy_ctx);
483 *conf_ctx = ctx;
484 return werr;
486 fail:
487 TALLOC_FREE(ctx);
488 return werr;
492 * Close the configuration.
494 void smbconf_close(struct smbconf_ctx *ctx)
496 /* this also closes the registry (by destructor): */
497 TALLOC_FREE(ctx);
501 * Detect changes in the configuration.
502 * The given csn struct is filled with the current csn.
503 * smbconf_changed() can also be used for initial retrieval
504 * of the csn.
506 bool smbconf_changed(struct smbconf_ctx *ctx, struct smbconf_csn *csn,
507 const char *service, const char *param)
509 struct smbconf_csn old_csn;
511 if (csn == NULL) {
512 return false;
515 old_csn = *csn;
517 smbconf_reg_get_csn(ctx, csn, service, param);
518 return (csn->csn != old_csn.csn);
522 * Drop the whole configuration (restarting empty).
524 WERROR smbconf_drop(struct smbconf_ctx *ctx)
526 char *path, *p;
527 WERROR werr = WERR_OK;
528 struct registry_key *parent_key = NULL;
529 struct registry_key *new_key = NULL;
530 TALLOC_CTX* mem_ctx = talloc_stackframe();
531 enum winreg_CreateAction action;
533 path = talloc_strdup(mem_ctx, KEY_SMBCONF);
534 if (path == NULL) {
535 werr = WERR_NOMEM;
536 goto done;
538 p = strrchr(path, '\\');
539 *p = '\0';
540 werr = smbconf_reg_open_path(mem_ctx, ctx, path, REG_KEY_WRITE,
541 &parent_key);
543 if (!W_ERROR_IS_OK(werr)) {
544 goto done;
547 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
549 if (!W_ERROR_IS_OK(werr)) {
550 goto done;
553 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
554 &new_key, &action);
556 done:
557 TALLOC_FREE(mem_ctx);
558 return werr;
562 * Get the whole configuration as lists of strings with counts:
564 * num_shares : number of shares
565 * share_names : list of length num_shares of share names
566 * num_params : list of length num_shares of parameter counts for each share
567 * param_names : list of lists of parameter names for each share
568 * param_values : list of lists of parameter values for each share
570 WERROR smbconf_get_config(struct smbconf_ctx *ctx,
571 TALLOC_CTX *mem_ctx,
572 uint32_t *num_shares,
573 char ***share_names, uint32_t **num_params,
574 char ****param_names, char ****param_values)
576 WERROR werr = WERR_OK;
577 TALLOC_CTX *tmp_ctx = NULL;
578 uint32_t tmp_num_shares;
579 char **tmp_share_names;
580 uint32_t *tmp_num_params;
581 char ***tmp_param_names;
582 char ***tmp_param_values;
583 uint32_t count;
585 if ((num_shares == NULL) || (share_names == NULL) ||
586 (num_params == NULL) || (param_names == NULL) ||
587 (param_values == NULL))
589 werr = WERR_INVALID_PARAM;
590 goto done;
593 tmp_ctx = talloc_stackframe();
594 if (tmp_ctx == NULL) {
595 werr = WERR_NOMEM;
596 goto done;
599 werr = smbconf_get_share_names(ctx, tmp_ctx, &tmp_num_shares,
600 &tmp_share_names);
601 if (!W_ERROR_IS_OK(werr)) {
602 goto done;
605 tmp_num_params = TALLOC_ARRAY(tmp_ctx, uint32_t, tmp_num_shares);
606 tmp_param_names = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
607 tmp_param_values = TALLOC_ARRAY(tmp_ctx, char **, tmp_num_shares);
609 if ((tmp_num_params == NULL) || (tmp_param_names == NULL) ||
610 (tmp_param_values == NULL))
612 werr = WERR_NOMEM;
613 goto done;
616 for (count = 0; count < tmp_num_shares; count++) {
617 werr = smbconf_get_share(ctx, mem_ctx,
618 tmp_share_names[count],
619 &tmp_num_params[count],
620 &tmp_param_names[count],
621 &tmp_param_values[count]);
622 if (!W_ERROR_IS_OK(werr)) {
623 goto done;
627 werr = WERR_OK;
629 *num_shares = tmp_num_shares;
630 if (tmp_num_shares > 0) {
631 *share_names = talloc_move(mem_ctx, &tmp_share_names);
632 *num_params = talloc_move(mem_ctx, &tmp_num_params);
633 *param_names = talloc_move(mem_ctx, &tmp_param_names);
634 *param_values = talloc_move(mem_ctx, &tmp_param_values);
635 } else {
636 *share_names = NULL;
637 *num_params = NULL;
638 *param_names = NULL;
639 *param_values = NULL;
642 done:
643 TALLOC_FREE(tmp_ctx);
644 return werr;
648 * get the list of share names defined in the configuration.
650 WERROR smbconf_get_share_names(struct smbconf_ctx *ctx,
651 TALLOC_CTX *mem_ctx,
652 uint32_t *num_shares,
653 char ***share_names)
655 uint32_t count;
656 uint32_t added_count = 0;
657 TALLOC_CTX *tmp_ctx = NULL;
658 WERROR werr = WERR_OK;
659 struct registry_key *key = NULL;
660 char *subkey_name = NULL;
661 char **tmp_share_names = NULL;
663 if ((num_shares == NULL) || (share_names == NULL)) {
664 werr = WERR_INVALID_PARAM;
665 goto done;
668 tmp_ctx = talloc_stackframe();
669 if (tmp_ctx == NULL) {
670 werr = WERR_NOMEM;
671 goto done;
674 /* make sure "global" is always listed first */
675 if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
676 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
677 0, GLOBAL_NAME);
678 if (!W_ERROR_IS_OK(werr)) {
679 goto done;
681 added_count++;
684 werr = smbconf_reg_open_base_key(tmp_ctx, ctx,
685 SEC_RIGHTS_ENUM_SUBKEYS, &key);
686 if (!W_ERROR_IS_OK(werr)) {
687 goto done;
690 for (count = 0;
691 W_ERROR_IS_OK(werr = reg_enumkey(tmp_ctx, key, count,
692 &subkey_name, NULL));
693 count++)
695 if (strequal(subkey_name, GLOBAL_NAME)) {
696 continue;
699 werr = smbconf_add_string_to_array(tmp_ctx,
700 &tmp_share_names,
701 added_count,
702 subkey_name);
703 if (!W_ERROR_IS_OK(werr)) {
704 goto done;
706 added_count++;
708 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
709 goto done;
711 werr = WERR_OK;
713 *num_shares = added_count;
714 if (added_count > 0) {
715 *share_names = talloc_move(mem_ctx, &tmp_share_names);
716 } else {
717 *share_names = NULL;
720 done:
721 TALLOC_FREE(tmp_ctx);
722 return werr;
726 * check if a share/service of a given name exists
728 bool smbconf_share_exists(struct smbconf_ctx *ctx,
729 const char *servicename)
731 bool ret = false;
732 WERROR werr = WERR_OK;
733 TALLOC_CTX *mem_ctx = talloc_stackframe();
734 struct registry_key *key = NULL;
736 werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
737 REG_KEY_READ, &key);
738 if (W_ERROR_IS_OK(werr)) {
739 ret = true;
742 TALLOC_FREE(mem_ctx);
743 return ret;
747 * Add a service if it does not already exist.
749 WERROR smbconf_create_share(struct smbconf_ctx *ctx,
750 const char *servicename)
752 WERROR werr;
753 TALLOC_CTX *mem_ctx = talloc_stackframe();
754 struct registry_key *key = NULL;
756 if (smbconf_share_exists(ctx, servicename)) {
757 werr = WERR_ALREADY_EXISTS;
758 goto done;
761 werr = smbconf_reg_create_service_key(mem_ctx, ctx, servicename, &key);
763 done:
764 TALLOC_FREE(mem_ctx);
765 return werr;
769 * get a definition of a share (service) from configuration.
771 WERROR smbconf_get_share(struct smbconf_ctx *ctx,
772 TALLOC_CTX *mem_ctx,
773 const char *servicename, uint32_t *num_params,
774 char ***param_names, char ***param_values)
776 WERROR werr = WERR_OK;
777 struct registry_key *key = NULL;
779 werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
780 REG_KEY_READ, &key);
781 if (!W_ERROR_IS_OK(werr)) {
782 goto done;
785 werr = smbconf_reg_get_values(mem_ctx, key, num_params,
786 param_names, param_values);
788 done:
789 TALLOC_FREE(key);
790 return werr;
794 * delete a service from configuration
796 WERROR smbconf_delete_share(struct smbconf_ctx *ctx, const char *servicename)
798 WERROR werr = WERR_OK;
799 struct registry_key *key = NULL;
800 TALLOC_CTX *mem_ctx = talloc_stackframe();
802 werr = smbconf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE, &key);
803 if (!W_ERROR_IS_OK(werr)) {
804 goto done;
807 werr = reg_deletekey_recursive(key, key, servicename);
809 done:
810 TALLOC_FREE(mem_ctx);
811 return werr;
815 * set a configuration parameter to the value provided.
817 WERROR smbconf_set_parameter(struct smbconf_ctx *ctx,
818 const char *service,
819 const char *param,
820 const char *valstr)
822 WERROR werr;
823 struct registry_key *key = NULL;
824 TALLOC_CTX *mem_ctx = talloc_stackframe();
826 if (!smbconf_share_exists(ctx, service)) {
827 werr = WERR_NO_SUCH_SERVICE;
828 goto done;
831 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
832 REG_KEY_WRITE, &key);
833 if (!W_ERROR_IS_OK(werr)) {
834 goto done;
837 werr = smbconf_reg_set_value(key, param, valstr);
839 done:
840 TALLOC_FREE(mem_ctx);
841 return werr;
845 * Set a global parameter
846 * (i.e. a parameter in the [global] service).
848 * This also creates [global] when it does not exist.
850 WERROR smbconf_set_global_parameter(struct smbconf_ctx *ctx,
851 const char *param, const char *val)
853 WERROR werr;
855 werr = smbconf_global_check(ctx);
856 if (W_ERROR_IS_OK(werr)) {
857 werr = smbconf_set_parameter(ctx, GLOBAL_NAME, param, val);
860 return werr;
864 * get the value of a configuration parameter as a string
866 WERROR smbconf_get_parameter(struct smbconf_ctx *ctx,
867 TALLOC_CTX *mem_ctx,
868 const char *service,
869 const char *param,
870 char **valstr)
872 WERROR werr = WERR_OK;
873 struct registry_key *key = NULL;
874 struct registry_value *value = NULL;
876 if (valstr == NULL) {
877 werr = WERR_INVALID_PARAM;
878 goto done;
881 if (!smbconf_share_exists(ctx, service)) {
882 werr = WERR_NO_SUCH_SERVICE;
883 goto done;
886 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
887 REG_KEY_READ, &key);
888 if (!W_ERROR_IS_OK(werr)) {
889 goto done;
892 if (!smbconf_value_exists(key, param)) {
893 werr = WERR_INVALID_PARAM;
894 goto done;
897 werr = reg_queryvalue(mem_ctx, key, param, &value);
898 if (!W_ERROR_IS_OK(werr)) {
899 goto done;
902 *valstr = smbconf_format_registry_value(mem_ctx, value);
904 if (*valstr == NULL) {
905 werr = WERR_NOMEM;
908 done:
909 TALLOC_FREE(key);
910 TALLOC_FREE(value);
911 return werr;
915 * Get the value of a global parameter.
917 * Create [global] if it does not exist.
919 WERROR smbconf_get_global_parameter(struct smbconf_ctx *ctx,
920 TALLOC_CTX *mem_ctx,
921 const char *param,
922 char **valstr)
924 WERROR werr;
926 werr = smbconf_global_check(ctx);
927 if (W_ERROR_IS_OK(werr)) {
928 werr = smbconf_get_parameter(ctx, mem_ctx, GLOBAL_NAME, param,
929 valstr);
932 return werr;
936 * delete a parameter from configuration
938 WERROR smbconf_delete_parameter(struct smbconf_ctx *ctx,
939 const char *service, const char *param)
941 struct registry_key *key = NULL;
942 WERROR werr = WERR_OK;
943 TALLOC_CTX *mem_ctx = talloc_stackframe();
945 if (!smbconf_share_exists(ctx, service)) {
946 return WERR_NO_SUCH_SERVICE;
949 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
950 REG_KEY_ALL, &key);
951 if (!W_ERROR_IS_OK(werr)) {
952 goto done;
955 if (!smbconf_value_exists(key, param)) {
956 werr = WERR_INVALID_PARAM;
957 goto done;
960 werr = reg_deletevalue(key, param);
962 done:
963 TALLOC_FREE(mem_ctx);
964 return werr;
968 * Delete a global parameter.
970 * Create [global] if it does not exist.
972 WERROR smbconf_delete_global_parameter(struct smbconf_ctx *ctx,
973 const char *param)
975 WERROR werr;
977 werr = smbconf_global_check(ctx);
978 if (W_ERROR_IS_OK(werr)) {
979 werr = smbconf_delete_parameter(ctx, GLOBAL_NAME, param);
982 return werr;