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
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 */
87 #define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
88 #define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
93 WCHAR
*name
; /* value name */
94 unsigned short namelen
; /* length of value name */
95 unsigned int type
; /* value type */
96 data_size_t len
; /* value data length in bytes */
97 void *data
; /* pointer to value data */
100 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
101 #define MIN_VALUES 8 /* min. number of allocated values per key */
103 #define MAX_NAME_LEN 256 /* max. length of a key name */
104 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
106 /* the root of the registry tree */
107 static struct key
*root_key
;
109 static const timeout_t ticks_1601_to_1970
= (timeout_t
)86400 * (369 * 365 + 89) * TICKS_PER_SEC
;
110 static const timeout_t save_period
= 30 * -TICKS_PER_SEC
; /* delay between periodic saves */
111 static struct timeout_user
*save_timeout_user
; /* saving timer */
112 static enum prefix_type
{ PREFIX_UNKNOWN
, PREFIX_32BIT
, PREFIX_64BIT
} prefix_type
;
114 static const WCHAR root_name
[] = { '\\','R','e','g','i','s','t','r','y','\\' };
115 static const WCHAR wow6432node
[] = {'W','o','w','6','4','3','2','N','o','d','e'};
116 static const WCHAR symlink_value
[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
117 static const struct unicode_str symlink_str
= { symlink_value
, sizeof(symlink_value
) };
119 static void set_periodic_save_timer(void);
120 static struct key_value
*find_value( const struct key
*key
, const struct unicode_str
*name
, int *index
);
122 /* information about where to save a registry branch */
123 struct save_branch_info
129 #define MAX_SAVE_BRANCH_INFO 3
130 static int save_branch_count
;
131 static struct save_branch_info save_branch_info
[MAX_SAVE_BRANCH_INFO
];
134 /* information about a file being loaded */
135 struct file_load_info
137 const char *filename
; /* input file name */
138 FILE *file
; /* input file */
139 char *buffer
; /* line buffer */
140 int len
; /* buffer length */
141 int line
; /* current input line */
142 WCHAR
*tmp
; /* temp buffer to use while parsing input */
143 size_t tmplen
; /* length of temp buffer */
147 static void key_dump( struct object
*obj
, int verbose
);
148 static struct object_type
*key_get_type( struct object
*obj
);
149 static unsigned int key_map_access( struct object
*obj
, unsigned int access
);
150 static struct security_descriptor
*key_get_sd( struct object
*obj
);
151 static WCHAR
*key_get_full_name( struct object
*obj
, data_size_t
*len
);
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 key_get_full_name
, /* get_full_name */
170 no_lookup_name
, /* lookup_name */
171 no_link_name
, /* link_name */
172 NULL
, /* unlink_name */
173 no_open_file
, /* open_file */
174 no_kernel_obj_list
, /* get_kernel_obj_list */
175 key_close_handle
, /* close_handle */
176 key_destroy
/* destroy */
180 static inline int is_wow6432node( const WCHAR
*name
, unsigned int len
)
182 return (len
== sizeof(wow6432node
) && !memicmp_strW( name
, wow6432node
, sizeof( 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
, 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
, 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
, 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
, 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 static WCHAR
*key_get_full_name( struct object
*obj
, data_size_t
*ret_len
)
400 static const WCHAR backslash
= '\\';
401 struct key
*key
= (struct key
*) obj
;
402 data_size_t len
= sizeof(root_name
) - sizeof(WCHAR
);
405 for (key
= (struct key
*)obj
; key
!= root_key
; key
= key
->parent
) len
+= key
->namelen
+ sizeof(WCHAR
);
406 if (!(ret
= malloc( len
))) return NULL
;
409 key
= (struct key
*)obj
;
410 for (key
= (struct key
*)obj
; key
!= root_key
; key
= key
->parent
)
412 memcpy( ret
+ len
- key
->namelen
, key
->name
, key
->namelen
);
413 len
-= key
->namelen
+ sizeof(WCHAR
);
414 memcpy( ret
+ len
, &backslash
, sizeof(WCHAR
) );
416 memcpy( ret
, root_name
, sizeof(root_name
) - sizeof(WCHAR
) );
420 /* close the notification associated with a handle */
421 static int key_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
423 struct key
* key
= (struct key
*) obj
;
424 struct notify
*notify
= find_notify( key
, process
, handle
);
425 if (notify
) do_notification( key
, notify
, 1 );
426 return 1; /* ok to close */
429 static void key_destroy( struct object
*obj
)
433 struct key
*key
= (struct key
*)obj
;
434 assert( obj
->ops
== &key_ops
);
438 for (i
= 0; i
<= key
->last_value
; i
++)
440 free( key
->values
[i
].name
);
441 free( key
->values
[i
].data
);
444 for (i
= 0; i
<= key
->last_subkey
; i
++)
446 key
->subkeys
[i
]->parent
= NULL
;
447 release_object( key
->subkeys
[i
] );
449 free( key
->subkeys
);
450 /* unconditionally notify everything waiting on this key */
451 while ((ptr
= list_head( &key
->notify_list
)))
453 struct notify
*notify
= LIST_ENTRY( ptr
, struct notify
, entry
);
454 do_notification( key
, notify
, 1 );
458 /* get the request vararg as registry path */
459 static inline void get_req_path( struct unicode_str
*str
, int skip_root
)
461 str
->str
= get_req_data();
462 str
->len
= (get_req_data_size() / sizeof(WCHAR
)) * sizeof(WCHAR
);
464 if (skip_root
&& str
->len
>= sizeof(root_name
) && !memicmp_strW( str
->str
, root_name
, sizeof(root_name
) ))
466 str
->str
+= ARRAY_SIZE( root_name
);
467 str
->len
-= sizeof(root_name
);
471 /* return the next token in a given path */
472 /* token->str must point inside the path, or be NULL for the first call */
473 static struct unicode_str
*get_path_token( const struct unicode_str
*path
, struct unicode_str
*token
)
475 data_size_t i
= 0, len
= path
->len
/ sizeof(WCHAR
);
477 if (!token
->str
) /* first time */
479 /* path cannot start with a backslash */
480 if (len
&& path
->str
[0] == '\\')
482 set_error( STATUS_OBJECT_PATH_INVALID
);
488 i
= token
->str
- path
->str
;
489 i
+= token
->len
/ sizeof(WCHAR
);
490 while (i
< len
&& path
->str
[i
] == '\\') i
++;
492 token
->str
= path
->str
+ i
;
493 while (i
< len
&& path
->str
[i
] != '\\') i
++;
494 token
->len
= (path
->str
+ i
- token
->str
) * sizeof(WCHAR
);
498 /* allocate a key object */
499 static struct key
*alloc_key( const struct unicode_str
*name
, timeout_t modif
)
502 if ((key
= alloc_object( &key_ops
)))
506 key
->namelen
= name
->len
;
509 key
->last_subkey
= -1;
513 key
->last_value
= -1;
517 list_init( &key
->notify_list
);
518 if (name
->len
&& !(key
->name
= memdup( name
->str
, name
->len
)))
520 release_object( key
);
527 /* mark a key and all its parents as dirty (modified) */
528 static void make_dirty( struct key
*key
)
532 if (key
->flags
& (KEY_DIRTY
|KEY_VOLATILE
)) return; /* nothing to do */
533 key
->flags
|= KEY_DIRTY
;
538 /* mark a key and all its subkeys as clean (not modified) */
539 static void make_clean( struct key
*key
)
543 if (key
->flags
& KEY_VOLATILE
) return;
544 if (!(key
->flags
& KEY_DIRTY
)) return;
545 key
->flags
&= ~KEY_DIRTY
;
546 for (i
= 0; i
<= key
->last_subkey
; i
++) make_clean( key
->subkeys
[i
] );
549 /* go through all the notifications and send them if necessary */
550 static void check_notify( struct key
*key
, unsigned int change
, int not_subtree
)
552 struct list
*ptr
, *next
;
554 LIST_FOR_EACH_SAFE( ptr
, next
, &key
->notify_list
)
556 struct notify
*n
= LIST_ENTRY( ptr
, struct notify
, entry
);
557 if ( ( not_subtree
|| n
->subtree
) && ( change
& n
->filter
) )
558 do_notification( key
, n
, 0 );
562 /* update key modification time */
563 static void touch_key( struct key
*key
, unsigned int change
)
567 key
->modif
= current_time
;
570 /* do notifications */
571 check_notify( key
, change
, 1 );
572 for ( k
= key
->parent
; k
; k
= k
->parent
)
573 check_notify( k
, change
, 0 );
576 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
577 static int grow_subkeys( struct key
*key
)
579 struct key
**new_subkeys
;
584 nb_subkeys
= key
->nb_subkeys
+ (key
->nb_subkeys
/ 2); /* grow by 50% */
585 if (!(new_subkeys
= realloc( key
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) )))
587 set_error( STATUS_NO_MEMORY
);
593 nb_subkeys
= MIN_SUBKEYS
;
594 if (!(new_subkeys
= mem_alloc( nb_subkeys
* sizeof(*new_subkeys
) ))) return 0;
596 key
->subkeys
= new_subkeys
;
597 key
->nb_subkeys
= nb_subkeys
;
601 /* allocate a subkey for a given key, and return its index */
602 static struct key
*alloc_subkey( struct key
*parent
, const struct unicode_str
*name
,
603 int index
, timeout_t modif
)
608 if (name
->len
> MAX_NAME_LEN
* sizeof(WCHAR
))
610 set_error( STATUS_INVALID_PARAMETER
);
613 if (parent
->last_subkey
+ 1 == parent
->nb_subkeys
)
615 /* need to grow the array */
616 if (!grow_subkeys( parent
)) return NULL
;
618 if ((key
= alloc_key( name
, modif
)) != NULL
)
620 key
->parent
= parent
;
621 for (i
= ++parent
->last_subkey
; i
> index
; i
--)
622 parent
->subkeys
[i
] = parent
->subkeys
[i
-1];
623 parent
->subkeys
[index
] = key
;
624 if (is_wow6432node( key
->name
, key
->namelen
) && !is_wow6432node( parent
->name
, parent
->namelen
))
625 parent
->flags
|= KEY_WOW64
;
630 /* free a subkey of a given key */
631 static void free_subkey( struct key
*parent
, int index
)
636 assert( index
>= 0 );
637 assert( index
<= parent
->last_subkey
);
639 key
= parent
->subkeys
[index
];
640 for (i
= index
; i
< parent
->last_subkey
; i
++) parent
->subkeys
[i
] = parent
->subkeys
[i
+ 1];
641 parent
->last_subkey
--;
642 key
->flags
|= KEY_DELETED
;
644 if (is_wow6432node( key
->name
, key
->namelen
)) parent
->flags
&= ~KEY_WOW64
;
645 release_object( key
);
647 /* try to shrink the array */
648 nb_subkeys
= parent
->nb_subkeys
;
649 if (nb_subkeys
> MIN_SUBKEYS
&& parent
->last_subkey
< nb_subkeys
/ 2)
651 struct key
**new_subkeys
;
652 nb_subkeys
-= nb_subkeys
/ 3; /* shrink by 33% */
653 if (nb_subkeys
< MIN_SUBKEYS
) nb_subkeys
= MIN_SUBKEYS
;
654 if (!(new_subkeys
= realloc( parent
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) ))) return;
655 parent
->subkeys
= new_subkeys
;
656 parent
->nb_subkeys
= nb_subkeys
;
660 /* find the named child of a given key and return its index */
661 static struct key
*find_subkey( const struct key
*key
, const struct unicode_str
*name
, int *index
)
663 int i
, min
, max
, res
;
667 max
= key
->last_subkey
;
671 len
= min( key
->subkeys
[i
]->namelen
, name
->len
);
672 res
= memicmp_strW( key
->subkeys
[i
]->name
, name
->str
, len
);
673 if (!res
) res
= key
->subkeys
[i
]->namelen
- name
->len
;
677 return key
->subkeys
[i
];
679 if (res
> 0) max
= i
- 1;
682 *index
= min
; /* this is where we should insert it */
686 /* return the wow64 variant of the key, or the key itself if none */
687 static struct key
*find_wow64_subkey( struct key
*key
, const struct unicode_str
*name
)
689 static const struct unicode_str wow6432node_str
= { wow6432node
, sizeof(wow6432node
) };
692 if (!(key
->flags
& KEY_WOW64
)) return key
;
693 if (!is_wow6432node( name
->str
, name
->len
))
695 key
= find_subkey( key
, &wow6432node_str
, &index
);
696 assert( key
); /* if KEY_WOW64 is set we must find it */
702 /* follow a symlink and return the resolved key */
703 static struct key
*follow_symlink( struct key
*key
, int iteration
)
705 struct unicode_str path
, token
;
706 struct key_value
*value
;
709 if (iteration
> 16) return NULL
;
710 if (!(key
->flags
& KEY_SYMLINK
)) return key
;
711 if (!(value
= find_value( key
, &symlink_str
, &index
))) return NULL
;
713 path
.str
= value
->data
;
714 path
.len
= (value
->len
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
715 if (path
.len
<= sizeof(root_name
)) return NULL
;
716 if (memicmp_strW( path
.str
, root_name
, sizeof(root_name
) )) return NULL
;
717 path
.str
+= ARRAY_SIZE( root_name
);
718 path
.len
-= sizeof(root_name
);
722 if (!get_path_token( &path
, &token
)) return NULL
;
725 if (!(key
= find_subkey( key
, &token
, &index
))) break;
726 if (!(key
= follow_symlink( key
, iteration
+ 1 ))) break;
727 get_path_token( &path
, &token
);
732 /* open a key until we find an element that doesn't exist */
733 /* helper for open_key and create_key */
734 static struct key
*open_key_prefix( struct key
*key
, const struct unicode_str
*name
,
735 unsigned int access
, struct unicode_str
*token
, int *index
)
738 if (!get_path_token( name
, token
)) return NULL
;
739 if (access
& KEY_WOW64_32KEY
) key
= find_wow64_subkey( key
, token
);
743 if (!(subkey
= find_subkey( key
, token
, index
)))
745 if ((key
->flags
& KEY_WOWSHARE
) && !(access
& KEY_WOW64_64KEY
))
747 /* try in the 64-bit parent */
749 subkey
= find_subkey( key
, token
, index
);
754 get_path_token( name
, token
);
755 if (!token
->len
) break;
756 if (!(access
& KEY_WOW64_64KEY
)) key
= find_wow64_subkey( key
, token
);
757 if (!(key
= follow_symlink( key
, 0 )))
759 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
767 static struct key
*open_key( struct key
*key
, const struct unicode_str
*name
, unsigned int access
,
768 unsigned int attributes
)
771 struct unicode_str token
;
773 if (!(key
= open_key_prefix( key
, name
, access
, &token
, &index
))) return NULL
;
777 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
780 if (!(access
& KEY_WOW64_64KEY
)) key
= find_wow64_subkey( key
, &token
);
781 if (!(attributes
& OBJ_OPENLINK
) && !(key
= follow_symlink( key
, 0 )))
783 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
786 if (debug_level
> 1) dump_operation( key
, NULL
, "Open" );
791 /* create a subkey */
792 static struct key
*create_key( struct key
*key
, const struct unicode_str
*name
,
793 const struct unicode_str
*class, unsigned int options
,
794 unsigned int access
, unsigned int attributes
,
795 const struct security_descriptor
*sd
, int *created
)
798 struct unicode_str token
, next
;
801 if (!(key
= open_key_prefix( key
, name
, access
, &token
, &index
))) return NULL
;
803 if (!token
.len
) /* the key already exists */
805 if (!(access
& KEY_WOW64_64KEY
)) key
= find_wow64_subkey( key
, &token
);
806 if (options
& REG_OPTION_CREATE_LINK
)
808 set_error( STATUS_OBJECT_NAME_COLLISION
);
811 if (!(attributes
& OBJ_OPENLINK
) && !(key
= follow_symlink( key
, 0 )))
813 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
816 if (debug_level
> 1) dump_operation( key
, NULL
, "Open" );
821 /* token must be the last path component at this point */
823 get_path_token( name
, &next
);
826 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
830 if ((key
->flags
& KEY_VOLATILE
) && !(options
& REG_OPTION_VOLATILE
))
832 set_error( STATUS_CHILD_MUST_BE_VOLATILE
);
837 if (!(key
= alloc_subkey( key
, &token
, index
, current_time
))) return NULL
;
839 if (options
& REG_OPTION_CREATE_LINK
) key
->flags
|= KEY_SYMLINK
;
840 if (options
& REG_OPTION_VOLATILE
) key
->flags
|= KEY_VOLATILE
;
841 else key
->flags
|= KEY_DIRTY
;
843 if (sd
) default_set_sd( &key
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
844 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
846 if (debug_level
> 1) dump_operation( key
, NULL
, "Create" );
847 if (class && class->len
)
849 key
->classlen
= class->len
;
851 if (!(key
->class = memdup( class->str
, key
->classlen
))) key
->classlen
= 0;
853 touch_key( key
->parent
, REG_NOTIFY_CHANGE_NAME
);
858 /* recursively create a subkey (for internal use only) */
859 static struct key
*create_key_recursive( struct key
*key
, const struct unicode_str
*name
, timeout_t modif
)
863 struct unicode_str token
;
866 if (!get_path_token( name
, &token
)) return NULL
;
870 if (!(subkey
= find_subkey( key
, &token
, &index
))) break;
872 if (!(key
= follow_symlink( key
, 0 )))
874 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
877 get_path_token( name
, &token
);
882 if (!(key
= alloc_subkey( key
, &token
, index
, modif
))) return NULL
;
886 get_path_token( name
, &token
);
887 if (!token
.len
) break;
888 /* we know the index is always 0 in a new key */
889 if (!(key
= alloc_subkey( key
, &token
, 0, modif
)))
891 free_subkey( base
, index
);
901 /* query information about a key or a subkey */
902 static void enum_key( struct key
*key
, int index
, int info_class
, struct enum_key_reply
*reply
)
905 data_size_t len
, namelen
, classlen
;
906 data_size_t max_subkey
= 0, max_class
= 0;
907 data_size_t max_value
= 0, max_data
= 0;
908 WCHAR
*fullname
= NULL
;
911 if (index
!= -1) /* -1 means use the specified key directly */
913 if ((index
< 0) || (index
> key
->last_subkey
))
915 set_error( STATUS_NO_MORE_ENTRIES
);
918 key
= key
->subkeys
[index
];
921 namelen
= key
->namelen
;
922 classlen
= key
->classlen
;
926 case KeyNameInformation
:
927 if (!(fullname
= key
->obj
.ops
->get_full_name( &key
->obj
, &namelen
))) return;
929 case KeyBasicInformation
:
930 classlen
= 0; /* only return the name */
932 case KeyNodeInformation
:
933 reply
->max_subkey
= 0;
934 reply
->max_class
= 0;
935 reply
->max_value
= 0;
938 case KeyFullInformation
:
939 case KeyCachedInformation
:
940 for (i
= 0; i
<= key
->last_subkey
; i
++)
942 if (key
->subkeys
[i
]->namelen
> max_subkey
) max_subkey
= key
->subkeys
[i
]->namelen
;
943 if (key
->subkeys
[i
]->classlen
> max_class
) max_class
= key
->subkeys
[i
]->classlen
;
945 for (i
= 0; i
<= key
->last_value
; i
++)
947 if (key
->values
[i
].namelen
> max_value
) max_value
= key
->values
[i
].namelen
;
948 if (key
->values
[i
].len
> max_data
) max_data
= key
->values
[i
].len
;
950 reply
->max_subkey
= max_subkey
;
951 reply
->max_class
= max_class
;
952 reply
->max_value
= max_value
;
953 reply
->max_data
= max_data
;
954 reply
->namelen
= namelen
;
955 if (info_class
== KeyCachedInformation
)
956 classlen
= 0; /* don't return any data, only its size */
957 namelen
= 0; /* don't return name */
960 set_error( STATUS_INVALID_PARAMETER
);
963 reply
->subkeys
= key
->last_subkey
+ 1;
964 reply
->values
= key
->last_value
+ 1;
965 reply
->modif
= key
->modif
;
966 reply
->total
= namelen
+ classlen
;
968 len
= min( reply
->total
, get_reply_max_size() );
969 if (len
&& (data
= set_reply_data_size( len
)))
973 reply
->namelen
= namelen
;
974 memcpy( data
, key
->name
, namelen
);
975 memcpy( data
+ namelen
, key
->class, len
- namelen
);
977 else if (info_class
== KeyNameInformation
)
979 reply
->namelen
= namelen
;
980 memcpy( data
, fullname
, len
);
984 reply
->namelen
= len
;
985 memcpy( data
, key
->name
, len
);
989 if (debug_level
> 1) dump_operation( key
, NULL
, "Enum" );
992 /* delete a key and its values */
993 static int delete_key( struct key
*key
, int recurse
)
996 struct key
*parent
= key
->parent
;
998 /* must find parent and index */
1001 set_error( STATUS_ACCESS_DENIED
);
1006 while (recurse
&& (key
->last_subkey
>=0))
1007 if (0 > delete_key(key
->subkeys
[key
->last_subkey
], 1))
1010 for (index
= 0; index
<= parent
->last_subkey
; index
++)
1011 if (parent
->subkeys
[index
] == key
) break;
1012 assert( index
<= parent
->last_subkey
);
1014 /* we can only delete a key that has no subkeys */
1015 if (key
->last_subkey
>= 0)
1017 set_error( STATUS_ACCESS_DENIED
);
1021 if (debug_level
> 1) dump_operation( key
, NULL
, "Delete" );
1022 free_subkey( parent
, index
);
1023 touch_key( parent
, REG_NOTIFY_CHANGE_NAME
);
1027 /* try to grow the array of values; return 1 if OK, 0 on error */
1028 static int grow_values( struct key
*key
)
1030 struct key_value
*new_val
;
1035 nb_values
= key
->nb_values
+ (key
->nb_values
/ 2); /* grow by 50% */
1036 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) )))
1038 set_error( STATUS_NO_MEMORY
);
1044 nb_values
= MIN_VALUES
;
1045 if (!(new_val
= mem_alloc( nb_values
* sizeof(*new_val
) ))) return 0;
1047 key
->values
= new_val
;
1048 key
->nb_values
= nb_values
;
1052 /* find the named value of a given key and return its index in the array */
1053 static struct key_value
*find_value( const struct key
*key
, const struct unicode_str
*name
, int *index
)
1055 int i
, min
, max
, res
;
1059 max
= key
->last_value
;
1062 i
= (min
+ max
) / 2;
1063 len
= min( key
->values
[i
].namelen
, name
->len
);
1064 res
= memicmp_strW( key
->values
[i
].name
, name
->str
, len
);
1065 if (!res
) res
= key
->values
[i
].namelen
- name
->len
;
1069 return &key
->values
[i
];
1071 if (res
> 0) max
= i
- 1;
1074 *index
= min
; /* this is where we should insert it */
1078 /* insert a new value; the index must have been returned by find_value */
1079 static struct key_value
*insert_value( struct key
*key
, const struct unicode_str
*name
, int index
)
1081 struct key_value
*value
;
1082 WCHAR
*new_name
= NULL
;
1085 if (name
->len
> MAX_VALUE_LEN
* sizeof(WCHAR
))
1087 set_error( STATUS_NAME_TOO_LONG
);
1090 if (key
->last_value
+ 1 == key
->nb_values
)
1092 if (!grow_values( key
)) return NULL
;
1094 if (name
->len
&& !(new_name
= memdup( name
->str
, name
->len
))) return NULL
;
1095 for (i
= ++key
->last_value
; i
> index
; i
--) key
->values
[i
] = key
->values
[i
- 1];
1096 value
= &key
->values
[index
];
1097 value
->name
= new_name
;
1098 value
->namelen
= name
->len
;
1104 /* set a key value */
1105 static void set_value( struct key
*key
, const struct unicode_str
*name
,
1106 int type
, const void *data
, data_size_t len
)
1108 struct key_value
*value
;
1112 if ((value
= find_value( key
, name
, &index
)))
1114 /* check if the new value is identical to the existing one */
1115 if (value
->type
== type
&& value
->len
== len
&&
1116 value
->data
&& !memcmp( value
->data
, data
, len
))
1118 if (debug_level
> 1) dump_operation( key
, value
, "Skip setting" );
1123 if (key
->flags
& KEY_SYMLINK
)
1125 if (type
!= REG_LINK
|| name
->len
!= symlink_str
.len
||
1126 memicmp_strW( name
->str
, symlink_str
.str
, name
->len
))
1128 set_error( STATUS_ACCESS_DENIED
);
1133 if (len
&& !(ptr
= memdup( data
, len
))) return;
1137 if (!(value
= insert_value( key
, name
, index
)))
1143 else free( value
->data
); /* already existing, free previous data */
1148 touch_key( key
, REG_NOTIFY_CHANGE_LAST_SET
);
1149 if (debug_level
> 1) dump_operation( key
, value
, "Set" );
1152 /* get a key value */
1153 static void get_value( struct key
*key
, const struct unicode_str
*name
, int *type
, data_size_t
*len
)
1155 struct key_value
*value
;
1158 if ((value
= find_value( key
, name
, &index
)))
1160 *type
= value
->type
;
1162 if (value
->data
) set_reply_data( value
->data
, min( value
->len
, get_reply_max_size() ));
1163 if (debug_level
> 1) dump_operation( key
, value
, "Get" );
1168 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
1172 /* enumerate a key value */
1173 static void enum_value( struct key
*key
, int i
, int info_class
, struct enum_key_value_reply
*reply
)
1175 struct key_value
*value
;
1177 if (i
< 0 || i
> key
->last_value
) set_error( STATUS_NO_MORE_ENTRIES
);
1181 data_size_t namelen
, maxlen
;
1183 value
= &key
->values
[i
];
1184 reply
->type
= value
->type
;
1185 namelen
= value
->namelen
;
1189 case KeyValueBasicInformation
:
1190 reply
->total
= namelen
;
1192 case KeyValueFullInformation
:
1193 reply
->total
= namelen
+ value
->len
;
1195 case KeyValuePartialInformation
:
1196 reply
->total
= value
->len
;
1200 set_error( STATUS_INVALID_PARAMETER
);
1204 maxlen
= min( reply
->total
, get_reply_max_size() );
1205 if (maxlen
&& ((data
= set_reply_data_size( maxlen
))))
1207 if (maxlen
> namelen
)
1209 reply
->namelen
= namelen
;
1210 memcpy( data
, value
->name
, namelen
);
1211 memcpy( (char *)data
+ namelen
, value
->data
, maxlen
- namelen
);
1215 reply
->namelen
= maxlen
;
1216 memcpy( data
, value
->name
, maxlen
);
1219 if (debug_level
> 1) dump_operation( key
, value
, "Enum" );
1223 /* delete a value */
1224 static void delete_value( struct key
*key
, const struct unicode_str
*name
)
1226 struct key_value
*value
;
1227 int i
, index
, nb_values
;
1229 if (!(value
= find_value( key
, name
, &index
)))
1231 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
1234 if (debug_level
> 1) dump_operation( key
, value
, "Delete" );
1235 free( value
->name
);
1236 free( value
->data
);
1237 for (i
= index
; i
< key
->last_value
; i
++) key
->values
[i
] = key
->values
[i
+ 1];
1239 touch_key( key
, REG_NOTIFY_CHANGE_LAST_SET
);
1241 /* try to shrink the array */
1242 nb_values
= key
->nb_values
;
1243 if (nb_values
> MIN_VALUES
&& key
->last_value
< nb_values
/ 2)
1245 struct key_value
*new_val
;
1246 nb_values
-= nb_values
/ 3; /* shrink by 33% */
1247 if (nb_values
< MIN_VALUES
) nb_values
= MIN_VALUES
;
1248 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) ))) return;
1249 key
->values
= new_val
;
1250 key
->nb_values
= nb_values
;
1254 /* get the registry key corresponding to an hkey handle */
1255 static struct key
*get_hkey_obj( obj_handle_t hkey
, unsigned int access
)
1257 struct key
*key
= (struct key
*)get_handle_obj( current
->process
, hkey
, access
, &key_ops
);
1259 if (key
&& key
->flags
& KEY_DELETED
)
1261 set_error( STATUS_KEY_DELETED
);
1262 release_object( key
);
1268 /* get the registry key corresponding to a parent key handle */
1269 static inline struct key
*get_parent_hkey_obj( obj_handle_t hkey
)
1271 if (!hkey
) return (struct key
*)grab_object( root_key
);
1272 return get_hkey_obj( hkey
, 0 );
1275 /* read a line from the input file */
1276 static int read_next_line( struct file_load_info
*info
)
1279 int newlen
, pos
= 0;
1284 if (!fgets( info
->buffer
+ pos
, info
->len
- pos
, info
->file
))
1285 return (pos
!= 0); /* EOF */
1286 pos
= strlen(info
->buffer
);
1287 if (info
->buffer
[pos
-1] == '\n')
1289 /* got a full line */
1290 info
->buffer
[--pos
] = 0;
1291 if (pos
> 0 && info
->buffer
[pos
-1] == '\r') info
->buffer
[pos
-1] = 0;
1294 if (pos
< info
->len
- 1) return 1; /* EOF but something was read */
1296 /* need to enlarge the buffer */
1297 newlen
= info
->len
+ info
->len
/ 2;
1298 if (!(newbuf
= realloc( info
->buffer
, newlen
)))
1300 set_error( STATUS_NO_MEMORY
);
1303 info
->buffer
= newbuf
;
1308 /* make sure the temp buffer holds enough space */
1309 static int get_file_tmp_space( struct file_load_info
*info
, size_t size
)
1312 if (info
->tmplen
>= size
) return 1;
1313 if (!(tmp
= realloc( info
->tmp
, size
)))
1315 set_error( STATUS_NO_MEMORY
);
1319 info
->tmplen
= size
;
1323 /* report an error while loading an input file */
1324 static void file_read_error( const char *err
, struct file_load_info
*info
)
1327 fprintf( stderr
, "%s:%d: %s '%s'\n", info
->filename
, info
->line
, err
, info
->buffer
);
1329 fprintf( stderr
, "<fd>:%d: %s '%s'\n", info
->line
, err
, info
->buffer
);
1332 /* convert a data type tag to a value type */
1333 static int get_data_type( const char *buffer
, int *type
, int *parse_type
)
1335 struct data_type
{ const char *tag
; int len
; int type
; int parse_type
; };
1337 static const struct data_type data_types
[] =
1338 { /* actual type */ /* type to assume for parsing */
1339 { "\"", 1, REG_SZ
, REG_SZ
},
1340 { "str:\"", 5, REG_SZ
, REG_SZ
},
1341 { "str(2):\"", 8, REG_EXPAND_SZ
, REG_SZ
},
1342 { "str(7):\"", 8, REG_MULTI_SZ
, REG_SZ
},
1343 { "hex:", 4, REG_BINARY
, REG_BINARY
},
1344 { "dword:", 6, REG_DWORD
, REG_DWORD
},
1345 { "hex(", 4, -1, REG_BINARY
},
1349 const struct data_type
*ptr
;
1352 for (ptr
= data_types
; ptr
->tag
; ptr
++)
1354 if (strncmp( ptr
->tag
, buffer
, ptr
->len
)) continue;
1355 *parse_type
= ptr
->parse_type
;
1356 if ((*type
= ptr
->type
) != -1) return ptr
->len
;
1357 /* "hex(xx):" is special */
1358 *type
= (int)strtoul( buffer
+ 4, &end
, 16 );
1359 if ((end
<= buffer
) || strncmp( end
, "):", 2 )) return 0;
1360 return end
+ 2 - buffer
;
1365 /* load and create a key from the input file */
1366 static struct key
*load_key( struct key
*base
, const char *buffer
, int prefix_len
,
1367 struct file_load_info
*info
, timeout_t
*modif
)
1370 struct unicode_str name
;
1375 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return NULL
;
1378 if ((res
= parse_strW( info
->tmp
, &len
, buffer
, ']' )) == -1)
1380 file_read_error( "Malformed key", info
);
1383 if (sscanf( buffer
+ res
, " %u", &mod
) == 1)
1384 *modif
= (timeout_t
)mod
* TICKS_PER_SEC
+ ticks_1601_to_1970
;
1386 *modif
= current_time
;
1389 while (prefix_len
&& *p
) { if (*p
++ == '\\') prefix_len
--; }
1395 file_read_error( "Malformed key", info
);
1398 /* empty key name, return base key */
1399 return (struct key
*)grab_object( base
);
1402 name
.len
= len
- (p
- info
->tmp
+ 1) * sizeof(WCHAR
);
1403 return create_key_recursive( base
, &name
, 0 );
1406 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1407 static void update_key_time( struct key
*key
, timeout_t modif
)
1409 while (key
&& !key
->modif
)
1416 /* load a global option from the input file */
1417 static int load_global_option( const char *buffer
, struct file_load_info
*info
)
1421 if (!strncmp( buffer
, "#arch=", 6 ))
1423 enum prefix_type type
;
1425 if (!strcmp( p
, "win32" )) type
= PREFIX_32BIT
;
1426 else if (!strcmp( p
, "win64" )) type
= PREFIX_64BIT
;
1429 file_read_error( "Unknown architecture", info
);
1430 set_error( STATUS_NOT_REGISTRY_FILE
);
1433 if (prefix_type
== PREFIX_UNKNOWN
) prefix_type
= type
;
1434 else if (type
!= prefix_type
)
1436 file_read_error( "Mismatched architecture", info
);
1437 set_error( STATUS_NOT_REGISTRY_FILE
);
1441 /* ignore unknown options */
1445 /* load a key option from the input file */
1446 static int load_key_option( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1451 if (!strncmp( buffer
, "#time=", 6 ))
1453 timeout_t modif
= 0;
1454 for (p
= buffer
+ 6; *p
; p
++)
1456 if (*p
>= '0' && *p
<= '9') modif
= (modif
<< 4) | (*p
- '0');
1457 else if (*p
>= 'A' && *p
<= 'F') modif
= (modif
<< 4) | (*p
- 'A' + 10);
1458 else if (*p
>= 'a' && *p
<= 'f') modif
= (modif
<< 4) | (*p
- 'a' + 10);
1461 update_key_time( key
, modif
);
1463 if (!strncmp( buffer
, "#class=", 7 ))
1466 if (*p
++ != '"') return 0;
1467 if (!get_file_tmp_space( info
, strlen(p
) * sizeof(WCHAR
) )) return 0;
1469 if (parse_strW( info
->tmp
, &len
, p
, '\"' ) == -1) return 0;
1471 if (!(key
->class = memdup( info
->tmp
, len
))) len
= 0;
1472 key
->classlen
= len
;
1474 if (!strncmp( buffer
, "#link", 5 )) key
->flags
|= KEY_SYMLINK
;
1475 /* ignore unknown options */
1479 /* parse a comma-separated list of hex digits */
1480 static int parse_hex( unsigned char *dest
, data_size_t
*len
, const char *buffer
)
1482 const char *p
= buffer
;
1483 data_size_t count
= 0;
1486 while (isxdigit(*p
))
1488 unsigned int val
= strtoul( p
, &end
, 16 );
1489 if (end
== p
|| val
> 0xff) return -1;
1490 if (count
++ >= *len
) return -1; /* dest buffer overflow */
1493 while (isspace(*p
)) p
++;
1495 while (isspace(*p
)) p
++;
1501 /* parse a value name and create the corresponding value */
1502 static struct key_value
*parse_value_name( struct key
*key
, const char *buffer
, data_size_t
*len
,
1503 struct file_load_info
*info
)
1505 struct key_value
*value
;
1506 struct unicode_str name
;
1509 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return NULL
;
1510 name
.str
= info
->tmp
;
1511 name
.len
= info
->tmplen
;
1512 if (buffer
[0] == '@')
1519 int r
= parse_strW( info
->tmp
, &name
.len
, buffer
+ 1, '\"' );
1520 if (r
== -1) goto error
;
1521 *len
= r
+ 1; /* for initial quote */
1522 name
.len
-= sizeof(WCHAR
); /* terminating null */
1524 while (isspace(buffer
[*len
])) (*len
)++;
1525 if (buffer
[*len
] != '=') goto error
;
1527 while (isspace(buffer
[*len
])) (*len
)++;
1528 if (!(value
= find_value( key
, &name
, &index
))) value
= insert_value( key
, &name
, index
);
1532 file_read_error( "Malformed value name", info
);
1536 /* load a value from the input file */
1537 static int load_value( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1541 int res
, type
, parse_type
;
1542 data_size_t maxlen
, len
;
1543 struct key_value
*value
;
1545 if (!(value
= parse_value_name( key
, buffer
, &len
, info
))) return 0;
1546 if (!(res
= get_data_type( buffer
+ len
, &type
, &parse_type
))) goto error
;
1547 buffer
+= len
+ res
;
1552 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return 0;
1554 if ((res
= parse_strW( info
->tmp
, &len
, buffer
, '\"' )) == -1) goto error
;
1558 dw
= strtoul( buffer
, NULL
, 16 );
1562 case REG_BINARY
: /* hex digits */
1566 maxlen
= 1 + strlen(buffer
) / 2; /* at least 2 chars for one hex byte */
1567 if (!get_file_tmp_space( info
, len
+ maxlen
)) return 0;
1568 if ((res
= parse_hex( (unsigned char *)info
->tmp
+ len
, &maxlen
, buffer
)) == -1) goto error
;
1571 while (isspace(*buffer
)) buffer
++;
1572 if (!*buffer
) break;
1573 if (*buffer
!= '\\') goto error
;
1574 if (read_next_line( info
) != 1) goto error
;
1575 buffer
= info
->buffer
;
1576 while (isspace(*buffer
)) buffer
++;
1582 ptr
= NULL
; /* keep compiler quiet */
1586 if (!len
) newptr
= NULL
;
1587 else if (!(newptr
= memdup( ptr
, len
))) return 0;
1589 free( value
->data
);
1590 value
->data
= newptr
;
1596 file_read_error( "Malformed value", info
);
1597 free( value
->data
);
1600 value
->type
= REG_NONE
;
1604 /* return the length (in path elements) of name that is part of the key name */
1605 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1606 static int get_prefix_len( struct key
*key
, const char *name
, struct file_load_info
*info
)
1612 if (!get_file_tmp_space( info
, strlen(name
) * sizeof(WCHAR
) )) return 0;
1615 if ((res
= parse_strW( info
->tmp
, &len
, name
, ']' )) == -1)
1617 file_read_error( "Malformed key", info
);
1620 for (p
= info
->tmp
; *p
; p
++) if (*p
== '\\') break;
1621 len
= (p
- info
->tmp
) * sizeof(WCHAR
);
1622 for (res
= 1; key
!= root_key
; res
++)
1624 if (len
== key
->namelen
&& !memicmp_strW( info
->tmp
, key
->name
, len
)) break;
1627 if (key
== root_key
) res
= 0; /* no matching name */
1631 /* load all the keys from the input file */
1632 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1633 static void load_keys( struct key
*key
, const char *filename
, FILE *f
, int prefix_len
)
1635 struct key
*subkey
= NULL
;
1636 struct file_load_info info
;
1637 timeout_t modif
= current_time
;
1640 info
.filename
= filename
;
1645 if (!(info
.buffer
= mem_alloc( info
.len
))) return;
1646 if (!(info
.tmp
= mem_alloc( info
.tmplen
)))
1648 free( info
.buffer
);
1652 if ((read_next_line( &info
) != 1) ||
1653 strcmp( info
.buffer
, "WINE REGISTRY Version 2" ))
1655 set_error( STATUS_NOT_REGISTRY_FILE
);
1659 while (read_next_line( &info
) == 1)
1662 while (*p
&& isspace(*p
)) p
++;
1665 case '[': /* new key */
1668 update_key_time( subkey
, modif
);
1669 release_object( subkey
);
1671 if (prefix_len
== -1) prefix_len
= get_prefix_len( key
, p
+ 1, &info
);
1672 if (!(subkey
= load_key( key
, p
+ 1, prefix_len
, &info
, &modif
)))
1673 file_read_error( "Error creating key", &info
);
1675 case '@': /* default value */
1676 case '\"': /* value */
1677 if (subkey
) load_value( subkey
, p
, &info
);
1678 else file_read_error( "Value without key", &info
);
1680 case '#': /* option */
1681 if (subkey
) load_key_option( subkey
, p
, &info
);
1682 else if (!load_global_option( p
, &info
)) goto done
;
1684 case ';': /* comment */
1685 case 0: /* empty line */
1688 file_read_error( "Unrecognized input", &info
);
1696 update_key_time( subkey
, modif
);
1697 release_object( subkey
);
1699 free( info
.buffer
);
1703 /* load a part of the registry from a file */
1704 static void load_registry( struct key
*key
, obj_handle_t handle
)
1709 if (!(file
= get_file_obj( current
->process
, handle
, FILE_READ_DATA
))) return;
1710 fd
= dup( get_file_unix_fd( file
) );
1711 release_object( file
);
1714 FILE *f
= fdopen( fd
, "r" );
1717 load_keys( key
, NULL
, f
, -1 );
1720 else file_set_error();
1724 /* load one of the initial registry files */
1725 static int load_init_registry_from_file( const char *filename
, struct key
*key
)
1729 if ((f
= fopen( filename
, "r" )))
1731 load_keys( key
, filename
, f
, 0 );
1733 if (get_error() == STATUS_NOT_REGISTRY_FILE
)
1735 fprintf( stderr
, "%s is not a valid registry file\n", filename
);
1740 assert( save_branch_count
< MAX_SAVE_BRANCH_INFO
);
1742 save_branch_info
[save_branch_count
].path
= filename
;
1743 save_branch_info
[save_branch_count
++].key
= (struct key
*)grab_object( key
);
1744 make_object_permanent( &key
->obj
);
1748 static WCHAR
*format_user_registry_path( const SID
*sid
, struct unicode_str
*path
)
1750 char buffer
[7 + 11 + 11 + 11 * SID_MAX_SUB_AUTHORITIES
], *p
= buffer
;
1753 p
+= sprintf( p
, "User\\S-%u-%u", sid
->Revision
,
1754 MAKELONG( MAKEWORD( sid
->IdentifierAuthority
.Value
[5],
1755 sid
->IdentifierAuthority
.Value
[4] ),
1756 MAKEWORD( sid
->IdentifierAuthority
.Value
[3],
1757 sid
->IdentifierAuthority
.Value
[2] )));
1758 for (i
= 0; i
< sid
->SubAuthorityCount
; i
++) p
+= sprintf( p
, "-%u", sid
->SubAuthority
[i
] );
1759 return ascii_to_unicode_str( buffer
, path
);
1762 /* get the cpu architectures that can be supported in the current prefix */
1763 unsigned int get_prefix_cpu_mask(void)
1765 /* Allowed server/client/prefix combinations:
1769 * server +------+------+ client
1771 * 32 +------+------+---
1772 * | fail | fail | 64
1773 * ---+------+------+---
1775 * 64 +------+------+---
1777 * ---+------+------+---
1779 switch (prefix_type
)
1782 /* 64-bit prefix requires 64-bit server */
1783 return sizeof(void *) > sizeof(int) ? ~0 : 0;
1786 return ~CPU_64BIT_MASK
; /* only 32-bit cpus supported on 32-bit prefix */
1790 /* registry initialisation */
1791 void init_registry(void)
1793 static const WCHAR HKLM
[] = { 'M','a','c','h','i','n','e' };
1794 static const WCHAR HKU_default
[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1795 static const WCHAR classes
[] = {'S','o','f','t','w','a','r','e','\\',
1796 'C','l','a','s','s','e','s','\\',
1797 'W','o','w','6','4','3','2','N','o','d','e'};
1798 static const struct unicode_str root_name
= { NULL
, 0 };
1799 static const struct unicode_str HKLM_name
= { HKLM
, sizeof(HKLM
) };
1800 static const struct unicode_str HKU_name
= { HKU_default
, sizeof(HKU_default
) };
1801 static const struct unicode_str classes_name
= { classes
, sizeof(classes
) };
1803 WCHAR
*current_user_path
;
1804 struct unicode_str current_user_str
;
1805 struct key
*key
, *hklm
, *hkcu
;
1808 /* switch to the config dir */
1810 if (fchdir( config_dir_fd
) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno
));
1812 /* create the root key */
1813 root_key
= alloc_key( &root_name
, current_time
);
1815 make_object_permanent( &root_key
->obj
);
1817 /* load system.reg into Registry\Machine */
1819 if (!(hklm
= create_key_recursive( root_key
, &HKLM_name
, current_time
)))
1820 fatal_error( "could not create Machine registry key\n" );
1822 if (!load_init_registry_from_file( "system.reg", hklm
))
1824 if ((p
= getenv( "WINEARCH" )) && !strcmp( p
, "win32" ))
1825 prefix_type
= PREFIX_32BIT
;
1827 prefix_type
= sizeof(void *) > sizeof(int) ? PREFIX_64BIT
: PREFIX_32BIT
;
1829 else if (prefix_type
== PREFIX_UNKNOWN
)
1830 prefix_type
= PREFIX_32BIT
;
1832 /* load userdef.reg into Registry\User\.Default */
1834 if (!(key
= create_key_recursive( root_key
, &HKU_name
, current_time
)))
1835 fatal_error( "could not create User\\.Default registry key\n" );
1837 load_init_registry_from_file( "userdef.reg", key
);
1838 release_object( key
);
1840 /* load user.reg into HKEY_CURRENT_USER */
1842 /* FIXME: match default user in token.c. should get from process token instead */
1843 current_user_path
= format_user_registry_path( security_local_user_sid
, ¤t_user_str
);
1844 if (!current_user_path
||
1845 !(hkcu
= create_key_recursive( root_key
, ¤t_user_str
, current_time
)))
1846 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1847 free( current_user_path
);
1848 load_init_registry_from_file( "user.reg", hkcu
);
1850 /* set the shared flag on Software\Classes\Wow6432Node */
1851 if (prefix_type
== PREFIX_64BIT
)
1853 if ((key
= create_key_recursive( hklm
, &classes_name
, current_time
)))
1855 key
->flags
|= KEY_WOWSHARE
;
1856 release_object( key
);
1858 /* FIXME: handle HKCU too */
1861 release_object( hklm
);
1862 release_object( hkcu
);
1864 /* start the periodic save timer */
1865 set_periodic_save_timer();
1867 /* create windows directories */
1869 if (!mkdir( "drive_c/windows", 0777 ))
1871 mkdir( "drive_c/windows/system32", 0777 );
1872 if (prefix_type
== PREFIX_64BIT
) mkdir( "drive_c/windows/syswow64", 0777 );
1875 /* go back to the server dir */
1876 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
1879 /* save a registry branch to a file */
1880 static void save_all_subkeys( struct key
*key
, FILE *f
)
1882 fprintf( f
, "WINE REGISTRY Version 2\n" );
1883 fprintf( f
, ";; All keys relative to " );
1884 dump_path( key
, NULL
, f
);
1886 switch (prefix_type
)
1889 fprintf( f
, "\n#arch=win32\n" );
1892 fprintf( f
, "\n#arch=win64\n" );
1897 save_subkeys( key
, key
, f
);
1900 /* save a registry branch to a file handle */
1901 static void save_registry( struct key
*key
, obj_handle_t handle
)
1906 if (!(file
= get_file_obj( current
->process
, handle
, FILE_WRITE_DATA
))) return;
1907 fd
= dup( get_file_unix_fd( file
) );
1908 release_object( file
);
1911 FILE *f
= fdopen( fd
, "w" );
1914 save_all_subkeys( key
, f
);
1915 if (fclose( f
)) file_set_error();
1925 /* save a registry branch to a file */
1926 static int save_branch( struct key
*key
, const char *path
)
1929 char *p
, *tmp
= NULL
;
1930 int fd
, count
= 0, ret
= 0;
1933 if (!(key
->flags
& KEY_DIRTY
))
1935 if (debug_level
> 1) dump_operation( key
, NULL
, "Not saving clean" );
1939 /* test the file type */
1941 if ((fd
= open( path
, O_WRONLY
)) != -1)
1943 /* if file is not a regular file or has multiple links or is accessed
1944 * via symbolic links, write directly into it; otherwise use a temp file */
1945 if (!lstat( path
, &st
) && (!S_ISREG(st
.st_mode
) || st
.st_nlink
> 1))
1953 /* create a temp file in the same directory */
1955 if (!(tmp
= malloc( strlen(path
) + 20 ))) goto done
;
1956 strcpy( tmp
, path
);
1957 if ((p
= strrchr( tmp
, '/' ))) p
++;
1961 sprintf( p
, "reg%lx%04x.tmp", (long) getpid(), count
++ );
1962 if ((fd
= open( tmp
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666 )) != -1) break;
1963 if (errno
!= EEXIST
) goto done
;
1967 /* now save to it */
1970 if (!(f
= fdopen( fd
, "w" )))
1972 if (tmp
) unlink( tmp
);
1977 if (debug_level
> 1)
1979 fprintf( stderr
, "%s: ", path
);
1980 dump_operation( key
, NULL
, "saving" );
1983 save_all_subkeys( key
, f
);
1988 /* if successfully written, rename to final name */
1989 if (ret
) ret
= !rename( tmp
, path
);
1990 if (!ret
) unlink( tmp
);
1995 if (ret
) make_clean( key
);
1999 /* periodic saving of the registry */
2000 static void periodic_save( void *arg
)
2004 if (fchdir( config_dir_fd
) == -1) return;
2005 save_timeout_user
= NULL
;
2006 for (i
= 0; i
< save_branch_count
; i
++)
2007 save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
);
2008 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
2009 set_periodic_save_timer();
2012 /* start the periodic save timer */
2013 static void set_periodic_save_timer(void)
2015 if (save_timeout_user
) remove_timeout_user( save_timeout_user
);
2016 save_timeout_user
= add_timeout_user( save_period
, periodic_save
, NULL
);
2019 /* save the modified registry branches to disk */
2020 void flush_registry(void)
2024 if (fchdir( config_dir_fd
) == -1) return;
2025 for (i
= 0; i
< save_branch_count
; i
++)
2027 if (!save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
))
2029 fprintf( stderr
, "wineserver: could not save registry branch to %s",
2030 save_branch_info
[i
].path
);
2034 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
2037 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2038 static int is_wow64_thread( struct thread
*thread
)
2040 return (prefix_type
== PREFIX_64BIT
&& !(CPU_FLAG(thread
->process
->cpu
) & CPU_64BIT_MASK
));
2044 /* create a registry key */
2045 DECL_HANDLER(create_key
)
2047 struct key
*key
= NULL
, *parent
;
2048 struct unicode_str name
, class;
2049 unsigned int access
= req
->access
;
2050 const struct security_descriptor
*sd
;
2051 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
2053 if (!objattr
) return;
2055 if (!is_wow64_thread( current
)) access
= (access
& ~KEY_WOW64_32KEY
) | KEY_WOW64_64KEY
;
2057 class.str
= get_req_data_after_objattr( objattr
, &class.len
);
2058 class.len
= (class.len
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
2060 if (!objattr
->rootdir
&& name
.len
>= sizeof(root_name
) &&
2061 !memicmp_strW( name
.str
, root_name
, sizeof(root_name
) ))
2063 name
.str
+= ARRAY_SIZE( root_name
);
2064 name
.len
-= sizeof(root_name
);
2067 /* NOTE: no access rights are required from the parent handle to create a key */
2068 if ((parent
= get_parent_hkey_obj( objattr
->rootdir
)))
2070 if ((key
= create_key( parent
, &name
, &class, req
->options
, access
,
2071 objattr
->attributes
, sd
, &reply
->created
)))
2073 reply
->hkey
= alloc_handle( current
->process
, key
, access
, objattr
->attributes
);
2074 release_object( key
);
2076 release_object( parent
);
2080 /* open a registry key */
2081 DECL_HANDLER(open_key
)
2083 struct key
*key
, *parent
;
2084 struct unicode_str name
;
2085 unsigned int access
= req
->access
;
2087 if (!is_wow64_thread( current
)) access
= (access
& ~KEY_WOW64_32KEY
) | KEY_WOW64_64KEY
;
2090 /* NOTE: no access rights are required to open the parent key, only the child key */
2091 if ((parent
= get_parent_hkey_obj( req
->parent
)))
2093 get_req_path( &name
, !req
->parent
);
2094 if ((key
= open_key( parent
, &name
, access
, req
->attributes
)))
2096 reply
->hkey
= alloc_handle( current
->process
, key
, access
, req
->attributes
);
2097 release_object( key
);
2099 release_object( parent
);
2103 /* delete a registry key */
2104 DECL_HANDLER(delete_key
)
2108 if ((key
= get_hkey_obj( req
->hkey
, DELETE
)))
2110 delete_key( key
, 0);
2111 release_object( key
);
2115 /* flush a registry key */
2116 DECL_HANDLER(flush_key
)
2118 struct key
*key
= get_hkey_obj( req
->hkey
, 0 );
2121 /* we don't need to do anything here with the current implementation */
2122 release_object( key
);
2126 /* enumerate registry subkeys */
2127 DECL_HANDLER(enum_key
)
2131 if ((key
= get_hkey_obj( req
->hkey
,
2132 req
->index
== -1 ? KEY_QUERY_VALUE
: KEY_ENUMERATE_SUB_KEYS
)))
2134 enum_key( key
, req
->index
, req
->info_class
, reply
);
2135 release_object( key
);
2139 /* set a value of a registry key */
2140 DECL_HANDLER(set_key_value
)
2143 struct unicode_str name
;
2145 if (req
->namelen
> get_req_data_size())
2147 set_error( STATUS_INVALID_PARAMETER
);
2150 name
.str
= get_req_data();
2151 name
.len
= (req
->namelen
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
2153 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
2155 data_size_t datalen
= get_req_data_size() - req
->namelen
;
2156 const char *data
= (const char *)get_req_data() + req
->namelen
;
2158 set_value( key
, &name
, req
->type
, data
, datalen
);
2159 release_object( key
);
2163 /* retrieve the value of a registry key */
2164 DECL_HANDLER(get_key_value
)
2167 struct unicode_str name
= get_req_unicode_str();
2170 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
2172 get_value( key
, &name
, &reply
->type
, &reply
->total
);
2173 release_object( key
);
2177 /* enumerate the value of a registry key */
2178 DECL_HANDLER(enum_key_value
)
2182 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
2184 enum_value( key
, req
->index
, req
->info_class
, reply
);
2185 release_object( key
);
2189 /* delete a value of a registry key */
2190 DECL_HANDLER(delete_key_value
)
2193 struct unicode_str name
= get_req_unicode_str();
2195 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
2197 delete_value( key
, &name
);
2198 release_object( key
);
2202 /* load a registry branch from a file */
2203 DECL_HANDLER(load_registry
)
2205 struct key
*key
, *parent
;
2206 struct unicode_str name
;
2207 const struct security_descriptor
*sd
;
2208 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
2210 if (!objattr
) return;
2212 if (!thread_single_check_privilege( current
, &SeRestorePrivilege
))
2214 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2218 if (!objattr
->rootdir
&& name
.len
>= sizeof(root_name
) &&
2219 !memicmp_strW( name
.str
, root_name
, sizeof(root_name
) ))
2221 name
.str
+= ARRAY_SIZE( root_name
);
2222 name
.len
-= sizeof(root_name
);
2225 if ((parent
= get_parent_hkey_obj( objattr
->rootdir
)))
2228 if ((key
= create_key( parent
, &name
, NULL
, 0, KEY_WOW64_64KEY
, 0, sd
, &dummy
)))
2230 load_registry( key
, req
->file
);
2231 release_object( key
);
2233 release_object( parent
);
2237 DECL_HANDLER(unload_registry
)
2241 if (!thread_single_check_privilege( current
, &SeRestorePrivilege
))
2243 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2247 if ((key
= get_hkey_obj( req
->hkey
, 0 )))
2249 delete_key( key
, 1 ); /* FIXME */
2250 release_object( key
);
2254 /* save a registry branch to a file */
2255 DECL_HANDLER(save_registry
)
2259 if (!thread_single_check_privilege( current
, &SeBackupPrivilege
))
2261 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2265 if ((key
= get_hkey_obj( req
->hkey
, 0 )))
2267 save_registry( key
, req
->file
);
2268 release_object( key
);
2272 /* add a registry key change notification */
2273 DECL_HANDLER(set_registry_notification
)
2276 struct event
*event
;
2277 struct notify
*notify
;
2279 key
= get_hkey_obj( req
->hkey
, KEY_NOTIFY
);
2282 event
= get_event_obj( current
->process
, req
->event
, SYNCHRONIZE
);
2285 notify
= find_notify( key
, current
->process
, req
->hkey
);
2289 release_object( notify
->event
);
2290 grab_object( event
);
2291 notify
->event
= event
;
2295 notify
= mem_alloc( sizeof(*notify
) );
2298 grab_object( event
);
2299 notify
->event
= event
;
2300 notify
->subtree
= req
->subtree
;
2301 notify
->filter
= req
->filter
;
2302 notify
->hkey
= req
->hkey
;
2303 notify
->process
= current
->process
;
2304 list_add_head( &key
->notify_list
, ¬ify
->entry
);
2309 reset_event( event
);
2310 set_error( STATUS_PENDING
);
2312 release_object( event
);
2314 release_object( key
);