r25193: Update headers to easy use by external apps.
[kai/samba.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, 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(const 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", name));
92                 return WERR_INVALID_PARAM;
93         }
94
95         if (parent->context->ops->open_key == NULL) {
96                 DEBUG(0, ("Registry backend doesn't have open_key!\n"));
97                 return WERR_NOT_SUPPORTED;
98         }
99
100         return parent->context->ops->open_key(mem_ctx, parent, name, result);
101 }
102
103 /**
104  * Get value by index
105  */
106 _PUBLIC_ WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx, 
107                                            const struct registry_key *key, 
108                                            uint32_t idx, const char **name,
109                                            uint32_t *type, DATA_BLOB *data)
110 {
111         if (key == NULL) 
112                 return WERR_INVALID_PARAM;
113
114         if (key->context->ops->enum_value == NULL)
115                 return WERR_NOT_SUPPORTED;
116
117         return key->context->ops->enum_value(mem_ctx, key, idx, name, type, 
118                                                                                                         data);
119 }
120
121 /** 
122  * Get the number of subkeys.
123  */
124 _PUBLIC_ WERROR reg_key_get_info(TALLOC_CTX *mem_ctx, 
125                                                                  const struct registry_key *key, 
126                                                                  const char **classname,
127                                                                  uint32_t *num_subkeys,
128                                                                  uint32_t *num_values,
129                                                                  NTTIME *last_change_time)
130 {
131         if (key == NULL) 
132                 return WERR_INVALID_PARAM;
133         
134         if (key->context->ops->get_key_info == NULL)
135                 return WERR_NOT_SUPPORTED;
136
137         return key->context->ops->get_key_info(mem_ctx,
138                                                                                    key, classname, num_subkeys, 
139                                                                                    num_values, last_change_time);
140 }
141
142 /**
143  * Get subkey by index.
144  */
145 _PUBLIC_ WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx, 
146                 const struct registry_key *key, int idx, const char **name,
147                 const char **keyclass, NTTIME *last_changed_time)
148 {
149         if (key == NULL) 
150                 return WERR_INVALID_PARAM;
151
152         if (key->context->ops->enum_key == NULL)
153                 return WERR_NOT_SUPPORTED;
154
155         return key->context->ops->enum_key(mem_ctx, key, idx, name,
156                                                                                   keyclass, last_changed_time);
157 }
158
159 /**
160  * Get value by name.
161  */
162 _PUBLIC_ WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx, 
163                                                                                   const struct registry_key *key, 
164                                                                                   const char *name, 
165                                                                                   uint32_t *type,
166                                                                                   DATA_BLOB *data)
167 {
168         if (key == NULL) 
169                 return WERR_INVALID_PARAM;
170
171         if (key->context->ops->get_value == NULL)
172                 return WERR_NOT_SUPPORTED;
173
174         return key->context->ops->get_value(mem_ctx, key, name, type, data);
175 }
176
177 /**
178  * Delete a key.
179  */
180 _PUBLIC_ WERROR reg_key_del(struct registry_key *parent, const char *name)
181 {
182         if (parent == NULL) 
183                 return WERR_INVALID_PARAM;
184         
185         if (parent->context->ops->delete_key == NULL)
186                 return WERR_NOT_SUPPORTED;
187         
188         return parent->context->ops->delete_key(parent, name);
189 }
190
191 /**
192  * Add a key.
193  */
194 _PUBLIC_ WERROR reg_key_add_name(TALLOC_CTX *mem_ctx, 
195                                                                  struct registry_key *parent, 
196                                                                  const char *name, const char *key_class, 
197                                                                  struct security_descriptor *desc, 
198                                                                  struct registry_key **newkey)
199 {
200         if (parent == NULL) 
201                 return WERR_INVALID_PARAM;
202         
203         if (parent->context->ops->create_key == NULL) {
204                 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", 
205                                   parent->context->ops->name));
206                 return WERR_NOT_SUPPORTED;
207         }
208
209         return parent->context->ops->create_key(mem_ctx, parent, name, 
210                                                                                         key_class, desc, newkey);
211 }
212
213 /**
214  * Set a value.
215  */
216 _PUBLIC_ WERROR reg_val_set(struct registry_key *key, const char *value, 
217                                                         uint32_t type, const DATA_BLOB data)
218 {
219         if (key == NULL)
220                 return WERR_INVALID_PARAM;
221
222         /* A 'real' set function has preference */
223         if (key->context->ops->set_value == NULL) {
224                 DEBUG(1, ("Backend '%s' doesn't support method set_value\n", 
225                                   key->context->ops->name));
226                 return WERR_NOT_SUPPORTED;
227         }
228
229         return key->context->ops->set_value(key, value, type, data);
230 }
231
232 /**
233  * Get the security descriptor on a key.
234  */
235 _PUBLIC_ WERROR reg_get_sec_desc(TALLOC_CTX *ctx, 
236                                                                  const struct registry_key *key, 
237                                                                  struct security_descriptor **secdesc)
238 {
239         if (key == NULL)
240                 return WERR_INVALID_PARAM;
241
242         /* A 'real' set function has preference */
243         if (key->context->ops->get_security == NULL)  
244                 return WERR_NOT_SUPPORTED;
245
246         return key->context->ops->get_security(ctx, key, secdesc);
247 }
248
249 /**
250  * Delete a value.
251  */
252 _PUBLIC_ WERROR reg_del_value(struct registry_key *key, const char *valname)
253 {
254         if (key == NULL)
255                 return WERR_INVALID_PARAM;
256
257         if (key->context->ops->delete_value == NULL)
258                 return WERR_NOT_SUPPORTED;
259
260         return key->context->ops->delete_value(key, valname);
261 }
262
263 /**
264  * Flush a key to disk.
265  */
266 _PUBLIC_ WERROR reg_key_flush(struct registry_key *key)
267 {
268         if (key == NULL)
269                 return WERR_INVALID_PARAM;
270         
271         if (key->context->ops->flush_key == NULL)
272                 return WERR_NOT_SUPPORTED;
273
274         return key->context->ops->flush_key(key);
275 }
276
277 _PUBLIC_ WERROR reg_get_security(TALLOC_CTX *mem_ctx, 
278                                                                  const struct registry_key *key, 
279                                                                  struct security_descriptor **security)
280 {
281         if (key == NULL)
282                 return WERR_INVALID_PARAM;
283         
284         if (key->context->ops->get_security == NULL)
285                 return WERR_NOT_SUPPORTED;
286
287         return key->context->ops->get_security(mem_ctx, key, security);
288 }
289
290 _PUBLIC_ WERROR reg_set_security(struct registry_key *key, 
291                                                                  struct security_descriptor *security)
292 {
293         if (key == NULL)
294                 return WERR_INVALID_PARAM;
295         
296         if (key->context->ops->set_security == NULL)
297                 return WERR_NOT_SUPPORTED;
298
299         return key->context->ops->set_security(key, security);
300 }