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
26 #include "wine/port.h"
41 #define WIN32_NO_STATUS
50 #include "wine/library.h"
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 */
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 */
83 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
84 #define KEY_DELETED 0x0002 /* key has been deleted */
85 #define KEY_DIRTY 0x0004 /* key has been modified */
86 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
91 WCHAR *name; /* value name */
92 unsigned short namelen; /* length of value name */
93 unsigned short type; /* value type */
94 data_size_t len; /* value data length in bytes */
95 void *data; /* pointer to value data */
98 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
99 #define MIN_VALUES 8 /* min. number of allocated values per key */
101 #define MAX_NAME_LEN MAX_PATH /* max. length of a key name */
102 #define MAX_VALUE_LEN MAX_PATH /* max. length of a value name */
104 /* the root of the registry tree */
105 static struct key *root_key;
107 static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
108 static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
109 static struct timeout_user *save_timeout_user; /* saving timer */
111 static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
112 static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
113 static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
115 static void set_periodic_save_timer(void);
116 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
118 /* information about where to save a registry branch */
119 struct save_branch_info
125 #define MAX_SAVE_BRANCH_INFO 3
126 static int save_branch_count;
127 static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
130 /* information about a file being loaded */
131 struct file_load_info
133 const char *filename; /* input file name */
134 FILE *file; /* input file */
135 char *buffer; /* line buffer */
136 int len; /* buffer length */
137 int line; /* current input line */
138 WCHAR *tmp; /* temp buffer to use while parsing input */
139 size_t tmplen; /* length of temp buffer */
143 static void key_dump( struct object *obj, int verbose );
144 static unsigned int key_map_access( struct object *obj, unsigned int access );
145 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
146 static void key_destroy( struct object *obj );
148 static const struct object_ops key_ops =
150 sizeof(struct key), /* size */
152 no_get_type, /* get_type */
153 no_add_queue, /* add_queue */
154 NULL, /* remove_queue */
156 NULL, /* satisfied */
157 no_signal, /* signal */
158 no_get_fd, /* get_fd */
159 key_map_access, /* map_access */
160 default_get_sd, /* get_sd */
161 default_set_sd, /* set_sd */
162 no_lookup_name, /* lookup_name */
163 no_open_file, /* open_file */
164 key_close_handle, /* close_handle */
165 key_destroy /* destroy */
170 * The registry text file format v2 used by this code is similar to the one
171 * used by REGEDIT import/export functionality, with the following differences:
172 * - strings and key names can contain \x escapes for Unicode
173 * - key names use escapes too in order to support Unicode
174 * - the modification time optionally follows the key name
175 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
178 /* dump the full path of a key */
179 static void dump_path( const struct key *key, const struct key *base, FILE *f )
181 if (key->parent && key->parent != base)
183 dump_path( key->parent, base, f );
184 fprintf( f, "\\\\" );
186 dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
189 /* dump a value to a text file */
190 static void dump_value( const struct key_value *value, FILE *f )
198 count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
199 count += fprintf( f, "\"=" );
201 else count = fprintf( f, "@=" );
208 /* only output properly terminated strings in string format */
209 if (value->len < sizeof(WCHAR)) break;
210 if (value->len % sizeof(WCHAR)) break;
211 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
212 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
214 dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
215 fprintf( f, "\"\n" );
219 if (value->len != sizeof(dw)) break;
220 memcpy( &dw, value->data, sizeof(dw) );
221 fprintf( f, "dword:%08x\n", dw );
225 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
226 else count += fprintf( f, "hex(%x):", value->type );
227 for (i = 0; i < value->len; i++)
229 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
230 if (i < value->len-1)
235 fprintf( f, "\\\n " );
243 /* save a registry and all its subkeys to a text file */
244 static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
248 if (key->flags & KEY_VOLATILE) return;
249 /* save key if it has either some values or no subkeys, or needs special options */
250 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
251 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class)
254 if (key != base) dump_path( key, base, f );
255 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
258 fprintf( f, "#class=\"" );
259 dump_strW( key->class, key->classlen / sizeof(WCHAR), f, "\"\"" );
260 fprintf( f, "\"\n" );
262 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
264 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
267 static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
269 fprintf( stderr, "%s key ", op );
270 if (key) dump_path( key, NULL, stderr );
271 else fprintf( stderr, "ERROR" );
274 fprintf( stderr, " value ");
275 dump_value( value, stderr );
277 else fprintf( stderr, "\n" );
280 static void key_dump( struct object *obj, int verbose )
282 struct key *key = (struct key *)obj;
283 assert( obj->ops == &key_ops );
284 fprintf( stderr, "Key flags=%x ", key->flags );
285 dump_path( key, NULL, stderr );
286 fprintf( stderr, "\n" );
289 /* notify waiter and maybe delete the notification */
290 static void do_notification( struct key *key, struct notify *notify, int del )
294 set_event( notify->event );
295 release_object( notify->event );
296 notify->event = NULL;
300 list_remove( ¬ify->entry );
305 static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
307 struct notify *notify;
309 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
311 if (notify->process == process && notify->hkey == hkey) return notify;
316 static unsigned int key_map_access( struct object *obj, unsigned int access )
318 if (access & GENERIC_READ) access |= KEY_READ;
319 if (access & GENERIC_WRITE) access |= KEY_WRITE;
320 if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
321 if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS;
322 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
325 /* close the notification associated with a handle */
326 static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
328 struct key * key = (struct key *) obj;
329 struct notify *notify = find_notify( key, process, handle );
330 if (notify) do_notification( key, notify, 1 );
331 return 1; /* ok to close */
334 static void key_destroy( struct object *obj )
338 struct key *key = (struct key *)obj;
339 assert( obj->ops == &key_ops );
343 for (i = 0; i <= key->last_value; i++)
345 free( key->values[i].name );
346 free( key->values[i].data );
349 for (i = 0; i <= key->last_subkey; i++)
351 key->subkeys[i]->parent = NULL;
352 release_object( key->subkeys[i] );
354 free( key->subkeys );
355 /* unconditionally notify everything waiting on this key */
356 while ((ptr = list_head( &key->notify_list )))
358 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
359 do_notification( key, notify, 1 );
363 /* get the request vararg as registry path */
364 static inline void get_req_path( struct unicode_str *str, int skip_root )
366 str->str = get_req_data();
367 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
369 if (skip_root && str->len >= sizeof(root_name) &&
370 !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
372 str->str += sizeof(root_name)/sizeof(WCHAR);
373 str->len -= sizeof(root_name);
377 /* return the next token in a given path */
378 /* token->str must point inside the path, or be NULL for the first call */
379 static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
381 data_size_t i = 0, len = path->len / sizeof(WCHAR);
383 if (!token->str) /* first time */
385 /* path cannot start with a backslash */
386 if (len && path->str[0] == '\\')
388 set_error( STATUS_OBJECT_PATH_INVALID );
394 i = token->str - path->str;
395 i += token->len / sizeof(WCHAR);
396 while (i < len && path->str[i] == '\\') i++;
398 token->str = path->str + i;
399 while (i < len && path->str[i] != '\\') i++;
400 token->len = (path->str + i - token->str) * sizeof(WCHAR);
404 /* allocate a key object */
405 static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
408 if ((key = alloc_object( &key_ops )))
412 key->namelen = name->len;
415 key->last_subkey = -1;
419 key->last_value = -1;
423 list_init( &key->notify_list );
424 if (name->len && !(key->name = memdup( name->str, name->len )))
426 release_object( key );
433 /* mark a key and all its parents as dirty (modified) */
434 static void make_dirty( struct key *key )
438 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
439 key->flags |= KEY_DIRTY;
444 /* mark a key and all its subkeys as clean (not modified) */
445 static void make_clean( struct key *key )
449 if (key->flags & KEY_VOLATILE) return;
450 if (!(key->flags & KEY_DIRTY)) return;
451 key->flags &= ~KEY_DIRTY;
452 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
455 /* go through all the notifications and send them if necessary */
456 static void check_notify( struct key *key, unsigned int change, int not_subtree )
458 struct list *ptr, *next;
460 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
462 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
463 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
464 do_notification( key, n, 0 );
468 /* update key modification time */
469 static void touch_key( struct key *key, unsigned int change )
473 key->modif = current_time;
476 /* do notifications */
477 check_notify( key, change, 1 );
478 for ( k = key->parent; k; k = k->parent )
479 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
482 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
483 static int grow_subkeys( struct key *key )
485 struct key **new_subkeys;
490 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
491 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
493 set_error( STATUS_NO_MEMORY );
499 nb_subkeys = MIN_VALUES;
500 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
502 key->subkeys = new_subkeys;
503 key->nb_subkeys = nb_subkeys;
507 /* allocate a subkey for a given key, and return its index */
508 static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
509 int index, timeout_t modif )
514 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
516 set_error( STATUS_NAME_TOO_LONG );
519 if (parent->last_subkey + 1 == parent->nb_subkeys)
521 /* need to grow the array */
522 if (!grow_subkeys( parent )) return NULL;
524 if ((key = alloc_key( name, modif )) != NULL)
526 key->parent = parent;
527 for (i = ++parent->last_subkey; i > index; i--)
528 parent->subkeys[i] = parent->subkeys[i-1];
529 parent->subkeys[index] = key;
534 /* free a subkey of a given key */
535 static void free_subkey( struct key *parent, int index )
540 assert( index >= 0 );
541 assert( index <= parent->last_subkey );
543 key = parent->subkeys[index];
544 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
545 parent->last_subkey--;
546 key->flags |= KEY_DELETED;
548 release_object( key );
550 /* try to shrink the array */
551 nb_subkeys = parent->nb_subkeys;
552 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
554 struct key **new_subkeys;
555 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
556 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
557 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
558 parent->subkeys = new_subkeys;
559 parent->nb_subkeys = nb_subkeys;
563 /* find the named child of a given key and return its index */
564 static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
566 int i, min, max, res;
570 max = key->last_subkey;
574 len = min( key->subkeys[i]->namelen, name->len );
575 res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
576 if (!res) res = key->subkeys[i]->namelen - name->len;
580 return key->subkeys[i];
582 if (res > 0) max = i - 1;
585 *index = min; /* this is where we should insert it */
589 /* follow a symlink and return the resolved key */
590 static struct key *follow_symlink( struct key *key, int iteration )
592 struct unicode_str path, token;
593 struct key_value *value;
596 if (iteration > 16) return NULL;
597 if (!(key->flags & KEY_SYMLINK)) return key;
598 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
600 path.str = value->data;
601 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
602 if (path.len <= sizeof(root_name)) return NULL;
603 if (memicmpW( path.str, root_name, sizeof(root_name)/sizeof(WCHAR) )) return NULL;
604 path.str += sizeof(root_name) / sizeof(WCHAR);
605 path.len -= sizeof(root_name);
609 if (!get_path_token( &path, &token )) return NULL;
612 if (!(key = find_subkey( key, &token, &index ))) break;
613 if (!(key = follow_symlink( key, iteration + 1 ))) break;
614 get_path_token( &path, &token );
620 static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int attributes )
623 struct unicode_str token;
626 if (!get_path_token( name, &token )) return NULL;
629 if (!(key = find_subkey( key, &token, &index )))
631 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
634 get_path_token( name, &token );
635 if (!token.len) break;
636 if (!(key = follow_symlink( key, 0 )))
638 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
643 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
645 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
648 if (debug_level > 1) dump_operation( key, NULL, "Open" );
653 /* create a subkey */
654 static struct key *create_key( struct key *key, const struct unicode_str *name,
655 const struct unicode_str *class, int flags, unsigned int options,
656 unsigned int attributes, timeout_t modif, int *created )
660 struct unicode_str token;
662 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
664 set_error( STATUS_KEY_DELETED );
669 if (!get_path_token( name, &token )) return NULL;
674 if (!(subkey = find_subkey( key, &token, &index ))) break;
676 get_path_token( name, &token );
677 if (!token.len) break;
678 if (!(subkey = follow_symlink( subkey, 0 )))
680 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
685 /* create the remaining part */
689 if (options & REG_OPTION_CREATE_LINK)
691 set_error( STATUS_OBJECT_NAME_COLLISION );
694 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
696 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
701 if (options & REG_OPTION_VOLATILE)
703 flags = (flags & ~KEY_DIRTY) | KEY_VOLATILE;
705 else if (key->flags & KEY_VOLATILE)
707 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
711 if (flags & KEY_DIRTY) make_dirty( key );
712 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
717 get_path_token( name, &token );
718 if (!token.len) break;
719 /* we know the index is always 0 in a new key */
720 if (!(key = alloc_subkey( key, &token, 0, modif )))
722 free_subkey( base, index );
726 /* FIXME: no saving of symlinks yet */
727 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK | KEY_VOLATILE;
730 if (debug_level > 1) dump_operation( key, NULL, "Create" );
731 if (class && class->len)
733 key->classlen = class->len;
735 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
741 /* query information about a key or a subkey */
742 static void enum_key( const struct key *key, int index, int info_class,
743 struct enum_key_reply *reply )
746 data_size_t len, namelen, classlen;
747 data_size_t max_subkey = 0, max_class = 0;
748 data_size_t max_value = 0, max_data = 0;
751 if (index != -1) /* -1 means use the specified key directly */
753 if ((index < 0) || (index > key->last_subkey))
755 set_error( STATUS_NO_MORE_ENTRIES );
758 key = key->subkeys[index];
761 namelen = key->namelen;
762 classlen = key->classlen;
766 case KeyBasicInformation:
767 classlen = 0; /* only return the name */
769 case KeyNodeInformation:
770 reply->max_subkey = 0;
771 reply->max_class = 0;
772 reply->max_value = 0;
775 case KeyFullInformation:
776 for (i = 0; i <= key->last_subkey; i++)
778 struct key *subkey = key->subkeys[i];
779 len = subkey->namelen / sizeof(WCHAR);
780 if (len > max_subkey) max_subkey = len;
781 len = subkey->classlen / sizeof(WCHAR);
782 if (len > max_class) max_class = len;
784 for (i = 0; i <= key->last_value; i++)
786 len = key->values[i].namelen / sizeof(WCHAR);
787 if (len > max_value) max_value = len;
788 len = key->values[i].len;
789 if (len > max_data) max_data = len;
791 reply->max_subkey = max_subkey;
792 reply->max_class = max_class;
793 reply->max_value = max_value;
794 reply->max_data = max_data;
795 namelen = 0; /* only return the class */
798 set_error( STATUS_INVALID_PARAMETER );
801 reply->subkeys = key->last_subkey + 1;
802 reply->values = key->last_value + 1;
803 reply->modif = key->modif;
804 reply->total = namelen + classlen;
806 len = min( reply->total, get_reply_max_size() );
807 if (len && (data = set_reply_data_size( len )))
811 reply->namelen = namelen;
812 memcpy( data, key->name, namelen );
813 memcpy( data + namelen, key->class, len - namelen );
817 reply->namelen = len;
818 memcpy( data, key->name, len );
821 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
824 /* delete a key and its values */
825 static int delete_key( struct key *key, int recurse )
830 /* must find parent and index */
833 set_error( STATUS_ACCESS_DENIED );
836 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
838 set_error( STATUS_KEY_DELETED );
842 while (recurse && (key->last_subkey>=0))
843 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
846 for (index = 0; index <= parent->last_subkey; index++)
847 if (parent->subkeys[index] == key) break;
848 assert( index <= parent->last_subkey );
850 /* we can only delete a key that has no subkeys */
851 if (key->last_subkey >= 0)
853 set_error( STATUS_ACCESS_DENIED );
857 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
858 free_subkey( parent, index );
859 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
863 /* try to grow the array of values; return 1 if OK, 0 on error */
864 static int grow_values( struct key *key )
866 struct key_value *new_val;
871 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
872 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
874 set_error( STATUS_NO_MEMORY );
880 nb_values = MIN_VALUES;
881 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
883 key->values = new_val;
884 key->nb_values = nb_values;
888 /* find the named value of a given key and return its index in the array */
889 static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
891 int i, min, max, res;
895 max = key->last_value;
899 len = min( key->values[i].namelen, name->len );
900 res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
901 if (!res) res = key->values[i].namelen - name->len;
905 return &key->values[i];
907 if (res > 0) max = i - 1;
910 *index = min; /* this is where we should insert it */
914 /* insert a new value; the index must have been returned by find_value */
915 static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
917 struct key_value *value;
918 WCHAR *new_name = NULL;
921 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
923 set_error( STATUS_NAME_TOO_LONG );
926 if (key->last_value + 1 == key->nb_values)
928 if (!grow_values( key )) return NULL;
930 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
931 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
932 value = &key->values[index];
933 value->name = new_name;
934 value->namelen = name->len;
940 /* set a key value */
941 static void set_value( struct key *key, const struct unicode_str *name,
942 int type, const void *data, data_size_t len )
944 struct key_value *value;
948 if ((value = find_value( key, name, &index )))
950 /* check if the new value is identical to the existing one */
951 if (value->type == type && value->len == len &&
952 value->data && !memcmp( value->data, data, len ))
954 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
959 if (key->flags & KEY_SYMLINK)
961 if (type != REG_LINK || name->len != symlink_str.len ||
962 memicmpW( name->str, symlink_str.str, name->len / sizeof(WCHAR) ))
964 set_error( STATUS_ACCESS_DENIED );
969 if (len && !(ptr = memdup( data, len ))) return;
973 if (!(value = insert_value( key, name, index )))
979 else free( value->data ); /* already existing, free previous data */
984 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
985 if (debug_level > 1) dump_operation( key, value, "Set" );
988 /* get a key value */
989 static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
991 struct key_value *value;
994 if ((value = find_value( key, name, &index )))
998 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
999 if (debug_level > 1) dump_operation( key, value, "Get" );
1004 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1008 /* enumerate a key value */
1009 static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
1011 struct key_value *value;
1013 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
1017 data_size_t namelen, maxlen;
1019 value = &key->values[i];
1020 reply->type = value->type;
1021 namelen = value->namelen;
1025 case KeyValueBasicInformation:
1026 reply->total = namelen;
1028 case KeyValueFullInformation:
1029 reply->total = namelen + value->len;
1031 case KeyValuePartialInformation:
1032 reply->total = value->len;
1036 set_error( STATUS_INVALID_PARAMETER );
1040 maxlen = min( reply->total, get_reply_max_size() );
1041 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1043 if (maxlen > namelen)
1045 reply->namelen = namelen;
1046 memcpy( data, value->name, namelen );
1047 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
1051 reply->namelen = maxlen;
1052 memcpy( data, value->name, maxlen );
1055 if (debug_level > 1) dump_operation( key, value, "Enum" );
1059 /* delete a value */
1060 static void delete_value( struct key *key, const struct unicode_str *name )
1062 struct key_value *value;
1063 int i, index, nb_values;
1065 if (!(value = find_value( key, name, &index )))
1067 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
1070 if (debug_level > 1) dump_operation( key, value, "Delete" );
1071 free( value->name );
1072 free( value->data );
1073 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1075 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
1077 /* try to shrink the array */
1078 nb_values = key->nb_values;
1079 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1081 struct key_value *new_val;
1082 nb_values -= nb_values / 3; /* shrink by 33% */
1083 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1084 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1085 key->values = new_val;
1086 key->nb_values = nb_values;
1090 /* get the registry key corresponding to an hkey handle */
1091 static inline struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
1093 return (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1096 /* get the registry key corresponding to a parent key handle */
1097 static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
1099 if (!hkey) return (struct key *)grab_object( root_key );
1100 return (struct key *)get_handle_obj( current->process, hkey, 0, &key_ops );
1103 /* read a line from the input file */
1104 static int read_next_line( struct file_load_info *info )
1107 int newlen, pos = 0;
1112 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1113 return (pos != 0); /* EOF */
1114 pos = strlen(info->buffer);
1115 if (info->buffer[pos-1] == '\n')
1117 /* got a full line */
1118 info->buffer[--pos] = 0;
1119 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1122 if (pos < info->len - 1) return 1; /* EOF but something was read */
1124 /* need to enlarge the buffer */
1125 newlen = info->len + info->len / 2;
1126 if (!(newbuf = realloc( info->buffer, newlen )))
1128 set_error( STATUS_NO_MEMORY );
1131 info->buffer = newbuf;
1136 /* make sure the temp buffer holds enough space */
1137 static int get_file_tmp_space( struct file_load_info *info, size_t size )
1140 if (info->tmplen >= size) return 1;
1141 if (!(tmp = realloc( info->tmp, size )))
1143 set_error( STATUS_NO_MEMORY );
1147 info->tmplen = size;
1151 /* report an error while loading an input file */
1152 static void file_read_error( const char *err, struct file_load_info *info )
1155 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1157 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
1160 /* convert a data type tag to a value type */
1161 static int get_data_type( const char *buffer, int *type, int *parse_type )
1163 struct data_type { const char *tag; int len; int type; int parse_type; };
1165 static const struct data_type data_types[] =
1166 { /* actual type */ /* type to assume for parsing */
1167 { "\"", 1, REG_SZ, REG_SZ },
1168 { "str:\"", 5, REG_SZ, REG_SZ },
1169 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1170 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1171 { "hex:", 4, REG_BINARY, REG_BINARY },
1172 { "dword:", 6, REG_DWORD, REG_DWORD },
1173 { "hex(", 4, -1, REG_BINARY },
1177 const struct data_type *ptr;
1180 for (ptr = data_types; ptr->tag; ptr++)
1182 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
1183 *parse_type = ptr->parse_type;
1184 if ((*type = ptr->type) != -1) return ptr->len;
1185 /* "hex(xx):" is special */
1186 *type = (int)strtoul( buffer + 4, &end, 16 );
1187 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
1188 return end + 2 - buffer;
1193 /* load and create a key from the input file */
1194 static struct key *load_key( struct key *base, const char *buffer, int flags,
1195 int prefix_len, struct file_load_info *info )
1198 struct unicode_str name;
1201 timeout_t modif = current_time;
1204 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1207 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
1209 file_read_error( "Malformed key", info );
1212 if (sscanf( buffer + res, " %u", &mod ) == 1)
1213 modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
1216 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1222 file_read_error( "Malformed key", info );
1225 /* empty key name, return base key */
1226 return (struct key *)grab_object( base );
1229 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
1230 return create_key( base, &name, NULL, flags, 0, 0, modif, &res );
1233 /* load a key option from the input file */
1234 static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1239 if (!strncmp( buffer, "#class=", 7 ))
1242 if (*p++ != '"') return 0;
1243 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1245 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1247 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1248 key->classlen = len;
1250 /* ignore unknown options */
1254 /* parse a comma-separated list of hex digits */
1255 static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
1257 const char *p = buffer;
1258 data_size_t count = 0;
1261 while (isxdigit(*p))
1263 unsigned int val = strtoul( p, &end, 16 );
1264 if (end == p || val > 0xff) return -1;
1265 if (count++ >= *len) return -1; /* dest buffer overflow */
1268 while (isspace(*p)) p++;
1270 while (isspace(*p)) p++;
1276 /* parse a value name and create the corresponding value */
1277 static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
1278 struct file_load_info *info )
1280 struct key_value *value;
1281 struct unicode_str name;
1284 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
1285 name.str = info->tmp;
1286 name.len = info->tmplen;
1287 if (buffer[0] == '@')
1294 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
1295 if (r == -1) goto error;
1296 *len = r + 1; /* for initial quote */
1297 name.len -= sizeof(WCHAR); /* terminating null */
1299 while (isspace(buffer[*len])) (*len)++;
1300 if (buffer[*len] != '=') goto error;
1302 while (isspace(buffer[*len])) (*len)++;
1303 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
1307 file_read_error( "Malformed value name", info );
1311 /* load a value from the input file */
1312 static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1316 int res, type, parse_type;
1317 data_size_t maxlen, len;
1318 struct key_value *value;
1320 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1321 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1322 buffer += len + res;
1327 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1329 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
1333 dw = strtoul( buffer, NULL, 16 );
1337 case REG_BINARY: /* hex digits */
1341 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
1342 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1343 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
1346 while (isspace(*buffer)) buffer++;
1347 if (!*buffer) break;
1348 if (*buffer != '\\') goto error;
1349 if (read_next_line( info) != 1) goto error;
1350 buffer = info->buffer;
1351 while (isspace(*buffer)) buffer++;
1357 ptr = NULL; /* keep compiler quiet */
1361 if (!len) newptr = NULL;
1362 else if (!(newptr = memdup( ptr, len ))) return 0;
1364 free( value->data );
1365 value->data = newptr;
1371 file_read_error( "Malformed value", info );
1372 free( value->data );
1375 value->type = REG_NONE;
1379 /* return the length (in path elements) of name that is part of the key name */
1380 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1381 static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1387 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
1390 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
1392 file_read_error( "Malformed key", info );
1395 for (p = info->tmp; *p; p++) if (*p == '\\') break;
1396 len = (p - info->tmp) * sizeof(WCHAR);
1397 for (res = 1; key != root_key; res++)
1399 if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
1402 if (key == root_key) res = 0; /* no matching name */
1406 /* load all the keys from the input file */
1407 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1408 static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
1410 struct key *subkey = NULL;
1411 struct file_load_info info;
1414 info.filename = filename;
1419 if (!(info.buffer = mem_alloc( info.len ))) return;
1420 if (!(info.tmp = mem_alloc( info.tmplen )))
1422 free( info.buffer );
1426 if ((read_next_line( &info ) != 1) ||
1427 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1429 set_error( STATUS_NOT_REGISTRY_FILE );
1433 while (read_next_line( &info ) == 1)
1436 while (*p && isspace(*p)) p++;
1439 case '[': /* new key */
1440 if (subkey) release_object( subkey );
1441 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
1442 if (!(subkey = load_key( key, p + 1, key->flags, prefix_len, &info )))
1443 file_read_error( "Error creating key", &info );
1445 case '@': /* default value */
1446 case '\"': /* value */
1447 if (subkey) load_value( subkey, p, &info );
1448 else file_read_error( "Value without key", &info );
1450 case '#': /* option */
1451 if (subkey) load_key_option( subkey, p, &info );
1453 case ';': /* comment */
1454 case 0: /* empty line */
1457 file_read_error( "Unrecognized input", &info );
1463 if (subkey) release_object( subkey );
1464 free( info.buffer );
1468 /* load a part of the registry from a file */
1469 static void load_registry( struct key *key, obj_handle_t handle )
1474 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
1475 fd = dup( get_file_unix_fd( file ) );
1476 release_object( file );
1479 FILE *f = fdopen( fd, "r" );
1482 load_keys( key, NULL, f, -1 );
1485 else file_set_error();
1489 /* load one of the initial registry files */
1490 static void load_init_registry_from_file( const char *filename, struct key *key )
1494 if ((f = fopen( filename, "r" )))
1496 load_keys( key, filename, f, 0 );
1498 if (get_error() == STATUS_NOT_REGISTRY_FILE)
1500 fprintf( stderr, "%s is not a valid registry file\n", filename );
1505 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
1507 save_branch_info[save_branch_count].path = filename;
1508 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1509 make_object_static( &key->obj );
1512 static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
1514 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1515 static const WCHAR formatW[] = {'-','%','u',0};
1516 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1520 strcpyW( p, prefixW );
1521 p += strlenW( prefixW );
1522 p += sprintfW( p, formatW, sid->Revision );
1523 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1524 sid->IdentifierAuthority.Value[4] ),
1525 MAKEWORD( sid->IdentifierAuthority.Value[3],
1526 sid->IdentifierAuthority.Value[2] )));
1527 for (i = 0; i < sid->SubAuthorityCount; i++)
1528 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1530 path->len = (p - buffer) * sizeof(WCHAR);
1531 path->str = p = memdup( buffer, path->len );
1535 /* registry initialisation */
1536 void init_registry(void)
1538 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1539 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1540 static const struct unicode_str root_name = { NULL, 0 };
1541 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1542 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
1544 WCHAR *current_user_path;
1545 struct unicode_str current_user_str;
1550 /* switch to the config dir */
1552 if (fchdir( config_dir_fd ) == -1) fatal_perror( "chdir to config dir" );
1554 /* create the root key */
1555 root_key = alloc_key( &root_name, current_time );
1557 make_object_static( &root_key->obj );
1559 /* load system.reg into Registry\Machine */
1561 if (!(key = create_key( root_key, &HKLM_name, NULL, 0, 0, 0, current_time, &dummy )))
1562 fatal_error( "could not create Machine registry key\n" );
1564 load_init_registry_from_file( "system.reg", key );
1565 release_object( key );
1567 /* load userdef.reg into Registry\User\.Default */
1569 if (!(key = create_key( root_key, &HKU_name, NULL, 0, 0, 0, current_time, &dummy )))
1570 fatal_error( "could not create User\\.Default registry key\n" );
1572 load_init_registry_from_file( "userdef.reg", key );
1573 release_object( key );
1575 /* load user.reg into HKEY_CURRENT_USER */
1577 /* FIXME: match default user in token.c. should get from process token instead */
1578 current_user_path = format_user_registry_path( security_interactive_sid, ¤t_user_str );
1579 if (!current_user_path ||
1580 !(key = create_key( root_key, ¤t_user_str, NULL, 0, 0, 0, current_time, &dummy )))
1581 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1582 free( current_user_path );
1583 load_init_registry_from_file( "user.reg", key );
1584 release_object( key );
1586 /* start the periodic save timer */
1587 set_periodic_save_timer();
1589 /* go back to the server dir */
1590 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1593 /* save a registry branch to a file */
1594 static void save_all_subkeys( struct key *key, FILE *f )
1596 fprintf( f, "WINE REGISTRY Version 2\n" );
1597 fprintf( f, ";; All keys relative to " );
1598 dump_path( key, NULL, f );
1600 save_subkeys( key, key, f );
1603 /* save a registry branch to a file handle */
1604 static void save_registry( struct key *key, obj_handle_t handle )
1609 if (key->flags & KEY_DELETED)
1611 set_error( STATUS_KEY_DELETED );
1614 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
1615 fd = dup( get_file_unix_fd( file ) );
1616 release_object( file );
1619 FILE *f = fdopen( fd, "w" );
1622 save_all_subkeys( key, f );
1623 if (fclose( f )) file_set_error();
1633 /* save a registry branch to a file */
1634 static int save_branch( struct key *key, const char *path )
1637 char *p, *tmp = NULL;
1638 int fd, count = 0, ret = 0;
1641 if (!(key->flags & KEY_DIRTY))
1643 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1647 /* test the file type */
1649 if ((fd = open( path, O_WRONLY )) != -1)
1651 /* if file is not a regular file or has multiple links or is accessed
1652 * via symbolic links, write directly into it; otherwise use a temp file */
1653 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1661 /* create a temp file in the same directory */
1663 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1664 strcpy( tmp, path );
1665 if ((p = strrchr( tmp, '/' ))) p++;
1669 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
1670 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1671 if (errno != EEXIST) goto done;
1675 /* now save to it */
1678 if (!(f = fdopen( fd, "w" )))
1680 if (tmp) unlink( tmp );
1685 if (debug_level > 1)
1687 fprintf( stderr, "%s: ", path );
1688 dump_operation( key, NULL, "saving" );
1691 save_all_subkeys( key, f );
1696 /* if successfully written, rename to final name */
1697 if (ret) ret = !rename( tmp, path );
1698 if (!ret) unlink( tmp );
1703 if (ret) make_clean( key );
1707 /* periodic saving of the registry */
1708 static void periodic_save( void *arg )
1712 if (fchdir( config_dir_fd ) == -1) return;
1713 save_timeout_user = NULL;
1714 for (i = 0; i < save_branch_count; i++)
1715 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1716 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1717 set_periodic_save_timer();
1720 /* start the periodic save timer */
1721 static void set_periodic_save_timer(void)
1723 if (save_timeout_user) remove_timeout_user( save_timeout_user );
1724 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
1727 /* save the modified registry branches to disk */
1728 void flush_registry(void)
1732 if (fchdir( config_dir_fd ) == -1) return;
1733 for (i = 0; i < save_branch_count; i++)
1735 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1737 fprintf( stderr, "wineserver: could not save registry branch to %s",
1738 save_branch_info[i].path );
1742 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
1746 /* create a registry key */
1747 DECL_HANDLER(create_key)
1749 struct key *key = NULL, *parent;
1750 struct unicode_str name, class;
1751 unsigned int access = req->access;
1755 if (req->namelen > get_req_data_size())
1757 set_error( STATUS_INVALID_PARAMETER );
1760 class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1761 class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1762 get_req_path( &name, !req->parent );
1763 if (name.str > class.str)
1765 set_error( STATUS_INVALID_PARAMETER );
1768 name.len = (class.str - name.str) * sizeof(WCHAR);
1770 /* NOTE: no access rights are required from the parent handle to create a key */
1771 if ((parent = get_parent_hkey_obj( req->parent )))
1773 if ((key = create_key( parent, &name, &class, KEY_DIRTY, req->options,
1774 req->attributes, current_time, &reply->created )))
1776 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1777 release_object( key );
1779 release_object( parent );
1783 /* open a registry key */
1784 DECL_HANDLER(open_key)
1786 struct key *key, *parent;
1787 struct unicode_str name;
1788 unsigned int access = req->access;
1791 /* NOTE: no access rights are required to open the parent key, only the child key */
1792 if ((parent = get_parent_hkey_obj( req->parent )))
1794 get_req_path( &name, !req->parent );
1795 if ((key = open_key( parent, &name, req->attributes )))
1797 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
1798 release_object( key );
1800 release_object( parent );
1804 /* delete a registry key */
1805 DECL_HANDLER(delete_key)
1809 if ((key = get_hkey_obj( req->hkey, DELETE )))
1811 delete_key( key, 0);
1812 release_object( key );
1816 /* flush a registry key */
1817 DECL_HANDLER(flush_key)
1819 struct key *key = get_hkey_obj( req->hkey, 0 );
1822 /* we don't need to do anything here with the current implementation */
1823 release_object( key );
1827 /* enumerate registry subkeys */
1828 DECL_HANDLER(enum_key)
1832 if ((key = get_hkey_obj( req->hkey,
1833 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
1835 enum_key( key, req->index, req->info_class, reply );
1836 release_object( key );
1840 /* set a value of a registry key */
1841 DECL_HANDLER(set_key_value)
1844 struct unicode_str name;
1846 if (req->namelen > get_req_data_size())
1848 set_error( STATUS_INVALID_PARAMETER );
1851 name.str = get_req_data();
1852 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
1854 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1856 data_size_t datalen = get_req_data_size() - req->namelen;
1857 const char *data = (const char *)get_req_data() + req->namelen;
1859 set_value( key, &name, req->type, data, datalen );
1860 release_object( key );
1864 /* retrieve the value of a registry key */
1865 DECL_HANDLER(get_key_value)
1868 struct unicode_str name;
1871 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1873 get_req_unicode_str( &name );
1874 get_value( key, &name, &reply->type, &reply->total );
1875 release_object( key );
1879 /* enumerate the value of a registry key */
1880 DECL_HANDLER(enum_key_value)
1884 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1886 enum_value( key, req->index, req->info_class, reply );
1887 release_object( key );
1891 /* delete a value of a registry key */
1892 DECL_HANDLER(delete_key_value)
1895 struct unicode_str name;
1897 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1899 get_req_unicode_str( &name );
1900 delete_value( key, &name );
1901 release_object( key );
1905 /* load a registry branch from a file */
1906 DECL_HANDLER(load_registry)
1908 struct key *key, *parent;
1909 struct token *token = thread_get_impersonation_token( current );
1910 struct unicode_str name;
1912 const LUID_AND_ATTRIBUTES privs[] =
1914 { SeBackupPrivilege, 0 },
1915 { SeRestorePrivilege, 0 },
1918 if (!token || !token_check_privileges( token, TRUE, privs,
1919 sizeof(privs)/sizeof(privs[0]), NULL ))
1921 set_error( STATUS_PRIVILEGE_NOT_HELD );
1925 if ((parent = get_parent_hkey_obj( req->hkey )))
1928 get_req_path( &name, !req->hkey );
1929 if ((key = create_key( parent, &name, NULL, KEY_DIRTY, 0, 0, current_time, &dummy )))
1931 load_registry( key, req->file );
1932 release_object( key );
1934 release_object( parent );
1938 DECL_HANDLER(unload_registry)
1941 struct token *token = thread_get_impersonation_token( current );
1943 const LUID_AND_ATTRIBUTES privs[] =
1945 { SeBackupPrivilege, 0 },
1946 { SeRestorePrivilege, 0 },
1949 if (!token || !token_check_privileges( token, TRUE, privs,
1950 sizeof(privs)/sizeof(privs[0]), NULL ))
1952 set_error( STATUS_PRIVILEGE_NOT_HELD );
1956 if ((key = get_hkey_obj( req->hkey, 0 )))
1958 delete_key( key, 1 ); /* FIXME */
1959 release_object( key );
1963 /* save a registry branch to a file */
1964 DECL_HANDLER(save_registry)
1968 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
1970 set_error( STATUS_PRIVILEGE_NOT_HELD );
1974 if ((key = get_hkey_obj( req->hkey, 0 )))
1976 save_registry( key, req->file );
1977 release_object( key );
1981 /* add a registry key change notification */
1982 DECL_HANDLER(set_registry_notification)
1985 struct event *event;
1986 struct notify *notify;
1988 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
1991 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
1994 notify = find_notify( key, current->process, req->hkey );
1998 release_object( notify->event );
1999 grab_object( event );
2000 notify->event = event;
2004 notify = mem_alloc( sizeof(*notify) );
2007 grab_object( event );
2008 notify->event = event;
2009 notify->subtree = req->subtree;
2010 notify->filter = req->filter;
2011 notify->hkey = req->hkey;
2012 notify->process = current->process;
2013 list_add_head( &key->notify_list, ¬ify->entry );
2016 release_object( event );
2018 release_object( key );