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
37 #include <sys/types.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
*class; /* key class */
82 data_size_t classlen
; /* length of class name */
83 int last_subkey
; /* last in use subkey */
84 int nb_subkeys
; /* count of allocated subkeys */
85 struct key
**subkeys
; /* subkeys array */
86 struct key
*wow6432node
; /* Wow6432Node subkey */
87 int last_value
; /* last in use value */
88 int nb_values
; /* count of allocated values in array */
89 struct key_value
*values
; /* values array */
90 unsigned int flags
; /* flags */
91 timeout_t modif
; /* last modification time */
92 struct list notify_list
; /* list of notifications */
96 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
97 #define KEY_DELETED 0x0002 /* key has been deleted */
98 #define KEY_DIRTY 0x0004 /* key has been modified */
99 #define KEY_SYMLINK 0x0008 /* key is a symbolic link */
100 #define KEY_WOWSHARE 0x0010 /* key is a Wow64 shared key (used for Software\Classes) */
101 #define KEY_PREDEF 0x0020 /* key is marked as predefined */
103 #define OBJ_KEY_WOW64 0x100000 /* magic flag added to attributes for WoW64 redirection */
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 wow6432node
[] = {'W','o','w','6','4','3','2','N','o','d','e'};
130 static const WCHAR symlink_value
[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
131 static const struct unicode_str symlink_str
= { symlink_value
, sizeof(symlink_value
) };
133 static void set_periodic_save_timer(void);
134 static struct key_value
*find_value( const struct key
*key
, const struct unicode_str
*name
, int *index
);
136 /* information about where to save a registry branch */
137 struct save_branch_info
143 #define MAX_SAVE_BRANCH_INFO 3
144 static int save_branch_count
;
145 static struct save_branch_info save_branch_info
[MAX_SAVE_BRANCH_INFO
];
147 unsigned int supported_machines_count
= 0;
148 unsigned short supported_machines
[8];
149 unsigned short native_machine
= 0;
151 /* information about a file being loaded */
152 struct file_load_info
154 const char *filename
; /* input file name */
155 FILE *file
; /* input file */
156 char *buffer
; /* line buffer */
157 int len
; /* buffer length */
158 int line
; /* current input line */
159 WCHAR
*tmp
; /* temp buffer to use while parsing input */
160 size_t tmplen
; /* length of temp buffer */
164 static void key_dump( struct object
*obj
, int verbose
);
165 static unsigned int key_map_access( struct object
*obj
, unsigned int access
);
166 static struct security_descriptor
*key_get_sd( struct object
*obj
);
167 static WCHAR
*key_get_full_name( struct object
*obj
, data_size_t
*len
);
168 static struct object
*key_lookup_name( struct object
*obj
, struct unicode_str
*name
,
169 unsigned int attr
, struct object
*root
);
170 static int key_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
);
171 static void key_unlink_name( struct object
*obj
, struct object_name
*name
);
172 static int key_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
173 static void key_destroy( struct object
*obj
);
175 static const struct object_ops key_ops
=
177 sizeof(struct key
), /* size */
178 &key_type
, /* type */
180 no_add_queue
, /* add_queue */
181 NULL
, /* remove_queue */
183 NULL
, /* satisfied */
184 no_signal
, /* signal */
185 no_get_fd
, /* get_fd */
186 key_map_access
, /* map_access */
187 key_get_sd
, /* get_sd */
188 default_set_sd
, /* set_sd */
189 key_get_full_name
, /* get_full_name */
190 key_lookup_name
, /* lookup_name */
191 key_link_name
, /* link_name */
192 key_unlink_name
, /* unlink_name */
193 no_open_file
, /* open_file */
194 no_kernel_obj_list
, /* get_kernel_obj_list */
195 key_close_handle
, /* close_handle */
196 key_destroy
/* destroy */
200 static inline int is_wow6432node( const WCHAR
*name
, unsigned int len
)
202 len
= get_path_element( name
, len
);
203 return (len
== sizeof(wow6432node
) && !memicmp_strW( name
, wow6432node
, sizeof( wow6432node
)));
206 static inline struct key
*get_parent( const struct key
*key
)
208 struct object
*parent
= key
->obj
.name
->parent
;
210 if (!parent
|| parent
->ops
!= &key_ops
) return NULL
;
211 return (struct key
*)parent
;
215 * The registry text file format v2 used by this code is similar to the one
216 * used by REGEDIT import/export functionality, with the following differences:
217 * - strings and key names can contain \x escapes for Unicode
218 * - key names use escapes too in order to support Unicode
219 * - the modification time optionally follows the key name
220 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
223 /* dump the full path of a key */
224 static void dump_path( const struct key
*key
, const struct key
*base
, FILE *f
)
226 if (get_parent( key
) && get_parent( key
) != base
)
228 dump_path( get_parent( key
), base
, f
);
229 fprintf( f
, "\\\\" );
231 dump_strW( key
->obj
.name
->name
, key
->obj
.name
->len
, f
, "[]" );
234 /* dump a value to a text file */
235 static void dump_value( const struct key_value
*value
, FILE *f
)
243 count
= 1 + dump_strW( value
->name
, value
->namelen
, f
, "\"\"" );
244 count
+= fprintf( f
, "\"=" );
246 else count
= fprintf( f
, "@=" );
253 /* only output properly terminated strings in string format */
254 if (value
->len
< sizeof(WCHAR
)) break;
255 if (value
->len
% sizeof(WCHAR
)) break;
256 if (((WCHAR
*)value
->data
)[value
->len
/ sizeof(WCHAR
) - 1]) break;
257 if (value
->type
!= REG_SZ
) fprintf( f
, "str(%x):", value
->type
);
259 dump_strW( (WCHAR
*)value
->data
, value
->len
, f
, "\"\"" );
260 fprintf( f
, "\"\n" );
264 if (value
->len
!= sizeof(dw
)) break;
265 memcpy( &dw
, value
->data
, sizeof(dw
) );
266 fprintf( f
, "dword:%08x\n", dw
);
270 if (value
->type
== REG_BINARY
) count
+= fprintf( f
, "hex:" );
271 else count
+= fprintf( f
, "hex(%x):", value
->type
);
272 for (i
= 0; i
< value
->len
; i
++)
274 count
+= fprintf( f
, "%02x", *((unsigned char *)value
->data
+ i
) );
275 if (i
< value
->len
-1)
280 fprintf( f
, "\\\n " );
288 /* find the named child of a given key and return its index */
289 static struct key
*find_subkey( const struct key
*key
, const struct unicode_str
*name
, int *index
)
291 int i
, min
, max
, res
;
295 max
= key
->last_subkey
;
299 len
= min( key
->subkeys
[i
]->obj
.name
->len
, name
->len
);
300 res
= memicmp_strW( key
->subkeys
[i
]->obj
.name
->name
, name
->str
, len
);
301 if (!res
) res
= key
->subkeys
[i
]->obj
.name
->len
- name
->len
;
305 return key
->subkeys
[i
];
307 if (res
> 0) max
= i
- 1;
310 *index
= min
; /* this is where we should insert it */
314 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
315 static int grow_subkeys( struct key
*key
)
317 struct key
**new_subkeys
;
322 nb_subkeys
= key
->nb_subkeys
+ (key
->nb_subkeys
/ 2); /* grow by 50% */
323 if (!(new_subkeys
= realloc( key
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) )))
325 set_error( STATUS_NO_MEMORY
);
331 nb_subkeys
= MIN_SUBKEYS
;
332 if (!(new_subkeys
= mem_alloc( nb_subkeys
* sizeof(*new_subkeys
) ))) return 0;
334 key
->subkeys
= new_subkeys
;
335 key
->nb_subkeys
= nb_subkeys
;
339 /* save a registry and all its subkeys to a text file */
340 static void save_subkeys( const struct key
*key
, const struct key
*base
, FILE *f
)
344 if (key
->flags
& KEY_VOLATILE
) return;
345 /* save key if it has either some values or no subkeys, or needs special options */
346 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
347 if ((key
->last_value
>= 0) || (key
->last_subkey
== -1) || key
->class || (key
->flags
& KEY_SYMLINK
))
350 if (key
!= base
) dump_path( key
, base
, f
);
351 fprintf( f
, "] %u\n", (unsigned int)((key
->modif
- ticks_1601_to_1970
) / TICKS_PER_SEC
) );
352 fprintf( f
, "#time=%x%08x\n", (unsigned int)(key
->modif
>> 32), (unsigned int)key
->modif
);
355 fprintf( f
, "#class=\"" );
356 dump_strW( key
->class, key
->classlen
, f
, "\"\"" );
357 fprintf( f
, "\"\n" );
359 if (key
->flags
& KEY_SYMLINK
) fputs( "#link\n", f
);
360 for (i
= 0; i
<= key
->last_value
; i
++) dump_value( &key
->values
[i
], f
);
362 for (i
= 0; i
<= key
->last_subkey
; i
++) save_subkeys( key
->subkeys
[i
], base
, f
);
365 static void dump_operation( const struct key
*key
, const struct key_value
*value
, const char *op
)
367 fprintf( stderr
, "%s key ", op
);
368 if (key
) dump_path( key
, NULL
, stderr
);
369 else fprintf( stderr
, "ERROR" );
372 fprintf( stderr
, " value ");
373 dump_value( value
, stderr
);
375 else fprintf( stderr
, "\n" );
378 static void key_dump( struct object
*obj
, int verbose
)
380 struct key
*key
= (struct key
*)obj
;
381 assert( obj
->ops
== &key_ops
);
382 fprintf( stderr
, "Key flags=%x ", key
->flags
);
383 dump_path( key
, NULL
, stderr
);
384 fprintf( stderr
, "\n" );
387 /* notify waiter and maybe delete the notification */
388 static void do_notification( struct key
*key
, struct notify
*notify
, int del
)
392 for (i
= 0; i
< notify
->event_count
; ++i
)
394 set_event( notify
->events
[i
] );
395 release_object( notify
->events
[i
] );
397 free( notify
->events
);
398 notify
->events
= NULL
;
399 notify
->event_count
= 0;
403 list_remove( ¬ify
->entry
);
408 static inline struct notify
*find_notify( struct key
*key
, struct process
*process
, obj_handle_t hkey
)
410 struct notify
*notify
;
412 LIST_FOR_EACH_ENTRY( notify
, &key
->notify_list
, struct notify
, entry
)
414 if (notify
->process
== process
&& notify
->hkey
== hkey
) return notify
;
419 static unsigned int key_map_access( struct object
*obj
, unsigned int access
)
421 access
= default_map_access( obj
, access
);
422 /* filter the WOW64 masks, as they aren't real access bits */
423 return access
& ~(KEY_WOW64_64KEY
| KEY_WOW64_32KEY
);
426 static struct security_descriptor
*key_get_sd( struct object
*obj
)
428 static struct security_descriptor
*key_default_sd
;
430 if (obj
->sd
) return obj
->sd
;
437 size_t users_sid_len
= sid_len( &builtin_users_sid
);
438 size_t admins_sid_len
= sid_len( &builtin_admins_sid
);
439 size_t dacl_len
= sizeof(*dacl
) + 2 * sizeof(*ace
) + users_sid_len
+ admins_sid_len
;
441 key_default_sd
= mem_alloc( sizeof(*key_default_sd
) + 2 * admins_sid_len
+ dacl_len
);
442 key_default_sd
->control
= SE_DACL_PRESENT
;
443 key_default_sd
->owner_len
= admins_sid_len
;
444 key_default_sd
->group_len
= admins_sid_len
;
445 key_default_sd
->sacl_len
= 0;
446 key_default_sd
->dacl_len
= dacl_len
;
447 sid
= (struct sid
*)(key_default_sd
+ 1);
448 sid
= copy_sid( sid
, &builtin_admins_sid
);
449 sid
= copy_sid( sid
, &builtin_admins_sid
);
451 dacl
= (struct acl
*)((char *)(key_default_sd
+ 1) + 2 * admins_sid_len
);
452 dacl
->revision
= ACL_REVISION
;
454 dacl
->size
= dacl_len
;
457 ace
= set_ace( ace_first( dacl
), &builtin_users_sid
, ACCESS_ALLOWED_ACE_TYPE
,
458 INHERIT_ONLY_ACE
| CONTAINER_INHERIT_ACE
, GENERIC_READ
);
459 set_ace( ace_next( ace
), &builtin_admins_sid
, ACCESS_ALLOWED_ACE_TYPE
, 0, KEY_ALL_ACCESS
);
461 return key_default_sd
;
464 static WCHAR
*key_get_full_name( struct object
*obj
, data_size_t
*ret_len
)
466 struct key
*key
= (struct key
*) obj
;
468 if (key
->flags
& KEY_DELETED
)
470 set_error( STATUS_KEY_DELETED
);
473 return default_get_full_name( obj
, ret_len
);
476 static struct object
*key_lookup_name( struct object
*obj
, struct unicode_str
*name
,
477 unsigned int attr
, struct object
*root
)
479 struct key
*found
, *key
= (struct key
*)obj
;
480 struct unicode_str tmp
;
484 assert( obj
->ops
== &key_ops
);
486 if (!name
) return NULL
; /* open the key itself */
488 if (key
->flags
& KEY_DELETED
)
490 set_error( STATUS_KEY_DELETED
);
494 if (key
->flags
& KEY_SYMLINK
)
496 struct unicode_str name_left
;
497 struct key_value
*value
;
499 if (!name
->len
&& (attr
& OBJ_OPENLINK
)) return NULL
;
501 if (!(value
= find_value( key
, &symlink_str
, &index
)) ||
502 value
->len
< sizeof(WCHAR
) || *(WCHAR
*)value
->data
!= '\\')
504 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
507 tmp
.str
= value
->data
;
508 tmp
.len
= (value
->len
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
509 if ((obj
= lookup_named_object( NULL
, &tmp
, OBJ_CASE_INSENSITIVE
, &name_left
)))
511 if (!name
->len
) *name
= name_left
; /* symlink destination can be created if missing */
512 else if (name_left
.len
) /* symlink must have been resolved completely */
514 release_object( obj
);
516 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
522 if (!name
->str
) return NULL
;
525 tmp
.len
= get_path_element( name
->str
, name
->len
);
527 if (tmp
.len
> MAX_NAME_LEN
* sizeof(WCHAR
))
529 set_error( STATUS_INVALID_PARAMETER
);
533 /* skip trailing backslashes */
534 for (next
= tmp
.len
; next
< name
->len
; next
+= sizeof(WCHAR
))
535 if (name
->str
[next
/ sizeof(WCHAR
)] != '\\') break;
537 if (!(found
= find_subkey( key
, &tmp
, &index
)))
539 if ((key
->flags
& KEY_WOWSHARE
) && (attr
& OBJ_KEY_WOW64
))
541 /* try in the 64-bit parent */
542 key
= get_parent( key
);
543 if (!(found
= find_subkey( key
, &tmp
, &index
))) return grab_object( key
);
549 if (next
< name
->len
) /* path still has elements */
550 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
551 else /* only trailing backslashes */
556 if (next
< name
->len
) /* move to the next element */
558 name
->str
+= next
/ sizeof(WCHAR
);
560 if ((attr
& OBJ_KEY_WOW64
) && found
->wow6432node
&& !is_wow6432node( name
->str
, name
->len
))
561 found
= found
->wow6432node
;
568 return grab_object( found
);
571 static int key_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
573 struct key
*key
= (struct key
*)obj
;
574 struct key
*parent_key
= (struct key
*)parent
;
575 struct unicode_str tmp
;
578 if (parent
->ops
!= &key_ops
)
580 /* only the root key can be created inside a normal directory */
581 if (!root_key
) return directory_link_name( obj
, name
, parent
);
582 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
586 if (parent_key
->last_subkey
+ 1 == parent_key
->nb_subkeys
)
588 /* need to grow the array */
589 if (!grow_subkeys( parent_key
)) return 0;
591 tmp
.str
= name
->name
;
593 find_subkey( parent_key
, &tmp
, &index
);
595 for (i
= ++parent_key
->last_subkey
; i
> index
; i
--)
596 parent_key
->subkeys
[i
] = parent_key
->subkeys
[i
- 1];
597 parent_key
->subkeys
[index
] = (struct key
*)grab_object( key
);
598 if (is_wow6432node( name
->name
, name
->len
) &&
599 !is_wow6432node( parent_key
->obj
.name
->name
, parent_key
->obj
.name
->len
))
600 parent_key
->wow6432node
= key
;
601 name
->parent
= parent
;
605 static void key_unlink_name( struct object
*obj
, struct object_name
*name
)
607 struct key
*key
= (struct key
*)obj
;
608 struct key
*parent
= (struct key
*)name
->parent
;
613 if (parent
->obj
.ops
!= &key_ops
)
615 default_unlink_name( obj
, name
);
619 for (i
= 0; i
<= parent
->last_subkey
; i
++) if (parent
->subkeys
[i
] == key
) break;
620 assert( i
<= parent
->last_subkey
);
621 for ( ; i
< parent
->last_subkey
; i
++) parent
->subkeys
[i
] = parent
->subkeys
[i
+ 1];
622 parent
->last_subkey
--;
624 if (parent
->wow6432node
== key
) parent
->wow6432node
= NULL
;
625 release_object( key
);
627 /* try to shrink the array */
628 nb_subkeys
= parent
->nb_subkeys
;
629 if (nb_subkeys
> MIN_SUBKEYS
&& parent
->last_subkey
< nb_subkeys
/ 2)
631 struct key
**new_subkeys
;
632 nb_subkeys
-= nb_subkeys
/ 3; /* shrink by 33% */
633 if (nb_subkeys
< MIN_SUBKEYS
) nb_subkeys
= MIN_SUBKEYS
;
634 if (!(new_subkeys
= realloc( parent
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) ))) return;
635 parent
->subkeys
= new_subkeys
;
636 parent
->nb_subkeys
= nb_subkeys
;
640 /* close the notification associated with a handle */
641 static int key_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
643 struct key
* key
= (struct key
*) obj
;
644 struct notify
*notify
= find_notify( key
, process
, handle
);
645 if (notify
) do_notification( key
, notify
, 1 );
646 return 1; /* ok to close */
649 static void key_destroy( struct object
*obj
)
653 struct key
*key
= (struct key
*)obj
;
654 assert( obj
->ops
== &key_ops
);
657 for (i
= 0; i
<= key
->last_value
; i
++)
659 free( key
->values
[i
].name
);
660 free( key
->values
[i
].data
);
663 for (i
= 0; i
<= key
->last_subkey
; i
++)
665 key
->subkeys
[i
]->obj
.name
->parent
= NULL
;
666 release_object( key
->subkeys
[i
] );
668 free( key
->subkeys
);
669 /* unconditionally notify everything waiting on this key */
670 while ((ptr
= list_head( &key
->notify_list
)))
672 struct notify
*notify
= LIST_ENTRY( ptr
, struct notify
, entry
);
673 do_notification( key
, notify
, 1 );
677 /* allocate a key object */
678 static struct key
*create_key_object( struct object
*parent
, const struct unicode_str
*name
,
679 unsigned int attributes
, unsigned int options
, timeout_t modif
,
680 const struct security_descriptor
*sd
)
684 if (!name
->len
) return open_named_object( parent
, &key_ops
, name
, attributes
);
686 if ((key
= create_named_object( parent
, &key_ops
, name
, attributes
, sd
)))
688 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
690 /* initialize it if it didn't already exist */
694 key
->last_subkey
= -1;
697 key
->wow6432node
= NULL
;
699 key
->last_value
= -1;
702 list_init( &key
->notify_list
);
704 if (options
& REG_OPTION_CREATE_LINK
) key
->flags
|= KEY_SYMLINK
;
705 if (options
& REG_OPTION_VOLATILE
) key
->flags
|= KEY_VOLATILE
;
706 else if (parent
&& (get_parent( key
)->flags
& KEY_VOLATILE
))
708 set_error( STATUS_CHILD_MUST_BE_VOLATILE
);
709 unlink_named_object( &key
->obj
);
710 release_object( key
);
713 else key
->flags
|= KEY_DIRTY
;
719 /* mark a key and all its parents as dirty (modified) */
720 static void make_dirty( struct key
*key
)
724 if (key
->flags
& (KEY_DIRTY
|KEY_VOLATILE
)) return; /* nothing to do */
725 key
->flags
|= KEY_DIRTY
;
726 key
= get_parent( key
);
730 /* mark a key and all its subkeys as clean (not modified) */
731 static void make_clean( struct key
*key
)
735 if (key
->flags
& KEY_VOLATILE
) return;
736 if (!(key
->flags
& KEY_DIRTY
)) return;
737 key
->flags
&= ~KEY_DIRTY
;
738 for (i
= 0; i
<= key
->last_subkey
; i
++) make_clean( key
->subkeys
[i
] );
741 /* go through all the notifications and send them if necessary */
742 static void check_notify( struct key
*key
, unsigned int change
, int not_subtree
)
744 struct list
*ptr
, *next
;
746 LIST_FOR_EACH_SAFE( ptr
, next
, &key
->notify_list
)
748 struct notify
*n
= LIST_ENTRY( ptr
, struct notify
, entry
);
749 if ( ( not_subtree
|| n
->subtree
) && ( change
& n
->filter
) )
750 do_notification( key
, n
, 0 );
754 /* update key modification time */
755 static void touch_key( struct key
*key
, unsigned int change
)
757 key
->modif
= current_time
;
760 /* do notifications */
761 check_notify( key
, change
, 1 );
762 for (key
= get_parent( key
); key
; key
= get_parent( key
)) check_notify( key
, change
, 0 );
765 /* get the wow6432node key if any, grabbing it and releasing the original key */
766 static struct key
*grab_wow6432node( struct key
*key
)
768 struct key
*ret
= key
->wow6432node
;
770 if (!ret
) return key
;
772 release_object( key
);
777 static struct key
*open_key( struct key
*parent
, const struct unicode_str
*name
,
778 unsigned int access
, unsigned int attributes
)
782 if (name
->len
>= 65534)
784 set_error( STATUS_OBJECT_NAME_INVALID
);
788 if (parent
&& (access
& KEY_WOW64_32KEY
))
790 if (parent
->wow6432node
&& !is_wow6432node( name
->str
, name
->len
))
791 parent
= parent
->wow6432node
;
794 if (!(access
& KEY_WOW64_64KEY
)) attributes
|= OBJ_KEY_WOW64
;
796 if (!(key
= open_named_object( &parent
->obj
, &key_ops
, name
, attributes
))) return NULL
;
798 if (!(access
& KEY_WOW64_64KEY
)) key
= grab_wow6432node( key
);
799 if (debug_level
> 1) dump_operation( key
, NULL
, "Open" );
800 if (key
->flags
& KEY_PREDEF
) set_error( STATUS_PREDEFINED_HANDLE
);
804 /* create a subkey */
805 static struct key
*create_key( struct key
*parent
, const struct unicode_str
*name
,
806 unsigned int options
, unsigned int access
, unsigned int attributes
,
807 const struct security_descriptor
*sd
)
811 if (options
& REG_OPTION_CREATE_LINK
) attributes
= (attributes
& ~OBJ_OPENIF
) | OBJ_OPENLINK
;
813 if (parent
&& (access
& KEY_WOW64_32KEY
))
815 if (parent
->wow6432node
&& !is_wow6432node( name
->str
, name
->len
)) parent
= parent
->wow6432node
;
818 if (!(access
& KEY_WOW64_64KEY
)) attributes
|= OBJ_KEY_WOW64
;
820 if (!(key
= create_key_object( &parent
->obj
, name
, attributes
, options
, current_time
, sd
)))
823 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
825 if (key
->flags
& KEY_PREDEF
) set_error( STATUS_PREDEFINED_HANDLE
);
826 if (!(access
& KEY_WOW64_64KEY
)) key
= grab_wow6432node( key
);
830 if (parent
) touch_key( get_parent( key
), REG_NOTIFY_CHANGE_NAME
);
831 if (debug_level
> 1) dump_operation( key
, NULL
, "Create" );
836 /* recursively create a subkey (for internal use only) */
837 static struct key
*create_key_recursive( struct key
*root
, const struct unicode_str
*name
, timeout_t modif
)
839 struct key
*key
, *parent
= (struct key
*)grab_object( root
);
840 struct unicode_str tmp
;
841 const WCHAR
*str
= name
->str
;
842 data_size_t len
= name
->len
;
847 tmp
.len
= get_path_element( str
, len
);
848 key
= create_key_object( &parent
->obj
, &tmp
, OBJ_OPENIF
, 0, modif
, NULL
);
849 release_object( parent
);
850 if (!key
) return NULL
;
853 /* skip trailing \\ and move to the next element */
856 tmp
.len
+= sizeof(WCHAR
);
857 str
+= tmp
.len
/ sizeof(WCHAR
);
865 /* query information about a key or a subkey */
866 static void enum_key( struct key
*key
, int index
, int info_class
, struct enum_key_reply
*reply
)
869 data_size_t len
, namelen
, classlen
;
870 data_size_t max_subkey
= 0, max_class
= 0;
871 data_size_t max_value
= 0, max_data
= 0;
872 WCHAR
*fullname
= NULL
;
875 if (key
->flags
& KEY_PREDEF
)
877 set_error( STATUS_INVALID_HANDLE
);
881 if (index
!= -1) /* -1 means use the specified key directly */
883 if ((index
< 0) || (index
> key
->last_subkey
))
885 set_error( STATUS_NO_MORE_ENTRIES
);
888 key
= key
->subkeys
[index
];
891 namelen
= key
->obj
.name
->len
;
892 classlen
= key
->classlen
;
896 case KeyNameInformation
:
897 if (!(fullname
= key
->obj
.ops
->get_full_name( &key
->obj
, &namelen
))) return;
899 case KeyBasicInformation
:
900 classlen
= 0; /* only return the name */
902 case KeyNodeInformation
:
903 reply
->max_subkey
= 0;
904 reply
->max_class
= 0;
905 reply
->max_value
= 0;
908 case KeyFullInformation
:
909 case KeyCachedInformation
:
910 for (i
= 0; i
<= key
->last_subkey
; i
++)
912 if (key
->subkeys
[i
]->obj
.name
->len
> max_subkey
) max_subkey
= key
->subkeys
[i
]->obj
.name
->len
;
913 if (key
->subkeys
[i
]->classlen
> max_class
) max_class
= key
->subkeys
[i
]->classlen
;
915 for (i
= 0; i
<= key
->last_value
; i
++)
917 if (key
->values
[i
].namelen
> max_value
) max_value
= key
->values
[i
].namelen
;
918 if (key
->values
[i
].len
> max_data
) max_data
= key
->values
[i
].len
;
920 reply
->max_subkey
= max_subkey
;
921 reply
->max_class
= max_class
;
922 reply
->max_value
= max_value
;
923 reply
->max_data
= max_data
;
924 reply
->namelen
= namelen
;
925 if (info_class
== KeyCachedInformation
)
926 classlen
= 0; /* don't return any data, only its size */
927 namelen
= 0; /* don't return name */
930 set_error( STATUS_INVALID_PARAMETER
);
933 reply
->subkeys
= key
->last_subkey
+ 1;
934 reply
->values
= key
->last_value
+ 1;
935 reply
->modif
= key
->modif
;
936 reply
->total
= namelen
+ classlen
;
938 len
= min( reply
->total
, get_reply_max_size() );
939 if (len
&& (data
= set_reply_data_size( len
)))
943 reply
->namelen
= namelen
;
944 memcpy( data
, key
->obj
.name
->name
, namelen
);
945 memcpy( data
+ namelen
, key
->class, len
- namelen
);
947 else if (info_class
== KeyNameInformation
)
949 reply
->namelen
= namelen
;
950 memcpy( data
, fullname
, len
);
954 reply
->namelen
= len
;
955 memcpy( data
, key
->obj
.name
->name
, len
);
959 if (debug_level
> 1) dump_operation( key
, NULL
, "Enum" );
962 /* rename a key and its values */
963 static void rename_key( struct key
*key
, const struct unicode_str
*new_name
)
965 struct object_name
*new_name_ptr
;
966 struct key
*subkey
, *parent
= get_parent( key
);
968 int i
, index
, cur_index
;
970 /* changing to a path is not allowed */
971 len
= get_path_element( new_name
->str
, new_name
->len
);
972 if (!len
|| len
!= new_name
->len
|| len
> MAX_NAME_LEN
* sizeof(WCHAR
))
974 set_error( STATUS_INVALID_PARAMETER
);
978 /* check for existing subkey with the same name */
979 if (!parent
|| (subkey
= find_subkey( parent
, new_name
, &index
)))
981 set_error( STATUS_CANNOT_DELETE
);
985 if (key
== parent
->wow6432node
|| is_wow6432node( new_name
->str
, new_name
->len
))
987 set_error( STATUS_INVALID_PARAMETER
);
991 if (!(new_name_ptr
= mem_alloc( offsetof( struct object_name
, name
[new_name
->len
/ sizeof(WCHAR
)] ))))
994 new_name_ptr
->obj
= &key
->obj
;
995 new_name_ptr
->len
= new_name
->len
;
996 new_name_ptr
->parent
= &parent
->obj
;
997 memcpy( new_name_ptr
->name
, new_name
->str
, new_name
->len
);
999 for (cur_index
= 0; cur_index
<= parent
->last_subkey
; cur_index
++)
1000 if (parent
->subkeys
[cur_index
] == key
) break;
1002 if (cur_index
< index
&& (index
- cur_index
) > 1)
1005 for (i
= cur_index
; i
< index
; ++i
) parent
->subkeys
[i
] = parent
->subkeys
[i
+1];
1007 else if (cur_index
> index
)
1009 for (i
= cur_index
; i
> index
; --i
) parent
->subkeys
[i
] = parent
->subkeys
[i
-1];
1011 parent
->subkeys
[index
] = key
;
1013 free( key
->obj
.name
);
1014 key
->obj
.name
= new_name_ptr
;
1016 if (debug_level
> 1) dump_operation( key
, NULL
, "Rename" );
1017 touch_key( key
, REG_NOTIFY_CHANGE_NAME
);
1020 /* delete a key and its values */
1021 static int delete_key( struct key
*key
, int recurse
)
1025 if (key
->flags
& KEY_DELETED
) return 1;
1027 if (!(parent
= get_parent( key
)))
1029 set_error( STATUS_ACCESS_DENIED
);
1032 if (key
->flags
& KEY_PREDEF
)
1034 set_error( STATUS_INVALID_HANDLE
);
1040 while (key
->last_subkey
>= 0)
1041 if (!delete_key( key
->subkeys
[key
->last_subkey
], 1 )) return 0;
1043 else if (key
->last_subkey
>= 0) /* we can only delete a key that has no subkeys */
1045 set_error( STATUS_ACCESS_DENIED
);
1049 if (debug_level
> 1) dump_operation( key
, NULL
, "Delete" );
1050 key
->flags
|= KEY_DELETED
;
1051 unlink_named_object( &key
->obj
);
1052 touch_key( parent
, REG_NOTIFY_CHANGE_NAME
);
1056 /* try to grow the array of values; return 1 if OK, 0 on error */
1057 static int grow_values( struct key
*key
)
1059 struct key_value
*new_val
;
1064 nb_values
= key
->nb_values
+ (key
->nb_values
/ 2); /* grow by 50% */
1065 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) )))
1067 set_error( STATUS_NO_MEMORY
);
1073 nb_values
= MIN_VALUES
;
1074 if (!(new_val
= mem_alloc( nb_values
* sizeof(*new_val
) ))) return 0;
1076 key
->values
= new_val
;
1077 key
->nb_values
= nb_values
;
1081 /* find the named value of a given key and return its index in the array */
1082 static struct key_value
*find_value( const struct key
*key
, const struct unicode_str
*name
, int *index
)
1084 int i
, min
, max
, res
;
1088 max
= key
->last_value
;
1091 i
= (min
+ max
) / 2;
1092 len
= min( key
->values
[i
].namelen
, name
->len
);
1093 res
= memicmp_strW( key
->values
[i
].name
, name
->str
, len
);
1094 if (!res
) res
= key
->values
[i
].namelen
- name
->len
;
1098 return &key
->values
[i
];
1100 if (res
> 0) max
= i
- 1;
1103 *index
= min
; /* this is where we should insert it */
1107 /* insert a new value; the index must have been returned by find_value */
1108 static struct key_value
*insert_value( struct key
*key
, const struct unicode_str
*name
, int index
)
1110 struct key_value
*value
;
1111 WCHAR
*new_name
= NULL
;
1114 if (name
->len
> MAX_VALUE_LEN
* sizeof(WCHAR
))
1116 set_error( STATUS_NAME_TOO_LONG
);
1119 if (key
->last_value
+ 1 == key
->nb_values
)
1121 if (!grow_values( key
)) return NULL
;
1123 if (name
->len
&& !(new_name
= memdup( name
->str
, name
->len
))) return NULL
;
1124 for (i
= ++key
->last_value
; i
> index
; i
--) key
->values
[i
] = key
->values
[i
- 1];
1125 value
= &key
->values
[index
];
1126 value
->name
= new_name
;
1127 value
->namelen
= name
->len
;
1133 /* set a key value */
1134 static void set_value( struct key
*key
, const struct unicode_str
*name
,
1135 int type
, const void *data
, data_size_t len
)
1137 struct key_value
*value
;
1141 if (key
->flags
& KEY_PREDEF
)
1143 set_error( STATUS_INVALID_HANDLE
);
1147 if ((value
= find_value( key
, name
, &index
)))
1149 /* check if the new value is identical to the existing one */
1150 if (value
->type
== type
&& value
->len
== len
&&
1151 value
->data
&& !memcmp( value
->data
, data
, len
))
1153 if (debug_level
> 1) dump_operation( key
, value
, "Skip setting" );
1158 if (key
->flags
& KEY_SYMLINK
)
1160 if (type
!= REG_LINK
|| name
->len
!= symlink_str
.len
||
1161 memicmp_strW( name
->str
, symlink_str
.str
, name
->len
))
1163 set_error( STATUS_ACCESS_DENIED
);
1168 if (len
&& !(ptr
= memdup( data
, len
))) return;
1172 if (!(value
= insert_value( key
, name
, index
)))
1178 else free( value
->data
); /* already existing, free previous data */
1183 touch_key( key
, REG_NOTIFY_CHANGE_LAST_SET
);
1184 if (debug_level
> 1) dump_operation( key
, value
, "Set" );
1187 /* get a key value */
1188 static void get_value( struct key
*key
, const struct unicode_str
*name
, int *type
, data_size_t
*len
)
1190 struct key_value
*value
;
1193 if (key
->flags
& KEY_PREDEF
)
1195 set_error( STATUS_INVALID_HANDLE
);
1199 if ((value
= find_value( key
, name
, &index
)))
1201 *type
= value
->type
;
1203 if (value
->data
) set_reply_data( value
->data
, min( value
->len
, get_reply_max_size() ));
1204 if (debug_level
> 1) dump_operation( key
, value
, "Get" );
1209 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
1213 /* enumerate a key value */
1214 static void enum_value( struct key
*key
, int i
, int info_class
, struct enum_key_value_reply
*reply
)
1216 struct key_value
*value
;
1218 if (key
->flags
& KEY_PREDEF
)
1220 set_error( STATUS_INVALID_HANDLE
);
1224 if (i
< 0 || i
> key
->last_value
) set_error( STATUS_NO_MORE_ENTRIES
);
1228 data_size_t namelen
, maxlen
;
1230 value
= &key
->values
[i
];
1231 reply
->type
= value
->type
;
1232 namelen
= value
->namelen
;
1236 case KeyValueBasicInformation
:
1237 reply
->total
= namelen
;
1239 case KeyValueFullInformation
:
1240 reply
->total
= namelen
+ value
->len
;
1242 case KeyValuePartialInformation
:
1243 reply
->total
= value
->len
;
1247 set_error( STATUS_INVALID_PARAMETER
);
1251 maxlen
= min( reply
->total
, get_reply_max_size() );
1252 if (maxlen
&& ((data
= set_reply_data_size( maxlen
))))
1254 if (maxlen
> namelen
)
1256 reply
->namelen
= namelen
;
1257 memcpy( data
, value
->name
, namelen
);
1258 memcpy( (char *)data
+ namelen
, value
->data
, maxlen
- namelen
);
1262 reply
->namelen
= maxlen
;
1263 memcpy( data
, value
->name
, maxlen
);
1266 if (debug_level
> 1) dump_operation( key
, value
, "Enum" );
1270 /* delete a value */
1271 static void delete_value( struct key
*key
, const struct unicode_str
*name
)
1273 struct key_value
*value
;
1274 int i
, index
, nb_values
;
1276 if (key
->flags
& KEY_PREDEF
)
1278 set_error( STATUS_INVALID_HANDLE
);
1282 if (!(value
= find_value( key
, name
, &index
)))
1284 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
1287 if (debug_level
> 1) dump_operation( key
, value
, "Delete" );
1288 free( value
->name
);
1289 free( value
->data
);
1290 for (i
= index
; i
< key
->last_value
; i
++) key
->values
[i
] = key
->values
[i
+ 1];
1292 touch_key( key
, REG_NOTIFY_CHANGE_LAST_SET
);
1294 /* try to shrink the array */
1295 nb_values
= key
->nb_values
;
1296 if (nb_values
> MIN_VALUES
&& key
->last_value
< nb_values
/ 2)
1298 struct key_value
*new_val
;
1299 nb_values
-= nb_values
/ 3; /* shrink by 33% */
1300 if (nb_values
< MIN_VALUES
) nb_values
= MIN_VALUES
;
1301 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) ))) return;
1302 key
->values
= new_val
;
1303 key
->nb_values
= nb_values
;
1307 /* get the registry key corresponding to an hkey handle */
1308 static struct key
*get_hkey_obj( obj_handle_t hkey
, unsigned int access
)
1310 struct key
*key
= (struct key
*)get_handle_obj( current
->process
, hkey
, access
, &key_ops
);
1312 if (key
&& key
->flags
& KEY_DELETED
)
1314 set_error( STATUS_KEY_DELETED
);
1315 release_object( key
);
1321 /* read a line from the input file */
1322 static int read_next_line( struct file_load_info
*info
)
1325 int newlen
, pos
= 0;
1330 if (!fgets( info
->buffer
+ pos
, info
->len
- pos
, info
->file
))
1331 return (pos
!= 0); /* EOF */
1332 pos
= strlen(info
->buffer
);
1333 if (info
->buffer
[pos
-1] == '\n')
1335 /* got a full line */
1336 info
->buffer
[--pos
] = 0;
1337 if (pos
> 0 && info
->buffer
[pos
-1] == '\r') info
->buffer
[pos
-1] = 0;
1340 if (pos
< info
->len
- 1) return 1; /* EOF but something was read */
1342 /* need to enlarge the buffer */
1343 newlen
= info
->len
+ info
->len
/ 2;
1344 if (!(newbuf
= realloc( info
->buffer
, newlen
)))
1346 set_error( STATUS_NO_MEMORY
);
1349 info
->buffer
= newbuf
;
1354 /* make sure the temp buffer holds enough space */
1355 static int get_file_tmp_space( struct file_load_info
*info
, size_t size
)
1358 if (info
->tmplen
>= size
) return 1;
1359 if (!(tmp
= realloc( info
->tmp
, size
)))
1361 set_error( STATUS_NO_MEMORY
);
1365 info
->tmplen
= size
;
1369 /* report an error while loading an input file */
1370 static void file_read_error( const char *err
, struct file_load_info
*info
)
1373 fprintf( stderr
, "%s:%d: %s '%s'\n", info
->filename
, info
->line
, err
, info
->buffer
);
1375 fprintf( stderr
, "<fd>:%d: %s '%s'\n", info
->line
, err
, info
->buffer
);
1378 /* convert a data type tag to a value type */
1379 static int get_data_type( const char *buffer
, int *type
, int *parse_type
)
1381 struct data_type
{ const char *tag
; int len
; int type
; int parse_type
; };
1383 static const struct data_type data_types
[] =
1384 { /* actual type */ /* type to assume for parsing */
1385 { "\"", 1, REG_SZ
, REG_SZ
},
1386 { "str:\"", 5, REG_SZ
, REG_SZ
},
1387 { "str(2):\"", 8, REG_EXPAND_SZ
, REG_SZ
},
1388 { "str(7):\"", 8, REG_MULTI_SZ
, REG_SZ
},
1389 { "hex:", 4, REG_BINARY
, REG_BINARY
},
1390 { "dword:", 6, REG_DWORD
, REG_DWORD
},
1391 { "hex(", 4, -1, REG_BINARY
},
1395 const struct data_type
*ptr
;
1398 for (ptr
= data_types
; ptr
->tag
; ptr
++)
1400 if (strncmp( ptr
->tag
, buffer
, ptr
->len
)) continue;
1401 *parse_type
= ptr
->parse_type
;
1402 if ((*type
= ptr
->type
) != -1) return ptr
->len
;
1403 /* "hex(xx):" is special */
1404 *type
= (int)strtoul( buffer
+ 4, &end
, 16 );
1405 if ((end
<= buffer
) || strncmp( end
, "):", 2 )) return 0;
1406 return end
+ 2 - buffer
;
1411 /* load and create a key from the input file */
1412 static struct key
*load_key( struct key
*base
, const char *buffer
, int prefix_len
,
1413 struct file_load_info
*info
, timeout_t
*modif
)
1416 struct unicode_str name
;
1421 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return NULL
;
1424 if ((res
= parse_strW( info
->tmp
, &len
, buffer
, ']' )) == -1)
1426 file_read_error( "Malformed key", info
);
1429 if (sscanf( buffer
+ res
, " %u", &mod
) == 1)
1430 *modif
= (timeout_t
)mod
* TICKS_PER_SEC
+ ticks_1601_to_1970
;
1432 *modif
= current_time
;
1435 while (prefix_len
&& *p
) { if (*p
++ == '\\') prefix_len
--; }
1441 file_read_error( "Malformed key", info
);
1444 /* empty key name, return base key */
1445 return (struct key
*)grab_object( base
);
1448 name
.len
= len
- (p
- info
->tmp
+ 1) * sizeof(WCHAR
);
1449 return create_key_recursive( base
, &name
, 0 );
1452 /* update the modification time of a key (and its parents) after it has been loaded from a file */
1453 static void update_key_time( struct key
*key
, timeout_t modif
)
1455 while (key
&& !key
->modif
)
1458 key
= get_parent( key
);
1462 /* load a global option from the input file */
1463 static int load_global_option( const char *buffer
, struct file_load_info
*info
)
1467 if (!strncmp( buffer
, "#arch=", 6 ))
1469 enum prefix_type type
;
1471 if (!strcmp( p
, "win32" )) type
= PREFIX_32BIT
;
1472 else if (!strcmp( p
, "win64" )) type
= PREFIX_64BIT
;
1475 file_read_error( "Unknown architecture", info
);
1476 set_error( STATUS_NOT_REGISTRY_FILE
);
1479 if (prefix_type
== PREFIX_UNKNOWN
) prefix_type
= type
;
1480 else if (type
!= prefix_type
)
1482 file_read_error( "Mismatched architecture", info
);
1483 set_error( STATUS_NOT_REGISTRY_FILE
);
1487 /* ignore unknown options */
1491 /* load a key option from the input file */
1492 static int load_key_option( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1497 if (!strncmp( buffer
, "#time=", 6 ))
1499 timeout_t modif
= 0;
1500 for (p
= buffer
+ 6; *p
; p
++)
1502 if (*p
>= '0' && *p
<= '9') modif
= (modif
<< 4) | (*p
- '0');
1503 else if (*p
>= 'A' && *p
<= 'F') modif
= (modif
<< 4) | (*p
- 'A' + 10);
1504 else if (*p
>= 'a' && *p
<= 'f') modif
= (modif
<< 4) | (*p
- 'a' + 10);
1507 update_key_time( key
, modif
);
1509 if (!strncmp( buffer
, "#class=", 7 ))
1512 if (*p
++ != '"') return 0;
1513 if (!get_file_tmp_space( info
, strlen(p
) * sizeof(WCHAR
) )) return 0;
1515 if (parse_strW( info
->tmp
, &len
, p
, '\"' ) == -1) return 0;
1517 if (!(key
->class = memdup( info
->tmp
, len
))) len
= 0;
1518 key
->classlen
= len
;
1520 if (!strncmp( buffer
, "#link", 5 )) key
->flags
|= KEY_SYMLINK
;
1521 /* ignore unknown options */
1525 /* parse a comma-separated list of hex digits */
1526 static int parse_hex( unsigned char *dest
, data_size_t
*len
, const char *buffer
)
1528 const char *p
= buffer
;
1529 data_size_t count
= 0;
1532 while (isxdigit(*p
))
1534 unsigned int val
= strtoul( p
, &end
, 16 );
1535 if (end
== p
|| val
> 0xff) return -1;
1536 if (count
++ >= *len
) return -1; /* dest buffer overflow */
1539 while (isspace(*p
)) p
++;
1541 while (isspace(*p
)) p
++;
1547 /* parse a value name and create the corresponding value */
1548 static struct key_value
*parse_value_name( struct key
*key
, const char *buffer
, data_size_t
*len
,
1549 struct file_load_info
*info
)
1551 struct key_value
*value
;
1552 struct unicode_str name
;
1555 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return NULL
;
1556 name
.str
= info
->tmp
;
1557 name
.len
= info
->tmplen
;
1558 if (buffer
[0] == '@')
1565 int r
= parse_strW( info
->tmp
, &name
.len
, buffer
+ 1, '\"' );
1566 if (r
== -1) goto error
;
1567 *len
= r
+ 1; /* for initial quote */
1568 name
.len
-= sizeof(WCHAR
); /* terminating null */
1570 while (isspace(buffer
[*len
])) (*len
)++;
1571 if (buffer
[*len
] != '=') goto error
;
1573 while (isspace(buffer
[*len
])) (*len
)++;
1574 if (!(value
= find_value( key
, &name
, &index
))) value
= insert_value( key
, &name
, index
);
1578 file_read_error( "Malformed value name", info
);
1582 /* load a value from the input file */
1583 static int load_value( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1587 int res
, type
, parse_type
;
1588 data_size_t maxlen
, len
;
1589 struct key_value
*value
;
1591 if (!(value
= parse_value_name( key
, buffer
, &len
, info
))) return 0;
1592 if (!(res
= get_data_type( buffer
+ len
, &type
, &parse_type
))) goto error
;
1593 buffer
+= len
+ res
;
1598 if (!get_file_tmp_space( info
, strlen(buffer
) * sizeof(WCHAR
) )) return 0;
1600 if ((res
= parse_strW( info
->tmp
, &len
, buffer
, '\"' )) == -1) goto error
;
1604 dw
= strtoul( buffer
, NULL
, 16 );
1608 case REG_BINARY
: /* hex digits */
1612 maxlen
= 1 + strlen(buffer
) / 2; /* at least 2 chars for one hex byte */
1613 if (!get_file_tmp_space( info
, len
+ maxlen
)) return 0;
1614 if ((res
= parse_hex( (unsigned char *)info
->tmp
+ len
, &maxlen
, buffer
)) == -1) goto error
;
1617 while (isspace(*buffer
)) buffer
++;
1618 if (!*buffer
) break;
1619 if (*buffer
!= '\\') goto error
;
1620 if (read_next_line( info
) != 1) goto error
;
1621 buffer
= info
->buffer
;
1622 while (isspace(*buffer
)) buffer
++;
1628 ptr
= NULL
; /* keep compiler quiet */
1632 if (!len
) newptr
= NULL
;
1633 else if (!(newptr
= memdup( ptr
, len
))) return 0;
1635 free( value
->data
);
1636 value
->data
= newptr
;
1642 file_read_error( "Malformed value", info
);
1643 free( value
->data
);
1646 value
->type
= REG_NONE
;
1650 /* return the length (in path elements) of name that is part of the key name */
1651 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1652 static int get_prefix_len( struct key
*key
, const char *name
, struct file_load_info
*info
)
1658 if (!get_file_tmp_space( info
, strlen(name
) * sizeof(WCHAR
) )) return 0;
1661 if ((res
= parse_strW( info
->tmp
, &len
, name
, ']' )) == -1)
1663 file_read_error( "Malformed key", info
);
1666 for (p
= info
->tmp
; *p
; p
++) if (*p
== '\\') break;
1667 len
= (p
- info
->tmp
) * sizeof(WCHAR
);
1668 for (res
= 1; key
!= root_key
; res
++)
1670 if (len
== key
->obj
.name
->len
&& !memicmp_strW( info
->tmp
, key
->obj
.name
->name
, len
)) break;
1671 key
= get_parent( key
);
1673 if (key
== root_key
) res
= 0; /* no matching name */
1677 /* load all the keys from the input file */
1678 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1679 static void load_keys( struct key
*key
, const char *filename
, FILE *f
, int prefix_len
)
1681 struct key
*subkey
= NULL
;
1682 struct file_load_info info
;
1683 timeout_t modif
= current_time
;
1686 info
.filename
= filename
;
1691 if (!(info
.buffer
= mem_alloc( info
.len
))) return;
1692 if (!(info
.tmp
= mem_alloc( info
.tmplen
)))
1694 free( info
.buffer
);
1698 if ((read_next_line( &info
) != 1) ||
1699 strcmp( info
.buffer
, "WINE REGISTRY Version 2" ))
1701 set_error( STATUS_NOT_REGISTRY_FILE
);
1705 while (read_next_line( &info
) == 1)
1708 while (*p
&& isspace(*p
)) p
++;
1711 case '[': /* new key */
1714 update_key_time( subkey
, modif
);
1715 release_object( subkey
);
1717 if (prefix_len
== -1) prefix_len
= get_prefix_len( key
, p
+ 1, &info
);
1718 if (!(subkey
= load_key( key
, p
+ 1, prefix_len
, &info
, &modif
)))
1719 file_read_error( "Error creating key", &info
);
1721 case '@': /* default value */
1722 case '\"': /* value */
1723 if (subkey
) load_value( subkey
, p
, &info
);
1724 else file_read_error( "Value without key", &info
);
1726 case '#': /* option */
1727 if (subkey
) load_key_option( subkey
, p
, &info
);
1728 else if (!load_global_option( p
, &info
)) goto done
;
1730 case ';': /* comment */
1731 case 0: /* empty line */
1734 file_read_error( "Unrecognized input", &info
);
1742 update_key_time( subkey
, modif
);
1743 release_object( subkey
);
1745 free( info
.buffer
);
1749 /* load a part of the registry from a file */
1750 static void load_registry( struct key
*key
, obj_handle_t handle
)
1755 if (!(file
= get_file_obj( current
->process
, handle
, FILE_READ_DATA
))) return;
1756 fd
= dup( get_file_unix_fd( file
) );
1757 release_object( file
);
1760 FILE *f
= fdopen( fd
, "r" );
1763 load_keys( key
, NULL
, f
, -1 );
1766 else file_set_error();
1770 /* load one of the initial registry files */
1771 static int load_init_registry_from_file( const char *filename
, struct key
*key
)
1775 if ((f
= fopen( filename
, "r" )))
1777 load_keys( key
, filename
, f
, 0 );
1779 if (get_error() == STATUS_NOT_REGISTRY_FILE
)
1781 fprintf( stderr
, "%s is not a valid registry file\n", filename
);
1786 assert( save_branch_count
< MAX_SAVE_BRANCH_INFO
);
1788 save_branch_info
[save_branch_count
].path
= filename
;
1789 save_branch_info
[save_branch_count
++].key
= (struct key
*)grab_object( key
);
1790 make_object_permanent( &key
->obj
);
1794 static WCHAR
*format_user_registry_path( const struct sid
*sid
, struct unicode_str
*path
)
1796 char buffer
[7 + 11 + 11 + 11 * ARRAY_SIZE(sid
->sub_auth
)], *p
= buffer
;
1799 p
+= sprintf( p
, "User\\S-%u-%u", sid
->revision
,
1800 ((unsigned int)sid
->id_auth
[2] << 24) |
1801 ((unsigned int)sid
->id_auth
[3] << 16) |
1802 ((unsigned int)sid
->id_auth
[4] << 8) |
1803 ((unsigned int)sid
->id_auth
[5]) );
1804 for (i
= 0; i
< sid
->sub_count
; i
++) p
+= sprintf( p
, "-%u", sid
->sub_auth
[i
] );
1805 return ascii_to_unicode_str( buffer
, path
);
1808 static void init_supported_machines(void)
1810 unsigned int count
= 0;
1812 if (prefix_type
== PREFIX_32BIT
) supported_machines
[count
++] = IMAGE_FILE_MACHINE_I386
;
1813 #elif defined(__x86_64__)
1814 if (prefix_type
== PREFIX_64BIT
) supported_machines
[count
++] = IMAGE_FILE_MACHINE_AMD64
;
1815 supported_machines
[count
++] = IMAGE_FILE_MACHINE_I386
;
1816 #elif defined(__arm__)
1817 if (prefix_type
== PREFIX_32BIT
) supported_machines
[count
++] = IMAGE_FILE_MACHINE_ARMNT
;
1818 #elif defined(__aarch64__)
1819 if (prefix_type
== PREFIX_64BIT
)
1821 supported_machines
[count
++] = IMAGE_FILE_MACHINE_ARM64
;
1822 supported_machines
[count
++] = IMAGE_FILE_MACHINE_I386
;
1824 supported_machines
[count
++] = IMAGE_FILE_MACHINE_ARMNT
;
1826 #error Unsupported machine
1828 supported_machines_count
= count
;
1829 native_machine
= supported_machines
[0];
1832 /* registry initialisation */
1833 void init_registry(void)
1835 static const WCHAR REGISTRY
[] = {'\\','R','E','G','I','S','T','R','Y'};
1836 static const WCHAR HKLM
[] = { 'M','a','c','h','i','n','e' };
1837 static const WCHAR HKU_default
[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1838 static const WCHAR classes_i386
[] = {'S','o','f','t','w','a','r','e','\\',
1839 'C','l','a','s','s','e','s','\\',
1840 'W','o','w','6','4','3','2','N','o','d','e'};
1841 static const WCHAR classes_amd64
[] = {'S','o','f','t','w','a','r','e','\\',
1842 'C','l','a','s','s','e','s','\\',
1843 'W','o','w','6','4','6','4','N','o','d','e'};
1844 static const WCHAR classes_arm
[] = {'S','o','f','t','w','a','r','e','\\',
1845 'C','l','a','s','s','e','s','\\',
1846 'W','o','w','A','A','3','2','N','o','d','e'};
1847 static const WCHAR classes_arm64
[] = {'S','o','f','t','w','a','r','e','\\',
1848 'C','l','a','s','s','e','s','\\',
1849 'W','o','w','A','A','6','4','N','o','d','e'};
1850 static const WCHAR perflib
[] = {'S','o','f','t','w','a','r','e','\\',
1851 'M','i','c','r','o','s','o','f','t','\\',
1852 'W','i','n','d','o','w','s',' ','N','T','\\',
1853 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
1854 'P','e','r','f','l','i','b','\\',
1856 static const struct unicode_str root_name
= { REGISTRY
, sizeof(REGISTRY
) };
1857 static const struct unicode_str HKLM_name
= { HKLM
, sizeof(HKLM
) };
1858 static const struct unicode_str HKU_name
= { HKU_default
, sizeof(HKU_default
) };
1859 static const struct unicode_str perflib_name
= { perflib
, sizeof(perflib
) };
1861 WCHAR
*current_user_path
;
1862 struct unicode_str current_user_str
;
1863 struct key
*key
, *hklm
, *hkcu
;
1867 /* switch to the config dir */
1869 if (fchdir( config_dir_fd
) == -1) fatal_error( "chdir to config dir: %s\n", strerror( errno
));
1871 /* create the root key */
1872 root_key
= create_key_object( NULL
, &root_name
, OBJ_PERMANENT
, 0, current_time
, NULL
);
1874 release_object( root_key
);
1876 /* load system.reg into Registry\Machine */
1878 if (!(hklm
= create_key_recursive( root_key
, &HKLM_name
, current_time
)))
1879 fatal_error( "could not create Machine registry key\n" );
1881 if (!load_init_registry_from_file( "system.reg", hklm
))
1883 if ((p
= getenv( "WINEARCH" )) && !strcmp( p
, "win32" ))
1884 prefix_type
= PREFIX_32BIT
;
1886 prefix_type
= sizeof(void *) > sizeof(int) ? PREFIX_64BIT
: PREFIX_32BIT
;
1888 else if (prefix_type
== PREFIX_UNKNOWN
)
1889 prefix_type
= PREFIX_32BIT
;
1891 init_supported_machines();
1893 /* load userdef.reg into Registry\User\.Default */
1895 if (!(key
= create_key_recursive( root_key
, &HKU_name
, current_time
)))
1896 fatal_error( "could not create User\\.Default registry key\n" );
1898 load_init_registry_from_file( "userdef.reg", key
);
1899 release_object( key
);
1901 /* load user.reg into HKEY_CURRENT_USER */
1903 /* FIXME: match default user in token.c. should get from process token instead */
1904 current_user_path
= format_user_registry_path( &local_user_sid
, ¤t_user_str
);
1905 if (!current_user_path
||
1906 !(hkcu
= create_key_recursive( root_key
, ¤t_user_str
, current_time
)))
1907 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1908 free( current_user_path
);
1909 load_init_registry_from_file( "user.reg", hkcu
);
1911 /* set the shared flag on Software\Classes\Wow6432Node for all platforms */
1912 for (i
= 1; i
< supported_machines_count
; i
++)
1914 struct unicode_str name
;
1916 switch (supported_machines
[i
])
1918 case IMAGE_FILE_MACHINE_I386
: name
.str
= classes_i386
; name
.len
= sizeof(classes_i386
); break;
1919 case IMAGE_FILE_MACHINE_ARMNT
: name
.str
= classes_arm
; name
.len
= sizeof(classes_arm
); break;
1920 case IMAGE_FILE_MACHINE_AMD64
: name
.str
= classes_amd64
; name
.len
= sizeof(classes_amd64
); break;
1921 case IMAGE_FILE_MACHINE_ARM64
: name
.str
= classes_arm64
; name
.len
= sizeof(classes_arm64
); break;
1923 if ((key
= create_key_recursive( hklm
, &name
, current_time
)))
1925 key
->flags
|= KEY_WOWSHARE
;
1926 release_object( key
);
1928 /* FIXME: handle HKCU too */
1931 if ((key
= create_key_recursive( hklm
, &perflib_name
, current_time
)))
1933 key
->flags
|= KEY_PREDEF
;
1934 release_object( key
);
1937 release_object( hklm
);
1938 release_object( hkcu
);
1940 /* start the periodic save timer */
1941 set_periodic_save_timer();
1943 /* create windows directories */
1945 if (!mkdir( "drive_c/windows", 0777 ))
1947 mkdir( "drive_c/windows/system32", 0777 );
1948 for (i
= 1; i
< supported_machines_count
; i
++)
1950 switch (supported_machines
[i
])
1952 case IMAGE_FILE_MACHINE_I386
: mkdir( "drive_c/windows/syswow64", 0777 ); break;
1953 case IMAGE_FILE_MACHINE_ARMNT
: mkdir( "drive_c/windows/sysarm32", 0777 ); break;
1954 case IMAGE_FILE_MACHINE_AMD64
: mkdir( "drive_c/windows/sysx8664", 0777 ); break;
1955 case IMAGE_FILE_MACHINE_ARM64
: mkdir( "drive_c/windows/sysarm64", 0777 ); break;
1960 /* go back to the server dir */
1961 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
1964 /* save a registry branch to a file */
1965 static void save_all_subkeys( struct key
*key
, FILE *f
)
1967 fprintf( f
, "WINE REGISTRY Version 2\n" );
1968 fprintf( f
, ";; All keys relative to " );
1969 dump_path( key
, NULL
, f
);
1971 switch (prefix_type
)
1974 fprintf( f
, "\n#arch=win32\n" );
1977 fprintf( f
, "\n#arch=win64\n" );
1982 save_subkeys( key
, key
, f
);
1985 /* save a registry branch to a file handle */
1986 static void save_registry( struct key
*key
, obj_handle_t handle
)
1991 if (!(file
= get_file_obj( current
->process
, handle
, FILE_WRITE_DATA
))) return;
1992 fd
= dup( get_file_unix_fd( file
) );
1993 release_object( file
);
1996 FILE *f
= fdopen( fd
, "w" );
1999 save_all_subkeys( key
, f
);
2000 if (fclose( f
)) file_set_error();
2010 /* save a registry branch to a file */
2011 static int save_branch( struct key
*key
, const char *path
)
2014 char *p
, *tmp
= NULL
;
2015 int fd
, count
= 0, ret
= 0;
2018 if (!(key
->flags
& KEY_DIRTY
))
2020 if (debug_level
> 1) dump_operation( key
, NULL
, "Not saving clean" );
2024 /* test the file type */
2026 if ((fd
= open( path
, O_WRONLY
)) != -1)
2028 /* if file is not a regular file or has multiple links or is accessed
2029 * via symbolic links, write directly into it; otherwise use a temp file */
2030 if (!lstat( path
, &st
) && (!S_ISREG(st
.st_mode
) || st
.st_nlink
> 1))
2038 /* create a temp file in the same directory */
2040 if (!(tmp
= malloc( strlen(path
) + 20 ))) goto done
;
2041 strcpy( tmp
, path
);
2042 if ((p
= strrchr( tmp
, '/' ))) p
++;
2046 sprintf( p
, "reg%lx%04x.tmp", (long) getpid(), count
++ );
2047 if ((fd
= open( tmp
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666 )) != -1) break;
2048 if (errno
!= EEXIST
) goto done
;
2052 /* now save to it */
2055 if (!(f
= fdopen( fd
, "w" )))
2057 if (tmp
) unlink( tmp
);
2062 if (debug_level
> 1)
2064 fprintf( stderr
, "%s: ", path
);
2065 dump_operation( key
, NULL
, "saving" );
2068 save_all_subkeys( key
, f
);
2073 /* if successfully written, rename to final name */
2074 if (ret
) ret
= !rename( tmp
, path
);
2075 if (!ret
) unlink( tmp
);
2080 if (ret
) make_clean( key
);
2084 /* periodic saving of the registry */
2085 static void periodic_save( void *arg
)
2089 if (fchdir( config_dir_fd
) == -1) return;
2090 save_timeout_user
= NULL
;
2091 for (i
= 0; i
< save_branch_count
; i
++)
2092 save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
);
2093 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
2094 set_periodic_save_timer();
2097 /* start the periodic save timer */
2098 static void set_periodic_save_timer(void)
2100 if (save_timeout_user
) remove_timeout_user( save_timeout_user
);
2101 save_timeout_user
= add_timeout_user( save_period
, periodic_save
, NULL
);
2104 /* save the modified registry branches to disk */
2105 void flush_registry(void)
2109 if (fchdir( config_dir_fd
) == -1) return;
2110 for (i
= 0; i
< save_branch_count
; i
++)
2112 if (!save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
))
2114 fprintf( stderr
, "wineserver: could not save registry branch to %s",
2115 save_branch_info
[i
].path
);
2119 if (fchdir( server_dir_fd
) == -1) fatal_error( "chdir to server dir: %s\n", strerror( errno
));
2122 /* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
2123 static int is_wow64_thread( struct thread
*thread
)
2125 return (is_machine_64bit( native_machine
) && !is_machine_64bit( thread
->process
->machine
));
2129 /* create a registry key */
2130 DECL_HANDLER(create_key
)
2132 struct key
*key
, *parent
= NULL
;
2133 unsigned int access
= req
->access
;
2135 struct unicode_str name
;
2136 const struct security_descriptor
*sd
;
2137 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
2139 if (!objattr
) return;
2141 if (!is_wow64_thread( current
)) access
= (access
& ~KEY_WOW64_32KEY
) | KEY_WOW64_64KEY
;
2143 if (objattr
->rootdir
)
2145 if (!(parent
= get_hkey_obj( objattr
->rootdir
, 0 ))) return;
2148 if ((key
= create_key( parent
, &name
, req
->options
, access
, objattr
->attributes
, sd
)))
2150 if ((class = get_req_data_after_objattr( objattr
, &key
->classlen
)))
2152 key
->classlen
= (key
->classlen
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
2153 if (!(key
->class = memdup( class, key
->classlen
))) key
->classlen
= 0;
2155 reply
->hkey
= alloc_handle( current
->process
, key
, access
, objattr
->attributes
);
2156 release_object( key
);
2158 if (parent
) release_object( parent
);
2161 /* open a registry key */
2162 DECL_HANDLER(open_key
)
2164 struct key
*key
, *parent
= NULL
;
2165 unsigned int access
= req
->access
;
2166 struct unicode_str name
= get_req_unicode_str();
2168 if (!is_wow64_thread( current
)) access
= (access
& ~KEY_WOW64_32KEY
) | KEY_WOW64_64KEY
;
2170 if (req
->parent
&& !(parent
= get_hkey_obj( req
->parent
, 0 ))) return;
2172 if ((key
= open_key( parent
, &name
, access
, req
->attributes
)))
2174 reply
->hkey
= alloc_handle( current
->process
, key
, access
, req
->attributes
);
2175 release_object( key
);
2177 if (parent
) release_object( parent
);
2180 /* delete a registry key */
2181 DECL_HANDLER(delete_key
)
2183 struct key
*key
= (struct key
*)get_handle_obj( current
->process
, req
->hkey
, DELETE
, &key_ops
);
2187 delete_key( key
, 0 );
2188 release_object( key
);
2192 /* flush a registry key */
2193 DECL_HANDLER(flush_key
)
2195 struct key
*key
= get_hkey_obj( req
->hkey
, 0 );
2198 /* we don't need to do anything here with the current implementation */
2199 release_object( key
);
2203 /* enumerate registry subkeys */
2204 DECL_HANDLER(enum_key
)
2208 if ((key
= get_hkey_obj( req
->hkey
, req
->index
== -1 ? 0 : KEY_ENUMERATE_SUB_KEYS
)))
2210 enum_key( key
, req
->index
, req
->info_class
, reply
);
2211 release_object( key
);
2215 /* set a value of a registry key */
2216 DECL_HANDLER(set_key_value
)
2219 struct unicode_str name
;
2221 if (req
->namelen
> get_req_data_size())
2223 set_error( STATUS_INVALID_PARAMETER
);
2226 name
.str
= get_req_data();
2227 name
.len
= (req
->namelen
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
2229 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
2231 data_size_t datalen
= get_req_data_size() - req
->namelen
;
2232 const char *data
= (const char *)get_req_data() + req
->namelen
;
2234 set_value( key
, &name
, req
->type
, data
, datalen
);
2235 release_object( key
);
2239 /* retrieve the value of a registry key */
2240 DECL_HANDLER(get_key_value
)
2243 struct unicode_str name
= get_req_unicode_str();
2246 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
2248 get_value( key
, &name
, &reply
->type
, &reply
->total
);
2249 release_object( key
);
2253 /* enumerate the value of a registry key */
2254 DECL_HANDLER(enum_key_value
)
2258 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
2260 enum_value( key
, req
->index
, req
->info_class
, reply
);
2261 release_object( key
);
2265 /* delete a value of a registry key */
2266 DECL_HANDLER(delete_key_value
)
2269 struct unicode_str name
= get_req_unicode_str();
2271 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
2273 delete_value( key
, &name
);
2274 release_object( key
);
2278 /* load a registry branch from a file */
2279 DECL_HANDLER(load_registry
)
2281 struct key
*key
, *parent
= NULL
;
2282 struct unicode_str name
;
2283 const struct security_descriptor
*sd
;
2284 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, NULL
);
2286 if (!objattr
) return;
2288 if (!thread_single_check_privilege( current
, SeRestorePrivilege
))
2290 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2293 if (objattr
->rootdir
)
2295 if (!(parent
= get_hkey_obj( objattr
->rootdir
, 0 ))) return;
2298 if ((key
= create_key( parent
, &name
, 0, KEY_WOW64_64KEY
, 0, sd
)))
2300 load_registry( key
, req
->file
);
2301 release_object( key
);
2303 if (parent
) release_object( parent
);
2306 DECL_HANDLER(unload_registry
)
2308 struct key
*key
, *parent
= NULL
;
2309 struct unicode_str name
= get_req_unicode_str();
2311 if (!thread_single_check_privilege( current
, SeRestorePrivilege
))
2313 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2317 if (req
->parent
&& !(parent
= get_hkey_obj( req
->parent
, 0 ))) return;
2319 if ((key
= open_key( parent
, &name
, KEY_WOW64_64KEY
, req
->attributes
)))
2321 if (key
->obj
.handle_count
)
2322 set_error( STATUS_CANNOT_DELETE
);
2324 delete_key( key
, 1 ); /* FIXME */
2325 release_object( key
);
2327 if (parent
) release_object( parent
);
2330 /* save a registry branch to a file */
2331 DECL_HANDLER(save_registry
)
2335 if (!thread_single_check_privilege( current
, SeBackupPrivilege
))
2337 set_error( STATUS_PRIVILEGE_NOT_HELD
);
2341 if ((key
= get_hkey_obj( req
->hkey
, 0 )))
2343 save_registry( key
, req
->file
);
2344 release_object( key
);
2348 /* add a registry key change notification */
2349 DECL_HANDLER(set_registry_notification
)
2352 struct event
*event
;
2353 struct notify
*notify
;
2355 key
= get_hkey_obj( req
->hkey
, KEY_NOTIFY
);
2358 event
= get_event_obj( current
->process
, req
->event
, SYNCHRONIZE
);
2361 notify
= find_notify( key
, current
->process
, req
->hkey
);
2364 notify
= mem_alloc( sizeof(*notify
) );
2367 notify
->events
= NULL
;
2368 notify
->event_count
= 0;
2369 notify
->subtree
= req
->subtree
;
2370 notify
->filter
= req
->filter
;
2371 notify
->hkey
= req
->hkey
;
2372 notify
->process
= current
->process
;
2373 list_add_head( &key
->notify_list
, ¬ify
->entry
);
2378 struct event
**new_array
;
2380 if ((new_array
= realloc( notify
->events
, (notify
->event_count
+ 1) * sizeof(*notify
->events
) )))
2382 notify
->events
= new_array
;
2383 notify
->events
[notify
->event_count
++] = (struct event
*)grab_object( event
);
2384 reset_event( event
);
2385 set_error( STATUS_PENDING
);
2387 else set_error( STATUS_NO_MEMORY
);
2389 release_object( event
);
2391 release_object( key
);
2395 /* rename a registry key */
2396 DECL_HANDLER(rename_key
)
2398 struct unicode_str name
= get_req_unicode_str();
2401 key
= get_hkey_obj( req
->hkey
, KEY_WRITE
);
2404 rename_key( key
, &name
);
2405 release_object( key
);