msi: Skip the FindRelatedProducts action when product is already installed.
[wine.git] / server / registry.c
blobad57bd21d19f751addbaf9bf4501160be8d54220
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 "unicode.h"
47 #include "security.h"
49 #include "winternl.h"
50 #include "wine/library.h"
52 struct notify
54 struct list entry; /* entry in list of notifications */
55 struct event *event; /* event to set when changing this key */
56 int subtree; /* true if subtree notification */
57 unsigned int filter; /* which events to notify on */
58 obj_handle_t hkey; /* hkey associated with this notification */
59 struct process *process; /* process in which the hkey is valid */
62 /* a registry key */
63 struct key
65 struct object obj; /* object header */
66 WCHAR *name; /* key name */
67 WCHAR *class; /* key class */
68 unsigned short namelen; /* length of key name */
69 unsigned short classlen; /* length of class name */
70 struct key *parent; /* parent key */
71 int last_subkey; /* last in use subkey */
72 int nb_subkeys; /* count of allocated subkeys */
73 struct key **subkeys; /* subkeys array */
74 int last_value; /* last in use value */
75 int nb_values; /* count of allocated values in array */
76 struct key_value *values; /* values array */
77 unsigned int flags; /* flags */
78 timeout_t modif; /* last modification time */
79 struct list notify_list; /* list of notifications */
82 /* key flags */
83 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
84 #define KEY_DELETED 0x0002 /* key has been deleted */
85 #define KEY_DIRTY 0x0004 /* key has been modified */
86 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
87 #define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
89 /* a key value */
90 struct key_value
92 WCHAR *name; /* value name */
93 unsigned short namelen; /* length of value name */
94 unsigned short type; /* value type */
95 data_size_t len; /* value data length in bytes */
96 void *data; /* pointer to value data */
99 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
100 #define MIN_VALUES 8 /* min. number of allocated values per key */
102 #define MAX_NAME_LEN MAX_PATH /* max. length of a key name */
103 #define MAX_VALUE_LEN MAX_PATH /* max. length of a value name */
105 /* the root of the registry tree */
106 static struct key *root_key;
108 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
109 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
110 static struct timeout_user *save_timeout_user; /* saving timer */
112 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
113 static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
114 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
115 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
117 static void set_periodic_save_timer(void);
118 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
120 /* information about where to save a registry branch */
121 struct save_branch_info
123 struct key *key;
124 const char *path;
127 #define MAX_SAVE_BRANCH_INFO 3
128 static int save_branch_count;
129 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
132 /* information about a file being loaded */
133 struct file_load_info
135 const char *filename; /* input file name */
136 FILE *file; /* input file */
137 char *buffer; /* line buffer */
138 int len; /* buffer length */
139 int line; /* current input line */
140 WCHAR *tmp; /* temp buffer to use while parsing input */
141 size_t tmplen; /* length of temp buffer */
145 static void key_dump( struct object *obj, int verbose );
146 static unsigned int key_map_access( struct object *obj, unsigned int access );
147 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
148 static void key_destroy( struct object *obj );
150 static const struct object_ops key_ops =
152 sizeof(struct key), /* size */
153 key_dump, /* dump */
154 no_get_type, /* get_type */
155 no_add_queue, /* add_queue */
156 NULL, /* remove_queue */
157 NULL, /* signaled */
158 NULL, /* satisfied */
159 no_signal, /* signal */
160 no_get_fd, /* get_fd */
161 key_map_access, /* map_access */
162 default_get_sd, /* get_sd */
163 default_set_sd, /* set_sd */
164 no_lookup_name, /* lookup_name */
165 no_open_file, /* open_file */
166 key_close_handle, /* close_handle */
167 key_destroy /* destroy */
171 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
173 return (len == sizeof(wow6432node) &&
174 !memicmpW( name, wow6432node, sizeof(wow6432node)/sizeof(WCHAR) ));
178 * The registry text file format v2 used by this code is similar to the one
179 * used by REGEDIT import/export functionality, with the following differences:
180 * - strings and key names can contain \x escapes for Unicode
181 * - key names use escapes too in order to support Unicode
182 * - the modification time optionally follows the key name
183 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
186 /* dump the full path of a key */
187 static void dump_path( const struct key *key, const struct key *base, FILE *f )
189 if (key->parent && key->parent != base)
191 dump_path( key->parent, base, f );
192 fprintf( f, "\\\\" );
194 dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
197 /* dump a value to a text file */
198 static void dump_value( const struct key_value *value, FILE *f )
200 unsigned int i, dw;
201 int count;
203 if (value->namelen)
205 fputc( '\"', f );
206 count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
207 count += fprintf( f, "\"=" );
209 else count = fprintf( f, "@=" );
211 switch(value->type)
213 case REG_SZ:
214 case REG_EXPAND_SZ:
215 case REG_MULTI_SZ:
216 /* only output properly terminated strings in string format */
217 if (value->len < sizeof(WCHAR)) break;
218 if (value->len % sizeof(WCHAR)) break;
219 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
220 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
221 fputc( '\"', f );
222 dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
223 fprintf( f, "\"\n" );
224 return;
226 case REG_DWORD:
227 if (value->len != sizeof(dw)) break;
228 memcpy( &dw, value->data, sizeof(dw) );
229 fprintf( f, "dword:%08x\n", dw );
230 return;
233 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
234 else count += fprintf( f, "hex(%x):", value->type );
235 for (i = 0; i < value->len; i++)
237 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
238 if (i < value->len-1)
240 fputc( ',', f );
241 if (++count > 76)
243 fprintf( f, "\\\n " );
244 count = 2;
248 fputc( '\n', f );
251 /* save a registry and all its subkeys to a text file */
252 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
254 int i;
256 if (key->flags & KEY_VOLATILE) return;
257 /* save key if it has either some values or no subkeys, or needs special options */
258 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
259 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
261 fprintf( f, "\n[" );
262 if (key != base) dump_path( key, base, f );
263 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
264 if (key->class)
266 fprintf( f, "#class=\"" );
267 dump_strW( key->class, key->classlen / sizeof(WCHAR), f, "\"\"" );
268 fprintf( f, "\"\n" );
270 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
271 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
273 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
276 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
278 fprintf( stderr, "%s key ", op );
279 if (key) dump_path( key, NULL, stderr );
280 else fprintf( stderr, "ERROR" );
281 if (value)
283 fprintf( stderr, " value ");
284 dump_value( value, stderr );
286 else fprintf( stderr, "\n" );
289 static void key_dump( struct object *obj, int verbose )
291 struct key *key = (struct key *)obj;
292 assert( obj->ops == &key_ops );
293 fprintf( stderr, "Key flags=%x ", key->flags );
294 dump_path( key, NULL, stderr );
295 fprintf( stderr, "\n" );
298 /* notify waiter and maybe delete the notification */
299 static void do_notification( struct key *key, struct notify *notify, int del )
301 if (notify->event)
303 set_event( notify->event );
304 release_object( notify->event );
305 notify->event = NULL;
307 if (del)
309 list_remove( &notify->entry );
310 free( notify );
314 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
316 struct notify *notify;
318 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
320 if (notify->process == process && notify->hkey == hkey) return notify;
322 return NULL;
325 static unsigned int key_map_access( struct object *obj, unsigned int access )
327 if (access & GENERIC_READ) access |= KEY_READ;
328 if (access & GENERIC_WRITE) access |= KEY_WRITE;
329 if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
330 if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS;
331 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
334 /* close the notification associated with a handle */
335 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
337 struct key * key = (struct key *) obj;
338 struct notify *notify = find_notify( key, process, handle );
339 if (notify) do_notification( key, notify, 1 );
340 return 1; /* ok to close */
343 static void key_destroy( struct object *obj )
345 int i;
346 struct list *ptr;
347 struct key *key = (struct key *)obj;
348 assert( obj->ops == &key_ops );
350 free( key->name );
351 free( key->class );
352 for (i = 0; i <= key->last_value; i++)
354 free( key->values[i].name );
355 free( key->values[i].data );
357 free( key->values );
358 for (i = 0; i <= key->last_subkey; i++)
360 key->subkeys[i]->parent = NULL;
361 release_object( key->subkeys[i] );
363 free( key->subkeys );
364 /* unconditionally notify everything waiting on this key */
365 while ((ptr = list_head( &key->notify_list )))
367 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
368 do_notification( key, notify, 1 );
372 /* get the request vararg as registry path */
373 static inline void get_req_path( struct unicode_str *str, int skip_root )
375 str->str = get_req_data();
376 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
378 if (skip_root && str->len >= sizeof(root_name) &&
379 !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
381 str->str += sizeof(root_name)/sizeof(WCHAR);
382 str->len -= sizeof(root_name);
386 /* return the next token in a given path */
387 /* token->str must point inside the path, or be NULL for the first call */
388 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
390 data_size_t i = 0, len = path->len / sizeof(WCHAR);
392 if (!token->str) /* first time */
394 /* path cannot start with a backslash */
395 if (len && path->str[0] == '\\')
397 set_error( STATUS_OBJECT_PATH_INVALID );
398 return NULL;
401 else
403 i = token->str - path->str;
404 i += token->len / sizeof(WCHAR);
405 while (i < len && path->str[i] == '\\') i++;
407 token->str = path->str + i;
408 while (i < len && path->str[i] != '\\') i++;
409 token->len = (path->str + i - token->str) * sizeof(WCHAR);
410 return token;
413 /* allocate a key object */
414 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
416 struct key *key;
417 if ((key = alloc_object( &key_ops )))
419 key->name = NULL;
420 key->class = NULL;
421 key->namelen = name->len;
422 key->classlen = 0;
423 key->flags = 0;
424 key->last_subkey = -1;
425 key->nb_subkeys = 0;
426 key->subkeys = NULL;
427 key->nb_values = 0;
428 key->last_value = -1;
429 key->values = NULL;
430 key->modif = modif;
431 key->parent = NULL;
432 list_init( &key->notify_list );
433 if (name->len && !(key->name = memdup( name->str, name->len )))
435 release_object( key );
436 key = NULL;
439 return key;
442 /* mark a key and all its parents as dirty (modified) */
443 static void make_dirty( struct key *key )
445 while (key)
447 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
448 key->flags |= KEY_DIRTY;
449 key = key->parent;
453 /* mark a key and all its subkeys as clean (not modified) */
454 static void make_clean( struct key *key )
456 int i;
458 if (key->flags & KEY_VOLATILE) return;
459 if (!(key->flags & KEY_DIRTY)) return;
460 key->flags &= ~KEY_DIRTY;
461 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
464 /* go through all the notifications and send them if necessary */
465 static void check_notify( struct key *key, unsigned int change, int not_subtree )
467 struct list *ptr, *next;
469 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
471 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
472 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
473 do_notification( key, n, 0 );
477 /* update key modification time */
478 static void touch_key( struct key *key, unsigned int change )
480 struct key *k;
482 key->modif = current_time;
483 make_dirty( key );
485 /* do notifications */
486 check_notify( key, change, 1 );
487 for ( k = key->parent; k; k = k->parent )
488 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
491 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
492 static int grow_subkeys( struct key *key )
494 struct key **new_subkeys;
495 int nb_subkeys;
497 if (key->nb_subkeys)
499 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
500 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
502 set_error( STATUS_NO_MEMORY );
503 return 0;
506 else
508 nb_subkeys = MIN_VALUES;
509 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
511 key->subkeys = new_subkeys;
512 key->nb_subkeys = nb_subkeys;
513 return 1;
516 /* allocate a subkey for a given key, and return its index */
517 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
518 int index, timeout_t modif )
520 struct key *key;
521 int i;
523 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
525 set_error( STATUS_NAME_TOO_LONG );
526 return NULL;
528 if (parent->last_subkey + 1 == parent->nb_subkeys)
530 /* need to grow the array */
531 if (!grow_subkeys( parent )) return NULL;
533 if ((key = alloc_key( name, modif )) != NULL)
535 key->parent = parent;
536 for (i = ++parent->last_subkey; i > index; i--)
537 parent->subkeys[i] = parent->subkeys[i-1];
538 parent->subkeys[index] = key;
539 if (is_wow6432node( key->name, key->namelen )) parent->flags |= KEY_WOW64;
541 return key;
544 /* free a subkey of a given key */
545 static void free_subkey( struct key *parent, int index )
547 struct key *key;
548 int i, nb_subkeys;
550 assert( index >= 0 );
551 assert( index <= parent->last_subkey );
553 key = parent->subkeys[index];
554 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
555 parent->last_subkey--;
556 key->flags |= KEY_DELETED;
557 key->parent = NULL;
558 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
559 release_object( key );
561 /* try to shrink the array */
562 nb_subkeys = parent->nb_subkeys;
563 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
565 struct key **new_subkeys;
566 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
567 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
568 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
569 parent->subkeys = new_subkeys;
570 parent->nb_subkeys = nb_subkeys;
574 /* find the named child of a given key and return its index */
575 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
577 int i, min, max, res;
578 data_size_t len;
580 min = 0;
581 max = key->last_subkey;
582 while (min <= max)
584 i = (min + max) / 2;
585 len = min( key->subkeys[i]->namelen, name->len );
586 res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
587 if (!res) res = key->subkeys[i]->namelen - name->len;
588 if (!res)
590 *index = i;
591 return key->subkeys[i];
593 if (res > 0) max = i - 1;
594 else min = i + 1;
596 *index = min; /* this is where we should insert it */
597 return NULL;
600 /* return the wow64 variant of the key, or the key itself if none */
601 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
603 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
604 int index;
606 if (!(key->flags & KEY_WOW64)) return key;
607 if (!is_wow6432node( name->str, name->len ))
609 key = find_subkey( key, &wow6432node_str, &index );
610 assert( key ); /* if KEY_WOW64 is set we must find it */
612 return key;
616 /* follow a symlink and return the resolved key */
617 static struct key *follow_symlink( struct key *key, int iteration )
619 struct unicode_str path, token;
620 struct key_value *value;
621 int index;
623 if (iteration > 16) return NULL;
624 if (!(key->flags & KEY_SYMLINK)) return key;
625 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
627 path.str = value->data;
628 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
629 if (path.len <= sizeof(root_name)) return NULL;
630 if (memicmpW( path.str, root_name, sizeof(root_name)/sizeof(WCHAR) )) return NULL;
631 path.str += sizeof(root_name) / sizeof(WCHAR);
632 path.len -= sizeof(root_name);
634 key = root_key;
635 token.str = NULL;
636 if (!get_path_token( &path, &token )) return NULL;
637 while (token.len)
639 if (!(key = find_subkey( key, &token, &index ))) break;
640 if (!(key = follow_symlink( key, iteration + 1 ))) break;
641 get_path_token( &path, &token );
643 return key;
646 /* open a subkey */
647 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
648 unsigned int attributes )
650 int index;
651 struct unicode_str token;
653 token.str = NULL;
654 if (!get_path_token( name, &token )) return NULL;
655 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, &token );
656 while (token.len)
658 if (!(key = find_subkey( key, &token, &index )))
660 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
661 return NULL;
663 get_path_token( name, &token );
664 if (!token.len) break;
665 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
666 if (!(key = follow_symlink( key, 0 )))
668 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
669 return NULL;
673 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
674 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
676 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
677 return NULL;
679 if (debug_level > 1) dump_operation( key, NULL, "Open" );
680 grab_object( key );
681 return key;
684 /* create a subkey */
685 static struct key *create_key( struct key *key, const struct unicode_str *name,
686 const struct unicode_str *class, unsigned int options,
687 unsigned int access, unsigned int attributes, int *created )
689 int index;
690 struct unicode_str token, next;
692 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
694 set_error( STATUS_KEY_DELETED );
695 return NULL;
698 token.str = NULL;
699 if (!get_path_token( name, &token )) return NULL;
700 *created = 0;
701 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, &token );
702 while (token.len)
704 struct key *subkey;
705 if (!(subkey = find_subkey( key, &token, &index ))) break;
706 key = subkey;
707 get_path_token( name, &token );
708 if (!token.len) break;
709 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
710 if (!(key = follow_symlink( key, 0 )))
712 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
713 return NULL;
717 if (!token.len) /* the key already exists */
719 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
720 if (options & REG_OPTION_CREATE_LINK)
722 set_error( STATUS_OBJECT_NAME_COLLISION );
723 return NULL;
725 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
727 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
728 return NULL;
730 if (debug_level > 1) dump_operation( key, NULL, "Open" );
731 grab_object( key );
732 return key;
735 /* token must be the last path component at this point */
736 next = token;
737 get_path_token( name, &next );
738 if (next.len)
740 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
741 return NULL;
744 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
746 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
747 return NULL;
749 *created = 1;
750 make_dirty( key );
751 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
753 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
754 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
755 else key->flags |= KEY_DIRTY;
757 if (debug_level > 1) dump_operation( key, NULL, "Create" );
758 if (class && class->len)
760 key->classlen = class->len;
761 free(key->class);
762 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
764 grab_object( key );
765 return key;
768 /* recursively create a subkey (for internal use only) */
769 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
771 struct key *base;
772 int index;
773 struct unicode_str token;
775 token.str = NULL;
776 if (!get_path_token( name, &token )) return NULL;
777 while (token.len)
779 struct key *subkey;
780 if (!(subkey = find_subkey( key, &token, &index ))) break;
781 key = subkey;
782 if (!(key = follow_symlink( key, 0 )))
784 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
785 return NULL;
787 get_path_token( name, &token );
790 if (token.len)
792 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
793 base = key;
794 for (;;)
796 get_path_token( name, &token );
797 if (!token.len) break;
798 /* we know the index is always 0 in a new key */
799 if (!(key = alloc_subkey( key, &token, 0, modif )))
801 free_subkey( base, index );
802 return NULL;
807 grab_object( key );
808 return key;
811 /* query information about a key or a subkey */
812 static void enum_key( const struct key *key, int index, int info_class,
813 struct enum_key_reply *reply )
815 int i;
816 data_size_t len, namelen, classlen;
817 data_size_t max_subkey = 0, max_class = 0;
818 data_size_t max_value = 0, max_data = 0;
819 char *data;
821 if (index != -1) /* -1 means use the specified key directly */
823 if ((index < 0) || (index > key->last_subkey))
825 set_error( STATUS_NO_MORE_ENTRIES );
826 return;
828 key = key->subkeys[index];
831 namelen = key->namelen;
832 classlen = key->classlen;
834 switch(info_class)
836 case KeyBasicInformation:
837 classlen = 0; /* only return the name */
838 /* fall through */
839 case KeyNodeInformation:
840 reply->max_subkey = 0;
841 reply->max_class = 0;
842 reply->max_value = 0;
843 reply->max_data = 0;
844 break;
845 case KeyFullInformation:
846 for (i = 0; i <= key->last_subkey; i++)
848 struct key *subkey = key->subkeys[i];
849 len = subkey->namelen / sizeof(WCHAR);
850 if (len > max_subkey) max_subkey = len;
851 len = subkey->classlen / sizeof(WCHAR);
852 if (len > max_class) max_class = len;
854 for (i = 0; i <= key->last_value; i++)
856 len = key->values[i].namelen / sizeof(WCHAR);
857 if (len > max_value) max_value = len;
858 len = key->values[i].len;
859 if (len > max_data) max_data = len;
861 reply->max_subkey = max_subkey;
862 reply->max_class = max_class;
863 reply->max_value = max_value;
864 reply->max_data = max_data;
865 namelen = 0; /* only return the class */
866 break;
867 default:
868 set_error( STATUS_INVALID_PARAMETER );
869 return;
871 reply->subkeys = key->last_subkey + 1;
872 reply->values = key->last_value + 1;
873 reply->modif = key->modif;
874 reply->total = namelen + classlen;
876 len = min( reply->total, get_reply_max_size() );
877 if (len && (data = set_reply_data_size( len )))
879 if (len > namelen)
881 reply->namelen = namelen;
882 memcpy( data, key->name, namelen );
883 memcpy( data + namelen, key->class, len - namelen );
885 else
887 reply->namelen = len;
888 memcpy( data, key->name, len );
891 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
894 /* delete a key and its values */
895 static int delete_key( struct key *key, int recurse )
897 int index;
898 struct key *parent;
900 /* must find parent and index */
901 if (key == root_key)
903 set_error( STATUS_ACCESS_DENIED );
904 return -1;
906 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
908 set_error( STATUS_KEY_DELETED );
909 return -1;
912 while (recurse && (key->last_subkey>=0))
913 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
914 return -1;
916 for (index = 0; index <= parent->last_subkey; index++)
917 if (parent->subkeys[index] == key) break;
918 assert( index <= parent->last_subkey );
920 /* we can only delete a key that has no subkeys */
921 if (key->last_subkey >= 0)
923 set_error( STATUS_ACCESS_DENIED );
924 return -1;
927 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
928 free_subkey( parent, index );
929 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
930 return 0;
933 /* try to grow the array of values; return 1 if OK, 0 on error */
934 static int grow_values( struct key *key )
936 struct key_value *new_val;
937 int nb_values;
939 if (key->nb_values)
941 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
942 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
944 set_error( STATUS_NO_MEMORY );
945 return 0;
948 else
950 nb_values = MIN_VALUES;
951 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
953 key->values = new_val;
954 key->nb_values = nb_values;
955 return 1;
958 /* find the named value of a given key and return its index in the array */
959 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
961 int i, min, max, res;
962 data_size_t len;
964 min = 0;
965 max = key->last_value;
966 while (min <= max)
968 i = (min + max) / 2;
969 len = min( key->values[i].namelen, name->len );
970 res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
971 if (!res) res = key->values[i].namelen - name->len;
972 if (!res)
974 *index = i;
975 return &key->values[i];
977 if (res > 0) max = i - 1;
978 else min = i + 1;
980 *index = min; /* this is where we should insert it */
981 return NULL;
984 /* insert a new value; the index must have been returned by find_value */
985 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
987 struct key_value *value;
988 WCHAR *new_name = NULL;
989 int i;
991 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
993 set_error( STATUS_NAME_TOO_LONG );
994 return NULL;
996 if (key->last_value + 1 == key->nb_values)
998 if (!grow_values( key )) return NULL;
1000 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
1001 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1002 value = &key->values[index];
1003 value->name = new_name;
1004 value->namelen = name->len;
1005 value->len = 0;
1006 value->data = NULL;
1007 return value;
1010 /* set a key value */
1011 static void set_value( struct key *key, const struct unicode_str *name,
1012 int type, const void *data, data_size_t len )
1014 struct key_value *value;
1015 void *ptr = NULL;
1016 int index;
1018 if ((value = find_value( key, name, &index )))
1020 /* check if the new value is identical to the existing one */
1021 if (value->type == type && value->len == len &&
1022 value->data && !memcmp( value->data, data, len ))
1024 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1025 return;
1029 if (key->flags & KEY_SYMLINK)
1031 if (type != REG_LINK || name->len != symlink_str.len ||
1032 memicmpW( name->str, symlink_str.str, name->len / sizeof(WCHAR) ))
1034 set_error( STATUS_ACCESS_DENIED );
1035 return;
1039 if (len && !(ptr = memdup( data, len ))) return;
1041 if (!value)
1043 if (!(value = insert_value( key, name, index )))
1045 free( ptr );
1046 return;
1049 else free( value->data ); /* already existing, free previous data */
1051 value->type = type;
1052 value->len = len;
1053 value->data = ptr;
1054 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1055 if (debug_level > 1) dump_operation( key, value, "Set" );
1058 /* get a key value */
1059 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1061 struct key_value *value;
1062 int index;
1064 if ((value = find_value( key, name, &index )))
1066 *type = value->type;
1067 *len = value->len;
1068 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1069 if (debug_level > 1) dump_operation( key, value, "Get" );
1071 else
1073 *type = -1;
1074 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1078 /* enumerate a key value */
1079 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1081 struct key_value *value;
1083 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1084 else
1086 void *data;
1087 data_size_t namelen, maxlen;
1089 value = &key->values[i];
1090 reply->type = value->type;
1091 namelen = value->namelen;
1093 switch(info_class)
1095 case KeyValueBasicInformation:
1096 reply->total = namelen;
1097 break;
1098 case KeyValueFullInformation:
1099 reply->total = namelen + value->len;
1100 break;
1101 case KeyValuePartialInformation:
1102 reply->total = value->len;
1103 namelen = 0;
1104 break;
1105 default:
1106 set_error( STATUS_INVALID_PARAMETER );
1107 return;
1110 maxlen = min( reply->total, get_reply_max_size() );
1111 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1113 if (maxlen > namelen)
1115 reply->namelen = namelen;
1116 memcpy( data, value->name, namelen );
1117 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1119 else
1121 reply->namelen = maxlen;
1122 memcpy( data, value->name, maxlen );
1125 if (debug_level > 1) dump_operation( key, value, "Enum" );
1129 /* delete a value */
1130 static void delete_value( struct key *key, const struct unicode_str *name )
1132 struct key_value *value;
1133 int i, index, nb_values;
1135 if (!(value = find_value( key, name, &index )))
1137 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1138 return;
1140 if (debug_level > 1) dump_operation( key, value, "Delete" );
1141 free( value->name );
1142 free( value->data );
1143 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1144 key->last_value--;
1145 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1147 /* try to shrink the array */
1148 nb_values = key->nb_values;
1149 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1151 struct key_value *new_val;
1152 nb_values -= nb_values / 3; /* shrink by 33% */
1153 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1154 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1155 key->values = new_val;
1156 key->nb_values = nb_values;
1160 /* get the registry key corresponding to an hkey handle */
1161 static inline struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1163 return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1166 /* get the registry key corresponding to a parent key handle */
1167 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1169 if (!hkey) return (struct key *)grab_object( root_key );
1170 return (struct key *)get_handle_obj( current->process, hkey, 0, &key_ops );
1173 /* read a line from the input file */
1174 static int read_next_line( struct file_load_info *info )
1176 char *newbuf;
1177 int newlen, pos = 0;
1179 info->line++;
1180 for (;;)
1182 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1183 return (pos != 0); /* EOF */
1184 pos = strlen(info->buffer);
1185 if (info->buffer[pos-1] == '\n')
1187 /* got a full line */
1188 info->buffer[--pos] = 0;
1189 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1190 return 1;
1192 if (pos < info->len - 1) return 1; /* EOF but something was read */
1194 /* need to enlarge the buffer */
1195 newlen = info->len + info->len / 2;
1196 if (!(newbuf = realloc( info->buffer, newlen )))
1198 set_error( STATUS_NO_MEMORY );
1199 return -1;
1201 info->buffer = newbuf;
1202 info->len = newlen;
1206 /* make sure the temp buffer holds enough space */
1207 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1209 WCHAR *tmp;
1210 if (info->tmplen >= size) return 1;
1211 if (!(tmp = realloc( info->tmp, size )))
1213 set_error( STATUS_NO_MEMORY );
1214 return 0;
1216 info->tmp = tmp;
1217 info->tmplen = size;
1218 return 1;
1221 /* report an error while loading an input file */
1222 static void file_read_error( const char *err, struct file_load_info *info )
1224 if (info->filename)
1225 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1226 else
1227 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1230 /* convert a data type tag to a value type */
1231 static int get_data_type( const char *buffer, int *type, int *parse_type )
1233 struct data_type { const char *tag; int len; int type; int parse_type; };
1235 static const struct data_type data_types[] =
1236 { /* actual type */ /* type to assume for parsing */
1237 { "\"", 1, REG_SZ, REG_SZ },
1238 { "str:\"", 5, REG_SZ, REG_SZ },
1239 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1240 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1241 { "hex:", 4, REG_BINARY, REG_BINARY },
1242 { "dword:", 6, REG_DWORD, REG_DWORD },
1243 { "hex(", 4, -1, REG_BINARY },
1244 { NULL, 0, 0, 0 }
1247 const struct data_type *ptr;
1248 char *end;
1250 for (ptr = data_types; ptr->tag; ptr++)
1252 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1253 *parse_type = ptr->parse_type;
1254 if ((*type = ptr->type) != -1) return ptr->len;
1255 /* "hex(xx):" is special */
1256 *type = (int)strtoul( buffer + 4, &end, 16 );
1257 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1258 return end + 2 - buffer;
1260 return 0;
1263 /* load and create a key from the input file */
1264 static struct key *load_key( struct key *base, const char *buffer,
1265 int prefix_len, struct file_load_info *info )
1267 WCHAR *p;
1268 struct unicode_str name;
1269 int res;
1270 unsigned int mod;
1271 timeout_t modif = current_time;
1272 data_size_t len;
1274 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1276 len = info->tmplen;
1277 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1279 file_read_error( "Malformed key", info );
1280 return NULL;
1282 if (sscanf( buffer + res, " %u", &mod ) == 1)
1283 modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1285 p = info->tmp;
1286 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1288 if (!*p)
1290 if (prefix_len > 1)
1292 file_read_error( "Malformed key", info );
1293 return NULL;
1295 /* empty key name, return base key */
1296 return (struct key *)grab_object( base );
1298 name.str = p;
1299 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1300 return create_key_recursive( base, &name, modif );
1303 /* load a key option from the input file */
1304 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1306 const char *p;
1307 data_size_t len;
1309 if (!strncmp( buffer, "#class=", 7 ))
1311 p = buffer + 7;
1312 if (*p++ != '"') return 0;
1313 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1314 len = info->tmplen;
1315 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1316 free( key->class );
1317 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1318 key->classlen = len;
1320 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1321 /* ignore unknown options */
1322 return 1;
1325 /* parse a comma-separated list of hex digits */
1326 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1328 const char *p = buffer;
1329 data_size_t count = 0;
1330 char *end;
1332 while (isxdigit(*p))
1334 unsigned int val = strtoul( p, &end, 16 );
1335 if (end == p || val > 0xff) return -1;
1336 if (count++ >= *len) return -1; /* dest buffer overflow */
1337 *dest++ = val;
1338 p = end;
1339 while (isspace(*p)) p++;
1340 if (*p == ',') p++;
1341 while (isspace(*p)) p++;
1343 *len = count;
1344 return p - buffer;
1347 /* parse a value name and create the corresponding value */
1348 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1349 struct file_load_info *info )
1351 struct key_value *value;
1352 struct unicode_str name;
1353 int index;
1355 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1356 name.str = info->tmp;
1357 name.len = info->tmplen;
1358 if (buffer[0] == '@')
1360 name.len = 0;
1361 *len = 1;
1363 else
1365 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1366 if (r == -1) goto error;
1367 *len = r + 1; /* for initial quote */
1368 name.len -= sizeof(WCHAR); /* terminating null */
1370 while (isspace(buffer[*len])) (*len)++;
1371 if (buffer[*len] != '=') goto error;
1372 (*len)++;
1373 while (isspace(buffer[*len])) (*len)++;
1374 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1375 return value;
1377 error:
1378 file_read_error( "Malformed value name", info );
1379 return NULL;
1382 /* load a value from the input file */
1383 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1385 DWORD dw;
1386 void *ptr, *newptr;
1387 int res, type, parse_type;
1388 data_size_t maxlen, len;
1389 struct key_value *value;
1391 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1392 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1393 buffer += len + res;
1395 switch(parse_type)
1397 case REG_SZ:
1398 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1399 len = info->tmplen;
1400 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1401 ptr = info->tmp;
1402 break;
1403 case REG_DWORD:
1404 dw = strtoul( buffer, NULL, 16 );
1405 ptr = &dw;
1406 len = sizeof(dw);
1407 break;
1408 case REG_BINARY: /* hex digits */
1409 len = 0;
1410 for (;;)
1412 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1413 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1414 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1415 len += maxlen;
1416 buffer += res;
1417 while (isspace(*buffer)) buffer++;
1418 if (!*buffer) break;
1419 if (*buffer != '\\') goto error;
1420 if (read_next_line( info) != 1) goto error;
1421 buffer = info->buffer;
1422 while (isspace(*buffer)) buffer++;
1424 ptr = info->tmp;
1425 break;
1426 default:
1427 assert(0);
1428 ptr = NULL; /* keep compiler quiet */
1429 break;
1432 if (!len) newptr = NULL;
1433 else if (!(newptr = memdup( ptr, len ))) return 0;
1435 free( value->data );
1436 value->data = newptr;
1437 value->len = len;
1438 value->type = type;
1439 return 1;
1441 error:
1442 file_read_error( "Malformed value", info );
1443 free( value->data );
1444 value->data = NULL;
1445 value->len = 0;
1446 value->type = REG_NONE;
1447 return 0;
1450 /* return the length (in path elements) of name that is part of the key name */
1451 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1452 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1454 WCHAR *p;
1455 int res;
1456 data_size_t len;
1458 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1460 len = info->tmplen;
1461 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1463 file_read_error( "Malformed key", info );
1464 return 0;
1466 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1467 len = (p - info->tmp) * sizeof(WCHAR);
1468 for (res = 1; key != root_key; res++)
1470 if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
1471 key = key->parent;
1473 if (key == root_key) res = 0; /* no matching name */
1474 return res;
1477 /* load all the keys from the input file */
1478 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1479 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1481 struct key *subkey = NULL;
1482 struct file_load_info info;
1483 char *p;
1485 info.filename = filename;
1486 info.file = f;
1487 info.len = 4;
1488 info.tmplen = 4;
1489 info.line = 0;
1490 if (!(info.buffer = mem_alloc( info.len ))) return;
1491 if (!(info.tmp = mem_alloc( info.tmplen )))
1493 free( info.buffer );
1494 return;
1497 if ((read_next_line( &info ) != 1) ||
1498 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1500 set_error( STATUS_NOT_REGISTRY_FILE );
1501 goto done;
1504 while (read_next_line( &info ) == 1)
1506 p = info.buffer;
1507 while (*p && isspace(*p)) p++;
1508 switch(*p)
1510 case '[': /* new key */
1511 if (subkey) release_object( subkey );
1512 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1513 if (!(subkey = load_key( key, p + 1, prefix_len, &info )))
1514 file_read_error( "Error creating key", &info );
1515 break;
1516 case '@': /* default value */
1517 case '\"': /* value */
1518 if (subkey) load_value( subkey, p, &info );
1519 else file_read_error( "Value without key", &info );
1520 break;
1521 case '#': /* option */
1522 if (subkey) load_key_option( subkey, p, &info );
1523 break;
1524 case ';': /* comment */
1525 case 0: /* empty line */
1526 break;
1527 default:
1528 file_read_error( "Unrecognized input", &info );
1529 break;
1533 done:
1534 if (subkey) release_object( subkey );
1535 free( info.buffer );
1536 free( info.tmp );
1539 /* load a part of the registry from a file */
1540 static void load_registry( struct key *key, obj_handle_t handle )
1542 struct file *file;
1543 int fd;
1545 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1546 fd = dup( get_file_unix_fd( file ) );
1547 release_object( file );
1548 if (fd != -1)
1550 FILE *f = fdopen( fd, "r" );
1551 if (f)
1553 load_keys( key, NULL, f, -1 );
1554 fclose( f );
1556 else file_set_error();
1560 /* load one of the initial registry files */
1561 static void load_init_registry_from_file( const char *filename, struct key *key )
1563 FILE *f;
1565 if ((f = fopen( filename, "r" )))
1567 load_keys( key, filename, f, 0 );
1568 fclose( f );
1569 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1571 fprintf( stderr, "%s is not a valid registry file\n", filename );
1572 return;
1576 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1578 save_branch_info[save_branch_count].path = filename;
1579 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1580 make_object_static( &key->obj );
1583 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1585 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1586 static const WCHAR formatW[] = {'-','%','u',0};
1587 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1588 WCHAR *p = buffer;
1589 unsigned int i;
1591 strcpyW( p, prefixW );
1592 p += strlenW( prefixW );
1593 p += sprintfW( p, formatW, sid->Revision );
1594 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1595 sid->IdentifierAuthority.Value[4] ),
1596 MAKEWORD( sid->IdentifierAuthority.Value[3],
1597 sid->IdentifierAuthority.Value[2] )));
1598 for (i = 0; i < sid->SubAuthorityCount; i++)
1599 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1601 path->len = (p - buffer) * sizeof(WCHAR);
1602 path->str = p = memdup( buffer, path->len );
1603 return p;
1606 /* registry initialisation */
1607 void init_registry(void)
1609 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1610 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1611 static const struct unicode_str root_name = { NULL, 0 };
1612 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1613 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1615 WCHAR *current_user_path;
1616 struct unicode_str current_user_str;
1617 struct key *key;
1619 /* switch to the config dir */
1621 if (fchdir( config_dir_fd ) == -1) fatal_perror( "chdir to config dir" );
1623 /* create the root key */
1624 root_key = alloc_key( &root_name, current_time );
1625 assert( root_key );
1626 make_object_static( &root_key->obj );
1628 /* load system.reg into Registry\Machine */
1630 if (!(key = create_key_recursive( root_key, &HKLM_name, current_time )))
1631 fatal_error( "could not create Machine registry key\n" );
1633 load_init_registry_from_file( "system.reg", key );
1634 release_object( key );
1636 /* load userdef.reg into Registry\User\.Default */
1638 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1639 fatal_error( "could not create User\\.Default registry key\n" );
1641 load_init_registry_from_file( "userdef.reg", key );
1642 release_object( key );
1644 /* load user.reg into HKEY_CURRENT_USER */
1646 /* FIXME: match default user in token.c. should get from process token instead */
1647 current_user_path = format_user_registry_path( security_interactive_sid, &current_user_str );
1648 if (!current_user_path ||
1649 !(key = create_key_recursive( root_key, &current_user_str, current_time )))
1650 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1651 free( current_user_path );
1652 load_init_registry_from_file( "user.reg", key );
1653 release_object( key );
1655 /* start the periodic save timer */
1656 set_periodic_save_timer();
1658 /* go back to the server dir */
1659 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1662 /* save a registry branch to a file */
1663 static void save_all_subkeys( struct key *key, FILE *f )
1665 fprintf( f, "WINE REGISTRY Version 2\n" );
1666 fprintf( f, ";; All keys relative to " );
1667 dump_path( key, NULL, f );
1668 fprintf( f, "\n" );
1669 save_subkeys( key, key, f );
1672 /* save a registry branch to a file handle */
1673 static void save_registry( struct key *key, obj_handle_t handle )
1675 struct file *file;
1676 int fd;
1678 if (key->flags & KEY_DELETED)
1680 set_error( STATUS_KEY_DELETED );
1681 return;
1683 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1684 fd = dup( get_file_unix_fd( file ) );
1685 release_object( file );
1686 if (fd != -1)
1688 FILE *f = fdopen( fd, "w" );
1689 if (f)
1691 save_all_subkeys( key, f );
1692 if (fclose( f )) file_set_error();
1694 else
1696 file_set_error();
1697 close( fd );
1702 /* save a registry branch to a file */
1703 static int save_branch( struct key *key, const char *path )
1705 struct stat st;
1706 char *p, *tmp = NULL;
1707 int fd, count = 0, ret = 0;
1708 FILE *f;
1710 if (!(key->flags & KEY_DIRTY))
1712 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1713 return 1;
1716 /* test the file type */
1718 if ((fd = open( path, O_WRONLY )) != -1)
1720 /* if file is not a regular file or has multiple links or is accessed
1721 * via symbolic links, write directly into it; otherwise use a temp file */
1722 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1724 ftruncate( fd, 0 );
1725 goto save;
1727 close( fd );
1730 /* create a temp file in the same directory */
1732 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1733 strcpy( tmp, path );
1734 if ((p = strrchr( tmp, '/' ))) p++;
1735 else p = tmp;
1736 for (;;)
1738 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1739 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1740 if (errno != EEXIST) goto done;
1741 close( fd );
1744 /* now save to it */
1746 save:
1747 if (!(f = fdopen( fd, "w" )))
1749 if (tmp) unlink( tmp );
1750 close( fd );
1751 goto done;
1754 if (debug_level > 1)
1756 fprintf( stderr, "%s: ", path );
1757 dump_operation( key, NULL, "saving" );
1760 save_all_subkeys( key, f );
1761 ret = !fclose(f);
1763 if (tmp)
1765 /* if successfully written, rename to final name */
1766 if (ret) ret = !rename( tmp, path );
1767 if (!ret) unlink( tmp );
1770 done:
1771 free( tmp );
1772 if (ret) make_clean( key );
1773 return ret;
1776 /* periodic saving of the registry */
1777 static void periodic_save( void *arg )
1779 int i;
1781 if (fchdir( config_dir_fd ) == -1) return;
1782 save_timeout_user = NULL;
1783 for (i = 0; i < save_branch_count; i++)
1784 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1785 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1786 set_periodic_save_timer();
1789 /* start the periodic save timer */
1790 static void set_periodic_save_timer(void)
1792 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1793 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
1796 /* save the modified registry branches to disk */
1797 void flush_registry(void)
1799 int i;
1801 if (fchdir( config_dir_fd ) == -1) return;
1802 for (i = 0; i < save_branch_count; i++)
1804 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1806 fprintf( stderr, "wineserver: could not save registry branch to %s",
1807 save_branch_info[i].path );
1808 perror( " " );
1811 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1815 /* create a registry key */
1816 DECL_HANDLER(create_key)
1818 struct key *key = NULL, *parent;
1819 struct unicode_str name, class;
1820 unsigned int access = req->access;
1822 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1824 reply->hkey = 0;
1826 if (req->namelen > get_req_data_size())
1828 set_error( STATUS_INVALID_PARAMETER );
1829 return;
1831 class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1832 class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1833 get_req_path( &name, !req->parent );
1834 if (name.str > class.str)
1836 set_error( STATUS_INVALID_PARAMETER );
1837 return;
1839 name.len = (class.str - name.str) * sizeof(WCHAR);
1841 /* NOTE: no access rights are required from the parent handle to create a key */
1842 if ((parent = get_parent_hkey_obj( req->parent )))
1844 if ((key = create_key( parent, &name, &class, req->options, access,
1845 req->attributes, &reply->created )))
1847 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1848 release_object( key );
1850 release_object( parent );
1854 /* open a registry key */
1855 DECL_HANDLER(open_key)
1857 struct key *key, *parent;
1858 struct unicode_str name;
1859 unsigned int access = req->access;
1861 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1863 reply->hkey = 0;
1864 /* NOTE: no access rights are required to open the parent key, only the child key */
1865 if ((parent = get_parent_hkey_obj( req->parent )))
1867 get_req_path( &name, !req->parent );
1868 if ((key = open_key( parent, &name, access, req->attributes )))
1870 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1871 release_object( key );
1873 release_object( parent );
1877 /* delete a registry key */
1878 DECL_HANDLER(delete_key)
1880 struct key *key;
1882 if ((key = get_hkey_obj( req->hkey, DELETE )))
1884 delete_key( key, 0);
1885 release_object( key );
1889 /* flush a registry key */
1890 DECL_HANDLER(flush_key)
1892 struct key *key = get_hkey_obj( req->hkey, 0 );
1893 if (key)
1895 /* we don't need to do anything here with the current implementation */
1896 release_object( key );
1900 /* enumerate registry subkeys */
1901 DECL_HANDLER(enum_key)
1903 struct key *key;
1905 if ((key = get_hkey_obj( req->hkey,
1906 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1908 enum_key( key, req->index, req->info_class, reply );
1909 release_object( key );
1913 /* set a value of a registry key */
1914 DECL_HANDLER(set_key_value)
1916 struct key *key;
1917 struct unicode_str name;
1919 if (req->namelen > get_req_data_size())
1921 set_error( STATUS_INVALID_PARAMETER );
1922 return;
1924 name.str = get_req_data();
1925 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
1927 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1929 data_size_t datalen = get_req_data_size() - req->namelen;
1930 const char *data = (const char *)get_req_data() + req->namelen;
1932 set_value( key, &name, req->type, data, datalen );
1933 release_object( key );
1937 /* retrieve the value of a registry key */
1938 DECL_HANDLER(get_key_value)
1940 struct key *key;
1941 struct unicode_str name;
1943 reply->total = 0;
1944 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1946 get_req_unicode_str( &name );
1947 get_value( key, &name, &reply->type, &reply->total );
1948 release_object( key );
1952 /* enumerate the value of a registry key */
1953 DECL_HANDLER(enum_key_value)
1955 struct key *key;
1957 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1959 enum_value( key, req->index, req->info_class, reply );
1960 release_object( key );
1964 /* delete a value of a registry key */
1965 DECL_HANDLER(delete_key_value)
1967 struct key *key;
1968 struct unicode_str name;
1970 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1972 get_req_unicode_str( &name );
1973 delete_value( key, &name );
1974 release_object( key );
1978 /* load a registry branch from a file */
1979 DECL_HANDLER(load_registry)
1981 struct key *key, *parent;
1982 struct token *token = thread_get_impersonation_token( current );
1983 struct unicode_str name;
1985 const LUID_AND_ATTRIBUTES privs[] =
1987 { SeBackupPrivilege, 0 },
1988 { SeRestorePrivilege, 0 },
1991 if (!token || !token_check_privileges( token, TRUE, privs,
1992 sizeof(privs)/sizeof(privs[0]), NULL ))
1994 set_error( STATUS_PRIVILEGE_NOT_HELD );
1995 return;
1998 if ((parent = get_parent_hkey_obj( req->hkey )))
2000 int dummy;
2001 get_req_path( &name, !req->hkey );
2002 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, &dummy )))
2004 load_registry( key, req->file );
2005 release_object( key );
2007 release_object( parent );
2011 DECL_HANDLER(unload_registry)
2013 struct key *key;
2014 struct token *token = thread_get_impersonation_token( current );
2016 const LUID_AND_ATTRIBUTES privs[] =
2018 { SeBackupPrivilege, 0 },
2019 { SeRestorePrivilege, 0 },
2022 if (!token || !token_check_privileges( token, TRUE, privs,
2023 sizeof(privs)/sizeof(privs[0]), NULL ))
2025 set_error( STATUS_PRIVILEGE_NOT_HELD );
2026 return;
2029 if ((key = get_hkey_obj( req->hkey, 0 )))
2031 delete_key( key, 1 ); /* FIXME */
2032 release_object( key );
2036 /* save a registry branch to a file */
2037 DECL_HANDLER(save_registry)
2039 struct key *key;
2041 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2043 set_error( STATUS_PRIVILEGE_NOT_HELD );
2044 return;
2047 if ((key = get_hkey_obj( req->hkey, 0 )))
2049 save_registry( key, req->file );
2050 release_object( key );
2054 /* add a registry key change notification */
2055 DECL_HANDLER(set_registry_notification)
2057 struct key *key;
2058 struct event *event;
2059 struct notify *notify;
2061 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2062 if (key)
2064 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2065 if (event)
2067 notify = find_notify( key, current->process, req->hkey );
2068 if (notify)
2070 if (notify->event)
2071 release_object( notify->event );
2072 grab_object( event );
2073 notify->event = event;
2075 else
2077 notify = mem_alloc( sizeof(*notify) );
2078 if (notify)
2080 grab_object( event );
2081 notify->event = event;
2082 notify->subtree = req->subtree;
2083 notify->filter = req->filter;
2084 notify->hkey = req->hkey;
2085 notify->process = current->process;
2086 list_add_head( &key->notify_list, &notify->entry );
2089 release_object( event );
2091 release_object( key );