|
root / base / usr / src / cmd / fs.d / nfs / nfsmapid
nfsmapid Plain Text 2237 lines 51.1 KB
   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
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Copyright 2020 Joyent, Inc.


FSTYPE	  =	nfs
TYPEPROG  =	nfsmapid
TESTPROG  =	nfsmapid_test
ATTMK	  =	$(TYPEPROG)

include		../../Makefile.fstype

LDLIBS   +=	-L$(ROOT)/usr/lib/nfs -R/usr/lib/nfs
$(TYPEPROG) : LDLIBS += -lnsl -lmapid -ldtrace -lidmap
COMMON    =	nfs_resolve.o
SRCS	  =	nfsmapid.c ../lib/nfs_resolve.c nfsmapid_server.c
DSRC	  =	nfsmapid_dt.d
DOBJ	  =	$(DSRC:%.d=%.o)
OBJS	  =	nfsmapid.o nfsmapid_server.o $(COMMON)
CPPFLAGS +=	-I../lib -D_POSIX_PTHREAD_SEMANTICS

CERRWARN +=	-Wno-implicit-function-declaration
CERRWARN +=	-Wno-unused-variable
CERRWARN +=	-Wno-parentheses
CERRWARN +=	$(CNOWARN_UNINIT)

# not linted
SMATCH=off

all:		$(TYPEPROG) $(TESTPROG)

$(TYPEPROG):	$(OBJS) $(DSRC)
		$(COMPILE.d) -s $(DSRC) -o $(DOBJ) $(OBJS)
		$(LINK.c) $(ZIGNORE) -o $@ $(DOBJ) $(OBJS) $(LDLIBS)
		$(POST_PROCESS)

nfs_resolve.o:	../lib/nfs_resolve.c
		$(COMPILE.c) ../lib/nfs_resolve.c

TESTSRCS  =	nfsmapid_test.c
TESTOBJS  =	$(TESTSRCS:%.c=%.o)
TEST_OBJS =	$(TESTOBJS)

$(TESTPROG):	$(TEST_OBJS)
		$(LINK.c) -o $@ $(TEST_OBJS) $(LDLIBS)
		$(POST_PROCESS)

POFILE	  =	nfsmapid.po
catalog:	$(POFILE)

$(POFILE):	$(SRCS)
		$(RM) $@
		$(COMPILE.cpp) $(SRCS) > $@.i
		$(XGETTEXT) $(XGETFLAGS) $@.i
		sed "/^domain/d" messages.po > $@
		$(RM) $@.i messages.po

clean:
		$(RM) $(OBJS) $(TESTPROG) $(TESTOBJS) $(DOBJ) $(POFILE)
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stropts.h>
#include <signal.h>
#include <fcntl.h>
#include <door.h>
#include <thread.h>
#include <priv_utils.h>
#include <locale.h>
#include <strings.h>
#include <syslog.h>
#include <unistd.h>
#include <nfs/nfs4.h>
#include <nfs/nfsid_map.h>
#include <rpcsvc/daemon_utils.h>
#include <arpa/nameser.h>
#include <nfs/nfssys.h>
#include <errno.h>
#include <pwd.h>
#include <grp.h>

extern struct group *_uncached_getgrgid_r(gid_t, struct group *, char *, int);
extern struct group *_uncached_getgrnam_r(const char *, struct group *,
    char *, int);
extern struct passwd *_uncached_getpwuid_r(uid_t, struct passwd *, char *, int);
extern struct passwd *_uncached_getpwnam_r(const char *, struct passwd *,
    char *, int);

/*
 * seconds to cache nfsmapid domain info
 */
#define	NFSCFG_DEFAULT_DOMAIN_TMOUT	(5 * 60)
#define	NFSMAPID_DOOR   "/var/run/nfsmapid_door"

extern void	nfsmapid_func(void *, char *, size_t, door_desc_t *, uint_t);

extern void	check_domain(int);
extern void	idmap_kcall(int);
extern void	open_diag_file(void);

size_t		pwd_buflen = 0;
size_t		grp_buflen = 0;
thread_t	sig_thread;
static char	*MyName;

/*
 * nfscfg_domain_tmout is used by nfsv4-test scripts to query
 * the nfsmapid daemon for the proper timeout. Don't delete !
 */
time_t		 nfscfg_domain_tmout = NFSCFG_DEFAULT_DOMAIN_TMOUT;

/*
 * Processing for daemonization
 */
static void
daemonize(void)
{
	switch (fork()) {
		case -1:
			perror("nfsmapid: can't fork");
			exit(2);
			/* NOTREACHED */
		case 0:		/* child */
			break;

		default:	/* parent */
			_exit(0);
	}

	if (chdir("/") < 0)
		syslog(LOG_ERR, gettext("chdir /: %m"));

	/*
	 * Close stdin, stdout, and stderr.
	 * Open again to redirect input+output
	 */
	(void) close(0);
	(void) close(1);
	(void) close(2);
	(void) open("/dev/null", O_RDONLY);
	(void) open("/dev/null", O_WRONLY);
	(void) dup(1);
	(void) setsid();
}

/* ARGSUSED */
static void *
sig_handler(void *arg)
{
	siginfo_t	si;
	sigset_t	sigset;
	struct timespec	tmout;
	int		ret;

	tmout.tv_nsec = 0;
	(void) sigemptyset(&sigset);
	(void) sigaddset(&sigset, SIGHUP);
	(void) sigaddset(&sigset, SIGTERM);
#ifdef	DEBUG
	(void) sigaddset(&sigset, SIGINT);
#endif

	/*CONSTCOND*/
	while (1) {
		tmout.tv_sec = nfscfg_domain_tmout;
		if ((ret = sigtimedwait(&sigset, &si, &tmout)) != 0) {
			/*
			 * EAGAIN: no signals arrived during timeout.
			 * check/update config files and continue.
			 */
			if (ret == -1 && errno == EAGAIN) {
				check_domain(0);
				continue;
			}

			switch (si.si_signo) {
				case SIGHUP:
					check_domain(1);
					break;
#ifdef DEBUG
				case SIGINT:
					exit(0);
#endif
				case SIGTERM:
				default:
					exit(si.si_signo);
			}
		}
	}
	/*NOTREACHED*/
	return (NULL);
}

/*
 * Thread initialization. Mask out all signals we want our
 * signal handler to handle for us from any other threads.
 */
static void
thr_init(void)
{
	sigset_t sigset;
	long	 thr_flags = (THR_NEW_LWP|THR_DAEMON|THR_SUSPENDED);

	/*
	 * Before we kick off any other threads, mask out desired
	 * signals from main thread so that any subsequent threads
	 * don't receive said signals.
	 */
	(void) thr_sigsetmask(0, NULL, &sigset);
	(void) sigaddset(&sigset, SIGHUP);
	(void) sigaddset(&sigset, SIGTERM);
#ifdef	DEBUG
	(void) sigaddset(&sigset, SIGINT);
#endif
	(void) thr_sigsetmask(SIG_SETMASK, &sigset, NULL);

	/*
	 * Create the signal handler thread suspended ! We do things
	 * this way at setup time to minimize the probability of
	 * introducing any race conditions _if_ the process were to
	 * get a SIGHUP signal while creating a new DNS query thread
	 * in get_dns_txt_domain().
	 */
	if (thr_create(NULL, 0, sig_handler, 0, thr_flags, &sig_thread)) {
		syslog(LOG_ERR,
			gettext("Failed to create signal handling thread"));
		exit(4);
	}
}

