wineoss: Fix missing break statement.
[wine.git] / server / registry.c
blob93e8a309593323c3e4bafa63e38ab5c6129e3a80
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"
27 #include <assert.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <limits.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <sys/stat.h>
37 #include <sys/types.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 struct acl *dacl;
373 struct ace *ace;
374 struct sid *sid;
375 size_t users_sid_len = sid_len( &builtin_users_sid );
376 size_t admins_sid_len = sid_len( &builtin_admins_sid );
377 size_t dacl_len = sizeof(*dacl) + 2 * sizeof(*ace) + users_sid_len + admins_sid_len;
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 sid = (struct sid *)(key_default_sd + 1);
386 sid = copy_sid( sid, &builtin_admins_sid );
387 sid = copy_sid( sid, &builtin_admins_sid );
389 dacl = (struct acl *)((char *)(key_default_sd + 1) + 2 * admins_sid_len);
390 dacl->revision = ACL_REVISION;
391 dacl->pad1 = 0;
392 dacl->size = dacl_len;
393 dacl->count = 2;
394 dacl->pad2 = 0;
395 ace = set_ace( ace_first( dacl ), &builtin_users_sid, ACCESS_ALLOWED_ACE_TYPE,
396 INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE, GENERIC_READ );
397 set_ace( ace_next( ace ), &builtin_admins_sid, ACCESS_ALLOWED_ACE_TYPE, 0, KEY_ALL_ACCESS );
399 return key_default_sd;
402 static WCHAR *key_get_full_name( struct object *obj, data_size_t *ret_len )
404 static const WCHAR backslash = '\\';
405 struct key *key = (struct key *) obj;
406 data_size_t len = sizeof(root_name) - sizeof(WCHAR);
407 char *ret;
409 if (key->flags & KEY_DELETED)
411 set_error( STATUS_KEY_DELETED );
412 return NULL;
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 if (key->flags & KEY_PREDEF) set_error( STATUS_PREDEFINED_HANDLE );
798 grab_object( key );
799 return key;
802 /* create a subkey */
803 static struct key *create_key( struct key *key, const struct unicode_str *name,
804 const struct unicode_str *class, unsigned int options,
805 unsigned int access, unsigned int attributes,
806 const struct security_descriptor *sd, int *created )
808 int index;
809 struct unicode_str token, next;
811 *created = 0;
812 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
814 if (!token.len) /* the key already exists */
816 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
817 if (options & REG_OPTION_CREATE_LINK)
819 set_error( STATUS_OBJECT_NAME_COLLISION );
820 return NULL;
822 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
824 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
825 return NULL;
827 if (debug_level > 1) dump_operation( key, NULL, "Open" );
828 if (key->flags & KEY_PREDEF) set_error( STATUS_PREDEFINED_HANDLE );
829 grab_object( key );
830 return key;
833 /* token must be the last path component at this point */
834 next = token;
835 get_path_token( name, &next );
836 if (next.len)
838 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
839 return NULL;
842 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
844 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
845 return NULL;
847 *created = 1;
848 make_dirty( key );
849 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
851 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
852 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
853 else key->flags |= KEY_DIRTY;
855 if (sd) default_set_sd( &key->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
856 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
858 if (debug_level > 1) dump_operation( key, NULL, "Create" );
859 if (class && class->len)
861 key->classlen = class->len;
862 free(key->class);
863 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
865 touch_key( key->parent, REG_NOTIFY_CHANGE_NAME );
866 grab_object( key );
867 return key;
870 /* recursively create a subkey (for internal use only) */
871 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
873 struct key *base;
874 int index;
875 struct unicode_str token;
877 token.str = NULL;
878 if (!get_path_token( name, &token )) return NULL;
879 while (token.len)
881 struct key *subkey;
882 if (!(subkey = find_subkey( key, &token, &index ))) break;
883 key = subkey;
884 if (!(key = follow_symlink( key, 0 )))
886 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
887 return NULL;
889 get_path_token( name, &token );
892 if (token.len)
894 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
895 base = key;
896 for (;;)
898 get_path_token( name, &token );
899 if (!token.len) break;
900 /* we know the index is always 0 in a new key */
901 if (!(key = alloc_subkey( key, &token, 0, modif )))
903 free_subkey( base, index );
904 return NULL;
909 grab_object( key );
910 return key;
913 /* query information about a key or a subkey */
914 static void enum_key( struct key *key, int index, int info_class, struct enum_key_reply *reply )
916 int i;
917 data_size_t len, namelen, classlen;
918 data_size_t max_subkey = 0, max_class = 0;
919 data_size_t max_value = 0, max_data = 0;
920 WCHAR *fullname = NULL;
921 char *data;
923 if (key->flags & KEY_PREDEF)
925 set_error( STATUS_INVALID_HANDLE );
926 return;
929 if (index != -1) /* -1 means use the specified key directly */
931 if ((index < 0) || (index > key->last_subkey))
933 set_error( STATUS_NO_MORE_ENTRIES );
934 return;
936 key = key->subkeys[index];
939 namelen = key->namelen;
940 classlen = key->classlen;
942 switch(info_class)
944 case KeyNameInformation:
945 if (!(fullname = key->obj.ops->get_full_name( &key->obj, &namelen ))) return;
946 /* fall through */
947 case KeyBasicInformation:
948 classlen = 0; /* only return the name */
949 /* fall through */
950 case KeyNodeInformation:
951 reply->max_subkey = 0;
952 reply->max_class = 0;
953 reply->max_value = 0;
954 reply->max_data = 0;
955 break;
956 case KeyFullInformation:
957 case KeyCachedInformation:
958 for (i = 0; i <= key->last_subkey; i++)
960 if (key->subkeys[i]->namelen > max_subkey) max_subkey = key->subkeys[i]->namelen;
961 if (key->subkeys[i]->classlen > max_class) max_class = key->subkeys[i]->classlen;
963 for (i = 0; i <= key->last_value; i++)
965 if (key->values[i].namelen > max_value) max_value = key->values[i].namelen;
966 if (key->values[i].len > max_data) max_data = key->values[i].len;
968 reply->max_subkey = max_subkey;
969 reply->max_class = max_class;
970 reply->max_value = max_value;
971 reply->max_data = max_data;
972 reply->namelen = namelen;
973 if (info_class == KeyCachedInformation)
974 classlen = 0; /* don't return any data, only its size */
975 namelen = 0; /* don't return name */
976 break;
977 default:
978 set_error( STATUS_INVALID_PARAMETER );
979 return;
981 reply->subkeys = key->last_subkey + 1;
982 reply->values = key->last_value + 1;
983 reply->modif = key->modif;
984 reply->total = namelen + classlen;
986 len = min( reply->total, get_reply_max_size() );
987 if (len && (data = set_reply_data_size( len )))
989 if (len > namelen)
991 reply->namelen = namelen;
992 memcpy( data, key->name, namelen );
993 memcpy( data + namelen, key->class, len - namelen );
995 else if (info_class == KeyNameInformation)
997 reply->namelen = namelen;
998 memcpy( data, fullname, len );
1000 else
1002 reply->namelen = len;
1003 memcpy( data, key->name, len );
1006 free( fullname );
1007 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
1010 /* delete a key and its values */
1011 static int delete_key( struct key *key, int recurse )
1013 int index;
1014 struct key *parent = key->parent;
1016 /* must find parent and index */
1017 if (key == root_key)
1019 set_error( STATUS_ACCESS_DENIED );
1020 return -1;
1022 assert( parent );
1024 if (key->flags & KEY_PREDEF)
1026 set_error( STATUS_INVALID_HANDLE );
1027 return -1;
1030 while (recurse && (key->last_subkey>=0))
1031 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
1032 return -1;
1034 for (index = 0; index <= parent->last_subkey; index++)
1035 if (parent->subkeys[index] == key) break;
1036 assert( index <= parent->last_subkey );
1038 /* we can only delete a key that has no subkeys */
1039 if (key->last_subkey >= 0)
1041 set_error( STATUS_ACCESS_DENIED );
1042 return -1;
1045 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
1046 free_subkey( parent, index );
1047 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
1048 return 0;
1051 /* try to grow the array of values; return 1 if OK, 0 on error */
1052 static int grow_values( struct key *key )
1054 struct key_value *new_val;
1055 int nb_values;
1057 if (key->nb_values)
1059 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
1060 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
1062 set_error( STATUS_NO_MEMORY );
1063 return 0;
1066 else
1068 nb_values = MIN_VALUES;
1069 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
1071 key->values = new_val;
1072 key->nb_values = nb_values;
1073 return 1;
1076 /* find the named value of a given key and return its index in the array */
1077 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
1079 int i, min, max, res;
1080 data_size_t len;
1082 min = 0;
1083 max = key->last_value;
1084 while (min <= max)
1086 i = (min + max) / 2;
1087 len = min( key->values[i].namelen, name->len );
1088 res = memicmp_strW( key->values[i].name, name->str, len );
1089 if (!res) res = key->values[i].namelen - name->len;
1090 if (!res)
1092 *index = i;
1093 return &key->values[i];
1095 if (res > 0) max = i - 1;
1096 else min = i + 1;
1098 *index = min; /* this is where we should insert it */
1099 return NULL;
1102 /* insert a new value; the index must have been returned by find_value */
1103 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
1105 struct key_value *value;
1106 WCHAR *new_name = NULL;
1107 int i;
1109 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
1111 set_error( STATUS_NAME_TOO_LONG );
1112 return NULL;
1114 if (key->last_value + 1 == key->nb_values)
1116 if (!grow_values( key )) return NULL;
1118 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1119 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1120 value = &key->values[index];
1121 value->name = new_name;
1122 value->namelen = name->len;
1123 value->len = 0;
1124 value->data = NULL;
1125 return value;
1128 /* set a key value */
1129 static void set_value( struct key *key, const struct unicode_str *name,
1130 int type, const void *data, data_size_t len )
1132 struct key_value *value;
1133 void *ptr = NULL;
1134 int index;
1136 if (key->flags & KEY_PREDEF)
1138 set_error( STATUS_INVALID_HANDLE );
1139 return;
1142 if ((value = find_value( key, name, &index )))
1144 /* check if the new value is identical to the existing one */
1145 if (value->type == type && value->len == len &&
1146 value->data && !memcmp( value->data, data, len ))
1148 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1149 return;
1153 if (key->flags & KEY_SYMLINK)
1155 if (type != REG_LINK || name->len != symlink_str.len ||
1156 memicmp_strW( name->str, symlink_str.str, name->len ))
1158 set_error( STATUS_ACCESS_DENIED );
1159 return;
1163 if (len && !(ptr = memdup( data, len ))) return;
1165 if (!value)
1167 if (!(value = insert_value( key, name, index )))
1169 free( ptr );
1170 return;
1173 else free( value->data ); /* already existing, free previous data */
1175 value->type = type;
1176 value->len = len;
1177 value->data = ptr;
1178 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1179 if (debug_level > 1) dump_operation( key, value, "Set" );
1182 /* get a key value */
1183 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1185 struct key_value *value;
1186 int index;
1188 if (key->flags & KEY_PREDEF)
1190 set_error( STATUS_INVALID_HANDLE );
1191 return;
1194 if ((value = find_value( key, name, &index )))
1196 *type = value->type;
1197 *len = value->len;
1198 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1199 if (debug_level > 1) dump_operation( key, value, "Get" );
1201 else
1203 *type = -1;
1204 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1208 /* enumerate a key value */
1209 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1211 struct key_value *value;
1213 if (key->flags & KEY_PREDEF)
1215 set_error( STATUS_INVALID_HANDLE );
1216 return;
1219 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1220 else
1222 void *data;
1223 data_size_t namelen, maxlen;
1225 value = &key->values[i];
1226 reply->type = value->type;
1227 namelen = value->namelen;
1229 switch(info_class)
1231 case KeyValueBasicInformation:
1232 reply->total = namelen;
1233 break;
1234 case KeyValueFullInformation:
1235 reply->total = namelen + value->len;
1236 break;
1237 case KeyValuePartialInformation:
1238 reply->total = value->len;
1239 namelen = 0;
1240 break;
1241 default:
1242 set_error( STATUS_INVALID_PARAMETER );
1243 return;
1246 maxlen = min( reply->total, get_reply_max_size() );
1247 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1249 if (maxlen > namelen)
1251 reply->namelen = namelen;
1252 memcpy( data, value->name, namelen );
1253 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1255 else
1257 reply->namelen = maxlen;
1258 memcpy( data, value->name, maxlen );
1261 if (debug_level > 1) dump_operation( key, value, "Enum" );
1265 /* delete a value */
1266 static void delete_value( struct key *key, const struct unicode_str *name )
1268 struct key_value *value;
1269 int i, index, nb_values;
1271 if (key->flags & KEY_PREDEF)
1273 set_error( STATUS_INVALID_HANDLE );
1274 return;
1277 if (!(value = find_value( key, name, &index )))
1279 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1280 return;
1282 if (debug_level > 1) dump_operation( key, value, "Delete" );
1283 free( value->name );
1284 free( value->data );
1285 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1286 key->last_value--;
1287 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1289 /* try to shrink the array */
1290 nb_values = key->nb_values;
1291 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1293 struct key_value *new_val;
1294 nb_values -= nb_values / 3; /* shrink by 33% */
1295 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1296 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1297 key->values = new_val;
1298 key->nb_values = nb_values;
1302 /* get the registry key corresponding to an hkey handle */
1303 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1305 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1307 if (key && key->flags & KEY_DELETED)
1309 set_error( STATUS_KEY_DELETED );
1310 release_object( key );
1311 key = NULL;
1313 return key;
1316 /* get the registry key corresponding to a parent key handle */
1317 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1319 if (!hkey) return (struct key *)grab_object( root_key );
1320 return get_hkey_obj( hkey, 0 );
1323 /* read a line from the input file */
1324 static int read_next_line( struct file_load_info *info )
1326 char *newbuf;
1327 int newlen, pos = 0;
1329 info->line++;
1330 for (;;)
1332 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1333 return (pos != 0); /* EOF */
1334 pos = strlen(info->buffer);
1335 if (info->buffer[pos-1] == '\n')
1337 /* got a full line */
1338 info->buffer[--pos] = 0;
1339 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1340 return 1;
1342 if (pos < info->len - 1) return 1; /* EOF but something was read */
1344 /* need to enlarge the buffer */
1345 newlen = info->len + info->len / 2;
1346 if (!(newbuf = realloc( info->buffer, newlen )))
1348 set_error( STATUS_NO_MEMORY );
1349 return -1;
1351 info->buffer = newbuf;
1352 info->len = newlen;
1356 /* make sure the temp buffer holds enough space */
1357 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1359 WCHAR *tmp;
1360 if (info->tmplen >= size) return 1;
1361 if (!(tmp = realloc( info->tmp, size )))
1363 set_error( STATUS_NO_MEMORY );
1364 return 0;
1366 info->tmp = tmp;
1367 info->tmplen = size;
1368 return 1;
1371 /* report an error while loading an input file */
1372 static void file_read_error( const char *err, struct file_load_info *info )
1374 if (info->filename)
1375 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1376 else
1377 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1380 /* convert a data type tag to a value type */
1381 static int get_data_type( const char *buffer, int *type, int *parse_type )
1383 struct data_type { const char *tag; int len; int type; int parse_type; };
1385 static const struct data_type data_types[] =
1386 { /* actual type */ /* type to assume for parsing */
1387 { "\"", 1, REG_SZ, REG_SZ },
1388 { "str:\"", 5, REG_SZ, REG_SZ },
1389 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1390 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1391 { "hex:", 4, REG_BINARY, REG_BINARY },
1392 { "dword:", 6, REG_DWORD, REG_DWORD },
1393 { "hex(", 4, -1, REG_BINARY },
1394 { NULL, 0, 0, 0 }
1397 const struct data_type *ptr;
1398 char *end;
1400 for (ptr = data_types; ptr->tag; ptr++)
1402 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1403 *parse_type = ptr->parse_type;
1404 if ((*type = ptr->type) != -1) return ptr->len;
1405 /* "hex(xx):" is special */
1406 *type = (int)strtoul( buffer + 4, &end, 16 );
1407 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1408 return end + 2 - buffer;
1410 return 0;
1413 /* load and create a key from the input file */
1414 static struct key *load_key( struct key *base, const char *buffer, int prefix_len,
1415 struct file_load_info *info, timeout_t *modif )
1417 WCHAR *p;
1418 struct unicode_str name;
1419 int res;
1420 unsigned int mod;
1421 data_size_t len;
1423 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1425 len = info->tmplen;
1426 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1428 file_read_error( "Malformed key", info );
1429 return NULL;
1431 if (sscanf( buffer + res, " %u", &mod ) == 1)
1432 *modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1433 else
1434 *modif = current_time;
1436 p = info->tmp;
1437 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1439 if (!*p)
1441 if (prefix_len > 1)
1443 file_read_error( "Malformed key", info );
1444 return NULL;
1446 /* empty key name, return base key */
1447 return (struct key *)grab_object( base );
1449 name.str = p;
1450 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1451 return create_key_recursive( base, &name, 0 );
1454 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1455 static void update_key_time( struct key *key, timeout_t modif )
1457 while (key && !key->modif)
1459 key->modif = modif;
1460 key = key->parent;
1464 /* load a global option from the input file */
1465 static int load_global_option( const char *buffer, struct file_load_info *info )
1467 const char *p;
1469 if (!strncmp( buffer, "#arch=", 6 ))
1471 enum prefix_type type;
1472 p = buffer + 6;
1473 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1474 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1475 else
1477 file_read_error( "Unknown architecture", info );
1478 set_error( STATUS_NOT_REGISTRY_FILE );
1479 return 0;
1481 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1482 else if (type != prefix_type)
1484 file_read_error( "Mismatched architecture", info );
1485 set_error( STATUS_NOT_REGISTRY_FILE );
1486 return 0;
1489 /* ignore unknown options */
1490 return 1;
1493 /* load a key option from the input file */
1494 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1496 const char *p;
1497 data_size_t len;
1499 if (!strncmp( buffer, "#time=", 6 ))
1501 timeout_t modif = 0;
1502 for (p = buffer + 6; *p; p++)
1504 if (*p >= '0' && *p <= '9') modif = (modif << 4) | (*p - '0');
1505 else if (*p >= 'A' && *p <= 'F') modif = (modif << 4) | (*p - 'A' + 10);
1506 else if (*p >= 'a' && *p <= 'f') modif = (modif << 4) | (*p - 'a' + 10);
1507 else break;
1509 update_key_time( key, modif );
1511 if (!strncmp( buffer, "#class=", 7 ))
1513 p = buffer + 7;
1514 if (*p++ != '"') return 0;
1515 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1516 len = info->tmplen;
1517 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1518 free( key->class );
1519 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1520 key->classlen = len;
1522 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1523 /* ignore unknown options */
1524 return 1;
1527 /* parse a comma-separated list of hex digits */
1528 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1530 const char *p = buffer;
1531 data_size_t count = 0;
1532 char *end;
1534 while (isxdigit(*p))
1536 unsigned int val = strtoul( p, &end, 16 );
1537 if (end == p || val > 0xff) return -1;
1538 if (count++ >= *len) return -1; /* dest buffer overflow */
1539 *dest++ = val;
1540 p = end;
1541 while (isspace(*p)) p++;
1542 if (*p == ',') p++;
1543 while (isspace(*p)) p++;
1545 *len = count;
1546 return p - buffer;
1549 /* parse a value name and create the corresponding value */
1550 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1551 struct file_load_info *info )
1553 struct key_value *value;
1554 struct unicode_str name;
1555 int index;
1557 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1558 name.str = info->tmp;
1559 name.len = info->tmplen;
1560 if (buffer[0] == '@')
1562 name.len = 0;
1563 *len = 1;
1565 else
1567 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1568 if (r == -1) goto error;
1569 *len = r + 1; /* for initial quote */
1570 name.len -= sizeof(WCHAR); /* terminating null */
1572 while (isspace(buffer[*len])) (*len)++;
1573 if (buffer[*len] != '=') goto error;
1574 (*len)++;
1575 while (isspace(buffer[*len])) (*len)++;
1576 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1577 return value;
1579 error:
1580 file_read_error( "Malformed value name", info );
1581 return NULL;
1584 /* load a value from the input file */
1585 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1587 DWORD dw;
1588 void *ptr, *newptr;
1589 int res, type, parse_type;
1590 data_size_t maxlen, len;
1591 struct key_value *value;
1593 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1594 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1595 buffer += len + res;
1597 switch(parse_type)
1599 case REG_SZ:
1600 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1601 len = info->tmplen;
1602 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1603 ptr = info->tmp;
1604 break;
1605 case REG_DWORD:
1606 dw = strtoul( buffer, NULL, 16 );
1607 ptr = &dw;
1608 len = sizeof(dw);
1609 break;
1610 case REG_BINARY: /* hex digits */
1611 len = 0;
1612 for (;;)
1614 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1615 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1616 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1617 len += maxlen;
1618 buffer += res;
1619 while (isspace(*buffer)) buffer++;
1620 if (!*buffer) break;
1621 if (*buffer != '\\') goto error;
1622 if (read_next_line( info) != 1) goto error;
1623 buffer = info->buffer;
1624 while (isspace(*buffer)) buffer++;
1626 ptr = info->tmp;
1627 break;
1628 default:
1629 assert(0);
1630 ptr = NULL; /* keep compiler quiet */
1631 break;
1634 if (!len) newptr = NULL;
1635 else if (!(newptr = memdup( ptr, len ))) return 0;
1637 free( value->data );
1638 value->data = newptr;
1639 value->len = len;
1640 value->type = type;
1641 return 1;
1643 error:
1644 file_read_error( "Malformed value", info );
1645 free( value->data );
1646 value->data = NULL;
1647 value->len = 0;
1648 value->type = REG_NONE;
1649 return 0;
1652 /* return the length (in path elements) of name that is part of the key name */
1653 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1654 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1656 WCHAR *p;
1657 int res;
1658 data_size_t len;
1660 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1662 len = info->tmplen;
1663 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1665 file_read_error( "Malformed key", info );
1666 return 0;
1668 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1669 len = (p - info->tmp) * sizeof(WCHAR);
1670 for (res = 1; key != root_key; res++)
1672 if (len == key->namelen && !memicmp_strW( info->tmp, key->name, len )) break;
1673 key = key->parent;
1675 if (key == root_key) res = 0; /* no matching name */
1676 return res;
1679 /* load all the keys from the input file */
1680 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1681 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1683 struct key *subkey = NULL;
1684 struct file_load_info info;
1685 timeout_t modif = current_time;
1686 char *p;
1688 info.filename = filename;
1689 info.file = f;
1690 info.len = 4;
1691 info.tmplen = 4;
1692 info.line = 0;
1693 if (!(info.buffer = mem_alloc( info.len ))) return;
1694 if (!(info.tmp = mem_alloc( info.tmplen )))
1696 free( info.buffer );
1697 return;
1700 if ((read_next_line( &info ) != 1) ||
1701 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1703 set_error( STATUS_NOT_REGISTRY_FILE );
1704 goto done;
1707 while (read_next_line( &info ) == 1)
1709 p = info.buffer;
1710 while (*p && isspace(*p)) p++;
1711 switch(*p)
1713 case '[': /* new key */
1714 if (subkey)
1716 update_key_time( subkey, modif );
1717 release_object( subkey );
1719 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1720 if (!(subkey = load_key( key, p + 1, prefix_len, &info, &modif )))
1721 file_read_error( "Error creating key", &info );
1722 break;
1723 case '@': /* default value */
1724 case '\"': /* value */
1725 if (subkey) load_value( subkey, p, &info );
1726 else file_read_error( "Value without key", &info );
1727 break;
1728 case '#': /* option */
1729 if (subkey) load_key_option( subkey, p, &info );
1730 else if (!load_global_option( p, &info )) goto done;
1731 break;
1732 case ';': /* comment */
1733 case 0: /* empty line */
1734 break;
1735 default:
1736 file_read_error( "Unrecognized input", &info );
1737 break;
1741 done:
1742 if (subkey)
1744 update_key_time( subkey, modif );
1745 release_object( subkey );
1747 free( info.buffer );
1748 free( info.tmp );
1751 /* load a part of the registry from a file */
1752 static void load_registry( struct key *key, obj_handle_t handle )
1754 struct file *file;
1755 int fd;
1757 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1758 fd = dup( get_file_unix_fd( file ) );
1759 release_object( file );
1760 if (fd != -1)
1762 FILE *f = fdopen( fd, "r" );
1763 if (f)
1765 load_keys( key, NULL, f, -1 );
1766 fclose( f );
1768 else file_set_error();
1772 /* load one of the initial registry files */
1773 static int load_init_registry_from_file( const char *filename, struct key *key )
1775 FILE *f;
1777 if ((f = fopen( filename, "r" )))
1779 load_keys( key, filename, f, 0 );
1780 fclose( f );
1781 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1783 fprintf( stderr, "%s is not a valid registry file\n", filename );
1784 return 1;
1788 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1790 save_branch_info[save_branch_count].path = filename;
1791 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1792 make_object_permanent( &key->obj );
1793 return (f != NULL);
1796 static WCHAR *format_user_registry_path( const struct sid *sid, struct unicode_str *path )
1798 char buffer[7 + 11 + 11 + 11 * ARRAY_SIZE(sid->sub_auth)], *p = buffer;
1799 unsigned int i;
1801 p += sprintf( p, "User\\S-%u-%u", sid->revision,
1802 ((unsigned int)sid->id_auth[2] << 24) |
1803 ((unsigned int)sid->id_auth[3] << 16) |
1804 ((unsigned int)sid->id_auth[4] << 8) |
1805 ((unsigned int)sid->id_auth[5]) );
1806 for (i = 0; i < sid->sub_count; i++) p += sprintf( p, "-%u", sid->sub_auth[i] );
1807 return ascii_to_unicode_str( buffer, path );
1810 static void init_supported_machines(void)
1812 unsigned int count = 0;
1813 #ifdef __i386__
1814 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1815 #elif defined(__x86_64__)
1816 if (prefix_type == PREFIX_64BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_AMD64;
1817 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1818 #elif defined(__arm__)
1819 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1820 #elif defined(__aarch64__)
1821 if (prefix_type == PREFIX_64BIT)
1823 supported_machines[count++] = IMAGE_FILE_MACHINE_ARM64;
1824 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1826 supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1827 #else
1828 #error Unsupported machine
1829 #endif
1830 supported_machines_count = count;
1831 native_machine = supported_machines[0];
1834 /* registry initialisation */
1835 void init_registry(void)
1837 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1838 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1839 static const WCHAR classes_i386[] = {'S','o','f','t','w','a','r','e','\\',
1840 'C','l','a','s','s','e','s','\\',
1841 'W','o','w','6','4','3','2','N','o','d','e'};
1842 static const WCHAR classes_amd64[] = {'S','o','f','t','w','a','r','e','\\',
1843 'C','l','a','s','s','e','s','\\',
1844 'W','o','w','6','4','6','4','N','o','d','e'};
1845 static const WCHAR classes_arm[] = {'S','o','f','t','w','a','r','e','\\',
1846 'C','l','a','s','s','e','s','\\',
1847 'W','o','w','A','A','3','2','N','o','d','e'};
1848 static const WCHAR classes_arm64[] = {'S','o','f','t','w','a','r','e','\\',
1849 'C','l','a','s','s','e','s','\\',
1850 'W','o','w','A','A','6','4','N','o','d','e'};
1851 static const WCHAR perflib[] = {'S','o','f','t','w','a','r','e','\\',
1852 'M','i','c','r','o','s','o','f','t','\\',
1853 'W','i','n','d','o','w','s',' ','N','T','\\',
1854 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1855 'P','e','r','f','l','i','b','\\',
1856 '0','0','9'};
1857 static const struct unicode_str root_name = { NULL, 0 };
1858 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1859 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1860 static const struct unicode_str perflib_name = { perflib, sizeof(perflib) };
1862 WCHAR *current_user_path;
1863 struct unicode_str current_user_str;
1864 struct key *key, *hklm, *hkcu;
1865 unsigned int i;
1866 char *p;
1868 /* switch to the config dir */
1870 if (fchdir( config_dir_fd ) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno ));
1872 /* create the root key */
1873 root_key = alloc_key( &root_name, current_time );
1874 assert( root_key );
1875 make_object_permanent( &root_key->obj );
1877 /* load system.reg into Registry\Machine */
1879 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1880 fatal_error( "could not create Machine registry key\n" );
1882 if (!load_init_registry_from_file( "system.reg", hklm ))
1884 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1885 prefix_type = PREFIX_32BIT;
1886 else
1887 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1889 else if (prefix_type == PREFIX_UNKNOWN)
1890 prefix_type = PREFIX_32BIT;
1892 init_supported_machines();
1894 /* load userdef.reg into Registry\User\.Default */
1896 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1897 fatal_error( "could not create User\\.Default registry key\n" );
1899 load_init_registry_from_file( "userdef.reg", key );
1900 release_object( key );
1902 /* load user.reg into HKEY_CURRENT_USER */
1904 /* FIXME: match default user in token.c. should get from process token instead */
1905 current_user_path = format_user_registry_path( &local_user_sid, &current_user_str );
1906 if (!current_user_path ||
1907 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1908 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1909 free( current_user_path );
1910 load_init_registry_from_file( "user.reg", hkcu );
1912 /* set the shared flag on Software\Classes\Wow6432Node for all platforms */
1913 for (i = 1; i < supported_machines_count; i++)
1915 struct unicode_str name;
1917 switch (supported_machines[i])
1919 case IMAGE_FILE_MACHINE_I386: name.str = classes_i386; name.len = sizeof(classes_i386); break;
1920 case IMAGE_FILE_MACHINE_ARMNT: name.str = classes_arm; name.len = sizeof(classes_arm); break;
1921 case IMAGE_FILE_MACHINE_AMD64: name.str = classes_amd64; name.len = sizeof(classes_amd64); break;
1922 case IMAGE_FILE_MACHINE_ARM64: name.str = classes_arm64; name.len = sizeof(classes_arm64); break;
1924 if ((key = create_key_recursive( hklm, &name, current_time )))
1926 key->flags |= KEY_WOWSHARE;
1927 release_object( key );
1929 /* FIXME: handle HKCU too */
1932 if ((key = create_key_recursive( hklm, &perflib_name, current_time )))
1934 key->flags |= KEY_PREDEF;
1935 release_object( key );
1938 release_object( hklm );
1939 release_object( hkcu );
1941 /* start the periodic save timer */
1942 set_periodic_save_timer();
1944 /* create windows directories */
1946 if (!mkdir( "drive_c/windows", 0777 ))
1948 mkdir( "drive_c/windows/system32", 0777 );
1949 for (i = 1; i < supported_machines_count; i++)
1951 switch (supported_machines[i])
1953 case IMAGE_FILE_MACHINE_I386: mkdir( "drive_c/windows/syswow64", 0777 ); break;
1954 case IMAGE_FILE_MACHINE_ARMNT: mkdir( "drive_c/windows/sysarm32", 0777 ); break;
1955 case IMAGE_FILE_MACHINE_AMD64: mkdir( "drive_c/windows/sysx8664", 0777 ); break;
1956 case IMAGE_FILE_MACHINE_ARM64: mkdir( "drive_c/windows/sysarm64", 0777 ); break;
1961 /* go back to the server dir */
1962 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
1965 /* save a registry branch to a file */
1966 static void save_all_subkeys( struct key *key, FILE *f )
1968 fprintf( f, "WINE REGISTRY Version 2\n" );
1969 fprintf( f, ";; All keys relative to " );
1970 dump_path( key, NULL, f );
1971 fprintf( f, "\n" );
1972 switch (prefix_type)
1974 case PREFIX_32BIT:
1975 fprintf( f, "\n#arch=win32\n" );
1976 break;
1977 case PREFIX_64BIT:
1978 fprintf( f, "\n#arch=win64\n" );
1979 break;
1980 default:
1981 break;
1983 save_subkeys( key, key, f );
1986 /* save a registry branch to a file handle */
1987 static void save_registry( struct key *key, obj_handle_t handle )
1989 struct file *file;
1990 int fd;
1992 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1993 fd = dup( get_file_unix_fd( file ) );
1994 release_object( file );
1995 if (fd != -1)
1997 FILE *f = fdopen( fd, "w" );
1998 if (f)
2000 save_all_subkeys( key, f );
2001 if (fclose( f )) file_set_error();
2003 else
2005 file_set_error();
2006 close( fd );
2011 /* save a registry branch to a file */
2012 static int save_branch( struct key *key, const char *path )
2014 struct stat st;
2015 char *p, *tmp = NULL;
2016 int fd, count = 0, ret = 0;
2017 FILE *f;
2019 if (!(key->flags & KEY_DIRTY))
2021 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
2022 return 1;
2025 /* test the file type */
2027 if ((fd = open( path, O_WRONLY )) != -1)
2029 /* if file is not a regular file or has multiple links or is accessed
2030 * via symbolic links, write directly into it; otherwise use a temp file */
2031 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
2033 ftruncate( fd, 0 );
2034 goto save;
2036 close( fd );
2039 /* create a temp file in the same directory */
2041 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
2042 strcpy( tmp, path );
2043 if ((p = strrchr( tmp, '/' ))) p++;
2044 else p = tmp;
2045 for (;;)
2047 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
2048 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
2049 if (errno != EEXIST) goto done;
2050 close( fd );
2053 /* now save to it */
2055 save:
2056 if (!(f = fdopen( fd, "w" )))
2058 if (tmp) unlink( tmp );
2059 close( fd );
2060 goto done;
2063 if (debug_level > 1)
2065 fprintf( stderr, "%s: ", path );
2066 dump_operation( key, NULL, "saving" );
2069 save_all_subkeys( key, f );
2070 ret = !fclose(f);
2072 if (tmp)
2074 /* if successfully written, rename to final name */
2075 if (ret) ret = !rename( tmp, path );
2076 if (!ret) unlink( tmp );
2079 done:
2080 free( tmp );
2081 if (ret) make_clean( key );
2082 return ret;
2085 /* periodic saving of the registry */
2086 static void periodic_save( void *arg )
2088 int i;
2090 if (fchdir( config_dir_fd ) == -1) return;
2091 save_timeout_user = NULL;
2092 for (i = 0; i < save_branch_count; i++)
2093 save_branch( save_branch_info[i].key, save_branch_info[i].path );
2094 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2095 set_periodic_save_timer();
2098 /* start the periodic save timer */
2099 static void set_periodic_save_timer(void)
2101 if (save_timeout_user) remove_timeout_user( save_timeout_user );
2102 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
2105 /* save the modified registry branches to disk */
2106 void flush_registry(void)
2108 int i;
2110 if (fchdir( config_dir_fd ) == -1) return;
2111 for (i = 0; i < save_branch_count; i++)
2113 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
2115 fprintf( stderr, "wineserver: could not save registry branch to %s",
2116 save_branch_info[i].path );
2117 perror( " " );
2120 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2123 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2124 static int is_wow64_thread( struct thread *thread )
2126 return (is_machine_64bit( native_machine ) && !is_machine_64bit( thread->process->machine ));
2130 /* create a registry key */
2131 DECL_HANDLER(create_key)
2133 struct key *key = NULL, *parent;
2134 struct unicode_str name, class;
2135 unsigned int access = req->access;
2136 const struct security_descriptor *sd;
2137 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2139 if (!objattr) return;
2141 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2143 class.str = get_req_data_after_objattr( objattr, &class.len );
2144 class.len = (class.len / sizeof(WCHAR)) * sizeof(WCHAR);
2146 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2147 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2149 name.str += ARRAY_SIZE( root_name );
2150 name.len -= sizeof(root_name);
2153 /* NOTE: no access rights are required from the parent handle to create a key */
2154 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2156 if ((key = create_key( parent, &name, &class, req->options, access,
2157 objattr->attributes, sd, &reply->created )))
2159 reply->hkey = alloc_handle( current->process, key, access, objattr->attributes );
2160 release_object( key );
2162 release_object( parent );
2166 /* open a registry key */
2167 DECL_HANDLER(open_key)
2169 struct key *key, *parent;
2170 struct unicode_str name;
2171 unsigned int access = req->access;
2173 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2175 reply->hkey = 0;
2176 /* NOTE: no access rights are required to open the parent key, only the child key */
2177 if ((parent = get_parent_hkey_obj( req->parent )))
2179 get_req_path( &name, !req->parent );
2180 if ((key = open_key( parent, &name, access, req->attributes )))
2182 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
2183 release_object( key );
2185 release_object( parent );
2189 /* delete a registry key */
2190 DECL_HANDLER(delete_key)
2192 struct key *key;
2194 if ((key = get_hkey_obj( req->hkey, DELETE )))
2196 delete_key( key, 0);
2197 release_object( key );
2201 /* flush a registry key */
2202 DECL_HANDLER(flush_key)
2204 struct key *key = get_hkey_obj( req->hkey, 0 );
2205 if (key)
2207 /* we don't need to do anything here with the current implementation */
2208 release_object( key );
2212 /* enumerate registry subkeys */
2213 DECL_HANDLER(enum_key)
2215 struct key *key;
2217 if ((key = get_hkey_obj( req->hkey,
2218 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
2220 enum_key( key, req->index, req->info_class, reply );
2221 release_object( key );
2225 /* set a value of a registry key */
2226 DECL_HANDLER(set_key_value)
2228 struct key *key;
2229 struct unicode_str name;
2231 if (req->namelen > get_req_data_size())
2233 set_error( STATUS_INVALID_PARAMETER );
2234 return;
2236 name.str = get_req_data();
2237 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2239 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2241 data_size_t datalen = get_req_data_size() - req->namelen;
2242 const char *data = (const char *)get_req_data() + req->namelen;
2244 set_value( key, &name, req->type, data, datalen );
2245 release_object( key );
2249 /* retrieve the value of a registry key */
2250 DECL_HANDLER(get_key_value)
2252 struct key *key;
2253 struct unicode_str name = get_req_unicode_str();
2255 reply->total = 0;
2256 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2258 get_value( key, &name, &reply->type, &reply->total );
2259 release_object( key );
2263 /* enumerate the value of a registry key */
2264 DECL_HANDLER(enum_key_value)
2266 struct key *key;
2268 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2270 enum_value( key, req->index, req->info_class, reply );
2271 release_object( key );
2275 /* delete a value of a registry key */
2276 DECL_HANDLER(delete_key_value)
2278 struct key *key;
2279 struct unicode_str name = get_req_unicode_str();
2281 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2283 delete_value( key, &name );
2284 release_object( key );
2288 /* load a registry branch from a file */
2289 DECL_HANDLER(load_registry)
2291 struct key *key, *parent;
2292 struct unicode_str name;
2293 const struct security_descriptor *sd;
2294 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2296 if (!objattr) return;
2298 if (!thread_single_check_privilege( current, SeRestorePrivilege ))
2300 set_error( STATUS_PRIVILEGE_NOT_HELD );
2301 return;
2304 if (!objattr->rootdir && name.len >= sizeof(root_name) &&
2305 !memicmp_strW( name.str, root_name, sizeof(root_name) ))
2307 name.str += ARRAY_SIZE( root_name );
2308 name.len -= sizeof(root_name);
2311 if ((parent = get_parent_hkey_obj( objattr->rootdir )))
2313 int dummy;
2314 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, sd, &dummy )))
2316 load_registry( key, req->file );
2317 release_object( key );
2319 release_object( parent );
2323 DECL_HANDLER(unload_registry)
2325 struct key *key, *parent;
2326 struct unicode_str name;
2327 unsigned int access = 0;
2329 if (!thread_single_check_privilege( current, SeRestorePrivilege ))
2331 set_error( STATUS_PRIVILEGE_NOT_HELD );
2332 return;
2335 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2337 if ((parent = get_parent_hkey_obj( req->parent )))
2339 get_req_path( &name, !req->parent );
2340 if ((key = open_key( parent, &name, access, req->attributes )))
2342 if (key->obj.handle_count)
2343 set_error( STATUS_CANNOT_DELETE );
2344 else
2345 delete_key( key, 1 ); /* FIXME */
2346 release_object( key );
2348 release_object( parent );
2352 /* save a registry branch to a file */
2353 DECL_HANDLER(save_registry)
2355 struct key *key;
2357 if (!thread_single_check_privilege( current, SeBackupPrivilege ))
2359 set_error( STATUS_PRIVILEGE_NOT_HELD );
2360 return;
2363 if ((key = get_hkey_obj( req->hkey, 0 )))
2365 save_registry( key, req->file );
2366 release_object( key );
2370 /* add a registry key change notification */
2371 DECL_HANDLER(set_registry_notification)
2373 struct key *key;
2374 struct event *event;
2375 struct notify *notify;
2377 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2378 if (key)
2380 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2381 if (event)
2383 notify = find_notify( key, current->process, req->hkey );
2384 if (!notify)
2386 notify = mem_alloc( sizeof(*notify) );
2387 if (notify)
2389 notify->events = NULL;
2390 notify->event_count = 0;
2391 notify->subtree = req->subtree;
2392 notify->filter = req->filter;
2393 notify->hkey = req->hkey;
2394 notify->process = current->process;
2395 list_add_head( &key->notify_list, &notify->entry );
2398 if (notify)
2400 struct event **new_array;
2402 if ((new_array = realloc( notify->events, (notify->event_count + 1) * sizeof(*notify->events) )))
2404 notify->events = new_array;
2405 notify->events[notify->event_count++] = (struct event *)grab_object( event );
2406 reset_event( event );
2407 set_error( STATUS_PENDING );
2409 else set_error( STATUS_NO_MEMORY );
2411 release_object( event );
2413 release_object( key );