aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/uinfo.cc
blob: bfcce00da03e4c96630053a0eb206bcb17fd7615 (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
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
/* uinfo.cc: user info (uid, gid, etc...)

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. */

#include "winsup.h"
#include <iptypes.h>
#include <lm.h>
#include <ntsecapi.h>
#include <wininet.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <sys/cygwin.h>
#include "cygerrno.h"
#include "pinfo.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "shared_info.h"
#include "registry.h"
#include "child_info.h"
#include "environ.h"
#include "tls_pbuf.h"
#include "miscfuncs.h"
#include "ntdll.h"
#include "ldap.h"
#include "cygserver_pwdgrp.h"

/* Initialize the part of cygheap_user that does not depend on files.
   The information is used in shared.cc to create the shared user_info
   region.  Final initialization occurs in uinfo_init */
void
cygheap_user::init ()
{
  WCHAR user_name[UNLEN + 1];
  DWORD user_name_len = UNLEN + 1;

  /* This method is only called if a Cygwin process gets started by a
     native Win32 process.  Try to get the username from the environment,
     first USERNAME (Win32), then USER (POSIX).  If that fails (which is
     very unlikely), it only has an impact if we don't have an entry in
     /etc/passwd for this user either.  In that case the username sticks
     to "unknown".  Since this is called early in initialization, and
     since we don't want to pull in a dependency to any other DLL except
     ntdll and kernel32 at this early stage, don't call GetUserName,
     GetUserNameEx, NetWkstaUserGetInfo, etc. */
  if (GetEnvironmentVariableW (L"USERNAME", user_name, user_name_len)
      || GetEnvironmentVariableW (L"USER", user_name, user_name_len))
    {
      char mb_user_name[user_name_len = sys_wcstombs (NULL, 0, user_name) + 1];
      sys_wcstombs (mb_user_name, user_name_len, user_name);
      set_name (mb_user_name);
    }
  else
    set_name ("unknown");

  NTSTATUS status;
  ULONG size;
  PSECURITY_DESCRIPTOR psd;

  status = NtQueryInformationToken (hProcToken, TokenPrimaryGroup,
				    &groups.pgsid, sizeof (cygsid), &size);
  if (!NT_SUCCESS (status))
    system_printf ("NtQueryInformationToken (TokenPrimaryGroup), %y", status);

  /* Get the SID from current process and store it in effec_cygsid */
  status = NtQueryInformationToken (hProcToken, TokenUser, &effec_cygsid,
				    sizeof (cygsid), &size);
  if (!NT_SUCCESS (status))
    {
      system_printf ("NtQueryInformationToken (TokenUser), %y", status);
      return;
    }

  /* Set token owner to the same value as token user */
  status = NtSetInformationToken (hProcToken, TokenOwner, &effec_cygsid,
				  sizeof (cygsid));
  if (!NT_SUCCESS (status))
    debug_printf ("NtSetInformationToken (TokenOwner), %y", status);

  /* Standard way to build a security descriptor with the usual DACL */
  PSECURITY_ATTRIBUTES sa_buf = (PSECURITY_ATTRIBUTES) alloca (1024);
  psd = (PSECURITY_DESCRIPTOR)
		(sec_user_nih (sa_buf, sid()))->lpSecurityDescriptor;

  BOOLEAN acl_exists, dummy;
  TOKEN_DEFAULT_DACL dacl;

  status = RtlGetDaclSecurityDescriptor (psd, &acl_exists, &dacl.DefaultDacl,
					 &dummy);
  if (NT_SUCCESS (status) && acl_exists && dacl.DefaultDacl)
    {

      /* Set the default DACL and the process DACL */
      status = NtSetInformationToken (hProcToken, TokenDefaultDacl, &dacl,
				      sizeof (dacl));
      if (!NT_SUCCESS (status))
	system_printf ("NtSetInformationToken (TokenDefaultDacl), %y", status);
      if ((status = NtSetSecurityObject (NtCurrentProcess (),
					 DACL_SECURITY_INFORMATION, psd)))
	system_printf ("NtSetSecurityObject, %y", status);
    }
  else
    system_printf("Cannot get dacl, %E");
}

/* Check if sid is an enabled SID in the token group list of the current
   effective token.  Note that we only check for ENABLED groups, not for
   INTEGRITY_ENABLED.  The latter just doesn't make sense in our scenario
   of using the group as primary group.

   This needs careful checking should we use check_token_membership in other
   circumstances. */
bool
check_token_membership (PSID sid)
{
  NTSTATUS status;
  ULONG size;
  tmp_pathbuf tp;
  PTOKEN_GROUPS groups = (PTOKEN_GROUPS) tp.w_get ();

  /* If impersonated, use impersonation token. */
  HANDLE tok = cygheap->user.issetuid () ? cygheap->user.primary_token ()
					 : hProcToken;
  status = NtQueryInformationToken (tok, TokenGroups, groups, 2 * NT_MAX_PATH,
				    &size);
  if (!NT_SUCCESS (status))
    debug_printf ("NtQueryInformationToken (TokenGroups) %y", status);
  else
    {
      for (DWORD pg = 0; pg < groups->GroupCount; ++pg)
	if (RtlEqualSid (sid, groups->Groups[pg].Sid)
	    && (groups->Groups[pg].Attributes & SE_GROUP_ENABLED))
	  return true;
    }
  return false;
}

static void
internal_getlogin (cygheap_user &user)
{
  struct passwd *pwd;
  struct group *pgrp, *grp, *grp2;
  cyg_ldap cldap;

  /* Fetch (and potentially generate) passwd and group entries for the user
     and the primary group in the token. */
  pwd = internal_getpwsid (user.sid (), &cldap);
  pgrp = internal_getgrsid (user.groups.pgsid, &cldap);
  if (!cygheap->pg.nss_cygserver_caching ())
    internal_getgroups (0, NULL, &cldap);
  if (!pwd)
    debug_printf ("user not found in passwd DB");
  else
    {
      cygsid gsid;

      user.set_name (pwd->pw_name);
      myself->uid = pwd->pw_uid;
      myself->gid = pwd->pw_gid;
      /* If the primary group in the passwd DB is different from the primary
	 group in the user token, we have to find the SID of that group and
	 try to override the token primary group. */
      if (!pgrp || myself->gid != pgrp->gr_gid)
	{
	  if (gsid.getfromgr (grp = internal_getgrgid (pwd->pw_gid, &cldap)))
	    {
	      /* We might have a group file with a group entry for the current
		 user's primary group, but the current user has no entry in
		 passwd.  If so, pw_gid is taken from windows and might
		 disagree with gr_gid from the group file.  Overwrite it. */
	      if ((grp2 = internal_getgrsid (gsid, &cldap)) && grp2 != grp)
		myself->gid = pwd->pw_gid = grp2->gr_gid;
	      /* Set primary group to the group in /etc/passwd, *iff*
		 the group in /etc/passwd is in the token *and* enabled. */
	      if (gsid != user.groups.pgsid)
		{
		  NTSTATUS status = STATUS_INVALID_PARAMETER;

		  if (check_token_membership (gsid))
		    {
		      status = NtSetInformationToken (hProcToken,
						      TokenPrimaryGroup,
						      &gsid, sizeof gsid);
		      if (!NT_SUCCESS (status))
			debug_printf ("NtSetInformationToken "
				      "(TokenPrimaryGroup), %y", status);
		    }
		  if (!NT_SUCCESS (status))
		    {
		      /* Revert the primary group setting and override the
			 setting in the passwd entry. */
		      if (pgrp)
			myself->gid = pwd->pw_gid = pgrp->gr_gid;
		    }
		  else
		    user.groups.pgsid = gsid;
		  clear_procimptoken ();
		}
	    }
	  else
	    debug_printf ("group not found in group DB");
	}
    }
  cygheap->user.ontherange (CH_HOME, pwd);
}

void
uinfo_init ()
{
  if (child_proc_info && !cygheap->user.has_impersonation_tokens ())
    return;

  if (!child_proc_info)
    internal_getlogin (cygheap->user); /* Set cygheap->user. */
  /* Conditions must match those in spawn to allow starting child
     processes with ruid != euid and rgid != egid. */
  else if (cygheap->user.issetuid ()
	   && cygheap->user.saved_uid == cygheap->user.real_uid
	   && cygheap->user.saved_gid == cygheap->user.real_gid
	   && !cygheap->user.groups.issetgroups ()
	   && !cygheap->user.setuid_to_restricted)
    {
      cygheap->user.reimpersonate ();
      return;
    }
  else
    cygheap->user.close_impersonation_tokens ();

  cygheap->user.saved_uid = cygheap->user.real_uid = myself->uid;
  cygheap->user.saved_gid = cygheap->user.real_gid = myself->gid;
  cygheap->user.external_token = NO_IMPERSONATION;
  cygheap->user.internal_token = NO_IMPERSONATION;
  cygheap->user.curr_primary_token = NO_IMPERSONATION;
  cygheap->user.curr_imp_token = NO_IMPERSONATION;
  cygheap->user.ext_token_is_restricted = false;
  cygheap->user.curr_token_is_restricted = false;
  cygheap->user.setuid_to_restricted = false;
  cygheap->user.set_saved_sid ();	/* Update the original sid */
  cygheap->user.deimpersonate ();
}

extern "C" int
getlogin_r (char *name, size_t namesize)
{
  const char *login = cygheap->user.name ();
  size_t len = strlen (login) + 1;
  if (len > namesize)
    return ERANGE;
  __try
    {
      strncpy (name, login, len);
    }
  __except (NO_ERROR)
    {
      return EFAULT;
    }
  __endtry
  return 0;
}

extern "C" char *
getlogin (void)
{
  static char username[UNLEN];
  int ret = getlogin_r (username, UNLEN);
  if (ret)
    {
      set_errno (ret);
      return NULL;
    }
  return username;
}

extern "C" uid_t
getuid32 (void)
{
  return cygheap->user.real_uid;
}

#ifdef __i386__
extern "C" __uid16_t
getuid (void)
{
  return cygheap->user.real_uid;
}
#else
EXPORT_ALIAS (getuid32, getuid)
#endif

extern "C" gid_t
getgid32 (void)
{
  return cygheap->user.real_gid;
}

#ifdef __i386__
extern "C" __gid16_t
getgid (void)
{
  return cygheap->user.real_gid;
}
#else
EXPORT_ALIAS (getgid32, getgid)
#endif

extern "C" uid_t
geteuid32 (void)
{
  return myself->uid;
}

#ifdef __i386__
extern "C" uid_t
geteuid (void)
{
  return myself->uid;
}
#else
EXPORT_ALIAS (geteuid32, geteuid)
#endif

extern "C" gid_t
getegid32 (void)
{
  return myself->gid;
}

#ifdef __i386__
extern "C" __gid16_t
getegid (void)
{
  return myself->gid;
}
#else
EXPORT_ALIAS (getegid32, getegid)
#endif

/* Not quite right - cuserid can change, getlogin can't */
extern "C" char *
cuserid (char *src)
{
  if (!src)
    return getlogin ();

  strcpy (src, getlogin ());
  return src;
}

const char *
cygheap_user::ontherange (homebodies what, struct passwd *pw)
{
  char homedrive_env_buf[3];
  char *newhomedrive = NULL;
  char *newhomepath = NULL;
  tmp_pathbuf tp;

  debug_printf ("what %d, pw %p", what, pw);
  if (what == CH_HOME)
    {
      char *p;

      if ((p = getenv ("HOME")))
	debug_printf ("HOME is already in the environment %s", p);
      if (!p)
	{
	  if (pw && pw->pw_dir && *pw->pw_dir)
	    {
	      debug_printf ("Set HOME (from account db) to %s", pw->pw_dir);
	      setenv ("HOME", pw->pw_dir, 1);
	    }
	  else
	    {
	      char home[strlen (name ()) + 8];

	      debug_printf ("Set HOME to default /home/USER");
	      __small_sprintf (home, "/home/%s", name ());
	      setenv ("HOME", home, 1);
	    }
	}
    }

  if (what != CH_HOME && homepath == NULL)
    {
      WCHAR wuser[UNLEN + 1];
      WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
      PUSER_INFO_3 ui = NULL;
      char *homepath_env_buf = tp.c_get ();
      WCHAR profile[MAX_PATH];
      WCHAR win_id[UNLEN + 1]; /* Large enough for SID */

      homepath_env_buf[0] = homepath_env_buf[1] = '\0';
      /* Use Cygwin home as HOMEDRIVE/HOMEPATH in the first place.  This
	 should cover it completely, in theory.  Still, it might be the
	 wrong choice in the long run, which might demand to set HOMEDRIVE/
	 HOMEPATH to the correct values per Windows.  Keep the entire rest
	 of the code mainly for this reason, so switching is easy. */
      pw = internal_getpwsid (sid ());
      if (pw && pw->pw_dir && *pw->pw_dir)
	cygwin_conv_path (CCP_POSIX_TO_WIN_A, pw->pw_dir, homepath_env_buf,
			  NT_MAX_PATH);
      /* First fallback: Windows path per Windows user DB. */
      else if (logsrv ())
	{
	  sys_mbstowcs (wlogsrv, sizeof (wlogsrv) / sizeof (*wlogsrv),
			logsrv ());
	  sys_mbstowcs (wuser, sizeof wuser / sizeof *wuser, winname ());
	  if (NetUserGetInfo (wlogsrv, wuser, 3, (LPBYTE *) &ui)
	      == NERR_Success)
	    {
	      if (ui->usri3_home_dir_drive && *ui->usri3_home_dir_drive)
		{
		  sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
				ui->usri3_home_dir_drive);
		  strcat (homepath_env_buf, "\\");
		}
	      else if (ui->usri3_home_dir && *ui->usri3_home_dir)
		sys_wcstombs (homepath_env_buf, NT_MAX_PATH,
			      ui->usri3_home_dir);
	    }
	  if (ui)
	    NetApiBufferFree (ui);
	}
      /* Second fallback: Windows profile dir. */
      if (!homepath_env_buf[0]
	  && get_user_profile_directory (get_windows_id (win_id),
					 profile, MAX_PATH))
	sys_wcstombs (homepath_env_buf, NT_MAX_PATH, profile);
      /* Last fallback: Cygwin root dir. */
      if (!homepath_env_buf[0])
	cygwin_conv_path (CCP_POSIX_TO_WIN_A | CCP_ABSOLUTE,
			  "/", homepath_env_buf, NT_MAX_PATH);

      if (homepath_env_buf[1] != ':')
	{
	  newhomedrive = almost_null;
	  newhomepath = homepath_env_buf;
	}
      else
	{
	  homedrive_env_buf[0] = homepath_env_buf[0];
	  homedrive_env_buf[1] = homepath_env_buf[1];
	  homedrive_env_buf[2] = '\0';
	  newhomedrive = homedrive_env_buf;
	  newhomepath = homepath_env_buf + 2;
	}
    }

  if (newhomedrive && newhomedrive != homedrive)
    cfree_and_set (homedrive, (newhomedrive == almost_null)
			      ? almost_null : cstrdup (newhomedrive));

  if (newhomepath && newhomepath != homepath)
    cfree_and_set (homepath, cstrdup (newhomepath));

  switch (what)
    {
    case CH_HOMEDRIVE:
      return homedrive;
    case CH_HOMEPATH:
      return homepath;
    default:
      return homepath;
    }
}

