aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/fhandler_registry.cc
blob: 6c6702afdee0c6581228e967a7efa28b28b6ffe5 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
/* fhandler_registry.cc: fhandler for /proc/registry virtual filesystem

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

/* FIXME: Access permissions are ignored at the moment.  */

#include "winsup.h"
#include <stdlib.h>
#include "cygerrno.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "child_info.h"

#define _COMPILING_NEWLIB
#include <dirent.h>

/* If this bit is set in __d_position then we are enumerating values,
 * else sub-keys. keeping track of where we are is horribly messy
 * the bottom 16 bits are the absolute position and the top 15 bits
 * make up the value index if we are enuerating values.
 */
static const __int32_t REG_ENUM_VALUES_MASK = 0x8000000;
static const __int32_t REG_POSITION_MASK = 0xffff;

/* These key paths are used below whenever we return key information.
   The problem is UAC virtualization when running an admin account with
   restricted rights.  In that case the subkey "Classes" in the VirtualStore
   points to the HKEY_CLASSES_ROOT key again.  If "Classes" is handled as a
   normal subdirectory, applications recursing throught the directory
   hirarchy will invariably run into an infinite recursion.  What we do here
   is to handle the "Classes" subkey as a symlink to HKEY_CLASSES_ROOT.  This
   avoids the infinite recursion, unless the application blindly follows
   symlinks pointing to directories, in which case it's their own fault. */
#define VIRT_CLASSES_KEY_PREFIX "/VirtualStore/MACHINE/SOFTWARE"
#define VIRT_CLASSES_KEY_SUFFIX "Classes"
#define VIRT_CLASSES_KEY VIRT_CLASSES_KEY_PREFIX "/" VIRT_CLASSES_KEY_SUFFIX
#define VIRT_CLASSES_LINKTGT "/proc/registry/HKEY_CLASSES_ROOT"

/* List of root keys in /proc/registry.
 * Possibly we should filter out those not relevant to the flavour of Windows
 * Cygwin is running on.
 */
static const char *registry_listing[] =
{
  ".",
  "..",
  "HKEY_CLASSES_ROOT",
  "HKEY_CURRENT_CONFIG",
  "HKEY_CURRENT_USER",
  "HKEY_LOCAL_MACHINE",
  "HKEY_USERS",
  "HKEY_PERFORMANCE_DATA",
  NULL
};

static const HKEY registry_keys[] =
{
  (HKEY) INVALID_HANDLE_VALUE,
  (HKEY) INVALID_HANDLE_VALUE,
  HKEY_CLASSES_ROOT,
  HKEY_CURRENT_CONFIG,
  HKEY_CURRENT_USER,
  HKEY_LOCAL_MACHINE,
  HKEY_USERS,
  HKEY_PERFORMANCE_DATA
};

static const int ROOT_KEY_COUNT = sizeof (registry_keys) / sizeof (HKEY);

/* Make sure to access the correct per-user HKCR and HKCU hives, even if
   the current user is only impersonated in another user's session. */
static HKEY
fetch_hkey (int idx) /* idx *must* be valid */
{
  HKEY key;

  if (registry_keys[idx] == HKEY_CLASSES_ROOT)
    {
      if (RegOpenUserClassesRoot (cygheap->user.issetuid ()
				  ? cygheap->user.imp_token () : hProcToken,
				  0, KEY_READ, &key) == ERROR_SUCCESS)
	return key;
    }
  else if (registry_keys[idx] == HKEY_CURRENT_USER)
    {
      if (RegOpenCurrentUser (KEY_READ, &key) == ERROR_SUCCESS)
	return key;
    }
  else if (registry_keys[idx] == HKEY_CURRENT_CONFIG)
    {
      if (RegOpenKeyExW (HKEY_LOCAL_MACHINE,
		       L"System\\CurrentControlSet\\Hardware Profiles\\Current",
		       0, KEY_READ, &key) == ERROR_SUCCESS)
	return key;
    }
  /* Unfortunately there's no way to generate a valid OS registry key for
     the other root keys.  HKEY_USERS and HKEY_LOCAL_MACHINE are file
     handles internally, HKEY_PERFORMANCE_DATA is just a bad hack and
     no registry key at all. */
  return registry_keys[idx];
}

/* These get added to each subdirectory in /proc/registry.
 * If we wanted to implement writing, we could maybe add a '.writable' entry or
 * suchlike.
 */
static const char *special_dot_files[] =
{
  ".",
  "..",
  NULL
};

