build: fix build on systems with picky make and without shared libwbclient
[Samba.git] / source / lib / smbconf / smbconf_reg.c
blobdfce7502c58c1eac6fdca406ad69fa40a1041b51
1 /*
2 * Unix SMB/CIFS implementation.
3 * libsmbconf - Samba configuration library, registry backend
4 * Copyright (C) Michael Adam 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "smbconf_private.h"
23 #define INCLUDES_VALNAME "includes"
25 struct reg_private_data {
26 NT_USER_TOKEN *token;
27 bool open; /* did _we_ open the registry? */
30 /**********************************************************************
32 * helper functions
34 **********************************************************************/
36 /**
37 * a convenience helper to cast the private data structure
39 static struct reg_private_data *rpd(struct smbconf_ctx *ctx)
41 return (struct reg_private_data *)(ctx->data);
45 * check whether a given value name is forbidden in registry (smbconf)
47 static bool smbconf_reg_valname_forbidden(const char *valname)
49 /* hard code the list of forbidden names here for now */
50 const char *forbidden_valnames[] = {
51 "lock directory",
52 "lock dir",
53 "config backend",
54 "include",
55 NULL
57 const char **forbidden = NULL;
59 for (forbidden = forbidden_valnames; *forbidden != NULL; forbidden++) {
60 if (strwicmp(valname, *forbidden) == 0) {
61 return true;
64 return false;
67 static bool smbconf_reg_valname_valid(const char *valname)
69 return (lp_parameter_is_valid(valname) &&
70 !smbconf_reg_valname_forbidden(valname));
73 /**
74 * Open a registry key specified by "path"
76 static WERROR smbconf_reg_open_path(TALLOC_CTX *mem_ctx,
77 struct smbconf_ctx *ctx,
78 const char *path,
79 uint32 desired_access,
80 struct registry_key **key)
82 WERROR werr = WERR_OK;
84 if (ctx == NULL) {
85 DEBUG(1, ("Error: configuration is not open!\n"));
86 werr = WERR_INVALID_PARAM;
87 goto done;
90 if (rpd(ctx)->token == NULL) {
91 DEBUG(1, ("Error: token missing from smbconf_ctx. "
92 "was smbconf_init() called?\n"));
93 werr = WERR_INVALID_PARAM;
94 goto done;
97 werr = ctx->ops->open_conf(ctx);
98 if (!W_ERROR_IS_OK(werr)) {
99 DEBUG(1, ("Error opening the registry.\n"));
100 goto done;
103 if (path == NULL) {
104 DEBUG(1, ("Error: NULL path string given\n"));
105 werr = WERR_INVALID_PARAM;
106 goto done;
109 werr = reg_open_path(mem_ctx, path, desired_access, rpd(ctx)->token,
110 key);
112 if (!W_ERROR_IS_OK(werr)) {
113 DEBUG(5, ("Error opening registry path '%s': %s\n",
114 path, dos_errstr(werr)));
117 done:
118 return werr;
122 * Open a subkey of the base key (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 path = talloc_strdup(mem_ctx, ctx->path);
135 } else {
136 path = talloc_asprintf(mem_ctx, "%s\\%s", ctx->path,
137 servicename);
139 if (path == NULL) {
140 werr = WERR_NOMEM;
141 goto done;
144 werr = smbconf_reg_open_path(mem_ctx, ctx, path, desired_access, key);
146 done:
147 TALLOC_FREE(path);
148 return werr;
152 * open the base key
154 static WERROR smbconf_reg_open_base_key(TALLOC_CTX *mem_ctx,
155 struct smbconf_ctx *ctx,
156 uint32 desired_access,
157 struct registry_key **key)
159 return smbconf_reg_open_path(mem_ctx, ctx, ctx->path, desired_access,
160 key);
164 * check if a value exists in a given registry key
166 static bool smbconf_value_exists(struct registry_key *key, const char *param)
168 bool ret = false;
169 WERROR werr = WERR_OK;
170 TALLOC_CTX *ctx = talloc_stackframe();
171 struct registry_value *value = NULL;
173 werr = reg_queryvalue(ctx, key, param, &value);
174 if (W_ERROR_IS_OK(werr)) {
175 ret = true;
178 TALLOC_FREE(ctx);
179 return ret;
183 * create a subkey of the base key (i.e. a service...)
185 static WERROR smbconf_reg_create_service_key(TALLOC_CTX *mem_ctx,
186 struct smbconf_ctx *ctx,
187 const char * subkeyname,
188 struct registry_key **newkey)
190 WERROR werr = WERR_OK;
191 struct registry_key *create_parent = NULL;
192 TALLOC_CTX *create_ctx;
193 enum winreg_CreateAction action = REG_ACTION_NONE;
195 /* create a new talloc ctx for creation. it will hold
196 * the intermediate parent key (SMBCONF) for creation
197 * and will be destroyed when leaving this function... */
198 create_ctx = talloc_stackframe();
200 werr = smbconf_reg_open_base_key(create_ctx, ctx, REG_KEY_WRITE,
201 &create_parent);
202 if (!W_ERROR_IS_OK(werr)) {
203 goto done;
206 werr = reg_createkey(mem_ctx, create_parent, subkeyname,
207 REG_KEY_WRITE, newkey, &action);
208 if (W_ERROR_IS_OK(werr) && (action != REG_CREATED_NEW_KEY)) {
209 DEBUG(10, ("Key '%s' already exists.\n", subkeyname));
210 werr = WERR_ALREADY_EXISTS;
212 if (!W_ERROR_IS_OK(werr)) {
213 DEBUG(5, ("Error creating key %s: %s\n",
214 subkeyname, dos_errstr(werr)));
217 done:
218 TALLOC_FREE(create_ctx);
219 return werr;
223 * add a value to a key.
225 static WERROR smbconf_reg_set_value(struct registry_key *key,
226 const char *valname,
227 const char *valstr)
229 struct registry_value val;
230 WERROR werr = WERR_OK;
231 char *subkeyname;
232 const char *canon_valname;
233 const char *canon_valstr;
235 if (!lp_canonicalize_parameter_with_value(valname, valstr,
236 &canon_valname,
237 &canon_valstr))
239 if (canon_valname == NULL) {
240 DEBUG(5, ("invalid parameter '%s' given\n",
241 valname));
242 } else {
243 DEBUG(5, ("invalid value '%s' given for "
244 "parameter '%s'\n", valstr, valname));
246 werr = WERR_INVALID_PARAM;
247 goto done;
250 if (smbconf_reg_valname_forbidden(canon_valname)) {
251 DEBUG(5, ("Parameter '%s' not allowed in registry.\n",
252 canon_valname));
253 werr = WERR_INVALID_PARAM;
254 goto done;
257 subkeyname = strrchr_m(key->key->name, '\\');
258 if ((subkeyname == NULL) || (*(subkeyname +1) == '\0')) {
259 DEBUG(5, ("Invalid registry key '%s' given as "
260 "smbconf section.\n", key->key->name));
261 werr = WERR_INVALID_PARAM;
262 goto done;
264 subkeyname++;
265 if (!strequal(subkeyname, GLOBAL_NAME) &&
266 lp_parameter_is_global(valname))
268 DEBUG(5, ("Global paramter '%s' not allowed in "
269 "service definition ('%s').\n", canon_valname,
270 subkeyname));
271 werr = WERR_INVALID_PARAM;
272 goto done;
275 ZERO_STRUCT(val);
277 val.type = REG_SZ;
278 val.v.sz.str = CONST_DISCARD(char *, canon_valstr);
279 val.v.sz.len = strlen(canon_valstr) + 1;
281 werr = reg_setvalue(key, canon_valname, &val);
282 if (!W_ERROR_IS_OK(werr)) {
283 DEBUG(5, ("Error adding value '%s' to "
284 "key '%s': %s\n",
285 canon_valname, key->key->name, dos_errstr(werr)));
288 done:
289 return werr;
292 static WERROR smbconf_reg_set_multi_sz_value(struct registry_key *key,
293 const char *valname,
294 const uint32_t num_strings,
295 const char **strings)
297 WERROR werr;
298 struct registry_value *value;
299 uint32_t count;
300 TALLOC_CTX *tmp_ctx = talloc_stackframe();
302 if (strings == NULL) {
303 werr = WERR_INVALID_PARAM;
304 goto done;
307 value = TALLOC_ZERO_P(tmp_ctx, struct registry_value);
309 value->type = REG_MULTI_SZ;
310 value->v.multi_sz.num_strings = num_strings;
311 value->v.multi_sz.strings = TALLOC_ARRAY(tmp_ctx, char *, num_strings);
312 if (value->v.multi_sz.strings == NULL) {
313 werr = WERR_NOMEM;
314 goto done;
316 for (count = 0; count < num_strings; count++) {
317 value->v.multi_sz.strings[count] =
318 talloc_strdup(value->v.multi_sz.strings,
319 strings[count]);
320 if (value->v.multi_sz.strings[count] == NULL) {
321 werr = WERR_NOMEM;
322 goto done;
326 werr = reg_setvalue(key, valname, value);
327 if (!W_ERROR_IS_OK(werr)) {
328 DEBUG(5, ("Error adding value '%s' to key '%s': %s\n",
329 valname, key->key->name, dos_errstr(werr)));
332 done:
333 TALLOC_FREE(tmp_ctx);
334 return werr;
338 * format a registry_value into a string.
340 * This is intended to be used for smbconf registry values,
341 * which are ar stored as REG_SZ values, so the incomplete
342 * handling should be ok.
344 static char *smbconf_format_registry_value(TALLOC_CTX *mem_ctx,
345 struct registry_value *value)
347 char *result = NULL;
349 /* alternatively, create a new talloc context? */
350 if (mem_ctx == NULL) {
351 return result;
354 switch (value->type) {
355 case REG_DWORD:
356 result = talloc_asprintf(mem_ctx, "%d", value->v.dword);
357 break;
358 case REG_SZ:
359 case REG_EXPAND_SZ:
360 result = talloc_asprintf(mem_ctx, "%s", value->v.sz.str);
361 break;
362 case REG_MULTI_SZ: {
363 uint32 j;
364 for (j = 0; j < value->v.multi_sz.num_strings; j++) {
365 result = talloc_asprintf(mem_ctx, "%s\"%s\" ",
366 result ? result : "" ,
367 value->v.multi_sz.strings[j]);
368 if (result == NULL) {
369 break;
372 break;
374 case REG_BINARY:
375 result = talloc_asprintf(mem_ctx, "binary (%d bytes)",
376 (int)value->v.binary.length);
377 break;
378 default:
379 result = talloc_asprintf(mem_ctx, "<unprintable>");
380 break;
382 return result;
385 static WERROR smbconf_reg_get_includes_internal(TALLOC_CTX *mem_ctx,
386 struct registry_key *key,
387 uint32_t *num_includes,
388 char ***includes)
390 WERROR werr;
391 uint32_t count;
392 struct registry_value *value = NULL;
393 char **tmp_includes = NULL;
394 TALLOC_CTX *tmp_ctx = talloc_stackframe();
396 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
397 /* no includes */
398 *num_includes = 0;
399 *includes = NULL;
400 werr = WERR_OK;
401 goto done;
404 werr = reg_queryvalue(tmp_ctx, key, INCLUDES_VALNAME, &value);
405 if (!W_ERROR_IS_OK(werr)) {
406 goto done;
409 if (value->type != REG_MULTI_SZ) {
410 /* wront type -- ignore */
411 goto done;
414 for (count = 0; count < value->v.multi_sz.num_strings; count++)
416 werr = smbconf_add_string_to_array(tmp_ctx,
417 &tmp_includes,
418 count,
419 value->v.multi_sz.strings[count]);
420 if (!W_ERROR_IS_OK(werr)) {
421 goto done;
425 if (count > 0) {
426 *includes = talloc_move(mem_ctx, &tmp_includes);
427 if (*includes == NULL) {
428 werr = WERR_NOMEM;
429 goto done;
431 *num_includes = count;
432 } else {
433 *num_includes = 0;
434 *includes = NULL;
437 done:
438 TALLOC_FREE(tmp_ctx);
439 return werr;
443 * Get the values of a key as a list of value names
444 * and a list of value strings (ordered)
446 static WERROR smbconf_reg_get_values(TALLOC_CTX *mem_ctx,
447 struct registry_key *key,
448 uint32_t *num_values,
449 char ***value_names,
450 char ***value_strings)
452 TALLOC_CTX *tmp_ctx = NULL;
453 WERROR werr = WERR_OK;
454 uint32_t count;
455 struct registry_value *valvalue = NULL;
456 char *valname = NULL;
457 uint32_t tmp_num_values = 0;
458 char **tmp_valnames = NULL;
459 char **tmp_valstrings = NULL;
460 uint32_t num_includes = 0;
461 char **includes = NULL;
463 if ((num_values == NULL) || (value_names == NULL) ||
464 (value_strings == NULL))
466 werr = WERR_INVALID_PARAM;
467 goto done;
470 tmp_ctx = talloc_stackframe();
472 for (count = 0;
473 werr = reg_enumvalue(tmp_ctx, key, count, &valname, &valvalue),
474 W_ERROR_IS_OK(werr);
475 count++)
477 char *valstring;
479 if (!smbconf_reg_valname_valid(valname)) {
480 continue;
483 werr = smbconf_add_string_to_array(tmp_ctx,
484 &tmp_valnames,
485 tmp_num_values, valname);
486 if (!W_ERROR_IS_OK(werr)) {
487 goto done;
490 valstring = smbconf_format_registry_value(tmp_ctx, valvalue);
491 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
492 tmp_num_values, valstring);
493 if (!W_ERROR_IS_OK(werr)) {
494 goto done;
496 tmp_num_values++;
498 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
499 goto done;
502 /* now add the includes at the end */
503 werr = smbconf_reg_get_includes_internal(tmp_ctx, key, &num_includes,
504 &includes);
505 if (!W_ERROR_IS_OK(werr)) {
506 goto done;
508 for (count = 0; count < num_includes; count++) {
509 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valnames,
510 tmp_num_values, "include");
511 if (!W_ERROR_IS_OK(werr)) {
512 goto done;
515 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_valstrings,
516 tmp_num_values,
517 includes[count]);
518 if (!W_ERROR_IS_OK(werr)) {
519 goto done;
522 tmp_num_values++;
525 *num_values = tmp_num_values;
526 if (tmp_num_values > 0) {
527 *value_names = talloc_move(mem_ctx, &tmp_valnames);
528 *value_strings = talloc_move(mem_ctx, &tmp_valstrings);
529 } else {
530 *value_names = NULL;
531 *value_strings = NULL;
534 done:
535 TALLOC_FREE(tmp_ctx);
536 return werr;
539 static bool smbconf_reg_key_has_values(struct registry_key *key)
541 WERROR werr;
542 uint32_t num_subkeys;
543 uint32_t max_subkeylen;
544 uint32_t max_subkeysize;
545 uint32_t num_values;
546 uint32_t max_valnamelen;
547 uint32_t max_valbufsize;
548 uint32_t secdescsize;
549 NTTIME last_changed_time;
551 werr = reg_queryinfokey(key, &num_subkeys, &max_subkeylen,
552 &max_subkeysize, &num_values, &max_valnamelen,
553 &max_valbufsize, &secdescsize,
554 &last_changed_time);
555 if (!W_ERROR_IS_OK(werr)) {
556 return false;
559 return (num_values != 0);
563 * delete all values from a key
565 static WERROR smbconf_reg_delete_values(struct registry_key *key)
567 WERROR werr;
568 char *valname;
569 struct registry_value *valvalue;
570 uint32_t count;
571 TALLOC_CTX *mem_ctx = talloc_stackframe();
573 for (count = 0;
574 werr = reg_enumvalue(mem_ctx, key, count, &valname, &valvalue),
575 W_ERROR_IS_OK(werr);
576 count++)
578 werr = reg_deletevalue(key, valname);
579 if (!W_ERROR_IS_OK(werr)) {
580 goto done;
583 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
584 DEBUG(1, ("smbconf_reg_delete_values: "
585 "Error enumerating values of %s: %s\n",
586 key->key->name,
587 dos_errstr(werr)));
588 goto done;
591 werr = WERR_OK;
593 done:
594 TALLOC_FREE(mem_ctx);
595 return werr;
598 /**********************************************************************
600 * smbconf operations: registry implementations
602 **********************************************************************/
605 * initialize the registry smbconf backend
607 static WERROR smbconf_reg_init(struct smbconf_ctx *ctx, const char *path)
609 WERROR werr = WERR_OK;
611 if (path == NULL) {
612 path = KEY_SMBCONF;
614 ctx->path = talloc_strdup(ctx, path);
615 if (ctx->path == NULL) {
616 werr = WERR_NOMEM;
617 goto done;
620 ctx->data = TALLOC_ZERO_P(ctx, struct reg_private_data);
622 werr = ntstatus_to_werror(registry_create_admin_token(ctx,
623 &(rpd(ctx)->token)));
624 if (!W_ERROR_IS_OK(werr)) {
625 DEBUG(1, ("Error creating admin token\n"));
626 goto done;
628 rpd(ctx)->open = false;
630 werr = registry_init_smbconf(path);
631 if (!W_ERROR_IS_OK(werr)) {
632 goto done;
635 done:
636 return werr;
639 static int smbconf_reg_shutdown(struct smbconf_ctx *ctx)
641 return ctx->ops->close_conf(ctx);
644 static WERROR smbconf_reg_open(struct smbconf_ctx *ctx)
646 WERROR werr;
648 if (rpd(ctx)->open) {
649 return WERR_OK;
652 werr = regdb_open();
653 if (W_ERROR_IS_OK(werr)) {
654 rpd(ctx)->open = true;
656 return werr;
659 static int smbconf_reg_close(struct smbconf_ctx *ctx)
661 int ret;
663 if (!rpd(ctx)->open) {
664 return 0;
667 ret = regdb_close();
668 if (ret == 0) {
669 rpd(ctx)->open = false;
671 return ret;
675 * Get the change sequence number of the given service/parameter.
676 * service and parameter strings may be NULL.
678 static void smbconf_reg_get_csn(struct smbconf_ctx *ctx,
679 struct smbconf_csn *csn,
680 const char *service, const char *param)
682 if (csn == NULL) {
683 return;
686 if (!W_ERROR_IS_OK(ctx->ops->open_conf(ctx))) {
687 return;
690 csn->csn = (uint64_t)regdb_get_seqnum();
694 * Drop the whole configuration (restarting empty) - registry version
696 static WERROR smbconf_reg_drop(struct smbconf_ctx *ctx)
698 char *path, *p;
699 WERROR werr = WERR_OK;
700 struct registry_key *parent_key = NULL;
701 struct registry_key *new_key = NULL;
702 TALLOC_CTX* mem_ctx = talloc_stackframe();
703 enum winreg_CreateAction action;
705 path = talloc_strdup(mem_ctx, ctx->path);
706 if (path == NULL) {
707 werr = WERR_NOMEM;
708 goto done;
710 p = strrchr(path, '\\');
711 *p = '\0';
712 werr = smbconf_reg_open_path(mem_ctx, ctx, path, REG_KEY_WRITE,
713 &parent_key);
715 if (!W_ERROR_IS_OK(werr)) {
716 goto done;
719 werr = reg_deletekey_recursive(mem_ctx, parent_key, p+1);
721 if (!W_ERROR_IS_OK(werr)) {
722 goto done;
725 werr = reg_createkey(mem_ctx, parent_key, p+1, REG_KEY_WRITE,
726 &new_key, &action);
728 done:
729 TALLOC_FREE(mem_ctx);
730 return werr;
734 * get the list of share names defined in the configuration.
735 * registry version.
737 static WERROR smbconf_reg_get_share_names(struct smbconf_ctx *ctx,
738 TALLOC_CTX *mem_ctx,
739 uint32_t *num_shares,
740 char ***share_names)
742 uint32_t count;
743 uint32_t added_count = 0;
744 TALLOC_CTX *tmp_ctx = NULL;
745 WERROR werr = WERR_OK;
746 struct registry_key *key = NULL;
747 char *subkey_name = NULL;
748 char **tmp_share_names = NULL;
750 if ((num_shares == NULL) || (share_names == NULL)) {
751 werr = WERR_INVALID_PARAM;
752 goto done;
755 tmp_ctx = talloc_stackframe();
757 /* if there are values in the base key, return NULL as share name */
758 werr = smbconf_reg_open_base_key(tmp_ctx, ctx,
759 SEC_RIGHTS_ENUM_SUBKEYS, &key);
760 if (!W_ERROR_IS_OK(werr)) {
761 goto done;
764 if (smbconf_reg_key_has_values(key)) {
765 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
766 0, NULL);
767 if (!W_ERROR_IS_OK(werr)) {
768 goto done;
770 added_count++;
773 /* make sure "global" is always listed first */
774 if (smbconf_share_exists(ctx, GLOBAL_NAME)) {
775 werr = smbconf_add_string_to_array(tmp_ctx, &tmp_share_names,
776 added_count, GLOBAL_NAME);
777 if (!W_ERROR_IS_OK(werr)) {
778 goto done;
780 added_count++;
783 for (count = 0;
784 werr = reg_enumkey(tmp_ctx, key, count, &subkey_name, NULL),
785 W_ERROR_IS_OK(werr);
786 count++)
788 if (strequal(subkey_name, GLOBAL_NAME)) {
789 continue;
792 werr = smbconf_add_string_to_array(tmp_ctx,
793 &tmp_share_names,
794 added_count,
795 subkey_name);
796 if (!W_ERROR_IS_OK(werr)) {
797 goto done;
799 added_count++;
801 if (!W_ERROR_EQUAL(WERR_NO_MORE_ITEMS, werr)) {
802 goto done;
804 werr = WERR_OK;
806 *num_shares = added_count;
807 if (added_count > 0) {
808 *share_names = talloc_move(mem_ctx, &tmp_share_names);
809 } else {
810 *share_names = NULL;
813 done:
814 TALLOC_FREE(tmp_ctx);
815 return werr;
819 * check if a share/service of a given name exists - registry version
821 static bool smbconf_reg_share_exists(struct smbconf_ctx *ctx,
822 const char *servicename)
824 bool ret = false;
825 WERROR werr = WERR_OK;
826 TALLOC_CTX *mem_ctx = talloc_stackframe();
827 struct registry_key *key = NULL;
829 werr = smbconf_reg_open_service_key(mem_ctx, ctx, servicename,
830 REG_KEY_READ, &key);
831 if (W_ERROR_IS_OK(werr)) {
832 ret = true;
835 TALLOC_FREE(mem_ctx);
836 return ret;
840 * Add a service if it does not already exist - registry version
842 static WERROR smbconf_reg_create_share(struct smbconf_ctx *ctx,
843 const char *servicename)
845 WERROR werr;
846 TALLOC_CTX *mem_ctx = talloc_stackframe();
847 struct registry_key *key = NULL;
849 if (servicename == NULL) {
850 werr = smbconf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE,
851 &key);
852 } else {
853 werr = smbconf_reg_create_service_key(mem_ctx, ctx,
854 servicename, &key);
857 TALLOC_FREE(mem_ctx);
858 return werr;
862 * get a definition of a share (service) from configuration.
864 static WERROR smbconf_reg_get_share(struct smbconf_ctx *ctx,
865 TALLOC_CTX *mem_ctx,
866 const char *servicename,
867 struct smbconf_service **service)
869 WERROR werr = WERR_OK;
870 struct registry_key *key = NULL;
871 struct smbconf_service *tmp_service = NULL;
872 TALLOC_CTX *tmp_ctx = talloc_stackframe();
874 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, servicename,
875 REG_KEY_READ, &key);
876 if (!W_ERROR_IS_OK(werr)) {
877 goto done;
880 tmp_service = TALLOC_ZERO_P(tmp_ctx, struct smbconf_service);
881 if (tmp_service == NULL) {
882 werr = WERR_NOMEM;
883 goto done;
886 if (servicename != NULL) {
887 tmp_service->name = talloc_strdup(tmp_service, servicename);
888 if (tmp_service->name == NULL) {
889 werr = WERR_NOMEM;
890 goto done;
894 werr = smbconf_reg_get_values(tmp_service, key,
895 &(tmp_service->num_params),
896 &(tmp_service->param_names),
897 &(tmp_service->param_values));
899 if (W_ERROR_IS_OK(werr)) {
900 *service = talloc_move(mem_ctx, &tmp_service);
903 done:
904 TALLOC_FREE(tmp_ctx);
905 return werr;
909 * delete a service from configuration
911 static WERROR smbconf_reg_delete_share(struct smbconf_ctx *ctx,
912 const char *servicename)
914 WERROR werr = WERR_OK;
915 struct registry_key *key = NULL;
916 TALLOC_CTX *mem_ctx = talloc_stackframe();
918 werr = smbconf_reg_open_base_key(mem_ctx, ctx, REG_KEY_WRITE, &key);
919 if (!W_ERROR_IS_OK(werr)) {
920 goto done;
923 if (servicename != NULL) {
924 werr = reg_deletekey_recursive(key, key, servicename);
925 } else {
926 werr = smbconf_reg_delete_values(key);
929 done:
930 TALLOC_FREE(mem_ctx);
931 return werr;
935 * set a configuration parameter to the value provided.
937 static WERROR smbconf_reg_set_parameter(struct smbconf_ctx *ctx,
938 const char *service,
939 const char *param,
940 const char *valstr)
942 WERROR werr;
943 struct registry_key *key = NULL;
944 TALLOC_CTX *mem_ctx = talloc_stackframe();
946 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
947 REG_KEY_WRITE, &key);
948 if (!W_ERROR_IS_OK(werr)) {
949 goto done;
952 werr = smbconf_reg_set_value(key, param, valstr);
954 done:
955 TALLOC_FREE(mem_ctx);
956 return werr;
960 * get the value of a configuration parameter as a string
962 static WERROR smbconf_reg_get_parameter(struct smbconf_ctx *ctx,
963 TALLOC_CTX *mem_ctx,
964 const char *service,
965 const char *param,
966 char **valstr)
968 WERROR werr = WERR_OK;
969 struct registry_key *key = NULL;
970 struct registry_value *value = NULL;
972 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
973 REG_KEY_READ, &key);
974 if (!W_ERROR_IS_OK(werr)) {
975 goto done;
978 if (!smbconf_reg_valname_valid(param)) {
979 werr = WERR_INVALID_PARAM;
980 goto done;
983 if (!smbconf_value_exists(key, param)) {
984 werr = WERR_INVALID_PARAM;
985 goto done;
988 werr = reg_queryvalue(mem_ctx, key, param, &value);
989 if (!W_ERROR_IS_OK(werr)) {
990 goto done;
993 *valstr = smbconf_format_registry_value(mem_ctx, value);
995 if (*valstr == NULL) {
996 werr = WERR_NOMEM;
999 done:
1000 TALLOC_FREE(key);
1001 TALLOC_FREE(value);
1002 return werr;
1006 * delete a parameter from configuration
1008 static WERROR smbconf_reg_delete_parameter(struct smbconf_ctx *ctx,
1009 const char *service,
1010 const char *param)
1012 struct registry_key *key = NULL;
1013 WERROR werr = WERR_OK;
1014 TALLOC_CTX *mem_ctx = talloc_stackframe();
1016 werr = smbconf_reg_open_service_key(mem_ctx, ctx, service,
1017 REG_KEY_ALL, &key);
1018 if (!W_ERROR_IS_OK(werr)) {
1019 goto done;
1022 if (!smbconf_reg_valname_valid(param)) {
1023 werr = WERR_INVALID_PARAM;
1024 goto done;
1027 if (!smbconf_value_exists(key, param)) {
1028 werr = WERR_INVALID_PARAM;
1029 goto done;
1032 werr = reg_deletevalue(key, param);
1034 done:
1035 TALLOC_FREE(mem_ctx);
1036 return werr;
1039 static WERROR smbconf_reg_get_includes(struct smbconf_ctx *ctx,
1040 TALLOC_CTX *mem_ctx,
1041 const char *service,
1042 uint32_t *num_includes,
1043 char ***includes)
1045 WERROR werr;
1046 struct registry_key *key = NULL;
1047 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1049 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1050 REG_KEY_READ, &key);
1051 if (!W_ERROR_IS_OK(werr)) {
1052 goto done;
1055 werr = smbconf_reg_get_includes_internal(mem_ctx, key, num_includes,
1056 includes);
1058 done:
1059 TALLOC_FREE(tmp_ctx);
1060 return werr;
1063 static WERROR smbconf_reg_set_includes(struct smbconf_ctx *ctx,
1064 const char *service,
1065 uint32_t num_includes,
1066 const char **includes)
1068 WERROR werr = WERR_OK;
1069 struct registry_key *key = NULL;
1070 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1072 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1073 REG_KEY_ALL, &key);
1074 if (!W_ERROR_IS_OK(werr)) {
1075 goto done;
1078 if (num_includes == 0) {
1079 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
1080 goto done;
1082 werr = reg_deletevalue(key, INCLUDES_VALNAME);
1083 } else {
1084 werr = smbconf_reg_set_multi_sz_value(key, INCLUDES_VALNAME,
1085 num_includes, includes);
1088 done:
1089 TALLOC_FREE(tmp_ctx);
1090 return werr;
1093 static WERROR smbconf_reg_delete_includes(struct smbconf_ctx *ctx,
1094 const char *service)
1096 WERROR werr = WERR_OK;
1097 struct registry_key *key = NULL;
1098 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1100 werr = smbconf_reg_open_service_key(tmp_ctx, ctx, service,
1101 REG_KEY_ALL, &key);
1102 if (!W_ERROR_IS_OK(werr)) {
1103 goto done;
1106 if (!smbconf_value_exists(key, INCLUDES_VALNAME)) {
1107 goto done;
1110 werr = reg_deletevalue(key, INCLUDES_VALNAME);
1113 done:
1114 TALLOC_FREE(tmp_ctx);
1115 return werr;
1118 struct smbconf_ops smbconf_ops_reg = {
1119 .init = smbconf_reg_init,
1120 .shutdown = smbconf_reg_shutdown,
1121 .open_conf = smbconf_reg_open,
1122 .close_conf = smbconf_reg_close,
1123 .get_csn = smbconf_reg_get_csn,
1124 .drop = smbconf_reg_drop,
1125 .get_share_names = smbconf_reg_get_share_names,
1126 .share_exists = smbconf_reg_share_exists,
1127 .create_share = smbconf_reg_create_share,
1128 .get_share = smbconf_reg_get_share,
1129 .delete_share = smbconf_reg_delete_share,
1130 .set_parameter = smbconf_reg_set_parameter,
1131 .get_parameter = smbconf_reg_get_parameter,
1132 .delete_parameter = smbconf_reg_delete_parameter,
1133 .get_includes = smbconf_reg_get_includes,
1134 .set_includes = smbconf_reg_set_includes,
1135 .delete_includes = smbconf_reg_delete_includes,
1140 * initialize the smbconf registry backend
1141 * the only function that is exported from this module
1143 WERROR smbconf_init_reg(TALLOC_CTX *mem_ctx, struct smbconf_ctx **conf_ctx,
1144 const char *path)
1146 return smbconf_init_internal(mem_ctx, conf_ctx, path, &smbconf_ops_reg);