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