static void
daemon_init(void)
{
	struct passwd pwd;
	struct group grp;
	char *pwd_buf;
	char *grp_buf;

	/*
	 * passwd/group reentrant interfaces limits
	 */
	pwd_buflen = (size_t)sysconf(_SC_GETPW_R_SIZE_MAX);
	grp_buflen = (size_t)sysconf(_SC_GETGR_R_SIZE_MAX);

	/*
	 * MT initialization is done first so that if there is the
	 * need to fire an additional thread to continue to query
	 * DNS, that thread is started off with the main thread's
	 * sigmask.
	 */
	thr_init();

	/*
	 * Determine nfsmapid domain.
	 */
	check_domain(0);

	/*
	 * In the case of nfsmapid running diskless, it is important
	 * to get the initial connections to the nameservices
	 * established to prevent problems like opening a devfs
	 * node to contact a nameservice being blocked by the
	 * resolution of an active devfs lookup.
	 * First issue a set*ent to "open" the databases and then
	 * get an entry and finally lookup a bogus entry to trigger
	 * any lazy opens.
	 */
	setpwent();
	setgrent();
	(void) getpwent();
	(void) getgrent();
	if ((pwd_buf = malloc(pwd_buflen)) == NULL)
		return;

	(void) _uncached_getpwnam_r("NF21dmvP", &pwd, pwd_buf, pwd_buflen);
	(void) _uncached_getpwuid_r(1181794, &pwd, pwd_buf, pwd_buflen);

	if ((grp_buf = realloc(pwd_buf, grp_buflen)) == NULL) {
		free(pwd_buf);
		return;
	}

	(void) _uncached_getgrnam_r("NF21dmvP", &grp, grp_buf, grp_buflen);
	(void) _uncached_getgrgid_r(1181794, &grp, grp_buf, grp_buflen);
	free(grp_buf);
}

