winewayland.drv: Implement SetCursor using cursor bitmap data.
[wine.git] / server / registry.c
blob3fea7dd79a48ea9bfa79912e405cfcfbb351a4f8
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 );
520 key = (struct key *)obj;
521 if (key && (key->flags & KEY_WOWSHARE) && (attr & OBJ_KEY_WOW64) && !name->str)
523 key = get_parent( key );
524 release_object( obj );
525 return grab_object( key );
528 return obj;
531 if (!name->str) return NULL;
533 tmp.str = name->str;
534 tmp.len = get_path_element( name->str, name->len );
536 if (tmp.len > MAX_NAME_LEN * sizeof(WCHAR))
538 set_error( STATUS_INVALID_PARAMETER );
539 return 0;
542 /* skip trailing backslashes */
543 for (next = tmp.len; next < name->len; next += sizeof(WCHAR))
544 if (name->str[next / sizeof(WCHAR)] != '\\') break;
546 if (!(found = find_subkey( key, &tmp, &index )))
548 if ((key->flags & KEY_WOWSHARE) && (attr & OBJ_KEY_WOW64))
550 /* try in the 64-bit parent */
551 key = get_parent( key );
552 if (!(found = find_subkey( key, &tmp, &index ))) return grab_object( key );
556 if (!found)
558 if (next < name->len) /* path still has elements */
559 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
560 else /* only trailing backslashes */
561 name->len = tmp.len;
562 return NULL;
565 if (next < name->len) /* move to the next element */
567 name->str += next / sizeof(WCHAR);
568 name->len -= next;
569 if ((attr & OBJ_KEY_WOW64) && found->wow6432node && !is_wow6432node( name->str, name->len ))
570 found = found->wow6432node;
572 else
574 name->str = NULL;
575 name->len = 0;
577 return grab_object( found );
580 static int key_link_name( struct object *obj, struct object_name *name, struct object *parent )
582 struct key *key = (struct key *)obj;
583 struct key *parent_key = (struct key *)parent;
584 struct unicode_str tmp;
585 int i, index;
587 if (parent->ops != &key_ops)
589 /* only the root key can be created inside a normal directory */
590 if (!root_key) return directory_link_name( obj, name, parent );
591 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
592 return 0;
595 if (parent_key->last_subkey + 1 == parent_key->nb_subkeys)
597 /* need to grow the array */
598 if (!grow_subkeys( parent_key )) return 0;
600 tmp.str = name->name;
601 tmp.len = name->len;
602 find_subkey( parent_key, &tmp, &index );
604 for (i = ++parent_key->last_subkey; i > index; i--)
605 parent_key->subkeys[i] = parent_key->subkeys[i - 1];
606 parent_key->subkeys[index] = (struct key *)grab_object( key );
607 if (is_wow6432node( name->name, name->len ) &&
608 !is_wow6432node( parent_key->obj.name->name, parent_key->obj.name->len ))
609 parent_key->wow6432node = key;
610 name->parent = parent;
611 return 1;
614 static void key_unlink_name( struct object *obj, struct object_name *name )
616 struct key *key = (struct key *)obj;
617 struct key *parent = (struct key *)name->parent;
618 int i, nb_subkeys;
620 if (!parent) return;
622 if (parent->obj.ops != &key_ops)
624 default_unlink_name( obj, name );
625 return;
628 for (i = 0; i <= parent->last_subkey; i++) if (parent->subkeys[i] == key) break;
629 assert( i <= parent->last_subkey );
630 for ( ; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
631 parent->last_subkey--;
632 name->parent = NULL;
633 if (parent->wow6432node == key) parent->wow6432node = NULL;
634 release_object( key );
636 /* try to shrink the array */
637 nb_subkeys = parent->nb_subkeys;
638 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
640 struct key **new_subkeys;
641 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
642 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
643 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
644 parent->subkeys = new_subkeys;
645 parent->nb_subkeys = nb_subkeys;
649 /* close the notification associated with a handle */
650 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
652 struct key * key = (struct key *) obj;
653 struct notify *notify = find_notify( key, process, handle );
654 if (notify) do_notification( key, notify, 1 );
655 return 1; /* ok to close */
658 static void key_destroy( struct object *obj )
660 int i;
661 struct list *ptr;
662 struct key *key = (struct key *)obj;
663 assert( obj->ops == &key_ops );
665 free( key->class );
666 for (i = 0; i <= key->last_value; i++)
668 free( key->values[i].name );
669 free( key->values[i].data );
671 free( key->values );
672 for (i = 0; i <= key->last_subkey; i++)
674 key->subkeys[i]->obj.name->parent = NULL;
675 release_object( key->subkeys[i] );
677 free( key->subkeys );
678 /* unconditionally notify everything waiting on this key */
679 while ((ptr = list_head( &key->notify_list )))
681 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
682 do_notification( key, notify, 1 );
686 /* allocate a key object */
687 static struct key *create_key_object( struct object *parent, const struct unicode_str *name,
688 unsigned int attributes, unsigned int options, timeout_t modif,
689 const struct security_descriptor *sd )
691 struct key *key;
693 if (!name->len) return open_named_object( parent, &key_ops, name, attributes );
695 if ((key = create_named_object( parent, &key_ops, name, attributes, sd )))
697 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
699 /* initialize it if it didn't already exist */
700 key->class = NULL;
701 key->classlen = 0;
702 key->flags = 0;
703 key->last_subkey = -1;
704 key->nb_subkeys = 0;
705 key->subkeys = NULL;
706 key->wow6432node = NULL;
707 key->nb_values = 0;
708 key->last_value = -1;
709 key->values = NULL;
710 key->modif = modif;
711 list_init( &key->notify_list );
713 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
714 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
715 else if (parent && (get_parent( key )->flags & KEY_VOLATILE))
717 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
718 unlink_named_object( &key->obj );
719 release_object( key );
720 return NULL;
722 else key->flags |= KEY_DIRTY;
725 return key;
728 /* mark a key and all its parents as dirty (modified) */
729 static void make_dirty( struct key *key )
731 while (key)
733 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
734 key->flags |= KEY_DIRTY;
735 key = get_parent( key );
739 /* mark a key and all its subkeys as clean (not modified) */
740 static void make_clean( struct key *key )
742 int i;
744 if (key->flags & KEY_VOLATILE) return;
745 if (!(key->flags & KEY_DIRTY)) return;
746 key->flags &= ~KEY_DIRTY;
747 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
750 /* go through all the notifications and send them if necessary */
751 static void check_notify( struct key *key, unsigned int change, int not_subtree )
753 struct list *ptr, *next;
755 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
757 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
758 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
759 do_notification( key, n, 0 );
763 /* update key modification time */
764 static void touch_key( struct key *key, unsigned int change )
766 key->modif = current_time;
767 make_dirty( key );
769 /* do notifications */
770 check_notify( key, change, 1 );
771 for (key = get_parent( key ); key; key = get_parent( key )) check_notify( key, change, 0 );
774 /* get the wow6432node key if any, grabbing it and releasing the original key */
775 static struct key *grab_wow6432node( struct key *key )
777 struct key *ret = key->wow6432node;
779 if (!ret) return key;
780 if (ret->flags & KEY_WOWSHARE) return key;
781 grab_object( ret );
782 release_object( key );
783 return ret;
786 /* recursively obtain the wow6432node parent key if any */
787 static struct key *get_wow6432node( struct key *key )
789 struct key *parent, *ret;
790 struct unicode_str name;
791 int index;
793 if (!key)
794 return NULL;
796 if (key->wow6432node)
797 return key->wow6432node;
799 parent = get_parent( key );
800 if (parent && key == parent->wow6432node)
801 return key;
803 if (!(ret = get_wow6432node( parent )))
804 return key;
806 name.str = key->obj.name->name;
807 name.len = key->obj.name->len;
808 return find_subkey( ret, &name, &index );
811 /* open a subkey */
812 static struct key *open_key( struct key *parent, const struct unicode_str *name,
813 unsigned int access, unsigned int attributes )
815 struct key *key;
817 if (name->len >= 65534)
819 set_error( STATUS_OBJECT_NAME_INVALID );
820 return NULL;
823 if (parent && !(access & KEY_WOW64_64KEY) && !is_wow6432node( name->str, name->len ))
825 key = get_wow6432node( parent );
826 if (key && ((access & KEY_WOW64_32KEY) || (key->flags & KEY_WOWSHARE)))
827 parent = key;
830 if (!(access & KEY_WOW64_64KEY)) attributes |= OBJ_KEY_WOW64;
832 if (!(key = open_named_object( &parent->obj, &key_ops, name, attributes ))) return NULL;
834 if (!(access & KEY_WOW64_64KEY)) key = grab_wow6432node( key );
835 if (debug_level > 1) dump_operation( key, NULL, "Open" );
836 if (key->flags & KEY_PREDEF) set_error( STATUS_PREDEFINED_HANDLE );
837 return key;
840 /* create a subkey */
841 static struct key *create_key( struct key *parent, const struct unicode_str *name,
842 unsigned int options, unsigned int access, unsigned int attributes,
843 const struct security_descriptor *sd )
845 struct key *key;
847 if (options & REG_OPTION_CREATE_LINK) attributes = (attributes & ~OBJ_OPENIF) | OBJ_OPENLINK;
849 if (parent && !(access & KEY_WOW64_64KEY) && !is_wow6432node( name->str, name->len ))
851 key = get_wow6432node( parent );
852 if (key && ((access & KEY_WOW64_32KEY) || (key->flags & KEY_WOWSHARE)))
853 parent = key;
856 if (!(access & KEY_WOW64_64KEY)) attributes |= OBJ_KEY_WOW64;
858 if (!(key = create_key_object( &parent->obj, name, attributes, options, current_time, sd )))
859 return NULL;
861 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
863 if (key->flags & KEY_PREDEF) set_error( STATUS_PREDEFINED_HANDLE );
864 if (!(access & KEY_WOW64_64KEY)) key = grab_wow6432node( key );
866 else
868 if (parent) touch_key( get_parent( key ), REG_NOTIFY_CHANGE_NAME );
869 if (debug_level > 1) dump_operation( key, NULL, "Create" );
871 return key;
874 /* recursively create a subkey (for internal use only) */
875 static struct key *create_key_recursive( struct key *root, const struct unicode_str *name, timeout_t modif )
877 struct key *key, *parent = (struct key *)grab_object( root );
878 struct unicode_str tmp;
879 const WCHAR *str = name->str;
880 data_size_t len = name->len;
882 while (len)
884 tmp.str = str;
885 tmp.len = get_path_element( str, len );
886 key = create_key_object( &parent->obj, &tmp, OBJ_OPENIF, 0, modif, NULL );
887 release_object( parent );
888 if (!key) return NULL;
889 parent = key;
891 /* skip trailing \\ and move to the next element */
892 if (tmp.len < len)
894 tmp.len += sizeof(WCHAR);
895 str += tmp.len / sizeof(WCHAR);
896 len -= tmp.len;
898 else break;
900 return parent;
903 /* query information about a key or a subkey */
904 static void enum_key( struct key *key, int index, int info_class, struct enum_key_reply *reply )
906 int i;
907 data_size_t len, namelen, classlen;
908 data_size_t max_subkey = 0, max_class = 0;
909 data_size_t max_value = 0, max_data = 0;
910 WCHAR *fullname = NULL;
911 char *data;
913 if (key->flags & KEY_PREDEF)
915 set_error( STATUS_INVALID_HANDLE );
916 return;
919 if (index != -1) /* -1 means use the specified key directly */
921 if ((index < 0) || (index > key->last_subkey))
923 set_error( STATUS_NO_MORE_ENTRIES );
924 return;
926 key = key->subkeys[index];
929 namelen = key->obj.name->len;
930 classlen = key->classlen;
932 switch(info_class)
934 case KeyNameInformation:
935 if (!(fullname = key->obj.ops->get_full_name( &key->obj, &namelen ))) return;
936 /* fall through */
937 case KeyBasicInformation:
938 classlen = 0; /* only return the name */
939 /* fall through */
940 case KeyNodeInformation:
941 reply->max_subkey = 0;
942 reply->max_class = 0;
943 reply->max_value = 0;
944 reply->max_data = 0;
945 break;
946 case KeyFullInformation:
947 case KeyCachedInformation:
948 for (i = 0; i <= key->last_subkey; i++)
950 if (key->subkeys[i]->obj.name->len > max_subkey) max_subkey = key->subkeys[i]->obj.name->len;
951 if (key->subkeys[i]->classlen > max_class) max_class = key->subkeys[i]->classlen;
953 for (i = 0; i <= key->last_value; i++)
955 if (key->values[i].namelen > max_value) max_value = key->values[i].namelen;
956 if (key->values[i].len > max_data) max_data = key->values[i].len;
958 reply->max_subkey = max_subkey;
959 reply->max_class = max_class;
960 reply->max_value = max_value;
961 reply->max_data = max_data;
962 reply->namelen = namelen;
963 if (info_class == KeyCachedInformation)
964 classlen = 0; /* don't return any data, only its size */
965 namelen = 0; /* don't return name */
966 break;
967 default:
968 set_error( STATUS_INVALID_PARAMETER );
969 return;
971 reply->subkeys = key->last_subkey + 1;
972 reply->values = key->last_value + 1;
973 reply->modif = key->modif;
974 reply->total = namelen + classlen;
976 len = min( reply->total, get_reply_max_size() );
977 if (len && (data = set_reply_data_size( len )))
979 if (len > namelen)
981 reply->namelen = namelen;
982 memcpy( data, key->obj.name->name, namelen );
983 memcpy( data + namelen, key->class, len - namelen );
985 else if (info_class == KeyNameInformation)
987 reply->namelen = namelen;
988 memcpy( data, fullname, len );
990 else
992 reply->namelen = len;
993 memcpy( data, key->obj.name->name, len );
996 free( fullname );
997 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
1000 /* rename a key and its values */
1001 static void rename_key( struct key *key, const struct unicode_str *new_name )
1003 struct object_name *new_name_ptr;
1004 struct key *subkey, *parent = get_parent( key );
1005 data_size_t len;
1006 int i, index, cur_index;
1008 /* changing to a path is not allowed */
1009 len = get_path_element( new_name->str, new_name->len );
1010 if (!len || len != new_name->len || len > MAX_NAME_LEN * sizeof(WCHAR))
1012 set_error( STATUS_INVALID_PARAMETER );
1013 return;
1016 /* check for existing subkey with the same name */
1017 if (!parent || (subkey = find_subkey( parent, new_name, &index )))
1019 set_error( STATUS_CANNOT_DELETE );
1020 return;
1023 if (key == parent->wow6432node || is_wow6432node( new_name->str, new_name->len ))
1025 set_error( STATUS_INVALID_PARAMETER );
1026 return;
1029 if (!(new_name_ptr = mem_alloc( offsetof( struct object_name, name[new_name->len / sizeof(WCHAR)] ))))
1030 return;
1032 new_name_ptr->obj = &key->obj;
1033 new_name_ptr->len = new_name->len;
1034 new_name_ptr->parent = &parent->obj;
1035 memcpy( new_name_ptr->name, new_name->str, new_name->len );
1037 for (cur_index = 0; cur_index <= parent->last_subkey; cur_index++)
1038 if (parent->subkeys[cur_index] == key) break;
1040 if (cur_index < index && (index - cur_index) > 1)
1042 --index;
1043 for (i = cur_index; i < index; ++i) parent->subkeys[i] = parent->subkeys[i+1];
1045 else if (cur_index > index)
1047 for (i = cur_index; i > index; --i) parent->subkeys[i] = parent->subkeys[i-1];
1049 parent->subkeys[index] = key;
1051 free( key->obj.name );
1052 key->obj.name = new_name_ptr;
1054 if (debug_level > 1) dump_operation( key, NULL, "Rename" );
1055 touch_key( key, REG_NOTIFY_CHANGE_NAME );
1058 /* delete a key and its values */
1059 static int delete_key( struct key *key, int recurse )
1061 struct key *parent;
1063 if (key->flags & KEY_DELETED) return 1;
1065 if (!(parent = get_parent( key )))
1067 set_error( STATUS_ACCESS_DENIED );
1068 return 0;
1070 if (key->flags & KEY_PREDEF)
1072 set_error( STATUS_INVALID_HANDLE );
1073 return 0;
1076 if (recurse)
1078 while (key->last_subkey >= 0)
1079 if (!delete_key( key->subkeys[key->last_subkey], 1 )) return 0;
1081 else if (key->last_subkey >= 0) /* we can only delete a key that has no subkeys */
1083 set_error( STATUS_ACCESS_DENIED );
1084 return 0;
1087 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
1088 key->flags |= KEY_DELETED;
1089 unlink_named_object( &key->obj );
1090 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
1091 return 1;
1094 /* try to grow the array of values; return 1 if OK, 0 on error */
1095 static int grow_values( struct key *key )
1097 struct key_value *new_val;
1098 int nb_values;
1100 if (key->nb_values)
1102 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
1103 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
1105 set_error( STATUS_NO_MEMORY );
1106 return 0;
1109 else
1111 nb_values = MIN_VALUES;
1112 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
1114 key->values = new_val;
1115 key->nb_values = nb_values;
1116 return 1;
1119 /* find the named value of a given key and return its index in the array */
1120 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
1122 int i, min, max, res;
1123 data_size_t len;
1125 min = 0;
1126 max = key->last_value;
1127 while (min <= max)
1129 i = (min + max) / 2;
1130 len = min( key->values[i].namelen, name->len );
1131 res = memicmp_strW( key->values[i].name, name->str, len );
1132 if (!res) res = key->values[i].namelen - name->len;
1133 if (!res)
1135 *index = i;
1136 return &key->values[i];
1138 if (res > 0) max = i - 1;
1139 else min = i + 1;
1141 *index = min; /* this is where we should insert it */
1142 return NULL;
1145 /* insert a new value; the index must have been returned by find_value */
1146 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
1148 struct key_value *value;
1149 WCHAR *new_name = NULL;
1150 int i;
1152 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
1154 set_error( STATUS_NAME_TOO_LONG );
1155 return NULL;
1157 if (key->last_value + 1 == key->nb_values)
1159 if (!grow_values( key )) return NULL;
1161 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1162 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1163 value = &key->values[index];
1164 value->name = new_name;
1165 value->namelen = name->len;
1166 value->len = 0;
1167 value->data = NULL;
1168 return value;
1171 /* set a key value */
1172 static void set_value( struct key *key, const struct unicode_str *name,
1173 int type, const void *data, data_size_t len )
1175 struct key_value *value;
1176 void *ptr = NULL;
1177 int index;
1179 if (key->flags & KEY_PREDEF)
1181 set_error( STATUS_INVALID_HANDLE );
1182 return;
1185 if ((value = find_value( key, name, &index )))
1187 /* check if the new value is identical to the existing one */
1188 if (value->type == type && value->len == len &&
1189 value->data && !memcmp( value->data, data, len ))
1191 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1192 return;
1196 if (key->flags & KEY_SYMLINK)
1198 if (type != REG_LINK || name->len != symlink_str.len ||
1199 memicmp_strW( name->str, symlink_str.str, name->len ))
1201 set_error( STATUS_ACCESS_DENIED );
1202 return;
1206 if (len && !(ptr = memdup( data, len ))) return;
1208 if (!value)
1210 if (!(value = insert_value( key, name, index )))
1212 free( ptr );
1213 return;
1216 else free( value->data ); /* already existing, free previous data */
1218 value->type = type;
1219 value->len = len;
1220 value->data = ptr;
1221 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1222 if (debug_level > 1) dump_operation( key, value, "Set" );
1225 /* get a key value */
1226 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1228 struct key_value *value;
1229 int index;
1231 if (key->flags & KEY_PREDEF)
1233 set_error( STATUS_INVALID_HANDLE );
1234 return;
1237 if ((value = find_value( key, name, &index )))
1239 *type = value->type;
1240 *len = value->len;
1241 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1242 if (debug_level > 1) dump_operation( key, value, "Get" );
1244 else
1246 *type = -1;
1247 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1251 /* enumerate a key value */
1252 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1254 struct key_value *value;
1256 if (key->flags & KEY_PREDEF)
1258 set_error( STATUS_INVALID_HANDLE );
1259 return;
1262 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1263 else
1265 void *data;
1266 data_size_t namelen, maxlen;
1268 value = &key->values[i];
1269 reply->type = value->type;
1270 namelen = value->namelen;
1272 switch(info_class)
1274 case KeyValueBasicInformation:
1275 reply->total = namelen;
1276 break;
1277 case KeyValueFullInformation:
1278 reply->total = namelen + value->len;
1279 break;
1280 case KeyValuePartialInformation:
1281 reply->total = value->len;
1282 namelen = 0;
1283 break;
1284 default:
1285 set_error( STATUS_INVALID_PARAMETER );
1286 return;
1289 maxlen = min( reply->total, get_reply_max_size() );
1290 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1292 if (maxlen > namelen)
1294 reply->namelen = namelen;
1295 memcpy( data, value->name, namelen );
1296 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1298 else
1300 reply->namelen = maxlen;
1301 memcpy( data, value->name, maxlen );
1304 if (debug_level > 1) dump_operation( key, value, "Enum" );
1308 /* delete a value */
1309 static void delete_value( struct key *key, const struct unicode_str *name )
1311 struct key_value *value;
1312 int i, index, nb_values;
1314 if (key->flags & KEY_PREDEF)
1316 set_error( STATUS_INVALID_HANDLE );
1317 return;
1320 if (!(value = find_value( key, name, &index )))
1322 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1323 return;
1325 if (debug_level > 1) dump_operation( key, value, "Delete" );
1326 free( value->name );
1327 free( value->data );
1328 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1329 key->last_value--;
1330 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1332 /* try to shrink the array */
1333 nb_values = key->nb_values;
1334 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1336 struct key_value *new_val;
1337 nb_values -= nb_values / 3; /* shrink by 33% */
1338 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1339 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1340 key->values = new_val;
1341 key->nb_values = nb_values;
1345 /* get the registry key corresponding to an hkey handle */
1346 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1348 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1350 if (key && key->flags & KEY_DELETED)
1352 set_error( STATUS_KEY_DELETED );
1353 release_object( key );
1354 key = NULL;
1356 return key;
1359 /* read a line from the input file */
1360 static int read_next_line( struct file_load_info *info )
1362 char *newbuf;
1363 int newlen, pos = 0;
1365 info->line++;
1366 for (;;)
1368 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1369 return (pos != 0); /* EOF */
1370 pos = strlen(info->buffer);
1371 if (info->buffer[pos-1] == '\n')
1373 /* got a full line */
1374 info->buffer[--pos] = 0;
1375 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1376 return 1;
1378 if (pos < info->len - 1) return 1; /* EOF but something was read */
1380 /* need to enlarge the buffer */
1381 newlen = info->len + info->len / 2;
1382 if (!(newbuf = realloc( info->buffer, newlen )))
1384 set_error( STATUS_NO_MEMORY );
1385 return -1;
1387 info->buffer = newbuf;
1388 info->len = newlen;
1392 /* make sure the temp buffer holds enough space */
1393 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1395 WCHAR *tmp;
1396 if (info->tmplen >= size) return 1;
1397 if (!(tmp = realloc( info->tmp, size )))
1399 set_error( STATUS_NO_MEMORY );
1400 return 0;
1402 info->tmp = tmp;
1403 info->tmplen = size;
1404 return 1;
1407 /* report an error while loading an input file */
1408 static void file_read_error( const char *err, struct file_load_info *info )
1410 if (info->filename)
1411 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1412 else
1413 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1416 /* convert a data type tag to a value type */
1417 static int get_data_type( const char *buffer, int *type, int *parse_type )
1419 struct data_type { const char *tag; int len; int type; int parse_type; };
1421 static const struct data_type data_types[] =
1422 { /* actual type */ /* type to assume for parsing */
1423 { "\"", 1, REG_SZ, REG_SZ },
1424 { "str:\"", 5, REG_SZ, REG_SZ },
1425 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1426 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1427 { "hex:", 4, REG_BINARY, REG_BINARY },
1428 { "dword:", 6, REG_DWORD, REG_DWORD },
1429 { "hex(", 4, -1, REG_BINARY },
1430 { NULL, 0, 0, 0 }
1433 const struct data_type *ptr;
1434 char *end;
1436 for (ptr = data_types; ptr->tag; ptr++)
1438 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1439 *parse_type = ptr->parse_type;
1440 if ((*type = ptr->type) != -1) return ptr->len;
1441 /* "hex(xx):" is special */
1442 *type = (int)strtoul( buffer + 4, &end, 16 );
1443 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1444 return end + 2 - buffer;
1446 return 0;
1449 /* load and create a key from the input file */
1450 static struct key *load_key( struct key *base, const char *buffer, int prefix_len,
1451 struct file_load_info *info, timeout_t *modif )
1453 WCHAR *p;
1454 struct unicode_str name;
1455 int res;
1456 unsigned int mod;
1457 data_size_t len;
1459 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1461 len = info->tmplen;
1462 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1464 file_read_error( "Malformed key", info );
1465 return NULL;
1467 if (sscanf( buffer + res, " %u", &mod ) == 1)
1468 *modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1469 else
1470 *modif = current_time;
1472 p = info->tmp;
1473 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1475 if (!*p)
1477 if (prefix_len > 1)
1479 file_read_error( "Malformed key", info );
1480 return NULL;
1482 /* empty key name, return base key */
1483 return (struct key *)grab_object( base );
1485 name.str = p;
1486 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1487 return create_key_recursive( base, &name, 0 );
1490 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1491 static void update_key_time( struct key *key, timeout_t modif )
1493 while (key && !key->modif)
1495 key->modif = modif;
1496 key = get_parent( key );
1500 /* load a global option from the input file */
1501 static int load_global_option( const char *buffer, struct file_load_info *info )
1503 const char *p;
1505 if (!strncmp( buffer, "#arch=", 6 ))
1507 enum prefix_type type;
1508 p = buffer + 6;
1509 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1510 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1511 else
1513 file_read_error( "Unknown architecture", info );
1514 set_error( STATUS_NOT_REGISTRY_FILE );
1515 return 0;
1517 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1518 else if (type != prefix_type)
1520 file_read_error( "Mismatched architecture", info );
1521 set_error( STATUS_NOT_REGISTRY_FILE );
1522 return 0;
1525 /* ignore unknown options */
1526 return 1;
1529 /* load a key option from the input file */
1530 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1532 const char *p;
1533 data_size_t len;
1535 if (!strncmp( buffer, "#time=", 6 ))
1537 timeout_t modif = 0;
1538 for (p = buffer + 6; *p; p++)
1540 if (*p >= '0' && *p <= '9') modif = (modif << 4) | (*p - '0');
1541 else if (*p >= 'A' && *p <= 'F') modif = (modif << 4) | (*p - 'A' + 10);
1542 else if (*p >= 'a' && *p <= 'f') modif = (modif << 4) | (*p - 'a' + 10);
1543 else break;
1545 update_key_time( key, modif );
1547 if (!strncmp( buffer, "#class=", 7 ))
1549 p = buffer + 7;
1550 if (*p++ != '"') return 0;
1551 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1552 len = info->tmplen;
1553 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1554 free( key->class );
1555 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1556 key->classlen = len;
1558 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1559 /* ignore unknown options */
1560 return 1;
1563 /* parse a comma-separated list of hex digits */
1564 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1566 const char *p = buffer;
1567 data_size_t count = 0;
1568 char *end;
1570 while (isxdigit(*p))
1572 unsigned int val = strtoul( p, &end, 16 );
1573 if (end == p || val > 0xff) return -1;
1574 if (count++ >= *len) return -1; /* dest buffer overflow */
1575 *dest++ = val;
1576 p = end;
1577 while (isspace(*p)) p++;
1578 if (*p == ',') p++;
1579 while (isspace(*p)) p++;
1581 *len = count;
1582 return p - buffer;
1585 /* parse a value name and create the corresponding value */
1586 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1587 struct file_load_info *info )
1589 struct key_value *value;
1590 struct unicode_str name;
1591 int index;
1593 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1594 name.str = info->tmp;
1595 name.len = info->tmplen;
1596 if (buffer[0] == '@')
1598 name.len = 0;
1599 *len = 1;
1601 else
1603 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1604 if (r == -1) goto error;
1605 *len = r + 1; /* for initial quote */
1606 name.len -= sizeof(WCHAR); /* terminating null */
1608 while (isspace(buffer[*len])) (*len)++;
1609 if (buffer[*len] != '=') goto error;
1610 (*len)++;
1611 while (isspace(buffer[*len])) (*len)++;
1612 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1613 return value;
1615 error:
1616 file_read_error( "Malformed value name", info );
1617 return NULL;
1620 /* load a value from the input file */
1621 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1623 DWORD dw;
1624 void *ptr, *newptr;
1625 int res, type, parse_type;
1626 data_size_t maxlen, len;
1627 struct key_value *value;
1629 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1630 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1631 buffer += len + res;
1633 switch(parse_type)
1635 case REG_SZ:
1636 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1637 len = info->tmplen;
1638 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1639 ptr = info->tmp;
1640 break;
1641 case REG_DWORD:
1642 dw = strtoul( buffer, NULL, 16 );
1643 ptr = &dw;
1644 len = sizeof(dw);
1645 break;
1646 case REG_BINARY: /* hex digits */
1647 len = 0;
1648 for (;;)
1650 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1651 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1652 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1653 len += maxlen;
1654 buffer += res;
1655 while (isspace(*buffer)) buffer++;
1656 if (!*buffer) break;
1657 if (*buffer != '\\') goto error;
1658 if (read_next_line( info) != 1) goto error;
1659 buffer = info->buffer;
1660 while (isspace(*buffer)) buffer++;
1662 ptr = info->tmp;
1663 break;
1664 default:
1665 assert(0);
1666 ptr = NULL; /* keep compiler quiet */
1667 break;
1670 if (!len) newptr = NULL;
1671 else if (!(newptr = memdup( ptr, len ))) return 0;
1673 free( value->data );
1674 value->data = newptr;
1675 value->len = len;
1676 value->type = type;
1677 return 1;
1679 error:
1680 file_read_error( "Malformed value", info );
1681 free( value->data );
1682 value->data = NULL;
1683 value->len = 0;
1684 value->type = REG_NONE;
1685 return 0;
1688 /* return the length (in path elements) of name that is part of the key name */
1689 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1690 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1692 WCHAR *p;
1693 int res;
1694 data_size_t len;
1696 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1698 len = info->tmplen;
1699 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1701 file_read_error( "Malformed key", info );
1702 return 0;
1704 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1705 len = (p - info->tmp) * sizeof(WCHAR);
1706 for (res = 1; key != root_key; res++)
1708 if (len == key->obj.name->len && !memicmp_strW( info->tmp, key->obj.name->name, len )) break;
1709 key = get_parent( key );
1711 if (key == root_key) res = 0; /* no matching name */
1712 return res;
1715 /* load all the keys from the input file */
1716 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1717 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1719 struct key *subkey = NULL;
1720 struct file_load_info info;
1721 timeout_t modif = current_time;
1722 char *p;
1724 info.filename = filename;
1725 info.file = f;
1726 info.len = 4;
1727 info.tmplen = 4;
1728 info.line = 0;
1729 if (!(info.buffer = mem_alloc( info.len ))) return;
1730 if (!(info.tmp = mem_alloc( info.tmplen )))
1732 free( info.buffer );
1733 return;
1736 if ((read_next_line( &info ) != 1) ||
1737 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1739 set_error( STATUS_NOT_REGISTRY_FILE );
1740 goto done;
1743 while (read_next_line( &info ) == 1)
1745 p = info.buffer;
1746 while (*p && isspace(*p)) p++;
1747 switch(*p)
1749 case '[': /* new key */
1750 if (subkey)
1752 update_key_time( subkey, modif );
1753 release_object( subkey );
1755 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1756 if (!(subkey = load_key( key, p + 1, prefix_len, &info, &modif )))
1757 file_read_error( "Error creating key", &info );
1758 break;
1759 case '@': /* default value */
1760 case '\"': /* value */
1761 if (subkey) load_value( subkey, p, &info );
1762 else file_read_error( "Value without key", &info );
1763 break;
1764 case '#': /* option */
1765 if (subkey) load_key_option( subkey, p, &info );
1766 else if (!load_global_option( p, &info )) goto done;
1767 break;
1768 case ';': /* comment */
1769 case 0: /* empty line */
1770 break;
1771 default:
1772 file_read_error( "Unrecognized input", &info );
1773 break;
1777 done:
1778 if (subkey)
1780 update_key_time( subkey, modif );
1781 release_object( subkey );
1783 free( info.buffer );
1784 free( info.tmp );
1787 /* load a part of the registry from a file */
1788 static void load_registry( struct key *key, obj_handle_t handle )
1790 struct file *file;
1791 int fd;
1793 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1794 fd = dup( get_file_unix_fd( file ) );
1795 release_object( file );
1796 if (fd != -1)
1798 FILE *f = fdopen( fd, "r" );
1799 if (f)
1801 load_keys( key, NULL, f, -1 );
1802 fclose( f );
1804 else file_set_error();
1808 /* load one of the initial registry files */
1809 static int load_init_registry_from_file( const char *filename, struct key *key )
1811 FILE *f;
1813 if ((f = fopen( filename, "r" )))
1815 load_keys( key, filename, f, 0 );
1816 fclose( f );
1817 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1819 fprintf( stderr, "%s is not a valid registry file\n", filename );
1820 return 1;
1824 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1826 save_branch_info[save_branch_count].path = filename;
1827 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1828 make_object_permanent( &key->obj );
1829 return (f != NULL);
1832 static WCHAR *format_user_registry_path( const struct sid *sid, struct unicode_str *path )
1834 char buffer[7 + 11 + 11 + 11 * ARRAY_SIZE(sid->sub_auth)], *p = buffer;
1835 unsigned int i;
1837 p += sprintf( p, "User\\S-%u-%u", sid->revision,
1838 ((unsigned int)sid->id_auth[2] << 24) |
1839 ((unsigned int)sid->id_auth[3] << 16) |
1840 ((unsigned int)sid->id_auth[4] << 8) |
1841 ((unsigned int)sid->id_auth[5]) );
1842 for (i = 0; i < sid->sub_count; i++) p += sprintf( p, "-%u", sid->sub_auth[i] );
1843 return ascii_to_unicode_str( buffer, path );
1846 static void init_supported_machines(void)
1848 unsigned int count = 0;
1849 #ifdef __i386__
1850 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1851 #elif defined(__x86_64__)
1852 if (prefix_type == PREFIX_64BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_AMD64;
1853 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1854 #elif defined(__arm__)
1855 if (prefix_type == PREFIX_32BIT) supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1856 #elif defined(__aarch64__)
1857 if (prefix_type == PREFIX_64BIT)
1859 supported_machines[count++] = IMAGE_FILE_MACHINE_ARM64;
1860 supported_machines[count++] = IMAGE_FILE_MACHINE_AMD64;
1861 supported_machines[count++] = IMAGE_FILE_MACHINE_I386;
1863 supported_machines[count++] = IMAGE_FILE_MACHINE_ARMNT;
1864 #else
1865 #error Unsupported machine
1866 #endif
1867 supported_machines_count = count;
1868 native_machine = supported_machines[0];
1871 /* registry initialisation */
1872 void init_registry(void)
1874 static const WCHAR REGISTRY[] = {'\\','R','E','G','I','S','T','R','Y'};
1875 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1876 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1877 static const WCHAR classes_i386[] = {'S','o','f','t','w','a','r','e','\\',
1878 'C','l','a','s','s','e','s','\\',
1879 'W','o','w','6','4','3','2','N','o','d','e'};
1880 static const WCHAR classes_amd64[] = {'S','o','f','t','w','a','r','e','\\',
1881 'C','l','a','s','s','e','s','\\',
1882 'W','o','w','6','4','6','4','N','o','d','e'};
1883 static const WCHAR classes_arm[] = {'S','o','f','t','w','a','r','e','\\',
1884 'C','l','a','s','s','e','s','\\',
1885 'W','o','w','A','A','3','2','N','o','d','e'};
1886 static const WCHAR classes_arm64[] = {'S','o','f','t','w','a','r','e','\\',
1887 'C','l','a','s','s','e','s','\\',
1888 'W','o','w','A','A','6','4','N','o','d','e'};
1889 static const WCHAR perflib[] = {'S','o','f','t','w','a','r','e','\\',
1890 'M','i','c','r','o','s','o','f','t','\\',
1891 'W','i','n','d','o','w','s',' ','N','T','\\',
1892 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1893 'P','e','r','f','l','i','b','\\',
1894 '0','0','9'};
1895 static const struct unicode_str root_name = { REGISTRY, sizeof(REGISTRY) };
1896 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1897 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1898 static const struct unicode_str perflib_name = { perflib, sizeof(perflib) };
1900 WCHAR *current_user_path;
1901 struct unicode_str current_user_str;
1902 struct key *key, *hklm, *hkcu;
1903 unsigned int i;
1904 char *p;
1906 /* switch to the config dir */
1908 if (fchdir( config_dir_fd ) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno ));
1910 /* create the root key */
1911 root_key = create_key_object( NULL, &root_name, OBJ_PERMANENT, 0, current_time, NULL );
1912 assert( root_key );
1913 release_object( root_key );
1915 /* load system.reg into Registry\Machine */
1917 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1918 fatal_error( "could not create Machine registry key\n" );
1920 if (!load_init_registry_from_file( "system.reg", hklm ))
1922 if ((p = getenv( "WINEARCH" )) && !strcmp( p, "win32" ))
1923 prefix_type = PREFIX_32BIT;
1924 else
1925 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1927 else if (prefix_type == PREFIX_UNKNOWN)
1928 prefix_type = PREFIX_32BIT;
1930 init_supported_machines();
1932 /* load userdef.reg into Registry\User\.Default */
1934 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1935 fatal_error( "could not create User\\.Default registry key\n" );
1937 load_init_registry_from_file( "userdef.reg", key );
1938 release_object( key );
1940 /* load user.reg into HKEY_CURRENT_USER */
1942 /* FIXME: match default user in token.c. should get from process token instead */
1943 current_user_path = format_user_registry_path( &local_user_sid, &current_user_str );
1944 if (!current_user_path ||
1945 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1946 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1947 free( current_user_path );
1948 load_init_registry_from_file( "user.reg", hkcu );
1950 /* set the shared flag on Software\Classes\Wow6432Node for all platforms */
1951 for (i = 1; i < supported_machines_count; i++)
1953 struct unicode_str name;
1955 switch (supported_machines[i])
1957 case IMAGE_FILE_MACHINE_I386: name.str = classes_i386; name.len = sizeof(classes_i386); break;
1958 case IMAGE_FILE_MACHINE_ARMNT: name.str = classes_arm; name.len = sizeof(classes_arm); break;
1959 case IMAGE_FILE_MACHINE_AMD64: name.str = classes_amd64; name.len = sizeof(classes_amd64); break;
1960 case IMAGE_FILE_MACHINE_ARM64: name.str = classes_arm64; name.len = sizeof(classes_arm64); break;
1962 if ((key = create_key_recursive( hklm, &name, current_time )))
1964 key->flags |= KEY_WOWSHARE;
1965 release_object( key );
1967 /* FIXME: handle HKCU too */
1970 if ((key = create_key_recursive( hklm, &perflib_name, current_time )))
1972 key->flags |= KEY_PREDEF;
1973 release_object( key );
1976 release_object( hklm );
1977 release_object( hkcu );
1979 /* start the periodic save timer */
1980 set_periodic_save_timer();
1982 /* create windows directories */
1984 if (!mkdir( "drive_c/windows", 0777 ))
1986 mkdir( "drive_c/windows/system32", 0777 );
1987 for (i = 1; i < supported_machines_count; i++)
1989 switch (supported_machines[i])
1991 case IMAGE_FILE_MACHINE_I386: mkdir( "drive_c/windows/syswow64", 0777 ); break;
1992 case IMAGE_FILE_MACHINE_ARMNT: mkdir( "drive_c/windows/sysarm32", 0777 ); break;
1993 case IMAGE_FILE_MACHINE_AMD64: mkdir( "drive_c/windows/sysx8664", 0777 ); break;
1994 case IMAGE_FILE_MACHINE_ARM64: mkdir( "drive_c/windows/sysarm64", 0777 ); break;
1999 /* go back to the server dir */
2000 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2003 /* save a registry branch to a file */
2004 static void save_all_subkeys( struct key *key, FILE *f )
2006 fprintf( f, "WINE REGISTRY Version 2\n" );
2007 fprintf( f, ";; All keys relative to " );
2008 dump_path( key, NULL, f );
2009 fprintf( f, "\n" );
2010 switch (prefix_type)
2012 case PREFIX_32BIT:
2013 fprintf( f, "\n#arch=win32\n" );
2014 break;
2015 case PREFIX_64BIT:
2016 fprintf( f, "\n#arch=win64\n" );
2017 break;
2018 default:
2019 break;
2021 save_subkeys( key, key, f );
2024 /* save a registry branch to a file handle */
2025 static void save_registry( struct key *key, obj_handle_t handle )
2027 struct file *file;
2028 int fd;
2030 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
2031 fd = dup( get_file_unix_fd( file ) );
2032 release_object( file );
2033 if (fd != -1)
2035 FILE *f = fdopen( fd, "w" );
2036 if (f)
2038 save_all_subkeys( key, f );
2039 if (fclose( f )) file_set_error();
2041 else
2043 file_set_error();
2044 close( fd );
2049 /* save a registry branch to a file */
2050 static int save_branch( struct key *key, const char *path )
2052 struct stat st;
2053 char *p, *tmp = NULL;
2054 int fd, count = 0, ret = 0;
2055 FILE *f;
2057 if (!(key->flags & KEY_DIRTY))
2059 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
2060 return 1;
2063 /* test the file type */
2065 if ((fd = open( path, O_WRONLY )) != -1)
2067 /* if file is not a regular file or has multiple links or is accessed
2068 * via symbolic links, write directly into it; otherwise use a temp file */
2069 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
2071 ftruncate( fd, 0 );
2072 goto save;
2074 close( fd );
2077 /* create a temp file in the same directory */
2079 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
2080 strcpy( tmp, path );
2081 if ((p = strrchr( tmp, '/' ))) p++;
2082 else p = tmp;
2083 for (;;)
2085 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
2086 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
2087 if (errno != EEXIST) goto done;
2088 close( fd );
2091 /* now save to it */
2093 save:
2094 if (!(f = fdopen( fd, "w" )))
2096 if (tmp) unlink( tmp );
2097 close( fd );
2098 goto done;
2101 if (debug_level > 1)
2103 fprintf( stderr, "%s: ", path );
2104 dump_operation( key, NULL, "saving" );
2107 save_all_subkeys( key, f );
2108 ret = !fclose(f);
2110 if (tmp)
2112 /* if successfully written, rename to final name */
2113 if (ret) ret = !rename( tmp, path );
2114 if (!ret) unlink( tmp );
2117 done:
2118 free( tmp );
2119 if (ret) make_clean( key );
2120 return ret;
2123 /* periodic saving of the registry */
2124 static void periodic_save( void *arg )
2126 int i;
2128 if (fchdir( config_dir_fd ) == -1) return;
2129 save_timeout_user = NULL;
2130 for (i = 0; i < save_branch_count; i++)
2131 save_branch( save_branch_info[i].key, save_branch_info[i].path );
2132 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2133 set_periodic_save_timer();
2136 /* start the periodic save timer */
2137 static void set_periodic_save_timer(void)
2139 if (save_timeout_user) remove_timeout_user( save_timeout_user );
2140 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
2143 /* save the modified registry branches to disk */
2144 void flush_registry(void)
2146 int i;
2148 if (fchdir( config_dir_fd ) == -1) return;
2149 for (i = 0; i < save_branch_count; i++)
2151 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
2153 fprintf( stderr, "wineserver: could not save registry branch to %s",
2154 save_branch_info[i].path );
2155 perror( " " );
2158 if (fchdir( server_dir_fd ) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno ));
2161 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2162 static int is_wow64_thread( struct thread *thread )
2164 return (is_machine_64bit( native_machine ) && !is_machine_64bit( thread->process->machine ));
2168 /* create a registry key */
2169 DECL_HANDLER(create_key)
2171 struct key *key, *parent = NULL;
2172 unsigned int access = req->access;
2173 const WCHAR *class;
2174 struct unicode_str name;
2175 const struct security_descriptor *sd;
2176 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2178 if (!objattr) return;
2180 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2182 if (objattr->rootdir)
2184 if (!(parent = get_hkey_obj( objattr->rootdir, 0 ))) return;
2187 if ((key = create_key( parent, &name, req->options, access, objattr->attributes, sd )))
2189 if ((class = get_req_data_after_objattr( objattr, &key->classlen )))
2191 key->classlen = (key->classlen / sizeof(WCHAR)) * sizeof(WCHAR);
2192 if (!(key->class = memdup( class, key->classlen ))) key->classlen = 0;
2194 reply->hkey = alloc_handle( current->process, key, access, objattr->attributes );
2195 release_object( key );
2197 if (parent) release_object( parent );
2200 /* open a registry key */
2201 DECL_HANDLER(open_key)
2203 struct key *key, *parent = NULL;
2204 unsigned int access = req->access;
2205 struct unicode_str name = get_req_unicode_str();
2207 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
2209 if (req->parent && !(parent = get_hkey_obj( req->parent, 0 ))) return;
2211 if ((key = open_key( parent, &name, access, req->attributes )))
2213 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
2214 release_object( key );
2216 if (parent) release_object( parent );
2219 /* delete a registry key */
2220 DECL_HANDLER(delete_key)
2222 struct key *key = (struct key *)get_handle_obj( current->process, req->hkey, DELETE, &key_ops );
2224 if (key)
2226 delete_key( key, 0 );
2227 release_object( key );
2231 /* flush a registry key */
2232 DECL_HANDLER(flush_key)
2234 struct key *key = get_hkey_obj( req->hkey, 0 );
2235 if (key)
2237 /* we don't need to do anything here with the current implementation */
2238 release_object( key );
2242 /* enumerate registry subkeys */
2243 DECL_HANDLER(enum_key)
2245 struct key *key;
2247 if ((key = get_hkey_obj( req->hkey, req->index == -1 ? 0 : KEY_ENUMERATE_SUB_KEYS )))
2249 enum_key( key, req->index, req->info_class, reply );
2250 release_object( key );
2254 /* set a value of a registry key */
2255 DECL_HANDLER(set_key_value)
2257 struct key *key;
2258 struct unicode_str name;
2260 if (req->namelen > get_req_data_size())
2262 set_error( STATUS_INVALID_PARAMETER );
2263 return;
2265 name.str = get_req_data();
2266 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2268 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2270 data_size_t datalen = get_req_data_size() - req->namelen;
2271 const char *data = (const char *)get_req_data() + req->namelen;
2273 set_value( key, &name, req->type, data, datalen );
2274 release_object( key );
2278 /* retrieve the value of a registry key */
2279 DECL_HANDLER(get_key_value)
2281 struct key *key;
2282 struct unicode_str name = get_req_unicode_str();
2284 reply->total = 0;
2285 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2287 get_value( key, &name, &reply->type, &reply->total );
2288 release_object( key );
2292 /* enumerate the value of a registry key */
2293 DECL_HANDLER(enum_key_value)
2295 struct key *key;
2297 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2299 enum_value( key, req->index, req->info_class, reply );
2300 release_object( key );
2304 /* delete a value of a registry key */
2305 DECL_HANDLER(delete_key_value)
2307 struct key *key;
2308 struct unicode_str name = get_req_unicode_str();
2310 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2312 delete_value( key, &name );
2313 release_object( key );
2317 /* load a registry branch from a file */
2318 DECL_HANDLER(load_registry)
2320 struct key *key, *parent = NULL;
2321 struct unicode_str name;
2322 const struct security_descriptor *sd;
2323 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
2325 if (!objattr) return;
2327 if (!thread_single_check_privilege( current, SeRestorePrivilege ))
2329 set_error( STATUS_PRIVILEGE_NOT_HELD );
2330 return;
2332 if (objattr->rootdir)
2334 if (!(parent = get_hkey_obj( objattr->rootdir, 0 ))) return;
2337 if ((key = create_key( parent, &name, 0, KEY_WOW64_64KEY, 0, sd )))
2339 load_registry( key, req->file );
2340 release_object( key );
2342 if (parent) release_object( parent );
2345 DECL_HANDLER(unload_registry)
2347 struct key *key, *parent = NULL;
2348 struct unicode_str name = get_req_unicode_str();
2350 if (!thread_single_check_privilege( current, SeRestorePrivilege ))
2352 set_error( STATUS_PRIVILEGE_NOT_HELD );
2353 return;
2356 if (req->parent && !(parent = get_hkey_obj( req->parent, 0 ))) return;
2358 if ((key = open_key( parent, &name, KEY_WOW64_64KEY, req->attributes )))
2360 if (key->obj.handle_count)
2361 set_error( STATUS_CANNOT_DELETE );
2362 else
2363 delete_key( key, 1 ); /* FIXME */
2364 release_object( key );
2366 if (parent) release_object( parent );
2369 /* save a registry branch to a file */
2370 DECL_HANDLER(save_registry)
2372 struct key *key;
2374 if (!thread_single_check_privilege( current, SeBackupPrivilege ))
2376 set_error( STATUS_PRIVILEGE_NOT_HELD );
2377 return;
2380 if ((key = get_hkey_obj( req->hkey, 0 )))
2382 save_registry( key, req->file );
2383 release_object( key );
2387 /* add a registry key change notification */
2388 DECL_HANDLER(set_registry_notification)
2390 struct key *key;
2391 struct event *event;
2392 struct notify *notify;
2394 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2395 if (key)
2397 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2398 if (event)
2400 notify = find_notify( key, current->process, req->hkey );
2401 if (!notify)
2403 notify = mem_alloc( sizeof(*notify) );
2404 if (notify)
2406 notify->events = NULL;
2407 notify->event_count = 0;
2408 notify->subtree = req->subtree;
2409 notify->filter = req->filter;
2410 notify->hkey = req->hkey;
2411 notify->process = current->process;
2412 list_add_head( &key->notify_list, &notify->entry );
2415 if (notify)
2417 struct event **new_array;
2419 if ((new_array = realloc( notify->events, (notify->event_count + 1) * sizeof(*notify->events) )))
2421 notify->events = new_array;
2422 notify->events[notify->event_count++] = (struct event *)grab_object( event );
2423 reset_event( event );
2424 set_error( STATUS_PENDING );
2426 else set_error( STATUS_NO_MEMORY );
2428 release_object( event );
2430 release_object( key );
2434 /* rename a registry key */
2435 DECL_HANDLER(rename_key)
2437 struct unicode_str name = get_req_unicode_str();
2438 struct key *key;
2440 key = get_hkey_obj( req->hkey, KEY_WRITE );
2441 if (key)
2443 rename_key( key, &name );
2444 release_object( key );