static const int SPECIAL_DOT_FILE_COUNT =
  (sizeof (special_dot_files) / sizeof (const char *)) - 1;

/* Value names for HKEY_PERFORMANCE_DATA.
 *
 * CAUTION: Never call RegQueryValueEx (HKEY_PERFORMANCE_DATA, "Add", ...).
 * It WRITES data and may destroy the perfc009.dat file.  Same applies to
 * name prefixes "Ad" and "A".
 */
static const char * const perf_data_files[] =
{
  "@",
  "Costly",
  "Global"
};

static const int PERF_DATA_FILE_COUNT =
  sizeof (perf_data_files) / sizeof (perf_data_files[0]);

static HKEY open_key (const char *name, REGSAM access, DWORD wow64, bool isValue);

/* Return true if char must be encoded.
 */
static inline bool
must_encode (wchar_t c)
{
  return (iswdirsep (c) || c == L':' || c == L'%');
}

/* Encode special chars in registry key or value name.
 * Returns 0: success, -1: error.
 */
static int
encode_regname (char *dst, const wchar_t *src, bool add_val)
{
  int di = 0;
  if (!src[0])
    dst[di++] = '@'; // Default value.
  else
    for (int si = 0; src[si]; si++)
      {
	wchar_t c = src[si];
	if (must_encode (c) ||
	    (si == 0 && ((c == L'.'
			  && (!src[1] || (src[1] == L'.' && !src[2])))
			 || (c == L'@' && !src[1]))))
	  {
	    if (di + 3 >= NAME_MAX + 1)
	      return -1;
	    __small_sprintf (dst + di, "%%%02x", c);
	    di += 3;
	  }
	else
	  di += sys_wcstombs (dst + di, NAME_MAX + 1 - di, &c, 1);
      }

  if (add_val)
    {
      if (di + 4 >= NAME_MAX + 1)
	return -1;
      memcpy (dst + di, "%val", 4);
      di += 4;
    }

  dst[di] = 0;
  return 0;
}

/* Decode special chars in registry key or value name.
 * Returns 0: success, 1: "%val" detected, -1: error.
 */
static int
decode_regname (wchar_t *wdst, const char *src, int len = -1)
{
  if (len < 0)
    len = strlen (src);
  char dst[len + 1];
  int res = 0;

  if (len > 4 && !memcmp (src + len - 4, "%val", 4))
    {
      len -= 4;
      res = 1;
    }

  int di = 0;
  if (len == 1 && src[0] == '@')
    ; // Default value.
  else
    for (int si = 0; si < len; si++)
      {
	char c = src[si];
	if (c == '%')
	  {
	    if (si + 2 >= len)
	      return -1;
	    char s[] = {src[si+1], src[si+2], '\0'};
	    char *p;
	    c = strtoul (s, &p, 16);
	    if (!(must_encode ((wchar_t) c) ||
		  (si == 0 && ((c == '.' && (len == 3 || (src[3] == '.' && len == 4))) ||
			       (c == '@' && len == 3)))))
	      return -1;
	    dst[di++] = c;
	    si += 2;
	  }
	else
	  dst[di++] = c;
      }

  dst[di] = 0;
  sys_mbstowcs (wdst, NAME_MAX + 1, dst);
  return res;
}


/* Hash table to limit calls to key_exists ().
 */
class __DIR_hash
{
public:
  __DIR_hash ()
    {
      memset (table, 0, sizeof(table));
    }

  void set (unsigned h)
    {
      table [(h >> 3) & (HASH_SIZE - 1)] |= (1 << (h & 0x3));
    }

  bool is_set (unsigned h) const
    {
      return (table [(h >> 3) & (HASH_SIZE - 1)] & (1 << (h & 0x3))) != 0;
    }

private:
  enum { HASH_SIZE = 1024 };
  unsigned char table[HASH_SIZE];
};

#define d_hash(d)	((__DIR_hash *) (d)->__d_internal)


/* Return true if subkey NAME exists in key PARENT.
 */
static bool
key_exists (HKEY parent, const wchar_t *name, DWORD wow64)
{
  HKEY hKey = (HKEY) INVALID_HANDLE_VALUE;
  LONG error = RegOpenKeyExW (parent, name, 0, KEY_READ | wow64, &hKey);
  if (error == ERROR_SUCCESS)
    RegCloseKey (hKey);

  return (error == ERROR_SUCCESS || error == ERROR_ACCESS_DENIED);
}

