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
**events
; /* events to set when changing this key */
56 unsigned int event_count
; /* number of events */
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 */
63 static const WCHAR key_name
[] = {'K','e','y'};
65 struct type_descr key_type
=
67 { key_name
, sizeof(key_name
) }, /* name */
68 KEY_ALL_ACCESS
| SYNCHRONIZE
, /* valid_access */
70 STANDARD_RIGHTS_READ
| KEY_NOTIFY
| KEY_ENUMERATE_SUB_KEYS
| KEY_QUERY_VALUE
,
71 STANDARD_RIGHTS_WRITE
| KEY_CREATE_SUB_KEY
| KEY_SET_VALUE
,
72 STANDARD_RIGHTS_EXECUTE
| KEY_CREATE_LINK
| KEY_NOTIFY
| KEY_ENUMERATE_SUB_KEYS
| KEY_QUERY_VALUE
,
80 struct object obj
; /* object header */
81 WCHAR
*name
; /* key name */
82 WCHAR
*class; /* key class */
83 unsigned short namelen
; /* length of key name */
84 unsigned short classlen
; /* length of class name */
85 struct key
*parent
; /* parent key */
86 int last_subkey
; /* last in use subkey */
87 int nb_subkeys
; /* count of allocated subkeys */
88 struct key
**subkeys
; /* subkeys array */
89 int last_value
; /* last in use value */
90 int nb_values
; /* count of allocated values in array */
91 struct key_value
*values
; /* values array */
92 unsigned int flags
; /* flags */
93 timeout_t modif
; /* last modification time */
94 struct list notify_list
; /* list of notifications */
98 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
99 #define KEY_DELETED 0x0002 /* key has been deleted */
100 #define KEY_DIRTY 0x0004 /* key has been modified */
101 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
102 #define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
103 #define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
108 WCHAR
*name
; /* value name */
109 unsigned short namelen
; /* length of value name */
110 unsigned int type
; /* value type */
111 data_size_t len
; /* value data length in bytes */
112 void *data
; /* pointer to value data */
115 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
116 #define MIN_VALUES 8 /* min. number of allocated values per key */
118 #define MAX_NAME_LEN 256 /* max. length of a key name */
119 #define MAX_VALUE_LEN 16383 /* max. length of a value name */
121 /* the root of the registry tree */
122 static struct key
*root_key
;
124 static const timeout_t ticks_1601_to_1970
= (timeout_t
)86400 * (369 * 365 + 89) * TICKS_PER_SEC
;
125 static const timeout_t save_period
= 30 * -TICKS_PER_SEC
; /* delay between periodic saves */
126 static struct timeout_user
*save_timeout_user
; /* saving timer */
127 static enum prefix_type
{ PREFIX_UNKNOWN
, PREFIX_32BIT
, PREFIX_64BIT
} prefix_type
;
129 static const WCHAR root_name
[] = { '\\','R','e','g','i','s','t','r','y','\\' };
130 static const WCHAR wow6432node
[] = {'W','o','w','6','4','3','2','N','o','d','e'};
131 static const WCHAR symlink_value
[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
132 static const struct unicode_str symlink_str
= { symlink_value
, sizeof(symlink_value
) };
134 static void set_periodic_save_timer(void);
135 static struct key_value
*find_value( const struct key
*key
, const struct unicode_str
*name
, int *index
);
137 /* information about where to save a registry branch */
138 struct save_branch_info
144 #define MAX_SAVE_BRANCH_INFO 3
145 static int save_branch_count
;
146 static struct save_branch_info save_branch_info
[MAX_SAVE_BRANCH_INFO
];
149 /* information about a file being loaded */
150 struct file_load_info
152 const char *filename
; /* input file name */
153 FILE *file
; /* input file */
154 char *buffer
; /* line buffer */
155 int len
; /* buffer length */
156 int line
; /* current input line */
157 WCHAR
*tmp
; /* temp buffer to use while parsing input */
158 size_t tmplen
; /* length of temp buffer */
162 static void key_dump( struct object
*obj
, int verbose
);
163 static unsigned int key_map_access( struct object
*obj
, unsigned int access
);
164 static struct security_descriptor
*key_get_sd( struct object
*obj
);
165 static WCHAR
*key_get_full_name( struct object
*obj
, data_size_t
*len
);
166 static int key_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
167 static void key_destroy( struct object
*obj
);
169 static const struct object_ops key_ops
=
171 sizeof(struct key
), /* size */
172 &key_type
, /* type */
174 no_add_queue
, /* add_queue */
175 NULL
, /* remove_queue */
177 NULL
, /* satisfied */
178 no_signal
, /* signal */
179 no_get_fd
, /* get_fd */
180 key_map_access
, /* map_access */
181 key_get_sd
, /* get_sd */
182 default_set_sd
, /* set_sd */
183 key_get_full_name
, /* get_full_name */
184 no_lookup_name
, /* lookup_name */
185 no_link_name
, /* link_name */
186 NULL
, /* unlink_name */
187 no_open_file
, /* open_file */
188 no_kernel_obj_list
, /* get_kernel_obj_list */
189 key_close_handle
, /* close_handle */
190 key_destroy
/* destroy */
194 static inline int is_wow6432node( const WCHAR
*name
, unsigned int len
)
196 return (len
== sizeof(wow6432node
) && !memicmp_strW( name
, wow6432node
, sizeof( wow6432node
)));
200 * The registry text file format v2 used by this code is similar to the one
201 * used by REGEDIT import/export functionality, with the following differences:
202 * - strings and key names can contain \x escapes for Unicode
203 * - key names use escapes too in order to support Unicode
204 * - the modification time optionally follows the key name
205 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
208 /* dump the full path of a key */
209 static void dump_path( const struct key
*key
, const struct key
*base
, FILE *f
)
211 if (key
->parent
&& key
->parent
!= base
)
213 dump_path( key
->parent
, base
, f
);
214 fprintf( f
, "\\\\" );
216 dump_strW( key
->name
, key
->namelen
, f
, "[]" );
219 /* dump a value to a text file */
220 static void dump_value( const struct key_value
*value
, FILE *f
)
228 count
= 1 + dump_strW( value
->name
, value
->namelen
, f
, "\"\"" );
229 count
+= fprintf( f
, "\"=" );
231 else count
= fprintf( f
, "@=" );
238 /* only output properly terminated strings in string format */
239 if (value
->len
< sizeof(WCHAR
)) break;
240 if (value
->len
% sizeof(WCHAR
)) break;
241 if (((WCHAR
*)value
->data
)[value
->len
/ sizeof(WCHAR
) - 1]) break;
242 if (value
->type
!= REG_SZ
) fprintf( f
, "str(%x):", value
->type
);
244 dump_strW( (WCHAR
*)value
->data
, value
->len
, f
, "\"\"" );
245 fprintf( f
, "\"\n" );
249 if (value
->len
!= sizeof(dw
)) break;
250 memcpy( &dw
, value
->data
, sizeof(dw
) );
251 fprintf( f
, "dword:%08x\n", dw
);
255 if (value
->type
== REG_BINARY
) count
+= fprintf( f
, "hex:" );
256 else count
+= fprintf( f
, "hex(%x):", value
->type
);
257 for (i
= 0; i
< value
->len
; i
++)
259 count
+= fprintf( f
, "%02x", *((unsigned char *)value
->data
+ i
) );
260 if (i
< value
->len
-1)
265 fprintf( f
, "\\\n " );
273 /* save a registry and all its subkeys to a text file */
274 static void save_subkeys( const struct key
*key
, const struct key
*base
, FILE *f
)
278 if (key
->flags
& KEY_VOLATILE
) return;
279 /* save key if it has either some values or no subkeys, or needs special options */
280 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
281 if ((key
->last_value
>= 0) || (key
->last_subkey
== -1) || key
->class || (key
->flags
& KEY_SYMLINK
))
284 if (key
!= base
) dump_path( key
, base
, f
);
285 fprintf( f
, "] %u\n", (unsigned int)((key
->modif
- ticks_1601_to_1970
) / TICKS_PER_SEC
) );
286 fprintf( f
, "#time=%x%08x\n", (unsigned int)(key
->modif
>> 32), (unsigned int)key
->modif
);
289 fprintf( f
, "#class=\"" );
290 dump_strW( key
->class, key
->classlen
, f
, "\"\"" );
291 fprintf( f
, "\"\n" );
293 if (key
->flags
& KEY_SYMLINK
) fputs( "#link\n", f
);
294 for (i
= 0; i
<= key
->last_value
; i
++) dump_value( &key
->values
[i
], f
);
296 for (i
= 0; i
<= key
->last_subkey
; i
++) save_subkeys( key
->subkeys
[i
], base
, f
);
299 static void dump_operation( const struct key
*key
, const struct key_value
*value
, const char *op
)
301 fprintf( stderr
, "%s key ", op
);
302 if (key
) dump_path( key
, NULL
, stderr
);
303 else fprintf( stderr
, "ERROR" );
306 fprintf( stderr
, " value ");
307 dump_value( value
, stderr
);
309 else fprintf( stderr
, "\n" );
312 static void key_dump( struct object
*obj
, int verbose
)
314 struct key
*key
= (struct key
*)obj
;
315 assert( obj
->ops
== &key_ops
);
316 fprintf( stderr
, "Key flags=%x ", key
->flags
);
317 dump_path( key
, NULL
, stderr
);
318 fprintf( stderr
, "\n" );
321 /* notify waiter and maybe delete the notification */
322 static void do_notification( struct key
*key
, struct notify
*notify
, int del
)
326 for (i
= 0; i
< notify
->event_count
; ++i
)
328 set_event( notify
->events
[i
] );
329 release_object( notify
->events
[i
] );
331 free( notify
->events
);
332 notify
->events
= NULL
;
333 notify
->event_count
= 0;
337 list_remove( ¬ify
->entry
);
342 static inline struct notify
*find_notify( struct key
*key
, struct process
*process
, obj_handle_t hkey
)
344 struct notify
*notify
;
346 LIST_FOR_EACH_ENTRY( notify
, &key
->notify_list
, struct notify
, entry
)
348 if (notify
->process
== process
&& notify
->hkey
== hkey
) return notify
;
353 static unsigned int key_map_access( struct object
*obj
, unsigned int access
)
355 access
= default_map_access( obj
, access
);
356 /* filter the WOW64 masks, as they aren't real access bits */
357 return access
& ~(KEY_WOW64_64KEY
| KEY_WOW64_32KEY
);
360 static struct security_descriptor
*key_get_sd( struct object
*obj
)
362 static struct security_descriptor
*key_default_sd
;
364 if (obj
->sd
) return obj
->sd
;
368 size_t users_sid_len
= security_sid_len( security_builtin_users_sid
);
369 size_t admins_sid_len
= security_sid_len( security_builtin_admins_sid
);
370 size_t dacl_len
= sizeof(ACL
) + 2 * offsetof( ACCESS_ALLOWED_ACE
, SidStart
)
371 + users_sid_len
+ admins_sid_len
;
372 ACCESS_ALLOWED_ACE
*aaa
;
375 key_default_sd
= mem_alloc( sizeof(*key_default_sd
) + 2 * admins_sid_len
+ dacl_len
);
376 key_default_sd
->control
= SE_DACL_PRESENT
;
377 key_default_sd
->owner_len
= admins_sid_len
;
378 key_default_sd
->group_len
= admins_sid_len
;
379 key_default_sd
->sacl_len
= 0;
380 key_default_sd
->dacl_len
= dacl_len
;
381 memcpy( key_default_sd
+ 1, security_builtin_admins_sid
, admins_sid_len
);
382 memcpy( (char *)(key_default_sd
+ 1) + admins_sid_len
, security_builtin_admins_sid
, admins_sid_len
);
384 dacl
= (ACL
*)((char *)(key_default_sd
+ 1) + 2 * admins_sid_len
);
385 dacl
->AclRevision
= ACL_REVISION
;
387 dacl
->AclSize
= dacl_len
;
390 aaa
= (ACCESS_ALLOWED_ACE
*)(dacl
+ 1);
391 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
392 aaa
->Header
.AceFlags
= INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
;
393 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + users_sid_len
;
394 aaa
->Mask
= GENERIC_READ
;
395 memcpy( &aaa
->SidStart
, security_builtin_users_sid
, users_sid_len
);
396 aaa
= (ACCESS_ALLOWED_ACE
*)((char *)aaa
+ aaa
->Header
.AceSize
);
397 aaa
->Header
.AceType
= ACCESS_ALLOWED_ACE_TYPE
;
398 aaa
->Header
.AceFlags
= 0;
399 aaa
->Header
.AceSize
= offsetof( ACCESS_ALLOWED_ACE
, SidStart
) + admins_sid_len
;
400 aaa
->Mask
= KEY_ALL_ACCESS
;
401 memcpy( &aaa
->SidStart
, security_builtin_admins_sid
, admins_sid_len
);
403 return key_default_sd
;
406 static WCHAR
*key_get_full_name( struct object
*obj
, data_size_t
*ret_len
)
408 static const WCHAR backslash
= '\\';
409 struct key
*key
= (struct key
*) obj
;
410 data_size_t len
= sizeof(root_name
) - sizeof(WCHAR
);
413 for (key
= (struct key
*)obj
; key
!= root_key
; key
= key
->parent
) len
+= key
->namelen
+ sizeof(WCHAR
);
414 if (!(ret
= malloc( len
))) return NULL
;
417 key
= (struct key
*)obj
;
418 for (key
= (struct key
*)obj
; key
!= root_key
; key
= key
->parent
)
420 memcpy( ret
+ len
- key
->namelen
, key
->name
, key
->namelen
);
421 len
-= key
->namelen
+ sizeof(WCHAR
);
422 memcpy( ret
+ len
, &backslash
, sizeof(WCHAR
) );
424 memcpy( ret
, root_name
, sizeof(root_name
) - sizeof(WCHAR
) );
428 /* close the notification associated with a handle */
429 static int key_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
431 struct key
* key
= (struct key
*) obj
;
432 struct notify
*notify
= find_notify( key
, process
, handle
);
433 if (notify
) do_notification( key
, notify
, 1 );
434 return 1; /* ok to close */
437 static void key_destroy( struct object
*obj
)
441 struct key
*key
= (struct key
*)obj
;
442 assert( obj
->ops
== &key_ops
);
446 for (i
= 0; i
<= key
->last_value
; i
++)
448 free( key
->values
[i
].name
);
449 free( key
->values
[i
].data
);
452 for (i
= 0; i
<= key
->last_subkey
; i
++)
454 key
->subkeys
[i
]->parent
= NULL
;
455 release_object( key
->subkeys
[i
] );
457 free( key
->subkeys
);
458 /* unconditionally notify everything waiting on this key */
459 while ((ptr
= list_head( &key
->notify_list
)))
461 struct notify
*notify
= LIST_ENTRY( ptr
, struct notify
, entry
);
462 do_notification( key
, notify
, 1 );
466 /* get the request vararg as registry path */
467 static inline void get_req_path( struct unicode_str
*str
, int skip_root
)
469 str
->str
= get_req_data();
470 str
->len
= (get_req_data_size() / sizeof(WCHAR
)) * sizeof(WCHAR
);
472 if (skip_root
&& str
->len
>= sizeof(root_name
) && !memicmp_strW( str
->str
, root_name
, sizeof(root_name
) ))
474 str
->str
+= ARRAY_SIZE( root_name
);
475 str
->len
-= sizeof(root_name
);
479 /* return the next token in a given path */
480 /* token->str must point inside the path, or be NULL for the first call */
481 static struct unicode_str
*get_path_token( const struct unicode_str
*path
, struct unicode_str
*token
)
483 data_size_t i
= 0, len
= path
->len
/ sizeof(WCHAR
);
485 if (!token
->str
) /* first time */
487 /* path cannot start with a backslash */
488 if (len
&& path
->str
[0] == '\\')
490 set_error( STATUS_OBJECT_PATH_INVALID
);
496 i
= token
->str
- path
->str
;
497 i
+= token
->len
/ sizeof(WCHAR
);
498 while (i
< len
&& path
->str
[i
] == '\\') i
++;
500 token
->str
= path
->str
+ i
;
501 while (i
< len
&& path
->str
[i
] != '\\') i
++;
502 token
->len
= (path
->str
+ i
- token
->str
) * sizeof(WCHAR
);
506 /* allocate a key object */
507 static struct key
*alloc_key( const struct unicode_str
*name
, timeout_t modif
)
510 if ((key
= alloc_object( &key_ops
)))
514 key
->namelen
= name
->len
;
517 key
->last_subkey
= -1;
521 key
->last_value
= -1;
525 list_init( &key
->notify_list
);
526 if (name
->len
&& !(key
->name
= memdup( name
->str
, name
->len
)))
528 release_object( key
);
535 /* mark a key and all its parents as dirty (modified) */
536 static void make_dirty( struct key
*key
)
540 if (key
->flags
& (KEY_DIRTY
|KEY_VOLATILE
)) return; /* nothing to do */
541 key
->flags
|= KEY_DIRTY
;
546 /* mark a key and all its subkeys as clean (not modified) */
547 static void make_clean( struct key
*key
)
551 if (key
->flags
& KEY_VOLATILE
) return;
552 if (!(key
->flags
& KEY_DIRTY
)) return;
553 key
->flags
&= ~KEY_DIRTY
;
554 for (i
= 0; i
<= key
->last_subkey
; i
++) make_clean( key
->subkeys
[i
] );
557 /* go through all the notifications and send them if necessary */
558 static void check_notify( struct key
*key
, unsigned int change
, int not_subtree
)
560 struct list
*ptr
, *next
;
562 LIST_FOR_EACH_SAFE( ptr
, next
, &key
->notify_list
)
564 struct notify
*n
= LIST_ENTRY( ptr
, struct notify
, entry
);
565 if ( ( not_subtree
|| n
->subtree
) && ( change
& n
->filter
) )
566 do_notification( key
, n
, 0 );
570 /* update key modification time */
571 static void touch_key( struct key
*key
, unsigned int change
)
575 key
->modif
= current_time
;
578 /* do notifications */
579 check_notify( key
, change
, 1 );
580 for ( k
= key
->parent
; k
; k
= k
->parent
)
581 check_notify( k
, change
, 0 );
584 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
585 static int grow_subkeys( struct key
*key
)
587 struct key
**new_subkeys
;
592 nb_subkeys
= key
->nb_subkeys
+ (key
->nb_subkeys
/ 2); /* grow by 50% */
593 if (!(new_subkeys
= realloc( key
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) )))
595 set_error( STATUS_NO_MEMORY
);
601 nb_subkeys
= MIN_SUBKEYS
;
602 if (!(new_subkeys
= mem_alloc( nb_subkeys
* sizeof(*new_subkeys
) ))) return 0;
604 key
->subkeys
= new_subkeys
;
605 key
->nb_subkeys
= nb_subkeys
;
609 /* allocate a subkey for a given key, and return its index */
610 static struct key
*alloc_subkey( struct key
*parent
, const struct unicode_str
*name
,
611 int index
, timeout_t modif
)
616 if (name
->len
> MAX_NAME_LEN
* sizeof(WCHAR
))
618 set_error( STATUS_INVALID_PARAMETER
);
621 if (parent
->last_subkey
+ 1 == parent
->nb_subkeys
)
623 /* need to grow the array */
624 if (!grow_subkeys( parent
)) return NULL
;
626 if ((key
= alloc_key( name
, modif
)) != NULL
)
628 key
->parent
= parent
;
629 for (i
= ++parent
->last_subkey
; i
> index
; i
--)
630 parent
->subkeys
[i
] = parent
->subkeys
[i
-1];
631 parent
->subkeys
[index
] = key
;
632 if (is_wow6432node( key
->name
, key
->namelen
) && !is_wow6432node( parent
->name
, parent
->namelen
))
633 parent
->flags
|= KEY_WOW64
;
638 /* free a subkey of a given key */
639 static void free_subkey( struct key
*parent
, int index
)
644 assert( index
>= 0 );
645 assert( index
<= parent
->last_subkey
);
647 key
= parent
->subkeys
[index
];
648 for (i
= index
; i
< parent
->last_subkey
; i
++) parent
->subkeys
[i
] = parent
->subkeys
[i
+ 1];
649 parent
->last_subkey
--;
650 key
->flags
|= KEY_DELETED
;
652 if (is_wow6432node( key
->name
, key
->namelen
)) parent
->flags
&= ~KEY_WOW64
;
653 release_object( key
);
655 /* try to shrink the array */
656 nb_subkeys
= parent
->nb_subkeys
;
657 if (nb_subkeys
> MIN_SUBKEYS
&& parent
->last_subkey
< nb_subkeys
/ 2)
659 struct key
**new_subkeys
;
660 nb_subkeys
-= nb_subkeys
/ 3; /* shrink by 33% */
661 if (nb_subkeys
< MIN_SUBKEYS
) nb_subkeys
= MIN_SUBKEYS
;
662 if (!(new_subkeys
= realloc( parent
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) ))) return;
663 parent
->subkeys
= new_subkeys
;
664 parent
->nb_subkeys
= nb_subkeys
;
668 /* find the named child of a given key and return its index */
669 static struct key
*find_subkey( const struct key
*key
, const struct unicode_str
*name
, int *index
)
671 int i
, min
, max
, res
;
675 max
= key
->last_subkey
;
679 len
= min( key
->subkeys
[i
]->namelen
, name
->len
);
680 res
= memicmp_strW( key
->subkeys
[i
]->name
, name
->str
, len
);
681 if (!res
) res
= key
->subkeys
[i
]->namelen
- name
->len
;
685 return key
->subkeys
[i
];
687 if (res
> 0) max
= i
- 1;
690 *index
= min
; /* this is where we should insert it */
694 /* return the wow64 variant of the key, or the key itself if none */
695 static struct key
*find_wow64_subkey( struct key
*key
, const struct unicode_str
*name
)
697 static const struct unicode_str wow6432node_str
= { wow6432node
, sizeof(wow6432node
) };
700 if (!(key
->flags
& KEY_WOW64
)) return key
;
701 if (!is_wow6432node( name
->str
, name
->len
))
703 key
= find_subkey( key
, &wow6432node_str
, &index
);
704 assert( key
); /* if KEY_WOW64 is set we must find it */
710 /* follow a symlink and return the resolved key */
711 static struct key
*follow_symlink( struct key
*key
, int iteration
)
713 struct unicode_str path
, token
;
714 struct key_value
*value
;
717 if (iteration
> 16) return NULL
;
718 if (!(key
->flags
& KEY_SYMLINK
)) return key
;
719 if (!(value
= find_value( key
, &symlink_str
, &index
))) return NULL
;
721 path
.str
= value
->data
;
722 path
.len
= (value
->len
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
723 if (path
.len
<= sizeof(root_name
)) return NULL
;
724 if (memicmp_strW( path
.str
, root_name
, sizeof(root_name
) )) return NULL
;
725 path
.str
+= ARRAY_SIZE( root_name
);
726 path
.len
-= sizeof(root_name
);
730 if (!get_path_token( &path
, &token
)) return NULL
;
733 if (!(key
= find_subkey( key
, &token
, &index
))) break;
734 if (!(key
= follow_symlink( key
, iteration
+ 1 ))) break;
735 get_path_token( &path
, &token
);
740 /* open a key until we find an element that doesn't exist */
741 /* helper for open_key and create_key */
742 static struct key
*open_key_prefix( struct key
*key
, const struct unicode_str
*name
,
743 unsigned int access
, struct unicode_str
*token
, int *index
)
746 if (!get_path_token( name
, token
)) return NULL
;
747 if (access
& KEY_WOW64_32KEY
) key
= find_wow64_subkey( key
, token
);
751 if (!(subkey
= find_subkey( key
, token
, index
)))
753 if ((key
->flags
& KEY_WOWSHARE
) && !(access
& KEY_WOW64_64KEY
))
755 /* try in the 64-bit parent */
757 subkey
= find_subkey( key
, token
, index
);
762 get_path_token( name
, token
);
763 if (!token
->len
) break;
764 if (!(access
& KEY_WOW64_64KEY
)) key
= find_wow64_subkey( key
, token
);
765 if (!(key
= follow_symlink( key
, 0 )))
767 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
775 static struct key
*open_key( struct key
*key
, const struct unicode_str
*name
, unsigned int access
,
776 unsigned int attributes
)
779 struct unicode_str token
;
781 if (!(key
= open_key_prefix( key
, name
, access
, &token
, &index
))) return NULL
;
785 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
788 if (!(access
& KEY_WOW64_64KEY
)) key
= find_wow64_subkey( key
, &token
);
789 if (!(attributes
& OBJ_OPENLINK
) && !(key
= follow_symlink( key
, 0 )))
791 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
794 if (debug_level
> 1) dump_operation( key
, NULL
, "Open" );
799 /* create a subkey */
800 static struct key
*create_key( struct key
*key
, const struct unicode_str
*name
,
801 const struct unicode_str
*class, unsigned int options
,
802 unsigned int access
, unsigned int attributes
,
803 const struct security_descriptor
*sd
, int *created
)
806 struct unicode_str token
, next
;
809 if (!(key
= open_key_prefix( key
, name
, access
, &token
, &index
))) return NULL
;
811 if (!token
.len
) /* the key already exists */
813 if (!(access
& KEY_WOW64_64KEY
)) key
= find_wow64_subkey( key
, &token
);
814 if (options
& REG_OPTION_CREATE_LINK
)
816 set_error( STATUS_OBJECT_NAME_COLLISION
);
819 if (!(attributes
& OBJ_OPENLINK
) && !(key
= follow_symlink( key
, 0 )))
821 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
824 if (debug_level
> 1) dump_operation( key
, NULL
, "Open" );
829 /* token must be the last path component at this point */
831 get_path_token( name
, &next
);
834 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
838 if ((key
->flags
& KEY_VOLATILE
) && !(options
& REG_OPTION_VOLATILE
))
840 set_error( STATUS_CHILD_MUST_BE_VOLATILE
);
845 if (!(key
= alloc_subkey( key
, &token
, index
, current_time
))) return NULL
;
847 if (options
& REG_OPTION_CREATE_LINK
) key
->flags
|= KEY_SYMLINK
;
848 if (options
& REG_OPTION_VOLATILE
) key
->flags
|= KEY_VOLATILE
;
849 else key
->flags
|= KEY_DIRTY
;
851 if (sd
) default_set_sd( &key
->obj
, sd
, OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
|
852 DACL_SECURITY_INFORMATION
| SACL_SECURITY_INFORMATION
);
854 if (debug_level
> 1) dump_operation( key
, NULL
, "Create" );
855 if (class && class->len
)
857 key
->classlen
= class->len
;
859 if (!(key
->class = memdup( class->str
, key
->classlen
))) key
->classlen
= 0;
861 touch_key( key
->parent
, REG_NOTIFY_CHANGE_NAME
);
866 /* recursively create a subkey (for internal use only) */
867 static struct key
*create_key_recursive( struct key
*key
, const struct unicode_str
*name
, timeout_t modif
)
871 struct unicode_str token
;
874 if (!get_path_token( name
, &token
)) return NULL
;
878 if (!(subkey
= find_subkey( key
, &token
, &index
))) break;
880 if (!(key
= follow_symlink( key
, 0 )))
882 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
885 get_path_token( name
, &token
);
890 if (!(key
= alloc_subkey( key
, &token
, index
, modif
))) return NULL
;
894 get_path_token( name
, &token
);
895 if (!token
.len
) break;
896 /* we know the index is always 0 in a new key */
897 if (!(key
= alloc_subkey( key
, &token
, 0, modif
)))
899 free_subkey( base
, index
);
909 /* query information about a key or a subkey */
910 static void enum_key( struct key
*key
, int index
, int info_class
, struct enum_key_reply
*reply
)
913 data_size_t len
, namelen
, classlen
;
914 data_size_t max_subkey
= 0, max_class
= 0;
915 data_size_t max_value
= 0, max_data
= 0;
916 WCHAR
*fullname
= NULL
;
919 if (index
!= -1) /* -1 means use the specified key directly */
921 if ((index
< 0) || (index
> key
->last_subkey
))
923 set_error( STATUS_NO_MORE_ENTRIES
);
926 key
= key
->subkeys
[index
];
929 namelen
= key
->namelen
;
930 classlen
= key
->classlen
;
934 case KeyNameInformation
:
935 if (!(fullname
= key
->obj
.ops
->get_full_name( &key
->obj
, &namelen
))) return;
937 case KeyBasicInformation
:
938 classlen
= 0; /* only return the name */
940 case KeyNodeInformation
:
941 reply
->max_subkey
= 0;
942 reply
->max_class
= 0;
943 reply
->max_value
= 0;
946 case KeyFullInformation
:
947 case KeyCachedInformation
:
948 for (i
= 0; i
<= key
->last_subkey
; i
++)
950 if (key
->subkeys
[i
]->namelen
> max_subkey
) max_subkey
= key
->subkeys
[i
]->namelen
;
951 if (key
->subkeys
[i
]->classlen
> max_class
) max_class
= key
->subkeys
[i
]->classlen
;
953 for (i
= 0; i
<= key
->last_value
; i
++)
955 if (key
->values
[i
].namelen
> max_value
) max_value
= key
->values
[i
].namelen
;
956 if (key
->values
[i
].len
> max_data
) max_data
= key
->values
[i
].len
;
958 reply
->max_subkey
= max_subkey
;
959 reply
->max_class
= max_class
;
960 reply
->max_value
= max_value
;
961 reply
->max_data
= max_data
;
962 reply
->namelen
= namelen
;
963 if (info_class
== KeyCachedInformation
)
964 classlen
= 0; /* don't return any data, only its size */
965 namelen
= 0; /* don't return name */
968 set_error( STATUS_INVALID_PARAMETER
);
971 reply
->subkeys
= key
->last_subkey
+ 1;
972 reply
->values
= key
->last_value
+ 1;
973 reply
->modif
= key
->modif
;
974 reply
->total
= namelen
+ classlen
;
976 len
= min( reply
->total
, get_reply_max_size() );
977 if (len
&& (data
= set_reply_data_size( len
)))
981 reply
->namelen
= namelen
;
982 memcpy( data
, key
->name
, namelen
);
983 memcpy( data
+ namelen
, key
->class, len
- namelen
);
985 else if (info_class
== KeyNameInformation
)
987 reply
->namelen
= namelen
;
988 memcpy( data
, fullname
, len
);
992 reply
->namelen
= len
;
993 memcpy( data
, key
->name
, len
);
997 if (debug_level
> 1) dump_operation( key
, NULL
, "Enum" );
1000 /* delete a key and its values */
1001 static int delete_key( struct key
*key
, int recurse
)
1004 struct key
*parent
= key
->parent
;
1006 /* must find parent and index */
1007 if (key
== root_key
)
1009 set_error( STATUS_ACCESS_DENIED
);
1014 while (recurse
&& (key
->last_subkey
>=0))
1015 if (0 > delete_key(key
->subkeys
[key
->last_subkey
], 1))
1018 for (index
= 0; index
<= parent
->last_subkey
; index
++)
1019 if (parent
->subkeys
[index
] == key
) break;
1020 assert( index
<= parent
->last_subkey
);
1022 /* we can only delete a key that has no subkeys */
1023 if (key
->last_subkey
>= 0)
1025 set_error( STATUS_ACCESS_DENIED
);
1029 if (debug_level
> 1) dump_operation( key
, NULL
, "Delete" );
1030 free_subkey( parent
, index
);
1031 touch_key( parent
, REG_NOTIFY_CHANGE_NAME
);
1035 /* try to grow the array of values; return 1 if OK, 0 on error */
1036 static int grow_values( struct key
*key
)
1038 struct key_value
*new_val
;
1043 nb_values
= key
->nb_values
+ (key
->nb_values
/ 2); /* grow by 50% */
1044 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) )))
1046 set_error( STATUS_NO_MEMORY
);
1052 nb_values
= MIN_VALUES
;
1053 if (!(new_val
= mem_alloc( nb_values
* sizeof(*new_val
) ))) return 0;
1055 key
->values
= new_val
;
1056 key
->nb_values
= nb_values
;
1060 /* find the named value of a given key and return its index in the array */
1061 static struct key_value
*find_value( const struct key
*key
, const struct unicode_str
*name
, int *index
)
1063 int i
, min
, max
, res
;
1067 max
= key
->last_value
;
1070 i
= (min
+ max
) / 2;
1071 len
= min( key
->values
[i
].namelen
, name
->len
);
1072 res
= memicmp_strW( key
->values
[i
].name
, name
->str
, len
);
1073 if (!res
) res
= key
->values
[i
].namelen
- name
->len
;
1077 return &key
->values
[i
];
1079 if (res
> 0) max
= i
- 1;
1082 *index
= min
; /* this is where we should insert it */
1086 /* insert a new value; the index must have been returned by find_value */
1087 static struct key_value
*insert_value( struct key
*key
, const struct unicode_str
*name
, int index
)
1089 struct key_value
*value
;
1090 WCHAR
*new_name
= NULL
;
1093 if (name
->len
> MAX_VALUE_LEN
* sizeof(WCHAR
))
1095 set_error( STATUS_NAME_TOO_LONG
);
1098 if (key
->last_value
+ 1 == key
->nb_values
)
1100 if (!grow_values( key
)) return NULL
;
1102 if (name
->len
&& !(new_name
= memdup( name
->str
, name
->len
))) return NULL
;
1103 for (i
= ++key
->last_value
; i
> index
; i
--) key
->values
[i
] = key
->values
[i
- 1];
1104 value
= &key
->values
[index
];
1105 value
->name
= new_name
;
1106 value
->namelen
= name
->len
;
1112 /* set a key value */
1113 static void set_value( struct key
*key
, const struct unicode_str
*name
,
1114 int type
, const void *data
, data_size_t len
)
1116 struct key_value
*value
;
1120 if ((value
= find_value( key
, name
, &index
)))
1122 /* check if the new value is identical to the existing one */
1123 if (value
->type
== type
&& value
->len
== len
&&
1124 value
->data
&& !memcmp( value
->data
, data
, len
))
1126 if (debug_level
> 1) dump_operation( key
, value
, "Skip setting" );
1131 if (key
->flags
& KEY_SYMLINK
)
1133 if (type
!= REG_LINK
|| name
->len
!= symlink_str
.len
||
1134 memicmp_strW( name
->str
, symlink_str
.str
, name
->len
))
1136 set_error( STATUS_ACCESS_DENIED
);
1141 if (len
&& !(ptr
= memdup( data
, len
))) return;
1145 if (!(value
= insert_value( key
, name
, index
)))
1151 else free( value
->data
); /* already existing, free previous data */
1156 touch_key( key
, REG_NOTIFY_CHANGE_LAST_SET
);
1157 if (debug_level
> 1) dump_operation( key
, value
, "Set" );
1160 /* get a key value */
1161 static void get_value( struct key
*key
, const struct unicode_str
*name
, int *type
, data_size_t
*len
)
1163 struct key_value
*value
;
1166 if ((value
= find_value( key
, name
, &index
)))
1168 *type
= value
->type
;
1170 if (value
->data
) set_reply_data( value
->data
, min( value
->len
, get_reply_max_size() ));
1171 if (debug_level
> 1) dump_operation( key
, value
, "Get" );
1176 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
1180 /* enumerate a key value */
1181 static void enum_value( struct key
*key
, int i
, int info_class
, struct enum_key_value_reply
*reply
)
1183 struct key_value
*value
;
1185 if (i
< 0 || i
> key
->last_value
) set_error( STATUS_NO_MORE_ENTRIES
);
1189 data_size_t namelen
, maxlen
;
1191 value
= &key
->values
[i
];
1192 reply
->type
= value
->type
;
1193 namelen
= value
->namelen
;
1197 case KeyValueBasicInformation
:
1198 reply
->total
= namelen
;
1200 case KeyValueFullInformation
:
1201 reply
->total
= namelen
+ value
->len
;
1203 case KeyValuePartialInformation
:
1204 reply
->total
= value
->len
;
1208 set_error( STATUS_INVALID_PARAMETER
);
1212 maxlen
= min( reply
->total
, get_reply_max_size() );
1213 if (maxlen
&& ((data
= set_reply_data_size( maxlen
))))
1215 if (maxlen
> namelen
)
1217 reply
->namelen
= namelen
;
1218 memcpy( data
, value
->name
, namelen
);
1219 memcpy( (char *)data
+ namelen
, value
->data
, maxlen
- namelen
);
1223 reply
->namelen
= maxlen
;
1224 memcpy( data
, value
->name
, maxlen
);
1227 if (debug_level
> 1) dump_operation( key
, value
, "Enum" );
1231 /* delete a value */
1232 static void delete_value( struct key
*key
, const struct unicode_str
*name
)
1234 struct key_value
*value
;
1235 int i
, index
, nb_values
;
1237 if (!(value
= find_value( key
, name
, &index
)))
1239 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
1242 if (debug_level
> 1) dump_operation( key
, value
, "Delete" );
1243 free( value
->name
);
1244 free( value
->data
);
1245 for (i
= index
; i
< key
->last_value
; i
++) key
->values
[i
] = key
->values
[i
+ 1];
1247 touch_key( key
, REG_NOTIFY_CHANGE_LAST_SET
);
1249 /* try to shrink the array */
1250 nb_values
= key
->nb_values
;
1251 if (nb_values
> MIN_VALUES
&& key
->last_value
< nb_values
/ 2)
1253 struct key_value
*new_val
;
1254 nb_values
-= nb_values
/ 3; /* shrink by 33% */
1255 if (nb_values
< MIN_VALUES
) nb_values
= MIN_VALUES
;
1256 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) ))) return;
1257 key
->values
= new_val
;
1258 key
->nb_values
= nb_values
;
1262 /* get the registry key corresponding to an hkey handle */
1263 static struct key
*get_hkey_obj( obj_handle_t hkey
, unsigned int access
)
1265 struct key
*key
= (struct key
*)get_handle_obj( current
->process
, hkey
, access
, &key_ops
);
1267 if (key
&& key
->flags
& KEY_DELETED
)
1269 set_error( STATUS_KEY_DELETED
);
1270 release_object( key
);
1276 /* get the registry key corresponding to a parent key handle */
1277 static inline struct key
*get_parent_hkey_obj( obj_handle_t hkey
)
1279 if (!hkey
) return (struct key
*)grab_object( root_key
);
1280 return get_hkey_obj( hkey
, 0 );
1283 /* read a line from the input file */
1284 static int read_next_line( struct file_load_info
*info
)
1287 int newlen
, pos
= 0;
1292 if (!fgets( info
->buffer
+ pos
, info
->len
- pos
, info
->file
))
1293 return (pos
!= 0); /* EOF */
1294 pos
= strlen(info
->buffer
);
1295 if (info
->buffer
[pos
-1] == '\n')
1297 /* got a full line */
1298 info
->buffer
[--pos
] = 0;
1299 if (pos
> 0 && info
->buffer
[pos
-1] == '\r') info
->buffer
[pos
-1] = 0;
1302 if (pos
< info
->len
- 1) return 1; /* EOF but something was read */
1304 /* need to enlarge the buffer */
1305 newlen
= info
->len
+ info
->len
/ 2;
1306 if (!(newbuf
= realloc( info
->buffer
, newlen
)))
1308 set_error( STATUS_NO_MEMORY
);
1311 info
->buffer
= newbuf
;
1316 /* make sure the temp buffer holds enough space */
1317 static int get_file_tmp_space( struct file_load_info
*info
, size_t size
)
1320 if (info
->tmplen
>= size
) return 1;
1321 if (!(tmp
= realloc( info
->tmp
, size
)))
1323 set_error( STATUS_NO_MEMORY
);
1327 info
->tmplen
= size
;
1331 /* report an error while loading an input file */
1332 static void file_read_error( const char *err
, struct file_load_info
*info
)
1335 fprintf( stderr
, "%s:%d: %s '%s'\n", info
->filename
, info
->line
, err
, info
->buffer
);
1337 fprintf( stderr
, "<fd>:%d: %s '%s'\n", info
->line
, err
, info
->buffer
);
1340 /* convert a data type tag to a value type */
1341 static int get_data_type( const char *buffer
, int *type
, int *parse_type
)
1343 struct data_type
{ const char *tag
; int len
; int type
; int parse_type
; };
1345 static const struct data_type data_types
[] =
1346 { /* actual type */ /* type to assume for parsing */
1347 { "\"", 1, REG_SZ
, REG_SZ
},
1348 { "str:\"", 5, REG_SZ
, REG_SZ
},
1349 { "str(2):\"", 8, REG_EXPAND_SZ
, REG_SZ
},
1350 { "str(7):\"", 8, REG_MULTI_SZ
, REG_SZ
},
1351 { "hex:", 4, REG_BINARY
, REG_BINARY
},
1352 { "dword:", 6, REG_DWORD
, REG_DWORD
},
1353 { "hex(", 4, -1, REG_BINARY
},
1357 const struct data_type
*ptr
;
1360 for (ptr
= data_types
; ptr
->tag
; ptr
++)
1362 if (strncmp( ptr
->tag
, buffer
, ptr
->len
)) continue;
1363 *parse_type
= ptr
->parse_type
;
1364 if ((*type
= ptr
->type
) != -1) return ptr
->len
;
1365 /* "hex(xx):" is special */
1366 *type
= (int)strtoul( buffer
+ 4, &end
, 16 );
1367 if ((end
<= buffer
) || strncmp( end
, "):", 2 )) return 0;
1368 return end
+ 2 - buffer
;
1373 /* load and create a key from the input file */
1374 static struct key
*load_key( struct key
*base
, const char *buffer
, int prefix_len
,
1375 struct file_load_info
*info
, timeout_t
*modif
)
1378 struct unicode_str name
;
1383 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return NULL
;
1386 if ((res
= parse_strW( info
->tmp
, &len
, buffer
, ']' )) == -1)
1388 file_read_error( "Malformed key", info
);
1391 if (sscanf( buffer
+ res
, " %u", &mod
) == 1)
1392 *modif
= (timeout_t
)mod
* TICKS_PER_SEC
+ ticks_1601_to_1970
;
1394 *modif
= current_time
;
1397 while (prefix_len
&& *p
) { if (*p
++ == '\\') prefix_len
--; }
1403 file_read_error( "Malformed key", info
);
1406 /* empty key name, return base key */
1407 return (struct key
*)grab_object( base
);
1410 name
.len
= len
- (p
- info
->tmp
+ 1) * sizeof(WCHAR
);
1411 return create_key_recursive( base
, &name
, 0 );
1414 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1415 static void update_key_time( struct key
*key
, timeout_t modif
)
1417 while (key
&& !key
->modif
)
1424 /* load a global option from the input file */
1425 static int load_global_option( const char *buffer
, struct file_load_info
*info
)
1429 if (!strncmp( buffer
, "#arch=", 6 ))
1431 enum prefix_type type
;
1433 if (!strcmp( p
, "win32" )) type
= PREFIX_32BIT
;
1434 else if (!strcmp( p
, "win64" )) type
= PREFIX_64BIT
;
1437 file_read_error( "Unknown architecture", info
);
1438 set_error( STATUS_NOT_REGISTRY_FILE
);
1441 if (prefix_type
== PREFIX_UNKNOWN
) prefix_type
= type
;
1442 else if (type
!= prefix_type
)
1444 file_read_error( "Mismatched architecture", info
);
1445 set_error( STATUS_NOT_REGISTRY_FILE
);
1449 /* ignore unknown options */
1453 /* load a key option from the input file */
1454 static int load_key_option( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1459 if (!strncmp( buffer
, "#time=", 6 ))
1461 timeout_t modif
= 0;
1462 for (p
= buffer
+ 6; *p
; p
++)
1464 if (*p
>= '0' && *p
<= '9') modif
= (modif
<< 4) | (*p
- '0');
1465 else if (*p
>= 'A' && *p
<= 'F') modif
= (modif
<< 4) | (*p
- 'A' + 10);
1466 else if (*p
>= 'a' && *p
<= 'f') modif
= (modif
<< 4) | (*p
- 'a' + 10);
1469 update_key_time( key
, modif
);
1471 if (!strncmp( buffer
, "#class=", 7 ))
1474 if (*p
++ != '"') return 0;
1475 if (!get_file_tmp_space( info
, strlen(p
) * sizeof(WCHAR
) )) return 0;
1477 if (parse_strW( info
->tmp
, &len
, p
, '\"' ) == -1) return 0;
1479 if (!(key
->class = memdup( info
->tmp
, len
))) len
= 0;
1480 key
->classlen
= len
;
1482 if (!strncmp( buffer
, "#link", 5 )) key
->flags
|= KEY_SYMLINK
;
1483 /* ignore unknown options */
1487 /* parse a comma-separated list of hex digits */
1488 static int parse_hex( unsigned char *dest
, data_size_t
*len
, const char *buffer
)
1490 const char *p
= buffer
;
1491 data_size_t count
= 0;
1494 while (isxdigit(*p
))
1496 unsigned int val
= strtoul( p
, &end
, 16 );
1497 if (end
== p
|| val
> 0xff) return -1;
1498 if (count
++ >= *len
) return -1; /* dest buffer overflow */
1501 while (isspace(*p
)) p
++;
1503 while (isspace(*p
)) p
++;
1509 /* parse a value name and create the corresponding value */
1510 static struct key_value
*parse_value_name( struct key
*key
, const char *buffer
, data_size_t
*len
,
1511 struct file_load_info
*info
)
1513 struct key_value
*value
;
1514 struct unicode_str name
;
1517 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return NULL
;
1518 name
.str
= info
->tmp
;
1519 name
.len
= info
->tmplen
;
1520 if (buffer
[0] == '@')
1527 int r
= parse_strW( info
->tmp
, &name
.len
, buffer
+ 1, '\"' );
1528 if (r
== -1) goto error
;
1529 *len
= r
+ 1; /* for initial quote */
1530 name
.len
-= sizeof(WCHAR
); /* terminating null */
1532 while (isspace(buffer
[*len
])) (*len
)++;
1533 if (buffer
[*len
] != '=') goto error
;
1535 while (isspace(buffer
[*len
])) (*len
)++;
1536 if (!(value
= find_value( key
, &name
, &index
))) value
= insert_value( key
, &name
, index
);
1540 file_read_error( "Malformed value name", info
);
1544 /* load a value from the input file */
1545 static int load_value( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1549 int res
, type
, parse_type
;
1550 data_size_t maxlen
, len
;
1551 struct key_value
*value
;
1553 if (!(value
= parse_value_name( key
, buffer
, &len
, info
))) return 0;
1554 if (!(res
= get_data_type( buffer
+ len
, &type
, &parse_type
))) goto error
;
1555 buffer
+= len
+ res
;
1560 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return 0;
1562 if ((res
= parse_strW( info
->tmp
, &len
, buffer
, '\"' )) == -1) goto error
;
1566 dw
= strtoul( buffer
, NULL
, 16 );
1570 case REG_BINARY
: /* hex digits */
1574 maxlen
= 1 + strlen(buffer
) / 2; /* at least 2 chars for one hex byte */
1575 if (!get_file_tmp_space( info
, len
+ maxlen
)) return 0;
1576 if ((res
= parse_hex( (unsigned char *)info
->tmp
+ len
, &maxlen
, buffer
)) == -1) goto error
;
1579 while (isspace(*buffer
)) buffer
++;
1580 if (!*buffer
) break;
1581 if (*buffer
!= '\\') goto error
;
1582 if (read_next_line( info
) != 1) goto error
;
1583 buffer
= info
->buffer
;
1584 while (isspace(*buffer
)) buffer
++;
1590 ptr
= NULL
; /* keep compiler quiet */
1594 if (!len
) newptr
= NULL
;
1595 else if (!(newptr
= memdup( ptr
, len
))) return 0;
1597 free( value
->data
);
1598 value
->data
= newptr
;
1604 file_read_error( "Malformed value", info
);
1605 free( value
->data
);
1608 value
->type
= REG_NONE
;
1612 /* return the length (in path elements) of name that is part of the key name */
1613 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1614 static int get_prefix_len( struct key
*key
, const char *name
, struct file_load_info
*info
)
1620 if (!get_file_tmp_space( info
, strlen(name
) * sizeof(WCHAR
) )) return 0;
1623 if ((res
= parse_strW( info
->tmp
, &len
, name
, ']' )) == -1)
1625 file_read_error( "Malformed key", info
);
1628 for (p
= info
->tmp
; *p
; p
++) if (*p
== '\\') break;
1629 len
= (p
- info
->tmp
) * sizeof(WCHAR
);
1630 for (res
= 1; key
!= root_key
; res
++)
1632 if (len
== key
->namelen
&& !memicmp_strW( info
->tmp
, key
->name
, len
)) break;
1635 if (key
== root_key
) res
= 0; /* no matching name */
1639 /* load all the keys from the input file */
1640 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1641 static void load_keys( struct key
*key
, const char *filename
, FILE *f
, int prefix_len
)
1643 struct key
*subkey
= NULL
;
1644 struct file_load_info info
;
1645 timeout_t modif
= current_time
;
1648 info
.filename
= filename
;
1653 if (!(info
.buffer
= mem_alloc( info
.len
))) return;
1654 if (!(info
.tmp
= mem_alloc( info
.tmplen
)))
1656 free( info
.buffer
);
1660 if ((read_next_line( &info
) != 1) ||
1661 strcmp( info
.buffer
, "WINE REGISTRY Version 2" ))
1663 set_error( STATUS_NOT_REGISTRY_FILE
);
1667 while (read_next_line( &info
) == 1)
1670 while (*p
&& isspace(*p
)) p
++;
1673 case '[': /* new key */
1676 update_key_time( subkey
, modif
);
1677 release_object( subkey
);
1679 if (prefix_len
== -1) prefix_len
= get_prefix_len( key
, p
+ 1, &info
);
1680 if (!(subkey
= load_key( key
, p
+ 1, prefix_len
, &info
, &modif
)))
1681 file_read_error( "Error creating key", &info
);
1683 case '@': /* default value */
1684 case '\"': /* value */
1685 if (subkey
) load_value( subkey
, p
, &info
);
1686 else file_read_error( "Value without key", &info
);
1688 case '#': /* option */
1689 if (subkey
) load_key_option( subkey
, p
, &info
);
1690 else if (!load_global_option( p
, &info
)) goto done
;
1692 case ';': /* comment */
1693 case 0: /* empty line */
1696 file_read_error( "Unrecognized input", &info
);
1704 update_key_time( subkey
, modif
);
1705 release_object( subkey
);
1707 free( info
.buffer
);
1711 /* load a part of the registry from a file */
1712 static void load_registry( struct key
*key
, obj_handle_t handle
)
1717 if (!(file
= get_file_obj( current
->process
, handle
, FILE_READ_DATA
))) return;
1718 fd
= dup( get_file_unix_fd( file
) );
1719 release_object( file
);
1722 FILE *f
= fdopen( fd
, "r" );
1725 load_keys( key
, NULL
, f
, -1 );
1728 else file_set_error();
1732 /* load one of the initial registry files */
1733 static int load_init_registry_from_file( const char *filename
, struct key
*key
)
1737 if ((f
= fopen( filename
, "r" )))
1739 load_keys( key
, filename
, f
, 0 );
1741 if (get_error() == STATUS_NOT_REGISTRY_FILE
)
1743 fprintf( stderr
, "%s is not a valid registry file\n", filename
);
1748 assert( save_branch_count
< MAX_SAVE_BRANCH_INFO
);
1750 save_branch_info
[save_branch_count
].path
= filename
;
1751 save_branch_info
[save_branch_count
++].key
= (struct key
*)grab_object( key
);
1752 make_object_permanent( &key
->obj
);
1756 static WCHAR
*format_user_registry_path( const SID
*sid
, struct unicode_str
*path
)
1758 char buffer
[7 + 11 + 11 + 11 * SID_MAX_SUB_AUTHORITIES
], *p
= buffer
;
1761 p
+= sprintf( p
, "User\\S-%u-%u", sid
->Revision
,
1762 MAKELONG( MAKEWORD( sid
->IdentifierAuthority
.Value
[5],
1763 sid
->IdentifierAuthority
.Value
[4] ),
1764 MAKEWORD( sid
->IdentifierAuthority
.Value
[3],
1765 sid
->IdentifierAuthority
.Value
[2] )));
1766 for (i
= 0; i
< sid
->SubAuthorityCount
; i
++) p
+= sprintf( p
, "-%u", sid
->SubAuthority
[i
] );
1767 return ascii_to_unicode_str( buffer
, path
);
1770 /* get the cpu architectures that can be supported in the current prefix */
1771 unsigned int get_prefix_cpu_mask(void)
1773 /* Allowed server/client/prefix combinations:
1777 * server +------+------+ client
1779 * 32 +------+------+---
1780 * | fail | fail | 64
1781 * ---+------+------+---
1783 * 64 +------+------+---
1785 * ---+------+------+---
1787 switch (prefix_type
)
1790 /* 64-bit prefix requires 64-bit server */
1791 return sizeof(void *) > sizeof(int) ? ~0 : 0;
1794 return ~CPU_64BIT_MASK
; /* only 32-bit cpus supported on 32-bit prefix */
1798 /* registry initialisation */
1799 void init_registry(void)
1801 static const WCHAR HKLM
[] = { 'M','a','c','h','i','n','e' };
1802 static const WCHAR HKU_default
[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1803 static const WCHAR classes
[] = {'S','o','f','t','w','a','r','e','\\',
1804 'C','l','a','s','s','e','s','\\',
1805 'W','o','w','6','4','3','2','N','o','d','e'};
1806 static const struct unicode_str root_name
= { NULL
, 0 };
1807 static const struct unicode_str HKLM_name
= { HKLM
, sizeof(HKLM
) };
1808 static const struct unicode_str HKU_name
= { HKU_default
, sizeof(HKU_default
) };
1809 static const struct unicode_str classes_name
= { classes
, sizeof(classes
) };
1811 WCHAR
*current_user_path
;
1812 struct unicode_str current_user_str
;
1813 struct key
*key
, *hklm
, *hkcu
;
1816 /* switch to the config dir */
1818 if (fchdir( config_dir_fd
) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno
));
1820 /* create the root key */
1821 root_key
= alloc_key( &root_name
, current_time
);
1823 make_object_permanent( &root_key
->obj
);
1825 /* load system.reg into Registry\Machine */
1827 if (!(hklm
= create_key_recursive( root_key
, &HKLM_name
, current_time
)))
1828 fatal_error( "could not create Machine registry key\n" );
1830 if (!load_init_registry_from_file( "system.reg", hklm
))
1832 if ((p
= getenv( "WINEARCH" )) && !strcmp( p
, "win32" ))
1833 prefix_type
= PREFIX_32BIT
;
1835 prefix_type
= sizeof(void *) > sizeof(int) ? PREFIX_64BIT
: PREFIX_32BIT
;
1837 else if (prefix_type
== PREFIX_UNKNOWN
)
1838 prefix_type
= PREFIX_32BIT
;
1840 /* load userdef.reg into Registry\User\.Default */
1842 if (!(key
= create_key_recursive( root_key
, &HKU_name
, current_time
)))
1843 fatal_error( "could not create User\\.Default registry key\n" );
1845 load_init_registry_from_file( "userdef.reg", key
);
1846 release_object( key
);
1848 /* load user.reg into HKEY_CURRENT_USER */
1850 /* FIXME: match default user in token.c. should get from process token instead */
1851 current_user_path
= format_user_registry_path( security_local_user_sid
, ¤t_user_str
);
1852 if (!current_user_path
||
1853 !(hkcu
= create_key_recursive( root_key
, ¤t_user_str
, current_time
)))
1854 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1855 free( current_user_path
);
1856 load_init_registry_from_file( "user.reg", hkcu
);
1858 /* set the shared flag on Software\Classes\Wow6432Node */
1859 if (prefix_type
== PREFIX_64BIT
)
1861 if ((key
= create_key_recursive( hklm
, &classes_name
, current_time
)))
1863 key
->flags
|= KEY_WOWSHARE
;
1864 release_object( key
);
1866 /* FIXME: handle HKCU too */
1869 release_object( hklm
);
1870 release_object( hkcu
);
1872 /* start the periodic save timer */
1873 set_periodic_save_timer();
1875 /* create windows directories */
1877 if (!mkdir( "drive_c/windows", 0777 ))
1879 mkdir( "drive_c/windows/system32", 0777 );
1880 if (prefix_type
== PREFIX_64BIT
) mkdir( "drive_c/windows/syswow64", 0777 );
1883 /* go back to the server dir */
1884 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
1887 /* save a registry branch to a file */
1888 static void save_all_subkeys( struct key
*key
, FILE *f
)
1890 fprintf( f
, "WINE REGISTRY Version 2\n" );
1891 fprintf( f
, ";; All keys relative to " );
1892 dump_path( key
, NULL
, f
);
1894 switch (prefix_type
)
1897 fprintf( f
, "\n#arch=win32\n" );
1900 fprintf( f
, "\n#arch=win64\n" );
1905 save_subkeys( key
, key
, f
);
1908 /* save a registry branch to a file handle */
1909 static void save_registry( struct key
*key
, obj_handle_t handle
)
1914 if (!(file
= get_file_obj( current
->process
, handle
, FILE_WRITE_DATA
))) return;
1915 fd
= dup( get_file_unix_fd( file
) );
1916 release_object( file
);
1919 FILE *f
= fdopen( fd
, "w" );
1922 save_all_subkeys( key
, f
);
1923 if (fclose( f
)) file_set_error();
1933 /* save a registry branch to a file */
1934 static int save_branch( struct key
*key
, const char *path
)
1937 char *p
, *tmp
= NULL
;
1938 int fd
, count
= 0, ret
= 0;
1941 if (!(key
->flags
& KEY_DIRTY
))
1943 if (debug_level
> 1) dump_operation( key
, NULL
, "Not saving clean" );
1947 /* test the file type */
1949 if ((fd
= open( path
, O_WRONLY
)) != -1)
1951 /* if file is not a regular file or has multiple links or is accessed
1952 * via symbolic links, write directly into it; otherwise use a temp file */
1953 if (!lstat( path
, &st
) && (!S_ISREG(st
.st_mode
) || st
.st_nlink
> 1))
1961 /* create a temp file in the same directory */
1963 if (!(tmp
= malloc( strlen(path
) + 20 ))) goto done
;
1964 strcpy( tmp
, path
);
1965 if ((p
= strrchr( tmp
, '/' ))) p
++;
1969 sprintf( p
, "reg%lx%04x.tmp", (long) getpid(), count
++ );
1970 if ((fd
= open( tmp
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666 )) != -1) break;
1971 if (errno
!= EEXIST
) goto done
;
1975 /* now save to it */
1978 if (!(f
= fdopen( fd
, "w" )))
1980 if (tmp
) unlink( tmp
);
1985 if (debug_level
> 1)
1987 fprintf( stderr
, "%s: ", path
);
1988 dump_operation( key
, NULL
, "saving" );
1991 save_all_subkeys( key
, f
);
1996 /* if successfully written, rename to final name */
1997 if (ret
) ret
= !rename( tmp
, path
);
1998 if (!ret
) unlink( tmp
);
2003 if (ret
) make_clean( key
);
2007 /* periodic saving of the registry */
2008 static void periodic_save( void *arg
)
2012 if (fchdir( config_dir_fd
) == -1) return;
2013 save_timeout_user
= NULL
;
2014 for (i
= 0; i
< save_branch_count
; i
++)
2015 save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
);
2016 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
2017 set_periodic_save_timer();
2020 /* start the periodic save timer */
2021 static void set_periodic_save_timer(void)
2023 if (save_timeout_user
) remove_timeout_user( save_timeout_user
);
2024 save_timeout_user
= add_timeout_user( save_period
, periodic_save
, NULL
);
2027 /* save the modified registry branches to disk */
2028 void flush_registry(void)
2032 if (fchdir( config_dir_fd
) == -1) return;
2033 for (i
= 0; i
< save_branch_count
; i
++)
2035 if (!save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
))
2037 fprintf( stderr
, "wineserver: could not save registry branch to %s",
2038 save_branch_info
[i
].path
);
2042 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
2045 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2046 static int is_wow64_thread( struct thread
*thread
)
2048 return (prefix_type
== PREFIX_64BIT
&& !(CPU_FLAG(thread
->process
->cpu
) & CPU_64BIT_MASK
));
2052 /* create a registry key */
2053 DECL_HANDLER(create_key
)
2055 struct key
*key
= NULL
, *parent
;
2056 struct unicode_str name
, class;
2057 unsigned int access
= req
->access
;
2058 const struct security_descriptor
*sd
;
2059 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
2061 if (!objattr
) return;
2063 if (!is_wow64_thread( current
)) access
= (access
& ~KEY_WOW64_32KEY
) | KEY_WOW64_64KEY
;
2065 class.str
= get_req_data_after_objattr( objattr
, &class.len
);
2066 class.len
= (class.len
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
2068 if (!objattr
->rootdir
&& name
.len
>= sizeof(root_name
) &&
2069 !memicmp_strW( name
.str
, root_name
, sizeof(root_name
) ))
2071 name
.str
+= ARRAY_SIZE( root_name
);
2072 name
.len
-= sizeof(root_name
);
2075 /* NOTE: no access rights are required from the parent handle to create a key */
2076 if ((parent
= get_parent_hkey_obj( objattr
->rootdir
)))
2078 if ((key
= create_key( parent
, &name
, &class, req
->options
, access
,
2079 objattr
->attributes
, sd
, &reply
->created
)))
2081 reply
->hkey
= alloc_handle( current
->process
, key
, access
, objattr
->attributes
);
2082 release_object( key
);
2084 release_object( parent
);
2088 /* open a registry key */
2089 DECL_HANDLER(open_key
)
2091 struct key
*key
, *parent
;
2092 struct unicode_str name
;
2093 unsigned int access
= req
->access
;
2095 if (!is_wow64_thread( current
)) access
= (access
& ~KEY_WOW64_32KEY
) | KEY_WOW64_64KEY
;
2098 /* NOTE: no access rights are required to open the parent key, only the child key */
2099 if ((parent
= get_parent_hkey_obj( req
->parent
)))
2101 get_req_path( &name
, !req
->parent
);
2102 if ((key
= open_key( parent
, &name
, access
, req
->attributes
)))
2104 reply
->hkey
= alloc_handle( current
->process
, key
, access
, req
->attributes
);
2105 release_object( key
);
2107 release_object( parent
);
2111 /* delete a registry key */
2112 DECL_HANDLER(delete_key
)
2116 if ((key
= get_hkey_obj( req
->hkey
, DELETE
)))
2118 delete_key( key
, 0);
2119 release_object( key
);
2123 /* flush a registry key */
2124 DECL_HANDLER(flush_key
)
2126 struct key
*key
= get_hkey_obj( req
->hkey
, 0 );
2129 /* we don't need to do anything here with the current implementation */
2130 release_object( key
);
2134 /* enumerate registry subkeys */
2135 DECL_HANDLER(enum_key
)
2139 if ((key
= get_hkey_obj( req
->hkey
,
2140 req
->index
== -1 ? KEY_QUERY_VALUE
: KEY_ENUMERATE_SUB_KEYS
)))
2142 enum_key( key
, req
->index
, req
->info_class
, reply
);
2143 release_object( key
);
2147 /* set a value of a registry key */
2148 DECL_HANDLER(set_key_value
)
2151 struct unicode_str name
;
2153 if (req
->namelen
> get_req_data_size())
2155 set_error( STATUS_INVALID_PARAMETER
);
2158 name
.str
= get_req_data();
2159 name
.len
= (req
->namelen
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
2161 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
2163 data_size_t datalen
= get_req_data_size() - req
->namelen
;
2164 const char *data
= (const char *)get_req_data() + req
->namelen
;
2166 set_value( key
, &name
, req
->type
, data
, datalen
);
2167 release_object( key
);
2171 /* retrieve the value of a registry key */
2172 DECL_HANDLER(get_key_value
)
2175 struct unicode_str name
= get_req_unicode_str();
2178 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
2180 get_value( key
, &name
, &reply
->type
, &reply
->total
);
2181 release_object( key
);
2185 /* enumerate the value of a registry key */
2186 DECL_HANDLER(enum_key_value
)
2190 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
2192 enum_value( key
, req
->index
, req
->info_class
, reply
);
2193 release_object( key
);
2197 /* delete a value of a registry key */
2198 DECL_HANDLER(delete_key_value
)
2201 struct unicode_str name
= get_req_unicode_str();
2203 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
2205 delete_value( key
, &name
);
2206 release_object( key
);
2210 /* load a registry branch from a file */
2211 DECL_HANDLER(load_registry
)
2213 struct key
*key
, *parent
;
2214 struct unicode_str name
;
2215 const struct security_descriptor
*sd
;
2216 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
2218 if (!objattr
) return;
2220 if (!thread_single_check_privilege( current
, &SeRestorePrivilege
))
2222 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2226 if (!objattr
->rootdir
&& name
.len
>= sizeof(root_name
) &&
2227 !memicmp_strW( name
.str
, root_name
, sizeof(root_name
) ))
2229 name
.str
+= ARRAY_SIZE( root_name
);
2230 name
.len
-= sizeof(root_name
);
2233 if ((parent
= get_parent_hkey_obj( objattr
->rootdir
)))
2236 if ((key
= create_key( parent
, &name
, NULL
, 0, KEY_WOW64_64KEY
, 0, sd
, &dummy
)))
2238 load_registry( key
, req
->file
);
2239 release_object( key
);
2241 release_object( parent
);
2245 DECL_HANDLER(unload_registry
)
2247 struct key
*key
, *parent
;
2248 struct unicode_str name
;
2249 unsigned int access
= 0;
2251 if (!thread_single_check_privilege( current
, &SeRestorePrivilege
))
2253 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2257 if (!is_wow64_thread( current
)) access
= (access
& ~KEY_WOW64_32KEY
) | KEY_WOW64_64KEY
;
2259 if ((parent
= get_parent_hkey_obj( req
->parent
)))
2261 get_req_path( &name
, !req
->parent
);
2262 if ((key
= open_key( parent
, &name
, access
, req
->attributes
)))
2264 if (key
->obj
.handle_count
)
2265 set_error( STATUS_CANNOT_DELETE
);
2267 delete_key( key
, 1 ); /* FIXME */
2268 release_object( key
);
2270 release_object( parent
);
2274 /* save a registry branch to a file */
2275 DECL_HANDLER(save_registry
)
2279 if (!thread_single_check_privilege( current
, &SeBackupPrivilege
))
2281 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2285 if ((key
= get_hkey_obj( req
->hkey
, 0 )))
2287 save_registry( key
, req
->file
);
2288 release_object( key
);
2292 /* add a registry key change notification */
2293 DECL_HANDLER(set_registry_notification
)
2296 struct event
*event
;
2297 struct notify
*notify
;
2299 key
= get_hkey_obj( req
->hkey
, KEY_NOTIFY
);
2302 event
= get_event_obj( current
->process
, req
->event
, SYNCHRONIZE
);
2305 notify
= find_notify( key
, current
->process
, req
->hkey
);
2308 notify
= mem_alloc( sizeof(*notify
) );
2311 notify
->events
= NULL
;
2312 notify
->event_count
= 0;
2313 notify
->subtree
= req
->subtree
;
2314 notify
->filter
= req
->filter
;
2315 notify
->hkey
= req
->hkey
;
2316 notify
->process
= current
->process
;
2317 list_add_head( &key
->notify_list
, ¬ify
->entry
);
2322 struct event
**new_array
;
2324 if ((new_array
= realloc( notify
->events
, (notify
->event_count
+ 1) * sizeof(*notify
->events
) )))
2326 notify
->events
= new_array
;
2327 notify
->events
[notify
->event_count
++] = (struct event
*)grab_object( event
);
2328 reset_event( event
);
2329 set_error( STATUS_PENDING
);
2331 else set_error( STATUS_NO_MEMORY
);
2333 release_object( event
);
2335 release_object( key
);