const char *
cygheap_user::test_uid (char *&what, const char *name, size_t namelen)
{
  if (!what && !issetuid ())
    what = getwinenveq (name, namelen, HEAP_STR);
  return what;
}

const char *
cygheap_user::env_logsrv (const char *name, size_t namelen)
{
  if (test_uid (plogsrv, name, namelen))
    return plogsrv;

  const char *mydomain = domain ();
  const char *myname = winname ();
  if (!mydomain || ascii_strcasematch (myname, "SYSTEM"))
    return almost_null;

  WCHAR wdomain[MAX_DOMAIN_NAME_LEN + 1];
  WCHAR wlogsrv[INTERNET_MAX_HOST_NAME_LENGTH + 3];
  sys_mbstowcs (wdomain, MAX_DOMAIN_NAME_LEN + 1, mydomain);
  cfree_and_set (plogsrv, almost_null);
  if (get_logon_server (wdomain, wlogsrv, DS_IS_FLAT_NAME))
    sys_wcstombs_alloc (&plogsrv, HEAP_STR, wlogsrv);
  return plogsrv;
}

const char *
cygheap_user::env_domain (const char *name, size_t namelen)
{
  if (pwinname && test_uid (pdomain, name, namelen))
    return pdomain;

  DWORD ulen = UNLEN + 1;
  WCHAR username[ulen];
  DWORD dlen = MAX_DOMAIN_NAME_LEN + 1;
  WCHAR userdomain[dlen];
  SID_NAME_USE use;

  cfree_and_set (pwinname, almost_null);
  cfree_and_set (pdomain, almost_null);
  if (!LookupAccountSidW (NULL, sid (), username, &ulen,
			  userdomain, &dlen, &use))
    __seterrno ();
  else
    {
      sys_wcstombs_alloc (&pwinname, HEAP_STR, username);
      sys_wcstombs_alloc (&pdomain, HEAP_STR, userdomain);
    }
  return pdomain;
}

const char *
cygheap_user::env_userprofile (const char *name, size_t namelen)
{
  if (test_uid (puserprof, name, namelen))
    return puserprof;

  /* User profile path is never longer than MAX_PATH. */
  WCHAR profile[MAX_PATH];
  WCHAR win_id[UNLEN + 1]; /* Large enough for SID */

  cfree_and_set (puserprof, almost_null);
  if (get_user_profile_directory (get_windows_id (win_id), profile, MAX_PATH))
    sys_wcstombs_alloc (&puserprof, HEAP_STR, profile);

  return puserprof;
}

const char *
cygheap_user::env_homepath (const char *name, size_t namelen)
{
  return ontherange (CH_HOMEPATH);
}

const char *
cygheap_user::env_homedrive (const char *name, size_t namelen)
{
  return ontherange (CH_HOMEDRIVE);
}

const char *
cygheap_user::env_name (const char *name, size_t namelen)
{
  if (!test_uid (pwinname, name, namelen))
    domain ();
  return pwinname;
}

const char *
cygheap_user::env_systemroot (const char *name, size_t namelen)
{
  if (!psystemroot)
    {
      int size = GetSystemWindowsDirectoryW (NULL, 0);
      if (size > 0)
	{
	  WCHAR wsystemroot[size];
	  size = GetSystemWindowsDirectoryW (wsystemroot, size);
	  if (size > 0)
	    sys_wcstombs_alloc (&psystemroot, HEAP_STR, wsystemroot);
	}
      if (size <= 0)
	debug_printf ("GetSystemWindowsDirectoryW(), %E");
    }
  return psystemroot;
}

char *
pwdgrp::next_str (char c)
{
  char *res = lptr;
  lptr = strchrnul (lptr, c);
  if (*lptr)
    *lptr++ = '\0';
  return res;
}

bool
pwdgrp::next_num (unsigned long& n)
{
  char *p = next_str (':');
  char *cp;
  n = strtoul (p, &cp, 10);
  return p != cp && !*cp;
}

char *
pwdgrp::add_line (char *eptr)
{
  if (eptr)
    {
      if (curr_lines >= max_lines)
	{
	  max_lines += 10;
	  pwdgrp_buf = crealloc_abort (pwdgrp_buf,
				       max_lines * pwdgrp_buf_elem_size);
	}
      lptr = eptr;
      if (!(this->*parse) ())
	return NULL;
      curr_lines++;
    }
  return eptr;
}

void
cygheap_pwdgrp::init ()
{
  pwd_cache.cygserver.init_pwd ();
  pwd_cache.file.init_pwd ();
  pwd_cache.win.init_pwd ();
  grp_cache.cygserver.init_grp ();
  grp_cache.file.init_grp ();
  grp_cache.win.init_grp ();
  /* Default settings:

     passwd: files db
     group:  files db
     db_prefix: auto		DISABLED
     db_separator: +		DISABLED
     db_home: cygwin desc
     db_shell: cygwin desc
     db_gecos: cygwin desc
     db_enum: cache builtin
  */
  pwd_src = (NSS_SRC_FILES | NSS_SRC_DB);
  grp_src = (NSS_SRC_FILES | NSS_SRC_DB);
  prefix = NSS_PFX_AUTO;
  separator[0] = L'+';
  enums = (ENUM_CACHE | ENUM_BUILTIN);
  enum_tdoms = NULL;
  caching = true;	/* INTERNAL ONLY */
}

#define NSS_NCMP(s) (!strncmp(c, (s), sizeof(s)-1))
#define NSS_CMP(s) (!strncmp(c, (s), sizeof(s)-1) \
		    && strchr (" \t", c[sizeof(s)-1]))

/* The /etc/nsswitch.conf file is read exactly once by the root process of a
   process tree.  We can't afford methodical changes during the lifetime of a
   process tree. */
void
cygheap_pwdgrp::nss_init_line (const char *line)
{
  const char *c = line + strspn (line, " \t");
  char *comment = strchr (c, '#');
  if (comment)
    *comment = '\0';
  switch (*c)
    {
    case 'p':
    case 'g':
      {
	uint32_t *src = NULL;
	if (NSS_NCMP ("passwd:"))
	  src = &pwd_src;
	else if (NSS_NCMP ("group:"))
	  src = &grp_src;
	c = strchr (c, ':') + 1;
	if (src)
	  {
	    *src = 0;
	    while (*(c += strspn (c, " \t")))
	      {
		if (NSS_CMP ("files"))
		  {
		    *src |= NSS_SRC_FILES;
		    c += 5;
		  }
		else if (NSS_CMP ("db"))
		  {
		    *src |= NSS_SRC_DB;
		    c += 2;
		  }
		else
		  {
		    c += strcspn (c, " \t");
		    debug_printf ("Invalid nsswitch.conf content: %s", line);
		  }
	      }
	    if (*src == 0)
	      *src = (NSS_SRC_FILES | NSS_SRC_DB);
	  }
      }
      break;
    case 'd':
      if (!NSS_NCMP ("db_"))
	{
	  debug_printf ("Invalid nsswitch.conf content: %s", line);
	  break;
	}
      c += 3;
#if 0 /* Disable setting prefix and separator from nsswitch.conf for now.
	 Remove if nobody complains too loudly. */
      if (NSS_NCMP ("prefix:"))
	{
	  c = strchr (c, ':') + 1;
	  c += strspn (c, " \t");
	  if (NSS_CMP ("auto"))
	    prefix = NSS_AUTO;
	  else if (NSS_CMP ("primary"))
	    prefix = NSS_PRIMARY;
	  else if (NSS_CMP ("always"))
	    prefix = NSS_ALWAYS;
	  else
	    debug_printf ("Invalid nsswitch.conf content: %s", line);
	}
      else if (NSS_NCMP ("separator:"))
	{
	  c = strchr (c, ':') + 1;
	  c += strspn (c, " \t");
	  if ((unsigned char) *c <= 0x7f && *c != ':' && strchr (" \t", c[1]))
	    separator[0] = (unsigned char) *c;
	  else
	    debug_printf ("Invalid nsswitch.conf content: %s", line);
	}
      else
#endif
      if (NSS_NCMP ("enum:"))
	{
	  tmp_pathbuf tp;
	  char *tdoms = tp.c_get ();
	  char *td = tdoms;
	  int new_enums = ENUM_NONE;

	  td[0] = '\0';
	  c = strchr (c, ':') + 1;
	  c += strspn (c, " \t");
	  while (!strchr (" \t", *c))
	    {
	      const char *e = c + strcspn (c, " \t");
	      if (NSS_CMP ("none"))
		new_enums = ENUM_NONE;
	      else if (NSS_CMP ("builtin"))
		new_enums |= ENUM_BUILTIN;
	      else if (NSS_CMP ("cache"))
		new_enums |= ENUM_CACHE;
	      else if (NSS_CMP ("files"))
		new_enums |= ENUM_FILES;
	      else if (NSS_CMP ("local"))
		new_enums |= ENUM_LOCAL;
	      else if (NSS_CMP ("primary"))
		new_enums |= ENUM_PRIMARY;
	      else if (NSS_CMP ("alltrusted"))
		new_enums |= ENUM_TDOMS | ENUM_TDOMS_ALL;
	      else if (NSS_CMP ("all"))
		new_enums |= ENUM_ALL;
	      else
		{
		  td = stpcpy (stpncpy (td, c, e - c), " ");
		  new_enums |= ENUM_TDOMS;
		}
	      c = e;
	      c += strspn (c, " \t");
	    }
	  if ((new_enums & (ENUM_TDOMS | ENUM_TDOMS_ALL)) == ENUM_TDOMS)
	    {
	      if (td > tdoms)
		{
		  PWCHAR spc;
		  sys_mbstowcs_alloc (&enum_tdoms, HEAP_BUF, tdoms);
		  /* Convert string to REG_MULTI_SZ-style. */
		  while ((spc = wcsrchr (enum_tdoms, L' ')))
		    *spc = L'\0';
		}
	      else
		new_enums &= ~(ENUM_TDOMS | ENUM_TDOMS_ALL);
	    }
	  enums = new_enums;
	}
      else
	{
	  nss_scheme_t *scheme = NULL;
	  if (NSS_NCMP ("home:"))
	    scheme = home_scheme;
	  else if (NSS_NCMP ("shell:"))
	    scheme = shell_scheme;
	  else if (NSS_NCMP ("gecos:"))
	    scheme = gecos_scheme;
	  if (scheme)
	    {
	      uint16_t idx = 0;

	      scheme[0].method = scheme[1].method = NSS_SCHEME_FALLBACK;
	      c = strchr (c, ':') + 1;
	      c += strspn (c, " \t");
	      while (*c && idx < NSS_SCHEME_MAX)
		{
		  if (NSS_CMP ("windows"))
		    scheme[idx].method = NSS_SCHEME_WINDOWS;
		  else if (NSS_CMP ("cygwin"))
		    scheme[idx].method = NSS_SCHEME_CYGWIN;
		  else if (NSS_CMP ("unix"))
		    scheme[idx].method = NSS_SCHEME_UNIX;
		  else if (NSS_CMP ("desc"))
		    scheme[idx].method = NSS_SCHEME_DESC;
		  else if (NSS_NCMP ("/"))
		    {
		      const char *e = c + strcspn (c, " \t");
		      scheme[idx].method = NSS_SCHEME_PATH;
		      sys_mbstowcs_alloc (&scheme[idx].attrib, HEAP_STR,
					  c, e - c);
		    }
		  else if (NSS_NCMP ("@") && isalnum ((unsigned) *++c))
		    {
		      const char *e = c + strcspn (c, " \t");
		      scheme[idx].method = NSS_SCHEME_FREEATTR;
		      sys_mbstowcs_alloc (&scheme[idx].attrib, HEAP_STR,
					  c, e - c);
		    }
		  else
		    debug_printf ("Invalid nsswitch.conf content: %s", line);
		  c += strcspn (c, " \t");
		  c += strspn (c, " \t");
		  ++idx;
		}
	      /* If nothing has been set, revert to default. */
	      if (scheme[0].method == NSS_SCHEME_FALLBACK)
		{
		  scheme[0].method = NSS_SCHEME_CYGWIN;
		  scheme[1].method = NSS_SCHEME_DESC;
		}
	    }
	}
      break;
    case '\0':
      break;
    default:
      debug_printf ("Invalid nsswitch.conf content: %s", line);
      break;
    }
}

