comctl32: Export TaskDialogIndirect by ordinal and name.
[wine/wine-gecko.git] / server / registry.c
blobefc2005487b0f8249a375cd771fb35c5b74c86c3
1 /*
2 * Server-side registry management
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* To do:
22 * - symbolic links
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "object.h"
43 #include "file.h"
44 #include "handle.h"
45 #include "request.h"
46 #include "process.h"
47 #include "unicode.h"
48 #include "security.h"
50 #include "winternl.h"
51 #include "wine/library.h"
53 struct notify
55 struct list entry; /* entry in list of notifications */
56 struct event *event; /* event to set when changing this key */
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 /* a registry key */
64 struct key
66 struct object obj; /* object header */
67 WCHAR *name; /* key name */
68 WCHAR *class; /* key class */
69 unsigned short namelen; /* length of key name */
70 unsigned short classlen; /* length of class name */
71 struct key *parent; /* parent key */
72 int last_subkey; /* last in use subkey */
73 int nb_subkeys; /* count of allocated subkeys */
74 struct key **subkeys; /* subkeys array */
75 int last_value; /* last in use value */
76 int nb_values; /* count of allocated values in array */
77 struct key_value *values; /* values array */
78 unsigned int flags; /* flags */
79 timeout_t modif; /* last modification time */
80 struct list notify_list; /* list of notifications */
83 /* key flags */
84 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
85 #define KEY_DELETED 0x0002 /* key has been deleted */
86 #define KEY_DIRTY 0x0004 /* key has been modified */
87 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
88 #define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
89 #define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
91 /* a key value */
92 struct key_value
94 WCHAR *name; /* value name */
95 unsigned short namelen; /* length of value name */
96 unsigned short type; /* value type */
97 data_size_t len; /* value data length in bytes */
98 void *data; /* pointer to value data */
101 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
102 #define MIN_VALUES 8 /* min. number of allocated values per key */
104 #define MAX_NAME_LEN 255 /* max. length of a key name */
105 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
107 /* the root of the registry tree */
108 static struct key *root_key;
110 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
111 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
112 static struct timeout_user *save_timeout_user; /* saving timer */
113 static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
115 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
116 static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
117 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
118 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
120 static void set_periodic_save_timer(void);
121 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
123 /* information about where to save a registry branch */
124 struct save_branch_info
126 struct key *key;
127 const char *path;
130 #define MAX_SAVE_BRANCH_INFO 3
131 static int save_branch_count;
132 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
135 /* information about a file being loaded */
136 struct file_load_info
138 const char *filename; /* input file name */
139 FILE *file; /* input file */
140 char *buffer; /* line buffer */
141 int len; /* buffer length */
142 int line; /* current input line */
143 WCHAR *tmp; /* temp buffer to use while parsing input */
144 size_t tmplen; /* length of temp buffer */
148 static void key_dump( struct object *obj, int verbose );
149 static unsigned int key_map_access( struct object *obj, unsigned int access );
150 static struct security_descriptor *key_get_sd( struct object *obj );
151 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
152 static void key_destroy( struct object *obj );
154 static const struct object_ops key_ops =
156 sizeof(struct key), /* size */
157 key_dump, /* dump */
158 no_get_type, /* get_type */
159 no_add_queue, /* add_queue */
160 NULL, /* remove_queue */
161 NULL, /* signaled */
162 NULL, /* satisfied */
163 no_signal, /* signal */
164 no_get_fd, /* get_fd */
165 key_map_access, /* map_access */
166 key_get_sd, /* get_sd */
167 default_set_sd, /* set_sd */
168 no_lookup_name, /* lookup_name */
169 no_open_file, /* open_file */
170 key_close_handle, /* close_handle */
171 key_destroy /* destroy */
175 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
177 return (len == sizeof(wow6432node) &&
178 !memicmpW( name, wow6432node, sizeof(wow6432node)/sizeof(WCHAR) ));
182 * The registry text file format v2 used by this code is similar to the one
183 * used by REGEDIT import/export functionality, with the following differences:
184 * - strings and key names can contain \x escapes for Unicode
185 * - key names use escapes too in order to support Unicode
186 * - the modification time optionally follows the key name
187 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
190 /* dump the full path of a key */
191 static void dump_path( const struct key *key, const struct key *base, FILE *f )
193 if (key->parent && key->parent != base)
195 dump_path( key->parent, base, f );
196 fprintf( f, "\\\\" );
198 dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
201 /* dump a value to a text file */
202 static void dump_value( const struct key_value *value, FILE *f )
204 unsigned int i, dw;
205 int count;
207 if (value->namelen)
209 fputc( '\"', f );
210 count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
211 count += fprintf( f, "\"=" );
213 else count = fprintf( f, "@=" );
215 switch(value->type)
217 case REG_SZ:
218 case REG_EXPAND_SZ:
219 case REG_MULTI_SZ:
220 /* only output properly terminated strings in string format */
221 if (value->len < sizeof(WCHAR)) break;
222 if (value->len % sizeof(WCHAR)) break;
223 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
224 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
225 fputc( '\"', f );
226 dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
227 fprintf( f, "\"\n" );
228 return;
230 case REG_DWORD:
231 if (value->len != sizeof(dw)) break;
232 memcpy( &dw, value->data, sizeof(dw) );
233 fprintf( f, "dword:%08x\n", dw );
234 return;
237 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
238 else count += fprintf( f, "hex(%x):", value->type );
239 for (i = 0; i < value->len; i++)
241 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
242 if (i < value->len-1)
244 fputc( ',', f );
245 if (++count > 76)
247 fprintf( f, "\\\n " );
248 count = 2;
252 fputc( '\n', f );
255 /* save a registry and all its subkeys to a text file */
256 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
258 int i;
260 if (key->flags & KEY_VOLATILE) return;
261 /* save key if it has either some values or no subkeys, or needs special options */
262 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
263 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
265 fprintf( f, "\n[" );
266 if (key != base) dump_path( key, base, f );
267 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
268 if (key->class)
270 fprintf( f, "#class=\"" );
271 dump_strW( key->class, key->classlen / sizeof(WCHAR), f, "\"\"" );
272 fprintf( f, "\"\n" );
274 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
275 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
277 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
280 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
282 fprintf( stderr, "%s key ", op );
283 if (key) dump_path( key, NULL, stderr );
284 else fprintf( stderr, "ERROR" );
285 if (value)
287 fprintf( stderr, " value ");
288 dump_value( value, stderr );
290 else fprintf( stderr, "\n" );
293 static void key_dump( struct object *obj, int verbose )
295 struct key *key = (struct key *)obj;
296 assert( obj->ops == &key_ops );
297 fprintf( stderr, "Key flags=%x ", key->flags );
298 dump_path( key, NULL, stderr );
299 fprintf( stderr, "\n" );
302 /* notify waiter and maybe delete the notification */
303 static void do_notification( struct key *key, struct notify *notify, int del )
305 if (notify->event)
307 set_event( notify->event );
308 release_object( notify->event );
309 notify->event = NULL;
311 if (del)
313 list_remove( &notify->entry );
314 free( notify );
318 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
320 struct notify *notify;
322 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
324 if (notify->process == process && notify->hkey == hkey) return notify;
326 return NULL;
329 static unsigned int key_map_access( struct object *obj, unsigned int access )
331 if (access & GENERIC_READ) access |= KEY_READ;
332 if (access & GENERIC_WRITE) access |= KEY_WRITE;
333 if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
334 if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS;
335 /* filter the WOW64 masks, as they aren't real access bits */
336 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL |
337 KEY_WOW64_64KEY | KEY_WOW64_32KEY);
340 static struct security_descriptor *key_get_sd( struct object *obj )
342 static struct security_descriptor *key_default_sd;
344 if (obj->sd) return obj->sd;
346 if (!key_default_sd)
348 size_t sid_len = security_sid_len( security_builtin_admins_sid );
350 key_default_sd = mem_alloc( sizeof(*key_default_sd) + 2 * sid_len );
351 key_default_sd->control = 0;
352 key_default_sd->owner_len = sid_len;
353 key_default_sd->group_len = sid_len;
354 key_default_sd->sacl_len = 0;
355 key_default_sd->dacl_len = 0;
356 memcpy( key_default_sd + 1, security_builtin_admins_sid, sid_len );
357 memcpy( (char *)(key_default_sd + 1) + sid_len, security_builtin_admins_sid, sid_len );
359 return key_default_sd;
362 /* close the notification associated with a handle */
363 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
365 struct key * key = (struct key *) obj;
366 struct notify *notify = find_notify( key, process, handle );
367 if (notify) do_notification( key, notify, 1 );
368 return 1; /* ok to close */
371 static void key_destroy( struct object *obj )
373 int i;
374 struct list *ptr;
375 struct key *key = (struct key *)obj;
376 assert( obj->ops == &key_ops );
378 free( key->name );
379 free( key->class );
380 for (i = 0; i <= key->last_value; i++)
382 free( key->values[i].name );
383 free( key->values[i].data );
385 free( key->values );
386 for (i = 0; i <= key->last_subkey; i++)
388 key->subkeys[i]->parent = NULL;
389 release_object( key->subkeys[i] );
391 free( key->subkeys );
392 /* unconditionally notify everything waiting on this key */
393 while ((ptr = list_head( &key->notify_list )))
395 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
396 do_notification( key, notify, 1 );
400 /* get the request vararg as registry path */
401 static inline void get_req_path( struct unicode_str *str, int skip_root )
403 str->str = get_req_data();
404 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
406 if (skip_root && str->len >= sizeof(root_name) &&
407 !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
409 str->str += sizeof(root_name)/sizeof(WCHAR);
410 str->len -= sizeof(root_name);
414 /* return the next token in a given path */
415 /* token->str must point inside the path, or be NULL for the first call */
416 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
418 data_size_t i = 0, len = path->len / sizeof(WCHAR);
420 if (!token->str) /* first time */
422 /* path cannot start with a backslash */
423 if (len && path->str[0] == '\\')
425 set_error( STATUS_OBJECT_PATH_INVALID );
426 return NULL;
429 else
431 i = token->str - path->str;
432 i += token->len / sizeof(WCHAR);
433 while (i < len && path->str[i] == '\\') i++;
435 token->str = path->str + i;
436 while (i < len && path->str[i] != '\\') i++;
437 token->len = (path->str + i - token->str) * sizeof(WCHAR);
438 return token;
441 /* allocate a key object */
442 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
444 struct key *key;
445 if ((key = alloc_object( &key_ops )))
447 key->name = NULL;
448 key->class = NULL;
449 key->namelen = name->len;
450 key->classlen = 0;
451 key->flags = 0;
452 key->last_subkey = -1;
453 key->nb_subkeys = 0;
454 key->subkeys = NULL;
455 key->nb_values = 0;
456 key->last_value = -1;
457 key->values = NULL;
458 key->modif = modif;
459 key->parent = NULL;
460 list_init( &key->notify_list );
461 if (name->len && !(key->name = memdup( name->str, name->len )))
463 release_object( key );
464 key = NULL;
467 return key;
470 /* mark a key and all its parents as dirty (modified) */
471 static void make_dirty( struct key *key )
473 while (key)
475 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
476 key->flags |= KEY_DIRTY;
477 key = key->parent;
481 /* mark a key and all its subkeys as clean (not modified) */
482 static void make_clean( struct key *key )
484 int i;
486 if (key->flags & KEY_VOLATILE) return;
487 if (!(key->flags & KEY_DIRTY)) return;
488 key->flags &= ~KEY_DIRTY;
489 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
492 /* go through all the notifications and send them if necessary */
493 static void check_notify( struct key *key, unsigned int change, int not_subtree )
495 struct list *ptr, *next;
497 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
499 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
500 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
501 do_notification( key, n, 0 );
505 /* update key modification time */
506 static void touch_key( struct key *key, unsigned int change )
508 struct key *k;
510 key->modif = current_time;
511 make_dirty( key );
513 /* do notifications */
514 check_notify( key, change, 1 );
515 for ( k = key->parent; k; k = k->parent )
516 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
519 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
520 static int grow_subkeys( struct key *key )
522 struct key **new_subkeys;
523 int nb_subkeys;
525 if (key->nb_subkeys)
527 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
528 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
530 set_error( STATUS_NO_MEMORY );
531 return 0;
534 else
536 nb_subkeys = MIN_VALUES;
537 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
539 key->subkeys = new_subkeys;
540 key->nb_subkeys = nb_subkeys;
541 return 1;
544 /* allocate a subkey for a given key, and return its index */
545 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
546 int index, timeout_t modif )
548 struct key *key;
549 int i;
551 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
553 set_error( STATUS_NAME_TOO_LONG );
554 return NULL;
556 if (parent->last_subkey + 1 == parent->nb_subkeys)
558 /* need to grow the array */
559 if (!grow_subkeys( parent )) return NULL;
561 if ((key = alloc_key( name, modif )) != NULL)
563 key->parent = parent;
564 for (i = ++parent->last_subkey; i > index; i--)
565 parent->subkeys[i] = parent->subkeys[i-1];
566 parent->subkeys[index] = key;
567 if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
568 parent->flags |= KEY_WOW64;
570 return key;
573 /* free a subkey of a given key */
574 static void free_subkey( struct key *parent, int index )
576 struct key *key;
577 int i, nb_subkeys;
579 assert( index >= 0 );
580 assert( index <= parent->last_subkey );
582 key = parent->subkeys[index];
583 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
584 parent->last_subkey--;
585 key->flags |= KEY_DELETED;
586 key->parent = NULL;
587 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
588 release_object( key );
590 /* try to shrink the array */
591 nb_subkeys = parent->nb_subkeys;
592 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
594 struct key **new_subkeys;
595 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
596 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
597 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
598 parent->subkeys = new_subkeys;
599 parent->nb_subkeys = nb_subkeys;
603 /* find the named child of a given key and return its index */
604 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
606 int i, min, max, res;
607 data_size_t len;
609 min = 0;
610 max = key->last_subkey;
611 while (min <= max)
613 i = (min + max) / 2;
614 len = min( key->subkeys[i]->namelen, name->len );
615 res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
616 if (!res) res = key->subkeys[i]->namelen - name->len;
617 if (!res)
619 *index = i;
620 return key->subkeys[i];
622 if (res > 0) max = i - 1;
623 else min = i + 1;
625 *index = min; /* this is where we should insert it */
626 return NULL;
629 /* return the wow64 variant of the key, or the key itself if none */
630 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
632 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
633 int index;
635 if (!(key->flags & KEY_WOW64)) return key;
636 if (!is_wow6432node( name->str, name->len ))
638 key = find_subkey( key, &wow6432node_str, &index );
639 assert( key ); /* if KEY_WOW64 is set we must find it */
641 return key;
645 /* follow a symlink and return the resolved key */
646 static struct key *follow_symlink( struct key *key, int iteration )
648 struct unicode_str path, token;
649 struct key_value *value;
650 int index;
652 if (iteration > 16) return NULL;
653 if (!(key->flags & KEY_SYMLINK)) return key;
654 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
656 path.str = value->data;
657 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
658 if (path.len <= sizeof(root_name)) return NULL;
659 if (memicmpW( path.str, root_name, sizeof(root_name)/sizeof(WCHAR) )) return NULL;
660 path.str += sizeof(root_name) / sizeof(WCHAR);
661 path.len -= sizeof(root_name);
663 key = root_key;
664 token.str = NULL;
665 if (!get_path_token( &path, &token )) return NULL;
666 while (token.len)
668 if (!(key = find_subkey( key, &token, &index ))) break;
669 if (!(key = follow_symlink( key, iteration + 1 ))) break;
670 get_path_token( &path, &token );
672 return key;
675 /* open a key until we find an element that doesn't exist */
676 /* helper for open_key and create_key */
677 static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
678 unsigned int access, struct unicode_str *token, int *index )
680 token->str = NULL;
681 if (!get_path_token( name, token )) return NULL;
682 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
683 while (token->len)
685 struct key *subkey;
686 if (!(subkey = find_subkey( key, token, index )))
688 if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
690 /* try in the 64-bit parent */
691 key = key->parent;
692 subkey = find_subkey( key, token, index );
695 if (!subkey) break;
696 key = subkey;
697 get_path_token( name, token );
698 if (!token->len) break;
699 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
700 if (!(key = follow_symlink( key, 0 )))
702 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
703 return NULL;
706 return key;
709 /* open a subkey */
710 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
711 unsigned int attributes )
713 int index;
714 struct unicode_str token;
716 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
718 if (token.len)
720 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
721 return NULL;
723 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
724 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
726 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
727 return NULL;
729 if (debug_level > 1) dump_operation( key, NULL, "Open" );
730 grab_object( key );
731 return key;
734 /* create a subkey */
735 static struct key *create_key( struct key *key, const struct unicode_str *name,
736 const struct unicode_str *class, unsigned int options,
737 unsigned int access, unsigned int attributes, int *created )
739 int index;
740 struct unicode_str token, next;
742 *created = 0;
743 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
745 if (!token.len) /* the key already exists */
747 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
748 if (options & REG_OPTION_CREATE_LINK)
750 set_error( STATUS_OBJECT_NAME_COLLISION );
751 return NULL;
753 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
755 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
756 return NULL;
758 if (debug_level > 1) dump_operation( key, NULL, "Open" );
759 grab_object( key );
760 return key;
763 /* token must be the last path component at this point */
764 next = token;
765 get_path_token( name, &next );
766 if (next.len)
768 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
769 return NULL;
772 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
774 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
775 return NULL;
777 *created = 1;
778 make_dirty( key );
779 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
781 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
782 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
783 else key->flags |= KEY_DIRTY;
785 if (debug_level > 1) dump_operation( key, NULL, "Create" );
786 if (class && class->len)
788 key->classlen = class->len;
789 free(key->class);
790 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
792 grab_object( key );
793 return key;
796 /* recursively create a subkey (for internal use only) */
797 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
799 struct key *base;
800 int index;
801 struct unicode_str token;
803 token.str = NULL;
804 if (!get_path_token( name, &token )) return NULL;
805 while (token.len)
807 struct key *subkey;
808 if (!(subkey = find_subkey( key, &token, &index ))) break;
809 key = subkey;
810 if (!(key = follow_symlink( key, 0 )))
812 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
813 return NULL;
815 get_path_token( name, &token );
818 if (token.len)
820 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
821 base = key;
822 for (;;)
824 get_path_token( name, &token );
825 if (!token.len) break;
826 /* we know the index is always 0 in a new key */
827 if (!(key = alloc_subkey( key, &token, 0, modif )))
829 free_subkey( base, index );
830 return NULL;
835 grab_object( key );
836 return key;
839 /* query information about a key or a subkey */
840 static void enum_key( const struct key *key, int index, int info_class,
841 struct enum_key_reply *reply )
843 int i;
844 data_size_t len, namelen, classlen;
845 data_size_t max_subkey = 0, max_class = 0;
846 data_size_t max_value = 0, max_data = 0;
847 char *data;
849 if (index != -1) /* -1 means use the specified key directly */
851 if ((index < 0) || (index > key->last_subkey))
853 set_error( STATUS_NO_MORE_ENTRIES );
854 return;
856 key = key->subkeys[index];
859 namelen = key->namelen;
860 classlen = key->classlen;
862 switch(info_class)
864 case KeyBasicInformation:
865 classlen = 0; /* only return the name */
866 /* fall through */
867 case KeyNodeInformation:
868 reply->max_subkey = 0;
869 reply->max_class = 0;
870 reply->max_value = 0;
871 reply->max_data = 0;
872 break;
873 case KeyFullInformation:
874 for (i = 0; i <= key->last_subkey; i++)
876 struct key *subkey = key->subkeys[i];
877 len = subkey->namelen / sizeof(WCHAR);
878 if (len > max_subkey) max_subkey = len;
879 len = subkey->classlen / sizeof(WCHAR);
880 if (len > max_class) max_class = len;
882 for (i = 0; i <= key->last_value; i++)
884 len = key->values[i].namelen / sizeof(WCHAR);
885 if (len > max_value) max_value = len;
886 len = key->values[i].len;
887 if (len > max_data) max_data = len;
889 reply->max_subkey = max_subkey;
890 reply->max_class = max_class;
891 reply->max_value = max_value;
892 reply->max_data = max_data;
893 namelen = 0; /* only return the class */
894 break;
895 default:
896 set_error( STATUS_INVALID_PARAMETER );
897 return;
899 reply->subkeys = key->last_subkey + 1;
900 reply->values = key->last_value + 1;
901 reply->modif = key->modif;
902 reply->total = namelen + classlen;
904 len = min( reply->total, get_reply_max_size() );
905 if (len && (data = set_reply_data_size( len )))
907 if (len > namelen)
909 reply->namelen = namelen;
910 memcpy( data, key->name, namelen );
911 memcpy( data + namelen, key->class, len - namelen );
913 else
915 reply->namelen = len;
916 memcpy( data, key->name, len );
919 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
922 /* delete a key and its values */
923 static int delete_key( struct key *key, int recurse )
925 int index;
926 struct key *parent = key->parent;
928 /* must find parent and index */
929 if (key == root_key)
931 set_error( STATUS_ACCESS_DENIED );
932 return -1;
934 assert( parent );
936 while (recurse && (key->last_subkey>=0))
937 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
938 return -1;
940 for (index = 0; index <= parent->last_subkey; index++)
941 if (parent->subkeys[index] == key) break;
942 assert( index <= parent->last_subkey );
944 /* we can only delete a key that has no subkeys */
945 if (key->last_subkey >= 0)
947 set_error( STATUS_ACCESS_DENIED );
948 return -1;
951 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
952 free_subkey( parent, index );
953 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
954 return 0;
957 /* try to grow the array of values; return 1 if OK, 0 on error */
958 static int grow_values( struct key *key )
960 struct key_value *new_val;
961 int nb_values;
963 if (key->nb_values)
965 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
966 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
968 set_error( STATUS_NO_MEMORY );
969 return 0;
972 else
974 nb_values = MIN_VALUES;
975 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
977 key->values = new_val;
978 key->nb_values = nb_values;
979 return 1;
982 /* find the named value of a given key and return its index in the array */
983 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
985 int i, min, max, res;
986 data_size_t len;
988 min = 0;
989 max = key->last_value;
990 while (min <= max)
992 i = (min + max) / 2;
993 len = min( key->values[i].namelen, name->len );
994 res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
995 if (!res) res = key->values[i].namelen - name->len;
996 if (!res)
998 *index = i;
999 return &key->values[i];
1001 if (res > 0) max = i - 1;
1002 else min = i + 1;
1004 *index = min; /* this is where we should insert it */
1005 return NULL;
1008 /* insert a new value; the index must have been returned by find_value */
1009 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
1011 struct key_value *value;
1012 WCHAR *new_name = NULL;
1013 int i;
1015 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
1017 set_error( STATUS_NAME_TOO_LONG );
1018 return NULL;
1020 if (key->last_value + 1 == key->nb_values)
1022 if (!grow_values( key )) return NULL;
1024 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1025 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1026 value = &key->values[index];
1027 value->name = new_name;
1028 value->namelen = name->len;
1029 value->len = 0;
1030 value->data = NULL;
1031 return value;
1034 /* set a key value */
1035 static void set_value( struct key *key, const struct unicode_str *name,
1036 int type, const void *data, data_size_t len )
1038 struct key_value *value;
1039 void *ptr = NULL;
1040 int index;
1042 if ((value = find_value( key, name, &index )))
1044 /* check if the new value is identical to the existing one */
1045 if (value->type == type && value->len == len &&
1046 value->data && !memcmp( value->data, data, len ))
1048 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1049 return;
1053 if (key->flags & KEY_SYMLINK)
1055 if (type != REG_LINK || name->len != symlink_str.len ||
1056 memicmpW( name->str, symlink_str.str, name->len / sizeof(WCHAR) ))
1058 set_error( STATUS_ACCESS_DENIED );
1059 return;
1063 if (len && !(ptr = memdup( data, len ))) return;
1065 if (!value)
1067 if (!(value = insert_value( key, name, index )))
1069 free( ptr );
1070 return;
1073 else free( value->data ); /* already existing, free previous data */
1075 value->type = type;
1076 value->len = len;
1077 value->data = ptr;
1078 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1079 if (debug_level > 1) dump_operation( key, value, "Set" );
1082 /* get a key value */
1083 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1085 struct key_value *value;
1086 int index;
1088 if ((value = find_value( key, name, &index )))
1090 *type = value->type;
1091 *len = value->len;
1092 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1093 if (debug_level > 1) dump_operation( key, value, "Get" );
1095 else
1097 *type = -1;
1098 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1102 /* enumerate a key value */
1103 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1105 struct key_value *value;
1107 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1108 else
1110 void *data;
1111 data_size_t namelen, maxlen;
1113 value = &key->values[i];
1114 reply->type = value->type;
1115 namelen = value->namelen;
1117 switch(info_class)
1119 case KeyValueBasicInformation:
1120 reply->total = namelen;
1121 break;
1122 case KeyValueFullInformation:
1123 reply->total = namelen + value->len;
1124 break;
1125 case KeyValuePartialInformation:
1126 reply->total = value->len;
1127 namelen = 0;
1128 break;
1129 default:
1130 set_error( STATUS_INVALID_PARAMETER );
1131 return;
1134 maxlen = min( reply->total, get_reply_max_size() );
1135 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1137 if (maxlen > namelen)
1139 reply->namelen = namelen;
1140 memcpy( data, value->name, namelen );
1141 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1143 else
1145 reply->namelen = maxlen;
1146 memcpy( data, value->name, maxlen );
1149 if (debug_level > 1) dump_operation( key, value, "Enum" );
1153 /* delete a value */
1154 static void delete_value( struct key *key, const struct unicode_str *name )
1156 struct key_value *value;
1157 int i, index, nb_values;
1159 if (!(value = find_value( key, name, &index )))
1161 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1162 return;
1164 if (debug_level > 1) dump_operation( key, value, "Delete" );
1165 free( value->name );
1166 free( value->data );
1167 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1168 key->last_value--;
1169 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1171 /* try to shrink the array */
1172 nb_values = key->nb_values;
1173 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1175 struct key_value *new_val;
1176 nb_values -= nb_values / 3; /* shrink by 33% */
1177 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1178 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1179 key->values = new_val;
1180 key->nb_values = nb_values;
1184 /* get the registry key corresponding to an hkey handle */
1185 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1187 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1189 if (key && key->flags & KEY_DELETED)
1191 set_error( STATUS_KEY_DELETED );
1192 release_object( key );
1193 key = NULL;
1195 return key;
1198 /* get the registry key corresponding to a parent key handle */
1199 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1201 if (!hkey) return (struct key *)grab_object( root_key );
1202 return get_hkey_obj( hkey, 0 );
1205 /* read a line from the input file */
1206 static int read_next_line( struct file_load_info *info )
1208 char *newbuf;
1209 int newlen, pos = 0;
1211 info->line++;
1212 for (;;)
1214 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1215 return (pos != 0); /* EOF */
1216 pos = strlen(info->buffer);
1217 if (info->buffer[pos-1] == '\n')
1219 /* got a full line */
1220 info->buffer[--pos] = 0;
1221 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1222 return 1;
1224 if (pos < info->len - 1) return 1; /* EOF but something was read */
1226 /* need to enlarge the buffer */
1227 newlen = info->len + info->len / 2;
1228 if (!(newbuf = realloc( info->buffer, newlen )))
1230 set_error( STATUS_NO_MEMORY );
1231 return -1;
1233 info->buffer = newbuf;
1234 info->len = newlen;
1238 /* make sure the temp buffer holds enough space */
1239 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1241 WCHAR *tmp;
1242 if (info->tmplen >= size) return 1;
1243 if (!(tmp = realloc( info->tmp, size )))
1245 set_error( STATUS_NO_MEMORY );
1246 return 0;
1248 info->tmp = tmp;
1249 info->tmplen = size;
1250 return 1;
1253 /* report an error while loading an input file */
1254 static void file_read_error( const char *err, struct file_load_info *info )
1256 if (info->filename)
1257 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1258 else
1259 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1262 /* convert a data type tag to a value type */
1263 static int get_data_type( const char *buffer, int *type, int *parse_type )
1265 struct data_type { const char *tag; int len; int type; int parse_type; };
1267 static const struct data_type data_types[] =
1268 { /* actual type */ /* type to assume for parsing */
1269 { "\"", 1, REG_SZ, REG_SZ },
1270 { "str:\"", 5, REG_SZ, REG_SZ },
1271 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1272 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1273 { "hex:", 4, REG_BINARY, REG_BINARY },
1274 { "dword:", 6, REG_DWORD, REG_DWORD },
1275 { "hex(", 4, -1, REG_BINARY },
1276 { NULL, 0, 0, 0 }
1279 const struct data_type *ptr;
1280 char *end;
1282 for (ptr = data_types; ptr->tag; ptr++)
1284 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1285 *parse_type = ptr->parse_type;
1286 if ((*type = ptr->type) != -1) return ptr->len;
1287 /* "hex(xx):" is special */
1288 *type = (int)strtoul( buffer + 4, &end, 16 );
1289 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1290 return end + 2 - buffer;
1292 return 0;
1295 /* load and create a key from the input file */
1296 static struct key *load_key( struct key *base, const char *buffer,
1297 int prefix_len, struct file_load_info *info )
1299 WCHAR *p;
1300 struct unicode_str name;
1301 int res;
1302 unsigned int mod;
1303 timeout_t modif = current_time;
1304 data_size_t len;
1306 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1308 len = info->tmplen;
1309 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1311 file_read_error( "Malformed key", info );
1312 return NULL;
1314 if (sscanf( buffer + res, " %u", &mod ) == 1)
1315 modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1317 p = info->tmp;
1318 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1320 if (!*p)
1322 if (prefix_len > 1)
1324 file_read_error( "Malformed key", info );
1325 return NULL;
1327 /* empty key name, return base key */
1328 return (struct key *)grab_object( base );
1330 name.str = p;
1331 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1332 return create_key_recursive( base, &name, modif );
1335 /* load a global option from the input file */
1336 static int load_global_option( const char *buffer, struct file_load_info *info )
1338 const char *p;
1340 if (!strncmp( buffer, "#arch=", 6 ))
1342 enum prefix_type type;
1343 p = buffer + 6;
1344 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1345 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1346 else
1348 file_read_error( "Unknown architecture", info );
1349 set_error( STATUS_NOT_REGISTRY_FILE );
1350 return 0;
1352 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1353 else if (type != prefix_type)
1355 file_read_error( "Mismatched architecture", info );
1356 set_error( STATUS_NOT_REGISTRY_FILE );
1357 return 0;
1360 /* ignore unknown options */
1361 return 1;
1364 /* load a key option from the input file */
1365 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1367 const char *p;
1368 data_size_t len;
1370 if (!strncmp( buffer, "#class=", 7 ))
1372 p = buffer + 7;
1373 if (*p++ != '"') return 0;
1374 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1375 len = info->tmplen;
1376 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1377 free( key->class );
1378 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1379 key->classlen = len;
1381 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1382 /* ignore unknown options */
1383 return 1;
1386 /* parse a comma-separated list of hex digits */
1387 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1389 const char *p = buffer;
1390 data_size_t count = 0;
1391 char *end;
1393 while (isxdigit(*p))
1395 unsigned int val = strtoul( p, &end, 16 );
1396 if (end == p || val > 0xff) return -1;
1397 if (count++ >= *len) return -1; /* dest buffer overflow */
1398 *dest++ = val;
1399 p = end;
1400 while (isspace(*p)) p++;
1401 if (*p == ',') p++;
1402 while (isspace(*p)) p++;
1404 *len = count;
1405 return p - buffer;
1408 /* parse a value name and create the corresponding value */
1409 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1410 struct file_load_info *info )
1412 struct key_value *value;
1413 struct unicode_str name;
1414 int index;
1416 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1417 name.str = info->tmp;
1418 name.len = info->tmplen;
1419 if (buffer[0] == '@')
1421 name.len = 0;
1422 *len = 1;
1424 else
1426 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1427 if (r == -1) goto error;
1428 *len = r + 1; /* for initial quote */
1429 name.len -= sizeof(WCHAR); /* terminating null */
1431 while (isspace(buffer[*len])) (*len)++;
1432 if (buffer[*len] != '=') goto error;
1433 (*len)++;
1434 while (isspace(buffer[*len])) (*len)++;
1435 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1436 return value;
1438 error:
1439 file_read_error( "Malformed value name", info );
1440 return NULL;
1443 /* load a value from the input file */
1444 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1446 DWORD dw;
1447 void *ptr, *newptr;
1448 int res, type, parse_type;
1449 data_size_t maxlen, len;
1450 struct key_value *value;
1452 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1453 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1454 buffer += len + res;
1456 switch(parse_type)
1458 case REG_SZ:
1459 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1460 len = info->tmplen;
1461 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1462 ptr = info->tmp;
1463 break;
1464 case REG_DWORD:
1465 dw = strtoul( buffer, NULL, 16 );
1466 ptr = &dw;
1467 len = sizeof(dw);
1468 break;
1469 case REG_BINARY: /* hex digits */
1470 len = 0;
1471 for (;;)
1473 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1474 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1475 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1476 len += maxlen;
1477 buffer += res;
1478 while (isspace(*buffer)) buffer++;
1479 if (!*buffer) break;
1480 if (*buffer != '\\') goto error;
1481 if (read_next_line( info) != 1) goto error;
1482 buffer = info->buffer;
1483 while (isspace(*buffer)) buffer++;
1485 ptr = info->tmp;
1486 break;
1487 default:
1488 assert(0);
1489 ptr = NULL; /* keep compiler quiet */
1490 break;
1493 if (!len) newptr = NULL;
1494 else if (!(newptr = memdup( ptr, len ))) return 0;
1496 free( value->data );
1497 value->data = newptr;
1498 value->len = len;
1499 value->type = type;
1500 return 1;
1502 error:
1503 file_read_error( "Malformed value", info );
1504 free( value->data );
1505 value->data = NULL;
1506 value->len = 0;
1507 value->type = REG_NONE;
1508 return 0;
1511 /* return the length (in path elements) of name that is part of the key name */
1512 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1513 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1515 WCHAR *p;
1516 int res;
1517 data_size_t len;
1519 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1521 len = info->tmplen;
1522 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1524 file_read_error( "Malformed key", info );
1525 return 0;
1527 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1528 len = (p - info->tmp) * sizeof(WCHAR);
1529 for (res = 1; key != root_key; res++)
1531 if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
1532 key = key->parent;
1534 if (key == root_key) res = 0; /* no matching name */
1535 return res;
1538 /* load all the keys from the input file */
1539 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1540 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1542 struct key *subkey = NULL;
1543 struct file_load_info info;
1544 char *p;
1546 info.filename = filename;
1547 info.file = f;
1548 info.len = 4;
1549 info.tmplen = 4;
1550 info.line = 0;
1551 if (!(info.buffer = mem_alloc( info.len ))) return;
1552 if (!(info.tmp = mem_alloc( info.tmplen )))
1554 free( info.buffer );
1555 return;
1558 if ((read_next_line( &info ) != 1) ||
1559 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1561 set_error( STATUS_NOT_REGISTRY_FILE );
1562 goto done;
1565 while (read_next_line( &info ) == 1)
1567 p = info.buffer;
1568 while (*p && isspace(*p)) p++;
1569 switch(*p)
1571 case '[': /* new key */
1572 if (subkey) release_object( subkey );
1573 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1574 if (!(subkey = load_key( key, p + 1, prefix_len, &info )))
1575 file_read_error( "Error creating key", &info );
1576 break;
1577 case '@': /* default value */
1578 case '\"': /* value */
1579 if (subkey) load_value( subkey, p, &info );
1580 else file_read_error( "Value without key", &info );
1581 break;
1582 case '#': /* option */
1583 if (subkey) load_key_option( subkey, p, &info );
1584 else if (!load_global_option( p, &info )) goto done;
1585 break;
1586 case ';': /* comment */
1587 case 0: /* empty line */
1588 break;
1589 default:
1590 file_read_error( "Unrecognized input", &info );
1591 break;
1595 done:
1596 if (subkey) release_object( subkey );
1597 free( info.buffer );
1598 free( info.tmp );
1601 /* load a part of the registry from a file */
1602 static void load_registry( struct key *key, obj_handle_t handle )
1604 struct file *file;
1605 int fd;
1607 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1608 fd = dup( get_file_unix_fd( file ) );
1609 release_object( file );
1610 if (fd != -1)
1612 FILE *f = fdopen( fd, "r" );
1613 if (f)
1615 load_keys( key, NULL, f, -1 );
1616 fclose( f );
1618 else file_set_error();
1622 /* load one of the initial registry files */
1623 static int load_init_registry_from_file( const char *filename, struct key *key )
1625 FILE *f;
1627 if ((f = fopen( filename, "r" )))
1629 load_keys( key, filename, f, 0 );
1630 fclose( f );
1631 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1633 fprintf( stderr, "%s is not a valid registry file\n", filename );
1634 return 1;
1638 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1640 save_branch_info[save_branch_count].path = filename;
1641 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1642 make_object_static( &key->obj );
1643 return (f != NULL);
1646 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1648 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1649 static const WCHAR formatW[] = {'-','%','u',0};
1650 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1651 WCHAR *p = buffer;
1652 unsigned int i;
1654 strcpyW( p, prefixW );
1655 p += strlenW( prefixW );
1656 p += sprintfW( p, formatW, sid->Revision );
1657 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1658 sid->IdentifierAuthority.Value[4] ),
1659 MAKEWORD( sid->IdentifierAuthority.Value[3],
1660 sid->IdentifierAuthority.Value[2] )));
1661 for (i = 0; i < sid->SubAuthorityCount; i++)
1662 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1664 path->len = (p - buffer) * sizeof(WCHAR);
1665 path->str = p = memdup( buffer, path->len );
1666 return p;
1669 /* get the cpu architectures that can be supported in the current prefix */
1670 unsigned int get_prefix_cpu_mask(void)
1672 /* Allowed server/client/prefix combinations:
1674 * prefix
1675 * 32 64
1676 * server +------+------+ client
1677 * | ok | fail | 32
1678 * 32 +------+------+---
1679 * | fail | fail | 64
1680 * ---+------+------+---
1681 * | ok | ok | 32
1682 * 64 +------+------+---
1683 * | fail | ok | 64
1684 * ---+------+------+---
1686 switch (prefix_type)
1688 case PREFIX_64BIT:
1689 /* 64-bit prefix requires 64-bit server */
1690 return sizeof(void *) > sizeof(int) ? ~0 : 0;
1691 case PREFIX_32BIT:
1692 default:
1693 return ~CPU_64BIT_MASK; /* only 32-bit cpus supported on 32-bit prefix */
1697 /* registry initialisation */
1698 void init_registry(void)
1700 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1701 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1702 static const WCHAR classes[] = {'S','o','f','t','w','a','r','e','\\',
1703 'C','l','a','s','s','e','s','\\',
1704 'W','o','w','6','4','3','2','N','o','d','e'};
1705 static const struct unicode_str root_name = { NULL, 0 };
1706 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1707 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1708 static const struct unicode_str classes_name = { classes, sizeof(classes) };
1710 WCHAR *current_user_path;
1711 struct unicode_str current_user_str;
1712 struct key *key, *hklm, *hkcu;
1714 /* switch to the config dir */
1716 if (fchdir( config_dir_fd ) == -1) fatal_perror( "chdir to config dir" );
1718 /* create the root key */
1719 root_key = alloc_key( &root_name, current_time );
1720 assert( root_key );
1721 make_object_static( &root_key->obj );
1723 /* load system.reg into Registry\Machine */
1725 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1726 fatal_error( "could not create Machine registry key\n" );
1728 if (!load_init_registry_from_file( "system.reg", hklm ))
1729 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1730 else if (prefix_type == PREFIX_UNKNOWN)
1731 prefix_type = PREFIX_32BIT;
1733 /* load userdef.reg into Registry\User\.Default */
1735 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1736 fatal_error( "could not create User\\.Default registry key\n" );
1738 load_init_registry_from_file( "userdef.reg", key );
1739 release_object( key );
1741 /* load user.reg into HKEY_CURRENT_USER */
1743 /* FIXME: match default user in token.c. should get from process token instead */
1744 current_user_path = format_user_registry_path( security_local_user_sid, &current_user_str );
1745 if (!current_user_path ||
1746 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1747 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1748 free( current_user_path );
1749 load_init_registry_from_file( "user.reg", hkcu );
1751 /* set the shared flag on Software\Classes\Wow6432Node */
1752 if (prefix_type == PREFIX_64BIT)
1754 if ((key = create_key_recursive( hklm, &classes_name, current_time )))
1756 key->flags |= KEY_WOWSHARE;
1757 release_object( key );
1759 /* FIXME: handle HKCU too */
1762 release_object( hklm );
1763 release_object( hkcu );
1765 /* start the periodic save timer */
1766 set_periodic_save_timer();
1768 /* go back to the server dir */
1769 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1772 /* save a registry branch to a file */
1773 static void save_all_subkeys( struct key *key, FILE *f )
1775 fprintf( f, "WINE REGISTRY Version 2\n" );
1776 fprintf( f, ";; All keys relative to " );
1777 dump_path( key, NULL, f );
1778 fprintf( f, "\n" );
1779 switch (prefix_type)
1781 case PREFIX_32BIT:
1782 fprintf( f, "\n#arch=win32\n" );
1783 break;
1784 case PREFIX_64BIT:
1785 fprintf( f, "\n#arch=win64\n" );
1786 break;
1787 default:
1788 break;
1790 save_subkeys( key, key, f );
1793 /* save a registry branch to a file handle */
1794 static void save_registry( struct key *key, obj_handle_t handle )
1796 struct file *file;
1797 int fd;
1799 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1800 fd = dup( get_file_unix_fd( file ) );
1801 release_object( file );
1802 if (fd != -1)
1804 FILE *f = fdopen( fd, "w" );
1805 if (f)
1807 save_all_subkeys( key, f );
1808 if (fclose( f )) file_set_error();
1810 else
1812 file_set_error();
1813 close( fd );
1818 /* save a registry branch to a file */
1819 static int save_branch( struct key *key, const char *path )
1821 struct stat st;
1822 char *p, *tmp = NULL;
1823 int fd, count = 0, ret = 0;
1824 FILE *f;
1826 if (!(key->flags & KEY_DIRTY))
1828 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1829 return 1;
1832 /* test the file type */
1834 if ((fd = open( path, O_WRONLY )) != -1)
1836 /* if file is not a regular file or has multiple links or is accessed
1837 * via symbolic links, write directly into it; otherwise use a temp file */
1838 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1840 ftruncate( fd, 0 );
1841 goto save;
1843 close( fd );
1846 /* create a temp file in the same directory */
1848 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1849 strcpy( tmp, path );
1850 if ((p = strrchr( tmp, '/' ))) p++;
1851 else p = tmp;
1852 for (;;)
1854 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1855 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1856 if (errno != EEXIST) goto done;
1857 close( fd );
1860 /* now save to it */
1862 save:
1863 if (!(f = fdopen( fd, "w" )))
1865 if (tmp) unlink( tmp );
1866 close( fd );
1867 goto done;
1870 if (debug_level > 1)
1872 fprintf( stderr, "%s: ", path );
1873 dump_operation( key, NULL, "saving" );
1876 save_all_subkeys( key, f );
1877 ret = !fclose(f);
1879 if (tmp)
1881 /* if successfully written, rename to final name */
1882 if (ret) ret = !rename( tmp, path );
1883 if (!ret) unlink( tmp );
1886 done:
1887 free( tmp );
1888 if (ret) make_clean( key );
1889 return ret;
1892 /* periodic saving of the registry */
1893 static void periodic_save( void *arg )
1895 int i;
1897 if (fchdir( config_dir_fd ) == -1) return;
1898 save_timeout_user = NULL;
1899 for (i = 0; i < save_branch_count; i++)
1900 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1901 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1902 set_periodic_save_timer();
1905 /* start the periodic save timer */
1906 static void set_periodic_save_timer(void)
1908 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1909 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
1912 /* save the modified registry branches to disk */
1913 void flush_registry(void)
1915 int i;
1917 if (fchdir( config_dir_fd ) == -1) return;
1918 for (i = 0; i < save_branch_count; i++)
1920 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1922 fprintf( stderr, "wineserver: could not save registry branch to %s",
1923 save_branch_info[i].path );
1924 perror( " " );
1927 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1930 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
1931 static int is_wow64_thread( struct thread *thread )
1933 return (prefix_type == PREFIX_64BIT && !(CPU_FLAG(thread->process->cpu) & CPU_64BIT_MASK));
1937 /* create a registry key */
1938 DECL_HANDLER(create_key)
1940 struct key *key = NULL, *parent;
1941 struct unicode_str name, class;
1942 unsigned int access = req->access;
1944 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1946 reply->hkey = 0;
1948 if (req->namelen > get_req_data_size())
1950 set_error( STATUS_INVALID_PARAMETER );
1951 return;
1953 class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1954 class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1955 get_req_path( &name, !req->parent );
1956 if (name.str > class.str)
1958 set_error( STATUS_INVALID_PARAMETER );
1959 return;
1961 name.len = (class.str - name.str) * sizeof(WCHAR);
1963 /* NOTE: no access rights are required from the parent handle to create a key */
1964 if ((parent = get_parent_hkey_obj( req->parent )))
1966 if ((key = create_key( parent, &name, &class, req->options, access,
1967 req->attributes, &reply->created )))
1969 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1970 release_object( key );
1972 release_object( parent );
1976 /* open a registry key */
1977 DECL_HANDLER(open_key)
1979 struct key *key, *parent;
1980 struct unicode_str name;
1981 unsigned int access = req->access;
1983 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1985 reply->hkey = 0;
1986 /* NOTE: no access rights are required to open the parent key, only the child key */
1987 if ((parent = get_parent_hkey_obj( req->parent )))
1989 get_req_path( &name, !req->parent );
1990 if ((key = open_key( parent, &name, access, req->attributes )))
1992 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1993 release_object( key );
1995 release_object( parent );
1999 /* delete a registry key */
2000 DECL_HANDLER(delete_key)
2002 struct key *key;
2004 if ((key = get_hkey_obj( req->hkey, DELETE )))
2006 delete_key( key, 0);
2007 release_object( key );
2011 /* flush a registry key */
2012 DECL_HANDLER(flush_key)
2014 struct key *key = get_hkey_obj( req->hkey, 0 );
2015 if (key)
2017 /* we don't need to do anything here with the current implementation */
2018 release_object( key );
2022 /* enumerate registry subkeys */
2023 DECL_HANDLER(enum_key)
2025 struct key *key;
2027 if ((key = get_hkey_obj( req->hkey,
2028 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
2030 enum_key( key, req->index, req->info_class, reply );
2031 release_object( key );
2035 /* set a value of a registry key */
2036 DECL_HANDLER(set_key_value)
2038 struct key *key;
2039 struct unicode_str name;
2041 if (req->namelen > get_req_data_size())
2043 set_error( STATUS_INVALID_PARAMETER );
2044 return;
2046 name.str = get_req_data();
2047 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2049 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2051 data_size_t datalen = get_req_data_size() - req->namelen;
2052 const char *data = (const char *)get_req_data() + req->namelen;
2054 set_value( key, &name, req->type, data, datalen );
2055 release_object( key );
2059 /* retrieve the value of a registry key */
2060 DECL_HANDLER(get_key_value)
2062 struct key *key;
2063 struct unicode_str name;
2065 reply->total = 0;
2066 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2068 get_req_unicode_str( &name );
2069 get_value( key, &name, &reply->type, &reply->total );
2070 release_object( key );
2074 /* enumerate the value of a registry key */
2075 DECL_HANDLER(enum_key_value)
2077 struct key *key;
2079 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2081 enum_value( key, req->index, req->info_class, reply );
2082 release_object( key );
2086 /* delete a value of a registry key */
2087 DECL_HANDLER(delete_key_value)
2089 struct key *key;
2090 struct unicode_str name;
2092 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2094 get_req_unicode_str( &name );
2095 delete_value( key, &name );
2096 release_object( key );
2100 /* load a registry branch from a file */
2101 DECL_HANDLER(load_registry)
2103 struct key *key, *parent;
2104 struct token *token = thread_get_impersonation_token( current );
2105 struct unicode_str name;
2107 const LUID_AND_ATTRIBUTES privs[] =
2109 { SeBackupPrivilege, 0 },
2110 { SeRestorePrivilege, 0 },
2113 if (!token || !token_check_privileges( token, TRUE, privs,
2114 sizeof(privs)/sizeof(privs[0]), NULL ))
2116 set_error( STATUS_PRIVILEGE_NOT_HELD );
2117 return;
2120 if ((parent = get_parent_hkey_obj( req->hkey )))
2122 int dummy;
2123 get_req_path( &name, !req->hkey );
2124 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, &dummy )))
2126 load_registry( key, req->file );
2127 release_object( key );
2129 release_object( parent );
2133 DECL_HANDLER(unload_registry)
2135 struct key *key;
2136 struct token *token = thread_get_impersonation_token( current );
2138 const LUID_AND_ATTRIBUTES privs[] =
2140 { SeBackupPrivilege, 0 },
2141 { SeRestorePrivilege, 0 },
2144 if (!token || !token_check_privileges( token, TRUE, privs,
2145 sizeof(privs)/sizeof(privs[0]), NULL ))
2147 set_error( STATUS_PRIVILEGE_NOT_HELD );
2148 return;
2151 if ((key = get_hkey_obj( req->hkey, 0 )))
2153 delete_key( key, 1 ); /* FIXME */
2154 release_object( key );
2158 /* save a registry branch to a file */
2159 DECL_HANDLER(save_registry)
2161 struct key *key;
2163 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2165 set_error( STATUS_PRIVILEGE_NOT_HELD );
2166 return;
2169 if ((key = get_hkey_obj( req->hkey, 0 )))
2171 save_registry( key, req->file );
2172 release_object( key );
2176 /* add a registry key change notification */
2177 DECL_HANDLER(set_registry_notification)
2179 struct key *key;
2180 struct event *event;
2181 struct notify *notify;
2183 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2184 if (key)
2186 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2187 if (event)
2189 notify = find_notify( key, current->process, req->hkey );
2190 if (notify)
2192 if (notify->event)
2193 release_object( notify->event );
2194 grab_object( event );
2195 notify->event = event;
2197 else
2199 notify = mem_alloc( sizeof(*notify) );
2200 if (notify)
2202 grab_object( event );
2203 notify->event = event;
2204 notify->subtree = req->subtree;
2205 notify->filter = req->filter;
2206 notify->hkey = req->hkey;
2207 notify->process = current->process;
2208 list_add_head( &key->notify_list, &notify->entry );
2211 release_object( event );
2213 release_object( key );