r128: Another registry update. Changes:
[ira/wip.git] / source4 / lib / registry / common / reg_interface.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Transparent registry backend handling
4    Copyright (C) Jelmer Vernooij                        2003-2004.
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include "lib/registry/common/registry.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_REGISTRY
26
27 /* List of available backends */
28 static struct reg_init_function_entry *backends = NULL;
29
30 static struct reg_init_function_entry *reg_find_backend_entry(const char *name);
31
32 /* Register new backend */
33 NTSTATUS registry_register(void *_function)  
34 {
35         struct registry_ops *functions = _function;
36         struct reg_init_function_entry *entry = backends;
37
38         if (!functions || !functions->name) {
39                 return NT_STATUS_INVALID_PARAMETER;
40         }
41
42         DEBUG(5,("Attempting to register registry backend %s\n", functions->name));
43
44         /* Check for duplicates */
45         if (reg_find_backend_entry(functions->name)) {
46                 DEBUG(0,("There already is a registry backend registered with the name %s!\n", functions->name));
47                 return NT_STATUS_OBJECT_NAME_COLLISION;
48         }
49
50         entry = malloc(sizeof(struct reg_init_function_entry));
51         entry->functions = functions;
52
53         DLIST_ADD(backends, entry);
54         DEBUG(5,("Successfully added registry backend '%s'\n", functions->name));
55         return NT_STATUS_OK;
56 }
57
58 /* Find a backend in the list of available backends */
59 static struct reg_init_function_entry *reg_find_backend_entry(const char *name)
60 {
61         struct reg_init_function_entry *entry = backends;
62
63         while(entry) {
64                 if (strcmp(entry->functions->name, name)==0) return entry;
65                 entry = entry->next;
66         }
67
68         return NULL;
69 }
70
71 /* Open a registry file/host/etc */
72 WERROR reg_open(const char *backend, const char *location, const char *credentials, REG_HANDLE **h)
73 {
74         struct reg_init_function_entry *entry;
75         static BOOL reg_first_init = True;
76         TALLOC_CTX *mem_ctx;
77         REG_HANDLE *ret;
78         NTSTATUS status;
79         WERROR werr;
80
81         if(reg_first_init) {
82                 status = register_subsystem("registry", registry_register);
83                 if (!NT_STATUS_IS_OK(status)) 
84                         return WERR_GENERAL_FAILURE;
85
86                 static_init_reg;
87                 reg_first_init = False;
88         }
89
90         entry = reg_find_backend_entry(backend);
91         
92         if (!entry) {
93                 DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
94                 return WERR_GENERAL_FAILURE;
95         }
96         
97         mem_ctx = talloc_init(backend);
98         ret = talloc(mem_ctx, sizeof(REG_HANDLE));
99         ZERO_STRUCTP(ret);      
100         ret->location = location?talloc_strdup(mem_ctx, location):NULL;
101         ret->functions = entry->functions;
102         ret->backend_data = NULL;
103         ret->mem_ctx = mem_ctx;
104         *h = ret;
105
106         if(!entry->functions->open_registry) {
107                 return WERR_OK;
108         }
109         
110         werr = entry->functions->open_registry(ret, location, credentials);
111
112         if(W_ERROR_IS_OK(werr)) 
113                 return WERR_OK;
114
115         talloc_destroy(mem_ctx);
116         return werr;
117 }
118
119 /* Open a key 
120  * First tries to use the open_key function from the backend
121  * then falls back to get_subkey_by_name and later get_subkey_by_index 
122  */
123 WERROR reg_open_key(REG_KEY *parent, const char *name, REG_KEY **result)
124 {
125         char *fullname;
126         WERROR status;
127         REG_KEY *ret = NULL;
128         TALLOC_CTX *mem_ctx;
129
130         if(!parent) {
131                 DEBUG(0, ("Invalid parent key specified"));
132                 return WERR_INVALID_PARAM;
133         }
134
135         if(!parent->handle->functions->open_key && 
136            (parent->handle->functions->get_subkey_by_name || 
137            parent->handle->functions->get_subkey_by_index)) {
138                 char *orig = strdup(name), 
139                          *curbegin = orig, 
140                          *curend = strchr(orig, '\\');
141                 REG_KEY *curkey = parent;
142
143                 while(curbegin && *curbegin) {
144                         if(curend)*curend = '\0';
145                         status = reg_key_get_subkey_by_name(curkey, curbegin, result);
146                         if(!NT_STATUS_IS_OK(status)) {
147                                 SAFE_FREE(orig);
148                                 return status;
149                         }
150                         if(!curend) break;
151                         curbegin = curend + 1;
152                         curend = strchr(curbegin, '\\');
153                 }
154                 SAFE_FREE(orig);
155                 
156                 *result = curkey;
157                 return WERR_OK;
158         }
159
160         mem_ctx = talloc_init("mem_ctx");
161
162         fullname = talloc_asprintf(mem_ctx, "%s%s%s", parent->path, parent->path[strlen(parent->path)-1] == '\\'?"":"\\", name);
163 \
164
165         if(!parent->handle->functions->open_key) {
166                 DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
167                 return WERR_NOT_SUPPORTED;
168         }
169
170         status = parent->handle->functions->open_key(parent->handle, fullname, result);
171
172         if(!NT_STATUS_IS_OK(status)) {
173                 talloc_destroy(mem_ctx);
174                 return status;
175         }
176                 
177         ret->handle = parent->handle;
178         ret->path = fullname;
179         talloc_steal(mem_ctx, ret->mem_ctx, fullname);
180
181         talloc_destroy(mem_ctx);
182
183         *result = ret;
184
185         return WERR_OK;
186 }
187
188 WERROR reg_key_get_value_by_index(REG_KEY *key, int idx, REG_VAL **val)
189 {
190         if(!key) return WERR_INVALID_PARAM;
191
192         if(!key->handle->functions->get_value_by_index) {
193                 if(!key->cache_values)
194                         key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
195                 
196                 if(idx < key->cache_values_count && idx >= 0) {
197                         *val = reg_val_dup(key->cache_values[idx]);
198                 } else {
199                         return WERR_NO_MORE_ITEMS;
200                 }
201         } else {
202                 WERROR status = key->handle->functions->get_value_by_index(key, idx, val);
203                 if(!W_ERROR_IS_OK(status)) 
204                         return status;
205         }
206         
207         (*val)->parent = key;
208         (*val)->handle = key->handle;
209         return WERR_OK;
210 }
211
212 WERROR reg_key_num_subkeys(REG_KEY *key, int *count)
213 {
214         if(!key) return WERR_INVALID_PARAM;
215         
216         if(!key->handle->functions->num_subkeys) {
217                 if(!key->cache_subkeys) 
218                         key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
219
220                 *count = key->cache_subkeys_count;
221                 return WERR_OK;
222         }
223
224         return key->handle->functions->num_subkeys(key, count);
225 }
226
227 WERROR reg_key_num_values(REG_KEY *key, int *count)
228 {
229         
230         if(!key) return WERR_INVALID_PARAM;
231         
232         if(!key->handle->functions->num_values) {
233                 if(!key->handle->functions->fetch_values) {
234                         DEBUG(1, ("Backend '%s' doesn't support enumerating values\n", key->handle->functions->name));
235                         return WERR_NOT_SUPPORTED;
236                 }
237                 
238                 if(!key->cache_values) 
239                         key->handle->functions->fetch_values(key, &key->cache_values_count, &key->cache_values);
240
241                 *count = key->cache_values_count;
242                 return WERR_OK;
243         }
244
245         
246         return key->handle->functions->num_values(key, count);
247 }
248
249 WERROR reg_key_get_subkey_by_index(REG_KEY *key, int idx, REG_KEY **subkey)
250 {
251         if(!key) return WERR_INVALID_PARAM;
252
253         if(!key->handle->functions->get_subkey_by_index) {
254                 if(!key->cache_subkeys) 
255                         key->handle->functions->fetch_subkeys(key, &key->cache_subkeys_count, &key->cache_subkeys);
256
257                 if(idx < key->cache_subkeys_count) {
258                         *subkey = reg_key_dup(key->cache_subkeys[idx]);
259                 } else {
260                         return WERR_NO_MORE_ITEMS;
261                 }
262         } else {
263                 WERROR status = key->handle->functions->get_subkey_by_index(key, idx, subkey);
264                 if(!NT_STATUS_IS_OK(status)) return status;
265         }
266
267         (*subkey)->path = talloc_asprintf((*subkey)->mem_ctx, "%s%s%s", key->path, key->path[strlen(key->path)-1] == '\\'?"":"\\", (*subkey)->name);
268         (*subkey)->handle = key->handle;
269
270
271         return WERR_OK;;
272 }
273
274 WERROR reg_key_get_subkey_by_name(REG_KEY *key, const char *name, REG_KEY **subkey)
275 {
276         int i;
277         REG_KEY *ret = NULL;
278         WERROR error = WERR_OK;
279
280         if(!key) return WERR_INVALID_PARAM;
281
282         if(key->handle->functions->get_subkey_by_name) {
283                 error = key->handle->functions->get_subkey_by_name(key,name,subkey);
284         } else {
285                 for(i = 0; W_ERROR_IS_OK(error); i++) {
286                         error = reg_key_get_subkey_by_index(key, i, subkey);
287                         if(W_ERROR_IS_OK(error) && !strcmp((*subkey)->name, name)) {
288                                 break;
289                         }
290                         reg_key_free(*subkey);
291                 }
292
293         }
294
295         if(!W_ERROR_IS_OK(error) && W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
296                 return error;
297
298         ret->path = talloc_asprintf(ret->mem_ctx, "%s%s%s", key->path, key->path[strlen(key->path)-1] == '\\'?"":"\\", ret->name);
299         ret->handle = key->handle;
300
301         *subkey = ret;
302                 
303         return WERR_OK; 
304 }
305
306 WERROR reg_key_get_value_by_name(REG_KEY *key, const char *name, REG_VAL **val)
307 {
308         int i, max;
309         REG_VAL *ret = NULL;
310         WERROR error = WERR_OK;
311
312         if(!key) return WERR_INVALID_PARAM;
313
314         if(key->handle->functions->get_value_by_name) {
315                 error = key->handle->functions->get_value_by_name(key,name, val);
316         } else {
317                 for(i = 0; W_ERROR_IS_OK(error); i++) {
318                         error = reg_key_get_value_by_index(key, i, val);
319                         if(W_ERROR_IS_OK(error) && StrCaseCmp((*val)->name, name)) {
320                                 break;
321                         }
322                         reg_val_free(*val);
323                 }
324         }
325
326         if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
327                 return error;
328         
329         (*val)->parent = key;
330         (*val)->handle = key->handle;
331         
332         return WERR_OK;
333 }
334
335 WERROR reg_key_del(REG_KEY *key)
336 {
337         WERROR error;
338         if(!key) return WERR_INVALID_PARAM;
339         
340         
341         if(!key->handle->functions->del_key)
342                 return WERR_NOT_SUPPORTED;
343         
344         error = key->handle->functions->del_key(key);
345         if(!W_ERROR_IS_OK(error)) return error;
346
347         /* Invalidate cache */
348         key->cache_subkeys = NULL;
349         key->cache_subkeys_count = 0;
350         return WERR_OK;
351 }
352
353 WERROR reg_sync(REG_KEY *h, const char *location)
354 {
355         if(!h->handle->functions->sync_key)
356                 return WERR_OK;
357
358         return h->handle->functions->sync_key(h, location);
359 }
360
361 WERROR reg_key_del_recursive(REG_KEY *key)
362 {
363         BOOL succeed = True;
364         WERROR error = WERR_OK;
365         int i;
366         
367         /* Delete all values for specified key */
368         for(i = 0; W_ERROR_IS_OK(error); i++) {
369                 REG_VAL *val;
370                 error = reg_key_get_value_by_index(key, i, &val);
371                 if(!W_ERROR_IS_OK(error) && !W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) 
372                         return error;
373
374                 if(W_ERROR_IS_OK(error)) {
375                         error = reg_val_del(val);
376                         if(!W_ERROR_IS_OK(error)) return error;
377                 }
378         }
379
380         error = WERR_OK;
381
382         /* Delete all keys below this one */
383         for(i = 0; W_ERROR_IS_OK(error); i++) {
384                 REG_KEY *subkey;
385
386                 error = reg_key_get_subkey_by_index(key, i, &subkey);
387                 if(!W_ERROR_IS_OK(error)) return error;
388
389                 error = reg_key_del_recursive(subkey);
390                 if(!W_ERROR_IS_OK(error)) return error;
391         }
392
393         return reg_key_del(key);
394 }
395
396 WERROR reg_val_del(REG_VAL *val)
397 {
398         WERROR error;
399         if (!val) return WERR_INVALID_PARAM;
400
401         if (!val->handle->functions->del_value) {
402                 DEBUG(1, ("Backend '%s' doesn't support method del_value\n", val->handle->functions->name));
403                 return WERR_NOT_SUPPORTED;
404         }
405         
406         error = val->handle->functions->del_value(val);
407
408         if(!W_ERROR_IS_OK(error)) return error;
409
410         val->parent->cache_values = NULL;
411         val->parent->cache_values_count = 0;
412
413         return WERR_OK;
414 }
415
416 WERROR reg_key_add_name_recursive(REG_KEY *parent, const char *path)
417 {
418         REG_KEY *cur, *prevcur = parent;
419         WERROR error;
420         char *begin = (char *)path, *end;
421
422         while(1) { 
423                 end = strchr(begin, '\\');
424                 if(end) *end = '\0';
425                 
426                 error = reg_key_get_subkey_by_name(prevcur, begin, &cur);
427
428                 /* Key is not there, add it */
429                 if(W_ERROR_EQUAL(error, WERR_DEST_NOT_FOUND)) {
430                         error = reg_key_add_name(prevcur, begin, 0, NULL, &cur);
431                         if(!W_ERROR_IS_OK(error)) return error;
432                 }
433
434                 if(!W_ERROR_IS_OK(error)) {
435                         if(end) *end = '\\';
436                         return error;
437                 }
438                 
439                 if(!end) break;
440                 *end = '\\';
441                 begin = end+1;
442                 prevcur = cur;
443         }
444         return WERR_OK;
445 }
446
447 WERROR reg_key_add_name(REG_KEY *parent, const char *name, uint32 access_mask, SEC_DESC *desc, REG_KEY **newkey)
448 {
449         WERROR error;
450         
451         if (!parent) return WERR_INVALID_PARAM;
452         
453         if (!parent->handle->functions->add_key) {
454                 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->handle->functions->name));
455                 return WERR_NOT_SUPPORTED;
456         }
457
458         error = parent->handle->functions->add_key(parent, name, access_mask, desc, newkey);
459
460         if(!W_ERROR_IS_OK(error)) return error;
461         
462         (*newkey)->handle = parent->handle;
463         (*newkey)->backend_data = talloc_asprintf((*newkey)->mem_ctx, "%s\\%s", reg_key_get_path(parent), name);
464
465         parent->cache_subkeys = NULL;
466         parent->cache_subkeys_count = 0;
467         return WERR_OK;
468 }
469
470 WERROR reg_val_update(REG_VAL *val, int type, void *data, int len)
471 {
472         WERROR error;
473         
474         /* A 'real' update function has preference */
475         if (val->handle->functions->update_value) 
476                 return val->handle->functions->update_value(val, type, data, len);
477
478         /* Otherwise, just remove and add again */
479         if (val->handle->functions->add_value && 
480                 val->handle->functions->del_value) {
481                 REG_VAL *new;
482                 if(!W_ERROR_IS_OK(error = val->handle->functions->del_value(val))) 
483                         return error;
484                 
485                 error = val->handle->functions->add_value(val->parent, val->name, type, data, len);
486                 if(!W_ERROR_IS_OK(error)) return error;
487                 memcpy(val, new, sizeof(REG_VAL));
488                 val->parent->cache_values = NULL;
489                 val->parent->cache_values_count = 0;
490                 return WERR_OK;
491         }
492                 
493         DEBUG(1, ("Backend '%s' doesn't support method update_value\n", val->handle->functions->name));
494         return WERR_NOT_SUPPORTED;
495 }
496
497 void reg_free(REG_HANDLE *h)
498 {
499         if(!h->functions->close_registry) return;
500
501         h->functions->close_registry(h);
502 }
503
504 WERROR reg_get_root(REG_HANDLE *h, REG_KEY **key) 
505 {
506         WERROR ret;
507         if(h->functions->open_root_key) {
508                 ret = h->functions->open_root_key(h, key);
509         } else if(h->functions->open_key) {
510                 ret = h->functions->open_key(h, "\\", key);
511         } else {
512                 DEBUG(0, ("Backend '%s' has neither open_root_key nor open_key method implemented\n", h->functions->name));
513                 ret = WERR_NOT_SUPPORTED;
514         }
515
516         if(W_ERROR_IS_OK(ret)) {
517                 (*key)->handle = h;
518                 (*key)->path = talloc_strdup((*key)->mem_ctx, "\\");
519         }
520
521         return ret;
522 }
523
524 WERROR reg_key_add_value(REG_KEY *key, const char *name, int type, void *value, size_t vallen)
525 {
526         WERROR ret = WERR_OK;
527         if(!key->handle->functions->add_value)
528                 return WERR_NOT_SUPPORTED;
529
530         ret = key->handle->functions->add_value(key, name, type, value, vallen);
531
532         if(!W_ERROR_IS_OK(ret)) return ret;
533
534         /* Invalidate the cache */
535         key->cache_values = NULL;
536         key->cache_values_count = 0;
537         return ret;
538 }
539
540 WERROR reg_save(REG_HANDLE *h, const char *location)
541 {
542         /* FIXME */     
543         return WERR_NOT_SUPPORTED;
544 }