static char *
fetch_windows_home (cyg_ldap *pldap, PUSER_INFO_3 ui, cygpsid &sid,
		    PCWSTR dnsdomain)
{
  PCWSTR home_from_db = NULL;
  char *home = NULL;

  if (pldap)
    {
      if (pldap->fetch_ad_account (sid, false, dnsdomain))
	{
#if 0
	  /* Disable preferring homeDrive for now.  The drive letter may not
	     be available when it's needed. */
	  home_from_db = pldap->get_string_attribute (L"homeDrive");
	  if (!home_from_db || !*home_from_db)
#endif
	  home_from_db = pldap->get_string_attribute (L"homeDirectory");
	}
    }
  else if (ui)
    {
#if 0
      /* Ditto. */
      if (ui->usri3_home_dir_drive && *ui->usri3_home_dir_drive)
	home_from_db = ui->usri3_home_dir_drive;
      else
#endif
      if (ui->usri3_home_dir && *ui->usri3_home_dir)
	home_from_db = ui->usri3_home_dir;
    }
  if (home_from_db && *home_from_db)
    home = (char *) cygwin_create_path (CCP_WIN_W_TO_POSIX, home_from_db);
  else
    {
      /* The db fields are empty, so we have to evaluate the local profile
	 path, which is the same thing as the default home directory on
	 Windows.  So what we do here is to try to find out if the user
	 already has a profile on this machine.
	 Note that we don't try to generate the profile if it doesn't exist.
	 Think what would happen if we actually have the permissions to do
	 so and call getpwent... in a domain environment.  The problem is,
	 of course, that we can't know the profile path, unless the OS
	 created it.
	 The only reason this could occur is if a user account, which never
	 logged on to the machine before, tries to logon via a Cygwin service
	 like sshd. */
      WCHAR profile[MAX_PATH];
      WCHAR sidstr[128];

      if (get_user_profile_directory (sid.string (sidstr), profile, MAX_PATH))
	home = (char *) cygwin_create_path (CCP_WIN_W_TO_POSIX, profile);
    }
  return home;
}

/* Local SAM accounts have only a handful attributes available to home users.
   Therefore, allow to fetch additional passwd/group attributes from the
   "Comment" field in XML short style.  For symmetry, this is also allowed
   from the equivalent "description" AD attribute. */
static char *
fetch_from_description (PCWSTR desc, PCWSTR search, size_t len)
{
  PWCHAR s, e;
  char *ret = NULL;

  if ((s = wcsstr (desc, L"<cygwin ")) && (e = wcsstr (s + 8, L"/>")))
    {
      s += 8;
      while (s && s < e)
	{
	  while (*s == L' ')
	    ++s;
	  if (!wcsncmp (s, search, len)) /* Found what we're searching? */
	    {
	      s += len;
	      if ((e = wcschr (s, L'"')))
		{
		  sys_wcstombs_alloc (&ret, HEAP_NOTHEAP, s, e - s);
		  s = e + 1;
		}
	      break;
	    }
	  else /* Skip the current foo="bar" string. */
	    if ((s = wcschr (s, L'"')) && (s = wcschr (s + 1, L'"')))
	      ++s;
	}
    }
  return ret;
}

static char *
fetch_from_path (cyg_ldap *pldap, PUSER_INFO_3 ui, cygpsid &sid, PCWSTR str,
		 PCWSTR dom, PCWSTR dnsdomain, PCWSTR name, bool full_qualified)
{
  tmp_pathbuf tp;
  PWCHAR wpath = tp.w_get ();
  PWCHAR w = wpath;
  PWCHAR we = wpath + NT_MAX_PATH - 1;
  char *home;
  char *ret = NULL;

  while (*str && w < we)
    {
      if (*str != L'%')
	*w++ = *str++;
      else
	{
	  switch (*++str)
	    {
	    case L'u':
	      if (full_qualified)
		{
		  w = wcpncpy (w, dom, we - w);
		  if (w < we)
		    *w++ = cygheap->pg.nss_separator ()[0];
		}
	      w = wcpncpy (w, name, we - w);
	      break;
	    case L'U':
	      w = wcpncpy (w, name, we - w);
	      break;
	    case L'D':
	      w = wcpncpy (w, dom, we - w);
	      break;
	    case L'H':
	      home = fetch_windows_home (pldap, ui, sid, dnsdomain);
	      if (home)
		{
		  /* Drop one leading slash to accommodate home being an
		     absolute path.  We don't check for broken usage of
		     %H here, of course. */
		  if (w > wpath && w[-1] == L'/')
		    --w;
		  w += sys_mbstowcs (w, we - w, home) - 1;
		  free (home);
		}
	      break;
	    case L'_':
	      *w++ = L' ';
	      break;
	    default:
	      *w++ = *str;
	      break;
	    }
	  ++str;
	}
    }
  *w = L'\0';
  sys_wcstombs_alloc (&ret, HEAP_NOTHEAP, wpath);
  return ret;
}

char *
cygheap_pwdgrp::get_home (cyg_ldap *pldap, cygpsid &sid, PCWSTR dom,
			  PCWSTR dnsdomain, PCWSTR name, bool full_qualified)
{
  PWCHAR val;
  char *home = NULL;

  for (uint16_t idx = 0; !home && idx < NSS_SCHEME_MAX; ++idx)
    {
      switch (home_scheme[idx].method)
	{
	case NSS_SCHEME_FALLBACK:
	  return NULL;
	case NSS_SCHEME_WINDOWS:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    home = fetch_windows_home (pldap, NULL, sid, dnsdomain);
	  break;
	case NSS_SCHEME_CYGWIN:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (L"cygwinHome");
	      if (val && *val)
		sys_wcstombs_alloc (&home, HEAP_NOTHEAP, val);
	    }
	  break;
	case NSS_SCHEME_UNIX:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (L"unixHomeDirectory");
	      if (val && *val)
		sys_wcstombs_alloc (&home, HEAP_NOTHEAP, val);
	    }
	  break;
	case NSS_SCHEME_DESC:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (L"description");
	      if (val && *val)
		home = fetch_from_description (val, L"home=\"", 6);
	    }
	  break;
	case NSS_SCHEME_PATH:
	  home = fetch_from_path (pldap, NULL, sid, home_scheme[idx].attrib,
				  dom, dnsdomain, name, full_qualified);
	  break;
	case NSS_SCHEME_FREEATTR:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (home_scheme[idx].attrib);
	      if (val && *val)
		{
		  if (isdrive (val) || *val == '\\')
		    home = (char *)
			   cygwin_create_path (CCP_WIN_W_TO_POSIX, val);
		  else
		    sys_wcstombs_alloc (&home, HEAP_NOTHEAP, val);
		}
	    }
	  break;
	}
    }
  return home;
}

char *
cygheap_pwdgrp::get_home (PUSER_INFO_3 ui, cygpsid &sid, PCWSTR dom,
			  PCWSTR name, bool full_qualified)
{
  char *home = NULL;

  for (uint16_t idx = 0; !home && idx < NSS_SCHEME_MAX; ++idx)
    {
      switch (home_scheme[idx].method)
	{
	case NSS_SCHEME_FALLBACK:
	  return NULL;
	case NSS_SCHEME_WINDOWS:
	  home = fetch_windows_home (NULL, ui, sid, NULL);
	  break;
	case NSS_SCHEME_CYGWIN:
	case NSS_SCHEME_UNIX:
	case NSS_SCHEME_FREEATTR:
	  break;
	case NSS_SCHEME_DESC:
	  if (ui)
	    home = fetch_from_description (ui->usri3_comment, L"home=\"", 6);
	  break;
	case NSS_SCHEME_PATH:
	  home = fetch_from_path (NULL, ui, sid, home_scheme[idx].attrib,
				  dom, NULL, name, full_qualified);
	  break;
	}
    }
  return home;
}

char *
cygheap_pwdgrp::get_shell (cyg_ldap *pldap, cygpsid &sid, PCWSTR dom,
			   PCWSTR dnsdomain, PCWSTR name, bool full_qualified)
{
  PWCHAR val;
  char *shell = NULL;

  for (uint16_t idx = 0; !shell && idx < NSS_SCHEME_MAX; ++idx)
    {
      switch (shell_scheme[idx].method)
	{
	case NSS_SCHEME_FALLBACK:
	  return NULL;
	case NSS_SCHEME_WINDOWS:
	  break;
	case NSS_SCHEME_CYGWIN:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (L"cygwinShell");
	      if (val && *val)
		sys_wcstombs_alloc (&shell, HEAP_NOTHEAP, val);
	    }
	  break;
	case NSS_SCHEME_UNIX:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (L"loginShell");
	      if (val && *val)
		sys_wcstombs_alloc (&shell, HEAP_NOTHEAP, val);
	    }
	  break;
	case NSS_SCHEME_DESC:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (L"description");
	      if (val && *val)
		shell = fetch_from_description (val, L"shell=\"", 7);
	    }
	  break;
	case NSS_SCHEME_PATH:
	  shell = fetch_from_path (pldap, NULL, sid, shell_scheme[idx].attrib,
				   dom, dnsdomain, name, full_qualified);
	  break;
	case NSS_SCHEME_FREEATTR:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (shell_scheme[idx].attrib);
	      if (val && *val)
		{
		  if (isdrive (val) || *val == '\\')
		    shell = (char *)
			    cygwin_create_path (CCP_WIN_W_TO_POSIX, val);
		  else
		    sys_wcstombs_alloc (&shell, HEAP_NOTHEAP, val);
		}
	    }
	  break;
	}
    }
  return shell;
}

char *
cygheap_pwdgrp::get_shell (PUSER_INFO_3 ui, cygpsid &sid, PCWSTR dom,
			   PCWSTR name, bool full_qualified)
{
  char *shell = NULL;

  for (uint16_t idx = 0; !shell && idx < NSS_SCHEME_MAX; ++idx)
    {
      switch (shell_scheme[idx].method)
	{
	case NSS_SCHEME_FALLBACK:
	  return NULL;
	case NSS_SCHEME_WINDOWS:
	case NSS_SCHEME_CYGWIN:
	case NSS_SCHEME_UNIX:
	case NSS_SCHEME_FREEATTR:
	  break;
	case NSS_SCHEME_DESC:
	  if (ui)
	    shell = fetch_from_description (ui->usri3_comment, L"shell=\"", 7);
	  break;
	case NSS_SCHEME_PATH:
	  shell = fetch_from_path (NULL, ui, sid, shell_scheme[idx].attrib,
				   dom, NULL, name, full_qualified);
	  break;
	}
    }
  return shell;
}