static size_t
multi_wcstombs (char *dst, size_t len, const wchar_t *src, size_t nwc)
{
  size_t siz, sum = 0;
  const wchar_t *nsrc;

  while (nwc)
    {
      siz = sys_wcstombs (dst, len, src, nwc) + 1;
      sum += siz;
      if (dst)
	{
	  dst += siz;
	  len -= siz;
	}
      nsrc = wcschr (src, L'\0') + 1;
      if ((size_t) (nsrc - src) >= nwc)
	break;
      nwc -= nsrc - src;
      src = nsrc;
      if (*src == L'\0')
	{
	  if (dst)
	    *dst++ = '\0';
	  ++sum;
	  break;
	}
    }
  return sum;
}

/* Returns 0 if path doesn't exist, >0 if path is a directory,
 * <0 if path is a file.
 *
 * We open the last key but one and then enum it's sub-keys and values to see if the
 * final component is there. This gets round the problem of not having security access
 * to the final key in the path.
 */
virtual_ftype_t
fhandler_registry::exists ()
{
  virtual_ftype_t file_type = virt_none;
  int index = 0, pathlen;
  DWORD buf_size = NAME_MAX + 1;
  LONG error;
  wchar_t buf[buf_size];
  const char *file;
  HKEY hKey = (HKEY) INVALID_HANDLE_VALUE;

  const char *path = get_name ();
  debug_printf ("exists (%s)", path);
  path += proc_len + prefix_len + 1;
  if (*path)
    path++;
  else
    {
      file_type = virt_rootdir;
      goto out;
    }
  pathlen = strlen (path);
  file = path + pathlen - 1;
  if (isdirsep (*file) && pathlen > 1)
    file--;
  while (!isdirsep (*file))
    file--;
  file++;

  if (file == path)
    {
      for (int i = 0; registry_listing[i]; i++)
	if (path_prefix_p (registry_listing[i], path,
			   strlen (registry_listing[i]), true))
	  {
	    file_type = virt_directory;
	    break;
	  }
    }
  else
    {
      wchar_t dec_file[NAME_MAX + 1];

      int val_only = decode_regname (dec_file, file);
      if (val_only < 0)
	goto out;

      if (!val_only)
	hKey = open_key (path, KEY_READ, wow64, false);
      if (hKey != (HKEY) INVALID_HANDLE_VALUE)
	{
	  if (!strcasecmp (path + strlen (path)
			   - sizeof (VIRT_CLASSES_KEY) + 1,
			   VIRT_CLASSES_KEY))
	    file_type = virt_symlink;
	  else
	    file_type = virt_directory;
	}
      else
	{
	  /* Key does not exist or open failed with EACCES,
	     enumerate subkey and value names of parent key.  */
	  hKey = open_key (path, KEY_READ, wow64, true);
	  if (hKey == (HKEY) INVALID_HANDLE_VALUE)
	    return virt_none;

	  if (hKey == HKEY_PERFORMANCE_DATA)
	    {
	      /* RegEnumValue () returns garbage for this key.
		 RegQueryValueEx () returns a PERF_DATA_BLOCK even
		 if a value does not contain any counter objects.
		 So allow access to the generic names and to
		 (blank separated) lists of counter numbers.
		 Never allow access to "Add", see above comment.  */
	      for (int i = 0; i < PERF_DATA_FILE_COUNT
			      && file_type == virt_none; i++)
		{
		  if (strcasematch (perf_data_files[i], file))
		    file_type = virt_file;
		}
	      if (file_type == virt_none && !file[strspn (file, " 0123456789")])
		file_type = virt_file;
	      goto out;
	    }

	  if (!val_only && dec_file[0])
	    {
	      while (ERROR_SUCCESS ==
		     (error = RegEnumKeyExW (hKey, index++, buf, &buf_size,
					     NULL, NULL, NULL, NULL))
		     || (error == ERROR_MORE_DATA))
		{
		  if (!wcscasecmp (buf, dec_file))
		    {
		      file_type = virt_directory;
		      goto out;
		    }
		    buf_size = NAME_MAX + 1;
		}
	      if (error != ERROR_NO_MORE_ITEMS)
		{
		  seterrno_from_win_error (__FILE__, __LINE__, error);
		  goto out;
		}
	      index = 0;
	      buf_size = NAME_MAX + 1;
	    }

	  while (ERROR_SUCCESS ==
		 (error = RegEnumValueW (hKey, index++, buf, &buf_size,
					 NULL, NULL, NULL, NULL))
		 || (error == ERROR_MORE_DATA))
	    {
	      if (!wcscasecmp (buf, dec_file))
		{
		  file_type = virt_file;
		  goto out;
		}
	      buf_size = NAME_MAX + 1;
	    }
	  if (error != ERROR_NO_MORE_ITEMS)
	    {
	      seterrno_from_win_error (__FILE__, __LINE__, error);
	      goto out;
	    }
	}
    }
out:
  if (hKey != (HKEY) INVALID_HANDLE_VALUE)
    RegCloseKey (hKey);
  return file_type;
}

