Handle the ^ character in MaskEdit controls.
[wine/dcerpc.git] / server / registry.c
blob0a0ca23ad33356acf0d6ad796c5926a809b4f131
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 "object.h"
41 #include "file.h"
42 #include "handle.h"
43 #include "request.h"
44 #include "unicode.h"
45 #include "security.h"
47 #include "winternl.h"
48 #include "wine/library.h"
50 struct notify
52 struct list entry; /* entry in list of notifications */
53 struct event *event; /* event to set when changing this key */
54 int subtree; /* true if subtree notification */
55 unsigned int filter; /* which events to notify on */
56 obj_handle_t hkey; /* hkey associated with this notification */
57 struct process *process; /* process in which the hkey is valid */
60 /* a registry key */
61 struct key
63 struct object obj; /* object header */
64 WCHAR *name; /* key name */
65 WCHAR *class; /* key class */
66 unsigned short namelen; /* length of key name */
67 unsigned short classlen; /* length of class name */
68 struct key *parent; /* parent key */
69 int last_subkey; /* last in use subkey */
70 int nb_subkeys; /* count of allocated subkeys */
71 struct key **subkeys; /* subkeys array */
72 int last_value; /* last in use value */
73 int nb_values; /* count of allocated values in array */
74 struct key_value *values; /* values array */
75 unsigned int flags; /* flags */
76 time_t modif; /* last modification time */
77 struct list notify_list; /* list of notifications */
80 /* key flags */
81 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
82 #define KEY_DELETED 0x0002 /* key has been deleted */
83 #define KEY_DIRTY 0x0004 /* key has been modified */
85 /* a key value */
86 struct key_value
88 WCHAR *name; /* value name */
89 unsigned short namelen; /* length of value name */
90 unsigned short type; /* value type */
91 size_t len; /* value data length in bytes */
92 void *data; /* pointer to value data */
95 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
96 #define MIN_VALUES 8 /* min. number of allocated values per key */
98 #define MAX_NAME_LEN MAX_PATH /* max. length of a key name */
99 #define MAX_VALUE_LEN MAX_PATH /* max. length of a value name */
101 /* the root of the registry tree */
102 static struct key *root_key;
104 static const int save_period = 30000; /* delay between periodic saves (in ms) */
105 static struct timeout_user *save_timeout_user; /* saving timer */
107 static void set_periodic_save_timer(void);
109 /* information about where to save a registry branch */
110 struct save_branch_info
112 struct key *key;
113 char *path;
116 #define MAX_SAVE_BRANCH_INFO 3
117 static int save_branch_count;
118 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
121 /* information about a file being loaded */
122 struct file_load_info
124 const char *filename; /* input file name */
125 FILE *file; /* input file */
126 char *buffer; /* line buffer */
127 int len; /* buffer length */
128 int line; /* current input line */
129 WCHAR *tmp; /* temp buffer to use while parsing input */
130 size_t tmplen; /* length of temp buffer */
134 static void key_dump( struct object *obj, int verbose );
135 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
136 static void key_destroy( struct object *obj );
138 static const struct object_ops key_ops =
140 sizeof(struct key), /* size */
141 key_dump, /* dump */
142 no_add_queue, /* add_queue */
143 NULL, /* remove_queue */
144 NULL, /* signaled */
145 NULL, /* satisfied */
146 no_signal, /* signal */
147 no_get_fd, /* get_fd */
148 no_lookup_name, /* lookup_name */
149 key_close_handle, /* close_handle */
150 key_destroy /* destroy */
155 * The registry text file format v2 used by this code is similar to the one
156 * used by REGEDIT import/export functionality, with the following differences:
157 * - strings and key names can contain \x escapes for Unicode
158 * - key names use escapes too in order to support Unicode
159 * - the modification time optionally follows the key name
160 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
163 static inline char to_hex( char ch )
165 if (isdigit(ch)) return ch - '0';
166 return tolower(ch) - 'a' + 10;
169 /* dump the full path of a key */
170 static void dump_path( const struct key *key, const struct key *base, FILE *f )
172 if (key->parent && key->parent != base)
174 dump_path( key->parent, base, f );
175 fprintf( f, "\\\\" );
177 dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
180 /* dump a value to a text file */
181 static void dump_value( const struct key_value *value, FILE *f )
183 unsigned int i;
184 int count;
186 if (value->namelen)
188 fputc( '\"', f );
189 count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
190 count += fprintf( f, "\"=" );
192 else count = fprintf( f, "@=" );
194 switch(value->type)
196 case REG_SZ:
197 case REG_EXPAND_SZ:
198 case REG_MULTI_SZ:
199 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
200 fputc( '\"', f );
201 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
202 fputc( '\"', f );
203 break;
204 case REG_DWORD:
205 if (value->len == sizeof(DWORD))
207 DWORD dw;
208 memcpy( &dw, value->data, sizeof(DWORD) );
209 fprintf( f, "dword:%08lx", dw );
210 break;
212 /* else fall through */
213 default:
214 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
215 else count += fprintf( f, "hex(%x):", value->type );
216 for (i = 0; i < value->len; i++)
218 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
219 if (i < value->len-1)
221 fputc( ',', f );
222 if (++count > 76)
224 fprintf( f, "\\\n " );
225 count = 2;
229 break;
231 fputc( '\n', f );
234 /* save a registry and all its subkeys to a text file */
235 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
237 int i;
239 if (key->flags & KEY_VOLATILE) return;
240 /* save key if it has either some values or no subkeys */
241 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
242 if ((key->last_value >= 0) || (key->last_subkey == -1))
244 fprintf( f, "\n[" );
245 if (key != base) dump_path( key, base, f );
246 fprintf( f, "] %ld\n", (long)key->modif );
247 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
249 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
252 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
254 fprintf( stderr, "%s key ", op );
255 if (key) dump_path( key, NULL, stderr );
256 else fprintf( stderr, "ERROR" );
257 if (value)
259 fprintf( stderr, " value ");
260 dump_value( value, stderr );
262 else fprintf( stderr, "\n" );
265 static void key_dump( struct object *obj, int verbose )
267 struct key *key = (struct key *)obj;
268 assert( obj->ops == &key_ops );
269 fprintf( stderr, "Key flags=%x ", key->flags );
270 dump_path( key, NULL, stderr );
271 fprintf( stderr, "\n" );
274 /* notify waiter and maybe delete the notification */
275 static void do_notification( struct key *key, struct notify *notify, int del )
277 if (notify->event)
279 set_event( notify->event );
280 release_object( notify->event );
281 notify->event = NULL;
283 if (del)
285 list_remove( &notify->entry );
286 free( notify );
290 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
292 struct notify *notify;
294 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
296 if (notify->process == process && notify->hkey == hkey) return notify;
298 return NULL;
301 /* close the notification associated with a handle */
302 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
304 struct key * key = (struct key *) obj;
305 struct notify *notify = find_notify( key, process, handle );
306 if (notify) do_notification( key, notify, 1 );
307 return 1; /* ok to close */
310 static void key_destroy( struct object *obj )
312 int i;
313 struct list *ptr;
314 struct key *key = (struct key *)obj;
315 assert( obj->ops == &key_ops );
317 if (key->name) free( key->name );
318 if (key->class) free( key->class );
319 for (i = 0; i <= key->last_value; i++)
321 if (key->values[i].name) free( key->values[i].name );
322 if (key->values[i].data) free( key->values[i].data );
324 if (key->values) free( key->values );
325 for (i = 0; i <= key->last_subkey; i++)
327 key->subkeys[i]->parent = NULL;
328 release_object( key->subkeys[i] );
330 if (key->subkeys) free( key->subkeys );
331 /* unconditionally notify everything waiting on this key */
332 while ((ptr = list_head( &key->notify_list )))
334 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
335 do_notification( key, notify, 1 );
339 /* get the request vararg as registry path */
340 inline static void get_req_path( struct unicode_str *str, int skip_root )
342 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
344 str->str = get_req_data();
345 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
347 if (skip_root && str->len >= sizeof(root_name) &&
348 !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
350 str->str += sizeof(root_name)/sizeof(WCHAR);
351 str->len -= sizeof(root_name);
355 /* return the next token in a given path */
356 /* token->str must point inside the path, or be NULL for the first call */
357 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
359 size_t i = 0, len = path->len / sizeof(WCHAR);
361 if (!token->str) /* first time */
363 /* path cannot start with a backslash */
364 if (len && path->str[0] == '\\')
366 set_error( STATUS_OBJECT_PATH_INVALID );
367 return NULL;
370 else
372 i = token->str - path->str;
373 i += token->len / sizeof(WCHAR);
374 while (i < len && path->str[i] == '\\') i++;
376 token->str = path->str + i;
377 while (i < len && path->str[i] != '\\') i++;
378 token->len = (path->str + i - token->str) * sizeof(WCHAR);
379 return token;
382 /* allocate a key object */
383 static struct key *alloc_key( const struct unicode_str *name, time_t modif )
385 struct key *key;
386 if ((key = alloc_object( &key_ops )))
388 key->name = NULL;
389 key->class = NULL;
390 key->namelen = name->len;
391 key->classlen = 0;
392 key->flags = 0;
393 key->last_subkey = -1;
394 key->nb_subkeys = 0;
395 key->subkeys = NULL;
396 key->nb_values = 0;
397 key->last_value = -1;
398 key->values = NULL;
399 key->modif = modif;
400 key->parent = NULL;
401 list_init( &key->notify_list );
402 if (name->len && !(key->name = memdup( name->str, name->len )))
404 release_object( key );
405 key = NULL;
408 return key;
411 /* mark a key and all its parents as dirty (modified) */
412 static void make_dirty( struct key *key )
414 while (key)
416 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
417 key->flags |= KEY_DIRTY;
418 key = key->parent;
422 /* mark a key and all its subkeys as clean (not modified) */
423 static void make_clean( struct key *key )
425 int i;
427 if (key->flags & KEY_VOLATILE) return;
428 if (!(key->flags & KEY_DIRTY)) return;
429 key->flags &= ~KEY_DIRTY;
430 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
433 /* go through all the notifications and send them if necessary */
434 static void check_notify( struct key *key, unsigned int change, int not_subtree )
436 struct list *ptr, *next;
438 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
440 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
441 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
442 do_notification( key, n, 0 );
446 /* update key modification time */
447 static void touch_key( struct key *key, unsigned int change )
449 struct key *k;
451 key->modif = time(NULL);
452 make_dirty( key );
454 /* do notifications */
455 check_notify( key, change, 1 );
456 for ( k = key->parent; k; k = k->parent )
457 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
460 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
461 static int grow_subkeys( struct key *key )
463 struct key **new_subkeys;
464 int nb_subkeys;
466 if (key->nb_subkeys)
468 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
469 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
471 set_error( STATUS_NO_MEMORY );
472 return 0;
475 else
477 nb_subkeys = MIN_VALUES;
478 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
480 key->subkeys = new_subkeys;
481 key->nb_subkeys = nb_subkeys;
482 return 1;
485 /* allocate a subkey for a given key, and return its index */
486 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
487 int index, time_t modif )
489 struct key *key;
490 int i;
492 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
494 set_error( STATUS_NAME_TOO_LONG );
495 return NULL;
497 if (parent->last_subkey + 1 == parent->nb_subkeys)
499 /* need to grow the array */
500 if (!grow_subkeys( parent )) return NULL;
502 if ((key = alloc_key( name, modif )) != NULL)
504 key->parent = parent;
505 for (i = ++parent->last_subkey; i > index; i--)
506 parent->subkeys[i] = parent->subkeys[i-1];
507 parent->subkeys[index] = key;
509 return key;
512 /* free a subkey of a given key */
513 static void free_subkey( struct key *parent, int index )
515 struct key *key;
516 int i, nb_subkeys;
518 assert( index >= 0 );
519 assert( index <= parent->last_subkey );
521 key = parent->subkeys[index];
522 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
523 parent->last_subkey--;
524 key->flags |= KEY_DELETED;
525 key->parent = NULL;
526 release_object( key );
528 /* try to shrink the array */
529 nb_subkeys = parent->nb_subkeys;
530 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
532 struct key **new_subkeys;
533 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
534 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
535 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
536 parent->subkeys = new_subkeys;
537 parent->nb_subkeys = nb_subkeys;
541 /* find the named child of a given key and return its index */
542 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
544 int i, min, max, res;
545 size_t len;
547 min = 0;
548 max = key->last_subkey;
549 while (min <= max)
551 i = (min + max) / 2;
552 len = min( key->subkeys[i]->namelen, name->len );
553 res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
554 if (!res) res = key->subkeys[i]->namelen - name->len;
555 if (!res)
557 *index = i;
558 return key->subkeys[i];
560 if (res > 0) max = i - 1;
561 else min = i + 1;
563 *index = min; /* this is where we should insert it */
564 return NULL;
567 /* open a subkey */
568 static struct key *open_key( struct key *key, const struct unicode_str *name )
570 int index;
571 struct unicode_str token;
573 token.str = NULL;
574 if (!get_path_token( name, &token )) return NULL;
575 while (token.len)
577 if (!(key = find_subkey( key, &token, &index )))
579 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
580 break;
582 get_path_token( name, &token );
585 if (debug_level > 1) dump_operation( key, NULL, "Open" );
586 if (key) grab_object( key );
587 return key;
590 /* create a subkey */
591 static struct key *create_key( struct key *key, const struct unicode_str *name,
592 const struct unicode_str *class, int flags, time_t modif, int *created )
594 struct key *base;
595 int index;
596 struct unicode_str token;
598 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
600 set_error( STATUS_KEY_DELETED );
601 return NULL;
603 if (!(flags & KEY_VOLATILE) && (key->flags & KEY_VOLATILE))
605 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
606 return NULL;
608 if (!modif) modif = time(NULL);
610 token.str = NULL;
611 if (!get_path_token( name, &token )) return NULL;
612 *created = 0;
613 while (token.len)
615 struct key *subkey;
616 if (!(subkey = find_subkey( key, &token, &index ))) break;
617 key = subkey;
618 get_path_token( name, &token );
621 /* create the remaining part */
623 if (!token.len) goto done;
624 *created = 1;
625 if (flags & KEY_DIRTY) make_dirty( key );
626 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
627 base = key;
628 for (;;)
630 key->flags |= flags;
631 get_path_token( name, &token );
632 if (!token.len) break;
633 /* we know the index is always 0 in a new key */
634 if (!(key = alloc_subkey( key, &token, 0, modif )))
636 free_subkey( base, index );
637 return NULL;
641 done:
642 if (debug_level > 1) dump_operation( key, NULL, "Create" );
643 if (class && class->len)
645 key->classlen = class->len;
646 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
648 grab_object( key );
649 return key;
652 /* query information about a key or a subkey */
653 static void enum_key( const struct key *key, int index, int info_class,
654 struct enum_key_reply *reply )
656 int i;
657 size_t len, namelen, classlen;
658 int max_subkey = 0, max_class = 0;
659 int max_value = 0, max_data = 0;
660 char *data;
662 if (index != -1) /* -1 means use the specified key directly */
664 if ((index < 0) || (index > key->last_subkey))
666 set_error( STATUS_NO_MORE_ENTRIES );
667 return;
669 key = key->subkeys[index];
672 namelen = key->namelen;
673 classlen = key->classlen;
675 switch(info_class)
677 case KeyBasicInformation:
678 classlen = 0; /* only return the name */
679 /* fall through */
680 case KeyNodeInformation:
681 reply->max_subkey = 0;
682 reply->max_class = 0;
683 reply->max_value = 0;
684 reply->max_data = 0;
685 break;
686 case KeyFullInformation:
687 for (i = 0; i <= key->last_subkey; i++)
689 struct key *subkey = key->subkeys[i];
690 len = subkey->namelen / sizeof(WCHAR);
691 if (len > max_subkey) max_subkey = len;
692 len = subkey->classlen / sizeof(WCHAR);
693 if (len > max_class) max_class = len;
695 for (i = 0; i <= key->last_value; i++)
697 len = key->values[i].namelen / sizeof(WCHAR);
698 if (len > max_value) max_value = len;
699 len = key->values[i].len;
700 if (len > max_data) max_data = len;
702 reply->max_subkey = max_subkey;
703 reply->max_class = max_class;
704 reply->max_value = max_value;
705 reply->max_data = max_data;
706 namelen = 0; /* only return the class */
707 break;
708 default:
709 set_error( STATUS_INVALID_PARAMETER );
710 return;
712 reply->subkeys = key->last_subkey + 1;
713 reply->values = key->last_value + 1;
714 reply->modif = key->modif;
715 reply->total = namelen + classlen;
717 len = min( reply->total, get_reply_max_size() );
718 if (len && (data = set_reply_data_size( len )))
720 if (len > namelen)
722 reply->namelen = namelen;
723 memcpy( data, key->name, namelen );
724 memcpy( data + namelen, key->class, len - namelen );
726 else
728 reply->namelen = len;
729 memcpy( data, key->name, len );
732 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
735 /* delete a key and its values */
736 static int delete_key( struct key *key, int recurse )
738 int index;
739 struct key *parent;
741 /* must find parent and index */
742 if (key == root_key)
744 set_error( STATUS_ACCESS_DENIED );
745 return -1;
747 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
749 set_error( STATUS_KEY_DELETED );
750 return -1;
753 while (recurse && (key->last_subkey>=0))
754 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
755 return -1;
757 for (index = 0; index <= parent->last_subkey; index++)
758 if (parent->subkeys[index] == key) break;
759 assert( index <= parent->last_subkey );
761 /* we can only delete a key that has no subkeys */
762 if (key->last_subkey >= 0)
764 set_error( STATUS_ACCESS_DENIED );
765 return -1;
768 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
769 free_subkey( parent, index );
770 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
771 return 0;
774 /* try to grow the array of values; return 1 if OK, 0 on error */
775 static int grow_values( struct key *key )
777 struct key_value *new_val;
778 int nb_values;
780 if (key->nb_values)
782 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
783 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
785 set_error( STATUS_NO_MEMORY );
786 return 0;
789 else
791 nb_values = MIN_VALUES;
792 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
794 key->values = new_val;
795 key->nb_values = nb_values;
796 return 1;
799 /* find the named value of a given key and return its index in the array */
800 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
802 int i, min, max, res;
803 size_t len;
805 min = 0;
806 max = key->last_value;
807 while (min <= max)
809 i = (min + max) / 2;
810 len = min( key->values[i].namelen, name->len );
811 res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
812 if (!res) res = key->values[i].namelen - name->len;
813 if (!res)
815 *index = i;
816 return &key->values[i];
818 if (res > 0) max = i - 1;
819 else min = i + 1;
821 *index = min; /* this is where we should insert it */
822 return NULL;
825 /* insert a new value; the index must have been returned by find_value */
826 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
828 struct key_value *value;
829 WCHAR *new_name = NULL;
830 int i;
832 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
834 set_error( STATUS_NAME_TOO_LONG );
835 return NULL;
837 if (key->last_value + 1 == key->nb_values)
839 if (!grow_values( key )) return NULL;
841 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
842 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
843 value = &key->values[index];
844 value->name = new_name;
845 value->namelen = name->len;
846 value->len = 0;
847 value->data = NULL;
848 return value;
851 /* set a key value */
852 static void set_value( struct key *key, const struct unicode_str *name,
853 int type, const void *data, size_t len )
855 struct key_value *value;
856 void *ptr = NULL;
857 int index;
859 if ((value = find_value( key, name, &index )))
861 /* check if the new value is identical to the existing one */
862 if (value->type == type && value->len == len &&
863 value->data && !memcmp( value->data, data, len ))
865 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
866 return;
870 if (len && !(ptr = memdup( data, len ))) return;
872 if (!value)
874 if (!(value = insert_value( key, name, index )))
876 if (ptr) free( ptr );
877 return;
880 else if (value->data) free( value->data ); /* already existing, free previous data */
882 value->type = type;
883 value->len = len;
884 value->data = ptr;
885 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
886 if (debug_level > 1) dump_operation( key, value, "Set" );
889 /* get a key value */
890 static void get_value( struct key *key, const struct unicode_str *name, int *type, unsigned int *len )
892 struct key_value *value;
893 int index;
895 if ((value = find_value( key, name, &index )))
897 *type = value->type;
898 *len = value->len;
899 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
900 if (debug_level > 1) dump_operation( key, value, "Get" );
902 else
904 *type = -1;
905 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
909 /* enumerate a key value */
910 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
912 struct key_value *value;
914 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
915 else
917 void *data;
918 size_t namelen, maxlen;
920 value = &key->values[i];
921 reply->type = value->type;
922 namelen = value->namelen;
924 switch(info_class)
926 case KeyValueBasicInformation:
927 reply->total = namelen;
928 break;
929 case KeyValueFullInformation:
930 reply->total = namelen + value->len;
931 break;
932 case KeyValuePartialInformation:
933 reply->total = value->len;
934 namelen = 0;
935 break;
936 default:
937 set_error( STATUS_INVALID_PARAMETER );
938 return;
941 maxlen = min( reply->total, get_reply_max_size() );
942 if (maxlen && ((data = set_reply_data_size( maxlen ))))
944 if (maxlen > namelen)
946 reply->namelen = namelen;
947 memcpy( data, value->name, namelen );
948 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
950 else
952 reply->namelen = maxlen;
953 memcpy( data, value->name, maxlen );
956 if (debug_level > 1) dump_operation( key, value, "Enum" );
960 /* delete a value */
961 static void delete_value( struct key *key, const struct unicode_str *name )
963 struct key_value *value;
964 int i, index, nb_values;
966 if (!(value = find_value( key, name, &index )))
968 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
969 return;
971 if (debug_level > 1) dump_operation( key, value, "Delete" );
972 if (value->name) free( value->name );
973 if (value->data) free( value->data );
974 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
975 key->last_value--;
976 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
978 /* try to shrink the array */
979 nb_values = key->nb_values;
980 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
982 struct key_value *new_val;
983 nb_values -= nb_values / 3; /* shrink by 33% */
984 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
985 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
986 key->values = new_val;
987 key->nb_values = nb_values;
991 /* get the registry key corresponding to an hkey handle */
992 static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
994 if (!hkey) return (struct key *)grab_object( root_key );
995 return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
998 /* read a line from the input file */
999 static int read_next_line( struct file_load_info *info )
1001 char *newbuf;
1002 int newlen, pos = 0;
1004 info->line++;
1005 for (;;)
1007 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1008 return (pos != 0); /* EOF */
1009 pos = strlen(info->buffer);
1010 if (info->buffer[pos-1] == '\n')
1012 /* got a full line */
1013 info->buffer[--pos] = 0;
1014 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1015 return 1;
1017 if (pos < info->len - 1) return 1; /* EOF but something was read */
1019 /* need to enlarge the buffer */
1020 newlen = info->len + info->len / 2;
1021 if (!(newbuf = realloc( info->buffer, newlen )))
1023 set_error( STATUS_NO_MEMORY );
1024 return -1;
1026 info->buffer = newbuf;
1027 info->len = newlen;
1031 /* make sure the temp buffer holds enough space */
1032 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1034 WCHAR *tmp;
1035 if (info->tmplen >= size) return 1;
1036 if (!(tmp = realloc( info->tmp, size )))
1038 set_error( STATUS_NO_MEMORY );
1039 return 0;
1041 info->tmp = tmp;
1042 info->tmplen = size;
1043 return 1;
1046 /* report an error while loading an input file */
1047 static void file_read_error( const char *err, struct file_load_info *info )
1049 if (info->filename)
1050 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1051 else
1052 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1055 /* parse an escaped string back into Unicode */
1056 /* return the number of chars read from the input, or -1 on output overflow */
1057 static int parse_strW( WCHAR *dest, size_t *len, const char *src, char endchar )
1059 size_t count = sizeof(WCHAR); /* for terminating null */
1060 const char *p = src;
1061 while (*p && *p != endchar)
1063 if (*p != '\\') *dest = (WCHAR)*p++;
1064 else
1066 p++;
1067 switch(*p)
1069 case 'a': *dest = '\a'; p++; break;
1070 case 'b': *dest = '\b'; p++; break;
1071 case 'e': *dest = '\e'; p++; break;
1072 case 'f': *dest = '\f'; p++; break;
1073 case 'n': *dest = '\n'; p++; break;
1074 case 'r': *dest = '\r'; p++; break;
1075 case 't': *dest = '\t'; p++; break;
1076 case 'v': *dest = '\v'; p++; break;
1077 case 'x': /* hex escape */
1078 p++;
1079 if (!isxdigit(*p)) *dest = 'x';
1080 else
1082 *dest = to_hex(*p++);
1083 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1084 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1085 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1087 break;
1088 case '0':
1089 case '1':
1090 case '2':
1091 case '3':
1092 case '4':
1093 case '5':
1094 case '6':
1095 case '7': /* octal escape */
1096 *dest = *p++ - '0';
1097 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1098 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1099 break;
1100 default:
1101 *dest = (WCHAR)*p++;
1102 break;
1105 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1106 dest++;
1108 *dest = 0;
1109 if (!*p) return -1; /* delimiter not found */
1110 *len = count;
1111 return p + 1 - src;
1114 /* convert a data type tag to a value type */
1115 static int get_data_type( const char *buffer, int *type, int *parse_type )
1117 struct data_type { const char *tag; int len; int type; int parse_type; };
1119 static const struct data_type data_types[] =
1120 { /* actual type */ /* type to assume for parsing */
1121 { "\"", 1, REG_SZ, REG_SZ },
1122 { "str:\"", 5, REG_SZ, REG_SZ },
1123 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1124 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1125 { "hex:", 4, REG_BINARY, REG_BINARY },
1126 { "dword:", 6, REG_DWORD, REG_DWORD },
1127 { "hex(", 4, -1, REG_BINARY },
1128 { NULL, 0, 0, 0 }
1131 const struct data_type *ptr;
1132 char *end;
1134 for (ptr = data_types; ptr->tag; ptr++)
1136 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1137 *parse_type = ptr->parse_type;
1138 if ((*type = ptr->type) != -1) return ptr->len;
1139 /* "hex(xx):" is special */
1140 *type = (int)strtoul( buffer + 4, &end, 16 );
1141 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1142 return end + 2 - buffer;
1144 return 0;
1147 /* load and create a key from the input file */
1148 static struct key *load_key( struct key *base, const char *buffer, int flags,
1149 int prefix_len, struct file_load_info *info,
1150 int default_modif )
1152 WCHAR *p;
1153 struct unicode_str name;
1154 int res, modif;
1155 size_t len = strlen(buffer) * sizeof(WCHAR);
1157 if (!get_file_tmp_space( info, len )) return NULL;
1159 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1161 file_read_error( "Malformed key", info );
1162 return NULL;
1164 if (sscanf( buffer + res, " %d", &modif ) != 1) modif = default_modif;
1166 p = info->tmp;
1167 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1169 if (!*p)
1171 if (prefix_len > 1)
1173 file_read_error( "Malformed key", info );
1174 return NULL;
1176 /* empty key name, return base key */
1177 return (struct key *)grab_object( base );
1179 name.str = p;
1180 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1181 return create_key( base, &name, NULL, flags, modif, &res );
1184 /* parse a comma-separated list of hex digits */
1185 static int parse_hex( unsigned char *dest, size_t *len, const char *buffer )
1187 const char *p = buffer;
1188 size_t count = 0;
1189 while (isxdigit(*p))
1191 int val;
1192 char buf[3];
1193 memcpy( buf, p, 2 );
1194 buf[2] = 0;
1195 sscanf( buf, "%x", &val );
1196 if (count++ >= *len) return -1; /* dest buffer overflow */
1197 *dest++ = (unsigned char )val;
1198 p += 2;
1199 if (*p == ',') p++;
1201 *len = count;
1202 return p - buffer;
1205 /* parse a value name and create the corresponding value */
1206 static struct key_value *parse_value_name( struct key *key, const char *buffer, size_t *len,
1207 struct file_load_info *info )
1209 struct key_value *value;
1210 struct unicode_str name;
1211 int index;
1212 size_t maxlen = strlen(buffer) * sizeof(WCHAR);
1214 if (!get_file_tmp_space( info, maxlen )) return NULL;
1215 name.str = info->tmp;
1216 if (buffer[0] == '@')
1218 name.len = 0;
1219 *len = 1;
1221 else
1223 if ((*len = parse_strW( info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1224 name.len = maxlen - sizeof(WCHAR);
1225 (*len)++; /* for initial quote */
1227 while (isspace(buffer[*len])) (*len)++;
1228 if (buffer[*len] != '=') goto error;
1229 (*len)++;
1230 while (isspace(buffer[*len])) (*len)++;
1231 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1232 return value;
1234 error:
1235 file_read_error( "Malformed value name", info );
1236 return NULL;
1239 /* load a value from the input file */
1240 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1242 DWORD dw;
1243 void *ptr, *newptr;
1244 int res, type, parse_type;
1245 size_t maxlen, len;
1246 struct key_value *value;
1248 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1249 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1250 buffer += len + res;
1252 switch(parse_type)
1254 case REG_SZ:
1255 len = strlen(buffer) * sizeof(WCHAR);
1256 if (!get_file_tmp_space( info, len )) return 0;
1257 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1258 ptr = info->tmp;
1259 break;
1260 case REG_DWORD:
1261 dw = strtoul( buffer, NULL, 16 );
1262 ptr = &dw;
1263 len = sizeof(dw);
1264 break;
1265 case REG_BINARY: /* hex digits */
1266 len = 0;
1267 for (;;)
1269 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1270 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1271 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1272 len += maxlen;
1273 buffer += res;
1274 while (isspace(*buffer)) buffer++;
1275 if (!*buffer) break;
1276 if (*buffer != '\\') goto error;
1277 if (read_next_line( info) != 1) goto error;
1278 buffer = info->buffer;
1279 while (isspace(*buffer)) buffer++;
1281 ptr = info->tmp;
1282 break;
1283 default:
1284 assert(0);
1285 ptr = NULL; /* keep compiler quiet */
1286 break;
1289 if (!len) newptr = NULL;
1290 else if (!(newptr = memdup( ptr, len ))) return 0;
1292 if (value->data) free( value->data );
1293 value->data = newptr;
1294 value->len = len;
1295 value->type = type;
1296 make_dirty( key );
1297 return 1;
1299 error:
1300 file_read_error( "Malformed value", info );
1301 return 0;
1304 /* return the length (in path elements) of name that is part of the key name */
1305 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1306 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1308 WCHAR *p;
1309 int res;
1310 size_t len = strlen(name) * sizeof(WCHAR);
1312 if (!get_file_tmp_space( info, len )) return 0;
1314 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1316 file_read_error( "Malformed key", info );
1317 return 0;
1319 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1320 len = (p - info->tmp) * sizeof(WCHAR);
1321 for (res = 1; key != root_key; res++)
1323 if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
1324 key = key->parent;
1326 if (key == root_key) res = 0; /* no matching name */
1327 return res;
1330 /* load all the keys from the input file */
1331 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1332 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1334 struct key *subkey = NULL;
1335 struct file_load_info info;
1336 char *p;
1337 int default_modif = time(NULL);
1338 int flags = (key->flags & KEY_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1340 info.filename = filename;
1341 info.file = f;
1342 info.len = 4;
1343 info.tmplen = 4;
1344 info.line = 0;
1345 if (!(info.buffer = mem_alloc( info.len ))) return;
1346 if (!(info.tmp = mem_alloc( info.tmplen )))
1348 free( info.buffer );
1349 return;
1352 if ((read_next_line( &info ) != 1) ||
1353 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1355 set_error( STATUS_NOT_REGISTRY_FILE );
1356 goto done;
1359 while (read_next_line( &info ) == 1)
1361 p = info.buffer;
1362 while (*p && isspace(*p)) p++;
1363 switch(*p)
1365 case '[': /* new key */
1366 if (subkey) release_object( subkey );
1367 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1368 if (!(subkey = load_key( key, p + 1, flags, prefix_len, &info, default_modif )))
1369 file_read_error( "Error creating key", &info );
1370 break;
1371 case '@': /* default value */
1372 case '\"': /* value */
1373 if (subkey) load_value( subkey, p, &info );
1374 else file_read_error( "Value without key", &info );
1375 break;
1376 case '#': /* comment */
1377 case ';': /* comment */
1378 case 0: /* empty line */
1379 break;
1380 default:
1381 file_read_error( "Unrecognized input", &info );
1382 break;
1386 done:
1387 if (subkey) release_object( subkey );
1388 free( info.buffer );
1389 free( info.tmp );
1392 /* load a part of the registry from a file */
1393 static void load_registry( struct key *key, obj_handle_t handle )
1395 struct file *file;
1396 int fd;
1398 if (!(file = get_file_obj( current->process, handle, GENERIC_READ ))) return;
1399 fd = dup( get_file_unix_fd( file ) );
1400 release_object( file );
1401 if (fd != -1)
1403 FILE *f = fdopen( fd, "r" );
1404 if (f)
1406 load_keys( key, NULL, f, -1 );
1407 fclose( f );
1409 else file_set_error();
1413 /* load one of the initial registry files */
1414 static void load_init_registry_from_file( const char *filename, struct key *key )
1416 FILE *f;
1418 if ((f = fopen( filename, "r" )))
1420 load_keys( key, filename, f, 0 );
1421 fclose( f );
1422 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1424 fprintf( stderr, "%s is not a valid registry file\n", filename );
1425 return;
1429 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1431 if ((save_branch_info[save_branch_count].path = strdup( filename )))
1432 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1435 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1437 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1438 static const WCHAR formatW[] = {'-','%','u',0};
1439 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1440 WCHAR *p = buffer;
1441 unsigned int i;
1443 strcpyW( p, prefixW );
1444 p += strlenW( prefixW );
1445 p += sprintfW( p, formatW, sid->Revision );
1446 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1447 sid->IdentifierAuthority.Value[4] ),
1448 MAKEWORD( sid->IdentifierAuthority.Value[3],
1449 sid->IdentifierAuthority.Value[2] )));
1450 for (i = 0; i < sid->SubAuthorityCount; i++)
1451 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1453 path->len = (p - buffer) * sizeof(WCHAR);
1454 path->str = p = memdup( buffer, path->len );
1455 return p;
1458 /* registry initialisation */
1459 void init_registry(void)
1461 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1462 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1463 static const struct unicode_str root_name = { NULL, 0 };
1464 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1465 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1467 WCHAR *current_user_path;
1468 struct unicode_str current_user_str;
1470 const char *config = wine_get_config_dir();
1471 char *p, *filename;
1472 struct key *key;
1473 int dummy;
1475 /* create the root key */
1476 root_key = alloc_key( &root_name, time(NULL) );
1477 assert( root_key );
1479 if (!(filename = malloc( strlen(config) + 16 ))) fatal_error( "out of memory\n" );
1480 strcpy( filename, config );
1481 p = filename + strlen(filename);
1483 /* load system.reg into Registry\Machine */
1485 if (!(key = create_key( root_key, &HKLM_name, NULL, 0, time(NULL), &dummy )))
1486 fatal_error( "could not create Machine registry key\n" );
1488 strcpy( p, "/system.reg" );
1489 load_init_registry_from_file( filename, key );
1490 release_object( key );
1492 /* load userdef.reg into Registry\User\.Default */
1494 if (!(key = create_key( root_key, &HKU_name, NULL, 0, time(NULL), &dummy )))
1495 fatal_error( "could not create User\\.Default registry key\n" );
1497 strcpy( p, "/userdef.reg" );
1498 load_init_registry_from_file( filename, key );
1499 release_object( key );
1501 /* load user.reg into HKEY_CURRENT_USER */
1503 /* FIXME: match default user in token.c. should get from process token instead */
1504 current_user_path = format_user_registry_path( security_interactive_sid, &current_user_str );
1505 if (!current_user_path ||
1506 !(key = create_key( root_key, &current_user_str, NULL, 0, time(NULL), &dummy )))
1507 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1508 free( current_user_path );
1509 strcpy( p, "/user.reg" );
1510 load_init_registry_from_file( filename, key );
1511 release_object( key );
1513 free( filename );
1515 /* start the periodic save timer */
1516 set_periodic_save_timer();
1519 /* save a registry branch to a file */
1520 static void save_all_subkeys( struct key *key, FILE *f )
1522 fprintf( f, "WINE REGISTRY Version 2\n" );
1523 fprintf( f, ";; All keys relative to " );
1524 dump_path( key, NULL, f );
1525 fprintf( f, "\n" );
1526 save_subkeys( key, key, f );
1529 /* save a registry branch to a file handle */
1530 static void save_registry( struct key *key, obj_handle_t handle )
1532 struct file *file;
1533 int fd;
1535 if (key->flags & KEY_DELETED)
1537 set_error( STATUS_KEY_DELETED );
1538 return;
1540 if (!(file = get_file_obj( current->process, handle, GENERIC_WRITE ))) return;
1541 fd = dup( get_file_unix_fd( file ) );
1542 release_object( file );
1543 if (fd != -1)
1545 FILE *f = fdopen( fd, "w" );
1546 if (f)
1548 save_all_subkeys( key, f );
1549 if (fclose( f )) file_set_error();
1551 else
1553 file_set_error();
1554 close( fd );
1559 /* save a registry branch to a file */
1560 static int save_branch( struct key *key, const char *path )
1562 struct stat st;
1563 char *p, *real, *tmp = NULL;
1564 int fd, count = 0, ret = 0, by_symlink;
1565 FILE *f;
1567 if (!(key->flags & KEY_DIRTY))
1569 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1570 return 1;
1573 /* get the real path */
1575 by_symlink = (!lstat(path, &st) && S_ISLNK (st.st_mode));
1576 if (!(real = malloc( PATH_MAX ))) return 0;
1577 if (!realpath( path, real ))
1579 free( real );
1580 real = NULL;
1582 else path = real;
1584 /* test the file type */
1586 if ((fd = open( path, O_WRONLY )) != -1)
1588 /* if file is not a regular file or has multiple links or is accessed
1589 * via symbolic links, write directly into it; otherwise use a temp file */
1590 if (by_symlink ||
1591 (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1)))
1593 ftruncate( fd, 0 );
1594 goto save;
1596 close( fd );
1599 /* create a temp file in the same directory */
1601 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1602 strcpy( tmp, path );
1603 if ((p = strrchr( tmp, '/' ))) p++;
1604 else p = tmp;
1605 for (;;)
1607 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1608 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1609 if (errno != EEXIST) goto done;
1610 close( fd );
1613 /* now save to it */
1615 save:
1616 if (!(f = fdopen( fd, "w" )))
1618 if (tmp) unlink( tmp );
1619 close( fd );
1620 goto done;
1623 if (debug_level > 1)
1625 fprintf( stderr, "%s: ", path );
1626 dump_operation( key, NULL, "saving" );
1629 save_all_subkeys( key, f );
1630 ret = !fclose(f);
1632 if (tmp)
1634 /* if successfully written, rename to final name */
1635 if (ret) ret = !rename( tmp, path );
1636 if (!ret) unlink( tmp );
1639 done:
1640 free( tmp );
1641 free( real );
1642 if (ret) make_clean( key );
1643 return ret;
1646 /* periodic saving of the registry */
1647 static void periodic_save( void *arg )
1649 int i;
1651 save_timeout_user = NULL;
1652 for (i = 0; i < save_branch_count; i++)
1653 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1654 set_periodic_save_timer();
1657 /* start the periodic save timer */
1658 static void set_periodic_save_timer(void)
1660 struct timeval next;
1662 gettimeofday( &next, NULL );
1663 add_timeout( &next, save_period );
1664 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1665 save_timeout_user = add_timeout_user( &next, periodic_save, NULL );
1668 /* save the modified registry branches to disk */
1669 void flush_registry(void)
1671 int i;
1673 for (i = 0; i < save_branch_count; i++)
1675 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1677 fprintf( stderr, "wineserver: could not save registry branch to %s",
1678 save_branch_info[i].path );
1679 perror( " " );
1684 /* close the top-level keys; used on server exit */
1685 void close_registry(void)
1687 int i;
1689 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1690 save_timeout_user = NULL;
1691 for (i = 0; i < save_branch_count; i++)
1693 release_object( save_branch_info[i].key );
1694 free( save_branch_info[i].path );
1696 release_object( root_key );
1700 /* create a registry key */
1701 DECL_HANDLER(create_key)
1703 struct key *key = NULL, *parent;
1704 struct unicode_str name, class;
1705 unsigned int access = req->access;
1707 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1708 reply->hkey = 0;
1710 if (req->namelen > get_req_data_size())
1712 set_error( STATUS_INVALID_PARAMETER );
1713 return;
1715 class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1716 class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1717 get_req_path( &name, !req->parent );
1718 if (name.str > class.str)
1720 set_error( STATUS_INVALID_PARAMETER );
1721 return;
1723 name.len = (class.str - name.str) * sizeof(WCHAR);
1725 /* NOTE: no access rights are required from the parent handle to create a key */
1726 if ((parent = get_hkey_obj( req->parent, 0 )))
1728 int flags = (req->options & REG_OPTION_VOLATILE) ? KEY_VOLATILE : KEY_DIRTY;
1730 if ((key = create_key( parent, &name, &class, flags, req->modif, &reply->created )))
1732 reply->hkey = alloc_handle( current->process, key, access, 0 );
1733 release_object( key );
1735 release_object( parent );
1739 /* open a registry key */
1740 DECL_HANDLER(open_key)
1742 struct key *key, *parent;
1743 struct unicode_str name;
1744 unsigned int access = req->access;
1746 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1747 reply->hkey = 0;
1748 /* NOTE: no access rights are required to open the parent key, only the child key */
1749 if ((parent = get_hkey_obj( req->parent, 0 )))
1751 get_req_path( &name, !req->parent );
1752 if ((key = open_key( parent, &name )))
1754 reply->hkey = alloc_handle( current->process, key, access, 0 );
1755 release_object( key );
1757 release_object( parent );
1761 /* delete a registry key */
1762 DECL_HANDLER(delete_key)
1764 struct key *key;
1766 if ((key = get_hkey_obj( req->hkey, DELETE )))
1768 delete_key( key, 0);
1769 release_object( key );
1773 /* flush a registry key */
1774 DECL_HANDLER(flush_key)
1776 struct key *key = get_hkey_obj( req->hkey, 0 );
1777 if (key)
1779 /* we don't need to do anything here with the current implementation */
1780 release_object( key );
1784 /* enumerate registry subkeys */
1785 DECL_HANDLER(enum_key)
1787 struct key *key;
1789 if ((key = get_hkey_obj( req->hkey,
1790 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1792 enum_key( key, req->index, req->info_class, reply );
1793 release_object( key );
1797 /* set a value of a registry key */
1798 DECL_HANDLER(set_key_value)
1800 struct key *key;
1801 struct unicode_str name;
1803 if (req->namelen > get_req_data_size())
1805 set_error( STATUS_INVALID_PARAMETER );
1806 return;
1808 name.str = get_req_data();
1809 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
1811 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1813 size_t datalen = get_req_data_size() - req->namelen;
1814 const char *data = (const char *)get_req_data() + req->namelen;
1816 set_value( key, &name, req->type, data, datalen );
1817 release_object( key );
1821 /* retrieve the value of a registry key */
1822 DECL_HANDLER(get_key_value)
1824 struct key *key;
1825 struct unicode_str name;
1827 reply->total = 0;
1828 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1830 get_req_unicode_str( &name );
1831 get_value( key, &name, &reply->type, &reply->total );
1832 release_object( key );
1836 /* enumerate the value of a registry key */
1837 DECL_HANDLER(enum_key_value)
1839 struct key *key;
1841 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1843 enum_value( key, req->index, req->info_class, reply );
1844 release_object( key );
1848 /* delete a value of a registry key */
1849 DECL_HANDLER(delete_key_value)
1851 struct key *key;
1852 struct unicode_str name;
1854 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1856 get_req_unicode_str( &name );
1857 delete_value( key, &name );
1858 release_object( key );
1862 /* load a registry branch from a file */
1863 DECL_HANDLER(load_registry)
1865 struct key *key, *parent;
1866 struct token *token = thread_get_impersonation_token( current );
1867 struct unicode_str name;
1869 const LUID_AND_ATTRIBUTES privs[] =
1871 { SeBackupPrivilege, 0 },
1872 { SeRestorePrivilege, 0 },
1875 if (!token || !token_check_privileges( token, TRUE, privs,
1876 sizeof(privs)/sizeof(privs[0]), NULL ))
1878 set_error( STATUS_PRIVILEGE_NOT_HELD );
1879 return;
1882 if ((parent = get_hkey_obj( req->hkey, 0 )))
1884 int dummy;
1885 get_req_path( &name, !req->hkey );
1886 if ((key = create_key( parent, &name, NULL, KEY_DIRTY, time(NULL), &dummy )))
1888 load_registry( key, req->file );
1889 release_object( key );
1891 release_object( parent );
1895 DECL_HANDLER(unload_registry)
1897 struct key *key;
1898 struct token *token = thread_get_impersonation_token( current );
1900 const LUID_AND_ATTRIBUTES privs[] =
1902 { SeBackupPrivilege, 0 },
1903 { SeRestorePrivilege, 0 },
1906 if (!token || !token_check_privileges( token, TRUE, privs,
1907 sizeof(privs)/sizeof(privs[0]), NULL ))
1909 set_error( STATUS_PRIVILEGE_NOT_HELD );
1910 return;
1913 if ((key = get_hkey_obj( req->hkey, 0 )))
1915 delete_key( key, 1 ); /* FIXME */
1916 release_object( key );
1920 /* save a registry branch to a file */
1921 DECL_HANDLER(save_registry)
1923 struct key *key;
1925 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
1927 set_error( STATUS_PRIVILEGE_NOT_HELD );
1928 return;
1931 if ((key = get_hkey_obj( req->hkey, 0 )))
1933 save_registry( key, req->file );
1934 release_object( key );
1938 /* add a registry key change notification */
1939 DECL_HANDLER(set_registry_notification)
1941 struct key *key;
1942 struct event *event;
1943 struct notify *notify;
1945 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
1946 if (key)
1948 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
1949 if (event)
1951 notify = find_notify( key, current->process, req->hkey );
1952 if (notify)
1954 release_object( notify->event );
1955 grab_object( event );
1956 notify->event = event;
1958 else
1960 notify = mem_alloc( sizeof(*notify) );
1961 if (notify)
1963 grab_object( event );
1964 notify->event = event;
1965 notify->subtree = req->subtree;
1966 notify->filter = req->filter;
1967 notify->hkey = req->hkey;
1968 notify->process = current->process;
1969 list_add_head( &key->notify_list, &notify->entry );
1972 release_object( event );
1974 release_object( key );