/* Helper function to replace colons with semicolons in pw_gecos field. */
static inline void
colon_to_semicolon (char *str)
{
  char *cp = str;
  while ((cp = strchr (cp, L':')) != NULL)
    *cp++ = L';';
}

char *
cygheap_pwdgrp::get_gecos (cyg_ldap *pldap, cygpsid &sid, PCWSTR dom,
			   PCWSTR dnsdomain, PCWSTR name, bool full_qualified)
{
  PWCHAR val;
  char *gecos = NULL;

  for (uint16_t idx = 0; !gecos && idx < NSS_SCHEME_MAX; ++idx)
    {
      switch (gecos_scheme[idx].method)
	{
	case NSS_SCHEME_FALLBACK:
	  return NULL;
	case NSS_SCHEME_WINDOWS:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (L"displayName");
	      if (val && *val)
		sys_wcstombs_alloc (&gecos, HEAP_NOTHEAP, val);
	    }
	  break;
	case NSS_SCHEME_CYGWIN:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (L"cygwinGecos");
	      if (val && *val)
		sys_wcstombs_alloc (&gecos, HEAP_NOTHEAP, val);
	    }
	  break;
	case NSS_SCHEME_UNIX:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (L"gecos");
	      if (val && *val)
		sys_wcstombs_alloc (&gecos, HEAP_NOTHEAP, val);
	    }
	  break;
	case NSS_SCHEME_DESC:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (L"description");
	      if (val && *val)
		gecos = fetch_from_description (val, L"gecos=\"", 7);
	    }
	  break;
	case NSS_SCHEME_PATH:
	  gecos = fetch_from_path (pldap, NULL, sid,
				   gecos_scheme[idx].attrib + 1,
				   dom, dnsdomain, name, full_qualified);
	  break;
	case NSS_SCHEME_FREEATTR:
	  if (pldap->fetch_ad_account (sid, false, dnsdomain))
	    {
	      val = pldap->get_string_attribute (gecos_scheme[idx].attrib);
	      if (val && *val)
		sys_wcstombs_alloc (&gecos, HEAP_NOTHEAP, val);
	    }
	  break;
	}
    }
  if (gecos)
    colon_to_semicolon (gecos);
  return gecos;
}

char *
cygheap_pwdgrp::get_gecos (PUSER_INFO_3 ui, cygpsid &sid, PCWSTR dom,
			   PCWSTR name, bool full_qualified)
{
  char *gecos = NULL;

  for (uint16_t idx = 0; !gecos && idx < NSS_SCHEME_MAX; ++idx)
    {
      switch (gecos_scheme[idx].method)
	{
	case NSS_SCHEME_FALLBACK:
	  return NULL;
	case NSS_SCHEME_WINDOWS:
	  if (ui && ui->usri3_full_name && *ui->usri3_full_name)
	    sys_wcstombs_alloc (&gecos, HEAP_NOTHEAP, ui->usri3_full_name);
	  break;
	case NSS_SCHEME_CYGWIN:
	case NSS_SCHEME_UNIX:
	case NSS_SCHEME_FREEATTR:
	  break;
	case NSS_SCHEME_DESC:
	  if (ui)
	    gecos = fetch_from_description (ui->usri3_comment, L"gecos=\"", 7);
	  break;
	case NSS_SCHEME_PATH:
	  gecos = fetch_from_path (NULL, ui, sid, gecos_scheme[idx].attrib + 1,
				   dom, NULL, name, full_qualified);
	  break;
	}
    }
  if (gecos)
    colon_to_semicolon (gecos);
  return gecos;
}

void
cygheap_pwdgrp::_nss_init ()
{
  UNICODE_STRING path;
  OBJECT_ATTRIBUTES attr;
  NT_readline rl;
  tmp_pathbuf tp;
  char *buf = tp.c_get ();

  PCWSTR rel_path = L"\\etc\\nsswitch.conf";
  path.Length = cygheap->installation_root.Length
		+ wcslen (rel_path) * sizeof (WCHAR);
  path.MaximumLength = path.Length + sizeof (WCHAR);
  path.Buffer = (PWCHAR) alloca (path.MaximumLength);
  wcpcpy (wcpcpy (path.Buffer, cygheap->installation_root.Buffer), rel_path);
  InitializeObjectAttributes (&attr, &path, OBJ_CASE_INSENSITIVE,
			      NULL, NULL);
  if (rl.init (&attr, buf, NT_MAX_PATH))
    while ((buf = rl.gets ()))
      nss_init_line (buf);
  nss_inited = true;
}

/* Override the ParentIndex value of the PDS_DOMAIN_TRUSTSW entry with the
   PosixOffset. */
#define PosixOffset ParentIndex

bool
cygheap_domain_info::init ()
{
  HANDLE lsa;
  NTSTATUS status;
  ULONG ret;
  /* We *have* to copy the information.  Apart from our wish to have the
     stuff in the cygheap, even when not calling LsaFreeMemory on the result,
     the data will be overwritten later.  From what I gather, the information
     is, in fact, stored on the stack. */
  PPOLICY_DNS_DOMAIN_INFO pdom;
  PPOLICY_ACCOUNT_DOMAIN_INFO adom;
  PDS_DOMAIN_TRUSTSW td;
  ULONG tdom_cnt;

  if (adom_name)
    return true;
  lsa = lsa_open_policy (NULL, POLICY_VIEW_LOCAL_INFORMATION);
  if (!lsa)
    {
      system_printf ("lsa_open_policy(NULL) failed");
      return false;
    }
  /* Fetch primary domain information from local LSA. */
  status = LsaQueryInformationPolicy (lsa, PolicyDnsDomainInformation,
				      (PVOID *) &pdom);
  if (status != STATUS_SUCCESS)
    {
      system_printf ("LsaQueryInformationPolicy(Primary) %y", status);
      return false;
    }
  /* Copy primary domain info to cygheap. */
  pdom_name = cwcsdup (pdom->Name.Buffer);
  pdom_dns_name = pdom->DnsDomainName.Length
		  ? cwcsdup (pdom->DnsDomainName.Buffer) : NULL;
  pdom_sid = pdom->Sid;
  LsaFreeMemory (pdom);
  /* Fetch account domain information from local LSA. */
  status = LsaQueryInformationPolicy (lsa, PolicyAccountDomainInformation,
				      (PVOID *) &adom);
  if (status != STATUS_SUCCESS)
    {
      system_printf ("LsaQueryInformationPolicy(Account) %y", status);
      return false;
    }
  /* Copy account domain info to cygheap.  If we're running on a DC the account
     domain is identical to the primary domain.  This leads to confusion when
     trying to compute the uid/gid values.  Therefore we invalidate the account
     domain name if we're running on a DC. */
  adom_sid = adom->DomainSid;
  adom_name = cwcsdup (pdom_sid == adom_sid ? L"@" : adom->DomainName.Buffer);
  LsaFreeMemory (adom);
  lsa_close_policy (lsa);
  if (cygheap->dom.member_machine ())
    {
      ret = DsEnumerateDomainTrustsW (NULL, DS_DOMAIN_DIRECT_INBOUND
					    | DS_DOMAIN_DIRECT_OUTBOUND
					    | DS_DOMAIN_IN_FOREST,
				      &td, &tdom_cnt);
      if (ret != ERROR_SUCCESS)
	{
	  SetLastError (ret);
	  debug_printf ("DsEnumerateDomainTrusts: %E");
	  return true;
	}
      if (tdom_cnt == 0)
	{
	  return true;
	}
      /* Copy trusted domain info to cygheap, setting PosixOffset on the fly. */
      tdom = (PDS_DOMAIN_TRUSTSW)
	cmalloc_abort (HEAP_BUF, tdom_cnt * sizeof (DS_DOMAIN_TRUSTSW));
      memcpy (tdom, td, tdom_cnt * sizeof (DS_DOMAIN_TRUSTSW));
      for (ULONG idx = 0; idx < tdom_cnt; ++idx)
	{
	  /* Copy... */
	  tdom[idx].NetbiosDomainName = cwcsdup (td[idx].NetbiosDomainName);
	  /* DnsDomainName as well as DomainSid can be NULL.  The reason is
	     usually a domain of type TRUST_TYPE_DOWNLEVEL.  This can be an
	     old pre-AD domain, or a Netware domain, etc.  If DnsDomainName
	     is NULL, just set it to NetbiosDomainName.  This simplifies
	     subsequent code which doesn't have to check for a NULL pointer. */
	  tdom[idx].DnsDomainName = td[idx].DnsDomainName
				    ? cwcsdup (td[idx].DnsDomainName)
				    : tdom[idx].NetbiosDomainName;
	  if (td[idx].DomainSid)
	    {
	      ULONG len = RtlLengthSid (td[idx].DomainSid);
	      tdom[idx].DomainSid = cmalloc_abort(HEAP_BUF, len);
	      RtlCopySid (len, tdom[idx].DomainSid, td[idx].DomainSid);
	    }
	  /* ...and set PosixOffset to 0.  This */
	  tdom[idx].PosixOffset = 0;
	}
      NetApiBufferFree (td);
      tdom_count = tdom_cnt;
    }
  /* If we have Microsoft Client for NFS installed, we make use of a name
     mapping server.  This can be either Active Directory to map uids/gids
     directly to Windows SIDs, or an AD LDS or other RFC 2307 compatible
     identity store.  The name of the mapping domain can be fetched from the
     registry key created by the NFS client installation and entered by the
     user via nfsadmin or the "Services For NFS" MMC snap-in.

     Reference:
     http://blogs.technet.com/b/filecab/archive/2012/10/09/nfs-identity-mapping-in-windows-server-2012.aspx
     Note that we neither support UNMP nor local passwd/group file mapping,
     nor UUUA.

     This function returns the mapping server from the aforementioned registry
     key, or, if none is configured, NULL, which will be resolved to the
     primary domain of the machine by the ldap_init function.

     The latter is useful to get an RFC 2307 mapping for Samba UNIX accounts,
     even if no NFS name mapping is configured on the machine.  Fortunately,
     the posixAccount and posixGroup schemas are already available in the
     Active Directory default setup. */
  reg_key reg (HKEY_LOCAL_MACHINE, KEY_READ | KEY_WOW64_64KEY,
	       L"SOFTWARE", L"Microsoft", L"ServicesForNFS", NULL);
  if (!reg.error ())
    {
      DWORD rfc2307 = reg.get_dword (L"Rfc2307", 0);
      if (rfc2307)
	{
	  rfc2307_domain_buf = (PWCHAR) ccalloc_abort (HEAP_STR, 257,
						       sizeof (WCHAR));
	  reg.get_string (L"Rfc2307Domain", rfc2307_domain_buf, 257, L"");
	  if (!rfc2307_domain_buf[0])
	    {
	      cfree (rfc2307_domain_buf);
	      rfc2307_domain_buf = NULL;
	    }
	}
    }
  return true;
}

PDS_DOMAIN_TRUSTSW
cygheap_domain_info::add_domain (PCWSTR domain, PSID sid)
{
  PDS_DOMAIN_TRUSTSW new_tdom;
  cygsid tsid (sid);

  new_tdom = (PDS_DOMAIN_TRUSTSW) crealloc (tdom, (tdom_count + 1)
						  * sizeof (DS_DOMAIN_TRUSTSW));
  if (!new_tdom)
    return NULL;

  tdom = new_tdom;
  new_tdom = &tdom[tdom_count];
  new_tdom->DnsDomainName = new_tdom->NetbiosDomainName = cwcsdup (domain);
  --*RtlSubAuthorityCountSid (tsid);
  ULONG len = RtlLengthSid (tsid);
  new_tdom->DomainSid = cmalloc_abort(HEAP_BUF, len);
  RtlCopySid (len, new_tdom->DomainSid, tsid);
  new_tdom->PosixOffset = 0;
  ++tdom_count;
  return new_tdom;
}

/* Per session, so it changes potentially when switching the user context. */
static cygsid logon_sid ("");

