wined3d: Return the map pitch in wined3d_device_context_map().
[wine.git] / server / registry.c
blobf36760e6cc110a1f31994d8e9dd14b8c06c3a019
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 for (key = (struct key *)obj; key != root_key; key = key->parent) len += key->namelen + sizeof(WCHAR);
417 if (!(ret = malloc( len ))) return NULL;
419 *ret_len = len;
420 key = (struct key *)obj;
421 for (key = (struct key *)obj; key != root_key; key = key->parent)
423 memcpy( ret + len - key->namelen, key->name, key->namelen );
424 len -= key->namelen + sizeof(WCHAR);
425 memcpy( ret + len, &backslash, sizeof(WCHAR) );
427 memcpy( ret, root_name, sizeof(root_name) - sizeof(WCHAR) );
428 return (WCHAR *)ret;
431 /* close the notification associated with a handle */
432 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
434 struct key * key = (struct key *) obj;
435 struct notify *notify = find_notify( key, process, handle );
436 if (notify) do_notification( key, notify, 1 );
437 return 1; /* ok to close */
440 static void key_destroy( struct object *obj )
442 int i;
443 struct list *ptr;
444 struct key *key = (struct key *)obj;
445 assert( obj->ops == &key_ops );
447 free( key->name );
448 free( key->class );
449 for (i = 0; i <= key->last_value; i++)
451 free( key->values[i].name );
452 free( key->values[i].data );
454 free( key->values );
455 for (i = 0; i <= key->last_subkey; i++)
457 key->subkeys[i]->parent = NULL;
458 release_object( key->subkeys[i] );
460 free( key->subkeys );
461 /* unconditionally notify everything waiting on this key */
462 while ((ptr = list_head( &key->notify_list )))
464 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
465 do_notification( key, notify, 1 );
469 /* get the request vararg as registry path */
470 static inline void get_req_path( struct unicode_str *str, int skip_root )
472 str->str = get_req_data();
473 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
475 if (skip_root && str->len >= sizeof(root_name) && !memicmp_strW( str->str, root_name, sizeof(root_name) ))
477 str->str += ARRAY_SIZE( root_name );
478 str->len -= sizeof(root_name);
482 /* return the next token in a given path */
483 /* token->str must point inside the path, or be NULL for the first call */
484 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
486 data_size_t i = 0, len = path->len / sizeof(WCHAR);
488 if (!token->str) /* first time */
490 /* path cannot start with a backslash */
491 if (len && path->str[0] == '\\')
493 set_error( STATUS_OBJECT_PATH_INVALID );
494 return NULL;
497 else
499 i = token->str - path->str;
500 i += token->len / sizeof(WCHAR);
501 while (i < len && path->str[i] == '\\') i++;
503 token->str = path->str + i;
504 while (i < len && path->str[i] != '\\') i++;
505 token->len = (path->str + i - token->str) * sizeof(WCHAR);
506 return token;
509 /* allocate a key object */
510 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
512 struct key *key;
513 if ((key = alloc_object( &key_ops )))
515 key->name = NULL;
516 key->class = NULL;
517 key->namelen = name->len;
518 key->classlen = 0;
519 key->flags = 0;
520 key->last_subkey = -1;
521 key->nb_subkeys = 0;
522 key->subkeys = NULL;
523 key->nb_values = 0;
524 key->last_value = -1;
525 key->values = NULL;
526 key->modif = modif;
527 key->parent = NULL;
528 list_init( &key->notify_list );
529 if (name->len && !(key->name = memdup( name->str, name->len )))
531 release_object( key );
532 key = NULL;
535 return key;
538 /* mark a key and all its parents as dirty (modified) */
539 static void make_dirty( struct key *key )
541 while (key)
543 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
544 key->flags |= KEY_DIRTY;
545 key = key->parent;
549 /* mark a key and all its subkeys as clean (not modified) */
550 static void make_clean( struct key *key )
552 int i;
554 if (key->flags & KEY_VOLATILE) return;
555 if (!(key->flags & KEY_DIRTY)) return;
556 key->flags &= ~KEY_DIRTY;
557 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
560 /* go through all the notifications and send them if necessary */
561 static void check_notify( struct key *key, unsigned int change, int not_subtree )
563 struct list *ptr, *next;
565 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
567 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
568 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
569 do_notification( key, n, 0 );
573 /* update key modification time */
574 static void touch_key( struct key *key, unsigned int change )
576 struct key *k;
578 key->modif = current_time;
579 make_dirty( key );
581 /* do notifications */
582 check_notify( key, change, 1 );
583 for ( k = key->parent; k; k = k->parent )
584 check_notify( k, change, 0 );
587 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
588 static int grow_subkeys( struct key *key )
590 struct key **new_subkeys;
591 int nb_subkeys;
593 if (key->nb_subkeys)
595 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
596 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
598 set_error( STATUS_NO_MEMORY );
599 return 0;
602 else
604 nb_subkeys = MIN_SUBKEYS;
605 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
607 key->subkeys = new_subkeys;
608 key->nb_subkeys = nb_subkeys;
609 return 1;
612 /* allocate a subkey for a given key, and return its index */
613 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
614 int index, timeout_t modif )
616 struct key *key;
617 int i;
619 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
621 set_error( STATUS_INVALID_PARAMETER );
622 return NULL;
624 if (parent->last_subkey + 1 == parent->nb_subkeys)
626 /* need to grow the array */
627 if (!grow_subkeys( parent )) return NULL;
629 if ((key = alloc_key( name, modif )) != NULL)
631 key->parent = parent;
632 for (i = ++parent->last_subkey; i > index; i--)
633 parent->subkeys[i] = parent->subkeys[i-1];
634 parent->subkeys[index] = key;
635 if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
636 parent->flags |= KEY_WOW64;
638 return key;
641 /* free a subkey of a given key */
642 static void free_subkey( struct key *parent, int index )
644 struct key *key;
645 int i, nb_subkeys;
647 assert( index >= 0 );
648 assert( index <= parent->last_subkey );
650 key = parent->subkeys[index];
651 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
652 parent->last_subkey--;
653 key->flags |= KEY_DELETED;
654 key->parent = NULL;
655 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
656 release_object( key );
658 /* try to shrink the array */
659 nb_subkeys = parent->nb_subkeys;
660 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
662 struct key **new_subkeys;
663 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
664 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
665 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
666 parent->subkeys = new_subkeys;
667 parent->nb_subkeys = nb_subkeys;
671 /* find the named child of a given key and return its index */
672 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
674 int i, min, max, res;
675 data_size_t len;
677 min = 0;
678 max = key->last_subkey;
679 while (min <= max)
681 i = (min + max) / 2;
682 len = min( key->subkeys[i]->namelen, name->len );
683 res = memicmp_strW( key->subkeys[i]->name, name->str, len );
684 if (!res) res = key->subkeys[i]->namelen - name->len;
685 if (!res)
687 *index = i;
688 return key->subkeys[i];
690 if (res > 0) max = i - 1;
691 else min = i + 1;
693 *index = min; /* this is where we should insert it */
694 return NULL;
697 /* return the wow64 variant of the key, or the key itself if none */
698 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
700 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
701 int index;
703 if (!(key->flags & KEY_WOW64)) return key;
704 if (!is_wow6432node( name->str, name->len ))
706 key = find_subkey( key, &wow6432node_str, &index );
707 assert( key ); /* if KEY_WOW64 is set we must find it */
709 return key;
713 /* follow a symlink and return the resolved key */
714 static struct key *follow_symlink( struct key *key, int iteration )
716 struct unicode_str path, token;
717 struct key_value *value;
718 int index;
720 if (iteration > 16) return NULL;
721 if (!(key->flags & KEY_SYMLINK)) return key;
722 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
724 path.str = value->data;
725 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
726 if (path.len <= sizeof(root_name)) return NULL;
727 if (memicmp_strW( path.str, root_name, sizeof(root_name) )) return NULL;
728 path.str += ARRAY_SIZE( root_name );
729 path.len -= sizeof(root_name);
731 key = root_key;
732 token.str = NULL;
733 if (!get_path_token( &path, &token )) return NULL;
734 while (token.len)
736 if (!(key = find_subkey( key, &token, &index ))) break;
737 if (!(key = follow_symlink( key, iteration + 1 ))) break;
738 get_path_token( &path, &token );
740 return key;
743 /* open a key until we find an element that doesn't exist */
744 /* helper for open_key and create_key */
745 static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
746 unsigned int access, struct unicode_str *token, int *index )
748 token->str = NULL;
749 if (!get_path_token( name, token )) return NULL;
750 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
751 while (token->len)
753 struct key *subkey;
754 if (!(subkey = find_subkey( key, token, index )))
756 if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
758 /* try in the 64-bit parent */
759 key = key->parent;
760 subkey = find_subkey( key, token, index );
763 if (!subkey) break;
764 key = subkey;
765 get_path_token( name, token );
766 if (!token->len) break;
767 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
768 if (!(key = follow_symlink( key, 0 )))
770 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
771 return NULL;
774 return key;
777 /* open a subkey */
778 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
779 unsigned int attributes )
781 int index;
782 struct unicode_str token;
784 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
786 if (token.len)
788 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
789 return NULL;
791 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
792 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
794 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
795 return NULL;
797 if (debug_level > 1) dump_operation( key, NULL, "Open" );
798 grab_object( key );
799 return key;
802 /* create a subkey */
803 static struct key *create_key( struct key *key, const struct unicode_str *name,
804 const struct unicode_str *class, unsigned int options,
805 unsigned int access, unsigned int attributes,
806 const struct security_descriptor *sd, int *created )
808 int index;
809 struct unicode_str token, next;
811 *created = 0;
812 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
814 if (!token.len) /* the key already exists */
816 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
817 if (options & REG_OPTION_CREATE_LINK)
819 set_error( STATUS_OBJECT_NAME_COLLISION );
820 return NULL;
822 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
824 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
825 return NULL;
827 if (debug_level > 1) dump_operation( key, NULL, "Open" );
828 grab_object( key );
829 return key;
832 /* token must be the last path component at this point */
833 next = token;
834 get_path_token( name, &next );
835 if (next.len)
837 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
838 return NULL;
841 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
843 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
844 return NULL;
846 *created = 1;
847 make_dirty( key );
848 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
850 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
851 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
852 else key->flags |= KEY_DIRTY;
854 if (sd) default_set_sd( &key->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
855 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
857 if (debug_level > 1) dump_operation( key, NULL, "Create" );
858 if (class && class->len)
860 key->classlen = class->len;
861 free(key->class);
862 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
864 touch_key( key->parent, REG_NOTIFY_CHANGE_NAME );
865 grab_object( key );
866 return key;
869 /* recursively create a subkey (for internal use only) */
870 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
872 struct key *base;
873 int index;
874 struct unicode_str token;
876 token.str = NULL;
877 if (!get_path_token( name, &token )) return NULL;
878 while (token.len)
880 struct key *subkey;
881 if (!(subkey = find_subkey( key, &token, &index ))) break;
882 key = subkey;
883 if (!(key = follow_symlink( key, 0 )))
885 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
886 return NULL;
888 get_path_token( name, &token );
891 if (token.len)
893 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
894 base = key;
895 for (;;)
897 get_path_token( name, &token );
898 if (!token.len) break;
899 /* we know the index is always 0 in a new key */
900 if (!(key = alloc_subkey( key, &token, 0, modif )))
902 free_subkey( base, index );
903 return NULL;
908 grab_object( key );
909 return key;
912 /* query information about a key or a subkey */
913 static void enum_key( struct key *key, int index, int info_class, struct enum_key_reply *reply )
915 int i;
916 data_size_t len, namelen, classlen;
917 data_size_t max_subkey = 0, max_class = 0;
918 data_size_t max_value = 0, max_data = 0;
919 WCHAR *fullname = NULL;
920 char *data;
922 if (index != -1) /* -1 means use the specified key directly */
924 if ((index < 0) || (index > key->last_subkey))
926 set_error( STATUS_NO_MORE_ENTRIES );
927 return;
929 key = key->subkeys[index];
932 namelen = key->namelen;
933 classlen = key->classlen;
935 switch(info_class)
937 case KeyNameInformation:
938 if (!(fullname = key->obj.ops->get_full_name( &key->obj, &namelen ))) return;
939 /* fall through */
940 case KeyBasicInformation:
941 classlen = 0; /* only return the name */
942 /* fall through */
943 case KeyNodeInformation:
944 reply->max_subkey = 0;
945 reply->max_class = 0;
946 reply->max_value = 0;
947 reply->max_data = 0;
948 break;
949 case KeyFullInformation:
950 case KeyCachedInformation:
951 for (i = 0; i <= key->last_subkey; i++)
953 if (key->subkeys[i]->namelen > max_subkey) max_subkey = key->subkeys[i]->namelen;
954 if (key->subkeys[i]->classlen > max_class) max_class = key->subkeys[i]->classlen;
956 for (i = 0; i <= key->last_value; i++)
958 if (key->values[i].namelen > max_value) max_value = key->values[i].namelen;
959 if (key->values[i].len > max_data) max_data = key->values[i].len;
961 reply->max_subkey = max_subkey;
962 reply->max_class = max_class;
963 reply->max_value = max_value;
964 reply->max_data = max_data;
965 reply->namelen = namelen;
966 if (info_class == KeyCachedInformation)
967 classlen = 0; /* don't return any data, only its size */
968 namelen = 0; /* don't return name */
969 break;
970 default:
971 set_error( STATUS_INVALID_PARAMETER );
972 return;
974 reply->subkeys = key->last_subkey + 1;
975 reply->values = key->last_value + 1;
976 reply->modif = key->modif;
977 reply->total = namelen + classlen;
979 len = min( reply->total, get_reply_max_size() );
980 if (len && (data = set_reply_data_size( len )))
982 if (len > namelen)
984 reply->namelen = namelen;
985 memcpy( data, key->name, namelen );
986 memcpy( data + namelen, key->class, len - namelen );
988 else if (info_class == KeyNameInformation)
990 reply->namelen = namelen;
991 memcpy( data, fullname, len );
993 else
995 reply->namelen = len;
996 memcpy( data, key->name, len );
999 free( fullname );
1000 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
1003 /* delete a key and its values */
1004 static int delete_key( struct key *key, int recurse )
1006 int index;
1007 struct key *parent = key->parent;
1009 /* must find parent and index */
1010 if (key == root_key)
1012 set_error( STATUS_ACCESS_DENIED );
1013 return -1;
1015 assert( parent );
1017 while (recurse && (key->last_subkey>=0))
1018 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
1019 return -1;
1021 for (index = 0; index <= parent->last_subkey; index++)
1022 if (parent->subkeys[index] == key) break;
1023 assert( index <= parent->last_subkey );
1025 /* we can only delete a key that has no subkeys */
1026 if (key->last_subkey >= 0)
1028 set_error( STATUS_ACCESS_DENIED );
1029 return -1;
1032 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
1033 free_subkey( parent, index );
1034 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
1035 return 0;
1038 /* try to grow the array of values; return 1 if OK, 0 on error */
1039 static int grow_values( struct key *key )
1041 struct key_value *new_val;
1042 int nb_values;
1044 if (key->nb_values)
1046 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
1047 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
1049 set_error( STATUS_NO_MEMORY );
1050 return 0;
1053 else
1055 nb_values = MIN_VALUES;
1056 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
1058 key->values = new_val;
1059 key->nb_values = nb_values;
1060 return 1;
1063 /* find the named value of a given key and return its index in the array */
1064 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
1066 int i, min, max, res;
1067 data_size_t len;
1069 min = 0;
1070 max = key->last_value;
1071 while (min <= max)
1073 i = (min + max) / 2;
1074 len = min( key->values[i].namelen, name->len );
1075 res = memicmp_strW( key->values[i].name, name->str, len );
1076 if (!res) res = key->values[i].namelen - name->len;
1077 if (!res)
1079 *index = i;
1080 return &key->values[i];
1082 if (res > 0) max = i - 1;
1083 else min = i + 1;
1085 *index = min; /* this is where we should insert it */
1086 return NULL;
1089 /* insert a new value; the index must have been returned by find_value */
1090 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
1092 struct key_value *value;
1093 WCHAR *new_name = NULL;
1094 int i;
1096 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
1098 set_error( STATUS_NAME_TOO_LONG );
1099 return NULL;
1101 if (key->last_value + 1 == key->nb_values)
1103 if (!grow_values( key )) return NULL;
1105 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1106 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1107 value = &key->values[index];
1108 value->name = new_name;
1109 value->namelen = name->len;
1110 value->len = 0;
1111 value->data = NULL;
1112 return value;
1115 /* set a key value */
1116 static void set_value( struct key *key, const struct unicode_str *name,
1117 int type, const void *data, data_size_t len )
1119 struct key_value *value;
1120 void *ptr = NULL;
1121 int index;
1123 if ((value = find_value( key, name, &index )))
1125 /* check if the new value is identical to the existing one */
1126 if (value->type == type && value->len == len &&
1127 value->data && !memcmp( value->data, data, len ))
1129 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1130 return;
1134 if (key->flags & KEY_SYMLINK)
1136 if (type != REG_LINK || name->len != symlink_str.len ||
1137 memicmp_strW( name->str, symlink_str.str, name->len ))
1139 set_error( STATUS_ACCESS_DENIED );
1140 return;
1144 if (len && !(ptr = memdup( data, len ))) return;
1146 if (!value)
1148 if (!(value = insert_value( key, name, index )))
1150 free( ptr );
1151 return;
1154 else free( value->data ); /* already existing, free previous data */
1156 value->type = type;
1157 value->len = len;
1158 value->data = ptr;
1159 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1160 if (debug_level > 1) dump_operation( key, value, "Set" );
1163 /* get a key value */
1164 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1166 struct key_value *value;
1167 int index;
1169 if ((value = find_value( key, name, &index )))
1171 *type = value->type;
1172 *len = value->len;
1173 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1174 if (debug_level > 1) dump_operation( key, value, "Get" );
1176 else
1178 *type = -1;
1179 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1183 /* enumerate a key value */
1184 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1186 struct key_value *value;
1188 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1189 else
1191 void *data;
1192 data_size_t namelen, maxlen;
1194 value = &key->values[i];
1195 reply->type = value->type;
1196 namelen = value->namelen;
1198 switch(info_class)
1200 case KeyValueBasicInformation:
1201 reply->total = namelen;
1202 break;
1203 case KeyValueFullInformation:
1204 reply->total = namelen + value->len;
1205 break;
1206 case KeyValuePartialInformation:
1207 reply->total = value->len;
1208 namelen = 0;
1209 break;
1210 default:
1211 set_error( STATUS_INVALID_PARAMETER );
1212 return;
1215 maxlen = min( reply->total, get_reply_max_size() );
1216 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1218 if (maxlen > namelen)
1220 reply->namelen = namelen;
1221 memcpy( data, value->name, namelen );
1222 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1224 else
1226 reply->namelen = maxlen;
1227 memcpy( data, value->name, maxlen );
1230 if (debug_level > 1) dump_operation( key, value, "Enum" );
1234 /* delete a value */
1235 static void delete_value( struct key *key, const struct unicode_str *name )
1237 struct key_value *value;
1238 int i, index, nb_values;
1240 if (!(value = find_value( key, name, &index )))
1242 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1243 return;
1245 if (debug_level > 1) dump_operation( key, value, "Delete" );
1246 free( value->name );
1247 free( value->data );
1248 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1249 key->last_value--;
1250 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1252 /* try to shrink the array */
1253 nb_values = key->nb_values;
1254 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1256 struct key_value *new_val;
1257 nb_values -= nb_values / 3; /* shrink by 33% */
1258 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1259 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1260 key->values = new_val;
1261 key->nb_values = nb_values;
1265 /* get the registry key corresponding to an hkey handle */
1266 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1268 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1270 if (key && key->flags & KEY_DELETED)
1272 set_error( STATUS_KEY_DELETED );
1273 release_object( key );
1274 key = NULL;
1276 return key;
1279 /* get the registry key corresponding to a parent key handle */
1280 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1282 if (!hkey) return (struct key *)grab_object( root_key );
1283 return get_hkey_obj( hkey, 0 );
1286 /* read a line from the input file */
1287 static int read_next_line( struct file_load_info *info )
1289 char *newbuf;
1290 int newlen, pos = 0;
1292 info->line++;
1293 for (;;)
1295 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1296 return (pos != 0); /* EOF */
1297 pos = strlen(info->buffer);
1298 if (info->buffer[pos-1] == '\n')
1300 /* got a full line */
1301 info->buffer[--pos] = 0;
1302 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1303 return 1;
1305 if (pos < info->len - 1) return 1; /* EOF but something was read */
1307 /* need to enlarge the buffer */
1308 newlen = info->len + info->len / 2;
1309 if (!(newbuf = realloc( info->buffer, newlen )))
1311 set_error( STATUS_NO_MEMORY );
1312 return -1;
1314 info->buffer = newbuf;
1315 info->len = newlen;
1319 /* make sure the temp buffer holds enough space */
1320 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1322 WCHAR *tmp;
1323 if (info->tmplen >= size) return 1;
1324 if (!(tmp = realloc( info->tmp, size )))
1326 set_error( STATUS_NO_MEMORY );
1327 return 0;
1329 info->tmp = tmp;
1330 info->tmplen = size;
1331 return 1;
1334 /* report an error while loading an input file */
1335 static void file_read_error( const char *err, struct file_load_info *info )
1337 if (info->filename)
1338 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1339 else
1340 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1343 /* convert a data type tag to a value type */
1344 static int get_data_type( const char *buffer, int *type, int *parse_type )
1346 struct data_type { const char *tag; int len; int type; int parse_type; };
1348 static const struct data_type data_types[] =
1349 { /* actual type */ /* type to assume for parsing */
1350 { "\"", 1, REG_SZ, REG_SZ },
1351 { "str:\"", 5, REG_SZ, REG_SZ },
1352 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1353 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1354 { "hex:", 4, REG_BINARY, REG_BINARY },
1355 { "dword:", 6, REG_DWORD, REG_DWORD },
1356 { "hex(", 4, -1, REG_BINARY },
1357 { NULL, 0, 0, 0 }
1360 const struct data_type *ptr;
1361 char *end;
1363 for (ptr = data_types; ptr->tag; ptr++)
1365 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1366 *parse_type = ptr->parse_type;
1367 if ((*type = ptr->type) != -1) return ptr->len;
1368 /* "hex(xx):" is special */
1369 *type = (int)strtoul( buffer + 4, &end, 16 );
1370 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1371 return end + 2 - buffer;
1373 return 0;
1376 /* load and create a key from the input file */
1377 static struct key *load_key( struct key *base, const char *buffer, int prefix_len,
1378 struct file_load_info *info, timeout_t *modif )
1380 WCHAR *p;
1381 struct unicode_str name;
1382 int res;
1383 unsigned int mod;
1384 data_size_t len;
1386 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1388 len = info->tmplen;
1389 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1391 file_read_error( "Malformed key", info );
1392 return NULL;
1394 if (sscanf( buffer + res, " %u", &mod ) == 1)
1395 *modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1396 else
1397 *modif = current_time;
1399 p = info->tmp;
1400 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1402 if (!*p)
1404 if (prefix_len > 1)
1406 file_read_error( "Malformed key", info );
1407 return NULL;
1409 /* empty key name, return base key */
1410 return (struct key *)grab_object( base );
1412 name.str = p;
1413 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1414 return create_key_recursive( base, &name, 0 );
1417 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1418 static void update_key_time( struct key *key, timeout_t modif )
1420 while (key && !key->modif)
1422 key->modif = modif;
1423 key = key->parent;
1427 /* load a global option from the input file */
1428 static int load_global_option( const char *buffer, struct file_load_info *info )
1430 const char *p;
1432 if (!strncmp( buffer, "#arch=", 6 ))
1434 enum prefix_type type;
1435 p = buffer + 6;
1436 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1437 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1438 else
1440 file_read_error( "Unknown architecture", info );
1441 set_error( STATUS_NOT_REGISTRY_FILE );
1442 return 0;
1444 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1445 else if (type != prefix_type)
1447 file_read_error( "Mismatched architecture", info );
1448 set_error( STATUS_NOT_REGISTRY_FILE );
1449 return 0;
1452 /* ignore unknown options */
1453 return 1;
1456 /* load a key option from the input file */
1457 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1459 const char *p;
1460 data_size_t len;
1462 if (!strncmp( buffer, "#time=", 6 ))
1464 timeout_t modif = 0;
1465 for (p = buffer + 6; *p; p++)
1467 if (*p >= '0' && *p <= '9') modif = (modif << 4) | (*p - '0');
1468 else if (*p >= 'A' && *p <= 'F') modif = (modif << 4) | (*p - 'A' + 10);
1469 else if (*p >= 'a' && *p <= 'f') modif = (modif << 4) | (*p - 'a' + 10);
1470 else break;
1472 update_key_time( key, modif );
1474 if (!strncmp( buffer, "#class=", 7 ))
1476 p = buffer + 7;
1477 if (*p++ != '"') return 0;
1478 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1479 len = info->tmplen;
1480 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1481 free( key->class );
1482 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1483 key->classlen = len;
1485 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1486 /* ignore unknown options */
1487 return 1;
1490 /* parse a comma-separated list of hex digits */
1491 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1493 const char *p = buffer;
1494 data_size_t count = 0;
1495 char *end;
1497 while (isxdigit(*p))
1499 unsigned int val = strtoul( p, &end, 16 );
1500 if (end == p || val > 0xff) return -1;
1501 if (count++ >= *len) return -1; /* dest buffer overflow */
1502 *dest++ = val;
1503 p = end;
1504 while (isspace(*p)) p++;
1505 if (*p == ',') p++;
1506 while (isspace(*p)) p++;
1508 *len = count;
1509 return p - buffer;
1512 /* parse a value name and create the corresponding value */
1513 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1514 struct file_load_info *info )
1516 struct key_value *value;
1517 struct unicode_str name;
1518 int index;
1520 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1521 name.str = info->tmp;
1522 name.len = info->tmplen;
1523 if (buffer[0] == '@')
1525 name.len = 0;
1526 *len = 1;
1528 else
1530 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1531 if (r == -1) goto error;
1532 *len = r + 1; /* for initial quote */
1533 name.len -= sizeof(WCHAR); /* terminating null */
1535 while (isspace(buffer[*len])) (*len)++;
1536 if (buffer[*len] != '=') goto error;
1537 (*len)++;
1538 while (isspace(buffer[*len])) (*len)++;
1539 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1540 return value;
1542 error:
1543 file_read_error( "Malformed value name", info );
1544 return NULL;
1547 /* load a value from the input file */
1548 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1550 DWORD dw;
1551 void *ptr, *newptr;
1552 int res, type, parse_type;
1553 data_size_t maxlen, len;
1554 struct key_value *value;
1556 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1557 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1558 buffer += len + res;
1560 switch(parse_type)
1562 case REG_SZ:
1563 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1564 len = info->tmplen;
1565 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1566 ptr = info->tmp;
1567 break;
1568 case REG_DWORD:
1569 dw = strtoul( buffer, NULL, 16 );
1570 ptr = &dw;
1571 len = sizeof(dw);
1572 break;
1573 case REG_BINARY: /* hex digits */
1574 len = 0;
1575 for (;;)
1577 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1578 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1579 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1580 len += maxlen;
1581 buffer += res;
1582 while (isspace(*buffer)) buffer++;
1583 if (!*buffer) break;
1584 if (*buffer != '\\') goto error;
1585 if (read_next_line( info) != 1) goto error;
1586 buffer = info->buffer;
1587 while (isspace(*buffer)) buffer++;
1589 ptr = info->tmp;
1590 break;
1591 default:
1592 assert(0);
1593 ptr = NULL; /* keep compiler quiet */
1594 break;
1597 if (!len) newptr = NULL;
1598 else if (!(newptr = memdup( ptr, len ))) return 0;
1600 free( value->data );
1601 value->data = newptr;
1602 value->len = len;
1603 value->type = type;
1604 return 1;
1606 error:
1607 file_read_error( "Malformed value", info );
1608 free( value->data );
1609 value->data = NULL;
1610 value->len = 0;
1611 value->type = REG_NONE;
1612 return 0;
1615 /* return the length (in path elements) of name that is part of the key name */
1616 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1617 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1619 WCHAR *p;
1620 int res;
1621 data_size_t len;
1623 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1625 len = info->tmplen;
1626 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1628 file_read_error( "Malformed key", info );
1629 return 0;
1631 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1632 len = (p - info->tmp) * sizeof(WCHAR);
1633 for (res = 1; key != root_key; res++)
1635 if (len == key->namelen && !memicmp_strW( info->tmp, key->name, len )) break;
1636 key = key->parent;
1638 if (key == root_key) res = 0; /* no matching name */
1639 return res;
1642 /* load all the keys from the input file */
1643 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1644 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1646 struct key *subkey = NULL;
1647 struct file_load_info info;
1648 timeout_t modif = current_time;
1649 char *p;
1651 info.filename = filename;
1652 info.file = f;
1653 info.len = 4;
1654 info.tmplen = 4;
1655 info.line = 0;
1656 if (!(info.buffer = mem_alloc( info.len ))) return;
1657 if (!(info.tmp = mem_alloc( info.tmplen )))
1659 free( info.buffer );
1660 return;
1663 if ((read_next_line( &info ) != 1) ||
1664 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1666 set_error( STATUS_NOT_REGISTRY_FILE );
1667 goto done;
1670 while (read_next_line( &info ) == 1)
1672 p = info.buffer;
1673 while (*p && isspace(*p)) p++;
1674 switch(*p)
1676 case '[': /* new key */
1677 if (subkey)
1679 update_key_time( subkey, modif );
1680 release_object( subkey );
1682 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1683 if (!(subkey = load_key( key, p + 1, prefix_len, &info, &modif )))
1684 file_read_error( "Error creating key", &info );
1685 break;
1686 case '@': /* default value */
1687 case '\"': /* value */
1688 if (subkey) load_value( subkey, p, &info );
1689 else file_read_error( "Value without key", &info );
1690 break;
1691 case '#': /* option */
1692 if (subkey) load_key_option( subkey, p, &info );
1693 else if (!load_global_option( p, &info )) goto done;
1694 break;
1695 case ';': /* comment */
1696 case 0: /* empty line */
1697 break;
1698 default:
1699 file_read_error( "Unrecognized input", &info );
1700 break;
1704 done:
1705 if (subkey)
1707 update_key_time( subkey, modif );
1708 release_object( subkey );
1710 free( info.buffer );
1711 free( info.tmp );
1714 /* load a part of the registry from a file */
1715 static void load_registry( struct key *key, obj_handle_t handle )
1717 struct file *file;
1718 int fd;
1720 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1721 fd = dup( get_file_unix_fd( file ) );
1722 release_object( file );
1723 if (fd != -1)
1725 FILE *f = fdopen( fd, "r" );
1726 if (f)
1728 load_keys( key, NULL, f, -1 );
1729 fclose( f );
1731 else file_set_error();
1735 /* load one of the initial registry files */
1736 static int load_init_registry_from_file( const char *filename, struct key *key )
1738 FILE *f;
1740 if ((f = fopen( filename, "r" )))
1742 load_keys( key, filename, f, 0 );
1743 fclose( f );
1744 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1746 fprintf( stderr, "%s is not a valid registry file\n", filename );
1747 return 1;
1751 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1753 save_branch_info[save_branch_count].path = filename;
1754 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1755 make_object_permanent( &key->obj );
1756 return (f != NULL);
1759 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1761 char buffer[7 + 11 + 11 + 11 * SID_MAX_SUB_AUTHORITIES], *p = buffer;
1762 unsigned int i;
1764 p += sprintf( p, "User\\S-%u-%u", sid->Revision,
1765 MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1766 sid->IdentifierAuthority.Value[4] ),
1767 MAKEWORD( sid->IdentifierAuthority.Value[3],
1768 sid->IdentifierAuthority.Value[2] )));
1769 for (i = 0; i < sid->SubAuthorityCount; i++) p += sprintf( p, "-%u", sid->SubAuthority[i] );
1770 return ascii_to_unicode_str( buffer, path );
1773 static void init_supported_machines(void)
1775 unsigned int count = 0;
1776 #ifdef __i386__
1777 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1778 #elif defined(__x86_64__)
1779 if (prefix_type == PREFIX_64BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_AMD64;
1780 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1781 #elif defined(__arm__)
1782 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1783 #elif defined(__aarch64__)
1784 if (prefix_type == PREFIX_64BIT)
1786 supported_machines[count++] = IMAGE_FILE_MACHINE_ARM64;
1787 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1789 supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1790 #else
1791 #error Unsupported machine
1792 #endif
1793 supported_machines_count = count;
1794 native_machine = supported_machines[0];
1797 /* registry initialisation */
1798 void init_registry(void)
1800 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1801 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1802 static const WCHAR classes_i386[] = {'S','o','f','t','w','a','r','e','\\',
1803 'C','l','a','s','s','e','s','\\',
1804 'W','o','w','6','4','3','2','N','o','d','e'};
1805 static const WCHAR classes_amd64[] = {'S','o','f','t','w','a','r','e','\\',
1806 'C','l','a','s','s','e','s','\\',
1807 'W','o','w','6','4','6','4','N','o','d','e'};
1808 static const WCHAR classes_arm[] = {'S','o','f','t','w','a','r','e','\\',
1809 'C','l','a','s','s','e','s','\\',
1810 'W','o','w','A','A','3','2','N','o','d','e'};
1811 static const WCHAR classes_arm64[] = {'S','o','f','t','w','a','r','e','\\',
1812 'C','l','a','s','s','e','s','\\',
1813 'W','o','w','A','A','6','4','N','o','d','e'};
1814 static const struct unicode_str root_name = { NULL, 0 };
1815 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1816 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1818 WCHAR *current_user_path;
1819 struct unicode_str current_user_str;
1820 struct key *key, *hklm, *hkcu;
1821 unsigned int i;
1822 char *p;
1824 /* switch to the config dir */
1826 if (fchdir( config_dir_fd ) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno ));
1828 /* create the root key */
1829 root_key = alloc_key( &root_name, current_time );
1830 assert( root_key );
1831 make_object_permanent( &root_key->obj );
1833 /* load system.reg into Registry\Machine */
1835 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1836 fatal_error( "could not create Machine registry key\n" );
1838 if (!load_init_registry_from_file( "system.reg", hklm ))
1840 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1841 prefix_type = PREFIX_32BIT;
1842 else
1843 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1845 else if (prefix_type == PREFIX_UNKNOWN)
1846 prefix_type = PREFIX_32BIT;
1848 init_supported_machines();
1850 /* load userdef.reg into Registry\User\.Default */
1852 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1853 fatal_error( "could not create User\\.Default registry key\n" );
1855 load_init_registry_from_file( "userdef.reg", key );
1856 release_object( key );
1858 /* load user.reg into HKEY_CURRENT_USER */
1860 /* FIXME: match default user in token.c. should get from process token instead */
1861 current_user_path = format_user_registry_path( security_local_user_sid, &current_user_str );
1862 if (!current_user_path ||
1863 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1864 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1865 free( current_user_path );
1866 load_init_registry_from_file( "user.reg", hkcu );
1868 /* set the shared flag on Software\Classes\Wow6432Node for all platforms */
1869 for (i = 1; i < supported_machines_count; i++)
1871 struct unicode_str name;
1873 switch (supported_machines[i])
1875 case IMAGE_FILE_MACHINE_I386: name.str = classes_i386; name.len = sizeof(classes_i386); break;
1876 case IMAGE_FILE_MACHINE_ARMNT: name.str = classes_arm; name.len = sizeof(classes_arm); break;
1877 case IMAGE_FILE_MACHINE_AMD64: name.str = classes_amd64; name.len = sizeof(classes_amd64); break;
1878 case IMAGE_FILE_MACHINE_ARM64: name.str = classes_arm64; name.len = sizeof(classes_arm64); break;
1880 if ((key = create_key_recursive( hklm, &name, current_time )))
1882 key->flags |= KEY_WOWSHARE;
1883 release_object( key );
1885 /* FIXME: handle HKCU too */
1888 release_object( hklm );
1889 release_object( hkcu );
1891 /* start the periodic save timer */
1892 set_periodic_save_timer();
1894 /* create windows directories */
1896 if (!mkdir( "drive_c/windows", 0777 ))
1898 mkdir( "drive_c/windows/system32", 0777 );
1899 for (i = 1; i < supported_machines_count; i++)
1901 switch (supported_machines[i])
1903 case IMAGE_FILE_MACHINE_I386: mkdir( "drive_c/windows/syswow64", 0777 ); break;
1904 case IMAGE_FILE_MACHINE_ARMNT: mkdir( "drive_c/windows/sysarm32", 0777 ); break;
1905 case IMAGE_FILE_MACHINE_AMD64: mkdir( "drive_c/windows/sysx8664", 0777 ); break;
1906 case IMAGE_FILE_MACHINE_ARM64: mkdir( "drive_c/windows/sysarm64", 0777 ); break;
1911 /* go back to the server dir */
1912 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
1915 /* save a registry branch to a file */
1916 static void save_all_subkeys( struct key *key, FILE *f )
1918 fprintf( f, "WINE REGISTRY Version 2\n" );
1919 fprintf( f, ";; All keys relative to " );
1920 dump_path( key, NULL, f );
1921 fprintf( f, "\n" );
1922 switch (prefix_type)
1924 case PREFIX_32BIT:
1925 fprintf( f, "\n#arch=win32\n" );
1926 break;
1927 case PREFIX_64BIT:
1928 fprintf( f, "\n#arch=win64\n" );
1929 break;
1930 default:
1931 break;
1933 save_subkeys( key, key, f );
1936 /* save a registry branch to a file handle */
1937 static void save_registry( struct key *key, obj_handle_t handle )
1939 struct file *file;
1940 int fd;
1942 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1943 fd = dup( get_file_unix_fd( file ) );
1944 release_object( file );
1945 if (fd != -1)
1947 FILE *f = fdopen( fd, "w" );
1948 if (f)
1950 save_all_subkeys( key, f );
1951 if (fclose( f )) file_set_error();
1953 else
1955 file_set_error();
1956 close( fd );
1961 /* save a registry branch to a file */
1962 static int save_branch( struct key *key, const char *path )
1964 struct stat st;
1965 char *p, *tmp = NULL;
1966 int fd, count = 0, ret = 0;
1967 FILE *f;
1969 if (!(key->flags & KEY_DIRTY))
1971 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1972 return 1;
1975 /* test the file type */
1977 if ((fd = open( path, O_WRONLY )) != -1)
1979 /* if file is not a regular file or has multiple links or is accessed
1980 * via symbolic links, write directly into it; otherwise use a temp file */
1981 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1983 ftruncate( fd, 0 );
1984 goto save;
1986 close( fd );
1989 /* create a temp file in the same directory */
1991 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1992 strcpy( tmp, path );
1993 if ((p = strrchr( tmp, '/' ))) p++;
1994 else p = tmp;
1995 for (;;)
1997 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1998 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1999 if (errno != EEXIST) goto done;
2000 close( fd );
2003 /* now save to it */
2005 save:
2006 if (!(f = fdopen( fd, "w" )))
2008 if (tmp) unlink( tmp );
2009 close( fd );
2010 goto done;
2013 if (debug_level > 1)
2015 fprintf( stderr, "%s: ", path );
2016 dump_operation( key, NULL, "saving" );
2019 save_all_subkeys( key, f );
2020 ret = !fclose(f);
2022 if (tmp)
2024 /* if successfully written, rename to final name */
2025 if (ret) ret = !rename( tmp, path );
2026 if (!ret) unlink( tmp );
2029 done:
2030 free( tmp );
2031 if (ret) make_clean( key );
2032 return ret;
2035 /* periodic saving of the registry */
2036 static void periodic_save( void *arg )
2038 int i;
2040 if (fchdir( config_dir_fd ) == -1) return;
2041 save_timeout_user = NULL;
2042 for (i = 0; i < save_branch_count; i++)
2043 save_branch( save_branch_info[i].key, save_branch_info[i].path );
2044 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2045 set_periodic_save_timer();
2048 /* start the periodic save timer */
2049 static void set_periodic_save_timer(void)
2051 if (save_timeout_user) remove_timeout_user( save_timeout_user );
2052 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
2055 /* save the modified registry branches to disk */
2056 void flush_registry(void)
2058 int i;
2060 if (fchdir( config_dir_fd ) == -1) return;
2061 for (i = 0; i < save_branch_count; i++)
2063 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
2065 fprintf( stderr, "wineserver: could not save registry branch to %s",
2066 save_branch_info[i].path );
2067 perror( " " );
2070 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2073 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2074 static int is_wow64_thread( struct thread *thread )
2076 return (is_machine_64bit( native_machine ) && !is_machine_64bit( thread->process->machine ));
2080 /* create a registry key */
2081 DECL_HANDLER(create_key)
2083 struct key *key = NULL, *parent;
2084 struct unicode_str name, class;
2085 unsigned int access = req->access;
2086 const struct security_descriptor *sd;
2087 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2089 if (!objattr) return;
2091 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2093 class.str = get_req_data_after_objattr( objattr, &class.len );
2094 class.len = (class.len / sizeof(WCHAR)) * sizeof(WCHAR);
2096 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2097 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2099 name.str += ARRAY_SIZE( root_name );
2100 name.len -= sizeof(root_name);
2103 /* NOTE: no access rights are required from the parent handle to create a key */
2104 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2106 if ((key = create_key( parent, &name, &class, req->options, access,
2107 objattr->attributes, sd, &reply->created )))
2109 reply->hkey = alloc_handle( current->process, key, access, objattr->attributes );
2110 release_object( key );
2112 release_object( parent );
2116 /* open a registry key */
2117 DECL_HANDLER(open_key)
2119 struct key *key, *parent;
2120 struct unicode_str name;
2121 unsigned int access = req->access;
2123 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2125 reply->hkey = 0;
2126 /* NOTE: no access rights are required to open the parent key, only the child key */
2127 if ((parent = get_parent_hkey_obj( req->parent )))
2129 get_req_path( &name, !req->parent );
2130 if ((key = open_key( parent, &name, access, req->attributes )))
2132 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
2133 release_object( key );
2135 release_object( parent );
2139 /* delete a registry key */
2140 DECL_HANDLER(delete_key)
2142 struct key *key;
2144 if ((key = get_hkey_obj( req->hkey, DELETE )))
2146 delete_key( key, 0);
2147 release_object( key );
2151 /* flush a registry key */
2152 DECL_HANDLER(flush_key)
2154 struct key *key = get_hkey_obj( req->hkey, 0 );
2155 if (key)
2157 /* we don't need to do anything here with the current implementation */
2158 release_object( key );
2162 /* enumerate registry subkeys */
2163 DECL_HANDLER(enum_key)
2165 struct key *key;
2167 if ((key = get_hkey_obj( req->hkey,
2168 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
2170 enum_key( key, req->index, req->info_class, reply );
2171 release_object( key );
2175 /* set a value of a registry key */
2176 DECL_HANDLER(set_key_value)
2178 struct key *key;
2179 struct unicode_str name;
2181 if (req->namelen > get_req_data_size())
2183 set_error( STATUS_INVALID_PARAMETER );
2184 return;
2186 name.str = get_req_data();
2187 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2189 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2191 data_size_t datalen = get_req_data_size() - req->namelen;
2192 const char *data = (const char *)get_req_data() + req->namelen;
2194 set_value( key, &name, req->type, data, datalen );
2195 release_object( key );
2199 /* retrieve the value of a registry key */
2200 DECL_HANDLER(get_key_value)
2202 struct key *key;
2203 struct unicode_str name = get_req_unicode_str();
2205 reply->total = 0;
2206 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2208 get_value( key, &name, &reply->type, &reply->total );
2209 release_object( key );
2213 /* enumerate the value of a registry key */
2214 DECL_HANDLER(enum_key_value)
2216 struct key *key;
2218 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2220 enum_value( key, req->index, req->info_class, reply );
2221 release_object( key );
2225 /* delete a value of a registry key */
2226 DECL_HANDLER(delete_key_value)
2228 struct key *key;
2229 struct unicode_str name = get_req_unicode_str();
2231 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2233 delete_value( key, &name );
2234 release_object( key );
2238 /* load a registry branch from a file */
2239 DECL_HANDLER(load_registry)
2241 struct key *key, *parent;
2242 struct unicode_str name;
2243 const struct security_descriptor *sd;
2244 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2246 if (!objattr) return;
2248 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2250 set_error( STATUS_PRIVILEGE_NOT_HELD );
2251 return;
2254 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2255 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2257 name.str += ARRAY_SIZE( root_name );
2258 name.len -= sizeof(root_name);
2261 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2263 int dummy;
2264 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, sd, &dummy )))
2266 load_registry( key, req->file );
2267 release_object( key );
2269 release_object( parent );
2273 DECL_HANDLER(unload_registry)
2275 struct key *key, *parent;
2276 struct unicode_str name;
2277 unsigned int access = 0;
2279 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2281 set_error( STATUS_PRIVILEGE_NOT_HELD );
2282 return;
2285 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2287 if ((parent = get_parent_hkey_obj( req->parent )))
2289 get_req_path( &name, !req->parent );
2290 if ((key = open_key( parent, &name, access, req->attributes )))
2292 if (key->obj.handle_count)
2293 set_error( STATUS_CANNOT_DELETE );
2294 else
2295 delete_key( key, 1 ); /* FIXME */
2296 release_object( key );
2298 release_object( parent );
2302 /* save a registry branch to a file */
2303 DECL_HANDLER(save_registry)
2305 struct key *key;
2307 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2309 set_error( STATUS_PRIVILEGE_NOT_HELD );
2310 return;
2313 if ((key = get_hkey_obj( req->hkey, 0 )))
2315 save_registry( key, req->file );
2316 release_object( key );
2320 /* add a registry key change notification */
2321 DECL_HANDLER(set_registry_notification)
2323 struct key *key;
2324 struct event *event;
2325 struct notify *notify;
2327 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2328 if (key)
2330 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2331 if (event)
2333 notify = find_notify( key, current->process, req->hkey );
2334 if (!notify)
2336 notify = mem_alloc( sizeof(*notify) );
2337 if (notify)
2339 notify->events = NULL;
2340 notify->event_count = 0;
2341 notify->subtree = req->subtree;
2342 notify->filter = req->filter;
2343 notify->hkey = req->hkey;
2344 notify->process = current->process;
2345 list_add_head( &key->notify_list, &notify->entry );
2348 if (notify)
2350 struct event **new_array;
2352 if ((new_array = realloc( notify->events, (notify->event_count + 1) * sizeof(*notify->events) )))
2354 notify->events = new_array;
2355 notify->events[notify->event_count++] = (struct event *)grab_object( event );
2356 reset_event( event );
2357 set_error( STATUS_PENDING );
2359 else set_error( STATUS_NO_MEMORY );
2361 release_object( event );
2363 release_object( key );