s3:registry: move (commented out) hilvl util functions to reg_api_util.c
[Samba/gebeck_regimport.git] / source3 / registry / reg_api_util.c
blob592b370d3b3cbc34962cb25e3831099e85d94d9b
1 /*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Volker Lendecke 2006
5 * Copyright (C) Michael Adam 2007-2010
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 * Higher level utility functions on top of reg_api.c
25 #include "includes.h"
26 #include "registry.h"
27 #include "reg_api_util.h"
29 /**
30 * Utility function to open a complete registry path including the hive prefix.
32 WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
33 uint32 desired_access, const struct security_token *token,
34 struct registry_key **pkey)
36 struct registry_key *hive, *key;
37 char *path, *p;
38 WERROR err;
40 if (!(path = SMB_STRDUP(orig_path))) {
41 return WERR_NOMEM;
44 p = strchr(path, '\\');
46 if ((p == NULL) || (p[1] == '\0')) {
48 * No key behind the hive, just return the hive
51 err = reg_openhive(mem_ctx, path, desired_access, token,
52 &hive);
53 if (!W_ERROR_IS_OK(err)) {
54 SAFE_FREE(path);
55 return err;
57 SAFE_FREE(path);
58 *pkey = hive;
59 return WERR_OK;
62 *p = '\0';
64 err = reg_openhive(mem_ctx, path, KEY_ENUMERATE_SUB_KEYS, token,
65 &hive);
66 if (!W_ERROR_IS_OK(err)) {
67 SAFE_FREE(path);
68 return err;
71 err = reg_openkey(mem_ctx, hive, p+1, desired_access, &key);
73 TALLOC_FREE(hive);
74 SAFE_FREE(path);
76 if (!W_ERROR_IS_OK(err)) {
77 return err;
80 *pkey = key;
81 return WERR_OK;
84 #if 0
85 /* these two functions are unused. */
87 /**
88 * Utility function to create a registry key without opening the hive
89 * before. Assumes the hive already exists.
92 WERROR reg_create_path(TALLOC_CTX *mem_ctx, const char *orig_path,
93 uint32 desired_access,
94 const struct security_token *token,
95 enum winreg_CreateAction *paction,
96 struct registry_key **pkey)
98 struct registry_key *hive;
99 char *path, *p;
100 WERROR err;
102 if (!(path = SMB_STRDUP(orig_path))) {
103 return WERR_NOMEM;
106 p = strchr(path, '\\');
108 if ((p == NULL) || (p[1] == '\0')) {
110 * No key behind the hive, just return the hive
113 err = reg_openhive(mem_ctx, path, desired_access, token,
114 &hive);
115 if (!W_ERROR_IS_OK(err)) {
116 SAFE_FREE(path);
117 return err;
119 SAFE_FREE(path);
120 *pkey = hive;
121 *paction = REG_OPENED_EXISTING_KEY;
122 return WERR_OK;
125 *p = '\0';
127 err = reg_openhive(mem_ctx, path,
128 (strchr(p+1, '\\') != NULL) ?
129 KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
130 token, &hive);
131 if (!W_ERROR_IS_OK(err)) {
132 SAFE_FREE(path);
133 return err;
136 err = reg_createkey(mem_ctx, hive, p+1, desired_access, pkey, paction);
137 SAFE_FREE(path);
138 TALLOC_FREE(hive);
139 return err;
143 * Utility function to create a registry key without opening the hive
144 * before. Will not delete a hive.
147 WERROR reg_delete_path(const struct security_token *token,
148 const char *orig_path)
150 struct registry_key *hive;
151 char *path, *p;
152 WERROR err;
154 if (!(path = SMB_STRDUP(orig_path))) {
155 return WERR_NOMEM;
158 p = strchr(path, '\\');
160 if ((p == NULL) || (p[1] == '\0')) {
161 SAFE_FREE(path);
162 return WERR_INVALID_PARAM;
165 *p = '\0';
167 err = reg_openhive(NULL, path,
168 (strchr(p+1, '\\') != NULL) ?
169 KEY_ENUMERATE_SUB_KEYS : KEY_CREATE_SUB_KEY,
170 token, &hive);
171 if (!W_ERROR_IS_OK(err)) {
172 SAFE_FREE(path);
173 return err;
176 err = reg_deletekey(hive, p+1);
177 SAFE_FREE(path);
178 TALLOC_FREE(hive);
179 return err;
181 #endif /* #if 0 */