static void
get_logon_sid ()
{
  if (PSID (logon_sid) == NO_SID)
    {
      NTSTATUS status;
      ULONG size;
      tmp_pathbuf tp;
      PTOKEN_GROUPS groups = (PTOKEN_GROUPS) tp.w_get ();

      status = NtQueryInformationToken (hProcToken, TokenGroups, groups,
					2 * NT_MAX_PATH, &size);
      if (!NT_SUCCESS (status))
	debug_printf ("NtQueryInformationToken (TokenGroups) %y", status);
      else
	{
	  for (DWORD pg = 0; pg < groups->GroupCount; ++pg)
	    if (groups->Groups[pg].Attributes & SE_GROUP_LOGON_ID)
	      {
		logon_sid = groups->Groups[pg].Sid;
		break;
	      }
	}
    }
}

/* Fetch special AzureAD group, which is part of the token group list but
   *not* recognized by LookupAccountSid (ERROR_NONE_MAPPED). */
static cygsid azure_grp_sid ("");

static void
get_azure_grp_sid ()
{
  if (PSID (azure_grp_sid) == NO_SID)
    {
      NTSTATUS status;
      ULONG size;
      tmp_pathbuf tp;
      PTOKEN_GROUPS groups = (PTOKEN_GROUPS) tp.w_get ();

      status = NtQueryInformationToken (hProcToken, TokenGroups, groups,
					2 * NT_MAX_PATH, &size);
      if (!NT_SUCCESS (status))
	debug_printf ("NtQueryInformationToken (TokenGroups) %y", status);
      else
	{
	  for (DWORD pg = 0; pg < groups->GroupCount; ++pg)
	    if (sid_id_auth (groups->Groups[pg].Sid) == 12)
	      {
		azure_grp_sid = groups->Groups[pg].Sid;
		break;
	      }
	}
    }
}

void *
pwdgrp::add_account_post_fetch (char *line, bool lock)
{
  void *ret = NULL;

  if (line)
    {
      if (lock)
	pglock.init ("pglock")->acquire ();
      if (add_line (line))
	ret = ((char *) pwdgrp_buf) + (curr_lines - 1) * pwdgrp_buf_elem_size;
      if (lock)
	pglock.release ();
    }
  return ret;
}

void *
pwdgrp::add_account_from_file (cygpsid &sid)
{
  if (!path.MaximumLength)
    return NULL;
  fetch_user_arg_t arg;
  arg.type = SID_arg;
  arg.sid = &sid;
  char *line = fetch_account_from_file (arg);
  return (struct passwd *) add_account_post_fetch (line, true);
}

void *
pwdgrp::add_account_from_file (const char *name)
{
  if (!path.MaximumLength)
    return NULL;
  fetch_user_arg_t arg;
  arg.type = NAME_arg;
  arg.name = name;
  char *line = fetch_account_from_file (arg);
  return (struct passwd *) add_account_post_fetch (line, true);
}

void *
pwdgrp::add_account_from_file (uint32_t id)
{
  if (!path.MaximumLength)
    return NULL;
  fetch_user_arg_t arg;
  arg.type = ID_arg;
  arg.id = id;
  char *line = fetch_account_from_file (arg);
  return (struct passwd *) add_account_post_fetch (line, true);
}

void *
pwdgrp::add_account_from_windows (cygpsid &sid, cyg_ldap *pldap)
{
  fetch_user_arg_t arg;
  arg.type = SID_arg;
  arg.sid = &sid;
  char *line = fetch_account_from_windows (arg, pldap);
  if (!line)
    return NULL;
  return add_account_post_fetch (line, true);
}

void *
pwdgrp::add_account_from_windows (const char *name, cyg_ldap *pldap)
{
  fetch_user_arg_t arg;
  arg.type = NAME_arg;
  arg.name = name;
  char *line = fetch_account_from_windows (arg, pldap);
  if (!line)
    return NULL;
  return add_account_post_fetch (line, true);
}

void *
pwdgrp::add_account_from_windows (uint32_t id, cyg_ldap *pldap)
{
  fetch_user_arg_t arg;
  arg.type = ID_arg;
  arg.id = id;
  char *line = fetch_account_from_windows (arg, pldap);
  if (!line)
    return NULL;
  return add_account_post_fetch (line, true);
}

/* Called from internal_getgrfull, in turn called from internal_getgroups. */
struct group *
pwdgrp::add_group_from_windows (fetch_acc_t &full_acc, cyg_ldap *pldap)
{
  fetch_user_arg_t arg;
  arg.type = FULL_acc_arg;
  arg.full_acc = &full_acc;
  char *line = fetch_account_from_windows (arg, pldap);
  if (!line)
    return NULL;
  return (struct group *) add_account_post_fetch (line, true);
}

/* Check if file exists and if it has been written to since last checked.
   If file has been changed, invalidate the current cache.

   If the file doesn't exist when this function is called the first time,
   by the first Cygwin process in a process tree, the file will never be
   visited again by any process in this process tree.  This is important,
   because we cannot allow a change of UID/GID values for the lifetime
   of a process tree.

   If the file gets deleted or unreadable, the file cache will stay in
   place, but we won't try to read new accounts from the file.

   The return code indicates to the calling function if the file exists. */
bool
pwdgrp::check_file ()
{
  FILE_BASIC_INFORMATION fbi;
  NTSTATUS status;

  if (!path.Buffer)
    {
      PCWSTR rel_path = is_group () ? L"\\etc\\group" : L"\\etc\\passwd";
      path.Length = cygheap->installation_root.Length
		    + wcslen (rel_path) * sizeof (WCHAR);
      path.MaximumLength = path.Length + sizeof (WCHAR);
      path.Buffer = (PWCHAR) cmalloc_abort (HEAP_BUF, path.MaximumLength);
      wcpcpy (wcpcpy (path.Buffer, cygheap->installation_root.Buffer),
	      rel_path);
      InitializeObjectAttributes (&attr, &path, OBJ_CASE_INSENSITIVE,
				  NULL, NULL);
    }
  else if (path.MaximumLength == 0) /* Indicates that the file doesn't exist. */
    return false;
  status = NtQueryAttributesFile (&attr, &fbi);
  if (!NT_SUCCESS (status))
    {
      if (last_modified.QuadPart)
	last_modified.QuadPart = 0LL;
      else
	path.MaximumLength = 0;
      return false;
    }
  if (fbi.LastWriteTime.QuadPart > last_modified.QuadPart)
    {
      last_modified.QuadPart = fbi.LastWriteTime.QuadPart;
      if (curr_lines > 0)
	{
	  pglock.init ("pglock")->acquire ();
	  int curr = curr_lines;
	  curr_lines = 0;
	  for (int i = 0; i < curr; ++i)
	    cfree (is_group () ? this->group ()[i].g.gr_name
			 : this->passwd ()[i].p.pw_name);
	  pglock.release ();
	}
    }
  return true;
}

char *
pwdgrp::fetch_account_from_line (fetch_user_arg_t &arg, const char *line)
{
  char *p, *e;

  switch (arg.type)
    {
    case SID_arg:
      /* Ignore fields, just scan for SID string. */
      if (!(p = strstr (line, arg.name)) || p[arg.len] != ':')
	return NULL;
      break;
    case NAME_arg:
      /* First field is always name. */
      if (!strncasematch (line, arg.name, arg.len) || line[arg.len] != ':')
	return NULL;
      break;
    case ID_arg:
      /* Skip to third field. */
      if (!(p = strchr (line, ':')) || !(p = strchr (p + 1, ':')))
	return NULL;
      if (strtoul (p + 1, &e, 10) != arg.id || !e || *e != ':')
	return NULL;
      break;
    default:
      return NULL;
    }
  return cstrdup (line);
}

char *
pwdgrp::fetch_account_from_file (fetch_user_arg_t &arg)
{
  NT_readline rl;
  tmp_pathbuf tp;
  char *buf = tp.c_get ();
  char str[128];
  char *ret = NULL;

  /* Create search string. */
  switch (arg.type)
    {
    case SID_arg:
      /* Override SID with SID string. */
      arg.sid->string (str);
      arg.name = str;
      /*FALLTHRU*/
    case NAME_arg:
      arg.len = strlen (arg.name);
      break;
    case ID_arg:
      break;
    default:
      return NULL;
    }
  if (rl.init (&attr, buf, NT_MAX_PATH))
    while ((buf = rl.gets ()))
      if ((ret = fetch_account_from_line (arg, buf)))
	return ret;
  return NULL;
}

static ULONG
fetch_posix_offset (PDS_DOMAIN_TRUSTSW td, cyg_ldap *cldap)
{
  uint32_t id_val = UINT32_MAX;

  if (!td->PosixOffset && !(td->Flags & DS_DOMAIN_PRIMARY) && td->DomainSid)
    {
      if (cldap->open (NULL) == NO_ERROR)
	id_val = cldap->fetch_posix_offset_for_domain (td->DnsDomainName);
      if (id_val < PRIMARY_POSIX_OFFSET)
	{
	  /* If the offset is less than the primay domain offset, we're bound
	     to suffer collisions with system and local accounts.  Move offset
	     to a fixed replacement fake offset.  This may result in collisions
	     between other domains all of which were moved to this replacement
	     offset, but we can't fix all problems caused by careless admins. */
	  id_val = UNUSABLE_POSIX_OFFSET;
	}
      else if (id_val == UINT32_MAX)
	{
	  /* We're probably running under a local account, so we're not allowed
	     to fetch any information from AD beyond the most obvious.  Fake a
	     reasonable posix offset as above and hope for the best. */
	  id_val = NOACCESS_POSIX_OFFSET;
	}
      td->PosixOffset = id_val;
    }
  return td->PosixOffset;
}

/* If LookupAccountName fails, we check the name for a known constructed name
   with this function.  Return true if we could create a valid SID from name
   in sid.  sep is either a pointer to thr backslash in name, or NULL if name
   is not a qualified DOMAIN\\name string. */
bool
pwdgrp::construct_sid_from_name (cygsid &sid, wchar_t *name, wchar_t *sep)
{
  unsigned long rid;
  wchar_t *endptr;

  if (sep && wcscmp (name, L"no\\body") == 0)
    {
      /* Special case "nobody" for reproducible construction of a
	 nobody SID for WinFsp and similar services.  We use the
	 value 65534 which is -2 with 16 bit uid/gids. */
      sid.create (0, 1, 0xfffe);
      return true;
    }
  if (sep && wcscmp (name, L"AzureAD\\Group") == 0)
    {
      get_azure_grp_sid ();
      if (PSID (logon_sid) != NO_SID)
	{
	  sid = azure_grp_sid;
	  return true;
	}
      return false;
    }
  if (!sep && wcscmp (name, L"CurrentSession") == 0)
    {
      get_logon_sid ();
      if (PSID (logon_sid) == NO_SID)
	return false;
      sid = logon_sid;
      return true;
    }
  if (!sep && wcscmp (name, L"Authentication authority asserted identity") == 0)
    {
      sid.create (18, 1, 1);
      return true;
    }
  if (!sep && wcscmp (name, L"Service asserted identity") == 0)
    {
      sid.create (18, 1, 2);
      return true;
    }
  if (sep && wcsncmp (name, L"Unix_", 5) == 0)
    {
      int type;

      if (wcsncmp (name + 5, L"User\\", 5) == 0)
	type = 1;
      else if (wcsncmp (name + 5, L"Group\\", 6) == 0)
	type = 2;
      else
	return false;

      rid = wcstoul (sep + 1, &endptr, 10);
      if (*endptr == L'\0')
	{
	  sid.create (22, 2, type, rid);
	  return true;
	}
      return false;
    }
  /* At this point we have to check if the domain name is one of the
     known domains, and if the account name is one of "User(DWORD)"
     or "Group(DWORD)". */
  if (sep)
    {
      wchar_t *numstr = NULL;
      bool have_domain = false;

      if (wcsncmp (sep + 1, L"User(", 5) == 0)
	numstr = sep + 1 + 5;
      else if (wcsncmp (sep + 1, L"Group(", 6) == 0)
	numstr = sep + 1 + 6;
      if (!numstr)
	return false;
      rid = wcstoul (numstr, &endptr, 10);
      if (wcscmp (endptr, L")") != 0)
	return false;

      if (wcsncasecmp (name, cygheap->dom.account_flat_name (), sep - name)
	  == 0)
	{
	  sid = cygheap->dom.account_sid ();
	  have_domain = true;
	}
      else if (wcsncasecmp (name, cygheap->dom.primary_flat_name (), sep - name)
	       == 0)
	{
	  sid = cygheap->dom.primary_sid ();
	  have_domain = true;
	}
      else
	{
	  PDS_DOMAIN_TRUSTSW td = NULL;

	  for (ULONG idx = 0; (td = cygheap->dom.trusted_domain (idx)); ++idx)
	    {
	      if (wcsncasecmp (name, td->NetbiosDomainName, sep - name) == 0)
		{
		  sid = td->DomainSid;
		  have_domain = true;
		  break;
		}
	    }
	}
      if (have_domain)
	{
	  sid.append (rid);
	  return true;
	}
    }
  return false;
}