void
fhandler_registry::set_name (path_conv &in_pc)
{
  if (strncasematch (in_pc.get_posix (), "/proc/registry32", 16))
    {
      wow64 = KEY_WOW64_32KEY;
      prefix_len += 2;
    }
  else if (strncasematch (in_pc.get_posix (), "/proc/registry64", 16))
    {
      wow64 = KEY_WOW64_64KEY;
      prefix_len += 2;
    }
  fhandler_base::set_name (in_pc);
}

fhandler_registry::fhandler_registry ():
fhandler_proc ()
{
  wow64 = 0;
  prefix_len = sizeof ("registry") - 1;
}

int __reg2
fhandler_registry::fstat (struct stat *buf)
{
  fhandler_base::fstat (buf);
  buf->st_mode &= ~_IFMT & NO_W;
  virtual_ftype_t file_type = exists ();
  switch (file_type)
    {
    case virt_none:
      set_errno (ENOENT);
      return -1;
    case virt_symlink:
      buf->st_mode |= S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
      break;
    case virt_directory:
      buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
      break;
    case virt_rootdir:
      buf->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
      buf->st_nlink = ROOT_KEY_COUNT;
      break;
    default:
    case virt_file:
      buf->st_mode |= S_IFREG;
      buf->st_mode &= NO_X;
      break;
    }
  if (file_type != virt_none && file_type != virt_rootdir)
    {
      HKEY hKey;
      const char *path = get_name () + proc_len + prefix_len + 2;
      hKey =
	open_key (path, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE, wow64,
		  (file_type < virt_none) ? true : false);

      if (hKey == HKEY_PERFORMANCE_DATA)
	/* RegQueryInfoKey () always returns write time 0,
	   RegQueryValueEx () does not return required buffer size.  */
	;
      else if (hKey != (HKEY) INVALID_HANDLE_VALUE)
	{
	  FILETIME ftLastWriteTime;
	  DWORD subkey_count;
	  if (ERROR_SUCCESS ==
	      RegQueryInfoKeyW (hKey, NULL, NULL, NULL, &subkey_count, NULL,
				NULL, NULL, NULL, NULL, NULL, &ftLastWriteTime))
	    {
	      to_timestruc_t ((PLARGE_INTEGER) &ftLastWriteTime, &buf->st_mtim);
	      buf->st_ctim = buf->st_birthtim = buf->st_mtim;
	      time_as_timestruc_t (&buf->st_atim);
	      if (file_type > virt_none)
		buf->st_nlink = subkey_count + 2;
	      else
		{
		  int pathlen = strlen (path);
		  const char *value_name = path + pathlen - 1;
		  if (isdirsep (*value_name) && pathlen > 1)
		    value_name--;
		  while (!isdirsep (*value_name))
		    value_name--;
		  value_name++;
		  wchar_t dec_value_name[NAME_MAX + 1];
		  DWORD dwSize = 0;
		  DWORD type;
		  if (decode_regname (dec_value_name, value_name) >= 0
		      && RegQueryValueExW (hKey, dec_value_name, NULL, &type,
					   NULL, &dwSize) == ERROR_SUCCESS
		      && (type == REG_SZ || type == REG_EXPAND_SZ
			  || type == REG_MULTI_SZ || type == REG_LINK))
		    {
		      PBYTE tmpbuf = (PBYTE) malloc (dwSize);
		      if (!tmpbuf
			  || RegQueryValueExW (hKey, dec_value_name,
					       NULL, NULL, tmpbuf, &dwSize)
			     != ERROR_SUCCESS)
			buf->st_size = dwSize / sizeof (wchar_t);
		      else if (type == REG_MULTI_SZ)
			buf->st_size = multi_wcstombs (NULL, 0,
						     (wchar_t *) tmpbuf,
						     dwSize / sizeof (wchar_t));
		      else
			buf->st_size = sys_wcstombs (NULL, 0,
						     (wchar_t *) tmpbuf,
						     dwSize / sizeof (wchar_t))
				       + 1;
		      if (tmpbuf)
			free (tmpbuf);
		    }
		  else
		    buf->st_size = dwSize;
		}
	      uid_t uid;
	      gid_t gid;
	      if (get_reg_attribute (hKey, &buf->st_mode, &uid, &gid) == 0)
		{
		  buf->st_uid = uid;
		  buf->st_gid = gid;
		  buf->st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
		  if (file_type > virt_none)
		    buf->st_mode |= S_IFDIR;
		  else
		    buf->st_mode &= NO_X;
		}
	    }
	  RegCloseKey (hKey);
	}
      else
	{
	  /* Here's the problem:  If we can't open the key, we don't know
	     nothing at all about the key/value.  It's only clear that
	     the current user has no read access.  At this point it's
	     rather unlikely that the user has write or execute access
	     and it's also rather unlikely that the user is the owner.
	     Therefore it's probably most safe to assume unknown ownership
	     and no permissions for nobody. */
	  buf->st_uid = ILLEGAL_UID;
	  buf->st_gid = ILLEGAL_GID;
	  buf->st_mode &= ~0777;
	}
    }
  return 0;
}

