winapi: trampoline is not used so remove it.
[wine/hacks.git] / server / registry.c
blobd83486b8d1b9ee000c766f4acf0a41c803f1366e
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 */
87 /* a key value */
88 struct key_value
90 WCHAR *name; /* value name */
91 unsigned short namelen; /* length of value name */
92 unsigned short type; /* value type */
93 data_size_t len; /* value data length in bytes */
94 void *data; /* pointer to value data */
97 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
98 #define MIN_VALUES 8 /* min. number of allocated values per key */
100 #define MAX_NAME_LEN MAX_PATH /* max. length of a key name */
101 #define MAX_VALUE_LEN MAX_PATH /* max. length of a value name */
103 /* the root of the registry tree */
104 static struct key *root_key;
106 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
107 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
108 static struct timeout_user *save_timeout_user; /* saving timer */
110 static void set_periodic_save_timer(void);
112 /* information about where to save a registry branch */
113 struct save_branch_info
115 struct key *key;
116 const char *path;
119 #define MAX_SAVE_BRANCH_INFO 3
120 static int save_branch_count;
121 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
124 /* information about a file being loaded */
125 struct file_load_info
127 const char *filename; /* input file name */
128 FILE *file; /* input file */
129 char *buffer; /* line buffer */
130 int len; /* buffer length */
131 int line; /* current input line */
132 WCHAR *tmp; /* temp buffer to use while parsing input */
133 size_t tmplen; /* length of temp buffer */
137 static void key_dump( struct object *obj, int verbose );
138 static unsigned int key_map_access( struct object *obj, unsigned int access );
139 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
140 static void key_destroy( struct object *obj );
142 static const struct object_ops key_ops =
144 sizeof(struct key), /* size */
145 key_dump, /* dump */
146 no_get_type, /* get_type */
147 no_add_queue, /* add_queue */
148 NULL, /* remove_queue */
149 NULL, /* signaled */
150 NULL, /* satisfied */
151 no_signal, /* signal */
152 no_get_fd, /* get_fd */
153 key_map_access, /* map_access */
154 default_get_sd, /* get_sd */
155 default_set_sd, /* set_sd */
156 no_lookup_name, /* lookup_name */
157 no_open_file, /* open_file */
158 key_close_handle, /* close_handle */
159 key_destroy /* destroy */
164 * The registry text file format v2 used by this code is similar to the one
165 * used by REGEDIT import/export functionality, with the following differences:
166 * - strings and key names can contain \x escapes for Unicode
167 * - key names use escapes too in order to support Unicode
168 * - the modification time optionally follows the key name
169 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
172 /* dump the full path of a key */
173 static void dump_path( const struct key *key, const struct key *base, FILE *f )
175 if (key->parent && key->parent != base)
177 dump_path( key->parent, base, f );
178 fprintf( f, "\\\\" );
180 dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
183 /* dump a value to a text file */
184 static void dump_value( const struct key_value *value, FILE *f )
186 unsigned int i, dw;
187 int count;
189 if (value->namelen)
191 fputc( '\"', f );
192 count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
193 count += fprintf( f, "\"=" );
195 else count = fprintf( f, "@=" );
197 switch(value->type)
199 case REG_SZ:
200 case REG_EXPAND_SZ:
201 case REG_MULTI_SZ:
202 /* only output properly terminated strings in string format */
203 if (value->len < sizeof(WCHAR)) break;
204 if (value->len % sizeof(WCHAR)) break;
205 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
206 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
207 fputc( '\"', f );
208 dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
209 fprintf( f, "\"\n" );
210 return;
212 case REG_DWORD:
213 if (value->len != sizeof(dw)) break;
214 memcpy( &dw, value->data, sizeof(dw) );
215 fprintf( f, "dword:%08x\n", dw );
216 return;
219 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
220 else count += fprintf( f, "hex(%x):", value->type );
221 for (i = 0; i < value->len; i++)
223 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
224 if (i < value->len-1)
226 fputc( ',', f );
227 if (++count > 76)
229 fprintf( f, "\\\n " );
230 count = 2;
234 fputc( '\n', f );
237 /* save a registry and all its subkeys to a text file */
238 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
240 int i;
242 if (key->flags & KEY_VOLATILE) return;
243 /* save key if it has either some values or no subkeys */
244 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
245 if ((key->last_value >= 0) || (key->last_subkey == -1))
247 fprintf( f, "\n[" );
248 if (key != base) dump_path( key, base, f );
249 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
250 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
252 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
255 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
257 fprintf( stderr, "%s key ", op );
258 if (key) dump_path( key, NULL, stderr );
259 else fprintf( stderr, "ERROR" );
260 if (value)
262 fprintf( stderr, " value ");
263 dump_value( value, stderr );
265 else fprintf( stderr, "\n" );
268 static void key_dump( struct object *obj, int verbose )
270 struct key *key = (struct key *)obj;
271 assert( obj->ops == &key_ops );
272 fprintf( stderr, "Key flags=%x ", key->flags );
273 dump_path( key, NULL, stderr );
274 fprintf( stderr, "\n" );
277 /* notify waiter and maybe delete the notification */
278 static void do_notification( struct key *key, struct notify *notify, int del )
280 if (notify->event)
282 set_event( notify->event );
283 release_object( notify->event );
284 notify->event = NULL;
286 if (del)
288 list_remove( &notify->entry );
289 free( notify );
293 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
295 struct notify *notify;
297 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
299 if (notify->process == process && notify->hkey == hkey) return notify;
301 return NULL;
304 static unsigned int key_map_access( struct object *obj, unsigned int access )
306 if (access & GENERIC_READ) access |= KEY_READ;
307 if (access & GENERIC_WRITE) access |= KEY_WRITE;
308 if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
309 if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS;
310 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
313 /* close the notification associated with a handle */
314 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
316 struct key * key = (struct key *) obj;
317 struct notify *notify = find_notify( key, process, handle );
318 if (notify) do_notification( key, notify, 1 );
319 return 1; /* ok to close */
322 static void key_destroy( struct object *obj )
324 int i;
325 struct list *ptr;
326 struct key *key = (struct key *)obj;
327 assert( obj->ops == &key_ops );
329 free( key->name );
330 free( key->class );
331 for (i = 0; i <= key->last_value; i++)
333 free( key->values[i].name );
334 free( key->values[i].data );
336 free( key->values );
337 for (i = 0; i <= key->last_subkey; i++)
339 key->subkeys[i]->parent = NULL;
340 release_object( key->subkeys[i] );
342 free( key->subkeys );
343 /* unconditionally notify everything waiting on this key */
344 while ((ptr = list_head( &key->notify_list )))
346 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
347 do_notification( key, notify, 1 );
351 /* get the request vararg as registry path */
352 static inline void get_req_path( struct unicode_str *str, int skip_root )
354 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
356 str->str = get_req_data();
357 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
359 if (skip_root && str->len >= sizeof(root_name) &&
360 !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
362 str->str += sizeof(root_name)/sizeof(WCHAR);
363 str->len -= sizeof(root_name);
367 /* return the next token in a given path */
368 /* token->str must point inside the path, or be NULL for the first call */
369 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
371 data_size_t i = 0, len = path->len / sizeof(WCHAR);
373 if (!token->str) /* first time */
375 /* path cannot start with a backslash */
376 if (len && path->str[0] == '\\')
378 set_error( STATUS_OBJECT_PATH_INVALID );
379 return NULL;
382 else
384 i = token->str - path->str;
385 i += token->len / sizeof(WCHAR);
386 while (i < len && path->str[i] == '\\') i++;
388 token->str = path->str + i;
389 while (i < len && path->str[i] != '\\') i++;
390 token->len = (path->str + i - token->str) * sizeof(WCHAR);
391 return token;
394 /* allocate a key object */
395 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
397 struct key *key;
398 if ((key = alloc_object( &key_ops )))
400 key->name = NULL;
401 key->class = NULL;
402 key->namelen = name->len;
403 key->classlen = 0;
404 key->flags = 0;
405 key->last_subkey = -1;
406 key->nb_subkeys = 0;
407 key->subkeys = NULL;
408 key->nb_values = 0;
409 key->last_value = -1;
410 key->values = NULL;
411 key->modif = modif;
412 key->parent = NULL;
413 list_init( &key->notify_list );
414 if (name->len && !(key->name = memdup( name->str, name->len )))
416 release_object( key );
417 key = NULL;
420 return key;
423 /* mark a key and all its parents as dirty (modified) */
424 static void make_dirty( struct key *key )
426 while (key)
428 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
429 key->flags |= KEY_DIRTY;
430 key = key->parent;
434 /* mark a key and all its subkeys as clean (not modified) */
435 static void make_clean( struct key *key )
437 int i;
439 if (key->flags & KEY_VOLATILE) return;
440 if (!(key->flags & KEY_DIRTY)) return;
441 key->flags &= ~KEY_DIRTY;
442 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
445 /* go through all the notifications and send them if necessary */
446 static void check_notify( struct key *key, unsigned int change, int not_subtree )
448 struct list *ptr, *next;
450 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
452 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
453 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
454 do_notification( key, n, 0 );
458 /* update key modification time */
459 static void touch_key( struct key *key, unsigned int change )
461 struct key *k;
463 key->modif = current_time;
464 make_dirty( key );
466 /* do notifications */
467 check_notify( key, change, 1 );
468 for ( k = key->parent; k; k = k->parent )
469 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
472 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
473 static int grow_subkeys( struct key *key )
475 struct key **new_subkeys;
476 int nb_subkeys;
478 if (key->nb_subkeys)
480 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
481 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
483 set_error( STATUS_NO_MEMORY );
484 return 0;
487 else
489 nb_subkeys = MIN_VALUES;
490 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
492 key->subkeys = new_subkeys;
493 key->nb_subkeys = nb_subkeys;
494 return 1;
497 /* allocate a subkey for a given key, and return its index */
498 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
499 int index, timeout_t modif )
501 struct key *key;
502 int i;
504 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
506 set_error( STATUS_NAME_TOO_LONG );
507 return NULL;
509 if (parent->last_subkey + 1 == parent->nb_subkeys)
511 /* need to grow the array */
512 if (!grow_subkeys( parent )) return NULL;
514 if ((key = alloc_key( name, modif )) != NULL)
516 key->parent = parent;
517 for (i = ++parent->last_subkey; i > index; i--)
518 parent->subkeys[i] = parent->subkeys[i-1];
519 parent->subkeys[index] = key;
521 return key;
524 /* free a subkey of a given key */
525 static void free_subkey( struct key *parent, int index )
527 struct key *key;
528 int i, nb_subkeys;
530 assert( index >= 0 );
531 assert( index <= parent->last_subkey );
533 key = parent->subkeys[index];
534 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
535 parent->last_subkey--;
536 key->flags |= KEY_DELETED;
537 key->parent = NULL;
538 release_object( key );
540 /* try to shrink the array */
541 nb_subkeys = parent->nb_subkeys;
542 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
544 struct key **new_subkeys;
545 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
546 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
547 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
548 parent->subkeys = new_subkeys;
549 parent->nb_subkeys = nb_subkeys;
553 /* find the named child of a given key and return its index */
554 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
556 int i, min, max, res;
557 data_size_t len;
559 min = 0;
560 max = key->last_subkey;
561 while (min <= max)
563 i = (min + max) / 2;
564 len = min( key->subkeys[i]->namelen, name->len );
565 res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
566 if (!res) res = key->subkeys[i]->namelen - name->len;
567 if (!res)
569 *index = i;
570 return key->subkeys[i];
572 if (res > 0) max = i - 1;
573 else min = i + 1;
575 *index = min; /* this is where we should insert it */
576 return NULL;
579 /* open a subkey */
580 static struct key *open_key( struct key *key, const struct unicode_str *name )
582 int index;
583 struct unicode_str token;
585 token.str = NULL;
586 if (!get_path_token( name, &token )) return NULL;
587 while (token.len)
589 if (!(key = find_subkey( key, &token, &index )))
591 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
592 break;
594 get_path_token( name, &token );
597 if (debug_level > 1) dump_operation( key, NULL, "Open" );
598 if (key) grab_object( key );
599 return key;
602 /* create a subkey */
603 static struct key *create_key( struct key *key, const struct unicode_str *name,
604 const struct unicode_str *class, int flags, timeout_t modif, int *created )
606 struct key *base;
607 int index;
608 struct unicode_str token;
610 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
612 set_error( STATUS_KEY_DELETED );
613 return NULL;
615 if (!(flags & KEY_VOLATILE) && (key->flags & KEY_VOLATILE))
617 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
618 return NULL;
621 token.str = NULL;
622 if (!get_path_token( name, &token )) return NULL;
623 *created = 0;
624 while (token.len)
626 struct key *subkey;
627 if (!(subkey = find_subkey( key, &token, &index ))) break;
628 key = subkey;
629 get_path_token( name, &token );
632 /* create the remaining part */
634 if (!token.len) goto done;
635 *created = 1;
636 if (flags & KEY_DIRTY) make_dirty( key );
637 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
638 base = key;
639 for (;;)
641 key->flags |= flags;
642 get_path_token( name, &token );
643 if (!token.len) break;
644 /* we know the index is always 0 in a new key */
645 if (!(key = alloc_subkey( key, &token, 0, modif )))
647 free_subkey( base, index );
648 return NULL;
652 done:
653 if (debug_level > 1) dump_operation( key, NULL, "Create" );
654 if (class && class->len)
656 key->classlen = class->len;
657 free(key->class);
658 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
660 grab_object( key );
661 return key;
664 /* query information about a key or a subkey */
665 static void enum_key( const struct key *key, int index, int info_class,
666 struct enum_key_reply *reply )
668 int i;
669 data_size_t len, namelen, classlen;
670 data_size_t max_subkey = 0, max_class = 0;
671 data_size_t max_value = 0, max_data = 0;
672 char *data;
674 if (index != -1) /* -1 means use the specified key directly */
676 if ((index < 0) || (index > key->last_subkey))
678 set_error( STATUS_NO_MORE_ENTRIES );
679 return;
681 key = key->subkeys[index];
684 namelen = key->namelen;
685 classlen = key->classlen;
687 switch(info_class)
689 case KeyBasicInformation:
690 classlen = 0; /* only return the name */
691 /* fall through */
692 case KeyNodeInformation:
693 reply->max_subkey = 0;
694 reply->max_class = 0;
695 reply->max_value = 0;
696 reply->max_data = 0;
697 break;
698 case KeyFullInformation:
699 for (i = 0; i <= key->last_subkey; i++)
701 struct key *subkey = key->subkeys[i];
702 len = subkey->namelen / sizeof(WCHAR);
703 if (len > max_subkey) max_subkey = len;
704 len = subkey->classlen / sizeof(WCHAR);
705 if (len > max_class) max_class = len;
707 for (i = 0; i <= key->last_value; i++)
709 len = key->values[i].namelen / sizeof(WCHAR);
710 if (len > max_value) max_value = len;
711 len = key->values[i].len;
712 if (len > max_data) max_data = len;
714 reply->max_subkey = max_subkey;
715 reply->max_class = max_class;
716 reply->max_value = max_value;
717 reply->max_data = max_data;
718 namelen = 0; /* only return the class */
719 break;
720 default:
721 set_error( STATUS_INVALID_PARAMETER );
722 return;
724 reply->subkeys = key->last_subkey + 1;
725 reply->values = key->last_value + 1;
726 reply->modif = key->modif;
727 reply->total = namelen + classlen;
729 len = min( reply->total, get_reply_max_size() );
730 if (len && (data = set_reply_data_size( len )))
732 if (len > namelen)
734 reply->namelen = namelen;
735 memcpy( data, key->name, namelen );
736 memcpy( data + namelen, key->class, len - namelen );
738 else
740 reply->namelen = len;
741 memcpy( data, key->name, len );
744 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
747 /* delete a key and its values */
748 static int delete_key( struct key *key, int recurse )
750 int index;
751 struct key *parent;
753 /* must find parent and index */
754 if (key == root_key)
756 set_error( STATUS_ACCESS_DENIED );
757 return -1;
759 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
761 set_error( STATUS_KEY_DELETED );
762 return -1;
765 while (recurse && (key->last_subkey>=0))
766 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
767 return -1;
769 for (index = 0; index <= parent->last_subkey; index++)
770 if (parent->subkeys[index] == key) break;
771 assert( index <= parent->last_subkey );
773 /* we can only delete a key that has no subkeys */
774 if (key->last_subkey >= 0)
776 set_error( STATUS_ACCESS_DENIED );
777 return -1;
780 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
781 free_subkey( parent, index );
782 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
783 return 0;
786 /* try to grow the array of values; return 1 if OK, 0 on error */
787 static int grow_values( struct key *key )
789 struct key_value *new_val;
790 int nb_values;
792 if (key->nb_values)
794 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
795 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
797 set_error( STATUS_NO_MEMORY );
798 return 0;
801 else
803 nb_values = MIN_VALUES;
804 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
806 key->values = new_val;
807 key->nb_values = nb_values;
808 return 1;
811 /* find the named value of a given key and return its index in the array */
812 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
814 int i, min, max, res;
815 data_size_t len;
817 min = 0;
818 max = key->last_value;
819 while (min <= max)
821 i = (min + max) / 2;
822 len = min( key->values[i].namelen, name->len );
823 res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
824 if (!res) res = key->values[i].namelen - name->len;
825 if (!res)
827 *index = i;
828 return &key->values[i];
830 if (res > 0) max = i - 1;
831 else min = i + 1;
833 *index = min; /* this is where we should insert it */
834 return NULL;
837 /* insert a new value; the index must have been returned by find_value */
838 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
840 struct key_value *value;
841 WCHAR *new_name = NULL;
842 int i;
844 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
846 set_error( STATUS_NAME_TOO_LONG );
847 return NULL;
849 if (key->last_value + 1 == key->nb_values)
851 if (!grow_values( key )) return NULL;
853 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
854 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
855 value = &key->values[index];
856 value->name = new_name;
857 value->namelen = name->len;
858 value->len = 0;
859 value->data = NULL;
860 return value;
863 /* set a key value */
864 static void set_value( struct key *key, const struct unicode_str *name,
865 int type, const void *data, data_size_t len )
867 struct key_value *value;
868 void *ptr = NULL;
869 int index;
871 if ((value = find_value( key, name, &index )))
873 /* check if the new value is identical to the existing one */
874 if (value->type == type && value->len == len &&
875 value->data && !memcmp( value->data, data, len ))
877 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
878 return;
882 if (len && !(ptr = memdup( data, len ))) return;
884 if (!value)
886 if (!(value = insert_value( key, name, index )))
888 free( ptr );
889 return;
892 else free( value->data ); /* already existing, free previous data */
894 value->type = type;
895 value->len = len;
896 value->data = ptr;
897 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
898 if (debug_level > 1) dump_operation( key, value, "Set" );
901 /* get a key value */
902 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
904 struct key_value *value;
905 int index;
907 if ((value = find_value( key, name, &index )))
909 *type = value->type;
910 *len = value->len;
911 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
912 if (debug_level > 1) dump_operation( key, value, "Get" );
914 else
916 *type = -1;
917 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
921 /* enumerate a key value */
922 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
924 struct key_value *value;
926 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
927 else
929 void *data;
930 data_size_t namelen, maxlen;
932 value = &key->values[i];
933 reply->type = value->type;
934 namelen = value->namelen;
936 switch(info_class)
938 case KeyValueBasicInformation:
939 reply->total = namelen;
940 break;
941 case KeyValueFullInformation:
942 reply->total = namelen + value->len;
943 break;
944 case KeyValuePartialInformation:
945 reply->total = value->len;
946 namelen = 0;
947 break;
948 default:
949 set_error( STATUS_INVALID_PARAMETER );
950 return;
953 maxlen = min( reply->total, get_reply_max_size() );
954 if (maxlen && ((data = set_reply_data_size( maxlen ))))
956 if (maxlen > namelen)
958 reply->namelen = namelen;
959 memcpy( data, value->name, namelen );
960 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
962 else
964 reply->namelen = maxlen;
965 memcpy( data, value->name, maxlen );
968 if (debug_level > 1) dump_operation( key, value, "Enum" );
972 /* delete a value */
973 static void delete_value( struct key *key, const struct unicode_str *name )
975 struct key_value *value;
976 int i, index, nb_values;
978 if (!(value = find_value( key, name, &index )))
980 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
981 return;
983 if (debug_level > 1) dump_operation( key, value, "Delete" );
984 free( value->name );
985 free( value->data );
986 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
987 key->last_value--;
988 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
990 /* try to shrink the array */
991 nb_values = key->nb_values;
992 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
994 struct key_value *new_val;
995 nb_values -= nb_values / 3; /* shrink by 33% */
996 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
997 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
998 key->values = new_val;
999 key->nb_values = nb_values;
1003 /* get the registry key corresponding to an hkey handle */
1004 static inline struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1006 return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1009 /* get the registry key corresponding to a parent key handle */
1010 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1012 if (!hkey) return (struct key *)grab_object( root_key );
1013 return (struct key *)get_handle_obj( current->process, hkey, 0, &key_ops );
1016 /* read a line from the input file */
1017 static int read_next_line( struct file_load_info *info )
1019 char *newbuf;
1020 int newlen, pos = 0;
1022 info->line++;
1023 for (;;)
1025 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1026 return (pos != 0); /* EOF */
1027 pos = strlen(info->buffer);
1028 if (info->buffer[pos-1] == '\n')
1030 /* got a full line */
1031 info->buffer[--pos] = 0;
1032 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1033 return 1;
1035 if (pos < info->len - 1) return 1; /* EOF but something was read */
1037 /* need to enlarge the buffer */
1038 newlen = info->len + info->len / 2;
1039 if (!(newbuf = realloc( info->buffer, newlen )))
1041 set_error( STATUS_NO_MEMORY );
1042 return -1;
1044 info->buffer = newbuf;
1045 info->len = newlen;
1049 /* make sure the temp buffer holds enough space */
1050 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1052 WCHAR *tmp;
1053 if (info->tmplen >= size) return 1;
1054 if (!(tmp = realloc( info->tmp, size )))
1056 set_error( STATUS_NO_MEMORY );
1057 return 0;
1059 info->tmp = tmp;
1060 info->tmplen = size;
1061 return 1;
1064 /* report an error while loading an input file */
1065 static void file_read_error( const char *err, struct file_load_info *info )
1067 if (info->filename)
1068 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1069 else
1070 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1073 /* convert a data type tag to a value type */
1074 static int get_data_type( const char *buffer, int *type, int *parse_type )
1076 struct data_type { const char *tag; int len; int type; int parse_type; };
1078 static const struct data_type data_types[] =
1079 { /* actual type */ /* type to assume for parsing */
1080 { "\"", 1, REG_SZ, REG_SZ },
1081 { "str:\"", 5, REG_SZ, REG_SZ },
1082 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1083 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1084 { "hex:", 4, REG_BINARY, REG_BINARY },
1085 { "dword:", 6, REG_DWORD, REG_DWORD },
1086 { "hex(", 4, -1, REG_BINARY },
1087 { NULL, 0, 0, 0 }
1090 const struct data_type *ptr;
1091 char *end;
1093 for (ptr = data_types; ptr->tag; ptr++)
1095 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1096 *parse_type = ptr->parse_type;
1097 if ((*type = ptr->type) != -1) return ptr->len;
1098 /* "hex(xx):" is special */
1099 *type = (int)strtoul( buffer + 4, &end, 16 );
1100 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1101 return end + 2 - buffer;
1103 return 0;
1106 /* load and create a key from the input file */
1107 static struct key *load_key( struct key *base, const char *buffer, int flags,
1108 int prefix_len, struct file_load_info *info )
1110 WCHAR *p;
1111 struct unicode_str name;
1112 int res;
1113 unsigned int mod;
1114 timeout_t modif = current_time;
1115 data_size_t len;
1117 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1119 len = info->tmplen;
1120 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1122 file_read_error( "Malformed key", info );
1123 return NULL;
1125 if (sscanf( buffer + res, " %u", &mod ) == 1)
1126 modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1128 p = info->tmp;
1129 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1131 if (!*p)
1133 if (prefix_len > 1)
1135 file_read_error( "Malformed key", info );
1136 return NULL;
1138 /* empty key name, return base key */
1139 return (struct key *)grab_object( base );
1141 name.str = p;
1142 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1143 return create_key( base, &name, NULL, flags, modif, &res );
1146 /* parse a comma-separated list of hex digits */
1147 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1149 const char *p = buffer;
1150 data_size_t count = 0;
1151 char *end;
1153 while (isxdigit(*p))
1155 unsigned int val = strtoul( p, &end, 16 );
1156 if (end == p || val > 0xff) return -1;
1157 if (count++ >= *len) return -1; /* dest buffer overflow */
1158 *dest++ = val;
1159 p = end;
1160 while (isspace(*p)) p++;
1161 if (*p == ',') p++;
1162 while (isspace(*p)) p++;
1164 *len = count;
1165 return p - buffer;
1168 /* parse a value name and create the corresponding value */
1169 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1170 struct file_load_info *info )
1172 struct key_value *value;
1173 struct unicode_str name;
1174 int index;
1176 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1177 name.str = info->tmp;
1178 name.len = info->tmplen;
1179 if (buffer[0] == '@')
1181 name.len = 0;
1182 *len = 1;
1184 else
1186 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1187 if (r == -1) goto error;
1188 *len = r + 1; /* for initial quote */
1189 name.len -= sizeof(WCHAR); /* terminating null */
1191 while (isspace(buffer[*len])) (*len)++;
1192 if (buffer[*len] != '=') goto error;
1193 (*len)++;
1194 while (isspace(buffer[*len])) (*len)++;
1195 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1196 return value;
1198 error:
1199 file_read_error( "Malformed value name", info );
1200 return NULL;
1203 /* load a value from the input file */
1204 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1206 DWORD dw;
1207 void *ptr, *newptr;
1208 int res, type, parse_type;
1209 data_size_t maxlen, len;
1210 struct key_value *value;
1212 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1213 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1214 buffer += len + res;
1216 switch(parse_type)
1218 case REG_SZ:
1219 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1220 len = info->tmplen;
1221 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1222 ptr = info->tmp;
1223 break;
1224 case REG_DWORD:
1225 dw = strtoul( buffer, NULL, 16 );
1226 ptr = &dw;
1227 len = sizeof(dw);
1228 break;
1229 case REG_BINARY: /* hex digits */
1230 len = 0;
1231 for (;;)
1233 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1234 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1235 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1236 len += maxlen;
1237 buffer += res;
1238 while (isspace(*buffer)) buffer++;
1239 if (!*buffer) break;
1240 if (*buffer != '\\') goto error;
1241 if (read_next_line( info) != 1) goto error;
1242 buffer = info->buffer;
1243 while (isspace(*buffer)) buffer++;
1245 ptr = info->tmp;
1246 break;
1247 default:
1248 assert(0);
1249 ptr = NULL; /* keep compiler quiet */
1250 break;
1253 if (!len) newptr = NULL;
1254 else if (!(newptr = memdup( ptr, len ))) return 0;
1256 free( value->data );
1257 value->data = newptr;
1258 value->len = len;
1259 value->type = type;
1260 make_dirty( key );
1261 return 1;
1263 error:
1264 file_read_error( "Malformed value", info );
1265 free( value->data );
1266 value->data = NULL;
1267 value->len = 0;
1268 value->type = REG_NONE;
1269 make_dirty( key );
1270 return 0;
1273 /* return the length (in path elements) of name that is part of the key name */
1274 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1275 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1277 WCHAR *p;
1278 int res;
1279 data_size_t len;
1281 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1283 len = info->tmplen;
1284 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1286 file_read_error( "Malformed key", info );
1287 return 0;
1289 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1290 len = (p - info->tmp) * sizeof(WCHAR);
1291 for (res = 1; key != root_key; res++)
1293 if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
1294 key = key->parent;
1296 if (key == root_key) res = 0; /* no matching name */
1297 return res;
1300 /* load all the keys from the input file */
1301 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1302 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1304 struct key *subkey = NULL;
1305 struct file_load_info info;
1306 char *p;
1307 int flags = (key->flags & KEY_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1309 info.filename = filename;
1310 info.file = f;
1311 info.len = 4;
1312 info.tmplen = 4;
1313 info.line = 0;
1314 if (!(info.buffer = mem_alloc( info.len ))) return;
1315 if (!(info.tmp = mem_alloc( info.tmplen )))
1317 free( info.buffer );
1318 return;
1321 if ((read_next_line( &info ) != 1) ||
1322 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1324 set_error( STATUS_NOT_REGISTRY_FILE );
1325 goto done;
1328 while (read_next_line( &info ) == 1)
1330 p = info.buffer;
1331 while (*p && isspace(*p)) p++;
1332 switch(*p)
1334 case '[': /* new key */
1335 if (subkey) release_object( subkey );
1336 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1337 if (!(subkey = load_key( key, p + 1, flags, prefix_len, &info )))
1338 file_read_error( "Error creating key", &info );
1339 break;
1340 case '@': /* default value */
1341 case '\"': /* value */
1342 if (subkey) load_value( subkey, p, &info );
1343 else file_read_error( "Value without key", &info );
1344 break;
1345 case '#': /* comment */
1346 case ';': /* comment */
1347 case 0: /* empty line */
1348 break;
1349 default:
1350 file_read_error( "Unrecognized input", &info );
1351 break;
1355 done:
1356 if (subkey) release_object( subkey );
1357 free( info.buffer );
1358 free( info.tmp );
1361 /* load a part of the registry from a file */
1362 static void load_registry( struct key *key, obj_handle_t handle )
1364 struct file *file;
1365 int fd;
1367 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1368 fd = dup( get_file_unix_fd( file ) );
1369 release_object( file );
1370 if (fd != -1)
1372 FILE *f = fdopen( fd, "r" );
1373 if (f)
1375 load_keys( key, NULL, f, -1 );
1376 fclose( f );
1378 else file_set_error();
1382 /* load one of the initial registry files */
1383 static void load_init_registry_from_file( const char *filename, struct key *key )
1385 FILE *f;
1387 if ((f = fopen( filename, "r" )))
1389 load_keys( key, filename, f, 0 );
1390 fclose( f );
1391 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1393 fprintf( stderr, "%s is not a valid registry file\n", filename );
1394 return;
1398 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1400 save_branch_info[save_branch_count].path = filename;
1401 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1402 make_object_static( &key->obj );
1405 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1407 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1408 static const WCHAR formatW[] = {'-','%','u',0};
1409 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1410 WCHAR *p = buffer;
1411 unsigned int i;
1413 strcpyW( p, prefixW );
1414 p += strlenW( prefixW );
1415 p += sprintfW( p, formatW, sid->Revision );
1416 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1417 sid->IdentifierAuthority.Value[4] ),
1418 MAKEWORD( sid->IdentifierAuthority.Value[3],
1419 sid->IdentifierAuthority.Value[2] )));
1420 for (i = 0; i < sid->SubAuthorityCount; i++)
1421 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1423 path->len = (p - buffer) * sizeof(WCHAR);
1424 path->str = p = memdup( buffer, path->len );
1425 return p;
1428 /* registry initialisation */
1429 void init_registry(void)
1431 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1432 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1433 static const struct unicode_str root_name = { NULL, 0 };
1434 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1435 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1437 WCHAR *current_user_path;
1438 struct unicode_str current_user_str;
1440 struct key *key;
1441 int dummy;
1443 /* switch to the config dir */
1445 if (fchdir( config_dir_fd ) == -1) fatal_perror( "chdir to config dir" );
1447 /* create the root key */
1448 root_key = alloc_key( &root_name, current_time );
1449 assert( root_key );
1450 make_object_static( &root_key->obj );
1452 /* load system.reg into Registry\Machine */
1454 if (!(key = create_key( root_key, &HKLM_name, NULL, 0, current_time, &dummy )))
1455 fatal_error( "could not create Machine registry key\n" );
1457 load_init_registry_from_file( "system.reg", key );
1458 release_object( key );
1460 /* load userdef.reg into Registry\User\.Default */
1462 if (!(key = create_key( root_key, &HKU_name, NULL, 0, current_time, &dummy )))
1463 fatal_error( "could not create User\\.Default registry key\n" );
1465 load_init_registry_from_file( "userdef.reg", key );
1466 release_object( key );
1468 /* load user.reg into HKEY_CURRENT_USER */
1470 /* FIXME: match default user in token.c. should get from process token instead */
1471 current_user_path = format_user_registry_path( security_interactive_sid, &current_user_str );
1472 if (!current_user_path ||
1473 !(key = create_key( root_key, &current_user_str, NULL, 0, current_time, &dummy )))
1474 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1475 free( current_user_path );
1476 load_init_registry_from_file( "user.reg", key );
1477 release_object( key );
1479 /* start the periodic save timer */
1480 set_periodic_save_timer();
1482 /* go back to the server dir */
1483 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1486 /* save a registry branch to a file */
1487 static void save_all_subkeys( struct key *key, FILE *f )
1489 fprintf( f, "WINE REGISTRY Version 2\n" );
1490 fprintf( f, ";; All keys relative to " );
1491 dump_path( key, NULL, f );
1492 fprintf( f, "\n" );
1493 save_subkeys( key, key, f );
1496 /* save a registry branch to a file handle */
1497 static void save_registry( struct key *key, obj_handle_t handle )
1499 struct file *file;
1500 int fd;
1502 if (key->flags & KEY_DELETED)
1504 set_error( STATUS_KEY_DELETED );
1505 return;
1507 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1508 fd = dup( get_file_unix_fd( file ) );
1509 release_object( file );
1510 if (fd != -1)
1512 FILE *f = fdopen( fd, "w" );
1513 if (f)
1515 save_all_subkeys( key, f );
1516 if (fclose( f )) file_set_error();
1518 else
1520 file_set_error();
1521 close( fd );
1526 /* save a registry branch to a file */
1527 static int save_branch( struct key *key, const char *path )
1529 struct stat st;
1530 char *p, *tmp = NULL;
1531 int fd, count = 0, ret = 0;
1532 FILE *f;
1534 if (!(key->flags & KEY_DIRTY))
1536 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1537 return 1;
1540 /* test the file type */
1542 if ((fd = open( path, O_WRONLY )) != -1)
1544 /* if file is not a regular file or has multiple links or is accessed
1545 * via symbolic links, write directly into it; otherwise use a temp file */
1546 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1548 ftruncate( fd, 0 );
1549 goto save;
1551 close( fd );
1554 /* create a temp file in the same directory */
1556 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1557 strcpy( tmp, path );
1558 if ((p = strrchr( tmp, '/' ))) p++;
1559 else p = tmp;
1560 for (;;)
1562 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1563 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1564 if (errno != EEXIST) goto done;
1565 close( fd );
1568 /* now save to it */
1570 save:
1571 if (!(f = fdopen( fd, "w" )))
1573 if (tmp) unlink( tmp );
1574 close( fd );
1575 goto done;
1578 if (debug_level > 1)
1580 fprintf( stderr, "%s: ", path );
1581 dump_operation( key, NULL, "saving" );
1584 save_all_subkeys( key, f );
1585 ret = !fclose(f);
1587 if (tmp)
1589 /* if successfully written, rename to final name */
1590 if (ret) ret = !rename( tmp, path );
1591 if (!ret) unlink( tmp );
1594 done:
1595 free( tmp );
1596 if (ret) make_clean( key );
1597 return ret;
1600 /* periodic saving of the registry */
1601 static void periodic_save( void *arg )
1603 int i;
1605 if (fchdir( config_dir_fd ) == -1) return;
1606 save_timeout_user = NULL;
1607 for (i = 0; i < save_branch_count; i++)
1608 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1609 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1610 set_periodic_save_timer();
1613 /* start the periodic save timer */
1614 static void set_periodic_save_timer(void)
1616 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1617 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
1620 /* save the modified registry branches to disk */
1621 void flush_registry(void)
1623 int i;
1625 if (fchdir( config_dir_fd ) == -1) return;
1626 for (i = 0; i < save_branch_count; i++)
1628 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1630 fprintf( stderr, "wineserver: could not save registry branch to %s",
1631 save_branch_info[i].path );
1632 perror( " " );
1635 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1639 /* create a registry key */
1640 DECL_HANDLER(create_key)
1642 struct key *key = NULL, *parent;
1643 struct unicode_str name, class;
1644 unsigned int access = req->access;
1646 reply->hkey = 0;
1648 if (req->namelen > get_req_data_size())
1650 set_error( STATUS_INVALID_PARAMETER );
1651 return;
1653 class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1654 class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1655 get_req_path( &name, !req->parent );
1656 if (name.str > class.str)
1658 set_error( STATUS_INVALID_PARAMETER );
1659 return;
1661 name.len = (class.str - name.str) * sizeof(WCHAR);
1663 /* NOTE: no access rights are required from the parent handle to create a key */
1664 if ((parent = get_parent_hkey_obj( req->parent )))
1666 int flags = (req->options & REG_OPTION_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1668 if ((key = create_key( parent, &name, &class, flags, current_time, &reply->created )))
1670 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1671 release_object( key );
1673 release_object( parent );
1677 /* open a registry key */
1678 DECL_HANDLER(open_key)
1680 struct key *key, *parent;
1681 struct unicode_str name;
1682 unsigned int access = req->access;
1684 reply->hkey = 0;
1685 /* NOTE: no access rights are required to open the parent key, only the child key */
1686 if ((parent = get_parent_hkey_obj( req->parent )))
1688 get_req_path( &name, !req->parent );
1689 if ((key = open_key( parent, &name )))
1691 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1692 release_object( key );
1694 release_object( parent );
1698 /* delete a registry key */
1699 DECL_HANDLER(delete_key)
1701 struct key *key;
1703 if ((key = get_hkey_obj( req->hkey, DELETE )))
1705 delete_key( key, 0);
1706 release_object( key );
1710 /* flush a registry key */
1711 DECL_HANDLER(flush_key)
1713 struct key *key = get_hkey_obj( req->hkey, 0 );
1714 if (key)
1716 /* we don't need to do anything here with the current implementation */
1717 release_object( key );
1721 /* enumerate registry subkeys */
1722 DECL_HANDLER(enum_key)
1724 struct key *key;
1726 if ((key = get_hkey_obj( req->hkey,
1727 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1729 enum_key( key, req->index, req->info_class, reply );
1730 release_object( key );
1734 /* set a value of a registry key */
1735 DECL_HANDLER(set_key_value)
1737 struct key *key;
1738 struct unicode_str name;
1740 if (req->namelen > get_req_data_size())
1742 set_error( STATUS_INVALID_PARAMETER );
1743 return;
1745 name.str = get_req_data();
1746 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
1748 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1750 data_size_t datalen = get_req_data_size() - req->namelen;
1751 const char *data = (const char *)get_req_data() + req->namelen;
1753 set_value( key, &name, req->type, data, datalen );
1754 release_object( key );
1758 /* retrieve the value of a registry key */
1759 DECL_HANDLER(get_key_value)
1761 struct key *key;
1762 struct unicode_str name;
1764 reply->total = 0;
1765 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1767 get_req_unicode_str( &name );
1768 get_value( key, &name, &reply->type, &reply->total );
1769 release_object( key );
1773 /* enumerate the value of a registry key */
1774 DECL_HANDLER(enum_key_value)
1776 struct key *key;
1778 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1780 enum_value( key, req->index, req->info_class, reply );
1781 release_object( key );
1785 /* delete a value of a registry key */
1786 DECL_HANDLER(delete_key_value)
1788 struct key *key;
1789 struct unicode_str name;
1791 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1793 get_req_unicode_str( &name );
1794 delete_value( key, &name );
1795 release_object( key );
1799 /* load a registry branch from a file */
1800 DECL_HANDLER(load_registry)
1802 struct key *key, *parent;
1803 struct token *token = thread_get_impersonation_token( current );
1804 struct unicode_str name;
1806 const LUID_AND_ATTRIBUTES privs[] =
1808 { SeBackupPrivilege, 0 },
1809 { SeRestorePrivilege, 0 },
1812 if (!token || !token_check_privileges( token, TRUE, privs,
1813 sizeof(privs)/sizeof(privs[0]), NULL ))
1815 set_error( STATUS_PRIVILEGE_NOT_HELD );
1816 return;
1819 if ((parent = get_parent_hkey_obj( req->hkey )))
1821 int dummy;
1822 get_req_path( &name, !req->hkey );
1823 if ((key = create_key( parent, &name, NULL, KEY_DIRTY, current_time, &dummy )))
1825 load_registry( key, req->file );
1826 release_object( key );
1828 release_object( parent );
1832 DECL_HANDLER(unload_registry)
1834 struct key *key;
1835 struct token *token = thread_get_impersonation_token( current );
1837 const LUID_AND_ATTRIBUTES privs[] =
1839 { SeBackupPrivilege, 0 },
1840 { SeRestorePrivilege, 0 },
1843 if (!token || !token_check_privileges( token, TRUE, privs,
1844 sizeof(privs)/sizeof(privs[0]), NULL ))
1846 set_error( STATUS_PRIVILEGE_NOT_HELD );
1847 return;
1850 if ((key = get_hkey_obj( req->hkey, 0 )))
1852 delete_key( key, 1 ); /* FIXME */
1853 release_object( key );
1857 /* save a registry branch to a file */
1858 DECL_HANDLER(save_registry)
1860 struct key *key;
1862 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
1864 set_error( STATUS_PRIVILEGE_NOT_HELD );
1865 return;
1868 if ((key = get_hkey_obj( req->hkey, 0 )))
1870 save_registry( key, req->file );
1871 release_object( key );
1875 /* add a registry key change notification */
1876 DECL_HANDLER(set_registry_notification)
1878 struct key *key;
1879 struct event *event;
1880 struct notify *notify;
1882 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
1883 if (key)
1885 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
1886 if (event)
1888 notify = find_notify( key, current->process, req->hkey );
1889 if (notify)
1891 if (notify->event)
1892 release_object( notify->event );
1893 grab_object( event );
1894 notify->event = event;
1896 else
1898 notify = mem_alloc( sizeof(*notify) );
1899 if (notify)
1901 grab_object( event );
1902 notify->event = event;
1903 notify->subtree = req->subtree;
1904 notify->filter = req->filter;
1905 notify->hkey = req->hkey;
1906 notify->process = current->process;
1907 list_add_head( &key->notify_list, &notify->entry );
1910 release_object( event );
1912 release_object( key );