ntdll: Add RtlDosPathNameToRelativeNtPathName_U.
[wine.git] / server / registry.c
blob96ba18a0a5a76a2b2ed084062ce3901f35b0c14a
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 *class; /* key class */
82 data_size_t classlen; /* length of class name */
83 int last_subkey; /* last in use subkey */
84 int nb_subkeys; /* count of allocated subkeys */
85 struct key **subkeys; /* subkeys array */
86 struct key *wow6432node; /* Wow6432Node subkey */
87 int last_value; /* last in use value */
88 int nb_values; /* count of allocated values in array */
89 struct key_value *values; /* values array */
90 unsigned int flags; /* flags */
91 timeout_t modif; /* last modification time */
92 struct list notify_list; /* list of notifications */
95 /* key flags */
96 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
97 #define KEY_DELETED 0x0002 /* key has been deleted */
98 #define KEY_DIRTY 0x0004 /* key has been modified */
99 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
100 #define KEY_WOWSHARE 0x0010 /* key is a Wow64 shared key (used for Software\Classes) */
101 #define KEY_PREDEF 0x0020 /* key is marked as predefined */
103 #define OBJ_KEY_WOW64 0x100000 /* magic flag added to attributes for WoW64 redirection */
105 /* a key value */
106 struct key_value
108 WCHAR *name; /* value name */
109 unsigned short namelen; /* length of value name */
110 unsigned int type; /* value type */
111 data_size_t len; /* value data length in bytes */
112 void *data; /* pointer to value data */
115 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
116 #define MIN_VALUES 8 /* min. number of allocated values per key */
118 #define MAX_NAME_LEN 256 /* max. length of a key name */
119 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
121 /* the root of the registry tree */
122 static struct key *root_key;
124 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
125 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
126 static struct timeout_user *save_timeout_user; /* saving timer */
127 static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
129 static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
130 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
131 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
133 static void set_periodic_save_timer(void);
134 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
136 /* information about where to save a registry branch */
137 struct save_branch_info
139 struct key *key;
140 const char *path;
143 #define MAX_SAVE_BRANCH_INFO 3
144 static int save_branch_count;
145 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
147 unsigned int supported_machines_count = 0;
148 unsigned short supported_machines[8];
149 unsigned short native_machine = 0;
151 /* information about a file being loaded */
152 struct file_load_info
154 const char *filename; /* input file name */
155 FILE *file; /* input file */
156 char *buffer; /* line buffer */
157 int len; /* buffer length */
158 int line; /* current input line */
159 WCHAR *tmp; /* temp buffer to use while parsing input */
160 size_t tmplen; /* length of temp buffer */
164 static void key_dump( struct object *obj, int verbose );
165 static unsigned int key_map_access( struct object *obj, unsigned int access );
166 static struct security_descriptor *key_get_sd( struct object *obj );
167 static WCHAR *key_get_full_name( struct object *obj, data_size_t *len );
168 static struct object *key_lookup_name( struct object *obj, struct unicode_str *name,
169 unsigned int attr, struct object *root );
170 static int key_link_name( struct object *obj, struct object_name *name, struct object *parent );
171 static void key_unlink_name( struct object *obj, struct object_name *name );
172 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
173 static void key_destroy( struct object *obj );
175 static const struct object_ops key_ops =
177 sizeof(struct key), /* size */
178 &key_type, /* type */
179 key_dump, /* dump */
180 no_add_queue, /* add_queue */
181 NULL, /* remove_queue */
182 NULL, /* signaled */
183 NULL, /* satisfied */
184 no_signal, /* signal */
185 no_get_fd, /* get_fd */
186 key_map_access, /* map_access */
187 key_get_sd, /* get_sd */
188 default_set_sd, /* set_sd */
189 key_get_full_name, /* get_full_name */
190 key_lookup_name, /* lookup_name */
191 key_link_name, /* link_name */
192 key_unlink_name, /* unlink_name */
193 no_open_file, /* open_file */
194 no_kernel_obj_list, /* get_kernel_obj_list */
195 key_close_handle, /* close_handle */
196 key_destroy /* destroy */
200 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
202 len = get_path_element( name, len );
203 return (len == sizeof(wow6432node) && !memicmp_strW( name, wow6432node, sizeof( wow6432node )));
206 static inline struct key *get_parent( const struct key *key )
208 struct object *parent = key->obj.name->parent;
210 if (!parent || parent->ops != &key_ops) return NULL;
211 return (struct key *)parent;
215 * The registry text file format v2 used by this code is similar to the one
216 * used by REGEDIT import/export functionality, with the following differences:
217 * - strings and key names can contain \x escapes for Unicode
218 * - key names use escapes too in order to support Unicode
219 * - the modification time optionally follows the key name
220 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
223 /* dump the full path of a key */
224 static void dump_path( const struct key *key, const struct key *base, FILE *f )
226 if (get_parent( key ) && get_parent( key ) != base)
228 dump_path( get_parent( key ), base, f );
229 fprintf( f, "\\\\" );
231 dump_strW( key->obj.name->name, key->obj.name->len, f, "[]" );
234 /* dump a value to a text file */
235 static void dump_value( const struct key_value *value, FILE *f )
237 unsigned int i, dw;
238 int count;
240 if (value->namelen)
242 fputc( '\"', f );
243 count = 1 + dump_strW( value->name, value->namelen, f, "\"\"" );
244 count += fprintf( f, "\"=" );
246 else count = fprintf( f, "@=" );
248 switch(value->type)
250 case REG_SZ:
251 case REG_EXPAND_SZ:
252 case REG_MULTI_SZ:
253 /* only output properly terminated strings in string format */
254 if (value->len < sizeof(WCHAR)) break;
255 if (value->len % sizeof(WCHAR)) break;
256 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
257 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
258 fputc( '\"', f );
259 dump_strW( (WCHAR *)value->data, value->len, f, "\"\"" );
260 fprintf( f, "\"\n" );
261 return;
263 case REG_DWORD:
264 if (value->len != sizeof(dw)) break;
265 memcpy( &dw, value->data, sizeof(dw) );
266 fprintf( f, "dword:%08x\n", dw );
267 return;
270 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
271 else count += fprintf( f, "hex(%x):", value->type );
272 for (i = 0; i < value->len; i++)
274 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
275 if (i < value->len-1)
277 fputc( ',', f );
278 if (++count > 76)
280 fprintf( f, "\\\n " );
281 count = 2;
285 fputc( '\n', f );
288 /* find the named child of a given key and return its index */
289 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
291 int i, min, max, res;
292 data_size_t len;
294 min = 0;
295 max = key->last_subkey;
296 while (min <= max)
298 i = (min + max) / 2;
299 len = min( key->subkeys[i]->obj.name->len, name->len );
300 res = memicmp_strW( key->subkeys[i]->obj.name->name, name->str, len );
301 if (!res) res = key->subkeys[i]->obj.name->len - name->len;
302 if (!res)
304 *index = i;
305 return key->subkeys[i];
307 if (res > 0) max = i - 1;
308 else min = i + 1;
310 *index = min; /* this is where we should insert it */
311 return NULL;
314 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
315 static int grow_subkeys( struct key *key )
317 struct key **new_subkeys;
318 int nb_subkeys;
320 if (key->nb_subkeys)
322 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
323 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
325 set_error( STATUS_NO_MEMORY );
326 return 0;
329 else
331 nb_subkeys = MIN_SUBKEYS;
332 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
334 key->subkeys = new_subkeys;
335 key->nb_subkeys = nb_subkeys;
336 return 1;
339 /* save a registry and all its subkeys to a text file */
340 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
342 int i;
344 if (key->flags & KEY_VOLATILE) return;
345 /* save key if it has either some values or no subkeys, or needs special options */
346 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
347 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
349 fprintf( f, "\n[" );
350 if (key != base) dump_path( key, base, f );
351 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
352 fprintf( f, "#time=%x%08x\n", (unsigned int)(key->modif >> 32), (unsigned int)key->modif );
353 if (key->class)
355 fprintf( f, "#class=\"" );
356 dump_strW( key->class, key->classlen, f, "\"\"" );
357 fprintf( f, "\"\n" );
359 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
360 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
362 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
365 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
367 fprintf( stderr, "%s key ", op );
368 if (key) dump_path( key, NULL, stderr );
369 else fprintf( stderr, "ERROR" );
370 if (value)
372 fprintf( stderr, " value ");
373 dump_value( value, stderr );
375 else fprintf( stderr, "\n" );
378 static void key_dump( struct object *obj, int verbose )
380 struct key *key = (struct key *)obj;
381 assert( obj->ops == &key_ops );
382 fprintf( stderr, "Key flags=%x ", key->flags );
383 dump_path( key, NULL, stderr );
384 fprintf( stderr, "\n" );
387 /* notify waiter and maybe delete the notification */
388 static void do_notification( struct key *key, struct notify *notify, int del )
390 unsigned int i;
392 for (i = 0; i < notify->event_count; ++i)
394 set_event( notify->events[i] );
395 release_object( notify->events[i] );
397 free( notify->events );
398 notify->events = NULL;
399 notify->event_count = 0;
401 if (del)
403 list_remove( &notify->entry );
404 free( notify );
408 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
410 struct notify *notify;
412 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
414 if (notify->process == process && notify->hkey == hkey) return notify;
416 return NULL;
419 static unsigned int key_map_access( struct object *obj, unsigned int access )
421 access = default_map_access( obj, access );
422 /* filter the WOW64 masks, as they aren't real access bits */
423 return access & ~(KEY_WOW64_64KEY | KEY_WOW64_32KEY);
426 static struct security_descriptor *key_get_sd( struct object *obj )
428 static struct security_descriptor *key_default_sd;
430 if (obj->sd) return obj->sd;
432 if (!key_default_sd)
434 struct acl *dacl;
435 struct ace *ace;
436 struct sid *sid;
437 size_t users_sid_len = sid_len( &builtin_users_sid );
438 size_t admins_sid_len = sid_len( &builtin_admins_sid );
439 size_t dacl_len = sizeof(*dacl) + 2 * sizeof(*ace) + users_sid_len + admins_sid_len;
441 key_default_sd = mem_alloc( sizeof(*key_default_sd) + 2 * admins_sid_len + dacl_len );
442 key_default_sd->control = SE_DACL_PRESENT;
443 key_default_sd->owner_len = admins_sid_len;
444 key_default_sd->group_len = admins_sid_len;
445 key_default_sd->sacl_len = 0;
446 key_default_sd->dacl_len = dacl_len;
447 sid = (struct sid *)(key_default_sd + 1);
448 sid = copy_sid( sid, &builtin_admins_sid );
449 sid = copy_sid( sid, &builtin_admins_sid );
451 dacl = (struct acl *)((char *)(key_default_sd + 1) + 2 * admins_sid_len);
452 dacl->revision = ACL_REVISION;
453 dacl->pad1 = 0;
454 dacl->size = dacl_len;
455 dacl->count = 2;
456 dacl->pad2 = 0;
457 ace = set_ace( ace_first( dacl ), &builtin_users_sid, ACCESS_ALLOWED_ACE_TYPE,
458 INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE, GENERIC_READ );
459 set_ace( ace_next( ace ), &builtin_admins_sid, ACCESS_ALLOWED_ACE_TYPE, 0, KEY_ALL_ACCESS );
461 return key_default_sd;
464 static WCHAR *key_get_full_name( struct object *obj, data_size_t *ret_len )
466 struct key *key = (struct key *) obj;
468 if (key->flags & KEY_DELETED)
470 set_error( STATUS_KEY_DELETED );
471 return NULL;
473 return default_get_full_name( obj, ret_len );
476 static struct object *key_lookup_name( struct object *obj, struct unicode_str *name,
477 unsigned int attr, struct object *root )
479 struct key *found, *key = (struct key *)obj;
480 struct unicode_str tmp;
481 data_size_t next;
482 int index;
484 assert( obj->ops == &key_ops );
486 if (!name) return NULL; /* open the key itself */
488 if (key->flags & KEY_DELETED)
490 set_error( STATUS_KEY_DELETED );
491 return NULL;
494 if (key->flags & KEY_SYMLINK)
496 struct unicode_str name_left;
497 struct key_value *value;
499 if (!name->len && (attr & OBJ_OPENLINK)) return NULL;
501 if (!(value = find_value( key, &symlink_str, &index )) ||
502 value->len < sizeof(WCHAR) || *(WCHAR *)value->data != '\\')
504 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
505 return NULL;
507 tmp.str = value->data;
508 tmp.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
509 if ((obj = lookup_named_object( NULL, &tmp, OBJ_CASE_INSENSITIVE, &name_left )))
511 if (!name->len) *name = name_left; /* symlink destination can be created if missing */
512 else if (name_left.len) /* symlink must have been resolved completely */
514 release_object( obj );
515 obj = NULL;
516 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
519 return obj;
522 if (!name->str) return NULL;
524 tmp.str = name->str;
525 tmp.len = get_path_element( name->str, name->len );
527 if (tmp.len > MAX_NAME_LEN * sizeof(WCHAR))
529 set_error( STATUS_INVALID_PARAMETER );
530 return 0;
533 /* skip trailing backslashes */
534 for (next = tmp.len; next < name->len; next += sizeof(WCHAR))
535 if (name->str[next / sizeof(WCHAR)] != '\\') break;
537 if (!(found = find_subkey( key, &tmp, &index )))
539 if ((key->flags & KEY_WOWSHARE) && (attr & OBJ_KEY_WOW64))
541 /* try in the 64-bit parent */
542 key = get_parent( key );
543 if (!(found = find_subkey( key, &tmp, &index ))) return grab_object( key );
547 if (!found)
549 if (next < name->len) /* path still has elements */
550 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
551 else /* only trailing backslashes */
552 name->len = tmp.len;
553 return NULL;
556 if (next < name->len) /* move to the next element */
558 name->str += next / sizeof(WCHAR);
559 name->len -= next;
560 if ((attr & OBJ_KEY_WOW64) && found->wow6432node && !is_wow6432node( name->str, name->len ))
561 found = found->wow6432node;
563 else
565 name->str = NULL;
566 name->len = 0;
568 return grab_object( found );
571 static int key_link_name( struct object *obj, struct object_name *name, struct object *parent )
573 struct key *key = (struct key *)obj;
574 struct key *parent_key = (struct key *)parent;
575 struct unicode_str tmp;
576 int i, index;
578 if (parent->ops != &key_ops)
580 /* only the root key can be created inside a normal directory */
581 if (!root_key) return directory_link_name( obj, name, parent );
582 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
583 return 0;
586 if (parent_key->last_subkey + 1 == parent_key->nb_subkeys)
588 /* need to grow the array */
589 if (!grow_subkeys( parent_key )) return 0;
591 tmp.str = name->name;
592 tmp.len = name->len;
593 find_subkey( parent_key, &tmp, &index );
595 for (i = ++parent_key->last_subkey; i > index; i--)
596 parent_key->subkeys[i] = parent_key->subkeys[i - 1];
597 parent_key->subkeys[index] = (struct key *)grab_object( key );
598 if (is_wow6432node( name->name, name->len ) &&
599 !is_wow6432node( parent_key->obj.name->name, parent_key->obj.name->len ))
600 parent_key->wow6432node = key;
601 name->parent = parent;
602 return 1;
605 static void key_unlink_name( struct object *obj, struct object_name *name )
607 struct key *key = (struct key *)obj;
608 struct key *parent = (struct key *)name->parent;
609 int i, nb_subkeys;
611 if (!parent) return;
613 if (parent->obj.ops != &key_ops)
615 default_unlink_name( obj, name );
616 return;
619 for (i = 0; i <= parent->last_subkey; i++) if (parent->subkeys[i] == key) break;
620 assert( i <= parent->last_subkey );
621 for ( ; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
622 parent->last_subkey--;
623 name->parent = NULL;
624 if (parent->wow6432node == key) parent->wow6432node = NULL;
625 release_object( key );
627 /* try to shrink the array */
628 nb_subkeys = parent->nb_subkeys;
629 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
631 struct key **new_subkeys;
632 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
633 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
634 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
635 parent->subkeys = new_subkeys;
636 parent->nb_subkeys = nb_subkeys;
640 /* close the notification associated with a handle */
641 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
643 struct key * key = (struct key *) obj;
644 struct notify *notify = find_notify( key, process, handle );
645 if (notify) do_notification( key, notify, 1 );
646 return 1; /* ok to close */
649 static void key_destroy( struct object *obj )
651 int i;
652 struct list *ptr;
653 struct key *key = (struct key *)obj;
654 assert( obj->ops == &key_ops );
656 free( key->class );
657 for (i = 0; i <= key->last_value; i++)
659 free( key->values[i].name );
660 free( key->values[i].data );
662 free( key->values );
663 for (i = 0; i <= key->last_subkey; i++)
665 key->subkeys[i]->obj.name->parent = NULL;
666 release_object( key->subkeys[i] );
668 free( key->subkeys );
669 /* unconditionally notify everything waiting on this key */
670 while ((ptr = list_head( &key->notify_list )))
672 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
673 do_notification( key, notify, 1 );
677 /* allocate a key object */
678 static struct key *create_key_object( struct object *parent, const struct unicode_str *name,
679 unsigned int attributes, unsigned int options, timeout_t modif,
680 const struct security_descriptor *sd )
682 struct key *key;
684 if (!name->len) return open_named_object( parent, &key_ops, name, attributes );
686 if ((key = create_named_object( parent, &key_ops, name, attributes, sd )))
688 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
690 /* initialize it if it didn't already exist */
691 key->class = NULL;
692 key->classlen = 0;
693 key->flags = 0;
694 key->last_subkey = -1;
695 key->nb_subkeys = 0;
696 key->subkeys = NULL;
697 key->wow6432node = NULL;
698 key->nb_values = 0;
699 key->last_value = -1;
700 key->values = NULL;
701 key->modif = modif;
702 list_init( &key->notify_list );
704 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
705 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
706 else if (parent && (get_parent( key )->flags & KEY_VOLATILE))
708 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
709 unlink_named_object( &key->obj );
710 release_object( key );
711 return NULL;
713 else key->flags |= KEY_DIRTY;
716 return key;
719 /* mark a key and all its parents as dirty (modified) */
720 static void make_dirty( struct key *key )
722 while (key)
724 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
725 key->flags |= KEY_DIRTY;
726 key = get_parent( key );
730 /* mark a key and all its subkeys as clean (not modified) */
731 static void make_clean( struct key *key )
733 int i;
735 if (key->flags & KEY_VOLATILE) return;
736 if (!(key->flags & KEY_DIRTY)) return;
737 key->flags &= ~KEY_DIRTY;
738 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
741 /* go through all the notifications and send them if necessary */
742 static void check_notify( struct key *key, unsigned int change, int not_subtree )
744 struct list *ptr, *next;
746 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
748 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
749 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
750 do_notification( key, n, 0 );
754 /* update key modification time */
755 static void touch_key( struct key *key, unsigned int change )
757 key->modif = current_time;
758 make_dirty( key );
760 /* do notifications */
761 check_notify( key, change, 1 );
762 for (key = get_parent( key ); key; key = get_parent( key )) check_notify( key, change, 0 );
765 /* get the wow6432node key if any, grabbing it and releasing the original key */
766 static struct key *grab_wow6432node( struct key *key )
768 struct key *ret = key->wow6432node;
770 if (!ret) return key;
771 grab_object( ret );
772 release_object( key );
773 return ret;
776 /* open a subkey */
777 static struct key *open_key( struct key *parent, const struct unicode_str *name,
778 unsigned int access, unsigned int attributes )
780 struct key *key;
782 if (name->len >= 65534)
784 set_error( STATUS_OBJECT_NAME_INVALID );
785 return NULL;
788 if (parent && (access & KEY_WOW64_32KEY))
790 if (parent->wow6432node && !is_wow6432node( name->str, name->len ))
791 parent = parent->wow6432node;
794 if (!(access & KEY_WOW64_64KEY)) attributes |= OBJ_KEY_WOW64;
796 if (!(key = open_named_object( &parent->obj, &key_ops, name, attributes ))) return NULL;
798 if (!(access & KEY_WOW64_64KEY)) key = grab_wow6432node( key );
799 if (debug_level > 1) dump_operation( key, NULL, "Open" );
800 if (key->flags & KEY_PREDEF) set_error( STATUS_PREDEFINED_HANDLE );
801 return key;
804 /* create a subkey */
805 static struct key *create_key( struct key *parent, const struct unicode_str *name,
806 unsigned int options, unsigned int access, unsigned int attributes,
807 const struct security_descriptor *sd )
809 struct key *key;
811 if (options & REG_OPTION_CREATE_LINK) attributes = (attributes & ~OBJ_OPENIF) | OBJ_OPENLINK;
813 if (parent && (access & KEY_WOW64_32KEY))
815 if (parent->wow6432node && !is_wow6432node( name->str, name->len )) parent = parent->wow6432node;
818 if (!(access & KEY_WOW64_64KEY)) attributes |= OBJ_KEY_WOW64;
820 if (!(key = create_key_object( &parent->obj, name, attributes, options, current_time, sd )))
821 return NULL;
823 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
825 if (key->flags & KEY_PREDEF) set_error( STATUS_PREDEFINED_HANDLE );
826 if (!(access & KEY_WOW64_64KEY)) key = grab_wow6432node( key );
828 else
830 if (parent) touch_key( get_parent( key ), REG_NOTIFY_CHANGE_NAME );
831 if (debug_level > 1) dump_operation( key, NULL, "Create" );
833 return key;
836 /* recursively create a subkey (for internal use only) */
837 static struct key *create_key_recursive( struct key *root, const struct unicode_str *name, timeout_t modif )
839 struct key *key, *parent = (struct key *)grab_object( root );
840 struct unicode_str tmp;
841 const WCHAR *str = name->str;
842 data_size_t len = name->len;
844 while (len)
846 tmp.str = str;
847 tmp.len = get_path_element( str, len );
848 key = create_key_object( &parent->obj, &tmp, OBJ_OPENIF, 0, modif, NULL );
849 release_object( parent );
850 if (!key) return NULL;
851 parent = key;
853 /* skip trailing \\ and move to the next element */
854 if (tmp.len < len)
856 tmp.len += sizeof(WCHAR);
857 str += tmp.len / sizeof(WCHAR);
858 len -= tmp.len;
860 else break;
862 return parent;
865 /* query information about a key or a subkey */
866 static void enum_key( struct key *key, int index, int info_class, struct enum_key_reply *reply )
868 int i;
869 data_size_t len, namelen, classlen;
870 data_size_t max_subkey = 0, max_class = 0;
871 data_size_t max_value = 0, max_data = 0;
872 WCHAR *fullname = NULL;
873 char *data;
875 if (key->flags & KEY_PREDEF)
877 set_error( STATUS_INVALID_HANDLE );
878 return;
881 if (index != -1) /* -1 means use the specified key directly */
883 if ((index < 0) || (index > key->last_subkey))
885 set_error( STATUS_NO_MORE_ENTRIES );
886 return;
888 key = key->subkeys[index];
891 namelen = key->obj.name->len;
892 classlen = key->classlen;
894 switch(info_class)
896 case KeyNameInformation:
897 if (!(fullname = key->obj.ops->get_full_name( &key->obj, &namelen ))) return;
898 /* fall through */
899 case KeyBasicInformation:
900 classlen = 0; /* only return the name */
901 /* fall through */
902 case KeyNodeInformation:
903 reply->max_subkey = 0;
904 reply->max_class = 0;
905 reply->max_value = 0;
906 reply->max_data = 0;
907 break;
908 case KeyFullInformation:
909 case KeyCachedInformation:
910 for (i = 0; i <= key->last_subkey; i++)
912 if (key->subkeys[i]->obj.name->len > max_subkey) max_subkey = key->subkeys[i]->obj.name->len;
913 if (key->subkeys[i]->classlen > max_class) max_class = key->subkeys[i]->classlen;
915 for (i = 0; i <= key->last_value; i++)
917 if (key->values[i].namelen > max_value) max_value = key->values[i].namelen;
918 if (key->values[i].len > max_data) max_data = key->values[i].len;
920 reply->max_subkey = max_subkey;
921 reply->max_class = max_class;
922 reply->max_value = max_value;
923 reply->max_data = max_data;
924 reply->namelen = namelen;
925 if (info_class == KeyCachedInformation)
926 classlen = 0; /* don't return any data, only its size */
927 namelen = 0; /* don't return name */
928 break;
929 default:
930 set_error( STATUS_INVALID_PARAMETER );
931 return;
933 reply->subkeys = key->last_subkey + 1;
934 reply->values = key->last_value + 1;
935 reply->modif = key->modif;
936 reply->total = namelen + classlen;
938 len = min( reply->total, get_reply_max_size() );
939 if (len && (data = set_reply_data_size( len )))
941 if (len > namelen)
943 reply->namelen = namelen;
944 memcpy( data, key->obj.name->name, namelen );
945 memcpy( data + namelen, key->class, len - namelen );
947 else if (info_class == KeyNameInformation)
949 reply->namelen = namelen;
950 memcpy( data, fullname, len );
952 else
954 reply->namelen = len;
955 memcpy( data, key->obj.name->name, len );
958 free( fullname );
959 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
962 /* rename a key and its values */
963 static void rename_key( struct key *key, const struct unicode_str *new_name )
965 struct object_name *new_name_ptr;
966 struct key *subkey, *parent = get_parent( key );
967 data_size_t len;
968 int i, index, cur_index;
970 /* changing to a path is not allowed */
971 len = get_path_element( new_name->str, new_name->len );
972 if (!len || len != new_name->len || len > MAX_NAME_LEN * sizeof(WCHAR))
974 set_error( STATUS_INVALID_PARAMETER );
975 return;
978 /* check for existing subkey with the same name */
979 if (!parent || (subkey = find_subkey( parent, new_name, &index )))
981 set_error( STATUS_CANNOT_DELETE );
982 return;
985 if (key == parent->wow6432node || is_wow6432node( new_name->str, new_name->len ))
987 set_error( STATUS_INVALID_PARAMETER );
988 return;
991 if (!(new_name_ptr = mem_alloc( offsetof( struct object_name, name[new_name->len / sizeof(WCHAR)] ))))
992 return;
994 new_name_ptr->obj = &key->obj;
995 new_name_ptr->len = new_name->len;
996 new_name_ptr->parent = &parent->obj;
997 memcpy( new_name_ptr->name, new_name->str, new_name->len );
999 for (cur_index = 0; cur_index <= parent->last_subkey; cur_index++)
1000 if (parent->subkeys[cur_index] == key) break;
1002 if (cur_index < index && (index - cur_index) > 1)
1004 --index;
1005 for (i = cur_index; i < index; ++i) parent->subkeys[i] = parent->subkeys[i+1];
1007 else if (cur_index > index)
1009 for (i = cur_index; i > index; --i) parent->subkeys[i] = parent->subkeys[i-1];
1011 parent->subkeys[index] = key;
1013 free( key->obj.name );
1014 key->obj.name = new_name_ptr;
1016 if (debug_level > 1) dump_operation( key, NULL, "Rename" );
1017 touch_key( key, REG_NOTIFY_CHANGE_NAME );
1020 /* delete a key and its values */
1021 static int delete_key( struct key *key, int recurse )
1023 struct key *parent;
1025 if (key->flags & KEY_DELETED) return 1;
1027 if (!(parent = get_parent( key )))
1029 set_error( STATUS_ACCESS_DENIED );
1030 return 0;
1032 if (key->flags & KEY_PREDEF)
1034 set_error( STATUS_INVALID_HANDLE );
1035 return 0;
1038 if (recurse)
1040 while (key->last_subkey >= 0)
1041 if (!delete_key( key->subkeys[key->last_subkey], 1 )) return 0;
1043 else if (key->last_subkey >= 0) /* we can only delete a key that has no subkeys */
1045 set_error( STATUS_ACCESS_DENIED );
1046 return 0;
1049 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
1050 key->flags |= KEY_DELETED;
1051 unlink_named_object( &key->obj );
1052 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
1053 return 1;
1056 /* try to grow the array of values; return 1 if OK, 0 on error */
1057 static int grow_values( struct key *key )
1059 struct key_value *new_val;
1060 int nb_values;
1062 if (key->nb_values)
1064 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
1065 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
1067 set_error( STATUS_NO_MEMORY );
1068 return 0;
1071 else
1073 nb_values = MIN_VALUES;
1074 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
1076 key->values = new_val;
1077 key->nb_values = nb_values;
1078 return 1;
1081 /* find the named value of a given key and return its index in the array */
1082 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
1084 int i, min, max, res;
1085 data_size_t len;
1087 min = 0;
1088 max = key->last_value;
1089 while (min <= max)
1091 i = (min + max) / 2;
1092 len = min( key->values[i].namelen, name->len );
1093 res = memicmp_strW( key->values[i].name, name->str, len );
1094 if (!res) res = key->values[i].namelen - name->len;
1095 if (!res)
1097 *index = i;
1098 return &key->values[i];
1100 if (res > 0) max = i - 1;
1101 else min = i + 1;
1103 *index = min; /* this is where we should insert it */
1104 return NULL;
1107 /* insert a new value; the index must have been returned by find_value */
1108 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
1110 struct key_value *value;
1111 WCHAR *new_name = NULL;
1112 int i;
1114 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
1116 set_error( STATUS_NAME_TOO_LONG );
1117 return NULL;
1119 if (key->last_value + 1 == key->nb_values)
1121 if (!grow_values( key )) return NULL;
1123 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1124 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1125 value = &key->values[index];
1126 value->name = new_name;
1127 value->namelen = name->len;
1128 value->len = 0;
1129 value->data = NULL;
1130 return value;
1133 /* set a key value */
1134 static void set_value( struct key *key, const struct unicode_str *name,
1135 int type, const void *data, data_size_t len )
1137 struct key_value *value;
1138 void *ptr = NULL;
1139 int index;
1141 if (key->flags & KEY_PREDEF)
1143 set_error( STATUS_INVALID_HANDLE );
1144 return;
1147 if ((value = find_value( key, name, &index )))
1149 /* check if the new value is identical to the existing one */
1150 if (value->type == type && value->len == len &&
1151 value->data && !memcmp( value->data, data, len ))
1153 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1154 return;
1158 if (key->flags & KEY_SYMLINK)
1160 if (type != REG_LINK || name->len != symlink_str.len ||
1161 memicmp_strW( name->str, symlink_str.str, name->len ))
1163 set_error( STATUS_ACCESS_DENIED );
1164 return;
1168 if (len && !(ptr = memdup( data, len ))) return;
1170 if (!value)
1172 if (!(value = insert_value( key, name, index )))
1174 free( ptr );
1175 return;
1178 else free( value->data ); /* already existing, free previous data */
1180 value->type = type;
1181 value->len = len;
1182 value->data = ptr;
1183 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1184 if (debug_level > 1) dump_operation( key, value, "Set" );
1187 /* get a key value */
1188 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1190 struct key_value *value;
1191 int index;
1193 if (key->flags & KEY_PREDEF)
1195 set_error( STATUS_INVALID_HANDLE );
1196 return;
1199 if ((value = find_value( key, name, &index )))
1201 *type = value->type;
1202 *len = value->len;
1203 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1204 if (debug_level > 1) dump_operation( key, value, "Get" );
1206 else
1208 *type = -1;
1209 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1213 /* enumerate a key value */
1214 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1216 struct key_value *value;
1218 if (key->flags & KEY_PREDEF)
1220 set_error( STATUS_INVALID_HANDLE );
1221 return;
1224 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1225 else
1227 void *data;
1228 data_size_t namelen, maxlen;
1230 value = &key->values[i];
1231 reply->type = value->type;
1232 namelen = value->namelen;
1234 switch(info_class)
1236 case KeyValueBasicInformation:
1237 reply->total = namelen;
1238 break;
1239 case KeyValueFullInformation:
1240 reply->total = namelen + value->len;
1241 break;
1242 case KeyValuePartialInformation:
1243 reply->total = value->len;
1244 namelen = 0;
1245 break;
1246 default:
1247 set_error( STATUS_INVALID_PARAMETER );
1248 return;
1251 maxlen = min( reply->total, get_reply_max_size() );
1252 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1254 if (maxlen > namelen)
1256 reply->namelen = namelen;
1257 memcpy( data, value->name, namelen );
1258 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1260 else
1262 reply->namelen = maxlen;
1263 memcpy( data, value->name, maxlen );
1266 if (debug_level > 1) dump_operation( key, value, "Enum" );
1270 /* delete a value */
1271 static void delete_value( struct key *key, const struct unicode_str *name )
1273 struct key_value *value;
1274 int i, index, nb_values;
1276 if (key->flags & KEY_PREDEF)
1278 set_error( STATUS_INVALID_HANDLE );
1279 return;
1282 if (!(value = find_value( key, name, &index )))
1284 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1285 return;
1287 if (debug_level > 1) dump_operation( key, value, "Delete" );
1288 free( value->name );
1289 free( value->data );
1290 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1291 key->last_value--;
1292 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1294 /* try to shrink the array */
1295 nb_values = key->nb_values;
1296 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1298 struct key_value *new_val;
1299 nb_values -= nb_values / 3; /* shrink by 33% */
1300 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1301 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1302 key->values = new_val;
1303 key->nb_values = nb_values;
1307 /* get the registry key corresponding to an hkey handle */
1308 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1310 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1312 if (key && key->flags & KEY_DELETED)
1314 set_error( STATUS_KEY_DELETED );
1315 release_object( key );
1316 key = NULL;
1318 return key;
1321 /* read a line from the input file */
1322 static int read_next_line( struct file_load_info *info )
1324 char *newbuf;
1325 int newlen, pos = 0;
1327 info->line++;
1328 for (;;)
1330 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1331 return (pos != 0); /* EOF */
1332 pos = strlen(info->buffer);
1333 if (info->buffer[pos-1] == '\n')
1335 /* got a full line */
1336 info->buffer[--pos] = 0;
1337 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1338 return 1;
1340 if (pos < info->len - 1) return 1; /* EOF but something was read */
1342 /* need to enlarge the buffer */
1343 newlen = info->len + info->len / 2;
1344 if (!(newbuf = realloc( info->buffer, newlen )))
1346 set_error( STATUS_NO_MEMORY );
1347 return -1;
1349 info->buffer = newbuf;
1350 info->len = newlen;
1354 /* make sure the temp buffer holds enough space */
1355 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1357 WCHAR *tmp;
1358 if (info->tmplen >= size) return 1;
1359 if (!(tmp = realloc( info->tmp, size )))
1361 set_error( STATUS_NO_MEMORY );
1362 return 0;
1364 info->tmp = tmp;
1365 info->tmplen = size;
1366 return 1;
1369 /* report an error while loading an input file */
1370 static void file_read_error( const char *err, struct file_load_info *info )
1372 if (info->filename)
1373 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1374 else
1375 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1378 /* convert a data type tag to a value type */
1379 static int get_data_type( const char *buffer, int *type, int *parse_type )
1381 struct data_type { const char *tag; int len; int type; int parse_type; };
1383 static const struct data_type data_types[] =
1384 { /* actual type */ /* type to assume for parsing */
1385 { "\"", 1, REG_SZ, REG_SZ },
1386 { "str:\"", 5, REG_SZ, REG_SZ },
1387 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1388 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1389 { "hex:", 4, REG_BINARY, REG_BINARY },
1390 { "dword:", 6, REG_DWORD, REG_DWORD },
1391 { "hex(", 4, -1, REG_BINARY },
1392 { NULL, 0, 0, 0 }
1395 const struct data_type *ptr;
1396 char *end;
1398 for (ptr = data_types; ptr->tag; ptr++)
1400 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1401 *parse_type = ptr->parse_type;
1402 if ((*type = ptr->type) != -1) return ptr->len;
1403 /* "hex(xx):" is special */
1404 *type = (int)strtoul( buffer + 4, &end, 16 );
1405 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1406 return end + 2 - buffer;
1408 return 0;
1411 /* load and create a key from the input file */
1412 static struct key *load_key( struct key *base, const char *buffer, int prefix_len,
1413 struct file_load_info *info, timeout_t *modif )
1415 WCHAR *p;
1416 struct unicode_str name;
1417 int res;
1418 unsigned int mod;
1419 data_size_t len;
1421 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1423 len = info->tmplen;
1424 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1426 file_read_error( "Malformed key", info );
1427 return NULL;
1429 if (sscanf( buffer + res, " %u", &mod ) == 1)
1430 *modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1431 else
1432 *modif = current_time;
1434 p = info->tmp;
1435 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1437 if (!*p)
1439 if (prefix_len > 1)
1441 file_read_error( "Malformed key", info );
1442 return NULL;
1444 /* empty key name, return base key */
1445 return (struct key *)grab_object( base );
1447 name.str = p;
1448 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1449 return create_key_recursive( base, &name, 0 );
1452 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1453 static void update_key_time( struct key *key, timeout_t modif )
1455 while (key && !key->modif)
1457 key->modif = modif;
1458 key = get_parent( key );
1462 /* load a global option from the input file */
1463 static int load_global_option( const char *buffer, struct file_load_info *info )
1465 const char *p;
1467 if (!strncmp( buffer, "#arch=", 6 ))
1469 enum prefix_type type;
1470 p = buffer + 6;
1471 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1472 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1473 else
1475 file_read_error( "Unknown architecture", info );
1476 set_error( STATUS_NOT_REGISTRY_FILE );
1477 return 0;
1479 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1480 else if (type != prefix_type)
1482 file_read_error( "Mismatched architecture", info );
1483 set_error( STATUS_NOT_REGISTRY_FILE );
1484 return 0;
1487 /* ignore unknown options */
1488 return 1;
1491 /* load a key option from the input file */
1492 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1494 const char *p;
1495 data_size_t len;
1497 if (!strncmp( buffer, "#time=", 6 ))
1499 timeout_t modif = 0;
1500 for (p = buffer + 6; *p; p++)
1502 if (*p >= '0' && *p <= '9') modif = (modif << 4) | (*p - '0');
1503 else if (*p >= 'A' && *p <= 'F') modif = (modif << 4) | (*p - 'A' + 10);
1504 else if (*p >= 'a' && *p <= 'f') modif = (modif << 4) | (*p - 'a' + 10);
1505 else break;
1507 update_key_time( key, modif );
1509 if (!strncmp( buffer, "#class=", 7 ))
1511 p = buffer + 7;
1512 if (*p++ != '"') return 0;
1513 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1514 len = info->tmplen;
1515 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1516 free( key->class );
1517 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1518 key->classlen = len;
1520 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1521 /* ignore unknown options */
1522 return 1;
1525 /* parse a comma-separated list of hex digits */
1526 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1528 const char *p = buffer;
1529 data_size_t count = 0;
1530 char *end;
1532 while (isxdigit(*p))
1534 unsigned int val = strtoul( p, &end, 16 );
1535 if (end == p || val > 0xff) return -1;
1536 if (count++ >= *len) return -1; /* dest buffer overflow */
1537 *dest++ = val;
1538 p = end;
1539 while (isspace(*p)) p++;
1540 if (*p == ',') p++;
1541 while (isspace(*p)) p++;
1543 *len = count;
1544 return p - buffer;
1547 /* parse a value name and create the corresponding value */
1548 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1549 struct file_load_info *info )
1551 struct key_value *value;
1552 struct unicode_str name;
1553 int index;
1555 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1556 name.str = info->tmp;
1557 name.len = info->tmplen;
1558 if (buffer[0] == '@')
1560 name.len = 0;
1561 *len = 1;
1563 else
1565 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1566 if (r == -1) goto error;
1567 *len = r + 1; /* for initial quote */
1568 name.len -= sizeof(WCHAR); /* terminating null */
1570 while (isspace(buffer[*len])) (*len)++;
1571 if (buffer[*len] != '=') goto error;
1572 (*len)++;
1573 while (isspace(buffer[*len])) (*len)++;
1574 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1575 return value;
1577 error:
1578 file_read_error( "Malformed value name", info );
1579 return NULL;
1582 /* load a value from the input file */
1583 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1585 DWORD dw;
1586 void *ptr, *newptr;
1587 int res, type, parse_type;
1588 data_size_t maxlen, len;
1589 struct key_value *value;
1591 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1592 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1593 buffer += len + res;
1595 switch(parse_type)
1597 case REG_SZ:
1598 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1599 len = info->tmplen;
1600 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1601 ptr = info->tmp;
1602 break;
1603 case REG_DWORD:
1604 dw = strtoul( buffer, NULL, 16 );
1605 ptr = &dw;
1606 len = sizeof(dw);
1607 break;
1608 case REG_BINARY: /* hex digits */
1609 len = 0;
1610 for (;;)
1612 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1613 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1614 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1615 len += maxlen;
1616 buffer += res;
1617 while (isspace(*buffer)) buffer++;
1618 if (!*buffer) break;
1619 if (*buffer != '\\') goto error;
1620 if (read_next_line( info) != 1) goto error;
1621 buffer = info->buffer;
1622 while (isspace(*buffer)) buffer++;
1624 ptr = info->tmp;
1625 break;
1626 default:
1627 assert(0);
1628 ptr = NULL; /* keep compiler quiet */
1629 break;
1632 if (!len) newptr = NULL;
1633 else if (!(newptr = memdup( ptr, len ))) return 0;
1635 free( value->data );
1636 value->data = newptr;
1637 value->len = len;
1638 value->type = type;
1639 return 1;
1641 error:
1642 file_read_error( "Malformed value", info );
1643 free( value->data );
1644 value->data = NULL;
1645 value->len = 0;
1646 value->type = REG_NONE;
1647 return 0;
1650 /* return the length (in path elements) of name that is part of the key name */
1651 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1652 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1654 WCHAR *p;
1655 int res;
1656 data_size_t len;
1658 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1660 len = info->tmplen;
1661 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1663 file_read_error( "Malformed key", info );
1664 return 0;
1666 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1667 len = (p - info->tmp) * sizeof(WCHAR);
1668 for (res = 1; key != root_key; res++)
1670 if (len == key->obj.name->len && !memicmp_strW( info->tmp, key->obj.name->name, len )) break;
1671 key = get_parent( key );
1673 if (key == root_key) res = 0; /* no matching name */
1674 return res;
1677 /* load all the keys from the input file */
1678 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1679 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1681 struct key *subkey = NULL;
1682 struct file_load_info info;
1683 timeout_t modif = current_time;
1684 char *p;
1686 info.filename = filename;
1687 info.file = f;
1688 info.len = 4;
1689 info.tmplen = 4;
1690 info.line = 0;
1691 if (!(info.buffer = mem_alloc( info.len ))) return;
1692 if (!(info.tmp = mem_alloc( info.tmplen )))
1694 free( info.buffer );
1695 return;
1698 if ((read_next_line( &info ) != 1) ||
1699 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1701 set_error( STATUS_NOT_REGISTRY_FILE );
1702 goto done;
1705 while (read_next_line( &info ) == 1)
1707 p = info.buffer;
1708 while (*p && isspace(*p)) p++;
1709 switch(*p)
1711 case '[': /* new key */
1712 if (subkey)
1714 update_key_time( subkey, modif );
1715 release_object( subkey );
1717 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1718 if (!(subkey = load_key( key, p + 1, prefix_len, &info, &modif )))
1719 file_read_error( "Error creating key", &info );
1720 break;
1721 case '@': /* default value */
1722 case '\"': /* value */
1723 if (subkey) load_value( subkey, p, &info );
1724 else file_read_error( "Value without key", &info );
1725 break;
1726 case '#': /* option */
1727 if (subkey) load_key_option( subkey, p, &info );
1728 else if (!load_global_option( p, &info )) goto done;
1729 break;
1730 case ';': /* comment */
1731 case 0: /* empty line */
1732 break;
1733 default:
1734 file_read_error( "Unrecognized input", &info );
1735 break;
1739 done:
1740 if (subkey)
1742 update_key_time( subkey, modif );
1743 release_object( subkey );
1745 free( info.buffer );
1746 free( info.tmp );
1749 /* load a part of the registry from a file */
1750 static void load_registry( struct key *key, obj_handle_t handle )
1752 struct file *file;
1753 int fd;
1755 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1756 fd = dup( get_file_unix_fd( file ) );
1757 release_object( file );
1758 if (fd != -1)
1760 FILE *f = fdopen( fd, "r" );
1761 if (f)
1763 load_keys( key, NULL, f, -1 );
1764 fclose( f );
1766 else file_set_error();
1770 /* load one of the initial registry files */
1771 static int load_init_registry_from_file( const char *filename, struct key *key )
1773 FILE *f;
1775 if ((f = fopen( filename, "r" )))
1777 load_keys( key, filename, f, 0 );
1778 fclose( f );
1779 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1781 fprintf( stderr, "%s is not a valid registry file\n", filename );
1782 return 1;
1786 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1788 save_branch_info[save_branch_count].path = filename;
1789 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1790 make_object_permanent( &key->obj );
1791 return (f != NULL);
1794 static WCHAR *format_user_registry_path( const struct sid *sid, struct unicode_str *path )
1796 char buffer[7 + 11 + 11 + 11 * ARRAY_SIZE(sid->sub_auth)], *p = buffer;
1797 unsigned int i;
1799 p += sprintf( p, "User\\S-%u-%u", sid->revision,
1800 ((unsigned int)sid->id_auth[2] << 24) |
1801 ((unsigned int)sid->id_auth[3] << 16) |
1802 ((unsigned int)sid->id_auth[4] << 8) |
1803 ((unsigned int)sid->id_auth[5]) );
1804 for (i = 0; i < sid->sub_count; i++) p += sprintf( p, "-%u", sid->sub_auth[i] );
1805 return ascii_to_unicode_str( buffer, path );
1808 static void init_supported_machines(void)
1810 unsigned int count = 0;
1811 #ifdef __i386__
1812 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1813 #elif defined(__x86_64__)
1814 if (prefix_type == PREFIX_64BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_AMD64;
1815 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1816 #elif defined(__arm__)
1817 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1818 #elif defined(__aarch64__)
1819 if (prefix_type == PREFIX_64BIT)
1821 supported_machines[count++] = IMAGE_FILE_MACHINE_ARM64;
1822 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1824 supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1825 #else
1826 #error Unsupported machine
1827 #endif
1828 supported_machines_count = count;
1829 native_machine = supported_machines[0];
1832 /* registry initialisation */
1833 void init_registry(void)
1835 static const WCHAR REGISTRY[] = {'\\','R','E','G','I','S','T','R','Y'};
1836 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1837 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1838 static const WCHAR classes_i386[] = {'S','o','f','t','w','a','r','e','\\',
1839 'C','l','a','s','s','e','s','\\',
1840 'W','o','w','6','4','3','2','N','o','d','e'};
1841 static const WCHAR classes_amd64[] = {'S','o','f','t','w','a','r','e','\\',
1842 'C','l','a','s','s','e','s','\\',
1843 'W','o','w','6','4','6','4','N','o','d','e'};
1844 static const WCHAR classes_arm[] = {'S','o','f','t','w','a','r','e','\\',
1845 'C','l','a','s','s','e','s','\\',
1846 'W','o','w','A','A','3','2','N','o','d','e'};
1847 static const WCHAR classes_arm64[] = {'S','o','f','t','w','a','r','e','\\',
1848 'C','l','a','s','s','e','s','\\',
1849 'W','o','w','A','A','6','4','N','o','d','e'};
1850 static const WCHAR perflib[] = {'S','o','f','t','w','a','r','e','\\',
1851 'M','i','c','r','o','s','o','f','t','\\',
1852 'W','i','n','d','o','w','s',' ','N','T','\\',
1853 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1854 'P','e','r','f','l','i','b','\\',
1855 '0','0','9'};
1856 static const struct unicode_str root_name = { REGISTRY, sizeof(REGISTRY) };
1857 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1858 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1859 static const struct unicode_str perflib_name = { perflib, sizeof(perflib) };
1861 WCHAR *current_user_path;
1862 struct unicode_str current_user_str;
1863 struct key *key, *hklm, *hkcu;
1864 unsigned int i;
1865 char *p;
1867 /* switch to the config dir */
1869 if (fchdir( config_dir_fd ) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno ));
1871 /* create the root key */
1872 root_key = create_key_object( NULL, &root_name, OBJ_PERMANENT, 0, current_time, NULL );
1873 assert( root_key );
1874 release_object( root_key );
1876 /* load system.reg into Registry\Machine */
1878 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1879 fatal_error( "could not create Machine registry key\n" );
1881 if (!load_init_registry_from_file( "system.reg", hklm ))
1883 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1884 prefix_type = PREFIX_32BIT;
1885 else
1886 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1888 else if (prefix_type == PREFIX_UNKNOWN)
1889 prefix_type = PREFIX_32BIT;
1891 init_supported_machines();
1893 /* load userdef.reg into Registry\User\.Default */
1895 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1896 fatal_error( "could not create User\\.Default registry key\n" );
1898 load_init_registry_from_file( "userdef.reg", key );
1899 release_object( key );
1901 /* load user.reg into HKEY_CURRENT_USER */
1903 /* FIXME: match default user in token.c. should get from process token instead */
1904 current_user_path = format_user_registry_path( &local_user_sid, &current_user_str );
1905 if (!current_user_path ||
1906 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1907 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1908 free( current_user_path );
1909 load_init_registry_from_file( "user.reg", hkcu );
1911 /* set the shared flag on Software\Classes\Wow6432Node for all platforms */
1912 for (i = 1; i < supported_machines_count; i++)
1914 struct unicode_str name;
1916 switch (supported_machines[i])
1918 case IMAGE_FILE_MACHINE_I386: name.str = classes_i386; name.len = sizeof(classes_i386); break;
1919 case IMAGE_FILE_MACHINE_ARMNT: name.str = classes_arm; name.len = sizeof(classes_arm); break;
1920 case IMAGE_FILE_MACHINE_AMD64: name.str = classes_amd64; name.len = sizeof(classes_amd64); break;
1921 case IMAGE_FILE_MACHINE_ARM64: name.str = classes_arm64; name.len = sizeof(classes_arm64); break;
1923 if ((key = create_key_recursive( hklm, &name, current_time )))
1925 key->flags |= KEY_WOWSHARE;
1926 release_object( key );
1928 /* FIXME: handle HKCU too */
1931 if ((key = create_key_recursive( hklm, &perflib_name, current_time )))
1933 key->flags |= KEY_PREDEF;
1934 release_object( key );
1937 release_object( hklm );
1938 release_object( hkcu );
1940 /* start the periodic save timer */
1941 set_periodic_save_timer();
1943 /* create windows directories */
1945 if (!mkdir( "drive_c/windows", 0777 ))
1947 mkdir( "drive_c/windows/system32", 0777 );
1948 for (i = 1; i < supported_machines_count; i++)
1950 switch (supported_machines[i])
1952 case IMAGE_FILE_MACHINE_I386: mkdir( "drive_c/windows/syswow64", 0777 ); break;
1953 case IMAGE_FILE_MACHINE_ARMNT: mkdir( "drive_c/windows/sysarm32", 0777 ); break;
1954 case IMAGE_FILE_MACHINE_AMD64: mkdir( "drive_c/windows/sysx8664", 0777 ); break;
1955 case IMAGE_FILE_MACHINE_ARM64: mkdir( "drive_c/windows/sysarm64", 0777 ); break;
1960 /* go back to the server dir */
1961 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
1964 /* save a registry branch to a file */
1965 static void save_all_subkeys( struct key *key, FILE *f )
1967 fprintf( f, "WINE REGISTRY Version 2\n" );
1968 fprintf( f, ";; All keys relative to " );
1969 dump_path( key, NULL, f );
1970 fprintf( f, "\n" );
1971 switch (prefix_type)
1973 case PREFIX_32BIT:
1974 fprintf( f, "\n#arch=win32\n" );
1975 break;
1976 case PREFIX_64BIT:
1977 fprintf( f, "\n#arch=win64\n" );
1978 break;
1979 default:
1980 break;
1982 save_subkeys( key, key, f );
1985 /* save a registry branch to a file handle */
1986 static void save_registry( struct key *key, obj_handle_t handle )
1988 struct file *file;
1989 int fd;
1991 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1992 fd = dup( get_file_unix_fd( file ) );
1993 release_object( file );
1994 if (fd != -1)
1996 FILE *f = fdopen( fd, "w" );
1997 if (f)
1999 save_all_subkeys( key, f );
2000 if (fclose( f )) file_set_error();
2002 else
2004 file_set_error();
2005 close( fd );
2010 /* save a registry branch to a file */
2011 static int save_branch( struct key *key, const char *path )
2013 struct stat st;
2014 char *p, *tmp = NULL;
2015 int fd, count = 0, ret = 0;
2016 FILE *f;
2018 if (!(key->flags & KEY_DIRTY))
2020 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
2021 return 1;
2024 /* test the file type */
2026 if ((fd = open( path, O_WRONLY )) != -1)
2028 /* if file is not a regular file or has multiple links or is accessed
2029 * via symbolic links, write directly into it; otherwise use a temp file */
2030 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
2032 ftruncate( fd, 0 );
2033 goto save;
2035 close( fd );
2038 /* create a temp file in the same directory */
2040 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
2041 strcpy( tmp, path );
2042 if ((p = strrchr( tmp, '/' ))) p++;
2043 else p = tmp;
2044 for (;;)
2046 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
2047 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
2048 if (errno != EEXIST) goto done;
2049 close( fd );
2052 /* now save to it */
2054 save:
2055 if (!(f = fdopen( fd, "w" )))
2057 if (tmp) unlink( tmp );
2058 close( fd );
2059 goto done;
2062 if (debug_level > 1)
2064 fprintf( stderr, "%s: ", path );
2065 dump_operation( key, NULL, "saving" );
2068 save_all_subkeys( key, f );
2069 ret = !fclose(f);
2071 if (tmp)
2073 /* if successfully written, rename to final name */
2074 if (ret) ret = !rename( tmp, path );
2075 if (!ret) unlink( tmp );
2078 done:
2079 free( tmp );
2080 if (ret) make_clean( key );
2081 return ret;
2084 /* periodic saving of the registry */
2085 static void periodic_save( void *arg )
2087 int i;
2089 if (fchdir( config_dir_fd ) == -1) return;
2090 save_timeout_user = NULL;
2091 for (i = 0; i < save_branch_count; i++)
2092 save_branch( save_branch_info[i].key, save_branch_info[i].path );
2093 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2094 set_periodic_save_timer();
2097 /* start the periodic save timer */
2098 static void set_periodic_save_timer(void)
2100 if (save_timeout_user) remove_timeout_user( save_timeout_user );
2101 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
2104 /* save the modified registry branches to disk */
2105 void flush_registry(void)
2107 int i;
2109 if (fchdir( config_dir_fd ) == -1) return;
2110 for (i = 0; i < save_branch_count; i++)
2112 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
2114 fprintf( stderr, "wineserver: could not save registry branch to %s",
2115 save_branch_info[i].path );
2116 perror( " " );
2119 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2122 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2123 static int is_wow64_thread( struct thread *thread )
2125 return (is_machine_64bit( native_machine ) && !is_machine_64bit( thread->process->machine ));
2129 /* create a registry key */
2130 DECL_HANDLER(create_key)
2132 struct key *key, *parent = NULL;
2133 unsigned int access = req->access;
2134 const WCHAR *class;
2135 struct unicode_str name;
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 if (objattr->rootdir)
2145 if (!(parent = get_hkey_obj( objattr->rootdir, 0 ))) return;
2148 if ((key = create_key( parent, &name, req->options, access, objattr->attributes, sd )))
2150 if ((class = get_req_data_after_objattr( objattr, &key->classlen )))
2152 key->classlen = (key->classlen / sizeof(WCHAR)) * sizeof(WCHAR);
2153 if (!(key->class = memdup( class, key->classlen ))) key->classlen = 0;
2155 reply->hkey = alloc_handle( current->process, key, access, objattr->attributes );
2156 release_object( key );
2158 if (parent) release_object( parent );
2161 /* open a registry key */
2162 DECL_HANDLER(open_key)
2164 struct key *key, *parent = NULL;
2165 unsigned int access = req->access;
2166 struct unicode_str name = get_req_unicode_str();
2168 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2170 if (req->parent && !(parent = get_hkey_obj( req->parent, 0 ))) return;
2172 if ((key = open_key( parent, &name, access, req->attributes )))
2174 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
2175 release_object( key );
2177 if (parent) release_object( parent );
2180 /* delete a registry key */
2181 DECL_HANDLER(delete_key)
2183 struct key *key = (struct key *)get_handle_obj( current->process, req->hkey, DELETE, &key_ops );
2185 if (key)
2187 delete_key( key, 0 );
2188 release_object( key );
2192 /* flush a registry key */
2193 DECL_HANDLER(flush_key)
2195 struct key *key = get_hkey_obj( req->hkey, 0 );
2196 if (key)
2198 /* we don't need to do anything here with the current implementation */
2199 release_object( key );
2203 /* enumerate registry subkeys */
2204 DECL_HANDLER(enum_key)
2206 struct key *key;
2208 if ((key = get_hkey_obj( req->hkey, req->index == -1 ? 0 : KEY_ENUMERATE_SUB_KEYS )))
2210 enum_key( key, req->index, req->info_class, reply );
2211 release_object( key );
2215 /* set a value of a registry key */
2216 DECL_HANDLER(set_key_value)
2218 struct key *key;
2219 struct unicode_str name;
2221 if (req->namelen > get_req_data_size())
2223 set_error( STATUS_INVALID_PARAMETER );
2224 return;
2226 name.str = get_req_data();
2227 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2229 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2231 data_size_t datalen = get_req_data_size() - req->namelen;
2232 const char *data = (const char *)get_req_data() + req->namelen;
2234 set_value( key, &name, req->type, data, datalen );
2235 release_object( key );
2239 /* retrieve the value of a registry key */
2240 DECL_HANDLER(get_key_value)
2242 struct key *key;
2243 struct unicode_str name = get_req_unicode_str();
2245 reply->total = 0;
2246 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2248 get_value( key, &name, &reply->type, &reply->total );
2249 release_object( key );
2253 /* enumerate the value of a registry key */
2254 DECL_HANDLER(enum_key_value)
2256 struct key *key;
2258 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2260 enum_value( key, req->index, req->info_class, reply );
2261 release_object( key );
2265 /* delete a value of a registry key */
2266 DECL_HANDLER(delete_key_value)
2268 struct key *key;
2269 struct unicode_str name = get_req_unicode_str();
2271 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2273 delete_value( key, &name );
2274 release_object( key );
2278 /* load a registry branch from a file */
2279 DECL_HANDLER(load_registry)
2281 struct key *key, *parent = NULL;
2282 struct unicode_str name;
2283 const struct security_descriptor *sd;
2284 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2286 if (!objattr) return;
2288 if (!thread_single_check_privilege( current, SeRestorePrivilege ))
2290 set_error( STATUS_PRIVILEGE_NOT_HELD );
2291 return;
2293 if (objattr->rootdir)
2295 if (!(parent = get_hkey_obj( objattr->rootdir, 0 ))) return;
2298 if ((key = create_key( parent, &name, 0, KEY_WOW64_64KEY, 0, sd )))
2300 load_registry( key, req->file );
2301 release_object( key );
2303 if (parent) release_object( parent );
2306 DECL_HANDLER(unload_registry)
2308 struct key *key, *parent = NULL;
2309 struct unicode_str name = get_req_unicode_str();
2311 if (!thread_single_check_privilege( current, SeRestorePrivilege ))
2313 set_error( STATUS_PRIVILEGE_NOT_HELD );
2314 return;
2317 if (req->parent && !(parent = get_hkey_obj( req->parent, 0 ))) return;
2319 if ((key = open_key( parent, &name, KEY_WOW64_64KEY, req->attributes )))
2321 if (key->obj.handle_count)
2322 set_error( STATUS_CANNOT_DELETE );
2323 else
2324 delete_key( key, 1 ); /* FIXME */
2325 release_object( key );
2327 if (parent) release_object( parent );
2330 /* save a registry branch to a file */
2331 DECL_HANDLER(save_registry)
2333 struct key *key;
2335 if (!thread_single_check_privilege( current, SeBackupPrivilege ))
2337 set_error( STATUS_PRIVILEGE_NOT_HELD );
2338 return;
2341 if ((key = get_hkey_obj( req->hkey, 0 )))
2343 save_registry( key, req->file );
2344 release_object( key );
2348 /* add a registry key change notification */
2349 DECL_HANDLER(set_registry_notification)
2351 struct key *key;
2352 struct event *event;
2353 struct notify *notify;
2355 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2356 if (key)
2358 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2359 if (event)
2361 notify = find_notify( key, current->process, req->hkey );
2362 if (!notify)
2364 notify = mem_alloc( sizeof(*notify) );
2365 if (notify)
2367 notify->events = NULL;
2368 notify->event_count = 0;
2369 notify->subtree = req->subtree;
2370 notify->filter = req->filter;
2371 notify->hkey = req->hkey;
2372 notify->process = current->process;
2373 list_add_head( &key->notify_list, &notify->entry );
2376 if (notify)
2378 struct event **new_array;
2380 if ((new_array = realloc( notify->events, (notify->event_count + 1) * sizeof(*notify->events) )))
2382 notify->events = new_array;
2383 notify->events[notify->event_count++] = (struct event *)grab_object( event );
2384 reset_event( event );
2385 set_error( STATUS_PENDING );
2387 else set_error( STATUS_NO_MEMORY );
2389 release_object( event );
2391 release_object( key );
2395 /* rename a registry key */
2396 DECL_HANDLER(rename_key)
2398 struct unicode_str name = get_req_unicode_str();
2399 struct key *key;
2401 key = get_hkey_obj( req->hkey, KEY_WRITE );
2402 if (key)
2404 rename_key( key, &name );
2405 release_object( key );