server: Store the prefix 32/64 architecture in the registry files.
[wine] / server / registry.c
1 /*
2  * Server-side registry management
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /* To do:
22  * - symbolic links
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
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>
39
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"
48
49 #include "winternl.h"
50 #include "wine/library.h"
51
52 struct notify
53 {
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 */
60 };
61
62 /* a registry key */
63 struct key
64 {
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 */
80 };
81
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 */
88 #define KEY_WOWSHARE 0x0020  /* key is a Wow64 shared key (used for Software\Classes) */
89
90 /* a key value */
91 struct key_value
92 {
93     WCHAR            *name;    /* value name */
94     unsigned short    namelen; /* length of value name */
95     unsigned short    type;    /* value type */
96     data_size_t       len;     /* value data length in bytes */
97     void             *data;    /* pointer to value data */
98 };
99
100 #define MIN_SUBKEYS  8   /* min. number of allocated subkeys per key */
101 #define MIN_VALUES   8   /* min. number of allocated values per key */
102
103 #define MAX_NAME_LEN  255    /* max. length of a key name */
104 #define MAX_VALUE_LEN 16383  /* max. length of a value name */
105
106 /* the root of the registry tree */
107 static struct key *root_key;
108
109 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
110 static const timeout_t save_period = 30 * -TICKS_PER_SEC;  /* delay between periodic saves */
111 static struct timeout_user *save_timeout_user;  /* saving timer */
112 static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
113
114 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
115 static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
116 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
117 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
118
119 static void set_periodic_save_timer(void);
120 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
121
122 /* information about where to save a registry branch */
123 struct save_branch_info
124 {
125     struct key  *key;
126     const char  *path;
127 };
128
129 #define MAX_SAVE_BRANCH_INFO 3
130 static int save_branch_count;
131 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
132
133
134 /* information about a file being loaded */
135 struct file_load_info
136 {
137     const char *filename; /* input file name */
138     FILE       *file;     /* input file */
139     char       *buffer;   /* line buffer */
140     int         len;      /* buffer length */
141     int         line;     /* current input line */
142     WCHAR      *tmp;      /* temp buffer to use while parsing input */
143     size_t      tmplen;   /* length of temp buffer */
144 };
145
146
147 static void key_dump( struct object *obj, int verbose );
148 static unsigned int key_map_access( struct object *obj, unsigned int access );
149 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
150 static void key_destroy( struct object *obj );
151
152 static const struct object_ops key_ops =
153 {
154     sizeof(struct key),      /* size */
155     key_dump,                /* dump */
156     no_get_type,             /* get_type */
157     no_add_queue,            /* add_queue */
158     NULL,                    /* remove_queue */
159     NULL,                    /* signaled */
160     NULL,                    /* satisfied */
161     no_signal,               /* signal */
162     no_get_fd,               /* get_fd */
163     key_map_access,          /* map_access */
164     default_get_sd,          /* get_sd */
165     default_set_sd,          /* set_sd */
166     no_lookup_name,          /* lookup_name */
167     no_open_file,            /* open_file */
168     key_close_handle,        /* close_handle */
169     key_destroy              /* destroy */
170 };
171
172
173 static inline int is_wow6432node( const WCHAR *name, unsigned int len )
174 {
175     return (len == sizeof(wow6432node) &&
176             !memicmpW( name, wow6432node, sizeof(wow6432node)/sizeof(WCHAR) ));
177 }
178
179 /*
180  * The registry text file format v2 used by this code is similar to the one
181  * used by REGEDIT import/export functionality, with the following differences:
182  * - strings and key names can contain \x escapes for Unicode
183  * - key names use escapes too in order to support Unicode
184  * - the modification time optionally follows the key name
185  * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
186  */
187
188 /* dump the full path of a key */
189 static void dump_path( const struct key *key, const struct key *base, FILE *f )
190 {
191     if (key->parent && key->parent != base)
192     {
193         dump_path( key->parent, base, f );
194         fprintf( f, "\\\\" );
195     }
196     dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
197 }
198
199 /* dump a value to a text file */
200 static void dump_value( const struct key_value *value, FILE *f )
201 {
202     unsigned int i, dw;
203     int count;
204
205     if (value->namelen)
206     {
207         fputc( '\"', f );
208         count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
209         count += fprintf( f, "\"=" );
210     }
211     else count = fprintf( f, "@=" );
212
213     switch(value->type)
214     {
215     case REG_SZ:
216     case REG_EXPAND_SZ:
217     case REG_MULTI_SZ:
218         /* only output properly terminated strings in string format */
219         if (value->len < sizeof(WCHAR)) break;
220         if (value->len % sizeof(WCHAR)) break;
221         if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
222         if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
223         fputc( '\"', f );
224         dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
225         fprintf( f, "\"\n" );
226         return;
227
228     case REG_DWORD:
229         if (value->len != sizeof(dw)) break;
230         memcpy( &dw, value->data, sizeof(dw) );
231         fprintf( f, "dword:%08x\n", dw );
232         return;
233     }
234
235     if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
236     else count += fprintf( f, "hex(%x):", value->type );
237     for (i = 0; i < value->len; i++)
238     {
239         count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
240         if (i < value->len-1)
241         {
242             fputc( ',', f );
243             if (++count > 76)
244             {
245                 fprintf( f, "\\\n  " );
246                 count = 2;
247             }
248         }
249     }
250     fputc( '\n', f );
251 }
252
253 /* save a registry and all its subkeys to a text file */
254 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
255 {
256     int i;
257
258     if (key->flags & KEY_VOLATILE) return;
259     /* save key if it has either some values or no subkeys, or needs special options */
260     /* keys with no values but subkeys are saved implicitly by saving the subkeys */
261     if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
262     {
263         fprintf( f, "\n[" );
264         if (key != base) dump_path( key, base, f );
265         fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
266         if (key->class)
267         {
268             fprintf( f, "#class=\"" );
269             dump_strW( key->class, key->classlen / sizeof(WCHAR), f, "\"\"" );
270             fprintf( f, "\"\n" );
271         }
272         if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
273         for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
274     }
275     for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
276 }
277
278 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
279 {
280     fprintf( stderr, "%s key ", op );
281     if (key) dump_path( key, NULL, stderr );
282     else fprintf( stderr, "ERROR" );
283     if (value)
284     {
285         fprintf( stderr, " value ");
286         dump_value( value, stderr );
287     }
288     else fprintf( stderr, "\n" );
289 }
290
291 static void key_dump( struct object *obj, int verbose )
292 {
293     struct key *key = (struct key *)obj;
294     assert( obj->ops == &key_ops );
295     fprintf( stderr, "Key flags=%x ", key->flags );
296     dump_path( key, NULL, stderr );
297     fprintf( stderr, "\n" );
298 }
299
300 /* notify waiter and maybe delete the notification */
301 static void do_notification( struct key *key, struct notify *notify, int del )
302 {
303     if (notify->event)
304     {
305         set_event( notify->event );
306         release_object( notify->event );
307         notify->event = NULL;
308     }
309     if (del)
310     {
311         list_remove( &notify->entry );
312         free( notify );
313     }
314 }
315
316 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
317 {
318     struct notify *notify;
319
320     LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
321     {
322         if (notify->process == process && notify->hkey == hkey) return notify;
323     }
324     return NULL;
325 }
326
327 static unsigned int key_map_access( struct object *obj, unsigned int access )
328 {
329     if (access & GENERIC_READ)    access |= KEY_READ;
330     if (access & GENERIC_WRITE)   access |= KEY_WRITE;
331     if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
332     if (access & GENERIC_ALL)     access |= KEY_ALL_ACCESS;
333     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
334 }
335
336 /* close the notification associated with a handle */
337 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
338 {
339     struct key * key = (struct key *) obj;
340     struct notify *notify = find_notify( key, process, handle );
341     if (notify) do_notification( key, notify, 1 );
342     return 1;  /* ok to close */
343 }
344
345 static void key_destroy( struct object *obj )
346 {
347     int i;
348     struct list *ptr;
349     struct key *key = (struct key *)obj;
350     assert( obj->ops == &key_ops );
351
352     free( key->name );
353     free( key->class );
354     for (i = 0; i <= key->last_value; i++)
355     {
356         free( key->values[i].name );
357         free( key->values[i].data );
358     }
359     free( key->values );
360     for (i = 0; i <= key->last_subkey; i++)
361     {
362         key->subkeys[i]->parent = NULL;
363         release_object( key->subkeys[i] );
364     }
365     free( key->subkeys );
366     /* unconditionally notify everything waiting on this key */
367     while ((ptr = list_head( &key->notify_list )))
368     {
369         struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
370         do_notification( key, notify, 1 );
371     }
372 }
373
374 /* get the request vararg as registry path */
375 static inline void get_req_path( struct unicode_str *str, int skip_root )
376 {
377     str->str = get_req_data();
378     str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
379
380     if (skip_root && str->len >= sizeof(root_name) &&
381         !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
382     {
383         str->str += sizeof(root_name)/sizeof(WCHAR);
384         str->len -= sizeof(root_name);
385     }
386 }
387
388 /* return the next token in a given path */
389 /* token->str must point inside the path, or be NULL for the first call */
390 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
391 {
392     data_size_t i = 0, len = path->len / sizeof(WCHAR);
393
394     if (!token->str)  /* first time */
395     {
396         /* path cannot start with a backslash */
397         if (len && path->str[0] == '\\')
398         {
399             set_error( STATUS_OBJECT_PATH_INVALID );
400             return NULL;
401         }
402     }
403     else
404     {
405         i = token->str - path->str;
406         i += token->len / sizeof(WCHAR);
407         while (i < len && path->str[i] == '\\') i++;
408     }
409     token->str = path->str + i;
410     while (i < len && path->str[i] != '\\') i++;
411     token->len = (path->str + i - token->str) * sizeof(WCHAR);
412     return token;
413 }
414
415 /* allocate a key object */
416 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
417 {
418     struct key *key;
419     if ((key = alloc_object( &key_ops )))
420     {
421         key->name        = NULL;
422         key->class       = NULL;
423         key->namelen     = name->len;
424         key->classlen    = 0;
425         key->flags       = 0;
426         key->last_subkey = -1;
427         key->nb_subkeys  = 0;
428         key->subkeys     = NULL;
429         key->nb_values   = 0;
430         key->last_value  = -1;
431         key->values      = NULL;
432         key->modif       = modif;
433         key->parent      = NULL;
434         list_init( &key->notify_list );
435         if (name->len && !(key->name = memdup( name->str, name->len )))
436         {
437             release_object( key );
438             key = NULL;
439         }
440     }
441     return key;
442 }
443
444 /* mark a key and all its parents as dirty (modified) */
445 static void make_dirty( struct key *key )
446 {
447     while (key)
448     {
449         if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return;  /* nothing to do */
450         key->flags |= KEY_DIRTY;
451         key = key->parent;
452     }
453 }
454
455 /* mark a key and all its subkeys as clean (not modified) */
456 static void make_clean( struct key *key )
457 {
458     int i;
459
460     if (key->flags & KEY_VOLATILE) return;
461     if (!(key->flags & KEY_DIRTY)) return;
462     key->flags &= ~KEY_DIRTY;
463     for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
464 }
465
466 /* go through all the notifications and send them if necessary */
467 static void check_notify( struct key *key, unsigned int change, int not_subtree )
468 {
469     struct list *ptr, *next;
470
471     LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
472     {
473         struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
474         if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
475             do_notification( key, n, 0 );
476     }
477 }
478
479 /* update key modification time */
480 static void touch_key( struct key *key, unsigned int change )
481 {
482     struct key *k;
483
484     key->modif = current_time;
485     make_dirty( key );
486
487     /* do notifications */
488     check_notify( key, change, 1 );
489     for ( k = key->parent; k; k = k->parent )
490         check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
491 }
492
493 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
494 static int grow_subkeys( struct key *key )
495 {
496     struct key **new_subkeys;
497     int nb_subkeys;
498
499     if (key->nb_subkeys)
500     {
501         nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2);  /* grow by 50% */
502         if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
503         {
504             set_error( STATUS_NO_MEMORY );
505             return 0;
506         }
507     }
508     else
509     {
510         nb_subkeys = MIN_VALUES;
511         if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
512     }
513     key->subkeys    = new_subkeys;
514     key->nb_subkeys = nb_subkeys;
515     return 1;
516 }
517
518 /* allocate a subkey for a given key, and return its index */
519 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
520                                  int index, timeout_t modif )
521 {
522     struct key *key;
523     int i;
524
525     if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
526     {
527         set_error( STATUS_NAME_TOO_LONG );
528         return NULL;
529     }
530     if (parent->last_subkey + 1 == parent->nb_subkeys)
531     {
532         /* need to grow the array */
533         if (!grow_subkeys( parent )) return NULL;
534     }
535     if ((key = alloc_key( name, modif )) != NULL)
536     {
537         key->parent = parent;
538         for (i = ++parent->last_subkey; i > index; i--)
539             parent->subkeys[i] = parent->subkeys[i-1];
540         parent->subkeys[index] = key;
541         if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
542             parent->flags |= KEY_WOW64;
543     }
544     return key;
545 }
546
547 /* free a subkey of a given key */
548 static void free_subkey( struct key *parent, int index )
549 {
550     struct key *key;
551     int i, nb_subkeys;
552
553     assert( index >= 0 );
554     assert( index <= parent->last_subkey );
555
556     key = parent->subkeys[index];
557     for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
558     parent->last_subkey--;
559     key->flags |= KEY_DELETED;
560     key->parent = NULL;
561     if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
562     release_object( key );
563
564     /* try to shrink the array */
565     nb_subkeys = parent->nb_subkeys;
566     if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
567     {
568         struct key **new_subkeys;
569         nb_subkeys -= nb_subkeys / 3;  /* shrink by 33% */
570         if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
571         if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
572         parent->subkeys = new_subkeys;
573         parent->nb_subkeys = nb_subkeys;
574     }
575 }
576
577 /* find the named child of a given key and return its index */
578 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
579 {
580     int i, min, max, res;
581     data_size_t len;
582
583     min = 0;
584     max = key->last_subkey;
585     while (min <= max)
586     {
587         i = (min + max) / 2;
588         len = min( key->subkeys[i]->namelen, name->len );
589         res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
590         if (!res) res = key->subkeys[i]->namelen - name->len;
591         if (!res)
592         {
593             *index = i;
594             return key->subkeys[i];
595         }
596         if (res > 0) max = i - 1;
597         else min = i + 1;
598     }
599     *index = min;  /* this is where we should insert it */
600     return NULL;
601 }
602
603 /* return the wow64 variant of the key, or the key itself if none */
604 static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
605 {
606     static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
607     int index;
608
609     if (!(key->flags & KEY_WOW64)) return key;
610     if (!is_wow6432node( name->str, name->len ))
611     {
612         key = find_subkey( key, &wow6432node_str, &index );
613         assert( key );  /* if KEY_WOW64 is set we must find it */
614     }
615     return key;
616 }
617
618
619 /* follow a symlink and return the resolved key */
620 static struct key *follow_symlink( struct key *key, int iteration )
621 {
622     struct unicode_str path, token;
623     struct key_value *value;
624     int index;
625
626     if (iteration > 16) return NULL;
627     if (!(key->flags & KEY_SYMLINK)) return key;
628     if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
629
630     path.str = value->data;
631     path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
632     if (path.len <= sizeof(root_name)) return NULL;
633     if (memicmpW( path.str, root_name, sizeof(root_name)/sizeof(WCHAR) )) return NULL;
634     path.str += sizeof(root_name) / sizeof(WCHAR);
635     path.len -= sizeof(root_name);
636
637     key = root_key;
638     token.str = NULL;
639     if (!get_path_token( &path, &token )) return NULL;
640     while (token.len)
641     {
642         if (!(key = find_subkey( key, &token, &index ))) break;
643         if (!(key = follow_symlink( key, iteration + 1 ))) break;
644         get_path_token( &path, &token );
645     }
646     return key;
647 }
648
649 /* open a key until we find an element that doesn't exist */
650 /* helper for open_key and create_key */
651 static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
652                                     unsigned int access, struct unicode_str *token, int *index )
653 {
654     token->str = NULL;
655     if (!get_path_token( name, token )) return NULL;
656     if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
657     while (token->len)
658     {
659         struct key *subkey;
660         if (!(subkey = find_subkey( key, token, index )))
661         {
662             if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
663             {
664                 /* try in the 64-bit parent */
665                 key = key->parent;
666                 subkey = find_subkey( key, token, index );
667             }
668         }
669         if (!subkey) break;
670         key = subkey;
671         get_path_token( name, token );
672         if (!token->len) break;
673         if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
674         if (!(key = follow_symlink( key, 0 )))
675         {
676             set_error( STATUS_OBJECT_NAME_NOT_FOUND );
677             return NULL;
678         }
679     }
680     return key;
681 }
682
683 /* open a subkey */
684 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
685                              unsigned int attributes )
686 {
687     int index;
688     struct unicode_str token;
689
690     if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
691
692     if (token.len)
693     {
694         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
695         return NULL;
696     }
697     if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
698     if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
699     {
700         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
701         return NULL;
702     }
703     if (debug_level > 1) dump_operation( key, NULL, "Open" );
704     grab_object( key );
705     return key;
706 }
707
708 /* create a subkey */
709 static struct key *create_key( struct key *key, const struct unicode_str *name,
710                                const struct unicode_str *class, unsigned int options,
711                                unsigned int access, unsigned int attributes, int *created )
712 {
713     int index;
714     struct unicode_str token, next;
715
716     *created = 0;
717     if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
718
719     if (!token.len)  /* the key already exists */
720     {
721         if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
722         if (options & REG_OPTION_CREATE_LINK)
723         {
724             set_error( STATUS_OBJECT_NAME_COLLISION );
725             return NULL;
726         }
727         if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
728         {
729             set_error( STATUS_OBJECT_NAME_NOT_FOUND );
730             return NULL;
731         }
732         if (debug_level > 1) dump_operation( key, NULL, "Open" );
733         grab_object( key );
734         return key;
735     }
736
737     /* token must be the last path component at this point */
738     next = token;
739     get_path_token( name, &next );
740     if (next.len)
741     {
742         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
743         return NULL;
744     }
745
746     if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
747     {
748         set_error( STATUS_CHILD_MUST_BE_VOLATILE );
749         return NULL;
750     }
751     *created = 1;
752     make_dirty( key );
753     if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
754
755     if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
756     if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
757     else key->flags |= KEY_DIRTY;
758
759     if (debug_level > 1) dump_operation( key, NULL, "Create" );
760     if (class && class->len)
761     {
762         key->classlen = class->len;
763         free(key->class);
764         if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
765     }
766     grab_object( key );
767     return key;
768 }
769
770 /* recursively create a subkey (for internal use only) */
771 static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
772 {
773     struct key *base;
774     int index;
775     struct unicode_str token;
776
777     token.str = NULL;
778     if (!get_path_token( name, &token )) return NULL;
779     while (token.len)
780     {
781         struct key *subkey;
782         if (!(subkey = find_subkey( key, &token, &index ))) break;
783         key = subkey;
784         if (!(key = follow_symlink( key, 0 )))
785         {
786             set_error( STATUS_OBJECT_NAME_NOT_FOUND );
787             return NULL;
788         }
789         get_path_token( name, &token );
790     }
791
792     if (token.len)
793     {
794         if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
795         base = key;
796         for (;;)
797         {
798             get_path_token( name, &token );
799             if (!token.len) break;
800             /* we know the index is always 0 in a new key */
801             if (!(key = alloc_subkey( key, &token, 0, modif )))
802             {
803                 free_subkey( base, index );
804                 return NULL;
805             }
806         }
807     }
808
809     grab_object( key );
810     return key;
811 }
812
813 /* query information about a key or a subkey */
814 static void enum_key( const struct key *key, int index, int info_class,
815                       struct enum_key_reply *reply )
816 {
817     int i;
818     data_size_t len, namelen, classlen;
819     data_size_t max_subkey = 0, max_class = 0;
820     data_size_t max_value = 0, max_data = 0;
821     char *data;
822
823     if (index != -1)  /* -1 means use the specified key directly */
824     {
825         if ((index < 0) || (index > key->last_subkey))
826         {
827             set_error( STATUS_NO_MORE_ENTRIES );
828             return;
829         }
830         key = key->subkeys[index];
831     }
832
833     namelen = key->namelen;
834     classlen = key->classlen;
835
836     switch(info_class)
837     {
838     case KeyBasicInformation:
839         classlen = 0; /* only return the name */
840         /* fall through */
841     case KeyNodeInformation:
842         reply->max_subkey = 0;
843         reply->max_class  = 0;
844         reply->max_value  = 0;
845         reply->max_data   = 0;
846         break;
847     case KeyFullInformation:
848         for (i = 0; i <= key->last_subkey; i++)
849         {
850             struct key *subkey = key->subkeys[i];
851             len = subkey->namelen / sizeof(WCHAR);
852             if (len > max_subkey) max_subkey = len;
853             len = subkey->classlen / sizeof(WCHAR);
854             if (len > max_class) max_class = len;
855         }
856         for (i = 0; i <= key->last_value; i++)
857         {
858             len = key->values[i].namelen / sizeof(WCHAR);
859             if (len > max_value) max_value = len;
860             len = key->values[i].len;
861             if (len > max_data) max_data = len;
862         }
863         reply->max_subkey = max_subkey;
864         reply->max_class  = max_class;
865         reply->max_value  = max_value;
866         reply->max_data   = max_data;
867         namelen = 0;  /* only return the class */
868         break;
869     default:
870         set_error( STATUS_INVALID_PARAMETER );
871         return;
872     }
873     reply->subkeys = key->last_subkey + 1;
874     reply->values  = key->last_value + 1;
875     reply->modif   = key->modif;
876     reply->total   = namelen + classlen;
877
878     len = min( reply->total, get_reply_max_size() );
879     if (len && (data = set_reply_data_size( len )))
880     {
881         if (len > namelen)
882         {
883             reply->namelen = namelen;
884             memcpy( data, key->name, namelen );
885             memcpy( data + namelen, key->class, len - namelen );
886         }
887         else
888         {
889             reply->namelen = len;
890             memcpy( data, key->name, len );
891         }
892     }
893     if (debug_level > 1) dump_operation( key, NULL, "Enum" );
894 }
895
896 /* delete a key and its values */
897 static int delete_key( struct key *key, int recurse )
898 {
899     int index;
900     struct key *parent = key->parent;
901
902     /* must find parent and index */
903     if (key == root_key)
904     {
905         set_error( STATUS_ACCESS_DENIED );
906         return -1;
907     }
908     assert( parent );
909
910     while (recurse && (key->last_subkey>=0))
911         if (0 > delete_key(key->subkeys[key->last_subkey], 1))
912             return -1;
913
914     for (index = 0; index <= parent->last_subkey; index++)
915         if (parent->subkeys[index] == key) break;
916     assert( index <= parent->last_subkey );
917
918     /* we can only delete a key that has no subkeys */
919     if (key->last_subkey >= 0)
920     {
921         set_error( STATUS_ACCESS_DENIED );
922         return -1;
923     }
924
925     if (debug_level > 1) dump_operation( key, NULL, "Delete" );
926     free_subkey( parent, index );
927     touch_key( parent, REG_NOTIFY_CHANGE_NAME );
928     return 0;
929 }
930
931 /* try to grow the array of values; return 1 if OK, 0 on error */
932 static int grow_values( struct key *key )
933 {
934     struct key_value *new_val;
935     int nb_values;
936
937     if (key->nb_values)
938     {
939         nb_values = key->nb_values + (key->nb_values / 2);  /* grow by 50% */
940         if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
941         {
942             set_error( STATUS_NO_MEMORY );
943             return 0;
944         }
945     }
946     else
947     {
948         nb_values = MIN_VALUES;
949         if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
950     }
951     key->values = new_val;
952     key->nb_values = nb_values;
953     return 1;
954 }
955
956 /* find the named value of a given key and return its index in the array */
957 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
958 {
959     int i, min, max, res;
960     data_size_t len;
961
962     min = 0;
963     max = key->last_value;
964     while (min <= max)
965     {
966         i = (min + max) / 2;
967         len = min( key->values[i].namelen, name->len );
968         res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
969         if (!res) res = key->values[i].namelen - name->len;
970         if (!res)
971         {
972             *index = i;
973             return &key->values[i];
974         }
975         if (res > 0) max = i - 1;
976         else min = i + 1;
977     }
978     *index = min;  /* this is where we should insert it */
979     return NULL;
980 }
981
982 /* insert a new value; the index must have been returned by find_value */
983 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
984 {
985     struct key_value *value;
986     WCHAR *new_name = NULL;
987     int i;
988
989     if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
990     {
991         set_error( STATUS_NAME_TOO_LONG );
992         return NULL;
993     }
994     if (key->last_value + 1 == key->nb_values)
995     {
996         if (!grow_values( key )) return NULL;
997     }
998     if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
999     for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1000     value = &key->values[index];
1001     value->name    = new_name;
1002     value->namelen = name->len;
1003     value->len     = 0;
1004     value->data    = NULL;
1005     return value;
1006 }
1007
1008 /* set a key value */
1009 static void set_value( struct key *key, const struct unicode_str *name,
1010                        int type, const void *data, data_size_t len )
1011 {
1012     struct key_value *value;
1013     void *ptr = NULL;
1014     int index;
1015
1016     if ((value = find_value( key, name, &index )))
1017     {
1018         /* check if the new value is identical to the existing one */
1019         if (value->type == type && value->len == len &&
1020             value->data && !memcmp( value->data, data, len ))
1021         {
1022             if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1023             return;
1024         }
1025     }
1026
1027     if (key->flags & KEY_SYMLINK)
1028     {
1029         if (type != REG_LINK || name->len != symlink_str.len ||
1030             memicmpW( name->str, symlink_str.str, name->len / sizeof(WCHAR) ))
1031         {
1032             set_error( STATUS_ACCESS_DENIED );
1033             return;
1034         }
1035     }
1036
1037     if (len && !(ptr = memdup( data, len ))) return;
1038
1039     if (!value)
1040     {
1041         if (!(value = insert_value( key, name, index )))
1042         {
1043             free( ptr );
1044             return;
1045         }
1046     }
1047     else free( value->data ); /* already existing, free previous data */
1048
1049     value->type  = type;
1050     value->len   = len;
1051     value->data  = ptr;
1052     touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1053     if (debug_level > 1) dump_operation( key, value, "Set" );
1054 }
1055
1056 /* get a key value */
1057 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
1058 {
1059     struct key_value *value;
1060     int index;
1061
1062     if ((value = find_value( key, name, &index )))
1063     {
1064         *type = value->type;
1065         *len  = value->len;
1066         if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
1067         if (debug_level > 1) dump_operation( key, value, "Get" );
1068     }
1069     else
1070     {
1071         *type = -1;
1072         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1073     }
1074 }
1075
1076 /* enumerate a key value */
1077 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1078 {
1079     struct key_value *value;
1080
1081     if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1082     else
1083     {
1084         void *data;
1085         data_size_t namelen, maxlen;
1086
1087         value = &key->values[i];
1088         reply->type = value->type;
1089         namelen = value->namelen;
1090
1091         switch(info_class)
1092         {
1093         case KeyValueBasicInformation:
1094             reply->total = namelen;
1095             break;
1096         case KeyValueFullInformation:
1097             reply->total = namelen + value->len;
1098             break;
1099         case KeyValuePartialInformation:
1100             reply->total = value->len;
1101             namelen = 0;
1102             break;
1103         default:
1104             set_error( STATUS_INVALID_PARAMETER );
1105             return;
1106         }
1107
1108         maxlen = min( reply->total, get_reply_max_size() );
1109         if (maxlen && ((data = set_reply_data_size( maxlen ))))
1110         {
1111             if (maxlen > namelen)
1112             {
1113                 reply->namelen = namelen;
1114                 memcpy( data, value->name, namelen );
1115                 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1116             }
1117             else
1118             {
1119                 reply->namelen = maxlen;
1120                 memcpy( data, value->name, maxlen );
1121             }
1122         }
1123         if (debug_level > 1) dump_operation( key, value, "Enum" );
1124     }
1125 }
1126
1127 /* delete a value */
1128 static void delete_value( struct key *key, const struct unicode_str *name )
1129 {
1130     struct key_value *value;
1131     int i, index, nb_values;
1132
1133     if (!(value = find_value( key, name, &index )))
1134     {
1135         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1136         return;
1137     }
1138     if (debug_level > 1) dump_operation( key, value, "Delete" );
1139     free( value->name );
1140     free( value->data );
1141     for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1142     key->last_value--;
1143     touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1144
1145     /* try to shrink the array */
1146     nb_values = key->nb_values;
1147     if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1148     {
1149         struct key_value *new_val;
1150         nb_values -= nb_values / 3;  /* shrink by 33% */
1151         if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1152         if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1153         key->values = new_val;
1154         key->nb_values = nb_values;
1155     }
1156 }
1157
1158 /* get the registry key corresponding to an hkey handle */
1159 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1160 {
1161     struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1162
1163     if (key && key->flags & KEY_DELETED)
1164     {
1165         set_error( STATUS_KEY_DELETED );
1166         release_object( key );
1167         key = NULL;
1168     }
1169     return key;
1170 }
1171
1172 /* get the registry key corresponding to a parent key handle */
1173 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1174 {
1175     if (!hkey) return (struct key *)grab_object( root_key );
1176     return get_hkey_obj( hkey, 0 );
1177 }
1178
1179 /* read a line from the input file */
1180 static int read_next_line( struct file_load_info *info )
1181 {
1182     char *newbuf;
1183     int newlen, pos = 0;
1184
1185     info->line++;
1186     for (;;)
1187     {
1188         if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1189             return (pos != 0);  /* EOF */
1190         pos = strlen(info->buffer);
1191         if (info->buffer[pos-1] == '\n')
1192         {
1193             /* got a full line */
1194             info->buffer[--pos] = 0;
1195             if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1196             return 1;
1197         }
1198         if (pos < info->len - 1) return 1;  /* EOF but something was read */
1199
1200         /* need to enlarge the buffer */
1201         newlen = info->len + info->len / 2;
1202         if (!(newbuf = realloc( info->buffer, newlen )))
1203         {
1204             set_error( STATUS_NO_MEMORY );
1205             return -1;
1206         }
1207         info->buffer = newbuf;
1208         info->len = newlen;
1209     }
1210 }
1211
1212 /* make sure the temp buffer holds enough space */
1213 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1214 {
1215     WCHAR *tmp;
1216     if (info->tmplen >= size) return 1;
1217     if (!(tmp = realloc( info->tmp, size )))
1218     {
1219         set_error( STATUS_NO_MEMORY );
1220         return 0;
1221     }
1222     info->tmp = tmp;
1223     info->tmplen = size;
1224     return 1;
1225 }
1226
1227 /* report an error while loading an input file */
1228 static void file_read_error( const char *err, struct file_load_info *info )
1229 {
1230     if (info->filename)
1231         fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1232     else
1233         fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1234 }
1235
1236 /* convert a data type tag to a value type */
1237 static int get_data_type( const char *buffer, int *type, int *parse_type )
1238 {
1239     struct data_type { const char *tag; int len; int type; int parse_type; };
1240
1241     static const struct data_type data_types[] =
1242     {                   /* actual type */  /* type to assume for parsing */
1243         { "\"",        1,   REG_SZ,              REG_SZ },
1244         { "str:\"",    5,   REG_SZ,              REG_SZ },
1245         { "str(2):\"", 8,   REG_EXPAND_SZ,       REG_SZ },
1246         { "str(7):\"", 8,   REG_MULTI_SZ,        REG_SZ },
1247         { "hex:",      4,   REG_BINARY,          REG_BINARY },
1248         { "dword:",    6,   REG_DWORD,           REG_DWORD },
1249         { "hex(",      4,   -1,                  REG_BINARY },
1250         { NULL,        0,    0,                  0 }
1251     };
1252
1253     const struct data_type *ptr;
1254     char *end;
1255
1256     for (ptr = data_types; ptr->tag; ptr++)
1257     {
1258         if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1259         *parse_type = ptr->parse_type;
1260         if ((*type = ptr->type) != -1) return ptr->len;
1261         /* "hex(xx):" is special */
1262         *type = (int)strtoul( buffer + 4, &end, 16 );
1263         if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1264         return end + 2 - buffer;
1265     }
1266     return 0;
1267 }
1268
1269 /* load and create a key from the input file */
1270 static struct key *load_key( struct key *base, const char *buffer,
1271                              int prefix_len, struct file_load_info *info )
1272 {
1273     WCHAR *p;
1274     struct unicode_str name;
1275     int res;
1276     unsigned int mod;
1277     timeout_t modif = current_time;
1278     data_size_t len;
1279
1280     if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1281
1282     len = info->tmplen;
1283     if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1284     {
1285         file_read_error( "Malformed key", info );
1286         return NULL;
1287     }
1288     if (sscanf( buffer + res, " %u", &mod ) == 1)
1289         modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1290
1291     p = info->tmp;
1292     while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1293
1294     if (!*p)
1295     {
1296         if (prefix_len > 1)
1297         {
1298             file_read_error( "Malformed key", info );
1299             return NULL;
1300         }
1301         /* empty key name, return base key */
1302         return (struct key *)grab_object( base );
1303     }
1304     name.str = p;
1305     name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1306     return create_key_recursive( base, &name, modif );
1307 }
1308
1309 /* load a global option from the input file */
1310 static int load_global_option( const char *buffer, struct file_load_info *info )
1311 {
1312     const char *p;
1313
1314     if (!strncmp( buffer, "#arch=", 6 ))
1315     {
1316         enum prefix_type type;
1317         p = buffer + 6;
1318         if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1319         else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1320         else
1321         {
1322             file_read_error( "Unknown architecture", info );
1323             set_error( STATUS_NOT_REGISTRY_FILE );
1324             return 0;
1325         }
1326         if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1327         else if (type != prefix_type)
1328         {
1329             file_read_error( "Mismatched architecture", info );
1330             set_error( STATUS_NOT_REGISTRY_FILE );
1331             return 0;
1332         }
1333     }
1334     /* ignore unknown options */
1335     return 1;
1336 }
1337
1338 /* load a key option from the input file */
1339 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1340 {
1341     const char *p;
1342     data_size_t len;
1343
1344     if (!strncmp( buffer, "#class=", 7 ))
1345     {
1346         p = buffer + 7;
1347         if (*p++ != '"') return 0;
1348         if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1349         len = info->tmplen;
1350         if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1351         free( key->class );
1352         if (!(key->class = memdup( info->tmp, len ))) len = 0;
1353         key->classlen = len;
1354     }
1355     if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
1356     /* ignore unknown options */
1357     return 1;
1358 }
1359
1360 /* parse a comma-separated list of hex digits */
1361 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1362 {
1363     const char *p = buffer;
1364     data_size_t count = 0;
1365     char *end;
1366
1367     while (isxdigit(*p))
1368     {
1369         unsigned int val = strtoul( p, &end, 16 );
1370         if (end == p || val > 0xff) return -1;
1371         if (count++ >= *len) return -1;  /* dest buffer overflow */
1372         *dest++ = val;
1373         p = end;
1374         while (isspace(*p)) p++;
1375         if (*p == ',') p++;
1376         while (isspace(*p)) p++;
1377     }
1378     *len = count;
1379     return p - buffer;
1380 }
1381
1382 /* parse a value name and create the corresponding value */
1383 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1384                                            struct file_load_info *info )
1385 {
1386     struct key_value *value;
1387     struct unicode_str name;
1388     int index;
1389
1390     if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1391     name.str = info->tmp;
1392     name.len = info->tmplen;
1393     if (buffer[0] == '@')
1394     {
1395         name.len = 0;
1396         *len = 1;
1397     }
1398     else
1399     {
1400         int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1401         if (r == -1) goto error;
1402         *len = r + 1; /* for initial quote */
1403         name.len -= sizeof(WCHAR);  /* terminating null */
1404     }
1405     while (isspace(buffer[*len])) (*len)++;
1406     if (buffer[*len] != '=') goto error;
1407     (*len)++;
1408     while (isspace(buffer[*len])) (*len)++;
1409     if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1410     return value;
1411
1412  error:
1413     file_read_error( "Malformed value name", info );
1414     return NULL;
1415 }
1416
1417 /* load a value from the input file */
1418 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1419 {
1420     DWORD dw;
1421     void *ptr, *newptr;
1422     int res, type, parse_type;
1423     data_size_t maxlen, len;
1424     struct key_value *value;
1425
1426     if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1427     if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1428     buffer += len + res;
1429
1430     switch(parse_type)
1431     {
1432     case REG_SZ:
1433         if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1434         len = info->tmplen;
1435         if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1436         ptr = info->tmp;
1437         break;
1438     case REG_DWORD:
1439         dw = strtoul( buffer, NULL, 16 );
1440         ptr = &dw;
1441         len = sizeof(dw);
1442         break;
1443     case REG_BINARY:  /* hex digits */
1444         len = 0;
1445         for (;;)
1446         {
1447             maxlen = 1 + strlen(buffer) / 2;  /* at least 2 chars for one hex byte */
1448             if (!get_file_tmp_space( info, len + maxlen )) return 0;
1449             if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1450             len += maxlen;
1451             buffer += res;
1452             while (isspace(*buffer)) buffer++;
1453             if (!*buffer) break;
1454             if (*buffer != '\\') goto error;
1455             if (read_next_line( info) != 1) goto error;
1456             buffer = info->buffer;
1457             while (isspace(*buffer)) buffer++;
1458         }
1459         ptr = info->tmp;
1460         break;
1461     default:
1462         assert(0);
1463         ptr = NULL;  /* keep compiler quiet */
1464         break;
1465     }
1466
1467     if (!len) newptr = NULL;
1468     else if (!(newptr = memdup( ptr, len ))) return 0;
1469
1470     free( value->data );
1471     value->data = newptr;
1472     value->len  = len;
1473     value->type = type;
1474     return 1;
1475
1476  error:
1477     file_read_error( "Malformed value", info );
1478     free( value->data );
1479     value->data = NULL;
1480     value->len  = 0;
1481     value->type = REG_NONE;
1482     return 0;
1483 }
1484
1485 /* return the length (in path elements) of name that is part of the key name */
1486 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1487 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1488 {
1489     WCHAR *p;
1490     int res;
1491     data_size_t len;
1492
1493     if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1494
1495     len = info->tmplen;
1496     if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1497     {
1498         file_read_error( "Malformed key", info );
1499         return 0;
1500     }
1501     for (p = info->tmp; *p; p++) if (*p == '\\') break;
1502     len = (p - info->tmp) * sizeof(WCHAR);
1503     for (res = 1; key != root_key; res++)
1504     {
1505         if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
1506         key = key->parent;
1507     }
1508     if (key == root_key) res = 0;  /* no matching name */
1509     return res;
1510 }
1511
1512 /* load all the keys from the input file */
1513 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1514 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1515 {
1516     struct key *subkey = NULL;
1517     struct file_load_info info;
1518     char *p;
1519
1520     info.filename = filename;
1521     info.file   = f;
1522     info.len    = 4;
1523     info.tmplen = 4;
1524     info.line   = 0;
1525     if (!(info.buffer = mem_alloc( info.len ))) return;
1526     if (!(info.tmp = mem_alloc( info.tmplen )))
1527     {
1528         free( info.buffer );
1529         return;
1530     }
1531
1532     if ((read_next_line( &info ) != 1) ||
1533         strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1534     {
1535         set_error( STATUS_NOT_REGISTRY_FILE );
1536         goto done;
1537     }
1538
1539     while (read_next_line( &info ) == 1)
1540     {
1541         p = info.buffer;
1542         while (*p && isspace(*p)) p++;
1543         switch(*p)
1544         {
1545         case '[':   /* new key */
1546             if (subkey) release_object( subkey );
1547             if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1548             if (!(subkey = load_key( key, p + 1, prefix_len, &info )))
1549                 file_read_error( "Error creating key", &info );
1550             break;
1551         case '@':   /* default value */
1552         case '\"':  /* value */
1553             if (subkey) load_value( subkey, p, &info );
1554             else file_read_error( "Value without key", &info );
1555             break;
1556         case '#':   /* option */
1557             if (subkey) load_key_option( subkey, p, &info );
1558             else if (!load_global_option( p, &info )) goto done;
1559             break;
1560         case ';':   /* comment */
1561         case 0:     /* empty line */
1562             break;
1563         default:
1564             file_read_error( "Unrecognized input", &info );
1565             break;
1566         }
1567     }
1568
1569  done:
1570     if (subkey) release_object( subkey );
1571     free( info.buffer );
1572     free( info.tmp );
1573 }
1574
1575 /* load a part of the registry from a file */
1576 static void load_registry( struct key *key, obj_handle_t handle )
1577 {
1578     struct file *file;
1579     int fd;
1580
1581     if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1582     fd = dup( get_file_unix_fd( file ) );
1583     release_object( file );
1584     if (fd != -1)
1585     {
1586         FILE *f = fdopen( fd, "r" );
1587         if (f)
1588         {
1589             load_keys( key, NULL, f, -1 );
1590             fclose( f );
1591         }
1592         else file_set_error();
1593     }
1594 }
1595
1596 /* load one of the initial registry files */
1597 static int load_init_registry_from_file( const char *filename, struct key *key )
1598 {
1599     FILE *f;
1600
1601     if ((f = fopen( filename, "r" )))
1602     {
1603         load_keys( key, filename, f, 0 );
1604         fclose( f );
1605         if (get_error() == STATUS_NOT_REGISTRY_FILE)
1606         {
1607             fprintf( stderr, "%s is not a valid registry file\n", filename );
1608             return 1;
1609         }
1610     }
1611
1612     assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1613
1614     save_branch_info[save_branch_count].path = filename;
1615     save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1616     make_object_static( &key->obj );
1617     return (f != NULL);
1618 }
1619
1620 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1621 {
1622     static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1623     static const WCHAR formatW[] = {'-','%','u',0};
1624     WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1625     WCHAR *p = buffer;
1626     unsigned int i;
1627
1628     strcpyW( p, prefixW );
1629     p += strlenW( prefixW );
1630     p += sprintfW( p, formatW, sid->Revision );
1631     p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1632                                                    sid->IdentifierAuthority.Value[4] ),
1633                                          MAKEWORD( sid->IdentifierAuthority.Value[3],
1634                                                    sid->IdentifierAuthority.Value[2] )));
1635     for (i = 0; i < sid->SubAuthorityCount; i++)
1636         p += sprintfW( p, formatW, sid->SubAuthority[i] );
1637
1638     path->len = (p - buffer) * sizeof(WCHAR);
1639     path->str = p = memdup( buffer, path->len );
1640     return p;
1641 }
1642
1643 /* registry initialisation */
1644 void init_registry(void)
1645 {
1646     static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1647     static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1648     static const WCHAR classes[] = {'S','o','f','t','w','a','r','e','\\',
1649                                     'C','l','a','s','s','e','s','\\',
1650                                     'W','o','w','6','4','3','2','N','o','d','e'};
1651     static const struct unicode_str root_name = { NULL, 0 };
1652     static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1653     static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1654     static const struct unicode_str classes_name = { classes, sizeof(classes) };
1655
1656     WCHAR *current_user_path;
1657     struct unicode_str current_user_str;
1658     struct key *key, *hklm, *hkcu;
1659
1660     /* switch to the config dir */
1661
1662     if (fchdir( config_dir_fd ) == -1) fatal_perror( "chdir to config dir" );
1663
1664     /* create the root key */
1665     root_key = alloc_key( &root_name, current_time );
1666     assert( root_key );
1667     make_object_static( &root_key->obj );
1668
1669     /* load system.reg into Registry\Machine */
1670
1671     if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
1672         fatal_error( "could not create Machine registry key\n" );
1673
1674     if (!load_init_registry_from_file( "system.reg", hklm ))
1675         prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1676     else if (prefix_type == PREFIX_UNKNOWN)
1677         prefix_type = PREFIX_32BIT;
1678
1679     /* load userdef.reg into Registry\User\.Default */
1680
1681     if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
1682         fatal_error( "could not create User\\.Default registry key\n" );
1683
1684     load_init_registry_from_file( "userdef.reg", key );
1685     release_object( key );
1686
1687     /* load user.reg into HKEY_CURRENT_USER */
1688
1689     /* FIXME: match default user in token.c. should get from process token instead */
1690     current_user_path = format_user_registry_path( security_interactive_sid, &current_user_str );
1691     if (!current_user_path ||
1692         !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
1693         fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1694     free( current_user_path );
1695     load_init_registry_from_file( "user.reg", hkcu );
1696
1697     /* set the shared flag on Software\Classes\Wow6432Node */
1698     if (prefix_type == PREFIX_64BIT)
1699     {
1700         if ((key = create_key_recursive( hklm, &classes_name, current_time )))
1701         {
1702             key->flags |= KEY_WOWSHARE;
1703             release_object( key );
1704         }
1705         /* FIXME: handle HKCU too */
1706     }
1707
1708     release_object( hklm );
1709     release_object( hkcu );
1710
1711     /* start the periodic save timer */
1712     set_periodic_save_timer();
1713
1714     /* go back to the server dir */
1715     if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1716 }
1717
1718 /* save a registry branch to a file */
1719 static void save_all_subkeys( struct key *key, FILE *f )
1720 {
1721     fprintf( f, "WINE REGISTRY Version 2\n" );
1722     fprintf( f, ";; All keys relative to " );
1723     dump_path( key, NULL, f );
1724     fprintf( f, "\n" );
1725     switch (prefix_type)
1726     {
1727     case PREFIX_32BIT:
1728         fprintf( f, "\n#arch=win32\n" );
1729         break;
1730     case PREFIX_64BIT:
1731         fprintf( f, "\n#arch=win64\n" );
1732         break;
1733     default:
1734         break;
1735     }
1736     save_subkeys( key, key, f );
1737 }
1738
1739 /* save a registry branch to a file handle */
1740 static void save_registry( struct key *key, obj_handle_t handle )
1741 {
1742     struct file *file;
1743     int fd;
1744
1745     if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1746     fd = dup( get_file_unix_fd( file ) );
1747     release_object( file );
1748     if (fd != -1)
1749     {
1750         FILE *f = fdopen( fd, "w" );
1751         if (f)
1752         {
1753             save_all_subkeys( key, f );
1754             if (fclose( f )) file_set_error();
1755         }
1756         else
1757         {
1758             file_set_error();
1759             close( fd );
1760         }
1761     }
1762 }
1763
1764 /* save a registry branch to a file */
1765 static int save_branch( struct key *key, const char *path )
1766 {
1767     struct stat st;
1768     char *p, *tmp = NULL;
1769     int fd, count = 0, ret = 0;
1770     FILE *f;
1771
1772     if (!(key->flags & KEY_DIRTY))
1773     {
1774         if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1775         return 1;
1776     }
1777
1778     /* test the file type */
1779
1780     if ((fd = open( path, O_WRONLY )) != -1)
1781     {
1782         /* if file is not a regular file or has multiple links or is accessed
1783          * via symbolic links, write directly into it; otherwise use a temp file */
1784         if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1785         {
1786             ftruncate( fd, 0 );
1787             goto save;
1788         }
1789         close( fd );
1790     }
1791
1792     /* create a temp file in the same directory */
1793
1794     if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1795     strcpy( tmp, path );
1796     if ((p = strrchr( tmp, '/' ))) p++;
1797     else p = tmp;
1798     for (;;)
1799     {
1800         sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1801         if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1802         if (errno != EEXIST) goto done;
1803         close( fd );
1804     }
1805
1806     /* now save to it */
1807
1808  save:
1809     if (!(f = fdopen( fd, "w" )))
1810     {
1811         if (tmp) unlink( tmp );
1812         close( fd );
1813         goto done;
1814     }
1815
1816     if (debug_level > 1)
1817     {
1818         fprintf( stderr, "%s: ", path );
1819         dump_operation( key, NULL, "saving" );
1820     }
1821
1822     save_all_subkeys( key, f );
1823     ret = !fclose(f);
1824
1825     if (tmp)
1826     {
1827         /* if successfully written, rename to final name */
1828         if (ret) ret = !rename( tmp, path );
1829         if (!ret) unlink( tmp );
1830     }
1831
1832 done:
1833     free( tmp );
1834     if (ret) make_clean( key );
1835     return ret;
1836 }
1837
1838 /* periodic saving of the registry */
1839 static void periodic_save( void *arg )
1840 {
1841     int i;
1842
1843     if (fchdir( config_dir_fd ) == -1) return;
1844     save_timeout_user = NULL;
1845     for (i = 0; i < save_branch_count; i++)
1846         save_branch( save_branch_info[i].key, save_branch_info[i].path );
1847     if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1848     set_periodic_save_timer();
1849 }
1850
1851 /* start the periodic save timer */
1852 static void set_periodic_save_timer(void)
1853 {
1854     if (save_timeout_user) remove_timeout_user( save_timeout_user );
1855     save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
1856 }
1857
1858 /* save the modified registry branches to disk */
1859 void flush_registry(void)
1860 {
1861     int i;
1862
1863     if (fchdir( config_dir_fd ) == -1) return;
1864     for (i = 0; i < save_branch_count; i++)
1865     {
1866         if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1867         {
1868             fprintf( stderr, "wineserver: could not save registry branch to %s",
1869                      save_branch_info[i].path );
1870             perror( " " );
1871         }
1872     }
1873     if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1874 }
1875
1876
1877 /* create a registry key */
1878 DECL_HANDLER(create_key)
1879 {
1880     struct key *key = NULL, *parent;
1881     struct unicode_str name, class;
1882     unsigned int access = req->access;
1883
1884     if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1885
1886     reply->hkey = 0;
1887
1888     if (req->namelen > get_req_data_size())
1889     {
1890         set_error( STATUS_INVALID_PARAMETER );
1891         return;
1892     }
1893     class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1894     class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1895     get_req_path( &name, !req->parent );
1896     if (name.str > class.str)
1897     {
1898         set_error( STATUS_INVALID_PARAMETER );
1899         return;
1900     }
1901     name.len = (class.str - name.str) * sizeof(WCHAR);
1902
1903     /* NOTE: no access rights are required from the parent handle to create a key */
1904     if ((parent = get_parent_hkey_obj( req->parent )))
1905     {
1906         if ((key = create_key( parent, &name, &class, req->options, access,
1907                                req->attributes, &reply->created )))
1908         {
1909             reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1910             release_object( key );
1911         }
1912         release_object( parent );
1913     }
1914 }
1915
1916 /* open a registry key */
1917 DECL_HANDLER(open_key)
1918 {
1919     struct key *key, *parent;
1920     struct unicode_str name;
1921     unsigned int access = req->access;
1922
1923     if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1924
1925     reply->hkey = 0;
1926     /* NOTE: no access rights are required to open the parent key, only the child key */
1927     if ((parent = get_parent_hkey_obj( req->parent )))
1928     {
1929         get_req_path( &name, !req->parent );
1930         if ((key = open_key( parent, &name, access, req->attributes )))
1931         {
1932             reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1933             release_object( key );
1934         }
1935         release_object( parent );
1936     }
1937 }
1938
1939 /* delete a registry key */
1940 DECL_HANDLER(delete_key)
1941 {
1942     struct key *key;
1943
1944     if ((key = get_hkey_obj( req->hkey, DELETE )))
1945     {
1946         delete_key( key, 0);
1947         release_object( key );
1948     }
1949 }
1950
1951 /* flush a registry key */
1952 DECL_HANDLER(flush_key)
1953 {
1954     struct key *key = get_hkey_obj( req->hkey, 0 );
1955     if (key)
1956     {
1957         /* we don't need to do anything here with the current implementation */
1958         release_object( key );
1959     }
1960 }
1961
1962 /* enumerate registry subkeys */
1963 DECL_HANDLER(enum_key)
1964 {
1965     struct key *key;
1966
1967     if ((key = get_hkey_obj( req->hkey,
1968                              req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1969     {
1970         enum_key( key, req->index, req->info_class, reply );
1971         release_object( key );
1972     }
1973 }
1974
1975 /* set a value of a registry key */
1976 DECL_HANDLER(set_key_value)
1977 {
1978     struct key *key;
1979     struct unicode_str name;
1980
1981     if (req->namelen > get_req_data_size())
1982     {
1983         set_error( STATUS_INVALID_PARAMETER );
1984         return;
1985     }
1986     name.str = get_req_data();
1987     name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
1988
1989     if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1990     {
1991         data_size_t datalen = get_req_data_size() - req->namelen;
1992         const char *data = (const char *)get_req_data() + req->namelen;
1993
1994         set_value( key, &name, req->type, data, datalen );
1995         release_object( key );
1996     }
1997 }
1998
1999 /* retrieve the value of a registry key */
2000 DECL_HANDLER(get_key_value)
2001 {
2002     struct key *key;
2003     struct unicode_str name;
2004
2005     reply->total = 0;
2006     if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2007     {
2008         get_req_unicode_str( &name );
2009         get_value( key, &name, &reply->type, &reply->total );
2010         release_object( key );
2011     }
2012 }
2013
2014 /* enumerate the value of a registry key */
2015 DECL_HANDLER(enum_key_value)
2016 {
2017     struct key *key;
2018
2019     if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2020     {
2021         enum_value( key, req->index, req->info_class, reply );
2022         release_object( key );
2023     }
2024 }
2025
2026 /* delete a value of a registry key */
2027 DECL_HANDLER(delete_key_value)
2028 {
2029     struct key *key;
2030     struct unicode_str name;
2031
2032     if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2033     {
2034         get_req_unicode_str( &name );
2035         delete_value( key, &name );
2036         release_object( key );
2037     }
2038 }
2039
2040 /* load a registry branch from a file */
2041 DECL_HANDLER(load_registry)
2042 {
2043     struct key *key, *parent;
2044     struct token *token = thread_get_impersonation_token( current );
2045     struct unicode_str name;
2046
2047     const LUID_AND_ATTRIBUTES privs[] =
2048     {
2049         { SeBackupPrivilege,  0 },
2050         { SeRestorePrivilege, 0 },
2051     };
2052
2053     if (!token || !token_check_privileges( token, TRUE, privs,
2054                                            sizeof(privs)/sizeof(privs[0]), NULL ))
2055     {
2056         set_error( STATUS_PRIVILEGE_NOT_HELD );
2057         return;
2058     }
2059
2060     if ((parent = get_parent_hkey_obj( req->hkey )))
2061     {
2062         int dummy;
2063         get_req_path( &name, !req->hkey );
2064         if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, &dummy )))
2065         {
2066             load_registry( key, req->file );
2067             release_object( key );
2068         }
2069         release_object( parent );
2070     }
2071 }
2072
2073 DECL_HANDLER(unload_registry)
2074 {
2075     struct key *key;
2076     struct token *token = thread_get_impersonation_token( current );
2077
2078     const LUID_AND_ATTRIBUTES privs[] =
2079     {
2080         { SeBackupPrivilege,  0 },
2081         { SeRestorePrivilege, 0 },
2082     };
2083
2084     if (!token || !token_check_privileges( token, TRUE, privs,
2085                                            sizeof(privs)/sizeof(privs[0]), NULL ))
2086     {
2087         set_error( STATUS_PRIVILEGE_NOT_HELD );
2088         return;
2089     }
2090
2091     if ((key = get_hkey_obj( req->hkey, 0 )))
2092     {
2093         delete_key( key, 1 );     /* FIXME */
2094         release_object( key );
2095     }
2096 }
2097
2098 /* save a registry branch to a file */
2099 DECL_HANDLER(save_registry)
2100 {
2101     struct key *key;
2102
2103     if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2104     {
2105         set_error( STATUS_PRIVILEGE_NOT_HELD );
2106         return;
2107     }
2108
2109     if ((key = get_hkey_obj( req->hkey, 0 )))
2110     {
2111         save_registry( key, req->file );
2112         release_object( key );
2113     }
2114 }
2115
2116 /* add a registry key change notification */
2117 DECL_HANDLER(set_registry_notification)
2118 {
2119     struct key *key;
2120     struct event *event;
2121     struct notify *notify;
2122
2123     key = get_hkey_obj( req->hkey, KEY_NOTIFY );
2124     if (key)
2125     {
2126         event = get_event_obj( current->process, req->event, SYNCHRONIZE );
2127         if (event)
2128         {
2129             notify = find_notify( key, current->process, req->hkey );
2130             if (notify)
2131             {
2132                 if (notify->event)
2133                     release_object( notify->event );
2134                 grab_object( event );
2135                 notify->event = event;
2136             }
2137             else
2138             {
2139                 notify = mem_alloc( sizeof(*notify) );
2140                 if (notify)
2141                 {
2142                     grab_object( event );
2143                     notify->event   = event;
2144                     notify->subtree = req->subtree;
2145                     notify->filter  = req->filter;
2146                     notify->hkey    = req->hkey;
2147                     notify->process = current->process;
2148                     list_add_head( &key->notify_list, &notify->entry );
2149                 }
2150             }
2151             release_object( event );
2152         }
2153         release_object( key );
2154     }
2155 }