blob: 483b064325a8d2ae2a36d7454a63f0bbd415ef7e [file] [log] [blame]
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001/*
2 * Server-side registry management
3 *
4 * Copyright (C) 1999 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000019 */
20
21/* To do:
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000022 * - symbolic links
23 */
24
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000025#include "config.h"
26#include "wine/port.h"
27
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000028#include <assert.h>
Alexandre Julliard7e495e12000-07-25 21:01:59 +000029#include <ctype.h>
Alexandre Julliardc9709042000-04-16 17:21:13 +000030#include <errno.h>
31#include <fcntl.h>
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000032#include <limits.h>
33#include <stdio.h>
Alexandre Julliard62986a02003-09-07 05:08:14 +000034#include <stdarg.h>
Alexandre Julliardc9709042000-04-16 17:21:13 +000035#include <string.h>
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000036#include <stdlib.h>
Alexandre Julliardc9709042000-04-16 17:21:13 +000037#include <sys/stat.h>
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000038#include <unistd.h>
Alexandre Julliard863637b2003-01-30 00:26:44 +000039
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010040#include "ntstatus.h"
41#define WIN32_NO_STATUS
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000042#include "object.h"
Alexandre Julliard863637b2003-01-30 00:26:44 +000043#include "file.h"
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000044#include "handle.h"
45#include "request.h"
Alexandre Julliardbbc03d52010-05-04 20:26:53 +020046#include "process.h"
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000047#include "unicode.h"
Robert Shearmanb3957e32005-05-05 16:57:55 +000048#include "security.h"
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000049
Patrik Stridvall9c1de6d2002-09-12 22:07:02 +000050#include "winternl.h"
Alexandre Julliard88e42612002-06-20 23:18:56 +000051#include "wine/library.h"
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000052
Mike McCormack11f4b442002-11-25 02:47:32 +000053struct notify
54{
Alexandre Julliard68abbc82005-02-24 19:43:53 +000055 struct list entry; /* entry in list of notifications */
Mike McCormack11f4b442002-11-25 02:47:32 +000056 struct event *event; /* event to set when changing this key */
57 int subtree; /* true if subtree notification */
58 unsigned int filter; /* which events to notify on */
59 obj_handle_t hkey; /* hkey associated with this notification */
Alexandre Julliard9b3b7ca2005-06-09 20:36:08 +000060 struct process *process; /* process in which the hkey is valid */
Mike McCormack11f4b442002-11-25 02:47:32 +000061};
62
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000063/* a registry key */
64struct key
65{
66 struct object obj; /* object header */
67 WCHAR *name; /* key name */
68 WCHAR *class; /* key class */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +000069 unsigned short namelen; /* length of key name */
70 unsigned short classlen; /* length of class name */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000071 struct key *parent; /* parent key */
72 int last_subkey; /* last in use subkey */
73 int nb_subkeys; /* count of allocated subkeys */
74 struct key **subkeys; /* subkeys array */
75 int last_value; /* last in use value */
76 int nb_values; /* count of allocated values in array */
77 struct key_value *values; /* values array */
Alexandre Julliarddcab7062005-03-14 11:00:43 +000078 unsigned int flags; /* flags */
Alexandre Julliard3343c402008-12-06 17:29:01 +010079 timeout_t modif; /* last modification time */
Alexandre Julliard68abbc82005-02-24 19:43:53 +000080 struct list notify_list; /* list of notifications */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000081};
82
83/* key flags */
84#define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
85#define KEY_DELETED 0x0002 /* key has been deleted */
Alexandre Julliard88e42612002-06-20 23:18:56 +000086#define KEY_DIRTY 0x0004 /* key has been modified */
Alexandre Julliardb139b932010-02-15 21:04:26 +010087#define KEY_SYMLINK 0x0008 /* key is a symbolic link */
Alexandre Julliardac08b032010-03-02 12:03:17 +010088#define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
Alexandre Julliard178cd202010-03-04 20:47:35 +010089#define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000090
91/* a key value */
92struct key_value
93{
94 WCHAR *name; /* value name */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +000095 unsigned short namelen; /* length of value name */
96 unsigned short type; /* value type */
Alexandre Julliard0f273c12006-07-26 10:43:25 +020097 data_size_t len; /* value data length in bytes */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000098 void *data; /* pointer to value data */
99};
100
101#define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
102#define MIN_VALUES 8 /* min. number of allocated values per key */
103
Alexandre Julliard935cc792010-04-02 11:48:14 +0200104#define MAX_NAME_LEN 255 /* max. length of a key name */
105#define MAX_VALUE_LEN 16383 /* max. length of a value name */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000106
Alexandre Julliard7e9757c2004-05-06 23:42:04 +0000107/* the root of the registry tree */
Alexandre Julliard6c8d9172000-08-26 04:40:07 +0000108static struct key *root_key;
109
Alexandre Julliard3343c402008-12-06 17:29:01 +0100110static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
Alexandre Julliardaaf477f2007-04-17 20:08:59 +0200111static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
Alexandre Julliardc9709042000-04-16 17:21:13 +0000112static struct timeout_user *save_timeout_user; /* saving timer */
Alexandre Julliard58791c92010-05-04 20:26:04 +0200113static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
Alexandre Julliardc9709042000-04-16 17:21:13 +0000114
Alexandre Julliardb139b932010-02-15 21:04:26 +0100115static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
Alexandre Julliardac08b032010-03-02 12:03:17 +0100116static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
Alexandre Julliardb139b932010-02-15 21:04:26 +0100117static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
118static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
119
Alexandre Julliarddcab7062005-03-14 11:00:43 +0000120static void set_periodic_save_timer(void);
Alexandre Julliardb139b932010-02-15 21:04:26 +0100121static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
Alexandre Julliarddcab7062005-03-14 11:00:43 +0000122
Alexandre Julliardc9709042000-04-16 17:21:13 +0000123/* information about where to save a registry branch */
124struct save_branch_info
125{
126 struct key *key;
Alexandre Julliard161160f2008-04-17 12:41:34 +0200127 const char *path;
Alexandre Julliardc9709042000-04-16 17:21:13 +0000128};
129
Alexandre Julliardc07ce052004-05-07 04:13:21 +0000130#define MAX_SAVE_BRANCH_INFO 3
Alexandre Julliardc9709042000-04-16 17:21:13 +0000131static int save_branch_count;
132static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
133
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000134
Alexandre Julliard53f3a831999-11-24 04:19:43 +0000135/* information about a file being loaded */
136struct file_load_info
137{
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000138 const char *filename; /* input file name */
139 FILE *file; /* input file */
140 char *buffer; /* line buffer */
141 int len; /* buffer length */
142 int line; /* current input line */
143 WCHAR *tmp; /* temp buffer to use while parsing input */
144 size_t tmplen; /* length of temp buffer */
Alexandre Julliard53f3a831999-11-24 04:19:43 +0000145};
146
147
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000148static void key_dump( struct object *obj, int verbose );
Alexandre Julliard32a93962005-12-12 15:02:26 +0100149static unsigned int key_map_access( struct object *obj, unsigned int access );
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000150static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000151static void key_destroy( struct object *obj );
152
153static const struct object_ops key_ops =
154{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000155 sizeof(struct key), /* size */
156 key_dump, /* dump */
Alexandre Julliard8382eb02007-12-05 18:16:42 +0100157 no_get_type, /* get_type */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000158 no_add_queue, /* add_queue */
159 NULL, /* remove_queue */
160 NULL, /* signaled */
161 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000162 no_signal, /* signal */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +0000163 no_get_fd, /* get_fd */
Alexandre Julliard32a93962005-12-12 15:02:26 +0100164 key_map_access, /* map_access */
Rob Shearmanc1707d82007-10-03 13:10:37 +0100165 default_get_sd, /* get_sd */
166 default_set_sd, /* set_sd */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000167 no_lookup_name, /* lookup_name */
Alexandre Julliard7e71c1d2007-03-22 11:44:29 +0100168 no_open_file, /* open_file */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000169 key_close_handle, /* close_handle */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000170 key_destroy /* destroy */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000171};
172
173
Alexandre Julliardac08b032010-03-02 12:03:17 +0100174static inline int is_wow6432node( const WCHAR *name, unsigned int len )
175{
176 return (len == sizeof(wow6432node) &&
177 !memicmpW( name, wow6432node, sizeof(wow6432node)/sizeof(WCHAR) ));
178}
179
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000180/*
181 * The registry text file format v2 used by this code is similar to the one
182 * used by REGEDIT import/export functionality, with the following differences:
183 * - strings and key names can contain \x escapes for Unicode
184 * - key names use escapes too in order to support Unicode
185 * - the modification time optionally follows the key name
186 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
187 */
188
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000189/* dump the full path of a key */
Alexandre Julliard88e42612002-06-20 23:18:56 +0000190static void dump_path( const struct key *key, const struct key *base, FILE *f )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000191{
Alexandre Julliard6c8d9172000-08-26 04:40:07 +0000192 if (key->parent && key->parent != base)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000193 {
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000194 dump_path( key->parent, base, f );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000195 fprintf( f, "\\\\" );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000196 }
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000197 dump_strW( key->name, key->namelen / sizeof(WCHAR), f, "[]" );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000198}
199
200/* dump a value to a text file */
Alexandre Julliard88e42612002-06-20 23:18:56 +0000201static void dump_value( const struct key_value *value, FILE *f )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000202{
Alexandre Julliardb647ded2008-01-07 21:06:49 +0100203 unsigned int i, dw;
Hans Leidekker719a7892004-09-22 02:46:38 +0000204 int count;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000205
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000206 if (value->namelen)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000207 {
208 fputc( '\"', f );
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000209 count = 1 + dump_strW( value->name, value->namelen / sizeof(WCHAR), f, "\"\"" );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000210 count += fprintf( f, "\"=" );
211 }
212 else count = fprintf( f, "@=" );
213
214 switch(value->type)
215 {
216 case REG_SZ:
217 case REG_EXPAND_SZ:
218 case REG_MULTI_SZ:
Alexandre Julliardb647ded2008-01-07 21:06:49 +0100219 /* only output properly terminated strings in string format */
220 if (value->len < sizeof(WCHAR)) break;
221 if (value->len % sizeof(WCHAR)) break;
222 if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
223 if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000224 fputc( '\"', f );
Marcus Meissner368d9352008-01-09 22:56:06 +0100225 dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
Alexandre Julliardb647ded2008-01-07 21:06:49 +0100226 fprintf( f, "\"\n" );
227 return;
228
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000229 case REG_DWORD:
Alexandre Julliardb647ded2008-01-07 21:06:49 +0100230 if (value->len != sizeof(dw)) break;
231 memcpy( &dw, value->data, sizeof(dw) );
232 fprintf( f, "dword:%08x\n", dw );
233 return;
234 }
235
236 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
237 else count += fprintf( f, "hex(%x):", value->type );
238 for (i = 0; i < value->len; i++)
239 {
240 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
241 if (i < value->len-1)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000242 {
Alexandre Julliardb647ded2008-01-07 21:06:49 +0100243 fputc( ',', f );
244 if (++count > 76)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000245 {
Alexandre Julliardb647ded2008-01-07 21:06:49 +0100246 fprintf( f, "\\\n " );
247 count = 2;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000248 }
249 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000250 }
251 fputc( '\n', f );
252}
253
254/* save a registry and all its subkeys to a text file */
Alexandre Julliard88e42612002-06-20 23:18:56 +0000255static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000256{
257 int i;
258
259 if (key->flags & KEY_VOLATILE) return;
Alexandre Julliard2a378672010-02-16 12:26:15 +0100260 /* save key if it has either some values or no subkeys, or needs special options */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000261 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
Alexandre Julliardb5d42892010-02-16 12:26:44 +0100262 if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000263 {
264 fprintf( f, "\n[" );
Alexandre Julliard6c8d9172000-08-26 04:40:07 +0000265 if (key != base) dump_path( key, base, f );
Alexandre Julliard3343c402008-12-06 17:29:01 +0100266 fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
Alexandre Julliard2a378672010-02-16 12:26:15 +0100267 if (key->class)
268 {
269 fprintf( f, "#class=\"" );
270 dump_strW( key->class, key->classlen / sizeof(WCHAR), f, "\"\"" );
271 fprintf( f, "\"\n" );
272 }
Alexandre Julliardb5d42892010-02-16 12:26:44 +0100273 if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000274 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
275 }
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000276 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000277}
278
Alexandre Julliard88e42612002-06-20 23:18:56 +0000279static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000280{
281 fprintf( stderr, "%s key ", op );
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000282 if (key) dump_path( key, NULL, stderr );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000283 else fprintf( stderr, "ERROR" );
284 if (value)
285 {
286 fprintf( stderr, " value ");
287 dump_value( value, stderr );
288 }
289 else fprintf( stderr, "\n" );
290}
291
292static void key_dump( struct object *obj, int verbose )
293{
294 struct key *key = (struct key *)obj;
295 assert( obj->ops == &key_ops );
296 fprintf( stderr, "Key flags=%x ", key->flags );
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000297 dump_path( key, NULL, stderr );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000298 fprintf( stderr, "\n" );
299}
300
Mike McCormack11f4b442002-11-25 02:47:32 +0000301/* notify waiter and maybe delete the notification */
302static void do_notification( struct key *key, struct notify *notify, int del )
303{
Robert Shearman37957092005-06-10 19:54:46 +0000304 if (notify->event)
Mike McCormack11f4b442002-11-25 02:47:32 +0000305 {
306 set_event( notify->event );
307 release_object( notify->event );
308 notify->event = NULL;
309 }
Alexandre Julliard68abbc82005-02-24 19:43:53 +0000310 if (del)
311 {
312 list_remove( &notify->entry );
313 free( notify );
314 }
Mike McCormack11f4b442002-11-25 02:47:32 +0000315}
316
Alexandre Julliard9b3b7ca2005-06-09 20:36:08 +0000317static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
Mike McCormack11f4b442002-11-25 02:47:32 +0000318{
Alexandre Julliard68abbc82005-02-24 19:43:53 +0000319 struct notify *notify;
Mike McCormack11f4b442002-11-25 02:47:32 +0000320
Alexandre Julliard68abbc82005-02-24 19:43:53 +0000321 LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
322 {
Alexandre Julliard9b3b7ca2005-06-09 20:36:08 +0000323 if (notify->process == process && notify->hkey == hkey) return notify;
Alexandre Julliard68abbc82005-02-24 19:43:53 +0000324 }
325 return NULL;
Mike McCormack11f4b442002-11-25 02:47:32 +0000326}
327
Alexandre Julliard32a93962005-12-12 15:02:26 +0100328static unsigned int key_map_access( struct object *obj, unsigned int access )
329{
330 if (access & GENERIC_READ) access |= KEY_READ;
331 if (access & GENERIC_WRITE) access |= KEY_WRITE;
332 if (access & GENERIC_EXECUTE) access |= KEY_EXECUTE;
333 if (access & GENERIC_ALL) access |= KEY_ALL_ACCESS;
334 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
335}
336
Mike McCormack11f4b442002-11-25 02:47:32 +0000337/* close the notification associated with a handle */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000338static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
Mike McCormack11f4b442002-11-25 02:47:32 +0000339{
340 struct key * key = (struct key *) obj;
Alexandre Julliard9b3b7ca2005-06-09 20:36:08 +0000341 struct notify *notify = find_notify( key, process, handle );
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000342 if (notify) do_notification( key, notify, 1 );
343 return 1; /* ok to close */
Mike McCormack11f4b442002-11-25 02:47:32 +0000344}
345
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000346static void key_destroy( struct object *obj )
347{
348 int i;
Alexandre Julliard68abbc82005-02-24 19:43:53 +0000349 struct list *ptr;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000350 struct key *key = (struct key *)obj;
351 assert( obj->ops == &key_ops );
352
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200353 free( key->name );
354 free( key->class );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000355 for (i = 0; i <= key->last_value; i++)
356 {
Lionel Debrouxe122f812007-10-20 09:37:43 +0200357 free( key->values[i].name );
358 free( key->values[i].data );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000359 }
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200360 free( key->values );
Alexandre Julliard53f3a831999-11-24 04:19:43 +0000361 for (i = 0; i <= key->last_subkey; i++)
362 {
363 key->subkeys[i]->parent = NULL;
364 release_object( key->subkeys[i] );
365 }
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200366 free( key->subkeys );
Mike McCormack11f4b442002-11-25 02:47:32 +0000367 /* unconditionally notify everything waiting on this key */
Alexandre Julliard68abbc82005-02-24 19:43:53 +0000368 while ((ptr = list_head( &key->notify_list )))
369 {
370 struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
371 do_notification( key, notify, 1 );
372 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000373}
374
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000375/* get the request vararg as registry path */
Andrew Talbotb1788c82007-03-17 10:52:14 +0000376static inline void get_req_path( struct unicode_str *str, int skip_root )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000377{
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000378 str->str = get_req_data();
379 str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000380
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000381 if (skip_root && str->len >= sizeof(root_name) &&
382 !memicmpW( str->str, root_name, sizeof(root_name)/sizeof(WCHAR) ))
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000383 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000384 str->str += sizeof(root_name)/sizeof(WCHAR);
385 str->len -= sizeof(root_name);
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000386 }
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000387}
388
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000389/* return the next token in a given path */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000390/* token->str must point inside the path, or be NULL for the first call */
391static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000392{
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200393 data_size_t i = 0, len = path->len / sizeof(WCHAR);
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000394
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000395 if (!token->str) /* first time */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000396 {
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000397 /* path cannot start with a backslash */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000398 if (len && path->str[0] == '\\')
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000399 {
400 set_error( STATUS_OBJECT_PATH_INVALID );
401 return NULL;
402 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000403 }
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000404 else
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000405 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000406 i = token->str - path->str;
407 i += token->len / sizeof(WCHAR);
408 while (i < len && path->str[i] == '\\') i++;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000409 }
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000410 token->str = path->str + i;
411 while (i < len && path->str[i] != '\\') i++;
412 token->len = (path->str + i - token->str) * sizeof(WCHAR);
413 return token;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000414}
415
416/* allocate a key object */
Alexandre Julliard3343c402008-12-06 17:29:01 +0100417static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000418{
419 struct key *key;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000420 if ((key = alloc_object( &key_ops )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000421 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000422 key->name = NULL;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000423 key->class = NULL;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000424 key->namelen = name->len;
425 key->classlen = 0;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000426 key->flags = 0;
427 key->last_subkey = -1;
428 key->nb_subkeys = 0;
429 key->subkeys = NULL;
430 key->nb_values = 0;
431 key->last_value = -1;
432 key->values = NULL;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000433 key->modif = modif;
434 key->parent = NULL;
Alexandre Julliard68abbc82005-02-24 19:43:53 +0000435 list_init( &key->notify_list );
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000436 if (name->len && !(key->name = memdup( name->str, name->len )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000437 {
438 release_object( key );
439 key = NULL;
440 }
441 }
442 return key;
443}
444
Alexandre Julliard88e42612002-06-20 23:18:56 +0000445/* mark a key and all its parents as dirty (modified) */
446static void make_dirty( struct key *key )
447{
448 while (key)
449 {
450 if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
451 key->flags |= KEY_DIRTY;
452 key = key->parent;
453 }
454}
455
456/* mark a key and all its subkeys as clean (not modified) */
457static void make_clean( struct key *key )
458{
459 int i;
460
461 if (key->flags & KEY_VOLATILE) return;
462 if (!(key->flags & KEY_DIRTY)) return;
463 key->flags &= ~KEY_DIRTY;
464 for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
465}
466
Mike McCormack11f4b442002-11-25 02:47:32 +0000467/* go through all the notifications and send them if necessary */
Robert Shearmanc5165712005-05-25 18:41:09 +0000468static void check_notify( struct key *key, unsigned int change, int not_subtree )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000469{
Alexandre Julliard68abbc82005-02-24 19:43:53 +0000470 struct list *ptr, *next;
471
472 LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
Mike McCormack11f4b442002-11-25 02:47:32 +0000473 {
Alexandre Julliard68abbc82005-02-24 19:43:53 +0000474 struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
Mike McCormack11f4b442002-11-25 02:47:32 +0000475 if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
476 do_notification( key, n, 0 );
Mike McCormack11f4b442002-11-25 02:47:32 +0000477 }
478}
479
480/* update key modification time */
481static void touch_key( struct key *key, unsigned int change )
482{
483 struct key *k;
484
Alexandre Julliard3343c402008-12-06 17:29:01 +0100485 key->modif = current_time;
Alexandre Julliard88e42612002-06-20 23:18:56 +0000486 make_dirty( key );
Mike McCormack11f4b442002-11-25 02:47:32 +0000487
488 /* do notifications */
489 check_notify( key, change, 1 );
490 for ( k = key->parent; k; k = k->parent )
491 check_notify( k, change & ~REG_NOTIFY_CHANGE_LAST_SET, 0 );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000492}
493
494/* try to grow the array of subkeys; return 1 if OK, 0 on error */
495static int grow_subkeys( struct key *key )
496{
497 struct key **new_subkeys;
498 int nb_subkeys;
499
500 if (key->nb_subkeys)
501 {
502 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
503 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
504 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000505 set_error( STATUS_NO_MEMORY );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000506 return 0;
507 }
508 }
509 else
510 {
511 nb_subkeys = MIN_VALUES;
512 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
513 }
514 key->subkeys = new_subkeys;
515 key->nb_subkeys = nb_subkeys;
516 return 1;
517}
518
519/* allocate a subkey for a given key, and return its index */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000520static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
Alexandre Julliard3343c402008-12-06 17:29:01 +0100521 int index, timeout_t modif )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000522{
523 struct key *key;
524 int i;
525
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000526 if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
527 {
528 set_error( STATUS_NAME_TOO_LONG );
529 return NULL;
530 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000531 if (parent->last_subkey + 1 == parent->nb_subkeys)
532 {
533 /* need to grow the array */
534 if (!grow_subkeys( parent )) return NULL;
535 }
536 if ((key = alloc_key( name, modif )) != NULL)
537 {
538 key->parent = parent;
539 for (i = ++parent->last_subkey; i > index; i--)
540 parent->subkeys[i] = parent->subkeys[i-1];
541 parent->subkeys[index] = key;
Alexandre Julliard0ea28062010-04-01 14:09:12 +0200542 if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
543 parent->flags |= KEY_WOW64;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000544 }
545 return key;
546}
547
548/* free a subkey of a given key */
549static void free_subkey( struct key *parent, int index )
550{
551 struct key *key;
552 int i, nb_subkeys;
553
554 assert( index >= 0 );
555 assert( index <= parent->last_subkey );
556
557 key = parent->subkeys[index];
558 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
559 parent->last_subkey--;
560 key->flags |= KEY_DELETED;
561 key->parent = NULL;
Alexandre Julliardac08b032010-03-02 12:03:17 +0100562 if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000563 release_object( key );
Vincent Béron9a624912002-05-31 23:06:46 +0000564
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000565 /* try to shrink the array */
Alexandre Julliard88e42612002-06-20 23:18:56 +0000566 nb_subkeys = parent->nb_subkeys;
567 if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000568 {
569 struct key **new_subkeys;
570 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
571 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
Alexandre Julliard88e42612002-06-20 23:18:56 +0000572 if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
573 parent->subkeys = new_subkeys;
574 parent->nb_subkeys = nb_subkeys;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000575 }
576}
577
578/* find the named child of a given key and return its index */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000579static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000580{
581 int i, min, max, res;
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200582 data_size_t len;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000583
584 min = 0;
585 max = key->last_subkey;
586 while (min <= max)
587 {
588 i = (min + max) / 2;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000589 len = min( key->subkeys[i]->namelen, name->len );
590 res = memicmpW( key->subkeys[i]->name, name->str, len / sizeof(WCHAR) );
591 if (!res) res = key->subkeys[i]->namelen - name->len;
592 if (!res)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000593 {
594 *index = i;
595 return key->subkeys[i];
596 }
597 if (res > 0) max = i - 1;
598 else min = i + 1;
599 }
600 *index = min; /* this is where we should insert it */
601 return NULL;
602}
603
Alexandre Julliardac08b032010-03-02 12:03:17 +0100604/* return the wow64 variant of the key, or the key itself if none */
605static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
606{
607 static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
608 int index;
609
610 if (!(key->flags & KEY_WOW64)) return key;
611 if (!is_wow6432node( name->str, name->len ))
612 {
613 key = find_subkey( key, &wow6432node_str, &index );
614 assert( key ); /* if KEY_WOW64 is set we must find it */
615 }
616 return key;
617}
618
619
Alexandre Julliardb139b932010-02-15 21:04:26 +0100620/* follow a symlink and return the resolved key */
621static struct key *follow_symlink( struct key *key, int iteration )
622{
623 struct unicode_str path, token;
624 struct key_value *value;
625 int index;
626
627 if (iteration > 16) return NULL;
628 if (!(key->flags & KEY_SYMLINK)) return key;
629 if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
630
631 path.str = value->data;
632 path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
633 if (path.len <= sizeof(root_name)) return NULL;
634 if (memicmpW( path.str, root_name, sizeof(root_name)/sizeof(WCHAR) )) return NULL;
635 path.str += sizeof(root_name) / sizeof(WCHAR);
636 path.len -= sizeof(root_name);
637
638 key = root_key;
639 token.str = NULL;
640 if (!get_path_token( &path, &token )) return NULL;
641 while (token.len)
642 {
643 if (!(key = find_subkey( key, &token, &index ))) break;
644 if (!(key = follow_symlink( key, iteration + 1 ))) break;
645 get_path_token( &path, &token );
646 }
647 return key;
648}
649
Alexandre Julliard606cc062010-03-04 20:46:42 +0100650/* open a key until we find an element that doesn't exist */
651/* helper for open_key and create_key */
652static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
653 unsigned int access, struct unicode_str *token, int *index )
654{
655 token->str = NULL;
656 if (!get_path_token( name, token )) return NULL;
657 if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
658 while (token->len)
659 {
660 struct key *subkey;
Alexandre Julliard178cd202010-03-04 20:47:35 +0100661 if (!(subkey = find_subkey( key, token, index )))
662 {
663 if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
664 {
665 /* try in the 64-bit parent */
666 key = key->parent;
667 subkey = find_subkey( key, token, index );
668 }
669 }
670 if (!subkey) break;
Alexandre Julliard606cc062010-03-04 20:46:42 +0100671 key = subkey;
672 get_path_token( name, token );
673 if (!token->len) break;
674 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
675 if (!(key = follow_symlink( key, 0 )))
676 {
677 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
678 return NULL;
679 }
680 }
681 return key;
682}
683
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000684/* open a subkey */
Alexandre Julliardac08b032010-03-02 12:03:17 +0100685static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
686 unsigned int attributes )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000687{
688 int index;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000689 struct unicode_str token;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000690
Alexandre Julliard606cc062010-03-04 20:46:42 +0100691 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000692
Alexandre Julliard606cc062010-03-04 20:46:42 +0100693 if (token.len)
694 {
695 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
696 return NULL;
697 }
Alexandre Julliardac08b032010-03-02 12:03:17 +0100698 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
Alexandre Julliardb139b932010-02-15 21:04:26 +0100699 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
700 {
701 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
702 return NULL;
703 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000704 if (debug_level > 1) dump_operation( key, NULL, "Open" );
Alexandre Julliardb139b932010-02-15 21:04:26 +0100705 grab_object( key );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000706 return key;
707}
708
709/* create a subkey */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000710static struct key *create_key( struct key *key, const struct unicode_str *name,
Alexandre Julliard22de1f42010-03-01 14:13:46 +0100711 const struct unicode_str *class, unsigned int options,
Alexandre Julliardac08b032010-03-02 12:03:17 +0100712 unsigned int access, unsigned int attributes, int *created )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000713{
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000714 int index;
Alexandre Julliard8eca1da2010-03-01 14:14:50 +0100715 struct unicode_str token, next;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000716
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000717 *created = 0;
Alexandre Julliard606cc062010-03-04 20:46:42 +0100718 if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000719
Alexandre Julliard8eca1da2010-03-01 14:14:50 +0100720 if (!token.len) /* the key already exists */
Alexandre Julliardb139b932010-02-15 21:04:26 +0100721 {
Alexandre Julliardac08b032010-03-02 12:03:17 +0100722 if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
Alexandre Julliardb139b932010-02-15 21:04:26 +0100723 if (options & REG_OPTION_CREATE_LINK)
724 {
725 set_error( STATUS_OBJECT_NAME_COLLISION );
726 return NULL;
727 }
728 if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
729 {
730 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
731 return NULL;
732 }
Alexandre Julliard8eca1da2010-03-01 14:14:50 +0100733 if (debug_level > 1) dump_operation( key, NULL, "Open" );
734 grab_object( key );
735 return key;
Alexandre Julliardb139b932010-02-15 21:04:26 +0100736 }
Alexandre Julliard8eca1da2010-03-01 14:14:50 +0100737
738 /* token must be the last path component at this point */
739 next = token;
740 get_path_token( name, &next );
741 if (next.len)
742 {
743 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
744 return NULL;
745 }
746
Alexandre Julliard22de1f42010-03-01 14:13:46 +0100747 if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
Alexandre Julliard72ba00f2009-11-20 11:39:55 +0100748 {
749 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
750 return NULL;
751 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000752 *created = 1;
Alexandre Julliard22de1f42010-03-01 14:13:46 +0100753 make_dirty( key );
754 if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000755
Alexandre Julliard8eca1da2010-03-01 14:14:50 +0100756 if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
757 if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
758 else key->flags |= KEY_DIRTY;
759
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000760 if (debug_level > 1) dump_operation( key, NULL, "Create" );
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000761 if (class && class->len)
762 {
763 key->classlen = class->len;
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +0200764 free(key->class);
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000765 if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
766 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000767 grab_object( key );
768 return key;
769}
770
Alexandre Julliard22de1f42010-03-01 14:13:46 +0100771/* recursively create a subkey (for internal use only) */
772static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
773{
774 struct key *base;
775 int index;
776 struct unicode_str token;
777
778 token.str = NULL;
779 if (!get_path_token( name, &token )) return NULL;
780 while (token.len)
781 {
782 struct key *subkey;
783 if (!(subkey = find_subkey( key, &token, &index ))) break;
784 key = subkey;
785 if (!(key = follow_symlink( key, 0 )))
786 {
787 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
788 return NULL;
789 }
790 get_path_token( name, &token );
791 }
792
793 if (token.len)
794 {
795 if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
796 base = key;
797 for (;;)
798 {
799 get_path_token( name, &token );
800 if (!token.len) break;
801 /* we know the index is always 0 in a new key */
802 if (!(key = alloc_subkey( key, &token, 0, modif )))
803 {
804 free_subkey( base, index );
805 return NULL;
806 }
807 }
808 }
809
810 grab_object( key );
811 return key;
812}
813
Alexandre Julliard454355e2000-10-02 03:46:58 +0000814/* query information about a key or a subkey */
Alexandre Julliard88e42612002-06-20 23:18:56 +0000815static void enum_key( const struct key *key, int index, int info_class,
816 struct enum_key_reply *reply )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000817{
Alexandre Julliard454355e2000-10-02 03:46:58 +0000818 int i;
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200819 data_size_t len, namelen, classlen;
Michael Stefaniuca6249772006-07-25 10:30:04 +0200820 data_size_t max_subkey = 0, max_class = 0;
821 data_size_t max_value = 0, max_data = 0;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000822 char *data;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000823
Alexandre Julliard454355e2000-10-02 03:46:58 +0000824 if (index != -1) /* -1 means use the specified key directly */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000825 {
Alexandre Julliard454355e2000-10-02 03:46:58 +0000826 if ((index < 0) || (index > key->last_subkey))
827 {
828 set_error( STATUS_NO_MORE_ENTRIES );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000829 return;
Alexandre Julliard454355e2000-10-02 03:46:58 +0000830 }
831 key = key->subkeys[index];
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000832 }
Alexandre Julliard454355e2000-10-02 03:46:58 +0000833
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000834 namelen = key->namelen;
835 classlen = key->classlen;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000836
837 switch(info_class)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000838 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000839 case KeyBasicInformation:
840 classlen = 0; /* only return the name */
841 /* fall through */
842 case KeyNodeInformation:
843 reply->max_subkey = 0;
844 reply->max_class = 0;
845 reply->max_value = 0;
846 reply->max_data = 0;
847 break;
848 case KeyFullInformation:
Alexandre Julliard454355e2000-10-02 03:46:58 +0000849 for (i = 0; i <= key->last_subkey; i++)
850 {
851 struct key *subkey = key->subkeys[i];
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000852 len = subkey->namelen / sizeof(WCHAR);
Alexandre Julliard454355e2000-10-02 03:46:58 +0000853 if (len > max_subkey) max_subkey = len;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000854 len = subkey->classlen / sizeof(WCHAR);
Alexandre Julliard454355e2000-10-02 03:46:58 +0000855 if (len > max_class) max_class = len;
856 }
857 for (i = 0; i <= key->last_value; i++)
858 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000859 len = key->values[i].namelen / sizeof(WCHAR);
Alexandre Julliard454355e2000-10-02 03:46:58 +0000860 if (len > max_value) max_value = len;
861 len = key->values[i].len;
862 if (len > max_data) max_data = len;
863 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000864 reply->max_subkey = max_subkey;
865 reply->max_class = max_class;
866 reply->max_value = max_value;
867 reply->max_data = max_data;
868 namelen = 0; /* only return the class */
869 break;
870 default:
871 set_error( STATUS_INVALID_PARAMETER );
872 return;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000873 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000874 reply->subkeys = key->last_subkey + 1;
875 reply->values = key->last_value + 1;
876 reply->modif = key->modif;
877 reply->total = namelen + classlen;
878
879 len = min( reply->total, get_reply_max_size() );
880 if (len && (data = set_reply_data_size( len )))
Alexandre Julliard454355e2000-10-02 03:46:58 +0000881 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000882 if (len > namelen)
883 {
884 reply->namelen = namelen;
885 memcpy( data, key->name, namelen );
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000886 memcpy( data + namelen, key->class, len - namelen );
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000887 }
888 else
889 {
890 reply->namelen = len;
891 memcpy( data, key->name, len );
892 }
Alexandre Julliard454355e2000-10-02 03:46:58 +0000893 }
Alexandre Julliard454355e2000-10-02 03:46:58 +0000894 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000895}
896
897/* delete a key and its values */
Mike McCormack5ac945c2003-08-19 03:08:17 +0000898static int delete_key( struct key *key, int recurse )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000899{
900 int index;
Alexandre Julliard6ebc6272010-04-02 12:43:31 +0200901 struct key *parent = key->parent;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000902
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000903 /* must find parent and index */
Alexandre Julliard7e9757c2004-05-06 23:42:04 +0000904 if (key == root_key)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000905 {
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000906 set_error( STATUS_ACCESS_DENIED );
Mike McCormack5ac945c2003-08-19 03:08:17 +0000907 return -1;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000908 }
Alexandre Julliard6ebc6272010-04-02 12:43:31 +0200909 assert( parent );
Mike McCormack5ac945c2003-08-19 03:08:17 +0000910
911 while (recurse && (key->last_subkey>=0))
Robert Shearman37957092005-06-10 19:54:46 +0000912 if (0 > delete_key(key->subkeys[key->last_subkey], 1))
Mike McCormack5ac945c2003-08-19 03:08:17 +0000913 return -1;
914
Alexandre Julliardbcf393a2000-10-01 01:44:50 +0000915 for (index = 0; index <= parent->last_subkey; index++)
916 if (parent->subkeys[index] == key) break;
917 assert( index <= parent->last_subkey );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000918
Alexandre Julliard7e9757c2004-05-06 23:42:04 +0000919 /* we can only delete a key that has no subkeys */
920 if (key->last_subkey >= 0)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000921 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000922 set_error( STATUS_ACCESS_DENIED );
Mike McCormack5ac945c2003-08-19 03:08:17 +0000923 return -1;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000924 }
Mike McCormack5ac945c2003-08-19 03:08:17 +0000925
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000926 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
927 free_subkey( parent, index );
Mike McCormack11f4b442002-11-25 02:47:32 +0000928 touch_key( parent, REG_NOTIFY_CHANGE_NAME );
Mike McCormack5ac945c2003-08-19 03:08:17 +0000929 return 0;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000930}
931
932/* try to grow the array of values; return 1 if OK, 0 on error */
933static int grow_values( struct key *key )
934{
935 struct key_value *new_val;
936 int nb_values;
937
938 if (key->nb_values)
939 {
940 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
941 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
942 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000943 set_error( STATUS_NO_MEMORY );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000944 return 0;
945 }
946 }
947 else
948 {
949 nb_values = MIN_VALUES;
950 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
951 }
952 key->values = new_val;
953 key->nb_values = nb_values;
954 return 1;
955}
956
957/* find the named value of a given key and return its index in the array */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000958static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000959{
960 int i, min, max, res;
Alexandre Julliard0f273c12006-07-26 10:43:25 +0200961 data_size_t len;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000962
963 min = 0;
964 max = key->last_value;
965 while (min <= max)
966 {
967 i = (min + max) / 2;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000968 len = min( key->values[i].namelen, name->len );
969 res = memicmpW( key->values[i].name, name->str, len / sizeof(WCHAR) );
970 if (!res) res = key->values[i].namelen - name->len;
971 if (!res)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000972 {
973 *index = i;
974 return &key->values[i];
975 }
976 if (res > 0) max = i - 1;
977 else min = i + 1;
978 }
979 *index = min; /* this is where we should insert it */
980 return NULL;
981}
982
Alexandre Julliard88e42612002-06-20 23:18:56 +0000983/* insert a new value; the index must have been returned by find_value */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000984static struct key_value *insert_value( struct key *key, const struct unicode_str *name, int index )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000985{
986 struct key_value *value;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000987 WCHAR *new_name = NULL;
Alexandre Julliard88e42612002-06-20 23:18:56 +0000988 int i;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000989
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000990 if (name->len > MAX_VALUE_LEN * sizeof(WCHAR))
991 {
992 set_error( STATUS_NAME_TOO_LONG );
993 return NULL;
994 }
Alexandre Julliard88e42612002-06-20 23:18:56 +0000995 if (key->last_value + 1 == key->nb_values)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000996 {
Alexandre Julliard88e42612002-06-20 23:18:56 +0000997 if (!grow_values( key )) return NULL;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000998 }
Alexandre Julliard7f9e2812005-11-22 12:05:36 +0000999 if (name->len && !(new_name = memdup( name->str, name->len ))) return NULL;
Alexandre Julliard88e42612002-06-20 23:18:56 +00001000 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
1001 value = &key->values[index];
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001002 value->name = new_name;
1003 value->namelen = name->len;
1004 value->len = 0;
1005 value->data = NULL;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001006 return value;
1007}
1008
1009/* set a key value */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001010static void set_value( struct key *key, const struct unicode_str *name,
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001011 int type, const void *data, data_size_t len )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001012{
1013 struct key_value *value;
1014 void *ptr = NULL;
Alexandre Julliard88e42612002-06-20 23:18:56 +00001015 int index;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001016
Alexandre Julliard88e42612002-06-20 23:18:56 +00001017 if ((value = find_value( key, name, &index )))
1018 {
1019 /* check if the new value is identical to the existing one */
1020 if (value->type == type && value->len == len &&
1021 value->data && !memcmp( value->data, data, len ))
1022 {
1023 if (debug_level > 1) dump_operation( key, value, "Skip setting" );
1024 return;
1025 }
1026 }
1027
Alexandre Julliardb139b932010-02-15 21:04:26 +01001028 if (key->flags & KEY_SYMLINK)
1029 {
1030 if (type != REG_LINK || name->len != symlink_str.len ||
1031 memicmpW( name->str, symlink_str.str, name->len / sizeof(WCHAR) ))
1032 {
1033 set_error( STATUS_ACCESS_DENIED );
1034 return;
1035 }
1036 }
1037
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001038 if (len && !(ptr = memdup( data, len ))) return;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001039
Alexandre Julliard88e42612002-06-20 23:18:56 +00001040 if (!value)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001041 {
Alexandre Julliard88e42612002-06-20 23:18:56 +00001042 if (!(value = insert_value( key, name, index )))
1043 {
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +02001044 free( ptr );
Alexandre Julliard88e42612002-06-20 23:18:56 +00001045 return;
1046 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001047 }
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +02001048 else free( value->data ); /* already existing, free previous data */
Alexandre Julliard88e42612002-06-20 23:18:56 +00001049
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001050 value->type = type;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001051 value->len = len;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001052 value->data = ptr;
Mike McCormack11f4b442002-11-25 02:47:32 +00001053 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001054 if (debug_level > 1) dump_operation( key, value, "Set" );
1055}
1056
1057/* get a key value */
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001058static void get_value( struct key *key, const struct unicode_str *name, int *type, data_size_t *len )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001059{
1060 struct key_value *value;
1061 int index;
1062
1063 if ((value = find_value( key, name, &index )))
1064 {
1065 *type = value->type;
1066 *len = value->len;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001067 if (value->data) set_reply_data( value->data, min( value->len, get_reply_max_size() ));
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001068 if (debug_level > 1) dump_operation( key, value, "Get" );
1069 }
1070 else
1071 {
1072 *type = -1;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +00001073 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001074 }
1075}
1076
1077/* enumerate a key value */
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001078static void enum_value( struct key *key, int i, int info_class, struct enum_key_value_reply *reply )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001079{
1080 struct key_value *value;
1081
Alexandre Julliardef886372000-04-04 19:33:27 +00001082 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001083 else
1084 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001085 void *data;
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001086 data_size_t namelen, maxlen;
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001087
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001088 value = &key->values[i];
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001089 reply->type = value->type;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001090 namelen = value->namelen;
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001091
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001092 switch(info_class)
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001093 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001094 case KeyValueBasicInformation:
1095 reply->total = namelen;
1096 break;
1097 case KeyValueFullInformation:
1098 reply->total = namelen + value->len;
1099 break;
1100 case KeyValuePartialInformation:
1101 reply->total = value->len;
1102 namelen = 0;
1103 break;
1104 default:
1105 set_error( STATUS_INVALID_PARAMETER );
1106 return;
1107 }
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001108
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001109 maxlen = min( reply->total, get_reply_max_size() );
1110 if (maxlen && ((data = set_reply_data_size( maxlen ))))
1111 {
1112 if (maxlen > namelen)
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001113 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001114 reply->namelen = namelen;
1115 memcpy( data, value->name, namelen );
1116 memcpy( (char *)data + namelen, value->data, maxlen - namelen );
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001117 }
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001118 else
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001119 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001120 reply->namelen = maxlen;
1121 memcpy( data, value->name, maxlen );
Alexandre Julliard0b6a79c2000-12-15 20:57:00 +00001122 }
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001123 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001124 if (debug_level > 1) dump_operation( key, value, "Enum" );
1125 }
1126}
1127
1128/* delete a value */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001129static void delete_value( struct key *key, const struct unicode_str *name )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001130{
1131 struct key_value *value;
1132 int i, index, nb_values;
1133
1134 if (!(value = find_value( key, name, &index )))
1135 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +00001136 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001137 return;
1138 }
1139 if (debug_level > 1) dump_operation( key, value, "Delete" );
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +02001140 free( value->name );
1141 free( value->data );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001142 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
1143 key->last_value--;
Mike McCormack11f4b442002-11-25 02:47:32 +00001144 touch_key( key, REG_NOTIFY_CHANGE_LAST_SET );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001145
1146 /* try to shrink the array */
1147 nb_values = key->nb_values;
1148 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
1149 {
1150 struct key_value *new_val;
1151 nb_values -= nb_values / 3; /* shrink by 33% */
1152 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
1153 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
1154 key->values = new_val;
1155 key->nb_values = nb_values;
1156 }
Alexandre Julliard6c8d9172000-08-26 04:40:07 +00001157}
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001158
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001159/* get the registry key corresponding to an hkey handle */
Alexandre Julliard6ebc6272010-04-02 12:43:31 +02001160static struct key *get_hkey_obj( obj_handle_t hkey, unsigned int access )
Alexandre Julliarddd77d1a2006-07-10 11:53:23 +02001161{
Alexandre Julliard6ebc6272010-04-02 12:43:31 +02001162 struct key *key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
1163
1164 if (key && key->flags & KEY_DELETED)
1165 {
1166 set_error( STATUS_KEY_DELETED );
1167 release_object( key );
1168 key = NULL;
1169 }
1170 return key;
Alexandre Julliarddd77d1a2006-07-10 11:53:23 +02001171}
1172
1173/* get the registry key corresponding to a parent key handle */
1174static inline struct key *get_parent_hkey_obj( obj_handle_t hkey )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001175{
Alexandre Julliard6c8d9172000-08-26 04:40:07 +00001176 if (!hkey) return (struct key *)grab_object( root_key );
Alexandre Julliard6ebc6272010-04-02 12:43:31 +02001177 return get_hkey_obj( hkey, 0 );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001178}
1179
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001180/* read a line from the input file */
1181static int read_next_line( struct file_load_info *info )
1182{
1183 char *newbuf;
1184 int newlen, pos = 0;
1185
1186 info->line++;
1187 for (;;)
1188 {
1189 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
1190 return (pos != 0); /* EOF */
1191 pos = strlen(info->buffer);
1192 if (info->buffer[pos-1] == '\n')
1193 {
1194 /* got a full line */
1195 info->buffer[--pos] = 0;
1196 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
1197 return 1;
1198 }
1199 if (pos < info->len - 1) return 1; /* EOF but something was read */
1200
1201 /* need to enlarge the buffer */
1202 newlen = info->len + info->len / 2;
1203 if (!(newbuf = realloc( info->buffer, newlen )))
1204 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +00001205 set_error( STATUS_NO_MEMORY );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001206 return -1;
1207 }
1208 info->buffer = newbuf;
1209 info->len = newlen;
1210 }
1211}
1212
1213/* make sure the temp buffer holds enough space */
Alexandre Julliardb0e091a2005-08-09 10:36:45 +00001214static int get_file_tmp_space( struct file_load_info *info, size_t size )
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001215{
Alexandre Julliardb0e091a2005-08-09 10:36:45 +00001216 WCHAR *tmp;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001217 if (info->tmplen >= size) return 1;
1218 if (!(tmp = realloc( info->tmp, size )))
1219 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +00001220 set_error( STATUS_NO_MEMORY );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001221 return 0;
1222 }
1223 info->tmp = tmp;
1224 info->tmplen = size;
1225 return 1;
1226}
1227
1228/* report an error while loading an input file */
1229static void file_read_error( const char *err, struct file_load_info *info )
1230{
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001231 if (info->filename)
1232 fprintf( stderr, "%s:%d: %s '%s'\n", info->filename, info->line, err, info->buffer );
1233 else
1234 fprintf( stderr, "<fd>:%d: %s '%s'\n", info->line, err, info->buffer );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001235}
1236
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001237/* convert a data type tag to a value type */
1238static int get_data_type( const char *buffer, int *type, int *parse_type )
1239{
1240 struct data_type { const char *tag; int len; int type; int parse_type; };
1241
Vincent Béron9a624912002-05-31 23:06:46 +00001242 static const struct data_type data_types[] =
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001243 { /* actual type */ /* type to assume for parsing */
1244 { "\"", 1, REG_SZ, REG_SZ },
1245 { "str:\"", 5, REG_SZ, REG_SZ },
1246 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1247 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1248 { "hex:", 4, REG_BINARY, REG_BINARY },
1249 { "dword:", 6, REG_DWORD, REG_DWORD },
1250 { "hex(", 4, -1, REG_BINARY },
Joerg Mayer959d73e2000-10-22 23:56:32 +00001251 { NULL, 0, 0, 0 }
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001252 };
1253
1254 const struct data_type *ptr;
1255 char *end;
1256
1257 for (ptr = data_types; ptr->tag; ptr++)
1258 {
Alexandre Julliardfb9955d2008-01-07 21:06:01 +01001259 if (strncmp( ptr->tag, buffer, ptr->len )) continue;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001260 *parse_type = ptr->parse_type;
1261 if ((*type = ptr->type) != -1) return ptr->len;
1262 /* "hex(xx):" is special */
1263 *type = (int)strtoul( buffer + 4, &end, 16 );
Alexandre Julliardfb9955d2008-01-07 21:06:01 +01001264 if ((end <= buffer) || strncmp( end, "):", 2 )) return 0;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001265 return end + 2 - buffer;
1266 }
1267 return 0;
1268}
1269
1270/* load and create a key from the input file */
Alexandre Julliard22de1f42010-03-01 14:13:46 +01001271static struct key *load_key( struct key *base, const char *buffer,
Alexandre Julliard3343c402008-12-06 17:29:01 +01001272 int prefix_len, struct file_load_info *info )
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001273{
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001274 WCHAR *p;
1275 struct unicode_str name;
Alexandre Julliard3343c402008-12-06 17:29:01 +01001276 int res;
1277 unsigned int mod;
1278 timeout_t modif = current_time;
Alexandre Julliard12717392008-01-07 20:17:24 +01001279 data_size_t len;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001280
Alexandre Julliard12717392008-01-07 20:17:24 +01001281 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001282
Alexandre Julliard12717392008-01-07 20:17:24 +01001283 len = info->tmplen;
Alexandre Julliardb0e091a2005-08-09 10:36:45 +00001284 if ((res = parse_strW( info->tmp, &len, buffer, ']' )) == -1)
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001285 {
1286 file_read_error( "Malformed key", info );
1287 return NULL;
1288 }
Alexandre Julliard3343c402008-12-06 17:29:01 +01001289 if (sscanf( buffer + res, " %u", &mod ) == 1)
1290 modif = (timeout_t)mod * TICKS_PER_SEC + ticks_1601_to_1970;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001291
Alexandre Julliardb0e091a2005-08-09 10:36:45 +00001292 p = info->tmp;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001293 while (prefix_len && *p) { if (*p++ == '\\') prefix_len--; }
1294
1295 if (!*p)
1296 {
1297 if (prefix_len > 1)
1298 {
1299 file_read_error( "Malformed key", info );
1300 return NULL;
1301 }
1302 /* empty key name, return base key */
1303 return (struct key *)grab_object( base );
1304 }
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001305 name.str = p;
1306 name.len = len - (p - info->tmp + 1) * sizeof(WCHAR);
Alexandre Julliard22de1f42010-03-01 14:13:46 +01001307 return create_key_recursive( base, &name, modif );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001308}
1309
Alexandre Julliard58791c92010-05-04 20:26:04 +02001310/* load a global option from the input file */
1311static int load_global_option( const char *buffer, struct file_load_info *info )
1312{
1313 const char *p;
1314
1315 if (!strncmp( buffer, "#arch=", 6 ))
1316 {
1317 enum prefix_type type;
1318 p = buffer + 6;
1319 if (!strcmp( p, "win32" )) type = PREFIX_32BIT;
1320 else if (!strcmp( p, "win64" )) type = PREFIX_64BIT;
1321 else
1322 {
1323 file_read_error( "Unknown architecture", info );
1324 set_error( STATUS_NOT_REGISTRY_FILE );
1325 return 0;
1326 }
1327 if (prefix_type == PREFIX_UNKNOWN) prefix_type = type;
1328 else if (type != prefix_type)
1329 {
1330 file_read_error( "Mismatched architecture", info );
1331 set_error( STATUS_NOT_REGISTRY_FILE );
1332 return 0;
1333 }
1334 }
1335 /* ignore unknown options */
1336 return 1;
1337}
1338
Alexandre Julliard2a378672010-02-16 12:26:15 +01001339/* load a key option from the input file */
1340static int load_key_option( struct key *key, const char *buffer, struct file_load_info *info )
1341{
1342 const char *p;
1343 data_size_t len;
1344
1345 if (!strncmp( buffer, "#class=", 7 ))
1346 {
1347 p = buffer + 7;
1348 if (*p++ != '"') return 0;
1349 if (!get_file_tmp_space( info, strlen(p) * sizeof(WCHAR) )) return 0;
1350 len = info->tmplen;
1351 if (parse_strW( info->tmp, &len, p, '\"' ) == -1) return 0;
1352 free( key->class );
1353 if (!(key->class = memdup( info->tmp, len ))) len = 0;
1354 key->classlen = len;
1355 }
Alexandre Julliardb5d42892010-02-16 12:26:44 +01001356 if (!strncmp( buffer, "#link", 5 )) key->flags |= KEY_SYMLINK;
Alexandre Julliard2a378672010-02-16 12:26:15 +01001357 /* ignore unknown options */
1358 return 1;
1359}
1360
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001361/* parse a comma-separated list of hex digits */
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001362static int parse_hex( unsigned char *dest, data_size_t *len, const char *buffer )
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001363{
1364 const char *p = buffer;
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001365 data_size_t count = 0;
Alexandre Julliardfb9955d2008-01-07 21:06:01 +01001366 char *end;
1367
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001368 while (isxdigit(*p))
1369 {
Alexandre Julliardfb9955d2008-01-07 21:06:01 +01001370 unsigned int val = strtoul( p, &end, 16 );
1371 if (end == p || val > 0xff) return -1;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001372 if (count++ >= *len) return -1; /* dest buffer overflow */
Alexandre Julliardfb9955d2008-01-07 21:06:01 +01001373 *dest++ = val;
1374 p = end;
1375 while (isspace(*p)) p++;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001376 if (*p == ',') p++;
Alexandre Julliardfb9955d2008-01-07 21:06:01 +01001377 while (isspace(*p)) p++;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001378 }
1379 *len = count;
1380 return p - buffer;
1381}
1382
1383/* parse a value name and create the corresponding value */
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001384static struct key_value *parse_value_name( struct key *key, const char *buffer, data_size_t *len,
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001385 struct file_load_info *info )
1386{
Alexandre Julliard88e42612002-06-20 23:18:56 +00001387 struct key_value *value;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001388 struct unicode_str name;
1389 int index;
Alexandre Julliard88e42612002-06-20 23:18:56 +00001390
Alexandre Julliard12717392008-01-07 20:17:24 +01001391 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return NULL;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001392 name.str = info->tmp;
Alexandre Julliard12717392008-01-07 20:17:24 +01001393 name.len = info->tmplen;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001394 if (buffer[0] == '@')
1395 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001396 name.len = 0;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001397 *len = 1;
1398 }
1399 else
1400 {
Alexandre Julliard12717392008-01-07 20:17:24 +01001401 int r = parse_strW( info->tmp, &name.len, buffer + 1, '\"' );
Mike McCormack14278b42006-04-07 15:06:15 +09001402 if (r == -1) goto error;
1403 *len = r + 1; /* for initial quote */
Alexandre Julliard12717392008-01-07 20:17:24 +01001404 name.len -= sizeof(WCHAR); /* terminating null */
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001405 }
Alexandre Julliard6c8d9172000-08-26 04:40:07 +00001406 while (isspace(buffer[*len])) (*len)++;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001407 if (buffer[*len] != '=') goto error;
1408 (*len)++;
Alexandre Julliard6c8d9172000-08-26 04:40:07 +00001409 while (isspace(buffer[*len])) (*len)++;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001410 if (!(value = find_value( key, &name, &index ))) value = insert_value( key, &name, index );
Alexandre Julliard88e42612002-06-20 23:18:56 +00001411 return value;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001412
1413 error:
1414 file_read_error( "Malformed value name", info );
1415 return NULL;
1416}
1417
1418/* load a value from the input file */
1419static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1420{
1421 DWORD dw;
1422 void *ptr, *newptr;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001423 int res, type, parse_type;
Alexandre Julliard0f273c12006-07-26 10:43:25 +02001424 data_size_t maxlen, len;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001425 struct key_value *value;
1426
1427 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1428 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1429 buffer += len + res;
1430
1431 switch(parse_type)
1432 {
1433 case REG_SZ:
Alexandre Julliard12717392008-01-07 20:17:24 +01001434 if (!get_file_tmp_space( info, strlen(buffer) * sizeof(WCHAR) )) return 0;
1435 len = info->tmplen;
Alexandre Julliardb0e091a2005-08-09 10:36:45 +00001436 if ((res = parse_strW( info->tmp, &len, buffer, '\"' )) == -1) goto error;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001437 ptr = info->tmp;
1438 break;
1439 case REG_DWORD:
1440 dw = strtoul( buffer, NULL, 16 );
1441 ptr = &dw;
1442 len = sizeof(dw);
1443 break;
1444 case REG_BINARY: /* hex digits */
1445 len = 0;
1446 for (;;)
1447 {
Alexandre Julliardfb9955d2008-01-07 21:06:01 +01001448 maxlen = 1 + strlen(buffer) / 2; /* at least 2 chars for one hex byte */
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001449 if (!get_file_tmp_space( info, len + maxlen )) return 0;
Alexandre Julliardb0e091a2005-08-09 10:36:45 +00001450 if ((res = parse_hex( (unsigned char *)info->tmp + len, &maxlen, buffer )) == -1) goto error;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001451 len += maxlen;
1452 buffer += res;
1453 while (isspace(*buffer)) buffer++;
1454 if (!*buffer) break;
1455 if (*buffer != '\\') goto error;
1456 if (read_next_line( info) != 1) goto error;
1457 buffer = info->buffer;
1458 while (isspace(*buffer)) buffer++;
1459 }
1460 ptr = info->tmp;
1461 break;
1462 default:
1463 assert(0);
1464 ptr = NULL; /* keep compiler quiet */
1465 break;
1466 }
1467
1468 if (!len) newptr = NULL;
1469 else if (!(newptr = memdup( ptr, len ))) return 0;
1470
Michael Stefaniuc5ceccec2006-10-09 23:34:36 +02001471 free( value->data );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001472 value->data = newptr;
1473 value->len = len;
1474 value->type = type;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001475 return 1;
1476
1477 error:
1478 file_read_error( "Malformed value", info );
Alexandre Julliardfb9955d2008-01-07 21:06:01 +01001479 free( value->data );
1480 value->data = NULL;
1481 value->len = 0;
1482 value->type = REG_NONE;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001483 return 0;
1484}
1485
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001486/* return the length (in path elements) of name that is part of the key name */
1487/* for instance if key is USER\foo\bar and name is foo\bar\baz, return 2 */
1488static int get_prefix_len( struct key *key, const char *name, struct file_load_info *info )
1489{
1490 WCHAR *p;
1491 int res;
Alexandre Julliard12717392008-01-07 20:17:24 +01001492 data_size_t len;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001493
Alexandre Julliard12717392008-01-07 20:17:24 +01001494 if (!get_file_tmp_space( info, strlen(name) * sizeof(WCHAR) )) return 0;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001495
Alexandre Julliard12717392008-01-07 20:17:24 +01001496 len = info->tmplen;
Alexandre Julliardb0e091a2005-08-09 10:36:45 +00001497 if ((res = parse_strW( info->tmp, &len, name, ']' )) == -1)
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001498 {
1499 file_read_error( "Malformed key", info );
Patrik Stridvall17d1e9e2000-05-23 23:38:32 +00001500 return 0;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001501 }
Alexandre Julliardb0e091a2005-08-09 10:36:45 +00001502 for (p = info->tmp; *p; p++) if (*p == '\\') break;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001503 len = (p - info->tmp) * sizeof(WCHAR);
Alexandre Julliard6c8d9172000-08-26 04:40:07 +00001504 for (res = 1; key != root_key; res++)
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001505 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001506 if (len == key->namelen && !memicmpW( info->tmp, key->name, len / sizeof(WCHAR) )) break;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001507 key = key->parent;
1508 }
Alexandre Julliard6c8d9172000-08-26 04:40:07 +00001509 if (key == root_key) res = 0; /* no matching name */
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001510 return res;
1511}
1512
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001513/* load all the keys from the input file */
Alexandre Julliard58447bc2004-04-30 18:35:13 +00001514/* prefix_len is the number of key name prefixes to skip, or -1 for autodetection */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001515static void load_keys( struct key *key, const char *filename, FILE *f, int prefix_len )
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001516{
1517 struct key *subkey = NULL;
1518 struct file_load_info info;
1519 char *p;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001520
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001521 info.filename = filename;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001522 info.file = f;
1523 info.len = 4;
1524 info.tmplen = 4;
1525 info.line = 0;
1526 if (!(info.buffer = mem_alloc( info.len ))) return;
1527 if (!(info.tmp = mem_alloc( info.tmplen )))
1528 {
1529 free( info.buffer );
1530 return;
1531 }
1532
1533 if ((read_next_line( &info ) != 1) ||
1534 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1535 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +00001536 set_error( STATUS_NOT_REGISTRY_FILE );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001537 goto done;
1538 }
1539
1540 while (read_next_line( &info ) == 1)
1541 {
Alexandre Julliard566a52a2001-03-05 19:34:17 +00001542 p = info.buffer;
1543 while (*p && isspace(*p)) p++;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001544 switch(*p)
1545 {
1546 case '[': /* new key */
1547 if (subkey) release_object( subkey );
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001548 if (prefix_len == -1) prefix_len = get_prefix_len( key, p + 1, &info );
Alexandre Julliard22de1f42010-03-01 14:13:46 +01001549 if (!(subkey = load_key( key, p + 1, prefix_len, &info )))
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001550 file_read_error( "Error creating key", &info );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001551 break;
1552 case '@': /* default value */
1553 case '\"': /* value */
1554 if (subkey) load_value( subkey, p, &info );
1555 else file_read_error( "Value without key", &info );
1556 break;
Alexandre Julliard2a378672010-02-16 12:26:15 +01001557 case '#': /* option */
1558 if (subkey) load_key_option( subkey, p, &info );
Alexandre Julliard58791c92010-05-04 20:26:04 +02001559 else if (!load_global_option( p, &info )) goto done;
Alexandre Julliard2a378672010-02-16 12:26:15 +01001560 break;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001561 case ';': /* comment */
1562 case 0: /* empty line */
1563 break;
1564 default:
1565 file_read_error( "Unrecognized input", &info );
1566 break;
1567 }
1568 }
1569
1570 done:
1571 if (subkey) release_object( subkey );
1572 free( info.buffer );
1573 free( info.tmp );
1574}
1575
1576/* load a part of the registry from a file */
Alexandre Julliard51885742002-05-30 20:12:58 +00001577static void load_registry( struct key *key, obj_handle_t handle )
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001578{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001579 struct file *file;
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001580 int fd;
1581
Alexandre Julliarda510a7e2005-12-12 16:46:17 +01001582 if (!(file = get_file_obj( current->process, handle, FILE_READ_DATA ))) return;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001583 fd = dup( get_file_unix_fd( file ) );
1584 release_object( file );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001585 if (fd != -1)
1586 {
1587 FILE *f = fdopen( fd, "r" );
1588 if (f)
1589 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001590 load_keys( key, NULL, f, -1 );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001591 fclose( f );
1592 }
1593 else file_set_error();
1594 }
1595}
1596
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001597/* load one of the initial registry files */
Alexandre Julliard58791c92010-05-04 20:26:04 +02001598static int load_init_registry_from_file( const char *filename, struct key *key )
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001599{
1600 FILE *f;
1601
Alexandre Julliard5b23efc2004-05-14 00:45:11 +00001602 if ((f = fopen( filename, "r" )))
1603 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001604 load_keys( key, filename, f, 0 );
Alexandre Julliard5b23efc2004-05-14 00:45:11 +00001605 fclose( f );
1606 if (get_error() == STATUS_NOT_REGISTRY_FILE)
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001607 {
1608 fprintf( stderr, "%s is not a valid registry file\n", filename );
Alexandre Julliard58791c92010-05-04 20:26:04 +02001609 return 1;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001610 }
Alexandre Julliard5b23efc2004-05-14 00:45:11 +00001611 }
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001612
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001613 assert( save_branch_count < MAX_SAVE_BRANCH_INFO );
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001614
Alexandre Julliard161160f2008-04-17 12:41:34 +02001615 save_branch_info[save_branch_count].path = filename;
1616 save_branch_info[save_branch_count++].key = (struct key *)grab_object( key );
1617 make_object_static( &key->obj );
Alexandre Julliard58791c92010-05-04 20:26:04 +02001618 return (f != NULL);
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001619}
1620
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001621static WCHAR *format_user_registry_path( const SID *sid, struct unicode_str *path )
Robert Shearman91eaea52005-07-18 13:22:55 +00001622{
1623 static const WCHAR prefixW[] = {'U','s','e','r','\\','S',0};
1624 static const WCHAR formatW[] = {'-','%','u',0};
1625 WCHAR buffer[7 + 10 + 10 + 10 * SID_MAX_SUB_AUTHORITIES];
1626 WCHAR *p = buffer;
1627 unsigned int i;
1628
1629 strcpyW( p, prefixW );
1630 p += strlenW( prefixW );
1631 p += sprintfW( p, formatW, sid->Revision );
1632 p += sprintfW( p, formatW, MAKELONG( MAKEWORD( sid->IdentifierAuthority.Value[5],
1633 sid->IdentifierAuthority.Value[4] ),
1634 MAKEWORD( sid->IdentifierAuthority.Value[3],
1635 sid->IdentifierAuthority.Value[2] )));
1636 for (i = 0; i < sid->SubAuthorityCount; i++)
1637 p += sprintfW( p, formatW, sid->SubAuthority[i] );
1638
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001639 path->len = (p - buffer) * sizeof(WCHAR);
1640 path->str = p = memdup( buffer, path->len );
1641 return p;
Robert Shearman91eaea52005-07-18 13:22:55 +00001642}
1643
Alexandre Julliardac69fcb2010-05-04 18:23:29 +02001644/* get the cpu architectures that can be supported in the current prefix */
1645unsigned int get_prefix_cpu_mask(void)
1646{
1647 /* Allowed server/client/prefix combinations:
1648 *
1649 * prefix
1650 * 32 64
1651 * server +------+------+ client
1652 * | ok | fail | 32
1653 * 32 +------+------+---
1654 * | fail | fail | 64
1655 * ---+------+------+---
1656 * | ok | ok | 32
1657 * 64 +------+------+---
1658 * | fail | ok | 64
1659 * ---+------+------+---
1660 */
1661 switch (prefix_type)
1662 {
1663 case PREFIX_64BIT:
1664 /* 64-bit prefix requires 64-bit server */
1665 return sizeof(void *) > sizeof(int) ? ~0 : 0;
1666 case PREFIX_32BIT:
1667 default:
1668 return ~CPU_64BIT_MASK; /* only 32-bit cpus supported on 32-bit prefix */
1669 }
1670}
1671
Alexandre Julliard27f0f802005-04-20 12:57:53 +00001672/* registry initialisation */
1673void init_registry(void)
1674{
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001675 static const WCHAR HKLM[] = { 'M','a','c','h','i','n','e' };
1676 static const WCHAR HKU_default[] = { 'U','s','e','r','\\','.','D','e','f','a','u','l','t' };
Alexandre Julliard178cd202010-03-04 20:47:35 +01001677 static const WCHAR classes[] = {'S','o','f','t','w','a','r','e','\\',
1678 'C','l','a','s','s','e','s','\\',
1679 'W','o','w','6','4','3','2','N','o','d','e'};
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001680 static const struct unicode_str root_name = { NULL, 0 };
1681 static const struct unicode_str HKLM_name = { HKLM, sizeof(HKLM) };
1682 static const struct unicode_str HKU_name = { HKU_default, sizeof(HKU_default) };
Alexandre Julliard178cd202010-03-04 20:47:35 +01001683 static const struct unicode_str classes_name = { classes, sizeof(classes) };
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001684
Robert Shearman91eaea52005-07-18 13:22:55 +00001685 WCHAR *current_user_path;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001686 struct unicode_str current_user_str;
Alexandre Julliard178cd202010-03-04 20:47:35 +01001687 struct key *key, *hklm, *hkcu;
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001688
Alexandre Julliard161160f2008-04-17 12:41:34 +02001689 /* switch to the config dir */
1690
1691 if (fchdir( config_dir_fd ) == -1) fatal_perror( "chdir to config dir" );
1692
Alexandre Julliard27f0f802005-04-20 12:57:53 +00001693 /* create the root key */
Alexandre Julliard3343c402008-12-06 17:29:01 +01001694 root_key = alloc_key( &root_name, current_time );
Alexandre Julliard27f0f802005-04-20 12:57:53 +00001695 assert( root_key );
Alexandre Julliardb00fb172006-03-22 20:32:04 +01001696 make_object_static( &root_key->obj );
Alexandre Julliard27f0f802005-04-20 12:57:53 +00001697
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001698 /* load system.reg into Registry\Machine */
1699
Alexandre Julliard178cd202010-03-04 20:47:35 +01001700 if (!(hklm = create_key_recursive( root_key, &HKLM_name, current_time )))
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001701 fatal_error( "could not create Machine registry key\n" );
1702
Alexandre Julliard58791c92010-05-04 20:26:04 +02001703 if (!load_init_registry_from_file( "system.reg", hklm ))
1704 prefix_type = sizeof(void *) > sizeof(int) ? PREFIX_64BIT : PREFIX_32BIT;
1705 else if (prefix_type == PREFIX_UNKNOWN)
1706 prefix_type = PREFIX_32BIT;
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001707
1708 /* load userdef.reg into Registry\User\.Default */
1709
Alexandre Julliard22de1f42010-03-01 14:13:46 +01001710 if (!(key = create_key_recursive( root_key, &HKU_name, current_time )))
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001711 fatal_error( "could not create User\\.Default registry key\n" );
1712
Alexandre Julliard161160f2008-04-17 12:41:34 +02001713 load_init_registry_from_file( "userdef.reg", key );
Alexandre Julliardc07ce052004-05-07 04:13:21 +00001714 release_object( key );
1715
Alexandre Julliard6aa0cc52005-07-11 20:44:59 +00001716 /* load user.reg into HKEY_CURRENT_USER */
1717
Robert Shearman91eaea52005-07-18 13:22:55 +00001718 /* FIXME: match default user in token.c. should get from process token instead */
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001719 current_user_path = format_user_registry_path( security_interactive_sid, &current_user_str );
Robert Shearman91eaea52005-07-18 13:22:55 +00001720 if (!current_user_path ||
Alexandre Julliard178cd202010-03-04 20:47:35 +01001721 !(hkcu = create_key_recursive( root_key, &current_user_str, current_time )))
Alexandre Julliard6aa0cc52005-07-11 20:44:59 +00001722 fatal_error( "could not create HKEY_CURRENT_USER registry key\n" );
Robert Shearman91eaea52005-07-18 13:22:55 +00001723 free( current_user_path );
Alexandre Julliard178cd202010-03-04 20:47:35 +01001724 load_init_registry_from_file( "user.reg", hkcu );
1725
1726 /* set the shared flag on Software\Classes\Wow6432Node */
Alexandre Julliard58791c92010-05-04 20:26:04 +02001727 if (prefix_type == PREFIX_64BIT)
Alexandre Julliard178cd202010-03-04 20:47:35 +01001728 {
1729 if ((key = create_key_recursive( hklm, &classes_name, current_time )))
1730 {
1731 key->flags |= KEY_WOWSHARE;
1732 release_object( key );
1733 }
1734 /* FIXME: handle HKCU too */
1735 }
1736
1737 release_object( hklm );
1738 release_object( hkcu );
Alexandre Julliard6aa0cc52005-07-11 20:44:59 +00001739
Alexandre Julliard6aa0cc52005-07-11 20:44:59 +00001740 /* start the periodic save timer */
1741 set_periodic_save_timer();
Alexandre Julliard161160f2008-04-17 12:41:34 +02001742
1743 /* go back to the server dir */
1744 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
Alexandre Julliard6c8d9172000-08-26 04:40:07 +00001745}
1746
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001747/* save a registry branch to a file */
1748static void save_all_subkeys( struct key *key, FILE *f )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001749{
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001750 fprintf( f, "WINE REGISTRY Version 2\n" );
1751 fprintf( f, ";; All keys relative to " );
1752 dump_path( key, NULL, f );
1753 fprintf( f, "\n" );
Alexandre Julliard58791c92010-05-04 20:26:04 +02001754 switch (prefix_type)
1755 {
1756 case PREFIX_32BIT:
1757 fprintf( f, "\n#arch=win32\n" );
1758 break;
1759 case PREFIX_64BIT:
1760 fprintf( f, "\n#arch=win64\n" );
1761 break;
1762 default:
1763 break;
1764 }
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001765 save_subkeys( key, key, f );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001766}
1767
1768/* save a registry branch to a file handle */
Alexandre Julliard51885742002-05-30 20:12:58 +00001769static void save_registry( struct key *key, obj_handle_t handle )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001770{
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001771 struct file *file;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001772 int fd;
1773
Alexandre Julliarda510a7e2005-12-12 16:46:17 +01001774 if (!(file = get_file_obj( current->process, handle, FILE_WRITE_DATA ))) return;
Alexandre Julliardcf27a7f2003-02-14 20:27:09 +00001775 fd = dup( get_file_unix_fd( file ) );
1776 release_object( file );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001777 if (fd != -1)
1778 {
1779 FILE *f = fdopen( fd, "w" );
1780 if (f)
1781 {
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001782 save_all_subkeys( key, f );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001783 if (fclose( f )) file_set_error();
1784 }
1785 else
1786 {
1787 file_set_error();
1788 close( fd );
1789 }
1790 }
1791}
1792
Alexandre Julliardc9709042000-04-16 17:21:13 +00001793/* save a registry branch to a file */
1794static int save_branch( struct key *key, const char *path )
1795{
Bill Medland309566d2002-12-18 05:03:51 +00001796 struct stat st;
Alexandre Julliard161160f2008-04-17 12:41:34 +02001797 char *p, *tmp = NULL;
1798 int fd, count = 0, ret = 0;
Alexandre Julliardc9709042000-04-16 17:21:13 +00001799 FILE *f;
1800
Alexandre Julliard88e42612002-06-20 23:18:56 +00001801 if (!(key->flags & KEY_DIRTY))
1802 {
1803 if (debug_level > 1) dump_operation( key, NULL, "Not saving clean" );
1804 return 1;
1805 }
1806
Alexandre Julliardc9709042000-04-16 17:21:13 +00001807 /* test the file type */
1808
1809 if ((fd = open( path, O_WRONLY )) != -1)
1810 {
Bill Medland309566d2002-12-18 05:03:51 +00001811 /* if file is not a regular file or has multiple links or is accessed
1812 * via symbolic links, write directly into it; otherwise use a temp file */
Alexandre Julliard161160f2008-04-17 12:41:34 +02001813 if (!lstat( path, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
Alexandre Julliardc9709042000-04-16 17:21:13 +00001814 {
1815 ftruncate( fd, 0 );
1816 goto save;
1817 }
1818 close( fd );
1819 }
1820
1821 /* create a temp file in the same directory */
1822
1823 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1824 strcpy( tmp, path );
1825 if ((p = strrchr( tmp, '/' ))) p++;
1826 else p = tmp;
1827 for (;;)
1828 {
Patrik Stridvalle29dbc52000-04-24 18:04:24 +00001829 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
Alexandre Julliardc9709042000-04-16 17:21:13 +00001830 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1831 if (errno != EEXIST) goto done;
1832 close( fd );
1833 }
1834
1835 /* now save to it */
1836
1837 save:
1838 if (!(f = fdopen( fd, "w" )))
1839 {
1840 if (tmp) unlink( tmp );
1841 close( fd );
1842 goto done;
1843 }
1844
1845 if (debug_level > 1)
1846 {
1847 fprintf( stderr, "%s: ", path );
1848 dump_operation( key, NULL, "saving" );
1849 }
1850
Alexandre Julliarda01004d2000-05-14 22:57:57 +00001851 save_all_subkeys( key, f );
Alexandre Julliardc9709042000-04-16 17:21:13 +00001852 ret = !fclose(f);
1853
1854 if (tmp)
1855 {
1856 /* if successfully written, rename to final name */
1857 if (ret) ret = !rename( tmp, path );
1858 if (!ret) unlink( tmp );
Alexandre Julliardc9709042000-04-16 17:21:13 +00001859 }
1860
1861done:
Peter Berg Larsen514f4322005-03-10 11:18:31 +00001862 free( tmp );
Alexandre Julliard88e42612002-06-20 23:18:56 +00001863 if (ret) make_clean( key );
Alexandre Julliardc9709042000-04-16 17:21:13 +00001864 return ret;
1865}
1866
1867/* periodic saving of the registry */
1868static void periodic_save( void *arg )
1869{
1870 int i;
Alexandre Julliarddcab7062005-03-14 11:00:43 +00001871
Alexandre Julliard161160f2008-04-17 12:41:34 +02001872 if (fchdir( config_dir_fd ) == -1) return;
Alexandre Julliarddcab7062005-03-14 11:00:43 +00001873 save_timeout_user = NULL;
Alexandre Julliardc9709042000-04-16 17:21:13 +00001874 for (i = 0; i < save_branch_count; i++)
1875 save_branch( save_branch_info[i].key, save_branch_info[i].path );
Alexandre Julliard161160f2008-04-17 12:41:34 +02001876 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
Alexandre Julliarddcab7062005-03-14 11:00:43 +00001877 set_periodic_save_timer();
1878}
1879
1880/* start the periodic save timer */
1881static void set_periodic_save_timer(void)
1882{
Alexandre Julliarddcab7062005-03-14 11:00:43 +00001883 if (save_timeout_user) remove_timeout_user( save_timeout_user );
Alexandre Julliardaaf477f2007-04-17 20:08:59 +02001884 save_timeout_user = add_timeout_user( save_period, periodic_save, NULL );
Alexandre Julliardc9709042000-04-16 17:21:13 +00001885}
1886
Alexandre Julliard88e42612002-06-20 23:18:56 +00001887/* save the modified registry branches to disk */
1888void flush_registry(void)
Alexandre Julliardc9709042000-04-16 17:21:13 +00001889{
1890 int i;
1891
Alexandre Julliard161160f2008-04-17 12:41:34 +02001892 if (fchdir( config_dir_fd ) == -1) return;
Alexandre Julliardc9709042000-04-16 17:21:13 +00001893 for (i = 0; i < save_branch_count; i++)
1894 {
1895 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1896 {
1897 fprintf( stderr, "wineserver: could not save registry branch to %s",
1898 save_branch_info[i].path );
1899 perror( " " );
1900 }
Alexandre Julliardc9709042000-04-16 17:21:13 +00001901 }
Alexandre Julliard161160f2008-04-17 12:41:34 +02001902 if (fchdir( server_dir_fd ) == -1) fatal_perror( "chdir to server dir" );
Alexandre Julliard88e42612002-06-20 23:18:56 +00001903}
1904
Alexandre Julliardbbc03d52010-05-04 20:26:53 +02001905/* determine if the thread is wow64 (32-bit client running on 64-bit prefix) */
1906static int is_wow64_thread( struct thread *thread )
1907{
1908 return (prefix_type == PREFIX_64BIT && !(CPU_FLAG(thread->process->cpu) & CPU_64BIT_MASK));
1909}
1910
Alexandre Julliardc9709042000-04-16 17:21:13 +00001911
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001912/* create a registry key */
1913DECL_HANDLER(create_key)
1914{
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00001915 struct key *key = NULL, *parent;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001916 struct unicode_str name, class;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001917 unsigned int access = req->access;
1918
Alexandre Julliardac08b032010-03-02 12:03:17 +01001919 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1920
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001921 reply->hkey = 0;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001922
1923 if (req->namelen > get_req_data_size())
1924 {
1925 set_error( STATUS_INVALID_PARAMETER );
1926 return;
1927 }
1928 class.str = (const WCHAR *)get_req_data() + req->namelen / sizeof(WCHAR);
1929 class.len = ((get_req_data_size() - req->namelen) / sizeof(WCHAR)) * sizeof(WCHAR);
1930 get_req_path( &name, !req->parent );
1931 if (name.str > class.str)
1932 {
1933 set_error( STATUS_INVALID_PARAMETER );
1934 return;
1935 }
1936 name.len = (class.str - name.str) * sizeof(WCHAR);
1937
Robert Shearman8cb3f922005-06-16 20:34:34 +00001938 /* NOTE: no access rights are required from the parent handle to create a key */
Alexandre Julliarddd77d1a2006-07-10 11:53:23 +02001939 if ((parent = get_parent_hkey_obj( req->parent )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001940 {
Alexandre Julliardac08b032010-03-02 12:03:17 +01001941 if ((key = create_key( parent, &name, &class, req->options, access,
1942 req->attributes, &reply->created )))
Alexandre Julliard454355e2000-10-02 03:46:58 +00001943 {
Alexandre Julliard03b040c2005-12-09 14:52:04 +01001944 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
Alexandre Julliard454355e2000-10-02 03:46:58 +00001945 release_object( key );
1946 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001947 release_object( parent );
1948 }
1949}
1950
1951/* open a registry key */
1952DECL_HANDLER(open_key)
1953{
1954 struct key *key, *parent;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001955 struct unicode_str name;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001956 unsigned int access = req->access;
1957
Alexandre Julliardac08b032010-03-02 12:03:17 +01001958 if (!is_wow64_thread( current )) access = (access & ~KEY_WOW64_32KEY) | KEY_WOW64_64KEY;
1959
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00001960 reply->hkey = 0;
Robert Shearmanb3957e32005-05-05 16:57:55 +00001961 /* NOTE: no access rights are required to open the parent key, only the child key */
Alexandre Julliarddd77d1a2006-07-10 11:53:23 +02001962 if ((parent = get_parent_hkey_obj( req->parent )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001963 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00001964 get_req_path( &name, !req->parent );
Alexandre Julliardac08b032010-03-02 12:03:17 +01001965 if ((key = open_key( parent, &name, access, req->attributes )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001966 {
Alexandre Julliard03b040c2005-12-09 14:52:04 +01001967 reply->hkey = alloc_handle( current->process, key, access, req->attributes );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001968 release_object( key );
1969 }
1970 release_object( parent );
1971 }
1972}
1973
1974/* delete a registry key */
1975DECL_HANDLER(delete_key)
1976{
1977 struct key *key;
1978
Robert Shearmanb3957e32005-05-05 16:57:55 +00001979 if ((key = get_hkey_obj( req->hkey, DELETE )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001980 {
Mike McCormack5ac945c2003-08-19 03:08:17 +00001981 delete_key( key, 0);
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001982 release_object( key );
1983 }
1984}
1985
Mike Hearn43cb03b2004-01-03 00:38:30 +00001986/* flush a registry key */
1987DECL_HANDLER(flush_key)
1988{
1989 struct key *key = get_hkey_obj( req->hkey, 0 );
1990 if (key)
1991 {
1992 /* we don't need to do anything here with the current implementation */
1993 release_object( key );
1994 }
1995}
1996
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001997/* enumerate registry subkeys */
1998DECL_HANDLER(enum_key)
1999{
2000 struct key *key;
2001
Alexandre Julliard454355e2000-10-02 03:46:58 +00002002 if ((key = get_hkey_obj( req->hkey,
2003 req->index == -1 ? KEY_QUERY_VALUE : KEY_ENUMERATE_SUB_KEYS )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002004 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00002005 enum_key( key, req->index, req->info_class, reply );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002006 release_object( key );
2007 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002008}
2009
2010/* set a value of a registry key */
2011DECL_HANDLER(set_key_value)
2012{
2013 struct key *key;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00002014 struct unicode_str name;
Alexandre Julliarda01004d2000-05-14 22:57:57 +00002015
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00002016 if (req->namelen > get_req_data_size())
2017 {
2018 set_error( STATUS_INVALID_PARAMETER );
2019 return;
2020 }
2021 name.str = get_req_data();
2022 name.len = (req->namelen / sizeof(WCHAR)) * sizeof(WCHAR);
2023
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002024 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2025 {
Alexandre Julliard0f273c12006-07-26 10:43:25 +02002026 data_size_t datalen = get_req_data_size() - req->namelen;
Eric Pouech0a258962004-11-30 21:38:57 +00002027 const char *data = (const char *)get_req_data() + req->namelen;
Alexandre Julliardbcf393a2000-10-01 01:44:50 +00002028
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00002029 set_value( key, &name, req->type, data, datalen );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002030 release_object( key );
2031 }
2032}
2033
2034/* retrieve the value of a registry key */
2035DECL_HANDLER(get_key_value)
2036{
2037 struct key *key;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00002038 struct unicode_str name;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002039
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00002040 reply->total = 0;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002041 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2042 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00002043 get_req_unicode_str( &name );
2044 get_value( key, &name, &reply->type, &reply->total );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002045 release_object( key );
2046 }
2047}
2048
2049/* enumerate the value of a registry key */
2050DECL_HANDLER(enum_key_value)
2051{
2052 struct key *key;
2053
2054 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
2055 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +00002056 enum_value( key, req->index, req->info_class, reply );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002057 release_object( key );
2058 }
2059}
2060
2061/* delete a value of a registry key */
2062DECL_HANDLER(delete_key_value)
2063{
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002064 struct key *key;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00002065 struct unicode_str name;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002066
2067 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
2068 {
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00002069 get_req_unicode_str( &name );
2070 delete_value( key, &name );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002071 release_object( key );
2072 }
2073}
2074
2075/* load a registry branch from a file */
2076DECL_HANDLER(load_registry)
2077{
James Hawkins580ded62005-03-29 11:38:58 +00002078 struct key *key, *parent;
Robert Shearmanb3957e32005-05-05 16:57:55 +00002079 struct token *token = thread_get_impersonation_token( current );
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00002080 struct unicode_str name;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002081
Robert Shearmanb3957e32005-05-05 16:57:55 +00002082 const LUID_AND_ATTRIBUTES privs[] =
2083 {
2084 { SeBackupPrivilege, 0 },
2085 { SeRestorePrivilege, 0 },
2086 };
2087
2088 if (!token || !token_check_privileges( token, TRUE, privs,
2089 sizeof(privs)/sizeof(privs[0]), NULL ))
2090 {
2091 set_error( STATUS_PRIVILEGE_NOT_HELD );
2092 return;
2093 }
2094
Alexandre Julliarddd77d1a2006-07-10 11:53:23 +02002095 if ((parent = get_parent_hkey_obj( req->hkey )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002096 {
James Hawkins580ded62005-03-29 11:38:58 +00002097 int dummy;
Alexandre Julliard7f9e2812005-11-22 12:05:36 +00002098 get_req_path( &name, !req->hkey );
Alexandre Julliardac08b032010-03-02 12:03:17 +01002099 if ((key = create_key( parent, &name, NULL, 0, KEY_WOW64_64KEY, 0, &dummy )))
James Hawkins580ded62005-03-29 11:38:58 +00002100 {
2101 load_registry( key, req->file );
2102 release_object( key );
2103 }
2104 release_object( parent );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002105 }
2106}
2107
Mike McCormack5ac945c2003-08-19 03:08:17 +00002108DECL_HANDLER(unload_registry)
2109{
2110 struct key *key;
Robert Shearmanb3957e32005-05-05 16:57:55 +00002111 struct token *token = thread_get_impersonation_token( current );
2112
2113 const LUID_AND_ATTRIBUTES privs[] =
2114 {
2115 { SeBackupPrivilege, 0 },
2116 { SeRestorePrivilege, 0 },
2117 };
2118
2119 if (!token || !token_check_privileges( token, TRUE, privs,
2120 sizeof(privs)/sizeof(privs[0]), NULL ))
2121 {
2122 set_error( STATUS_PRIVILEGE_NOT_HELD );
2123 return;
2124 }
Mike McCormack5ac945c2003-08-19 03:08:17 +00002125
2126 if ((key = get_hkey_obj( req->hkey, 0 )))
2127 {
2128 delete_key( key, 1 ); /* FIXME */
2129 release_object( key );
2130 }
2131}
2132
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002133/* save a registry branch to a file */
2134DECL_HANDLER(save_registry)
2135{
2136 struct key *key;
2137
Robert Shearmanb3957e32005-05-05 16:57:55 +00002138 if (!thread_single_check_privilege( current, &SeBackupPrivilege ))
2139 {
2140 set_error( STATUS_PRIVILEGE_NOT_HELD );
2141 return;
2142 }
2143
2144 if ((key = get_hkey_obj( req->hkey, 0 )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00002145 {
2146 save_registry( key, req->file );
2147 release_object( key );
2148 }
2149}
2150
Mike McCormack11f4b442002-11-25 02:47:32 +00002151/* add a registry key change notification */
2152DECL_HANDLER(set_registry_notification)
2153{
2154 struct key *key;
2155 struct event *event;
2156 struct notify *notify;
2157
2158 key = get_hkey_obj( req->hkey, KEY_NOTIFY );
Robert Shearman37957092005-06-10 19:54:46 +00002159 if (key)
Mike McCormack11f4b442002-11-25 02:47:32 +00002160 {
2161 event = get_event_obj( current->process, req->event, SYNCHRONIZE );
Robert Shearman37957092005-06-10 19:54:46 +00002162 if (event)
Mike McCormack11f4b442002-11-25 02:47:32 +00002163 {
Alexandre Julliard9b3b7ca2005-06-09 20:36:08 +00002164 notify = find_notify( key, current->process, req->hkey );
Robert Shearman37957092005-06-10 19:54:46 +00002165 if (notify)
Mike McCormack11f4b442002-11-25 02:47:32 +00002166 {
Mike McCormack5fb6e0e2006-05-14 17:11:54 +09002167 if (notify->event)
2168 release_object( notify->event );
Mike McCormack11f4b442002-11-25 02:47:32 +00002169 grab_object( event );
2170 notify->event = event;
2171 }
2172 else
2173 {
Alexandre Julliard68abbc82005-02-24 19:43:53 +00002174 notify = mem_alloc( sizeof(*notify) );
Robert Shearman37957092005-06-10 19:54:46 +00002175 if (notify)
Mike McCormack11f4b442002-11-25 02:47:32 +00002176 {
2177 grab_object( event );
2178 notify->event = event;
2179 notify->subtree = req->subtree;
2180 notify->filter = req->filter;
2181 notify->hkey = req->hkey;
Alexandre Julliard9b3b7ca2005-06-09 20:36:08 +00002182 notify->process = current->process;
Alexandre Julliard68abbc82005-02-24 19:43:53 +00002183 list_add_head( &key->notify_list, &notify->entry );
Mike McCormack11f4b442002-11-25 02:47:32 +00002184 }
Mike McCormack11f4b442002-11-25 02:47:32 +00002185 }
2186 release_object( event );
2187 }
2188 release_object( key );
2189 }
2190}