blob: 6d6b034943d9ae7d9f18b74ae527d741e1f916f6 [file] [log] [blame]
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001/*
2 * Server-side registry management
3 *
4 * Copyright (C) 1999 Alexandre Julliard
5 */
6
7/* To do:
8 * - behavior with deleted keys
9 * - values larger than request buffer
10 * - symbolic links
11 */
12
13#include <assert.h>
Alexandre Julliardc9709042000-04-16 17:21:13 +000014#include <errno.h>
15#include <fcntl.h>
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000016#include <limits.h>
17#include <stdio.h>
Alexandre Julliardc9709042000-04-16 17:21:13 +000018#include <string.h>
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000019#include <stdlib.h>
Alexandre Julliardc9709042000-04-16 17:21:13 +000020#include <sys/stat.h>
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000021#include <unistd.h>
Juergen Schmied788d8d52000-01-27 05:37:54 +000022#include <pwd.h>
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000023#include "object.h"
24#include "handle.h"
25#include "request.h"
26#include "unicode.h"
27
28#include "winbase.h"
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000029#include "winreg.h"
Andreas Mohr32a5b632000-03-26 14:41:10 +000030#include "winnt.h" /* registry definitions */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +000031
32
33/* a registry key */
34struct key
35{
36 struct object obj; /* object header */
37 WCHAR *name; /* key name */
38 WCHAR *class; /* key class */
39 struct key *parent; /* parent key */
40 int last_subkey; /* last in use subkey */
41 int nb_subkeys; /* count of allocated subkeys */
42 struct key **subkeys; /* subkeys array */
43 int last_value; /* last in use value */
44 int nb_values; /* count of allocated values in array */
45 struct key_value *values; /* values array */
46 short flags; /* flags */
47 short level; /* saving level */
48 time_t modif; /* last modification time */
49};
50
51/* key flags */
52#define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
53#define KEY_DELETED 0x0002 /* key has been deleted */
54#define KEY_ROOT 0x0004 /* key is a root key */
55
56/* a key value */
57struct key_value
58{
59 WCHAR *name; /* value name */
60 int type; /* value type */
61 size_t len; /* value data length in bytes */
62 void *data; /* pointer to value data */
63};
64
65#define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
66#define MIN_VALUES 8 /* min. number of allocated values per key */
67
68
69/* the root keys */
70#define HKEY_ROOT_FIRST HKEY_CLASSES_ROOT
71#define HKEY_ROOT_LAST HKEY_DYN_DATA
72#define NB_ROOT_KEYS (HKEY_ROOT_LAST - HKEY_ROOT_FIRST + 1)
73#define IS_ROOT_HKEY(h) (((h) >= HKEY_ROOT_FIRST) && ((h) <= HKEY_ROOT_LAST))
74static struct key *root_keys[NB_ROOT_KEYS];
75
76static const char * const root_key_names[NB_ROOT_KEYS] =
77{
78 "HKEY_CLASSES_ROOT",
79 "HKEY_CURRENT_USER",
80 "HKEY_LOCAL_MACHINE",
81 "HKEY_USERS",
82 "HKEY_PERFORMANCE_DATA",
83 "HKEY_CURRENT_CONFIG",
84 "HKEY_DYN_DATA"
85};
86
87
88/* keys saving level */
89/* current_level is the level that is put into all newly created or modified keys */
90/* saving_level is the minimum level that a key needs in order to get saved */
91static int current_level;
92static int saving_level;
93
94static int saving_version = 1; /* file format version */
95
Alexandre Julliardc9709042000-04-16 17:21:13 +000096static struct timeval next_save_time; /* absolute time of next periodic save */
97static int save_period; /* delay between periodic saves (ms) */
98static struct timeout_user *save_timeout_user; /* saving timer */
99
100/* information about where to save a registry branch */
101struct save_branch_info
102{
103 struct key *key;
104 char *path;
105};
106
107#define MAX_SAVE_BRANCH_INFO 8
108static int save_branch_count;
109static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
110
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000111
Alexandre Julliard53f3a831999-11-24 04:19:43 +0000112/* information about a file being loaded */
113struct file_load_info
114{
115 FILE *file; /* input file */
116 char *buffer; /* line buffer */
117 int len; /* buffer length */
118 int line; /* current input line */
119 char *tmp; /* temp buffer to use while parsing input */
120 int tmplen; /* length of temp buffer */
121};
122
123
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000124static void key_dump( struct object *obj, int verbose );
125static void key_destroy( struct object *obj );
126
127static const struct object_ops key_ops =
128{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000129 sizeof(struct key), /* size */
130 key_dump, /* dump */
131 no_add_queue, /* add_queue */
132 NULL, /* remove_queue */
133 NULL, /* signaled */
134 NULL, /* satisfied */
135 NULL, /* get_poll_events */
136 NULL, /* poll_event */
137 no_read_fd, /* get_read_fd */
138 no_write_fd, /* get_write_fd */
139 no_flush, /* flush */
140 no_get_file_info, /* get_file_info */
141 key_destroy /* destroy */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000142};
143
144
145/*
146 * The registry text file format v2 used by this code is similar to the one
147 * used by REGEDIT import/export functionality, with the following differences:
148 * - strings and key names can contain \x escapes for Unicode
149 * - key names use escapes too in order to support Unicode
150 * - the modification time optionally follows the key name
151 * - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
152 */
153
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000154static inline char to_hex( char ch )
155{
156 if (isdigit(ch)) return ch - '0';
157 return tolower(ch) - 'a' + 10;
158}
159
160/* dump the full path of a key */
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000161static void dump_path( struct key *key, struct key *base, FILE *f )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000162{
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000163 if (key->parent && key != base)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000164 {
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000165 dump_path( key->parent, base, f );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000166 fprintf( f, "\\\\" );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000167 }
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000168
169 if (key->name) dump_strW( key->name, strlenW(key->name), f, "[]" );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000170 else /* root key */
171 {
172 int i;
173 for (i = 0; i < NB_ROOT_KEYS; i++)
174 if (root_keys[i] == key) fprintf( f, "%s", root_key_names[i] );
175 }
176}
177
178/* dump a value to a text file */
179static void dump_value( struct key_value *value, FILE *f )
180{
181 int i, count;
182
183 if (value->name[0])
184 {
185 fputc( '\"', f );
186 count = 1 + dump_strW( value->name, strlenW(value->name), f, "\"\"" );
187 count += fprintf( f, "\"=" );
188 }
189 else count = fprintf( f, "@=" );
190
191 switch(value->type)
192 {
193 case REG_SZ:
194 case REG_EXPAND_SZ:
195 case REG_MULTI_SZ:
196 if (value->type != REG_SZ) fprintf( f, "str(%d):", value->type );
197 fputc( '\"', f );
198 if (value->data) dump_strW( (WCHAR *)value->data, value->len / sizeof(WCHAR), f, "\"\"" );
199 fputc( '\"', f );
200 break;
201 case REG_DWORD:
202 if (value->len == sizeof(DWORD))
203 {
204 DWORD dw;
205 memcpy( &dw, value->data, sizeof(DWORD) );
206 fprintf( f, "dword:%08lx", dw );
207 break;
208 }
209 /* else fall through */
210 default:
211 if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
212 else count += fprintf( f, "hex(%x):", value->type );
213 for (i = 0; i < value->len; i++)
214 {
215 count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
216 if (i < value->len-1)
217 {
218 fputc( ',', f );
219 if (++count > 76)
220 {
221 fprintf( f, "\\\n " );
222 count = 2;
223 }
224 }
225 }
226 break;
227 }
228 fputc( '\n', f );
229}
230
231/* save a registry and all its subkeys to a text file */
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000232static void save_subkeys( struct key *key, struct key *base, FILE *f )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000233{
234 int i;
235
236 if (key->flags & KEY_VOLATILE) return;
237 /* save key if it has the proper level, and has either some values or no subkeys */
238 /* keys with no values but subkeys are saved implicitly by saving the subkeys */
239 if ((key->level >= saving_level) && ((key->last_value >= 0) || (key->last_subkey == -1)))
240 {
241 fprintf( f, "\n[" );
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000242 dump_path( key, base, f );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000243 fprintf( f, "] %ld\n", key->modif );
244 for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
245 }
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000246 for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000247}
248
249static void dump_operation( struct key *key, struct key_value *value, const char *op )
250{
251 fprintf( stderr, "%s key ", op );
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000252 if (key) dump_path( key, NULL, stderr );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000253 else fprintf( stderr, "ERROR" );
254 if (value)
255 {
256 fprintf( stderr, " value ");
257 dump_value( value, stderr );
258 }
259 else fprintf( stderr, "\n" );
260}
261
262static void key_dump( struct object *obj, int verbose )
263{
264 struct key *key = (struct key *)obj;
265 assert( obj->ops == &key_ops );
266 fprintf( stderr, "Key flags=%x ", key->flags );
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000267 dump_path( key, NULL, stderr );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000268 fprintf( stderr, "\n" );
269}
270
271static void key_destroy( struct object *obj )
272{
273 int i;
274 struct key *key = (struct key *)obj;
275 assert( obj->ops == &key_ops );
276
277 free( key->name );
278 if (key->class) free( key->class );
279 for (i = 0; i <= key->last_value; i++)
280 {
281 free( key->values[i].name );
282 if (key->values[i].data) free( key->values[i].data );
283 }
Alexandre Julliard53f3a831999-11-24 04:19:43 +0000284 for (i = 0; i <= key->last_subkey; i++)
285 {
286 key->subkeys[i]->parent = NULL;
287 release_object( key->subkeys[i] );
288 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000289}
290
291/* duplicate a key path from the request buffer */
292/* returns a pointer to a static buffer, so only useable once per request */
293static WCHAR *copy_path( const path_t path )
294{
295 static WCHAR buffer[MAX_PATH+1];
296 WCHAR *p = buffer;
297
298 while (p < buffer + sizeof(buffer) - 1) if (!(*p++ = *path++)) break;
299 *p = 0;
300 return buffer;
301}
302
303/* return the next token in a given path */
304/* returns a pointer to a static buffer, so only useable once per request */
305static WCHAR *get_path_token( const WCHAR *initpath, size_t maxlen )
306{
307 static const WCHAR *path;
308 static const WCHAR *end;
309 static WCHAR buffer[MAX_PATH+1];
310 WCHAR *p = buffer;
311
312 if (initpath)
313 {
314 path = initpath;
315 end = path + maxlen / sizeof(WCHAR);
316 }
317 while ((path < end) && (*path == '\\')) path++;
318 while ((path < end) && (p < buffer + sizeof(buffer) - 1))
319 {
320 WCHAR ch = *path;
321 if (!ch || (ch == '\\')) break;
322 *p++ = ch;
323 path++;
324 }
325 *p = 0;
326 return buffer;
327}
328
329/* duplicate a Unicode string from the request buffer */
Alexandre Julliardef886372000-04-04 19:33:27 +0000330static WCHAR *req_strdupW( const void *req, const WCHAR *str )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000331{
332 WCHAR *name;
Alexandre Julliardef886372000-04-04 19:33:27 +0000333 size_t len = get_req_strlenW( req, str );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000334 if ((name = mem_alloc( (len + 1) * sizeof(WCHAR) )) != NULL)
335 {
336 memcpy( name, str, len * sizeof(WCHAR) );
337 name[len] = 0;
338 }
339 return name;
340}
341
342/* allocate a key object */
343static struct key *alloc_key( const WCHAR *name, time_t modif )
344{
345 struct key *key;
Alexandre Julliard1dca5e22000-01-01 00:56:27 +0000346 if ((key = (struct key *)alloc_object( &key_ops, -1 )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000347 {
348 key->name = NULL;
349 key->class = NULL;
350 key->flags = 0;
351 key->last_subkey = -1;
352 key->nb_subkeys = 0;
353 key->subkeys = NULL;
354 key->nb_values = 0;
355 key->last_value = -1;
356 key->values = NULL;
357 key->level = current_level;
358 key->modif = modif;
359 key->parent = NULL;
360 if (name && !(key->name = strdupW( name )))
361 {
362 release_object( key );
363 key = NULL;
364 }
365 }
366 return key;
367}
368
369/* update key modification time */
370static void touch_key( struct key *key )
371{
372 key->modif = time(NULL);
Francois Gouget6d77d3a2000-03-25 21:44:35 +0000373 key->level = max( key->level, current_level );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000374}
375
376/* try to grow the array of subkeys; return 1 if OK, 0 on error */
377static int grow_subkeys( struct key *key )
378{
379 struct key **new_subkeys;
380 int nb_subkeys;
381
382 if (key->nb_subkeys)
383 {
384 nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
385 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
386 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000387 set_error( STATUS_NO_MEMORY );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000388 return 0;
389 }
390 }
391 else
392 {
393 nb_subkeys = MIN_VALUES;
394 if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
395 }
396 key->subkeys = new_subkeys;
397 key->nb_subkeys = nb_subkeys;
398 return 1;
399}
400
401/* allocate a subkey for a given key, and return its index */
402static struct key *alloc_subkey( struct key *parent, const WCHAR *name, int index, time_t modif )
403{
404 struct key *key;
405 int i;
406
407 if (parent->last_subkey + 1 == parent->nb_subkeys)
408 {
409 /* need to grow the array */
410 if (!grow_subkeys( parent )) return NULL;
411 }
412 if ((key = alloc_key( name, modif )) != NULL)
413 {
414 key->parent = parent;
415 for (i = ++parent->last_subkey; i > index; i--)
416 parent->subkeys[i] = parent->subkeys[i-1];
417 parent->subkeys[index] = key;
418 }
419 return key;
420}
421
422/* free a subkey of a given key */
423static void free_subkey( struct key *parent, int index )
424{
425 struct key *key;
426 int i, nb_subkeys;
427
428 assert( index >= 0 );
429 assert( index <= parent->last_subkey );
430
431 key = parent->subkeys[index];
432 for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
433 parent->last_subkey--;
434 key->flags |= KEY_DELETED;
435 key->parent = NULL;
436 release_object( key );
437
438 /* try to shrink the array */
439 nb_subkeys = key->nb_subkeys;
440 if (nb_subkeys > MIN_SUBKEYS && key->last_subkey < nb_subkeys / 2)
441 {
442 struct key **new_subkeys;
443 nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
444 if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
445 if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
446 key->subkeys = new_subkeys;
447 key->nb_subkeys = nb_subkeys;
448 }
449}
450
451/* find the named child of a given key and return its index */
452static struct key *find_subkey( struct key *key, const WCHAR *name, int *index )
453{
454 int i, min, max, res;
455
456 min = 0;
457 max = key->last_subkey;
458 while (min <= max)
459 {
460 i = (min + max) / 2;
461 if (!(res = strcmpiW( key->subkeys[i]->name, name )))
462 {
463 *index = i;
464 return key->subkeys[i];
465 }
466 if (res > 0) max = i - 1;
467 else min = i + 1;
468 }
469 *index = min; /* this is where we should insert it */
470 return NULL;
471}
472
473/* open a subkey */
474static struct key *open_key( struct key *key, const WCHAR *name, size_t maxlen )
475{
476 int index;
477 WCHAR *path;
478
479 path = get_path_token( name, maxlen );
480 while (*path)
481 {
482 if (!(key = find_subkey( key, path, &index )))
483 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000484 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000485 break;
486 }
487 path = get_path_token( NULL, 0 );
488 }
489
490 if (debug_level > 1) dump_operation( key, NULL, "Open" );
491 if (key) grab_object( key );
492 return key;
493}
494
495/* create a subkey */
496static struct key *create_key( struct key *key, const WCHAR *name, size_t maxlen, WCHAR *class,
497 unsigned int options, time_t modif, int *created )
498{
499 struct key *base;
500 int base_idx, index, flags = 0;
501 WCHAR *path;
502
503 if (key->flags & KEY_DELETED) /* we cannot create a subkey under a deleted key */
504 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000505 set_error( STATUS_KEY_DELETED );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000506 return NULL;
507 }
508 if (options & REG_OPTION_VOLATILE) flags |= KEY_VOLATILE;
509 else if (key->flags & KEY_VOLATILE)
510 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000511 set_error( STATUS_CHILD_MUST_BE_VOLATILE );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000512 return NULL;
513 }
514
515 path = get_path_token( name, maxlen );
516 *created = 0;
517 while (*path)
518 {
519 struct key *subkey;
520 if (!(subkey = find_subkey( key, path, &index ))) break;
521 key = subkey;
522 path = get_path_token( NULL, 0 );
523 }
524
525 /* create the remaining part */
526
527 if (!*path) goto done;
528 *created = 1;
529 base = key;
530 base_idx = index;
531 key = alloc_subkey( key, path, index, modif );
532 while (key)
533 {
534 key->flags |= flags;
535 path = get_path_token( NULL, 0 );
536 if (!*path) goto done;
537 /* we know the index is always 0 in a new key */
538 key = alloc_subkey( key, path, 0, modif );
539 }
540 if (base_idx != -1) free_subkey( base, base_idx );
541 return NULL;
542
543 done:
544 if (debug_level > 1) dump_operation( key, NULL, "Create" );
545 if (class) key->class = strdupW(class);
546 grab_object( key );
547 return key;
548}
549
550/* find a subkey of a given key by its index */
551static void enum_key( struct key *parent, int index, WCHAR *name, WCHAR *class, time_t *modif )
552{
553 struct key *key;
554
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000555 if ((index < 0) || (index > parent->last_subkey)) set_error( STATUS_NO_MORE_ENTRIES );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000556 else
557 {
558 key = parent->subkeys[index];
559 *modif = key->modif;
560 strcpyW( name, key->name );
561 if (key->class) strcpyW( class, key->class ); /* FIXME: length */
562 else *class = 0;
563 if (debug_level > 1) dump_operation( key, NULL, "Enum" );
564 }
565}
566
567/* query information about a key */
568static void query_key( struct key *key, struct query_key_info_request *req )
569{
570 int i, len;
571 int max_subkey = 0, max_class = 0;
572 int max_value = 0, max_data = 0;
573
Ulrich Czekalla6f676cd1999-12-10 03:43:07 +0000574 for (i = 0; i <= key->last_subkey; i++)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000575 {
576 struct key *subkey = key->subkeys[i];
577 len = strlenW( subkey->name );
578 if (len > max_subkey) max_subkey = len;
579 if (!subkey->class) continue;
580 len = strlenW( subkey->class );
581 if (len > max_class) max_class = len;
582 }
Ulrich Czekalla6f676cd1999-12-10 03:43:07 +0000583 for (i = 0; i <= key->last_value; i++)
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000584 {
585 len = strlenW( key->values[i].name );
586 if (len > max_value) max_value = len;
587 len = key->values[i].len;
588 if (len > max_data) max_data = len;
589 }
590 req->subkeys = key->last_subkey + 1;
591 req->max_subkey = max_subkey;
592 req->max_class = max_class;
593 req->values = key->last_value + 1;
594 req->max_value = max_value;
595 req->max_data = max_data;
596 req->modif = key->modif;
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000597 strcpyW( req->name, key->name);
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000598 if (key->class) strcpyW( req->class, key->class ); /* FIXME: length */
599 else req->class[0] = 0;
600 if (debug_level > 1) dump_operation( key, NULL, "Query" );
601}
602
603/* delete a key and its values */
604static void delete_key( struct key *key, const WCHAR *name, size_t maxlen )
605{
606 int index;
607 struct key *parent;
608 WCHAR *path;
609
610 path = get_path_token( name, maxlen );
611 if (!*path)
612 {
613 /* deleting this key, must find parent and index */
614 if (key->flags & KEY_ROOT)
615 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000616 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000617 return;
618 }
619 if (!(parent = key->parent) || (key->flags & KEY_DELETED))
620 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000621 set_error( STATUS_KEY_DELETED );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000622 return;
623 }
624 for (index = 0; index <= parent->last_subkey; index++)
625 if (parent->subkeys[index] == key) break;
626 assert( index <= parent->last_subkey );
627 }
628 else while (*path)
629 {
630 parent = key;
631 if (!(key = find_subkey( parent, path, &index )))
632 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000633 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000634 return;
635 }
636 path = get_path_token( NULL, 0 );
637 }
638
639 /* we can only delete a key that has no subkeys (FIXME) */
640 if ((key->flags & KEY_ROOT) || (key->last_subkey >= 0))
641 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000642 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000643 return;
644 }
645 if (debug_level > 1) dump_operation( key, NULL, "Delete" );
646 free_subkey( parent, index );
647 touch_key( parent );
648}
649
650/* try to grow the array of values; return 1 if OK, 0 on error */
651static int grow_values( struct key *key )
652{
653 struct key_value *new_val;
654 int nb_values;
655
656 if (key->nb_values)
657 {
658 nb_values = key->nb_values + (key->nb_values / 2); /* grow by 50% */
659 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) )))
660 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000661 set_error( STATUS_NO_MEMORY );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000662 return 0;
663 }
664 }
665 else
666 {
667 nb_values = MIN_VALUES;
668 if (!(new_val = mem_alloc( nb_values * sizeof(*new_val) ))) return 0;
669 }
670 key->values = new_val;
671 key->nb_values = nb_values;
672 return 1;
673}
674
675/* find the named value of a given key and return its index in the array */
676static struct key_value *find_value( const struct key *key, const WCHAR *name, int *index )
677{
678 int i, min, max, res;
679
680 min = 0;
681 max = key->last_value;
682 while (min <= max)
683 {
684 i = (min + max) / 2;
685 if (!(res = strcmpiW( key->values[i].name, name )))
686 {
687 *index = i;
688 return &key->values[i];
689 }
690 if (res > 0) max = i - 1;
691 else min = i + 1;
692 }
693 *index = min; /* this is where we should insert it */
694 return NULL;
695}
696
697/* insert a new value or return a pointer to an existing one */
698static struct key_value *insert_value( struct key *key, const WCHAR *name )
699{
700 struct key_value *value;
701 WCHAR *new_name;
702 int i, index;
703
704 if (!(value = find_value( key, name, &index )))
705 {
706 /* not found, add it */
707 if (key->last_value + 1 == key->nb_values)
708 {
709 if (!grow_values( key )) return NULL;
710 }
711 if (!(new_name = strdupW(name))) return NULL;
712 for (i = ++key->last_value; i > index; i--) key->values[i] = key->values[i - 1];
713 value = &key->values[index];
714 value->name = new_name;
715 value->len = 0;
716 value->data = NULL;
717 }
718 return value;
719}
720
721/* set a key value */
722static void set_value( struct key *key, WCHAR *name, int type, int datalen, void *data )
723{
724 struct key_value *value;
725 void *ptr = NULL;
726
727 /* first copy the data */
728 if (datalen)
729 {
730 if (!(ptr = mem_alloc( datalen ))) return;
731 memcpy( ptr, data, datalen );
732 }
733
734 if (!(value = insert_value( key, name )))
735 {
736 if (ptr) free( ptr );
737 return;
738 }
739 if (value->data) free( value->data ); /* already existing, free previous data */
740 value->type = type;
741 value->len = datalen;
742 value->data = ptr;
743 touch_key( key );
744 if (debug_level > 1) dump_operation( key, value, "Set" );
745}
746
747/* get a key value */
748static void get_value( struct key *key, WCHAR *name, int *type, int *len, void *data )
749{
750 struct key_value *value;
751 int index;
752
753 if ((value = find_value( key, name, &index )))
754 {
755 *type = value->type;
756 *len = value->len;
757 if (value->data) memcpy( data, value->data, value->len );
758 if (debug_level > 1) dump_operation( key, value, "Get" );
759 }
760 else
761 {
762 *type = -1;
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000763 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000764 }
765}
766
767/* enumerate a key value */
768static void enum_value( struct key *key, int i, WCHAR *name, int *type, int *len, void *data )
769{
770 struct key_value *value;
771
Alexandre Julliardef886372000-04-04 19:33:27 +0000772 if (i < 0 || i > key->last_value) set_error( STATUS_NO_MORE_ENTRIES );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000773 else
774 {
775 value = &key->values[i];
776 strcpyW( name, value->name );
777 *type = value->type;
778 *len = value->len;
779 if (value->data) memcpy( data, value->data, value->len );
780 if (debug_level > 1) dump_operation( key, value, "Enum" );
781 }
782}
783
784/* delete a value */
785static void delete_value( struct key *key, const WCHAR *name )
786{
787 struct key_value *value;
788 int i, index, nb_values;
789
790 if (!(value = find_value( key, name, &index )))
791 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000792 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000793 return;
794 }
795 if (debug_level > 1) dump_operation( key, value, "Delete" );
796 free( value->name );
797 if (value->data) free( value->data );
798 for (i = index; i < key->last_value; i++) key->values[i] = key->values[i + 1];
799 key->last_value--;
800 touch_key( key );
801
802 /* try to shrink the array */
803 nb_values = key->nb_values;
804 if (nb_values > MIN_VALUES && key->last_value < nb_values / 2)
805 {
806 struct key_value *new_val;
807 nb_values -= nb_values / 3; /* shrink by 33% */
808 if (nb_values < MIN_VALUES) nb_values = MIN_VALUES;
809 if (!(new_val = realloc( key->values, nb_values * sizeof(*new_val) ))) return;
810 key->values = new_val;
811 key->nb_values = nb_values;
812 }
813}
814
815static struct key *get_hkey_obj( int hkey, unsigned int access );
816
817static struct key *create_root_key( int hkey )
818{
819 int dummy;
820 struct key *key;
821
822 switch(hkey)
823 {
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000824 /* the two real root-keys */
825 case HKEY_LOCAL_MACHINE:
826 {
827 static const WCHAR name[] = { 'M','A','C','H','I','N','E',0 };
828 key = alloc_key( name, time(NULL) );
829 }
830 break;
831 case HKEY_USERS:
832 {
833 static const WCHAR name[] = { 'U','S','E','R',0 };
834 key = alloc_key( name, time(NULL) );
835 }
836 break;
837 /* special subkeys */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000838 case HKEY_CLASSES_ROOT:
839 {
840 static const WCHAR name[] =
841 { 'S','O','F','T','W','A','R','E','\\','C','l','a','s','s','e','s',0 };
842
843 struct key *root = get_hkey_obj( HKEY_LOCAL_MACHINE, 0 );
Juergen Schmied788d8d52000-01-27 05:37:54 +0000844 assert( root );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000845 key = create_key( root, name, sizeof(name), NULL, 0, time(NULL), &dummy );
846 release_object( root );
847 }
848 break;
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000849 case HKEY_CURRENT_CONFIG:
850 {
851 static const WCHAR name[] = {
852 'S','Y','S','T','E','M','\\',
853 'C','U','R','R','E','N','T','C','O','N','T','R','O','L','S','E','T','\\',
854 'H','A','R','D','W','A','R','E','P','R','O','F','I','L','E','S','\\',
855 'C','U','R','R','E','N','T',0};
856 struct key *root = get_hkey_obj( HKEY_LOCAL_MACHINE, 0 );
Juergen Schmied788d8d52000-01-27 05:37:54 +0000857 assert( root );
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000858 key = create_key( root, name, sizeof(name), NULL, 0, time(NULL), &dummy );
859 release_object( root );
860 }
861 break;
862 case HKEY_CURRENT_USER:
863 {
Juergen Schmied788d8d52000-01-27 05:37:54 +0000864 /* get the current user name */
865 int i, len;
866 WCHAR *name;
867 char buffer[10];
868 const char *p;
869 struct passwd *pwd = getpwuid( getuid() );
870
871 if (pwd) p = pwd->pw_name;
872 else
873 {
Patrik Stridvall7e9913f2000-03-08 18:25:22 +0000874 sprintf( buffer, "%ld", (long) getuid() );
Juergen Schmied788d8d52000-01-27 05:37:54 +0000875 p = buffer;
876 }
877 len = strlen(p);
878 if ((name = mem_alloc( (len+1) * sizeof(WCHAR) )))
879 {
880 struct key *root = get_hkey_obj( HKEY_USERS, 0 );
881 assert( root );
882 for (i = 0; i <= len; i++) name[i] = p[i];
883 key = create_key( root, name, (len+1) * sizeof(WCHAR),
884 NULL, 0, time(NULL), &dummy );
885 release_object( root );
886 free( name );
887 }
888 else key = NULL;
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +0000889 }
890 break;
891 /* dynamically generated keys */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000892 case HKEY_PERFORMANCE_DATA:
893 case HKEY_DYN_DATA:
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000894 key = alloc_key( NULL, time(NULL) );
895 break;
896 default:
897 key = NULL;
898 assert(0);
899 }
900 if (key)
901 {
902 root_keys[hkey - HKEY_ROOT_FIRST] = key;
903 key->flags |= KEY_ROOT;
904 }
905 return key;
906}
907
Alexandre Julliardd7e85d61999-11-23 19:39:11 +0000908/* get the registry key corresponding to an hkey handle */
909static struct key *get_hkey_obj( int hkey, unsigned int access )
910{
911 struct key *key;
912
913 if (IS_ROOT_HKEY(hkey))
914 {
915 if (!(key = root_keys[hkey - HKEY_ROOT_FIRST])) key = create_root_key( hkey );
916 grab_object( key );
917 }
918 else
919 key = (struct key *)get_handle_obj( current->process, hkey, access, &key_ops );
920 return key;
921}
922
Alexandre Julliard53f3a831999-11-24 04:19:43 +0000923/* read a line from the input file */
924static int read_next_line( struct file_load_info *info )
925{
926 char *newbuf;
927 int newlen, pos = 0;
928
929 info->line++;
930 for (;;)
931 {
932 if (!fgets( info->buffer + pos, info->len - pos, info->file ))
933 return (pos != 0); /* EOF */
934 pos = strlen(info->buffer);
935 if (info->buffer[pos-1] == '\n')
936 {
937 /* got a full line */
938 info->buffer[--pos] = 0;
939 if (pos > 0 && info->buffer[pos-1] == '\r') info->buffer[pos-1] = 0;
940 return 1;
941 }
942 if (pos < info->len - 1) return 1; /* EOF but something was read */
943
944 /* need to enlarge the buffer */
945 newlen = info->len + info->len / 2;
946 if (!(newbuf = realloc( info->buffer, newlen )))
947 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000948 set_error( STATUS_NO_MEMORY );
Alexandre Julliard53f3a831999-11-24 04:19:43 +0000949 return -1;
950 }
951 info->buffer = newbuf;
952 info->len = newlen;
953 }
954}
955
956/* make sure the temp buffer holds enough space */
957static int get_file_tmp_space( struct file_load_info *info, int size )
958{
959 char *tmp;
960 if (info->tmplen >= size) return 1;
961 if (!(tmp = realloc( info->tmp, size )))
962 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000963 set_error( STATUS_NO_MEMORY );
Alexandre Julliard53f3a831999-11-24 04:19:43 +0000964 return 0;
965 }
966 info->tmp = tmp;
967 info->tmplen = size;
968 return 1;
969}
970
971/* report an error while loading an input file */
972static void file_read_error( const char *err, struct file_load_info *info )
973{
974 fprintf( stderr, "Line %d: %s '%s'\n", info->line, err, info->buffer );
975}
976
977/* parse an escaped string back into Unicode */
978/* return the number of chars read from the input, or -1 on output overflow */
979static int parse_strW( WCHAR *dest, int *len, const char *src, char endchar )
980{
981 int count = sizeof(WCHAR); /* for terminating null */
982 const char *p = src;
983 while (*p && *p != endchar)
984 {
985 if (*p != '\\') *dest = (WCHAR)*p++;
986 else
987 {
988 p++;
989 switch(*p)
990 {
991 case 'a': *dest = '\a'; p++; break;
992 case 'b': *dest = '\b'; p++; break;
993 case 'e': *dest = '\e'; p++; break;
994 case 'f': *dest = '\f'; p++; break;
995 case 'n': *dest = '\n'; p++; break;
996 case 'r': *dest = '\r'; p++; break;
997 case 't': *dest = '\t'; p++; break;
998 case 'v': *dest = '\v'; p++; break;
999 case 'x': /* hex escape */
1000 p++;
1001 if (!isxdigit(*p)) *dest = 'x';
1002 else
1003 {
1004 *dest = to_hex(*p++);
1005 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1006 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1007 if (isxdigit(*p)) *dest = (*dest * 16) + to_hex(*p++);
1008 }
1009 break;
1010 case '0':
1011 case '1':
1012 case '2':
1013 case '3':
1014 case '4':
1015 case '5':
1016 case '6':
1017 case '7': /* octal escape */
1018 *dest = *p++ - '0';
1019 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1020 if (*p >= '0' && *p <= '7') *dest = (*dest * 8) + (*p++ - '0');
1021 break;
1022 default:
1023 *dest = (WCHAR)*p++;
1024 break;
1025 }
1026 }
1027 if ((count += sizeof(WCHAR)) > *len) return -1; /* dest buffer overflow */
1028 dest++;
1029 }
1030 *dest = 0;
1031 if (!*p) return -1; /* delimiter not found */
1032 *len = count;
1033 return p + 1 - src;
1034}
1035
1036/* convert a data type tag to a value type */
1037static int get_data_type( const char *buffer, int *type, int *parse_type )
1038{
1039 struct data_type { const char *tag; int len; int type; int parse_type; };
1040
1041 static const struct data_type data_types[] =
1042 { /* actual type */ /* type to assume for parsing */
1043 { "\"", 1, REG_SZ, REG_SZ },
1044 { "str:\"", 5, REG_SZ, REG_SZ },
1045 { "str(2):\"", 8, REG_EXPAND_SZ, REG_SZ },
1046 { "str(7):\"", 8, REG_MULTI_SZ, REG_SZ },
1047 { "hex:", 4, REG_BINARY, REG_BINARY },
1048 { "dword:", 6, REG_DWORD, REG_DWORD },
1049 { "hex(", 4, -1, REG_BINARY },
1050 { NULL, }
1051 };
1052
1053 const struct data_type *ptr;
1054 char *end;
1055
1056 for (ptr = data_types; ptr->tag; ptr++)
1057 {
1058 if (memcmp( ptr->tag, buffer, ptr->len )) continue;
1059 *parse_type = ptr->parse_type;
1060 if ((*type = ptr->type) != -1) return ptr->len;
1061 /* "hex(xx):" is special */
1062 *type = (int)strtoul( buffer + 4, &end, 16 );
1063 if ((end <= buffer) || memcmp( end, "):", 2 )) return 0;
1064 return end + 2 - buffer;
1065 }
1066 return 0;
1067}
1068
1069/* load and create a key from the input file */
1070static struct key *load_key( struct key *base, const char *buffer, struct file_load_info *info )
1071{
1072 WCHAR *p;
1073 int res, len, modif;
1074
1075 len = strlen(buffer) * sizeof(WCHAR);
1076 if (!get_file_tmp_space( info, len )) return NULL;
1077
1078 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, ']' )) == -1)
1079 {
1080 file_read_error( "Malformed key", info );
1081 return NULL;
1082 }
1083 if (!sscanf( buffer + res, " %d", &modif )) modif = time(NULL);
1084
1085 for (p = (WCHAR *)info->tmp; *p; p++) if (*p == '\\') { p++; break; }
1086 return create_key( base, p, len - ((char *)p - info->tmp), NULL, 0, modif, &res );
1087}
1088
1089/* parse a comma-separated list of hex digits */
1090static int parse_hex( unsigned char *dest, int *len, const char *buffer )
1091{
1092 const char *p = buffer;
1093 int count = 0;
1094 while (isxdigit(*p))
1095 {
1096 int val;
1097 char buf[3];
1098 memcpy( buf, p, 2 );
1099 buf[2] = 0;
1100 sscanf( buf, "%x", &val );
1101 if (count++ >= *len) return -1; /* dest buffer overflow */
1102 *dest++ = (unsigned char )val;
1103 p += 2;
1104 if (*p == ',') p++;
1105 }
1106 *len = count;
1107 return p - buffer;
1108}
1109
1110/* parse a value name and create the corresponding value */
1111static struct key_value *parse_value_name( struct key *key, const char *buffer, int *len,
1112 struct file_load_info *info )
1113{
1114 int maxlen = strlen(buffer) * sizeof(WCHAR);
1115 if (!get_file_tmp_space( info, maxlen )) return NULL;
1116 if (buffer[0] == '@')
1117 {
1118 info->tmp[0] = info->tmp[1] = 0;
1119 *len = 1;
1120 }
1121 else
1122 {
1123 if ((*len = parse_strW( (WCHAR *)info->tmp, &maxlen, buffer + 1, '\"' )) == -1) goto error;
1124 (*len)++; /* for initial quote */
1125 }
1126 if (buffer[*len] != '=') goto error;
1127 (*len)++;
1128 return insert_value( key, (WCHAR *)info->tmp );
1129
1130 error:
1131 file_read_error( "Malformed value name", info );
1132 return NULL;
1133}
1134
1135/* load a value from the input file */
1136static int load_value( struct key *key, const char *buffer, struct file_load_info *info )
1137{
1138 DWORD dw;
1139 void *ptr, *newptr;
1140 int maxlen, len, res;
1141 int type, parse_type;
1142 struct key_value *value;
1143
1144 if (!(value = parse_value_name( key, buffer, &len, info ))) return 0;
1145 if (!(res = get_data_type( buffer + len, &type, &parse_type ))) goto error;
1146 buffer += len + res;
1147
1148 switch(parse_type)
1149 {
1150 case REG_SZ:
1151 len = strlen(buffer) * sizeof(WCHAR);
1152 if (!get_file_tmp_space( info, len )) return 0;
1153 if ((res = parse_strW( (WCHAR *)info->tmp, &len, buffer, '\"' )) == -1) goto error;
1154 ptr = info->tmp;
1155 break;
1156 case REG_DWORD:
1157 dw = strtoul( buffer, NULL, 16 );
1158 ptr = &dw;
1159 len = sizeof(dw);
1160 break;
1161 case REG_BINARY: /* hex digits */
1162 len = 0;
1163 for (;;)
1164 {
1165 maxlen = 1 + strlen(buffer)/3; /* 3 chars for one hex byte */
1166 if (!get_file_tmp_space( info, len + maxlen )) return 0;
1167 if ((res = parse_hex( info->tmp + len, &maxlen, buffer )) == -1) goto error;
1168 len += maxlen;
1169 buffer += res;
1170 while (isspace(*buffer)) buffer++;
1171 if (!*buffer) break;
1172 if (*buffer != '\\') goto error;
1173 if (read_next_line( info) != 1) goto error;
1174 buffer = info->buffer;
1175 while (isspace(*buffer)) buffer++;
1176 }
1177 ptr = info->tmp;
1178 break;
1179 default:
1180 assert(0);
1181 ptr = NULL; /* keep compiler quiet */
1182 break;
1183 }
1184
1185 if (!len) newptr = NULL;
1186 else if (!(newptr = memdup( ptr, len ))) return 0;
1187
1188 if (value->data) free( value->data );
1189 value->data = newptr;
1190 value->len = len;
1191 value->type = type;
1192 /* update the key level but not the modification time */
Francois Gouget6d77d3a2000-03-25 21:44:35 +00001193 key->level = max( key->level, current_level );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001194 return 1;
1195
1196 error:
1197 file_read_error( "Malformed value", info );
1198 return 0;
1199}
1200
1201/* load all the keys from the input file */
1202static void load_keys( struct key *key, FILE *f )
1203{
1204 struct key *subkey = NULL;
1205 struct file_load_info info;
1206 char *p;
1207
1208 info.file = f;
1209 info.len = 4;
1210 info.tmplen = 4;
1211 info.line = 0;
1212 if (!(info.buffer = mem_alloc( info.len ))) return;
1213 if (!(info.tmp = mem_alloc( info.tmplen )))
1214 {
1215 free( info.buffer );
1216 return;
1217 }
1218
1219 if ((read_next_line( &info ) != 1) ||
1220 strcmp( info.buffer, "WINE REGISTRY Version 2" ))
1221 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +00001222 set_error( STATUS_NOT_REGISTRY_FILE );
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001223 goto done;
1224 }
1225
1226 while (read_next_line( &info ) == 1)
1227 {
1228 for (p = info.buffer; *p && isspace(*p); p++);
1229 switch(*p)
1230 {
1231 case '[': /* new key */
1232 if (subkey) release_object( subkey );
1233 subkey = load_key( key, p + 1, &info );
1234 break;
1235 case '@': /* default value */
1236 case '\"': /* value */
1237 if (subkey) load_value( subkey, p, &info );
1238 else file_read_error( "Value without key", &info );
1239 break;
1240 case '#': /* comment */
1241 case ';': /* comment */
1242 case 0: /* empty line */
1243 break;
1244 default:
1245 file_read_error( "Unrecognized input", &info );
1246 break;
1247 }
1248 }
1249
1250 done:
1251 if (subkey) release_object( subkey );
1252 free( info.buffer );
1253 free( info.tmp );
1254}
1255
1256/* load a part of the registry from a file */
1257static void load_registry( struct key *key, int handle )
1258{
1259 struct object *obj;
1260 int fd;
1261
1262 if (!(obj = get_handle_obj( current->process, handle, GENERIC_READ, NULL ))) return;
1263 fd = obj->ops->get_read_fd( obj );
1264 release_object( obj );
1265 if (fd != -1)
1266 {
1267 FILE *f = fdopen( fd, "r" );
1268 if (f)
1269 {
1270 load_keys( key, f );
1271 fclose( f );
1272 }
1273 else file_set_error();
1274 }
1275}
1276
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001277/* update the level of the parents of a key (only needed for the old format) */
1278static int update_level( struct key *key )
1279{
1280 int i;
1281 int max = key->level;
1282 for (i = 0; i <= key->last_subkey; i++)
1283 {
1284 int sub = update_level( key->subkeys[i] );
1285 if (sub > max) max = sub;
1286 }
1287 key->level = max;
1288 return max;
1289}
1290
1291/* dump a string to a registry save file in the old v1 format */
Huw D M Davies361adc82000-03-24 19:46:30 +00001292static void save_string_v1( LPCWSTR str, FILE *f, int len )
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001293{
1294 if (!str) return;
Huw D M Davies361adc82000-03-24 19:46:30 +00001295 while ((len == -1) ? *str : (*str && len--))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001296 {
1297 if ((*str > 0x7f) || (*str == '\n') || (*str == '='))
1298 fprintf( f, "\\u%04x", *str );
1299 else
1300 {
1301 if (*str == '\\') fputc( '\\', f );
1302 fputc( (char)*str, f );
1303 }
1304 str++;
1305 }
1306}
1307
1308/* save a registry and all its subkeys to a text file in the old v1 format */
1309static void save_subkeys_v1( struct key *key, int nesting, FILE *f )
1310{
1311 int i, j;
1312
1313 if (key->flags & KEY_VOLATILE) return;
1314 if (key->level < saving_level) return;
1315 for (i = 0; i <= key->last_value; i++)
1316 {
1317 struct key_value *value = &key->values[i];
1318 for (j = nesting; j > 0; j --) fputc( '\t', f );
Huw D M Davies361adc82000-03-24 19:46:30 +00001319 save_string_v1( value->name, f, -1 );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001320 fprintf( f, "=%d,%d,", value->type, 0 );
1321 if (value->type == REG_SZ || value->type == REG_EXPAND_SZ)
Huw D M Davies361adc82000-03-24 19:46:30 +00001322 save_string_v1( (LPWSTR)value->data, f, value->len / 2 );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001323 else
1324 for (j = 0; j < value->len; j++)
1325 fprintf( f, "%02x", *((unsigned char *)value->data + j) );
1326 fputc( '\n', f );
1327 }
1328 for (i = 0; i <= key->last_subkey; i++)
1329 {
1330 for (j = nesting; j > 0; j --) fputc( '\t', f );
Huw D M Davies361adc82000-03-24 19:46:30 +00001331 save_string_v1( key->subkeys[i]->name, f, -1 );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001332 fputc( '\n', f );
1333 save_subkeys_v1( key->subkeys[i], nesting + 1, f );
1334 }
1335}
1336
1337/* save a registry branch to a file handle */
1338static void save_registry( struct key *key, int handle )
1339{
1340 struct object *obj;
1341 int fd;
1342
1343 if (key->flags & KEY_DELETED)
1344 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +00001345 set_error( STATUS_KEY_DELETED );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001346 return;
1347 }
1348 if (!(obj = get_handle_obj( current->process, handle, GENERIC_WRITE, NULL ))) return;
1349 fd = obj->ops->get_write_fd( obj );
1350 release_object( obj );
1351 if (fd != -1)
1352 {
1353 FILE *f = fdopen( fd, "w" );
1354 if (f)
1355 {
1356 fprintf( f, "WINE REGISTRY Version %d\n", saving_version );
Juergen Schmied5d0ae2d2000-01-09 21:07:01 +00001357 if (saving_version == 2) save_subkeys( key, key, f );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001358 else
1359 {
1360 update_level( key );
1361 save_subkeys_v1( key, 0, f );
1362 }
1363 if (fclose( f )) file_set_error();
1364 }
1365 else
1366 {
1367 file_set_error();
1368 close( fd );
1369 }
1370 }
1371}
1372
Alexandre Julliardc9709042000-04-16 17:21:13 +00001373/* register a key branch for being saved on exit */
1374static void register_branch_for_saving( struct key *key, const char *path, size_t len )
1375{
1376 if (save_branch_count >= MAX_SAVE_BRANCH_INFO)
1377 {
1378 set_error( STATUS_NO_MORE_ENTRIES );
1379 return;
1380 }
1381 if (!(save_branch_info[save_branch_count].path = memdup( path, len+1 ))) return;
1382 save_branch_info[save_branch_count].path[len] = 0;
1383 save_branch_info[save_branch_count].key = (struct key *)grab_object( key );
1384 save_branch_count++;
1385}
1386
1387/* save a registry branch to a file */
1388static int save_branch( struct key *key, const char *path )
1389{
1390 char *p, *real, *tmp = NULL;
1391 int fd, count = 0, ret = 0;
1392 FILE *f;
1393
1394 /* get the real path */
1395
1396 if (!(real = malloc( PATH_MAX ))) return 0;
1397 if (!realpath( path, real ))
1398 {
1399 free( real );
1400 real = NULL;
1401 }
1402 else path = real;
1403
1404 /* test the file type */
1405
1406 if ((fd = open( path, O_WRONLY )) != -1)
1407 {
1408 struct stat st;
1409 /* if file is not a regular file or has multiple links,
1410 write directly into it; otherwise use a temp file */
1411 if (!fstat( fd, &st ) && (!S_ISREG(st.st_mode) || st.st_nlink > 1))
1412 {
1413 ftruncate( fd, 0 );
1414 goto save;
1415 }
1416 close( fd );
1417 }
1418
1419 /* create a temp file in the same directory */
1420
1421 if (!(tmp = malloc( strlen(path) + 20 ))) goto done;
1422 strcpy( tmp, path );
1423 if ((p = strrchr( tmp, '/' ))) p++;
1424 else p = tmp;
1425 for (;;)
1426 {
Patrik Stridvalle29dbc52000-04-24 18:04:24 +00001427 sprintf( p, "reg%lx%04x.tmp", (long) getpid(), count++ );
Alexandre Julliardc9709042000-04-16 17:21:13 +00001428 if ((fd = open( tmp, O_CREAT | O_EXCL | O_WRONLY, 0666 )) != -1) break;
1429 if (errno != EEXIST) goto done;
1430 close( fd );
1431 }
1432
1433 /* now save to it */
1434
1435 save:
1436 if (!(f = fdopen( fd, "w" )))
1437 {
1438 if (tmp) unlink( tmp );
1439 close( fd );
1440 goto done;
1441 }
1442
1443 if (debug_level > 1)
1444 {
1445 fprintf( stderr, "%s: ", path );
1446 dump_operation( key, NULL, "saving" );
1447 }
1448
1449 fprintf( f, "WINE REGISTRY Version %d\n", saving_version );
1450 if (saving_version == 2) save_subkeys( key, key, f );
1451 else
1452 {
1453 update_level( key );
1454 save_subkeys_v1( key, 0, f );
1455 }
1456 ret = !fclose(f);
1457
1458 if (tmp)
1459 {
1460 /* if successfully written, rename to final name */
1461 if (ret) ret = !rename( tmp, path );
1462 if (!ret) unlink( tmp );
1463 free( tmp );
1464 }
1465
1466done:
1467 if (real) free( real );
1468 return ret;
1469}
1470
1471/* periodic saving of the registry */
1472static void periodic_save( void *arg )
1473{
1474 int i;
1475 for (i = 0; i < save_branch_count; i++)
1476 save_branch( save_branch_info[i].key, save_branch_info[i].path );
1477 add_timeout( &next_save_time, save_period );
1478 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1479}
1480
1481/* save the registry and close the top-level keys; used on server exit */
1482void close_registry(void)
1483{
1484 int i;
1485
1486 for (i = 0; i < save_branch_count; i++)
1487 {
1488 if (!save_branch( save_branch_info[i].key, save_branch_info[i].path ))
1489 {
1490 fprintf( stderr, "wineserver: could not save registry branch to %s",
1491 save_branch_info[i].path );
1492 perror( " " );
1493 }
1494 release_object( save_branch_info[i].key );
1495 }
1496 for (i = 0; i < NB_ROOT_KEYS; i++)
1497 {
1498 if (root_keys[i]) release_object( root_keys[i] );
1499 }
1500}
1501
1502
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001503/* create a registry key */
1504DECL_HANDLER(create_key)
1505{
1506 struct key *key, *parent;
1507 WCHAR *class;
1508 unsigned int access = req->access;
1509
1510 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1511 req->hkey = -1;
Alexandre Julliard27d31a52000-04-16 20:28:42 +00001512 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001513 {
Alexandre Julliardef886372000-04-04 19:33:27 +00001514 if ((class = req_strdupW( req, req->class )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001515 {
1516 if ((key = create_key( parent, req->name, sizeof(req->name), class, req->options,
1517 req->modif, &req->created )))
1518 {
1519 req->hkey = alloc_handle( current->process, key, access, 0 );
1520 release_object( key );
1521 }
1522 free( class );
1523 }
1524 release_object( parent );
1525 }
1526}
1527
1528/* open a registry key */
1529DECL_HANDLER(open_key)
1530{
1531 struct key *key, *parent;
1532 unsigned int access = req->access;
1533
1534 if (access & MAXIMUM_ALLOWED) access = KEY_ALL_ACCESS; /* FIXME: needs general solution */
1535 req->hkey = -1;
1536 if ((parent = get_hkey_obj( req->parent, 0 /*FIXME*/ )))
1537 {
1538 if ((key = open_key( parent, req->name, sizeof(req->name) )))
1539 {
1540 req->hkey = alloc_handle( current->process, key, access, 0 );
1541 release_object( key );
1542 }
1543 release_object( parent );
1544 }
1545}
1546
1547/* delete a registry key */
1548DECL_HANDLER(delete_key)
1549{
1550 struct key *key;
1551
Alexandre Julliard27d31a52000-04-16 20:28:42 +00001552 if ((key = get_hkey_obj( req->hkey, 0 /*FIXME*/ )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001553 {
1554 delete_key( key, req->name, sizeof(req->name) );
1555 release_object( key );
1556 }
1557}
1558
1559/* close a registry key */
1560DECL_HANDLER(close_key)
1561{
1562 int hkey = req->hkey;
1563 /* ignore attempts to close a root key */
1564 if (!IS_ROOT_HKEY(hkey)) close_handle( current->process, hkey );
1565}
1566
1567/* enumerate registry subkeys */
1568DECL_HANDLER(enum_key)
1569{
1570 struct key *key;
1571
Alexandre Julliardef886372000-04-04 19:33:27 +00001572 req->name[0] = req->class[0] = 0;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001573 if ((key = get_hkey_obj( req->hkey, KEY_ENUMERATE_SUB_KEYS )))
1574 {
1575 enum_key( key, req->index, req->name, req->class, &req->modif );
1576 release_object( key );
1577 }
1578}
1579
1580/* query information about a registry key */
1581DECL_HANDLER(query_key_info)
1582{
1583 struct key *key;
1584
Alexandre Julliardef886372000-04-04 19:33:27 +00001585 req->name[0] = req->class[0] = 0;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001586 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1587 {
1588 query_key( key, req );
1589 release_object( key );
1590 }
1591}
1592
1593/* set a value of a registry key */
1594DECL_HANDLER(set_key_value)
1595{
1596 struct key *key;
Alexandre Julliardef886372000-04-04 19:33:27 +00001597 int max = get_req_size( req, req->data, sizeof(req->data[0]) );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001598 int datalen = req->len;
1599 if (datalen > max)
1600 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +00001601 set_error( STATUS_NO_MEMORY ); /* FIXME */
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001602 return;
1603 }
1604 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1605 {
1606 set_value( key, copy_path( req->name ), req->type, datalen, req->data );
1607 release_object( key );
1608 }
1609}
1610
1611/* retrieve the value of a registry key */
1612DECL_HANDLER(get_key_value)
1613{
1614 struct key *key;
1615
Alexandre Julliardef886372000-04-04 19:33:27 +00001616 req->len = 0;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001617 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1618 {
1619 get_value( key, copy_path( req->name ), &req->type, &req->len, req->data );
1620 release_object( key );
1621 }
1622}
1623
1624/* enumerate the value of a registry key */
1625DECL_HANDLER(enum_key_value)
1626{
1627 struct key *key;
1628
Alexandre Julliardef886372000-04-04 19:33:27 +00001629 req->len = 0;
1630 req->name[0] = 0;
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001631 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE )))
1632 {
1633 enum_value( key, req->index, req->name, &req->type, &req->len, req->data );
1634 release_object( key );
1635 }
1636}
1637
1638/* delete a value of a registry key */
1639DECL_HANDLER(delete_key_value)
1640{
1641 WCHAR *name;
1642 struct key *key;
1643
1644 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE )))
1645 {
Alexandre Julliardef886372000-04-04 19:33:27 +00001646 if ((name = req_strdupW( req, req->name )))
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001647 {
1648 delete_value( key, name );
1649 free( name );
1650 }
1651 release_object( key );
1652 }
1653}
1654
1655/* load a registry branch from a file */
1656DECL_HANDLER(load_registry)
1657{
1658 struct key *key;
1659
1660 if ((key = get_hkey_obj( req->hkey, KEY_SET_VALUE | KEY_CREATE_SUB_KEY )))
1661 {
Alexandre Julliard53f3a831999-11-24 04:19:43 +00001662 /* FIXME: use subkey name */
1663 load_registry( key, req->file );
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001664 release_object( key );
1665 }
1666}
1667
1668/* save a registry branch to a file */
1669DECL_HANDLER(save_registry)
1670{
1671 struct key *key;
1672
1673 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1674 {
1675 save_registry( key, req->file );
1676 release_object( key );
1677 }
1678}
1679
1680/* set the current and saving level for the registry */
1681DECL_HANDLER(set_registry_levels)
1682{
1683 current_level = req->current;
1684 saving_level = req->saving;
Alexandre Julliard705686e1999-11-24 19:34:32 +00001685 saving_version = req->version;
Alexandre Julliardc9709042000-04-16 17:21:13 +00001686
1687 /* set periodic save timer */
1688
1689 if (save_timeout_user)
1690 {
1691 remove_timeout_user( save_timeout_user );
1692 save_timeout_user = NULL;
1693 }
1694 if ((save_period = req->period))
1695 {
1696 if (save_period < 10000) save_period = 10000; /* limit rate */
1697 gettimeofday( &next_save_time, 0 );
1698 add_timeout( &next_save_time, save_period );
1699 save_timeout_user = add_timeout_user( &next_save_time, periodic_save, 0 );
1700 }
Alexandre Julliardd7e85d61999-11-23 19:39:11 +00001701}
1702
Alexandre Julliardc9709042000-04-16 17:21:13 +00001703/* save a registry branch at server exit */
1704DECL_HANDLER(save_registry_atexit)
1705{
1706 struct key *key;
1707
1708 if ((key = get_hkey_obj( req->hkey, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS )))
1709 {
1710 register_branch_for_saving( key, req->file, get_req_strlen( req, req->file ) );
1711 release_object( key );
1712 }
1713}