ntdll/tests: Account for some timer resolution rounding.
[wine.git] / server / registry.c
blobb7971770728acb0f4339f801d81ac501af1de517
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"
52 struct notify
54 struct list entry; /* entry in list of notifications */
55 struct event **events; /* events to set when changing this key */
56 unsigned int event_count; /* number of events */
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 static const WCHAR key_name[] = {'K','e','y'};
65 struct type_descr key_type =
67 { key_name, sizeof(key_name) }, /* name */
68 KEY_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
69 { /* mapping */
70 STANDARD_RIGHTS_READ | KEY_NOTIFY | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
71 STANDARD_RIGHTS_WRITE | KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
72 STANDARD_RIGHTS_EXECUTE | KEY_CREATE_LINK | KEY_NOTIFY | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
73 KEY_ALL_ACCESS
77 /* a registry key */
78 struct key
80 struct object obj; /* object header */
81 WCHAR *name; /* key name */
82 WCHAR *class; /* key class */
83 unsigned short namelen; /* length of key name */
84 unsigned short classlen; /* length of class name */
85 struct key *parent; /* parent key */
86 int last_subkey; /* last in use subkey */
87 int nb_subkeys; /* count of allocated subkeys */
88 struct key **subkeys; /* subkeys array */
89 int last_value; /* last in use value */
90 int nb_values; /* count of allocated values in array */
91 struct key_value *values; /* values array */
92 unsigned int flags; /* flags */
93 timeout_t modif; /* last modification time */
94 struct list notify_list; /* list of notifications */
97 /* key flags */
98 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
99 #define KEY_DELETED 0x0002 /* key has been deleted */
100 #define KEY_DIRTY 0x0004 /* key has been modified */
101 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
102 #define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
103 #define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
105 /* a key value */
106 struct key_value
108 WCHAR *name; /* value name */
109 unsigned short namelen; /* length of value name */
110 unsigned int type; /* value type */
111 data_size_t len; /* value data length in bytes */
112 void *data; /* pointer to value data */
115 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
116 #define MIN_VALUES 8 /* min. number of allocated values per key */
118 #define MAX_NAME_LEN 256 /* max. length of a key name */
119 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
121 /* the root of the registry tree */
122 static struct key *root_key;
124 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
125 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
126 static struct timeout_user *save_timeout_user; /* saving timer */
127 static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
129 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
130 static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
131 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
132 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
134 static void set_periodic_save_timer(void);
135 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
137 /* information about where to save a registry branch */
138 struct save_branch_info
140 struct key *key;
141 const char *path;
144 #define MAX_SAVE_BRANCH_INFO 3
145 static int save_branch_count;
146 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
148 unsigned int supported_machines_count = 0;
149 unsigned short supported_machines[8];
150 unsigned short native_machine = 0;
152 /* information about a file being loaded */
153 struct file_load_info
155 const char *filename; /* input file name */
156 FILE *file; /* input file */
157 char *buffer; /* line buffer */
158 int len; /* buffer length */
159 int line; /* current input line */
160 WCHAR *tmp; /* temp buffer to use while parsing input */
161 size_t tmplen; /* length of temp buffer */
165 static void key_dump( struct object *obj, int verbose );
166 static unsigned int key_map_access( struct object *obj, unsigned int access );
167 static struct security_descriptor *key_get_sd( struct object *obj );
168 static WCHAR *key_get_full_name( struct object *obj, data_size_t *len );
169 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
170 static void key_destroy( struct object *obj );
172 static const struct object_ops key_ops =
174 sizeof(struct key), /* size */
175 &key_type, /* type */
176 key_dump, /* dump */
177 no_add_queue, /* add_queue */
178 NULL, /* remove_queue */
179 NULL, /* signaled */
180 NULL, /* satisfied */
181 no_signal, /* signal */
182 no_get_fd, /* get_fd */
183 key_map_access, /* map_access */
184 key_get_sd, /* get_sd */
185 default_set_sd, /* set_sd */
186 key_get_full_name, /* get_full_name */
187 no_lookup_name, /* lookup_name */
188 no_link_name, /* link_name */
189 NULL, /* unlink_name */
190 no_open_file, /* open_file */
191 no_kernel_obj_list, /* get_kernel_obj_list */
192 key_close_handle, /* close_handle */
193 key_destroy /* destroy */
197 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
199 return (len == sizeof(wow6432node) && !memicmp_strW( name, wow6432node, sizeof( wow6432node )));
203 * The registry text file format v2 used by this code is similar to the one
204 * used by REGEDIT import/export functionality, with the following differences:
205 * - strings and key names can contain \x escapes for Unicode
206 * - key names use escapes too in order to support Unicode
207 * - the modification time optionally follows the key name
208 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
211 /* dump the full path of a key */
212 static void dump_path( const struct key *key, const struct key *base, FILE *f )
214 if (key->parent && key->parent != base)
216 dump_path( key->parent, base, f );
217 fprintf( f, "\\\\" );
219 dump_strW( key->name, key->namelen, f, "[]" );
222 /* dump a value to a text file */
223 static void dump_value( const struct key_value *value, FILE *f )
225 unsigned int i, dw;
226 int count;
228 if (value->namelen)
230 fputc( '\"', f );
231 count = 1 + dump_strW( value->name, value->namelen, f, "\"\"" );
232 count += fprintf( f, "\"=" );
234 else count = fprintf( f, "@=" );
236 switch(value->type)
238 case REG_SZ:
239 case REG_EXPAND_SZ:
240 case REG_MULTI_SZ:
241 /* only output properly terminated strings in string format */
242 if (value->len < sizeof(WCHAR)) break;
243 if (value->len % sizeof(WCHAR)) break;
244 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
245 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
246 fputc( '\"', f );
247 dump_strW( (WCHAR *)value->data, value->len, f, "\"\"" );
248 fprintf( f, "\"\n" );
249 return;
251 case REG_DWORD:
252 if (value->len != sizeof(dw)) break;
253 memcpy( &dw, value->data, sizeof(dw) );
254 fprintf( f, "dword:%08x\n", dw );
255 return;
258 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
259 else count += fprintf( f, "hex(%x):", value->type );
260 for (i = 0; i < value->len; i++)
262 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
263 if (i < value->len-1)
265 fputc( ',', f );
266 if (++count > 76)
268 fprintf( f, "\\\n " );
269 count = 2;
273 fputc( '\n', f );
276 /* save a registry and all its subkeys to a text file */
277 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
279 int i;
281 if (key->flags & KEY_VOLATILE) return;
282 /* save key if it has either some values or no subkeys, or needs special options */
283 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
284 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
286 fprintf( f, "\n[" );
287 if (key != base) dump_path( key, base, f );
288 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
289 fprintf( f, "#time=%x%08x\n", (unsigned int)(key->modif >> 32), (unsigned int)key->modif );
290 if (key->class)
292 fprintf( f, "#class=\"" );
293 dump_strW( key->class, key->classlen, f, "\"\"" );
294 fprintf( f, "\"\n" );
296 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
297 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
299 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
302 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
304 fprintf( stderr, "%s key ", op );
305 if (key) dump_path( key, NULL, stderr );
306 else fprintf( stderr, "ERROR" );
307 if (value)
309 fprintf( stderr, " value ");
310 dump_value( value, stderr );
312 else fprintf( stderr, "\n" );
315 static void key_dump( struct object *obj, int verbose )
317 struct key *key = (struct key *)obj;
318 assert( obj->ops == &key_ops );
319 fprintf( stderr, "Key flags=%x ", key->flags );
320 dump_path( key, NULL, stderr );
321 fprintf( stderr, "\n" );
324 /* notify waiter and maybe delete the notification */
325 static void do_notification( struct key *key, struct notify *notify, int del )
327 unsigned int i;
329 for (i = 0; i < notify->event_count; ++i)
331 set_event( notify->events[i] );
332 release_object( notify->events[i] );
334 free( notify->events );
335 notify->events = NULL;
336 notify->event_count = 0;
338 if (del)
340 list_remove( &notify->entry );
341 free( notify );
345 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
347 struct notify *notify;
349 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
351 if (notify->process == process && notify->hkey == hkey) return notify;
353 return NULL;
356 static unsigned int key_map_access( struct object *obj, unsigned int access )
358 access = default_map_access( obj, access );
359 /* filter the WOW64 masks, as they aren't real access bits */
360 return access & ~(KEY_WOW64_64KEY | KEY_WOW64_32KEY);
363 static struct security_descriptor *key_get_sd( struct object *obj )
365 static struct security_descriptor *key_default_sd;
367 if (obj->sd) return obj->sd;
369 if (!key_default_sd)
371 size_t users_sid_len = security_sid_len( security_builtin_users_sid );
372 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
373 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
374 + users_sid_len + admins_sid_len;
375 ACCESS_ALLOWED_ACE *aaa;
376 ACL *dacl;
378 key_default_sd = mem_alloc( sizeof(*key_default_sd) + 2 * admins_sid_len + dacl_len );
379 key_default_sd->control = SE_DACL_PRESENT;
380 key_default_sd->owner_len = admins_sid_len;
381 key_default_sd->group_len = admins_sid_len;
382 key_default_sd->sacl_len = 0;
383 key_default_sd->dacl_len = dacl_len;
384 memcpy( key_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
385 memcpy( (char *)(key_default_sd + 1) + admins_sid_len, security_builtin_admins_sid, admins_sid_len );
387 dacl = (ACL *)((char *)(key_default_sd + 1) + 2 * admins_sid_len);
388 dacl->AclRevision = ACL_REVISION;
389 dacl->Sbz1 = 0;
390 dacl->AclSize = dacl_len;
391 dacl->AceCount = 2;
392 dacl->Sbz2 = 0;
393 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
394 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
395 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
396 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
397 aaa->Mask = GENERIC_READ;
398 memcpy( &aaa->SidStart, security_builtin_users_sid, users_sid_len );
399 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
400 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
401 aaa->Header.AceFlags = 0;
402 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
403 aaa->Mask = KEY_ALL_ACCESS;
404 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
406 return key_default_sd;
409 static WCHAR *key_get_full_name( struct object *obj, data_size_t *ret_len )
411 static const WCHAR backslash = '\\';
412 struct key *key = (struct key *) obj;
413 data_size_t len = sizeof(root_name) - sizeof(WCHAR);
414 char *ret;
416 if (key->flags & KEY_DELETED)
418 set_error( STATUS_KEY_DELETED );
419 return NULL;
422 for (key = (struct key *)obj; key != root_key; key = key->parent) len += key->namelen + sizeof(WCHAR);
423 if (!(ret = malloc( len ))) return NULL;
425 *ret_len = len;
426 key = (struct key *)obj;
427 for (key = (struct key *)obj; key != root_key; key = key->parent)
429 memcpy( ret + len - key->namelen, key->name, key->namelen );
430 len -= key->namelen + sizeof(WCHAR);
431 memcpy( ret + len, &backslash, sizeof(WCHAR) );
433 memcpy( ret, root_name, sizeof(root_name) - sizeof(WCHAR) );
434 return (WCHAR *)ret;
437 /* close the notification associated with a handle */
438 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
440 struct key * key = (struct key *) obj;
441 struct notify *notify = find_notify( key, process, handle );
442 if (notify) do_notification( key, notify, 1 );
443 return 1; /* ok to close */
446 static void key_destroy( struct object *obj )
448 int i;
449 struct list *ptr;
450 struct key *key = (struct key *)obj;
451 assert( obj->ops == &key_ops );
453 free( key->name );
454 free( key->class );
455 for (i = 0; i <= key->last_value; i++)
457 free( key->values[i].name );
458 free( key->values[i].data );
460 free( key->values );
461 for (i = 0; i <= key->last_subkey; i++)
463 key->subkeys[i]->parent = NULL;
464 release_object( key->subkeys[i] );
466 free( key->subkeys );
467 /* unconditionally notify everything waiting on this key */
468 while ((ptr = list_head( &key->notify_list )))
470 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
471 do_notification( key, notify, 1 );
475 /* get the request vararg as registry path */
476 static inline void get_req_path( struct unicode_str *str, int skip_root )
478 str->str = get_req_data();
479 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
481 if (skip_root && str->len >= sizeof(root_name) && !memicmp_strW( str->str, root_name, sizeof(root_name) ))
483 str->str += ARRAY_SIZE( root_name );
484 str->len -= sizeof(root_name);
488 /* return the next token in a given path */
489 /* token->str must point inside the path, or be NULL for the first call */
490 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
492 data_size_t i = 0, len = path->len / sizeof(WCHAR);
494 if (!token->str) /* first time */
496 /* path cannot start with a backslash */
497 if (len && path->str[0] == '\\')
499 set_error( STATUS_OBJECT_PATH_INVALID );
500 return NULL;
503 else
505 i = token->str - path->str;
506 i += token->len / sizeof(WCHAR);
507 while (i < len && path->str[i] == '\\') i++;
509 token->str = path->str + i;
510 while (i < len && path->str[i] != '\\') i++;
511 token->len = (path->str + i - token->str) * sizeof(WCHAR);
512 return token;
515 /* allocate a key object */
516 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
518 struct key *key;
519 if ((key = alloc_object( &key_ops )))
521 key->name = NULL;
522 key->class = NULL;
523 key->namelen = name->len;
524 key->classlen = 0;
525 key->flags = 0;
526 key->last_subkey = -1;
527 key->nb_subkeys = 0;
528 key->subkeys = NULL;
529 key->nb_values = 0;
530 key->last_value = -1;
531 key->values = NULL;
532 key->modif = modif;
533 key->parent = NULL;
534 list_init( &key->notify_list );
535 if (name->len && !(key->name = memdup( name->str, name->len )))
537 release_object( key );
538 key = NULL;
541 return key;
544 /* mark a key and all its parents as dirty (modified) */
545 static void make_dirty( struct key *key )
547 while (key)
549 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
550 key->flags |= KEY_DIRTY;
551 key = key->parent;
555 /* mark a key and all its subkeys as clean (not modified) */
556 static void make_clean( struct key *key )
558 int i;
560 if (key->flags & KEY_VOLATILE) return;
561 if (!(key->flags & KEY_DIRTY)) return;
562 key->flags &= ~KEY_DIRTY;
563 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
566 /* go through all the notifications and send them if necessary */
567 static void check_notify( struct key *key, unsigned int change, int not_subtree )
569 struct list *ptr, *next;
571 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
573 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
574 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
575 do_notification( key, n, 0 );
579 /* update key modification time */
580 static void touch_key( struct key *key, unsigned int change )
582 struct key *k;
584 key->modif = current_time;
585 make_dirty( key );
587 /* do notifications */
588 check_notify( key, change, 1 );
589 for ( k = key->parent; k; k = k->parent )
590 check_notify( k, change, 0 );
593 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
594 static int grow_subkeys( struct key *key )
596 struct key **new_subkeys;
597 int nb_subkeys;
599 if (key->nb_subkeys)
601 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
602 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
604 set_error( STATUS_NO_MEMORY );
605 return 0;
608 else
610 nb_subkeys = MIN_SUBKEYS;
611 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
613 key->subkeys = new_subkeys;
614 key->nb_subkeys = nb_subkeys;
615 return 1;
618 /* allocate a subkey for a given key, and return its index */
619 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
620 int index, timeout_t modif )
622 struct key *key;
623 int i;
625 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
627 set_error( STATUS_INVALID_PARAMETER );
628 return NULL;
630 if (parent->last_subkey + 1 == parent->nb_subkeys)
632 /* need to grow the array */
633 if (!grow_subkeys( parent )) return NULL;
635 if ((key = alloc_key( name, modif )) != NULL)
637 key->parent = parent;
638 for (i = ++parent->last_subkey; i > index; i--)
639 parent->subkeys[i] = parent->subkeys[i-1];
640 parent->subkeys[index] = key;
641 if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
642 parent->flags |= KEY_WOW64;
644 return key;
647 /* free a subkey of a given key */
648 static void free_subkey( struct key *parent, int index )
650 struct key *key;
651 int i, nb_subkeys;
653 assert( index >= 0 );
654 assert( index <= parent->last_subkey );
656 key = parent->subkeys[index];
657 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
658 parent->last_subkey--;
659 key->flags |= KEY_DELETED;
660 key->parent = NULL;
661 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
662 release_object( key );
664 /* try to shrink the array */
665 nb_subkeys = parent->nb_subkeys;
666 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
668 struct key **new_subkeys;
669 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
670 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
671 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
672 parent->subkeys = new_subkeys;
673 parent->nb_subkeys = nb_subkeys;
677 /* find the named child of a given key and return its index */
678 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
680 int i, min, max, res;
681 data_size_t len;
683 min = 0;
684 max = key->last_subkey;
685 while (min <= max)
687 i = (min + max) / 2;
688 len = min( key->subkeys[i]->namelen, name->len );
689 res = memicmp_strW( key->subkeys[i]->name, name->str, len );
690 if (!res) res = key->subkeys[i]->namelen - name->len;
691 if (!res)
693 *index = i;
694 return key->subkeys[i];
696 if (res > 0) max = i - 1;
697 else min = i + 1;
699 *index = min; /* this is where we should insert it */
700 return NULL;
703 /* return the wow64 variant of the key, or the key itself if none */
704 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
706 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
707 int index;
709 if (!(key->flags & KEY_WOW64)) return key;
710 if (!is_wow6432node( name->str, name->len ))
712 key = find_subkey( key, &wow6432node_str, &index );
713 assert( key ); /* if KEY_WOW64 is set we must find it */
715 return key;
719 /* follow a symlink and return the resolved key */
720 static struct key *follow_symlink( struct key *key, int iteration )
722 struct unicode_str path, token;
723 struct key_value *value;
724 int index;
726 if (iteration > 16) return NULL;
727 if (!(key->flags & KEY_SYMLINK)) return key;
728 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
730 path.str = value->data;
731 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
732 if (path.len <= sizeof(root_name)) return NULL;
733 if (memicmp_strW( path.str, root_name, sizeof(root_name) )) return NULL;
734 path.str += ARRAY_SIZE( root_name );
735 path.len -= sizeof(root_name);
737 key = root_key;
738 token.str = NULL;
739 if (!get_path_token( &path, &token )) return NULL;
740 while (token.len)
742 if (!(key = find_subkey( key, &token, &index ))) break;
743 if (!(key = follow_symlink( key, iteration + 1 ))) break;
744 get_path_token( &path, &token );
746 return key;
749 /* open a key until we find an element that doesn't exist */
750 /* helper for open_key and create_key */
751 static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
752 unsigned int access, struct unicode_str *token, int *index )
754 token->str = NULL;
755 if (!get_path_token( name, token )) return NULL;
756 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
757 while (token->len)
759 struct key *subkey;
760 if (!(subkey = find_subkey( key, token, index )))
762 if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
764 /* try in the 64-bit parent */
765 key = key->parent;
766 subkey = find_subkey( key, token, index );
769 if (!subkey) break;
770 key = subkey;
771 get_path_token( name, token );
772 if (!token->len) break;
773 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
774 if (!(key = follow_symlink( key, 0 )))
776 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
777 return NULL;
780 return key;
783 /* open a subkey */
784 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
785 unsigned int attributes )
787 int index;
788 struct unicode_str token;
790 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
792 if (token.len)
794 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
795 return NULL;
797 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
798 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
800 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
801 return NULL;
803 if (debug_level > 1) dump_operation( key, NULL, "Open" );
804 grab_object( key );
805 return key;
808 /* create a subkey */
809 static struct key *create_key( struct key *key, const struct unicode_str *name,
810 const struct unicode_str *class, unsigned int options,
811 unsigned int access, unsigned int attributes,
812 const struct security_descriptor *sd, int *created )
814 int index;
815 struct unicode_str token, next;
817 *created = 0;
818 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
820 if (!token.len) /* the key already exists */
822 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
823 if (options & REG_OPTION_CREATE_LINK)
825 set_error( STATUS_OBJECT_NAME_COLLISION );
826 return NULL;
828 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
830 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
831 return NULL;
833 if (debug_level > 1) dump_operation( key, NULL, "Open" );
834 grab_object( key );
835 return key;
838 /* token must be the last path component at this point */
839 next = token;
840 get_path_token( name, &next );
841 if (next.len)
843 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
844 return NULL;
847 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
849 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
850 return NULL;
852 *created = 1;
853 make_dirty( key );
854 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
856 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
857 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
858 else key->flags |= KEY_DIRTY;
860 if (sd) default_set_sd( &key->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
861 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
863 if (debug_level > 1) dump_operation( key, NULL, "Create" );
864 if (class && class->len)
866 key->classlen = class->len;
867 free(key->class);
868 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
870 touch_key( key->parent, REG_NOTIFY_CHANGE_NAME );
871 grab_object( key );
872 return key;
875 /* recursively create a subkey (for internal use only) */
876 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
878 struct key *base;
879 int index;
880 struct unicode_str token;
882 token.str = NULL;
883 if (!get_path_token( name, &token )) return NULL;
884 while (token.len)
886 struct key *subkey;
887 if (!(subkey = find_subkey( key, &token, &index ))) break;
888 key = subkey;
889 if (!(key = follow_symlink( key, 0 )))
891 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
892 return NULL;
894 get_path_token( name, &token );
897 if (token.len)
899 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
900 base = key;
901 for (;;)
903 get_path_token( name, &token );
904 if (!token.len) break;
905 /* we know the index is always 0 in a new key */
906 if (!(key = alloc_subkey( key, &token, 0, modif )))
908 free_subkey( base, index );
909 return NULL;
914 grab_object( key );
915 return key;
918 /* query information about a key or a subkey */
919 static void enum_key( struct key *key, int index, int info_class, struct enum_key_reply *reply )
921 int i;
922 data_size_t len, namelen, classlen;
923 data_size_t max_subkey = 0, max_class = 0;
924 data_size_t max_value = 0, max_data = 0;
925 WCHAR *fullname = NULL;
926 char *data;
928 if (index != -1) /* -1 means use the specified key directly */
930 if ((index < 0) || (index > key->last_subkey))
932 set_error( STATUS_NO_MORE_ENTRIES );
933 return;
935 key = key->subkeys[index];
938 namelen = key->namelen;
939 classlen = key->classlen;
941 switch(info_class)
943 case KeyNameInformation:
944 if (!(fullname = key->obj.ops->get_full_name( &key->obj, &namelen ))) return;
945 /* fall through */
946 case KeyBasicInformation:
947 classlen = 0; /* only return the name */
948 /* fall through */
949 case KeyNodeInformation:
950 reply->max_subkey = 0;
951 reply->max_class = 0;
952 reply->max_value = 0;
953 reply->max_data = 0;
954 break;
955 case KeyFullInformation:
956 case KeyCachedInformation:
957 for (i = 0; i <= key->last_subkey; i++)
959 if (key->subkeys[i]->namelen > max_subkey) max_subkey = key->subkeys[i]->namelen;
960 if (key->subkeys[i]->classlen > max_class) max_class = key->subkeys[i]->classlen;
962 for (i = 0; i <= key->last_value; i++)
964 if (key->values[i].namelen > max_value) max_value = key->values[i].namelen;
965 if (key->values[i].len > max_data) max_data = key->values[i].len;
967 reply->max_subkey = max_subkey;
968 reply->max_class = max_class;
969 reply->max_value = max_value;
970 reply->max_data = max_data;
971 reply->namelen = namelen;
972 if (info_class == KeyCachedInformation)
973 classlen = 0; /* don't return any data, only its size */
974 namelen = 0; /* don't return name */
975 break;
976 default:
977 set_error( STATUS_INVALID_PARAMETER );
978 return;
980 reply->subkeys = key->last_subkey + 1;
981 reply->values = key->last_value + 1;
982 reply->modif = key->modif;
983 reply->total = namelen + classlen;
985 len = min( reply->total, get_reply_max_size() );
986 if (len && (data = set_reply_data_size( len )))
988 if (len > namelen)
990 reply->namelen = namelen;
991 memcpy( data, key->name, namelen );
992 memcpy( data + namelen, key->class, len - namelen );
994 else if (info_class == KeyNameInformation)
996 reply->namelen = namelen;
997 memcpy( data, fullname, len );
999 else
1001 reply->namelen = len;
1002 memcpy( data, key->name, len );
1005 free( fullname );
1006 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
1009 /* delete a key and its values */
1010 static int delete_key( struct key *key, int recurse )
1012 int index;
1013 struct key *parent = key->parent;
1015 /* must find parent and index */
1016 if (key == root_key)
1018 set_error( STATUS_ACCESS_DENIED );
1019 return -1;
1021 assert( parent );
1023 while (recurse && (key->last_subkey>=0))
1024 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
1025 return -1;
1027 for (index = 0; index <= parent->last_subkey; index++)
1028 if (parent->subkeys[index] == key) break;
1029 assert( index <= parent->last_subkey );
1031 /* we can only delete a key that has no subkeys */
1032 if (key->last_subkey >= 0)
1034 set_error( STATUS_ACCESS_DENIED );
1035 return -1;
1038 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
1039 free_subkey( parent, index );
1040 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
1041 return 0;
1044 /* try to grow the array of values; return 1 if OK, 0 on error */
1045 static int grow_values( struct key *key )
1047 struct key_value *new_val;
1048 int nb_values;
1050 if (key->nb_values)
1052 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
1053 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
1055 set_error( STATUS_NO_MEMORY );
1056 return 0;
1059 else
1061 nb_values = MIN_VALUES;
1062 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
1064 key->values = new_val;
1065 key->nb_values = nb_values;
1066 return 1;
1069 /* find the named value of a given key and return its index in the array */
1070 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
1072 int i, min, max, res;
1073 data_size_t len;
1075 min = 0;
1076 max = key->last_value;
1077 while (min <= max)
1079 i = (min + max) / 2;
1080 len = min( key->values[i].namelen, name->len );
1081 res = memicmp_strW( key->values[i].name, name->str, len );
1082 if (!res) res = key->values[i].namelen - name->len;
1083 if (!res)
1085 *index = i;
1086 return &key->values[i];
1088 if (res > 0) max = i - 1;
1089 else min = i + 1;
1091 *index = min; /* this is where we should insert it */
1092 return NULL;
1095 /* insert a new value; the index must have been returned by find_value */
1096 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
1098 struct key_value *value;
1099 WCHAR *new_name = NULL;
1100 int i;
1102 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
1104 set_error( STATUS_NAME_TOO_LONG );
1105 return NULL;
1107 if (key->last_value + 1 == key->nb_values)
1109 if (!grow_values( key )) return NULL;
1111 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1112 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1113 value = &key->values[index];
1114 value->name = new_name;
1115 value->namelen = name->len;
1116 value->len = 0;
1117 value->data = NULL;
1118 return value;
1121 /* set a key value */
1122 static void set_value( struct key *key, const struct unicode_str *name,
1123 int type, const void *data, data_size_t len )
1125 struct key_value *value;
1126 void *ptr = NULL;
1127 int index;
1129 if ((value = find_value( key, name, &index )))
1131 /* check if the new value is identical to the existing one */
1132 if (value->type == type && value->len == len &&
1133 value->data && !memcmp( value->data, data, len ))
1135 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1136 return;
1140 if (key->flags & KEY_SYMLINK)
1142 if (type != REG_LINK || name->len != symlink_str.len ||
1143 memicmp_strW( name->str, symlink_str.str, name->len ))
1145 set_error( STATUS_ACCESS_DENIED );
1146 return;
1150 if (len && !(ptr = memdup( data, len ))) return;
1152 if (!value)
1154 if (!(value = insert_value( key, name, index )))
1156 free( ptr );
1157 return;
1160 else free( value->data ); /* already existing, free previous data */
1162 value->type = type;
1163 value->len = len;
1164 value->data = ptr;
1165 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1166 if (debug_level > 1) dump_operation( key, value, "Set" );
1169 /* get a key value */
1170 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1172 struct key_value *value;
1173 int index;
1175 if ((value = find_value( key, name, &index )))
1177 *type = value->type;
1178 *len = value->len;
1179 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1180 if (debug_level > 1) dump_operation( key, value, "Get" );
1182 else
1184 *type = -1;
1185 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1189 /* enumerate a key value */
1190 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1192 struct key_value *value;
1194 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1195 else
1197 void *data;
1198 data_size_t namelen, maxlen;
1200 value = &key->values[i];
1201 reply->type = value->type;
1202 namelen = value->namelen;
1204 switch(info_class)
1206 case KeyValueBasicInformation:
1207 reply->total = namelen;
1208 break;
1209 case KeyValueFullInformation:
1210 reply->total = namelen + value->len;
1211 break;
1212 case KeyValuePartialInformation:
1213 reply->total = value->len;
1214 namelen = 0;
1215 break;
1216 default:
1217 set_error( STATUS_INVALID_PARAMETER );
1218 return;
1221 maxlen = min( reply->total, get_reply_max_size() );
1222 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1224 if (maxlen > namelen)
1226 reply->namelen = namelen;
1227 memcpy( data, value->name, namelen );
1228 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1230 else
1232 reply->namelen = maxlen;
1233 memcpy( data, value->name, maxlen );
1236 if (debug_level > 1) dump_operation( key, value, "Enum" );
1240 /* delete a value */
1241 static void delete_value( struct key *key, const struct unicode_str *name )
1243 struct key_value *value;
1244 int i, index, nb_values;
1246 if (!(value = find_value( key, name, &index )))
1248 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1249 return;
1251 if (debug_level > 1) dump_operation( key, value, "Delete" );
1252 free( value->name );
1253 free( value->data );
1254 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1255 key->last_value--;
1256 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1258 /* try to shrink the array */
1259 nb_values = key->nb_values;
1260 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1262 struct key_value *new_val;
1263 nb_values -= nb_values / 3; /* shrink by 33% */
1264 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1265 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1266 key->values = new_val;
1267 key->nb_values = nb_values;
1271 /* get the registry key corresponding to an hkey handle */
1272 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1274 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1276 if (key && key->flags & KEY_DELETED)
1278 set_error( STATUS_KEY_DELETED );
1279 release_object( key );
1280 key = NULL;
1282 return key;
1285 /* get the registry key corresponding to a parent key handle */
1286 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1288 if (!hkey) return (struct key *)grab_object( root_key );
1289 return get_hkey_obj( hkey, 0 );
1292 /* read a line from the input file */
1293 static int read_next_line( struct file_load_info *info )
1295 char *newbuf;
1296 int newlen, pos = 0;
1298 info->line++;
1299 for (;;)
1301 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1302 return (pos != 0); /* EOF */
1303 pos = strlen(info->buffer);
1304 if (info->buffer[pos-1] == '\n')
1306 /* got a full line */
1307 info->buffer[--pos] = 0;
1308 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1309 return 1;
1311 if (pos < info->len - 1) return 1; /* EOF but something was read */
1313 /* need to enlarge the buffer */
1314 newlen = info->len + info->len / 2;
1315 if (!(newbuf = realloc( info->buffer, newlen )))
1317 set_error( STATUS_NO_MEMORY );
1318 return -1;
1320 info->buffer = newbuf;
1321 info->len = newlen;
1325 /* make sure the temp buffer holds enough space */
1326 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1328 WCHAR *tmp;
1329 if (info->tmplen >= size) return 1;
1330 if (!(tmp = realloc( info->tmp, size )))
1332 set_error( STATUS_NO_MEMORY );
1333 return 0;
1335 info->tmp = tmp;
1336 info->tmplen = size;
1337 return 1;
1340 /* report an error while loading an input file */
1341 static void file_read_error( const char *err, struct file_load_info *info )
1343 if (info->filename)
1344 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1345 else
1346 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1349 /* convert a data type tag to a value type */
1350 static int get_data_type( const char *buffer, int *type, int *parse_type )
1352 struct data_type { const char *tag; int len; int type; int parse_type; };
1354 static const struct data_type data_types[] =
1355 { /* actual type */ /* type to assume for parsing */
1356 { "\"", 1, REG_SZ, REG_SZ },
1357 { "str:\"", 5, REG_SZ, REG_SZ },
1358 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1359 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1360 { "hex:", 4, REG_BINARY, REG_BINARY },
1361 { "dword:", 6, REG_DWORD, REG_DWORD },
1362 { "hex(", 4, -1, REG_BINARY },
1363 { NULL, 0, 0, 0 }
1366 const struct data_type *ptr;
1367 char *end;
1369 for (ptr = data_types; ptr->tag; ptr++)
1371 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1372 *parse_type = ptr->parse_type;
1373 if ((*type = ptr->type) != -1) return ptr->len;
1374 /* "hex(xx):" is special */
1375 *type = (int)strtoul( buffer + 4, &end, 16 );
1376 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1377 return end + 2 - buffer;
1379 return 0;
1382 /* load and create a key from the input file */
1383 static struct key *load_key( struct key *base, const char *buffer, int prefix_len,
1384 struct file_load_info *info, timeout_t *modif )
1386 WCHAR *p;
1387 struct unicode_str name;
1388 int res;
1389 unsigned int mod;
1390 data_size_t len;
1392 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1394 len = info->tmplen;
1395 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1397 file_read_error( "Malformed key", info );
1398 return NULL;
1400 if (sscanf( buffer + res, " %u", &mod ) == 1)
1401 *modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1402 else
1403 *modif = current_time;
1405 p = info->tmp;
1406 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1408 if (!*p)
1410 if (prefix_len > 1)
1412 file_read_error( "Malformed key", info );
1413 return NULL;
1415 /* empty key name, return base key */
1416 return (struct key *)grab_object( base );
1418 name.str = p;
1419 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1420 return create_key_recursive( base, &name, 0 );
1423 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1424 static void update_key_time( struct key *key, timeout_t modif )
1426 while (key && !key->modif)
1428 key->modif = modif;
1429 key = key->parent;
1433 /* load a global option from the input file */
1434 static int load_global_option( const char *buffer, struct file_load_info *info )
1436 const char *p;
1438 if (!strncmp( buffer, "#arch=", 6 ))
1440 enum prefix_type type;
1441 p = buffer + 6;
1442 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1443 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1444 else
1446 file_read_error( "Unknown architecture", info );
1447 set_error( STATUS_NOT_REGISTRY_FILE );
1448 return 0;
1450 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1451 else if (type != prefix_type)
1453 file_read_error( "Mismatched architecture", info );
1454 set_error( STATUS_NOT_REGISTRY_FILE );
1455 return 0;
1458 /* ignore unknown options */
1459 return 1;
1462 /* load a key option from the input file */
1463 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1465 const char *p;
1466 data_size_t len;
1468 if (!strncmp( buffer, "#time=", 6 ))
1470 timeout_t modif = 0;
1471 for (p = buffer + 6; *p; p++)
1473 if (*p >= '0' && *p <= '9') modif = (modif << 4) | (*p - '0');
1474 else if (*p >= 'A' && *p <= 'F') modif = (modif << 4) | (*p - 'A' + 10);
1475 else if (*p >= 'a' && *p <= 'f') modif = (modif << 4) | (*p - 'a' + 10);
1476 else break;
1478 update_key_time( key, modif );
1480 if (!strncmp( buffer, "#class=", 7 ))
1482 p = buffer + 7;
1483 if (*p++ != '"') return 0;
1484 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1485 len = info->tmplen;
1486 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1487 free( key->class );
1488 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1489 key->classlen = len;
1491 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1492 /* ignore unknown options */
1493 return 1;
1496 /* parse a comma-separated list of hex digits */
1497 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1499 const char *p = buffer;
1500 data_size_t count = 0;
1501 char *end;
1503 while (isxdigit(*p))
1505 unsigned int val = strtoul( p, &end, 16 );
1506 if (end == p || val > 0xff) return -1;
1507 if (count++ >= *len) return -1; /* dest buffer overflow */
1508 *dest++ = val;
1509 p = end;
1510 while (isspace(*p)) p++;
1511 if (*p == ',') p++;
1512 while (isspace(*p)) p++;
1514 *len = count;
1515 return p - buffer;
1518 /* parse a value name and create the corresponding value */
1519 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1520 struct file_load_info *info )
1522 struct key_value *value;
1523 struct unicode_str name;
1524 int index;
1526 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1527 name.str = info->tmp;
1528 name.len = info->tmplen;
1529 if (buffer[0] == '@')
1531 name.len = 0;
1532 *len = 1;
1534 else
1536 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1537 if (r == -1) goto error;
1538 *len = r + 1; /* for initial quote */
1539 name.len -= sizeof(WCHAR); /* terminating null */
1541 while (isspace(buffer[*len])) (*len)++;
1542 if (buffer[*len] != '=') goto error;
1543 (*len)++;
1544 while (isspace(buffer[*len])) (*len)++;
1545 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1546 return value;
1548 error:
1549 file_read_error( "Malformed value name", info );
1550 return NULL;
1553 /* load a value from the input file */
1554 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1556 DWORD dw;
1557 void *ptr, *newptr;
1558 int res, type, parse_type;
1559 data_size_t maxlen, len;
1560 struct key_value *value;
1562 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1563 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1564 buffer += len + res;
1566 switch(parse_type)
1568 case REG_SZ:
1569 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1570 len = info->tmplen;
1571 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1572 ptr = info->tmp;
1573 break;
1574 case REG_DWORD:
1575 dw = strtoul( buffer, NULL, 16 );
1576 ptr = &dw;
1577 len = sizeof(dw);
1578 break;
1579 case REG_BINARY: /* hex digits */
1580 len = 0;
1581 for (;;)
1583 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1584 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1585 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1586 len += maxlen;
1587 buffer += res;
1588 while (isspace(*buffer)) buffer++;
1589 if (!*buffer) break;
1590 if (*buffer != '\\') goto error;
1591 if (read_next_line( info) != 1) goto error;
1592 buffer = info->buffer;
1593 while (isspace(*buffer)) buffer++;
1595 ptr = info->tmp;
1596 break;
1597 default:
1598 assert(0);
1599 ptr = NULL; /* keep compiler quiet */
1600 break;
1603 if (!len) newptr = NULL;
1604 else if (!(newptr = memdup( ptr, len ))) return 0;
1606 free( value->data );
1607 value->data = newptr;
1608 value->len = len;
1609 value->type = type;
1610 return 1;
1612 error:
1613 file_read_error( "Malformed value", info );
1614 free( value->data );
1615 value->data = NULL;
1616 value->len = 0;
1617 value->type = REG_NONE;
1618 return 0;
1621 /* return the length (in path elements) of name that is part of the key name */
1622 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1623 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1625 WCHAR *p;
1626 int res;
1627 data_size_t len;
1629 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1631 len = info->tmplen;
1632 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1634 file_read_error( "Malformed key", info );
1635 return 0;
1637 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1638 len = (p - info->tmp) * sizeof(WCHAR);
1639 for (res = 1; key != root_key; res++)
1641 if (len == key->namelen && !memicmp_strW( info->tmp, key->name, len )) break;
1642 key = key->parent;
1644 if (key == root_key) res = 0; /* no matching name */
1645 return res;
1648 /* load all the keys from the input file */
1649 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1650 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1652 struct key *subkey = NULL;
1653 struct file_load_info info;
1654 timeout_t modif = current_time;
1655 char *p;
1657 info.filename = filename;
1658 info.file = f;
1659 info.len = 4;
1660 info.tmplen = 4;
1661 info.line = 0;
1662 if (!(info.buffer = mem_alloc( info.len ))) return;
1663 if (!(info.tmp = mem_alloc( info.tmplen )))
1665 free( info.buffer );
1666 return;
1669 if ((read_next_line( &info ) != 1) ||
1670 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1672 set_error( STATUS_NOT_REGISTRY_FILE );
1673 goto done;
1676 while (read_next_line( &info ) == 1)
1678 p = info.buffer;
1679 while (*p && isspace(*p)) p++;
1680 switch(*p)
1682 case '[': /* new key */
1683 if (subkey)
1685 update_key_time( subkey, modif );
1686 release_object( subkey );
1688 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1689 if (!(subkey = load_key( key, p + 1, prefix_len, &info, &modif )))
1690 file_read_error( "Error creating key", &info );
1691 break;
1692 case '@': /* default value */
1693 case '\"': /* value */
1694 if (subkey) load_value( subkey, p, &info );
1695 else file_read_error( "Value without key", &info );
1696 break;
1697 case '#': /* option */
1698 if (subkey) load_key_option( subkey, p, &info );
1699 else if (!load_global_option( p, &info )) goto done;
1700 break;
1701 case ';': /* comment */
1702 case 0: /* empty line */
1703 break;
1704 default:
1705 file_read_error( "Unrecognized input", &info );
1706 break;
1710 done:
1711 if (subkey)
1713 update_key_time( subkey, modif );
1714 release_object( subkey );
1716 free( info.buffer );
1717 free( info.tmp );
1720 /* load a part of the registry from a file */
1721 static void load_registry( struct key *key, obj_handle_t handle )
1723 struct file *file;
1724 int fd;
1726 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1727 fd = dup( get_file_unix_fd( file ) );
1728 release_object( file );
1729 if (fd != -1)
1731 FILE *f = fdopen( fd, "r" );
1732 if (f)
1734 load_keys( key, NULL, f, -1 );
1735 fclose( f );
1737 else file_set_error();
1741 /* load one of the initial registry files */
1742 static int load_init_registry_from_file( const char *filename, struct key *key )
1744 FILE *f;
1746 if ((f = fopen( filename, "r" )))
1748 load_keys( key, filename, f, 0 );
1749 fclose( f );
1750 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1752 fprintf( stderr, "%s is not a valid registry file\n", filename );
1753 return 1;
1757 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1759 save_branch_info[save_branch_count].path = filename;
1760 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1761 make_object_permanent( &key->obj );
1762 return (f != NULL);
1765 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1767 char buffer[7 + 11 + 11 + 11 * SID_MAX_SUB_AUTHORITIES], *p = buffer;
1768 unsigned int i;
1770 p += sprintf( p, "User\\S-%u-%u", sid->Revision,
1771 MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1772 sid->IdentifierAuthority.Value[4] ),
1773 MAKEWORD( sid->IdentifierAuthority.Value[3],
1774 sid->IdentifierAuthority.Value[2] )));
1775 for (i = 0; i < sid->SubAuthorityCount; i++) p += sprintf( p, "-%u", sid->SubAuthority[i] );
1776 return ascii_to_unicode_str( buffer, path );
1779 static void init_supported_machines(void)
1781 unsigned int count = 0;
1782 #ifdef __i386__
1783 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1784 #elif defined(__x86_64__)
1785 if (prefix_type == PREFIX_64BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_AMD64;
1786 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1787 #elif defined(__arm__)
1788 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1789 #elif defined(__aarch64__)
1790 if (prefix_type == PREFIX_64BIT)
1792 supported_machines[count++] = IMAGE_FILE_MACHINE_ARM64;
1793 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1795 supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1796 #else
1797 #error Unsupported machine
1798 #endif
1799 supported_machines_count = count;
1800 native_machine = supported_machines[0];
1803 /* registry initialisation */
1804 void init_registry(void)
1806 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1807 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1808 static const WCHAR classes_i386[] = {'S','o','f','t','w','a','r','e','\\',
1809 'C','l','a','s','s','e','s','\\',
1810 'W','o','w','6','4','3','2','N','o','d','e'};
1811 static const WCHAR classes_amd64[] = {'S','o','f','t','w','a','r','e','\\',
1812 'C','l','a','s','s','e','s','\\',
1813 'W','o','w','6','4','6','4','N','o','d','e'};
1814 static const WCHAR classes_arm[] = {'S','o','f','t','w','a','r','e','\\',
1815 'C','l','a','s','s','e','s','\\',
1816 'W','o','w','A','A','3','2','N','o','d','e'};
1817 static const WCHAR classes_arm64[] = {'S','o','f','t','w','a','r','e','\\',
1818 'C','l','a','s','s','e','s','\\',
1819 'W','o','w','A','A','6','4','N','o','d','e'};
1820 static const struct unicode_str root_name = { NULL, 0 };
1821 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1822 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1824 WCHAR *current_user_path;
1825 struct unicode_str current_user_str;
1826 struct key *key, *hklm, *hkcu;
1827 unsigned int i;
1828 char *p;
1830 /* switch to the config dir */
1832 if (fchdir( config_dir_fd ) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno ));
1834 /* create the root key */
1835 root_key = alloc_key( &root_name, current_time );
1836 assert( root_key );
1837 make_object_permanent( &root_key->obj );
1839 /* load system.reg into Registry\Machine */
1841 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1842 fatal_error( "could not create Machine registry key\n" );
1844 if (!load_init_registry_from_file( "system.reg", hklm ))
1846 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1847 prefix_type = PREFIX_32BIT;
1848 else
1849 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1851 else if (prefix_type == PREFIX_UNKNOWN)
1852 prefix_type = PREFIX_32BIT;
1854 init_supported_machines();
1856 /* load userdef.reg into Registry\User\.Default */
1858 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1859 fatal_error( "could not create User\\.Default registry key\n" );
1861 load_init_registry_from_file( "userdef.reg", key );
1862 release_object( key );
1864 /* load user.reg into HKEY_CURRENT_USER */
1866 /* FIXME: match default user in token.c. should get from process token instead */
1867 current_user_path = format_user_registry_path( security_local_user_sid, &current_user_str );
1868 if (!current_user_path ||
1869 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1870 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1871 free( current_user_path );
1872 load_init_registry_from_file( "user.reg", hkcu );
1874 /* set the shared flag on Software\Classes\Wow6432Node for all platforms */
1875 for (i = 1; i < supported_machines_count; i++)
1877 struct unicode_str name;
1879 switch (supported_machines[i])
1881 case IMAGE_FILE_MACHINE_I386: name.str = classes_i386; name.len = sizeof(classes_i386); break;
1882 case IMAGE_FILE_MACHINE_ARMNT: name.str = classes_arm; name.len = sizeof(classes_arm); break;
1883 case IMAGE_FILE_MACHINE_AMD64: name.str = classes_amd64; name.len = sizeof(classes_amd64); break;
1884 case IMAGE_FILE_MACHINE_ARM64: name.str = classes_arm64; name.len = sizeof(classes_arm64); break;
1886 if ((key = create_key_recursive( hklm, &name, current_time )))
1888 key->flags |= KEY_WOWSHARE;
1889 release_object( key );
1891 /* FIXME: handle HKCU too */
1894 release_object( hklm );
1895 release_object( hkcu );
1897 /* start the periodic save timer */
1898 set_periodic_save_timer();
1900 /* create windows directories */
1902 if (!mkdir( "drive_c/windows", 0777 ))
1904 mkdir( "drive_c/windows/system32", 0777 );
1905 for (i = 1; i < supported_machines_count; i++)
1907 switch (supported_machines[i])
1909 case IMAGE_FILE_MACHINE_I386: mkdir( "drive_c/windows/syswow64", 0777 ); break;
1910 case IMAGE_FILE_MACHINE_ARMNT: mkdir( "drive_c/windows/sysarm32", 0777 ); break;
1911 case IMAGE_FILE_MACHINE_AMD64: mkdir( "drive_c/windows/sysx8664", 0777 ); break;
1912 case IMAGE_FILE_MACHINE_ARM64: mkdir( "drive_c/windows/sysarm64", 0777 ); break;
1917 /* go back to the server dir */
1918 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
1921 /* save a registry branch to a file */
1922 static void save_all_subkeys( struct key *key, FILE *f )
1924 fprintf( f, "WINE REGISTRY Version 2\n" );
1925 fprintf( f, ";; All keys relative to " );
1926 dump_path( key, NULL, f );
1927 fprintf( f, "\n" );
1928 switch (prefix_type)
1930 case PREFIX_32BIT:
1931 fprintf( f, "\n#arch=win32\n" );
1932 break;
1933 case PREFIX_64BIT:
1934 fprintf( f, "\n#arch=win64\n" );
1935 break;
1936 default:
1937 break;
1939 save_subkeys( key, key, f );
1942 /* save a registry branch to a file handle */
1943 static void save_registry( struct key *key, obj_handle_t handle )
1945 struct file *file;
1946 int fd;
1948 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1949 fd = dup( get_file_unix_fd( file ) );
1950 release_object( file );
1951 if (fd != -1)
1953 FILE *f = fdopen( fd, "w" );
1954 if (f)
1956 save_all_subkeys( key, f );
1957 if (fclose( f )) file_set_error();
1959 else
1961 file_set_error();
1962 close( fd );
1967 /* save a registry branch to a file */
1968 static int save_branch( struct key *key, const char *path )
1970 struct stat st;
1971 char *p, *tmp = NULL;
1972 int fd, count = 0, ret = 0;
1973 FILE *f;
1975 if (!(key->flags & KEY_DIRTY))
1977 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1978 return 1;
1981 /* test the file type */
1983 if ((fd = open( path, O_WRONLY )) != -1)
1985 /* if file is not a regular file or has multiple links or is accessed
1986 * via symbolic links, write directly into it; otherwise use a temp file */
1987 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1989 ftruncate( fd, 0 );
1990 goto save;
1992 close( fd );
1995 /* create a temp file in the same directory */
1997 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1998 strcpy( tmp, path );
1999 if ((p = strrchr( tmp, '/' ))) p++;
2000 else p = tmp;
2001 for (;;)
2003 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
2004 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
2005 if (errno != EEXIST) goto done;
2006 close( fd );
2009 /* now save to it */
2011 save:
2012 if (!(f = fdopen( fd, "w" )))
2014 if (tmp) unlink( tmp );
2015 close( fd );
2016 goto done;
2019 if (debug_level > 1)
2021 fprintf( stderr, "%s: ", path );
2022 dump_operation( key, NULL, "saving" );
2025 save_all_subkeys( key, f );
2026 ret = !fclose(f);
2028 if (tmp)
2030 /* if successfully written, rename to final name */
2031 if (ret) ret = !rename( tmp, path );
2032 if (!ret) unlink( tmp );
2035 done:
2036 free( tmp );
2037 if (ret) make_clean( key );
2038 return ret;
2041 /* periodic saving of the registry */
2042 static void periodic_save( void *arg )
2044 int i;
2046 if (fchdir( config_dir_fd ) == -1) return;
2047 save_timeout_user = NULL;
2048 for (i = 0; i < save_branch_count; i++)
2049 save_branch( save_branch_info[i].key, save_branch_info[i].path );
2050 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2051 set_periodic_save_timer();
2054 /* start the periodic save timer */
2055 static void set_periodic_save_timer(void)
2057 if (save_timeout_user) remove_timeout_user( save_timeout_user );
2058 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
2061 /* save the modified registry branches to disk */
2062 void flush_registry(void)
2064 int i;
2066 if (fchdir( config_dir_fd ) == -1) return;
2067 for (i = 0; i < save_branch_count; i++)
2069 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
2071 fprintf( stderr, "wineserver: could not save registry branch to %s",
2072 save_branch_info[i].path );
2073 perror( " " );
2076 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2079 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2080 static int is_wow64_thread( struct thread *thread )
2082 return (is_machine_64bit( native_machine ) && !is_machine_64bit( thread->process->machine ));
2086 /* create a registry key */
2087 DECL_HANDLER(create_key)
2089 struct key *key = NULL, *parent;
2090 struct unicode_str name, class;
2091 unsigned int access = req->access;
2092 const struct security_descriptor *sd;
2093 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2095 if (!objattr) return;
2097 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2099 class.str = get_req_data_after_objattr( objattr, &class.len );
2100 class.len = (class.len / sizeof(WCHAR)) * sizeof(WCHAR);
2102 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2103 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2105 name.str += ARRAY_SIZE( root_name );
2106 name.len -= sizeof(root_name);
2109 /* NOTE: no access rights are required from the parent handle to create a key */
2110 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2112 if ((key = create_key( parent, &name, &class, req->options, access,
2113 objattr->attributes, sd, &reply->created )))
2115 reply->hkey = alloc_handle( current->process, key, access, objattr->attributes );
2116 release_object( key );
2118 release_object( parent );
2122 /* open a registry key */
2123 DECL_HANDLER(open_key)
2125 struct key *key, *parent;
2126 struct unicode_str name;
2127 unsigned int access = req->access;
2129 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2131 reply->hkey = 0;
2132 /* NOTE: no access rights are required to open the parent key, only the child key */
2133 if ((parent = get_parent_hkey_obj( req->parent )))
2135 get_req_path( &name, !req->parent );
2136 if ((key = open_key( parent, &name, access, req->attributes )))
2138 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
2139 release_object( key );
2141 release_object( parent );
2145 /* delete a registry key */
2146 DECL_HANDLER(delete_key)
2148 struct key *key;
2150 if ((key = get_hkey_obj( req->hkey, DELETE )))
2152 delete_key( key, 0);
2153 release_object( key );
2157 /* flush a registry key */
2158 DECL_HANDLER(flush_key)
2160 struct key *key = get_hkey_obj( req->hkey, 0 );
2161 if (key)
2163 /* we don't need to do anything here with the current implementation */
2164 release_object( key );
2168 /* enumerate registry subkeys */
2169 DECL_HANDLER(enum_key)
2171 struct key *key;
2173 if ((key = get_hkey_obj( req->hkey,
2174 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
2176 enum_key( key, req->index, req->info_class, reply );
2177 release_object( key );
2181 /* set a value of a registry key */
2182 DECL_HANDLER(set_key_value)
2184 struct key *key;
2185 struct unicode_str name;
2187 if (req->namelen > get_req_data_size())
2189 set_error( STATUS_INVALID_PARAMETER );
2190 return;
2192 name.str = get_req_data();
2193 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2195 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2197 data_size_t datalen = get_req_data_size() - req->namelen;
2198 const char *data = (const char *)get_req_data() + req->namelen;
2200 set_value( key, &name, req->type, data, datalen );
2201 release_object( key );
2205 /* retrieve the value of a registry key */
2206 DECL_HANDLER(get_key_value)
2208 struct key *key;
2209 struct unicode_str name = get_req_unicode_str();
2211 reply->total = 0;
2212 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2214 get_value( key, &name, &reply->type, &reply->total );
2215 release_object( key );
2219 /* enumerate the value of a registry key */
2220 DECL_HANDLER(enum_key_value)
2222 struct key *key;
2224 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2226 enum_value( key, req->index, req->info_class, reply );
2227 release_object( key );
2231 /* delete a value of a registry key */
2232 DECL_HANDLER(delete_key_value)
2234 struct key *key;
2235 struct unicode_str name = get_req_unicode_str();
2237 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2239 delete_value( key, &name );
2240 release_object( key );
2244 /* load a registry branch from a file */
2245 DECL_HANDLER(load_registry)
2247 struct key *key, *parent;
2248 struct unicode_str name;
2249 const struct security_descriptor *sd;
2250 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2252 if (!objattr) return;
2254 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2256 set_error( STATUS_PRIVILEGE_NOT_HELD );
2257 return;
2260 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2261 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2263 name.str += ARRAY_SIZE( root_name );
2264 name.len -= sizeof(root_name);
2267 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2269 int dummy;
2270 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, sd, &dummy )))
2272 load_registry( key, req->file );
2273 release_object( key );
2275 release_object( parent );
2279 DECL_HANDLER(unload_registry)
2281 struct key *key, *parent;
2282 struct unicode_str name;
2283 unsigned int access = 0;
2285 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2287 set_error( STATUS_PRIVILEGE_NOT_HELD );
2288 return;
2291 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2293 if ((parent = get_parent_hkey_obj( req->parent )))
2295 get_req_path( &name, !req->parent );
2296 if ((key = open_key( parent, &name, access, req->attributes )))
2298 if (key->obj.handle_count)
2299 set_error( STATUS_CANNOT_DELETE );
2300 else
2301 delete_key( key, 1 ); /* FIXME */
2302 release_object( key );
2304 release_object( parent );
2308 /* save a registry branch to a file */
2309 DECL_HANDLER(save_registry)
2311 struct key *key;
2313 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2315 set_error( STATUS_PRIVILEGE_NOT_HELD );
2316 return;
2319 if ((key = get_hkey_obj( req->hkey, 0 )))
2321 save_registry( key, req->file );
2322 release_object( key );
2326 /* add a registry key change notification */
2327 DECL_HANDLER(set_registry_notification)
2329 struct key *key;
2330 struct event *event;
2331 struct notify *notify;
2333 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2334 if (key)
2336 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2337 if (event)
2339 notify = find_notify( key, current->process, req->hkey );
2340 if (!notify)
2342 notify = mem_alloc( sizeof(*notify) );
2343 if (notify)
2345 notify->events = NULL;
2346 notify->event_count = 0;
2347 notify->subtree = req->subtree;
2348 notify->filter = req->filter;
2349 notify->hkey = req->hkey;
2350 notify->process = current->process;
2351 list_add_head( &key->notify_list, &notify->entry );
2354 if (notify)
2356 struct event **new_array;
2358 if ((new_array = realloc( notify->events, (notify->event_count + 1) * sizeof(*notify->events) )))
2360 notify->events = new_array;
2361 notify->events[notify->event_count++] = (struct event *)grab_object( event );
2362 reset_event( event );
2363 set_error( STATUS_PENDING );
2365 else set_error( STATUS_NO_MEMORY );
2367 release_object( event );
2369 release_object( key );