DIR *
fhandler_registry::opendir (int fd)
{
  /* Skip fhandler_proc::opendir, which allocates dir->_d_handle for its
     own devilish purposes... */
  return fhandler_virtual::opendir (fd);
}

int
fhandler_registry::readdir (DIR *dir, dirent *de)
{
  DWORD buf_size = NAME_MAX + 1;
  wchar_t buf[buf_size];
  const char *path = dir->__d_dirname + proc_len + 1 + prefix_len;
  LONG error;
  int res = ENMFILE;

  dir->__flags |= dirent_saw_dot | dirent_saw_dot_dot;
  if (*path == 0)
    {
      if (dir->__d_position >= ROOT_KEY_COUNT)
	goto out;
      strcpy (de->d_name, registry_listing[dir->__d_position++]);
      res = 0;
      goto out;
    }
  if (dir->__handle == INVALID_HANDLE_VALUE)
    {
      if (dir->__d_position != 0)
	goto out;
      dir->__handle = open_key (path + 1, KEY_READ, wow64, false);
      if (dir->__handle == INVALID_HANDLE_VALUE)
	goto out;
      dir->__d_internal = (uintptr_t) new __DIR_hash ();
    }
  if (dir->__d_position < SPECIAL_DOT_FILE_COUNT)
    {
      strcpy (de->d_name, special_dot_files[dir->__d_position++]);
      res = 0;
      goto out;
    }
  if ((HKEY) dir->__handle == HKEY_PERFORMANCE_DATA)
    {
      /* RegEnumValue () returns garbage for this key,
	 simulate only a minimal listing of the generic names.  */
      if (dir->__d_position >= SPECIAL_DOT_FILE_COUNT + PERF_DATA_FILE_COUNT)
	goto out;
      strcpy (de->d_name, perf_data_files[dir->__d_position - SPECIAL_DOT_FILE_COUNT]);
      dir->__d_position++;
      res = 0;
      goto out;
    }

retry:
  if (dir->__d_position & REG_ENUM_VALUES_MASK)
    /* For the moment, the type of key is ignored here. when write access is added,
     * maybe add an extension for the type of each value?
     */
    error = RegEnumValueW ((HKEY) dir->__handle,
			   (dir->__d_position & ~REG_ENUM_VALUES_MASK) >> 16,
			   buf, &buf_size, NULL, NULL, NULL, NULL);
  else
    error =
      RegEnumKeyExW ((HKEY) dir->__handle, dir->__d_position -
		     SPECIAL_DOT_FILE_COUNT, buf, &buf_size,
		     NULL, NULL, NULL, NULL);
  if (error == ERROR_NO_MORE_ITEMS
      && (dir->__d_position & REG_ENUM_VALUES_MASK) == 0)
    {
      /* If we're finished with sub-keys, start on values under this key.  */
      dir->__d_position |= REG_ENUM_VALUES_MASK;
      buf_size = NAME_MAX + 1;
      goto retry;
    }
  if (error != ERROR_SUCCESS && error != ERROR_MORE_DATA)
    {
      delete d_hash (dir);
      RegCloseKey ((HKEY) dir->__handle);
      dir->__handle = INVALID_HANDLE_VALUE;
      if (error != ERROR_NO_MORE_ITEMS)
	seterrno_from_win_error (__FILE__, __LINE__, error);
      goto out;
    }

  /* We get here if `buf' contains valid data.  */
  dir->__d_position++;
  if (dir->__d_position & REG_ENUM_VALUES_MASK)
    dir->__d_position += 0x10000;

  {
    /* Append "%val" if value name is identical to a previous key name.  */
    unsigned h = hash_path_name (1, buf);
    bool add_val = false;
    if (! (dir->__d_position & REG_ENUM_VALUES_MASK))
      d_hash (dir)->set (h);
    else if (d_hash (dir)->is_set (h)
	     && key_exists ((HKEY) dir->__handle, buf, wow64))
      add_val = true;

    if (encode_regname (de->d_name, buf, add_val))
      {
	buf_size = NAME_MAX + 1;
	goto retry;
      }
  }

  if (dir->__d_position & REG_ENUM_VALUES_MASK)
    de->d_type = DT_REG;
  else if (!strcasecmp (de->d_name, "Classes")
	   && !strcasecmp (path + strlen (path)
			   - sizeof (VIRT_CLASSES_KEY_PREFIX) + 1,
			   VIRT_CLASSES_KEY_PREFIX))
    de->d_type = DT_LNK;
  else
    de->d_type = DT_DIR;

  res = 0;
out:
  syscall_printf ("%d = readdir(%p, %p)", res, dir, de);
  return res;
}

