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
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
|
/*
* 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) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2016 by Delphix. All rights reserved.
* Copyright 2016 Nexenta Systems, Inc. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <string.h>
#include <syslog.h>
#include <sys/param.h>
#include <rpc/rpc.h>
#include <sys/stat.h>
#include <netconfig.h>
#include <netdir.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/errno.h>
#include <rpcsvc/mount.h>
#include <sys/pathconf.h>
#include <sys/systeminfo.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <signal.h>
#include <locale.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <thread.h>
#include <assert.h>
#include <priv_utils.h>
#include <nfs/auth.h>
#include <nfs/nfssys.h>
#include <nfs/nfs.h>
#include <nfs/nfs_sec.h>
#include <rpcsvc/daemon_utils.h>
#include <deflt.h>
#include "../../fslib.h"
#include <sharefs/share.h>
#include <sharefs/sharetab.h>
#include "../lib/sharetab.h"
#include "mountd.h"
#include <tsol/label.h>
#include <sys/tsol/label_macro.h>
#include <libtsnet.h>
#include <sys/sdt.h>
#include <libscf.h>
#include <limits.h>
#include <sys/nvpair.h>
#include <attr.h>
#include "smfcfg.h"
#include <pwd.h>
#include <grp.h>
#include <alloca.h>
extern int daemonize_init(void);
extern void daemonize_fini(int);
extern int _nfssys(int, void *);
struct sh_list *share_list;
rwlock_t sharetab_lock; /* lock to protect the cached sharetab */
static mutex_t mnttab_lock; /* prevent concurrent mnttab readers */
static mutex_t logging_queue_lock;
static cond_t logging_queue_cv;
static share_t *find_lofsentry(char *, int *);
static int getclientsflavors_old(share_t *, struct cln *, int *);
static int getclientsflavors_new(share_t *, struct cln *, int *);
static int check_client_old(share_t *, struct cln *, int, uid_t, gid_t, uint_t,
gid_t *, uid_t *, gid_t *, uint_t *, gid_t **);
static int check_client_new(share_t *, struct cln *, int, uid_t, gid_t, uint_t,
gid_t *, uid_t *, gid_t *i, uint_t *, gid_t **);
static void mnt(struct svc_req *, SVCXPRT *);
static void mnt_pathconf(struct svc_req *);
static int mount(struct svc_req *r);
static void sh_free(struct sh_list *);
static void umount(struct svc_req *);
static void umountall(struct svc_req *);
static int newopts(char *);
static tsol_tpent_t *get_client_template(struct sockaddr *);
static int debug;
static int verbose;
static int rejecting;
static int mount_vers_min = MOUNTVERS;
static int mount_vers_max = MOUNTVERS3;
static int mountd_port = 0;
extern void nfscmd_func(void *, char *, size_t, door_desc_t *, uint_t);
thread_t nfsauth_thread;
thread_t cmd_thread;
thread_t logging_thread;
typedef struct logging_data {
char *ld_host;
char *ld_path;
char *ld_rpath;
int ld_status;
char *ld_netid;
struct netbuf *ld_nb;
struct logging_data *ld_next;
} logging_data;
static logging_data *logging_head = NULL;
static logging_data *logging_tail = NULL;
/*
* Our copy of some system variables obtained using sysconf(3c)
*/
static long ngroups_max; /* _SC_NGROUPS_MAX */
static long pw_size; /* _SC_GETPW_R_SIZE_MAX */
/* ARGSUSED */
static void *
nfsauth_svc(void *arg)
{
int doorfd = -1;
uint_t darg;
#ifdef DEBUG
int dfd;
#endif
if ((doorfd = door_create(nfsauth_func, NULL,
DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
syslog(LOG_ERR, "Unable to create door: %m\n");
exit(10);
}
#ifdef DEBUG
/*
* Create a file system path for the door
*/
if ((dfd = open(MOUNTD_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", MOUNTD_DOOR);
(void) close(doorfd);
exit(11);
}
/*
* Clean up any stale namespace associations
*/
(void) fdetach(MOUNTD_DOOR);
/*
* Register in namespace to pass to the kernel to door_ki_open
*/
if (fattach(doorfd, MOUNTD_DOOR) == -1) {
syslog(LOG_ERR, "Unable to fattach door: %m\n");
(void) close(dfd);
(void) close(doorfd);
exit(12);
}
(void) close(dfd);
#endif
/*
* Must pass the doorfd down to the kernel.
*/
darg = doorfd;
(void) _nfssys(MOUNTD_ARGS, &darg);
/*
* Wait for incoming calls
*/
/*CONSTCOND*/
for (;;)
(void) pause();
/*NOTREACHED*/
syslog(LOG_ERR, gettext("Door server exited"));
return (NULL);
}
/*
* NFS command service thread code for setup and handling of the
* nfs_cmd requests for character set conversion and other future
* events.
*/
static void *
cmd_svc(void *arg)
{
int doorfd = -1;
uint_t darg;
if ((doorfd = door_create(nfscmd_func, NULL,
DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
syslog(LOG_ERR, "Unable to create cmd door: %m\n");
exit(10);
}
/*
* Must pass the doorfd down to the kernel.
*/
darg = doorfd;
(void) _nfssys(NFSCMD_ARGS, &darg);
/*
* Wait for incoming calls
*/
/*CONSTCOND*/
for (;;)
(void) pause();
/*NOTREACHED*/
syslog(LOG_ERR, gettext("Cmd door server exited"));
return (NULL);
}
static void
free_logging_data(logging_data *lq)
{
if (lq != NULL) {
free(lq->ld_host);
free(lq->ld_netid);
if (lq->ld_nb != NULL) {
free(lq->ld_nb->buf);
free(lq->ld_nb);
}
free(lq->ld_path);
free(lq->ld_rpath);
free(lq);
}
}
static logging_data *
remove_head_of_queue(void)
{
logging_data *lq;
/*
* Pull it off the queue.
*/
lq = logging_head;
if (lq) {
logging_head = lq->ld_next;
/*
* Drained it.
*/
if (logging_head == NULL) {
logging_tail = NULL;
}
}
return (lq);
}
static void
do_logging_queue(logging_data *lq)
{
int cleared = 0;
char *host;
while (lq) {
struct cln cln;
if (lq->ld_host == NULL) {
DTRACE_PROBE(mountd, name_by_lazy);
cln_init_lazy(&cln, lq->ld_netid, lq->ld_nb);
host = cln_gethost(&cln);
} else
host = lq->ld_host;
audit_mountd_mount(host, lq->ld_path, lq->ld_status); /* BSM */
/* add entry to mount list */
if (lq->ld_rpath)
mntlist_new(host, lq->ld_rpath);
if (lq->ld_host == NULL)
cln_fini(&cln);
free_logging_data(lq);
cleared++;
(void) mutex_lock(&logging_queue_lock);
lq = remove_head_of_queue();
(void) mutex_unlock(&logging_queue_lock);
}
DTRACE_PROBE1(mountd, logging_cleared, cleared);
}
static void *
logging_svc(void *arg)
{
logging_data *lq;
for (;;) {
(void) mutex_lock(&logging_queue_lock);
while (logging_head == NULL) {
(void) cond_wait(&logging_queue_cv,
&logging_queue_lock);
}
lq = remove_head_of_queue();
(void) mutex_unlock(&logging_queue_lock);
do_logging_queue(lq);
}
/*NOTREACHED*/
syslog(LOG_ERR, gettext("Logging server exited"));
return (NULL);
}
static int
convert_int(int *val, char *str)
{
long lval;
if (str == NULL || !isdigit(*str))
return (-1);
lval = strtol(str, &str, 10);
if (*str != '\0' || lval > INT_MAX)
return (-2);
*val = (int)lval;
return (0);
}
/*
* This function is called for each configured network type to
* bind and register our RPC service programs.
*
* On TCP or UDP, we may want to bind MOUNTPROG on a specific port
* (when mountd_port is specified) in which case we'll use the
* variant of svc_tp_create() that lets us pass a bind address.
*/
static void
md_svc_tp_create(struct netconfig *nconf)
{
char port_str[8];
struct nd_hostserv hs;
struct nd_addrlist *al = NULL;
SVCXPRT *xprt = NULL;
rpcvers_t vers;
vers = mount_vers_max;
/*
* If mountd_port is set and this is an inet transport,
* bind this service on the specified port. The TLI way
* to create such a bind address is netdir_getbyname()
* with the special "host" HOST_SELF_BIND. This builds
* an all-zeros IP address with the specified port.
*/
if (mountd_port != 0 &&
(strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
strcmp(nconf->nc_protofmly, NC_INET6) == 0)) {
int err;
snprintf(port_str, sizeof (port_str), "%u",
(unsigned short)mountd_port);
hs.h_host = HOST_SELF_BIND;
hs.h_serv = port_str;
err = netdir_getbyname((struct netconfig *)nconf, &hs, &al);
if (err == 0 && al != NULL) {
xprt = svc_tp_create_addr(mnt, MOUNTPROG, vers,
nconf, al->n_addrs);
netdir_free(al, ND_ADDRLIST);
}
if (xprt == NULL) {
syslog(LOG_ERR, "mountd: unable to create "
"(MOUNTD,%d) on transport %s (port %d)",
vers, nconf->nc_netid, mountd_port);
}
/* fall-back to default bind */
}
if (xprt == NULL) {
/*
* Had mountd_port=0, or non-inet transport,
* or the bind to a specific port failed.
* Do a default bind.
*/
xprt = svc_tp_create(mnt, MOUNTPROG, vers, nconf);
}
if (xprt == NULL) {
syslog(LOG_ERR, "mountd: unable to create "
"(MOUNTD,%d) on transport %s",
vers, nconf->nc_netid);
return;
}
/*
* Register additional versions on this transport.
*/
while (--vers >= mount_vers_min) {
if (!svc_reg(xprt, MOUNTPROG, vers, mnt, nconf)) {
(void) syslog(LOG_ERR, "mountd: "
"failed to register vers %d on %s",
vers, nconf->nc_netid);
}
}
}
int
main(int argc, char *argv[])
{
int pid;
int c;
int rpc_svc_fdunlim = 1;
int rpc_svc_mode = RPC_SVC_MT_AUTO;
int maxrecsz = RPC_MAXDATASIZE;
bool_t exclbind = TRUE;
bool_t can_do_mlp;
long thr_flags = (THR_NEW_LWP|THR_DAEMON);
char defval[4];
int defvers, ret, bufsz;
struct rlimit rl;
int listen_backlog = 0;
int max_threads = 0;
int tmp;
struct netconfig *nconf;
NCONF_HANDLE *nc;
int pipe_fd = -1;
/*
* Mountd requires uid 0 for:
* /etc/rmtab updates (we could chown it to daemon)
* /etc/dfs/dfstab reading (it wants to lock out share which
* doesn't do any locking before first truncate;
* NFS share does; should use fcntl locking instead)
* Needed privileges:
* auditing
* nfs syscall
* file dac search (so it can stat all files)
* Optional privileges:
* MLP
*/
can_do_mlp = priv_ineffect(PRIV_NET_BINDMLP);
if (__init_daemon_priv(PU_RESETGROUPS|PU_CLEARLIMITSET, -1, -1,
PRIV_SYS_NFS, PRIV_PROC_AUDIT, PRIV_FILE_DAC_SEARCH,
PRIV_NET_PRIVADDR,
can_do_mlp ? PRIV_NET_BINDMLP : NULL, NULL) == -1) {
(void) fprintf(stderr,
"%s: must be run with sufficient privileges\n",
argv[0]);
exit(1);
}
if (getrlimit(RLIMIT_NOFILE, &rl) != 0) {
syslog(LOG_ERR, "getrlimit failed");
} else {
rl.rlim_cur = rl.rlim_max;
if (setrlimit(RLIMIT_NOFILE, &rl) != 0)
syslog(LOG_ERR, "setrlimit failed");
}
(void) enable_extended_FILE_stdio(-1, -1);
ret = nfs_smf_get_iprop("mountd_max_threads", &max_threads,
DEFAULT_INSTANCE, SCF_TYPE_INTEGER, NFSD);
if (ret != SA_OK) {
syslog(LOG_ERR, "Reading of mountd_max_threads from SMF "
"failed, using default value");
}
ret = nfs_smf_get_iprop("mountd_port", &mountd_port,
DEFAULT_INSTANCE, SCF_TYPE_INTEGER, NFSD);
if (ret != SA_OK) {
syslog(LOG_ERR, "Reading of mountd_port from SMF "
"failed, using default value");
}
while ((c = getopt(argc, argv, "dvrm:p:")) != EOF) {
switch (c) {
case 'd':
debug++;
break;
case 'v':
verbose++;
break;
case 'r':
rejecting = 1;
break;
case 'm':
if (convert_int(&tmp, optarg) != 0 || tmp < 1) {
(void) fprintf(stderr, "%s: invalid "
"max_threads option, using defaults\n",
argv[0]);
break;
}
max_threads = tmp;
break;
case 'p':
if (convert_int(&tmp, optarg) != 0 || tmp < 1 ||
tmp > UINT16_MAX) {
(void) fprintf(stderr, "%s: invalid port "
"number\n", argv[0]);
break;
}
mountd_port = tmp;
break;
default:
fprintf(stderr, "usage: mountd [-v] [-r]\n");
exit(1);
}
}
/*
* Read in the NFS version values from config file.
*/
bufsz = 4;
ret = nfs_smf_get_prop("server_versmin", defval, DEFAULT_INSTANCE,
SCF_TYPE_INTEGER, NFSD, &bufsz);
if (ret == SA_OK) {
errno = 0;
defvers = strtol(defval, (char **)NULL, 10);
if (errno == 0) {
mount_vers_min = defvers;
/*
* special because NFSv2 is
* supported by mount v1 & v2
*/
if (defvers == NFS_VERSION)
mount_vers_min = MOUNTVERS;
}
}
bufsz = 4;
ret = nfs_smf_get_prop("server_versmax", defval, DEFAULT_INSTANCE,
SCF_TYPE_INTEGER, NFSD, &bufsz);
if (ret == SA_OK) {
errno = 0;
defvers = strtol(defval, (char **)NULL, 10);
if (errno == 0) {
mount_vers_max = defvers;
}
}
ret = nfs_smf_get_iprop("mountd_listen_backlog", &listen_backlog,
DEFAULT_INSTANCE, SCF_TYPE_INTEGER, NFSD);
if (ret != SA_OK) {
syslog(LOG_ERR, "Reading of mountd_listen_backlog from SMF "
"failed, using default value");
}
/*
* Sanity check versions,
* even though we may get versions > MOUNTVERS3, we still need
* to start nfsauth service, so continue on regardless of values.
*/
if (mount_vers_max > MOUNTVERS3)
mount_vers_max = MOUNTVERS3;
if (mount_vers_min > mount_vers_max) {
fprintf(stderr, "server_versmin > server_versmax\n");
mount_vers_max = mount_vers_min;
}
(void) setlocale(LC_ALL, "");
(void) rwlock_init(&sharetab_lock, USYNC_THREAD, NULL);
(void) mutex_init(&mnttab_lock, USYNC_THREAD, NULL);
(void) mutex_init(&logging_queue_lock, USYNC_THREAD, NULL);
(void) cond_init(&logging_queue_cv, USYNC_THREAD, NULL);
netgroup_init();
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
/* Don't drop core if the NFS module isn't loaded. */
(void) signal(SIGSYS, SIG_IGN);
if (!debug)
pipe_fd = daemonize_init();
/*
* If we coredump it'll be in /core
*/
if (chdir("/") < 0)
fprintf(stderr, "chdir /: %s\n", strerror(errno));
if (!debug)
openlog("mountd", LOG_PID, LOG_DAEMON);
/*
* establish our lock on the lock file and write our pid to it.
* exit if some other process holds the lock, or if there's any
* error in writing/locking the file.
*/
pid = _enter_daemon_lock(MOUNTD);
switch (pid) {
case 0:
break;
case -1:
fprintf(stderr, "error locking for %s: %s\n", MOUNTD,
strerror(errno));
exit(2);
default:
/* daemon was already running */
exit(0);
}
audit_mountd_setup(); /* BSM */
/*
* Get required system variables
*/
if ((ngroups_max = sysconf(_SC_NGROUPS_MAX)) == -1) {
syslog(LOG_ERR, "Unable to get _SC_NGROUPS_MAX");
exit(1);
}
if ((pw_size = sysconf(_SC_GETPW_R_SIZE_MAX)) == -1) {
syslog(LOG_ERR, "Unable to get _SC_GETPW_R_SIZE_MAX");
exit(1);
}
/*
* Set number of file descriptors to unlimited
*/
if (!rpc_control(RPC_SVC_USE_POLLFD, &rpc_svc_fdunlim)) {
syslog(LOG_INFO, "unable to set number of FDs to unlimited");
}
/*
* Tell RPC that we want automatic thread mode.
* A new thread will be spawned for each request.
*/
if (!rpc_control(RPC_SVC_MTMODE_SET, &rpc_svc_mode)) {
fprintf(stderr, "unable to set automatic MT mode\n");
exit(1);
}
/*
* Enable non-blocking mode and maximum record size checks for
* connection oriented transports.
*/
if (!rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrecsz)) {
fprintf(stderr, "unable to set RPC max record size\n");
}
/*
* Prevent our non-priv udp and tcp ports bound w/wildcard addr
* from being hijacked by a bind to a more specific addr.
*/
if (!rpc_control(__RPC_SVC_EXCLBIND_SET, &exclbind)) {
fprintf(stderr, "warning: unable to set udp/tcp EXCLBIND\n");
}
/*
* Set the maximum number of outstanding connection
* indications (listen backlog) to the value specified.
*/
if (listen_backlog > 0 && !rpc_control(__RPC_SVC_LSTNBKLOG_SET,
&listen_backlog)) {
fprintf(stderr, "unable to set listen backlog\n");
exit(1);
}
/*
* If max_threads was specified, then set the
* maximum number of threads to the value specified.
*/
if (max_threads > 0 && !rpc_control(RPC_SVC_THRMAX_SET, &max_threads)) {
fprintf(stderr, "unable to set max_threads\n");
exit(1);
}
if (mountd_port < 0 || mountd_port > UINT16_MAX) {
fprintf(stderr, "unable to use specified port\n");
exit(1);
}
/*
* Make sure to unregister any previous versions in case the
* user is reconfiguring the server in interesting ways.
*/
svc_unreg(MOUNTPROG, MOUNTVERS);
svc_unreg(MOUNTPROG, MOUNTVERS_POSIX);
svc_unreg(MOUNTPROG, MOUNTVERS3);
/*
* Create the nfsauth thread with same signal disposition
* as the main thread. We need to create a separate thread
* since mountd() will be both an RPC server (for remote
* traffic) _and_ a doors server (for kernel upcalls).
*/
if (thr_create(NULL, 0, nfsauth_svc, 0, thr_flags, &nfsauth_thread)) {
fprintf(stderr,
gettext("Failed to create NFSAUTH svc thread\n"));
exit(2);
}
/*
* Create the cmd service thread with same signal disposition
* as the main thread. We need to create a separate thread
* since mountd() will be both an RPC server (for remote
* traffic) _and_ a doors server (for kernel upcalls).
*/
if (thr_create(NULL, 0, cmd_svc, 0, thr_flags, &cmd_thread)) {
syslog(LOG_ERR, gettext("Failed to create CMD svc thread"));
exit(2);
}
/*
* Create an additional thread to service the rmtab and
* audit_mountd_mount logging for mount requests. Use the same
* signal disposition as the main thread. We create
* a separate thread to allow the mount request threads to
* clear as soon as possible.
*/
if (thr_create(NULL, 0, logging_svc, 0, thr_flags, &logging_thread)) {
syslog(LOG_ERR, gettext("Failed to create LOGGING svc thread"));
exit(2);
}
/*
* Enumerate network transports and create service listeners
* as appropriate for each.
*/
if ((nc = setnetconfig()) == NULL) {
syslog(LOG_ERR, "setnetconfig failed: %m");
return (-1);
}
while ((nconf = getnetconfig(nc)) != NULL) {
/*
* Skip things like tpi_raw, invisible...
*/
if ((nconf->nc_flag & NC_VISIBLE) == 0)
continue;
if (nconf->nc_semantics != NC_TPI_CLTS &&
nconf->nc_semantics != NC_TPI_COTS &&
nconf->nc_semantics != NC_TPI_COTS_ORD)
continue;
md_svc_tp_create(nconf);
}
(void) endnetconfig(nc);
/*
* Start serving
*/
rmtab_load();
daemonize_fini(pipe_fd);
/* Get rid of the most dangerous basic privileges. */
__fini_daemon_priv(PRIV_PROC_EXEC, PRIV_PROC_INFO, PRIV_PROC_SESSION,
(char *)NULL);
svc_run();
syslog(LOG_ERR, "Error: svc_run shouldn't have returned");
abort();
/* NOTREACHED */
return (0);
}
/*
* Server procedure switch routine
*/
void
mnt(struct svc_req *rqstp, SVCXPRT *transp)
{
switch (rqstp->rq_proc) {
case NULLPROC:
errno = 0;
if (!svc_sendreply(transp, xdr_void, (char *)0))
log_cant_reply(transp);
return;
case MOUNTPROC_MNT:
(void) mount(rqstp);
return;
case MOUNTPROC_DUMP:
mntlist_send(transp);
return;
case MOUNTPROC_UMNT:
umount(rqstp);
return;
case MOUNTPROC_UMNTALL:
umountall(rqstp);
return;
case MOUNTPROC_EXPORT:
case MOUNTPROC_EXPORTALL:
export(rqstp);
return;
case MOUNTPROC_PATHCONF:
if (rqstp->rq_vers == MOUNTVERS_POSIX)
mnt_pathconf(rqstp);
else
svcerr_noproc(transp);
return;
default:
svcerr_noproc(transp);
return;
}
}
void
log_cant_reply_cln(struct cln *cln)
{
int saverrno;
char *host;
saverrno = errno; /* save error code */
host = cln_gethost(cln);
if (host == NULL)
return;
errno = saverrno;
if (errno == 0)
syslog(LOG_ERR, "couldn't send reply to %s", host);
else
syslog(LOG_ERR, "couldn't send reply to %s: %m", host);
}
void
log_cant_reply(SVCXPRT *transp)
{
int saverrno;
struct cln cln;
saverrno = errno; /* save error code */
cln_init(&cln, transp);
errno = saverrno;
log_cant_reply_cln(&cln);
cln_fini(&cln);
}
/*
* Answer pathconf questions for the mount point fs
*/
static void
mnt_pathconf(struct svc_req *rqstp)
{
SVCXPRT *transp;
struct pathcnf p;
char *path, rpath[MAXPATHLEN];
struct stat st;
transp = rqstp->rq_xprt;
path = NULL;
(void) memset((caddr_t)&p, 0, sizeof (p));
if (!svc_getargs(transp, xdr_dirpath, (caddr_t)&path)) {
svcerr_decode(transp);
return;
}
if (lstat(path, &st) < 0) {
_PC_SET(_PC_ERROR, p.pc_mask);
goto done;
}
/*
* Get a path without symbolic links.
*/
if (realpath(path, rpath) == NULL) {
syslog(LOG_DEBUG,
"mount request: realpath failed on %s: %m",
path);
_PC_SET(_PC_ERROR, p.pc_mask);
goto done;
}
(void) memset((caddr_t)&p, 0, sizeof (p));
/*
* can't ask about devices over NFS
*/
_PC_SET(_PC_MAX_CANON, p.pc_mask);
_PC_SET(_PC_MAX_INPUT, p.pc_mask);
_PC_SET(_PC_PIPE_BUF, p.pc_mask);
_PC_SET(_PC_VDISABLE, p.pc_mask);
errno = 0;
p.pc_link_max = pathconf(rpath, _PC_LINK_MAX);
if (errno)
_PC_SET(_PC_LINK_MAX, p.pc_mask);
p.pc_name_max = pathconf(rpath, _PC_NAME_MAX);
if (errno)
_PC_SET(_PC_NAME_MAX, p.pc_mask);
p.pc_path_max = pathconf(rpath, _PC_PATH_MAX);
if (errno)
_PC_SET(_PC_PATH_MAX, p.pc_mask);
if (pathconf(rpath, _PC_NO_TRUNC) == 1)
_PC_SET(_PC_NO_TRUNC, p.pc_mask);
if (pathconf(rpath, _PC_CHOWN_RESTRICTED) == 1)
_PC_SET(_PC_CHOWN_RESTRICTED, p.pc_mask);
done:
errno = 0;
if (!svc_sendreply(transp, xdr_ppathcnf, (char *)&p))
log_cant_reply(transp);
if (path != NULL)
svc_freeargs(transp, xdr_dirpath, (caddr_t)&path);
}
/*
* If the rootmount (export) option is specified, the all mount requests for
* subdirectories return EACCES.
*/
static int
checkrootmount(share_t *sh, char *rpath)
{
char *val;
if ((val = getshareopt(sh->sh_opts, SHOPT_NOSUB)) != NULL) {
free(val);
if (strcmp(sh->sh_path, rpath) != 0)
return (0);
else
return (1);
} else
return (1);
}
#define MAX_FLAVORS 128
/*
* Return only EACCES if client does not have access
* to this directory.
* "If the server exports only /a/b, an attempt to
* mount a/b/c will fail with ENOENT if the directory
* does not exist"... However, if the client
* does not have access to /a/b, an attacker can
* determine whether the directory exists.
* This routine checks either existence of the file or
* existence of the file name entry in the mount table.
* If the file exists and there is no file name entry,
* the error returned should be EACCES.
* If the file does not exist, it must be determined
* whether the client has access to a parent
* directory. If the client has access to a parent
* directory, the error returned should be ENOENT,
* otherwise EACCES.
*/
static int
mount_enoent_error(struct cln *cln, char *path, char *rpath, int *flavor_list)
{
char *checkpath, *dp;
share_t *sh = NULL;
int realpath_error = ENOENT, reply_error = EACCES, lofs_tried = 0;
int flavor_count;
checkpath = strdup(path);
if (checkpath == NULL) {
syslog(LOG_ERR, "mount_enoent: no memory");
return (EACCES);
}
/* CONSTCOND */
while (1) {
if (sh) {
sharefree(sh);
sh = NULL;
}
if ((sh = findentry(rpath)) == NULL &&
(sh = find_lofsentry(rpath, &lofs_tried)) == NULL) {
/*
* There is no file name entry.
* If the file (with symbolic links resolved) exists,
* the error returned should be EACCES.
*/
if (realpath_error == 0)
break;
} else if (checkrootmount(sh, rpath) == 0) {
/*
* This is a "nosub" only export, in which case,
* mounting subdirectories isn't allowed.
* If the file (with symbolic links resolved) exists,
* the error returned should be EACCES.
*/
if (realpath_error == 0)
break;
} else {
/*
* Check permissions in mount table.
*/
if (newopts(sh->sh_opts))
flavor_count = getclientsflavors_new(sh, cln,
flavor_list);
else
flavor_count = getclientsflavors_old(sh, cln,
flavor_list);
if (flavor_count != 0) {
/*
* Found entry in table and
* client has correct permissions.
*/
reply_error = ENOENT;
break;
}
}
/*
* Check all parent directories.
*/
dp = strrchr(checkpath, '/');
if (dp == NULL)
break;
*dp = '\0';
if (strlen(checkpath) == 0)
break;
/*
* Get the real path (no symbolic links in it)
*/
if (realpath(checkpath, rpath) == NULL) {
if (errno != ENOENT)
break;
} else {
realpath_error = 0;
}
}
if (sh)
sharefree(sh);
free(checkpath);
return (reply_error);
}
/*
* We need to inform the caller whether or not we were
* able to add a node to the queue. If we are not, then
* it is up to the caller to go ahead and log the data.
*/
static int
enqueue_logging_data(char *host, SVCXPRT *transp, char *path,
char *rpath, int status, int error)
{
logging_data *lq;
struct netbuf *nb;
lq = (logging_data *)calloc(1, sizeof (logging_data));
if (lq == NULL)
goto cleanup;
/*
* We might not yet have the host...
*/
if (host) {
DTRACE_PROBE1(mountd, log_host, host);
lq->ld_host = strdup(host);
if (lq->ld_host == NULL)
goto cleanup;
} else {
DTRACE_PROBE(mountd, log_no_host);
lq->ld_netid = strdup(transp->xp_netid);
if (lq->ld_netid == NULL)
goto cleanup;
lq->ld_nb = calloc(1, sizeof (struct netbuf));
if (lq->ld_nb == NULL)
goto cleanup;
nb = svc_getrpccaller(transp);
if (nb == NULL) {
DTRACE_PROBE(mountd, e__nb__enqueue);
goto cleanup;
}
DTRACE_PROBE(mountd, nb_set_enqueue);
lq->ld_nb->maxlen = nb->maxlen;
lq->ld_nb->len = nb->len;
lq->ld_nb->buf = malloc(lq->ld_nb->len);
if (lq->ld_nb->buf == NULL)
goto cleanup;
bcopy(nb->buf, lq->ld_nb->buf, lq->ld_nb->len);
}
lq->ld_path = strdup(path);
if (lq->ld_path == NULL)
goto cleanup;
if (!error) {
lq->ld_rpath = strdup(rpath);
if (lq->ld_rpath == NULL)
goto cleanup;
}
lq->ld_status = status;
/*
* Add to the tail of the logging queue.
*/
(void) mutex_lock(&logging_queue_lock);
if (logging_tail == NULL) {
logging_tail = logging_head = lq;
} else {
logging_tail->ld_next = lq;
logging_tail = lq;
}
(void) cond_signal(&logging_queue_cv);
(void) mutex_unlock(&logging_queue_lock);
return (TRUE);
cleanup:
free_logging_data(lq);
return (FALSE);
}
#define CLN_CLNAMES (1 << 0)
#define CLN_HOST (1 << 1)
static void
cln_init_common(struct cln *cln, SVCXPRT *transp, char *netid,
struct netbuf *nbuf)
{
if ((cln->transp = transp) != NULL) {
assert(netid == NULL && nbuf == NULL);
cln->netid = transp->xp_netid;
cln->nbuf = svc_getrpccaller(transp);
} else {
cln->netid = netid;
cln->nbuf = nbuf;
}
cln->nconf = NULL;
cln->clnames = NULL;
cln->host = NULL;
cln->flags = 0;
}
void
cln_init(struct cln *cln, SVCXPRT *transp)
{
cln_init_common(cln, transp, NULL, NULL);
}
void
cln_init_lazy(struct cln *cln, char *netid, struct netbuf *nbuf)
{
cln_init_common(cln, NULL, netid, nbuf);
}
void
cln_fini(struct cln *cln)
{
if (cln->nconf != NULL)
freenetconfigent(cln->nconf);
if (cln->clnames != NULL)
netdir_free(cln->clnames, ND_HOSTSERVLIST);
free(cln->host);
}
struct netbuf *
cln_getnbuf(struct cln *cln)
{
return (cln->nbuf);
}
struct nd_hostservlist *
cln_getclientsnames(struct cln *cln)
{
if ((cln->flags & CLN_CLNAMES) == 0) {
/*
* nconf is not needed if we do not have nbuf (see
* cln_gethost() too), so we check for nbuf and in a case it is
* NULL we do not try to get nconf.
*/
if (cln->netid != NULL && cln->nbuf != NULL) {
cln->nconf = getnetconfigent(cln->netid);
if (cln->nconf == NULL)
syslog(LOG_ERR, "%s: getnetconfigent failed",
cln->netid);
}
if (cln->nconf != NULL && cln->nbuf != NULL)
(void) __netdir_getbyaddr_nosrv(cln->nconf,
&cln->clnames, cln->nbuf);
cln->flags |= CLN_CLNAMES;
}
return (cln->clnames);
}
/*
* Return B_TRUE if the host is already available at no cost
*/
boolean_t
cln_havehost(struct cln *cln)
{
return ((cln->flags & (CLN_CLNAMES | CLN_HOST)) != 0);
}
char *
cln_gethost(struct cln *cln)
{
if (cln_getclientsnames(cln) != NULL)
return (cln->clnames->h_hostservs[0].h_host);
if ((cln->flags & CLN_HOST) == 0) {
if (cln->nconf == NULL || cln->nbuf == NULL) {
cln->host = strdup("(anon)");
} else {
char host[MAXIPADDRLEN];
if (strcmp(cln->nconf->nc_protofmly, NC_INET) == 0) {
struct sockaddr_in *sa;
/* LINTED pointer alignment */
sa = (struct sockaddr_in *)(cln->nbuf->buf);
(void) inet_ntoa_r(sa->sin_addr, host);
cln->host = strdup(host);
} else if (strcmp(cln->nconf->nc_protofmly,
NC_INET6) == 0) {
struct sockaddr_in6 *sa;
/* LINTED pointer alignment */
sa = (struct sockaddr_in6 *)(cln->nbuf->buf);
(void) inet_ntop(AF_INET6,
sa->sin6_addr.s6_addr,
host, INET6_ADDRSTRLEN);
cln->host = strdup(host);
} else {
syslog(LOG_ERR, gettext("Client's address is "
"neither IPv4 nor IPv6"));
cln->host = strdup("(anon)");
}
}
cln->flags |= CLN_HOST;
}
return (cln->host);
}
/*
* Check mount requests, add to mounted list if ok
*/
static int
mount(struct svc_req *rqstp)
{
SVCXPRT *transp;
int version, vers;
struct fhstatus fhs;
struct mountres3 mountres3;
char fh[FHSIZE3];
int len = FHSIZE3;
char *path, rpath[MAXPATHLEN];
share_t *sh = NULL;
struct cln cln;
char *host = NULL;
int error = 0, lofs_tried = 0, enqueued;
int flavor_list[MAX_FLAVORS];
int flavor_count;
ucred_t *uc = NULL;
int audit_status;
transp = rqstp->rq_xprt;
version = rqstp->rq_vers;
path = NULL;
if (!svc_getargs(transp, xdr_dirpath, (caddr_t)&path)) {
svcerr_decode(transp);
return (EACCES);
}
cln_init(&cln, transp);
/*
* Put off getting the name for the client until we
* need it. This is a performance gain. If we are logging,
* then we don't care about performance and might as well
* get the host name now in case we need to spit out an
* error message.
*/
if (verbose) {
DTRACE_PROBE(mountd, name_by_verbose);
if ((host = cln_gethost(&cln)) == NULL) {
/*
* We failed to get a name for the client, even
* 'anon', probably because we ran out of memory.
* In this situation it doesn't make sense to
* allow the mount to succeed.
*/
error = EACCES;
goto reply;
}
}
/*
* If the version being used is less than the minimum version,
* the filehandle translation should not be provided to the
* client.
*/
if (rejecting || version < mount_vers_min) {
if (verbose)
syslog(LOG_NOTICE, "Rejected mount: %s for %s",
host, path);
error = EACCES;
goto reply;
}
/*
* Trusted Extension doesn't support nfsv2. nfsv2 client
* uses MOUNT protocol v1 and v2. To prevent circumventing
* TX label policy via using nfsv2 client, reject a mount
* request with version less than 3 and log an error.
*/
if (is_system_labeled()) {
if (version < 3) {
if (verbose)
syslog(LOG_ERR,
"Rejected mount: TX doesn't support NFSv2");
error = EACCES;
goto reply;
}
}
/*
* Get the real path (no symbolic links in it)
*/
if (realpath(path, rpath) == NULL) {
error = errno;
if (verbose)
syslog(LOG_ERR,
"mount request: realpath: %s: %m", path);
if (error == ENOENT)
error = mount_enoent_error(&cln, path, rpath,
flavor_list);
goto reply;
}
if ((sh = findentry(rpath)) == NULL &&
(sh = find_lofsentry(rpath, &lofs_tried)) == NULL) {
error = EACCES;
goto reply;
}
/*
* Check if this is a "nosub" only export, in which case, mounting
* subdirectories isn't allowed. Bug 1184573.
*/
if (checkrootmount(sh, rpath) == 0) {
error = EACCES;
goto reply;
}
if (newopts(sh->sh_opts))
flavor_count = getclientsflavors_new(sh, &cln, flavor_list);
else
flavor_count = getclientsflavors_old(sh, &cln, flavor_list);
if (flavor_count == 0) {
error = EACCES;
goto reply;
}
/*
* Check MAC policy here. The server side policy should be
* consistent with client side mount policy, i.e.
* - we disallow an admin_low unlabeled client to mount
* - we disallow mount from a lower labeled client.
*/
if (is_system_labeled()) {
m_label_t *clabel = NULL;
m_label_t *slabel = NULL;
m_label_t admin_low;
if (svc_getcallerucred(rqstp->rq_xprt, &uc) != 0) {
syslog(LOG_ERR,
"mount request: Failed to get caller's ucred : %m");
error = EACCES;
goto reply;
}
if ((clabel = ucred_getlabel(uc)) == NULL) {
syslog(LOG_ERR,
"mount request: can't get client label from ucred");
error = EACCES;
goto reply;
}
bsllow(&admin_low);
if (blequal(&admin_low, clabel)) {
struct sockaddr *ca;
tsol_tpent_t *tp;
ca = (struct sockaddr *)(void *)svc_getrpccaller(
rqstp->rq_xprt)->buf;
if (ca == NULL) {
error = EACCES;
goto reply;
}
/*
* get trusted network template associated
* with the client.
*/
tp = get_client_template(ca);
if (tp == NULL || tp->host_type != SUN_CIPSO) {
if (tp != NULL)
tsol_freetpent(tp);
error = EACCES;
goto reply;
}
tsol_freetpent(tp);
} else {
if ((slabel = m_label_alloc(MAC_LABEL)) == NULL) {
error = EACCES;
goto reply;
}
if (getlabel(rpath, slabel) != 0) {
m_label_free(slabel);
error = EACCES;
goto reply;
}
if (!bldominates(clabel, slabel)) {
m_label_free(slabel);
error = EACCES;
goto reply;
}
m_label_free(slabel);
}
}
/*
* Now get the filehandle.
*
* NFS V2 clients get a 32 byte filehandle.
* NFS V3 clients get a 32 or 64 byte filehandle, depending on
* the embedded FIDs.
*/
vers = (version == MOUNTVERS3) ? NFS_V3 : NFS_VERSION;
/* LINTED pointer alignment */
while (nfs_getfh(rpath, vers, &len, fh) < 0) {
if (errno == EINVAL &&
(sh = find_lofsentry(rpath, &lofs_tried)) != NULL) {
errno = 0;
continue;
}
error = errno == EINVAL ? EACCES : errno;
syslog(LOG_DEBUG, "mount request: getfh failed on %s: %m",
path);
break;
}
if (version == MOUNTVERS3) {
mountres3.mountres3_u.mountinfo.fhandle.fhandle3_len = len;
mountres3.mountres3_u.mountinfo.fhandle.fhandle3_val = fh;
} else {
bcopy(fh, &fhs.fhstatus_u.fhs_fhandle, NFS_FHSIZE);
}
reply:
if (uc != NULL)
ucred_free(uc);
switch (version) {
case MOUNTVERS:
case MOUNTVERS_POSIX:
if (error == EINVAL)
fhs.fhs_status = NFSERR_ACCES;
else if (error == EREMOTE)
fhs.fhs_status = NFSERR_REMOTE;
else
fhs.fhs_status = error;
if (!svc_sendreply(transp, xdr_fhstatus, (char *)&fhs))
log_cant_reply_cln(&cln);
audit_status = fhs.fhs_status;
break;
case MOUNTVERS3:
if (!error) {
mountres3.mountres3_u.mountinfo.auth_flavors.auth_flavors_val =
flavor_list;
mountres3.mountres3_u.mountinfo.auth_flavors.auth_flavors_len =
flavor_count;
} else if (error == ENAMETOOLONG)
error = MNT3ERR_NAMETOOLONG;
mountres3.fhs_status = error;
if (!svc_sendreply(transp, xdr_mountres3, (char *)&mountres3))
log_cant_reply_cln(&cln);
audit_status = mountres3.fhs_status;
break;
}
if (cln_havehost(&cln))
host = cln_gethost(&cln);
if (verbose)
syslog(LOG_NOTICE, "MOUNT: %s %s %s",
(host == NULL) ? "unknown host" : host,
error ? "denied" : "mounted", path);
/*
* If we can not create a queue entry, go ahead and do it
* in the context of this thread.
*/
enqueued = enqueue_logging_data(host, transp, path, rpath,
audit_status, error);
if (enqueued == FALSE) {
if (host == NULL) {
DTRACE_PROBE(mountd, name_by_in_thread);
host = cln_gethost(&cln);
}
DTRACE_PROBE(mountd, logged_in_thread);
audit_mountd_mount(host, path, audit_status); /* BSM */
if (!error)
mntlist_new(host, rpath); /* add entry to mount list */
}
if (path != NULL)
svc_freeargs(transp, xdr_dirpath, (caddr_t)&path);
if (sh)
sharefree(sh);
cln_fini(&cln);
return (error);
}
/*
* Determine whether two paths are within the same file system.
* Returns nonzero (true) if paths are the same, zero (false) if
* they are different. If an error occurs, return false.
*
* Use the actual FSID if it's available (via getattrat()); otherwise,
* fall back on st_dev.
*
* With ZFS snapshots, st_dev differs from the regular file system
* versus the snapshot. But the fsid is the same throughout. Thus
* the fsid is a better test.
*/
static int
same_file_system(const char *path1, const char *path2)
{
uint64_t fsid1, fsid2;
struct stat64 st1, st2;
nvlist_t *nvl1 = NULL;
nvlist_t *nvl2 = NULL;
if ((getattrat(AT_FDCWD, XATTR_VIEW_READONLY, path1, &nvl1) == 0) &&
(getattrat(AT_FDCWD, XATTR_VIEW_READONLY, path2, &nvl2) == 0) &&
(nvlist_lookup_uint64(nvl1, A_FSID, &fsid1) == 0) &&
(nvlist_lookup_uint64(nvl2, A_FSID, &fsid2) == 0)) {
nvlist_free(nvl1);
nvlist_free(nvl2);
/*
* We have found fsid's for both paths.
*/
if (fsid1 == fsid2)
return (B_TRUE);
return (B_FALSE);
}
nvlist_free(nvl1);
nvlist_free(nvl2);
/*
* We were unable to find fsid's for at least one of the paths.
* fall back on st_dev.
*/
if (stat64(path1, &st1) < 0) {
syslog(LOG_NOTICE, "%s: %m", path1);
return (B_FALSE);
}
if (stat64(path2, &st2) < 0) {
syslog(LOG_NOTICE, "%s: %m", path2);
return (B_FALSE);
}
if (st1.st_dev == st2.st_dev)
return (B_TRUE);
return (B_FALSE);
}
share_t *
findentry(char *path)
{
share_t *sh = NULL;
struct sh_list *shp;
char *p1, *p2;
check_sharetab();
(void) rw_rdlock(&sharetab_lock);
for (shp = share_list; shp; shp = shp->shl_next) {
sh = shp->shl_sh;
for (p1 = sh->sh_path, p2 = path; *p1 == *p2; p1++, p2++)
if (*p1 == '\0')
goto done; /* exact match */
/*
* Now compare the pathnames for three cases:
*
* Parent: /export/foo (no trailing slash on parent)
* Child: /export/foo/bar
*
* Parent: /export/foo/ (trailing slash on parent)
* Child: /export/foo/bar
*
* Parent: /export/foo/ (no trailing slash on child)
* Child: /export/foo
*/
if ((*p1 == '\0' && *p2 == '/') ||
(*p1 == '\0' && *(p1-1) == '/') ||
(*p2 == '\0' && *p1 == '/' && *(p1+1) == '\0')) {
/*
* We have a subdirectory. Test whether the
* subdirectory is in the same file system.
*/
if (same_file_system(path, sh->sh_path))
goto done;
}
}
done:
sh = shp ? sharedup(sh) : NULL;
(void) rw_unlock(&sharetab_lock);
return (sh);
}
static int
is_substring(char **mntp, char **path)
{
char *p1 = *mntp, *p2 = *path;
if (*p1 == '\0' && *p2 == '\0') /* exact match */
return (1);
else if (*p1 == '\0' && *p2 == '/')
return (1);
else if (*p1 == '\0' && *(p1-1) == '/') {
*path = --p2; /* we need the slash in p2 */
return (1);
} else if (*p2 == '\0') {
while (*p1 == '/')
p1++;
if (*p1 == '\0') /* exact match */
return (1);
}
return (0);
}
/*
* find_lofsentry() searches for the real path which this requested LOFS path
* (rpath) shadows. If found, it will return the sharetab entry of
* the real path that corresponds to the LOFS path.
* We first search mnttab to see if the requested path is an automounted
* path. If it is an automounted path, it will trigger the mount by stat()ing
* the requested path. Note that it is important to check that this path is
* actually an automounted path, otherwise we would stat() a path which may
* turn out to be NFS and block indefinitely on a dead server. The automounter
* times-out if the server is dead, so there's no risk of hanging this
* thread waiting for stat().
* After the mount has been triggered (if necessary), we look for a
* mountpoint of type LOFS (by searching /etc/mnttab again) which
* is a substring of the rpath. If found, we construct a new path by
* concatenating the mnt_special and the remaining of rpath, call findentry()
* to make sure the 'real path' is shared.
*/
static share_t *
find_lofsentry(char *rpath, int *done_flag)
{
struct stat r_stbuf;
mntlist_t *ml, *mntl, *mntpnt = NULL;
share_t *retcode = NULL;
char tmp_path[MAXPATHLEN];
int mntpnt_len = 0, tmp;
char *p1, *p2;
if ((*done_flag)++)
return (retcode);
/*
* While fsgetmntlist() uses lockf() to
* lock the mnttab before reading it in,
* the lock ignores threads in the same process.
* Read in the mnttab with the protection of a mutex.
*/
(void) mutex_lock(&mnttab_lock);
mntl = fsgetmntlist();
(void) mutex_unlock(&mnttab_lock);
/*
* Obtain the mountpoint for the requested path.
*/
for (ml = mntl; ml; ml = ml->mntl_next) {
for (p1 = ml->mntl_mnt->mnt_mountp, p2 = rpath;
*p1 == *p2 && *p1; p1++, p2++)
;
if (is_substring(&p1, &p2) &&
(tmp = strlen(ml->mntl_mnt->mnt_mountp)) >= mntpnt_len) {
mntpnt = ml;
mntpnt_len = tmp;
}
}
/*
* If the path needs to be autoFS mounted, trigger the mount by
* stat()ing it. This is determined by checking whether the
* mountpoint we just found is of type autofs.
*/
if (mntpnt != NULL &&
strcmp(mntpnt->mntl_mnt->mnt_fstype, "autofs") == 0) {
/*
* The requested path is a substring of an autoFS filesystem.
* Trigger the mount.
*/
if (stat(rpath, &r_stbuf) < 0) {
if (verbose)
syslog(LOG_NOTICE, "%s: %m", rpath);
goto done;
}
if ((r_stbuf.st_mode & S_IFMT) == S_IFDIR) {
/*
* The requested path is a directory, stat(2) it
* again with a trailing '.' to force the autoFS
* module to trigger the mount of indirect
* automount entries, such as /net/jurassic/.
*/
if (strlen(rpath) + 2 > MAXPATHLEN) {
if (verbose) {
syslog(LOG_NOTICE,
"%s/.: exceeds MAXPATHLEN %d",
rpath, MAXPATHLEN);
}
goto done;
}
(void) strcpy(tmp_path, rpath);
(void) strcat(tmp_path, "/.");
if (stat(tmp_path, &r_stbuf) < 0) {
if (verbose)
syslog(LOG_NOTICE, "%s: %m", tmp_path);
goto done;
}
}
/*
* The mount has been triggered, re-read mnttab to pick up
* the changes made by autoFS.
*/
fsfreemntlist(mntl);
(void) mutex_lock(&mnttab_lock);
mntl = fsgetmntlist();
(void) mutex_unlock(&mnttab_lock);
}
/*
* The autoFS mountpoint has been triggered if necessary,
* now search mnttab again to determine if the requested path
* is an LOFS mount of a shared path.
*/
mntpnt_len = 0;
for (ml = mntl; ml; ml = ml->mntl_next) {
if (strcmp(ml->mntl_mnt->mnt_fstype, "lofs"))
continue;
for (p1 = ml->mntl_mnt->mnt_mountp, p2 = rpath;
*p1 == *p2 && *p1; p1++, p2++)
;
if (is_substring(&p1, &p2) &&
((tmp = strlen(ml->mntl_mnt->mnt_mountp)) >= mntpnt_len)) {
mntpnt_len = tmp;
if ((strlen(ml->mntl_mnt->mnt_special) + strlen(p2)) >
MAXPATHLEN) {
if (verbose) {
syslog(LOG_NOTICE, "%s%s: exceeds %d",
ml->mntl_mnt->mnt_special, p2,
MAXPATHLEN);
}
if (retcode)
sharefree(retcode);
retcode = NULL;
goto done;
}
(void) strcpy(tmp_path, ml->mntl_mnt->mnt_special);
(void) strcat(tmp_path, p2);
if (retcode)
sharefree(retcode);
retcode = findentry(tmp_path);
}
}
if (retcode) {
assert(strlen(tmp_path) > 0);
(void) strcpy(rpath, tmp_path);
}
done:
fsfreemntlist(mntl);
return (retcode);
}
/*
* Determine whether an access list grants rights to a particular host.
* We match on aliases of the hostname as well as on the canonical name.
* Names in the access list may be either hosts or netgroups; they're
* not distinguished syntactically. We check for hosts first because
* it's cheaper, then try netgroups.
*
* Return values:
* 1 - access is granted
* 0 - access is denied
* -1 - an error occured
*/
int
in_access_list(struct cln *cln,
char *access_list) /* N.B. we clobber this "input" parameter */
{
char addr[INET_ADDRSTRLEN];
char buff[256];
int nentries = 0;
char *cstr = access_list;
char *gr = access_list;
int i;
int response;
int ret;
struct netbuf *pnb;
struct nd_hostservlist *clnames = NULL;
/* If no access list - then it's unrestricted */
if (access_list == NULL || *access_list == '\0')
return (1);
if ((pnb = cln_getnbuf(cln)) == NULL)
return (-1);
for (;;) {
if ((cstr = strpbrk(cstr, "[:")) != NULL) {
if (*cstr == ':') {
*cstr = '\0';
} else {
assert(*cstr == '[');
cstr = strchr(cstr + 1, ']');
if (cstr == NULL)
return (-1);
cstr++;
continue;
}
}
/*
* If the list name has a '-' prepended then a match of
* the following name implies failure instead of success.
*/
if (*gr == '-') {
response = 0;
gr++;
} else {
response = 1;
}
/*
* First check if we have '@' entry, as it doesn't
* require client hostname.
*/
if (*gr == '@') {
gr++;
/* Netname support */
if (!isdigit(*gr) && *gr != '[') {
struct netent n, *np;
if ((np = getnetbyname_r(gr, &n, buff,
sizeof (buff))) != NULL &&
np->n_net != 0) {
while ((np->n_net & 0xFF000000u) == 0)
np->n_net <<= 8;
np->n_net = htonl(np->n_net);
if (inet_ntop(AF_INET, &np->n_net, addr,
INET_ADDRSTRLEN) == NULL)
break;
ret = inet_matchaddr(pnb->buf, addr);
if (ret == -1) {
if (errno == EINVAL) {
syslog(LOG_WARNING,
"invalid access "
"list entry: %s",
addr);
}
return (-1);
} else if (ret == 1) {
return (response);
}
}
} else {
ret = inet_matchaddr(pnb->buf, gr);
if (ret == -1) {
if (errno == EINVAL) {
syslog(LOG_WARNING,
"invalid access list "
"entry: %s", gr);
}
return (-1);
} else if (ret == 1) {
return (response);
}
}
goto next;
}
/*
* No other checks can be performed if client address
* can't be resolved.
*/
if ((clnames = cln_getclientsnames(cln)) == NULL)
goto next;
/* Otherwise loop through all client hostname aliases */
for (i = 0; i < clnames->h_cnt; i++) {
char *host = clnames->h_hostservs[i].h_host;
/*
* If the list name begins with a dot then
* do a domain name suffix comparison.
* A single dot matches any name with no
* suffix.
*/
if (*gr == '.') {
if (*(gr + 1) == '\0') { /* single dot */
if (strchr(host, '.') == NULL)
return (response);
} else {
int off = strlen(host) - strlen(gr);
if (off > 0 &&
strcasecmp(host + off, gr) == 0) {
return (response);
}
}
} else {
/* Just do a hostname match */
if (strcasecmp(gr, host) == 0)
return (response);
}
}
nentries++;
next:
if (cstr == NULL)
break;
gr = ++cstr;
}
if (clnames == NULL)
return (0);
return (netgroup_check(clnames, access_list, nentries));
}
static char *optlist[] = {
#define OPT_RO 0
SHOPT_RO,
#define OPT_RW 1
SHOPT_RW,
#define OPT_ROOT 2
SHOPT_ROOT,
#define OPT_SECURE 3
SHOPT_SECURE,
#define OPT_ANON 4
SHOPT_ANON,
#define OPT_WINDOW 5
SHOPT_WINDOW,
#define OPT_NOSUID 6
SHOPT_NOSUID,
#define OPT_ACLOK 7
SHOPT_ACLOK,
#define OPT_SEC 8
SHOPT_SEC,
#define OPT_NONE 9
SHOPT_NONE,
#define OPT_UIDMAP 10
SHOPT_UIDMAP,
#define OPT_GIDMAP 11
SHOPT_GIDMAP,
NULL
};
static int
map_flavor(char *str)
{
seconfig_t sec;
if (nfs_getseconfig_byname(str, &sec))
return (-1);
return (sec.sc_nfsnum);
}
/*
* If the option string contains a "sec="
* option, then use new option syntax.
*/
static int
newopts(char *opts)
{
char *head, *p, *val;
if (!opts || *opts == '\0')
return (0);
head = strdup(opts);
if (head == NULL) {
syslog(LOG_ERR, "opts: no memory");
return (0);
}
p = head;
while (*p) {
if (getsubopt(&p, optlist, &val) == OPT_SEC) {
free(head);
return (1);
}
}
free(head);
return (0);
}
/*
* Given an export and the clients hostname(s)
* determine the security flavors that this
* client is permitted to use.
*
* This routine is called only for "old" syntax, i.e.
* only one security flavor is allowed. So we need
* to determine two things: the particular flavor,
* and whether the client is allowed to use this
* flavor, i.e. is in the access list.
*
* Note that if there is no access list, then the
* default is that access is granted.
*/
static int
getclientsflavors_old(share_t *sh, struct cln *cln, int *flavors)
{
char *opts, *p, *val;
boolean_t ok = B_FALSE;
int defaultaccess = 1;
boolean_t reject = B_FALSE;
opts = strdup(sh->sh_opts);
if (opts == NULL) {
syslog(LOG_ERR, "getclientsflavors: no memory");
return (0);
}
flavors[0] = AUTH_SYS;
p = opts;
while (*p) {
switch (getsubopt(&p, optlist, &val)) {
case OPT_SECURE:
flavors[0] = AUTH_DES;
break;
case OPT_RO:
case OPT_RW:
defaultaccess = 0;
if (in_access_list(cln, val) > 0)
ok = B_TRUE;
break;
case OPT_NONE:
defaultaccess = 0;
if (in_access_list(cln, val) > 0)
reject = B_TRUE;
}
}
free(opts);
/* none takes precedence over everything else */
if (reject)
ok = B_FALSE;
return (defaultaccess || ok);
}
/*
* Given an export and the clients hostname(s)
* determine the security flavors that this
* client is permitted to use.
*
* This is somewhat more complicated than the "old"
* routine because the options may contain multiple
* security flavors (sec=) each with its own access
* lists. So a client could be granted access based
* on a number of security flavors. Note that the
* type of access might not always be the same, the
* client may get readonly access with one flavor
* and readwrite with another, however the client
* is not told this detail, it gets only the list
* of flavors, and only if the client is using
* version 3 of the mount protocol.
*/
static int
getclientsflavors_new(share_t *sh, struct cln *cln, int *flavors)
{
char *opts, *p, *val;
char *lasts;
char *f;
boolean_t defaultaccess = B_TRUE; /* default access is rw */
boolean_t access_ok = B_FALSE;
int count, c;
boolean_t reject = B_FALSE;
opts = strdup(sh->sh_opts);
if (opts == NULL) {
syslog(LOG_ERR, "getclientsflavors: no memory");
return (0);
}
p = opts;
count = c = 0;
while (*p) {
switch (getsubopt(&p, optlist, &val)) {
case OPT_SEC:
if (reject)
access_ok = B_FALSE;
/*
* Before a new sec=xxx option, check if we need
* to move the c index back to the previous count.
*/
if (!defaultaccess && !access_ok) {
c = count;
}
/* get all the sec=f1[:f2] flavors */
while ((f = strtok_r(val, ":", &lasts)) != NULL) {
flavors[c++] = map_flavor(f);
val = NULL;
}
/* for a new sec=xxx option, default is rw access */
defaultaccess = B_TRUE;
access_ok = B_FALSE;
reject = B_FALSE;
break;
case OPT_RO:
case OPT_RW:
defaultaccess = B_FALSE;
if (in_access_list(cln, val) > 0)
access_ok = B_TRUE;
break;
case OPT_NONE:
defaultaccess = B_FALSE;
if (in_access_list(cln, val) > 0)
reject = B_TRUE; /* none overides rw/ro */
break;
}
}
if (reject)
access_ok = B_FALSE;
if (!defaultaccess && !access_ok)
c = count;
free(opts);
return (c);
}
/*
* This is a tricky piece of code that parses the
* share options looking for a match on the auth
* flavor that the client is using. If it finds
* a match, then the client is given ro, rw, or
* no access depending whether it is in the access
* list. There is a special case for "secure"
* flavor. Other flavors are values of the new "sec=" option.
*/
int
check_client(share_t *sh, struct cln *cln, int flavor, uid_t clnt_uid,
gid_t clnt_gid, uint_t clnt_ngids, gid_t *clnt_gids, uid_t *srv_uid,
gid_t *srv_gid, uint_t *srv_ngids, gid_t **srv_gids)
{
if (newopts(sh->sh_opts))
return (check_client_new(sh, cln, flavor, clnt_uid, clnt_gid,
clnt_ngids, clnt_gids, srv_uid, srv_gid, srv_ngids,
srv_gids));
else
return (check_client_old(sh, cln, flavor, clnt_uid, clnt_gid,
clnt_ngids, clnt_gids, srv_uid, srv_gid, srv_ngids,
srv_gids));
}
extern int _getgroupsbymember(const char *, gid_t[], int, int);
/*
* Get supplemental groups for uid
*/
static int
getusergroups(uid_t uid, uint_t *ngrps, gid_t **grps)
{
struct passwd pwd;
char *pwbuf = alloca(pw_size);
gid_t *tmpgrps = alloca(ngroups_max * sizeof (gid_t));
int tmpngrps;
if (getpwuid_r(uid, &pwd, pwbuf, pw_size) == NULL)
return (-1);
tmpgrps[0] = pwd.pw_gid;
tmpngrps = _getgroupsbymember(pwd.pw_name, tmpgrps, ngroups_max, 1);
if (tmpngrps <= 0) {
syslog(LOG_WARNING,
"getusergroups(): Unable to get groups for user %s",
pwd.pw_name);
return (-1);
}
*grps = malloc(tmpngrps * sizeof (gid_t));
if (*grps == NULL) {
syslog(LOG_ERR,
"getusergroups(): Memory allocation failed: %m");
return (-1);
}
*ngrps = tmpngrps;
(void) memcpy(*grps, tmpgrps, tmpngrps * sizeof (gid_t));
return (0);
}
/*
* is_a_number(number)
*
* is the string a number in one of the forms we want to use?
*/
static int
is_a_number(char *number)
{
int ret = 1;
int hex = 0;
if (strncmp(number, "0x", 2) == 0) {
number += 2;
hex = 1;
} else if (*number == '-') {
number++; /* skip the minus */
}
while (ret == 1 && *number != '\0') {
if (hex) {
ret = isxdigit(*number++);
} else {
ret = isdigit(*number++);
}
}
return (ret);
}
static boolean_t
get_uid(char *value, uid_t *uid)
{
if (!is_a_number(value)) {
struct passwd *pw;
/*
* in this case it would have to be a
* user name
*/
pw = getpwnam(value);
if (pw == NULL)
return (B_FALSE);
*uid = pw->pw_uid;
endpwent();
} else {
uint64_t intval;
intval = strtoull(value, NULL, 0);
if (intval > UID_MAX && intval != -1)
return (B_FALSE);
*uid = (uid_t)intval;
}
return (B_TRUE);
}
static boolean_t
get_gid(char *value, gid_t *gid)
{
if (!is_a_number(value)) {
struct group *gr;
/*
* in this case it would have to be a
* group name
*/
gr = getgrnam(value);
if (gr == NULL)
return (B_FALSE);
*gid = gr->gr_gid;
endgrent();
} else {
uint64_t intval;
intval = strtoull(value, NULL, 0);
if (intval > UID_MAX && intval != -1)
return (B_FALSE);
*gid = (gid_t)intval;
}
return (B_TRUE);
}
static int
check_client_old(share_t *sh, struct cln *cln, int flavor, uid_t clnt_uid,
gid_t clnt_gid, uint_t clnt_ngids, gid_t *clnt_gids, uid_t *srv_uid,
gid_t *srv_gid, uint_t *srv_ngids, gid_t **srv_gids)
{
char *opts, *p, *val;
int match; /* Set when a flavor is matched */
int perm = 0; /* Set when "ro", "rw" or "root" is matched */
int list = 0; /* Set when "ro", "rw" is found */
int ro_val = 0; /* Set if ro option is 'ro=' */
int rw_val = 0; /* Set if rw option is 'rw=' */
boolean_t map_deny = B_FALSE;
opts = strdup(sh->sh_opts);
if (opts == NULL) {
syslog(LOG_ERR, "check_client: no memory");
return (0);
}
/*
* If client provided 16 supplemental groups with AUTH_SYS, lookup
* locally for all of them
*/
if (flavor == AUTH_SYS && clnt_ngids == NGRPS && ngroups_max > NGRPS)
if (getusergroups(clnt_uid, srv_ngids, srv_gids) == 0)
perm |= NFSAUTH_GROUPS;
p = opts;
match = AUTH_UNIX;
while (*p) {
switch (getsubopt(&p, optlist, &val)) {
case OPT_SECURE:
match = AUTH_DES;
if (perm & NFSAUTH_GROUPS) {
free(*srv_gids);
*srv_ngids = 0;
*srv_gids = NULL;
perm &= ~NFSAUTH_GROUPS;
}
break;
case OPT_RO:
list++;
if (val != NULL)
ro_val++;
if (in_access_list(cln, val) > 0)
perm |= NFSAUTH_RO;
break;
case OPT_RW:
list++;
if (val != NULL)
rw_val++;
if (in_access_list(cln, val) > 0)
perm |= NFSAUTH_RW;
break;
case OPT_ROOT:
/*
* Check if the client is in
* the root list. Only valid
* for AUTH_SYS.
*/
if (flavor != AUTH_SYS)
break;
if (val == NULL || *val == '\0')
break;
if (clnt_uid != 0)
break;
if (in_access_list(cln, val) > 0) {
perm |= NFSAUTH_ROOT;
perm |= NFSAUTH_UIDMAP | NFSAUTH_GIDMAP;
map_deny = B_FALSE;
if (perm & NFSAUTH_GROUPS) {
free(*srv_gids);
*srv_ngids = 0;
*srv_gids = NULL;
perm &= ~NFSAUTH_GROUPS;
}
}
break;
case OPT_NONE:
/*
* Check if the client should have no access
* to this share at all. This option behaves
* more like "root" than either "rw" or "ro".
*/
if (in_access_list(cln, val) > 0)
perm |= NFSAUTH_DENIED;
break;
case OPT_UIDMAP: {
char *c;
char *n;
/*
* The uidmap is supported for AUTH_SYS only.
*/
if (flavor != AUTH_SYS)
break;
if (perm & NFSAUTH_UIDMAP || map_deny)
break;
for (c = val; c != NULL; c = n) {
char *s;
char *al;
uid_t srv;
n = strchr(c, '~');
if (n != NULL)
*n++ = '\0';
s = strchr(c, ':');
if (s != NULL) {
*s++ = '\0';
al = strchr(s, ':');
if (al != NULL)
*al++ = '\0';
}
if (s == NULL || al == NULL)
continue;
if (*c == '\0') {
if (clnt_uid != (uid_t)-1)
continue;
} else if (strcmp(c, "*") != 0) {
uid_t clnt;
if (!get_uid(c, &clnt))
continue;
if (clnt_uid != clnt)
continue;
}
if (*s == '\0')
srv = UID_NOBODY;
else if (!get_uid(s, &srv))
continue;
else if (srv == (uid_t)-1) {
map_deny = B_TRUE;
break;
}
if (in_access_list(cln, al) > 0) {
*srv_uid = srv;
perm |= NFSAUTH_UIDMAP;
if (perm & NFSAUTH_GROUPS) {
free(*srv_gids);
*srv_ngids = 0;
*srv_gids = NULL;
perm &= ~NFSAUTH_GROUPS;
}
break;
}
}
break;
}
case OPT_GIDMAP: {
char *c;
char *n;
/*
* The gidmap is supported for AUTH_SYS only.
*/
if (flavor != AUTH_SYS)
break;
if (perm & NFSAUTH_GIDMAP || map_deny)
break;
for (c = val; c != NULL; c = n) {
char *s;
char *al;
gid_t srv;
n = strchr(c, '~');
if (n != NULL)
*n++ = '\0';
s = strchr(c, ':');
if (s != NULL) {
*s++ = '\0';
al = strchr(s, ':');
if (al != NULL)
*al++ = '\0';
}
if (s == NULL || al == NULL)
break;
if (*c == '\0') {
if (clnt_gid != (gid_t)-1)
continue;
} else if (strcmp(c, "*") != 0) {
gid_t clnt;
if (!get_gid(c, &clnt))
continue;
if (clnt_gid != clnt)
continue;
}
if (*s == '\0')
srv = UID_NOBODY;
else if (!get_gid(s, &srv))
continue;
else if (srv == (gid_t)-1) {
map_deny = B_TRUE;
break;
}
if (in_access_list(cln, al) > 0) {
*srv_gid = srv;
perm |= NFSAUTH_GIDMAP;
if (perm & NFSAUTH_GROUPS) {
free(*srv_gids);
*srv_ngids = 0;
*srv_gids = NULL;
perm &= ~NFSAUTH_GROUPS;
}
break;
}
}
break;
}
default:
break;
}
}
free(opts);
if (perm & NFSAUTH_ROOT) {
*srv_uid = 0;
*srv_gid = 0;
}
if (map_deny)
perm |= NFSAUTH_DENIED;
if (!(perm & NFSAUTH_UIDMAP))
*srv_uid = clnt_uid;
if (!(perm & NFSAUTH_GIDMAP))
*srv_gid = clnt_gid;
if (flavor != match || perm & NFSAUTH_DENIED)
return (NFSAUTH_DENIED);
if (list) {
/*
* If the client doesn't match an "ro" or "rw"
* list then set no access.
*/
if ((perm & (NFSAUTH_RO | NFSAUTH_RW)) == 0)
perm |= NFSAUTH_DENIED;
} else {
/*
* The client matched a flavor entry that
* has no explicit "rw" or "ro" determination.
* Default it to "rw".
*/
perm |= NFSAUTH_RW;
}
/*
* The client may show up in both ro= and rw=
* lists. If so, then turn off the RO access
* bit leaving RW access.
*/
if (perm & NFSAUTH_RO && perm & NFSAUTH_RW) {
/*
* Logically cover all permutations of rw=,ro=.
* In the case where, rw,ro=<host> we would like
* to remove RW access for the host. In all other cases
* RW wins the precedence battle.
*/
if (!rw_val && ro_val) {
perm &= ~(NFSAUTH_RW);
} else {
perm &= ~(NFSAUTH_RO);
}
}
return (perm);
}
/*
* Check if the client has access by using a flavor different from
* the given "flavor". If "flavor" is not in the flavor list,
* return TRUE to indicate that this "flavor" is a wrong sec.
*/
static bool_t
is_wrongsec(share_t *sh, struct cln *cln, int flavor)
{
int flavor_list[MAX_FLAVORS];
int flavor_count, i;
/* get the flavor list that the client has access with */
flavor_count = getclientsflavors_new(sh, cln, flavor_list);
if (flavor_count == 0)
return (FALSE);
/*
* Check if the given "flavor" is in the flavor_list.
*/
for (i = 0; i < flavor_count; i++) {
if (flavor == flavor_list[i])
return (FALSE);
}
/*
* If "flavor" is not in the flavor_list, return TRUE to indicate
* that the client should have access by using a security flavor
* different from this "flavor".
*/
return (TRUE);
}
/*
* Given an export and the client's hostname, we
* check the security options to see whether the
* client is allowed to use the given security flavor.
*
* The strategy is to proceed through the options looking
* for a flavor match, then pay attention to the ro, rw,
* and root options.
*
* Note that an entry may list several flavors in a
* single entry, e.g.
*
* sec=krb5,rw=clnt1:clnt2,ro,sec=sys,ro
*
*/
static int
check_client_new(share_t *sh, struct cln *cln, int flavor, uid_t clnt_uid,
gid_t clnt_gid, uint_t clnt_ngids, gid_t *clnt_gids, uid_t *srv_uid,
gid_t *srv_gid, uint_t *srv_ngids, gid_t **srv_gids)
{
char *opts, *p, *val;
char *lasts;
char *f;
int match = 0; /* Set when a flavor is matched */
int perm = 0; /* Set when "ro", "rw" or "root" is matched */
int list = 0; /* Set when "ro", "rw" is found */
int ro_val = 0; /* Set if ro option is 'ro=' */
int rw_val = 0; /* Set if rw option is 'rw=' */
boolean_t map_deny = B_FALSE;
opts = strdup(sh->sh_opts);
if (opts == NULL) {
syslog(LOG_ERR, "check_client: no memory");
return (0);
}
/*
* If client provided 16 supplemental groups with AUTH_SYS, lookup
* locally for all of them
*/
if (flavor == AUTH_SYS && clnt_ngids == NGRPS && ngroups_max > NGRPS)
if (getusergroups(clnt_uid, srv_ngids, srv_gids) == 0)
perm |= NFSAUTH_GROUPS;
p = opts;
while (*p) {
switch (getsubopt(&p, optlist, &val)) {
case OPT_SEC:
if (match)
goto done;
while ((f = strtok_r(val, ":", &lasts))
!= NULL) {
if (flavor == map_flavor(f)) {
match = 1;
break;
}
val = NULL;
}
break;
case OPT_RO:
if (!match)
break;
list++;
if (val != NULL)
ro_val++;
if (in_access_list(cln, val) > 0)
perm |= NFSAUTH_RO;
break;
case OPT_RW:
if (!match)
break;
list++;
if (val != NULL)
rw_val++;
if (in_access_list(cln, val) > 0)
perm |= NFSAUTH_RW;
break;
case OPT_ROOT:
/*
* Check if the client is in
* the root list. Only valid
* for AUTH_SYS.
*/
if (flavor != AUTH_SYS)
break;
if (!match)
break;
if (val == NULL || *val == '\0')
break;
if (clnt_uid != 0)
break;
if (in_access_list(cln, val) > 0) {
perm |= NFSAUTH_ROOT;
perm |= NFSAUTH_UIDMAP | NFSAUTH_GIDMAP;
map_deny = B_FALSE;
if (perm & NFSAUTH_GROUPS) {
free(*srv_gids);
*srv_gids = NULL;
*srv_ngids = 0;
perm &= ~NFSAUTH_GROUPS;
}
}
break;
case OPT_NONE:
/*
* Check if the client should have no access
* to this share at all. This option behaves
* more like "root" than either "rw" or "ro".
*/
if (in_access_list(cln, val) > 0)
perm |= NFSAUTH_DENIED;
break;
case OPT_UIDMAP: {
char *c;
char *n;
/*
* The uidmap is supported for AUTH_SYS only.
*/
if (flavor != AUTH_SYS)
break;
if (!match || perm & NFSAUTH_UIDMAP || map_deny)
break;
for (c = val; c != NULL; c = n) {
char *s;
char *al;
uid_t srv;
n = strchr(c, '~');
if (n != NULL)
*n++ = '\0';
s = strchr(c, ':');
if (s != NULL) {
*s++ = '\0';
al = strchr(s, ':');
if (al != NULL)
*al++ = '\0';
}
if (s == NULL || al == NULL)
continue;
if (*c == '\0') {
if (clnt_uid != (uid_t)-1)
continue;
} else if (strcmp(c, "*") != 0) {
uid_t clnt;
if (!get_uid(c, &clnt))
continue;
if (clnt_uid != clnt)
continue;
}
if (*s == '\0')
srv = UID_NOBODY;
else if (!get_uid(s, &srv))
continue;
else if (srv == (uid_t)-1) {
map_deny = B_TRUE;
break;
}
if (in_access_list(cln, al) > 0) {
*srv_uid = srv;
perm |= NFSAUTH_UIDMAP;
if (perm & NFSAUTH_GROUPS) {
free(*srv_gids);
*srv_gids = NULL;
*srv_ngids = 0;
perm &= ~NFSAUTH_GROUPS;
}
break;
}
}
break;
}
case OPT_GIDMAP: {
char *c;
char *n;
/*
* The gidmap is supported for AUTH_SYS only.
*/
if (flavor != AUTH_SYS)
break;
if (!match || perm & NFSAUTH_GIDMAP || map_deny)
break;
for (c = val; c != NULL; c = n) {
char *s;
char *al;
gid_t srv;
n = strchr(c, '~');
if (n != NULL)
*n++ = '\0';
s = strchr(c, ':');
if (s != NULL) {
*s++ = '\0';
al = strchr(s, ':');
if (al != NULL)
*al++ = '\0';
}
if (s == NULL || al == NULL)
break;
if (*c == '\0') {
if (clnt_gid != (gid_t)-1)
continue;
} else if (strcmp(c, "*") != 0) {
gid_t clnt;
if (!get_gid(c, &clnt))
continue;
if (clnt_gid != clnt)
continue;
}
if (*s == '\0')
srv = UID_NOBODY;
else if (!get_gid(s, &srv))
continue;
else if (srv == (gid_t)-1) {
map_deny = B_TRUE;
break;
}
if (in_access_list(cln, al) > 0) {
*srv_gid = srv;
perm |= NFSAUTH_GIDMAP;
if (perm & NFSAUTH_GROUPS) {
free(*srv_gids);
*srv_gids = NULL;
*srv_ngids = 0;
perm &= ~NFSAUTH_GROUPS;
}
break;
}
}
break;
}
default:
break;
}
}
done:
if (perm & NFSAUTH_ROOT) {
*srv_uid = 0;
*srv_gid = 0;
}
if (map_deny)
perm |= NFSAUTH_DENIED;
if (!(perm & NFSAUTH_UIDMAP))
*srv_uid = clnt_uid;
if (!(perm & NFSAUTH_GIDMAP))
*srv_gid = clnt_gid;
/*
* If no match then set the perm accordingly
*/
if (!match || perm & NFSAUTH_DENIED) {
free(opts);
return (NFSAUTH_DENIED);
}
if (list) {
/*
* If the client doesn't match an "ro" or "rw" list then
* check if it may have access by using a different flavor.
* If so, return NFSAUTH_WRONGSEC.
* If not, return NFSAUTH_DENIED.
*/
if ((perm & (NFSAUTH_RO | NFSAUTH_RW)) == 0) {
if (is_wrongsec(sh, cln, flavor))
perm |= NFSAUTH_WRONGSEC;
else
perm |= NFSAUTH_DENIED;
}
} else {
/*
* The client matched a flavor entry that
* has no explicit "rw" or "ro" determination.
* Make sure it defaults to "rw".
*/
perm |= NFSAUTH_RW;
}
/*
* The client may show up in both ro= and rw=
* lists. If so, then turn off the RO access
* bit leaving RW access.
*/
if (perm & NFSAUTH_RO && perm & NFSAUTH_RW) {
/*
* Logically cover all permutations of rw=,ro=.
* In the case where, rw,ro=<host> we would like
* to remove RW access for the host. In all other cases
* RW wins the precedence battle.
*/
if (!rw_val && ro_val) {
perm &= ~(NFSAUTH_RW);
} else {
perm &= ~(NFSAUTH_RO);
}
}
free(opts);
return (perm);
}
void
check_sharetab()
{
FILE *f;
struct stat st;
static timestruc_t last_sharetab_time;
timestruc_t prev_sharetab_time;
share_t *sh;
struct sh_list *shp, *shp_prev;
int res, c = 0;
/*
* read in /etc/dfs/sharetab if it has changed
*/
if (stat(SHARETAB, &st) != 0) {
syslog(LOG_ERR, "Cannot stat %s: %m", SHARETAB);
return;
}
if (st.st_mtim.tv_sec == last_sharetab_time.tv_sec &&
st.st_mtim.tv_nsec == last_sharetab_time.tv_nsec) {
/*
* No change.
*/
return;
}
/*
* Remember the mod time, then after getting the
* write lock check again. If another thread
* already did the update, then there's no
* work to do.
*/
prev_sharetab_time = last_sharetab_time;
(void) rw_wrlock(&sharetab_lock);
if (prev_sharetab_time.tv_sec != last_sharetab_time.tv_sec ||
prev_sharetab_time.tv_nsec != last_sharetab_time.tv_nsec) {
(void) rw_unlock(&sharetab_lock);
return;
}
/*
* Note that since the sharetab is now in memory
* and a snapshot is taken, we no longer have to
* lock the file.
*/
f = fopen(SHARETAB, "r");
if (f == NULL) {
syslog(LOG_ERR, "Cannot open %s: %m", SHARETAB);
(void) rw_unlock(&sharetab_lock);
return;
}
/*
* Once we are sure /etc/dfs/sharetab has been
* modified, flush netgroup cache entries.
*/
netgrp_cache_flush();
sh_free(share_list); /* free old list */
share_list = NULL;
while ((res = getshare(f, &sh)) > 0) {
c++;
if (strcmp(sh->sh_fstype, "nfs") != 0)
continue;
shp = malloc(sizeof (*shp));
if (shp == NULL)
goto alloc_failed;
if (share_list == NULL)
share_list = shp;
else
/* LINTED not used before set */
shp_prev->shl_next = shp;
shp_prev = shp;
shp->shl_next = NULL;
shp->shl_sh = sharedup(sh);
if (shp->shl_sh == NULL)
goto alloc_failed;
}
if (res < 0)
syslog(LOG_ERR, "%s: invalid at line %d\n",
SHARETAB, c + 1);
if (stat(SHARETAB, &st) != 0) {
syslog(LOG_ERR, "Cannot stat %s: %m", SHARETAB);
(void) fclose(f);
(void) rw_unlock(&sharetab_lock);
return;
}
last_sharetab_time = st.st_mtim;
(void) fclose(f);
(void) rw_unlock(&sharetab_lock);
return;
alloc_failed:
syslog(LOG_ERR, "check_sharetab: no memory");
sh_free(share_list);
share_list = NULL;
(void) fclose(f);
(void) rw_unlock(&sharetab_lock);
}
static void
sh_free(struct sh_list *shp)
{
struct sh_list *next;
while (shp) {
sharefree(shp->shl_sh);
next = shp->shl_next;
free(shp);
shp = next;
}
}
/*
* Remove an entry from mounted list
*/
static void
umount(struct svc_req *rqstp)
{
char *host, *path, *remove_path;
char rpath[MAXPATHLEN];
SVCXPRT *transp;
struct cln cln;
transp = rqstp->rq_xprt;
path = NULL;
if (!svc_getargs(transp, xdr_dirpath, (caddr_t)&path)) {
svcerr_decode(transp);
return;
}
cln_init(&cln, transp);
errno = 0;
if (!svc_sendreply(transp, xdr_void, (char *)NULL))
log_cant_reply_cln(&cln);
host = cln_gethost(&cln);
if (host == NULL) {
/*
* Without the hostname we can't do audit or delete
* this host from the mount entries.
*/
svc_freeargs(transp, xdr_dirpath, (caddr_t)&path);
return;
}
if (verbose)
syslog(LOG_NOTICE, "UNMOUNT: %s unmounted %s", host, path);
audit_mountd_umount(host, path);
remove_path = rpath; /* assume we will use the cannonical path */
if (realpath(path, rpath) == NULL) {
if (verbose)
syslog(LOG_WARNING, "UNMOUNT: realpath: %s: %m ", path);
remove_path = path; /* use path provided instead */
}
mntlist_delete(host, remove_path); /* remove from mount list */
cln_fini(&cln);
svc_freeargs(transp, xdr_dirpath, (caddr_t)&path);
}
/*
* Remove all entries for one machine from mounted list
*/
static void
umountall(struct svc_req *rqstp)
{
SVCXPRT *transp;
char *host;
struct cln cln;
transp = rqstp->rq_xprt;
if (!svc_getargs(transp, xdr_void, NULL)) {
svcerr_decode(transp);
return;
}
/*
* We assume that this call is asynchronous and made via rpcbind
* callit routine. Therefore return control immediately. The error
* causes rpcbind to remain silent, as opposed to every machine
* on the net blasting the requester with a response.
*/
svcerr_systemerr(transp);
cln_init(&cln, transp);
host = cln_gethost(&cln);
if (host == NULL) {
/* Can't do anything without the name of the client */
return;
}
/*
* Remove all hosts entries from mount list
*/
mntlist_delete_all(host);
if (verbose)
syslog(LOG_NOTICE, "UNMOUNTALL: from %s", host);
cln_fini(&cln);
}
void *
exmalloc(size_t size)
{
void *ret;
if ((ret = malloc(size)) == NULL) {
syslog(LOG_ERR, "Out of memory");
exit(1);
}
return (ret);
}
static tsol_tpent_t *
get_client_template(struct sockaddr *sock)
{
in_addr_t v4client;
in6_addr_t v6client;
char v4_addr[INET_ADDRSTRLEN];
char v6_addr[INET6_ADDRSTRLEN];
tsol_rhent_t *rh;
tsol_tpent_t *tp;
switch (sock->sa_family) {
case AF_INET:
v4client = ((struct sockaddr_in *)(void *)sock)->
sin_addr.s_addr;
if (inet_ntop(AF_INET, &v4client, v4_addr, INET_ADDRSTRLEN) ==
NULL)
return (NULL);
rh = tsol_getrhbyaddr(v4_addr, sizeof (v4_addr), AF_INET);
if (rh == NULL)
return (NULL);
tp = tsol_gettpbyname(rh->rh_template);
tsol_freerhent(rh);
return (tp);
break;
case AF_INET6:
v6client = ((struct sockaddr_in6 *)(void *)sock)->sin6_addr;
if (inet_ntop(AF_INET6, &v6client, v6_addr, INET6_ADDRSTRLEN) ==
NULL)
return (NULL);
rh = tsol_getrhbyaddr(v6_addr, sizeof (v6_addr), AF_INET6);
if (rh == NULL)
return (NULL);
tp = tsol_gettpbyname(rh->rh_template);
tsol_freerhent(rh);
return (tp);
break;
default:
return (NULL);
}
}
|