source: trunk/server/source3/registry/reg_api.c

Last change on this file was 751, checked in by Silvan Scherrer, 13 years ago

Samba Server: updated trunk to 3.6.9

File size: 24.3 KB
Line 
1/*
2 * Unix SMB/CIFS implementation.
3 * Virtual Windows Registry Layer
4 * Copyright (C) Volker Lendecke 2006
5 * Copyright (C) Michael Adam 2007-2010
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
21/* Attempt to wrap the existing API in a more winreg.idl-like way */
22
23/*
24 * Here is a list of winreg.idl functions and corresponding implementations
25 * provided here:
26 *
27 * 0x00 winreg_OpenHKCR
28 * 0x01 winreg_OpenHKCU
29 * 0x02 winreg_OpenHKLM
30 * 0x03 winreg_OpenHKPD
31 * 0x04 winreg_OpenHKU
32 * 0x05 winreg_CloseKey
33 * 0x06 winreg_CreateKey reg_createkey
34 * 0x07 winreg_DeleteKey reg_deletekey
35 * 0x08 winreg_DeleteValue reg_deletevalue
36 * 0x09 winreg_EnumKey reg_enumkey
37 * 0x0a winreg_EnumValue reg_enumvalue
38 * 0x0b winreg_FlushKey
39 * 0x0c winreg_GetKeySecurity reg_getkeysecurity
40 * 0x0d winreg_LoadKey
41 * 0x0e winreg_NotifyChangeKeyValue
42 * 0x0f winreg_OpenKey reg_openkey
43 * 0x10 winreg_QueryInfoKey reg_queryinfokey
44 * 0x11 winreg_QueryValue reg_queryvalue
45 * 0x12 winreg_ReplaceKey
46 * 0x13 winreg_RestoreKey reg_restorekey
47 * 0x14 winreg_SaveKey reg_savekey
48 * 0x15 winreg_SetKeySecurity reg_setkeysecurity
49 * 0x16 winreg_SetValue reg_setvalue
50 * 0x17 winreg_UnLoadKey
51 * 0x18 winreg_InitiateSystemShutdown
52 * 0x19 winreg_AbortSystemShutdown
53 * 0x1a winreg_GetVersion reg_getversion
54 * 0x1b winreg_OpenHKCC
55 * 0x1c winreg_OpenHKDD
56 * 0x1d winreg_QueryMultipleValues reg_querymultiplevalues
57 * 0x1e winreg_InitiateSystemShutdownEx
58 * 0x1f winreg_SaveKeyEx
59 * 0x20 winreg_OpenHKPT
60 * 0x21 winreg_OpenHKPN
61 * 0x22 winreg_QueryMultipleValues2 reg_querymultiplevalues
62 *
63 */
64
65#include "includes.h"
66#include "registry.h"
67#include "reg_api.h"
68#include "reg_cachehook.h"
69#include "reg_backend_db.h"
70#include "reg_dispatcher.h"
71#include "reg_objects.h"
72#include "../librpc/gen_ndr/ndr_security.h"
73
74#undef DBGC_CLASS
75#define DBGC_CLASS DBGC_REGISTRY
76
77
78/**********************************************************************
79 * Helper functions
80 **********************************************************************/
81
82static WERROR fill_value_cache(struct registry_key *key)
83{
84 WERROR werr;
85
86 if (key->values != NULL) {
87 if (!reg_values_need_update(key->key, key->values)) {
88 return WERR_OK;
89 }
90 }
91
92 TALLOC_FREE(key->values);
93 werr = regval_ctr_init(key, &(key->values));
94 W_ERROR_NOT_OK_RETURN(werr);
95
96 if (fetch_reg_values(key->key, key->values) == -1) {
97 TALLOC_FREE(key->values);
98 return WERR_BADFILE;
99 }
100
101 return WERR_OK;
102}
103
104static WERROR fill_subkey_cache(struct registry_key *key)
105{
106 WERROR werr;
107
108 if (key->subkeys != NULL) {
109 if (!reg_subkeys_need_update(key->key, key->subkeys)) {
110 return WERR_OK;
111 }
112 }
113
114 werr = regsubkey_ctr_init(key, &(key->subkeys));
115 W_ERROR_NOT_OK_RETURN(werr);
116
117 if (fetch_reg_keys(key->key, key->subkeys) == -1) {
118 TALLOC_FREE(key->subkeys);
119 return WERR_BADFILE;
120 }
121
122 return WERR_OK;
123}
124
125static int regkey_destructor(struct registry_key_handle *key)
126{
127 return regdb_close();
128}
129
130static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx,
131 struct registry_key *parent,
132 const char *name,
133 const struct security_token *token,
134 uint32 access_desired,
135 struct registry_key **pregkey)
136{
137 WERROR result = WERR_OK;
138 struct registry_key *regkey;
139 struct registry_key_handle *key;
140
141 DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
142
143 SMB_ASSERT(strchr(name, '\\') == NULL);
144
145 if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
146 !(regkey->token = dup_nt_token(regkey, token)) ||
147 !(regkey->key = TALLOC_ZERO_P(regkey, struct registry_key_handle)))
148 {
149 result = WERR_NOMEM;
150 goto done;
151 }
152
153 result = regdb_open();
154 if (!(W_ERROR_IS_OK(result))) {
155 goto done;
156 }
157
158 key = regkey->key;
159 talloc_set_destructor(key, regkey_destructor);
160
161 /* initialization */
162
163 key->type = REG_KEY_GENERIC;
164
165 if (name[0] == '\0') {
166 /*
167 * Open a copy of the parent key
168 */
169 if (!parent) {
170 result = WERR_BADFILE;
171 goto done;
172 }
173 key->name = talloc_strdup(key, parent->key->name);
174 }
175 else {
176 /*
177 * Normal subkey open
178 */
179 key->name = talloc_asprintf(key, "%s%s%s",
180 parent ? parent->key->name : "",
181 parent ? "\\": "",
182 name);
183 }
184
185 if (key->name == NULL) {
186 result = WERR_NOMEM;
187 goto done;
188 }
189
190 /* Tag this as a Performance Counter Key */
191
192 if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
193 key->type = REG_KEY_HKPD;
194
195 /* Look up the table of registry I/O operations */
196
197 key->ops = reghook_cache_find( key->name );
198 if (key->ops == NULL) {
199 DEBUG(0,("reg_open_onelevel: Failed to assign "
200 "registry_ops to [%s]\n", key->name ));
201 result = WERR_BADFILE;
202 goto done;
203 }
204
205 /* FIXME: Existence is currently checked by fetching the subkeys */
206
207 result = fill_subkey_cache(regkey);
208 if (!W_ERROR_IS_OK(result)) {
209 goto done;
210 }
211
212 if ( !regkey_access_check( key, access_desired, &key->access_granted,
213 token ) ) {
214 result = WERR_ACCESS_DENIED;
215 goto done;
216 }
217
218 *pregkey = regkey;
219 result = WERR_OK;
220
221done:
222 if ( !W_ERROR_IS_OK(result) ) {
223 TALLOC_FREE(regkey);
224 }
225
226 return result;
227}
228
229WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
230 uint32 desired_access,
231 const struct security_token *token,
232 struct registry_key **pkey)
233{
234 SMB_ASSERT(hive != NULL);
235 SMB_ASSERT(hive[0] != '\0');
236 SMB_ASSERT(strchr(hive, '\\') == NULL);
237
238 return regkey_open_onelevel(mem_ctx, NULL, hive, token, desired_access,
239 pkey);
240}
241
242
243/**********************************************************************
244 * The API functions
245 **********************************************************************/
246
247WERROR reg_openkey(TALLOC_CTX *mem_ctx, struct registry_key *parent,
248 const char *name, uint32 desired_access,
249 struct registry_key **pkey)
250{
251 struct registry_key *direct_parent = parent;
252 WERROR err;
253 char *p, *path;
254 size_t len;
255 TALLOC_CTX *frame = talloc_stackframe();
256
257 path = talloc_strdup(frame, name);
258 if (path == NULL) {
259 err = WERR_NOMEM;