long
fhandler_registry::telldir (DIR * dir)
{
  return dir->__d_position & REG_POSITION_MASK;
}

void
fhandler_registry::seekdir (DIR * dir, long loc)
{
  /* Unfortunately cannot simply set __d_position due to transition from sub-keys to
   * values.
   */
  rewinddir (dir);
  while (loc > (dir->__d_position & REG_POSITION_MASK))
    if (readdir (dir, dir->__d_dirent))
      break;
}

void
fhandler_registry::rewinddir (DIR * dir)
{
  if (dir->__handle != INVALID_HANDLE_VALUE)
    {
      delete d_hash (dir);
      RegCloseKey ((HKEY) dir->__handle);
      dir->__handle = INVALID_HANDLE_VALUE;
    }
  dir->__d_position = 0;
  dir->__flags = dirent_saw_dot | dirent_saw_dot_dot;
}

int
fhandler_registry::closedir (DIR * dir)
{
  int res = 0;
  if (dir->__handle != INVALID_HANDLE_VALUE)
    {
      delete d_hash (dir);
      if (RegCloseKey ((HKEY) dir->__handle) != ERROR_SUCCESS)
	{
	  __seterrno ();
	  res = -1;
	}
    }
  syscall_printf ("%d = closedir(%p)", res, dir);
  return 0;
}

int
fhandler_registry::open (int flags, mode_t mode)
{
  int pathlen;
  const char *file;
  HKEY handle = (HKEY) INVALID_HANDLE_VALUE;

  int res = fhandler_virtual::open (flags, mode);
  if (!res)
    goto out;

  const char *path;
  path = get_name () + proc_len + 1 + prefix_len;
  if (!*path)
    {
      if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
	{
	  set_errno (EEXIST);
	  res = 0;
	  goto out;
	}
      else if (flags & O_WRONLY)
	{
	  set_errno (EISDIR);
	  res = 0;
	  goto out;
	}
      else
	{
	  flags |= O_DIROPEN;
	  /* Marking as nohandle allows to call dup. */
	  nohandle (true);
	  goto success;
	}
    }
  path++;
  pathlen = strlen (path);
  file = path + pathlen - 1;
  if (isdirsep (*file) && pathlen > 1)
    file--;
  while (!isdirsep (*file))
    file--;
  file++;

  if (file == path)
    {
      for (int i = 0; registry_listing[i]; i++)
	if (path_prefix_p (registry_listing[i], path,
			   strlen (registry_listing[i]), true))
	  {
	    if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
	      {
		set_errno (EEXIST);
		res = 0;
		goto out;
	      }
	    else if (flags & O_WRONLY)
	      {
		set_errno (EISDIR);
		res = 0;
		goto out;
	      }
	    else
	      {
		set_io_handle (fetch_hkey (i));
		/* Marking as nohandle allows to call dup on pseudo registry
		   handles. */
		if (get_handle () >= HKEY_CLASSES_ROOT)
		  nohandle (true);
		flags |= O_DIROPEN;
		goto success;
	      }
	  }

      if (flags & O_CREAT)
	{
	  set_errno (EROFS);
	  res = 0;
	}
      else
	{
	  set_errno (ENOENT);
	  res = 0;
	}
      goto out;
    }

  if (flags & O_WRONLY)
    {
      set_errno (EROFS);
      res = 0;
      goto out;
    }
  else
    {
      wchar_t dec_file[NAME_MAX + 1];
      int val_only = decode_regname (dec_file, file);
      if (val_only < 0)
	{
	  set_errno (EINVAL);
	  res = 0;
	  goto out;
	}

      if (!val_only)
	handle = open_key (path, KEY_READ, wow64, false);
      if (handle == (HKEY) INVALID_HANDLE_VALUE)
	{
	  if (val_only || get_errno () != EACCES)
	    handle = open_key (path, KEY_READ, wow64, true);
	  if (handle == (HKEY) INVALID_HANDLE_VALUE)
	    {
	      res = 0;
	      goto out;
	    }
	}
      else
	flags |= O_DIROPEN;

      set_io_handle (handle);
      set_close_on_exec (!!(flags & O_CLOEXEC));
      value_name = cwcsdup (dec_file);

      if (!(flags & O_DIROPEN) && !fill_filebuf ())
	{
	  RegCloseKey (handle);
	  res = 0;
	  goto out;
	}

      if (flags & O_APPEND)
	position = filesize;
      else
	position = 0;
  }

success:
  res = 1;
  set_flags ((flags & ~O_TEXT) | O_BINARY);
  set_open_status ();
out:
  syscall_printf ("%d = fhandler_registry::open(%p, 0%o)", res, flags, mode);
  return res;
}

