configure: Add dll name recognition for cygwin.
[wine/multimedia.git] / server / registry.c
blobea7ea2067649d17ac739a5c941a70eecf8f2df3d
1 /*
2 * Server-side registry management
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* To do:
22 * - symbolic links
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "object.h"
43 #include "file.h"
44 #include "handle.h"
45 #include "request.h"
46 #include "unicode.h"
47 #include "security.h"
49 #include "winternl.h"
50 #include "wine/library.h"
52 struct notify
54 struct list entry; /* entry in list of notifications */
55 struct event *event; /* event to set when changing this key */
56 int subtree; /* true if subtree notification */
57 unsigned int filter; /* which events to notify on */
58 obj_handle_t hkey; /* hkey associated with this notification */
59 struct process *process; /* process in which the hkey is valid */
62 /* a registry key */
63 struct key
65 struct object obj; /* object header */
66 WCHAR *name; /* key name */
67 WCHAR *class; /* key class */
68 unsigned short namelen; /* length of key name */
69 unsigned short classlen; /* length of class name */
70 struct key *parent; /* parent key */
71 int last_subkey; /* last in use subkey */
72 int nb_subkeys; /* count of allocated subkeys */
73 struct key **subkeys; /* subkeys array */
74 int last_value; /* last in use value */
75 int nb_values; /* count of allocated values in array */
76 struct key_value *values; /* values array */
77 unsigned int flags; /* flags */
78 timeout_t modif; /* last modification time */
79 struct list notify_list; /* list of notifications */
82 /* key flags */
83 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
84 #define KEY_DELETED 0x0002 /* key has been deleted */
85 #define KEY_DIRTY 0x0004 /* key has been modified */
86 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
88 /* a key value */
89 struct key_value
91 WCHAR *name; /* value name */
92 unsigned short namelen; /* length of value name */
93 unsigned short type; /* value type */
94 data_size_t len; /* value data length in bytes */
95 void *data; /* pointer to value data */
98 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
99 #define MIN_VALUES 8 /* min. number of allocated values per key */
101 #define MAX_NAME_LEN MAX_PATH /* max. length of a key name */
102 #define MAX_VALUE_LEN MAX_PATH /* max. length of a value name */
104 /* the root of the registry tree */
105 static struct key *root_key;
107 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
108 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
109 static struct timeout_user *save_timeout_user; /* saving timer */
111 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
112 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
113 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
115 static void set_periodic_save_timer(void);
116 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
118 /* information about where to save a registry branch */
119 struct save_branch_info
121 struct key *key;
122 const char *path;
125 #define MAX_SAVE_BRANCH_INFO 3
126 static int save_branch_count;
127 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
130 /* information about a file being loaded */
131 struct file_load_info
133 const char *filename; /* input file name */
134 FILE *file; /* input file */
135 char *buffer; /* line buffer */
136 int len; /* buffer length */
137 int line; /* current input line */
138 WCHAR *tmp; /* temp buffer to use while parsing input */
139 size_t tmplen; /* length of temp buffer */
143 static void key_dump( struct object *obj, int verbose );
144 static unsigned int key_map_access( struct object *obj, unsigned int access );
145 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
146 static void key_destroy( struct object *obj );
148 static const struct object_ops key_ops =
150 sizeof(struct key), /* size */
151 key_dump, /* dump */
152 no_get_type, /* get_type */
153 no_add_queue, /* add_queue */
154 NULL, /* remove_queue */
155 NULL, /* signaled */
156 NULL, /* satisfied */
157 no_signal, /* signal */
158 no_get_fd, /* get_fd */
159 key_map_access, /* map_access */
160 default_get_sd, /* get_sd */
161 default_set_sd, /* set_sd */
162 no_lookup_name, /* lookup_name */
163 no_open_file, /* open_file */
164 key_close_handle, /* close_handle */
165 key_destroy /* destroy */
170 * The registry text file format v2 used by this code is similar to the one
171 * used by REGEDIT import/export functionality, with the following differences:
172 * - strings and key names can contain \x escapes for Unicode
173 * - key names use escapes too in order to support Unicode
174 * - the modification time optionally follows the key name
175 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
178 /* dump the full path of a key */
179 static void dump_path( const struct key *key, const struct key *base, FILE *f )
181 if (key->parent && key->parent != base)
183 dump_path( key->parent, base, f );
184 fprintf( f, "\\\\" );
186 dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
189 /* dump a value to a text file */
190 static void dump_value( const struct key_value *value, FILE *f )
192 unsigned int i, dw;
193 int count;
195 if (value->namelen)
197 fputc( '\"', f );
198 count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
199 count += fprintf( f, "\"=" );
201 else count = fprintf( f, "@=" );
203 switch(value->type)
205 case REG_SZ:
206 case REG_EXPAND_SZ:
207 case REG_MULTI_SZ:
208 /* only output properly terminated strings in string format */
209 if (value->len < sizeof(WCHAR)) break;
210 if (value->len % sizeof(WCHAR)) break;
211 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
212 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
213 fputc( '\"', f );
214 dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
215 fprintf( f, "\"\n" );
216 return;
218 case REG_DWORD:
219 if (value->len != sizeof(dw)) break;
220 memcpy( &dw, value->data, sizeof(dw) );
221 fprintf( f, "dword:%08x\n", dw );
222 return;
225 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
226 else count += fprintf( f, "hex(%x):", value->type );
227 for (i = 0; i < value->len; i++)
229 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
230 if (i < value->len-1)
232 fputc( ',', f );
233 if (++count > 76)
235 fprintf( f, "\\\n " );
236 count = 2;
240 fputc( '\n', f );
243 /* save a registry and all its subkeys to a text file */
244 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
246 int i;
248 if (key->flags & KEY_VOLATILE) return;
249 /* save key if it has either some values or no subkeys, or needs special options */
250 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
251 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
253 fprintf( f, "\n[" );
254 if (key != base) dump_path( key, base, f );
255 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
256 if (key->class)
258 fprintf( f, "#class=\"" );
259 dump_strW( key->class, key->classlen / sizeof(WCHAR), f, "\"\"" );
260 fprintf( f, "\"\n" );
262 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
263 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
265 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
268 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
270 fprintf( stderr, "%s key ", op );
271 if (key) dump_path( key, NULL, stderr );
272 else fprintf( stderr, "ERROR" );
273 if (value)
275 fprintf( stderr, " value ");
276 dump_value( value, stderr );
278 else fprintf( stderr, "\n" );
281 static void key_dump( struct object *obj, int verbose )
283 struct key *key = (struct key *)obj;
284 assert( obj->ops == &key_ops );
285 fprintf( stderr, "Key flags=%x ", key->flags );
286 dump_path( key, NULL, stderr );
287 fprintf( stderr, "\n" );
290 /* notify waiter and maybe delete the notification */
291 static void do_notification( struct key *key, struct notify *notify, int del )
293 if (notify->event)
295 set_event( notify->event );
296 release_object( notify->event );
297 notify->event = NULL;
299 if (del)
301 list_remove( &notify->entry );
302 free( notify );
306 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
308 struct notify *notify;
310 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
312 if (notify->process == process && notify->hkey == hkey) return notify;
314 return NULL;
317 static unsigned int key_map_access( struct object *obj, unsigned int access )
319 if (access & GENERIC_READ) access |= KEY_READ;
320 if (access & GENERIC_WRITE) access |= KEY_WRITE;
321 if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
322 if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS;
323 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
326 /* close the notification associated with a handle */
327 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
329 struct key * key = (struct key *) obj;
330 struct notify *notify = find_notify( key, process, handle );
331 if (notify) do_notification( key, notify, 1 );
332 return 1; /* ok to close */
335 static void key_destroy( struct object *obj )
337 int i;
338 struct list *ptr;
339 struct key *key = (struct key *)obj;
340 assert( obj->ops == &key_ops );
342 free( key->name );
343 free( key->class );
344 for (i = 0; i <= key->last_value; i++)
346 free( key->values[i].name );
347 free( key->values[i].data );
349 free( key->values );
350 for (i = 0; i <= key->last_subkey; i++)
352 key->subkeys[i]->parent = NULL;
353 release_object( key->subkeys[i] );
355 free( key->subkeys );
356 /* unconditionally notify everything waiting on this key */
357 while ((ptr = list_head( &key->notify_list )))
359 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
360 do_notification( key, notify, 1 );
364 /* get the request vararg as registry path */
365 static inline void get_req_path( struct unicode_str *str, int skip_root )
367 str->str = get_req_data();
368 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
370 if (skip_root && str->len >= sizeof(root_name) &&
371 !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
373 str->str += sizeof(root_name)/sizeof(WCHAR);
374 str->len -= sizeof(root_name);
378 /* return the next token in a given path */
379 /* token->str must point inside the path, or be NULL for the first call */
380 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
382 data_size_t i = 0, len = path->len / sizeof(WCHAR);
384 if (!token->str) /* first time */
386 /* path cannot start with a backslash */
387 if (len && path->str[0] == '\\')
389 set_error( STATUS_OBJECT_PATH_INVALID );
390 return NULL;
393 else
395 i = token->str - path->str;
396 i += token->len / sizeof(WCHAR);
397 while (i < len && path->str[i] == '\\') i++;
399 token->str = path->str + i;
400 while (i < len && path->str[i] != '\\') i++;
401 token->len = (path->str + i - token->str) * sizeof(WCHAR);
402 return token;
405 /* allocate a key object */
406 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
408 struct key *key;
409 if ((key = alloc_object( &key_ops )))
411 key->name = NULL;
412 key->class = NULL;
413 key->namelen = name->len;
414 key->classlen = 0;
415 key->flags = 0;
416 key->last_subkey = -1;
417 key->nb_subkeys = 0;
418 key->subkeys = NULL;
419 key->nb_values = 0;
420 key->last_value = -1;
421 key->values = NULL;
422 key->modif = modif;
423 key->parent = NULL;
424 list_init( &key->notify_list );
425 if (name->len && !(key->name = memdup( name->str, name->len )))
427 release_object( key );
428 key = NULL;
431 return key;
434 /* mark a key and all its parents as dirty (modified) */
435 static void make_dirty( struct key *key )
437 while (key)
439 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
440 key->flags |= KEY_DIRTY;
441 key = key->parent;
445 /* mark a key and all its subkeys as clean (not modified) */
446 static void make_clean( struct key *key )
448 int i;
450 if (key->flags & KEY_VOLATILE) return;
451 if (!(key->flags & KEY_DIRTY)) return;
452 key->flags &= ~KEY_DIRTY;
453 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
456 /* go through all the notifications and send them if necessary */
457 static void check_notify( struct key *key, unsigned int change, int not_subtree )
459 struct list *ptr, *next;
461 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
463 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
464 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
465 do_notification( key, n, 0 );
469 /* update key modification time */
470 static void touch_key( struct key *key, unsigned int change )
472 struct key *k;
474 key->modif = current_time;
475 make_dirty( key );
477 /* do notifications */
478 check_notify( key, change, 1 );
479 for ( k = key->parent; k; k = k->parent )
480 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
483 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
484 static int grow_subkeys( struct key *key )
486 struct key **new_subkeys;
487 int nb_subkeys;
489 if (key->nb_subkeys)
491 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
492 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
494 set_error( STATUS_NO_MEMORY );
495 return 0;
498 else
500 nb_subkeys = MIN_VALUES;
501 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
503 key->subkeys = new_subkeys;
504 key->nb_subkeys = nb_subkeys;
505 return 1;
508 /* allocate a subkey for a given key, and return its index */
509 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
510 int index, timeout_t modif )
512 struct key *key;
513 int i;
515 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
517 set_error( STATUS_NAME_TOO_LONG );
518 return NULL;
520 if (parent->last_subkey + 1 == parent->nb_subkeys)
522 /* need to grow the array */
523 if (!grow_subkeys( parent )) return NULL;
525 if ((key = alloc_key( name, modif )) != NULL)
527 key->parent = parent;
528 for (i = ++parent->last_subkey; i > index; i--)
529 parent->subkeys[i] = parent->subkeys[i-1];
530 parent->subkeys[index] = key;
532 return key;
535 /* free a subkey of a given key */
536 static void free_subkey( struct key *parent, int index )
538 struct key *key;
539 int i, nb_subkeys;
541 assert( index >= 0 );
542 assert( index <= parent->last_subkey );
544 key = parent->subkeys[index];
545 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
546 parent->last_subkey--;
547 key->flags |= KEY_DELETED;
548 key->parent = NULL;
549 release_object( key );
551 /* try to shrink the array */
552 nb_subkeys = parent->nb_subkeys;
553 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
555 struct key **new_subkeys;
556 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
557 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
558 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
559 parent->subkeys = new_subkeys;
560 parent->nb_subkeys = nb_subkeys;
564 /* find the named child of a given key and return its index */
565 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
567 int i, min, max, res;
568 data_size_t len;
570 min = 0;
571 max = key->last_subkey;
572 while (min <= max)
574 i = (min + max) / 2;
575 len = min( key->subkeys[i]->namelen, name->len );
576 res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
577 if (!res) res = key->subkeys[i]->namelen - name->len;
578 if (!res)
580 *index = i;
581 return key->subkeys[i];
583 if (res > 0) max = i - 1;
584 else min = i + 1;
586 *index = min; /* this is where we should insert it */
587 return NULL;
590 /* follow a symlink and return the resolved key */
591 static struct key *follow_symlink( struct key *key, int iteration )
593 struct unicode_str path, token;
594 struct key_value *value;
595 int index;
597 if (iteration > 16) return NULL;
598 if (!(key->flags & KEY_SYMLINK)) return key;
599 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
601 path.str = value->data;
602 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
603 if (path.len <= sizeof(root_name)) return NULL;
604 if (memicmpW( path.str, root_name, sizeof(root_name)/sizeof(WCHAR) )) return NULL;
605 path.str += sizeof(root_name) / sizeof(WCHAR);
606 path.len -= sizeof(root_name);
608 key = root_key;
609 token.str = NULL;
610 if (!get_path_token( &path, &token )) return NULL;
611 while (token.len)
613 if (!(key = find_subkey( key, &token, &index ))) break;
614 if (!(key = follow_symlink( key, iteration + 1 ))) break;
615 get_path_token( &path, &token );
617 return key;
620 /* open a subkey */
621 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int attributes )
623 int index;
624 struct unicode_str token;
626 token.str = NULL;
627 if (!get_path_token( name, &token )) return NULL;
628 while (token.len)
630 if (!(key = find_subkey( key, &token, &index )))
632 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
633 return NULL;
635 get_path_token( name, &token );
636 if (!token.len) break;
637 if (!(key = follow_symlink( key, 0 )))
639 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
640 return NULL;
644 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
646 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
647 return NULL;
649 if (debug_level > 1) dump_operation( key, NULL, "Open" );
650 grab_object( key );
651 return key;
654 /* create a subkey */
655 static struct key *create_key( struct key *key, const struct unicode_str *name,
656 const struct unicode_str *class, unsigned int options,
657 unsigned int attributes, int *created )
659 int index;
660 struct unicode_str token, next;
662 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
664 set_error( STATUS_KEY_DELETED );
665 return NULL;
668 token.str = NULL;
669 if (!get_path_token( name, &token )) return NULL;
670 *created = 0;
671 while (token.len)
673 struct key *subkey;
674 if (!(subkey = find_subkey( key, &token, &index ))) break;
675 key = subkey;
676 get_path_token( name, &token );
677 if (!token.len) break;
678 if (!(key = follow_symlink( key, 0 )))
680 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
681 return NULL;
685 if (!token.len) /* the key already exists */
687 if (options & REG_OPTION_CREATE_LINK)
689 set_error( STATUS_OBJECT_NAME_COLLISION );
690 return NULL;
692 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
694 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
695 return NULL;
697 if (debug_level > 1) dump_operation( key, NULL, "Open" );
698 grab_object( key );
699 return key;
702 /* token must be the last path component at this point */
703 next = token;
704 get_path_token( name, &next );
705 if (next.len)
707 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
708 return NULL;
711 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
713 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
714 return NULL;
716 *created = 1;
717 make_dirty( key );
718 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
720 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
721 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
722 else key->flags |= KEY_DIRTY;
724 if (debug_level > 1) dump_operation( key, NULL, "Create" );
725 if (class && class->len)
727 key->classlen = class->len;
728 free(key->class);
729 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
731 grab_object( key );
732 return key;
735 /* recursively create a subkey (for internal use only) */
736 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
738 struct key *base;
739 int index;
740 struct unicode_str token;
742 token.str = NULL;
743 if (!get_path_token( name, &token )) return NULL;
744 while (token.len)
746 struct key *subkey;
747 if (!(subkey = find_subkey( key, &token, &index ))) break;
748 key = subkey;
749 if (!(key = follow_symlink( key, 0 )))
751 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
752 return NULL;
754 get_path_token( name, &token );
757 if (token.len)
759 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
760 base = key;
761 for (;;)
763 get_path_token( name, &token );
764 if (!token.len) break;
765 /* we know the index is always 0 in a new key */
766 if (!(key = alloc_subkey( key, &token, 0, modif )))
768 free_subkey( base, index );
769 return NULL;
774 grab_object( key );
775 return key;
778 /* query information about a key or a subkey */
779 static void enum_key( const struct key *key, int index, int info_class,
780 struct enum_key_reply *reply )
782 int i;
783 data_size_t len, namelen, classlen;
784 data_size_t max_subkey = 0, max_class = 0;
785 data_size_t max_value = 0, max_data = 0;
786 char *data;
788 if (index != -1) /* -1 means use the specified key directly */
790 if ((index < 0) || (index > key->last_subkey))
792 set_error( STATUS_NO_MORE_ENTRIES );
793 return;
795 key = key->subkeys[index];
798 namelen = key->namelen;
799 classlen = key->classlen;
801 switch(info_class)
803 case KeyBasicInformation:
804 classlen = 0; /* only return the name */
805 /* fall through */
806 case KeyNodeInformation:
807 reply->max_subkey = 0;
808 reply->max_class = 0;
809 reply->max_value = 0;
810 reply->max_data = 0;
811 break;
812 case KeyFullInformation:
813 for (i = 0; i <= key->last_subkey; i++)
815 struct key *subkey = key->subkeys[i];
816 len = subkey->namelen / sizeof(WCHAR);
817 if (len > max_subkey) max_subkey = len;
818 len = subkey->classlen / sizeof(WCHAR);
819 if (len > max_class) max_class = len;
821 for (i = 0; i <= key->last_value; i++)
823 len = key->values[i].namelen / sizeof(WCHAR);
824 if (len > max_value) max_value = len;
825 len = key->values[i].len;
826 if (len > max_data) max_data = len;
828 reply->max_subkey = max_subkey;
829 reply->max_class = max_class;
830 reply->max_value = max_value;
831 reply->max_data = max_data;
832 namelen = 0; /* only return the class */
833 break;
834 default:
835 set_error( STATUS_INVALID_PARAMETER );
836 return;
838 reply->subkeys = key->last_subkey + 1;
839 reply->values = key->last_value + 1;
840 reply->modif = key->modif;
841 reply->total = namelen + classlen;
843 len = min( reply->total, get_reply_max_size() );
844 if (len && (data = set_reply_data_size( len )))
846 if (len > namelen)
848 reply->namelen = namelen;
849 memcpy( data, key->name, namelen );
850 memcpy( data + namelen, key->class, len - namelen );
852 else
854 reply->namelen = len;
855 memcpy( data, key->name, len );
858 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
861 /* delete a key and its values */
862 static int delete_key( struct key *key, int recurse )
864 int index;
865 struct key *parent;
867 /* must find parent and index */
868 if (key == root_key)
870 set_error( STATUS_ACCESS_DENIED );
871 return -1;
873 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
875 set_error( STATUS_KEY_DELETED );
876 return -1;
879 while (recurse && (key->last_subkey>=0))
880 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
881 return -1;
883 for (index = 0; index <= parent->last_subkey; index++)
884 if (parent->subkeys[index] == key) break;
885 assert( index <= parent->last_subkey );
887 /* we can only delete a key that has no subkeys */
888 if (key->last_subkey >= 0)
890 set_error( STATUS_ACCESS_DENIED );
891 return -1;
894 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
895 free_subkey( parent, index );
896 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
897 return 0;
900 /* try to grow the array of values; return 1 if OK, 0 on error */
901 static int grow_values( struct key *key )
903 struct key_value *new_val;
904 int nb_values;
906 if (key->nb_values)
908 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
909 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
911 set_error( STATUS_NO_MEMORY );
912 return 0;
915 else
917 nb_values = MIN_VALUES;
918 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
920 key->values = new_val;
921 key->nb_values = nb_values;
922 return 1;
925 /* find the named value of a given key and return its index in the array */
926 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
928 int i, min, max, res;
929 data_size_t len;
931 min = 0;
932 max = key->last_value;
933 while (min <= max)
935 i = (min + max) / 2;
936 len = min( key->values[i].namelen, name->len );
937 res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
938 if (!res) res = key->values[i].namelen - name->len;
939 if (!res)
941 *index = i;
942 return &key->values[i];
944 if (res > 0) max = i - 1;
945 else min = i + 1;
947 *index = min; /* this is where we should insert it */
948 return NULL;
951 /* insert a new value; the index must have been returned by find_value */
952 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
954 struct key_value *value;
955 WCHAR *new_name = NULL;
956 int i;
958 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
960 set_error( STATUS_NAME_TOO_LONG );
961 return NULL;
963 if (key->last_value + 1 == key->nb_values)
965 if (!grow_values( key )) return NULL;
967 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
968 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
969 value = &key->values[index];
970 value->name = new_name;
971 value->namelen = name->len;
972 value->len = 0;
973 value->data = NULL;
974 return value;
977 /* set a key value */
978 static void set_value( struct key *key, const struct unicode_str *name,
979 int type, const void *data, data_size_t len )
981 struct key_value *value;
982 void *ptr = NULL;
983 int index;
985 if ((value = find_value( key, name, &index )))
987 /* check if the new value is identical to the existing one */
988 if (value->type == type && value->len == len &&
989 value->data && !memcmp( value->data, data, len ))
991 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
992 return;
996 if (key->flags & KEY_SYMLINK)
998 if (type != REG_LINK || name->len != symlink_str.len ||
999 memicmpW( name->str, symlink_str.str, name->len / sizeof(WCHAR) ))
1001 set_error( STATUS_ACCESS_DENIED );
1002 return;
1006 if (len && !(ptr = memdup( data, len ))) return;
1008 if (!value)
1010 if (!(value = insert_value( key, name, index )))
1012 free( ptr );
1013 return;
1016 else free( value->data ); /* already existing, free previous data */
1018 value->type = type;
1019 value->len = len;
1020 value->data = ptr;
1021 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1022 if (debug_level > 1) dump_operation( key, value, "Set" );
1025 /* get a key value */
1026 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1028 struct key_value *value;
1029 int index;
1031 if ((value = find_value( key, name, &index )))
1033 *type = value->type;
1034 *len = value->len;
1035 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1036 if (debug_level > 1) dump_operation( key, value, "Get" );
1038 else
1040 *type = -1;
1041 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1045 /* enumerate a key value */
1046 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1048 struct key_value *value;
1050 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1051 else
1053 void *data;
1054 data_size_t namelen, maxlen;
1056 value = &key->values[i];
1057 reply->type = value->type;
1058 namelen = value->namelen;
1060 switch(info_class)
1062 case KeyValueBasicInformation:
1063 reply->total = namelen;
1064 break;
1065 case KeyValueFullInformation:
1066 reply->total = namelen + value->len;
1067 break;
1068 case KeyValuePartialInformation:
1069 reply->total = value->len;
1070 namelen = 0;
1071 break;
1072 default:
1073 set_error( STATUS_INVALID_PARAMETER );
1074 return;
1077 maxlen = min( reply->total, get_reply_max_size() );
1078 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1080 if (maxlen > namelen)
1082 reply->namelen = namelen;
1083 memcpy( data, value->name, namelen );
1084 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1086 else
1088 reply->namelen = maxlen;
1089 memcpy( data, value->name, maxlen );
1092 if (debug_level > 1) dump_operation( key, value, "Enum" );
1096 /* delete a value */
1097 static void delete_value( struct key *key, const struct unicode_str *name )
1099 struct key_value *value;
1100 int i, index, nb_values;
1102 if (!(value = find_value( key, name, &index )))
1104 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1105 return;
1107 if (debug_level > 1) dump_operation( key, value, "Delete" );
1108 free( value->name );
1109 free( value->data );
1110 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1111 key->last_value--;
1112 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1114 /* try to shrink the array */
1115 nb_values = key->nb_values;
1116 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1118 struct key_value *new_val;
1119 nb_values -= nb_values / 3; /* shrink by 33% */
1120 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1121 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1122 key->values = new_val;
1123 key->nb_values = nb_values;
1127 /* get the registry key corresponding to an hkey handle */
1128 static inline struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1130 return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1133 /* get the registry key corresponding to a parent key handle */
1134 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1136 if (!hkey) return (struct key *)grab_object( root_key );
1137 return (struct key *)get_handle_obj( current->process, hkey, 0, &key_ops );
1140 /* read a line from the input file */
1141 static int read_next_line( struct file_load_info *info )
1143 char *newbuf;
1144 int newlen, pos = 0;
1146 info->line++;
1147 for (;;)
1149 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1150 return (pos != 0); /* EOF */
1151 pos = strlen(info->buffer);
1152 if (info->buffer[pos-1] == '\n')
1154 /* got a full line */
1155 info->buffer[--pos] = 0;
1156 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1157 return 1;
1159 if (pos < info->len - 1) return 1; /* EOF but something was read */
1161 /* need to enlarge the buffer */
1162 newlen = info->len + info->len / 2;
1163 if (!(newbuf = realloc( info->buffer, newlen )))
1165 set_error( STATUS_NO_MEMORY );
1166 return -1;
1168 info->buffer = newbuf;
1169 info->len = newlen;
1173 /* make sure the temp buffer holds enough space */
1174 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1176 WCHAR *tmp;
1177 if (info->tmplen >= size) return 1;
1178 if (!(tmp = realloc( info->tmp, size )))
1180 set_error( STATUS_NO_MEMORY );
1181 return 0;
1183 info->tmp = tmp;
1184 info->tmplen = size;
1185 return 1;
1188 /* report an error while loading an input file */
1189 static void file_read_error( const char *err, struct file_load_info *info )
1191 if (info->filename)
1192 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1193 else
1194 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1197 /* convert a data type tag to a value type */
1198 static int get_data_type( const char *buffer, int *type, int *parse_type )
1200 struct data_type { const char *tag; int len; int type; int parse_type; };
1202 static const struct data_type data_types[] =
1203 { /* actual type */ /* type to assume for parsing */
1204 { "\"", 1, REG_SZ, REG_SZ },
1205 { "str:\"", 5, REG_SZ, REG_SZ },
1206 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1207 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1208 { "hex:", 4, REG_BINARY, REG_BINARY },
1209 { "dword:", 6, REG_DWORD, REG_DWORD },
1210 { "hex(", 4, -1, REG_BINARY },
1211 { NULL, 0, 0, 0 }
1214 const struct data_type *ptr;
1215 char *end;
1217 for (ptr = data_types; ptr->tag; ptr++)
1219 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1220 *parse_type = ptr->parse_type;
1221 if ((*type = ptr->type) != -1) return ptr->len;
1222 /* "hex(xx):" is special */
1223 *type = (int)strtoul( buffer + 4, &end, 16 );
1224 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1225 return end + 2 - buffer;
1227 return 0;
1230 /* load and create a key from the input file */
1231 static struct key *load_key( struct key *base, const char *buffer,
1232 int prefix_len, struct file_load_info *info )
1234 WCHAR *p;
1235 struct unicode_str name;
1236 int res;
1237 unsigned int mod;
1238 timeout_t modif = current_time;
1239 data_size_t len;
1241 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1243 len = info->tmplen;
1244 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1246 file_read_error( "Malformed key", info );
1247 return NULL;
1249 if (sscanf( buffer + res, " %u", &mod ) == 1)
1250 modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1252 p = info->tmp;
1253 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1255 if (!*p)
1257 if (prefix_len > 1)
1259 file_read_error( "Malformed key", info );
1260 return NULL;
1262 /* empty key name, return base key */
1263 return (struct key *)grab_object( base );
1265 name.str = p;
1266 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1267 return create_key_recursive( base, &name, modif );
1270 /* load a key option from the input file */
1271 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1273 const char *p;
1274 data_size_t len;
1276 if (!strncmp( buffer, "#class=", 7 ))
1278 p = buffer + 7;
1279 if (*p++ != '"') return 0;
1280 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1281 len = info->tmplen;
1282 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1283 free( key->class );
1284 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1285 key->classlen = len;
1287 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1288 /* ignore unknown options */
1289 return 1;
1292 /* parse a comma-separated list of hex digits */
1293 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1295 const char *p = buffer;
1296 data_size_t count = 0;
1297 char *end;
1299 while (isxdigit(*p))
1301 unsigned int val = strtoul( p, &end, 16 );
1302 if (end == p || val > 0xff) return -1;
1303 if (count++ >= *len) return -1; /* dest buffer overflow */
1304 *dest++ = val;
1305 p = end;
1306 while (isspace(*p)) p++;
1307 if (*p == ',') p++;
1308 while (isspace(*p)) p++;
1310 *len = count;
1311 return p - buffer;
1314 /* parse a value name and create the corresponding value */
1315 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1316 struct file_load_info *info )
1318 struct key_value *value;
1319 struct unicode_str name;
1320 int index;
1322 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1323 name.str = info->tmp;
1324 name.len = info->tmplen;
1325 if (buffer[0] == '@')
1327 name.len = 0;
1328 *len = 1;
1330 else
1332 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1333 if (r == -1) goto error;
1334 *len = r + 1; /* for initial quote */
1335 name.len -= sizeof(WCHAR); /* terminating null */
1337 while (isspace(buffer[*len])) (*len)++;
1338 if (buffer[*len] != '=') goto error;
1339 (*len)++;
1340 while (isspace(buffer[*len])) (*len)++;
1341 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1342 return value;
1344 error:
1345 file_read_error( "Malformed value name", info );
1346 return NULL;
1349 /* load a value from the input file */
1350 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1352 DWORD dw;
1353 void *ptr, *newptr;
1354 int res, type, parse_type;
1355 data_size_t maxlen, len;
1356 struct key_value *value;
1358 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1359 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1360 buffer += len + res;
1362 switch(parse_type)
1364 case REG_SZ:
1365 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1366 len = info->tmplen;
1367 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1368 ptr = info->tmp;
1369 break;
1370 case REG_DWORD:
1371 dw = strtoul( buffer, NULL, 16 );
1372 ptr = &dw;
1373 len = sizeof(dw);
1374 break;
1375 case REG_BINARY: /* hex digits */
1376 len = 0;
1377 for (;;)
1379 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1380 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1381 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1382 len += maxlen;
1383 buffer += res;
1384 while (isspace(*buffer)) buffer++;
1385 if (!*buffer) break;
1386 if (*buffer != '\\') goto error;
1387 if (read_next_line( info) != 1) goto error;
1388 buffer = info->buffer;
1389 while (isspace(*buffer)) buffer++;
1391 ptr = info->tmp;
1392 break;
1393 default:
1394 assert(0);
1395 ptr = NULL; /* keep compiler quiet */
1396 break;
1399 if (!len) newptr = NULL;
1400 else if (!(newptr = memdup( ptr, len ))) return 0;
1402 free( value->data );
1403 value->data = newptr;
1404 value->len = len;
1405 value->type = type;
1406 return 1;
1408 error:
1409 file_read_error( "Malformed value", info );
1410 free( value->data );
1411 value->data = NULL;
1412 value->len = 0;
1413 value->type = REG_NONE;
1414 return 0;
1417 /* return the length (in path elements) of name that is part of the key name */
1418 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1419 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1421 WCHAR *p;
1422 int res;
1423 data_size_t len;
1425 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1427 len = info->tmplen;
1428 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1430 file_read_error( "Malformed key", info );
1431 return 0;
1433 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1434 len = (p - info->tmp) * sizeof(WCHAR);
1435 for (res = 1; key != root_key; res++)
1437 if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
1438 key = key->parent;
1440 if (key == root_key) res = 0; /* no matching name */
1441 return res;
1444 /* load all the keys from the input file */
1445 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1446 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1448 struct key *subkey = NULL;
1449 struct file_load_info info;
1450 char *p;
1452 info.filename = filename;
1453 info.file = f;
1454 info.len = 4;
1455 info.tmplen = 4;
1456 info.line = 0;
1457 if (!(info.buffer = mem_alloc( info.len ))) return;
1458 if (!(info.tmp = mem_alloc( info.tmplen )))
1460 free( info.buffer );
1461 return;
1464 if ((read_next_line( &info ) != 1) ||
1465 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1467 set_error( STATUS_NOT_REGISTRY_FILE );
1468 goto done;
1471 while (read_next_line( &info ) == 1)
1473 p = info.buffer;
1474 while (*p && isspace(*p)) p++;
1475 switch(*p)
1477 case '[': /* new key */
1478 if (subkey) release_object( subkey );
1479 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1480 if (!(subkey = load_key( key, p + 1, prefix_len, &info )))
1481 file_read_error( "Error creating key", &info );
1482 break;
1483 case '@': /* default value */
1484 case '\"': /* value */
1485 if (subkey) load_value( subkey, p, &info );
1486 else file_read_error( "Value without key", &info );
1487 break;
1488 case '#': /* option */
1489 if (subkey) load_key_option( subkey, p, &info );
1490 break;
1491 case ';': /* comment */
1492 case 0: /* empty line */
1493 break;
1494 default:
1495 file_read_error( "Unrecognized input", &info );
1496 break;
1500 done:
1501 if (subkey) release_object( subkey );
1502 free( info.buffer );
1503 free( info.tmp );
1506 /* load a part of the registry from a file */
1507 static void load_registry( struct key *key, obj_handle_t handle )
1509 struct file *file;
1510 int fd;
1512 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1513 fd = dup( get_file_unix_fd( file ) );
1514 release_object( file );
1515 if (fd != -1)
1517 FILE *f = fdopen( fd, "r" );
1518 if (f)
1520 load_keys( key, NULL, f, -1 );
1521 fclose( f );
1523 else file_set_error();
1527 /* load one of the initial registry files */
1528 static void load_init_registry_from_file( const char *filename, struct key *key )
1530 FILE *f;
1532 if ((f = fopen( filename, "r" )))
1534 load_keys( key, filename, f, 0 );
1535 fclose( f );
1536 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1538 fprintf( stderr, "%s is not a valid registry file\n", filename );
1539 return;
1543 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1545 save_branch_info[save_branch_count].path = filename;
1546 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1547 make_object_static( &key->obj );
1550 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1552 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1553 static const WCHAR formatW[] = {'-','%','u',0};
1554 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1555 WCHAR *p = buffer;
1556 unsigned int i;
1558 strcpyW( p, prefixW );
1559 p += strlenW( prefixW );
1560 p += sprintfW( p, formatW, sid->Revision );
1561 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1562 sid->IdentifierAuthority.Value[4] ),
1563 MAKEWORD( sid->IdentifierAuthority.Value[3],
1564 sid->IdentifierAuthority.Value[2] )));
1565 for (i = 0; i < sid->SubAuthorityCount; i++)
1566 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1568 path->len = (p - buffer) * sizeof(WCHAR);
1569 path->str = p = memdup( buffer, path->len );
1570 return p;
1573 /* registry initialisation */
1574 void init_registry(void)
1576 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1577 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1578 static const struct unicode_str root_name = { NULL, 0 };
1579 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1580 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1582 WCHAR *current_user_path;
1583 struct unicode_str current_user_str;
1584 struct key *key;
1586 /* switch to the config dir */
1588 if (fchdir( config_dir_fd ) == -1) fatal_perror( "chdir to config dir" );
1590 /* create the root key */
1591 root_key = alloc_key( &root_name, current_time );
1592 assert( root_key );
1593 make_object_static( &root_key->obj );
1595 /* load system.reg into Registry\Machine */
1597 if (!(key = create_key_recursive( root_key, &HKLM_name, current_time )))
1598 fatal_error( "could not create Machine registry key\n" );
1600 load_init_registry_from_file( "system.reg", key );
1601 release_object( key );
1603 /* load userdef.reg into Registry\User\.Default */
1605 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1606 fatal_error( "could not create User\\.Default registry key\n" );
1608 load_init_registry_from_file( "userdef.reg", key );
1609 release_object( key );
1611 /* load user.reg into HKEY_CURRENT_USER */
1613 /* FIXME: match default user in token.c. should get from process token instead */
1614 current_user_path = format_user_registry_path( security_interactive_sid, &current_user_str );
1615 if (!current_user_path ||
1616 !(key = create_key_recursive( root_key, &current_user_str, current_time )))
1617 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1618 free( current_user_path );
1619 load_init_registry_from_file( "user.reg", key );
1620 release_object( key );
1622 /* start the periodic save timer */
1623 set_periodic_save_timer();
1625 /* go back to the server dir */
1626 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1629 /* save a registry branch to a file */
1630 static void save_all_subkeys( struct key *key, FILE *f )
1632 fprintf( f, "WINE REGISTRY Version 2\n" );
1633 fprintf( f, ";; All keys relative to " );
1634 dump_path( key, NULL, f );
1635 fprintf( f, "\n" );
1636 save_subkeys( key, key, f );
1639 /* save a registry branch to a file handle */
1640 static void save_registry( struct key *key, obj_handle_t handle )
1642 struct file *file;
1643 int fd;
1645 if (key->flags & KEY_DELETED)
1647 set_error( STATUS_KEY_DELETED );
1648 return;
1650 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1651 fd = dup( get_file_unix_fd( file ) );
1652 release_object( file );
1653 if (fd != -1)
1655 FILE *f = fdopen( fd, "w" );
1656 if (f)
1658 save_all_subkeys( key, f );
1659 if (fclose( f )) file_set_error();
1661 else
1663 file_set_error();
1664 close( fd );
1669 /* save a registry branch to a file */
1670 static int save_branch( struct key *key, const char *path )
1672 struct stat st;
1673 char *p, *tmp = NULL;
1674 int fd, count = 0, ret = 0;
1675 FILE *f;
1677 if (!(key->flags & KEY_DIRTY))
1679 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1680 return 1;
1683 /* test the file type */
1685 if ((fd = open( path, O_WRONLY )) != -1)
1687 /* if file is not a regular file or has multiple links or is accessed
1688 * via symbolic links, write directly into it; otherwise use a temp file */
1689 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1691 ftruncate( fd, 0 );
1692 goto save;
1694 close( fd );
1697 /* create a temp file in the same directory */
1699 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1700 strcpy( tmp, path );
1701 if ((p = strrchr( tmp, '/' ))) p++;
1702 else p = tmp;
1703 for (;;)
1705 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1706 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1707 if (errno != EEXIST) goto done;
1708 close( fd );
1711 /* now save to it */
1713 save:
1714 if (!(f = fdopen( fd, "w" )))
1716 if (tmp) unlink( tmp );
1717 close( fd );
1718 goto done;
1721 if (debug_level > 1)
1723 fprintf( stderr, "%s: ", path );
1724 dump_operation( key, NULL, "saving" );
1727 save_all_subkeys( key, f );
1728 ret = !fclose(f);
1730 if (tmp)
1732 /* if successfully written, rename to final name */
1733 if (ret) ret = !rename( tmp, path );
1734 if (!ret) unlink( tmp );
1737 done:
1738 free( tmp );
1739 if (ret) make_clean( key );
1740 return ret;
1743 /* periodic saving of the registry */
1744 static void periodic_save( void *arg )
1746 int i;
1748 if (fchdir( config_dir_fd ) == -1) return;
1749 save_timeout_user = NULL;
1750 for (i = 0; i < save_branch_count; i++)
1751 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1752 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1753 set_periodic_save_timer();
1756 /* start the periodic save timer */
1757 static void set_periodic_save_timer(void)
1759 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1760 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
1763 /* save the modified registry branches to disk */
1764 void flush_registry(void)
1766 int i;
1768 if (fchdir( config_dir_fd ) == -1) return;
1769 for (i = 0; i < save_branch_count; i++)
1771 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1773 fprintf( stderr, "wineserver: could not save registry branch to %s",
1774 save_branch_info[i].path );
1775 perror( " " );
1778 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1782 /* create a registry key */
1783 DECL_HANDLER(create_key)
1785 struct key *key = NULL, *parent;
1786 struct unicode_str name, class;
1787 unsigned int access = req->access;
1789 reply->hkey = 0;
1791 if (req->namelen > get_req_data_size())
1793 set_error( STATUS_INVALID_PARAMETER );
1794 return;
1796 class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1797 class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1798 get_req_path( &name, !req->parent );
1799 if (name.str > class.str)
1801 set_error( STATUS_INVALID_PARAMETER );
1802 return;
1804 name.len = (class.str - name.str) * sizeof(WCHAR);
1806 /* NOTE: no access rights are required from the parent handle to create a key */
1807 if ((parent = get_parent_hkey_obj( req->parent )))
1809 if ((key = create_key( parent, &name, &class, req->options, req->attributes, &reply->created )))
1811 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1812 release_object( key );
1814 release_object( parent );
1818 /* open a registry key */
1819 DECL_HANDLER(open_key)
1821 struct key *key, *parent;
1822 struct unicode_str name;
1823 unsigned int access = req->access;
1825 reply->hkey = 0;
1826 /* NOTE: no access rights are required to open the parent key, only the child key */
1827 if ((parent = get_parent_hkey_obj( req->parent )))
1829 get_req_path( &name, !req->parent );
1830 if ((key = open_key( parent, &name, req->attributes )))
1832 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1833 release_object( key );
1835 release_object( parent );
1839 /* delete a registry key */
1840 DECL_HANDLER(delete_key)
1842 struct key *key;
1844 if ((key = get_hkey_obj( req->hkey, DELETE )))
1846 delete_key( key, 0);
1847 release_object( key );
1851 /* flush a registry key */
1852 DECL_HANDLER(flush_key)
1854 struct key *key = get_hkey_obj( req->hkey, 0 );
1855 if (key)
1857 /* we don't need to do anything here with the current implementation */
1858 release_object( key );
1862 /* enumerate registry subkeys */
1863 DECL_HANDLER(enum_key)
1865 struct key *key;
1867 if ((key = get_hkey_obj( req->hkey,
1868 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1870 enum_key( key, req->index, req->info_class, reply );
1871 release_object( key );
1875 /* set a value of a registry key */
1876 DECL_HANDLER(set_key_value)
1878 struct key *key;
1879 struct unicode_str name;
1881 if (req->namelen > get_req_data_size())
1883 set_error( STATUS_INVALID_PARAMETER );
1884 return;
1886 name.str = get_req_data();
1887 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
1889 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1891 data_size_t datalen = get_req_data_size() - req->namelen;
1892 const char *data = (const char *)get_req_data() + req->namelen;
1894 set_value( key, &name, req->type, data, datalen );
1895 release_object( key );
1899 /* retrieve the value of a registry key */
1900 DECL_HANDLER(get_key_value)
1902 struct key *key;
1903 struct unicode_str name;
1905 reply->total = 0;
1906 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1908 get_req_unicode_str( &name );
1909 get_value( key, &name, &reply->type, &reply->total );
1910 release_object( key );
1914 /* enumerate the value of a registry key */
1915 DECL_HANDLER(enum_key_value)
1917 struct key *key;
1919 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1921 enum_value( key, req->index, req->info_class, reply );
1922 release_object( key );
1926 /* delete a value of a registry key */
1927 DECL_HANDLER(delete_key_value)
1929 struct key *key;
1930 struct unicode_str name;
1932 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1934 get_req_unicode_str( &name );
1935 delete_value( key, &name );
1936 release_object( key );
1940 /* load a registry branch from a file */
1941 DECL_HANDLER(load_registry)
1943 struct key *key, *parent;
1944 struct token *token = thread_get_impersonation_token( current );
1945 struct unicode_str name;
1947 const LUID_AND_ATTRIBUTES privs[] =
1949 { SeBackupPrivilege, 0 },
1950 { SeRestorePrivilege, 0 },
1953 if (!token || !token_check_privileges( token, TRUE, privs,
1954 sizeof(privs)/sizeof(privs[0]), NULL ))
1956 set_error( STATUS_PRIVILEGE_NOT_HELD );
1957 return;
1960 if ((parent = get_parent_hkey_obj( req->hkey )))
1962 int dummy;
1963 get_req_path( &name, !req->hkey );
1964 if ((key = create_key( parent, &name, NULL, 0, 0, &dummy )))
1966 load_registry( key, req->file );
1967 release_object( key );
1969 release_object( parent );
1973 DECL_HANDLER(unload_registry)
1975 struct key *key;
1976 struct token *token = thread_get_impersonation_token( current );
1978 const LUID_AND_ATTRIBUTES privs[] =
1980 { SeBackupPrivilege, 0 },
1981 { SeRestorePrivilege, 0 },
1984 if (!token || !token_check_privileges( token, TRUE, privs,
1985 sizeof(privs)/sizeof(privs[0]), NULL ))
1987 set_error( STATUS_PRIVILEGE_NOT_HELD );
1988 return;
1991 if ((key = get_hkey_obj( req->hkey, 0 )))
1993 delete_key( key, 1 ); /* FIXME */
1994 release_object( key );
1998 /* save a registry branch to a file */
1999 DECL_HANDLER(save_registry)
2001 struct key *key;
2003 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2005 set_error( STATUS_PRIVILEGE_NOT_HELD );
2006 return;
2009 if ((key = get_hkey_obj( req->hkey, 0 )))
2011 save_registry( key, req->file );
2012 release_object( key );
2016 /* add a registry key change notification */
2017 DECL_HANDLER(set_registry_notification)
2019 struct key *key;
2020 struct event *event;
2021 struct notify *notify;
2023 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2024 if (key)
2026 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2027 if (event)
2029 notify = find_notify( key, current->process, req->hkey );
2030 if (notify)
2032 if (notify->event)
2033 release_object( notify->event );
2034 grab_object( event );
2035 notify->event = event;
2037 else
2039 notify = mem_alloc( sizeof(*notify) );
2040 if (notify)
2042 grab_object( event );
2043 notify->event = event;
2044 notify->subtree = req->subtree;
2045 notify->filter = req->filter;
2046 notify->hkey = req->hkey;
2047 notify->process = current->process;
2048 list_add_head( &key->notify_list, &notify->entry );
2051 release_object( event );
2053 release_object( key );