2 * Server-side registry management
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
41 #define WIN32_NO_STATUS
50 #include "wine/library.h"
54 struct list entry
; /* entry in list of notifications */
55 struct event
*event
; /* event to set when changing this key */
56 int subtree
; /* true if subtree notification */
57 unsigned int filter
; /* which events to notify on */
58 obj_handle_t hkey
; /* hkey associated with this notification */
59 struct process
*process
; /* process in which the hkey is valid */
65 struct object obj
; /* object header */
66 WCHAR
*name
; /* key name */
67 WCHAR
*class; /* key class */
68 unsigned short namelen
; /* length of key name */
69 unsigned short classlen
; /* length of class name */
70 struct key
*parent
; /* parent key */
71 int last_subkey
; /* last in use subkey */
72 int nb_subkeys
; /* count of allocated subkeys */
73 struct key
**subkeys
; /* subkeys array */
74 int last_value
; /* last in use value */
75 int nb_values
; /* count of allocated values in array */
76 struct key_value
*values
; /* values array */
77 unsigned int flags
; /* flags */
78 time_t modif
; /* last modification time */
79 struct list notify_list
; /* list of notifications */
83 #define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
84 #define KEY_DELETED 0x0002 /* key has been deleted */
85 #define KEY_DIRTY 0x0004 /* key has been modified */
90 WCHAR
*name
; /* value name */
91 unsigned short namelen
; /* length of value name */
92 unsigned short type
; /* value type */
93 size_t len
; /* value data length in bytes */
94 void *data
; /* pointer to value data */
97 #define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
98 #define MIN_VALUES 8 /* min. number of allocated values per key */
100 #define MAX_NAME_LEN MAX_PATH /* max. length of a key name */
101 #define MAX_VALUE_LEN MAX_PATH /* max. length of a value name */
103 /* the root of the registry tree */
104 static struct key
*root_key
;
106 static const int save_period
= 30000; /* delay between periodic saves (in ms) */
107 static struct timeout_user
*save_timeout_user
; /* saving timer */
109 static void set_periodic_save_timer(void);
111 /* information about where to save a registry branch */
112 struct save_branch_info
118 #define MAX_SAVE_BRANCH_INFO 3
119 static int save_branch_count
;
120 static struct save_branch_info save_branch_info
[MAX_SAVE_BRANCH_INFO
];
123 /* information about a file being loaded */
124 struct file_load_info
126 const char *filename
; /* input file name */
127 FILE *file
; /* input file */
128 char *buffer
; /* line buffer */
129 int len
; /* buffer length */
130 int line
; /* current input line */
131 WCHAR
*tmp
; /* temp buffer to use while parsing input */
132 size_t tmplen
; /* length of temp buffer */
136 static void key_dump( struct object
*obj
, int verbose
);
137 static unsigned int key_map_access( struct object
*obj
, unsigned int access
);
138 static int key_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
);
139 static void key_destroy( struct object
*obj
);
141 static const struct object_ops key_ops
=
143 sizeof(struct key
), /* size */
145 no_add_queue
, /* add_queue */
146 NULL
, /* remove_queue */
148 NULL
, /* satisfied */
149 no_signal
, /* signal */
150 no_get_fd
, /* get_fd */
151 key_map_access
, /* map_access */
152 no_lookup_name
, /* lookup_name */
153 key_close_handle
, /* close_handle */
154 key_destroy
/* destroy */
159 * The registry text file format v2 used by this code is similar to the one
160 * used by REGEDIT import/export functionality, with the following differences:
161 * - strings and key names can contain \x escapes for Unicode
162 * - key names use escapes too in order to support Unicode
163 * - the modification time optionally follows the key name
164 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
167 static inline char to_hex( char ch
)
169 if (isdigit(ch
)) return ch
- '0';
170 return tolower(ch
) - 'a' + 10;
173 /* dump the full path of a key */
174 static void dump_path( const struct key
*key
, const struct key
*base
, FILE *f
)
176 if (key
->parent
&& key
->parent
!= base
)
178 dump_path( key
->parent
, base
, f
);
179 fprintf( f
, "\\\\" );
181 dump_strW( key
->name
, key
->namelen
/ sizeof(WCHAR
), f
, "[]" );
184 /* dump a value to a text file */
185 static void dump_value( const struct key_value
*value
, FILE *f
)
193 count
= 1 + dump_strW( value
->name
, value
->namelen
/ sizeof(WCHAR
), f
, "\"\"" );
194 count
+= fprintf( f
, "\"=" );
196 else count
= fprintf( f
, "@=" );
203 if (value
->type
!= REG_SZ
) fprintf( f
, "str(%d):", value
->type
);
205 if (value
->data
) dump_strW( (WCHAR
*)value
->data
, value
->len
/ sizeof(WCHAR
), f
, "\"\"" );
209 if (value
->len
== sizeof(DWORD
))
212 memcpy( &dw
, value
->data
, sizeof(DWORD
) );
213 fprintf( f
, "dword:%08lx", dw
);
216 /* else fall through */
218 if (value
->type
== REG_BINARY
) count
+= fprintf( f
, "hex:" );
219 else count
+= fprintf( f
, "hex(%x):", value
->type
);
220 for (i
= 0; i
< value
->len
; i
++)
222 count
+= fprintf( f
, "%02x", *((unsigned char *)value
->data
+ i
) );
223 if (i
< value
->len
-1)
228 fprintf( f
, "\\\n " );
238 /* save a registry and all its subkeys to a text file */
239 static void save_subkeys( const struct key
*key
, const struct key
*base
, FILE *f
)
243 if (key
->flags
& KEY_VOLATILE
) return;
244 /* save key if it has either some values or no subkeys */
245 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
246 if ((key
->last_value
>= 0) || (key
->last_subkey
== -1))
249 if (key
!= base
) dump_path( key
, base
, f
);
250 fprintf( f
, "] %ld\n", (long)key
->modif
);
251 for (i
= 0; i
<= key
->last_value
; i
++) dump_value( &key
->values
[i
], f
);
253 for (i
= 0; i
<= key
->last_subkey
; i
++) save_subkeys( key
->subkeys
[i
], base
, f
);
256 static void dump_operation( const struct key
*key
, const struct key_value
*value
, const char *op
)
258 fprintf( stderr
, "%s key ", op
);
259 if (key
) dump_path( key
, NULL
, stderr
);
260 else fprintf( stderr
, "ERROR" );
263 fprintf( stderr
, " value ");
264 dump_value( value
, stderr
);
266 else fprintf( stderr
, "\n" );
269 static void key_dump( struct object
*obj
, int verbose
)
271 struct key
*key
= (struct key
*)obj
;
272 assert( obj
->ops
== &key_ops
);
273 fprintf( stderr
, "Key flags=%x ", key
->flags
);
274 dump_path( key
, NULL
, stderr
);
275 fprintf( stderr
, "\n" );
278 /* notify waiter and maybe delete the notification */
279 static void do_notification( struct key
*key
, struct notify
*notify
, int del
)
283 set_event( notify
->event
);
284 release_object( notify
->event
);
285 notify
->event
= NULL
;
289 list_remove( ¬ify
->entry
);
294 static inline struct notify
*find_notify( struct key
*key
, struct process
*process
, obj_handle_t hkey
)
296 struct notify
*notify
;
298 LIST_FOR_EACH_ENTRY( notify
, &key
->notify_list
, struct notify
, entry
)
300 if (notify
->process
== process
&& notify
->hkey
== hkey
) return notify
;
305 static unsigned int key_map_access( struct object
*obj
, unsigned int access
)
307 if (access
& GENERIC_READ
) access
|= KEY_READ
;
308 if (access
& GENERIC_WRITE
) access
|= KEY_WRITE
;
309 if (access
& GENERIC_EXECUTE
) access
|= KEY_EXECUTE
;
310 if (access
& GENERIC_ALL
) access
|= KEY_ALL_ACCESS
;
311 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
314 /* close the notification associated with a handle */
315 static int key_close_handle( struct object
*obj
, struct process
*process
, obj_handle_t handle
)
317 struct key
* key
= (struct key
*) obj
;
318 struct notify
*notify
= find_notify( key
, process
, handle
);
319 if (notify
) do_notification( key
, notify
, 1 );
320 return 1; /* ok to close */
323 static void key_destroy( struct object
*obj
)
327 struct key
*key
= (struct key
*)obj
;
328 assert( obj
->ops
== &key_ops
);
330 if (key
->name
) free( key
->name
);
331 if (key
->class) free( key
->class );
332 for (i
= 0; i
<= key
->last_value
; i
++)
334 if (key
->values
[i
].name
) free( key
->values
[i
].name
);
335 if (key
->values
[i
].data
) free( key
->values
[i
].data
);
337 if (key
->values
) free( key
->values
);
338 for (i
= 0; i
<= key
->last_subkey
; i
++)
340 key
->subkeys
[i
]->parent
= NULL
;
341 release_object( key
->subkeys
[i
] );
343 if (key
->subkeys
) free( key
->subkeys
);
344 /* unconditionally notify everything waiting on this key */
345 while ((ptr
= list_head( &key
->notify_list
)))
347 struct notify
*notify
= LIST_ENTRY( ptr
, struct notify
, entry
);
348 do_notification( key
, notify
, 1 );
352 /* get the request vararg as registry path */
353 inline static void get_req_path( struct unicode_str
*str
, int skip_root
)
355 static const WCHAR root_name
[] = { '\\','R','e','g','i','s','t','r','y','\\' };
357 str
->str
= get_req_data();
358 str
->len
= (get_req_data_size() / sizeof(WCHAR
)) * sizeof(WCHAR
);
360 if (skip_root
&& str
->len
>= sizeof(root_name
) &&
361 !memicmpW( str
->str
, root_name
, sizeof(root_name
)/sizeof(WCHAR
) ))
363 str
->str
+= sizeof(root_name
)/sizeof(WCHAR
);
364 str
->len
-= sizeof(root_name
);
368 /* return the next token in a given path */
369 /* token->str must point inside the path, or be NULL for the first call */
370 static struct unicode_str
*get_path_token( const struct unicode_str
*path
, struct unicode_str
*token
)
372 size_t i
= 0, len
= path
->len
/ sizeof(WCHAR
);
374 if (!token
->str
) /* first time */
376 /* path cannot start with a backslash */
377 if (len
&& path
->str
[0] == '\\')
379 set_error( STATUS_OBJECT_PATH_INVALID
);
385 i
= token
->str
- path
->str
;
386 i
+= token
->len
/ sizeof(WCHAR
);
387 while (i
< len
&& path
->str
[i
] == '\\') i
++;
389 token
->str
= path
->str
+ i
;
390 while (i
< len
&& path
->str
[i
] != '\\') i
++;
391 token
->len
= (path
->str
+ i
- token
->str
) * sizeof(WCHAR
);
395 /* allocate a key object */
396 static struct key
*alloc_key( const struct unicode_str
*name
, time_t modif
)
399 if ((key
= alloc_object( &key_ops
)))
403 key
->namelen
= name
->len
;
406 key
->last_subkey
= -1;
410 key
->last_value
= -1;
414 list_init( &key
->notify_list
);
415 if (name
->len
&& !(key
->name
= memdup( name
->str
, name
->len
)))
417 release_object( key
);
424 /* mark a key and all its parents as dirty (modified) */
425 static void make_dirty( struct key
*key
)
429 if (key
->flags
& (KEY_DIRTY
|KEY_VOLATILE
)) return; /* nothing to do */
430 key
->flags
|= KEY_DIRTY
;
435 /* mark a key and all its subkeys as clean (not modified) */
436 static void make_clean( struct key
*key
)
440 if (key
->flags
& KEY_VOLATILE
) return;
441 if (!(key
->flags
& KEY_DIRTY
)) return;
442 key
->flags
&= ~KEY_DIRTY
;
443 for (i
= 0; i
<= key
->last_subkey
; i
++) make_clean( key
->subkeys
[i
] );
446 /* go through all the notifications and send them if necessary */
447 static void check_notify( struct key
*key
, unsigned int change
, int not_subtree
)
449 struct list
*ptr
, *next
;
451 LIST_FOR_EACH_SAFE( ptr
, next
, &key
->notify_list
)
453 struct notify
*n
= LIST_ENTRY( ptr
, struct notify
, entry
);
454 if ( ( not_subtree
|| n
->subtree
) && ( change
& n
->filter
) )
455 do_notification( key
, n
, 0 );
459 /* update key modification time */
460 static void touch_key( struct key
*key
, unsigned int change
)
464 key
->modif
= time(NULL
);
467 /* do notifications */
468 check_notify( key
, change
, 1 );
469 for ( k
= key
->parent
; k
; k
= k
->parent
)
470 check_notify( k
, change
& ~REG_NOTIFY_CHANGE_LAST_SET
, 0 );
473 /* try to grow the array of subkeys; return 1 if OK, 0 on error */
474 static int grow_subkeys( struct key
*key
)
476 struct key
**new_subkeys
;
481 nb_subkeys
= key
->nb_subkeys
+ (key
->nb_subkeys
/ 2); /* grow by 50% */
482 if (!(new_subkeys
= realloc( key
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) )))
484 set_error( STATUS_NO_MEMORY
);
490 nb_subkeys
= MIN_VALUES
;
491 if (!(new_subkeys
= mem_alloc( nb_subkeys
* sizeof(*new_subkeys
) ))) return 0;
493 key
->subkeys
= new_subkeys
;
494 key
->nb_subkeys
= nb_subkeys
;
498 /* allocate a subkey for a given key, and return its index */
499 static struct key
*alloc_subkey( struct key
*parent
, const struct unicode_str
*name
,
500 int index
, time_t modif
)
505 if (name
->len
> MAX_NAME_LEN
* sizeof(WCHAR
))
507 set_error( STATUS_NAME_TOO_LONG
);
510 if (parent
->last_subkey
+ 1 == parent
->nb_subkeys
)
512 /* need to grow the array */
513 if (!grow_subkeys( parent
)) return NULL
;
515 if ((key
= alloc_key( name
, modif
)) != NULL
)
517 key
->parent
= parent
;
518 for (i
= ++parent
->last_subkey
; i
> index
; i
--)
519 parent
->subkeys
[i
] = parent
->subkeys
[i
-1];
520 parent
->subkeys
[index
] = key
;
525 /* free a subkey of a given key */
526 static void free_subkey( struct key
*parent
, int index
)
531 assert( index
>= 0 );
532 assert( index
<= parent
->last_subkey
);
534 key
= parent
->subkeys
[index
];
535 for (i
= index
; i
< parent
->last_subkey
; i
++) parent
->subkeys
[i
] = parent
->subkeys
[i
+ 1];
536 parent
->last_subkey
--;
537 key
->flags
|= KEY_DELETED
;
539 release_object( key
);
541 /* try to shrink the array */
542 nb_subkeys
= parent
->nb_subkeys
;
543 if (nb_subkeys
> MIN_SUBKEYS
&& parent
->last_subkey
< nb_subkeys
/ 2)
545 struct key
**new_subkeys
;
546 nb_subkeys
-= nb_subkeys
/ 3; /* shrink by 33% */
547 if (nb_subkeys
< MIN_SUBKEYS
) nb_subkeys
= MIN_SUBKEYS
;
548 if (!(new_subkeys
= realloc( parent
->subkeys
, nb_subkeys
* sizeof(*new_subkeys
) ))) return;
549 parent
->subkeys
= new_subkeys
;
550 parent
->nb_subkeys
= nb_subkeys
;
554 /* find the named child of a given key and return its index */
555 static struct key
*find_subkey( const struct key
*key
, const struct unicode_str
*name
, int *index
)
557 int i
, min
, max
, res
;
561 max
= key
->last_subkey
;
565 len
= min( key
->subkeys
[i
]->namelen
, name
->len
);
566 res
= memicmpW( key
->subkeys
[i
]->name
, name
->str
, len
/ sizeof(WCHAR
) );
567 if (!res
) res
= key
->subkeys
[i
]->namelen
- name
->len
;
571 return key
->subkeys
[i
];
573 if (res
> 0) max
= i
- 1;
576 *index
= min
; /* this is where we should insert it */
581 static struct key
*open_key( struct key
*key
, const struct unicode_str
*name
)
584 struct unicode_str token
;
587 if (!get_path_token( name
, &token
)) return NULL
;
590 if (!(key
= find_subkey( key
, &token
, &index
)))
592 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
595 get_path_token( name
, &token
);
598 if (debug_level
> 1) dump_operation( key
, NULL
, "Open" );
599 if (key
) grab_object( key
);
603 /* create a subkey */
604 static struct key
*create_key( struct key
*key
, const struct unicode_str
*name
,
605 const struct unicode_str
*class, int flags
, time_t modif
, int *created
)
609 struct unicode_str token
;
611 if (key
->flags
& KEY_DELETED
) /* we cannot create a subkey under a deleted key */
613 set_error( STATUS_KEY_DELETED
);
616 if (!(flags
& KEY_VOLATILE
) && (key
->flags
& KEY_VOLATILE
))
618 set_error( STATUS_CHILD_MUST_BE_VOLATILE
);
621 if (!modif
) modif
= time(NULL
);
624 if (!get_path_token( name
, &token
)) return NULL
;
629 if (!(subkey
= find_subkey( key
, &token
, &index
))) break;
631 get_path_token( name
, &token
);
634 /* create the remaining part */
636 if (!token
.len
) goto done
;
638 if (flags
& KEY_DIRTY
) make_dirty( key
);
639 if (!(key
= alloc_subkey( key
, &token
, index
, modif
))) return NULL
;
644 get_path_token( name
, &token
);
645 if (!token
.len
) break;
646 /* we know the index is always 0 in a new key */
647 if (!(key
= alloc_subkey( key
, &token
, 0, modif
)))
649 free_subkey( base
, index
);
655 if (debug_level
> 1) dump_operation( key
, NULL
, "Create" );
656 if (class && class->len
)
658 key
->classlen
= class->len
;
659 if (key
->class) free(key
->class);
660 if (!(key
->class = memdup( class->str
, key
->classlen
))) key
->classlen
= 0;
666 /* query information about a key or a subkey */
667 static void enum_key( const struct key
*key
, int index
, int info_class
,
668 struct enum_key_reply
*reply
)
671 size_t len
, namelen
, classlen
;
672 int max_subkey
= 0, max_class
= 0;
673 int max_value
= 0, max_data
= 0;
676 if (index
!= -1) /* -1 means use the specified key directly */
678 if ((index
< 0) || (index
> key
->last_subkey
))
680 set_error( STATUS_NO_MORE_ENTRIES
);
683 key
= key
->subkeys
[index
];
686 namelen
= key
->namelen
;
687 classlen
= key
->classlen
;
691 case KeyBasicInformation
:
692 classlen
= 0; /* only return the name */
694 case KeyNodeInformation
:
695 reply
->max_subkey
= 0;
696 reply
->max_class
= 0;
697 reply
->max_value
= 0;
700 case KeyFullInformation
:
701 for (i
= 0; i
<= key
->last_subkey
; i
++)
703 struct key
*subkey
= key
->subkeys
[i
];
704 len
= subkey
->namelen
/ sizeof(WCHAR
);
705 if (len
> max_subkey
) max_subkey
= len
;
706 len
= subkey
->classlen
/ sizeof(WCHAR
);
707 if (len
> max_class
) max_class
= len
;
709 for (i
= 0; i
<= key
->last_value
; i
++)
711 len
= key
->values
[i
].namelen
/ sizeof(WCHAR
);
712 if (len
> max_value
) max_value
= len
;
713 len
= key
->values
[i
].len
;
714 if (len
> max_data
) max_data
= len
;
716 reply
->max_subkey
= max_subkey
;
717 reply
->max_class
= max_class
;
718 reply
->max_value
= max_value
;
719 reply
->max_data
= max_data
;
720 namelen
= 0; /* only return the class */
723 set_error( STATUS_INVALID_PARAMETER
);
726 reply
->subkeys
= key
->last_subkey
+ 1;
727 reply
->values
= key
->last_value
+ 1;
728 reply
->modif
= key
->modif
;
729 reply
->total
= namelen
+ classlen
;
731 len
= min( reply
->total
, get_reply_max_size() );
732 if (len
&& (data
= set_reply_data_size( len
)))
736 reply
->namelen
= namelen
;
737 memcpy( data
, key
->name
, namelen
);
738 memcpy( data
+ namelen
, key
->class, len
- namelen
);
742 reply
->namelen
= len
;
743 memcpy( data
, key
->name
, len
);
746 if (debug_level
> 1) dump_operation( key
, NULL
, "Enum" );
749 /* delete a key and its values */
750 static int delete_key( struct key
*key
, int recurse
)
755 /* must find parent and index */
758 set_error( STATUS_ACCESS_DENIED
);
761 if (!(parent
= key
->parent
) || (key
->flags
& KEY_DELETED
))
763 set_error( STATUS_KEY_DELETED
);
767 while (recurse
&& (key
->last_subkey
>=0))
768 if (0 > delete_key(key
->subkeys
[key
->last_subkey
], 1))
771 for (index
= 0; index
<= parent
->last_subkey
; index
++)
772 if (parent
->subkeys
[index
] == key
) break;
773 assert( index
<= parent
->last_subkey
);
775 /* we can only delete a key that has no subkeys */
776 if (key
->last_subkey
>= 0)
778 set_error( STATUS_ACCESS_DENIED
);
782 if (debug_level
> 1) dump_operation( key
, NULL
, "Delete" );
783 free_subkey( parent
, index
);
784 touch_key( parent
, REG_NOTIFY_CHANGE_NAME
);
788 /* try to grow the array of values; return 1 if OK, 0 on error */
789 static int grow_values( struct key
*key
)
791 struct key_value
*new_val
;
796 nb_values
= key
->nb_values
+ (key
->nb_values
/ 2); /* grow by 50% */
797 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) )))
799 set_error( STATUS_NO_MEMORY
);
805 nb_values
= MIN_VALUES
;
806 if (!(new_val
= mem_alloc( nb_values
* sizeof(*new_val
) ))) return 0;
808 key
->values
= new_val
;
809 key
->nb_values
= nb_values
;
813 /* find the named value of a given key and return its index in the array */
814 static struct key_value
*find_value( const struct key
*key
, const struct unicode_str
*name
, int *index
)
816 int i
, min
, max
, res
;
820 max
= key
->last_value
;
824 len
= min( key
->values
[i
].namelen
, name
->len
);
825 res
= memicmpW( key
->values
[i
].name
, name
->str
, len
/ sizeof(WCHAR
) );
826 if (!res
) res
= key
->values
[i
].namelen
- name
->len
;
830 return &key
->values
[i
];
832 if (res
> 0) max
= i
- 1;
835 *index
= min
; /* this is where we should insert it */
839 /* insert a new value; the index must have been returned by find_value */
840 static struct key_value
*insert_value( struct key
*key
, const struct unicode_str
*name
, int index
)
842 struct key_value
*value
;
843 WCHAR
*new_name
= NULL
;
846 if (name
->len
> MAX_VALUE_LEN
* sizeof(WCHAR
))
848 set_error( STATUS_NAME_TOO_LONG
);
851 if (key
->last_value
+ 1 == key
->nb_values
)
853 if (!grow_values( key
)) return NULL
;
855 if (name
->len
&& !(new_name
= memdup( name
->str
, name
->len
))) return NULL
;
856 for (i
= ++key
->last_value
; i
> index
; i
--) key
->values
[i
] = key
->values
[i
- 1];
857 value
= &key
->values
[index
];
858 value
->name
= new_name
;
859 value
->namelen
= name
->len
;
865 /* set a key value */
866 static void set_value( struct key
*key
, const struct unicode_str
*name
,
867 int type
, const void *data
, size_t len
)
869 struct key_value
*value
;
873 if ((value
= find_value( key
, name
, &index
)))
875 /* check if the new value is identical to the existing one */
876 if (value
->type
== type
&& value
->len
== len
&&
877 value
->data
&& !memcmp( value
->data
, data
, len
))
879 if (debug_level
> 1) dump_operation( key
, value
, "Skip setting" );
884 if (len
&& !(ptr
= memdup( data
, len
))) return;
888 if (!(value
= insert_value( key
, name
, index
)))
890 if (ptr
) free( ptr
);
894 else if (value
->data
) free( value
->data
); /* already existing, free previous data */
899 touch_key( key
, REG_NOTIFY_CHANGE_LAST_SET
);
900 if (debug_level
> 1) dump_operation( key
, value
, "Set" );
903 /* get a key value */
904 static void get_value( struct key
*key
, const struct unicode_str
*name
, int *type
, size_t *len
)
906 struct key_value
*value
;
909 if ((value
= find_value( key
, name
, &index
)))
913 if (value
->data
) set_reply_data( value
->data
, min( value
->len
, get_reply_max_size() ));
914 if (debug_level
> 1) dump_operation( key
, value
, "Get" );
919 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
923 /* enumerate a key value */
924 static void enum_value( struct key
*key
, int i
, int info_class
, struct enum_key_value_reply
*reply
)
926 struct key_value
*value
;
928 if (i
< 0 || i
> key
->last_value
) set_error( STATUS_NO_MORE_ENTRIES
);
932 size_t namelen
, maxlen
;
934 value
= &key
->values
[i
];
935 reply
->type
= value
->type
;
936 namelen
= value
->namelen
;
940 case KeyValueBasicInformation
:
941 reply
->total
= namelen
;
943 case KeyValueFullInformation
:
944 reply
->total
= namelen
+ value
->len
;
946 case KeyValuePartialInformation
:
947 reply
->total
= value
->len
;
951 set_error( STATUS_INVALID_PARAMETER
);
955 maxlen
= min( reply
->total
, get_reply_max_size() );
956 if (maxlen
&& ((data
= set_reply_data_size( maxlen
))))
958 if (maxlen
> namelen
)
960 reply
->namelen
= namelen
;
961 memcpy( data
, value
->name
, namelen
);
962 memcpy( (char *)data
+ namelen
, value
->data
, maxlen
- namelen
);
966 reply
->namelen
= maxlen
;
967 memcpy( data
, value
->name
, maxlen
);
970 if (debug_level
> 1) dump_operation( key
, value
, "Enum" );
975 static void delete_value( struct key
*key
, const struct unicode_str
*name
)
977 struct key_value
*value
;
978 int i
, index
, nb_values
;
980 if (!(value
= find_value( key
, name
, &index
)))
982 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
985 if (debug_level
> 1) dump_operation( key
, value
, "Delete" );
986 if (value
->name
) free( value
->name
);
987 if (value
->data
) free( value
->data
);
988 for (i
= index
; i
< key
->last_value
; i
++) key
->values
[i
] = key
->values
[i
+ 1];
990 touch_key( key
, REG_NOTIFY_CHANGE_LAST_SET
);
992 /* try to shrink the array */
993 nb_values
= key
->nb_values
;
994 if (nb_values
> MIN_VALUES
&& key
->last_value
< nb_values
/ 2)
996 struct key_value
*new_val
;
997 nb_values
-= nb_values
/ 3; /* shrink by 33% */
998 if (nb_values
< MIN_VALUES
) nb_values
= MIN_VALUES
;
999 if (!(new_val
= realloc( key
->values
, nb_values
* sizeof(*new_val
) ))) return;
1000 key
->values
= new_val
;
1001 key
->nb_values
= nb_values
;
1005 /* get the registry key corresponding to an hkey handle */
1006 static struct key
*get_hkey_obj( obj_handle_t hkey
, unsigned int access
)
1008 if (!hkey
) return (struct key
*)grab_object( root_key
);
1009 return (struct key
*)get_handle_obj( current
->process
, hkey
, access
, &key_ops
);
1012 /* read a line from the input file */
1013 static int read_next_line( struct file_load_info
*info
)
1016 int newlen
, pos
= 0;
1021 if (!fgets( info
->buffer
+ pos
, info
->len
- pos
, info
->file
))
1022 return (pos
!= 0); /* EOF */
1023 pos
= strlen(info
->buffer
);
1024 if (info
->buffer
[pos
-1] == '\n')
1026 /* got a full line */
1027 info
->buffer
[--pos
] = 0;
1028 if (pos
> 0 && info
->buffer
[pos
-1] == '\r') info
->buffer
[pos
-1] = 0;
1031 if (pos
< info
->len
- 1) return 1; /* EOF but something was read */
1033 /* need to enlarge the buffer */
1034 newlen
= info
->len
+ info
->len
/ 2;
1035 if (!(newbuf
= realloc( info
->buffer
, newlen
)))
1037 set_error( STATUS_NO_MEMORY
);
1040 info
->buffer
= newbuf
;
1045 /* make sure the temp buffer holds enough space */
1046 static int get_file_tmp_space( struct file_load_info
*info
, size_t size
)
1049 if (info
->tmplen
>= size
) return 1;
1050 if (!(tmp
= realloc( info
->tmp
, size
)))
1052 set_error( STATUS_NO_MEMORY
);
1056 info
->tmplen
= size
;
1060 /* report an error while loading an input file */
1061 static void file_read_error( const char *err
, struct file_load_info
*info
)
1064 fprintf( stderr
, "%s:%d: %s '%s'\n", info
->filename
, info
->line
, err
, info
->buffer
);
1066 fprintf( stderr
, "<fd>:%d: %s '%s'\n", info
->line
, err
, info
->buffer
);
1069 /* parse an escaped string back into Unicode */
1070 /* return the number of chars read from the input, or -1 on output overflow */
1071 static int parse_strW( WCHAR
*dest
, size_t *len
, const char *src
, char endchar
)
1073 size_t count
= sizeof(WCHAR
); /* for terminating null */
1074 const char *p
= src
;
1075 while (*p
&& *p
!= endchar
)
1077 if (*p
!= '\\') *dest
= (WCHAR
)*p
++;
1083 case 'a': *dest
= '\a'; p
++; break;
1084 case 'b': *dest
= '\b'; p
++; break;
1085 case 'e': *dest
= '\e'; p
++; break;
1086 case 'f': *dest
= '\f'; p
++; break;
1087 case 'n': *dest
= '\n'; p
++; break;
1088 case 'r': *dest
= '\r'; p
++; break;
1089 case 't': *dest
= '\t'; p
++; break;
1090 case 'v': *dest
= '\v'; p
++; break;
1091 case 'x': /* hex escape */
1093 if (!isxdigit(*p
)) *dest
= 'x';
1096 *dest
= to_hex(*p
++);
1097 if (isxdigit(*p
)) *dest
= (*dest
* 16) + to_hex(*p
++);
1098 if (isxdigit(*p
)) *dest
= (*dest
* 16) + to_hex(*p
++);
1099 if (isxdigit(*p
)) *dest
= (*dest
* 16) + to_hex(*p
++);
1109 case '7': /* octal escape */
1111 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
1112 if (*p
>= '0' && *p
<= '7') *dest
= (*dest
* 8) + (*p
++ - '0');
1115 *dest
= (WCHAR
)*p
++;
1119 if ((count
+= sizeof(WCHAR
)) > *len
) return -1; /* dest buffer overflow */
1123 if (!*p
) return -1; /* delimiter not found */
1128 /* convert a data type tag to a value type */
1129 static int get_data_type( const char *buffer
, int *type
, int *parse_type
)
1131 struct data_type
{ const char *tag
; int len
; int type
; int parse_type
; };
1133 static const struct data_type data_types
[] =
1134 { /* actual type */ /* type to assume for parsing */
1135 { "\"", 1, REG_SZ
, REG_SZ
},
1136 { "str:\"", 5, REG_SZ
, REG_SZ
},
1137 { "str(2):\"", 8, REG_EXPAND_SZ
, REG_SZ
},
1138 { "str(7):\"", 8, REG_MULTI_SZ
, REG_SZ
},
1139 { "hex:", 4, REG_BINARY
, REG_BINARY
},
1140 { "dword:", 6, REG_DWORD
, REG_DWORD
},
1141 { "hex(", 4, -1, REG_BINARY
},
1145 const struct data_type
*ptr
;
1148 for (ptr
= data_types
; ptr
->tag
; ptr
++)
1150 if (memcmp( ptr
->tag
, buffer
, ptr
->len
)) continue;
1151 *parse_type
= ptr
->parse_type
;
1152 if ((*type
= ptr
->type
) != -1) return ptr
->len
;
1153 /* "hex(xx):" is special */
1154 *type
= (int)strtoul( buffer
+ 4, &end
, 16 );
1155 if ((end
<= buffer
) || memcmp( end
, "):", 2 )) return 0;
1156 return end
+ 2 - buffer
;
1161 /* load and create a key from the input file */
1162 static struct key
*load_key( struct key
*base
, const char *buffer
, int flags
,
1163 int prefix_len
, struct file_load_info
*info
,
1167 struct unicode_str name
;
1169 size_t len
= strlen(buffer
) * sizeof(WCHAR
);
1171 if (!get_file_tmp_space( info
, len
)) return NULL
;
1173 if ((res
= parse_strW( info
->tmp
, &len
, buffer
, ']' )) == -1)
1175 file_read_error( "Malformed key", info
);
1178 if (sscanf( buffer
+ res
, " %d", &modif
) != 1) modif
= default_modif
;
1181 while (prefix_len
&& *p
) { if (*p
++ == '\\') prefix_len
--; }
1187 file_read_error( "Malformed key", info
);
1190 /* empty key name, return base key */
1191 return (struct key
*)grab_object( base
);
1194 name
.len
= len
- (p
- info
->tmp
+ 1) * sizeof(WCHAR
);
1195 return create_key( base
, &name
, NULL
, flags
, modif
, &res
);
1198 /* parse a comma-separated list of hex digits */
1199 static int parse_hex( unsigned char *dest
, size_t *len
, const char *buffer
)
1201 const char *p
= buffer
;
1203 while (isxdigit(*p
))
1207 memcpy( buf
, p
, 2 );
1209 sscanf( buf
, "%x", &val
);
1210 if (count
++ >= *len
) return -1; /* dest buffer overflow */
1211 *dest
++ = (unsigned char )val
;
1219 /* parse a value name and create the corresponding value */
1220 static struct key_value
*parse_value_name( struct key
*key
, const char *buffer
, size_t *len
,
1221 struct file_load_info
*info
)
1223 struct key_value
*value
;
1224 struct unicode_str name
;
1226 size_t maxlen
= strlen(buffer
) * sizeof(WCHAR
);
1228 if (!get_file_tmp_space( info
, maxlen
)) return NULL
;
1229 name
.str
= info
->tmp
;
1230 if (buffer
[0] == '@')
1237 int r
= parse_strW( info
->tmp
, &maxlen
, buffer
+ 1, '\"' );
1238 if (r
== -1) goto error
;
1239 *len
= r
+ 1; /* for initial quote */
1240 name
.len
= maxlen
- sizeof(WCHAR
);
1242 while (isspace(buffer
[*len
])) (*len
)++;
1243 if (buffer
[*len
] != '=') goto error
;
1245 while (isspace(buffer
[*len
])) (*len
)++;
1246 if (!(value
= find_value( key
, &name
, &index
))) value
= insert_value( key
, &name
, index
);
1250 file_read_error( "Malformed value name", info
);
1254 /* load a value from the input file */
1255 static int load_value( struct key
*key
, const char *buffer
, struct file_load_info
*info
)
1259 int res
, type
, parse_type
;
1261 struct key_value
*value
;
1263 if (!(value
= parse_value_name( key
, buffer
, &len
, info
))) return 0;
1264 if (!(res
= get_data_type( buffer
+ len
, &type
, &parse_type
))) goto error
;
1265 buffer
+= len
+ res
;
1270 len
= strlen(buffer
) * sizeof(WCHAR
);
1271 if (!get_file_tmp_space( info
, len
)) return 0;
1272 if ((res
= parse_strW( info
->tmp
, &len
, buffer
, '\"' )) == -1) goto error
;
1276 dw
= strtoul( buffer
, NULL
, 16 );
1280 case REG_BINARY
: /* hex digits */
1284 maxlen
= 1 + strlen(buffer
)/3; /* 3 chars for one hex byte */
1285 if (!get_file_tmp_space( info
, len
+ maxlen
)) return 0;
1286 if ((res
= parse_hex( (unsigned char *)info
->tmp
+ len
, &maxlen
, buffer
)) == -1) goto error
;
1289 while (isspace(*buffer
)) buffer
++;
1290 if (!*buffer
) break;
1291 if (*buffer
!= '\\') goto error
;
1292 if (read_next_line( info
) != 1) goto error
;
1293 buffer
= info
->buffer
;
1294 while (isspace(*buffer
)) buffer
++;
1300 ptr
= NULL
; /* keep compiler quiet */
1304 if (!len
) newptr
= NULL
;
1305 else if (!(newptr
= memdup( ptr
, len
))) return 0;
1307 if (value
->data
) free( value
->data
);
1308 value
->data
= newptr
;
1315 file_read_error( "Malformed value", info
);
1319 /* return the length (in path elements) of name that is part of the key name */
1320 /* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1321 static int get_prefix_len( struct key
*key
, const char *name
, struct file_load_info
*info
)
1325 size_t len
= strlen(name
) * sizeof(WCHAR
);
1327 if (!get_file_tmp_space( info
, len
)) return 0;
1329 if ((res
= parse_strW( info
->tmp
, &len
, name
, ']' )) == -1)
1331 file_read_error( "Malformed key", info
);
1334 for (p
= info
->tmp
; *p
; p
++) if (*p
== '\\') break;
1335 len
= (p
- info
->tmp
) * sizeof(WCHAR
);
1336 for (res
= 1; key
!= root_key
; res
++)
1338 if (len
== key
->namelen
&& !memicmpW( info
->tmp
, key
->name
, len
/ sizeof(WCHAR
) )) break;
1341 if (key
== root_key
) res
= 0; /* no matching name */
1345 /* load all the keys from the input file */
1346 /* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
1347 static void load_keys( struct key
*key
, const char *filename
, FILE *f
, int prefix_len
)
1349 struct key
*subkey
= NULL
;
1350 struct file_load_info info
;
1352 int default_modif
= time(NULL
);
1353 int flags
= (key
->flags
& KEY_VOLATILE
) ? KEY_VOLATILE
: KEY_DIRTY
;
1355 info
.filename
= filename
;
1360 if (!(info
.buffer
= mem_alloc( info
.len
))) return;
1361 if (!(info
.tmp
= mem_alloc( info
.tmplen
)))
1363 free( info
.buffer
);
1367 if ((read_next_line( &info
) != 1) ||
1368 strcmp( info
.buffer
, "WINE REGISTRY Version 2" ))
1370 set_error( STATUS_NOT_REGISTRY_FILE
);
1374 while (read_next_line( &info
) == 1)
1377 while (*p
&& isspace(*p
)) p
++;
1380 case '[': /* new key */
1381 if (subkey
) release_object( subkey
);
1382 if (prefix_len
== -1) prefix_len
= get_prefix_len( key
, p
+ 1, &info
);
1383 if (!(subkey
= load_key( key
, p
+ 1, flags
, prefix_len
, &info
, default_modif
)))
1384 file_read_error( "Error creating key", &info
);
1386 case '@': /* default value */
1387 case '\"': /* value */
1388 if (subkey
) load_value( subkey
, p
, &info
);
1389 else file_read_error( "Value without key", &info
);
1391 case '#': /* comment */
1392 case ';': /* comment */
1393 case 0: /* empty line */
1396 file_read_error( "Unrecognized input", &info
);
1402 if (subkey
) release_object( subkey
);
1403 free( info
.buffer
);
1407 /* load a part of the registry from a file */
1408 static void load_registry( struct key
*key
, obj_handle_t handle
)
1413 if (!(file
= get_file_obj( current
->process
, handle
, FILE_READ_DATA
))) return;
1414 fd
= dup( get_file_unix_fd( file
) );
1415 release_object( file
);
1418 FILE *f
= fdopen( fd
, "r" );
1421 load_keys( key
, NULL
, f
, -1 );
1424 else file_set_error();
1428 /* load one of the initial registry files */
1429 static void load_init_registry_from_file( const char *filename
, struct key
*key
)
1433 if ((f
= fopen( filename
, "r" )))
1435 load_keys( key
, filename
, f
, 0 );
1437 if (get_error() == STATUS_NOT_REGISTRY_FILE
)
1439 fprintf( stderr
, "%s is not a valid registry file\n", filename
);
1444 assert( save_branch_count
< MAX_SAVE_BRANCH_INFO
);
1446 if ((save_branch_info
[save_branch_count
].path
= strdup( filename
)))
1448 save_branch_info
[save_branch_count
++].key
= (struct key
*)grab_object( key
);
1449 make_object_static( &key
->obj
);
1453 static WCHAR
*format_user_registry_path( const SID
*sid
, struct unicode_str
*path
)
1455 static const WCHAR prefixW
[] = {'U','s','e','r','\\','S',0};
1456 static const WCHAR formatW
[] = {'-','%','u',0};
1457 WCHAR buffer
[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES
];
1461 strcpyW( p
, prefixW
);
1462 p
+= strlenW( prefixW
);
1463 p
+= sprintfW( p
, formatW
, sid
->Revision
);
1464 p
+= sprintfW( p
, formatW
, MAKELONG( MAKEWORD( sid
->IdentifierAuthority
.Value
[5],
1465 sid
->IdentifierAuthority
.Value
[4] ),
1466 MAKEWORD( sid
->IdentifierAuthority
.Value
[3],
1467 sid
->IdentifierAuthority
.Value
[2] )));
1468 for (i
= 0; i
< sid
->SubAuthorityCount
; i
++)
1469 p
+= sprintfW( p
, formatW
, sid
->SubAuthority
[i
] );
1471 path
->len
= (p
- buffer
) * sizeof(WCHAR
);
1472 path
->str
= p
= memdup( buffer
, path
->len
);
1476 /* registry initialisation */
1477 void init_registry(void)
1479 static const WCHAR HKLM
[] = { 'M','a','c','h','i','n','e' };
1480 static const WCHAR HKU_default
[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
1481 static const struct unicode_str root_name
= { NULL
, 0 };
1482 static const struct unicode_str HKLM_name
= { HKLM
, sizeof(HKLM
) };
1483 static const struct unicode_str HKU_name
= { HKU_default
, sizeof(HKU_default
) };
1485 WCHAR
*current_user_path
;
1486 struct unicode_str current_user_str
;
1488 const char *config
= wine_get_config_dir();
1493 /* create the root key */
1494 root_key
= alloc_key( &root_name
, time(NULL
) );
1496 make_object_static( &root_key
->obj
);
1498 if (!(filename
= malloc( strlen(config
) + 16 ))) fatal_error( "out of memory\n" );
1499 strcpy( filename
, config
);
1500 p
= filename
+ strlen(filename
);
1502 /* load system.reg into Registry\Machine */
1504 if (!(key
= create_key( root_key
, &HKLM_name
, NULL
, 0, time(NULL
), &dummy
)))
1505 fatal_error( "could not create Machine registry key\n" );
1507 strcpy( p
, "/system.reg" );
1508 load_init_registry_from_file( filename
, key
);
1509 release_object( key
);
1511 /* load userdef.reg into Registry\User\.Default */
1513 if (!(key
= create_key( root_key
, &HKU_name
, NULL
, 0, time(NULL
), &dummy
)))
1514 fatal_error( "could not create User\\.Default registry key\n" );
1516 strcpy( p
, "/userdef.reg" );
1517 load_init_registry_from_file( filename
, key
);
1518 release_object( key
);
1520 /* load user.reg into HKEY_CURRENT_USER */
1522 /* FIXME: match default user in token.c. should get from process token instead */
1523 current_user_path
= format_user_registry_path( security_interactive_sid
, ¤t_user_str
);
1524 if (!current_user_path
||
1525 !(key
= create_key( root_key
, ¤t_user_str
, NULL
, 0, time(NULL
), &dummy
)))
1526 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
1527 free( current_user_path
);
1528 strcpy( p
, "/user.reg" );
1529 load_init_registry_from_file( filename
, key
);
1530 release_object( key
);
1534 /* start the periodic save timer */
1535 set_periodic_save_timer();
1538 /* save a registry branch to a file */
1539 static void save_all_subkeys( struct key
*key
, FILE *f
)
1541 fprintf( f
, "WINE REGISTRY Version 2\n" );
1542 fprintf( f
, ";; All keys relative to " );
1543 dump_path( key
, NULL
, f
);
1545 save_subkeys( key
, key
, f
);
1548 /* save a registry branch to a file handle */
1549 static void save_registry( struct key
*key
, obj_handle_t handle
)
1554 if (key
->flags
& KEY_DELETED
)
1556 set_error( STATUS_KEY_DELETED
);
1559 if (!(file
= get_file_obj( current
->process
, handle
, FILE_WRITE_DATA
))) return;
1560 fd
= dup( get_file_unix_fd( file
) );
1561 release_object( file
);
1564 FILE *f
= fdopen( fd
, "w" );
1567 save_all_subkeys( key
, f
);
1568 if (fclose( f
)) file_set_error();
1578 /* save a registry branch to a file */
1579 static int save_branch( struct key
*key
, const char *path
)
1582 char *p
, *real
, *tmp
= NULL
;
1583 int fd
, count
= 0, ret
= 0, by_symlink
;
1586 if (!(key
->flags
& KEY_DIRTY
))
1588 if (debug_level
> 1) dump_operation( key
, NULL
, "Not saving clean" );
1592 /* get the real path */
1594 by_symlink
= (!lstat(path
, &st
) && S_ISLNK (st
.st_mode
));
1595 if (!(real
= malloc( PATH_MAX
))) return 0;
1596 if (!realpath( path
, real
))
1603 /* test the file type */
1605 if ((fd
= open( path
, O_WRONLY
)) != -1)
1607 /* if file is not a regular file or has multiple links or is accessed
1608 * via symbolic links, write directly into it; otherwise use a temp file */
1610 (!fstat( fd
, &st
) && (!S_ISREG(st
.st_mode
) || st
.st_nlink
> 1)))
1618 /* create a temp file in the same directory */
1620 if (!(tmp
= malloc( strlen(path
) + 20 ))) goto done
;
1621 strcpy( tmp
, path
);
1622 if ((p
= strrchr( tmp
, '/' ))) p
++;
1626 sprintf( p
, "reg%lx%04x.tmp", (long) getpid(), count
++ );
1627 if ((fd
= open( tmp
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666 )) != -1) break;
1628 if (errno
!= EEXIST
) goto done
;
1632 /* now save to it */
1635 if (!(f
= fdopen( fd
, "w" )))
1637 if (tmp
) unlink( tmp
);
1642 if (debug_level
> 1)
1644 fprintf( stderr
, "%s: ", path
);
1645 dump_operation( key
, NULL
, "saving" );
1648 save_all_subkeys( key
, f
);
1653 /* if successfully written, rename to final name */
1654 if (ret
) ret
= !rename( tmp
, path
);
1655 if (!ret
) unlink( tmp
);
1661 if (ret
) make_clean( key
);
1665 /* periodic saving of the registry */
1666 static void periodic_save( void *arg
)
1670 save_timeout_user
= NULL
;
1671 for (i
= 0; i
< save_branch_count
; i
++)
1672 save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
);
1673 set_periodic_save_timer();
1676 /* start the periodic save timer */
1677 static void set_periodic_save_timer(void)
1679 struct timeval next
;
1681 gettimeofday( &next
, NULL
);
1682 add_timeout( &next
, save_period
);
1683 if (save_timeout_user
) remove_timeout_user( save_timeout_user
);
1684 save_timeout_user
= add_timeout_user( &next
, periodic_save
, NULL
);
1687 /* save the modified registry branches to disk */
1688 void flush_registry(void)
1692 for (i
= 0; i
< save_branch_count
; i
++)
1694 if (!save_branch( save_branch_info
[i
].key
, save_branch_info
[i
].path
))
1696 fprintf( stderr
, "wineserver: could not save registry branch to %s",
1697 save_branch_info
[i
].path
);
1704 /* create a registry key */
1705 DECL_HANDLER(create_key
)
1707 struct key
*key
= NULL
, *parent
;
1708 struct unicode_str name
, class;
1709 unsigned int access
= req
->access
;
1711 if (access
& MAXIMUM_ALLOWED
) access
= KEY_ALL_ACCESS
; /* FIXME: needs general solution */
1714 if (req
->namelen
> get_req_data_size())
1716 set_error( STATUS_INVALID_PARAMETER
);
1719 class.str
= (const WCHAR
*)get_req_data() + req
->namelen
/ sizeof(WCHAR
);
1720 class.len
= ((get_req_data_size() - req
->namelen
) / sizeof(WCHAR
)) * sizeof(WCHAR
);
1721 get_req_path( &name
, !req
->parent
);
1722 if (name
.str
> class.str
)
1724 set_error( STATUS_INVALID_PARAMETER
);
1727 name
.len
= (class.str
- name
.str
) * sizeof(WCHAR
);
1729 /* NOTE: no access rights are required from the parent handle to create a key */
1730 if ((parent
= get_hkey_obj( req
->parent
, 0 )))
1732 int flags
= (req
->options
& REG_OPTION_VOLATILE
) ? KEY_VOLATILE
: KEY_DIRTY
;
1734 if ((key
= create_key( parent
, &name
, &class, flags
, req
->modif
, &reply
->created
)))
1736 reply
->hkey
= alloc_handle( current
->process
, key
, access
, req
->attributes
);
1737 release_object( key
);
1739 release_object( parent
);
1743 /* open a registry key */
1744 DECL_HANDLER(open_key
)
1746 struct key
*key
, *parent
;
1747 struct unicode_str name
;
1748 unsigned int access
= req
->access
;
1750 if (access
& MAXIMUM_ALLOWED
) access
= KEY_ALL_ACCESS
; /* FIXME: needs general solution */
1752 /* NOTE: no access rights are required to open the parent key, only the child key */
1753 if ((parent
= get_hkey_obj( req
->parent
, 0 )))
1755 get_req_path( &name
, !req
->parent
);
1756 if ((key
= open_key( parent
, &name
)))
1758 reply
->hkey
= alloc_handle( current
->process
, key
, access
, req
->attributes
);
1759 release_object( key
);
1761 release_object( parent
);
1765 /* delete a registry key */
1766 DECL_HANDLER(delete_key
)
1770 if ((key
= get_hkey_obj( req
->hkey
, DELETE
)))
1772 delete_key( key
, 0);
1773 release_object( key
);
1777 /* flush a registry key */
1778 DECL_HANDLER(flush_key
)
1780 struct key
*key
= get_hkey_obj( req
->hkey
, 0 );
1783 /* we don't need to do anything here with the current implementation */
1784 release_object( key
);
1788 /* enumerate registry subkeys */
1789 DECL_HANDLER(enum_key
)
1793 if ((key
= get_hkey_obj( req
->hkey
,
1794 req
->index
== -1 ? KEY_QUERY_VALUE
: KEY_ENUMERATE_SUB_KEYS
)))
1796 enum_key( key
, req
->index
, req
->info_class
, reply
);
1797 release_object( key
);
1801 /* set a value of a registry key */
1802 DECL_HANDLER(set_key_value
)
1805 struct unicode_str name
;
1807 if (req
->namelen
> get_req_data_size())
1809 set_error( STATUS_INVALID_PARAMETER
);
1812 name
.str
= get_req_data();
1813 name
.len
= (req
->namelen
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
1815 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
1817 size_t datalen
= get_req_data_size() - req
->namelen
;
1818 const char *data
= (const char *)get_req_data() + req
->namelen
;
1820 set_value( key
, &name
, req
->type
, data
, datalen
);
1821 release_object( key
);
1825 /* retrieve the value of a registry key */
1826 DECL_HANDLER(get_key_value
)
1829 struct unicode_str name
;
1832 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
1834 get_req_unicode_str( &name
);
1835 get_value( key
, &name
, &reply
->type
, &reply
->total
);
1836 release_object( key
);
1840 /* enumerate the value of a registry key */
1841 DECL_HANDLER(enum_key_value
)
1845 if ((key
= get_hkey_obj( req
->hkey
, KEY_QUERY_VALUE
)))
1847 enum_value( key
, req
->index
, req
->info_class
, reply
);
1848 release_object( key
);
1852 /* delete a value of a registry key */
1853 DECL_HANDLER(delete_key_value
)
1856 struct unicode_str name
;
1858 if ((key
= get_hkey_obj( req
->hkey
, KEY_SET_VALUE
)))
1860 get_req_unicode_str( &name
);
1861 delete_value( key
, &name
);
1862 release_object( key
);
1866 /* load a registry branch from a file */
1867 DECL_HANDLER(load_registry
)
1869 struct key
*key
, *parent
;
1870 struct token
*token
= thread_get_impersonation_token( current
);
1871 struct unicode_str name
;
1873 const LUID_AND_ATTRIBUTES privs
[] =
1875 { SeBackupPrivilege
, 0 },
1876 { SeRestorePrivilege
, 0 },
1879 if (!token
|| !token_check_privileges( token
, TRUE
, privs
,
1880 sizeof(privs
)/sizeof(privs
[0]), NULL
))
1882 set_error( STATUS_PRIVILEGE_NOT_HELD
);
1886 if ((parent
= get_hkey_obj( req
->hkey
, 0 )))
1889 get_req_path( &name
, !req
->hkey
);
1890 if ((key
= create_key( parent
, &name
, NULL
, KEY_DIRTY
, time(NULL
), &dummy
)))
1892 load_registry( key
, req
->file
);
1893 release_object( key
);
1895 release_object( parent
);
1899 DECL_HANDLER(unload_registry
)
1902 struct token
*token
= thread_get_impersonation_token( current
);
1904 const LUID_AND_ATTRIBUTES privs
[] =
1906 { SeBackupPrivilege
, 0 },
1907 { SeRestorePrivilege
, 0 },
1910 if (!token
|| !token_check_privileges( token
, TRUE
, privs
,
1911 sizeof(privs
)/sizeof(privs
[0]), NULL
))
1913 set_error( STATUS_PRIVILEGE_NOT_HELD
);
1917 if ((key
= get_hkey_obj( req
->hkey
, 0 )))
1919 delete_key( key
, 1 ); /* FIXME */
1920 release_object( key
);
1924 /* save a registry branch to a file */
1925 DECL_HANDLER(save_registry
)
1929 if (!thread_single_check_privilege( current
, &SeBackupPrivilege
))
1931 set_error( STATUS_PRIVILEGE_NOT_HELD
);
1935 if ((key
= get_hkey_obj( req
->hkey
, 0 )))
1937 save_registry( key
, req
->file
);
1938 release_object( key
);
1942 /* add a registry key change notification */
1943 DECL_HANDLER(set_registry_notification
)
1946 struct event
*event
;
1947 struct notify
*notify
;
1949 key
= get_hkey_obj( req
->hkey
, KEY_NOTIFY
);
1952 event
= get_event_obj( current
->process
, req
->event
, SYNCHRONIZE
);
1955 notify
= find_notify( key
, current
->process
, req
->hkey
);
1959 release_object( notify
->event
);
1960 grab_object( event
);
1961 notify
->event
= event
;
1965 notify
= mem_alloc( sizeof(*notify
) );
1968 grab_object( event
);
1969 notify
->event
= event
;
1970 notify
->subtree
= req
->subtree
;
1971 notify
->filter
= req
->filter
;
1972 notify
->hkey
= req
->hkey
;
1973 notify
->process
= current
->process
;
1974 list_add_head( &key
->notify_list
, ¬ify
->entry
);
1977 release_object( event
);
1979 release_object( key
);