int
fhandler_registry::close ()
{
  int res = fhandler_virtual::close ();
  if (res != 0)
    return res;
  HKEY handle = (HKEY) get_handle ();
  if (handle != (HKEY) INVALID_HANDLE_VALUE && handle < HKEY_CLASSES_ROOT)
    {
      if (RegCloseKey (handle) != ERROR_SUCCESS)
	{
	  __seterrno ();
	  res = -1;
	}
    }
  if (!have_execed && value_name)
    {
      cfree (value_name);
      value_name = NULL;
    }
  return res;
}

bool
fhandler_registry::fill_filebuf ()
{
  DWORD type, size;
  LONG error;
  HKEY handle = (HKEY) get_handle ();
  size_t bufalloc;

  if (handle != HKEY_PERFORMANCE_DATA)
    {
      error = RegQueryValueExW (handle, value_name, NULL, &type, NULL, &size);
      if (error != ERROR_SUCCESS)
	{
	  if (error == ERROR_INVALID_HANDLE
	      && !strcasecmp (get_name () + strlen (get_name ())
			      - sizeof (VIRT_CLASSES_KEY) + 1,
			      VIRT_CLASSES_KEY))
	    {
	      filesize = sizeof (VIRT_CLASSES_LINKTGT);
	      filebuf = (char *) cmalloc_abort (HEAP_BUF, filesize);
	      strcpy (filebuf, VIRT_CLASSES_LINKTGT);
	      return true;
	    }
	  if (error != ERROR_FILE_NOT_FOUND)
	    {
	      seterrno_from_win_error (__FILE__, __LINE__, error);
	      return false;
	    }
	  goto value_not_found;
	}
      PBYTE tmpbuf = (PBYTE) cmalloc_abort (HEAP_BUF, size);
      error =
	RegQueryValueExW (handle, value_name, NULL, NULL, tmpbuf, &size);
      if (error != ERROR_SUCCESS)
	{
	  seterrno_from_win_error (__FILE__, __LINE__, error);
	  return true;
	}
      if (type == REG_SZ || type == REG_EXPAND_SZ || type == REG_LINK)
	bufalloc = sys_wcstombs (NULL, 0, (wchar_t *) tmpbuf,
				 size / sizeof (wchar_t)) + 1;
      else if (type == REG_MULTI_SZ)
	bufalloc = multi_wcstombs (NULL, 0, (wchar_t *) tmpbuf,
				   size / sizeof (wchar_t));
      else
	bufalloc = size;
      filebuf = (char *) cmalloc_abort (HEAP_BUF, bufalloc);
      if (type == REG_SZ || type == REG_EXPAND_SZ || type == REG_LINK)
	sys_wcstombs (filebuf, bufalloc, (wchar_t *) tmpbuf,
		      size / sizeof (wchar_t));
      else if (type == REG_MULTI_SZ)
	multi_wcstombs (filebuf, bufalloc, (wchar_t *) tmpbuf,
			size / sizeof (wchar_t));
      else
	memcpy (filebuf, tmpbuf, bufalloc);
      filesize = bufalloc;
    }
  else
    {
      bufalloc = 0;
      do
	{
	  bufalloc += 16 * 1024;
	  filebuf = (char *) crealloc_abort (filebuf, bufalloc);
	  size = bufalloc;
	  error = RegQueryValueExW (handle, value_name, NULL, &type,
				    (PBYTE) filebuf, &size);
	  if (error != ERROR_SUCCESS && error != ERROR_MORE_DATA)
	    {
	      seterrno_from_win_error (__FILE__, __LINE__, error);
	      return false;
	    }
	}
      while (error == ERROR_MORE_DATA);
      filesize = size;
      /* RegQueryValueEx () opens HKEY_PERFORMANCE_DATA.  */
      RegCloseKey (handle);
    }
  return true;
value_not_found:
  DWORD buf_size = NAME_MAX + 1;
  wchar_t buf[buf_size];
  int index = 0;
  while (ERROR_SUCCESS ==
	 (error = RegEnumKeyExW (handle, index++, buf, &buf_size, NULL, NULL,
				 NULL, NULL)) || (error == ERROR_MORE_DATA))
    {
      if (!wcscasecmp (buf, value_name))
	{
	  set_errno (EISDIR);
	  return false;
	}
      buf_size = NAME_MAX + 1;
    }
  if (error != ERROR_NO_MORE_ITEMS)
    {
      seterrno_from_win_error (__FILE__, __LINE__, error);
      return false;
    }
  set_errno (ENOENT);
  return false;
}

