reg/tests: Use string literals instead of a char buffer for REG_MULTI_SZ tests.
[wine.git] / server / registry.c
blob68ec4f9e39a7a5876041a90bfdd9dd459a0db305
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];
151 /* information about a file being loaded */
152 struct file_load_info
154 const char *filename; /* input file name */
155 FILE *file; /* input file */
156 char *buffer; /* line buffer */
157 int len; /* buffer length */
158 int line; /* current input line */
159 WCHAR *tmp; /* temp buffer to use while parsing input */
160 size_t tmplen; /* length of temp buffer */
164 static void key_dump( struct object *obj, int verbose );
165 static unsigned int key_map_access( struct object *obj, unsigned int access );
166 static struct security_descriptor *key_get_sd( struct object *obj );
167 static WCHAR *key_get_full_name( struct object *obj, data_size_t *len );
168 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
169 static void key_destroy( struct object *obj );
171 static const struct object_ops key_ops =
173 sizeof(struct key), /* size */
174 &key_type, /* type */
175 key_dump, /* dump */
176 no_add_queue, /* add_queue */
177 NULL, /* remove_queue */
178 NULL, /* signaled */
179 NULL, /* satisfied */
180 no_signal, /* signal */
181 no_get_fd, /* get_fd */
182 key_map_access, /* map_access */
183 key_get_sd, /* get_sd */
184 default_set_sd, /* set_sd */
185 key_get_full_name, /* get_full_name */
186 no_lookup_name, /* lookup_name */
187 no_link_name, /* link_name */
188 NULL, /* unlink_name */
189 no_open_file, /* open_file */
190 no_kernel_obj_list, /* get_kernel_obj_list */
191 key_close_handle, /* close_handle */
192 key_destroy /* destroy */
196 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
198 return (len == sizeof(wow6432node) && !memicmp_strW( name, wow6432node, sizeof( wow6432node )));
202 * The registry text file format v2 used by this code is similar to the one
203 * used by REGEDIT import/export functionality, with the following differences:
204 * - strings and key names can contain \x escapes for Unicode
205 * - key names use escapes too in order to support Unicode
206 * - the modification time optionally follows the key name
207 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
210 /* dump the full path of a key */
211 static void dump_path( const struct key *key, const struct key *base, FILE *f )
213 if (key->parent && key->parent != base)
215 dump_path( key->parent, base, f );
216 fprintf( f, "\\\\" );
218 dump_strW( key->name, key->namelen, f, "[]" );
221 /* dump a value to a text file */
222 static void dump_value( const struct key_value *value, FILE *f )
224 unsigned int i, dw;
225 int count;
227 if (value->namelen)
229 fputc( '\"', f );
230 count = 1 + dump_strW( value->name, value->namelen, f, "\"\"" );
231 count += fprintf( f, "\"=" );
233 else count = fprintf( f, "@=" );
235 switch(value->type)
237 case REG_SZ:
238 case REG_EXPAND_SZ:
239 case REG_MULTI_SZ:
240 /* only output properly terminated strings in string format */
241 if (value->len < sizeof(WCHAR)) break;
242 if (value->len % sizeof(WCHAR)) break;
243 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
244 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
245 fputc( '\"', f );
246 dump_strW( (WCHAR *)value->data, value->len, f, "\"\"" );
247 fprintf( f, "\"\n" );
248 return;
250 case REG_DWORD:
251 if (value->len != sizeof(dw)) break;
252 memcpy( &dw, value->data, sizeof(dw) );
253 fprintf( f, "dword:%08x\n", dw );
254 return;
257 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
258 else count += fprintf( f, "hex(%x):", value->type );
259 for (i = 0; i < value->len; i++)
261 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
262 if (i < value->len-1)
264 fputc( ',', f );
265 if (++count > 76)
267 fprintf( f, "\\\n " );
268 count = 2;
272 fputc( '\n', f );
275 /* save a registry and all its subkeys to a text file */
276 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
278 int i;
280 if (key->flags & KEY_VOLATILE) return;
281 /* save key if it has either some values or no subkeys, or needs special options */
282 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
283 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
285 fprintf( f, "\n[" );
286 if (key != base) dump_path( key, base, f );
287 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
288 fprintf( f, "#time=%x%08x\n", (unsigned int)(key->modif >> 32), (unsigned int)key->modif );
289 if (key->class)
291 fprintf( f, "#class=\"" );
292 dump_strW( key->class, key->classlen, f, "\"\"" );
293 fprintf( f, "\"\n" );
295 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
296 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
298 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
301 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
303 fprintf( stderr, "%s key ", op );
304 if (key) dump_path( key, NULL, stderr );
305 else fprintf( stderr, "ERROR" );
306 if (value)
308 fprintf( stderr, " value ");
309 dump_value( value, stderr );
311 else fprintf( stderr, "\n" );
314 static void key_dump( struct object *obj, int verbose )
316 struct key *key = (struct key *)obj;
317 assert( obj->ops == &key_ops );
318 fprintf( stderr, "Key flags=%x ", key->flags );
319 dump_path( key, NULL, stderr );
320 fprintf( stderr, "\n" );
323 /* notify waiter and maybe delete the notification */
324 static void do_notification( struct key *key, struct notify *notify, int del )
326 unsigned int i;
328 for (i = 0; i < notify->event_count; ++i)
330 set_event( notify->events[i] );
331 release_object( notify->events[i] );
333 free( notify->events );
334 notify->events = NULL;
335 notify->event_count = 0;
337 if (del)
339 list_remove( &notify->entry );
340 free( notify );
344 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
346 struct notify *notify;
348 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
350 if (notify->process == process && notify->hkey == hkey) return notify;
352 return NULL;
355 static unsigned int key_map_access( struct object *obj, unsigned int access )
357 access = default_map_access( obj, access );
358 /* filter the WOW64 masks, as they aren't real access bits */
359 return access & ~(KEY_WOW64_64KEY | KEY_WOW64_32KEY);
362 static struct security_descriptor *key_get_sd( struct object *obj )
364 static struct security_descriptor *key_default_sd;
366 if (obj->sd) return obj->sd;
368 if (!key_default_sd)
370 size_t users_sid_len = security_sid_len( security_builtin_users_sid );
371 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
372 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
373 + users_sid_len + admins_sid_len;
374 ACCESS_ALLOWED_ACE *aaa;
375 ACL *dacl;
377 key_default_sd = mem_alloc( sizeof(*key_default_sd) + 2 * admins_sid_len + dacl_len );
378 key_default_sd->control = SE_DACL_PRESENT;
379 key_default_sd->owner_len = admins_sid_len;
380 key_default_sd->group_len = admins_sid_len;
381 key_default_sd->sacl_len = 0;
382 key_default_sd->dacl_len = dacl_len;
383 memcpy( key_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
384 memcpy( (char *)(key_default_sd + 1) + admins_sid_len, security_builtin_admins_sid, admins_sid_len );
386 dacl = (ACL *)((char *)(key_default_sd + 1) + 2 * admins_sid_len);
387 dacl->AclRevision = ACL_REVISION;
388 dacl->Sbz1 = 0;
389 dacl->AclSize = dacl_len;
390 dacl->AceCount = 2;
391 dacl->Sbz2 = 0;
392 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
393 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
394 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
395 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
396 aaa->Mask = GENERIC_READ;
397 memcpy( &aaa->SidStart, security_builtin_users_sid, users_sid_len );
398 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
399 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
400 aaa->Header.AceFlags = 0;
401 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
402 aaa->Mask = KEY_ALL_ACCESS;
403 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
405 return key_default_sd;
408 static WCHAR *key_get_full_name( struct object *obj, data_size_t *ret_len )
410 static const WCHAR backslash = '\\';
411 struct key *key = (struct key *) obj;
412 data_size_t len = sizeof(root_name) - sizeof(WCHAR);
413 char *ret;
415 for (key = (struct key *)obj; key != root_key; key = key->parent) len += key->namelen + sizeof(WCHAR);
416 if (!(ret = malloc( len ))) return NULL;
418 *ret_len = len;
419 key = (struct key *)obj;
420 for (key = (struct key *)obj; key != root_key; key = key->parent)
422 memcpy( ret + len - key->namelen, key->name, key->namelen );
423 len -= key->namelen + sizeof(WCHAR);
424 memcpy( ret + len, &backslash, sizeof(WCHAR) );
426 memcpy( ret, root_name, sizeof(root_name) - sizeof(WCHAR) );
427 return (WCHAR *)ret;
430 /* close the notification associated with a handle */
431 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
433 struct key * key = (struct key *) obj;
434 struct notify *notify = find_notify( key, process, handle );
435 if (notify) do_notification( key, notify, 1 );
436 return 1; /* ok to close */
439 static void key_destroy( struct object *obj )
441 int i;
442 struct list *ptr;
443 struct key *key = (struct key *)obj;
444 assert( obj->ops == &key_ops );
446 free( key->name );
447 free( key->class );
448 for (i = 0; i <= key->last_value; i++)
450 free( key->values[i].name );
451 free( key->values[i].data );
453 free( key->values );
454 for (i = 0; i <= key->last_subkey; i++)
456 key->subkeys[i]->parent = NULL;
457 release_object( key->subkeys[i] );
459 free( key->subkeys );
460 /* unconditionally notify everything waiting on this key */
461 while ((ptr = list_head( &key->notify_list )))
463 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
464 do_notification( key, notify, 1 );
468 /* get the request vararg as registry path */
469 static inline void get_req_path( struct unicode_str *str, int skip_root )
471 str->str = get_req_data();
472 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
474 if (skip_root && str->len >= sizeof(root_name) && !memicmp_strW( str->str, root_name, sizeof(root_name) ))
476 str->str += ARRAY_SIZE( root_name );
477 str->len -= sizeof(root_name);
481 /* return the next token in a given path */
482 /* token->str must point inside the path, or be NULL for the first call */
483 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
485 data_size_t i = 0, len = path->len / sizeof(WCHAR);
487 if (!token->str) /* first time */
489 /* path cannot start with a backslash */
490 if (len && path->str[0] == '\\')
492 set_error( STATUS_OBJECT_PATH_INVALID );
493 return NULL;
496 else
498 i = token->str - path->str;
499 i += token->len / sizeof(WCHAR);
500 while (i < len && path->str[i] == '\\') i++;
502 token->str = path->str + i;
503 while (i < len && path->str[i] != '\\') i++;
504 token->len = (path->str + i - token->str) * sizeof(WCHAR);
505 return token;
508 /* allocate a key object */
509 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
511 struct key *key;
512 if ((key = alloc_object( &key_ops )))
514 key->name = NULL;
515 key->class = NULL;
516 key->namelen = name->len;
517 key->classlen = 0;
518 key->flags = 0;
519 key->last_subkey = -1;
520 key->nb_subkeys = 0;
521 key->subkeys = NULL;
522 key->nb_values = 0;
523 key->last_value = -1;
524 key->values = NULL;
525 key->modif = modif;
526 key->parent = NULL;
527 list_init( &key->notify_list );
528 if (name->len && !(key->name = memdup( name->str, name->len )))
530 release_object( key );
531 key = NULL;
534 return key;
537 /* mark a key and all its parents as dirty (modified) */
538 static void make_dirty( struct key *key )
540 while (key)
542 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
543 key->flags |= KEY_DIRTY;
544 key = key->parent;
548 /* mark a key and all its subkeys as clean (not modified) */
549 static void make_clean( struct key *key )
551 int i;
553 if (key->flags & KEY_VOLATILE) return;
554 if (!(key->flags & KEY_DIRTY)) return;
555 key->flags &= ~KEY_DIRTY;
556 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
559 /* go through all the notifications and send them if necessary */
560 static void check_notify( struct key *key, unsigned int change, int not_subtree )
562 struct list *ptr, *next;
564 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
566 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
567 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
568 do_notification( key, n, 0 );
572 /* update key modification time */
573 static void touch_key( struct key *key, unsigned int change )
575 struct key *k;
577 key->modif = current_time;
578 make_dirty( key );
580 /* do notifications */
581 check_notify( key, change, 1 );
582 for ( k = key->parent; k; k = k->parent )
583 check_notify( k, change, 0 );
586 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
587 static int grow_subkeys( struct key *key )
589 struct key **new_subkeys;
590 int nb_subkeys;
592 if (key->nb_subkeys)
594 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
595 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
597 set_error( STATUS_NO_MEMORY );
598 return 0;
601 else
603 nb_subkeys = MIN_SUBKEYS;
604 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
606 key->subkeys = new_subkeys;
607 key->nb_subkeys = nb_subkeys;
608 return 1;
611 /* allocate a subkey for a given key, and return its index */
612 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
613 int index, timeout_t modif )
615 struct key *key;
616 int i;
618 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
620 set_error( STATUS_INVALID_PARAMETER );
621 return NULL;
623 if (parent->last_subkey + 1 == parent->nb_subkeys)
625 /* need to grow the array */
626 if (!grow_subkeys( parent )) return NULL;
628 if ((key = alloc_key( name, modif )) != NULL)
630 key->parent = parent;
631 for (i = ++parent->last_subkey; i > index; i--)
632 parent->subkeys[i] = parent->subkeys[i-1];
633 parent->subkeys[index] = key;
634 if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
635 parent->flags |= KEY_WOW64;
637 return key;
640 /* free a subkey of a given key */
641 static void free_subkey( struct key *parent, int index )
643 struct key *key;
644 int i, nb_subkeys;
646 assert( index >= 0 );
647 assert( index <= parent->last_subkey );
649 key = parent->subkeys[index];
650 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
651 parent->last_subkey--;
652 key->flags |= KEY_DELETED;
653 key->parent = NULL;
654 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
655 release_object( key );
657 /* try to shrink the array */
658 nb_subkeys = parent->nb_subkeys;
659 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
661 struct key **new_subkeys;
662 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
663 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
664 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
665 parent->subkeys = new_subkeys;
666 parent->nb_subkeys = nb_subkeys;
670 /* find the named child of a given key and return its index */
671 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
673 int i, min, max, res;
674 data_size_t len;
676 min = 0;
677 max = key->last_subkey;
678 while (min <= max)
680 i = (min + max) / 2;
681 len = min( key->subkeys[i]->namelen, name->len );
682 res = memicmp_strW( key->subkeys[i]->name, name->str, len );
683 if (!res) res = key->subkeys[i]->namelen - name->len;
684 if (!res)
686 *index = i;
687 return key->subkeys[i];
689 if (res > 0) max = i - 1;
690 else min = i + 1;
692 *index = min; /* this is where we should insert it */
693 return NULL;
696 /* return the wow64 variant of the key, or the key itself if none */
697 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
699 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
700 int index;
702 if (!(key->flags & KEY_WOW64)) return key;
703 if (!is_wow6432node( name->str, name->len ))
705 key = find_subkey( key, &wow6432node_str, &index );
706 assert( key ); /* if KEY_WOW64 is set we must find it */
708 return key;
712 /* follow a symlink and return the resolved key */
713 static struct key *follow_symlink( struct key *key, int iteration )
715 struct unicode_str path, token;
716 struct key_value *value;
717 int index;
719 if (iteration > 16) return NULL;
720 if (!(key->flags & KEY_SYMLINK)) return key;
721 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
723 path.str = value->data;
724 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
725 if (path.len <= sizeof(root_name)) return NULL;
726 if (memicmp_strW( path.str, root_name, sizeof(root_name) )) return NULL;
727 path.str += ARRAY_SIZE( root_name );
728 path.len -= sizeof(root_name);
730 key = root_key;
731 token.str = NULL;
732 if (!get_path_token( &path, &token )) return NULL;
733 while (token.len)
735 if (!(key = find_subkey( key, &token, &index ))) break;
736 if (!(key = follow_symlink( key, iteration + 1 ))) break;
737 get_path_token( &path, &token );
739 return key;
742 /* open a key until we find an element that doesn't exist */
743 /* helper for open_key and create_key */
744 static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
745 unsigned int access, struct unicode_str *token, int *index )
747 token->str = NULL;
748 if (!get_path_token( name, token )) return NULL;
749 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
750 while (token->len)
752 struct key *subkey;
753 if (!(subkey = find_subkey( key, token, index )))
755 if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
757 /* try in the 64-bit parent */
758 key = key->parent;
759 subkey = find_subkey( key, token, index );
762 if (!subkey) break;
763 key = subkey;
764 get_path_token( name, token );
765 if (!token->len) break;
766 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
767 if (!(key = follow_symlink( key, 0 )))
769 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
770 return NULL;
773 return key;
776 /* open a subkey */
777 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
778 unsigned int attributes )
780 int index;
781 struct unicode_str token;
783 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
785 if (token.len)
787 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
788 return NULL;
790 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
791 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
793 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
794 return NULL;
796 if (debug_level > 1) dump_operation( key, NULL, "Open" );
797 grab_object( key );
798 return key;
801 /* create a subkey */
802 static struct key *create_key( struct key *key, const struct unicode_str *name,
803 const struct unicode_str *class, unsigned int options,
804 unsigned int access, unsigned int attributes,
805 const struct security_descriptor *sd, int *created )
807 int index;
808 struct unicode_str token, next;
810 *created = 0;
811 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
813 if (!token.len) /* the key already exists */
815 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
816 if (options & REG_OPTION_CREATE_LINK)
818 set_error( STATUS_OBJECT_NAME_COLLISION );
819 return NULL;
821 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
823 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
824 return NULL;
826 if (debug_level > 1) dump_operation( key, NULL, "Open" );
827 grab_object( key );
828 return key;
831 /* token must be the last path component at this point */
832 next = token;
833 get_path_token( name, &next );
834 if (next.len)
836 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
837 return NULL;
840 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
842 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
843 return NULL;
845 *created = 1;
846 make_dirty( key );
847 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
849 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
850 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
851 else key->flags |= KEY_DIRTY;
853 if (sd) default_set_sd( &key->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
854 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
856 if (debug_level > 1) dump_operation( key, NULL, "Create" );
857 if (class && class->len)
859 key->classlen = class->len;
860 free(key->class);
861 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
863 touch_key( key->parent, REG_NOTIFY_CHANGE_NAME );
864 grab_object( key );
865 return key;
868 /* recursively create a subkey (for internal use only) */
869 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
871 struct key *base;
872 int index;
873 struct unicode_str token;
875 token.str = NULL;
876 if (!get_path_token( name, &token )) return NULL;
877 while (token.len)
879 struct key *subkey;
880 if (!(subkey = find_subkey( key, &token, &index ))) break;
881 key = subkey;
882 if (!(key = follow_symlink( key, 0 )))
884 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
885 return NULL;
887 get_path_token( name, &token );
890 if (token.len)
892 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
893 base = key;
894 for (;;)
896 get_path_token( name, &token );
897 if (!token.len) break;
898 /* we know the index is always 0 in a new key */
899 if (!(key = alloc_subkey( key, &token, 0, modif )))
901 free_subkey( base, index );
902 return NULL;
907 grab_object( key );
908 return key;
911 /* query information about a key or a subkey */
912 static void enum_key( struct key *key, int index, int info_class, struct enum_key_reply *reply )
914 int i;
915 data_size_t len, namelen, classlen;
916 data_size_t max_subkey = 0, max_class = 0;
917 data_size_t max_value = 0, max_data = 0;
918 WCHAR *fullname = NULL;
919 char *data;
921 if (index != -1) /* -1 means use the specified key directly */
923 if ((index < 0) || (index > key->last_subkey))
925 set_error( STATUS_NO_MORE_ENTRIES );
926 return;
928 key = key->subkeys[index];
931 namelen = key->namelen;
932 classlen = key->classlen;
934 switch(info_class)
936 case KeyNameInformation:
937 if (!(fullname = key->obj.ops->get_full_name( &key->obj, &namelen ))) return;
938 /* fall through */
939 case KeyBasicInformation:
940 classlen = 0; /* only return the name */
941 /* fall through */
942 case KeyNodeInformation:
943 reply->max_subkey = 0;
944 reply->max_class = 0;
945 reply->max_value = 0;
946 reply->max_data = 0;
947 break;
948 case KeyFullInformation:
949 case KeyCachedInformation:
950 for (i = 0; i <= key->last_subkey; i++)
952 if (key->subkeys[i]->namelen > max_subkey) max_subkey = key->subkeys[i]->namelen;
953 if (key->subkeys[i]->classlen > max_class) max_class = key->subkeys[i]->classlen;
955 for (i = 0; i <= key->last_value; i++)
957 if (key->values[i].namelen > max_value) max_value = key->values[i].namelen;
958 if (key->values[i].len > max_data) max_data = key->values[i].len;
960 reply->max_subkey = max_subkey;
961 reply->max_class = max_class;
962 reply->max_value = max_value;
963 reply->max_data = max_data;
964 reply->namelen = namelen;
965 if (info_class == KeyCachedInformation)
966 classlen = 0; /* don't return any data, only its size */
967 namelen = 0; /* don't return name */
968 break;
969 default:
970 set_error( STATUS_INVALID_PARAMETER );
971 return;
973 reply->subkeys = key->last_subkey + 1;
974 reply->values = key->last_value + 1;
975 reply->modif = key->modif;
976 reply->total = namelen + classlen;
978 len = min( reply->total, get_reply_max_size() );
979 if (len && (data = set_reply_data_size( len )))
981 if (len > namelen)
983 reply->namelen = namelen;
984 memcpy( data, key->name, namelen );
985 memcpy( data + namelen, key->class, len - namelen );
987 else if (info_class == KeyNameInformation)
989 reply->namelen = namelen;
990 memcpy( data, fullname, len );
992 else
994 reply->namelen = len;
995 memcpy( data, key->name, len );
998 free( fullname );
999 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
1002 /* delete a key and its values */
1003 static int delete_key( struct key *key, int recurse )
1005 int index;
1006 struct key *parent = key->parent;
1008 /* must find parent and index */
1009 if (key == root_key)
1011 set_error( STATUS_ACCESS_DENIED );
1012 return -1;
1014 assert( parent );
1016 while (recurse && (key->last_subkey>=0))
1017 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
1018 return -1;
1020 for (index = 0; index <= parent->last_subkey; index++)
1021 if (parent->subkeys[index] == key) break;
1022 assert( index <= parent->last_subkey );
1024 /* we can only delete a key that has no subkeys */
1025 if (key->last_subkey >= 0)
1027 set_error( STATUS_ACCESS_DENIED );
1028 return -1;
1031 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
1032 free_subkey( parent, index );
1033 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
1034 return 0;
1037 /* try to grow the array of values; return 1 if OK, 0 on error */
1038 static int grow_values( struct key *key )
1040 struct key_value *new_val;
1041 int nb_values;
1043 if (key->nb_values)
1045 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
1046 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
1048 set_error( STATUS_NO_MEMORY );
1049 return 0;
1052 else
1054 nb_values = MIN_VALUES;
1055 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
1057 key->values = new_val;
1058 key->nb_values = nb_values;
1059 return 1;
1062 /* find the named value of a given key and return its index in the array */
1063 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
1065 int i, min, max, res;
1066 data_size_t len;
1068 min = 0;
1069 max = key->last_value;
1070 while (min <= max)
1072 i = (min + max) / 2;
1073 len = min( key->values[i].namelen, name->len );
1074 res = memicmp_strW( key->values[i].name, name->str, len );
1075 if (!res) res = key->values[i].namelen - name->len;
1076 if (!res)
1078 *index = i;
1079 return &key->values[i];
1081 if (res > 0) max = i - 1;
1082 else min = i + 1;
1084 *index = min; /* this is where we should insert it */
1085 return NULL;
1088 /* insert a new value; the index must have been returned by find_value */
1089 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
1091 struct key_value *value;
1092 WCHAR *new_name = NULL;
1093 int i;
1095 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
1097 set_error( STATUS_NAME_TOO_LONG );
1098 return NULL;
1100 if (key->last_value + 1 == key->nb_values)
1102 if (!grow_values( key )) return NULL;
1104 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1105 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1106 value = &key->values[index];
1107 value->name = new_name;
1108 value->namelen = name->len;
1109 value->len = 0;
1110 value->data = NULL;
1111 return value;
1114 /* set a key value */
1115 static void set_value( struct key *key, const struct unicode_str *name,
1116 int type, const void *data, data_size_t len )
1118 struct key_value *value;
1119 void *ptr = NULL;
1120 int index;
1122 if ((value = find_value( key, name, &index )))
1124 /* check if the new value is identical to the existing one */
1125 if (value->type == type && value->len == len &&
1126 value->data && !memcmp( value->data, data, len ))
1128 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1129 return;
1133 if (key->flags & KEY_SYMLINK)
1135 if (type != REG_LINK || name->len != symlink_str.len ||
1136 memicmp_strW( name->str, symlink_str.str, name->len ))
1138 set_error( STATUS_ACCESS_DENIED );
1139 return;
1143 if (len && !(ptr = memdup( data, len ))) return;
1145 if (!value)
1147 if (!(value = insert_value( key, name, index )))
1149 free( ptr );
1150 return;
1153 else free( value->data ); /* already existing, free previous data */
1155 value->type = type;
1156 value->len = len;
1157 value->data = ptr;
1158 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1159 if (debug_level > 1) dump_operation( key, value, "Set" );
1162 /* get a key value */
1163 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1165 struct key_value *value;
1166 int index;
1168 if ((value = find_value( key, name, &index )))
1170 *type = value->type;
1171 *len = value->len;
1172 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1173 if (debug_level > 1) dump_operation( key, value, "Get" );
1175 else
1177 *type = -1;
1178 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1182 /* enumerate a key value */
1183 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1185 struct key_value *value;
1187 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1188 else
1190 void *data;
1191 data_size_t namelen, maxlen;
1193 value = &key->values[i];
1194 reply->type = value->type;
1195 namelen = value->namelen;
1197 switch(info_class)
1199 case KeyValueBasicInformation:
1200 reply->total = namelen;
1201 break;
1202 case KeyValueFullInformation:
1203 reply->total = namelen + value->len;
1204 break;
1205 case KeyValuePartialInformation:
1206 reply->total = value->len;
1207 namelen = 0;
1208 break;
1209 default:
1210 set_error( STATUS_INVALID_PARAMETER );
1211 return;
1214 maxlen = min( reply->total, get_reply_max_size() );
1215 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1217 if (maxlen > namelen)
1219 reply->namelen = namelen;
1220 memcpy( data, value->name, namelen );
1221 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1223 else
1225 reply->namelen = maxlen;
1226 memcpy( data, value->name, maxlen );
1229 if (debug_level > 1) dump_operation( key, value, "Enum" );
1233 /* delete a value */
1234 static void delete_value( struct key *key, const struct unicode_str *name )
1236 struct key_value *value;
1237 int i, index, nb_values;
1239 if (!(value = find_value( key, name, &index )))
1241 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1242 return;
1244 if (debug_level > 1) dump_operation( key, value, "Delete" );
1245 free( value->name );
1246 free( value->data );
1247 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1248 key->last_value--;
1249 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1251 /* try to shrink the array */
1252 nb_values = key->nb_values;
1253 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1255 struct key_value *new_val;
1256 nb_values -= nb_values / 3; /* shrink by 33% */
1257 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1258 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1259 key->values = new_val;
1260 key->nb_values = nb_values;
1264 /* get the registry key corresponding to an hkey handle */
1265 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1267 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1269 if (key && key->flags & KEY_DELETED)
1271 set_error( STATUS_KEY_DELETED );
1272 release_object( key );
1273 key = NULL;
1275 return key;
1278 /* get the registry key corresponding to a parent key handle */
1279 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1281 if (!hkey) return (struct key *)grab_object( root_key );
1282 return get_hkey_obj( hkey, 0 );
1285 /* read a line from the input file */
1286 static int read_next_line( struct file_load_info *info )
1288 char *newbuf;
1289 int newlen, pos = 0;
1291 info->line++;
1292 for (;;)
1294 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1295 return (pos != 0); /* EOF */
1296 pos = strlen(info->buffer);
1297 if (info->buffer[pos-1] == '\n')
1299 /* got a full line */
1300 info->buffer[--pos] = 0;
1301 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1302 return 1;
1304 if (pos < info->len - 1) return 1; /* EOF but something was read */
1306 /* need to enlarge the buffer */
1307 newlen = info->len + info->len / 2;
1308 if (!(newbuf = realloc( info->buffer, newlen )))
1310 set_error( STATUS_NO_MEMORY );
1311 return -1;
1313 info->buffer = newbuf;
1314 info->len = newlen;
1318 /* make sure the temp buffer holds enough space */
1319 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1321 WCHAR *tmp;
1322 if (info->tmplen >= size) return 1;
1323 if (!(tmp = realloc( info->tmp, size )))
1325 set_error( STATUS_NO_MEMORY );
1326 return 0;
1328 info->tmp = tmp;
1329 info->tmplen = size;
1330 return 1;
1333 /* report an error while loading an input file */
1334 static void file_read_error( const char *err, struct file_load_info *info )
1336 if (info->filename)
1337 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1338 else
1339 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1342 /* convert a data type tag to a value type */
1343 static int get_data_type( const char *buffer, int *type, int *parse_type )
1345 struct data_type { const char *tag; int len; int type; int parse_type; };
1347 static const struct data_type data_types[] =
1348 { /* actual type */ /* type to assume for parsing */
1349 { "\"", 1, REG_SZ, REG_SZ },
1350 { "str:\"", 5, REG_SZ, REG_SZ },
1351 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1352 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1353 { "hex:", 4, REG_BINARY, REG_BINARY },
1354 { "dword:", 6, REG_DWORD, REG_DWORD },
1355 { "hex(", 4, -1, REG_BINARY },
1356 { NULL, 0, 0, 0 }
1359 const struct data_type *ptr;
1360 char *end;
1362 for (ptr = data_types; ptr->tag; ptr++)
1364 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1365 *parse_type = ptr->parse_type;
1366 if ((*type = ptr->type) != -1) return ptr->len;
1367 /* "hex(xx):" is special */
1368 *type = (int)strtoul( buffer + 4, &end, 16 );
1369 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1370 return end + 2 - buffer;
1372 return 0;
1375 /* load and create a key from the input file */
1376 static struct key *load_key( struct key *base, const char *buffer, int prefix_len,
1377 struct file_load_info *info, timeout_t *modif )
1379 WCHAR *p;
1380 struct unicode_str name;
1381 int res;
1382 unsigned int mod;
1383 data_size_t len;
1385 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1387 len = info->tmplen;
1388 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1390 file_read_error( "Malformed key", info );
1391 return NULL;
1393 if (sscanf( buffer + res, " %u", &mod ) == 1)
1394 *modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1395 else
1396 *modif = current_time;
1398 p = info->tmp;
1399 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1401 if (!*p)
1403 if (prefix_len > 1)
1405 file_read_error( "Malformed key", info );
1406 return NULL;
1408 /* empty key name, return base key */
1409 return (struct key *)grab_object( base );
1411 name.str = p;
1412 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1413 return create_key_recursive( base, &name, 0 );
1416 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1417 static void update_key_time( struct key *key, timeout_t modif )
1419 while (key && !key->modif)
1421 key->modif = modif;
1422 key = key->parent;
1426 /* load a global option from the input file */
1427 static int load_global_option( const char *buffer, struct file_load_info *info )
1429 const char *p;
1431 if (!strncmp( buffer, "#arch=", 6 ))
1433 enum prefix_type type;
1434 p = buffer + 6;
1435 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1436 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1437 else
1439 file_read_error( "Unknown architecture", info );
1440 set_error( STATUS_NOT_REGISTRY_FILE );
1441 return 0;
1443 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1444 else if (type != prefix_type)
1446 file_read_error( "Mismatched architecture", info );
1447 set_error( STATUS_NOT_REGISTRY_FILE );
1448 return 0;
1451 /* ignore unknown options */
1452 return 1;
1455 /* load a key option from the input file */
1456 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1458 const char *p;
1459 data_size_t len;
1461 if (!strncmp( buffer, "#time=", 6 ))
1463 timeout_t modif = 0;
1464 for (p = buffer + 6; *p; p++)
1466 if (*p >= '0' && *p <= '9') modif = (modif << 4) | (*p - '0');
1467 else if (*p >= 'A' && *p <= 'F') modif = (modif << 4) | (*p - 'A' + 10);
1468 else if (*p >= 'a' && *p <= 'f') modif = (modif << 4) | (*p - 'a' + 10);
1469 else break;
1471 update_key_time( key, modif );
1473 if (!strncmp( buffer, "#class=", 7 ))
1475 p = buffer + 7;
1476 if (*p++ != '"') return 0;
1477 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1478 len = info->tmplen;
1479 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1480 free( key->class );
1481 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1482 key->classlen = len;
1484 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1485 /* ignore unknown options */
1486 return 1;
1489 /* parse a comma-separated list of hex digits */
1490 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1492 const char *p = buffer;
1493 data_size_t count = 0;
1494 char *end;
1496 while (isxdigit(*p))
1498 unsigned int val = strtoul( p, &end, 16 );
1499 if (end == p || val > 0xff) return -1;
1500 if (count++ >= *len) return -1; /* dest buffer overflow */
1501 *dest++ = val;
1502 p = end;
1503 while (isspace(*p)) p++;
1504 if (*p == ',') p++;
1505 while (isspace(*p)) p++;
1507 *len = count;
1508 return p - buffer;
1511 /* parse a value name and create the corresponding value */
1512 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1513 struct file_load_info *info )
1515 struct key_value *value;
1516 struct unicode_str name;
1517 int index;
1519 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1520 name.str = info->tmp;
1521 name.len = info->tmplen;
1522 if (buffer[0] == '@')
1524 name.len = 0;
1525 *len = 1;
1527 else
1529 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1530 if (r == -1) goto error;
1531 *len = r + 1; /* for initial quote */
1532 name.len -= sizeof(WCHAR); /* terminating null */
1534 while (isspace(buffer[*len])) (*len)++;
1535 if (buffer[*len] != '=') goto error;
1536 (*len)++;
1537 while (isspace(buffer[*len])) (*len)++;
1538 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1539 return value;
1541 error:
1542 file_read_error( "Malformed value name", info );
1543 return NULL;
1546 /* load a value from the input file */
1547 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1549 DWORD dw;
1550 void *ptr, *newptr;
1551 int res, type, parse_type;
1552 data_size_t maxlen, len;
1553 struct key_value *value;
1555 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1556 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1557 buffer += len + res;
1559 switch(parse_type)
1561 case REG_SZ:
1562 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1563 len = info->tmplen;
1564 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1565 ptr = info->tmp;
1566 break;
1567 case REG_DWORD:
1568 dw = strtoul( buffer, NULL, 16 );
1569 ptr = &dw;
1570 len = sizeof(dw);
1571 break;
1572 case REG_BINARY: /* hex digits */
1573 len = 0;
1574 for (;;)
1576 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1577 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1578 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1579 len += maxlen;
1580 buffer += res;
1581 while (isspace(*buffer)) buffer++;
1582 if (!*buffer) break;
1583 if (*buffer != '\\') goto error;
1584 if (read_next_line( info) != 1) goto error;
1585 buffer = info->buffer;
1586 while (isspace(*buffer)) buffer++;
1588 ptr = info->tmp;
1589 break;
1590 default:
1591 assert(0);
1592 ptr = NULL; /* keep compiler quiet */
1593 break;
1596 if (!len) newptr = NULL;
1597 else if (!(newptr = memdup( ptr, len ))) return 0;
1599 free( value->data );
1600 value->data = newptr;
1601 value->len = len;
1602 value->type = type;
1603 return 1;
1605 error:
1606 file_read_error( "Malformed value", info );
1607 free( value->data );
1608 value->data = NULL;
1609 value->len = 0;
1610 value->type = REG_NONE;
1611 return 0;
1614 /* return the length (in path elements) of name that is part of the key name */
1615 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1616 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1618 WCHAR *p;
1619 int res;
1620 data_size_t len;
1622 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1624 len = info->tmplen;
1625 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1627 file_read_error( "Malformed key", info );
1628 return 0;
1630 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1631 len = (p - info->tmp) * sizeof(WCHAR);
1632 for (res = 1; key != root_key; res++)
1634 if (len == key->namelen && !memicmp_strW( info->tmp, key->name, len )) break;
1635 key = key->parent;
1637 if (key == root_key) res = 0; /* no matching name */
1638 return res;
1641 /* load all the keys from the input file */
1642 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1643 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1645 struct key *subkey = NULL;
1646 struct file_load_info info;
1647 timeout_t modif = current_time;
1648 char *p;
1650 info.filename = filename;
1651 info.file = f;
1652 info.len = 4;
1653 info.tmplen = 4;
1654 info.line = 0;
1655 if (!(info.buffer = mem_alloc( info.len ))) return;
1656 if (!(info.tmp = mem_alloc( info.tmplen )))
1658 free( info.buffer );
1659 return;
1662 if ((read_next_line( &info ) != 1) ||
1663 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1665 set_error( STATUS_NOT_REGISTRY_FILE );
1666 goto done;
1669 while (read_next_line( &info ) == 1)
1671 p = info.buffer;
1672 while (*p && isspace(*p)) p++;
1673 switch(*p)
1675 case '[': /* new key */
1676 if (subkey)
1678 update_key_time( subkey, modif );
1679 release_object( subkey );
1681 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1682 if (!(subkey = load_key( key, p + 1, prefix_len, &info, &modif )))
1683 file_read_error( "Error creating key", &info );
1684 break;
1685 case '@': /* default value */
1686 case '\"': /* value */
1687 if (subkey) load_value( subkey, p, &info );
1688 else file_read_error( "Value without key", &info );
1689 break;
1690 case '#': /* option */
1691 if (subkey) load_key_option( subkey, p, &info );
1692 else if (!load_global_option( p, &info )) goto done;
1693 break;
1694 case ';': /* comment */
1695 case 0: /* empty line */
1696 break;
1697 default:
1698 file_read_error( "Unrecognized input", &info );
1699 break;
1703 done:
1704 if (subkey)
1706 update_key_time( subkey, modif );
1707 release_object( subkey );
1709 free( info.buffer );
1710 free( info.tmp );
1713 /* load a part of the registry from a file */
1714 static void load_registry( struct key *key, obj_handle_t handle )
1716 struct file *file;
1717 int fd;
1719 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1720 fd = dup( get_file_unix_fd( file ) );
1721 release_object( file );
1722 if (fd != -1)
1724 FILE *f = fdopen( fd, "r" );
1725 if (f)
1727 load_keys( key, NULL, f, -1 );
1728 fclose( f );
1730 else file_set_error();
1734 /* load one of the initial registry files */
1735 static int load_init_registry_from_file( const char *filename, struct key *key )
1737 FILE *f;
1739 if ((f = fopen( filename, "r" )))
1741 load_keys( key, filename, f, 0 );
1742 fclose( f );
1743 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1745 fprintf( stderr, "%s is not a valid registry file\n", filename );
1746 return 1;
1750 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1752 save_branch_info[save_branch_count].path = filename;
1753 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1754 make_object_permanent( &key->obj );
1755 return (f != NULL);
1758 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1760 char buffer[7 + 11 + 11 + 11 * SID_MAX_SUB_AUTHORITIES], *p = buffer;
1761 unsigned int i;
1763 p += sprintf( p, "User\\S-%u-%u", sid->Revision,
1764 MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1765 sid->IdentifierAuthority.Value[4] ),
1766 MAKEWORD( sid->IdentifierAuthority.Value[3],
1767 sid->IdentifierAuthority.Value[2] )));
1768 for (i = 0; i < sid->SubAuthorityCount; i++) p += sprintf( p, "-%u", sid->SubAuthority[i] );
1769 return ascii_to_unicode_str( buffer, path );
1772 static void init_supported_machines(void)
1774 unsigned int count = 0;
1775 #ifdef __i386__
1776 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1777 #elif defined(__x86_64__)
1778 if (prefix_type == PREFIX_64BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_AMD64;
1779 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1780 #elif defined(__arm__)
1781 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1782 #elif defined(__aarch64__)
1783 if (prefix_type == PREFIX_64BIT)
1785 supported_machines[count++] = IMAGE_FILE_MACHINE_ARM64;
1786 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1788 supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1789 #else
1790 #error Unsupported machine
1791 #endif
1792 supported_machines_count = count;
1795 /* registry initialisation */
1796 void init_registry(void)
1798 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1799 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1800 static const WCHAR classes_i386[] = {'S','o','f','t','w','a','r','e','\\',
1801 'C','l','a','s','s','e','s','\\',
1802 'W','o','w','6','4','3','2','N','o','d','e'};
1803 static const WCHAR classes_amd64[] = {'S','o','f','t','w','a','r','e','\\',
1804 'C','l','a','s','s','e','s','\\',
1805 'W','o','w','6','4','6','4','N','o','d','e'};
1806 static const WCHAR classes_arm[] = {'S','o','f','t','w','a','r','e','\\',
1807 'C','l','a','s','s','e','s','\\',
1808 'W','o','w','A','A','3','2','N','o','d','e'};
1809 static const WCHAR classes_arm64[] = {'S','o','f','t','w','a','r','e','\\',
1810 'C','l','a','s','s','e','s','\\',
1811 'W','o','w','A','A','6','4','N','o','d','e'};
1812 static const struct unicode_str root_name = { NULL, 0 };
1813 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1814 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1816 WCHAR *current_user_path;
1817 struct unicode_str current_user_str;
1818 struct key *key, *hklm, *hkcu;
1819 unsigned int i;
1820 char *p;
1822 /* switch to the config dir */
1824 if (fchdir( config_dir_fd ) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno ));
1826 /* create the root key */
1827 root_key = alloc_key( &root_name, current_time );
1828 assert( root_key );
1829 make_object_permanent( &root_key->obj );
1831 /* load system.reg into Registry\Machine */
1833 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1834 fatal_error( "could not create Machine registry key\n" );
1836 if (!load_init_registry_from_file( "system.reg", hklm ))
1838 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1839 prefix_type = PREFIX_32BIT;
1840 else
1841 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1843 else if (prefix_type == PREFIX_UNKNOWN)
1844 prefix_type = PREFIX_32BIT;
1846 init_supported_machines();
1848 /* load userdef.reg into Registry\User\.Default */
1850 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1851 fatal_error( "could not create User\\.Default registry key\n" );
1853 load_init_registry_from_file( "userdef.reg", key );
1854 release_object( key );
1856 /* load user.reg into HKEY_CURRENT_USER */
1858 /* FIXME: match default user in token.c. should get from process token instead */
1859 current_user_path = format_user_registry_path( security_local_user_sid, &current_user_str );
1860 if (!current_user_path ||
1861 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1862 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1863 free( current_user_path );
1864 load_init_registry_from_file( "user.reg", hkcu );
1866 /* set the shared flag on Software\Classes\Wow6432Node for all platforms */
1867 for (i = 1; i < supported_machines_count; i++)
1869 struct unicode_str name;
1871 switch (supported_machines[i])
1873 case IMAGE_FILE_MACHINE_I386: name.str = classes_i386; name.len = sizeof(classes_i386); break;
1874 case IMAGE_FILE_MACHINE_ARMNT: name.str = classes_arm; name.len = sizeof(classes_arm); break;
1875 case IMAGE_FILE_MACHINE_AMD64: name.str = classes_amd64; name.len = sizeof(classes_amd64); break;
1876 case IMAGE_FILE_MACHINE_ARM64: name.str = classes_arm64; name.len = sizeof(classes_arm64); break;
1878 if ((key = create_key_recursive( hklm, &name, current_time )))
1880 key->flags |= KEY_WOWSHARE;
1881 release_object( key );
1883 /* FIXME: handle HKCU too */
1886 release_object( hklm );
1887 release_object( hkcu );
1889 /* start the periodic save timer */
1890 set_periodic_save_timer();
1892 /* create windows directories */
1894 if (!mkdir( "drive_c/windows", 0777 ))
1896 mkdir( "drive_c/windows/system32", 0777 );
1897 for (i = 1; i < supported_machines_count; i++)
1899 switch (supported_machines[i])
1901 case IMAGE_FILE_MACHINE_I386: mkdir( "drive_c/windows/syswow64", 0777 ); break;
1902 case IMAGE_FILE_MACHINE_ARMNT: mkdir( "drive_c/windows/sysarm32", 0777 ); break;
1903 case IMAGE_FILE_MACHINE_AMD64: mkdir( "drive_c/windows/sysx8664", 0777 ); break;
1904 case IMAGE_FILE_MACHINE_ARM64: mkdir( "drive_c/windows/sysarm64", 0777 ); break;
1909 /* go back to the server dir */
1910 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
1913 /* save a registry branch to a file */
1914 static void save_all_subkeys( struct key *key, FILE *f )
1916 fprintf( f, "WINE REGISTRY Version 2\n" );
1917 fprintf( f, ";; All keys relative to " );
1918 dump_path( key, NULL, f );
1919 fprintf( f, "\n" );
1920 switch (prefix_type)
1922 case PREFIX_32BIT:
1923 fprintf( f, "\n#arch=win32\n" );
1924 break;
1925 case PREFIX_64BIT:
1926 fprintf( f, "\n#arch=win64\n" );
1927 break;
1928 default:
1929 break;
1931 save_subkeys( key, key, f );
1934 /* save a registry branch to a file handle */
1935 static void save_registry( struct key *key, obj_handle_t handle )
1937 struct file *file;
1938 int fd;
1940 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1941 fd = dup( get_file_unix_fd( file ) );
1942 release_object( file );
1943 if (fd != -1)
1945 FILE *f = fdopen( fd, "w" );
1946 if (f)
1948 save_all_subkeys( key, f );
1949 if (fclose( f )) file_set_error();
1951 else
1953 file_set_error();
1954 close( fd );
1959 /* save a registry branch to a file */
1960 static int save_branch( struct key *key, const char *path )
1962 struct stat st;
1963 char *p, *tmp = NULL;
1964 int fd, count = 0, ret = 0;
1965 FILE *f;
1967 if (!(key->flags & KEY_DIRTY))
1969 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1970 return 1;
1973 /* test the file type */
1975 if ((fd = open( path, O_WRONLY )) != -1)
1977 /* if file is not a regular file or has multiple links or is accessed
1978 * via symbolic links, write directly into it; otherwise use a temp file */
1979 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1981 ftruncate( fd, 0 );
1982 goto save;
1984 close( fd );
1987 /* create a temp file in the same directory */
1989 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1990 strcpy( tmp, path );
1991 if ((p = strrchr( tmp, '/' ))) p++;
1992 else p = tmp;
1993 for (;;)
1995 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1996 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1997 if (errno != EEXIST) goto done;
1998 close( fd );
2001 /* now save to it */
2003 save:
2004 if (!(f = fdopen( fd, "w" )))
2006 if (tmp) unlink( tmp );
2007 close( fd );
2008 goto done;
2011 if (debug_level > 1)
2013 fprintf( stderr, "%s: ", path );
2014 dump_operation( key, NULL, "saving" );
2017 save_all_subkeys( key, f );
2018 ret = !fclose(f);
2020 if (tmp)
2022 /* if successfully written, rename to final name */
2023 if (ret) ret = !rename( tmp, path );
2024 if (!ret) unlink( tmp );
2027 done:
2028 free( tmp );
2029 if (ret) make_clean( key );
2030 return ret;
2033 /* periodic saving of the registry */
2034 static void periodic_save( void *arg )
2036 int i;
2038 if (fchdir( config_dir_fd ) == -1) return;
2039 save_timeout_user = NULL;
2040 for (i = 0; i < save_branch_count; i++)
2041 save_branch( save_branch_info[i].key, save_branch_info[i].path );
2042 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2043 set_periodic_save_timer();
2046 /* start the periodic save timer */
2047 static void set_periodic_save_timer(void)
2049 if (save_timeout_user) remove_timeout_user( save_timeout_user );
2050 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
2053 /* save the modified registry branches to disk */
2054 void flush_registry(void)
2056 int i;
2058 if (fchdir( config_dir_fd ) == -1) return;
2059 for (i = 0; i < save_branch_count; i++)
2061 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
2063 fprintf( stderr, "wineserver: could not save registry branch to %s",
2064 save_branch_info[i].path );
2065 perror( " " );
2068 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2071 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2072 static int is_wow64_thread( struct thread *thread )
2074 return (is_machine_64bit( supported_machines[0] ) && !is_machine_64bit( thread->process->machine ));
2078 /* create a registry key */
2079 DECL_HANDLER(create_key)
2081 struct key *key = NULL, *parent;
2082 struct unicode_str name, class;
2083 unsigned int access = req->access;
2084 const struct security_descriptor *sd;
2085 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2087 if (!objattr) return;
2089 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2091 class.str = get_req_data_after_objattr( objattr, &class.len );
2092 class.len = (class.len / sizeof(WCHAR)) * sizeof(WCHAR);
2094 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2095 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2097 name.str += ARRAY_SIZE( root_name );
2098 name.len -= sizeof(root_name);
2101 /* NOTE: no access rights are required from the parent handle to create a key */
2102 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2104 if ((key = create_key( parent, &name, &class, req->options, access,
2105 objattr->attributes, sd, &reply->created )))
2107 reply->hkey = alloc_handle( current->process, key, access, objattr->attributes );
2108 release_object( key );
2110 release_object( parent );
2114 /* open a registry key */
2115 DECL_HANDLER(open_key)
2117 struct key *key, *parent;
2118 struct unicode_str name;
2119 unsigned int access = req->access;
2121 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2123 reply->hkey = 0;
2124 /* NOTE: no access rights are required to open the parent key, only the child key */
2125 if ((parent = get_parent_hkey_obj( req->parent )))
2127 get_req_path( &name, !req->parent );
2128 if ((key = open_key( parent, &name, access, req->attributes )))
2130 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
2131 release_object( key );
2133 release_object( parent );
2137 /* delete a registry key */
2138 DECL_HANDLER(delete_key)
2140 struct key *key;
2142 if ((key = get_hkey_obj( req->hkey, DELETE )))
2144 delete_key( key, 0);
2145 release_object( key );
2149 /* flush a registry key */
2150 DECL_HANDLER(flush_key)
2152 struct key *key = get_hkey_obj( req->hkey, 0 );
2153 if (key)
2155 /* we don't need to do anything here with the current implementation */
2156 release_object( key );
2160 /* enumerate registry subkeys */
2161 DECL_HANDLER(enum_key)
2163 struct key *key;
2165 if ((key = get_hkey_obj( req->hkey,
2166 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
2168 enum_key( key, req->index, req->info_class, reply );
2169 release_object( key );
2173 /* set a value of a registry key */
2174 DECL_HANDLER(set_key_value)
2176 struct key *key;
2177 struct unicode_str name;
2179 if (req->namelen > get_req_data_size())
2181 set_error( STATUS_INVALID_PARAMETER );
2182 return;
2184 name.str = get_req_data();
2185 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2187 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2189 data_size_t datalen = get_req_data_size() - req->namelen;
2190 const char *data = (const char *)get_req_data() + req->namelen;
2192 set_value( key, &name, req->type, data, datalen );
2193 release_object( key );
2197 /* retrieve the value of a registry key */
2198 DECL_HANDLER(get_key_value)
2200 struct key *key;
2201 struct unicode_str name = get_req_unicode_str();
2203 reply->total = 0;
2204 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2206 get_value( key, &name, &reply->type, &reply->total );
2207 release_object( key );
2211 /* enumerate the value of a registry key */
2212 DECL_HANDLER(enum_key_value)
2214 struct key *key;
2216 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2218 enum_value( key, req->index, req->info_class, reply );
2219 release_object( key );
2223 /* delete a value of a registry key */
2224 DECL_HANDLER(delete_key_value)
2226 struct key *key;
2227 struct unicode_str name = get_req_unicode_str();
2229 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2231 delete_value( key, &name );
2232 release_object( key );
2236 /* load a registry branch from a file */
2237 DECL_HANDLER(load_registry)
2239 struct key *key, *parent;
2240 struct unicode_str name;
2241 const struct security_descriptor *sd;
2242 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2244 if (!objattr) return;
2246 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2248 set_error( STATUS_PRIVILEGE_NOT_HELD );
2249 return;
2252 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2253 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2255 name.str += ARRAY_SIZE( root_name );
2256 name.len -= sizeof(root_name);
2259 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2261 int dummy;
2262 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, sd, &dummy )))
2264 load_registry( key, req->file );
2265 release_object( key );
2267 release_object( parent );
2271 DECL_HANDLER(unload_registry)
2273 struct key *key, *parent;
2274 struct unicode_str name;
2275 unsigned int access = 0;
2277 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2279 set_error( STATUS_PRIVILEGE_NOT_HELD );
2280 return;
2283 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2285 if ((parent = get_parent_hkey_obj( req->parent )))
2287 get_req_path( &name, !req->parent );
2288 if ((key = open_key( parent, &name, access, req->attributes )))
2290 if (key->obj.handle_count)
2291 set_error( STATUS_CANNOT_DELETE );
2292 else
2293 delete_key( key, 1 ); /* FIXME */
2294 release_object( key );
2296 release_object( parent );
2300 /* save a registry branch to a file */
2301 DECL_HANDLER(save_registry)
2303 struct key *key;
2305 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2307 set_error( STATUS_PRIVILEGE_NOT_HELD );
2308 return;
2311 if ((key = get_hkey_obj( req->hkey, 0 )))
2313 save_registry( key, req->file );
2314 release_object( key );
2318 /* add a registry key change notification */
2319 DECL_HANDLER(set_registry_notification)
2321 struct key *key;
2322 struct event *event;
2323 struct notify *notify;
2325 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2326 if (key)
2328 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2329 if (event)
2331 notify = find_notify( key, current->process, req->hkey );
2332 if (!notify)
2334 notify = mem_alloc( sizeof(*notify) );
2335 if (notify)
2337 notify->events = NULL;
2338 notify->event_count = 0;
2339 notify->subtree = req->subtree;
2340 notify->filter = req->filter;
2341 notify->hkey = req->hkey;
2342 notify->process = current->process;
2343 list_add_head( &key->notify_list, &notify->entry );
2346 if (notify)
2348 struct event **new_array;
2350 if ((new_array = realloc( notify->events, (notify->event_count + 1) * sizeof(*notify->events) )))
2352 notify->events = new_array;
2353 notify->events[notify->event_count++] = (struct event *)grab_object( event );
2354 reset_event( event );
2355 set_error( STATUS_PENDING );
2357 else set_error( STATUS_NO_MEMORY );
2359 release_object( event );
2361 release_object( key );