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
51 #include "wine/library.h"
55 struct list entry
; /* entry in list of notifications */
56 struct event
*event
; /* event to set when changing this key */
57 int subtree
; /* true if subtree notification */
58 unsigned int filter
; /* which events to notify on */
59 obj_handle_t hkey
; /* hkey associated with this notification */
60 struct process
*process
; /* process in which the hkey is valid */
66 struct object obj
; /* object header */
67 WCHAR
*name
; /* key name */
68 WCHAR
*class; /* key class */
69 unsigned short namelen
; /* length of key name */
70 unsigned short classlen
; /* length of class name */
71 struct key
*parent
; /* parent key */
72 int last_subkey
; /* last in use subkey */
73 int nb_subkeys
; /* count of allocated subkeys */
74 struct key
**subkeys
; /* subkeys array */
75 int last_value
; /* last in use value */
76 int nb_values
; /* count of allocated values in array */
77 struct key_value
*values
; /* values array */
78 unsigned int flags
; /* flags */
79 timeout_t modif
; /* last modification time */
80 struct list notify_list
; /* list of notifications */
84 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
85 #define KEY_DELETED 0x0002 /* key has been deleted */
86 #define KEY_DIRTY 0x0004 /* key has been modified */
87 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
88 #define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
89 #define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
94 WCHAR
*name
; /* value name */
95 unsigned short namelen
; /* length of value name */
96 unsigned int type
; /* value type */
97 data_size_t len
; /* value data length in bytes */
98 void *data
; /* pointer to value data */
101 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
102 #define MIN_VALUES 8 /* min. number of allocated values per key */
104 #define MAX_NAME_LEN 256 /* max. length of a key name */
105 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
107 /* the root of the registry tree */
108 static struct key
*root_key
;
110 static const timeout_t ticks_1601_to_1970
= (timeout_t
)86400 * (369 * 365 + 89) * TICKS_PER_SEC
;
111 static const timeout_t save_period
= 30 * -TICKS_PER_SEC
; /* delay between periodic saves */
112 static struct timeout_user
*save_timeout_user
; /* saving timer */
113 static enum prefix_type
{ PREFIX_UNKNOWN
, PREFIX_32BIT
, PREFIX_64BIT
} prefix_type
;
115 static const WCHAR root_name
[] = { '\\','R','e','g','i','s','t','r','y','\\' };
116 static const WCHAR wow6432node
[] = {'W','o','w','6','4','3','2','N','o','d','e'};
117 static const WCHAR symlink_value
[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
118 static const struct unicode_str symlink_str
= { symlink_value
, sizeof(symlink_value
) };
120 static void set_periodic_save_timer(void);
121 static struct key_value
*find_value( const struct key
*key
, const struct unicode_str
*name
, int *index
);
123 /* information about where to save a registry branch */
124 struct save_branch_info
130 #define MAX_SAVE_BRANCH_INFO 3
131 static int save_branch_count
;
132 static struct save_branch_info save_branch_info
[MAX_SAVE_BRANCH_INFO
];
135 /* information about a file being loaded */
136 struct file_load_info
138 const char *filename
; /* input file name */
139 FILE *file
; /* input file */
140 char *buffer
; /* line buffer */
141 int len
; /* buffer length */
142 int line
; /* current input line */
143 WCHAR
*tmp
; /* temp buffer to use while parsing input */
144 size_t tmplen
; /* length of temp buffer */
148 static void key_dump( struct object
*obj
, int verbose
);
149 static struct object_type
*key_get_type( struct object
*obj
);
150 static unsigned int key_map_access( struct object
*obj
, unsigned int access
);
151 static struct security_descriptor
*key_get_sd( struct object
*obj
);
152 static int key_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
153 static void key_destroy( struct object
*obj
);
155 static const struct object_ops key_ops
=
157 sizeof(struct key
), /* size */
159 key_get_type
, /* get_type */
160 no_add_queue
, /* add_queue */
161 NULL
, /* remove_queue */
163 NULL
, /* satisfied */
164 no_signal
, /* signal */
165 no_get_fd
, /* get_fd */
166 key_map_access
, /* map_access */
167 key_get_sd
, /* get_sd */
168 default_set_sd
, /* set_sd */
169 no_lookup_name
, /* lookup_name */
170 no_link_name
, /* link_name */
171 NULL
, /* unlink_name */
172 no_open_file
, /* open_file */
173 no_kernel_obj_list
, /* get_kernel_obj_list */
174 key_close_handle
, /* close_handle */
175 key_destroy
/* destroy */
179 static inline int is_wow6432node( const WCHAR
*name
, unsigned int len
)
181 return (len
== sizeof(wow6432node
) &&
182 !memicmpW( name
, wow6432node
, ARRAY_SIZE( wow6432node
)));
186 * The registry text file format v2 used by this code is similar to the one
187 * used by REGEDIT import/export functionality, with the following differences:
188 * - strings and key names can contain \x escapes for Unicode
189 * - key names use escapes too in order to support Unicode
190 * - the modification time optionally follows the key name
191 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
194 /* dump the full path of a key */
195 static void dump_path( const struct key
*key
, const struct key
*base
, FILE *f
)
197 if (key
->parent
&& key
->parent
!= base
)
199 dump_path( key
->parent
, base
, f
);
200 fprintf( f
, "\\\\" );
202 dump_strW( key
->name
, key
->namelen
/ sizeof(WCHAR
), f
, "[]" );
205 /* dump a value to a text file */
206 static void dump_value( const struct key_value
*value
, FILE *f
)
214 count
= 1 + dump_strW( value
->name
, value
->namelen
/ sizeof(WCHAR
), f
, "\"\"" );
215 count
+= fprintf( f
, "\"=" );
217 else count
= fprintf( f
, "@=" );
224 /* only output properly terminated strings in string format */
225 if (value
->len
< sizeof(WCHAR
)) break;
226 if (value
->len
% sizeof(WCHAR
)) break;
227 if (((WCHAR
*)value
->data
)[value
->len
/ sizeof(WCHAR
) - 1]) break;
228 if (value
->type
!= REG_SZ
) fprintf( f
, "str(%x):", value
->type
);
230 dump_strW( (WCHAR
*)value
->data
, value
->len
/ sizeof(WCHAR
), f
, "\"\"" );
231 fprintf( f
, "\"\n" );
235 if (value
->len
!= sizeof(dw
)) break;
236 memcpy( &dw
, value
->data
, sizeof(dw
) );
237 fprintf( f
, "dword:%08x\n", dw
);
241 if (value
->type
== REG_BINARY
) count
+= fprintf( f
, "hex:" );
242 else count
+= fprintf( f
, "hex(%x):", value
->type
);
243 for (i
= 0; i
< value
->len
; i
++)
245 count
+= fprintf( f
, "%02x", *((unsigned char *)value
->data
+ i
) );
246 if (i
< value
->len
-1)
251 fprintf( f
, "\\\n " );
259 /* save a registry and all its subkeys to a text file */
260 static void save_subkeys( const struct key
*key
, const struct key
*base
, FILE *f
)
264 if (key
->flags
& KEY_VOLATILE
) return;
265 /* save key if it has either some values or no subkeys, or needs special options */
266 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
267 if ((key
->last_value
>= 0) || (key
->last_subkey
== -1) || key
->class || (key
->flags
& KEY_SYMLINK
))
270 if (key
!= base
) dump_path( key
, base
, f
);
271 fprintf( f
, "] %u\n", (unsigned int)((key
->modif
- ticks_1601_to_1970
) / TICKS_PER_SEC
) );
272 fprintf( f
, "#time=%x%08x\n", (unsigned int)(key
->modif
>> 32), (unsigned int)key
->modif
);
275 fprintf( f
, "#class=\"" );
276 dump_strW( key
->class, key
->classlen
/ sizeof(WCHAR
), f
, "\"\"" );
277 fprintf( f
, "\"\n" );
279 if (key
->flags
& KEY_SYMLINK
) fputs( "#link\n", f
);
280 for (i
= 0; i
<= key
->last_value
; i
++) dump_value( &key
->values
[i
], f
);
282 for (i
= 0; i
<= key
->last_subkey
; i
++) save_subkeys( key
->subkeys
[i
], base
, f
);
285 static void dump_operation( const struct key
*key
, const struct key_value
*value
, const char *op
)
287 fprintf( stderr
, "%s key ", op
);
288 if (key
) dump_path( key
, NULL
, stderr
);
289 else fprintf( stderr
, "ERROR" );
292 fprintf( stderr
, " value ");
293 dump_value( value
, stderr
);
295 else fprintf( stderr
, "\n" );
298 static void key_dump( struct object
*obj
, int verbose
)
300 struct key
*key
= (struct key
*)obj
;
301 assert( obj
->ops
== &key_ops
);
302 fprintf( stderr
, "Key flags=%x ", key
->flags
);
303 dump_path( key
, NULL
, stderr
);
304 fprintf( stderr
, "\n" );
307 static struct object_type
*key_get_type( struct object
*obj
)
309 static const WCHAR name
[] = {'K','e','y'};
310 static const struct unicode_str str
= { name
, sizeof(name
) };
311 return get_object_type( &str
);
314 /* notify waiter and maybe delete the notification */
315 static void do_notification( struct key
*key
, struct notify
*notify
, int del
)
319 set_event( notify
->event
);
320 release_object( notify
->event
);
321 notify
->event
= NULL
;
325 list_remove( ¬ify
->entry
);
330 static inline struct notify
*find_notify( struct key
*key
, struct process
*process
, obj_handle_t hkey
)
332 struct notify
*notify
;
334 LIST_FOR_EACH_ENTRY( notify
, &key
->notify_list
, struct notify
, entry
)
336 if (notify
->process
== process
&& notify
->hkey
== hkey
) return notify
;
341 static unsigned int key_map_access( struct object
*obj
, unsigned int access
)
343 if (access
& GENERIC_READ
) access
|= KEY_READ
;
344 if (access
& GENERIC_WRITE
) access
|= KEY_WRITE
;
345 if (access
& GENERIC_EXECUTE
) access
|= KEY_EXECUTE
;
346 if (access
& GENERIC_ALL
) access
|= KEY_ALL_ACCESS
;
347 /* filter the WOW64 masks, as they aren't real access bits */
348 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
|
349 KEY_WOW64_64KEY
| KEY_WOW64_32KEY
);
352 static struct security_descriptor
*key_get_sd( struct object
*obj
)
354 static struct security_descriptor
*key_default_sd
;
356 if (obj
->sd
) return obj
->sd
;
360 size_t users_sid_len
= security_sid_len( security_builtin_users_sid
);
361 size_t admins_sid_len
= security_sid_len( security_builtin_admins_sid
);
362 size_t dacl_len
= sizeof(ACL
) + 2 * offsetof( ACCESS_ALLOWED_ACE
, SidStart
)
363 + users_sid_len
+ admins_sid_len
;
364 ACCESS_ALLOWED_ACE
*aaa
;
367 key_default_sd
= mem_alloc( sizeof(*key_default_sd
) + 2 * admins_sid_len
+ dacl_len
);
368 key_default_sd
->control
= SE_DACL_PRESENT
;
369 key_default_sd
->owner_len
= admins_sid_len
;
370 key_default_sd
->group_len
= admins_sid_len
;
371 key_default_sd
->sacl_len
= 0;
372 key_default_sd
->dacl_len
= dacl_len
;
373 memcpy( key_default_sd
+ 1, security_builtin_admins_sid
, admins_sid_len
);
374 memcpy( (char *)(key_default_sd
+ 1) + admins_sid_len
, security_builtin_admins_sid
, admins_sid_len
);
376 dacl
= (ACL
*)((char *)(key_default_sd
+ 1) + 2 * admins_sid_len
);
377 dacl
->AclRevision
= ACL_REVISION
;
379 dacl
->AclSize
= dacl_len
;
382 aaa
= (ACCESS_ALLOWED_ACE
*)(dacl
+ 1);
383 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
384 aaa
->Header
.AceFlags
= INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
;
385 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + users_sid_len
;
386 aaa
->Mask
= GENERIC_READ
;
387 memcpy( &aaa
->SidStart
, security_builtin_users_sid
, users_sid_len
);
388 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
389 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
390 aaa
->Header
.AceFlags
= 0;
391 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + admins_sid_len
;
392 aaa
->Mask
= KEY_ALL_ACCESS
;
393 memcpy( &aaa
->SidStart
, security_builtin_admins_sid
, admins_sid_len
);
395 return key_default_sd
;
398 /* close the notification associated with a handle */
399 static int key_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
401 struct key
* key
= (struct key
*) obj
;
402 struct notify
*notify
= find_notify( key
, process
, handle
);
403 if (notify
) do_notification( key
, notify
, 1 );
404 return 1; /* ok to close */
407 static void key_destroy( struct object
*obj
)
411 struct key
*key
= (struct key
*)obj
;
412 assert( obj
->ops
== &key_ops
);
416 for (i
= 0; i
<= key
->last_value
; i
++)
418 free( key
->values
[i
].name
);
419 free( key
->values
[i
].data
);
422 for (i
= 0; i
<= key
->last_subkey
; i
++)
424 key
->subkeys
[i
]->parent
= NULL
;
425 release_object( key
->subkeys
[i
] );
427 free( key
->subkeys
);
428 /* unconditionally notify everything waiting on this key */
429 while ((ptr
= list_head( &key
->notify_list
)))
431 struct notify
*notify
= LIST_ENTRY( ptr
, struct notify
, entry
);
432 do_notification( key
, notify
, 1 );
436 /* get the request vararg as registry path */
437 static inline void get_req_path( struct unicode_str
*str
, int skip_root
)
439 str
->str
= get_req_data();
440 str
->len
= (get_req_data_size() / sizeof(WCHAR
)) * sizeof(WCHAR
);
442 if (skip_root
&& str
->len
>= sizeof(root_name
) &&
443 !memicmpW( str
->str
, root_name
, ARRAY_SIZE( root_name
)))
445 str
->str
+= ARRAY_SIZE( root_name
);
446 str
->len
-= sizeof(root_name
);
450 /* return the next token in a given path */
451 /* token->str must point inside the path, or be NULL for the first call */
452 static struct unicode_str
*get_path_token( const struct unicode_str
*path
, struct unicode_str
*token
)
454 data_size_t i
= 0, len
= path
->len
/ sizeof(WCHAR
);
456 if (!token
->str
) /* first time */
458 /* path cannot start with a backslash */
459 if (len
&& path
->str
[0] == '\\')
461 set_error( STATUS_OBJECT_PATH_INVALID
);
467 i
= token
->str
- path
->str
;
468 i
+= token
->len
/ sizeof(WCHAR
);
469 while (i
< len
&& path
->str
[i
] == '\\') i
++;
471 token
->str
= path
->str
+ i
;
472 while (i
< len
&& path
->str
[i
] != '\\') i
++;
473 token
->len
= (path
->str
+ i
- token
->str
) * sizeof(WCHAR
);
477 /* allocate a key object */
478 static struct key
*alloc_key( const struct unicode_str
*name
, timeout_t modif
)
481 if ((key
= alloc_object( &key_ops
)))
485 key
->namelen
= name
->len
;
488 key
->last_subkey
= -1;
492 key
->last_value
= -1;
496 list_init( &key
->notify_list
);
497 if (name
->len
&& !(key
->name
= memdup( name
->str
, name
->len
)))
499 release_object( key
);
506 /* mark a key and all its parents as dirty (modified) */
507 static void make_dirty( struct key
*key
)
511 if (key
->flags
& (KEY_DIRTY
|KEY_VOLATILE
)) return; /* nothing to do */
512 key
->flags
|= KEY_DIRTY
;
517 /* mark a key and all its subkeys as clean (not modified) */
518 static void make_clean( struct key
*key
)
522 if (key
->flags
& KEY_VOLATILE
) return;
523 if (!(key
->flags
& KEY_DIRTY
)) return;
524 key
->flags
&= ~KEY_DIRTY
;
525 for (i
= 0; i
<= key
->last_subkey
; i
++) make_clean( key
->subkeys
[i
] );
528 /* go through all the notifications and send them if necessary */
529 static void check_notify( struct key
*key
, unsigned int change
, int not_subtree
)
531 struct list
*ptr
, *next
;
533 LIST_FOR_EACH_SAFE( ptr
, next
, &key
->notify_list
)
535 struct notify
*n
= LIST_ENTRY( ptr
, struct notify
, entry
);
536 if ( ( not_subtree
|| n
->subtree
) && ( change
& n
->filter
) )
537 do_notification( key
, n
, 0 );
541 /* update key modification time */
542 static void touch_key( struct key
*key
, unsigned int change
)
546 key
->modif
= current_time
;
549 /* do notifications */
550 check_notify( key
, change
, 1 );
551 for ( k
= key
->parent
; k
; k
= k
->parent
)
552 check_notify( k
, change
, 0 );
555 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
556 static int grow_subkeys( struct key
*key
)
558 struct key
**new_subkeys
;
563 nb_subkeys
= key
->nb_subkeys
+ (key
->nb_subkeys
/ 2); /* grow by 50% */
564 if (!(new_subkeys
= realloc( key
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) )))
566 set_error( STATUS_NO_MEMORY
);
572 nb_subkeys
= MIN_SUBKEYS
;
573 if (!(new_subkeys
= mem_alloc( nb_subkeys
* sizeof(*new_subkeys
) ))) return 0;
575 key
->subkeys
= new_subkeys
;
576 key
->nb_subkeys
= nb_subkeys
;
580 /* allocate a subkey for a given key, and return its index */
581 static struct key
*alloc_subkey( struct key
*parent
, const struct unicode_str
*name
,
582 int index
, timeout_t modif
)
587 if (name
->len
> MAX_NAME_LEN
* sizeof(WCHAR
))
589 set_error( STATUS_INVALID_PARAMETER
);
592 if (parent
->last_subkey
+ 1 == parent
->nb_subkeys
)
594 /* need to grow the array */
595 if (!grow_subkeys( parent
)) return NULL
;
597 if ((key
= alloc_key( name
, modif
)) != NULL
)
599 key
->parent
= parent
;
600 for (i
= ++parent
->last_subkey
; i
> index
; i
--)
601 parent
->subkeys
[i
] = parent
->subkeys
[i
-1];
602 parent
->subkeys
[index
] = key
;
603 if (is_wow6432node( key
->name
, key
->namelen
) && !is_wow6432node( parent
->name
, parent
->namelen
))
604 parent
->flags
|= KEY_WOW64
;
609 /* free a subkey of a given key */
610 static void free_subkey( struct key
*parent
, int index
)
615 assert( index
>= 0 );
616 assert( index
<= parent
->last_subkey
);
618 key
= parent
->subkeys
[index
];
619 for (i
= index
; i
< parent
->last_subkey
; i
++) parent
->subkeys
[i
] = parent
->subkeys
[i
+ 1];
620 parent
->last_subkey
--;
621 key
->flags
|= KEY_DELETED
;
623 if (is_wow6432node( key
->name
, key
->namelen
)) parent
->flags
&= ~KEY_WOW64
;
624 release_object( key
);
626 /* try to shrink the array */
627 nb_subkeys
= parent
->nb_subkeys
;
628 if (nb_subkeys
> MIN_SUBKEYS
&& parent
->last_subkey
< nb_subkeys
/ 2)
630 struct key
**new_subkeys
;
631 nb_subkeys
-= nb_subkeys
/ 3; /* shrink by 33% */
632 if (nb_subkeys
< MIN_SUBKEYS
) nb_subkeys
= MIN_SUBKEYS
;
633 if (!(new_subkeys
= realloc( parent
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) ))) return;
634 parent
->subkeys
= new_subkeys
;
635 parent
->nb_subkeys
= nb_subkeys
;
639 /* find the named child of a given key and return its index */
640 static struct key
*find_subkey( const struct key
*key
, const struct unicode_str
*name
, int *index
)
642 int i
, min
, max
, res
;
646 max
= key
->last_subkey
;
650 len
= min( key
->subkeys
[i
]->namelen
, name
->len
);
651 res
= memicmpW( key
->subkeys
[i
]->name
, name
->str
, len
/ sizeof(WCHAR
) );
652 if (!res
) res
= key
->subkeys
[i
]->namelen
- name
->len
;
656 return key
->subkeys
[i
];
658 if (res
> 0) max
= i
- 1;
661 *index
= min
; /* this is where we should insert it */
665 /* return the wow64 variant of the key, or the key itself if none */
666 static struct key
*find_wow64_subkey( struct key
*key
, const struct unicode_str
*name
)
668 static const struct unicode_str wow6432node_str
= { wow6432node
, sizeof(wow6432node
) };
671 if (!(key
->flags
& KEY_WOW64
)) return key
;
672 if (!is_wow6432node( name
->str
, name
->len
))
674 key
= find_subkey( key
, &wow6432node_str
, &index
);
675 assert( key
); /* if KEY_WOW64 is set we must find it */
681 /* follow a symlink and return the resolved key */
682 static struct key
*follow_symlink( struct key
*key
, int iteration
)
684 struct unicode_str path
, token
;
685 struct key_value
*value
;
688 if (iteration
> 16) return NULL
;
689 if (!(key
->flags
& KEY_SYMLINK
)) return key
;
690 if (!(value
= find_value( key
, &symlink_str
, &index
))) return NULL
;
692 path
.str
= value
->data
;
693 path
.len
= (value
->len
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
694 if (path
.len
<= sizeof(root_name
)) return NULL
;
695 if (memicmpW( path
.str
, root_name
, ARRAY_SIZE( root_name
))) return NULL
;
696 path
.str
+= ARRAY_SIZE( root_name
);
697 path
.len
-= sizeof(root_name
);
701 if (!get_path_token( &path
, &token
)) return NULL
;
704 if (!(key
= find_subkey( key
, &token
, &index
))) break;
705 if (!(key
= follow_symlink( key
, iteration
+ 1 ))) break;
706 get_path_token( &path
, &token
);
711 /* open a key until we find an element that doesn't exist */
712 /* helper for open_key and create_key */
713 static struct key
*open_key_prefix( struct key
*key
, const struct unicode_str
*name
,
714 unsigned int access
, struct unicode_str
*token
, int *index
)
717 if (!get_path_token( name
, token
)) return NULL
;
718 if (access
& KEY_WOW64_32KEY
) key
= find_wow64_subkey( key
, token
);
722 if (!(subkey
= find_subkey( key
, token
, index
)))
724 if ((key
->flags
& KEY_WOWSHARE
) && !(access
& KEY_WOW64_64KEY
))
726 /* try in the 64-bit parent */
728 subkey
= find_subkey( key
, token
, index
);
733 get_path_token( name
, token
);
734 if (!token
->len
) break;
735 if (!(access
& KEY_WOW64_64KEY
)) key
= find_wow64_subkey( key
, token
);
736 if (!(key
= follow_symlink( key
, 0 )))
738 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
746 static struct key
*open_key( struct key
*key
, const struct unicode_str
*name
, unsigned int access
,
747 unsigned int attributes
)
750 struct unicode_str token
;
752 if (!(key
= open_key_prefix( key
, name
, access
, &token
, &index
))) return NULL
;
756 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
759 if (!(access
& KEY_WOW64_64KEY
)) key
= find_wow64_subkey( key
, &token
);
760 if (!(attributes
& OBJ_OPENLINK
) && !(key
= follow_symlink( key
, 0 )))
762 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
765 if (debug_level
> 1) dump_operation( key
, NULL
, "Open" );
770 /* create a subkey */
771 static struct key
*create_key( struct key
*key
, const struct unicode_str
*name
,
772 const struct unicode_str
*class, unsigned int options
,
773 unsigned int access
, unsigned int attributes
,
774 const struct security_descriptor
*sd
, int *created
)
777 struct unicode_str token
, next
;
780 if (!(key
= open_key_prefix( key
, name
, access
, &token
, &index
))) return NULL
;
782 if (!token
.len
) /* the key already exists */
784 if (!(access
& KEY_WOW64_64KEY
)) key
= find_wow64_subkey( key
, &token
);
785 if (options
& REG_OPTION_CREATE_LINK
)
787 set_error( STATUS_OBJECT_NAME_COLLISION
);
790 if (!(attributes
& OBJ_OPENLINK
) && !(key
= follow_symlink( key
, 0 )))
792 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
795 if (debug_level
> 1) dump_operation( key
, NULL
, "Open" );
800 /* token must be the last path component at this point */
802 get_path_token( name
, &next
);
805 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
809 if ((key
->flags
& KEY_VOLATILE
) && !(options
& REG_OPTION_VOLATILE
))
811 set_error( STATUS_CHILD_MUST_BE_VOLATILE
);
816 if (!(key
= alloc_subkey( key
, &token
, index
, current_time
))) return NULL
;
818 if (options
& REG_OPTION_CREATE_LINK
) key
->flags
|= KEY_SYMLINK
;
819 if (options
& REG_OPTION_VOLATILE
) key
->flags
|= KEY_VOLATILE
;
820 else key
->flags
|= KEY_DIRTY
;
822 if (sd
) default_set_sd( &key
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
823 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
825 if (debug_level
> 1) dump_operation( key
, NULL
, "Create" );
826 if (class && class->len
)
828 key
->classlen
= class->len
;
830 if (!(key
->class = memdup( class->str
, key
->classlen
))) key
->classlen
= 0;
832 touch_key( key
->parent
, REG_NOTIFY_CHANGE_NAME
);
837 /* recursively create a subkey (for internal use only) */
838 static struct key
*create_key_recursive( struct key
*key
, const struct unicode_str
*name
, timeout_t modif
)
842 struct unicode_str token
;
845 if (!get_path_token( name
, &token
)) return NULL
;
849 if (!(subkey
= find_subkey( key
, &token
, &index
))) break;
851 if (!(key
= follow_symlink( key
, 0 )))
853 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
856 get_path_token( name
, &token
);
861 if (!(key
= alloc_subkey( key
, &token
, index
, modif
))) return NULL
;
865 get_path_token( name
, &token
);
866 if (!token
.len
) break;
867 /* we know the index is always 0 in a new key */
868 if (!(key
= alloc_subkey( key
, &token
, 0, modif
)))
870 free_subkey( base
, index
);
880 /* query information about a key or a subkey */
881 static void enum_key( const struct key
*key
, int index
, int info_class
,
882 struct enum_key_reply
*reply
)
884 static const WCHAR backslash
[] = { '\\' };
886 data_size_t len
, namelen
, classlen
;
887 data_size_t max_subkey
= 0, max_class
= 0;
888 data_size_t max_value
= 0, max_data
= 0;
892 if (index
!= -1) /* -1 means use the specified key directly */
894 if ((index
< 0) || (index
> key
->last_subkey
))
896 set_error( STATUS_NO_MORE_ENTRIES
);
899 key
= key
->subkeys
[index
];
902 namelen
= key
->namelen
;
903 classlen
= key
->classlen
;
907 case KeyNameInformation
:
909 for (k
= key
; k
!= root_key
; k
= k
->parent
)
910 namelen
+= k
->namelen
+ sizeof(backslash
);
911 if (!namelen
) return;
912 namelen
+= sizeof(root_name
) - sizeof(backslash
);
914 case KeyBasicInformation
:
915 classlen
= 0; /* only return the name */
917 case KeyNodeInformation
:
918 reply
->max_subkey
= 0;
919 reply
->max_class
= 0;
920 reply
->max_value
= 0;
923 case KeyFullInformation
:
924 case KeyCachedInformation
:
925 for (i
= 0; i
<= key
->last_subkey
; i
++)
927 if (key
->subkeys
[i
]->namelen
> max_subkey
) max_subkey
= key
->subkeys
[i
]->namelen
;
928 if (key
->subkeys
[i
]->classlen
> max_class
) max_class
= key
->subkeys
[i
]->classlen
;
930 for (i
= 0; i
<= key
->last_value
; i
++)
932 if (key
->values
[i
].namelen
> max_value
) max_value
= key
->values
[i
].namelen
;
933 if (key
->values
[i
].len
> max_data
) max_data
= key
->values
[i
].len
;
935 reply
->max_subkey
= max_subkey
;
936 reply
->max_class
= max_class
;
937 reply
->max_value
= max_value
;
938 reply
->max_data
= max_data
;
939 reply
->namelen
= namelen
;
940 if (info_class
== KeyCachedInformation
)
941 classlen
= 0; /* don't return any data, only its size */
942 namelen
= 0; /* don't return name */
945 set_error( STATUS_INVALID_PARAMETER
);
948 reply
->subkeys
= key
->last_subkey
+ 1;
949 reply
->values
= key
->last_value
+ 1;
950 reply
->modif
= key
->modif
;
951 reply
->total
= namelen
+ classlen
;
953 len
= min( reply
->total
, get_reply_max_size() );
954 if (len
&& (data
= set_reply_data_size( len
)))
958 reply
->namelen
= namelen
;
959 memcpy( data
, key
->name
, namelen
);
960 memcpy( data
+ namelen
, key
->class, len
- namelen
);
962 else if (info_class
== KeyNameInformation
)
964 data_size_t pos
= namelen
;
965 reply
->namelen
= namelen
;
966 for (k
= key
; k
!= root_key
; k
= k
->parent
)
969 if (pos
< len
) memcpy( data
+ pos
, k
->name
,
970 min( k
->namelen
, len
- pos
) );
971 pos
-= sizeof(backslash
);
972 if (pos
< len
) memcpy( data
+ pos
, backslash
,
973 min( sizeof(backslash
), len
- pos
) );
975 memcpy( data
, root_name
, min( sizeof(root_name
) - sizeof(backslash
), len
) );
979 reply
->namelen
= len
;
980 memcpy( data
, key
->name
, len
);
983 if (debug_level
> 1) dump_operation( key
, NULL
, "Enum" );
986 /* delete a key and its values */
987 static int delete_key( struct key
*key
, int recurse
)
990 struct key
*parent
= key
->parent
;
992 /* must find parent and index */
995 set_error( STATUS_ACCESS_DENIED
);
1000 while (recurse
&& (key
->last_subkey
>=0))
1001 if (0 > delete_key(key
->subkeys
[key
->last_subkey
], 1))
1004 for (index
= 0; index
<= parent
->last_subkey
; index
++)
1005 if (parent
->subkeys
[index
] == key
) break;
1006 assert( index
<= parent
->last_subkey
);
1008 /* we can only delete a key that has no subkeys */
1009 if (key
->last_subkey
>= 0)
1011 set_error( STATUS_ACCESS_DENIED
);
1015 if (debug_level
> 1) dump_operation( key
, NULL
, "Delete" );
1016 free_subkey( parent
, index
);
1017 touch_key( parent
, REG_NOTIFY_CHANGE_NAME
);
1021 /* try to grow the array of values; return 1 if OK, 0 on error */
1022 static int grow_values( struct key
*key
)
1024 struct key_value
*new_val
;
1029 nb_values
= key
->nb_values
+ (key
->nb_values
/ 2); /* grow by 50% */
1030 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) )))
1032 set_error( STATUS_NO_MEMORY
);
1038 nb_values
= MIN_VALUES
;
1039 if (!(new_val
= mem_alloc( nb_values
* sizeof(*new_val
) ))) return 0;
1041 key
->values
= new_val
;
1042 key
->nb_values
= nb_values
;
1046 /* find the named value of a given key and return its index in the array */
1047 static struct key_value
*find_value( const struct key
*key
, const struct unicode_str
*name
, int *index
)
1049 int i
, min
, max
, res
;
1053 max
= key
->last_value
;
1056 i
= (min
+ max
) / 2;
1057 len
= min( key
->values
[i
].namelen
, name
->len
);
1058 res
= memicmpW( key
->values
[i
].name
, name
->str
, len
/ sizeof(WCHAR
) );
1059 if (!res
) res
= key
->values
[i
].namelen
- name
->len
;
1063 return &key
->values
[i
];
1065 if (res
> 0) max
= i
- 1;
1068 *index
= min
; /* this is where we should insert it */
1072 /* insert a new value; the index must have been returned by find_value */
1073 static struct key_value
*insert_value( struct key
*key
, const struct unicode_str
*name
, int index
)
1075 struct key_value
*value
;
1076 WCHAR
*new_name
= NULL
;
1079 if (name
->len
> MAX_VALUE_LEN
* sizeof(WCHAR
))
1081 set_error( STATUS_NAME_TOO_LONG
);
1084 if (key
->last_value
+ 1 == key
->nb_values
)
1086 if (!grow_values( key
)) return NULL
;
1088 if (name
->len
&& !(new_name
= memdup( name
->str
, name
->len
))) return NULL
;
1089 for (i
= ++key
->last_value
; i
> index
; i
--) key
->values
[i
] = key
->values
[i
- 1];
1090 value
= &key
->values
[index
];
1091 value
->name
= new_name
;
1092 value
->namelen
= name
->len
;
1098 /* set a key value */
1099 static void set_value( struct key
*key
, const struct unicode_str
*name
,
1100 int type
, const void *data
, data_size_t len
)
1102 struct key_value
*value
;
1106 if ((value
= find_value( key
, name
, &index
)))
1108 /* check if the new value is identical to the existing one */
1109 if (value
->type
== type
&& value
->len
== len
&&
1110 value
->data
&& !memcmp( value
->data
, data
, len
))
1112 if (debug_level
> 1) dump_operation( key
, value
, "Skip setting" );
1117 if (key
->flags
& KEY_SYMLINK
)
1119 if (type
!= REG_LINK
|| name
->len
!= symlink_str
.len
||
1120 memicmpW( name
->str
, symlink_str
.str
, name
->len
/ sizeof(WCHAR
) ))
1122 set_error( STATUS_ACCESS_DENIED
);
1127 if (len
&& !(ptr
= memdup( data
, len
))) return;
1131 if (!(value
= insert_value( key
, name
, index
)))
1137 else free( value
->data
); /* already existing, free previous data */
1142 touch_key( key
, REG_NOTIFY_CHANGE_LAST_SET
);
1143 if (debug_level
> 1) dump_operation( key
, value
, "Set" );
1146 /* get a key value */
1147 static void get_value( struct key
*key
, const struct unicode_str
*name
, int *type
, data_size_t
*len
)
1149 struct key_value
*value
;
1152 if ((value
= find_value( key
, name
, &index
)))
1154 *type
= value
->type
;
1156 if (value
->data
) set_reply_data( value
->data
, min( value
->len
, get_reply_max_size() ));
1157 if (debug_level
> 1) dump_operation( key
, value
, "Get" );
1162 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
1166 /* enumerate a key value */
1167 static void enum_value( struct key
*key
, int i
, int info_class
, struct enum_key_value_reply
*reply
)
1169 struct key_value
*value
;
1171 if (i
< 0 || i
> key
->last_value
) set_error( STATUS_NO_MORE_ENTRIES
);
1175 data_size_t namelen
, maxlen
;
1177 value
= &key
->values
[i
];
1178 reply
->type
= value
->type
;
1179 namelen
= value
->namelen
;
1183 case KeyValueBasicInformation
:
1184 reply
->total
= namelen
;
1186 case KeyValueFullInformation
:
1187 reply
->total
= namelen
+ value
->len
;
1189 case KeyValuePartialInformation
:
1190 reply
->total
= value
->len
;
1194 set_error( STATUS_INVALID_PARAMETER
);
1198 maxlen
= min( reply
->total
, get_reply_max_size() );
1199 if (maxlen
&& ((data
= set_reply_data_size( maxlen
))))
1201 if (maxlen
> namelen
)
1203 reply
->namelen
= namelen
;
1204 memcpy( data
, value
->name
, namelen
);
1205 memcpy( (char *)data
+ namelen
, value
->data
, maxlen
- namelen
);
1209 reply
->namelen
= maxlen
;
1210 memcpy( data
, value
->name
, maxlen
);
1213 if (debug_level
> 1) dump_operation( key
, value
, "Enum" );
1217 /* delete a value */
1218 static void delete_value( struct key
*key
, const struct unicode_str
*name
)
1220 struct key_value
*value
;
1221 int i
, index
, nb_values
;
1223 if (!(value
= find_value( key
, name
, &index
)))
1225 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
1228 if (debug_level
> 1) dump_operation( key
, value
, "Delete" );
1229 free( value
->name
);
1230 free( value
->data
);
1231 for (i
= index
; i
< key
->last_value
; i
++) key
->values
[i
] = key
->values
[i
+ 1];
1233 touch_key( key
, REG_NOTIFY_CHANGE_LAST_SET
);
1235 /* try to shrink the array */
1236 nb_values
= key
->nb_values
;
1237 if (nb_values
> MIN_VALUES
&& key
->last_value
< nb_values
/ 2)
1239 struct key_value
*new_val
;
1240 nb_values
-= nb_values
/ 3; /* shrink by 33% */
1241 if (nb_values
< MIN_VALUES
) nb_values
= MIN_VALUES
;
1242 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) ))) return;
1243 key
->values
= new_val
;
1244 key
->nb_values
= nb_values
;
1248 /* get the registry key corresponding to an hkey handle */
1249 static struct key
*get_hkey_obj( obj_handle_t hkey
, unsigned int access
)
1251 struct key
*key
= (struct key
*)get_handle_obj( current
->process
, hkey
, access
, &key_ops
);
1253 if (key
&& key
->flags
& KEY_DELETED
)
1255 set_error( STATUS_KEY_DELETED
);
1256 release_object( key
);
1262 /* get the registry key corresponding to a parent key handle */
1263 static inline struct key
*get_parent_hkey_obj( obj_handle_t hkey
)
1265 if (!hkey
) return (struct key
*)grab_object( root_key
);
1266 return get_hkey_obj( hkey
, 0 );
1269 /* read a line from the input file */
1270 static int read_next_line( struct file_load_info
*info
)
1273 int newlen
, pos
= 0;
1278 if (!fgets( info
->buffer
+ pos
, info
->len
- pos
, info
->file
))
1279 return (pos
!= 0); /* EOF */
1280 pos
= strlen(info
->buffer
);
1281 if (info
->buffer
[pos
-1] == '\n')
1283 /* got a full line */
1284 info
->buffer
[--pos
] = 0;
1285 if (pos
> 0 && info
->buffer
[pos
-1] == '\r') info
->buffer
[pos
-1] = 0;
1288 if (pos
< info
->len
- 1) return 1; /* EOF but something was read */
1290 /* need to enlarge the buffer */
1291 newlen
= info
->len
+ info
->len
/ 2;
1292 if (!(newbuf
= realloc( info
->buffer
, newlen
)))
1294 set_error( STATUS_NO_MEMORY
);
1297 info
->buffer
= newbuf
;
1302 /* make sure the temp buffer holds enough space */
1303 static int get_file_tmp_space( struct file_load_info
*info
, size_t size
)
1306 if (info
->tmplen
>= size
) return 1;
1307 if (!(tmp
= realloc( info
->tmp
, size
)))
1309 set_error( STATUS_NO_MEMORY
);
1313 info
->tmplen
= size
;
1317 /* report an error while loading an input file */
1318 static void file_read_error( const char *err
, struct file_load_info
*info
)
1321 fprintf( stderr
, "%s:%d: %s '%s'\n", info
->filename
, info
->line
, err
, info
->buffer
);
1323 fprintf( stderr
, "<fd>:%d: %s '%s'\n", info
->line
, err
, info
->buffer
);
1326 /* convert a data type tag to a value type */
1327 static int get_data_type( const char *buffer
, int *type
, int *parse_type
)
1329 struct data_type
{ const char *tag
; int len
; int type
; int parse_type
; };
1331 static const struct data_type data_types
[] =
1332 { /* actual type */ /* type to assume for parsing */
1333 { "\"", 1, REG_SZ
, REG_SZ
},
1334 { "str:\"", 5, REG_SZ
, REG_SZ
},
1335 { "str(2):\"", 8, REG_EXPAND_SZ
, REG_SZ
},
1336 { "str(7):\"", 8, REG_MULTI_SZ
, REG_SZ
},
1337 { "hex:", 4, REG_BINARY
, REG_BINARY
},
1338 { "dword:", 6, REG_DWORD
, REG_DWORD
},
1339 { "hex(", 4, -1, REG_BINARY
},
1343 const struct data_type
*ptr
;
1346 for (ptr
= data_types
; ptr
->tag
; ptr
++)
1348 if (strncmp( ptr
->tag
, buffer
, ptr
->len
)) continue;
1349 *parse_type
= ptr
->parse_type
;
1350 if ((*type
= ptr
->type
) != -1) return ptr
->len
;
1351 /* "hex(xx):" is special */
1352 *type
= (int)strtoul( buffer
+ 4, &end
, 16 );
1353 if ((end
<= buffer
) || strncmp( end
, "):", 2 )) return 0;
1354 return end
+ 2 - buffer
;
1359 /* load and create a key from the input file */
1360 static struct key
*load_key( struct key
*base
, const char *buffer
, int prefix_len
,
1361 struct file_load_info
*info
, timeout_t
*modif
)
1364 struct unicode_str name
;
1369 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return NULL
;
1372 if ((res
= parse_strW( info
->tmp
, &len
, buffer
, ']' )) == -1)
1374 file_read_error( "Malformed key", info
);
1377 if (sscanf( buffer
+ res
, " %u", &mod
) == 1)
1378 *modif
= (timeout_t
)mod
* TICKS_PER_SEC
+ ticks_1601_to_1970
;
1380 *modif
= current_time
;
1383 while (prefix_len
&& *p
) { if (*p
++ == '\\') prefix_len
--; }
1389 file_read_error( "Malformed key", info
);
1392 /* empty key name, return base key */
1393 return (struct key
*)grab_object( base
);
1396 name
.len
= len
- (p
- info
->tmp
+ 1) * sizeof(WCHAR
);
1397 return create_key_recursive( base
, &name
, 0 );
1400 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1401 static void update_key_time( struct key
*key
, timeout_t modif
)
1403 while (key
&& !key
->modif
)
1410 /* load a global option from the input file */
1411 static int load_global_option( const char *buffer
, struct file_load_info
*info
)
1415 if (!strncmp( buffer
, "#arch=", 6 ))
1417 enum prefix_type type
;
1419 if (!strcmp( p
, "win32" )) type
= PREFIX_32BIT
;
1420 else if (!strcmp( p
, "win64" )) type
= PREFIX_64BIT
;
1423 file_read_error( "Unknown architecture", info
);
1424 set_error( STATUS_NOT_REGISTRY_FILE
);
1427 if (prefix_type
== PREFIX_UNKNOWN
) prefix_type
= type
;
1428 else if (type
!= prefix_type
)
1430 file_read_error( "Mismatched architecture", info
);
1431 set_error( STATUS_NOT_REGISTRY_FILE
);
1435 /* ignore unknown options */
1439 /* load a key option from the input file */
1440 static int load_key_option( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1445 if (!strncmp( buffer
, "#time=", 6 ))
1447 timeout_t modif
= 0;
1448 for (p
= buffer
+ 6; *p
; p
++)
1450 if (*p
>= '0' && *p
<= '9') modif
= (modif
<< 4) | (*p
- '0');
1451 else if (*p
>= 'A' && *p
<= 'F') modif
= (modif
<< 4) | (*p
- 'A' + 10);
1452 else if (*p
>= 'a' && *p
<= 'f') modif
= (modif
<< 4) | (*p
- 'a' + 10);
1455 update_key_time( key
, modif
);
1457 if (!strncmp( buffer
, "#class=", 7 ))
1460 if (*p
++ != '"') return 0;
1461 if (!get_file_tmp_space( info
, strlen(p
) * sizeof(WCHAR
) )) return 0;
1463 if (parse_strW( info
->tmp
, &len
, p
, '\"' ) == -1) return 0;
1465 if (!(key
->class = memdup( info
->tmp
, len
))) len
= 0;
1466 key
->classlen
= len
;
1468 if (!strncmp( buffer
, "#link", 5 )) key
->flags
|= KEY_SYMLINK
;
1469 /* ignore unknown options */
1473 /* parse a comma-separated list of hex digits */
1474 static int parse_hex( unsigned char *dest
, data_size_t
*len
, const char *buffer
)
1476 const char *p
= buffer
;
1477 data_size_t count
= 0;
1480 while (isxdigit(*p
))
1482 unsigned int val
= strtoul( p
, &end
, 16 );
1483 if (end
== p
|| val
> 0xff) return -1;
1484 if (count
++ >= *len
) return -1; /* dest buffer overflow */
1487 while (isspace(*p
)) p
++;
1489 while (isspace(*p
)) p
++;
1495 /* parse a value name and create the corresponding value */
1496 static struct key_value
*parse_value_name( struct key
*key
, const char *buffer
, data_size_t
*len
,
1497 struct file_load_info
*info
)
1499 struct key_value
*value
;
1500 struct unicode_str name
;
1503 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return NULL
;
1504 name
.str
= info
->tmp
;
1505 name
.len
= info
->tmplen
;
1506 if (buffer
[0] == '@')
1513 int r
= parse_strW( info
->tmp
, &name
.len
, buffer
+ 1, '\"' );
1514 if (r
== -1) goto error
;
1515 *len
= r
+ 1; /* for initial quote */
1516 name
.len
-= sizeof(WCHAR
); /* terminating null */
1518 while (isspace(buffer
[*len
])) (*len
)++;
1519 if (buffer
[*len
] != '=') goto error
;
1521 while (isspace(buffer
[*len
])) (*len
)++;
1522 if (!(value
= find_value( key
, &name
, &index
))) value
= insert_value( key
, &name
, index
);
1526 file_read_error( "Malformed value name", info
);
1530 /* load a value from the input file */
1531 static int load_value( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1535 int res
, type
, parse_type
;
1536 data_size_t maxlen
, len
;
1537 struct key_value
*value
;
1539 if (!(value
= parse_value_name( key
, buffer
, &len
, info
))) return 0;
1540 if (!(res
= get_data_type( buffer
+ len
, &type
, &parse_type
))) goto error
;
1541 buffer
+= len
+ res
;
1546 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return 0;
1548 if ((res
= parse_strW( info
->tmp
, &len
, buffer
, '\"' )) == -1) goto error
;
1552 dw
= strtoul( buffer
, NULL
, 16 );
1556 case REG_BINARY
: /* hex digits */
1560 maxlen
= 1 + strlen(buffer
) / 2; /* at least 2 chars for one hex byte */
1561 if (!get_file_tmp_space( info
, len
+ maxlen
)) return 0;
1562 if ((res
= parse_hex( (unsigned char *)info
->tmp
+ len
, &maxlen
, buffer
)) == -1) goto error
;
1565 while (isspace(*buffer
)) buffer
++;
1566 if (!*buffer
) break;
1567 if (*buffer
!= '\\') goto error
;
1568 if (read_next_line( info
) != 1) goto error
;
1569 buffer
= info
->buffer
;
1570 while (isspace(*buffer
)) buffer
++;
1576 ptr
= NULL
; /* keep compiler quiet */
1580 if (!len
) newptr
= NULL
;
1581 else if (!(newptr
= memdup( ptr
, len
))) return 0;
1583 free( value
->data
);
1584 value
->data
= newptr
;
1590 file_read_error( "Malformed value", info
);
1591 free( value
->data
);
1594 value
->type
= REG_NONE
;
1598 /* return the length (in path elements) of name that is part of the key name */
1599 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1600 static int get_prefix_len( struct key
*key
, const char *name
, struct file_load_info
*info
)
1606 if (!get_file_tmp_space( info
, strlen(name
) * sizeof(WCHAR
) )) return 0;
1609 if ((res
= parse_strW( info
->tmp
, &len
, name
, ']' )) == -1)
1611 file_read_error( "Malformed key", info
);
1614 for (p
= info
->tmp
; *p
; p
++) if (*p
== '\\') break;
1615 len
= (p
- info
->tmp
) * sizeof(WCHAR
);
1616 for (res
= 1; key
!= root_key
; res
++)
1618 if (len
== key
->namelen
&& !memicmpW( info
->tmp
, key
->name
, len
/ sizeof(WCHAR
) )) break;
1621 if (key
== root_key
) res
= 0; /* no matching name */
1625 /* load all the keys from the input file */
1626 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1627 static void load_keys( struct key
*key
, const char *filename
, FILE *f
, int prefix_len
)
1629 struct key
*subkey
= NULL
;
1630 struct file_load_info info
;
1631 timeout_t modif
= current_time
;
1634 info
.filename
= filename
;
1639 if (!(info
.buffer
= mem_alloc( info
.len
))) return;
1640 if (!(info
.tmp
= mem_alloc( info
.tmplen
)))
1642 free( info
.buffer
);
1646 if ((read_next_line( &info
) != 1) ||
1647 strcmp( info
.buffer
, "WINE REGISTRY Version 2" ))
1649 set_error( STATUS_NOT_REGISTRY_FILE
);
1653 while (read_next_line( &info
) == 1)
1656 while (*p
&& isspace(*p
)) p
++;
1659 case '[': /* new key */
1662 update_key_time( subkey
, modif
);
1663 release_object( subkey
);
1665 if (prefix_len
== -1) prefix_len
= get_prefix_len( key
, p
+ 1, &info
);
1666 if (!(subkey
= load_key( key
, p
+ 1, prefix_len
, &info
, &modif
)))
1667 file_read_error( "Error creating key", &info
);
1669 case '@': /* default value */
1670 case '\"': /* value */
1671 if (subkey
) load_value( subkey
, p
, &info
);
1672 else file_read_error( "Value without key", &info
);
1674 case '#': /* option */
1675 if (subkey
) load_key_option( subkey
, p
, &info
);
1676 else if (!load_global_option( p
, &info
)) goto done
;
1678 case ';': /* comment */
1679 case 0: /* empty line */
1682 file_read_error( "Unrecognized input", &info
);
1690 update_key_time( subkey
, modif
);
1691 release_object( subkey
);
1693 free( info
.buffer
);
1697 /* load a part of the registry from a file */
1698 static void load_registry( struct key
*key
, obj_handle_t handle
)
1703 if (!(file
= get_file_obj( current
->process
, handle
, FILE_READ_DATA
))) return;
1704 fd
= dup( get_file_unix_fd( file
) );
1705 release_object( file
);
1708 FILE *f
= fdopen( fd
, "r" );
1711 load_keys( key
, NULL
, f
, -1 );
1714 else file_set_error();
1718 /* load one of the initial registry files */
1719 static int load_init_registry_from_file( const char *filename
, struct key
*key
)
1723 if ((f
= fopen( filename
, "r" )))
1725 load_keys( key
, filename
, f
, 0 );
1727 if (get_error() == STATUS_NOT_REGISTRY_FILE
)
1729 fprintf( stderr
, "%s is not a valid registry file\n", filename
);
1734 assert( save_branch_count
< MAX_SAVE_BRANCH_INFO
);
1736 save_branch_info
[save_branch_count
].path
= filename
;
1737 save_branch_info
[save_branch_count
++].key
= (struct key
*)grab_object( key
);
1738 make_object_static( &key
->obj
);
1742 static WCHAR
*format_user_registry_path( const SID
*sid
, struct unicode_str
*path
)
1744 static const WCHAR prefixW
[] = {'U','s','e','r','\\','S',0};
1745 static const WCHAR formatW
[] = {'-','%','u',0};
1746 WCHAR buffer
[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES
];
1750 strcpyW( p
, prefixW
);
1751 p
+= strlenW( prefixW
);
1752 p
+= sprintfW( p
, formatW
, sid
->Revision
);
1753 p
+= sprintfW( p
, formatW
, MAKELONG( MAKEWORD( sid
->IdentifierAuthority
.Value
[5],
1754 sid
->IdentifierAuthority
.Value
[4] ),
1755 MAKEWORD( sid
->IdentifierAuthority
.Value
[3],
1756 sid
->IdentifierAuthority
.Value
[2] )));
1757 for (i
= 0; i
< sid
->SubAuthorityCount
; i
++)
1758 p
+= sprintfW( p
, formatW
, sid
->SubAuthority
[i
] );
1760 path
->len
= (p
- buffer
) * sizeof(WCHAR
);
1761 path
->str
= p
= memdup( buffer
, path
->len
);
1765 /* get the cpu architectures that can be supported in the current prefix */
1766 unsigned int get_prefix_cpu_mask(void)
1768 /* Allowed server/client/prefix combinations:
1772 * server +------+------+ client
1774 * 32 +------+------+---
1775 * | fail | fail | 64
1776 * ---+------+------+---
1778 * 64 +------+------+---
1780 * ---+------+------+---
1782 switch (prefix_type
)
1785 /* 64-bit prefix requires 64-bit server */
1786 return sizeof(void *) > sizeof(int) ? ~0 : 0;
1789 return ~CPU_64BIT_MASK
; /* only 32-bit cpus supported on 32-bit prefix */
1793 /* registry initialisation */
1794 void init_registry(void)
1796 static const WCHAR HKLM
[] = { 'M','a','c','h','i','n','e' };
1797 static const WCHAR HKU_default
[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1798 static const WCHAR classes
[] = {'S','o','f','t','w','a','r','e','\\',
1799 'C','l','a','s','s','e','s','\\',
1800 'W','o','w','6','4','3','2','N','o','d','e'};
1801 static const struct unicode_str root_name
= { NULL
, 0 };
1802 static const struct unicode_str HKLM_name
= { HKLM
, sizeof(HKLM
) };
1803 static const struct unicode_str HKU_name
= { HKU_default
, sizeof(HKU_default
) };
1804 static const struct unicode_str classes_name
= { classes
, sizeof(classes
) };
1806 WCHAR
*current_user_path
;
1807 struct unicode_str current_user_str
;
1808 struct key
*key
, *hklm
, *hkcu
;
1811 /* switch to the config dir */
1813 if (fchdir( config_dir_fd
) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno
));
1815 /* create the root key */
1816 root_key
= alloc_key( &root_name
, current_time
);
1818 make_object_static( &root_key
->obj
);
1820 /* load system.reg into Registry\Machine */
1822 if (!(hklm
= create_key_recursive( root_key
, &HKLM_name
, current_time
)))
1823 fatal_error( "could not create Machine registry key\n" );
1825 if (!load_init_registry_from_file( "system.reg", hklm
))
1827 if ((p
= getenv( "WINEARCH" )) && !strcmp( p
, "win32" ))
1828 prefix_type
= PREFIX_32BIT
;
1830 prefix_type
= sizeof(void *) > sizeof(int) ? PREFIX_64BIT
: PREFIX_32BIT
;
1832 else if (prefix_type
== PREFIX_UNKNOWN
)
1833 prefix_type
= PREFIX_32BIT
;
1835 /* load userdef.reg into Registry\User\.Default */
1837 if (!(key
= create_key_recursive( root_key
, &HKU_name
, current_time
)))
1838 fatal_error( "could not create User\\.Default registry key\n" );
1840 load_init_registry_from_file( "userdef.reg", key
);
1841 release_object( key
);
1843 /* load user.reg into HKEY_CURRENT_USER */
1845 /* FIXME: match default user in token.c. should get from process token instead */
1846 current_user_path
= format_user_registry_path( security_local_user_sid
, ¤t_user_str
);
1847 if (!current_user_path
||
1848 !(hkcu
= create_key_recursive( root_key
, ¤t_user_str
, current_time
)))
1849 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1850 free( current_user_path
);
1851 load_init_registry_from_file( "user.reg", hkcu
);
1853 /* set the shared flag on Software\Classes\Wow6432Node */
1854 if (prefix_type
== PREFIX_64BIT
)
1856 if ((key
= create_key_recursive( hklm
, &classes_name
, current_time
)))
1858 key
->flags
|= KEY_WOWSHARE
;
1859 release_object( key
);
1861 /* FIXME: handle HKCU too */
1864 release_object( hklm
);
1865 release_object( hkcu
);
1867 /* start the periodic save timer */
1868 set_periodic_save_timer();
1870 /* create windows directories */
1872 if (!mkdir( "drive_c/windows", 0777 ))
1874 mkdir( "drive_c/windows/system32", 0777 );
1875 if (prefix_type
== PREFIX_64BIT
) mkdir( "drive_c/windows/syswow64", 0777 );
1878 /* go back to the server dir */
1879 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
1882 /* save a registry branch to a file */
1883 static void save_all_subkeys( struct key
*key
, FILE *f
)
1885 fprintf( f
, "WINE REGISTRY Version 2\n" );
1886 fprintf( f
, ";; All keys relative to " );
1887 dump_path( key
, NULL
, f
);
1889 switch (prefix_type
)
1892 fprintf( f
, "\n#arch=win32\n" );
1895 fprintf( f
, "\n#arch=win64\n" );
1900 save_subkeys( key
, key
, f
);
1903 /* save a registry branch to a file handle */
1904 static void save_registry( struct key
*key
, obj_handle_t handle
)
1909 if (!(file
= get_file_obj( current
->process
, handle
, FILE_WRITE_DATA
))) return;
1910 fd
= dup( get_file_unix_fd( file
) );
1911 release_object( file
);
1914 FILE *f
= fdopen( fd
, "w" );
1917 save_all_subkeys( key
, f
);
1918 if (fclose( f
)) file_set_error();
1928 /* save a registry branch to a file */
1929 static int save_branch( struct key
*key
, const char *path
)
1932 char *p
, *tmp
= NULL
;
1933 int fd
, count
= 0, ret
= 0;
1936 if (!(key
->flags
& KEY_DIRTY
))
1938 if (debug_level
> 1) dump_operation( key
, NULL
, "Not saving clean" );
1942 /* test the file type */
1944 if ((fd
= open( path
, O_WRONLY
)) != -1)
1946 /* if file is not a regular file or has multiple links or is accessed
1947 * via symbolic links, write directly into it; otherwise use a temp file */
1948 if (!lstat( path
, &st
) && (!S_ISREG(st
.st_mode
) || st
.st_nlink
> 1))
1956 /* create a temp file in the same directory */
1958 if (!(tmp
= malloc( strlen(path
) + 20 ))) goto done
;
1959 strcpy( tmp
, path
);
1960 if ((p
= strrchr( tmp
, '/' ))) p
++;
1964 sprintf( p
, "reg%lx%04x.tmp", (long) getpid(), count
++ );
1965 if ((fd
= open( tmp
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666 )) != -1) break;
1966 if (errno
!= EEXIST
) goto done
;
1970 /* now save to it */
1973 if (!(f
= fdopen( fd
, "w" )))
1975 if (tmp
) unlink( tmp
);
1980 if (debug_level
> 1)
1982 fprintf( stderr
, "%s: ", path
);
1983 dump_operation( key
, NULL
, "saving" );
1986 save_all_subkeys( key
, f
);
1991 /* if successfully written, rename to final name */
1992 if (ret
) ret
= !rename( tmp
, path
);
1993 if (!ret
) unlink( tmp
);
1998 if (ret
) make_clean( key
);
2002 /* periodic saving of the registry */
2003 static void periodic_save( void *arg
)
2007 if (fchdir( config_dir_fd
) == -1) return;
2008 save_timeout_user
= NULL
;
2009 for (i
= 0; i
< save_branch_count
; i
++)
2010 save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
);
2011 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
2012 set_periodic_save_timer();
2015 /* start the periodic save timer */
2016 static void set_periodic_save_timer(void)
2018 if (save_timeout_user
) remove_timeout_user( save_timeout_user
);
2019 save_timeout_user
= add_timeout_user( save_period
, periodic_save
, NULL
);
2022 /* save the modified registry branches to disk */
2023 void flush_registry(void)
2027 if (fchdir( config_dir_fd
) == -1) return;
2028 for (i
= 0; i
< save_branch_count
; i
++)
2030 if (!save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
))
2032 fprintf( stderr
, "wineserver: could not save registry branch to %s",
2033 save_branch_info
[i
].path
);
2037 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
2040 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2041 static int is_wow64_thread( struct thread
*thread
)
2043 return (prefix_type
== PREFIX_64BIT
&& !(CPU_FLAG(thread
->process
->cpu
) & CPU_64BIT_MASK
));
2047 /* create a registry key */
2048 DECL_HANDLER(create_key
)
2050 struct key
*key
= NULL
, *parent
;
2051 struct unicode_str name
, class;
2052 unsigned int access
= req
->access
;
2053 const struct security_descriptor
*sd
;
2054 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
2056 if (!objattr
) return;
2058 if (!is_wow64_thread( current
)) access
= (access
& ~KEY_WOW64_32KEY
) | KEY_WOW64_64KEY
;
2060 class.str
= get_req_data_after_objattr( objattr
, &class.len
);
2061 class.len
= (class.len
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
2063 if (!objattr
->rootdir
&& name
.len
>= sizeof(root_name
) &&
2064 !memicmpW( name
.str
, root_name
, ARRAY_SIZE( root_name
)))
2066 name
.str
+= ARRAY_SIZE( root_name
);
2067 name
.len
-= sizeof(root_name
);
2070 /* NOTE: no access rights are required from the parent handle to create a key */
2071 if ((parent
= get_parent_hkey_obj( objattr
->rootdir
)))
2073 if ((key
= create_key( parent
, &name
, &class, req
->options
, access
,
2074 objattr
->attributes
, sd
, &reply
->created
)))
2076 reply
->hkey
= alloc_handle( current
->process
, key
, access
, objattr
->attributes
);
2077 release_object( key
);
2079 release_object( parent
);
2083 /* open a registry key */
2084 DECL_HANDLER(open_key
)
2086 struct key
*key
, *parent
;
2087 struct unicode_str name
;
2088 unsigned int access
= req
->access
;
2090 if (!is_wow64_thread( current
)) access
= (access
& ~KEY_WOW64_32KEY
) | KEY_WOW64_64KEY
;
2093 /* NOTE: no access rights are required to open the parent key, only the child key */
2094 if ((parent
= get_parent_hkey_obj( req
->parent
)))
2096 get_req_path( &name
, !req
->parent
);
2097 if ((key
= open_key( parent
, &name
, access
, req
->attributes
)))
2099 reply
->hkey
= alloc_handle( current
->process
, key
, access
, req
->attributes
);
2100 release_object( key
);
2102 release_object( parent
);
2106 /* delete a registry key */
2107 DECL_HANDLER(delete_key
)
2111 if ((key
= get_hkey_obj( req
->hkey
, DELETE
)))
2113 delete_key( key
, 0);
2114 release_object( key
);
2118 /* flush a registry key */
2119 DECL_HANDLER(flush_key
)
2121 struct key
*key
= get_hkey_obj( req
->hkey
, 0 );
2124 /* we don't need to do anything here with the current implementation */
2125 release_object( key
);
2129 /* enumerate registry subkeys */
2130 DECL_HANDLER(enum_key
)
2134 if ((key
= get_hkey_obj( req
->hkey
,
2135 req
->index
== -1 ? KEY_QUERY_VALUE
: KEY_ENUMERATE_SUB_KEYS
)))
2137 enum_key( key
, req
->index
, req
->info_class
, reply
);
2138 release_object( key
);
2142 /* set a value of a registry key */
2143 DECL_HANDLER(set_key_value
)
2146 struct unicode_str name
;
2148 if (req
->namelen
> get_req_data_size())
2150 set_error( STATUS_INVALID_PARAMETER
);
2153 name
.str
= get_req_data();
2154 name
.len
= (req
->namelen
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
2156 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
2158 data_size_t datalen
= get_req_data_size() - req
->namelen
;
2159 const char *data
= (const char *)get_req_data() + req
->namelen
;
2161 set_value( key
, &name
, req
->type
, data
, datalen
);
2162 release_object( key
);
2166 /* retrieve the value of a registry key */
2167 DECL_HANDLER(get_key_value
)
2170 struct unicode_str name
= get_req_unicode_str();
2173 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
2175 get_value( key
, &name
, &reply
->type
, &reply
->total
);
2176 release_object( key
);
2180 /* enumerate the value of a registry key */
2181 DECL_HANDLER(enum_key_value
)
2185 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
2187 enum_value( key
, req
->index
, req
->info_class
, reply
);
2188 release_object( key
);
2192 /* delete a value of a registry key */
2193 DECL_HANDLER(delete_key_value
)
2196 struct unicode_str name
= get_req_unicode_str();
2198 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
2200 delete_value( key
, &name
);
2201 release_object( key
);
2205 /* load a registry branch from a file */
2206 DECL_HANDLER(load_registry
)
2208 struct key
*key
, *parent
;
2209 struct unicode_str name
;
2210 const struct security_descriptor
*sd
;
2211 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
2213 if (!objattr
) return;
2215 if (!thread_single_check_privilege( current
, &SeRestorePrivilege
))
2217 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2221 if (!objattr
->rootdir
&& name
.len
>= sizeof(root_name
) &&
2222 !memicmpW( name
.str
, root_name
, ARRAY_SIZE( root_name
)))
2224 name
.str
+= ARRAY_SIZE( root_name
);
2225 name
.len
-= sizeof(root_name
);
2228 if ((parent
= get_parent_hkey_obj( objattr
->rootdir
)))
2231 if ((key
= create_key( parent
, &name
, NULL
, 0, KEY_WOW64_64KEY
, 0, sd
, &dummy
)))
2233 load_registry( key
, req
->file
);
2234 release_object( key
);
2236 release_object( parent
);
2240 DECL_HANDLER(unload_registry
)
2244 if (!thread_single_check_privilege( current
, &SeRestorePrivilege
))
2246 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2250 if ((key
= get_hkey_obj( req
->hkey
, 0 )))
2252 delete_key( key
, 1 ); /* FIXME */
2253 release_object( key
);
2257 /* save a registry branch to a file */
2258 DECL_HANDLER(save_registry
)
2262 if (!thread_single_check_privilege( current
, &SeBackupPrivilege
))
2264 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2268 if ((key
= get_hkey_obj( req
->hkey
, 0 )))
2270 save_registry( key
, req
->file
);
2271 release_object( key
);
2275 /* add a registry key change notification */
2276 DECL_HANDLER(set_registry_notification
)
2279 struct event
*event
;
2280 struct notify
*notify
;
2282 key
= get_hkey_obj( req
->hkey
, KEY_NOTIFY
);
2285 event
= get_event_obj( current
->process
, req
->event
, SYNCHRONIZE
);
2288 notify
= find_notify( key
, current
->process
, req
->hkey
);
2292 release_object( notify
->event
);
2293 grab_object( event
);
2294 notify
->event
= event
;
2298 notify
= mem_alloc( sizeof(*notify
) );
2301 grab_object( event
);
2302 notify
->event
= event
;
2303 notify
->subtree
= req
->subtree
;
2304 notify
->filter
= req
->filter
;
2305 notify
->hkey
= req
->hkey
;
2306 notify
->process
= current
->process
;
2307 list_add_head( &key
->notify_list
, ¬ify
->entry
);
2312 reset_event( event
);
2313 set_error( STATUS_PENDING
);
2315 release_object( event
);
2317 release_object( key
);