forked from wine-mirror/wine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.c
2423 lines (2150 loc) · 74.4 KB
/
registry.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Server-side registry management
*
* Copyright (C) 1999 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/* To do:
* - symbolic links
*/
#include "config.h"
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "object.h"
#include "file.h"
#include "handle.h"
#include "request.h"
#include "process.h"
#include "unicode.h"
#include "security.h"
#include "winternl.h"
struct notify
{
struct list entry; /* entry in list of notifications */
struct event **events; /* events to set when changing this key */
unsigned int event_count; /* number of events */
int subtree; /* true if subtree notification */
unsigned int filter; /* which events to notify on */
obj_handle_t hkey; /* hkey associated with this notification */
struct process *process; /* process in which the hkey is valid */
};
static const WCHAR key_name[] = {'K','e','y'};
struct type_descr key_type =
{
{ key_name, sizeof(key_name) }, /* name */
KEY_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
{ /* mapping */
STANDARD_RIGHTS_READ | KEY_NOTIFY | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
STANDARD_RIGHTS_WRITE | KEY_CREATE_SUB_KEY | KEY_SET_VALUE,
STANDARD_RIGHTS_EXECUTE | KEY_CREATE_LINK | KEY_NOTIFY | KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE,
KEY_ALL_ACCESS
},
};
/* a registry key */
struct key
{
struct object obj; /* object header */
WCHAR *name; /* key name */
WCHAR *class; /* key class */
unsigned short namelen; /* length of key name */
unsigned short classlen; /* length of class name */
struct key *parent; /* parent key */
int last_subkey; /* last in use subkey */
int nb_subkeys; /* count of allocated subkeys */
struct key **subkeys; /* subkeys array */
int last_value; /* last in use value */
int nb_values; /* count of allocated values in array */
struct key_value *values; /* values array */
unsigned int flags; /* flags */
timeout_t modif; /* last modification time */
struct list notify_list; /* list of notifications */
};
/* key flags */
#define KEY_VOLATILE 0x0001 /* key is volatile (not saved to disk) */
#define KEY_DELETED 0x0002 /* key has been deleted */
#define KEY_DIRTY 0x0004 /* key has been modified */
#define KEY_SYMLINK 0x0008 /* key is a symbolic link */
#define KEY_WOW64 0x0010 /* key contains a Wow6432Node subkey */
#define KEY_WOWSHARE 0x0020 /* key is a Wow64 shared key (used for Software\Classes) */
#define KEY_PREDEF 0x0040 /* key is marked as predefined */
/* a key value */
struct key_value
{
WCHAR *name; /* value name */
unsigned short namelen; /* length of value name */
unsigned int type; /* value type */
data_size_t len; /* value data length in bytes */
void *data; /* pointer to value data */
};
#define MIN_SUBKEYS 8 /* min. number of allocated subkeys per key */
#define MIN_VALUES 8 /* min. number of allocated values per key */
#define MAX_NAME_LEN 256 /* max. length of a key name */
#define MAX_VALUE_LEN 16383 /* max. length of a value name */
/* the root of the registry tree */
static struct key *root_key;
static const timeout_t ticks_1601_to_1970 = (timeout_t)86400 * (369 * 365 + 89) * TICKS_PER_SEC;
static const timeout_t save_period = 30 * -TICKS_PER_SEC; /* delay between periodic saves */
static struct timeout_user *save_timeout_user; /* saving timer */
static enum prefix_type { PREFIX_UNKNOWN, PREFIX_32BIT, PREFIX_64BIT } prefix_type;
static const WCHAR root_name[] = { '\\','R','e','g','i','s','t','r','y','\\' };
static const WCHAR wow6432node[] = {'W','o','w','6','4','3','2','N','o','d','e'};
static const WCHAR symlink_value[] = {'S','y','m','b','o','l','i','c','L','i','n','k','V','a','l','u','e'};
static const struct unicode_str symlink_str = { symlink_value, sizeof(symlink_value) };
static void set_periodic_save_timer(void);
static struct key_value *find_value( const struct key *key, const struct unicode_str *name, int *index );
/* information about where to save a registry branch */
struct save_branch_info
{
struct key *key;
const char *path;
};
#define MAX_SAVE_BRANCH_INFO 3
static int save_branch_count;
static struct save_branch_info save_branch_info[MAX_SAVE_BRANCH_INFO];
unsigned int supported_machines_count = 0;
unsigned short supported_machines[8];
unsigned short native_machine = 0;
/* information about a file being loaded */
struct file_load_info
{
const char *filename; /* input file name */
FILE *file; /* input file */
char *buffer; /* line buffer */
int len; /* buffer length */
int line; /* current input line */
WCHAR *tmp; /* temp buffer to use while parsing input */
size_t tmplen; /* length of temp buffer */
};
static void key_dump( struct object *obj, int verbose );
static unsigned int key_map_access( struct object *obj, unsigned int access );
static struct security_descriptor *key_get_sd( struct object *obj );
static WCHAR *key_get_full_name( struct object *obj, data_size_t *len );
static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
static void key_destroy( struct object *obj );
static const struct object_ops key_ops =
{
sizeof(struct key), /* size */
&key_type, /* type */
key_dump, /* dump */
no_add_queue, /* add_queue */
NULL, /* remove_queue */
NULL, /* signaled */
NULL, /* satisfied */
no_signal, /* signal */
no_get_fd, /* get_fd */
key_map_access, /* map_access */
key_get_sd, /* get_sd */
default_set_sd, /* set_sd */
key_get_full_name, /* get_full_name */
no_lookup_name, /* lookup_name */
no_link_name, /* link_name */
NULL, /* unlink_name */
no_open_file, /* open_file */
no_kernel_obj_list, /* get_kernel_obj_list */
key_close_handle, /* close_handle */
key_destroy /* destroy */
};
static inline int is_wow6432node( const WCHAR *name, unsigned int len )
{
return (len == sizeof(wow6432node) && !memicmp_strW( name, wow6432node, sizeof( wow6432node )));
}
/*
* The registry text file format v2 used by this code is similar to the one
* used by REGEDIT import/export functionality, with the following differences:
* - strings and key names can contain \x escapes for Unicode
* - key names use escapes too in order to support Unicode
* - the modification time optionally follows the key name
* - REG_EXPAND_SZ and REG_MULTI_SZ are saved as strings instead of hex
*/
/* dump the full path of a key */
static void dump_path( const struct key *key, const struct key *base, FILE *f )
{
if (key->parent && key->parent != base)
{
dump_path( key->parent, base, f );
fprintf( f, "\\\\" );
}
dump_strW( key->name, key->namelen, f, "[]" );
}
/* dump a value to a text file */
static void dump_value( const struct key_value *value, FILE *f )
{
unsigned int i, dw;
int count;
if (value->namelen)
{
fputc( '\"', f );
count = 1 + dump_strW( value->name, value->namelen, f, "\"\"" );
count += fprintf( f, "\"=" );
}
else count = fprintf( f, "@=" );
switch(value->type)
{
case REG_SZ:
case REG_EXPAND_SZ:
case REG_MULTI_SZ:
/* only output properly terminated strings in string format */
if (value->len < sizeof(WCHAR)) break;
if (value->len % sizeof(WCHAR)) break;
if (((WCHAR *)value->data)[value->len / sizeof(WCHAR) - 1]) break;
if (value->type != REG_SZ) fprintf( f, "str(%x):", value->type );
fputc( '\"', f );
dump_strW( (WCHAR *)value->data, value->len, f, "\"\"" );
fprintf( f, "\"\n" );
return;
case REG_DWORD:
if (value->len != sizeof(dw)) break;
memcpy( &dw, value->data, sizeof(dw) );
fprintf( f, "dword:%08x\n", dw );
return;
}
if (value->type == REG_BINARY) count += fprintf( f, "hex:" );
else count += fprintf( f, "hex(%x):", value->type );
for (i = 0; i < value->len; i++)
{
count += fprintf( f, "%02x", *((unsigned char *)value->data + i) );
if (i < value->len-1)
{
fputc( ',', f );
if (++count > 76)
{
fprintf( f, "\\\n " );
count = 2;
}
}
}
fputc( '\n', f );
}
/* save a registry and all its subkeys to a text file */
static void save_subkeys( const struct key *key, const struct key *base, FILE *f )
{
int i;
if (key->flags & KEY_VOLATILE) return;
/* save key if it has either some values or no subkeys, or needs special options */
/* keys with no values but subkeys are saved implicitly by saving the subkeys */
if ((key->last_value >= 0) || (key->last_subkey == -1) || key->class || (key->flags & KEY_SYMLINK))
{
fprintf( f, "\n[" );
if (key != base) dump_path( key, base, f );
fprintf( f, "] %u\n", (unsigned int)((key->modif - ticks_1601_to_1970) / TICKS_PER_SEC) );
fprintf( f, "#time=%x%08x\n", (unsigned int)(key->modif >> 32), (unsigned int)key->modif );
if (key->class)
{
fprintf( f, "#class=\"" );
dump_strW( key->class, key->classlen, f, "\"\"" );
fprintf( f, "\"\n" );
}
if (key->flags & KEY_SYMLINK) fputs( "#link\n", f );
for (i = 0; i <= key->last_value; i++) dump_value( &key->values[i], f );
}
for (i = 0; i <= key->last_subkey; i++) save_subkeys( key->subkeys[i], base, f );
}
static void dump_operation( const struct key *key, const struct key_value *value, const char *op )
{
fprintf( stderr, "%s key ", op );
if (key) dump_path( key, NULL, stderr );
else fprintf( stderr, "ERROR" );
if (value)
{
fprintf( stderr, " value ");
dump_value( value, stderr );
}
else fprintf( stderr, "\n" );
}
static void key_dump( struct object *obj, int verbose )
{
struct key *key = (struct key *)obj;
assert( obj->ops == &key_ops );
fprintf( stderr, "Key flags=%x ", key->flags );
dump_path( key, NULL, stderr );
fprintf( stderr, "\n" );
}
/* notify waiter and maybe delete the notification */
static void do_notification( struct key *key, struct notify *notify, int del )
{
unsigned int i;
for (i = 0; i < notify->event_count; ++i)
{
set_event( notify->events[i] );
release_object( notify->events[i] );
}
free( notify->events );
notify->events = NULL;
notify->event_count = 0;
if (del)
{
list_remove( ¬ify->entry );
free( notify );
}
}
static inline struct notify *find_notify( struct key *key, struct process *process, obj_handle_t hkey )
{
struct notify *notify;
LIST_FOR_EACH_ENTRY( notify, &key->notify_list, struct notify, entry )
{
if (notify->process == process && notify->hkey == hkey) return notify;
}
return NULL;
}
static unsigned int key_map_access( struct object *obj, unsigned int access )
{
access = default_map_access( obj, access );
/* filter the WOW64 masks, as they aren't real access bits */
return access & ~(KEY_WOW64_64KEY | KEY_WOW64_32KEY);
}
static struct security_descriptor *key_get_sd( struct object *obj )
{
static struct security_descriptor *key_default_sd;
if (obj->sd) return obj->sd;
if (!key_default_sd)
{
size_t users_sid_len = security_sid_len( security_builtin_users_sid );
size_t admins_sid_len = security_sid_len( security_builtin_admins_sid );
size_t dacl_len = sizeof(ACL) + 2 * offsetof( ACCESS_ALLOWED_ACE, SidStart )
+ users_sid_len + admins_sid_len;
ACCESS_ALLOWED_ACE *aaa;
ACL *dacl;
key_default_sd = mem_alloc( sizeof(*key_default_sd) + 2 * admins_sid_len + dacl_len );
key_default_sd->control = SE_DACL_PRESENT;
key_default_sd->owner_len = admins_sid_len;
key_default_sd->group_len = admins_sid_len;
key_default_sd->sacl_len = 0;
key_default_sd->dacl_len = dacl_len;
memcpy( key_default_sd + 1, security_builtin_admins_sid, admins_sid_len );
memcpy( (char *)(key_default_sd + 1) + admins_sid_len, security_builtin_admins_sid, admins_sid_len );
dacl = (ACL *)((char *)(key_default_sd + 1) + 2 * admins_sid_len);
dacl->AclRevision = ACL_REVISION;
dacl->Sbz1 = 0;
dacl->AclSize = dacl_len;
dacl->AceCount = 2;
dacl->Sbz2 = 0;
aaa = (ACCESS_ALLOWED_ACE *)(dacl + 1);
aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
aaa->Header.AceFlags = INHERIT_ONLY_ACE | CONTAINER_INHERIT_ACE;
aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + users_sid_len;
aaa->Mask = GENERIC_READ;
memcpy( &aaa->SidStart, security_builtin_users_sid, users_sid_len );
aaa = (ACCESS_ALLOWED_ACE *)((char *)aaa + aaa->Header.AceSize);
aaa->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
aaa->Header.AceFlags = 0;
aaa->Header.AceSize = offsetof( ACCESS_ALLOWED_ACE, SidStart ) + admins_sid_len;
aaa->Mask = KEY_ALL_ACCESS;
memcpy( &aaa->SidStart, security_builtin_admins_sid, admins_sid_len );
}
return key_default_sd;
}
static WCHAR *key_get_full_name( struct object *obj, data_size_t *ret_len )
{
static const WCHAR backslash = '\\';
struct key *key = (struct key *) obj;
data_size_t len = sizeof(root_name) - sizeof(WCHAR);
char *ret;
if (key->flags & KEY_DELETED)
{
set_error( STATUS_KEY_DELETED );
return NULL;
}
for (key = (struct key *)obj; key != root_key; key = key->parent) len += key->namelen + sizeof(WCHAR);
if (!(ret = malloc( len ))) return NULL;
*ret_len = len;
key = (struct key *)obj;
for (key = (struct key *)obj; key != root_key; key = key->parent)
{
memcpy( ret + len - key->namelen, key->name, key->namelen );
len -= key->namelen + sizeof(WCHAR);
memcpy( ret + len, &backslash, sizeof(WCHAR) );
}
memcpy( ret, root_name, sizeof(root_name) - sizeof(WCHAR) );
return (WCHAR *)ret;
}
/* close the notification associated with a handle */
static int key_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
{
struct key * key = (struct key *) obj;
struct notify *notify = find_notify( key, process, handle );
if (notify) do_notification( key, notify, 1 );
return 1; /* ok to close */
}
static void key_destroy( struct object *obj )
{
int i;
struct list *ptr;
struct key *key = (struct key *)obj;
assert( obj->ops == &key_ops );
free( key->name );
free( key->class );
for (i = 0; i <= key->last_value; i++)
{
free( key->values[i].name );
free( key->values[i].data );
}
free( key->values );
for (i = 0; i <= key->last_subkey; i++)
{
key->subkeys[i]->parent = NULL;
release_object( key->subkeys[i] );
}
free( key->subkeys );
/* unconditionally notify everything waiting on this key */
while ((ptr = list_head( &key->notify_list )))
{
struct notify *notify = LIST_ENTRY( ptr, struct notify, entry );
do_notification( key, notify, 1 );
}
}
/* get the request vararg as registry path */
static inline void get_req_path( struct unicode_str *str, int skip_root )
{
str->str = get_req_data();
str->len = (get_req_data_size() / sizeof(WCHAR)) * sizeof(WCHAR);
if (skip_root && str->len >= sizeof(root_name) && !memicmp_strW( str->str, root_name, sizeof(root_name) ))
{
str->str += ARRAY_SIZE( root_name );
str->len -= sizeof(root_name);
}
}
/* return the next token in a given path */
/* token->str must point inside the path, or be NULL for the first call */
static struct unicode_str *get_path_token( const struct unicode_str *path, struct unicode_str *token )
{
data_size_t i = 0, len = path->len / sizeof(WCHAR);
if (!token->str) /* first time */
{
/* path cannot start with a backslash */
if (len && path->str[0] == '\\')
{
set_error( STATUS_OBJECT_PATH_INVALID );
return NULL;
}
}
else
{
i = token->str - path->str;
i += token->len / sizeof(WCHAR);
while (i < len && path->str[i] == '\\') i++;
}
token->str = path->str + i;
while (i < len && path->str[i] != '\\') i++;
token->len = (path->str + i - token->str) * sizeof(WCHAR);
return token;
}
/* allocate a key object */
static struct key *alloc_key( const struct unicode_str *name, timeout_t modif )
{
struct key *key;
if ((key = alloc_object( &key_ops )))
{
key->name = NULL;
key->class = NULL;
key->namelen = name->len;
key->classlen = 0;
key->flags = 0;
key->last_subkey = -1;
key->nb_subkeys = 0;
key->subkeys = NULL;
key->nb_values = 0;
key->last_value = -1;
key->values = NULL;
key->modif = modif;
key->parent = NULL;
list_init( &key->notify_list );
if (name->len && !(key->name = memdup( name->str, name->len )))
{
release_object( key );
key = NULL;
}
}
return key;
}
/* mark a key and all its parents as dirty (modified) */
static void make_dirty( struct key *key )
{
while (key)
{
if (key->flags & (KEY_DIRTY|KEY_VOLATILE)) return; /* nothing to do */
key->flags |= KEY_DIRTY;
key = key->parent;
}
}
/* mark a key and all its subkeys as clean (not modified) */
static void make_clean( struct key *key )
{
int i;
if (key->flags & KEY_VOLATILE) return;
if (!(key->flags & KEY_DIRTY)) return;
key->flags &= ~KEY_DIRTY;
for (i = 0; i <= key->last_subkey; i++) make_clean( key->subkeys[i] );
}
/* go through all the notifications and send them if necessary */
static void check_notify( struct key *key, unsigned int change, int not_subtree )
{
struct list *ptr, *next;
LIST_FOR_EACH_SAFE( ptr, next, &key->notify_list )
{
struct notify *n = LIST_ENTRY( ptr, struct notify, entry );
if ( ( not_subtree || n->subtree ) && ( change & n->filter ) )
do_notification( key, n, 0 );
}
}
/* update key modification time */
static void touch_key( struct key *key, unsigned int change )
{
struct key *k;
key->modif = current_time;
make_dirty( key );
/* do notifications */
check_notify( key, change, 1 );
for ( k = key->parent; k; k = k->parent )
check_notify( k, change, 0 );
}
/* try to grow the array of subkeys; return 1 if OK, 0 on error */
static int grow_subkeys( struct key *key )
{
struct key **new_subkeys;
int nb_subkeys;
if (key->nb_subkeys)
{
nb_subkeys = key->nb_subkeys + (key->nb_subkeys / 2); /* grow by 50% */
if (!(new_subkeys = realloc( key->subkeys, nb_subkeys * sizeof(*new_subkeys) )))
{
set_error( STATUS_NO_MEMORY );
return 0;
}
}
else
{
nb_subkeys = MIN_SUBKEYS;
if (!(new_subkeys = mem_alloc( nb_subkeys * sizeof(*new_subkeys) ))) return 0;
}
key->subkeys = new_subkeys;
key->nb_subkeys = nb_subkeys;
return 1;
}
/* allocate a subkey for a given key, and return its index */
static struct key *alloc_subkey( struct key *parent, const struct unicode_str *name,
int index, timeout_t modif )
{
struct key *key;
int i;
if (name->len > MAX_NAME_LEN * sizeof(WCHAR))
{
set_error( STATUS_INVALID_PARAMETER );
return NULL;
}
if (parent->last_subkey + 1 == parent->nb_subkeys)
{
/* need to grow the array */
if (!grow_subkeys( parent )) return NULL;
}
if ((key = alloc_key( name, modif )) != NULL)
{
key->parent = parent;
for (i = ++parent->last_subkey; i > index; i--)
parent->subkeys[i] = parent->subkeys[i-1];
parent->subkeys[index] = key;
if (is_wow6432node( key->name, key->namelen ) && !is_wow6432node( parent->name, parent->namelen ))
parent->flags |= KEY_WOW64;
}
return key;
}
/* free a subkey of a given key */
static void free_subkey( struct key *parent, int index )
{
struct key *key;
int i, nb_subkeys;
assert( index >= 0 );
assert( index <= parent->last_subkey );
key = parent->subkeys[index];
for (i = index; i < parent->last_subkey; i++) parent->subkeys[i] = parent->subkeys[i + 1];
parent->last_subkey--;
key->flags |= KEY_DELETED;
key->parent = NULL;
if (is_wow6432node( key->name, key->namelen )) parent->flags &= ~KEY_WOW64;
release_object( key );
/* try to shrink the array */
nb_subkeys = parent->nb_subkeys;
if (nb_subkeys > MIN_SUBKEYS && parent->last_subkey < nb_subkeys / 2)
{
struct key **new_subkeys;
nb_subkeys -= nb_subkeys / 3; /* shrink by 33% */
if (nb_subkeys < MIN_SUBKEYS) nb_subkeys = MIN_SUBKEYS;
if (!(new_subkeys = realloc( parent->subkeys, nb_subkeys * sizeof(*new_subkeys) ))) return;
parent->subkeys = new_subkeys;
parent->nb_subkeys = nb_subkeys;
}
}
/* find the named child of a given key and return its index */
static struct key *find_subkey( const struct key *key, const struct unicode_str *name, int *index )
{
int i, min, max, res;
data_size_t len;
min = 0;
max = key->last_subkey;
while (min <= max)
{
i = (min + max) / 2;
len = min( key->subkeys[i]->namelen, name->len );
res = memicmp_strW( key->subkeys[i]->name, name->str, len );
if (!res) res = key->subkeys[i]->namelen - name->len;
if (!res)
{
*index = i;
return key->subkeys[i];
}
if (res > 0) max = i - 1;
else min = i + 1;
}
*index = min; /* this is where we should insert it */
return NULL;
}
/* return the wow64 variant of the key, or the key itself if none */
static struct key *find_wow64_subkey( struct key *key, const struct unicode_str *name )
{
static const struct unicode_str wow6432node_str = { wow6432node, sizeof(wow6432node) };
int index;
if (!(key->flags & KEY_WOW64)) return key;
if (!is_wow6432node( name->str, name->len ))
{
key = find_subkey( key, &wow6432node_str, &index );
assert( key ); /* if KEY_WOW64 is set we must find it */
}
return key;
}
/* follow a symlink and return the resolved key */
static struct key *follow_symlink( struct key *key, int iteration )
{
struct unicode_str path, token;
struct key_value *value;
int index;
if (iteration > 16) return NULL;
if (!(key->flags & KEY_SYMLINK)) return key;
if (!(value = find_value( key, &symlink_str, &index ))) return NULL;
path.str = value->data;
path.len = (value->len / sizeof(WCHAR)) * sizeof(WCHAR);
if (path.len <= sizeof(root_name)) return NULL;
if (memicmp_strW( path.str, root_name, sizeof(root_name) )) return NULL;
path.str += ARRAY_SIZE( root_name );
path.len -= sizeof(root_name);
key = root_key;
token.str = NULL;
if (!get_path_token( &path, &token )) return NULL;
while (token.len)
{
if (!(key = find_subkey( key, &token, &index ))) break;
if (!(key = follow_symlink( key, iteration + 1 ))) break;
get_path_token( &path, &token );
}
return key;
}
/* open a key until we find an element that doesn't exist */
/* helper for open_key and create_key */
static struct key *open_key_prefix( struct key *key, const struct unicode_str *name,
unsigned int access, struct unicode_str *token, int *index )
{
token->str = NULL;
if (!get_path_token( name, token )) return NULL;
if (access & KEY_WOW64_32KEY) key = find_wow64_subkey( key, token );
while (token->len)
{
struct key *subkey;
if (!(subkey = find_subkey( key, token, index )))
{
if ((key->flags & KEY_WOWSHARE) && !(access & KEY_WOW64_64KEY))
{
/* try in the 64-bit parent */
key = key->parent;
subkey = find_subkey( key, token, index );
}
}
if (!subkey) break;
key = subkey;
get_path_token( name, token );
if (!token->len) break;
if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, token );
if (!(key = follow_symlink( key, 0 )))
{
set_error( STATUS_OBJECT_NAME_NOT_FOUND );
return NULL;
}
}
return key;
}
/* open a subkey */
static struct key *open_key( struct key *key, const struct unicode_str *name, unsigned int access,
unsigned int attributes )
{
int index;
struct unicode_str token;
if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
if (token.len)
{
set_error( STATUS_OBJECT_NAME_NOT_FOUND );
return NULL;
}
if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
{
set_error( STATUS_OBJECT_NAME_NOT_FOUND );
return NULL;
}
if (debug_level > 1) dump_operation( key, NULL, "Open" );
if (key->flags & KEY_PREDEF) set_error( STATUS_PREDEFINED_HANDLE );
grab_object( key );
return key;
}
/* create a subkey */
static struct key *create_key( struct key *key, const struct unicode_str *name,
const struct unicode_str *class, unsigned int options,
unsigned int access, unsigned int attributes,
const struct security_descriptor *sd, int *created )
{
int index;
struct unicode_str token, next;
*created = 0;
if (!(key = open_key_prefix( key, name, access, &token, &index ))) return NULL;
if (!token.len) /* the key already exists */
{
if (!(access & KEY_WOW64_64KEY)) key = find_wow64_subkey( key, &token );
if (options & REG_OPTION_CREATE_LINK)
{
set_error( STATUS_OBJECT_NAME_COLLISION );
return NULL;
}
if (!(attributes & OBJ_OPENLINK) && !(key = follow_symlink( key, 0 )))
{
set_error( STATUS_OBJECT_NAME_NOT_FOUND );
return NULL;
}
if (debug_level > 1) dump_operation( key, NULL, "Open" );
if (key->flags & KEY_PREDEF) set_error( STATUS_PREDEFINED_HANDLE );
grab_object( key );
return key;
}
/* token must be the last path component at this point */
next = token;
get_path_token( name, &next );
if (next.len)
{
set_error( STATUS_OBJECT_NAME_NOT_FOUND );
return NULL;
}
if ((key->flags & KEY_VOLATILE) && !(options & REG_OPTION_VOLATILE))
{
set_error( STATUS_CHILD_MUST_BE_VOLATILE );
return NULL;
}
*created = 1;
make_dirty( key );
if (!(key = alloc_subkey( key, &token, index, current_time ))) return NULL;
if (options & REG_OPTION_CREATE_LINK) key->flags |= KEY_SYMLINK;
if (options & REG_OPTION_VOLATILE) key->flags |= KEY_VOLATILE;
else key->flags |= KEY_DIRTY;
if (sd) default_set_sd( &key->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
if (debug_level > 1) dump_operation( key, NULL, "Create" );
if (class && class->len)
{
key->classlen = class->len;
free(key->class);
if (!(key->class = memdup( class->str, key->classlen ))) key->classlen = 0;
}
touch_key( key->parent, REG_NOTIFY_CHANGE_NAME );
grab_object( key );
return key;
}
/* recursively create a subkey (for internal use only) */
static struct key *create_key_recursive( struct key *key, const struct unicode_str *name, timeout_t modif )
{
struct key *base;
int index;
struct unicode_str token;
token.str = NULL;
if (!get_path_token( name, &token )) return NULL;
while (token.len)
{
struct key *subkey;
if (!(subkey = find_subkey( key, &token, &index ))) break;
key = subkey;
if (!(key = follow_symlink( key, 0 )))
{
set_error( STATUS_OBJECT_NAME_NOT_FOUND );
return NULL;
}
get_path_token( name, &token );
}
if (token.len)
{
if (!(key = alloc_subkey( key, &token, index, modif ))) return NULL;
base = key;
for (;;)
{
get_path_token( name, &token );
if (!token.len) break;
/* we know the index is always 0 in a new key */
if (!(key = alloc_subkey( key, &token, 0, modif )))
{
free_subkey( base, index );
return NULL;
}
}
}
grab_object( key );
return key;
}
/* query information about a key or a subkey */
static void enum_key( struct key *key, int index, int info_class, struct enum_key_reply *reply )
{
int i;
data_size_t len, namelen, classlen;
data_size_t max_subkey = 0, max_class = 0;
data_size_t max_value = 0, max_data = 0;
WCHAR *fullname = NULL;
char *data;
if (key->flags & KEY_PREDEF)
{
set_error( STATUS_INVALID_HANDLE );
return;
}
if (index != -1) /* -1 means use the specified key directly */
{
if ((index < 0) || (index > key->last_subkey))
{
set_error( STATUS_NO_MORE_ENTRIES );
return;
}
key = key->subkeys[index];
}
namelen = key->namelen;
classlen = key->classlen;
switch(info_class)
{
case KeyNameInformation:
if (!(fullname = key->obj.ops->get_full_name( &key->obj, &namelen ))) return;
/* fall through */
case KeyBasicInformation:
classlen = 0; /* only return the name */
/* fall through */
case KeyNodeInformation:
reply->max_subkey = 0;
reply->max_class = 0;
reply->max_value = 0;
reply->max_data = 0;
break;
case KeyFullInformation:
case KeyCachedInformation:
for (i = 0; i <= key->last_subkey; i++)
{
if (key->subkeys[i]->namelen > max_subkey) max_subkey = key->subkeys[i]->namelen;
if (key->subkeys[i]->classlen > max_class) max_class = key->subkeys[i]->classlen;
}
for (i = 0; i <= key->last_value; i++)
{
if (key->values[i].namelen > max_value) max_value = key->values[i].namelen;
if (key->values[i].len > max_data) max_data = key->values[i].len;
}
reply->max_subkey = max_subkey;
reply->max_class = max_class;
reply->max_value = max_value;
reply->max_data = max_data;
reply->namelen = namelen;
if (info_class == KeyCachedInformation)
classlen = 0; /* don't return any data, only its size */
namelen = 0; /* don't return name */
break;
default:
set_error( STATUS_INVALID_PARAMETER );
return;
}
reply->subkeys = key->last_subkey + 1;
reply->values = key->last_value + 1;
reply->modif = key->modif;
reply->total = namelen + classlen;
len = min( reply->total, get_reply_max_size() );
if (len && (data = set_reply_data_size( len )))
{
if (len > namelen)
{
reply->namelen = namelen;
memcpy( data, key->name, namelen );