static int
start_svcs(void)
{
	int doorfd = -1;
#ifdef DEBUG
	int dfd;
#endif

	if ((doorfd = door_create(nfsmapid_func, NULL,
	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
		syslog(LOG_ERR, "Unable to create door: %m\n");
		return (1);
	}

#ifdef DEBUG
	/*
	 * Create a file system path for the door
	 */
	if ((dfd = open(NFSMAPID_DOOR, O_RDWR|O_CREAT|O_TRUNC,
				S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) {
		syslog(LOG_ERR, "Unable to open %s: %m\n", NFSMAPID_DOOR);
		(void) close(doorfd);
		return (1);
	}

	/*
	 * Clean up any stale associations
	 */
	(void) fdetach(NFSMAPID_DOOR);

	/*
	 * Register in namespace to pass to the kernel to door_ki_open
	 */
	if (fattach(doorfd, NFSMAPID_DOOR) == -1) {
		syslog(LOG_ERR, "Unable to fattach door: %m\n");
		(void) close(dfd);
		(void) close(doorfd);
		return (1);
	}
	(void) close(dfd);
#endif

	/*
	 * Now that we're actually running, go
	 * ahead and flush the kernel flushes
	 * Pass door name to kernel for door_ki_open
	 */
	idmap_kcall(doorfd);

	/*
	 * Wait for incoming calls
	 */
	/*CONSTCOND*/
	while (1)
		(void) pause();

	syslog(LOG_ERR, gettext("Door server exited"));
	return (10);
}

/* ARGSUSED */
int
main(int argc, char **argv)
{
	MyName = argv[0];

	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);

	/* _check_services() framework setup */
	(void) _create_daemon_lock(NFSMAPID, DAEMON_UID, DAEMON_GID);

	/*
	 * Open diag file in /var/run while we've got the perms
	 */
	open_diag_file();

	/*
	 * Initialize the daemon to basic + sys_nfs
	 */
#ifndef	DEBUG
	if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET,
	    DAEMON_UID, DAEMON_GID, PRIV_SYS_NFS, (char *)NULL) == -1) {
		(void) fprintf(stderr, gettext("%s PRIV_SYS_NFS privilege "
			"missing\n"), MyName);
		exit(1);
	}
#endif

	/*
	 * Take away a subset of basic, while this is not the absolute
	 * minimum, it is important that it is unique among other
	 * daemons to insure that we get a unique cred that will
	 * result in a unique open_owner.  If not, we run the risk
	 * of a diskless client deadlocking with a thread holding
	 * the open_owner seqid lock while upcalling the daemon.
	 * XXX This restriction will go away once we stop holding
	 * XXX open_owner lock across rfscalls!
	 */
	(void) priv_set(PRIV_OFF, PRIV_PERMITTED,
		PRIV_FILE_LINK_ANY, PRIV_PROC_SESSION,
		(char *)NULL);

#ifndef DEBUG
	daemonize();
	switch (_enter_daemon_lock(NFSMAPID)) {
		case 0:
			break;

		case -1:
			syslog(LOG_ERR, "error locking for %s: %s", NFSMAPID,
			    strerror(errno));
			exit(3);

		default:
			/* daemon was already running */
			exit(0);
	}
#endif
	openlog(MyName, LOG_PID | LOG_NDELAY, LOG_DAEMON);

	/* Initialize daemon subsystems */
	daemon_init();

	/* start services */
	return (start_svcs());
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

provider nfsmapid {
	probe daemon__domain(string);
};

#pragma D attributes Private/Private/Common provider nfsmapid provider
#pragma D attributes Private/Private/Common provider nfsmapid module
#pragma D attributes Private/Private/Common provider nfsmapid function
#pragma D attributes Private/Private/Common provider nfsmapid name
#pragma D attributes Private/Private/Common provider nfsmapid args
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
 */


/*
 * Door server routines for nfsmapid daemon
 * Translate NFSv4 users and groups between numeric and string values
 */
#include <stdio.h>
#include <stdlib.h>
#include <alloca.h>
#include <signal.h>
#include <libintl.h>
#include <limits.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <memory.h>
#include <pwd.h>
#include <grp.h>
#include <door.h>
#include <syslog.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <deflt.h>
#include <nfs/nfs4.h>
#include <nfs/nfssys.h>
#include <nfs/nfsid_map.h>
#include <nfs/mapid.h>
#include <sys/sdt.h>
#include <sys/idmap.h>
#include <idmap.h>
#include <sys/fs/autofs.h>
#include <sys/mkdev.h>
#include "nfs_resolve.h"

#define		UID_MAX_STR_LEN		11	/* Digits in UID_MAX + 1 */
#define		DIAG_FILE		"/var/run/nfs4_domain"

/*
 * idmap_kcall() takes a door descriptor as it's argument when we
 * need to (re)establish the in-kernel door handles. When we only
 * want to flush the id kernel caches, we don't redo the door setup.
 */
#define		FLUSH_KCACHES_ONLY	(int)-1

FILE		*n4_fp;
int		 n4_fd;

extern size_t	pwd_buflen;
extern size_t	grp_buflen;
extern thread_t	sig_thread;

/*
 * Prototypes
 */
extern void	 check_domain(int);
extern void	 idmap_kcall(int);
extern int	 _nfssys(int, void *);
extern int	 valid_domain(const char *);
extern int	 validate_id_str(const char *);
extern int	 extract_domain(char *, char **, char **);
extern void	 update_diag_file(char *);
extern void	*cb_update_domain(void *);
extern int	 cur_domain_null(void);

void
nfsmapid_str_uid(struct mapid_arg *argp, size_t arg_size)
{
	struct mapid_res result;
	struct passwd	 pwd;
	struct passwd	*pwd_ptr;
	int		 pwd_rc;
	char		*pwd_buf;
	char		*user;
	char		*domain;
	idmap_stat	 rc;

	if (argp->u_arg.len <= 0 || arg_size < MAPID_ARG_LEN(argp->u_arg.len)) {
		result.status = NFSMAPID_INVALID;
		result.u_res.uid = UID_NOBODY;
		goto done;
	}

	if (!extract_domain(argp->str, &user, &domain)) {
		unsigned long id;

		/*
		 * Invalid "user@domain" string. Still, the user
		 * part might be an encoded uid, so do a final check.
		 * Remember, domain part of string was not set since
		 * not a valid string.
		 */
		if (!validate_id_str(user)) {
			result.status = NFSMAPID_UNMAPPABLE;
			result.u_res.uid = UID_NOBODY;
			goto done;
		}

		errno = 0;
		id = strtoul(user, (char **)NULL, 10);

		/*
		 * We don't accept ephemeral ids from the wire.
		 */
		if (errno || id > UID_MAX) {
			result.status = NFSMAPID_UNMAPPABLE;
			result.u_res.uid = UID_NOBODY;
			goto done;
		}

		result.u_res.uid = (uid_t)id;
		result.status = NFSMAPID_NUMSTR;
		goto done;
	}

	/*
	 * String properly constructed. Now we check for domain and
	 * group validity.
	 */
	if (!cur_domain_null() && !valid_domain(domain)) {
		/*
		 * If the domain part of the string does not
		 * match the NFS domain, try to map it using
		 * idmap service.
		 */
		rc = idmap_getuidbywinname(user, domain, 0, &result.u_res.uid);
		if (rc != IDMAP_SUCCESS) {
			result.status = NFSMAPID_BADDOMAIN;
			result.u_res.uid = UID_NOBODY;
			goto done;
		}
		result.status = NFSMAPID_OK;
		goto done;
	}

	if ((pwd_buf = malloc(pwd_buflen)) == NULL ||
	    (pwd_rc = getpwnam_r(user, &pwd, pwd_buf, pwd_buflen, &pwd_ptr))
	    != 0 || pwd_ptr == NULL) {

		if (pwd_buf == NULL || pwd_rc != 0)
			result.status = NFSMAPID_INTERNAL;
		else {
			/*
			 * Not a valid user
			 */
			result.status = NFSMAPID_NOTFOUND;
			free(pwd_buf);
		}
		result.u_res.uid = UID_NOBODY;
		goto done;
	}

	/*
	 * Valid user entry
	 */
	result.u_res.uid = pwd.pw_uid;
	result.status = NFSMAPID_OK;
	free(pwd_buf);
done:
	(void) door_return((char *)&result, sizeof (struct mapid_res), NULL, 0);
}

/* ARGSUSED1 */
void
nfsmapid_uid_str(struct mapid_arg *argp, size_t arg_size)
{
	struct mapid_res	 result;
	struct mapid_res	*resp;
	struct passwd		 pwd;
	struct passwd		 *pwd_ptr;
	char			*pwd_buf = NULL;
	char			*idmap_buf = NULL;
	uid_t			 uid = argp->u_arg.uid;
	size_t			 uid_str_len;
	char			*pw_str;
	size_t			 pw_str_len;
	char			*at_str;
	size_t			 at_str_len;
	char			 dom_str[DNAMEMAX];
	size_t			 dom_str_len;
	idmap_stat		 rc;

	if (uid == (uid_t)-1) {
		/*
		 * Sentinel uid is not a valid id
		 */
		resp = &result;
		resp->status = NFSMAPID_BADID;
		resp->u_res.len = 0;
		goto done;
	}

	/*
	 * Make local copy of domain for further manipuation
	 * NOTE: mapid_get_domain() returns a ptr to TSD.
	 */
	if (cur_domain_null()) {
		dom_str_len = 0;
		dom_str[0] = '\0';
	} else {
		dom_str_len = strlcpy(dom_str, mapid_get_domain(), DNAMEMAX);
	}

	/*
	 * If uid is ephemeral then resolve it using idmap service
	 */
	if (uid > UID_MAX) {
		rc = idmap_getwinnamebyuid(uid, 0, &idmap_buf, NULL);
		if (rc != IDMAP_SUCCESS) {
			/*
			 * We don't put stringified ephemeral uids on
			 * the wire.
			 */
			resp = &result;
			resp->status = NFSMAPID_UNMAPPABLE;
			resp->u_res.len = 0;
			goto done;
		}

		/*
		 * idmap_buf is already in the desired form i.e. name@domain
		 */
		pw_str = idmap_buf;
		pw_str_len = strlen(pw_str);
		at_str_len = dom_str_len = 0;
		at_str = "";
		dom_str[0] = '\0';
		goto gen_result;
	}

	/*
	 * Handling non-ephemeral uids
	 *
	 * We want to encode the uid into a literal string... :
	 *
	 *	- upon failure to allocate space from the heap
	 *	- if there is no current domain configured
	 *	- if there is no such uid in the passwd DB's
	 */
	if ((pwd_buf = malloc(pwd_buflen)) == NULL || dom_str_len == 0 ||
	    getpwuid_r(uid, &pwd, pwd_buf, pwd_buflen, &pwd_ptr) != 0 ||
	    pwd_ptr == NULL) {

		/*
		 * If we could not allocate from the heap, try
		 * allocating from the stack as a last resort.
		 */
		if (pwd_buf == NULL && (pwd_buf =
		    alloca(MAPID_RES_LEN(UID_MAX_STR_LEN))) == NULL) {
			resp = &result;
			resp->status = NFSMAPID_INTERNAL;
			resp->u_res.len = 0;
			goto done;
		}

		/*
		 * Constructing literal string without '@' so that
		 * we'll know that it's not a user, but rather a
		 * uid encoded string.
		 */
		pw_str = pwd_buf;
		(void) sprintf(pw_str, "%u", uid);
		pw_str_len = strlen(pw_str);
		at_str_len = dom_str_len = 0;
		at_str = "";
		dom_str[0] = '\0';
	} else {
		/*
		 * Otherwise, we construct the "user@domain" string if
		 * it's not already in that form.
		 */
		pw_str = pwd.pw_name;
		pw_str_len = strlen(pw_str);
		if (strchr(pw_str, '@') == NULL) {
			at_str = "@";
			at_str_len = 1;
		} else {
			at_str_len = dom_str_len = 0;
			at_str = "";
			dom_str[0] = '\0';
		}
	}

gen_result:
	uid_str_len = pw_str_len + at_str_len + dom_str_len;
	if ((resp = alloca(MAPID_RES_LEN(uid_str_len))) == NULL) {
		resp = &result;
		resp->status = NFSMAPID_INTERNAL;
		resp->u_res.len = 0;
		goto done;
	}
	/* LINTED format argument to sprintf */
	(void) sprintf(resp->str, "%s%s%s", pw_str, at_str, dom_str);
	resp->u_res.len = uid_str_len;
	if (pwd_buf)
		free(pwd_buf);
	if (idmap_buf)
		idmap_free(idmap_buf);
	resp->status = NFSMAPID_OK;

done:
	/*
	 * There is a chance that the door_return will fail because the
	 * resulting string is too large, try to indicate that if possible
	 */
	if (door_return((char *)resp,
	    MAPID_RES_LEN(resp->u_res.len), NULL, 0) == -1) {
		resp->status = NFSMAPID_INTERNAL;
		resp->u_res.len = 0;
		(void) door_return((char *)&result, sizeof (struct mapid_res),
		    NULL, 0);
	}
}

void
nfsmapid_str_gid(struct mapid_arg *argp, size_t arg_size)
{
	struct mapid_res	result;
	struct group		grp;
	struct group		*grp_ptr;
	int			grp_rc;
	char			*grp_buf;
	char			*group;
	char			*domain;
	idmap_stat		rc;

	if (argp->u_arg.len <= 0 ||
	    arg_size < MAPID_ARG_LEN(argp->u_arg.len)) {
		result.status = NFSMAPID_INVALID;
		result.u_res.gid = GID_NOBODY;
		goto done;
	}

	if (!extract_domain(argp->str, &group, &domain)) {
		unsigned long id;

		/*
		 * Invalid "group@domain" string. Still, the
		 * group part might be an encoded gid, so do a
		 * final check. Remember, domain part of string
		 * was not set since not a valid string.
		 */
		if (!validate_id_str(group)) {
			result.status = NFSMAPID_UNMAPPABLE;
			result.u_res.gid = GID_NOBODY;
			goto done;
		}

		errno = 0;
		id = strtoul(group, (char **)NULL, 10);

		/*
		 * We don't accept ephemeral ids from the wire.
		 */
		if (errno || id > UID_MAX) {
			result.status = NFSMAPID_UNMAPPABLE;
			result.u_res.gid = GID_NOBODY;
			goto done;
		}

		result.u_res.gid = (gid_t)id;
		result.status = NFSMAPID_NUMSTR;
		goto done;
	}

	/*
	 * String properly constructed. Now we check for domain and
	 * group validity.
	 */
	if (!cur_domain_null() && !valid_domain(domain)) {
		/*
		 * If the domain part of the string does not
		 * match the NFS domain, try to map it using
		 * idmap service.
		 */
		rc = idmap_getgidbywinname(group, domain, 0, &result.u_res.gid);
		if (rc != IDMAP_SUCCESS) {
			result.status = NFSMAPID_BADDOMAIN;
			result.u_res.gid = GID_NOBODY;
			goto done;
		}
		result.status = NFSMAPID_OK;
		goto done;
	}

	if ((grp_buf = malloc(grp_buflen)) == NULL ||
	    (grp_rc = getgrnam_r(group, &grp, grp_buf, grp_buflen, &grp_ptr))
	    != 0 || grp_ptr == NULL) {

		if (grp_buf == NULL || grp_rc != 0)
			result.status = NFSMAPID_INTERNAL;
		else {
			/*
			 * Not a valid group
			 */
			result.status = NFSMAPID_NOTFOUND;
			free(grp_buf);
		}
		result.u_res.gid = GID_NOBODY;
		goto done;
	}

	/*
	 * Valid group entry
	 */
	result.status = NFSMAPID_OK;
	result.u_res.gid = grp.gr_gid;
	free(grp_buf);
done:
	(void) door_return((char *)&result, sizeof (struct mapid_res), NULL, 0);
}

/* ARGSUSED1 */
void
nfsmapid_gid_str(struct mapid_arg *argp, size_t arg_size)
{
	struct mapid_res	 result;
	struct mapid_res	*resp;
	struct group		 grp;
	struct group		*grp_ptr;
	char			*grp_buf = NULL;
	char			*idmap_buf = NULL;
	idmap_stat		 rc;
	gid_t			 gid = argp->u_arg.gid;
	size_t			 gid_str_len;
	char			*gr_str;
	size_t			 gr_str_len;
	char			*at_str;
	size_t			 at_str_len;
	char			 dom_str[DNAMEMAX];
	size_t			 dom_str_len;

	if (gid == (gid_t)-1) {
		/*
		 * Sentinel gid is not a valid id
		 */
		resp = &result;
		resp->status = NFSMAPID_BADID;
		resp->u_res.len = 0;
		goto done;
	}

	/*
	 * Make local copy of domain for further manipuation
	 * NOTE: mapid_get_domain() returns a ptr to TSD.
	 */
	if (cur_domain_null()) {
		dom_str_len = 0;
		dom_str[0] = '\0';
	} else {
		dom_str_len = strlen(mapid_get_domain());
		bcopy(mapid_get_domain(), dom_str, dom_str_len);
		dom_str[dom_str_len] = '\0';
	}

	/*
	 * If gid is ephemeral then resolve it using idmap service
	 */
	if (gid > UID_MAX) {
		rc = idmap_getwinnamebygid(gid, 0, &idmap_buf, NULL);
		if (rc != IDMAP_SUCCESS) {
			/*
			 * We don't put stringified ephemeral gids on
			 * the wire.
			 */
			resp = &result;
			resp->status = NFSMAPID_UNMAPPABLE;
			resp->u_res.len = 0;
			goto done;
		}

		/*
		 * idmap_buf is already in the desired form i.e. name@domain
		 */
		gr_str = idmap_buf;
		gr_str_len = strlen(gr_str);
		at_str_len = dom_str_len = 0;
		at_str = "";
		dom_str[0] = '\0';
		goto gen_result;
	}

	/*
	 * Handling non-ephemeral gids
	 *
	 * We want to encode the gid into a literal string... :
	 *
	 *	- upon failure to allocate space from the heap
	 *	- if there is no current domain configured
	 *	- if there is no such gid in the group DB's
	 */
	if ((grp_buf = malloc(grp_buflen)) == NULL || dom_str_len == 0 ||
	    getgrgid_r(gid, &grp, grp_buf, grp_buflen, &grp_ptr) != 0 ||
	    grp_ptr == NULL) {

		/*
		 * If we could not allocate from the heap, try
		 * allocating from the stack as a last resort.
		 */
		if (grp_buf == NULL && (grp_buf =
		    alloca(MAPID_RES_LEN(UID_MAX_STR_LEN))) == NULL) {
			resp = &result;
			resp->status = NFSMAPID_INTERNAL;
			resp->u_res.len = 0;
			goto done;
		}

		/*
		 * Constructing literal string without '@' so that
		 * we'll know that it's not a group, but rather a
		 * gid encoded string.
		 */
		gr_str = grp_buf;
		(void) sprintf(gr_str, "%u", gid);
		gr_str_len = strlen(gr_str);
		at_str_len = dom_str_len = 0;
		at_str = "";
		dom_str[0] = '\0';
	} else {
		/*
		 * Otherwise, we construct the "group@domain" string if
		 * it's not already in that form.
		 */
		gr_str = grp.gr_name;
		gr_str_len = strlen(gr_str);
		if (strchr(gr_str, '@') == NULL) {
			at_str = "@";
			at_str_len = 1;
		} else {
			at_str_len = dom_str_len = 0;
			at_str = "";
			dom_str[0] = '\0';
		}
	}

gen_result:
	gid_str_len = gr_str_len + at_str_len + dom_str_len;
	if ((resp = alloca(MAPID_RES_LEN(gid_str_len))) == NULL) {
		resp = &result;
		resp->status = NFSMAPID_INTERNAL;
		resp->u_res.len = 0;
		goto done;
	}
	/* LINTED format argument to sprintf */
	(void) sprintf(resp->str, "%s%s%s", gr_str, at_str, dom_str);
	resp->u_res.len = gid_str_len;
	if (grp_buf)
		free(grp_buf);
	if (idmap_buf)
		idmap_free(idmap_buf);
	resp->status = NFSMAPID_OK;

done:
	/*
	 * There is a chance that the door_return will fail because the
	 * resulting string is too large, try to indicate that if possible
	 */
	if (door_return((char *)resp,
	    MAPID_RES_LEN(resp->u_res.len), NULL, 0) == -1) {
		resp->status = NFSMAPID_INTERNAL;
		resp->u_res.len = 0;
		(void) door_return((char *)&result, sizeof (struct mapid_res),
		    NULL, 0);
	}
}

void
nfsmapid_server_netinfo(refd_door_args_t *referral_args, size_t arg_size)
{
	char *res;
	int res_size;
	int error;
	int srsz = 0;
	char host[MAXHOSTNAMELEN];
	utf8string	*nfsfsloc_args;
	refd_door_res_t	*door_res;
	refd_door_res_t	failed_res;
	struct nfs_fsl_info *nfs_fsloc_res;

	if (arg_size < sizeof (refd_door_args_t)) {
		failed_res.res_status = EINVAL;
		res = (char *)&failed_res;
		res_size = sizeof (refd_door_res_t);
		syslog(LOG_ERR,
		    "nfsmapid_server_netinfo failed: Invalid data\n");
		goto send_response;
	}

	if (decode_args(xdr_utf8string, (refd_door_args_t *)referral_args,
	    (caddr_t *)&nfsfsloc_args, sizeof (utf8string))) {
		syslog(LOG_ERR, "cannot allocate memory");
		failed_res.res_status = ENOMEM;
		failed_res.xdr_len = 0;
		res = (caddr_t)&failed_res;
		res_size = sizeof (refd_door_res_t);
		goto send_response;
	}

	if (nfsfsloc_args->utf8string_len >= MAXHOSTNAMELEN) {
		syslog(LOG_ERR, "argument too large");
		failed_res.res_status = EOVERFLOW;
		failed_res.xdr_len = 0;
		res = (caddr_t)&failed_res;
		res_size = sizeof (refd_door_res_t);
		goto send_response;
	}

	snprintf(host, nfsfsloc_args->utf8string_len + 1,
	    "%s", nfsfsloc_args->utf8string_val);

	nfs_fsloc_res =
	    get_nfs4ref_info(host, NFS_PORT, NFS_V4);

	xdr_free(xdr_utf8string, (char *)&nfsfsloc_args);

	if (nfs_fsloc_res) {
		error = 0;
		error = encode_res(xdr_nfs_fsl_info, &door_res,
		    (caddr_t)nfs_fsloc_res, &res_size);
		free_nfs4ref_info(nfs_fsloc_res);
		if (error != 0) {
			syslog(LOG_ERR,
			    "error allocating fs_locations "
			    "results buffer");
			failed_res.res_status = error;
			failed_res.xdr_len = srsz;
			res = (caddr_t)&failed_res;
			res_size = sizeof (refd_door_res_t);
		} else {
			door_res->res_status = 0;
			res = (caddr_t)door_res;
		}
	} else {
		failed_res.res_status = EINVAL;
		failed_res.xdr_len = 0;
		res = (caddr_t)&failed_res;
		res_size = sizeof (refd_door_res_t);
	}

send_response:
	srsz = res_size;
	errno = 0;

	error = door_return(res, res_size, NULL, 0);
	if (errno == E2BIG) {
		failed_res.res_status = EOVERFLOW;
		failed_res.xdr_len = srsz;
		res = (caddr_t)&failed_res;
		res_size = sizeof (refd_door_res_t);
	} else {
		res = NULL;
		res_size = 0;
	}

	door_return(res, res_size, NULL, 0);
}

/* ARGSUSED */
void
nfsmapid_func(void *cookie, char *argp, size_t arg_size,
						door_desc_t *dp, uint_t n_desc)
{
	struct mapid_arg	*mapargp;
	struct mapid_res	mapres;
	refd_door_args_t	*referral_args;

	/*
	 * Make sure we have a valid argument
	 */
	if (arg_size < sizeof (struct mapid_arg)) {
		mapres.status = NFSMAPID_INVALID;
		mapres.u_res.len = 0;
		(void) door_return((char *)&mapres, sizeof (struct mapid_res),
		    NULL, 0);
		return;
	}

	/* LINTED pointer cast */
	mapargp = (struct mapid_arg *)argp;
	referral_args = (refd_door_args_t *)argp;
	switch (mapargp->cmd) {
	case NFSMAPID_STR_UID:
		nfsmapid_str_uid(mapargp, arg_size);
		return;
	case NFSMAPID_UID_STR:
		nfsmapid_uid_str(mapargp, arg_size);
		return;
	case NFSMAPID_STR_GID:
		nfsmapid_str_gid(mapargp, arg_size);
		return;
	case NFSMAPID_GID_STR:
		nfsmapid_gid_str(mapargp, arg_size);
		return;
	case NFSMAPID_SRV_NETINFO:
		nfsmapid_server_netinfo(referral_args, arg_size);
	default:
		break;
	}
	mapres.status = NFSMAPID_INVALID;
	mapres.u_res.len = 0;
	(void) door_return((char *)&mapres, sizeof (struct mapid_res), NULL, 0);
}

/*
 * mapid_get_domain() always returns a ptr to TSD, so the
 * check for a NULL domain is not a simple comparison with
 * NULL but we need to check the contents of the TSD data.
 */
int
cur_domain_null(void)
{
	char	*p;

	if ((p = mapid_get_domain()) == NULL)
		return (1);

	return (p[0] == '\0');
}

int
extract_domain(char *cp, char **upp, char **dpp)
{
	/*
	 * Caller must insure that the string is valid
	 */
	*upp = cp;

	if ((*dpp = strchr(cp, '@')) == NULL)
		return (0);
	*(*dpp)++ = '\0';
	return (1);
}

int
valid_domain(const char *dom)
{
	const char	*whoami = "valid_domain";

	if (!mapid_stdchk_domain(dom)) {
		syslog(LOG_ERR, gettext("%s: Invalid inbound domain name %s."),
		    whoami, dom);
		return (0);
	}

	/*
	 * NOTE: mapid_get_domain() returns a ptr to TSD.
	 */
	return (strcasecmp(dom, mapid_get_domain()) == 0);
}

int
validate_id_str(const char *id)
{
	while (*id) {
		if (!isdigit(*id++))
			return (0);
	}
	return (1);
}

void
idmap_kcall(int door_id)
{
	struct nfsidmap_args args;

	if (door_id >= 0) {
		args.state = 1;
		args.did = door_id;
	} else {
		args.state = 0;
		args.did = 0;
	}
	(void) _nfssys(NFS_IDMAP, &args);
}

/*
 * Get the current NFS domain.
 *
 * If nfsmapid_domain is set in NFS SMF, then it is the NFS domain;
 * otherwise, the DNS domain is used.
 */
void
check_domain(int sighup)
{
	const char	*whoami = "check_domain";
	static int	 setup_done = 0;
	static cb_t	 cb;

	/*
	 * Construct the arguments to be passed to libmapid interface
	 * If called in response to a SIGHUP, reset any cached DNS TXT
	 * RR state.
	 */
	cb.fcn = cb_update_domain;
	cb.signal = sighup;
	mapid_reeval_domain(&cb);

	/*
	 * Restart the signal handler thread if we're still setting up
	 */
	if (!setup_done) {
		setup_done = 1;
		if (thr_continue(sig_thread)) {
			syslog(LOG_ERR, gettext("%s: Fatal error: signal "
			    "handler thread could not be restarted."), whoami);
			exit(6);
		}
	}
}

/*
 * Need to be able to open the DIAG_FILE before nfsmapid(8)
 * releases it's root priviledges. The DIAG_FILE then remains
 * open for the duration of this nfsmapid instance via n4_fd.
 */
void
open_diag_file()
{
	static int	msg_done = 0;

	if ((n4_fp = fopen(DIAG_FILE, "w+")) != NULL) {
		n4_fd = fileno(n4_fp);
		return;
	}

	if (msg_done)
		return;

	syslog(LOG_ERR, "Failed to create %s. Enable syslog "
	    "daemon.debug for more info", DIAG_FILE);
	msg_done = 1;
}

/*
 * When a new domain name is configured, save to DIAG_FILE
 * and log to syslog, with LOG_DEBUG level (if configured).
 */
void
update_diag_file(char *new)
{
	char	buf[DNAMEMAX];
	ssize_t	n;
	size_t	len;

	(void) lseek(n4_fd, (off_t)0, SEEK_SET);
	(void) ftruncate(n4_fd, 0);
	(void) snprintf(buf, DNAMEMAX, "%s\n", new);

	len = strlen(buf);
	n = write(n4_fd, buf, len);
	if (n < 0 || n < len)
		syslog(LOG_DEBUG, "Could not write %s to diag file", new);
	(void) fsync(n4_fd);

	syslog(LOG_DEBUG, "nfsmapid domain = %s", new);
}

/*
 * Callback function for libmapid. This will be called
 * by the lib, everytime the nfsmapid(8) domain changes.
 */
void *
cb_update_domain(void *arg)
{
	char	*new_dname = (char *)arg;

	DTRACE_PROBE1(nfsmapid, daemon__domain, new_dname);
	update_diag_file(new_dname);
	idmap_kcall(FLUSH_KCACHES_ONLY);

	return (NULL);
}

bool_t
xdr_utf8string(XDR *xdrs, utf8string *objp)
{
	if (xdrs->x_op != XDR_FREE)
		return (xdr_bytes(xdrs, (char **)&objp->utf8string_val,
		    (uint_t *)&objp->utf8string_len, NFS4_MAX_UTF8STRING));
	return (TRUE);
}


int
decode_args(xdrproc_t xdrfunc, refd_door_args_t *argp, caddr_t *xdrargs,
    int size)
{
	XDR xdrs;

	caddr_t tmpargs = (caddr_t)&((refd_door_args_t *)argp)->xdr_arg;
	size_t arg_size = ((refd_door_args_t *)argp)->xdr_len;

	xdrmem_create(&xdrs, tmpargs, arg_size, XDR_DECODE);

	*xdrargs = calloc(1, size);
	if (*xdrargs == NULL) {
		syslog(LOG_ERR, "error allocating arguments buffer");
		return (ENOMEM);
	}

	if (!(*xdrfunc)(&xdrs, *xdrargs)) {
		free(*xdrargs);
		*xdrargs = NULL;
		syslog(LOG_ERR, "error decoding arguments");
		return (EINVAL);
	}

	return (0);
}

int
encode_res(
	xdrproc_t xdrfunc,
	refd_door_res_t **results,
	caddr_t resp,
	int *size)
{
	XDR xdrs;

	*size = xdr_sizeof((*xdrfunc), resp);
	*results = malloc(sizeof (refd_door_res_t) + *size);
	if (*results == NULL) {
		return (ENOMEM);
	}
	(*results)->xdr_len = *size;
	*size = sizeof (refd_door_res_t) + (*results)->xdr_len;
	xdrmem_create(&xdrs, (caddr_t)((*results)->xdr_res),
	    (*results)->xdr_len, XDR_ENCODE);
	if (!(*xdrfunc)(&xdrs, resp)) {
		(*results)->res_status = EINVAL;
		syslog(LOG_ERR, "error encoding results");
		return ((*results)->res_status);
	}
	(*results)->res_status = 0;
	return ((*results)->res_status);
}


bool_t
xdr_knetconfig(XDR *xdrs, struct knetconfig *objp)
{
	rpc_inline_t *buf;
	int i;
	u_longlong_t dev64;
#if !defined(_LP64)
	uint32_t major, minor;
#endif

	if (!xdr_u_int(xdrs, &objp->knc_semantics))
		return (FALSE);
	if (!xdr_opaque(xdrs, objp->knc_protofmly, KNC_STRSIZE))
		return (FALSE);
	if (!xdr_opaque(xdrs, objp->knc_proto, KNC_STRSIZE))
		return (FALSE);

	/*
	 * For interoperability between 32-bit daemon and 64-bit kernel,
	 * we always treat dev_t as 64-bit number and do the expanding
	 * or compression of dev_t as needed.
	 * We have to hand craft the conversion since there is no available
	 * function in ddi.c. Besides ddi.c is available only in the kernel
	 * and we want to keep both user and kernel of xdr_knetconfig() the
	 * same for consistency.
	 */

	if (xdrs->x_op == XDR_ENCODE) {
#if defined(_LP64)
		dev64 = objp->knc_rdev;
#else
		major = (objp->knc_rdev >> NBITSMINOR32) & MAXMAJ32;
		minor = objp->knc_rdev & MAXMIN32;
		dev64 = (((unsigned long long)major) << NBITSMINOR64) | minor;
#endif
		if (!xdr_u_longlong_t(xdrs, &dev64))
			return (FALSE);
	}
	if (xdrs->x_op == XDR_DECODE) {
#if defined(_LP64)
		if (!xdr_u_longlong_t(xdrs, (u_longlong_t *)&objp->knc_rdev))
			return (FALSE);
#else
		if (!xdr_u_longlong_t(xdrs, &dev64))
			return (FALSE);

		major = (dev64 >> NBITSMINOR64) & L_MAXMAJ32;
		minor = dev64 & L_MAXMIN32;
		objp->knc_rdev = (major << L_BITSMINOR32) | minor;
#endif
	}

	if (xdrs->x_op == XDR_ENCODE) {
		buf = XDR_INLINE(xdrs, (8) * BYTES_PER_XDR_UNIT);
		if (buf == NULL) {
			if (!xdr_vector(xdrs, (char *)objp->knc_unused, 8,
			    sizeof (uint_t), (xdrproc_t)xdr_u_int))
				return (FALSE);
		} else {
			uint_t *genp;

			for (i = 0, genp = objp->knc_unused;
			    i < 8; i++) {
#if defined(_LP64) || defined(_KERNEL)
				IXDR_PUT_U_INT32(buf, *genp++);
#else
				IXDR_PUT_U_LONG(buf, *genp++);
#endif
			}
		}
		return (TRUE);
	} else if (xdrs->x_op == XDR_DECODE) {
		buf = XDR_INLINE(xdrs, (8) * BYTES_PER_XDR_UNIT);
		if (buf == NULL) {
			if (!xdr_vector(xdrs, (char *)objp->knc_unused, 8,
			    sizeof (uint_t), (xdrproc_t)xdr_u_int))
				return (FALSE);
		} else {
			uint_t *genp;

			for (i = 0, genp = objp->knc_unused;
			    i < 8; i++) {
#if defined(_LP64) || defined(_KERNEL)
				*genp++ = IXDR_GET_U_INT32(buf);
#else
				*genp++ = IXDR_GET_U_LONG(buf);
#endif
			}
		}
		return (TRUE);
	}

	if (!xdr_vector(xdrs, (char *)objp->knc_unused, 8,
	    sizeof (uint_t), (xdrproc_t)xdr_u_int))
		return (FALSE);
	return (TRUE);
}

/*
 * used by NFSv4 referrals to get info needed for NFSv4 referral mount.
 */
bool_t
xdr_nfs_fsl_info(XDR *xdrs, struct nfs_fsl_info *objp)
{

	if (!xdr_u_int(xdrs, &objp->netbuf_len))
		return (FALSE);
	if (!xdr_u_int(xdrs, &objp->netnm_len))
		return (FALSE);
	if (!xdr_u_int(xdrs, &objp->knconf_len))
		return (FALSE);
	if (!xdr_string(xdrs, &objp->netname, ~0))
		return (FALSE);
	if (!xdr_pointer(xdrs, (char **)&objp->addr, objp->netbuf_len,
	    (xdrproc_t)xdr_netbuf))
		return (FALSE);
	if (!xdr_pointer(xdrs, (char **)&objp->knconf,
	    objp->knconf_len, (xdrproc_t)xdr_knetconfig))
		return (FALSE);
	return (TRUE);
}
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Test nfsmapid. This program is not shipped on the binary release.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stropts.h>
#include <strings.h>
#include <signal.h>
#include <fcntl.h>
#include <locale.h>
#include <unistd.h>
#include <netconfig.h>
#include <door.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/cred.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <rpcsvc/nfs4_prot.h>
#include <nfs/nfsid_map.h>

static char nobody_str[] = "nobody";
static int nfs_idmap_str_uid(utf8string *, uid_t *);
static int nfs_idmap_uid_str(uid_t, utf8string *);
static int nfs_idmap_str_gid(utf8string *, gid_t *);
static int nfs_idmap_gid_str(gid_t, utf8string *);

static void
usage()
{
	fprintf(stderr, gettext(
	    "\nUsage:\tstr2uid string\n"
	    "\tstr2gid string\n"
	    "\tuid2str uid\n"
	    "\tgid2str gid\n"
	    "\techo string\n"
	    "\texit|quit\n"));
}

static int read_line(char *buf, int size)
{
	int len;

	/* read the next line. If cntl-d, return with zero char count */
	printf(gettext("\n> "));

	if (fgets(buf, size, stdin) == NULL)
		return (0);

	len = strlen(buf);
	buf[--len] = '\0';
	return (len);
}

static int
parse_input_line(char *input_line, int *argc, char ***argv)
{
	const char nil = '\0';
	char *chptr;
	int chr_cnt;
	int arg_cnt = 0;
	int ch_was_space = 1;
	int ch_is_space;

	chr_cnt = strlen(input_line);

	/* Count the arguments in the input_line string */

	*argc = 1;

	for (chptr = &input_line[0]; *chptr != nil; chptr++) {
		ch_is_space = isspace(*chptr);
		if (ch_is_space && !ch_was_space) {
			(*argc)++;
		}
		ch_was_space = ch_is_space;
	}

	if (ch_was_space) {
		(*argc)--;
	}	/* minus trailing spaces */

	/* Now that we know how many args calloc the argv array */

	*argv = calloc((*argc)+1, sizeof (char *));
	chptr = (char *)(&input_line[0]);

	for (ch_was_space = 1; *chptr != nil; chptr++) {
		ch_is_space = isspace(*chptr);
		if (ch_is_space) {
			*chptr = nil;	/* replace each space with nil  */
		} else if (ch_was_space) {	/* begining of word? */
			(*argv)[arg_cnt++] = chptr;	/* new argument ? */
		}

		ch_was_space = ch_is_space;
	}

	return (chr_cnt);
}

char *
mapstat(int stat)
{
	switch (stat) {
	case NFSMAPID_OK:
		return ("NFSMAPID_OK");
	case NFSMAPID_NUMSTR:
		return ("NFSMAPID_NUMSTR");
	case NFSMAPID_UNMAPPABLE:
		return ("NFSMAPID_UNMAPPABLE");
	case NFSMAPID_INVALID:
		return ("NFSMAPID_INVALID");
	case NFSMAPID_INTERNAL:
		return ("NFSMAPID_INTERNAL");
	case NFSMAPID_BADDOMAIN:
		return ("NFSMAPID_BADDOMAIN");
	case NFSMAPID_BADID:
		return ("NFSMAPID_BADID");
	case NFSMAPID_NOTFOUND:
		return ("NFSMAPID_NOTFOUND");
	case EINVAL:
		return ("EINVAL");
	case ECOMM:
		return ("ECOMM");
	case ENOMEM:
		return ("ENOMEM");
	default:
		printf(" unknown error %d ", stat);
		return ("...");
	}
}

int
do_test(char *input_buf)
{
	int argc, seal_argc;
	char **argv, **argv_array;
	char *cmd;
	int i, bufsize = 512;
	char str_buf[512];
	utf8string str;
	uid_t uid;
	gid_t gid;
	int stat;

	argv = 0;

	if (parse_input_line(input_buf, &argc, &argv) == 0) {
		printf(gettext("\n"));
		return (1);
	}

	/*
	 * remember argv_array address, which is memory calloc'd by
	 * parse_input_line, so it can be free'd at the end of the loop.
	 */
	argv_array = argv;

	if (argc < 1) {
		usage();
		free(argv_array);
		return (0);
	}

	cmd = argv[0];

	if (strcmp(cmd, "str2uid") == 0) {
		if (argc < 2) {
			usage();
			free(argv_array);
			return (0);
		}
		str.utf8string_val = argv[1];
		str.utf8string_len = strlen(argv[1]);
		stat = nfs_idmap_str_uid(&str, &uid);
		printf(gettext("%u stat=%s \n"), uid, mapstat(stat));

	} else if (strcmp(cmd, "str2gid") == 0) {
		if (argc < 2) {
			usage();
			free(argv_array);
			return (0);
		}
		str.utf8string_val = argv[1];
		str.utf8string_len = strlen(argv[1]);
		stat = nfs_idmap_str_gid(&str, &gid);
		printf(gettext("%u stat=%s \n"), gid, mapstat(stat));

	} else if (strcmp(cmd, "uid2str") == 0) {
		if (argc < 2) {
			usage();
			free(argv_array);
			return (0);
		}
		uid = atoi(argv[1]);
		bzero(str_buf, bufsize);
		str.utf8string_val = str_buf;
		stat = nfs_idmap_uid_str(uid, &str);
		printf(gettext("%s stat=%s\n"), str.utf8string_val,
		    mapstat(stat));

	} else if (strcmp(cmd, "gid2str") == 0) {
		if (argc < 2) {
			usage();
			free(argv_array);
			return (0);
		}
		gid = atoi(argv[1]);
		bzero(str_buf, bufsize);
		str.utf8string_val = str_buf;
		stat = nfs_idmap_gid_str(gid, &str);
		printf(gettext("%s stat=%s\n"), str.utf8string_val,
		    mapstat(stat));

	} else if (strcmp(cmd, "echo") == 0) {
		for (i = 1; i < argc; i++)
			printf("%s ", argv[i]);
		printf("\n");
	} else if (strcmp(cmd, "exit") == 0 ||
	    strcmp(cmd, "quit") == 0) {
		printf(gettext("\n"));
		free(argv_array);
		return (1);

	} else
		usage();

	/* free argv array */
	free(argv_array);
	return (0);
}


int
main(int argc, char **argv)
{
	char buf[512];
	int len, ret;

	(void) setlocale(LC_ALL, "");
#ifndef TEXT_DOMAIN
#define	TEXT_DOMAIN	""
#endif
	(void) textdomain(TEXT_DOMAIN);

	usage();

	/*
	 * Loop, repeatedly calling parse_input_line() to get the
	 * next line and parse it into argc and argv. Act on the
	 * arguements found on the line.
	 */

	do {
		len = read_line(buf, 512);
		if (len)
			ret = do_test(buf);
	} while (!ret);

	return (0);
}

#define	NFSMAPID_DOOR	"/var/run/nfsmapid_door"

/*
 * Gen the door handle for connecting to the nfsmapid process.
 * Keep the door cached.  This call may be made quite often.
 */
int
nfs_idmap_doorget()
{
	static int doorfd = -1;

	if (doorfd != -1)
		return (doorfd);

	if ((doorfd = open(NFSMAPID_DOOR, O_RDWR)) == -1) {
		perror(NFSMAPID_DOOR);
		exit(1);
	}
	return (doorfd);
}

/*
 * Convert a user utf-8 string identifier into its local uid.
 */
int
nfs_idmap_str_uid(utf8string *u8s, uid_t *uid)
{
	struct mapid_arg *mapargp;
	struct mapid_res mapres;
	struct mapid_res *mapresp = &mapres;
	struct mapid_res *resp = mapresp;
	door_arg_t	door_args;
	int		doorfd;
	int		error = 0;
	static int	msg_done = 0;

	if (!u8s || !u8s->utf8string_val || !u8s->utf8string_len ||
	    (u8s->utf8string_val[0] == '\0')) {
		error = EINVAL;
		goto s2u_done;
	}

	if (bcmp(u8s->utf8string_val, "nobody", 6) == 0) {
		/*
		 * If "nobody", just short circuit and bail
		 */
		*uid = UID_NOBODY;
		goto s2u_done;

	}

	if ((mapargp = malloc(MAPID_ARG_LEN(u8s->utf8string_len))) == NULL) {
		(void) fprintf(stderr, "Unable to malloc %d bytes\n",
		    MAPID_ARG_LEN(u8s->utf8string_len));
		error = ENOMEM;
		goto s2u_done;
	}
	mapargp->cmd = NFSMAPID_STR_UID;
	mapargp->u_arg.len = u8s->utf8string_len;
	(void) bcopy(u8s->utf8string_val, mapargp->str, mapargp->u_arg.len);
	mapargp->str[mapargp->u_arg.len] = '\0';

	door_args.data_ptr = (char *)mapargp;
	door_args.data_size = MAPID_ARG_LEN(mapargp->u_arg.len);
	door_args.desc_ptr = NULL;
	door_args.desc_num = 0;
	door_args.rbuf = (char *)mapresp;
	door_args.rsize = sizeof (struct mapid_res);

	/*
	 * call to the nfsmapid daemon
	 */
	if ((doorfd = nfs_idmap_doorget()) == -1) {
		if (!msg_done) {
			fprintf(stderr, "nfs_idmap_str_uid: Can't communicate"
			    " with mapping daemon nfsmapid\n");
			msg_done = 1;
		}
		error = ECOMM;
		free(mapargp);
		goto s2u_done;
	}

	if (door_call(doorfd, &door_args) == -1) {
		perror("door_call failed");
		error = EINVAL;
		free(mapargp);
		goto s2u_done;
	}

	free(mapargp);

	resp = (struct mapid_res *)door_args.rbuf;
	switch (resp->status) {
	case NFSMAPID_OK:
		*uid = resp->u_res.uid;
		break;

	case NFSMAPID_NUMSTR:
		*uid = resp->u_res.uid;
		error = resp->status;
		goto out;

	default:
	case NFSMAPID_UNMAPPABLE:
	case NFSMAPID_INVALID:
	case NFSMAPID_INTERNAL:
	case NFSMAPID_BADDOMAIN:
	case NFSMAPID_BADID:
	case NFSMAPID_NOTFOUND:
		error = resp->status;
		goto s2u_done;
	}

s2u_done:
	if (error)
		*uid = UID_NOBODY;
out:
	if (resp != mapresp)
		munmap(door_args.rbuf, door_args.rsize);
	return (error);
}

/*
 * Convert a uid into its utf-8 string representation.
 */
int
nfs_idmap_uid_str(uid_t uid,		/* uid to map */
		utf8string *u8s)	/* resulting utf-8 string for uid */
{
	struct mapid_arg maparg;
	struct mapid_res mapres;
	struct mapid_res *mapresp = &mapres;
	struct mapid_res *resp = mapresp;
	door_arg_t	door_args;
	int		doorfd;
	int		error = 0;
	static int	msg_done = 0;

	if (uid == UID_NOBODY) {
		u8s->utf8string_len = strlen("nobody");
		u8s->utf8string_val = nobody_str;
		goto u2s_done;
	}

	/*
	 * Daemon call...
	 */
	maparg.cmd = NFSMAPID_UID_STR;
	maparg.u_arg.uid = uid;

	door_args.data_ptr = (char *)&maparg;
	door_args.data_size = sizeof (struct mapid_arg);
	door_args.desc_ptr = NULL;
	door_args.desc_num = 0;
	door_args.rbuf = (char *)mapresp;
	door_args.rsize = sizeof (struct mapid_res);

	if ((doorfd = nfs_idmap_doorget()) == -1) {
		if (!msg_done) {
			fprintf(stderr, "nfs_idmap_uid_str: Can't "
			    "communicate with mapping daemon nfsmapid\n");
			msg_done = 1;
		}
		error = ECOMM;
		goto u2s_done;
	}

	if (door_call(doorfd, &door_args) == -1) {
		perror("door_call failed");
		error = EINVAL;
		goto u2s_done;
	}

	resp = (struct mapid_res *)door_args.rbuf;
	if (resp->status != NFSMAPID_OK) {
		error = resp->status;
		goto u2s_done;
	}

	if (resp->u_res.len != strlen(resp->str)) {
		(void) fprintf(stderr, "Incorrect length %d expected %d\n",
		    resp->u_res.len, strlen(resp->str));
		error = NFSMAPID_INVALID;
		goto u2s_done;
	}
	u8s->utf8string_len = resp->u_res.len;
	bcopy(resp->str, u8s->utf8string_val, u8s->utf8string_len);

u2s_done:
	if (resp != mapresp)
		munmap(door_args.rbuf, door_args.rsize);
	return (error);
}

/*
 * Convert a group utf-8 string identifier into its local gid.
 */
int
nfs_idmap_str_gid(utf8string *u8s, gid_t *gid)
{
	struct mapid_arg *mapargp;
	struct mapid_res mapres;
	struct mapid_res *mapresp = &mapres;
	struct mapid_res *resp = mapresp;
	door_arg_t	door_args;
	int		doorfd;
	int		error = 0;
	static int	msg_done = 0;

	if (!u8s || !u8s->utf8string_val || !u8s->utf8string_len ||
	    (u8s->utf8string_val[0] == '\0')) {
		error = EINVAL;
		goto s2g_done;
	}

	if (bcmp(u8s->utf8string_val, "nobody", 6) == 0) {
		/*
		 * If "nobody", just short circuit and bail
		 */
		*gid = GID_NOBODY;
		goto s2g_done;

	}

	if ((mapargp = malloc(MAPID_ARG_LEN(u8s->utf8string_len))) == NULL) {
		(void) fprintf(stderr, "Unable to malloc %d bytes\n",
		    MAPID_ARG_LEN(u8s->utf8string_len));
		error = ENOMEM;
		goto s2g_done;
	}
	mapargp->cmd = NFSMAPID_STR_GID;
	mapargp->u_arg.len = u8s->utf8string_len;
	(void) bcopy(u8s->utf8string_val, mapargp->str, mapargp->u_arg.len);
	mapargp->str[mapargp->u_arg.len] = '\0';

	door_args.data_ptr = (char *)mapargp;
	door_args.data_size = MAPID_ARG_LEN(mapargp->u_arg.len);
	door_args.desc_ptr = NULL;
	door_args.desc_num = 0;
	door_args.rbuf = (char *)mapresp;
	door_args.rsize = sizeof (struct mapid_res);

	/*
	 * call to the nfsmapid daemon
	 */
	if ((doorfd = nfs_idmap_doorget()) == -1) {
		if (!msg_done) {
			fprintf(stderr, "nfs_idmap_str_uid: Can't communicate"
			    " with mapping daemon nfsmapid\n");
			msg_done = 1;
		}
		error = ECOMM;
		free(mapargp);
		goto s2g_done;
	}

	if (door_call(doorfd, &door_args) == -1) {
		perror("door_call failed");
		error = EINVAL;
		free(mapargp);
		goto s2g_done;
	}

	free(mapargp);

	resp = (struct mapid_res *)door_args.rbuf;
	switch (resp->status) {
	case NFSMAPID_OK:
		*gid = resp->u_res.gid;
		break;

	case NFSMAPID_NUMSTR:
		*gid = resp->u_res.gid;
		error = resp->status;
		goto out;

	default:
	case NFSMAPID_UNMAPPABLE:
	case NFSMAPID_INVALID:
	case NFSMAPID_INTERNAL:
	case NFSMAPID_BADDOMAIN:
	case NFSMAPID_BADID:
	case NFSMAPID_NOTFOUND:
		error = resp->status;
		goto s2g_done;
	}

s2g_done:
	if (error)
		*gid = GID_NOBODY;
out:
	if (resp != mapresp)
		munmap(door_args.rbuf, door_args.rsize);
	return (error);
}

/*
 * Convert a gid into its utf-8 string representation.
 */
int
nfs_idmap_gid_str(gid_t gid,		/* gid to map */
		utf8string *g8s)	/* resulting utf-8 string for gid */
{
	struct mapid_arg maparg;
	struct mapid_res mapres;
	struct mapid_res *mapresp = &mapres;
	struct mapid_res *resp = mapresp;
	door_arg_t	door_args;
	int		error = 0;
	int		doorfd;
	static int	msg_done = 0;

	if (gid == GID_NOBODY) {
		g8s->utf8string_len = strlen("nobody");
		g8s->utf8string_val = nobody_str;
		goto g2s_done;

	}

	/*
	 * Daemon call...
	 */
	maparg.cmd = NFSMAPID_GID_STR;
	maparg.u_arg.gid = gid;

	door_args.data_ptr = (char *)&maparg;
	door_args.data_size = sizeof (struct mapid_arg);
	door_args.desc_ptr = NULL;
	door_args.desc_num = 0;
	door_args.rbuf = (char *)mapresp;
	door_args.rsize = sizeof (struct mapid_res);

	if ((doorfd = nfs_idmap_doorget()) == -1) {
		if (!msg_done) {
			fprintf(stderr, "nfs_idmap_uid_str: Can't "
			    "communicate with mapping daemon nfsmapid\n");
			msg_done = 1;
		}
		error = ECOMM;
		goto g2s_done;
	}

	if (door_call(doorfd, &door_args) == -1) {
		perror("door_call failed");
		error = EINVAL;
		goto g2s_done;
	}

	resp = (struct mapid_res *)door_args.rbuf;
	if (resp->status != NFSMAPID_OK) {
		error = resp->status;
		goto g2s_done;
	}

	if (resp->u_res.len != strlen(resp->str)) {
		(void) fprintf(stderr, "Incorrect length %d expected %d\n",
		    resp->u_res.len, strlen(resp->str));
		error = NFSMAPID_INVALID;
		goto g2s_done;
	}
	g8s->utf8string_len = resp->u_res.len;
	bcopy(resp->str, g8s->utf8string_val, g8s->utf8string_len);

g2s_done:
	if (resp != mapresp)
		munmap(door_args.rbuf, door_args.rsize);
	return (error);
}