r23792: convert Samba4 to GPLv3
[samba.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 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 "build.h"
24
25 /**
26  * @file
27  * @brief Main registry functions
28  */
29
30 /* List of available backends */
31 static struct reg_init_function_entry *backends = NULL;
32
33 static struct reg_init_function_entry *reg_find_backend_entry(const char *name);
34
35 /** Register a new backend. */
36 _PUBLIC_ NTSTATUS registry_register(const void *_hive_ops)
37 {
38         const struct hive_operations *hive_ops = _hive_ops;
39         struct reg_init_function_entry *entry = backends;
40
41         DEBUG(5,("Attempting to register registry backend %s\n", hive_ops->name));
42
43         /* Check for duplicates */
44         if (reg_find_backend_entry(hive_ops->name)) {
45                 DEBUG(0,("There already is a registry backend registered with the name %s!\n", hive_ops->name));
46                 return NT_STATUS_OBJECT_NAME_COLLISION;
47         }
48
49         entry = talloc(talloc_autofree_context(), struct reg_init_function_entry);
50         entry->hive_functions = hive_ops;
51
52         DLIST_ADD(backends, entry);
53         DEBUG(5,("Successfully added registry backend '%s'\n", hive_ops->name));
54         return NT_STATUS_OK;
55 }
56
57 /** Find a backend in the list of available backends */
58 static struct reg_init_function_entry *reg_find_backend_entry(const char *name)
59 {
60         struct reg_init_function_entry *entry;
61
62         entry = backends;
63
64         while(entry) {
65                 if (strcmp(entry->hive_functions->name, name) == 0) return entry;
66                 entry = entry->next;
67         }
68
69         return NULL;
70 }
71
72 /** Initialize the registry subsystem */
73 _PUBLIC_ NTSTATUS registry_init(void)
74 {
75         init_module_fn static_init[] = STATIC_registry_MODULES;
76         init_module_fn *shared_init = load_samba_modules(NULL, "registry");
77
78         run_init_functions(static_init);
79         run_init_functions(shared_init);
80
81         talloc_free(shared_init);
82         
83         return NT_STATUS_OK;
84 }
85
86 /** Check whether a certain backend is present. */
87 _PUBLIC_ BOOL reg_has_backend(const char *backend)
88 {
89         return reg_find_backend_entry(backend) != NULL?True:False;
90 }
91
92 const struct reg_predefined_key reg_predefined_keys[] = {
93         {HKEY_CLASSES_ROOT,"HKEY_CLASSES_ROOT" },
94         {HKEY_CURRENT_USER,"HKEY_CURRENT_USER" },
95         {HKEY_LOCAL_MACHINE, "HKEY_LOCAL_MACHINE" },
96         {HKEY_PERFORMANCE_DATA, "HKEY_PERFORMANCE_DATA" },
97         {HKEY_USERS, "HKEY_USERS" },
98         {HKEY_CURRENT_CONFIG, "HKEY_CURRENT_CONFIG" },
99         {HKEY_DYN_DATA, "HKEY_DYN_DATA" },
100         {HKEY_PERFORMANCE_TEXT, "HKEY_PERFORMANCE_TEXT" },
101         {HKEY_PERFORMANCE_NLSTEXT, "HKEY_PERFORMANCE_NLSTEXT" },
102         { 0, NULL }
103 };
104
105 /** Obtain a list of predefined keys. */
106 _PUBLIC_ int reg_list_predefs(TALLOC_CTX *mem_ctx, char ***predefs, uint32_t **hkeys)
107 {
108         int i;
109         *predefs = talloc_array(mem_ctx, char *, ARRAY_SIZE(reg_predefined_keys));
110         *hkeys = talloc_array(mem_ctx, uint32_t, ARRAY_SIZE(reg_predefined_keys));
111
112         for (i = 0; reg_predefined_keys[i].name; i++) {
113                 (*predefs)[i] = talloc_strdup(mem_ctx, reg_predefined_keys[i].name);
114                 (*hkeys)[i] = reg_predefined_keys[i].handle;
115         }
116
117         return i;
118 }
119
120 /** Obtain name of specific hkey. */
121 _PUBLIC_ const char *reg_get_predef_name(uint32_t hkey)
122 {
123         int i;
124         for (i = 0; reg_predefined_keys[i].name; i++) {
125                 if (reg_predefined_keys[i].handle == hkey) return reg_predefined_keys[i].name;
126         }
127
128         return NULL;
129 }
130
131 /** Get predefined key by name. */
132 _PUBLIC_ WERROR reg_get_predefined_key_by_name(struct registry_context *ctx, const char *name, struct registry_key **key)
133 {
134         int i;
135         
136         for (i = 0; reg_predefined_keys[i].name; i++) {
137                 if (!strcasecmp(reg_predefined_keys[i].name, name)) return reg_get_predefined_key(ctx, reg_predefined_keys[i].handle, key);
138         }
139
140         DEBUG(1, ("No predefined key with name '%s'\n", name));
141         
142         return WERR_BADFILE;
143 }
144
145 /** Get predefined key by id. */
146 _PUBLIC_ WERROR reg_get_predefined_key(struct registry_context *ctx, uint32_t hkey, struct registry_key **key)
147 {
148         WERROR ret = ctx->get_predefined_key(ctx, hkey, key);
149
150         if (W_ERROR_IS_OK(ret)) {
151                 (*key)->name = talloc_strdup(*key, reg_get_predef_name(hkey));
152                 (*key)->path = ""; 
153         }
154
155         return ret;
156 }
157
158 /** Open a registry file/host/etc */
159 _PUBLIC_ WERROR reg_open_hive(TALLOC_CTX *parent_ctx, const char *backend, const char *location, struct auth_session_info *session_info, struct cli_credentials *credentials, struct registry_key **root)
160 {
161         struct registry_hive *rethive;
162         struct registry_key *retkey = NULL;
163         struct reg_init_function_entry *entry;
164         WERROR werr;
165
166         entry = reg_find_backend_entry(backend);
167         
168         if (!entry) {
169                 DEBUG(0, ("No such registry backend '%s' loaded!\n", backend));
170                 return WERR_GENERAL_FAILURE;
171         }
172
173         if(!entry->hive_functions || !entry->hive_functions->open_hive) {
174                 return WERR_NOT_SUPPORTED;
175         }
176         
177         rethive = talloc(parent_ctx, struct registry_hive);
178         rethive->location = location?talloc_strdup(rethive, location):NULL;
179         rethive->session_info = talloc_reference(rethive, session_info);
180         rethive->credentials = talloc_reference(rethive, credentials);
181         rethive->functions = entry->hive_functions;
182         rethive->backend_data = NULL;
183
184         werr = entry->hive_functions->open_hive(rethive, &retkey);
185
186         if(!W_ERROR_IS_OK(werr)) {
187                 return werr;
188         }
189
190         if(!retkey) {
191                 DEBUG(0, ("Backend %s didn't provide root key!\n", backend));
192                 return WERR_GENERAL_FAILURE;
193         }
194
195         rethive->root = retkey;
196
197         retkey->hive = rethive;
198         retkey->name = NULL;
199         retkey->path = talloc_strdup(retkey, "");
200         
201         *root = retkey;
202
203         return WERR_OK;
204 }
205
206 /**
207  * Open a key 
208  * First tries to use the open_key function from the backend
209  * then falls back to get_subkey_by_name and later get_subkey_by_index 
210  */
211 _PUBLIC_ WERROR reg_open_key(TALLOC_CTX *mem_ctx, struct registry_key *parent, const char *name, struct registry_key **result)
212 {
213         WERROR error;
214
215         if(!parent) {
216                 DEBUG(0, ("Invalid parent key specified for open of '%s'\n", name));
217                 return WERR_INVALID_PARAM;
218         }
219
220         if(!parent->hive->functions->open_key && 
221            (parent->hive->functions->get_subkey_by_name || 
222            parent->hive->functions->get_subkey_by_index)) {
223                 char *orig = strdup(name), 
224                          *curbegin = orig, 
225                          *curend = strchr(orig, '\\');
226                 struct registry_key *curkey = parent;
227
228                 while(curbegin && *curbegin) {
229                         if(curend)*curend = '\0';
230                         error = reg_key_get_subkey_by_name(mem_ctx, curkey, curbegin, &curkey);
231                         if(!W_ERROR_IS_OK(error)) {
232                                 SAFE_FREE(orig);
233                                 return error;
234                         }
235                         if(!curend) break;
236                         curbegin = curend + 1;
237                         curend = strchr(curbegin, '\\');
238                 }
239                 SAFE_FREE(orig);
240
241                 *result = curkey;
242                 
243                 return WERR_OK;
244         }
245
246         if(!parent->hive->functions->open_key) {
247                 DEBUG(0, ("Registry backend doesn't have get_subkey_by_name nor open_key!\n"));
248                 return WERR_NOT_SUPPORTED;
249         }
250
251         error = parent->hive->functions->open_key(mem_ctx, parent, name, result);
252
253         if(!W_ERROR_IS_OK(error)) return error;
254                 
255         (*result)->hive = parent->hive;
256         (*result)->path = ((parent->hive->root == parent)?talloc_strdup(mem_ctx, name):talloc_asprintf(mem_ctx, "%s\\%s", parent->path, name));
257         (*result)->hive = parent->hive;
258
259         return WERR_OK;
260 }
261
262 /**
263  * Get value by index
264  */
265 _PUBLIC_ WERROR reg_key_get_value_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *key, int idx, struct registry_value **val)
266 {
267         if(!key) return WERR_INVALID_PARAM;
268
269         if(key->hive->functions->get_value_by_index) {
270                 WERROR status = key->hive->functions->get_value_by_index(mem_ctx, key, idx, val);
271                 if(!W_ERROR_IS_OK(status)) 
272                         return status;
273         } else {
274                 return WERR_NOT_SUPPORTED;
275         }
276         
277         return WERR_OK;
278 }
279
280 /** 
281  * Get the number of subkeys.
282  */
283 _PUBLIC_ WERROR reg_key_num_subkeys(const struct registry_key *key, uint32_t *count)
284 {
285         if(!key) return WERR_INVALID_PARAM;
286         
287         if(key->hive->functions->num_subkeys) {
288                 return key->hive->functions->num_subkeys(key, count);
289         }
290
291         if(key->hive->functions->get_subkey_by_index) {
292                 int i;
293                 WERROR error;
294                 struct registry_key *dest = NULL;
295                 TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
296                 
297                 for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, key, i, &dest)); i++);
298                 talloc_free(mem_ctx);
299
300                 *count = i;
301                 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
302                 return error;
303         }
304
305         return WERR_NOT_SUPPORTED;
306 }
307
308 /**
309  * Get the number of values of a key.
310  */
311 _PUBLIC_ WERROR reg_key_num_values(const struct registry_key *key, uint32_t *count)
312 {
313         
314         if(!key) return WERR_INVALID_PARAM;
315
316         if (key->hive->functions->num_values) {
317                 return key->hive->functions->num_values(key, count);
318         }
319
320         if(key->hive->functions->get_value_by_index) {
321                 int i;
322                 WERROR error;
323                 struct registry_value *dest;
324                 TALLOC_CTX *mem_ctx = talloc_init("num_subkeys");
325                 
326                 for(i = 0; W_ERROR_IS_OK(error = key->hive->functions->get_value_by_index(mem_ctx, key, i, &dest)); i++);
327                 talloc_free(mem_ctx);
328
329                 *count = i;
330                 if(W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) error = WERR_OK;
331                 return error;
332         }
333
334         return WERR_NOT_SUPPORTED;
335 }
336
337 /**
338  * Get subkey by index.
339  */
340 _PUBLIC_ WERROR reg_key_get_subkey_by_index(TALLOC_CTX *mem_ctx, const struct registry_key *key, int idx, struct registry_key **subkey)
341 {
342         if(!key) return WERR_INVALID_PARAM;
343
344         if(key->hive->functions->get_subkey_by_index) {
345                 WERROR status = key->hive->functions->get_subkey_by_index(mem_ctx, key, idx, subkey);
346                 if(!NT_STATUS_IS_OK(status)) return status;
347         } else {
348                 return WERR_NOT_SUPPORTED;
349         }
350
351         if(key->hive->root == key) 
352                 (*subkey)->path = talloc_strdup(mem_ctx, (*subkey)->name);
353         else 
354                 (*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
355
356         (*subkey)->hive = key->hive;
357         return WERR_OK;;
358 }
359
360 /**
361  * Get subkey by name.
362  */
363 WERROR reg_key_get_subkey_by_name(TALLOC_CTX *mem_ctx, const struct registry_key *key, const char *name, struct registry_key **subkey)
364 {
365         int i;
366         WERROR error = WERR_OK;
367
368         if(!key) return WERR_INVALID_PARAM;
369
370         if(key->hive->functions->get_subkey_by_name) {
371                 error = key->hive->functions->get_subkey_by_name(mem_ctx, key,name,subkey);
372         } else if(key->hive->functions->open_key) {
373                 error = key->hive->functions->open_key(mem_ctx, key, name, subkey);
374         } else if(key->hive->functions->get_subkey_by_index) {
375                 for(i = 0; W_ERROR_IS_OK(error); i++) {
376                         error = reg_key_get_subkey_by_index(mem_ctx, key, i, subkey);
377                         if(W_ERROR_IS_OK(error) && !strcasecmp((*subkey)->name, name)) {
378                                 break;
379                         }
380                 }
381
382                 if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) 
383                         error = WERR_DEST_NOT_FOUND;
384         } else {
385                 return WERR_NOT_SUPPORTED;
386         }
387
388         if(!W_ERROR_IS_OK(error)) return error;
389
390         (*subkey)->path = talloc_asprintf(mem_ctx, "%s\\%s", key->path, (*subkey)->name);
391         (*subkey)->hive = key->hive;
392
393         return WERR_OK; 
394 }
395
396 /**
397  * Get value by name.
398  */
399 _PUBLIC_ WERROR reg_key_get_value_by_name(TALLOC_CTX *mem_ctx, const struct registry_key *key, const char *name, struct registry_value **val)
400 {
401         int i;
402         WERROR error = WERR_OK;
403
404         if(!key) return WERR_INVALID_PARAM;
405
406         if(key->hive->functions->get_value_by_name) {
407                 error = key->hive->functions->get_value_by_name(mem_ctx, key,name, val);
408         } else {
409                 for(i = 0; W_ERROR_IS_OK(error); i++) {
410                         error = reg_key_get_value_by_index(mem_ctx, key, i, val);
411                         if(W_ERROR_IS_OK(error) && !strcasecmp((*val)->name, name)) {
412                                 break;
413                         }
414                 }
415         }
416
417         if (W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS))
418                 return WERR_DEST_NOT_FOUND;
419
420         return error;
421 }
422
423 /**
424  * Delete a key.
425  */
426 _PUBLIC_ WERROR reg_key_del(struct registry_key *parent, const char *name)
427 {
428         WERROR error;
429         if(!parent) return WERR_INVALID_PARAM;
430         
431         
432         if(!parent->hive->functions->del_key)
433                 return WERR_NOT_SUPPORTED;
434         
435         error = parent->hive->functions->del_key(parent, name);
436         if(!W_ERROR_IS_OK(error)) return error;
437
438         return WERR_OK;
439 }
440
441 /**
442  * Add a key.
443  */
444 _PUBLIC_ WERROR reg_key_add_name(TALLOC_CTX *mem_ctx, const struct registry_key *parent, const char *name, uint32_t access_mask, struct security_descriptor *desc, struct registry_key **newkey)
445 {
446         WERROR error;
447         
448         if (!parent) return WERR_INVALID_PARAM;
449         
450         if (!parent->hive->functions->add_key) {
451                 DEBUG(1, ("Backend '%s' doesn't support method add_key\n", parent->hive->functions->name));
452                 return WERR_NOT_SUPPORTED;
453         }
454
455         error = parent->hive->functions->add_key(mem_ctx, parent, name, access_mask, desc, newkey);
456
457         if(!W_ERROR_IS_OK(error)) return error;
458
459         if (!*newkey) {
460                 DEBUG(0, ("Backend returned WERR_OK, but didn't specify key!\n"));
461                 return WERR_GENERAL_FAILURE;
462         }
463         
464         (*newkey)->hive = parent->hive;
465
466         return WERR_OK;
467 }
468
469 /**
470  * Set a value.
471  */
472 _PUBLIC_ WERROR reg_val_set(struct registry_key *key, const char *value, uint32_t type, DATA_BLOB data)
473 {
474         /* A 'real' set function has preference */
475         if (key->hive->functions->set_value) 
476                 return key->hive->functions->set_value(key, value, type, data);
477
478         DEBUG(1, ("Backend '%s' doesn't support method set_value\n", key->hive->functions->name));
479         return WERR_NOT_SUPPORTED;
480 }
481
482 /**
483  * Get the security descriptor on a key.
484  */
485 _PUBLIC_ WERROR reg_get_sec_desc(TALLOC_CTX *ctx, const struct registry_key *key, struct security_descriptor **secdesc)
486 {
487         /* A 'real' set function has preference */
488         if (key->hive->functions->key_get_sec_desc) 
489                 return key->hive->functions->key_get_sec_desc(ctx, key, secdesc);
490
491         DEBUG(1, ("Backend '%s' doesn't support method get_sec_desc\n", key->hive->functions->name));
492         return WERR_NOT_SUPPORTED;
493 }
494
495 /**
496  * Delete a value.
497  */
498 _PUBLIC_ WERROR reg_del_value(const struct registry_key *key, const char *valname)
499 {
500         WERROR ret = WERR_OK;
501         if(!key->hive->functions->del_value)
502                 return WERR_NOT_SUPPORTED;
503
504         ret = key->hive->functions->del_value(key, valname);
505
506         if(!W_ERROR_IS_OK(ret)) return ret;
507
508         return ret;
509 }
510
511 /**
512  * Flush a key to disk.
513  */
514 _PUBLIC_ WERROR reg_key_flush(const struct registry_key *key)
515 {
516         if (!key) {
517                 return WERR_INVALID_PARAM;
518         }
519         
520         if (key->hive->functions->flush_key) {
521                 return key->hive->functions->flush_key(key);
522         }
523         
524         /* No need for flushing, apparently */
525         return WERR_OK;
526 }
527
528 /**
529  * Get the maximum name and data lengths of the subkeys.
530  */
531 _PUBLIC_ WERROR reg_key_subkeysizes(const struct registry_key *key, uint32_t *max_subkeylen, uint32_t *max_subkeysize)
532 {
533         int i = 0; 
534         struct registry_key *subkey;
535         WERROR error;
536         TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
537
538         *max_subkeylen = *max_subkeysize = 0;
539
540         do {
541                 error = reg_key_get_subkey_by_index(mem_ctx, key, i, &subkey);
542
543                 if (W_ERROR_IS_OK(error)) {
544                         *max_subkeysize = MAX(*max_subkeysize, 0xFF);
545                         *max_subkeylen = MAX(*max_subkeylen, strlen(subkey->name));
546                 }
547
548                 i++;
549         } while (W_ERROR_IS_OK(error));
550
551         talloc_free(mem_ctx);
552
553         return WERR_OK;
554 }
555
556 /**
557  * Get the maximum name and data lengths of the values.
558  */
559 _PUBLIC_ WERROR reg_key_valuesizes(const struct registry_key *key, uint32_t *max_valnamelen, uint32_t *max_valbufsize)
560 {
561         int i = 0; 
562         struct registry_value *value;
563         WERROR error;
564         TALLOC_CTX *mem_ctx = talloc_init("subkeysize");
565
566         *max_valnamelen = *max_valbufsize = 0;
567
568         do {
569                 error = reg_key_get_value_by_index(mem_ctx, key, i, &value);
570
571                 if (W_ERROR_IS_OK(error)) {
572                         if (value->name) {
573                                 *max_valnamelen = MAX(*max_valnamelen, strlen(value->name));
574                         }
575                         *max_valbufsize = MAX(*max_valbufsize, value->data.length);
576                 }
577
578                 i++;
579         } while (W_ERROR_IS_OK(error));
580
581         talloc_free(mem_ctx);
582
583         return WERR_OK;
584 }