/* CV 2018-08-28: SidTypeLogonSession is not yet defined in Mingw64. */
#define SidTypeLogonSession 11

char *
pwdgrp::fetch_account_from_windows (fetch_user_arg_t &arg, cyg_ldap *pldap)
{
  /* Used in LookupAccount calls. */
  WCHAR namebuf[DNLEN + 1 + UNLEN + 1], *name = namebuf;
  WCHAR dom[DNLEN + 1] = L"";
  cygsid csid;
  DWORD nlen = UNLEN + 1;
  DWORD dlen = DNLEN + 1;
  DWORD slen = SECURITY_MAX_SID_SIZE;
  cygpsid sid (NO_SID);
  SID_NAME_USE acc_type;
  BOOL ret = false;
  /* Cygwin user name style. */
  bool fully_qualified_name = false;
  /* Computed stuff. */
  uid_t uid = ILLEGAL_UID;
  gid_t gid = ILLEGAL_GID;
  bool is_domain_account = true;
  PCWSTR domain = NULL;
  bool is_current_user = false;
  char *shell = NULL;
  char *home = NULL;
  char *gecos = NULL;
  /* Temporary stuff. */
  PWCHAR p;
  PWCHAR val;
  WCHAR sidstr[128];
  ULONG posix_offset = 0;
  uint32_t id_val;
  cyg_ldap loc_ldap;
  cyg_ldap *cldap = pldap ?: &loc_ldap;

  /* Initialize */
  if (!cygheap->dom.init ())
    return NULL;

  switch (arg.type)
    {
    case FULL_acc_arg:
      {
	sid = arg.full_acc->sid;
	*wcpncpy (name, arg.full_acc->name->Buffer,
		  arg.full_acc->name->Length / sizeof (WCHAR)) = L'\0';
	*wcpncpy (dom, arg.full_acc->dom->Buffer,
		  arg.full_acc->dom->Length / sizeof (WCHAR)) = L'\0';
	acc_type = arg.full_acc->acc_type;
	ret = acc_type != SidTypeUnknown;
      }
      break;
    case SID_arg:
      sid = *arg.sid;
      ret = LookupAccountSidW (NULL, sid, name, &nlen, dom, &dlen, &acc_type);
      if (!ret
	  && cygheap->dom.member_machine ()
	  && sid_id_auth (sid) == 5 /* SECURITY_NT_AUTHORITY */
	  && sid_sub_auth (sid, 0) == SECURITY_BUILTIN_DOMAIN_RID)
	{
	  /* LookupAccountSid called on a non-DC cannot resolve aliases which
	     are not defined in the local SAM.  If we encounter an alias which
	     can't be resolved, and if we're a domain member machine, ask a DC.
	     Do *not* use LookupAccountSidW.  It can take ages when called on a
	     DC for some weird reason.  Use LDAP instead. */
	  if (cldap->fetch_ad_account (sid, true)
	      && (val = cldap->get_account_name ()))
	    {
	      wcpcpy (name, val);
	      wcpcpy (dom, L"BUILTIN");
	      acc_type = SidTypeAlias;
	      ret = true;
	    }
	}
      if (!ret)
	debug_printf ("LookupAccountSid(%W), %E", sid.string (sidstr));
      break;
    case NAME_arg:
      bool fq_name;

      fq_name = false;
      /* Copy over to wchar for search. */
      sys_mbstowcs (name, UNLEN + 1, arg.name);
      /* If the incoming name has a backslash or at sign, and neither backslash
	 nor at are the domain separator chars, the name is invalid. */
      if ((p = wcspbrk (name, L"\\@")) && *p != cygheap->pg.nss_separator ()[0])
	{
	  debug_printf ("Invalid account name <%s> (backslash/at)", arg.name);
	  return NULL;
	}
      /* Replace domain separator char with backslash and make sure p is NULL
	 or points to the backslash. */
      if ((p = wcschr (name, cygheap->pg.nss_separator ()[0])))
	{
	  fq_name = true;
	  *p = L'\\';
	}
      sid = csid;
      ret = LookupAccountNameW (NULL, name, sid, &slen, dom, &dlen, &acc_type);
      /* If this is a name-only S-1-5-21 account *and* it's a machine account
	 on a domain member machine, then we found the wrong one.  Another
	 weird, but perfectly valid case is, if the group name is identical
	 to the domain name.  Try again with domain name prepended. */
      if (ret
	  && !fq_name
	  && sid_id_auth (sid) == 5 /* SECURITY_NT_AUTHORITY */
	  && sid_sub_auth (sid, 0) == SECURITY_NT_NON_UNIQUE
	  && cygheap->dom.member_machine ()
	  && (wcscasecmp (dom, cygheap->dom.account_flat_name ()) == 0
	      || acc_type == SidTypeDomain))
	{
	  p = wcpcpy (name, cygheap->dom.primary_flat_name ());
	  *p = L'\\';
	  sys_mbstowcs (p + 1, UNLEN + 1, arg.name);
	  slen = SECURITY_MAX_SID_SIZE;
	  dlen = DNLEN + 1;
	  sid = csid;
	  ret = LookupAccountNameW (NULL, name, sid, &slen, dom, &dlen,
				    &acc_type);
	}
      if (!ret)
	{
	  /* For accounts which can't be resolved by Windows, try if
	     it's one of the special names we use for special accounts. */
	  if (construct_sid_from_name (csid, name, p))
	    break;
	  debug_printf ("LookupAccountNameW (%W), %E", name);
	  return NULL;
	}
      /* We can skip the backslash in the rest of this function. */
      if (p)
	name = p + 1;
      /* Last but not least, some validity checks on the name style. */
      if (!fq_name)
	{
	  /* AzureAD user must be prepended by "domain" name. */
	  if (sid_id_auth (sid) == 12)
	    return NULL;
	  /* name_only only if db_prefix is auto. */
	  if (!cygheap->pg.nss_prefix_auto ())
	    {
	      debug_printf ("Invalid account name <%s> (name only/"
			    "db_prefix not auto)", arg.name);
	      return NULL;
	    }
	  /* name_only account is either builtin or primary domain, or
	     account domain on non-domain machines. */
	  if (sid_id_auth (sid) == 5 /* SECURITY_NT_AUTHORITY */
	      && sid_sub_auth (sid, 0) == SECURITY_NT_NON_UNIQUE)
	    {
	      if (cygheap->dom.member_machine ())
		{
		  if (wcscasecmp (dom, cygheap->dom.primary_flat_name ()) != 0)
		    {
		      debug_printf ("Invalid account name <%s> (name only/"
				    "non primary on domain machine)", arg.name);
		      return NULL;
		    }
		}
	      else if (wcscasecmp (dom, cygheap->dom.account_flat_name ()) != 0)
		{
		  debug_printf ("Invalid account name <%s> (name only/"
				"non machine on non-domain machine)", arg.name);
		  return NULL;
		}
	    }
	}
      else
	{
	  /* All is well if db_prefix is always. */
	  if (cygheap->pg.nss_prefix_always ())
	    break;
	  /* AzureAD accounts should be fully qualifed either. */
	  if (sid_id_auth (sid) == 12)
	    break;
	  /* Otherwise, no fully_qualified for builtin accounts, except for
	     NT SERVICE, for which we require the prefix.  Note that there's
	     no equivalent test in the `if (!fq_name)' branch above, because
	     LookupAccountName never returns NT SERVICE accounts if they are
	     not prependend with the domain anyway. */
	  if (sid_id_auth (sid) != 5 /* SECURITY_NT_AUTHORITY */
	      || (sid_sub_auth (sid, 0) != SECURITY_NT_NON_UNIQUE
		  && sid_sub_auth (sid, 0) != SECURITY_SERVICE_ID_BASE_RID))
	    {
	      debug_printf ("Invalid account name <%s> (fully qualified/"
			    "not NON_UNIQUE or NT_SERVICE)", arg.name);
	      return NULL;
	    }
	  /* All is well if db_prefix is primary. */
	  if (cygheap->pg.nss_prefix_primary ())
	    break;
	  /* Domain member and domain == primary domain? */
	  if (cygheap->dom.member_machine ())
	    {
	      if (!wcscasecmp (dom, cygheap->dom.primary_flat_name ()))
		{
		  debug_printf ("Invalid account name <%s> (fully qualified/"
				"primary domain account)", arg.name);
		  return NULL;
		}
	    }
	  /* Not domain member and domain == account domain? */
	  else if (!wcscasecmp (dom, cygheap->dom.account_flat_name ()))
	    {
	      debug_printf ("Invalid account name <%s> (fully qualified/"
			    "local account)", arg.name);
	      return NULL;
	    }
	}
      break;
    case ID_arg:
      /* Construct SID from ID using the SFU rules, just like the code below
         goes the opposite route. */
#ifndef INTERIX_COMPATIBLE
      /* Except for Builtin and Alias groups in the SECURITY_NT_AUTHORITY.
	 We create uid/gid values compatible with the old values generated
	 by mkpasswd/mkgroup. */
      if (arg.id < 0x200)
	csid.create (5, 1, arg.id & 0x1ff);
      else if (arg.id <= 0x3e7)
	csid.create (5, 2, 32, arg.id & 0x3ff);
      else if (arg.id == 0x3e8) /* Special case "Other Organization" */
	csid.create (5, 1, 1000);
      else
#endif
      if (arg.id == 0xffe)
	{
	  /* OtherSession != Logon SID. */
	  get_logon_sid ();
	  /* LookupAccountSidW will fail. */
	  sid = csid = logon_sid;
	  sid_sub_auth_rid (sid) = 0;
	  break;
	}
      else if (arg.id == 0xfff)
	{
	  /* CurrentSession == Logon SID. */
	  get_logon_sid ();
	  /* LookupAccountSidW will fail. */
	  sid = logon_sid;
	  break;
	}
      else if (arg.id == 0x1000)
        {
	  /* AzureAD S-1-12-1-W-X-Y-Z user */
	  csid = cygheap->user.saved_sid ();
	}
      else if (arg.id == 0x1001)
        {
	  /* Special AzureAD group SID */
	  get_azure_grp_sid ();
	  /* LookupAccountSidW will fail. */
	  sid = csid = azure_grp_sid;
	  break;
	}
      else if (arg.id == 0xfffe)
	{
	  /* Special case "nobody" for reproducible construction of a
	     nobody SID for WinFsp and similar services.  We use the
	     value 65534 which is -2 with 16 bit uid/gids. */
	  csid.create (0, 1, 0xfffe);
	  sid = csid;
	  break;
	}
      else if (arg.id < 0x10000)
	{
	  /* Nothing. */
	  debug_printf ("Invalid POSIX id %u", arg.id);
	  return NULL;
	}
      else if (arg.id < 0x20000)
	{
	  /* Well-Known Group */
	  arg.id -= 0x10000;
	  /* SECURITY_APP_PACKAGE_AUTHORITY */
	  if (arg.id >= 0xf20 && arg.id <= 0xf3f)
	    csid.create (15, 2, (arg.id >> 4) & 0xf, arg.id & 0xf);
	  else
	    csid.create (arg.id >> 8, 1, arg.id & 0xff);
	}
      else if (arg.id >= 0x30000 && arg.id < 0x40000)
	{
	  /* Account domain user or group. */
	  csid = cygheap->dom.account_sid ();
	  csid.append (arg.id & 0xffff);
	}
      else if (arg.id < 0x60000)
	{
	  /* Builtin Alias */
	  csid.create (5, 2, arg.id >> 12, arg.id & 0xffff);
	}
      else if (arg.id < 0x70000)
	{
	  /* Mandatory Label. */
	  csid.create (16, 1, arg.id & 0xffff);
	}
      else if (arg.id < 0x80000)
	{
	  /* Identity assertion SIDs. */
	  csid.create (18, 1, arg.id & 0xffff);
	}
      else if (arg.id < PRIMARY_POSIX_OFFSET)
	{
	  /* Nothing. */
	  debug_printf ("Invalid POSIX id %u", arg.id);
	  return NULL;
	}
      else if (arg.id == ILLEGAL_UID)
	{
	  /* Just some fake. */
	  sid = csid.create (99, 1, 0);
	  break;
	}
      else if (arg.id >= UNIX_POSIX_OFFSET)
	{
	  /* UNIX (unknown NFS or Samba) user account. */
	  csid.create (22, 2, is_group () ? 2 : 1,  arg.id & UNIX_POSIX_MASK);
	  /* LookupAccountSidW will fail. */
	  sid = csid;
	  break;
	}
      else
	{
	  /* Some trusted domain? */
	  PDS_DOMAIN_TRUSTSW td = NULL, this_td = NULL;

	  for (ULONG idx = 0; (td = cygheap->dom.trusted_domain (idx)); ++idx)
	    {
	      fetch_posix_offset (td, &loc_ldap);
	      if (td->PosixOffset > posix_offset && td->PosixOffset <= arg.id)
		posix_offset = (this_td = td)->PosixOffset;
	    }
	  if (this_td)
	    {
	      csid = this_td->DomainSid;
	      csid.append (arg.id - posix_offset);
	    }
	  else
	    {
	      /* Primary domain */
	      csid = cygheap->dom.primary_sid ();
	      csid.append (arg.id - PRIMARY_POSIX_OFFSET);
	    }
	  posix_offset = 0;
	}
      sid = csid;
      ret = LookupAccountSidW (NULL, sid, name, &nlen, dom, &dlen, &acc_type);
      if (!ret)
	{
	  debug_printf ("LookupAccountSidW (%W), %E", sid.string (sidstr));
	  return NULL;
	}
      break;
    }
  if (ret)
    {
      /* Builtin account?  SYSTEM, for instance, is returned as SidTypeUser,
	 if a process is running as LocalSystem service.
	 Microsoft Account?  These show up in the user's group list, using the
	 undocumented security authority 11.  Even though this is officially a
	 user account, it only matters as part of the group list, so we convert
	 it to a well-known group here. */
      if (acc_type == SidTypeUser
	  && (sid_sub_auth_count (sid) <= 3 || sid_id_auth (sid) == 11))
	acc_type = SidTypeWellKnownGroup;
      switch ((int) acc_type)
	{
	case SidTypeUser:
	  if (is_group ())
	    {
	      /* Don't allow users as group.  While this is technically
		 possible, it doesn't make sense in a POSIX scenario.

		 Microsoft Accounts as well as AzureAD accounts have the
		 primary group SID in their user token set to their own
		 user SID.

		 Those we let pass, but no others. */
	      bool its_ok = false;
	      if (sid_id_auth (sid) == 12)
		its_ok = true;
	      else if (wincap.has_microsoft_accounts ())
		{
		  USER_INFO_24 *ui24;
		  if (NetUserGetInfo (NULL, name, 24, (PBYTE *) &ui24)
		      == NERR_Success)
		    {
		      if (ui24->usri24_internet_identity)
			its_ok = true;
		      NetApiBufferFree (ui24);
		    }
		}
	      if (!its_ok)
		return NULL;
	    }
	  /*FALLTHRU*/
	case SidTypeGroup:
	case SidTypeAlias:
	  /* Predefined alias? */
	  if (acc_type == SidTypeAlias
	      && sid_sub_auth (sid, 0) != SECURITY_NT_NON_UNIQUE)
	    {
#ifdef INTERIX_COMPATIBLE
	      posix_offset = 0x30000;
	      uid = 0x1000 * sid_sub_auth (sid, 0)
		    + (sid_sub_auth_rid (sid) & 0xffff);
#else
	      posix_offset = 0;
#endif
	      fully_qualified_name = cygheap->pg.nss_prefix_always ();
	      is_domain_account = false;
	    }
	  /* Account domain account? */
	  else if (!wcscasecmp (dom, cygheap->dom.account_flat_name ()))
	    {
	      posix_offset = 0x30000;
	      if (cygheap->dom.member_machine ()
		  || !cygheap->pg.nss_prefix_auto ())
		fully_qualified_name = true;
	      is_domain_account = false;
	    }
	  /* Domain member machine? */
	  else if (cygheap->dom.member_machine ())
	    {
	      /* Primary domain account? */
	      if (!wcscasecmp (dom, cygheap->dom.primary_flat_name ()))
		{
		  posix_offset = PRIMARY_POSIX_OFFSET;
		  /* In theory domain should have been set to
		     cygheap->dom.primary_dns_name (), but it turns out that
		     not setting the domain here has advantages.  We open the
		     ldap connection to NULL (== some DC of our primary domain)
		     anyway.  So the domain is only used later on.  So, don't
		     set domain here to non-NULL, unless you're sure you have
		     also changed subsequent assumptions that domain is NULL
		     if it's a primary domain account. */
		  if (!cygheap->pg.nss_prefix_auto ())
		    fully_qualified_name = true;
		}
	      else
		{
		  /* No, fetch POSIX offset. */
		  PDS_DOMAIN_TRUSTSW td = NULL;

		  fully_qualified_name = true;
		  for (ULONG idx = 0;
		       (td = cygheap->dom.trusted_domain (idx));
		       ++idx)
		    if (!wcscasecmp (dom, td->NetbiosDomainName))
		      {
			domain = td->DnsDomainName;
			break;
		      }
		  if (!domain)
		    {
		      /* This shouldn't happen, in theory, but it does.  There
			 are cases where the user's logon domain does not show
			 up in the list of trusted domains.  We're desperately
			 trying to workaround that here by adding an entry for
			 this domain to the trusted domains and ask the DC for
			 a  posix_offset.  There's a good chance this doesn't
			 work either, but at least we tried, and the user can
			 work. */
		      debug_printf ("Unknown domain %W", dom);
		      td = cygheap->dom.add_domain (dom, sid);
		      if (td)
			domain = td->DnsDomainName;
		    }
		  if (domain)
		    posix_offset = fetch_posix_offset (td, &loc_ldap);
		}
	    }
	  /* AzureAD S-1-12-1-W-X-Y-Z user */
	  else if (sid_id_auth (sid) == 12)
	    {
	      uid = gid = 0x1000;
	      fully_qualified_name = true;
	      home = cygheap->pg.get_home ((PUSER_INFO_3) NULL, sid, dom, name,
					   fully_qualified_name);
	      shell = cygheap->pg.get_shell ((PUSER_INFO_3) NULL, sid, dom,
					     name, fully_qualified_name);
	      gecos = cygheap->pg.get_gecos ((PUSER_INFO_3) NULL, sid, dom,
					     name, fully_qualified_name);
	      break;
	    }
	  /* If the domain returned by LookupAccountSid is not our machine
	     name, and if our machine is no domain member, we lose.  We have
	     nobody to ask for the POSIX offset. */
	  else
	    {
	      debug_printf ("Unknown domain %W", dom);
	      return NULL;
	    }
	  /* Generate uid/gid values. */
	  if (uid == ILLEGAL_UID)
	    uid = posix_offset + sid_sub_auth_rid (sid);
	  if (!is_group () && acc_type == SidTypeUser)
	    {
	      /* Default primary group.  If the sid is the current user, fetch
		 the default group from the current user token, otherwise make
		 the educated guess that the user is in group "Domain Users"
		 or "None". */
	      if (sid == cygheap->user.sid ())
		{
		  is_current_user = true;
		  gid = posix_offset
			+ sid_sub_auth_rid (cygheap->user.groups.pgsid);
		}
	      else
		gid = posix_offset + DOMAIN_GROUP_RID_USERS;
	    }

	  if (is_domain_account)
	    {
	      /* Skip this when creating group entries and for non-users. */
	      if (is_group() || acc_type != SidTypeUser)
		break;
	      /* On AD machines, use LDAP to fetch domain account infos. */
	      if (cygheap->dom.primary_dns_name ())
		{
		  /* For the current user we got correctly cased username and
		     the primary group via process token.  For any other user
		     we fetch it from AD and overwrite it. */
		  if (!is_current_user
		      && cldap->fetch_ad_account (sid, false, domain))
		    {
		      if ((val = cldap->get_account_name ()))
			wcscpy (name, val);
		      if ((id_val = cldap->get_primary_gid ()) != ILLEGAL_GID)
			gid = posix_offset + id_val;
		    }
		  home = cygheap->pg.get_home (cldap, sid, dom, domain,
					       name, fully_qualified_name);
		  shell = cygheap->pg.get_shell (cldap, sid, dom, domain,
						 name,
						 fully_qualified_name);
		  gecos = cygheap->pg.get_gecos (cldap, sid, dom, domain,
						 name, fully_qualified_name);
		  /* Check and, if necessary, add unix<->windows id mapping
		     on the fly, unless we're called from getpwent. */
		  if (!pldap && cldap->is_open ())
		    {
		      id_val = cldap->get_unix_uid ();
		      if (id_val != ILLEGAL_UID
			  && cygheap->ugid_cache.get_uid (id_val)
			     == ILLEGAL_UID)
			cygheap->ugid_cache.add_uid (id_val, uid);
		    }
		}
	      /* If primary_dns_name() is empty, we're likely running under an
		 NT4 domain, so we can't use LDAP.  For user accounts fall back
		 to NetUserGetInfo.  This isn't overly fast, but keep in mind
		 that NT4 domains are mostly replaced by AD these days. */
	      else
		{
		  WCHAR server[INTERNET_MAX_HOST_NAME_LENGTH + 3];
		  NET_API_STATUS nas;
		  PUSER_INFO_3 ui;

		  if (!get_logon_server (cygheap->dom.primary_flat_name (),
					 server, DS_IS_FLAT_NAME))
		    break;
		  nas = NetUserGetInfo (server, name, 3, (PBYTE *) &ui);
		  if (nas != NERR_Success)
		    {
		      debug_printf ("NetUserGetInfo(%W) %u", name, nas);
		      break;
		    }
		  /* Overwrite name to be sure case is same as in SAM */
		  wcscpy (name, ui->usri3_name);
		  gid = posix_offset + ui->usri3_primary_group_id;
		  home = cygheap->pg.get_home (ui, sid, dom, name,
					       fully_qualified_name);
		  shell = cygheap->pg.get_shell (ui, sid, dom, name,
						 fully_qualified_name);
		  gecos = cygheap->pg.get_gecos (ui, sid, dom, name,
						 fully_qualified_name);
		}
	    }
	  /* Otherwise check account domain (local SAM).*/
	  else
	    {
	      NET_API_STATUS nas;
	      PUSER_INFO_3 ui;
	      PLOCALGROUP_INFO_1 gi;
	      char *pgrp = NULL;
	      char *uxid = NULL;

	      if (acc_type == SidTypeUser)
		{
		  nas = NetUserGetInfo (NULL, name, 3, (PBYTE *) &ui);
		  if (nas != NERR_Success)
		    {
		      debug_printf ("NetUserGetInfo(%W) %u", name, nas);
		      break;
		    }
		  /* Overwrite name to be sure case is same as in SAM */
		  wcscpy (name, ui->usri3_name);
		  /* Fetch user attributes. */
		  home = cygheap->pg.get_home (ui, sid, dom, name,
					       fully_qualified_name);
		  shell = cygheap->pg.get_shell (ui, sid, dom, name,
						 fully_qualified_name);
		  gecos = cygheap->pg.get_gecos (ui, sid, dom, name,
						 fully_qualified_name);
		  uxid = fetch_from_description (ui->usri3_comment,
						 L"unix=\"", 6);
		  pgrp = fetch_from_description (ui->usri3_comment,
						 L"group=\"", 7);
		}
	      else /* acc_type == SidTypeAlias */
		{
		  nas = NetLocalGroupGetInfo (NULL, name, 1, (PBYTE *) &gi);
		  if (nas != NERR_Success)
		    {
		      debug_printf ("NetLocalGroupGetInfo(%W) %u", name, nas);
		      break;
		    }
		  /* Overwrite name to be sure case is same as in SAM */
		  wcscpy (name, gi->lgrpi1_name);
		  /* Fetch unix gid from comment field. */
		  uxid = fetch_from_description (gi->lgrpi1_comment,
						 L"unix=\"", 6);
		}

	      if (acc_type == SidTypeUser)
		NetApiBufferFree (ui);
	      else
		NetApiBufferFree (gi);
	      if (pgrp)
		{
		  /* Set primary group from the "Description" field.  Prepend
		     account domain if this is a domain member machine or the
		     db_prefix setting requires it. */
		  char gname[2 * DNLEN + strlen (pgrp) + 1], *gp = gname;
		  struct group *gr;

		  if (cygheap->dom.member_machine ()
		      || !cygheap->pg.nss_prefix_auto ())
		    {
		      gp = gname
			   + sys_wcstombs (gname, sizeof gname,
					   cygheap->dom.account_flat_name ());
		      *gp++ = cygheap->pg.nss_separator ()[0];
		    }
		  stpcpy (gp, pgrp);
		  if ((gr = internal_getgrnam (gname, cldap)))
		    gid = gr->gr_gid;
		}
	      char *e;
	      if (!pldap && uxid && ((id_val = strtoul (uxid, &e, 10)), !*e))
		{
		  if (acc_type == SidTypeUser)
		    {
		      if (cygheap->ugid_cache.get_uid (id_val) == ILLEGAL_UID)
			cygheap->ugid_cache.add_uid (id_val, uid);
		    }
		  else if (cygheap->ugid_cache.get_gid (id_val) == ILLEGAL_GID)
		    cygheap->ugid_cache.add_gid (id_val, uid);
		}
	      if (pgrp)
		free (pgrp);
	      if (uxid)
		free (uxid);
	    }
	  break;
	case SidTypeWellKnownGroup:
	  fully_qualified_name = (cygheap->pg.nss_prefix_always ()
		  /* NT SERVICE Account */
		  || (sid_id_auth (sid) == 5 /* SECURITY_NT_AUTHORITY */
		      && sid_sub_auth (sid, 0) == SECURITY_SERVICE_ID_BASE_RID)
		  /* Microsoft Account */
		  || sid_id_auth (sid) == 11);
#ifdef INTERIX_COMPATIBLE
	  if (sid_id_auth (sid) == 5 /* SECURITY_NT_AUTHORITY */
	      && sid_sub_auth_count (sid) > 1)
	    {
	      uid = 0x1000 * sid_sub_auth (sid, 0)
		    + (sid_sub_auth_rid (sid) & 0xffff);
	      fully_qualified_name = true;
	    }
	  else
	    uid = 0x10000 + 0x100 * sid_id_auth (sid)
		  + (sid_sub_auth_rid (sid) & 0xff);
#else
	  if (sid_id_auth (sid) == 15 /* SECURITY_APP_PACKAGE_AUTHORITY */)
	    uid = 0x10000 + 0x100 * sid_id_auth (sid)
		  + 0x10 * sid_sub_auth (sid, 0)
		  + (sid_sub_auth_rid (sid) & 0xf);
	  else if (sid_id_auth (sid) != 5 /* SECURITY_NT_AUTHORITY */)
	    uid = 0x10000 + 0x100 * sid_id_auth (sid)
		  + (sid_sub_auth_rid (sid) & 0xff);
	  else if (sid_sub_auth (sid, 0) < SECURITY_PACKAGE_BASE_RID
		   || sid_sub_auth (sid, 0) > SECURITY_MAX_BASE_RID)
	    uid = sid_sub_auth_rid (sid) & 0x7ff;
	  else
	    {
	      uid = 0x1000 * sid_sub_auth (sid, 0)
		    + (sid_sub_auth_rid (sid) & 0xffff);
	    }
#endif
	  /* Special case for "NULL SID", S-1-0-0 and "Everyone", S-1-1-0.
	     Never return "NULL SID" or Everyone as user or group. */
	  if (uid == 0x10000 || uid == 0x10100)
	    return NULL;
	  break;
	case SidTypeLogonSession:
	  /* Starting with Windows 10, LookupAccountSid/Name return valid
	     info for the login session with new SID_NAME_USE value
	     SidTypeLogonSession.  To return the same info as on
	     pre-Windows 10, we have to handle this type explicitely here
	     now and convert the name to "CurrentSession". */
	  get_logon_sid ();
	  if (PSID (logon_sid) == NO_SID)
	    return NULL;
	  if (RtlEqualSid (sid, logon_sid))
	    {
	      uid = 0xfff;
	      wcpcpy (name = namebuf, L"CurrentSession");
	    }
	  else
	    {
	      uid = 0xffe;
	      wcpcpy (name = namebuf, L"OtherSession");
	    }
	  break;
	case SidTypeLabel:
	  uid = 0x60000 + sid_sub_auth_rid (sid);
	  fully_qualified_name = cygheap->pg.nss_prefix_always ();
	  break;
	default:
	  return NULL;
	}
    }
  else if (sid_id_auth (sid) == 0 && sid_sub_auth (sid, 0) == 0xfffe)
    {
      /* Special case "nobody" for reproducible construction of a
	 nobody SID for WinFsp and similar services.  We use the
	 value 65534 which is -2 with 16 bit uid/gids. */
      uid = gid = 0xfffe;
      wcpcpy (dom, L"no");
      wcpcpy (name = namebuf, L"body");
      fully_qualified_name = true;
      acc_type = SidTypeUnknown;
    }
  else if (sid_id_auth (sid) == 12 && sid_sub_auth (sid, 0) == 1)
    {
      /* Special AzureAD group SID which can't be resolved by
         LookupAccountSid (ERROR_NONE_MAPPED).  This is only allowed
	 as group entry, not as passwd entry. */
      if (is_passwd ())
	return NULL;
      uid = gid = 0x1001;
      wcpcpy (dom, L"AzureAD");
      wcpcpy (name = namebuf, L"Group");
      fully_qualified_name = true;
      acc_type = SidTypeUnknown;
    }
  else if (sid_id_auth (sid) == 5 /* SECURITY_NT_AUTHORITY */
	   && sid_sub_auth (sid, 0) == SECURITY_LOGON_IDS_RID)
    {
      /* Logon ID. Mine or other? */
      get_logon_sid ();
      if (PSID (logon_sid) == NO_SID)
	return NULL;
      if (RtlEqualSid (sid, logon_sid))
	{
	  uid = 0xfff;
	  wcpcpy (name = namebuf, L"CurrentSession");
	}
      else
	{
	  uid = 0xffe;
	  wcpcpy (name = namebuf, L"OtherSession");
	}
      acc_type = SidTypeUnknown;
    }
  else if (sid_id_auth (sid) == 18)
    {
      /* Authentication assertion SIDs.

         Available when using a 2012R2 DC, but not supported by
	 LookupAccountXXX on pre Windows 8/2012 machines */
      uid = 0x11200 + sid_sub_auth_rid (sid);
      wcpcpy (name = namebuf, sid_sub_auth_rid (sid) == 1
	      ? (PWCHAR) L"Authentication authority asserted identity"
	      : (PWCHAR) L"Service asserted identity");
      fully_qualified_name = false;
      acc_type = SidTypeUnknown;
    }
  else if (sid_id_auth (sid) == 22)
    {
      /* Samba UNIX Users/Groups

         This *might* collide with a posix_offset of some trusted domain.
         It's just very unlikely. */
      uid = MAP_UNIX_TO_CYGWIN_ID (sid_sub_auth_rid (sid));
      /* Unfortunately we have no access to the file server from here,
	 so we can't generate correct user names. */
      p = wcpcpy (dom, L"Unix_");
      wcpcpy (p, sid_sub_auth (sid, 0) == 1 ? L"User" : L"Group");
      __small_swprintf (name = namebuf, L"%d", uid & UNIX_POSIX_MASK);
      fully_qualified_name = true;
      acc_type = SidTypeUnknown;
    }
  else
    {
      if (cygheap->dom.member_machine ()
	  && sid_id_auth (sid) == 5 /* SECURITY_NT_AUTHORITY */
	  && sid_sub_auth (sid, 0) == SECURITY_NT_NON_UNIQUE)
	{
	  /* Check if we know the domain.  If so, create a passwd/group
	     entry with domain prefix and RID as username. */
	  PDS_DOMAIN_TRUSTSW td = NULL;

	  sid_sub_auth_count (sid) = sid_sub_auth_count (sid) - 1;
	  if (RtlEqualSid (sid, cygheap->dom.primary_sid ()))
	    {
	      domain = cygheap->dom.primary_flat_name ();
	      posix_offset = PRIMARY_POSIX_OFFSET;
	    }
	  else
	    for (ULONG idx = 0; (td = cygheap->dom.trusted_domain (idx)); ++idx)
	      if (td->DomainSid && RtlEqualSid (sid, td->DomainSid))
		{
		  domain = td->NetbiosDomainName;
		  posix_offset = fetch_posix_offset (td, &loc_ldap);
		  break;
		}
	  sid_sub_auth_count (sid) = sid_sub_auth_count (sid) + 1;
	}
      if (domain)
	{
	  wcscpy (dom, domain);
	  __small_swprintf (name = namebuf, L"%W(%u)",
			    is_group () ? L"Group" : L"User",
			    sid_sub_auth_rid (sid));
	  uid = posix_offset + sid_sub_auth_rid (sid);
	}
      else
	{
	  wcpcpy (dom, L"Unknown");
	  wcpcpy (name = namebuf, is_group () ? L"Group" : L"User");
	}
      fully_qualified_name = true;
      acc_type = SidTypeUnknown;
    }

  tmp_pathbuf tp;
  char *linebuf = tp.c_get ();
  char *line = NULL;

  WCHAR posix_name[UNLEN + 1 + DNLEN + 1];
  p = posix_name;
  if (gid == ILLEGAL_GID)
    gid = uid;
  if (fully_qualified_name)
    p = wcpcpy (wcpcpy (p, dom), cygheap->pg.nss_separator ());
  wcpcpy (p, name);

  if (is_group ())
    __small_sprintf (linebuf, "%W:%s:%u:",
		     posix_name, sid.string ((char *) sidstr), uid);
  /* For non-users, create a passwd entry which doesn't allow interactive
     logon.  Unless it's the SYSTEM account.  This conveniently allows to
     logon interactively as SYSTEM for debugging purposes. */
  else if (acc_type != SidTypeUser && sid != well_known_system_sid)
    __small_sprintf (linebuf, "%W:*:%u:%u:U-%W\\%W,%s:/:/sbin/nologin",
		     posix_name, uid, gid,
		     dom, name,
		     sid.string ((char *) sidstr));
  else
    __small_sprintf (linebuf, "%W:*:%u:%u:%s%sU-%W\\%W,%s:%s%W:%s",
		     posix_name, uid, gid,
		     gecos ?: "", gecos ? "," : "",
		     dom, name,
		     sid.string ((char *) sidstr),
		     home ?: "/home/", home ? L"" : name,
		     shell ?: "/bin/bash");
  if (gecos)
    free (gecos);
  if (home)
    free (home);
  if (shell)
    free (shell);
  line = cstrdup (linebuf);
  debug_printf ("line: <%s>", line);
  return line;
}

