s4:registry:regdiff: use existing talloc context for the event context
[sfrench/samba-autobuild/.git] / source4 / lib / registry / interface.c
1 /*
2    Unix SMB/CIFS implementation.
3    Transparent registry backend handling
4    Copyright (C) Jelmer Vernooij                        2003-2007.
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "../lib/util/dlinklist.h"
22 #include "lib/registry/registry.h"
23 #include "system/filesys.h"
24
25
26 /**
27  * @file
28  * @brief Main registry functions
29  */
30
31 const struct reg_predefined_key reg_predefined_keys[] = {
32         {HKEY_CLASSES_ROOT,"HKEY_CLASSES_ROOT" },
33         {HKEY_CURRENT_USER,"HKEY_CURRENT_USER" },
34         {HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" },
35         {HKEY_PERFORMANCE_DATA, "HKEY_PERFORMANCE_DATA" },
36         {HKEY_USERS, "HKEY_USERS" },
37         {HKEY_CURRENT_CONFIG, "HKEY_CURRENT_CONFIG" },
38         {HKEY_DYN_DATA, "HKEY_DYN_DATA" },
39         {HKEY_PERFORMANCE_TEXT, "HKEY_PERFORMANCE_TEXT" },
40         {HKEY_PERFORMANCE_NLSTEXT, "HKEY_PERFORMANCE_NLSTEXT" },
41         { 0, NULL }
42 };
43
44 /** Obtain name of specific hkey. */
45 _PUBLIC_ const char *reg_get_predef_name(uint32_t hkey)
46 {
47         unsigned int i;
48         for (i = 0; reg_predefined_keys[i].name; i++) {
49                 if (reg_predefined_keys[i].handle == hkey)
50                         return reg_predefined_keys[i].name;
51         }
52
53         return NULL;
54 }
55
56 /** Get predefined key by name. */
57 _PUBLIC_ WERROR reg_get_predefined_key_by_name(struct registry_context *ctx,
58                                                const char *name,
59                                                struct registry_key **key)
60 {
61         unsigned int i;
62
63         for (i = 0; reg_predefined_keys[i].name; i++) {
64                 if (!strcasecmp(reg_predefined_keys[i].name, name))
65                         return reg_get_predefined_key(ctx,
66                                                       reg_predefined_keys[i].handle,
67                                                       key);
68         }
69
70         DEBUG(1, ("No predefined key with name '%s'\n", name));
71
72         return WERR_BADFILE;
73 }
74
75 /** Get predefined key by id. */
76 _PUBLIC_ WERROR reg_get_predefined_key(struct registry_context *ctx,
77                                        uint32_t hkey, struct registry_key **key)
78 {
79         return ctx->ops->get_predefined_key(ctx, hkey, key);
80 }
81
82 /**
83  * Open a key
84  * First tries to use the open_key function from the backend
85  * then falls back to get_subkey_by_name and later get_subkey_by_index
86  */
87 _PUBLIC_ WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent,
88                              const char *name, struct registry_key **result)
89 {
90         if (parent == NULL) {
91                 DEBUG(0, ("Invalid parent key specified for open of '%s'\n",
92                         name));
93                 return WERR_INVALID_PARAM;
94         }
95
96         if (parent->context->ops->open_key == NULL) {
97                 DEBUG(0, ("Registry backend doesn't have open_key!\n"));
98                 return WERR_NOT_SUPPORTED;
99         }
100
101         return parent->context->ops->open_key(mem_ctx, parent, name, result);
102 }
103
104 /**
105  * Get value by index
106  */
107 _PUBLIC_ WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx,
108                                            const struct registry_key *key,
109                                            uint32_t idx, const char **name,
110                                            uint32_t *type, DATA_BLOB *data)
111 {
112         if (key == NULL)
113                 return WERR_INVALID_PARAM;
114
115         if (key->context->ops->enum_value == NULL)
116                 return WERR_NOT_SUPPORTED;
117
118         return key->context->ops->enum_value(mem_ctx, key, idx, name,
119                                              type, data);
120 }
121
122 /**
123  * Get the number of subkeys.
124  */
125 _PUBLIC_ WERROR reg_key_get_info(TALLOC_CTX *mem_ctx,
126                                  const struct registry_key *key,
127                                  const char **classname,
128                                  uint32_t *num_subkeys,
129                                  uint32_t *num_values,
130                                  NTTIME *last_change_time,
131                                  uint32_t *max_subkeynamelen,
132                                  uint32_t *max_valnamelen,
133                                  uint32_t *max_valbufsize)
134 {
135         if (key == NULL)
136                 return WERR_INVALID_PARAM;
137
138         if (key->context->ops->get_key_info == NULL)
139                 return WERR_NOT_SUPPORTED;
140
141         return key->context->ops->get_key_info(mem_ctx,
142                                                key, classname, num_subkeys,
143                                                num_values, last_change_time,
144                                                max_subkeynamelen,
145                                                max_valnamelen, max_valbufsize);
146 }
147
148 /**
149  * Get subkey by index.
150  */
151 _PUBLIC_ WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx,
152                                             const struct registry_key *key,
153                                             uint32_t idx, const char **name,
154                                             const char **keyclass,
155                                             NTTIME *last_changed_time)
156 {
157         if (key == NULL)
158                 return WERR_INVALID_PARAM;
159
160         if (key->context->ops->enum_key == NULL)
161                 return WERR_NOT_SUPPORTED;
162
163         return key->context->ops->enum_key(mem_ctx, key, idx, name,
164                                            keyclass, last_changed_time);
165 }
166
167 /**
168  * Get value by name.
169  */
170 _PUBLIC_ WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx,
171                                           const struct registry_key *key,
172                                           const char *name,
173                                           uint32_t *type,
174                                           DATA_BLOB *data)
175 {
176         if (key == NULL)
177                 return WERR_INVALID_PARAM;
178
179         if (key->context->ops->get_value == NULL)
180                 return WERR_NOT_SUPPORTED;
181
182         return key->context->ops->get_value(mem_ctx, key, name, type, data);
183 }
184
185 /**
186  * Delete a key.
187  */
188 _PUBLIC_ WERROR reg_key_del(TALLOC_CTX *mem_ctx, struct registry_key *parent,
189                             const char *name)
190 {
191         if (parent == NULL)
192                 return WERR_INVALID_PARAM;
193
194         if (parent->context->ops->delete_key == NULL)
195                 return WERR_NOT_SUPPORTED;
196
197         return parent->context->ops->delete_key(mem_ctx, parent, name);
198 }
199
200 /**
201  * Add a key.
202  */
203 _PUBLIC_ WERROR reg_key_add_name(TALLOC_CTX *mem_ctx,
204                                  struct registry_key *parent,
205                                  const char *path, const char *key_class,
206                                  struct security_descriptor *desc,
207                                  struct registry_key **newkey)
208 {
209         if (parent == NULL)
210                 return WERR_INVALID_PARAM;
211
212         if (parent->context->ops->create_key == NULL) {
213                 DEBUG(1, ("Backend '%s' doesn't support method add_key\n",
214                                   parent->context->ops->name));
215                 return WERR_NOT_SUPPORTED;
216         }
217
218         return parent->context->ops->create_key(mem_ctx, parent, path,
219                                                 key_class, desc, newkey);
220 }
221
222 /**
223  * Set a value.
224  */
225 _PUBLIC_ WERROR reg_val_set(struct registry_key *key, const char *value,
226                             uint32_t type, const DATA_BLOB data)
227 {
228         if (key == NULL)
229                 return WERR_INVALID_PARAM;
230
231         /* A 'real' set function has preference */
232         if (key->context->ops->set_value == NULL) {
233                 DEBUG(1, ("Backend '%s' doesn't support method set_value\n",
234                                   key->context->ops->name));
235                 return WERR_NOT_SUPPORTED;
236         }
237
238         return key->context->ops->set_value(key, value, type, data);
239 }
240
241 /**
242  * Get the security descriptor on a key.
243  */
244 _PUBLIC_ WERROR reg_get_sec_desc(TALLOC_CTX *ctx,
245                                  const struct registry_key *key,
246                                  struct security_descriptor **secdesc)
247 {
248         if (key == NULL)
249                 return WERR_INVALID_PARAM;
250
251         /* A 'real' set function has preference */
252         if (key->context->ops->get_sec_desc == NULL)
253                 return WERR_NOT_SUPPORTED;
254
255         return key->context->ops->get_sec_desc(ctx, key, secdesc);
256 }
257
258 /**
259  * Delete a value.
260  */
261 _PUBLIC_ WERROR reg_del_value(TALLOC_CTX *mem_ctx, struct registry_key *key,
262                               const char *valname)
263 {
264         if (key == NULL)
265                 return WERR_INVALID_PARAM;
266
267         if (key->context->ops->delete_value == NULL)
268                 return WERR_NOT_SUPPORTED;
269
270         return key->context->ops->delete_value(mem_ctx, key, valname);
271 }
272
273 /**
274  * Flush a key to disk.
275  */
276 _PUBLIC_ WERROR reg_key_flush(struct registry_key *key)
277 {
278         if (key == NULL)
279                 return WERR_INVALID_PARAM;
280
281         if (key->context->ops->flush_key == NULL)
282                 return WERR_NOT_SUPPORTED;
283
284         return key->context->ops->flush_key(key);
285 }
286
287 _PUBLIC_ WERROR reg_set_sec_desc(struct registry_key *key,
288                                  const struct security_descriptor *security)
289 {
290         if (key == NULL)
291                 return WERR_INVALID_PARAM;
292
293         if (key->context->ops->set_sec_desc == NULL)
294                 return WERR_NOT_SUPPORTED;
295
296         return key->context->ops->set_sec_desc(key, security);
297 }