server: Store the prefix 32/64 architecture in the registry files.
[wine/wine-gecko.git] / server / registry.c
blobe5c0da496eed8388a1b3d11cf465222637a52e86
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 */
87 #define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
88 #define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
90 /* a key value */
91 struct key_value
93 WCHAR *name; /* value name */
94 unsigned short namelen; /* length of value name */
95 unsigned short type; /* value type */
96 data_size_t len; /* value data length in bytes */
97 void *data; /* pointer to value data */
100 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
101 #define MIN_VALUES 8 /* min. number of allocated values per key */
103 #define MAX_NAME_LEN 255 /* max. length of a key name */
104 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
106 /* the root of the registry tree */
107 static struct key *root_key;
109 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
110 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
111 static struct timeout_user *save_timeout_user; /* saving timer */
112 static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
114 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
115 static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
116 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
117 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
119 static void set_periodic_save_timer(void);
120 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
122 /* information about where to save a registry branch */
123 struct save_branch_info
125 struct key *key;
126 const char *path;
129 #define MAX_SAVE_BRANCH_INFO 3
130 static int save_branch_count;
131 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
134 /* information about a file being loaded */
135 struct file_load_info
137 const char *filename; /* input file name */
138 FILE *file; /* input file */
139 char *buffer; /* line buffer */
140 int len; /* buffer length */
141 int line; /* current input line */
142 WCHAR *tmp; /* temp buffer to use while parsing input */
143 size_t tmplen; /* length of temp buffer */
147 static void key_dump( struct object *obj, int verbose );
148 static unsigned int key_map_access( struct object *obj, unsigned int access );
149 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
150 static void key_destroy( struct object *obj );
152 static const struct object_ops key_ops =
154 sizeof(struct key), /* size */
155 key_dump, /* dump */
156 no_get_type, /* get_type */
157 no_add_queue, /* add_queue */
158 NULL, /* remove_queue */
159 NULL, /* signaled */
160 NULL, /* satisfied */
161 no_signal, /* signal */
162 no_get_fd, /* get_fd */
163 key_map_access, /* map_access */
164 default_get_sd, /* get_sd */
165 default_set_sd, /* set_sd */
166 no_lookup_name, /* lookup_name */
167 no_open_file, /* open_file */
168 key_close_handle, /* close_handle */
169 key_destroy /* destroy */
173 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
175 return (len == sizeof(wow6432node) &&
176 !memicmpW( name, wow6432node, sizeof(wow6432node)/sizeof(WCHAR) ));
180 * The registry text file format v2 used by this code is similar to the one
181 * used by REGEDIT import/export functionality, with the following differences:
182 * - strings and key names can contain \x escapes for Unicode
183 * - key names use escapes too in order to support Unicode
184 * - the modification time optionally follows the key name
185 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
188 /* dump the full path of a key */
189 static void dump_path( const struct key *key, const struct key *base, FILE *f )
191 if (key->parent && key->parent != base)
193 dump_path( key->parent, base, f );
194 fprintf( f, "\\\\" );
196 dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
199 /* dump a value to a text file */
200 static void dump_value( const struct key_value *value, FILE *f )
202 unsigned int i, dw;
203 int count;
205 if (value->namelen)
207 fputc( '\"', f );
208 count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
209 count += fprintf( f, "\"=" );
211 else count = fprintf( f, "@=" );
213 switch(value->type)
215 case REG_SZ:
216 case REG_EXPAND_SZ:
217 case REG_MULTI_SZ:
218 /* only output properly terminated strings in string format */
219 if (value->len < sizeof(WCHAR)) break;
220 if (value->len % sizeof(WCHAR)) break;
221 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
222 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
223 fputc( '\"', f );
224 dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
225 fprintf( f, "\"\n" );
226 return;
228 case REG_DWORD:
229 if (value->len != sizeof(dw)) break;
230 memcpy( &dw, value->data, sizeof(dw) );
231 fprintf( f, "dword:%08x\n", dw );
232 return;
235 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
236 else count += fprintf( f, "hex(%x):", value->type );
237 for (i = 0; i < value->len; i++)
239 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
240 if (i < value->len-1)
242 fputc( ',', f );
243 if (++count > 76)
245 fprintf( f, "\\\n " );
246 count = 2;
250 fputc( '\n', f );
253 /* save a registry and all its subkeys to a text file */
254 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
256 int i;
258 if (key->flags & KEY_VOLATILE) return;
259 /* save key if it has either some values or no subkeys, or needs special options */
260 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
261 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
263 fprintf( f, "\n[" );
264 if (key != base) dump_path( key, base, f );
265 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
266 if (key->class)
268 fprintf( f, "#class=\"" );
269 dump_strW( key->class, key->classlen / sizeof(WCHAR), f, "\"\"" );
270 fprintf( f, "\"\n" );
272 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
273 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
275 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
278 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
280 fprintf( stderr, "%s key ", op );
281 if (key) dump_path( key, NULL, stderr );
282 else fprintf( stderr, "ERROR" );
283 if (value)
285 fprintf( stderr, " value ");
286 dump_value( value, stderr );
288 else fprintf( stderr, "\n" );
291 static void key_dump( struct object *obj, int verbose )
293 struct key *key = (struct key *)obj;
294 assert( obj->ops == &key_ops );
295 fprintf( stderr, "Key flags=%x ", key->flags );
296 dump_path( key, NULL, stderr );
297 fprintf( stderr, "\n" );
300 /* notify waiter and maybe delete the notification */
301 static void do_notification( struct key *key, struct notify *notify, int del )
303 if (notify->event)
305 set_event( notify->event );
306 release_object( notify->event );
307 notify->event = NULL;
309 if (del)
311 list_remove( &notify->entry );
312 free( notify );
316 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
318 struct notify *notify;
320 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
322 if (notify->process == process && notify->hkey == hkey) return notify;
324 return NULL;
327 static unsigned int key_map_access( struct object *obj, unsigned int access )
329 if (access & GENERIC_READ) access |= KEY_READ;
330 if (access & GENERIC_WRITE) access |= KEY_WRITE;
331 if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
332 if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS;
333 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
336 /* close the notification associated with a handle */
337 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
339 struct key * key = (struct key *) obj;
340 struct notify *notify = find_notify( key, process, handle );
341 if (notify) do_notification( key, notify, 1 );
342 return 1; /* ok to close */
345 static void key_destroy( struct object *obj )
347 int i;
348 struct list *ptr;
349 struct key *key = (struct key *)obj;
350 assert( obj->ops == &key_ops );
352 free( key->name );
353 free( key->class );
354 for (i = 0; i <= key->last_value; i++)
356 free( key->values[i].name );
357 free( key->values[i].data );
359 free( key->values );
360 for (i = 0; i <= key->last_subkey; i++)
362 key->subkeys[i]->parent = NULL;
363 release_object( key->subkeys[i] );
365 free( key->subkeys );
366 /* unconditionally notify everything waiting on this key */
367 while ((ptr = list_head( &key->notify_list )))
369 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
370 do_notification( key, notify, 1 );
374 /* get the request vararg as registry path */
375 static inline void get_req_path( struct unicode_str *str, int skip_root )
377 str->str = get_req_data();
378 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
380 if (skip_root && str->len >= sizeof(root_name) &&
381 !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
383 str->str += sizeof(root_name)/sizeof(WCHAR);
384 str->len -= sizeof(root_name);
388 /* return the next token in a given path */
389 /* token->str must point inside the path, or be NULL for the first call */
390 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
392 data_size_t i = 0, len = path->len / sizeof(WCHAR);
394 if (!token->str) /* first time */
396 /* path cannot start with a backslash */
397 if (len && path->str[0] == '\\')
399 set_error( STATUS_OBJECT_PATH_INVALID );
400 return NULL;
403 else
405 i = token->str - path->str;
406 i += token->len / sizeof(WCHAR);
407 while (i < len && path->str[i] == '\\') i++;
409 token->str = path->str + i;
410 while (i < len && path->str[i] != '\\') i++;
411 token->len = (path->str + i - token->str) * sizeof(WCHAR);
412 return token;
415 /* allocate a key object */
416 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
418 struct key *key;
419 if ((key = alloc_object( &key_ops )))
421 key->name = NULL;
422 key->class = NULL;
423 key->namelen = name->len;
424 key->classlen = 0;
425 key->flags = 0;
426 key->last_subkey = -1;
427 key->nb_subkeys = 0;
428 key->subkeys = NULL;
429 key->nb_values = 0;
430 key->last_value = -1;
431 key->values = NULL;
432 key->modif = modif;
433 key->parent = NULL;
434 list_init( &key->notify_list );
435 if (name->len && !(key->name = memdup( name->str, name->len )))
437 release_object( key );
438 key = NULL;
441 return key;
444 /* mark a key and all its parents as dirty (modified) */
445 static void make_dirty( struct key *key )
447 while (key)
449 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
450 key->flags |= KEY_DIRTY;
451 key = key->parent;
455 /* mark a key and all its subkeys as clean (not modified) */
456 static void make_clean( struct key *key )
458 int i;
460 if (key->flags & KEY_VOLATILE) return;
461 if (!(key->flags & KEY_DIRTY)) return;
462 key->flags &= ~KEY_DIRTY;
463 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
466 /* go through all the notifications and send them if necessary */
467 static void check_notify( struct key *key, unsigned int change, int not_subtree )
469 struct list *ptr, *next;
471 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
473 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
474 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
475 do_notification( key, n, 0 );
479 /* update key modification time */
480 static void touch_key( struct key *key, unsigned int change )
482 struct key *k;
484 key->modif = current_time;
485 make_dirty( key );
487 /* do notifications */
488 check_notify( key, change, 1 );
489 for ( k = key->parent; k; k = k->parent )
490 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
493 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
494 static int grow_subkeys( struct key *key )
496 struct key **new_subkeys;
497 int nb_subkeys;
499 if (key->nb_subkeys)
501 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
502 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
504 set_error( STATUS_NO_MEMORY );
505 return 0;
508 else
510 nb_subkeys = MIN_VALUES;
511 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
513 key->subkeys = new_subkeys;
514 key->nb_subkeys = nb_subkeys;
515 return 1;
518 /* allocate a subkey for a given key, and return its index */
519 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
520 int index, timeout_t modif )
522 struct key *key;
523 int i;
525 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
527 set_error( STATUS_NAME_TOO_LONG );
528 return NULL;
530 if (parent->last_subkey + 1 == parent->nb_subkeys)
532 /* need to grow the array */
533 if (!grow_subkeys( parent )) return NULL;
535 if ((key = alloc_key( name, modif )) != NULL)
537 key->parent = parent;
538 for (i = ++parent->last_subkey; i > index; i--)
539 parent->subkeys[i] = parent->subkeys[i-1];
540 parent->subkeys[index] = key;
541 if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
542 parent->flags |= KEY_WOW64;
544 return key;
547 /* free a subkey of a given key */
548 static void free_subkey( struct key *parent, int index )
550 struct key *key;
551 int i, nb_subkeys;
553 assert( index >= 0 );
554 assert( index <= parent->last_subkey );
556 key = parent->subkeys[index];
557 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
558 parent->last_subkey--;
559 key->flags |= KEY_DELETED;
560 key->parent = NULL;
561 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
562 release_object( key );
564 /* try to shrink the array */
565 nb_subkeys = parent->nb_subkeys;
566 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
568 struct key **new_subkeys;
569 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
570 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
571 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
572 parent->subkeys = new_subkeys;
573 parent->nb_subkeys = nb_subkeys;
577 /* find the named child of a given key and return its index */
578 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
580 int i, min, max, res;
581 data_size_t len;
583 min = 0;
584 max = key->last_subkey;
585 while (min <= max)
587 i = (min + max) / 2;
588 len = min( key->subkeys[i]->namelen, name->len );
589 res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
590 if (!res) res = key->subkeys[i]->namelen - name->len;
591 if (!res)
593 *index = i;
594 return key->subkeys[i];
596 if (res > 0) max = i - 1;
597 else min = i + 1;
599 *index = min; /* this is where we should insert it */
600 return NULL;
603 /* return the wow64 variant of the key, or the key itself if none */
604 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
606 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
607 int index;
609 if (!(key->flags & KEY_WOW64)) return key;
610 if (!is_wow6432node( name->str, name->len ))
612 key = find_subkey( key, &wow6432node_str, &index );
613 assert( key ); /* if KEY_WOW64 is set we must find it */
615 return key;
619 /* follow a symlink and return the resolved key */
620 static struct key *follow_symlink( struct key *key, int iteration )
622 struct unicode_str path, token;
623 struct key_value *value;
624 int index;
626 if (iteration > 16) return NULL;
627 if (!(key->flags & KEY_SYMLINK)) return key;
628 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
630 path.str = value->data;
631 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
632 if (path.len <= sizeof(root_name)) return NULL;
633 if (memicmpW( path.str, root_name, sizeof(root_name)/sizeof(WCHAR) )) return NULL;
634 path.str += sizeof(root_name) / sizeof(WCHAR);
635 path.len -= sizeof(root_name);
637 key = root_key;
638 token.str = NULL;
639 if (!get_path_token( &path, &token )) return NULL;
640 while (token.len)
642 if (!(key = find_subkey( key, &token, &index ))) break;
643 if (!(key = follow_symlink( key, iteration + 1 ))) break;
644 get_path_token( &path, &token );
646 return key;
649 /* open a key until we find an element that doesn't exist */
650 /* helper for open_key and create_key */
651 static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
652 unsigned int access, struct unicode_str *token, int *index )
654 token->str = NULL;
655 if (!get_path_token( name, token )) return NULL;
656 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
657 while (token->len)
659 struct key *subkey;
660 if (!(subkey = find_subkey( key, token, index )))
662 if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
664 /* try in the 64-bit parent */
665 key = key->parent;
666 subkey = find_subkey( key, token, index );
669 if (!subkey) break;
670 key = subkey;
671 get_path_token( name, token );
672 if (!token->len) break;
673 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
674 if (!(key = follow_symlink( key, 0 )))
676 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
677 return NULL;
680 return key;
683 /* open a subkey */
684 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
685 unsigned int attributes )
687 int index;
688 struct unicode_str token;
690 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
692 if (token.len)
694 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
695 return NULL;
697 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
698 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
700 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
701 return NULL;
703 if (debug_level > 1) dump_operation( key, NULL, "Open" );
704 grab_object( key );
705 return key;
708 /* create a subkey */
709 static struct key *create_key( struct key *key, const struct unicode_str *name,
710 const struct unicode_str *class, unsigned int options,
711 unsigned int access, unsigned int attributes, int *created )
713 int index;
714 struct unicode_str token, next;
716 *created = 0;
717 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
719 if (!token.len) /* the key already exists */
721 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
722 if (options & REG_OPTION_CREATE_LINK)
724 set_error( STATUS_OBJECT_NAME_COLLISION );
725 return NULL;
727 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
729 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
730 return NULL;
732 if (debug_level > 1) dump_operation( key, NULL, "Open" );
733 grab_object( key );
734 return key;
737 /* token must be the last path component at this point */
738 next = token;
739 get_path_token( name, &next );
740 if (next.len)
742 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
743 return NULL;
746 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
748 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
749 return NULL;
751 *created = 1;
752 make_dirty( key );
753 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
755 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
756 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
757 else key->flags |= KEY_DIRTY;
759 if (debug_level > 1) dump_operation( key, NULL, "Create" );
760 if (class && class->len)
762 key->classlen = class->len;
763 free(key->class);
764 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
766 grab_object( key );
767 return key;
770 /* recursively create a subkey (for internal use only) */
771 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
773 struct key *base;
774 int index;
775 struct unicode_str token;
777 token.str = NULL;
778 if (!get_path_token( name, &token )) return NULL;
779 while (token.len)
781 struct key *subkey;
782 if (!(subkey = find_subkey( key, &token, &index ))) break;
783 key = subkey;
784 if (!(key = follow_symlink( key, 0 )))
786 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
787 return NULL;
789 get_path_token( name, &token );
792 if (token.len)
794 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
795 base = key;
796 for (;;)
798 get_path_token( name, &token );
799 if (!token.len) break;
800 /* we know the index is always 0 in a new key */
801 if (!(key = alloc_subkey( key, &token, 0, modif )))
803 free_subkey( base, index );
804 return NULL;
809 grab_object( key );
810 return key;
813 /* query information about a key or a subkey */
814 static void enum_key( const struct key *key, int index, int info_class,
815 struct enum_key_reply *reply )
817 int i;
818 data_size_t len, namelen, classlen;
819 data_size_t max_subkey = 0, max_class = 0;
820 data_size_t max_value = 0, max_data = 0;
821 char *data;
823 if (index != -1) /* -1 means use the specified key directly */
825 if ((index < 0) || (index > key->last_subkey))
827 set_error( STATUS_NO_MORE_ENTRIES );
828 return;
830 key = key->subkeys[index];
833 namelen = key->namelen;
834 classlen = key->classlen;
836 switch(info_class)
838 case KeyBasicInformation:
839 classlen = 0; /* only return the name */
840 /* fall through */
841 case KeyNodeInformation:
842 reply->max_subkey = 0;
843 reply->max_class = 0;
844 reply->max_value = 0;
845 reply->max_data = 0;
846 break;
847 case KeyFullInformation:
848 for (i = 0; i <= key->last_subkey; i++)
850 struct key *subkey = key->subkeys[i];
851 len = subkey->namelen / sizeof(WCHAR);
852 if (len > max_subkey) max_subkey = len;
853 len = subkey->classlen / sizeof(WCHAR);
854 if (len > max_class) max_class = len;
856 for (i = 0; i <= key->last_value; i++)
858 len = key->values[i].namelen / sizeof(WCHAR);
859 if (len > max_value) max_value = len;
860 len = key->values[i].len;
861 if (len > max_data) max_data = len;
863 reply->max_subkey = max_subkey;
864 reply->max_class = max_class;
865 reply->max_value = max_value;
866 reply->max_data = max_data;
867 namelen = 0; /* only return the class */
868 break;
869 default:
870 set_error( STATUS_INVALID_PARAMETER );
871 return;
873 reply->subkeys = key->last_subkey + 1;
874 reply->values = key->last_value + 1;
875 reply->modif = key->modif;
876 reply->total = namelen + classlen;
878 len = min( reply->total, get_reply_max_size() );
879 if (len && (data = set_reply_data_size( len )))
881 if (len > namelen)
883 reply->namelen = namelen;
884 memcpy( data, key->name, namelen );
885 memcpy( data + namelen, key->class, len - namelen );
887 else
889 reply->namelen = len;
890 memcpy( data, key->name, len );
893 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
896 /* delete a key and its values */
897 static int delete_key( struct key *key, int recurse )
899 int index;
900 struct key *parent = key->parent;
902 /* must find parent and index */
903 if (key == root_key)
905 set_error( STATUS_ACCESS_DENIED );
906 return -1;
908 assert( parent );
910 while (recurse && (key->last_subkey>=0))
911 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
912 return -1;
914 for (index = 0; index <= parent->last_subkey; index++)
915 if (parent->subkeys[index] == key) break;
916 assert( index <= parent->last_subkey );
918 /* we can only delete a key that has no subkeys */
919 if (key->last_subkey >= 0)
921 set_error( STATUS_ACCESS_DENIED );
922 return -1;
925 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
926 free_subkey( parent, index );
927 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
928 return 0;
931 /* try to grow the array of values; return 1 if OK, 0 on error */
932 static int grow_values( struct key *key )
934 struct key_value *new_val;
935 int nb_values;
937 if (key->nb_values)
939 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
940 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
942 set_error( STATUS_NO_MEMORY );
943 return 0;
946 else
948 nb_values = MIN_VALUES;
949 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
951 key->values = new_val;
952 key->nb_values = nb_values;
953 return 1;
956 /* find the named value of a given key and return its index in the array */
957 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
959 int i, min, max, res;
960 data_size_t len;
962 min = 0;
963 max = key->last_value;
964 while (min <= max)
966 i = (min + max) / 2;
967 len = min( key->values[i].namelen, name->len );
968 res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
969 if (!res) res = key->values[i].namelen - name->len;
970 if (!res)
972 *index = i;
973 return &key->values[i];
975 if (res > 0) max = i - 1;
976 else min = i + 1;
978 *index = min; /* this is where we should insert it */
979 return NULL;
982 /* insert a new value; the index must have been returned by find_value */
983 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
985 struct key_value *value;
986 WCHAR *new_name = NULL;
987 int i;
989 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
991 set_error( STATUS_NAME_TOO_LONG );
992 return NULL;
994 if (key->last_value + 1 == key->nb_values)
996 if (!grow_values( key )) return NULL;
998 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
999 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1000 value = &key->values[index];
1001 value->name = new_name;
1002 value->namelen = name->len;
1003 value->len = 0;
1004 value->data = NULL;
1005 return value;
1008 /* set a key value */
1009 static void set_value( struct key *key, const struct unicode_str *name,
1010 int type, const void *data, data_size_t len )
1012 struct key_value *value;
1013 void *ptr = NULL;
1014 int index;
1016 if ((value = find_value( key, name, &index )))
1018 /* check if the new value is identical to the existing one */
1019 if (value->type == type && value->len == len &&
1020 value->data && !memcmp( value->data, data, len ))
1022 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1023 return;
1027 if (key->flags & KEY_SYMLINK)
1029 if (type != REG_LINK || name->len != symlink_str.len ||
1030 memicmpW( name->str, symlink_str.str, name->len / sizeof(WCHAR) ))
1032 set_error( STATUS_ACCESS_DENIED );
1033 return;
1037 if (len && !(ptr = memdup( data, len ))) return;
1039 if (!value)
1041 if (!(value = insert_value( key, name, index )))
1043 free( ptr );
1044 return;
1047 else free( value->data ); /* already existing, free previous data */
1049 value->type = type;
1050 value->len = len;
1051 value->data = ptr;
1052 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1053 if (debug_level > 1) dump_operation( key, value, "Set" );
1056 /* get a key value */
1057 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1059 struct key_value *value;
1060 int index;
1062 if ((value = find_value( key, name, &index )))
1064 *type = value->type;
1065 *len = value->len;
1066 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1067 if (debug_level > 1) dump_operation( key, value, "Get" );
1069 else
1071 *type = -1;
1072 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1076 /* enumerate a key value */
1077 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1079 struct key_value *value;
1081 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1082 else
1084 void *data;
1085 data_size_t namelen, maxlen;
1087 value = &key->values[i];
1088 reply->type = value->type;
1089 namelen = value->namelen;
1091 switch(info_class)
1093 case KeyValueBasicInformation:
1094 reply->total = namelen;
1095 break;
1096 case KeyValueFullInformation:
1097 reply->total = namelen + value->len;
1098 break;
1099 case KeyValuePartialInformation:
1100 reply->total = value->len;
1101 namelen = 0;
1102 break;
1103 default:
1104 set_error( STATUS_INVALID_PARAMETER );
1105 return;
1108 maxlen = min( reply->total, get_reply_max_size() );
1109 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1111 if (maxlen > namelen)
1113 reply->namelen = namelen;
1114 memcpy( data, value->name, namelen );
1115 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1117 else
1119 reply->namelen = maxlen;
1120 memcpy( data, value->name, maxlen );
1123 if (debug_level > 1) dump_operation( key, value, "Enum" );
1127 /* delete a value */
1128 static void delete_value( struct key *key, const struct unicode_str *name )
1130 struct key_value *value;
1131 int i, index, nb_values;
1133 if (!(value = find_value( key, name, &index )))
1135 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1136 return;
1138 if (debug_level > 1) dump_operation( key, value, "Delete" );
1139 free( value->name );
1140 free( value->data );
1141 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1142 key->last_value--;
1143 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1145 /* try to shrink the array */
1146 nb_values = key->nb_values;
1147 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1149 struct key_value *new_val;
1150 nb_values -= nb_values / 3; /* shrink by 33% */
1151 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1152 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1153 key->values = new_val;
1154 key->nb_values = nb_values;
1158 /* get the registry key corresponding to an hkey handle */
1159 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1161 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1163 if (key && key->flags & KEY_DELETED)
1165 set_error( STATUS_KEY_DELETED );
1166 release_object( key );
1167 key = NULL;
1169 return key;
1172 /* get the registry key corresponding to a parent key handle */
1173 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1175 if (!hkey) return (struct key *)grab_object( root_key );
1176 return get_hkey_obj( hkey, 0 );
1179 /* read a line from the input file */
1180 static int read_next_line( struct file_load_info *info )
1182 char *newbuf;
1183 int newlen, pos = 0;
1185 info->line++;
1186 for (;;)
1188 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1189 return (pos != 0); /* EOF */
1190 pos = strlen(info->buffer);
1191 if (info->buffer[pos-1] == '\n')
1193 /* got a full line */
1194 info->buffer[--pos] = 0;
1195 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1196 return 1;
1198 if (pos < info->len - 1) return 1; /* EOF but something was read */
1200 /* need to enlarge the buffer */
1201 newlen = info->len + info->len / 2;
1202 if (!(newbuf = realloc( info->buffer, newlen )))
1204 set_error( STATUS_NO_MEMORY );
1205 return -1;
1207 info->buffer = newbuf;
1208 info->len = newlen;
1212 /* make sure the temp buffer holds enough space */
1213 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1215 WCHAR *tmp;
1216 if (info->tmplen >= size) return 1;
1217 if (!(tmp = realloc( info->tmp, size )))
1219 set_error( STATUS_NO_MEMORY );
1220 return 0;
1222 info->tmp = tmp;
1223 info->tmplen = size;
1224 return 1;
1227 /* report an error while loading an input file */
1228 static void file_read_error( const char *err, struct file_load_info *info )
1230 if (info->filename)
1231 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1232 else
1233 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1236 /* convert a data type tag to a value type */
1237 static int get_data_type( const char *buffer, int *type, int *parse_type )
1239 struct data_type { const char *tag; int len; int type; int parse_type; };
1241 static const struct data_type data_types[] =
1242 { /* actual type */ /* type to assume for parsing */
1243 { "\"", 1, REG_SZ, REG_SZ },
1244 { "str:\"", 5, REG_SZ, REG_SZ },
1245 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1246 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1247 { "hex:", 4, REG_BINARY, REG_BINARY },
1248 { "dword:", 6, REG_DWORD, REG_DWORD },
1249 { "hex(", 4, -1, REG_BINARY },
1250 { NULL, 0, 0, 0 }
1253 const struct data_type *ptr;
1254 char *end;
1256 for (ptr = data_types; ptr->tag; ptr++)
1258 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1259 *parse_type = ptr->parse_type;
1260 if ((*type = ptr->type) != -1) return ptr->len;
1261 /* "hex(xx):" is special */
1262 *type = (int)strtoul( buffer + 4, &end, 16 );
1263 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1264 return end + 2 - buffer;
1266 return 0;
1269 /* load and create a key from the input file */
1270 static struct key *load_key( struct key *base, const char *buffer,
1271 int prefix_len, struct file_load_info *info )
1273 WCHAR *p;
1274 struct unicode_str name;
1275 int res;
1276 unsigned int mod;
1277 timeout_t modif = current_time;
1278 data_size_t len;
1280 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1282 len = info->tmplen;
1283 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1285 file_read_error( "Malformed key", info );
1286 return NULL;
1288 if (sscanf( buffer + res, " %u", &mod ) == 1)
1289 modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1291 p = info->tmp;
1292 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1294 if (!*p)
1296 if (prefix_len > 1)
1298 file_read_error( "Malformed key", info );
1299 return NULL;
1301 /* empty key name, return base key */
1302 return (struct key *)grab_object( base );
1304 name.str = p;
1305 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1306 return create_key_recursive( base, &name, modif );
1309 /* load a global option from the input file */
1310 static int load_global_option( const char *buffer, struct file_load_info *info )
1312 const char *p;
1314 if (!strncmp( buffer, "#arch=", 6 ))
1316 enum prefix_type type;
1317 p = buffer + 6;
1318 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1319 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1320 else
1322 file_read_error( "Unknown architecture", info );
1323 set_error( STATUS_NOT_REGISTRY_FILE );
1324 return 0;
1326 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1327 else if (type != prefix_type)
1329 file_read_error( "Mismatched architecture", info );
1330 set_error( STATUS_NOT_REGISTRY_FILE );
1331 return 0;
1334 /* ignore unknown options */
1335 return 1;
1338 /* load a key option from the input file */
1339 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1341 const char *p;
1342 data_size_t len;
1344 if (!strncmp( buffer, "#class=", 7 ))
1346 p = buffer + 7;
1347 if (*p++ != '"') return 0;
1348 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1349 len = info->tmplen;
1350 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1351 free( key->class );
1352 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1353 key->classlen = len;
1355 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1356 /* ignore unknown options */
1357 return 1;
1360 /* parse a comma-separated list of hex digits */
1361 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1363 const char *p = buffer;
1364 data_size_t count = 0;
1365 char *end;
1367 while (isxdigit(*p))
1369 unsigned int val = strtoul( p, &end, 16 );
1370 if (end == p || val > 0xff) return -1;
1371 if (count++ >= *len) return -1; /* dest buffer overflow */
1372 *dest++ = val;
1373 p = end;
1374 while (isspace(*p)) p++;
1375 if (*p == ',') p++;
1376 while (isspace(*p)) p++;
1378 *len = count;
1379 return p - buffer;
1382 /* parse a value name and create the corresponding value */
1383 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1384 struct file_load_info *info )
1386 struct key_value *value;
1387 struct unicode_str name;
1388 int index;
1390 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1391 name.str = info->tmp;
1392 name.len = info->tmplen;
1393 if (buffer[0] == '@')
1395 name.len = 0;
1396 *len = 1;
1398 else
1400 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1401 if (r == -1) goto error;
1402 *len = r + 1; /* for initial quote */
1403 name.len -= sizeof(WCHAR); /* terminating null */
1405 while (isspace(buffer[*len])) (*len)++;
1406 if (buffer[*len] != '=') goto error;
1407 (*len)++;
1408 while (isspace(buffer[*len])) (*len)++;
1409 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1410 return value;
1412 error:
1413 file_read_error( "Malformed value name", info );
1414 return NULL;
1417 /* load a value from the input file */
1418 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1420 DWORD dw;
1421 void *ptr, *newptr;
1422 int res, type, parse_type;
1423 data_size_t maxlen, len;
1424 struct key_value *value;
1426 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1427 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1428 buffer += len + res;
1430 switch(parse_type)
1432 case REG_SZ:
1433 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1434 len = info->tmplen;
1435 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1436 ptr = info->tmp;
1437 break;
1438 case REG_DWORD:
1439 dw = strtoul( buffer, NULL, 16 );
1440 ptr = &dw;
1441 len = sizeof(dw);
1442 break;
1443 case REG_BINARY: /* hex digits */
1444 len = 0;
1445 for (;;)
1447 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1448 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1449 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1450 len += maxlen;
1451 buffer += res;
1452 while (isspace(*buffer)) buffer++;
1453 if (!*buffer) break;
1454 if (*buffer != '\\') goto error;
1455 if (read_next_line( info) != 1) goto error;
1456 buffer = info->buffer;
1457 while (isspace(*buffer)) buffer++;
1459 ptr = info->tmp;
1460 break;
1461 default:
1462 assert(0);
1463 ptr = NULL; /* keep compiler quiet */
1464 break;
1467 if (!len) newptr = NULL;
1468 else if (!(newptr = memdup( ptr, len ))) return 0;
1470 free( value->data );
1471 value->data = newptr;
1472 value->len = len;
1473 value->type = type;
1474 return 1;
1476 error:
1477 file_read_error( "Malformed value", info );
1478 free( value->data );
1479 value->data = NULL;
1480 value->len = 0;
1481 value->type = REG_NONE;
1482 return 0;
1485 /* return the length (in path elements) of name that is part of the key name */
1486 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1487 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1489 WCHAR *p;
1490 int res;
1491 data_size_t len;
1493 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1495 len = info->tmplen;
1496 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1498 file_read_error( "Malformed key", info );
1499 return 0;
1501 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1502 len = (p - info->tmp) * sizeof(WCHAR);
1503 for (res = 1; key != root_key; res++)
1505 if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
1506 key = key->parent;
1508 if (key == root_key) res = 0; /* no matching name */
1509 return res;
1512 /* load all the keys from the input file */
1513 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1514 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1516 struct key *subkey = NULL;
1517 struct file_load_info info;
1518 char *p;
1520 info.filename = filename;
1521 info.file = f;
1522 info.len = 4;
1523 info.tmplen = 4;
1524 info.line = 0;
1525 if (!(info.buffer = mem_alloc( info.len ))) return;
1526 if (!(info.tmp = mem_alloc( info.tmplen )))
1528 free( info.buffer );
1529 return;
1532 if ((read_next_line( &info ) != 1) ||
1533 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1535 set_error( STATUS_NOT_REGISTRY_FILE );
1536 goto done;
1539 while (read_next_line( &info ) == 1)
1541 p = info.buffer;
1542 while (*p && isspace(*p)) p++;
1543 switch(*p)
1545 case '[': /* new key */
1546 if (subkey) release_object( subkey );
1547 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1548 if (!(subkey = load_key( key, p + 1, prefix_len, &info )))
1549 file_read_error( "Error creating key", &info );
1550 break;
1551 case '@': /* default value */
1552 case '\"': /* value */
1553 if (subkey) load_value( subkey, p, &info );
1554 else file_read_error( "Value without key", &info );
1555 break;
1556 case '#': /* option */
1557 if (subkey) load_key_option( subkey, p, &info );
1558 else if (!load_global_option( p, &info )) goto done;
1559 break;
1560 case ';': /* comment */
1561 case 0: /* empty line */
1562 break;
1563 default:
1564 file_read_error( "Unrecognized input", &info );
1565 break;
1569 done:
1570 if (subkey) release_object( subkey );
1571 free( info.buffer );
1572 free( info.tmp );
1575 /* load a part of the registry from a file */
1576 static void load_registry( struct key *key, obj_handle_t handle )
1578 struct file *file;
1579 int fd;
1581 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1582 fd = dup( get_file_unix_fd( file ) );
1583 release_object( file );
1584 if (fd != -1)
1586 FILE *f = fdopen( fd, "r" );
1587 if (f)
1589 load_keys( key, NULL, f, -1 );
1590 fclose( f );
1592 else file_set_error();
1596 /* load one of the initial registry files */
1597 static int load_init_registry_from_file( const char *filename, struct key *key )
1599 FILE *f;
1601 if ((f = fopen( filename, "r" )))
1603 load_keys( key, filename, f, 0 );
1604 fclose( f );
1605 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1607 fprintf( stderr, "%s is not a valid registry file\n", filename );
1608 return 1;
1612 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1614 save_branch_info[save_branch_count].path = filename;
1615 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1616 make_object_static( &key->obj );
1617 return (f != NULL);
1620 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1622 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1623 static const WCHAR formatW[] = {'-','%','u',0};
1624 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1625 WCHAR *p = buffer;
1626 unsigned int i;
1628 strcpyW( p, prefixW );
1629 p += strlenW( prefixW );
1630 p += sprintfW( p, formatW, sid->Revision );
1631 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1632 sid->IdentifierAuthority.Value[4] ),
1633 MAKEWORD( sid->IdentifierAuthority.Value[3],
1634 sid->IdentifierAuthority.Value[2] )));
1635 for (i = 0; i < sid->SubAuthorityCount; i++)
1636 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1638 path->len = (p - buffer) * sizeof(WCHAR);
1639 path->str = p = memdup( buffer, path->len );
1640 return p;
1643 /* registry initialisation */
1644 void init_registry(void)
1646 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1647 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1648 static const WCHAR classes[] = {'S','o','f','t','w','a','r','e','\\',
1649 'C','l','a','s','s','e','s','\\',
1650 'W','o','w','6','4','3','2','N','o','d','e'};
1651 static const struct unicode_str root_name = { NULL, 0 };
1652 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1653 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1654 static const struct unicode_str classes_name = { classes, sizeof(classes) };
1656 WCHAR *current_user_path;
1657 struct unicode_str current_user_str;
1658 struct key *key, *hklm, *hkcu;
1660 /* switch to the config dir */
1662 if (fchdir( config_dir_fd ) == -1) fatal_perror( "chdir to config dir" );
1664 /* create the root key */
1665 root_key = alloc_key( &root_name, current_time );
1666 assert( root_key );
1667 make_object_static( &root_key->obj );
1669 /* load system.reg into Registry\Machine */
1671 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1672 fatal_error( "could not create Machine registry key\n" );
1674 if (!load_init_registry_from_file( "system.reg", hklm ))
1675 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1676 else if (prefix_type == PREFIX_UNKNOWN)
1677 prefix_type = PREFIX_32BIT;
1679 /* load userdef.reg into Registry\User\.Default */
1681 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1682 fatal_error( "could not create User\\.Default registry key\n" );
1684 load_init_registry_from_file( "userdef.reg", key );
1685 release_object( key );
1687 /* load user.reg into HKEY_CURRENT_USER */
1689 /* FIXME: match default user in token.c. should get from process token instead */
1690 current_user_path = format_user_registry_path( security_interactive_sid, &current_user_str );
1691 if (!current_user_path ||
1692 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1693 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1694 free( current_user_path );
1695 load_init_registry_from_file( "user.reg", hkcu );
1697 /* set the shared flag on Software\Classes\Wow6432Node */
1698 if (prefix_type == PREFIX_64BIT)
1700 if ((key = create_key_recursive( hklm, &classes_name, current_time )))
1702 key->flags |= KEY_WOWSHARE;
1703 release_object( key );
1705 /* FIXME: handle HKCU too */
1708 release_object( hklm );
1709 release_object( hkcu );
1711 /* start the periodic save timer */
1712 set_periodic_save_timer();
1714 /* go back to the server dir */
1715 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1718 /* save a registry branch to a file */
1719 static void save_all_subkeys( struct key *key, FILE *f )
1721 fprintf( f, "WINE REGISTRY Version 2\n" );
1722 fprintf( f, ";; All keys relative to " );
1723 dump_path( key, NULL, f );
1724 fprintf( f, "\n" );
1725 switch (prefix_type)
1727 case PREFIX_32BIT:
1728 fprintf( f, "\n#arch=win32\n" );
1729 break;
1730 case PREFIX_64BIT:
1731 fprintf( f, "\n#arch=win64\n" );
1732 break;
1733 default:
1734 break;
1736 save_subkeys( key, key, f );
1739 /* save a registry branch to a file handle */
1740 static void save_registry( struct key *key, obj_handle_t handle )
1742 struct file *file;
1743 int fd;
1745 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1746 fd = dup( get_file_unix_fd( file ) );
1747 release_object( file );
1748 if (fd != -1)
1750 FILE *f = fdopen( fd, "w" );
1751 if (f)
1753 save_all_subkeys( key, f );
1754 if (fclose( f )) file_set_error();
1756 else
1758 file_set_error();
1759 close( fd );
1764 /* save a registry branch to a file */
1765 static int save_branch( struct key *key, const char *path )
1767 struct stat st;
1768 char *p, *tmp = NULL;
1769 int fd, count = 0, ret = 0;
1770 FILE *f;
1772 if (!(key->flags & KEY_DIRTY))
1774 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1775 return 1;
1778 /* test the file type */
1780 if ((fd = open( path, O_WRONLY )) != -1)
1782 /* if file is not a regular file or has multiple links or is accessed
1783 * via symbolic links, write directly into it; otherwise use a temp file */
1784 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1786 ftruncate( fd, 0 );
1787 goto save;
1789 close( fd );
1792 /* create a temp file in the same directory */
1794 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1795 strcpy( tmp, path );
1796 if ((p = strrchr( tmp, '/' ))) p++;
1797 else p = tmp;
1798 for (;;)
1800 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1801 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1802 if (errno != EEXIST) goto done;
1803 close( fd );
1806 /* now save to it */
1808 save:
1809 if (!(f = fdopen( fd, "w" )))
1811 if (tmp) unlink( tmp );
1812 close( fd );
1813 goto done;
1816 if (debug_level > 1)
1818 fprintf( stderr, "%s: ", path );
1819 dump_operation( key, NULL, "saving" );
1822 save_all_subkeys( key, f );
1823 ret = !fclose(f);
1825 if (tmp)
1827 /* if successfully written, rename to final name */
1828 if (ret) ret = !rename( tmp, path );
1829 if (!ret) unlink( tmp );
1832 done:
1833 free( tmp );
1834 if (ret) make_clean( key );
1835 return ret;
1838 /* periodic saving of the registry */
1839 static void periodic_save( void *arg )
1841 int i;
1843 if (fchdir( config_dir_fd ) == -1) return;
1844 save_timeout_user = NULL;
1845 for (i = 0; i < save_branch_count; i++)
1846 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1847 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1848 set_periodic_save_timer();
1851 /* start the periodic save timer */
1852 static void set_periodic_save_timer(void)
1854 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1855 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
1858 /* save the modified registry branches to disk */
1859 void flush_registry(void)
1861 int i;
1863 if (fchdir( config_dir_fd ) == -1) return;
1864 for (i = 0; i < save_branch_count; i++)
1866 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1868 fprintf( stderr, "wineserver: could not save registry branch to %s",
1869 save_branch_info[i].path );
1870 perror( " " );
1873 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1877 /* create a registry key */
1878 DECL_HANDLER(create_key)
1880 struct key *key = NULL, *parent;
1881 struct unicode_str name, class;
1882 unsigned int access = req->access;
1884 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1886 reply->hkey = 0;
1888 if (req->namelen > get_req_data_size())
1890 set_error( STATUS_INVALID_PARAMETER );
1891 return;
1893 class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1894 class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1895 get_req_path( &name, !req->parent );
1896 if (name.str > class.str)
1898 set_error( STATUS_INVALID_PARAMETER );
1899 return;
1901 name.len = (class.str - name.str) * sizeof(WCHAR);
1903 /* NOTE: no access rights are required from the parent handle to create a key */
1904 if ((parent = get_parent_hkey_obj( req->parent )))
1906 if ((key = create_key( parent, &name, &class, req->options, access,
1907 req->attributes, &reply->created )))
1909 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1910 release_object( key );
1912 release_object( parent );
1916 /* open a registry key */
1917 DECL_HANDLER(open_key)
1919 struct key *key, *parent;
1920 struct unicode_str name;
1921 unsigned int access = req->access;
1923 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1925 reply->hkey = 0;
1926 /* NOTE: no access rights are required to open the parent key, only the child key */
1927 if ((parent = get_parent_hkey_obj( req->parent )))
1929 get_req_path( &name, !req->parent );
1930 if ((key = open_key( parent, &name, access, req->attributes )))
1932 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1933 release_object( key );
1935 release_object( parent );
1939 /* delete a registry key */
1940 DECL_HANDLER(delete_key)
1942 struct key *key;
1944 if ((key = get_hkey_obj( req->hkey, DELETE )))
1946 delete_key( key, 0);
1947 release_object( key );
1951 /* flush a registry key */
1952 DECL_HANDLER(flush_key)
1954 struct key *key = get_hkey_obj( req->hkey, 0 );
1955 if (key)
1957 /* we don't need to do anything here with the current implementation */
1958 release_object( key );
1962 /* enumerate registry subkeys */
1963 DECL_HANDLER(enum_key)
1965 struct key *key;
1967 if ((key = get_hkey_obj( req->hkey,
1968 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1970 enum_key( key, req->index, req->info_class, reply );
1971 release_object( key );
1975 /* set a value of a registry key */
1976 DECL_HANDLER(set_key_value)
1978 struct key *key;
1979 struct unicode_str name;
1981 if (req->namelen > get_req_data_size())
1983 set_error( STATUS_INVALID_PARAMETER );
1984 return;
1986 name.str = get_req_data();
1987 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
1989 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1991 data_size_t datalen = get_req_data_size() - req->namelen;
1992 const char *data = (const char *)get_req_data() + req->namelen;
1994 set_value( key, &name, req->type, data, datalen );
1995 release_object( key );
1999 /* retrieve the value of a registry key */
2000 DECL_HANDLER(get_key_value)
2002 struct key *key;
2003 struct unicode_str name;
2005 reply->total = 0;
2006 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2008 get_req_unicode_str( &name );
2009 get_value( key, &name, &reply->type, &reply->total );
2010 release_object( key );
2014 /* enumerate the value of a registry key */
2015 DECL_HANDLER(enum_key_value)
2017 struct key *key;
2019 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2021 enum_value( key, req->index, req->info_class, reply );
2022 release_object( key );
2026 /* delete a value of a registry key */
2027 DECL_HANDLER(delete_key_value)
2029 struct key *key;
2030 struct unicode_str name;
2032 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2034 get_req_unicode_str( &name );
2035 delete_value( key, &name );
2036 release_object( key );
2040 /* load a registry branch from a file */
2041 DECL_HANDLER(load_registry)
2043 struct key *key, *parent;
2044 struct token *token = thread_get_impersonation_token( current );
2045 struct unicode_str name;
2047 const LUID_AND_ATTRIBUTES privs[] =
2049 { SeBackupPrivilege, 0 },
2050 { SeRestorePrivilege, 0 },
2053 if (!token || !token_check_privileges( token, TRUE, privs,
2054 sizeof(privs)/sizeof(privs[0]), NULL ))
2056 set_error( STATUS_PRIVILEGE_NOT_HELD );
2057 return;
2060 if ((parent = get_parent_hkey_obj( req->hkey )))
2062 int dummy;
2063 get_req_path( &name, !req->hkey );
2064 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, &dummy )))
2066 load_registry( key, req->file );
2067 release_object( key );
2069 release_object( parent );
2073 DECL_HANDLER(unload_registry)
2075 struct key *key;
2076 struct token *token = thread_get_impersonation_token( current );
2078 const LUID_AND_ATTRIBUTES privs[] =
2080 { SeBackupPrivilege, 0 },
2081 { SeRestorePrivilege, 0 },
2084 if (!token || !token_check_privileges( token, TRUE, privs,
2085 sizeof(privs)/sizeof(privs[0]), NULL ))
2087 set_error( STATUS_PRIVILEGE_NOT_HELD );
2088 return;
2091 if ((key = get_hkey_obj( req->hkey, 0 )))
2093 delete_key( key, 1 ); /* FIXME */
2094 release_object( key );
2098 /* save a registry branch to a file */
2099 DECL_HANDLER(save_registry)
2101 struct key *key;
2103 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2105 set_error( STATUS_PRIVILEGE_NOT_HELD );
2106 return;
2109 if ((key = get_hkey_obj( req->hkey, 0 )))
2111 save_registry( key, req->file );
2112 release_object( key );
2116 /* add a registry key change notification */
2117 DECL_HANDLER(set_registry_notification)
2119 struct key *key;
2120 struct event *event;
2121 struct notify *notify;
2123 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2124 if (key)
2126 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2127 if (event)
2129 notify = find_notify( key, current->process, req->hkey );
2130 if (notify)
2132 if (notify->event)
2133 release_object( notify->event );
2134 grab_object( event );
2135 notify->event = event;
2137 else
2139 notify = mem_alloc( sizeof(*notify) );
2140 if (notify)
2142 grab_object( event );
2143 notify->event = event;
2144 notify->subtree = req->subtree;
2145 notify->filter = req->filter;
2146 notify->hkey = req->hkey;
2147 notify->process = current->process;
2148 list_add_head( &key->notify_list, &notify->entry );
2151 release_object( event );
2153 release_object( key );