msvcp90: Add basic_ios<wchar_t> stubs.
[wine/multimedia.git] / server / registry.c
bloba144c26585ab1abfc6997828f0ebc7abe2cdd6e4
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 "process.h"
47 #include "unicode.h"
48 #include "security.h"
50 #include "winternl.h"
51 #include "wine/library.h"
53 struct notify
55 struct list entry; /* entry in list of notifications */
56 struct event *event; /* event to set when changing this key */
57 int subtree; /* true if subtree notification */
58 unsigned int filter; /* which events to notify on */
59 obj_handle_t hkey; /* hkey associated with this notification */
60 struct process *process; /* process in which the hkey is valid */
63 /* a registry key */
64 struct key
66 struct object obj; /* object header */
67 WCHAR *name; /* key name */
68 WCHAR *class; /* key class */
69 unsigned short namelen; /* length of key name */
70 unsigned short classlen; /* length of class name */
71 struct key *parent; /* parent key */
72 int last_subkey; /* last in use subkey */
73 int nb_subkeys; /* count of allocated subkeys */
74 struct key **subkeys; /* subkeys array */
75 int last_value; /* last in use value */
76 int nb_values; /* count of allocated values in array */
77 struct key_value *values; /* values array */
78 unsigned int flags; /* flags */
79 timeout_t modif; /* last modification time */
80 struct list notify_list; /* list of notifications */
83 /* key flags */
84 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
85 #define KEY_DELETED 0x0002 /* key has been deleted */
86 #define KEY_DIRTY 0x0004 /* key has been modified */
87 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
88 #define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
89 #define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
91 /* a key value */
92 struct key_value
94 WCHAR *name; /* value name */
95 unsigned short namelen; /* length of value name */
96 unsigned short type; /* value type */
97 data_size_t len; /* value data length in bytes */
98 void *data; /* pointer to value data */
101 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
102 #define MIN_VALUES 8 /* min. number of allocated values per key */
104 #define MAX_NAME_LEN 255 /* max. length of a key name */
105 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
107 /* the root of the registry tree */
108 static struct key *root_key;
110 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
111 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
112 static struct timeout_user *save_timeout_user; /* saving timer */
113 static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
115 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
116 static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
117 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
118 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
120 static void set_periodic_save_timer(void);
121 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
123 /* information about where to save a registry branch */
124 struct save_branch_info
126 struct key *key;
127 const char *path;
130 #define MAX_SAVE_BRANCH_INFO 3
131 static int save_branch_count;
132 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
135 /* information about a file being loaded */
136 struct file_load_info
138 const char *filename; /* input file name */
139 FILE *file; /* input file */
140 char *buffer; /* line buffer */
141 int len; /* buffer length */
142 int line; /* current input line */
143 WCHAR *tmp; /* temp buffer to use while parsing input */
144 size_t tmplen; /* length of temp buffer */
148 static void key_dump( struct object *obj, int verbose );
149 static unsigned int key_map_access( struct object *obj, unsigned int access );
150 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
151 static void key_destroy( struct object *obj );
153 static const struct object_ops key_ops =
155 sizeof(struct key), /* size */
156 key_dump, /* dump */
157 no_get_type, /* get_type */
158 no_add_queue, /* add_queue */
159 NULL, /* remove_queue */
160 NULL, /* signaled */
161 NULL, /* satisfied */
162 no_signal, /* signal */
163 no_get_fd, /* get_fd */
164 key_map_access, /* map_access */
165 default_get_sd, /* get_sd */
166 default_set_sd, /* set_sd */
167 no_lookup_name, /* lookup_name */
168 no_open_file, /* open_file */
169 key_close_handle, /* close_handle */
170 key_destroy /* destroy */
174 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
176 return (len == sizeof(wow6432node) &&
177 !memicmpW( name, wow6432node, sizeof(wow6432node)/sizeof(WCHAR) ));
181 * The registry text file format v2 used by this code is similar to the one
182 * used by REGEDIT import/export functionality, with the following differences:
183 * - strings and key names can contain \x escapes for Unicode
184 * - key names use escapes too in order to support Unicode
185 * - the modification time optionally follows the key name
186 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
189 /* dump the full path of a key */
190 static void dump_path( const struct key *key, const struct key *base, FILE *f )
192 if (key->parent && key->parent != base)
194 dump_path( key->parent, base, f );
195 fprintf( f, "\\\\" );
197 dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
200 /* dump a value to a text file */
201 static void dump_value( const struct key_value *value, FILE *f )
203 unsigned int i, dw;
204 int count;
206 if (value->namelen)
208 fputc( '\"', f );
209 count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
210 count += fprintf( f, "\"=" );
212 else count = fprintf( f, "@=" );
214 switch(value->type)
216 case REG_SZ:
217 case REG_EXPAND_SZ:
218 case REG_MULTI_SZ:
219 /* only output properly terminated strings in string format */
220 if (value->len < sizeof(WCHAR)) break;
221 if (value->len % sizeof(WCHAR)) break;
222 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
223 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
224 fputc( '\"', f );
225 dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
226 fprintf( f, "\"\n" );
227 return;
229 case REG_DWORD:
230 if (value->len != sizeof(dw)) break;
231 memcpy( &dw, value->data, sizeof(dw) );
232 fprintf( f, "dword:%08x\n", dw );
233 return;
236 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
237 else count += fprintf( f, "hex(%x):", value->type );
238 for (i = 0; i < value->len; i++)
240 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
241 if (i < value->len-1)
243 fputc( ',', f );
244 if (++count > 76)
246 fprintf( f, "\\\n " );
247 count = 2;
251 fputc( '\n', f );
254 /* save a registry and all its subkeys to a text file */
255 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
257 int i;
259 if (key->flags & KEY_VOLATILE) return;
260 /* save key if it has either some values or no subkeys, or needs special options */
261 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
262 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
264 fprintf( f, "\n[" );
265 if (key != base) dump_path( key, base, f );
266 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
267 if (key->class)
269 fprintf( f, "#class=\"" );
270 dump_strW( key->class, key->classlen / sizeof(WCHAR), f, "\"\"" );
271 fprintf( f, "\"\n" );
273 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
274 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
276 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
279 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
281 fprintf( stderr, "%s key ", op );
282 if (key) dump_path( key, NULL, stderr );
283 else fprintf( stderr, "ERROR" );
284 if (value)
286 fprintf( stderr, " value ");
287 dump_value( value, stderr );
289 else fprintf( stderr, "\n" );
292 static void key_dump( struct object *obj, int verbose )
294 struct key *key = (struct key *)obj;
295 assert( obj->ops == &key_ops );
296 fprintf( stderr, "Key flags=%x ", key->flags );
297 dump_path( key, NULL, stderr );
298 fprintf( stderr, "\n" );
301 /* notify waiter and maybe delete the notification */
302 static void do_notification( struct key *key, struct notify *notify, int del )
304 if (notify->event)
306 set_event( notify->event );
307 release_object( notify->event );
308 notify->event = NULL;
310 if (del)
312 list_remove( &notify->entry );
313 free( notify );
317 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
319 struct notify *notify;
321 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
323 if (notify->process == process && notify->hkey == hkey) return notify;
325 return NULL;
328 static unsigned int key_map_access( struct object *obj, unsigned int access )
330 if (access & GENERIC_READ) access |= KEY_READ;
331 if (access & GENERIC_WRITE) access |= KEY_WRITE;
332 if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
333 if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS;
334 /* filter the WOW64 masks, as they aren't real access bits */
335 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL |
336 KEY_WOW64_64KEY | KEY_WOW64_32KEY);
339 /* close the notification associated with a handle */
340 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
342 struct key * key = (struct key *) obj;
343 struct notify *notify = find_notify( key, process, handle );
344 if (notify) do_notification( key, notify, 1 );
345 return 1; /* ok to close */
348 static void key_destroy( struct object *obj )
350 int i;
351 struct list *ptr;
352 struct key *key = (struct key *)obj;
353 assert( obj->ops == &key_ops );
355 free( key->name );
356 free( key->class );
357 for (i = 0; i <= key->last_value; i++)
359 free( key->values[i].name );
360 free( key->values[i].data );
362 free( key->values );
363 for (i = 0; i <= key->last_subkey; i++)
365 key->subkeys[i]->parent = NULL;
366 release_object( key->subkeys[i] );
368 free( key->subkeys );
369 /* unconditionally notify everything waiting on this key */
370 while ((ptr = list_head( &key->notify_list )))
372 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
373 do_notification( key, notify, 1 );
377 /* get the request vararg as registry path */
378 static inline void get_req_path( struct unicode_str *str, int skip_root )
380 str->str = get_req_data();
381 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
383 if (skip_root && str->len >= sizeof(root_name) &&
384 !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
386 str->str += sizeof(root_name)/sizeof(WCHAR);
387 str->len -= sizeof(root_name);
391 /* return the next token in a given path */
392 /* token->str must point inside the path, or be NULL for the first call */
393 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
395 data_size_t i = 0, len = path->len / sizeof(WCHAR);
397 if (!token->str) /* first time */
399 /* path cannot start with a backslash */
400 if (len && path->str[0] == '\\')
402 set_error( STATUS_OBJECT_PATH_INVALID );
403 return NULL;
406 else
408 i = token->str - path->str;
409 i += token->len / sizeof(WCHAR);
410 while (i < len && path->str[i] == '\\') i++;
412 token->str = path->str + i;
413 while (i < len && path->str[i] != '\\') i++;
414 token->len = (path->str + i - token->str) * sizeof(WCHAR);
415 return token;
418 /* allocate a key object */
419 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
421 struct key *key;
422 if ((key = alloc_object( &key_ops )))
424 key->name = NULL;
425 key->class = NULL;
426 key->namelen = name->len;
427 key->classlen = 0;
428 key->flags = 0;
429 key->last_subkey = -1;
430 key->nb_subkeys = 0;
431 key->subkeys = NULL;
432 key->nb_values = 0;
433 key->last_value = -1;
434 key->values = NULL;
435 key->modif = modif;
436 key->parent = NULL;
437 list_init( &key->notify_list );
438 if (name->len && !(key->name = memdup( name->str, name->len )))
440 release_object( key );
441 key = NULL;
444 return key;
447 /* mark a key and all its parents as dirty (modified) */
448 static void make_dirty( struct key *key )
450 while (key)
452 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
453 key->flags |= KEY_DIRTY;
454 key = key->parent;
458 /* mark a key and all its subkeys as clean (not modified) */
459 static void make_clean( struct key *key )
461 int i;
463 if (key->flags & KEY_VOLATILE) return;
464 if (!(key->flags & KEY_DIRTY)) return;
465 key->flags &= ~KEY_DIRTY;
466 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
469 /* go through all the notifications and send them if necessary */
470 static void check_notify( struct key *key, unsigned int change, int not_subtree )
472 struct list *ptr, *next;
474 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
476 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
477 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
478 do_notification( key, n, 0 );
482 /* update key modification time */
483 static void touch_key( struct key *key, unsigned int change )
485 struct key *k;
487 key->modif = current_time;
488 make_dirty( key );
490 /* do notifications */
491 check_notify( key, change, 1 );
492 for ( k = key->parent; k; k = k->parent )
493 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
496 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
497 static int grow_subkeys( struct key *key )
499 struct key **new_subkeys;
500 int nb_subkeys;
502 if (key->nb_subkeys)
504 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
505 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
507 set_error( STATUS_NO_MEMORY );
508 return 0;
511 else
513 nb_subkeys = MIN_VALUES;
514 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
516 key->subkeys = new_subkeys;
517 key->nb_subkeys = nb_subkeys;
518 return 1;
521 /* allocate a subkey for a given key, and return its index */
522 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
523 int index, timeout_t modif )
525 struct key *key;
526 int i;
528 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
530 set_error( STATUS_NAME_TOO_LONG );
531 return NULL;
533 if (parent->last_subkey + 1 == parent->nb_subkeys)
535 /* need to grow the array */
536 if (!grow_subkeys( parent )) return NULL;
538 if ((key = alloc_key( name, modif )) != NULL)
540 key->parent = parent;
541 for (i = ++parent->last_subkey; i > index; i--)
542 parent->subkeys[i] = parent->subkeys[i-1];
543 parent->subkeys[index] = key;
544 if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
545 parent->flags |= KEY_WOW64;
547 return key;
550 /* free a subkey of a given key */
551 static void free_subkey( struct key *parent, int index )
553 struct key *key;
554 int i, nb_subkeys;
556 assert( index >= 0 );
557 assert( index <= parent->last_subkey );
559 key = parent->subkeys[index];
560 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
561 parent->last_subkey--;
562 key->flags |= KEY_DELETED;
563 key->parent = NULL;
564 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
565 release_object( key );
567 /* try to shrink the array */
568 nb_subkeys = parent->nb_subkeys;
569 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
571 struct key **new_subkeys;
572 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
573 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
574 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
575 parent->subkeys = new_subkeys;
576 parent->nb_subkeys = nb_subkeys;
580 /* find the named child of a given key and return its index */
581 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
583 int i, min, max, res;
584 data_size_t len;
586 min = 0;
587 max = key->last_subkey;
588 while (min <= max)
590 i = (min + max) / 2;
591 len = min( key->subkeys[i]->namelen, name->len );
592 res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
593 if (!res) res = key->subkeys[i]->namelen - name->len;
594 if (!res)
596 *index = i;
597 return key->subkeys[i];
599 if (res > 0) max = i - 1;
600 else min = i + 1;
602 *index = min; /* this is where we should insert it */
603 return NULL;
606 /* return the wow64 variant of the key, or the key itself if none */
607 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
609 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
610 int index;
612 if (!(key->flags & KEY_WOW64)) return key;
613 if (!is_wow6432node( name->str, name->len ))
615 key = find_subkey( key, &wow6432node_str, &index );
616 assert( key ); /* if KEY_WOW64 is set we must find it */
618 return key;
622 /* follow a symlink and return the resolved key */
623 static struct key *follow_symlink( struct key *key, int iteration )
625 struct unicode_str path, token;
626 struct key_value *value;
627 int index;
629 if (iteration > 16) return NULL;
630 if (!(key->flags & KEY_SYMLINK)) return key;
631 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
633 path.str = value->data;
634 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
635 if (path.len <= sizeof(root_name)) return NULL;
636 if (memicmpW( path.str, root_name, sizeof(root_name)/sizeof(WCHAR) )) return NULL;
637 path.str += sizeof(root_name) / sizeof(WCHAR);
638 path.len -= sizeof(root_name);
640 key = root_key;
641 token.str = NULL;
642 if (!get_path_token( &path, &token )) return NULL;
643 while (token.len)
645 if (!(key = find_subkey( key, &token, &index ))) break;
646 if (!(key = follow_symlink( key, iteration + 1 ))) break;
647 get_path_token( &path, &token );
649 return key;
652 /* open a key until we find an element that doesn't exist */
653 /* helper for open_key and create_key */
654 static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
655 unsigned int access, struct unicode_str *token, int *index )
657 token->str = NULL;
658 if (!get_path_token( name, token )) return NULL;
659 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
660 while (token->len)
662 struct key *subkey;
663 if (!(subkey = find_subkey( key, token, index )))
665 if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
667 /* try in the 64-bit parent */
668 key = key->parent;
669 subkey = find_subkey( key, token, index );
672 if (!subkey) break;
673 key = subkey;
674 get_path_token( name, token );
675 if (!token->len) break;
676 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
677 if (!(key = follow_symlink( key, 0 )))
679 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
680 return NULL;
683 return key;
686 /* open a subkey */
687 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
688 unsigned int attributes )
690 int index;
691 struct unicode_str token;
693 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
695 if (token.len)
697 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
698 return NULL;
700 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
701 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
703 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
704 return NULL;
706 if (debug_level > 1) dump_operation( key, NULL, "Open" );
707 grab_object( key );
708 return key;
711 /* create a subkey */
712 static struct key *create_key( struct key *key, const struct unicode_str *name,
713 const struct unicode_str *class, unsigned int options,
714 unsigned int access, unsigned int attributes, int *created )
716 int index;
717 struct unicode_str token, next;
719 *created = 0;
720 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
722 if (!token.len) /* the key already exists */
724 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
725 if (options & REG_OPTION_CREATE_LINK)
727 set_error( STATUS_OBJECT_NAME_COLLISION );
728 return NULL;
730 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
732 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
733 return NULL;
735 if (debug_level > 1) dump_operation( key, NULL, "Open" );
736 grab_object( key );
737 return key;
740 /* token must be the last path component at this point */
741 next = token;
742 get_path_token( name, &next );
743 if (next.len)
745 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
746 return NULL;
749 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
751 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
752 return NULL;
754 *created = 1;
755 make_dirty( key );
756 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
758 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
759 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
760 else key->flags |= KEY_DIRTY;
762 if (debug_level > 1) dump_operation( key, NULL, "Create" );
763 if (class && class->len)
765 key->classlen = class->len;
766 free(key->class);
767 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
769 grab_object( key );
770 return key;
773 /* recursively create a subkey (for internal use only) */
774 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
776 struct key *base;
777 int index;
778 struct unicode_str token;
780 token.str = NULL;
781 if (!get_path_token( name, &token )) return NULL;
782 while (token.len)
784 struct key *subkey;
785 if (!(subkey = find_subkey( key, &token, &index ))) break;
786 key = subkey;
787 if (!(key = follow_symlink( key, 0 )))
789 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
790 return NULL;
792 get_path_token( name, &token );
795 if (token.len)
797 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
798 base = key;
799 for (;;)
801 get_path_token( name, &token );
802 if (!token.len) break;
803 /* we know the index is always 0 in a new key */
804 if (!(key = alloc_subkey( key, &token, 0, modif )))
806 free_subkey( base, index );
807 return NULL;
812 grab_object( key );
813 return key;
816 /* query information about a key or a subkey */
817 static void enum_key( const struct key *key, int index, int info_class,
818 struct enum_key_reply *reply )
820 int i;
821 data_size_t len, namelen, classlen;
822 data_size_t max_subkey = 0, max_class = 0;
823 data_size_t max_value = 0, max_data = 0;
824 char *data;
826 if (index != -1) /* -1 means use the specified key directly */
828 if ((index < 0) || (index > key->last_subkey))
830 set_error( STATUS_NO_MORE_ENTRIES );
831 return;
833 key = key->subkeys[index];
836 namelen = key->namelen;
837 classlen = key->classlen;
839 switch(info_class)
841 case KeyBasicInformation:
842 classlen = 0; /* only return the name */
843 /* fall through */
844 case KeyNodeInformation:
845 reply->max_subkey = 0;
846 reply->max_class = 0;
847 reply->max_value = 0;
848 reply->max_data = 0;
849 break;
850 case KeyFullInformation:
851 for (i = 0; i <= key->last_subkey; i++)
853 struct key *subkey = key->subkeys[i];
854 len = subkey->namelen / sizeof(WCHAR);
855 if (len > max_subkey) max_subkey = len;
856 len = subkey->classlen / sizeof(WCHAR);
857 if (len > max_class) max_class = len;
859 for (i = 0; i <= key->last_value; i++)
861 len = key->values[i].namelen / sizeof(WCHAR);
862 if (len > max_value) max_value = len;
863 len = key->values[i].len;
864 if (len > max_data) max_data = len;
866 reply->max_subkey = max_subkey;
867 reply->max_class = max_class;
868 reply->max_value = max_value;
869 reply->max_data = max_data;
870 namelen = 0; /* only return the class */
871 break;
872 default:
873 set_error( STATUS_INVALID_PARAMETER );
874 return;
876 reply->subkeys = key->last_subkey + 1;
877 reply->values = key->last_value + 1;
878 reply->modif = key->modif;
879 reply->total = namelen + classlen;
881 len = min( reply->total, get_reply_max_size() );
882 if (len && (data = set_reply_data_size( len )))
884 if (len > namelen)
886 reply->namelen = namelen;
887 memcpy( data, key->name, namelen );
888 memcpy( data + namelen, key->class, len - namelen );
890 else
892 reply->namelen = len;
893 memcpy( data, key->name, len );
896 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
899 /* delete a key and its values */
900 static int delete_key( struct key *key, int recurse )
902 int index;
903 struct key *parent = key->parent;
905 /* must find parent and index */
906 if (key == root_key)
908 set_error( STATUS_ACCESS_DENIED );
909 return -1;
911 assert( parent );
913 while (recurse && (key->last_subkey>=0))
914 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
915 return -1;
917 for (index = 0; index <= parent->last_subkey; index++)
918 if (parent->subkeys[index] == key) break;
919 assert( index <= parent->last_subkey );
921 /* we can only delete a key that has no subkeys */
922 if (key->last_subkey >= 0)
924 set_error( STATUS_ACCESS_DENIED );
925 return -1;
928 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
929 free_subkey( parent, index );
930 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
931 return 0;
934 /* try to grow the array of values; return 1 if OK, 0 on error */
935 static int grow_values( struct key *key )
937 struct key_value *new_val;
938 int nb_values;
940 if (key->nb_values)
942 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
943 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
945 set_error( STATUS_NO_MEMORY );
946 return 0;
949 else
951 nb_values = MIN_VALUES;
952 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
954 key->values = new_val;
955 key->nb_values = nb_values;
956 return 1;
959 /* find the named value of a given key and return its index in the array */
960 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
962 int i, min, max, res;
963 data_size_t len;
965 min = 0;
966 max = key->last_value;
967 while (min <= max)
969 i = (min + max) / 2;
970 len = min( key->values[i].namelen, name->len );
971 res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
972 if (!res) res = key->values[i].namelen - name->len;
973 if (!res)
975 *index = i;
976 return &key->values[i];
978 if (res > 0) max = i - 1;
979 else min = i + 1;
981 *index = min; /* this is where we should insert it */
982 return NULL;
985 /* insert a new value; the index must have been returned by find_value */
986 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
988 struct key_value *value;
989 WCHAR *new_name = NULL;
990 int i;
992 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
994 set_error( STATUS_NAME_TOO_LONG );
995 return NULL;
997 if (key->last_value + 1 == key->nb_values)
999 if (!grow_values( key )) return NULL;
1001 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1002 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1003 value = &key->values[index];
1004 value->name = new_name;
1005 value->namelen = name->len;
1006 value->len = 0;
1007 value->data = NULL;
1008 return value;
1011 /* set a key value */
1012 static void set_value( struct key *key, const struct unicode_str *name,
1013 int type, const void *data, data_size_t len )
1015 struct key_value *value;
1016 void *ptr = NULL;
1017 int index;
1019 if ((value = find_value( key, name, &index )))
1021 /* check if the new value is identical to the existing one */
1022 if (value->type == type && value->len == len &&
1023 value->data && !memcmp( value->data, data, len ))
1025 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1026 return;
1030 if (key->flags & KEY_SYMLINK)
1032 if (type != REG_LINK || name->len != symlink_str.len ||
1033 memicmpW( name->str, symlink_str.str, name->len / sizeof(WCHAR) ))
1035 set_error( STATUS_ACCESS_DENIED );
1036 return;
1040 if (len && !(ptr = memdup( data, len ))) return;
1042 if (!value)
1044 if (!(value = insert_value( key, name, index )))
1046 free( ptr );
1047 return;
1050 else free( value->data ); /* already existing, free previous data */
1052 value->type = type;
1053 value->len = len;
1054 value->data = ptr;
1055 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1056 if (debug_level > 1) dump_operation( key, value, "Set" );
1059 /* get a key value */
1060 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1062 struct key_value *value;
1063 int index;
1065 if ((value = find_value( key, name, &index )))
1067 *type = value->type;
1068 *len = value->len;
1069 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1070 if (debug_level > 1) dump_operation( key, value, "Get" );
1072 else
1074 *type = -1;
1075 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1079 /* enumerate a key value */
1080 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1082 struct key_value *value;
1084 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1085 else
1087 void *data;
1088 data_size_t namelen, maxlen;
1090 value = &key->values[i];
1091 reply->type = value->type;
1092 namelen = value->namelen;
1094 switch(info_class)
1096 case KeyValueBasicInformation:
1097 reply->total = namelen;
1098 break;
1099 case KeyValueFullInformation:
1100 reply->total = namelen + value->len;
1101 break;
1102 case KeyValuePartialInformation:
1103 reply->total = value->len;
1104 namelen = 0;
1105 break;
1106 default:
1107 set_error( STATUS_INVALID_PARAMETER );
1108 return;
1111 maxlen = min( reply->total, get_reply_max_size() );
1112 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1114 if (maxlen > namelen)
1116 reply->namelen = namelen;
1117 memcpy( data, value->name, namelen );
1118 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1120 else
1122 reply->namelen = maxlen;
1123 memcpy( data, value->name, maxlen );
1126 if (debug_level > 1) dump_operation( key, value, "Enum" );
1130 /* delete a value */
1131 static void delete_value( struct key *key, const struct unicode_str *name )
1133 struct key_value *value;
1134 int i, index, nb_values;
1136 if (!(value = find_value( key, name, &index )))
1138 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1139 return;
1141 if (debug_level > 1) dump_operation( key, value, "Delete" );
1142 free( value->name );
1143 free( value->data );
1144 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1145 key->last_value--;
1146 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1148 /* try to shrink the array */
1149 nb_values = key->nb_values;
1150 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1152 struct key_value *new_val;
1153 nb_values -= nb_values / 3; /* shrink by 33% */
1154 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1155 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1156 key->values = new_val;
1157 key->nb_values = nb_values;
1161 /* get the registry key corresponding to an hkey handle */
1162 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1164 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1166 if (key && key->flags & KEY_DELETED)
1168 set_error( STATUS_KEY_DELETED );
1169 release_object( key );
1170 key = NULL;
1172 return key;
1175 /* get the registry key corresponding to a parent key handle */
1176 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1178 if (!hkey) return (struct key *)grab_object( root_key );
1179 return get_hkey_obj( hkey, 0 );
1182 /* read a line from the input file */
1183 static int read_next_line( struct file_load_info *info )
1185 char *newbuf;
1186 int newlen, pos = 0;
1188 info->line++;
1189 for (;;)
1191 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1192 return (pos != 0); /* EOF */
1193 pos = strlen(info->buffer);
1194 if (info->buffer[pos-1] == '\n')
1196 /* got a full line */
1197 info->buffer[--pos] = 0;
1198 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1199 return 1;
1201 if (pos < info->len - 1) return 1; /* EOF but something was read */
1203 /* need to enlarge the buffer */
1204 newlen = info->len + info->len / 2;
1205 if (!(newbuf = realloc( info->buffer, newlen )))
1207 set_error( STATUS_NO_MEMORY );
1208 return -1;
1210 info->buffer = newbuf;
1211 info->len = newlen;
1215 /* make sure the temp buffer holds enough space */
1216 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1218 WCHAR *tmp;
1219 if (info->tmplen >= size) return 1;
1220 if (!(tmp = realloc( info->tmp, size )))
1222 set_error( STATUS_NO_MEMORY );
1223 return 0;
1225 info->tmp = tmp;
1226 info->tmplen = size;
1227 return 1;
1230 /* report an error while loading an input file */
1231 static void file_read_error( const char *err, struct file_load_info *info )
1233 if (info->filename)
1234 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1235 else
1236 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1239 /* convert a data type tag to a value type */
1240 static int get_data_type( const char *buffer, int *type, int *parse_type )
1242 struct data_type { const char *tag; int len; int type; int parse_type; };
1244 static const struct data_type data_types[] =
1245 { /* actual type */ /* type to assume for parsing */
1246 { "\"", 1, REG_SZ, REG_SZ },
1247 { "str:\"", 5, REG_SZ, REG_SZ },
1248 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1249 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1250 { "hex:", 4, REG_BINARY, REG_BINARY },
1251 { "dword:", 6, REG_DWORD, REG_DWORD },
1252 { "hex(", 4, -1, REG_BINARY },
1253 { NULL, 0, 0, 0 }
1256 const struct data_type *ptr;
1257 char *end;
1259 for (ptr = data_types; ptr->tag; ptr++)
1261 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1262 *parse_type = ptr->parse_type;
1263 if ((*type = ptr->type) != -1) return ptr->len;
1264 /* "hex(xx):" is special */
1265 *type = (int)strtoul( buffer + 4, &end, 16 );
1266 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1267 return end + 2 - buffer;
1269 return 0;
1272 /* load and create a key from the input file */
1273 static struct key *load_key( struct key *base, const char *buffer,
1274 int prefix_len, struct file_load_info *info )
1276 WCHAR *p;
1277 struct unicode_str name;
1278 int res;
1279 unsigned int mod;
1280 timeout_t modif = current_time;
1281 data_size_t len;
1283 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1285 len = info->tmplen;
1286 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1288 file_read_error( "Malformed key", info );
1289 return NULL;
1291 if (sscanf( buffer + res, " %u", &mod ) == 1)
1292 modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1294 p = info->tmp;
1295 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1297 if (!*p)
1299 if (prefix_len > 1)
1301 file_read_error( "Malformed key", info );
1302 return NULL;
1304 /* empty key name, return base key */
1305 return (struct key *)grab_object( base );
1307 name.str = p;
1308 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1309 return create_key_recursive( base, &name, modif );
1312 /* load a global option from the input file */
1313 static int load_global_option( const char *buffer, struct file_load_info *info )
1315 const char *p;
1317 if (!strncmp( buffer, "#arch=", 6 ))
1319 enum prefix_type type;
1320 p = buffer + 6;
1321 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1322 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1323 else
1325 file_read_error( "Unknown architecture", info );
1326 set_error( STATUS_NOT_REGISTRY_FILE );
1327 return 0;
1329 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1330 else if (type != prefix_type)
1332 file_read_error( "Mismatched architecture", info );
1333 set_error( STATUS_NOT_REGISTRY_FILE );
1334 return 0;
1337 /* ignore unknown options */
1338 return 1;
1341 /* load a key option from the input file */
1342 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1344 const char *p;
1345 data_size_t len;
1347 if (!strncmp( buffer, "#class=", 7 ))
1349 p = buffer + 7;
1350 if (*p++ != '"') return 0;
1351 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1352 len = info->tmplen;
1353 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1354 free( key->class );
1355 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1356 key->classlen = len;
1358 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1359 /* ignore unknown options */
1360 return 1;
1363 /* parse a comma-separated list of hex digits */
1364 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1366 const char *p = buffer;
1367 data_size_t count = 0;
1368 char *end;
1370 while (isxdigit(*p))
1372 unsigned int val = strtoul( p, &end, 16 );
1373 if (end == p || val > 0xff) return -1;
1374 if (count++ >= *len) return -1; /* dest buffer overflow */
1375 *dest++ = val;
1376 p = end;
1377 while (isspace(*p)) p++;
1378 if (*p == ',') p++;
1379 while (isspace(*p)) p++;
1381 *len = count;
1382 return p - buffer;
1385 /* parse a value name and create the corresponding value */
1386 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1387 struct file_load_info *info )
1389 struct key_value *value;
1390 struct unicode_str name;
1391 int index;
1393 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1394 name.str = info->tmp;
1395 name.len = info->tmplen;
1396 if (buffer[0] == '@')
1398 name.len = 0;
1399 *len = 1;
1401 else
1403 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1404 if (r == -1) goto error;
1405 *len = r + 1; /* for initial quote */
1406 name.len -= sizeof(WCHAR); /* terminating null */
1408 while (isspace(buffer[*len])) (*len)++;
1409 if (buffer[*len] != '=') goto error;
1410 (*len)++;
1411 while (isspace(buffer[*len])) (*len)++;
1412 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1413 return value;
1415 error:
1416 file_read_error( "Malformed value name", info );
1417 return NULL;
1420 /* load a value from the input file */
1421 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1423 DWORD dw;
1424 void *ptr, *newptr;
1425 int res, type, parse_type;
1426 data_size_t maxlen, len;
1427 struct key_value *value;
1429 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1430 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1431 buffer += len + res;
1433 switch(parse_type)
1435 case REG_SZ:
1436 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1437 len = info->tmplen;
1438 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1439 ptr = info->tmp;
1440 break;
1441 case REG_DWORD:
1442 dw = strtoul( buffer, NULL, 16 );
1443 ptr = &dw;
1444 len = sizeof(dw);
1445 break;
1446 case REG_BINARY: /* hex digits */
1447 len = 0;
1448 for (;;)
1450 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1451 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1452 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1453 len += maxlen;
1454 buffer += res;
1455 while (isspace(*buffer)) buffer++;
1456 if (!*buffer) break;
1457 if (*buffer != '\\') goto error;
1458 if (read_next_line( info) != 1) goto error;
1459 buffer = info->buffer;
1460 while (isspace(*buffer)) buffer++;
1462 ptr = info->tmp;
1463 break;
1464 default:
1465 assert(0);
1466 ptr = NULL; /* keep compiler quiet */
1467 break;
1470 if (!len) newptr = NULL;
1471 else if (!(newptr = memdup( ptr, len ))) return 0;
1473 free( value->data );
1474 value->data = newptr;
1475 value->len = len;
1476 value->type = type;
1477 return 1;
1479 error:
1480 file_read_error( "Malformed value", info );
1481 free( value->data );
1482 value->data = NULL;
1483 value->len = 0;
1484 value->type = REG_NONE;
1485 return 0;
1488 /* return the length (in path elements) of name that is part of the key name */
1489 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1490 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1492 WCHAR *p;
1493 int res;
1494 data_size_t len;
1496 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1498 len = info->tmplen;
1499 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1501 file_read_error( "Malformed key", info );
1502 return 0;
1504 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1505 len = (p - info->tmp) * sizeof(WCHAR);
1506 for (res = 1; key != root_key; res++)
1508 if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
1509 key = key->parent;
1511 if (key == root_key) res = 0; /* no matching name */
1512 return res;
1515 /* load all the keys from the input file */
1516 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1517 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1519 struct key *subkey = NULL;
1520 struct file_load_info info;
1521 char *p;
1523 info.filename = filename;
1524 info.file = f;
1525 info.len = 4;
1526 info.tmplen = 4;
1527 info.line = 0;
1528 if (!(info.buffer = mem_alloc( info.len ))) return;
1529 if (!(info.tmp = mem_alloc( info.tmplen )))
1531 free( info.buffer );
1532 return;
1535 if ((read_next_line( &info ) != 1) ||
1536 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1538 set_error( STATUS_NOT_REGISTRY_FILE );
1539 goto done;
1542 while (read_next_line( &info ) == 1)
1544 p = info.buffer;
1545 while (*p && isspace(*p)) p++;
1546 switch(*p)
1548 case '[': /* new key */
1549 if (subkey) release_object( subkey );
1550 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1551 if (!(subkey = load_key( key, p + 1, prefix_len, &info )))
1552 file_read_error( "Error creating key", &info );
1553 break;
1554 case '@': /* default value */
1555 case '\"': /* value */
1556 if (subkey) load_value( subkey, p, &info );
1557 else file_read_error( "Value without key", &info );
1558 break;
1559 case '#': /* option */
1560 if (subkey) load_key_option( subkey, p, &info );
1561 else if (!load_global_option( p, &info )) goto done;
1562 break;
1563 case ';': /* comment */
1564 case 0: /* empty line */
1565 break;
1566 default:
1567 file_read_error( "Unrecognized input", &info );
1568 break;
1572 done:
1573 if (subkey) release_object( subkey );
1574 free( info.buffer );
1575 free( info.tmp );
1578 /* load a part of the registry from a file */
1579 static void load_registry( struct key *key, obj_handle_t handle )
1581 struct file *file;
1582 int fd;
1584 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1585 fd = dup( get_file_unix_fd( file ) );
1586 release_object( file );
1587 if (fd != -1)
1589 FILE *f = fdopen( fd, "r" );
1590 if (f)
1592 load_keys( key, NULL, f, -1 );
1593 fclose( f );
1595 else file_set_error();
1599 /* load one of the initial registry files */
1600 static int load_init_registry_from_file( const char *filename, struct key *key )
1602 FILE *f;
1604 if ((f = fopen( filename, "r" )))
1606 load_keys( key, filename, f, 0 );
1607 fclose( f );
1608 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1610 fprintf( stderr, "%s is not a valid registry file\n", filename );
1611 return 1;
1615 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1617 save_branch_info[save_branch_count].path = filename;
1618 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1619 make_object_static( &key->obj );
1620 return (f != NULL);
1623 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1625 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1626 static const WCHAR formatW[] = {'-','%','u',0};
1627 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1628 WCHAR *p = buffer;
1629 unsigned int i;
1631 strcpyW( p, prefixW );
1632 p += strlenW( prefixW );
1633 p += sprintfW( p, formatW, sid->Revision );
1634 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1635 sid->IdentifierAuthority.Value[4] ),
1636 MAKEWORD( sid->IdentifierAuthority.Value[3],
1637 sid->IdentifierAuthority.Value[2] )));
1638 for (i = 0; i < sid->SubAuthorityCount; i++)
1639 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1641 path->len = (p - buffer) * sizeof(WCHAR);
1642 path->str = p = memdup( buffer, path->len );
1643 return p;
1646 /* get the cpu architectures that can be supported in the current prefix */
1647 unsigned int get_prefix_cpu_mask(void)
1649 /* Allowed server/client/prefix combinations:
1651 * prefix
1652 * 32 64
1653 * server +------+------+ client
1654 * | ok | fail | 32
1655 * 32 +------+------+---
1656 * | fail | fail | 64
1657 * ---+------+------+---
1658 * | ok | ok | 32
1659 * 64 +------+------+---
1660 * | fail | ok | 64
1661 * ---+------+------+---
1663 switch (prefix_type)
1665 case PREFIX_64BIT:
1666 /* 64-bit prefix requires 64-bit server */
1667 return sizeof(void *) > sizeof(int) ? ~0 : 0;
1668 case PREFIX_32BIT:
1669 default:
1670 return ~CPU_64BIT_MASK; /* only 32-bit cpus supported on 32-bit prefix */
1674 /* registry initialisation */
1675 void init_registry(void)
1677 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1678 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1679 static const WCHAR classes[] = {'S','o','f','t','w','a','r','e','\\',
1680 'C','l','a','s','s','e','s','\\',
1681 'W','o','w','6','4','3','2','N','o','d','e'};
1682 static const struct unicode_str root_name = { NULL, 0 };
1683 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1684 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1685 static const struct unicode_str classes_name = { classes, sizeof(classes) };
1687 WCHAR *current_user_path;
1688 struct unicode_str current_user_str;
1689 struct key *key, *hklm, *hkcu;
1691 /* switch to the config dir */
1693 if (fchdir( config_dir_fd ) == -1) fatal_perror( "chdir to config dir" );
1695 /* create the root key */
1696 root_key = alloc_key( &root_name, current_time );
1697 assert( root_key );
1698 make_object_static( &root_key->obj );
1700 /* load system.reg into Registry\Machine */
1702 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1703 fatal_error( "could not create Machine registry key\n" );
1705 if (!load_init_registry_from_file( "system.reg", hklm ))
1706 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1707 else if (prefix_type == PREFIX_UNKNOWN)
1708 prefix_type = PREFIX_32BIT;
1710 /* load userdef.reg into Registry\User\.Default */
1712 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1713 fatal_error( "could not create User\\.Default registry key\n" );
1715 load_init_registry_from_file( "userdef.reg", key );
1716 release_object( key );
1718 /* load user.reg into HKEY_CURRENT_USER */
1720 /* FIXME: match default user in token.c. should get from process token instead */
1721 current_user_path = format_user_registry_path( security_local_user_sid, &current_user_str );
1722 if (!current_user_path ||
1723 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1724 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1725 free( current_user_path );
1726 load_init_registry_from_file( "user.reg", hkcu );
1728 /* set the shared flag on Software\Classes\Wow6432Node */
1729 if (prefix_type == PREFIX_64BIT)
1731 if ((key = create_key_recursive( hklm, &classes_name, current_time )))
1733 key->flags |= KEY_WOWSHARE;
1734 release_object( key );
1736 /* FIXME: handle HKCU too */
1739 release_object( hklm );
1740 release_object( hkcu );
1742 /* start the periodic save timer */
1743 set_periodic_save_timer();
1745 /* go back to the server dir */
1746 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1749 /* save a registry branch to a file */
1750 static void save_all_subkeys( struct key *key, FILE *f )
1752 fprintf( f, "WINE REGISTRY Version 2\n" );
1753 fprintf( f, ";; All keys relative to " );
1754 dump_path( key, NULL, f );
1755 fprintf( f, "\n" );
1756 switch (prefix_type)
1758 case PREFIX_32BIT:
1759 fprintf( f, "\n#arch=win32\n" );
1760 break;
1761 case PREFIX_64BIT:
1762 fprintf( f, "\n#arch=win64\n" );
1763 break;
1764 default:
1765 break;
1767 save_subkeys( key, key, f );
1770 /* save a registry branch to a file handle */
1771 static void save_registry( struct key *key, obj_handle_t handle )
1773 struct file *file;
1774 int fd;
1776 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1777 fd = dup( get_file_unix_fd( file ) );
1778 release_object( file );
1779 if (fd != -1)
1781 FILE *f = fdopen( fd, "w" );
1782 if (f)
1784 save_all_subkeys( key, f );
1785 if (fclose( f )) file_set_error();
1787 else
1789 file_set_error();
1790 close( fd );
1795 /* save a registry branch to a file */
1796 static int save_branch( struct key *key, const char *path )
1798 struct stat st;
1799 char *p, *tmp = NULL;
1800 int fd, count = 0, ret = 0;
1801 FILE *f;
1803 if (!(key->flags & KEY_DIRTY))
1805 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1806 return 1;
1809 /* test the file type */
1811 if ((fd = open( path, O_WRONLY )) != -1)
1813 /* if file is not a regular file or has multiple links or is accessed
1814 * via symbolic links, write directly into it; otherwise use a temp file */
1815 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1817 ftruncate( fd, 0 );
1818 goto save;
1820 close( fd );
1823 /* create a temp file in the same directory */
1825 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1826 strcpy( tmp, path );
1827 if ((p = strrchr( tmp, '/' ))) p++;
1828 else p = tmp;
1829 for (;;)
1831 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1832 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1833 if (errno != EEXIST) goto done;
1834 close( fd );
1837 /* now save to it */
1839 save:
1840 if (!(f = fdopen( fd, "w" )))
1842 if (tmp) unlink( tmp );
1843 close( fd );
1844 goto done;
1847 if (debug_level > 1)
1849 fprintf( stderr, "%s: ", path );
1850 dump_operation( key, NULL, "saving" );
1853 save_all_subkeys( key, f );
1854 ret = !fclose(f);
1856 if (tmp)
1858 /* if successfully written, rename to final name */
1859 if (ret) ret = !rename( tmp, path );
1860 if (!ret) unlink( tmp );
1863 done:
1864 free( tmp );
1865 if (ret) make_clean( key );
1866 return ret;
1869 /* periodic saving of the registry */
1870 static void periodic_save( void *arg )
1872 int i;
1874 if (fchdir( config_dir_fd ) == -1) return;
1875 save_timeout_user = NULL;
1876 for (i = 0; i < save_branch_count; i++)
1877 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1878 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1879 set_periodic_save_timer();
1882 /* start the periodic save timer */
1883 static void set_periodic_save_timer(void)
1885 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1886 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
1889 /* save the modified registry branches to disk */
1890 void flush_registry(void)
1892 int i;
1894 if (fchdir( config_dir_fd ) == -1) return;
1895 for (i = 0; i < save_branch_count; i++)
1897 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1899 fprintf( stderr, "wineserver: could not save registry branch to %s",
1900 save_branch_info[i].path );
1901 perror( " " );
1904 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1907 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
1908 static int is_wow64_thread( struct thread *thread )
1910 return (prefix_type == PREFIX_64BIT && !(CPU_FLAG(thread->process->cpu) & CPU_64BIT_MASK));
1914 /* create a registry key */
1915 DECL_HANDLER(create_key)
1917 struct key *key = NULL, *parent;
1918 struct unicode_str name, class;
1919 unsigned int access = req->access;
1921 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1923 reply->hkey = 0;
1925 if (req->namelen > get_req_data_size())
1927 set_error( STATUS_INVALID_PARAMETER );
1928 return;
1930 class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1931 class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1932 get_req_path( &name, !req->parent );
1933 if (name.str > class.str)
1935 set_error( STATUS_INVALID_PARAMETER );
1936 return;
1938 name.len = (class.str - name.str) * sizeof(WCHAR);
1940 /* NOTE: no access rights are required from the parent handle to create a key */
1941 if ((parent = get_parent_hkey_obj( req->parent )))
1943 if ((key = create_key( parent, &name, &class, req->options, access,
1944 req->attributes, &reply->created )))
1946 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1947 release_object( key );
1949 release_object( parent );
1953 /* open a registry key */
1954 DECL_HANDLER(open_key)
1956 struct key *key, *parent;
1957 struct unicode_str name;
1958 unsigned int access = req->access;
1960 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1962 reply->hkey = 0;
1963 /* NOTE: no access rights are required to open the parent key, only the child key */
1964 if ((parent = get_parent_hkey_obj( req->parent )))
1966 get_req_path( &name, !req->parent );
1967 if ((key = open_key( parent, &name, access, req->attributes )))
1969 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1970 release_object( key );
1972 release_object( parent );
1976 /* delete a registry key */
1977 DECL_HANDLER(delete_key)
1979 struct key *key;
1981 if ((key = get_hkey_obj( req->hkey, DELETE )))
1983 delete_key( key, 0);
1984 release_object( key );
1988 /* flush a registry key */
1989 DECL_HANDLER(flush_key)
1991 struct key *key = get_hkey_obj( req->hkey, 0 );
1992 if (key)
1994 /* we don't need to do anything here with the current implementation */
1995 release_object( key );
1999 /* enumerate registry subkeys */
2000 DECL_HANDLER(enum_key)
2002 struct key *key;
2004 if ((key = get_hkey_obj( req->hkey,
2005 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
2007 enum_key( key, req->index, req->info_class, reply );
2008 release_object( key );
2012 /* set a value of a registry key */
2013 DECL_HANDLER(set_key_value)
2015 struct key *key;
2016 struct unicode_str name;
2018 if (req->namelen > get_req_data_size())
2020 set_error( STATUS_INVALID_PARAMETER );
2021 return;
2023 name.str = get_req_data();
2024 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2026 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2028 data_size_t datalen = get_req_data_size() - req->namelen;
2029 const char *data = (const char *)get_req_data() + req->namelen;
2031 set_value( key, &name, req->type, data, datalen );
2032 release_object( key );
2036 /* retrieve the value of a registry key */
2037 DECL_HANDLER(get_key_value)
2039 struct key *key;
2040 struct unicode_str name;
2042 reply->total = 0;
2043 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2045 get_req_unicode_str( &name );
2046 get_value( key, &name, &reply->type, &reply->total );
2047 release_object( key );
2051 /* enumerate the value of a registry key */
2052 DECL_HANDLER(enum_key_value)
2054 struct key *key;
2056 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2058 enum_value( key, req->index, req->info_class, reply );
2059 release_object( key );
2063 /* delete a value of a registry key */
2064 DECL_HANDLER(delete_key_value)
2066 struct key *key;
2067 struct unicode_str name;
2069 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2071 get_req_unicode_str( &name );
2072 delete_value( key, &name );
2073 release_object( key );
2077 /* load a registry branch from a file */
2078 DECL_HANDLER(load_registry)
2080 struct key *key, *parent;
2081 struct token *token = thread_get_impersonation_token( current );
2082 struct unicode_str name;
2084 const LUID_AND_ATTRIBUTES privs[] =
2086 { SeBackupPrivilege, 0 },
2087 { SeRestorePrivilege, 0 },
2090 if (!token || !token_check_privileges( token, TRUE, privs,
2091 sizeof(privs)/sizeof(privs[0]), NULL ))
2093 set_error( STATUS_PRIVILEGE_NOT_HELD );
2094 return;
2097 if ((parent = get_parent_hkey_obj( req->hkey )))
2099 int dummy;
2100 get_req_path( &name, !req->hkey );
2101 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, &dummy )))
2103 load_registry( key, req->file );
2104 release_object( key );
2106 release_object( parent );
2110 DECL_HANDLER(unload_registry)
2112 struct key *key;
2113 struct token *token = thread_get_impersonation_token( current );
2115 const LUID_AND_ATTRIBUTES privs[] =
2117 { SeBackupPrivilege, 0 },
2118 { SeRestorePrivilege, 0 },
2121 if (!token || !token_check_privileges( token, TRUE, privs,
2122 sizeof(privs)/sizeof(privs[0]), NULL ))
2124 set_error( STATUS_PRIVILEGE_NOT_HELD );
2125 return;
2128 if ((key = get_hkey_obj( req->hkey, 0 )))
2130 delete_key( key, 1 ); /* FIXME */
2131 release_object( key );
2135 /* save a registry branch to a file */
2136 DECL_HANDLER(save_registry)
2138 struct key *key;
2140 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2142 set_error( STATUS_PRIVILEGE_NOT_HELD );
2143 return;
2146 if ((key = get_hkey_obj( req->hkey, 0 )))
2148 save_registry( key, req->file );
2149 release_object( key );
2153 /* add a registry key change notification */
2154 DECL_HANDLER(set_registry_notification)
2156 struct key *key;
2157 struct event *event;
2158 struct notify *notify;
2160 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2161 if (key)
2163 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2164 if (event)
2166 notify = find_notify( key, current->process, req->hkey );
2167 if (notify)
2169 if (notify->event)
2170 release_object( notify->event );
2171 grab_object( event );
2172 notify->event = event;
2174 else
2176 notify = mem_alloc( sizeof(*notify) );
2177 if (notify)
2179 grab_object( event );
2180 notify->event = event;
2181 notify->subtree = req->subtree;
2182 notify->filter = req->filter;
2183 notify->hkey = req->hkey;
2184 notify->process = current->process;
2185 list_add_head( &key->notify_list, &notify->entry );
2188 release_object( event );
2190 release_object( key );