/* Auxillary member function to open registry keys.  */
static HKEY
open_key (const char *name, REGSAM access, DWORD wow64, bool isValue)
{
  HKEY hKey = (HKEY) INVALID_HANDLE_VALUE;
  HKEY hParentKey = (HKEY) INVALID_HANDLE_VALUE;
  bool parentOpened = false;
  wchar_t component[NAME_MAX + 1];

  while (*name)
    {
      const char *anchor = name;
      while (*name && !isdirsep (*name))
	name++;
      int val_only = decode_regname (component, anchor, name - anchor);
      if (val_only < 0)
	{
	  set_errno (EINVAL);
	  if (parentOpened)
	    RegCloseKey (hParentKey);
	  hKey = (HKEY) INVALID_HANDLE_VALUE;
	  break;
	}
      if (*name)
	name++;
      if (*name == 0 && isValue == true)
	break;

      if (val_only || !component[0] || hKey == HKEY_PERFORMANCE_DATA)
	{
	  set_errno (ENOENT);
	  if (parentOpened)
	    RegCloseKey (hParentKey);
	  hKey = (HKEY) INVALID_HANDLE_VALUE;
	  break;
	}

      if (hParentKey != (HKEY) INVALID_HANDLE_VALUE)
	{
	  REGSAM effective_access = KEY_READ;
	  if ((strchr (name, '/') == NULL && isValue == true) || *name == 0)
	    effective_access = access;
	  LONG error = RegOpenKeyExW (hParentKey, component, 0,
				      effective_access | wow64, &hKey);
	  if (error == ERROR_ACCESS_DENIED) /* Try opening with backup intent */
	    error = RegCreateKeyExW (hParentKey, component, 0, NULL,
				     REG_OPTION_BACKUP_RESTORE,
				     effective_access | wow64, NULL,
				     &hKey, NULL);
	  if (parentOpened)
	    RegCloseKey (hParentKey);
	  if (error != ERROR_SUCCESS)
	    {
	      hKey = (HKEY) INVALID_HANDLE_VALUE;
	      seterrno_from_win_error (__FILE__, __LINE__, error);
	      return hKey;
	    }
	  hParentKey = hKey;
	  parentOpened = true;
	}
      else
	{
	  for (int i = 0; registry_listing[i]; i++)
	    if (strncasematch (anchor, registry_listing[i], name - anchor - 1))
	      hKey = fetch_hkey (i);
	  if (hKey == (HKEY) INVALID_HANDLE_VALUE)
	    return hKey;
	  hParentKey = hKey;
	}
    }
  return hKey;
}

int
fhandler_registry::dup (fhandler_base *child, int flags)
{
  debug_printf ("here");
  fhandler_registry *fhs = (fhandler_registry *) child;

  int ret = fhandler_virtual::dup (fhs, flags);
  /* Pseudo registry handles can't be duplicated using DuplicateHandle.
     Therefore those fhandlers are marked with the nohandle flag.  This
     allows fhandler_base::dup to succeed as usual for nohandle fhandlers.
     Here we just have to fix up by copying the pseudo handle value. */
  if ((HKEY) get_handle () >= HKEY_CLASSES_ROOT)
    fhs->set_io_handle (get_handle ());
  if (value_name)
    fhs->value_name = cwcsdup (value_name);
  return ret;
}