client_request_pwdgrp::client_request_pwdgrp (fetch_user_arg_t &arg, bool group)
  : client_request (CYGSERVER_REQUEST_PWDGRP, &_parameters, sizeof (_parameters))
{
  size_t len = 0;
  char *p;

  _parameters.in.group = group;
  _parameters.in.type = arg.type;
  switch (arg.type)
    {
    case SID_arg:
      RtlCopySid (sizeof (DBGSID), (PSID) &_parameters.in.arg.sid, *arg.sid);
      len = RtlLengthSid (*arg.sid);
      break;
    case NAME_arg:
      p = stpcpy (_parameters.in.arg.name, arg.name);
      len = p - _parameters.in.arg.name + 1;
      break;
    case ID_arg:
      _parameters.in.arg.id = arg.id;
      len = sizeof (uint32_t);
      break;
    default:
      api_fatal ("Fetching account info from cygserver with wrong arg.type "
		 "%d", arg.type);
    }
  msglen (__builtin_offsetof (struct _pwdgrp_param_t::_pwdgrp_in_t, arg) + len);
}

char *
pwdgrp::fetch_account_from_cygserver (fetch_user_arg_t &arg)
{
  client_request_pwdgrp request (arg, is_group ());
  if (request.make_request () == -1 || request.error_code ())
    {
      /* Cygserver not running?  Don't try again.  This will automatically
	 avoid an endless loop in cygserver itself. */
      if (request.error_code () == ENOSYS)
	cygheap->pg.nss_disable_cygserver_caching ();
      return NULL;
    }
  if (!request.line ())
    return NULL;
  return cstrdup (request.line ());
}

void *
pwdgrp::add_account_from_cygserver (cygpsid &sid)
{
  /* No, Everyone is no group in terms of POSIX. */
  if (sid_id_auth (sid) == 1 /* SECURITY_WORLD_SID_AUTHORITY */
      && sid_sub_auth (sid, 0) == SECURITY_WORLD_RID)
    return NULL;
  fetch_user_arg_t arg;
  arg.type = SID_arg;
  arg.sid = &sid;
  char *line = fetch_account_from_cygserver (arg);
  return add_account_post_fetch (line, true);
}

void *
pwdgrp::add_account_from_cygserver (const char *name)
{
  fetch_user_arg_t arg;
  arg.type = NAME_arg;
  arg.name = name;
  char *line = fetch_account_from_cygserver (arg);
  return add_account_post_fetch (line, true);
}

void *
pwdgrp::add_account_from_cygserver (uint32_t id)
{
  fetch_user_arg_t arg;
  arg.type = ID_arg;
  arg.id = id;
  char *line = fetch_account_from_cygserver (arg);
  return add_account_post_fetch (line, true);
}