gdi32: Support MWT_SET in NtGdiModifyWorldTransform.
[wine.git] / server / registry.c
blob427da8a5883f3bc7f002913cb4dfd2ca999ff194
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) */
104 #define KEY_PREDEF 0x0040 /* key is marked as predefined */
106 /* a key value */
107 struct key_value
109 WCHAR *name; /* value name */
110 unsigned short namelen; /* length of value name */
111 unsigned int type; /* value type */
112 data_size_t len; /* value data length in bytes */
113 void *data; /* pointer to value data */
116 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
117 #define MIN_VALUES 8 /* min. number of allocated values per key */
119 #define MAX_NAME_LEN 256 /* max. length of a key name */
120 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
122 /* the root of the registry tree */
123 static struct key *root_key;
125 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
126 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
127 static struct timeout_user *save_timeout_user; /* saving timer */
128 static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
130 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
131 static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
132 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
133 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
135 static void set_periodic_save_timer(void);
136 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
138 /* information about where to save a registry branch */
139 struct save_branch_info
141 struct key *key;
142 const char *path;
145 #define MAX_SAVE_BRANCH_INFO 3
146 static int save_branch_count;
147 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
149 unsigned int supported_machines_count = 0;
150 unsigned short supported_machines[8];
151 unsigned short native_machine = 0;
153 /* information about a file being loaded */
154 struct file_load_info
156 const char *filename; /* input file name */
157 FILE *file; /* input file */
158 char *buffer; /* line buffer */
159 int len; /* buffer length */
160 int line; /* current input line */
161 WCHAR *tmp; /* temp buffer to use while parsing input */
162 size_t tmplen; /* length of temp buffer */
166 static void key_dump( struct object *obj, int verbose );
167 static unsigned int key_map_access( struct object *obj, unsigned int access );
168 static struct security_descriptor *key_get_sd( struct object *obj );
169 static WCHAR *key_get_full_name( struct object *obj, data_size_t *len );
170 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
171 static void key_destroy( struct object *obj );
173 static const struct object_ops key_ops =
175 sizeof(struct key), /* size */
176 &key_type, /* type */
177 key_dump, /* dump */
178 no_add_queue, /* add_queue */
179 NULL, /* remove_queue */
180 NULL, /* signaled */
181 NULL, /* satisfied */
182 no_signal, /* signal */
183 no_get_fd, /* get_fd */
184 key_map_access, /* map_access */
185 key_get_sd, /* get_sd */
186 default_set_sd, /* set_sd */
187 key_get_full_name, /* get_full_name */
188 no_lookup_name, /* lookup_name */
189 no_link_name, /* link_name */
190 NULL, /* unlink_name */
191 no_open_file, /* open_file */
192 no_kernel_obj_list, /* get_kernel_obj_list */
193 key_close_handle, /* close_handle */
194 key_destroy /* destroy */
198 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
200 return (len == sizeof(wow6432node) && !memicmp_strW( name, wow6432node, sizeof( wow6432node )));
204 * The registry text file format v2 used by this code is similar to the one
205 * used by REGEDIT import/export functionality, with the following differences:
206 * - strings and key names can contain \x escapes for Unicode
207 * - key names use escapes too in order to support Unicode
208 * - the modification time optionally follows the key name
209 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
212 /* dump the full path of a key */
213 static void dump_path( const struct key *key, const struct key *base, FILE *f )
215 if (key->parent && key->parent != base)
217 dump_path( key->parent, base, f );
218 fprintf( f, "\\\\" );
220 dump_strW( key->name, key->namelen, f, "[]" );
223 /* dump a value to a text file */
224 static void dump_value( const struct key_value *value, FILE *f )
226 unsigned int i, dw;
227 int count;
229 if (value->namelen)
231 fputc( '\"', f );
232 count = 1 + dump_strW( value->name, value->namelen, f, "\"\"" );
233 count += fprintf( f, "\"=" );
235 else count = fprintf( f, "@=" );
237 switch(value->type)
239 case REG_SZ:
240 case REG_EXPAND_SZ:
241 case REG_MULTI_SZ:
242 /* only output properly terminated strings in string format */
243 if (value->len < sizeof(WCHAR)) break;
244 if (value->len % sizeof(WCHAR)) break;
245 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
246 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
247 fputc( '\"', f );
248 dump_strW( (WCHAR *)value->data, value->len, f, "\"\"" );
249 fprintf( f, "\"\n" );
250 return;
252 case REG_DWORD:
253 if (value->len != sizeof(dw)) break;
254 memcpy( &dw, value->data, sizeof(dw) );
255 fprintf( f, "dword:%08x\n", dw );
256 return;
259 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
260 else count += fprintf( f, "hex(%x):", value->type );
261 for (i = 0; i < value->len; i++)
263 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
264 if (i < value->len-1)
266 fputc( ',', f );
267 if (++count > 76)
269 fprintf( f, "\\\n " );
270 count = 2;
274 fputc( '\n', f );
277 /* save a registry and all its subkeys to a text file */
278 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
280 int i;
282 if (key->flags & KEY_VOLATILE) return;
283 /* save key if it has either some values or no subkeys, or needs special options */
284 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
285 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
287 fprintf( f, "\n[" );
288 if (key != base) dump_path( key, base, f );
289 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
290 fprintf( f, "#time=%x%08x\n", (unsigned int)(key->modif >> 32), (unsigned int)key->modif );
291 if (key->class)
293 fprintf( f, "#class=\"" );
294 dump_strW( key->class, key->classlen, f, "\"\"" );
295 fprintf( f, "\"\n" );
297 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
298 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
300 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
303 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
305 fprintf( stderr, "%s key ", op );
306 if (key) dump_path( key, NULL, stderr );
307 else fprintf( stderr, "ERROR" );
308 if (value)
310 fprintf( stderr, " value ");
311 dump_value( value, stderr );
313 else fprintf( stderr, "\n" );
316 static void key_dump( struct object *obj, int verbose )
318 struct key *key = (struct key *)obj;
319 assert( obj->ops == &key_ops );
320 fprintf( stderr, "Key flags=%x ", key->flags );
321 dump_path( key, NULL, stderr );
322 fprintf( stderr, "\n" );
325 /* notify waiter and maybe delete the notification */
326 static void do_notification( struct key *key, struct notify *notify, int del )
328 unsigned int i;
330 for (i = 0; i < notify->event_count; ++i)
332 set_event( notify->events[i] );
333 release_object( notify->events[i] );
335 free( notify->events );
336 notify->events = NULL;
337 notify->event_count = 0;
339 if (del)
341 list_remove( &notify->entry );
342 free( notify );
346 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
348 struct notify *notify;
350 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
352 if (notify->process == process && notify->hkey == hkey) return notify;
354 return NULL;
357 static unsigned int key_map_access( struct object *obj, unsigned int access )
359 access = default_map_access( obj, access );
360 /* filter the WOW64 masks, as they aren't real access bits */
361 return access & ~(KEY_WOW64_64KEY | KEY_WOW64_32KEY);
364 static struct security_descriptor *key_get_sd( struct object *obj )
366 static struct security_descriptor *key_default_sd;
368 if (obj->sd) return obj->sd;
370 if (!key_default_sd)
372 size_t users_sid_len = security_sid_len( security_builtin_users_sid );
373 size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
374 size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
375 + users_sid_len + admins_sid_len;
376 ACCESS_ALLOWED_ACE *aaa;
377 ACL *dacl;
379 key_default_sd = mem_alloc( sizeof(*key_default_sd) + 2 * admins_sid_len + dacl_len );
380 key_default_sd->control = SE_DACL_PRESENT;
381 key_default_sd->owner_len = admins_sid_len;
382 key_default_sd->group_len = admins_sid_len;
383 key_default_sd->sacl_len = 0;
384 key_default_sd->dacl_len = dacl_len;
385 memcpy( key_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
386 memcpy( (char *)(key_default_sd + 1) + admins_sid_len, security_builtin_admins_sid, admins_sid_len );
388 dacl = (ACL *)((char *)(key_default_sd + 1) + 2 * admins_sid_len);
389 dacl->AclRevision = ACL_REVISION;
390 dacl->Sbz1 = 0;
391 dacl->AclSize = dacl_len;
392 dacl->AceCount = 2;
393 dacl->Sbz2 = 0;
394 aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
395 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
396 aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
397 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
398 aaa->Mask = GENERIC_READ;
399 memcpy( &aaa->SidStart, security_builtin_users_sid, users_sid_len );
400 aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
401 aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
402 aaa->Header.AceFlags = 0;
403 aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
404 aaa->Mask = KEY_ALL_ACCESS;
405 memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
407 return key_default_sd;
410 static WCHAR *key_get_full_name( struct object *obj, data_size_t *ret_len )
412 static const WCHAR backslash = '\\';
413 struct key *key = (struct key *) obj;
414 data_size_t len = sizeof(root_name) - sizeof(WCHAR);
415 char *ret;
417 if (key->flags & KEY_DELETED)
419 set_error( STATUS_KEY_DELETED );
420 return NULL;
423 for (key = (struct key *)obj; key != root_key; key = key->parent) len += key->namelen + sizeof(WCHAR);
424 if (!(ret = malloc( len ))) return NULL;
426 *ret_len = len;
427 key = (struct key *)obj;
428 for (key = (struct key *)obj; key != root_key; key = key->parent)
430 memcpy( ret + len - key->namelen, key->name, key->namelen );
431 len -= key->namelen + sizeof(WCHAR);
432 memcpy( ret + len, &backslash, sizeof(WCHAR) );
434 memcpy( ret, root_name, sizeof(root_name) - sizeof(WCHAR) );
435 return (WCHAR *)ret;
438 /* close the notification associated with a handle */
439 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
441 struct key * key = (struct key *) obj;
442 struct notify *notify = find_notify( key, process, handle );
443 if (notify) do_notification( key, notify, 1 );
444 return 1; /* ok to close */
447 static void key_destroy( struct object *obj )
449 int i;
450 struct list *ptr;
451 struct key *key = (struct key *)obj;
452 assert( obj->ops == &key_ops );
454 free( key->name );
455 free( key->class );
456 for (i = 0; i <= key->last_value; i++)
458 free( key->values[i].name );
459 free( key->values[i].data );
461 free( key->values );
462 for (i = 0; i <= key->last_subkey; i++)
464 key->subkeys[i]->parent = NULL;
465 release_object( key->subkeys[i] );
467 free( key->subkeys );
468 /* unconditionally notify everything waiting on this key */
469 while ((ptr = list_head( &key->notify_list )))
471 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
472 do_notification( key, notify, 1 );
476 /* get the request vararg as registry path */
477 static inline void get_req_path( struct unicode_str *str, int skip_root )
479 str->str = get_req_data();
480 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
482 if (skip_root && str->len >= sizeof(root_name) && !memicmp_strW( str->str, root_name, sizeof(root_name) ))
484 str->str += ARRAY_SIZE( root_name );
485 str->len -= sizeof(root_name);
489 /* return the next token in a given path */
490 /* token->str must point inside the path, or be NULL for the first call */
491 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
493 data_size_t i = 0, len = path->len / sizeof(WCHAR);
495 if (!token->str) /* first time */
497 /* path cannot start with a backslash */
498 if (len && path->str[0] == '\\')
500 set_error( STATUS_OBJECT_PATH_INVALID );
501 return NULL;
504 else
506 i = token->str - path->str;
507 i += token->len / sizeof(WCHAR);
508 while (i < len && path->str[i] == '\\') i++;
510 token->str = path->str + i;
511 while (i < len && path->str[i] != '\\') i++;
512 token->len = (path->str + i - token->str) * sizeof(WCHAR);
513 return token;
516 /* allocate a key object */
517 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
519 struct key *key;
520 if ((key = alloc_object( &key_ops )))
522 key->name = NULL;
523 key->class = NULL;
524 key->namelen = name->len;
525 key->classlen = 0;
526 key->flags = 0;
527 key->last_subkey = -1;
528 key->nb_subkeys = 0;
529 key->subkeys = NULL;
530 key->nb_values = 0;
531 key->last_value = -1;
532 key->values = NULL;
533 key->modif = modif;
534 key->parent = NULL;
535 list_init( &key->notify_list );
536 if (name->len && !(key->name = memdup( name->str, name->len )))
538 release_object( key );
539 key = NULL;
542 return key;
545 /* mark a key and all its parents as dirty (modified) */
546 static void make_dirty( struct key *key )
548 while (key)
550 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
551 key->flags |= KEY_DIRTY;
552 key = key->parent;
556 /* mark a key and all its subkeys as clean (not modified) */
557 static void make_clean( struct key *key )
559 int i;
561 if (key->flags & KEY_VOLATILE) return;
562 if (!(key->flags & KEY_DIRTY)) return;
563 key->flags &= ~KEY_DIRTY;
564 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
567 /* go through all the notifications and send them if necessary */
568 static void check_notify( struct key *key, unsigned int change, int not_subtree )
570 struct list *ptr, *next;
572 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
574 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
575 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
576 do_notification( key, n, 0 );
580 /* update key modification time */
581 static void touch_key( struct key *key, unsigned int change )
583 struct key *k;
585 key->modif = current_time;
586 make_dirty( key );
588 /* do notifications */
589 check_notify( key, change, 1 );
590 for ( k = key->parent; k; k = k->parent )
591 check_notify( k, change, 0 );
594 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
595 static int grow_subkeys( struct key *key )
597 struct key **new_subkeys;
598 int nb_subkeys;
600 if (key->nb_subkeys)
602 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
603 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
605 set_error( STATUS_NO_MEMORY );
606 return 0;
609 else
611 nb_subkeys = MIN_SUBKEYS;
612 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
614 key->subkeys = new_subkeys;
615 key->nb_subkeys = nb_subkeys;
616 return 1;
619 /* allocate a subkey for a given key, and return its index */
620 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
621 int index, timeout_t modif )
623 struct key *key;
624 int i;
626 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
628 set_error( STATUS_INVALID_PARAMETER );
629 return NULL;
631 if (parent->last_subkey + 1 == parent->nb_subkeys)
633 /* need to grow the array */
634 if (!grow_subkeys( parent )) return NULL;
636 if ((key = alloc_key( name, modif )) != NULL)
638 key->parent = parent;
639 for (i = ++parent->last_subkey; i > index; i--)
640 parent->subkeys[i] = parent->subkeys[i-1];
641 parent->subkeys[index] = key;
642 if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
643 parent->flags |= KEY_WOW64;
645 return key;
648 /* free a subkey of a given key */
649 static void free_subkey( struct key *parent, int index )
651 struct key *key;
652 int i, nb_subkeys;
654 assert( index >= 0 );
655 assert( index <= parent->last_subkey );
657 key = parent->subkeys[index];
658 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
659 parent->last_subkey--;
660 key->flags |= KEY_DELETED;
661 key->parent = NULL;
662 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
663 release_object( key );
665 /* try to shrink the array */
666 nb_subkeys = parent->nb_subkeys;
667 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
669 struct key **new_subkeys;
670 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
671 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
672 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
673 parent->subkeys = new_subkeys;
674 parent->nb_subkeys = nb_subkeys;
678 /* find the named child of a given key and return its index */
679 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
681 int i, min, max, res;
682 data_size_t len;
684 min = 0;
685 max = key->last_subkey;
686 while (min <= max)
688 i = (min + max) / 2;
689 len = min( key->subkeys[i]->namelen, name->len );
690 res = memicmp_strW( key->subkeys[i]->name, name->str, len );
691 if (!res) res = key->subkeys[i]->namelen - name->len;
692 if (!res)
694 *index = i;
695 return key->subkeys[i];
697 if (res > 0) max = i - 1;
698 else min = i + 1;
700 *index = min; /* this is where we should insert it */
701 return NULL;
704 /* return the wow64 variant of the key, or the key itself if none */
705 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
707 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
708 int index;
710 if (!(key->flags & KEY_WOW64)) return key;
711 if (!is_wow6432node( name->str, name->len ))
713 key = find_subkey( key, &wow6432node_str, &index );
714 assert( key ); /* if KEY_WOW64 is set we must find it */
716 return key;
720 /* follow a symlink and return the resolved key */
721 static struct key *follow_symlink( struct key *key, int iteration )
723 struct unicode_str path, token;
724 struct key_value *value;
725 int index;
727 if (iteration > 16) return NULL;
728 if (!(key->flags & KEY_SYMLINK)) return key;
729 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
731 path.str = value->data;
732 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
733 if (path.len <= sizeof(root_name)) return NULL;
734 if (memicmp_strW( path.str, root_name, sizeof(root_name) )) return NULL;
735 path.str += ARRAY_SIZE( root_name );
736 path.len -= sizeof(root_name);
738 key = root_key;
739 token.str = NULL;
740 if (!get_path_token( &path, &token )) return NULL;
741 while (token.len)
743 if (!(key = find_subkey( key, &token, &index ))) break;
744 if (!(key = follow_symlink( key, iteration + 1 ))) break;
745 get_path_token( &path, &token );
747 return key;
750 /* open a key until we find an element that doesn't exist */
751 /* helper for open_key and create_key */
752 static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
753 unsigned int access, struct unicode_str *token, int *index )
755 token->str = NULL;
756 if (!get_path_token( name, token )) return NULL;
757 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
758 while (token->len)
760 struct key *subkey;
761 if (!(subkey = find_subkey( key, token, index )))
763 if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
765 /* try in the 64-bit parent */
766 key = key->parent;
767 subkey = find_subkey( key, token, index );
770 if (!subkey) break;
771 key = subkey;
772 get_path_token( name, token );
773 if (!token->len) break;
774 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
775 if (!(key = follow_symlink( key, 0 )))
777 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
778 return NULL;
781 return key;
784 /* open a subkey */
785 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
786 unsigned int attributes )
788 int index;
789 struct unicode_str token;
791 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
793 if (token.len)
795 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
796 return NULL;
798 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
799 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
801 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
802 return NULL;
804 if (debug_level > 1) dump_operation( key, NULL, "Open" );
805 if (key->flags & KEY_PREDEF) set_error( STATUS_PREDEFINED_HANDLE );
806 grab_object( key );
807 return key;
810 /* create a subkey */
811 static struct key *create_key( struct key *key, const struct unicode_str *name,
812 const struct unicode_str *class, unsigned int options,
813 unsigned int access, unsigned int attributes,
814 const struct security_descriptor *sd, int *created )
816 int index;
817 struct unicode_str token, next;
819 *created = 0;
820 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
822 if (!token.len) /* the key already exists */
824 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
825 if (options & REG_OPTION_CREATE_LINK)
827 set_error( STATUS_OBJECT_NAME_COLLISION );
828 return NULL;
830 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
832 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
833 return NULL;
835 if (debug_level > 1) dump_operation( key, NULL, "Open" );
836 if (key->flags & KEY_PREDEF) set_error( STATUS_PREDEFINED_HANDLE );
837 grab_object( key );
838 return key;
841 /* token must be the last path component at this point */
842 next = token;
843 get_path_token( name, &next );
844 if (next.len)
846 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
847 return NULL;
850 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
852 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
853 return NULL;
855 *created = 1;
856 make_dirty( key );
857 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
859 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
860 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
861 else key->flags |= KEY_DIRTY;
863 if (sd) default_set_sd( &key->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
864 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
866 if (debug_level > 1) dump_operation( key, NULL, "Create" );
867 if (class && class->len)
869 key->classlen = class->len;
870 free(key->class);
871 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
873 touch_key( key->parent, REG_NOTIFY_CHANGE_NAME );
874 grab_object( key );
875 return key;
878 /* recursively create a subkey (for internal use only) */
879 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
881 struct key *base;
882 int index;
883 struct unicode_str token;
885 token.str = NULL;
886 if (!get_path_token( name, &token )) return NULL;
887 while (token.len)
889 struct key *subkey;
890 if (!(subkey = find_subkey( key, &token, &index ))) break;
891 key = subkey;
892 if (!(key = follow_symlink( key, 0 )))
894 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
895 return NULL;
897 get_path_token( name, &token );
900 if (token.len)
902 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
903 base = key;
904 for (;;)
906 get_path_token( name, &token );
907 if (!token.len) break;
908 /* we know the index is always 0 in a new key */
909 if (!(key = alloc_subkey( key, &token, 0, modif )))
911 free_subkey( base, index );
912 return NULL;
917 grab_object( key );
918 return key;
921 /* query information about a key or a subkey */
922 static void enum_key( struct key *key, int index, int info_class, struct enum_key_reply *reply )
924 int i;
925 data_size_t len, namelen, classlen;
926 data_size_t max_subkey = 0, max_class = 0;
927 data_size_t max_value = 0, max_data = 0;
928 WCHAR *fullname = NULL;
929 char *data;
931 if (key->flags & KEY_PREDEF)
933 set_error( STATUS_INVALID_HANDLE );
934 return;
937 if (index != -1) /* -1 means use the specified key directly */
939 if ((index < 0) || (index > key->last_subkey))
941 set_error( STATUS_NO_MORE_ENTRIES );
942 return;
944 key = key->subkeys[index];
947 namelen = key->namelen;
948 classlen = key->classlen;
950 switch(info_class)
952 case KeyNameInformation:
953 if (!(fullname = key->obj.ops->get_full_name( &key->obj, &namelen ))) return;
954 /* fall through */
955 case KeyBasicInformation:
956 classlen = 0; /* only return the name */
957 /* fall through */
958 case KeyNodeInformation:
959 reply->max_subkey = 0;
960 reply->max_class = 0;
961 reply->max_value = 0;
962 reply->max_data = 0;
963 break;
964 case KeyFullInformation:
965 case KeyCachedInformation:
966 for (i = 0; i <= key->last_subkey; i++)
968 if (key->subkeys[i]->namelen > max_subkey) max_subkey = key->subkeys[i]->namelen;
969 if (key->subkeys[i]->classlen > max_class) max_class = key->subkeys[i]->classlen;
971 for (i = 0; i <= key->last_value; i++)
973 if (key->values[i].namelen > max_value) max_value = key->values[i].namelen;
974 if (key->values[i].len > max_data) max_data = key->values[i].len;
976 reply->max_subkey = max_subkey;
977 reply->max_class = max_class;
978 reply->max_value = max_value;
979 reply->max_data = max_data;
980 reply->namelen = namelen;
981 if (info_class == KeyCachedInformation)
982 classlen = 0; /* don't return any data, only its size */
983 namelen = 0; /* don't return name */
984 break;
985 default:
986 set_error( STATUS_INVALID_PARAMETER );
987 return;
989 reply->subkeys = key->last_subkey + 1;
990 reply->values = key->last_value + 1;
991 reply->modif = key->modif;
992 reply->total = namelen + classlen;
994 len = min( reply->total, get_reply_max_size() );
995 if (len && (data = set_reply_data_size( len )))
997 if (len > namelen)
999 reply->namelen = namelen;
1000 memcpy( data, key->name, namelen );
1001 memcpy( data + namelen, key->class, len - namelen );
1003 else if (info_class == KeyNameInformation)
1005 reply->namelen = namelen;
1006 memcpy( data, fullname, len );
1008 else
1010 reply->namelen = len;
1011 memcpy( data, key->name, len );
1014 free( fullname );
1015 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
1018 /* delete a key and its values */
1019 static int delete_key( struct key *key, int recurse )
1021 int index;
1022 struct key *parent = key->parent;
1024 /* must find parent and index */
1025 if (key == root_key)
1027 set_error( STATUS_ACCESS_DENIED );
1028 return -1;
1030 assert( parent );
1032 if (key->flags & KEY_PREDEF)
1034 set_error( STATUS_INVALID_HANDLE );
1035 return -1;
1038 while (recurse && (key->last_subkey>=0))
1039 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
1040 return -1;
1042 for (index = 0; index <= parent->last_subkey; index++)
1043 if (parent->subkeys[index] == key) break;
1044 assert( index <= parent->last_subkey );
1046 /* we can only delete a key that has no subkeys */
1047 if (key->last_subkey >= 0)
1049 set_error( STATUS_ACCESS_DENIED );
1050 return -1;
1053 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
1054 free_subkey( parent, index );
1055 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
1056 return 0;
1059 /* try to grow the array of values; return 1 if OK, 0 on error */
1060 static int grow_values( struct key *key )
1062 struct key_value *new_val;
1063 int nb_values;
1065 if (key->nb_values)
1067 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
1068 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
1070 set_error( STATUS_NO_MEMORY );
1071 return 0;
1074 else
1076 nb_values = MIN_VALUES;
1077 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
1079 key->values = new_val;
1080 key->nb_values = nb_values;
1081 return 1;
1084 /* find the named value of a given key and return its index in the array */
1085 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
1087 int i, min, max, res;
1088 data_size_t len;
1090 min = 0;
1091 max = key->last_value;
1092 while (min <= max)
1094 i = (min + max) / 2;
1095 len = min( key->values[i].namelen, name->len );
1096 res = memicmp_strW( key->values[i].name, name->str, len );
1097 if (!res) res = key->values[i].namelen - name->len;
1098 if (!res)
1100 *index = i;
1101 return &key->values[i];
1103 if (res > 0) max = i - 1;
1104 else min = i + 1;
1106 *index = min; /* this is where we should insert it */
1107 return NULL;
1110 /* insert a new value; the index must have been returned by find_value */
1111 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
1113 struct key_value *value;
1114 WCHAR *new_name = NULL;
1115 int i;
1117 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
1119 set_error( STATUS_NAME_TOO_LONG );
1120 return NULL;
1122 if (key->last_value + 1 == key->nb_values)
1124 if (!grow_values( key )) return NULL;
1126 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1127 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1128 value = &key->values[index];
1129 value->name = new_name;
1130 value->namelen = name->len;
1131 value->len = 0;
1132 value->data = NULL;
1133 return value;
1136 /* set a key value */
1137 static void set_value( struct key *key, const struct unicode_str *name,
1138 int type, const void *data, data_size_t len )
1140 struct key_value *value;
1141 void *ptr = NULL;
1142 int index;
1144 if (key->flags & KEY_PREDEF)
1146 set_error( STATUS_INVALID_HANDLE );
1147 return;
1150 if ((value = find_value( key, name, &index )))
1152 /* check if the new value is identical to the existing one */
1153 if (value->type == type && value->len == len &&
1154 value->data && !memcmp( value->data, data, len ))
1156 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1157 return;
1161 if (key->flags & KEY_SYMLINK)
1163 if (type != REG_LINK || name->len != symlink_str.len ||
1164 memicmp_strW( name->str, symlink_str.str, name->len ))
1166 set_error( STATUS_ACCESS_DENIED );
1167 return;
1171 if (len && !(ptr = memdup( data, len ))) return;
1173 if (!value)
1175 if (!(value = insert_value( key, name, index )))
1177 free( ptr );
1178 return;
1181 else free( value->data ); /* already existing, free previous data */
1183 value->type = type;
1184 value->len = len;
1185 value->data = ptr;
1186 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1187 if (debug_level > 1) dump_operation( key, value, "Set" );
1190 /* get a key value */
1191 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1193 struct key_value *value;
1194 int index;
1196 if (key->flags & KEY_PREDEF)
1198 set_error( STATUS_INVALID_HANDLE );
1199 return;
1202 if ((value = find_value( key, name, &index )))
1204 *type = value->type;
1205 *len = value->len;
1206 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1207 if (debug_level > 1) dump_operation( key, value, "Get" );
1209 else
1211 *type = -1;
1212 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1216 /* enumerate a key value */
1217 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1219 struct key_value *value;
1221 if (key->flags & KEY_PREDEF)
1223 set_error( STATUS_INVALID_HANDLE );
1224 return;
1227 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1228 else
1230 void *data;
1231 data_size_t namelen, maxlen;
1233 value = &key->values[i];
1234 reply->type = value->type;
1235 namelen = value->namelen;
1237 switch(info_class)
1239 case KeyValueBasicInformation:
1240 reply->total = namelen;
1241 break;
1242 case KeyValueFullInformation:
1243 reply->total = namelen + value->len;
1244 break;
1245 case KeyValuePartialInformation:
1246 reply->total = value->len;
1247 namelen = 0;
1248 break;
1249 default:
1250 set_error( STATUS_INVALID_PARAMETER );
1251 return;
1254 maxlen = min( reply->total, get_reply_max_size() );
1255 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1257 if (maxlen > namelen)
1259 reply->namelen = namelen;
1260 memcpy( data, value->name, namelen );
1261 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1263 else
1265 reply->namelen = maxlen;
1266 memcpy( data, value->name, maxlen );
1269 if (debug_level > 1) dump_operation( key, value, "Enum" );
1273 /* delete a value */
1274 static void delete_value( struct key *key, const struct unicode_str *name )
1276 struct key_value *value;
1277 int i, index, nb_values;
1279 if (key->flags & KEY_PREDEF)
1281 set_error( STATUS_INVALID_HANDLE );
1282 return;
1285 if (!(value = find_value( key, name, &index )))
1287 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1288 return;
1290 if (debug_level > 1) dump_operation( key, value, "Delete" );
1291 free( value->name );
1292 free( value->data );
1293 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1294 key->last_value--;
1295 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1297 /* try to shrink the array */
1298 nb_values = key->nb_values;
1299 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1301 struct key_value *new_val;
1302 nb_values -= nb_values / 3; /* shrink by 33% */
1303 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1304 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1305 key->values = new_val;
1306 key->nb_values = nb_values;
1310 /* get the registry key corresponding to an hkey handle */
1311 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1313 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1315 if (key && key->flags & KEY_DELETED)
1317 set_error( STATUS_KEY_DELETED );
1318 release_object( key );
1319 key = NULL;
1321 return key;
1324 /* get the registry key corresponding to a parent key handle */
1325 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1327 if (!hkey) return (struct key *)grab_object( root_key );
1328 return get_hkey_obj( hkey, 0 );
1331 /* read a line from the input file */
1332 static int read_next_line( struct file_load_info *info )
1334 char *newbuf;
1335 int newlen, pos = 0;
1337 info->line++;
1338 for (;;)
1340 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1341 return (pos != 0); /* EOF */
1342 pos = strlen(info->buffer);
1343 if (info->buffer[pos-1] == '\n')
1345 /* got a full line */
1346 info->buffer[--pos] = 0;
1347 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1348 return 1;
1350 if (pos < info->len - 1) return 1; /* EOF but something was read */
1352 /* need to enlarge the buffer */
1353 newlen = info->len + info->len / 2;
1354 if (!(newbuf = realloc( info->buffer, newlen )))
1356 set_error( STATUS_NO_MEMORY );
1357 return -1;
1359 info->buffer = newbuf;
1360 info->len = newlen;
1364 /* make sure the temp buffer holds enough space */
1365 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1367 WCHAR *tmp;
1368 if (info->tmplen >= size) return 1;
1369 if (!(tmp = realloc( info->tmp, size )))
1371 set_error( STATUS_NO_MEMORY );
1372 return 0;
1374 info->tmp = tmp;
1375 info->tmplen = size;
1376 return 1;
1379 /* report an error while loading an input file */
1380 static void file_read_error( const char *err, struct file_load_info *info )
1382 if (info->filename)
1383 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1384 else
1385 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1388 /* convert a data type tag to a value type */
1389 static int get_data_type( const char *buffer, int *type, int *parse_type )
1391 struct data_type { const char *tag; int len; int type; int parse_type; };
1393 static const struct data_type data_types[] =
1394 { /* actual type */ /* type to assume for parsing */
1395 { "\"", 1, REG_SZ, REG_SZ },
1396 { "str:\"", 5, REG_SZ, REG_SZ },
1397 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1398 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1399 { "hex:", 4, REG_BINARY, REG_BINARY },
1400 { "dword:", 6, REG_DWORD, REG_DWORD },
1401 { "hex(", 4, -1, REG_BINARY },
1402 { NULL, 0, 0, 0 }
1405 const struct data_type *ptr;
1406 char *end;
1408 for (ptr = data_types; ptr->tag; ptr++)
1410 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1411 *parse_type = ptr->parse_type;
1412 if ((*type = ptr->type) != -1) return ptr->len;
1413 /* "hex(xx):" is special */
1414 *type = (int)strtoul( buffer + 4, &end, 16 );
1415 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1416 return end + 2 - buffer;
1418 return 0;
1421 /* load and create a key from the input file */
1422 static struct key *load_key( struct key *base, const char *buffer, int prefix_len,
1423 struct file_load_info *info, timeout_t *modif )
1425 WCHAR *p;
1426 struct unicode_str name;
1427 int res;
1428 unsigned int mod;
1429 data_size_t len;
1431 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1433 len = info->tmplen;
1434 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1436 file_read_error( "Malformed key", info );
1437 return NULL;
1439 if (sscanf( buffer + res, " %u", &mod ) == 1)
1440 *modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1441 else
1442 *modif = current_time;
1444 p = info->tmp;
1445 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1447 if (!*p)
1449 if (prefix_len > 1)
1451 file_read_error( "Malformed key", info );
1452 return NULL;
1454 /* empty key name, return base key */
1455 return (struct key *)grab_object( base );
1457 name.str = p;
1458 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1459 return create_key_recursive( base, &name, 0 );
1462 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1463 static void update_key_time( struct key *key, timeout_t modif )
1465 while (key && !key->modif)
1467 key->modif = modif;
1468 key = key->parent;
1472 /* load a global option from the input file */
1473 static int load_global_option( const char *buffer, struct file_load_info *info )
1475 const char *p;
1477 if (!strncmp( buffer, "#arch=", 6 ))
1479 enum prefix_type type;
1480 p = buffer + 6;
1481 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1482 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1483 else
1485 file_read_error( "Unknown architecture", info );
1486 set_error( STATUS_NOT_REGISTRY_FILE );
1487 return 0;
1489 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1490 else if (type != prefix_type)
1492 file_read_error( "Mismatched architecture", info );
1493 set_error( STATUS_NOT_REGISTRY_FILE );
1494 return 0;
1497 /* ignore unknown options */
1498 return 1;
1501 /* load a key option from the input file */
1502 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1504 const char *p;
1505 data_size_t len;
1507 if (!strncmp( buffer, "#time=", 6 ))
1509 timeout_t modif = 0;
1510 for (p = buffer + 6; *p; p++)
1512 if (*p >= '0' && *p <= '9') modif = (modif << 4) | (*p - '0');
1513 else if (*p >= 'A' && *p <= 'F') modif = (modif << 4) | (*p - 'A' + 10);
1514 else if (*p >= 'a' && *p <= 'f') modif = (modif << 4) | (*p - 'a' + 10);
1515 else break;
1517 update_key_time( key, modif );
1519 if (!strncmp( buffer, "#class=", 7 ))
1521 p = buffer + 7;
1522 if (*p++ != '"') return 0;
1523 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1524 len = info->tmplen;
1525 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1526 free( key->class );
1527 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1528 key->classlen = len;
1530 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1531 /* ignore unknown options */
1532 return 1;
1535 /* parse a comma-separated list of hex digits */
1536 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1538 const char *p = buffer;
1539 data_size_t count = 0;
1540 char *end;
1542 while (isxdigit(*p))
1544 unsigned int val = strtoul( p, &end, 16 );
1545 if (end == p || val > 0xff) return -1;
1546 if (count++ >= *len) return -1; /* dest buffer overflow */
1547 *dest++ = val;
1548 p = end;
1549 while (isspace(*p)) p++;
1550 if (*p == ',') p++;
1551 while (isspace(*p)) p++;
1553 *len = count;
1554 return p - buffer;
1557 /* parse a value name and create the corresponding value */
1558 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1559 struct file_load_info *info )
1561 struct key_value *value;
1562 struct unicode_str name;
1563 int index;
1565 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1566 name.str = info->tmp;
1567 name.len = info->tmplen;
1568 if (buffer[0] == '@')
1570 name.len = 0;
1571 *len = 1;
1573 else
1575 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1576 if (r == -1) goto error;
1577 *len = r + 1; /* for initial quote */
1578 name.len -= sizeof(WCHAR); /* terminating null */
1580 while (isspace(buffer[*len])) (*len)++;
1581 if (buffer[*len] != '=') goto error;
1582 (*len)++;
1583 while (isspace(buffer[*len])) (*len)++;
1584 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1585 return value;
1587 error:
1588 file_read_error( "Malformed value name", info );
1589 return NULL;
1592 /* load a value from the input file */
1593 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1595 DWORD dw;
1596 void *ptr, *newptr;
1597 int res, type, parse_type;
1598 data_size_t maxlen, len;
1599 struct key_value *value;
1601 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1602 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1603 buffer += len + res;
1605 switch(parse_type)
1607 case REG_SZ:
1608 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1609 len = info->tmplen;
1610 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1611 ptr = info->tmp;
1612 break;
1613 case REG_DWORD:
1614 dw = strtoul( buffer, NULL, 16 );
1615 ptr = &dw;
1616 len = sizeof(dw);
1617 break;
1618 case REG_BINARY: /* hex digits */
1619 len = 0;
1620 for (;;)
1622 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1623 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1624 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1625 len += maxlen;
1626 buffer += res;
1627 while (isspace(*buffer)) buffer++;
1628 if (!*buffer) break;
1629 if (*buffer != '\\') goto error;
1630 if (read_next_line( info) != 1) goto error;
1631 buffer = info->buffer;
1632 while (isspace(*buffer)) buffer++;
1634 ptr = info->tmp;
1635 break;
1636 default:
1637 assert(0);
1638 ptr = NULL; /* keep compiler quiet */
1639 break;
1642 if (!len) newptr = NULL;
1643 else if (!(newptr = memdup( ptr, len ))) return 0;
1645 free( value->data );
1646 value->data = newptr;
1647 value->len = len;
1648 value->type = type;
1649 return 1;
1651 error:
1652 file_read_error( "Malformed value", info );
1653 free( value->data );
1654 value->data = NULL;
1655 value->len = 0;
1656 value->type = REG_NONE;
1657 return 0;
1660 /* return the length (in path elements) of name that is part of the key name */
1661 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1662 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1664 WCHAR *p;
1665 int res;
1666 data_size_t len;
1668 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1670 len = info->tmplen;
1671 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1673 file_read_error( "Malformed key", info );
1674 return 0;
1676 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1677 len = (p - info->tmp) * sizeof(WCHAR);
1678 for (res = 1; key != root_key; res++)
1680 if (len == key->namelen && !memicmp_strW( info->tmp, key->name, len )) break;
1681 key = key->parent;
1683 if (key == root_key) res = 0; /* no matching name */
1684 return res;
1687 /* load all the keys from the input file */
1688 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1689 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1691 struct key *subkey = NULL;
1692 struct file_load_info info;
1693 timeout_t modif = current_time;
1694 char *p;
1696 info.filename = filename;
1697 info.file = f;
1698 info.len = 4;
1699 info.tmplen = 4;
1700 info.line = 0;
1701 if (!(info.buffer = mem_alloc( info.len ))) return;
1702 if (!(info.tmp = mem_alloc( info.tmplen )))
1704 free( info.buffer );
1705 return;
1708 if ((read_next_line( &info ) != 1) ||
1709 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1711 set_error( STATUS_NOT_REGISTRY_FILE );
1712 goto done;
1715 while (read_next_line( &info ) == 1)
1717 p = info.buffer;
1718 while (*p && isspace(*p)) p++;
1719 switch(*p)
1721 case '[': /* new key */
1722 if (subkey)
1724 update_key_time( subkey, modif );
1725 release_object( subkey );
1727 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1728 if (!(subkey = load_key( key, p + 1, prefix_len, &info, &modif )))
1729 file_read_error( "Error creating key", &info );
1730 break;
1731 case '@': /* default value */
1732 case '\"': /* value */
1733 if (subkey) load_value( subkey, p, &info );
1734 else file_read_error( "Value without key", &info );
1735 break;
1736 case '#': /* option */
1737 if (subkey) load_key_option( subkey, p, &info );
1738 else if (!load_global_option( p, &info )) goto done;
1739 break;
1740 case ';': /* comment */
1741 case 0: /* empty line */
1742 break;
1743 default:
1744 file_read_error( "Unrecognized input", &info );
1745 break;
1749 done:
1750 if (subkey)
1752 update_key_time( subkey, modif );
1753 release_object( subkey );
1755 free( info.buffer );
1756 free( info.tmp );
1759 /* load a part of the registry from a file */
1760 static void load_registry( struct key *key, obj_handle_t handle )
1762 struct file *file;
1763 int fd;
1765 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1766 fd = dup( get_file_unix_fd( file ) );
1767 release_object( file );
1768 if (fd != -1)
1770 FILE *f = fdopen( fd, "r" );
1771 if (f)
1773 load_keys( key, NULL, f, -1 );
1774 fclose( f );
1776 else file_set_error();
1780 /* load one of the initial registry files */
1781 static int load_init_registry_from_file( const char *filename, struct key *key )
1783 FILE *f;
1785 if ((f = fopen( filename, "r" )))
1787 load_keys( key, filename, f, 0 );
1788 fclose( f );
1789 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1791 fprintf( stderr, "%s is not a valid registry file\n", filename );
1792 return 1;
1796 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1798 save_branch_info[save_branch_count].path = filename;
1799 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1800 make_object_permanent( &key->obj );
1801 return (f != NULL);
1804 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1806 char buffer[7 + 11 + 11 + 11 * SID_MAX_SUB_AUTHORITIES], *p = buffer;
1807 unsigned int i;
1809 p += sprintf( p, "User\\S-%u-%u", sid->Revision,
1810 MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1811 sid->IdentifierAuthority.Value[4] ),
1812 MAKEWORD( sid->IdentifierAuthority.Value[3],
1813 sid->IdentifierAuthority.Value[2] )));
1814 for (i = 0; i < sid->SubAuthorityCount; i++) p += sprintf( p, "-%u", sid->SubAuthority[i] );
1815 return ascii_to_unicode_str( buffer, path );
1818 static void init_supported_machines(void)
1820 unsigned int count = 0;
1821 #ifdef __i386__
1822 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1823 #elif defined(__x86_64__)
1824 if (prefix_type == PREFIX_64BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_AMD64;
1825 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1826 #elif defined(__arm__)
1827 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1828 #elif defined(__aarch64__)
1829 if (prefix_type == PREFIX_64BIT)
1831 supported_machines[count++] = IMAGE_FILE_MACHINE_ARM64;
1832 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1834 supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1835 #else
1836 #error Unsupported machine
1837 #endif
1838 supported_machines_count = count;
1839 native_machine = supported_machines[0];
1842 /* registry initialisation */
1843 void init_registry(void)
1845 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1846 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1847 static const WCHAR classes_i386[] = {'S','o','f','t','w','a','r','e','\\',
1848 'C','l','a','s','s','e','s','\\',
1849 'W','o','w','6','4','3','2','N','o','d','e'};
1850 static const WCHAR classes_amd64[] = {'S','o','f','t','w','a','r','e','\\',
1851 'C','l','a','s','s','e','s','\\',
1852 'W','o','w','6','4','6','4','N','o','d','e'};
1853 static const WCHAR classes_arm[] = {'S','o','f','t','w','a','r','e','\\',
1854 'C','l','a','s','s','e','s','\\',
1855 'W','o','w','A','A','3','2','N','o','d','e'};
1856 static const WCHAR classes_arm64[] = {'S','o','f','t','w','a','r','e','\\',
1857 'C','l','a','s','s','e','s','\\',
1858 'W','o','w','A','A','6','4','N','o','d','e'};
1859 static const WCHAR perflib[] = {'S','o','f','t','w','a','r','e','\\',
1860 'M','i','c','r','o','s','o','f','t','\\',
1861 'W','i','n','d','o','w','s',' ','N','T','\\',
1862 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1863 'P','e','r','f','l','i','b','\\',
1864 '0','0','9'};
1865 static const struct unicode_str root_name = { NULL, 0 };
1866 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1867 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1868 static const struct unicode_str perflib_name = { perflib, sizeof(perflib) };
1870 WCHAR *current_user_path;
1871 struct unicode_str current_user_str;
1872 struct key *key, *hklm, *hkcu;
1873 unsigned int i;
1874 char *p;
1876 /* switch to the config dir */
1878 if (fchdir( config_dir_fd ) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno ));
1880 /* create the root key */
1881 root_key = alloc_key( &root_name, current_time );
1882 assert( root_key );
1883 make_object_permanent( &root_key->obj );
1885 /* load system.reg into Registry\Machine */
1887 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1888 fatal_error( "could not create Machine registry key\n" );
1890 if (!load_init_registry_from_file( "system.reg", hklm ))
1892 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1893 prefix_type = PREFIX_32BIT;
1894 else
1895 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1897 else if (prefix_type == PREFIX_UNKNOWN)
1898 prefix_type = PREFIX_32BIT;
1900 init_supported_machines();
1902 /* load userdef.reg into Registry\User\.Default */
1904 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1905 fatal_error( "could not create User\\.Default registry key\n" );
1907 load_init_registry_from_file( "userdef.reg", key );
1908 release_object( key );
1910 /* load user.reg into HKEY_CURRENT_USER */
1912 /* FIXME: match default user in token.c. should get from process token instead */
1913 current_user_path = format_user_registry_path( security_local_user_sid, &current_user_str );
1914 if (!current_user_path ||
1915 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1916 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1917 free( current_user_path );
1918 load_init_registry_from_file( "user.reg", hkcu );
1920 /* set the shared flag on Software\Classes\Wow6432Node for all platforms */
1921 for (i = 1; i < supported_machines_count; i++)
1923 struct unicode_str name;
1925 switch (supported_machines[i])
1927 case IMAGE_FILE_MACHINE_I386: name.str = classes_i386; name.len = sizeof(classes_i386); break;
1928 case IMAGE_FILE_MACHINE_ARMNT: name.str = classes_arm; name.len = sizeof(classes_arm); break;
1929 case IMAGE_FILE_MACHINE_AMD64: name.str = classes_amd64; name.len = sizeof(classes_amd64); break;
1930 case IMAGE_FILE_MACHINE_ARM64: name.str = classes_arm64; name.len = sizeof(classes_arm64); break;
1932 if ((key = create_key_recursive( hklm, &name, current_time )))
1934 key->flags |= KEY_WOWSHARE;
1935 release_object( key );
1937 /* FIXME: handle HKCU too */
1940 if ((key = create_key_recursive( hklm, &perflib_name, current_time )))
1942 key->flags |= KEY_PREDEF;
1943 release_object( key );
1946 release_object( hklm );
1947 release_object( hkcu );
1949 /* start the periodic save timer */
1950 set_periodic_save_timer();
1952 /* create windows directories */
1954 if (!mkdir( "drive_c/windows", 0777 ))
1956 mkdir( "drive_c/windows/system32", 0777 );
1957 for (i = 1; i < supported_machines_count; i++)
1959 switch (supported_machines[i])
1961 case IMAGE_FILE_MACHINE_I386: mkdir( "drive_c/windows/syswow64", 0777 ); break;
1962 case IMAGE_FILE_MACHINE_ARMNT: mkdir( "drive_c/windows/sysarm32", 0777 ); break;
1963 case IMAGE_FILE_MACHINE_AMD64: mkdir( "drive_c/windows/sysx8664", 0777 ); break;
1964 case IMAGE_FILE_MACHINE_ARM64: mkdir( "drive_c/windows/sysarm64", 0777 ); break;
1969 /* go back to the server dir */
1970 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
1973 /* save a registry branch to a file */
1974 static void save_all_subkeys( struct key *key, FILE *f )
1976 fprintf( f, "WINE REGISTRY Version 2\n" );
1977 fprintf( f, ";; All keys relative to " );
1978 dump_path( key, NULL, f );
1979 fprintf( f, "\n" );
1980 switch (prefix_type)
1982 case PREFIX_32BIT:
1983 fprintf( f, "\n#arch=win32\n" );
1984 break;
1985 case PREFIX_64BIT:
1986 fprintf( f, "\n#arch=win64\n" );
1987 break;
1988 default:
1989 break;
1991 save_subkeys( key, key, f );
1994 /* save a registry branch to a file handle */
1995 static void save_registry( struct key *key, obj_handle_t handle )
1997 struct file *file;
1998 int fd;
2000 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
2001 fd = dup( get_file_unix_fd( file ) );
2002 release_object( file );
2003 if (fd != -1)
2005 FILE *f = fdopen( fd, "w" );
2006 if (f)
2008 save_all_subkeys( key, f );
2009 if (fclose( f )) file_set_error();
2011 else
2013 file_set_error();
2014 close( fd );
2019 /* save a registry branch to a file */
2020 static int save_branch( struct key *key, const char *path )
2022 struct stat st;
2023 char *p, *tmp = NULL;
2024 int fd, count = 0, ret = 0;
2025 FILE *f;
2027 if (!(key->flags & KEY_DIRTY))
2029 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
2030 return 1;
2033 /* test the file type */
2035 if ((fd = open( path, O_WRONLY )) != -1)
2037 /* if file is not a regular file or has multiple links or is accessed
2038 * via symbolic links, write directly into it; otherwise use a temp file */
2039 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
2041 ftruncate( fd, 0 );
2042 goto save;
2044 close( fd );
2047 /* create a temp file in the same directory */
2049 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
2050 strcpy( tmp, path );
2051 if ((p = strrchr( tmp, '/' ))) p++;
2052 else p = tmp;
2053 for (;;)
2055 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
2056 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
2057 if (errno != EEXIST) goto done;
2058 close( fd );
2061 /* now save to it */
2063 save:
2064 if (!(f = fdopen( fd, "w" )))
2066 if (tmp) unlink( tmp );
2067 close( fd );
2068 goto done;
2071 if (debug_level > 1)
2073 fprintf( stderr, "%s: ", path );
2074 dump_operation( key, NULL, "saving" );
2077 save_all_subkeys( key, f );
2078 ret = !fclose(f);
2080 if (tmp)
2082 /* if successfully written, rename to final name */
2083 if (ret) ret = !rename( tmp, path );
2084 if (!ret) unlink( tmp );
2087 done:
2088 free( tmp );
2089 if (ret) make_clean( key );
2090 return ret;
2093 /* periodic saving of the registry */
2094 static void periodic_save( void *arg )
2096 int i;
2098 if (fchdir( config_dir_fd ) == -1) return;
2099 save_timeout_user = NULL;
2100 for (i = 0; i < save_branch_count; i++)
2101 save_branch( save_branch_info[i].key, save_branch_info[i].path );
2102 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2103 set_periodic_save_timer();
2106 /* start the periodic save timer */
2107 static void set_periodic_save_timer(void)
2109 if (save_timeout_user) remove_timeout_user( save_timeout_user );
2110 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
2113 /* save the modified registry branches to disk */
2114 void flush_registry(void)
2116 int i;
2118 if (fchdir( config_dir_fd ) == -1) return;
2119 for (i = 0; i < save_branch_count; i++)
2121 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
2123 fprintf( stderr, "wineserver: could not save registry branch to %s",
2124 save_branch_info[i].path );
2125 perror( " " );
2128 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2131 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2132 static int is_wow64_thread( struct thread *thread )
2134 return (is_machine_64bit( native_machine ) && !is_machine_64bit( thread->process->machine ));
2138 /* create a registry key */
2139 DECL_HANDLER(create_key)
2141 struct key *key = NULL, *parent;
2142 struct unicode_str name, class;
2143 unsigned int access = req->access;
2144 const struct security_descriptor *sd;
2145 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2147 if (!objattr) return;
2149 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2151 class.str = get_req_data_after_objattr( objattr, &class.len );
2152 class.len = (class.len / sizeof(WCHAR)) * sizeof(WCHAR);
2154 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2155 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2157 name.str += ARRAY_SIZE( root_name );
2158 name.len -= sizeof(root_name);
2161 /* NOTE: no access rights are required from the parent handle to create a key */
2162 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2164 if ((key = create_key( parent, &name, &class, req->options, access,
2165 objattr->attributes, sd, &reply->created )))
2167 reply->hkey = alloc_handle( current->process, key, access, objattr->attributes );
2168 release_object( key );
2170 release_object( parent );
2174 /* open a registry key */
2175 DECL_HANDLER(open_key)
2177 struct key *key, *parent;
2178 struct unicode_str name;
2179 unsigned int access = req->access;
2181 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2183 reply->hkey = 0;
2184 /* NOTE: no access rights are required to open the parent key, only the child key */
2185 if ((parent = get_parent_hkey_obj( req->parent )))
2187 get_req_path( &name, !req->parent );
2188 if ((key = open_key( parent, &name, access, req->attributes )))
2190 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
2191 release_object( key );
2193 release_object( parent );
2197 /* delete a registry key */
2198 DECL_HANDLER(delete_key)
2200 struct key *key;
2202 if ((key = get_hkey_obj( req->hkey, DELETE )))
2204 delete_key( key, 0);
2205 release_object( key );
2209 /* flush a registry key */
2210 DECL_HANDLER(flush_key)
2212 struct key *key = get_hkey_obj( req->hkey, 0 );
2213 if (key)
2215 /* we don't need to do anything here with the current implementation */
2216 release_object( key );
2220 /* enumerate registry subkeys */
2221 DECL_HANDLER(enum_key)
2223 struct key *key;
2225 if ((key = get_hkey_obj( req->hkey,
2226 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
2228 enum_key( key, req->index, req->info_class, reply );
2229 release_object( key );
2233 /* set a value of a registry key */
2234 DECL_HANDLER(set_key_value)
2236 struct key *key;
2237 struct unicode_str name;
2239 if (req->namelen > get_req_data_size())
2241 set_error( STATUS_INVALID_PARAMETER );
2242 return;
2244 name.str = get_req_data();
2245 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2247 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2249 data_size_t datalen = get_req_data_size() - req->namelen;
2250 const char *data = (const char *)get_req_data() + req->namelen;
2252 set_value( key, &name, req->type, data, datalen );
2253 release_object( key );
2257 /* retrieve the value of a registry key */
2258 DECL_HANDLER(get_key_value)
2260 struct key *key;
2261 struct unicode_str name = get_req_unicode_str();
2263 reply->total = 0;
2264 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2266 get_value( key, &name, &reply->type, &reply->total );
2267 release_object( key );
2271 /* enumerate the value of a registry key */
2272 DECL_HANDLER(enum_key_value)
2274 struct key *key;
2276 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2278 enum_value( key, req->index, req->info_class, reply );
2279 release_object( key );
2283 /* delete a value of a registry key */
2284 DECL_HANDLER(delete_key_value)
2286 struct key *key;
2287 struct unicode_str name = get_req_unicode_str();
2289 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2291 delete_value( key, &name );
2292 release_object( key );
2296 /* load a registry branch from a file */
2297 DECL_HANDLER(load_registry)
2299 struct key *key, *parent;
2300 struct unicode_str name;
2301 const struct security_descriptor *sd;
2302 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2304 if (!objattr) return;
2306 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2308 set_error( STATUS_PRIVILEGE_NOT_HELD );
2309 return;
2312 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2313 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2315 name.str += ARRAY_SIZE( root_name );
2316 name.len -= sizeof(root_name);
2319 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2321 int dummy;
2322 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, sd, &dummy )))
2324 load_registry( key, req->file );
2325 release_object( key );
2327 release_object( parent );
2331 DECL_HANDLER(unload_registry)
2333 struct key *key, *parent;
2334 struct unicode_str name;
2335 unsigned int access = 0;
2337 if (!thread_single_check_privilege( current, &SeRestorePrivilege ))
2339 set_error( STATUS_PRIVILEGE_NOT_HELD );
2340 return;
2343 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2345 if ((parent = get_parent_hkey_obj( req->parent )))
2347 get_req_path( &name, !req->parent );
2348 if ((key = open_key( parent, &name, access, req->attributes )))
2350 if (key->obj.handle_count)
2351 set_error( STATUS_CANNOT_DELETE );
2352 else
2353 delete_key( key, 1 ); /* FIXME */
2354 release_object( key );
2356 release_object( parent );
2360 /* save a registry branch to a file */
2361 DECL_HANDLER(save_registry)
2363 struct key *key;
2365 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2367 set_error( STATUS_PRIVILEGE_NOT_HELD );
2368 return;
2371 if ((key = get_hkey_obj( req->hkey, 0 )))
2373 save_registry( key, req->file );
2374 release_object( key );
2378 /* add a registry key change notification */
2379 DECL_HANDLER(set_registry_notification)
2381 struct key *key;
2382 struct event *event;
2383 struct notify *notify;
2385 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2386 if (key)
2388 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2389 if (event)
2391 notify = find_notify( key, current->process, req->hkey );
2392 if (!notify)
2394 notify = mem_alloc( sizeof(*notify) );
2395 if (notify)
2397 notify->events = NULL;
2398 notify->event_count = 0;
2399 notify->subtree = req->subtree;
2400 notify->filter = req->filter;
2401 notify->hkey = req->hkey;
2402 notify->process = current->process;
2403 list_add_head( &key->notify_list, &notify->entry );
2406 if (notify)
2408 struct event **new_array;
2410 if ((new_array = realloc( notify->events, (notify->event_count + 1) * sizeof(*notify->events) )))
2412 notify->events = new_array;
2413 notify->events[notify->event_count++] = (struct event *)grab_object( event );
2414 reset_event( event );
2415 set_error( STATUS_PENDING );
2417 else set_error( STATUS_NO_MEMORY );
2419 release_object( event );
2421 release_object( key );