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
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
|
/* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
/* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
/* All Rights Reserved */
/* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF */
/* UNIX System Laboratories, Inc. */
/* The copyright notice above does not evidence any */
/* actual or intended publication of such source code. */
#ident "@(#)login:login.c 1.43.6.83"
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
/* This Module contains Proprietary Information of Microsoft */
/* Corporation and should be treated as Confidential. */
/***************************************************************************
* Command: login
*
* Usage: login [[-p] name [env-var ... ]]
*
*
* Files: /etc/utmp
* /etc/wtmp
* /etc/dialups
* /etc/d_passwd
* /var/adm/lastlog
* /var/adm/loginlog
* /etc/default/login
*
* Notes: Conditional assemblies:
* NO_MAIL causes the MAIL environment variable not to be set
* specified by CONSOLE. CONSOLE MUST NOT be defined as
* either "/dev/syscon" or "/dev/systty"!!
* MAXTRYS is the number of attempts permitted. 0 is "no limit".
* AUX_SECURITY enables authentication through external programs
* instead of encrypted passwords.
***************************************************************************/
/* LINTLIBRARY */
#include <sys/types.h>
#include <utmpx.h>
#include <signal.h>
#include <pwd.h>
#include <ctype.h>
#include <syslog.h>
#include <proj.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h> /* For logfile locking */
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/utsname.h>
#include <utime.h>
#include <termio.h>
#include <sys/stropts.h>
#include <shadow.h> /* shadow password header file */
#include <time.h>
#include <sys/param.h>
#include <sys/fcntl.h>
#include <deflt.h>
#include <grp.h>
#include <ia.h>
#include <libgen.h>
#include <stdlib.h>
#include <sat.h>
#include <mls.h>
#include <sys/mac.h>
#include <sys/capability.h>
#include <capability.h>
#include <sys/file.h> /* for flock in update_count() */
#include <errno.h>
#include <lastlog.h>
#include <locale.h>
#include <pfmt.h>
#include <sys/stream.h>
#include <paths.h>
#include <sys/quota.h>
#include <sys/syslog.h>
#include <sys/wait.h>
#include <di_aux.h>
#include <limits.h>
#if defined(_SHAREII) || defined(DCE)
#include <dlfcn.h>
#endif /* defined(_SHAREII) || defined(DCE) */
#ifdef _SHAREII
#include <shareIIhooks.h>
SH_DECLARE_HOOK(SETMYNAME);
SH_DECLARE_HOOK(LOGIN);
#endif /* _SHAREII */
#define LANG_FILE "/.lang" /* default lang file */
#define TZ_FILE "/.timezone" /* default timezone file */
#ifdef AUX_SECURITY
#define SITE_OK 0
#define SITE_FAIL 1
#define SITE_AGAIN 2
#define SITE_CONTINUE 3
#endif /* AUX_SECURITY */
/*
* Lastlog structure from 4.0 release.
*/
struct lastlog4 {
int ll_status; /* boolean to indicate login success
* or failure. */
time_t ll_time;
char ll_line[12]; /* if local: tty name,
* if rlogin: remote user name. */
char ll_host[64];
};
/*
* Lastlog structure from 5.0 alpha releases.
*/
struct lastlog5a {
time_t ll_time;
char ll_line[12]; /* same as in utmp */
char ll_host[16]; /* same as in utmp */
ulong ll_level; /* MAC security level */
};
/*
* The following defines are macros used throughout login.
*/
#define SCPYN(a, b) (void) strncpy((a), (b), (sizeof((a))-1))
#define EQN(a, b) (!strncmp((a), (b), strlen(a)))
#define ENVSTRNCAT(to, from) {size_t deflen; deflen = strlen(to);\
(void) strncpy((to) + deflen, (from),\
sizeof(to) - (1 + deflen));}
/*
* The following defines are for different files.
*/
#define SHELL _PATH_BSHELL
#define SHELL2 "/sbin/sh"
#define LASTLOG _PATH_LASTLOG
#define LOGINLOG _PATH_LOGINLOG
#define DIAL_FILE _PATH_DIALUPS
#define DPASS_FILE _PATH_DIALPASS
#define BADLOGDIR "/var/adm/badlogin" /* used by update_count() */
#ifdef EXECSH
#define SUBLOGIN "<!sublogin>"
#define NMAX 32
#endif /* EXECSH */
/*
* The following defines are for MAXIMUM values.
*/
#define MAXENV 1024
#define MAXLINE 256
#define MAXTIME 60 /* default */
#define MAXTRYS 3 /* default */
#define MAXARGS 63
#define MAX_TIMEOUT (15 * 60)
#define MAX_FAILURES 20 /* MAX value LOGFAILURES */
/*
* The following defines are for DEFAULT values.
*/
#define DEF_TZ "PST8PDT"
#define DEF_HZ "100"
#define DEFUMASK 077
#define DEF_PATH _PATH_USERPATH
#define DEF_SUPATH _PATH_ROOTPATH
#define DEF_TIMEOUT 60
#define DEF_LANG "C"
/*
* The following defines don't fit into the MAXIMUM or DEFAULT
* categories listed above.
*/
#define PBUFSIZE PASS_MAX /* max significant chars in a password */
#define SLEEPTIME 1 /* sleeptime before login incorrect msg */
#define LNAME_SIZE 32 /* size of logname */
#define TTYN_SIZE 15 /* size of logged tty name */
#define TIME_SIZE 30 /* size of logged time string */
#define L_WAITTIME 5 /* waittime for log file to unlock */
#define DISABLETIME 20 /* seconds login disabled after LOGFAILURES or
MAXTRYS unsuccesful attempts. */
#define LOGFAILURES 3 /* default */
#define ENT_SIZE (LNAME_SIZE + TTYN_SIZE + TIME_SIZE + 3)
#define NAME_DELIM 32 /* LOCKOUTEXEMPT name delimiter */
#define MAC_SESSION 0
#define MAC_CLEARANCE 1
/* XXX from libcmd */
extern FILE *defopen(char *);
extern char *defread(FILE *, char *);
extern char *sttyname(struct stat *);
/* XXX from libc */
extern int __ruserok_x(char *, int, char *, char *, uid_t, char *);
#ifdef DCE
int dce_verify(char *user, uid_t uid, gid_t gid, char *passwd, char **msg);
#endif /* DCE */
static char u_name[LNAME_SIZE],
term[5+64] = {""}, /* "TERM=" */
hertz[10] = { "HZ=" },
timez[100] = { "TZ=" },
#ifdef EXECSH
minusnam[16] = {"-"},
#endif /* EXECSH */
env_remotehost[257+12] = { "REMOTEHOST=" },
env_remotename[12+NMAX] = { "REMOTEUSER=" },
user[LNAME_SIZE+6] = { "USER=" },
path[1024] = { "PATH=" },
home[1024] = { "HOME=" },
shell[1024] = { "SHELL=" },
logname[LNAME_SIZE + 8] = {"LOGNAME="},
#ifndef NO_MAIL
mail[LNAME_SIZE + 15] = { "MAIL=" },
#endif
lang[NL_LANGMAX + 8] = { "LANG=" },
*incorrectmsg = "Login incorrect\n",
*incorrectmsgid = ":309",
*envinit[10 + MAXARGS] = {home, path, logname, hertz, timez, term, user, lang, 0, 0};
static char zero[] = { "" };
static char *ttyn = zero,
*rttyn = zero,
*Def_tz = zero,
*Console = zero,
*Passreq = zero,
*Altshell = zero,
*Mandpass = zero,
*Initgroups = zero,
*SVR4_Signals = zero,
*Def_path = zero,
*Def_term = zero,
*Def_hertz = zero,
*Def_syslog = zero,
#ifdef AUX_SECURITY
*Def_sitepath = zero,
#endif
*Def_lang = zero,
*Def_supath = zero,
*Def_notlockout = zero;
static unsigned Def_timeout = DEF_TIMEOUT;
static mode_t Umask = DEFUMASK;
static long Def_maxtrys = MAXTRYS,
Def_slptime = SLEEPTIME,
Def_distime = DISABLETIME,
Def_failures = LOGFAILURES,
Mac_Remote = MAC_SESSION,
Lockout = 0;
static int pflag = 0, /* bsd: don't destroy the environ */
#ifdef EXECSH
rflag = 0,
rflag_set = 0,
#endif /* EXECSH */
pwflag = 0, /* svr4: prompt for new password */
hflag = 0,
intrupt = 0,
Idleweeks = -1;
static void donothing(void);
static int set_uthost(char *, int);
static void getstr(char *, int, char *);
static int dialpass(char *shellp, uid_t priv_uid);
static int gpass(char *prmt, char *pswd, uid_t priv_uid);
static char ** chk_args(char **pp);
static char ** getargs(char *inline);
static char ** getargs2(char *inline);
static int quotec(void);
static char * quotec2(char *s, int *cp);
static int legalenvvar(char *s);
static void badlogin(int trys, char **log_entry);
static char *mygetpass(char *prompt);
static char * fgetpass(FILE *fi, FILE *fo, char *prompt);
static void catch(void);
static void uppercaseterm(char *strp);
static char * findttyname(int fd);
static void init_defaults(void);
static int exec_pass(char *usernam);
static int doremotelogin(char *host);
static int doremoteterm(char *term);
static int get_options(int argc, char **argv);
static void usage(void);
static int do_lastlog(struct utmpx *utmp);
static void setup_environ(char **envp, char **renvp, char *dirp, char **shellp);
static void pr_msgs(int lastlog_msg);
static void update_utmp(struct utmpx *utmp);
static void verify_pwd(int nopass, uid_t priv_uid);
static int init_badtry(char **log_entry);
static void logbadtry(int trys, char **log_entry);
static int on_console(uid_t priv_uid);
static int read_pass(uid_t priv_uid, int *nopass);
static long get_logoffval(void);
static int at_shell_level(void);
static void failure_log(void);
static int set_uthost(char *ut_hostp, int uthlen);
static unsigned char dositecheck(void);
static char * getsenv(char *varname, char **envp);
static void count_badlogins(int outcome, char *username);
static int update_count(int outcome, char *user);
static void early_advice(int rflag, int hflag);
static int cap_user_cleared(const char *, const cap_t);
static uinfo_t uinfo;
static uid_t ia_uid;
static gid_t ia_gid;
static char *ia_pwdpgm;
static struct lastlog ll;
static int syslog_fail;
static int syslog_success;
static char *args[MAXARGS]; /* pointer to arguments in envbuf[] */
static char envbuf[MAXLINE]; /* storage for argument text */
#ifdef EXECSH
static char QUOTAWARN[] = _PATH_QUOTA;
/* usererr is more like "remote_user_must_give_pwd", -1=yes, 0=no */
static int usererr = -1;
char **remenvp = (char **)0;
char luser[MAXLINE + 1];
char rusername[NMAX+1], lusername[NMAX+1];
char rpassword[NMAX+1];
char remotehost[257];
char terminal[64];
extern char **environ;
#endif /* EXECSH */
#ifdef AFS
extern int (*__afs_getsym(char *))();
extern int __afs_iskauth(void);
int (*afs_verify)(char *, char *, long *, int);
char *(*afs_gettktstring)(void);
long afs_exp = -1;
char afs_tktfile[1024] = { "KRBTKFILE=" };
char afs_passwdexp[22+12];
#endif
#ifdef DCE
static int dfs_authentication = 0;
char dce_tktfile[1024] = { "KRB5CCNAME=" };
#endif /* DCE */
/*
* look for LANG= as argument and switch to that locale
* last LANG= wins
*/
static char *
set_lang(char **envp)
{
register char **ulp, *langp = (char *)0;
if(ulp = envp) {
for(; *ulp; ulp++) {
if( !strncmp(*ulp, "LANG=", 5))
langp = *ulp + 5;
}
}
if(langp)
(void)setlocale(LC_ALL, langp);
return(langp);
}
/*
* Procedure: main
*
*/
int
main(int argc, char **argv, char **renvp)
{
register uid_t priv_uid;
register int
trys = 0, /* value for login attempts */
lastlogok,
writelog = 0,
firstime = 1,
uinfo_open = 0;
static struct utmpx utmp; /* static zeros the struct */
char **envp,
*ia_dirp,
*pdir = NULL,
*ia_shellp,
*pshell = NULL,
*ttyprompt = NULL,
*pwdmsgid = ":308",
*pwdmsg = "Password:",
inputline[MAXLINE],
*log_entry[MAX_FAILURES],
*loginmsg = "login: ",
*loginmsgid = ":307";
long ia_expire,
log_attempts = 0;
int log_trys = 0, /* value for writing to logfile */
nopassword = 1;
struct spwd noupass = { "", "no:password" };
cap_t u_cap = (cap_t) NULL, ocap;
mac_t u_mac = (mac_t) NULL;
cap_value_t capv;
#ifdef EXECSH
char *endptr;
int i;
#endif /* EXECSH */
if (cap_envl(0, CAP_AUDIT_CONTROL, CAP_AUDIT_WRITE, CAP_CHROOT,
CAP_DAC_WRITE, CAP_FOWNER, CAP_MAC_DOWNGRADE,
CAP_MAC_RELABEL_SUBJ, CAP_MAC_UPGRADE, CAP_MAC_WRITE,
CAP_PRIV_PORT, CAP_SETGID, CAP_SETUID, CAP_SETPCAP,
(cap_value_t) 0) == -1) {
fprintf(stderr, "insufficient privilege\n");
exit(1);
}
/*
* ignore the quit and interrupt signals early so
* no strange interrupts can occur.
*/
(void) signal(SIGQUIT, SIG_IGN);
(void) signal(SIGINT, SIG_IGN);
(void) setlocale(LC_ALL, "");
(void) setcat("uxcore");
(void) setlabel("UX:login");
tzset();
errno = 0;
u_name[0] = utmp.ut_user[0] = '\0';
terminal[0] = '\0';
/*
* Determine if this command was called by the user from
* shell level. If so, issue a diagnostic and exit.
*/
if (at_shell_level()) {
pfmt(stderr, MM_ERROR, ":666:cannot execute login at shell level.\n");
exit(1);
}
/* indicate an ID-based privilege mechanism, ID 0 == root */
priv_uid = 0;
init_defaults();
#ifdef AFS
afs_verify = (int (*)())__afs_getsym("afs_verify");
afs_gettktstring = (char *(*)()) __afs_getsym("afs_gettktstring");
if (afs_verify == NULL || afs_gettktstring == NULL)
afs_verify = NULL;
#endif
#ifndef sgi
/* don't set alarm here - some operations -like if yp is down
* can take a long time and shouldn't constitute the user
* not doing anything. Instead, we activate the alarm around
* the various places user's are asked for input
*/
/*
* Set the alarm to timeout in Def_timeout seconds if
* the user doesn't respond. Also, set process priority.
*/
(void) alarm(Def_timeout);
#endif
(void) nice(0);
if (get_options(argc, argv) == -1) {
usage();
exit(1);
}
/*
* if devicename is not passed as argument, call findttyname(0)
* and findttyname(0)
*/
if (ttyn == NULL || *ttyn == NULL) {
ttyn = findttyname(0);
if (ttyn == NULL)
ttyn = "/dev/???";
#ifdef sgi
/* what? think you'll get a different answer next time?? */
rttyn = ttyn;
#else
rttyn = findttyname(0);
if (rttyn == NULL)
rttyn = "/dev/???";
#endif
}
else
rttyn = ttyn;
writelog = init_badtry(log_entry);
#ifdef EXECSH
if (rflag) {
/*
* next stdin string is the remote username; initialize
* `rusername', `luser' (needed by set_uthost), and
* set up utmp.ut_host before calling doremotelogin()
*/
#ifdef sgi
(void) alarm(Def_timeout);
#endif
getstr(rusername, sizeof(rusername)-1, "remuser");
getstr(luser, sizeof(luser)-1, "locuser");
#ifdef sgi
(void) alarm(0);
#endif
set_uthost(&(utmp.ut_host[0]), sizeof(utmp.ut_host)-1);
usererr = doremotelogin(remotehost);
if (0 != doremoteterm(terminal)) {
syslog(LOG_ERR|LOG_AUTH, "ioctl(TCSETA) on %s failed - bad baud rate?",
ttyn);
pfmt(stderr, MM_ERROR, "uxue:57:ioctl() failed: %s\n","TCSETA");
exit(1);
}
}
else if (hflag) {
strcpy(rusername, "UNKNOWN");
set_uthost(&(utmp.ut_host[0]), sizeof(utmp.ut_host)-1);
}
early_advice(rflag, hflag);
#endif /* EXECSH */
/*
* determine the number of login attempts to allow.
* A value of 0 is infinite.
*/
log_attempts = get_logoffval();
/*
* get the prompt set by ttymon
*/
ttyprompt = getenv("TTYPROMPT");
if ((ttyprompt != NULL) && (*ttyprompt != '\0')) {
#ifdef sgi
(void) alarm(Def_timeout);
#endif
/*
* if ttyprompt is set, there should be data on
* the stream already.
*/
if ((envp = getargs(inputline)) != (char**)NULL) {
uppercaseterm(*envp);
/* call chk_args to process options */
envp = chk_args(envp);
if (*envp != (char *) NULL) {
SCPYN(utmp.ut_user, *envp);
SCPYN(u_name, *envp++);
}
}
#ifdef sgi
(void) alarm(0);
#endif
}
else if (optind < argc) {
SCPYN(utmp.ut_user, argv[optind]);
SCPYN(u_name, argv[optind]);
(void) strncpy(inputline, u_name, sizeof(inputline)-5);
(void) strcat(inputline, " \n");
envp = &argv[optind + 1];
}
/*
* enter an infinite loop. This loop will terminate on one of
* three conditions:
*
* 1) a successful login,
*
* 2) number of failed login attempts is greater than log_attempts,
*
* 3) an error occured and the loop exits.
*
*
*/
/* LINTED */
while (1) {
#ifdef sgi
/* in case we 'continue' from bad password.. */
(void) alarm(0);
#endif
ia_uid = ia_gid = -1;
/*
* free the storage for the master file
* information if it was previously allocated.
*/
if (uinfo_open) {
uinfo_open = 0;
ia_closeinfo(uinfo);
}
if ((pshell != NULL) && (*pshell != '\0')) {
free(pshell);
pshell = NULL;
}
if (pdir != NULL) {
free(pdir);
pdir = NULL;
}
/* If logging is turned on and there is an unsuccessful
* login attempt, put it in the string storage area
*/
if (writelog && (Def_failures > 0)) {
logbadtry(log_trys, log_entry);
if (log_trys == Def_failures) {
/*
* write "log_trys" number of records out
* to the log file and reset log_trys to 1.
*/
badlogin(log_trys, log_entry);
log_trys = 1;
}
else {
++log_trys;
}
}
/*
* On unsuccessfull login, update user's system-wide
* bad login count and lock out user if count = Lockout.
*/
if (Lockout > 0 && trys)
count_badlogins(0, u_name);
/*
* don't do this the first time through. Do it EVERY
* time after that, though.
*/
if (!firstime) {
u_name[0] = utmp.ut_user[0] = '\0';
}
(void) fflush(stdout);
/*
* one of the loop terminators. If either of these
* conditions exists, exit when "trys" is greater
* than log_attempts and Def_maxtrys isn't 0.
*/
if (log_attempts && Def_maxtrys) {
if (++trys > log_attempts) {
/*
* If logging is turned on, output the string
* storage area to the log file, and sleep for
* DISABLETIME seconds before exiting.
*/
if (log_trys) {
badlogin(log_trys, log_entry);
}
alarm(0);
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", "nobody", 0,
"Too many unsuccessful login attempts");
cap_surrender(ocap);
(void) sleep((unsigned)Def_distime);
exit(1);
}
}
/*
* keep prompting until the user enters something
*/
while (utmp.ut_user[0] == '\0') {
if (rflag && firstime) {
firstime = 0;
SCPYN(utmp.ut_user, lusername);
SCPYN(u_name, lusername);
envp = remenvp;
} else {
/*
* if TTYPROMPT is not set, print out our own
* prompt
* otherwise, print out ttyprompt
*/
if ((ttyprompt == NULL) || (*ttyprompt == '\0'))
pfmt(stdout, MM_NOSTD|MM_NOGET,
gettxt(loginmsgid, loginmsg));
else
(void) fputs(ttyprompt, stdout);
(void) fflush(stdout);
#ifdef sgi
(void) alarm(Def_timeout);
#endif
if ((envp = getargs(inputline)) != (char**)NULL) {
envp = chk_args(envp);
if (*envp != (char *) NULL) {
SCPYN(utmp.ut_user, *envp);
SCPYN(u_name, *envp++);
}
}
#ifdef sgi
(void) alarm(0);
#endif
}
}
(void)set_lang(envp);
firstime = 0;
/*
* If any of the common login messages was the input, we must be
* looking at a tty that is running login. We exit because
* they will chat at each other until one times out.
*/
if (EQN(loginmsg, inputline) || EQN(pwdmsg, inputline) ||
EQN(incorrectmsg, inputline)) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"Looking at a login line.");
cap_surrender(ocap);
pfmt(stderr, MM_ERROR, ":311:Looking at a login line.\n");
exit(8);
}
if (ia_openinfo(u_name, &uinfo) || (uinfo == NULL)) {
#ifdef sgi
(void) alarm(Def_timeout);
#endif
(void) gpass(gettxt(pwdmsgid, pwdmsg), noupass.sp_pwdp,
priv_uid);
(void) dialpass("/sbin/sh", priv_uid);
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "Invalid user name");
cap_surrender(ocap);
(void) sleep ((unsigned)Def_slptime);
pfmt(stderr, MM_ERROR|MM_NOGET, gettxt(incorrectmsgid,
incorrectmsg));
failure_log();
continue;
}
/*
* set ``uinfo_open'' to 1 to indicate that the information
* from the master file needs to be freed if we go back to
* the top of the loop.
*/
uinfo_open = 1;
/*
* get uid and gid info early for AUDIT
*/
ia_get_uid(uinfo, &ia_uid);
ia_get_gid(uinfo, &ia_gid);
ia_get_pwdpgm(uinfo, &ia_pwdpgm);
ia_get_dir(uinfo, &ia_dirp);
pdir = strdup(ia_dirp);
ia_get_sh(uinfo, &ia_shellp);
pshell = strdup(ia_shellp);
#ifdef AUX_SECURITY
/*
* Enable auxilary security, allowing system administrators to specify
* actions for login to take before asking for passwords. This is done
* via a switch statement and two labels. ``accept'' is where to go
* when the user is to be allowed in. ``scont'' is where to go if
* the sitecheck program fails for some reason and you still want to
* allow the user the chance to log in. If you want to reprompt,
* you should just break out of the loop, since it is immediately
* followed by a continue. This will increment the number of tries, etc.
*/
#ifdef EXECSH
if (usererr == -1)
#endif /* EXECSH */
if (Def_sitepath && *Def_sitepath) {
switch (dositecheck()) {
case SITE_OK:
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 1,
"external authentication succeeded");
cap_surrender(ocap);
#ifdef sgi
(void) alarm(Def_timeout);
#endif
goto accept;
case SITE_FAIL:
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"external authentication failed");
cap_surrender(ocap);
sleep((unsigned)Def_slptime);
exit(1);
case SITE_AGAIN:
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"external authentication retry");
cap_surrender(ocap);
sleep((unsigned)Def_slptime);
break;
case SITE_CONTINUE:
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"external authentication complete, also using IRIX authentication");
cap_surrender(ocap);
goto scont;
}
continue;
}
scont:
#endif /* AUX_SECURITY */
#ifdef sgi
(void) alarm(Def_timeout);
#endif
/*
* get the user's password.
*/
#ifdef EXECSH
if (usererr == -1)
#endif /* EXECSH */
if (read_pass(priv_uid, &nopassword)) {
(void) dialpass(pshell, priv_uid);
(void) sleep ((unsigned)Def_slptime);
pfmt(stderr, MM_ERROR|MM_NOGET, gettxt(incorrectmsgid,
incorrectmsg));
failure_log();
continue;
}
/*
* get dialup password, if necessary
*/
accept:
if (dialpass(pshell, priv_uid)) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "Invalid dialup password");
cap_surrender(ocap);
(void) sleep ((unsigned)Def_slptime);
pfmt(stderr, MM_ERROR|MM_NOGET, gettxt(incorrectmsgid,
incorrectmsg));
failure_log();
continue;
}
#ifdef sgi
(void) alarm(0);
/*
* Verify the user is within clearance. If MAC is not
* configured, the user is always within clearance.
*/
if (sysconf(_SC_MAC) > 0) {
struct clearance *clp;
char *mac_requested;
/*
* get MAC info from database
*/
clp = sgi_getclearancebyname (u_name);
if (clp == (struct clearance *) NULL) {
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"MAC clearance check failed");
cap_surrender(ocap);
continue;
}
/*
* determine what MAC label was requested
*/
mac_requested = getsenv ("MAC", envp);
if (mac_requested == (char *) NULL) {
if (clp->cl_default == (char *) NULL) {
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"MAC clearance check failed");
cap_surrender(ocap);
continue;
}
mac_requested = clp->cl_default;
}
/*
* convert user input into internal form.
* if remote login ignore user input and
* use the current process label unless
* MACREMOTE is CLEARANCE.
*/
if (Mac_Remote == MAC_SESSION && (rflag || hflag))
u_mac = mac_get_proc ();
else
u_mac = mac_from_text (mac_requested);
if (u_mac == (mac_t) NULL) {
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"MAC clearance check failed");
cap_surrender(ocap);
continue;
}
/*
* verify that the requested MAC label is permitted
*/
if (mac_clearedlbl (clp, u_mac) != MAC_CLEARED) {
mac_free(u_mac);
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"MAC clearance check failed");
cap_surrender(ocap);
continue;
}
}
/*
* Verify the user's capabilities (privileges)
*/
if (sysconf(_SC_CAP) > 0) {
char *cap_requested;
/*
* If capability was not explicitly requested this
* will be NULL.
*/
cap_requested = getsenv("CAP", envp);
/*
* Look for an implicit capability request
*/
if (cap_requested == NULL) {
struct user_cap *clp;
if (clp = sgi_getcapabilitybyname(u_name))
cap_requested = clp->ca_default;
else
cap_requested = "all=";
}
/*
* However you got it, convert it to binary form.
*/
u_cap = cap_from_text(cap_requested);
/*
* Verify that this user is allowed the requested
* capability set.
*/
if (!cap_user_cleared(u_name, u_cap)) {
cap_free(u_cap);
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"CAP clearance check failed");
cap_surrender(ocap);
continue;
}
}
#endif
/*
* Check for login expiration
*/
ia_get_logexpire(uinfo, &ia_expire);
if (ia_expire > 0) {
if (ia_expire < DAY_NOW) {
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"Account password has expired");
cap_surrender(ocap);
pfmt(stderr, MM_ERROR|MM_NOGET,
gettxt(incorrectmsgid, incorrectmsg));
exit(1);
}
}
/*
* if this is an ID-based privilege mechanism and the
* user is privileged but NOT on the system console, exit!
*/
if ((priv_uid >= 0) && !on_console(priv_uid)) {
/* for consistency with unicos and to address pv: 200420
* print out slightly misleading error message
*/
pfmt(stderr, MM_ERROR|MM_NOGET, gettxt(incorrectmsgid,
incorrectmsg));
exit(10);
}
/*
* Have to set the process label before the chdir.
*/
if (sysconf(_SC_MAC) > 0) {
capv = CAP_MAC_RELABEL_SUBJ;
ocap = cap_acquire (1, &capv);
if (mac_set_proc (u_mac) == -1) {
cap_surrender (ocap);
mac_free (u_mac);
pfmt(stderr, MM_ERROR,
":321:Bad user clearance.\n");
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"Bad user clearance");
cap_surrender(ocap);
exit(1);
}
cap_surrender (ocap);
mac_free (u_mac);
}
#ifdef EXECSH
if (*ia_shellp == '*') {
capv = CAP_CHROOT, ocap = cap_acquire (1, &capv);
if (chroot(pdir) < 0 ) {
cap_surrender (ocap);
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"No directory for subsystem root");
cap_surrender(ocap);
pfmt(stderr, MM_ERROR,
"uxsgicore:84:No root directory\n");
continue;
}
cap_surrender (ocap);
envinit[0] = SUBLOGIN;
envinit[1] = (char*)NULL;
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
satvwrite(SAT_AE_IDENTITY, SAT_SUCCESS,
"LOGIN|+|%s|Subsystem root: %s", u_name, pdir);
cap_surrender(ocap);
pfmt(stderr, MM_INFO,
":316:Logging in to subsystem root %s\n", pdir);
execle("/usr/lib/iaf/scheme",
"login", (char*)0, &envinit[0]);
execle("/usr/bin/login", "login",
(char*)0, &envinit[0]);
execle("/etc/login", "login", (char*)0, &envinit[0]);
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"Sublogin: No login programs on root");
cap_surrender(ocap);
pfmt(stderr, MM_ERROR, "uxsgicore:85:No /usr/lib/iaf/scheme or /usr/bin/login or /etc/login on root\n");
exit(1);
}
{
const cap_value_t caps[] = {CAP_SETUID,
CAP_SETGID,
CAP_AUDIT_WRITE};
uid_t euid = geteuid();
gid_t egid = getegid();
int okchdir;
/*
* We must set our effective uid to that of the
* new user, otherwise a chdir through a mode 700
* directory or onto a filesystem like DFS or NFS
* will fail.
*/
ocap = cap_acquire(2, caps);
if (setreuid(-1, ia_uid) == -1) {
cap_surrender(ocap);
perror("setreuid");
exit(1);
}
if (setregid(-1, ia_gid) == -1) {
cap_surrender(ocap);
perror("setregid");
exit(1);
}
cap_surrender(ocap);
okchdir = chdir(pdir);
/*
* These should never fail, but we check Just In Case
*/
if (setreuid(-1, euid) == -1) {
perror("setreuid");
exit(1);
}
if (setregid(-1, egid) == -1) {
perror("setregid");
exit(1);
}
if (okchdir == -1) {
ocap = cap_acquire (1, &caps[2]);
satvwrite(SAT_AE_IDENTITY, SAT_FAILURE, "LOGIN|-|%s|No home directory \"%s\"", u_name, pdir);
cap_surrender(ocap);
pfmt(stderr, MM_ERROR, ":735:unable to change directory to \"%s\"\n", pdir);
exit(1);
}
}
#endif /* EXECSH */
/*
* get the information for the last time this user logged
* in, and set up the information to be recorded for this
* session.
*/
lastlogok = do_lastlog(&utmp);
break; /* break out of while loop */
} /* end of infinite while loop */
/*
* On successfull login: reset user's system-wide
* bad login count to zero.
*/
if (Lockout > 0)
count_badlogins(1, u_name);
if (syslog_success) {
openlog("login", LOG_PID, LOG_AUTH);
if (rflag_set || hflag) {
syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s",
hflag ? "?" : rusername, remotehost, u_name);
} else
syslog(LOG_NOTICE|LOG_AUTH, "%s on %s", u_name, ttyn);
closelog();
}
/*
* update the utmp and wtmp file entries.
*/
update_utmp(&utmp);
/*
* check if the password has expired, the user wants to
* change password, etc.
*/
verify_pwd(nopassword, priv_uid);
/*
* print advisory messages such as the Copyright messages,
*
*/
pr_msgs(lastlogok);
/*
* release the information held by the different "ia_"
* routines since that information is no longer needed.
*/
ia_closeinfo(uinfo);
#ifdef EXECSH
if (quotactl(Q_SYNC, NULL, 0, NULL) == 0) {
pid_t pid;
char buf[10];
sprintf(buf, "%d", ia_uid);
if ((pid = fork()) == 0) {
execl(QUOTAWARN, QUOTAWARN, "-n", buf, (char *)0);
exit(1);
} else if (pid != -1) {
(void) waitpid(pid, (int *)NULL, 0);
}
}
capv = CAP_FOWNER, ocap = cap_acquire (1, &capv);
chmod(ttyn, S_IRUSR|S_IWUSR|S_IWGRP);
chown(ttyn, ia_uid, ia_gid);
cap_surrender (ocap);
/* Set the sat_id to the user's UID. */
capv = CAP_AUDIT_CONTROL, ocap = cap_acquire (1, &capv);
if (sysconf(_SC_AUDIT) > 0 && satsetid(ia_uid) < 0) {
cap_surrender(ocap);
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "warning: satsetid failed");
cap_surrender(ocap);
exit(1);
}
cap_surrender(ocap);
capv = CAP_SETGID, ocap = cap_acquire (1, &capv);
if( setgid(ia_gid) == -1 ) {
cap_surrender (ocap);
pfmt(stderr, MM_ERROR, ":319:Bad group id.\n");
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "Bad group id");
cap_surrender(ocap);
exit(1);
}
cap_surrender (ocap);
if (Initgroups && *Initgroups &&
strncasecmp(Initgroups, "YES", 3) == 0) {
/* Initialize the supplementary group access list. */
capv = CAP_SETGID, ocap = cap_acquire (1, &capv);
if (initgroups(u_name, ia_gid) == -1) {
cap_surrender (ocap);
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"Could not initialize groups");
cap_surrender(ocap);
pfmt(stdout, MM_ERROR,
":320:Could not initialize groups.\n");
exit(1);
}
if (initauxgroup(u_name, ia_gid, stdout) == -1) {
cap_surrender (ocap);
exit(1);
}
cap_surrender (ocap);
} else {
capv = CAP_SETGID, ocap = cap_acquire (1, &capv);
if (setgroups(1, &ia_gid) == -1) {
cap_surrender (ocap);
pfmt(stdout, MM_ERROR,
":320:Could not initialize groups.\n");
exit(1);
}
cap_surrender (ocap);
}
/*
* Start a new array session and set up this user's default
* project ID while we still have root privileges
*/
capv = CAP_SETUID, ocap = cap_acquire (1, &capv);
newarraysess();
setprid(getdfltprojuser(u_name));
cap_surrender (ocap);
/*
* Audit successful login (must be done as root, so we can't
* audit anything past the setuid).
*/
if (rflag_set || hflag) {
/* expanded ia_audit for variable args */
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
satvwrite(SAT_AE_IDENTITY, SAT_SUCCESS,
"LOGIN|+|%s|Remote login from %s@%s", u_name,
hflag ? "?" : rusername, remotehost);
cap_surrender(ocap);
} else {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
satvwrite(SAT_AE_IDENTITY, SAT_SUCCESS,
"LOGIN|+|%s|Successful login on %s", u_name, ttyn);
cap_surrender(ocap);
}
capv = CAP_SETUID, ocap = cap_acquire (1, &capv);
#ifdef _SHAREII
/*
* Perform Share II resource limit checks and attach to the
* user's lnode. Root is exempt from resource checks.
*/
if (sgidladd(SH_LIMITS_LIB, RTLD_LAZY))
{
static const char *Myname = "login";
SH_HOOK_SETMYNAME(Myname);
if (SH_HOOK_LOGIN(ia_uid, ttyn))
{
cap_surrender(ocap);
exit(1);
}
}
#endif /* _SHAREII */
if( setuid(ia_uid) == -1 ){
cap_surrender (ocap);
pfmt(stderr, MM_ERROR, ":321:Bad user id.\n");
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "Bad user id");
cap_surrender(ocap);
exit(1);
}
cap_surrender (ocap);
#ifdef DCE
/*
* This routine sets up the basic environment.
*/
setup_environ(envp, renvp, pdir, &pshell);
#endif /* DCE */
#ifdef AFS
/*
* AFS environment vars must be set after the setuid
* so we get the proper identity for the KRBTKFILE name.
* This code used to be executed in setup_environ.
*/
if (afs_verify && __afs_iskauth()){
for (i = 0; envinit[i] != NULL; ++i) {};
(void) strncat(afs_tktfile, (*afs_gettktstring)(), sizeof(afs_tktfile));
envinit[i] = afs_tktfile;
capv = CAP_FOWNER, ocap = cap_acquire (1, &capv);
chown((*afs_gettktstring)(), ia_uid, ia_gid);
cap_surrender (ocap);
}
if (afs_verify) {
envinit[++i] = afs_passwdexp;
sprintf(afs_passwdexp, "AFS_PASSWORD_EXPIRES=%u", afs_exp);
}
#endif /* AFS */
/*
* Set the user's capability set.
*/
if (sysconf(_SC_CAP) > 0) {
capv = CAP_SETPPRIV, ocap = cap_acquire (1, &capv);
if (cap_set_proc(u_cap) == -1)
{
cap_surrender(ocap);
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "Bad capability set");
cap_surrender(ocap);
exit(1);
}
cap_free(u_cap);
cap_free(ocap);
}
/*
* Re-enable the "BSD" signals SIGXCPU and SIGXFSZ if the
* user doesn't want SVR4-type signal semantics.
*/
if (SVR4_Signals && *SVR4_Signals &&
!strncasecmp(SVR4_Signals, "NO", 2)) {
(void) signal(SIGXCPU, SIG_DFL);
(void) signal(SIGXFSZ, SIG_DFL);
}
ENVSTRNCAT(minusnam, basename(pshell));
execl(pshell, minusnam, (char*)0);
/* pshell was not an executable object file, maybe it
* is a shell proceedure or a command line with arguments.
* If so, turn off the SHELL= environment variable.
*/
for (i = 0; envinit[i] != NULL; ++i) {
if ((envinit[i] == shell) &&
((endptr = strchr(shell, '=')) != NULL))
(*++endptr) = '\0';
}
if( access( pshell, R_OK|X_OK ) == 0 )
execl(SHELL, "sh", pshell, (char*)0);
pfmt(stderr, MM_ERROR, ":321:No shell\n");
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "No shell");
cap_surrender(ocap);
exit(1);
#else /* !EXECSH */
exit(0);
#endif /* EXECSH */
/* NOTREACHED */
}
/*
* Procedure: dialpass
*
*
* Notes: Opens either the DIAL_FILE or DPASS_FILE to determine
* if there is a dialup password on this system.
*/
static int
dialpass(char *shellp, uid_t priv_uid)
{
register FILE *fp;
char defpass[PASS_MAX+1];
char line[80];
register char *p1, *p2;
if ((fp = fopen(DIAL_FILE, "r")) == NULL) {
return 0;
}
while ((p1 = fgets(line, sizeof(line), fp)) != NULL) {
while (*p1 != '\n' && *p1 != ' ' && *p1 != '\t')
p1++;
*p1 = '\0';
if (strcmp(line, rttyn) == 0)
break;
}
(void) fclose(fp);
if (p1 == NULL || (fp = fopen(DPASS_FILE, "r")) == NULL) {
return 0;
}
defpass[0] = '\0';
p2 = 0;
while ((p1 = fgets(line, sizeof(line)-1, fp)) != NULL) {
while (*p1 && *p1 != ':')
p1++;
*p1++ = '\0';
p2 = p1;
while (*p1 && *p1 != ':')
p1++;
*p1 = '\0';
if (strcmp(shellp, line) == 0)
break;
/* Existing sites have /bin/sh as the default in d_passwd.
* To keep them secure, check both SHELL (/sbin/sh)
* and /bin/sh to use as the default. Last one found is
* used if both are present.
* BUG 184400
*/
if ((strcmp(SHELL, line) == 0) || (strcmp("/bin/sh", line)==0))
{
SCPYN(defpass, p2);
}
p2 = 0;
}
(void) fclose(fp);
if (!p2)
p2 = defpass;
if (*p2 != '\0')
return gpass(gettxt(":332", "Dialup Password:"), p2, priv_uid);
return 0;
}
/*
* Procedure: gpass
*
* Notes: getpass() fails if it cannot open /dev/tty.
* If this happens, and the real UID is privileged,
* (in an ID-based privilege mechanism) then use the
* current stdin and stderr.
*
* This allows login to work with network connections
* and other non-ttys.
*/
static int
gpass(char *prmt, char *pswd, uid_t priv_uid)
{
register char *p1;
cap_value_t capv;
cap_t ocap;
if (((p1 = mygetpass(prmt)) == (char *)0) && (getuid() == priv_uid)) {
p1 = fgetpass(stdin, stderr, prmt);
}
#ifdef AFS
if (p1 && afs_verify) {
if ((*afs_verify)(u_name, p1, &afs_exp, 0) == 0)
return 0;
}
#endif /* AFS */
#ifdef DCE
if (p1 && dfs_authentication) {
char *dce_err=NULL;
if (dce_verify(u_name, ia_uid, ia_gid, p1, &dce_err)) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
if (dce_err) {
satvwrite(SAT_AE_IDENTITY, SAT_FAILURE,
"LOGIN|-|%s|DCE authentication failed: %s",
u_name, dce_err);
free(dce_err);
} else {
satvwrite(SAT_AE_IDENTITY, SAT_FAILURE,
"LOGIN|-|%s|DCE authentication failed",
u_name);
}
cap_surrender(ocap);
return 1;
}
return 0;
}
#endif /* DCE */
if (!p1 || strcmp(crypt(p1, pswd), pswd)) {
return 1;
}
return 0;
}
/*
* Procedure: chk_args
*
*/
static char **
chk_args(char **pp)
{
char *p,
*invalidopt = ":669:Invalid options -h, -v\n",
*badservice = ":593:System service not installed\n";
pwflag = 0;
while (*pp) {
p = *pp;
if (*p++ != '-') {
return pp;
}
else {
pp++;
switch(*p++) {
case 'v':
case 'h':
pfmt(stderr, MM_ERROR, invalidopt);
pfmt(stderr, MM_ERROR, badservice);
exit(1);
break;
case 'p':
/*
* XXX This is the SVR4 login -p option which
* collides with the IRIX/BSD -p option. See
* comments in get_options().
*/
pwflag++;
break;
}
}
}
return pp;
}
/*
* Procedure: getargs
*
* Notes: scans the data enetered at the prompt and stores the
* information in the argument passed. Exits if EOF is
* enetered.
*/
static char **
getargs(char *inline)
{
int c, llen = MAXLINE - 1;
char *ptr = envbuf, **reply = args;
enum {
WHITESPACE, ARGUMENT
} state = WHITESPACE;
while ((c = getc(stdin)) != '\n') {
/*
* check ``llen'' to avoid overflow on ``inline''.
*/
if (llen > 0) {
--llen;
/*
* Save a literal copy of the input in ``inline''.
* which is checked in main() to determine if
* this login process "talking" to another login
* process.
*/
*(inline++) = (char) c;
}
switch (c) {
case EOF:
/*
* if the user enters an EOF character, exit
* immediately with the value of one (1) so it
* doesn't appear as if this login was successful.
*/
exit(1);
/* FALLTHROUGH */
case ' ':
case '\t':
if (state == ARGUMENT) {
*ptr++ = '\0';
state = WHITESPACE;
}
break;
case '\\':
c = quotec();
/* FALLTHROUGH */
default:
if (state == WHITESPACE) {
*reply++ = ptr;
state = ARGUMENT;
}
*ptr++ = (char) c;
}
/*
* check if either the ``envbuf'' array or the ``args''
* array is overflowing.
*/
if (ptr >= envbuf + MAXLINE - 1
|| reply >= args + MAXARGS - 1 && state == WHITESPACE) {
(void) putc('\n', stdout);
break;
}
}
*ptr = '\0';
*inline = '\0';
*reply = NULL;
return ((reply == args) ? NULL : args);
}
/*
* like getargs() but from string
*/
static char **
getargs2(char *inline)
{
int c, llen = MAXLINE - 1;
char *ptr = envbuf, **reply = args;
enum {
WHITESPACE, ARGUMENT
} state = WHITESPACE;
while(c = *inline++) {
if(llen > 0)
--llen;
switch(c) {
case ' ':
case '\t':
if(state == ARGUMENT) {
*ptr++ = '\0';
state = WHITESPACE;
}
break;
case '\\':
inline = quotec2(inline, &c);
default:
if(state == WHITESPACE) {
*reply++ = ptr;
state = ARGUMENT;
}
*ptr++ = (char) c;
}
/*
* check if either the ``envbuf'' array or the ``args''
* array is overflowing.
*/
if(ptr >= envbuf + MAXLINE - 1
|| reply >= args + MAXARGS - 1 && state == WHITESPACE) {
break;
}
}
*ptr = '\0';
*reply = NULL;
return ((reply == args) ? NULL : args);
}
/*
* Procedure: quotec
*
* Notes: Reads from the "standard input" of the tty. It is
* called by the routine "getargs".
*/
static int
quotec(void)
{
register int c, i, num;
switch (c = getc(stdin)) {
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 'v':
c = '\013';
break;
case 'b':
c = '\b';
break;
case 't':
c = '\t';
break;
case 'f':
c = '\f';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
for (num=0, i=0; i<3; i++) {
num = num * 8 + (c - '0');
if ((c = getc(stdin)) < '0' || c > '7')
break;
}
(void) ungetc(c, stdin);
c = num & 0377;
break;
default:
break;
}
return c;
}
/*
* like quotec() but from string
*/
static char *
quotec2(char *s, int *cp)
{
register int c, i, num;
switch (c = *s++) {
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 'v':
c = '\013';
break;
case 'b':
c = '\b';
break;
case 't':
c = '\t';
break;
case 'f':
c = '\f';
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
for (num=0, i=0; i<3; i++) {
num = num * 8 + (c - '0');
c = *s++;
if (c < '0' || c > '7')
break;
}
s--;
c = num & 0377;
break;
default:
break;
}
*cp = c;
return(s);
}
static char *illegal[] = {
"SHELL=",
"HOME=",
"LOGNAME=",
#ifndef NO_MAIL
"MAIL=",
#endif
"CDPATH=",
"IFS=",
"PATH=",
"USER=",
0
};
static char *illegal_log[] = { /* we syslog if in this set */
"_RLD", /* no =; any of the _RLD variables; include future */
"LD_LIBRARY", /* no =; any of the 3 ISAs variables; include future */
0
};
/*
* Procedure: legalenvvar
*
* Notes: Determines if it is legal to insert this
* environmental variable.
*/
static int
legalenvvar(char *s)
{
register char **p;
for (p = illegal; *p; p++)
if (!strncmp(s, *p, strlen(*p)))
return 0;
for (p = illegal_log; *p; p++)
if (!strncmp(s, *p, strlen(*p))) {
/* use sprintf to avoid possibility of overrunning
* syslog buffer. */
char msg[256];
sprintf(msg, "ignored attempt to setenv(%.128s)", s);
openlog("login", LOG_PID, LOG_AUTH);
syslog(LOG_AUTH, msg);
closelog();
return 0;
}
return 1;
}
/*
* Procedure: badlogin
*
*
* Notes: log to the log file after "trys" unsuccessful attempts
*/
static void
badlogin(int trys, char **log_entry)
{
int retval, count, fildes;
failure_log();
/* Tries to open the log file. If succeed, lock it and write
in the failed attempts */
if ((fildes = open (LOGINLOG, O_APPEND|O_WRONLY)) == -1)
return;
else {
(void) sigset(SIGALRM, donothing);
(void) alarm(L_WAITTIME);
retval = lockf(fildes, F_LOCK, 0L);
(void) alarm(0);
(void) sigset(SIGALRM, SIG_DFL);
if (retval == 0) {
for (count = 0 ; count < trys ; count++) {
(void) write(fildes, log_entry[count],
(unsigned) strlen (log_entry[count]));
*log_entry[count] = '\0';
}
(void) lockf(fildes, F_ULOCK, 0L);
(void) close(fildes);
}
return;
}
}
/*
* Procedure: donothing
*
* Notes: called by "badlogin" routine when SIGALRM is
* caught. The intent is to do nothing when the
* alarm is caught.
*/
static void
donothing(void) {}
/*
* Procedure: getpass
*
* Restrictions:
* fopen: none
* setbuf: none
* fclose: none
*
* Notes: calls "fgetpass" to read the user's password entry.
*/
static char *
mygetpass(char *prompt)
{
char *p;
FILE *fi;
if ((fi = fopen("/dev/tty", "r")) == NULL) {
return (char*)NULL;
}
setbuf(fi, (char*)NULL);
p = fgetpass(fi, stderr, prompt);
if (fi != stdin)
(void) fclose(fi);
return p;
}
/*
* Procedure: fgetpass
* Restrictions:
*
* ioctl(2): None
*
* Notes: issues the "Password: " prompt and reads the input
* after turning off character echoing.
*/
static char *
fgetpass(FILE *fi, FILE *fo, char *prompt)
{
struct termio ttyb;
tcflag_t flags;
register char *p;
register int c;
static char pbuf[PBUFSIZE + 1];
void (*sig)();
sig = signal(SIGINT, catch);
intrupt = 0;
(void) ioctl(fileno(fi), TCGETA, &ttyb);
flags = ttyb.c_lflag;
ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
(void) ioctl(fileno(fi), TCSETAF, &ttyb);
(void) fputs(prompt, fo);
for (p = pbuf; !intrupt && (c = getc(fi)) != '\n' && c != EOF;) {
if (p < &pbuf[PBUFSIZE])
*p++ = (char) c;
}
*p = '\0';
(void) putc('\n', fo);
ttyb.c_lflag = flags;
(void) ioctl(fileno(fi), TCSETAW, &ttyb);
(void) signal(SIGINT, sig);
if (intrupt)
(void) kill(getpid(), SIGINT);
return pbuf;
}
/*
* Procedure: catch
*
* Notes: called by fgetpass if the process catches an
* INTERRUPT signal.
*/
static void
catch(void)
{
++intrupt;
}
/*
* Procedure: uppercaseterm
*
* Restrictions:
* ioctl(2): None
*
* Notes: if all input characters are upper case set the
* corresponding termio so ALL input and output is
* UPPER case.
*/
static void
uppercaseterm(char *strp)
{
int upper = 0;
int lower = 0;
char *sp;
struct termio termio;
for (sp = strp; *sp; sp++) {
if (islower(*sp))
lower++;
else if (isupper(*sp))
upper++;
}
if (upper > 0 && lower == 0) {
(void) ioctl(0,TCGETA,&termio);
termio.c_iflag |= IUCLC;
termio.c_oflag |= OLCUC;
termio.c_lflag |= XCASE;
(void) ioctl(0,TCSETAW,&termio);
for (sp = strp; *sp; sp++)
if (*sp >= 'A' && *sp <= 'Z' ) *sp += ('a' - 'A');
}
}
/*
* Procedure: findttyname
*
*
* Notes: call ttyname(), but do not return syscon, systty,
* or sysconreal do not use syscon or systty if console
* is present, assuming they are links.
*/
static char *
findttyname(int fd)
{
char *lttyn;
lttyn = ttyname(fd);
if (lttyn == NULL) return NULL;
if (((strcmp(lttyn, "/dev/syscon") == 0) ||
(strcmp(lttyn, "/dev/sysconreal") == 0) ||
(strcmp(lttyn, "/dev/systty") == 0)) &&
(access("/dev/console", F_OK) == 0))
lttyn = "/dev/console";
return lttyn;
}
/*
* Procedure: init_defaults
*
* Restrictions:
* defopen: None
* lvlin: None
* lvlvalid: None
*
* Notes: reads the "login" default file in "/etc/defaults"
* directory. Also initializes other variables used
* throughout the code.
*/
static void
init_defaults(void)
{
FILE *defltfp;
register char *ptr,
*Pndefault = "login";
if ((defltfp = defopen(Pndefault)) != NULL) {
if ((Console = defread(defltfp, "CONSOLE")) != NULL)
if (*Console)
Console = strdup(Console);
else
Console = NULL;
if ((Altshell = defread(defltfp, "ALTSHELL")) != NULL)
if (*Altshell)
Altshell = strdup(Altshell);
else
Altshell = NULL;
if ((Passreq = defread(defltfp, "PASSREQ")) != NULL)
if (*Passreq)
Passreq = strdup(Passreq);
else
Passreq = NULL;
if ((Mandpass = defread(defltfp, "MANDPASS")) != NULL)
if (*Mandpass)
Mandpass = strdup(Mandpass);
else
Mandpass = NULL;
if ((Initgroups = defread(defltfp, "INITGROUPS")) != NULL)
if (*Initgroups)
Initgroups = strdup(Initgroups);
else
Initgroups = NULL;
if ((SVR4_Signals = defread(defltfp, "SVR4_SIGNALS")) != NULL)
if (*SVR4_Signals)
SVR4_Signals = strdup(SVR4_Signals);
else
SVR4_Signals = NULL;
if ((Def_hertz = defread(defltfp, "HZ")) != NULL)
if (*Def_hertz)
Def_hertz = strdup(Def_hertz);
else
Def_hertz = NULL;
if ((Def_path = defread(defltfp, "PATH")) != NULL)
if (*Def_path)
Def_path = strdup(Def_path);
else
Def_path = NULL;
#ifdef AUX_SECURITY
if ((Def_sitepath = defread(defltfp, "SITECHECK")) != NULL)
if (*Def_sitepath)
Def_sitepath = strdup(Def_sitepath);
else
Def_sitepath = NULL;
#endif /* AUX_SECURITY */
/*
* have to setlocale() here, to get msgs in LANG
*/
if((Def_lang = getenv("LANG")) == NULL) {
if((Def_lang = defread(defltfp, "LANG")) != NULL)
Def_lang = *Def_lang? strdup(Def_lang) : NULL;
}
if(Def_lang)
(void)setlocale(LC_ALL, Def_lang);
if ((Def_supath = defread(defltfp, "SUPATH")) != NULL)
if (*Def_supath)
Def_supath = strdup(Def_supath);
else
Def_supath = NULL;
if ((Def_syslog = defread(defltfp, "SYSLOG")) != NULL)
if (Def_syslog && *Def_syslog) {
if (strcmp (Def_syslog, "FAIL") == 0)
syslog_fail = 1;
else if (strcmp (Def_syslog, "ALL") == 0)
syslog_success = syslog_fail = 1;
}
if ((Def_notlockout = defread(defltfp, "LOCKOUTEXEMPT")) != NULL)
if (*Def_notlockout)
Def_notlockout = strdup(Def_notlockout);
else
Def_notlockout = NULL;
if ((ptr = defread(defltfp, "TIMEOUT")) != NULL)
Def_timeout = (unsigned) atoi(ptr);
if ((ptr = defread(defltfp, "SLEEPTIME")) != NULL)
Def_slptime = atol(ptr);
if ((ptr = defread(defltfp, "DISABLETIME")) != NULL)
Def_distime = atol(ptr);
if ((ptr = defread(defltfp, "MAXTRYS")) != NULL)
Def_maxtrys = atol(ptr);
if ((ptr = defread(defltfp, "LOGFAILURES")) != NULL)
Def_failures = atol(ptr);
if ((ptr = defread(defltfp, "LOCKOUT")) != NULL)
Lockout = atol(ptr);
if ((ptr = defread(defltfp, "UMASK")) != NULL)
if (sscanf(ptr, "%lo", &Umask) != 1)
Umask = DEFUMASK;
if ((ptr = defread(defltfp, "IDLEWEEKS")) != NULL)
Idleweeks = atoi(ptr);
if ((ptr = defread(defltfp, "MACREMOTE")) != NULL)
if (strcmp(ptr, "CLEARANCE") == 0)
Mac_Remote = MAC_CLEARANCE;
(void) defclose(defltfp);
}
if (((mode_t) 0777) < Umask)
Umask = DEFUMASK;
(void) umask(Umask);
if (!Def_tz || (Def_tz && !*Def_tz))
Def_tz = getenv("TZ");
if (!Def_tz)
(void) strcat(timez, DEF_TZ);
else
ENVSTRNCAT(timez, Def_tz);
(void) putenv(timez);
if (Def_timeout > MAX_TIMEOUT)
Def_timeout = MAX_TIMEOUT;
if (Def_slptime > DEF_TIMEOUT)
Def_slptime = DEF_TIMEOUT;
if (Def_failures < 0 )
Def_failures = LOGFAILURES;
if (Def_failures > MAX_FAILURES)
Def_failures = MAX_FAILURES;
if (Def_maxtrys < 0 )
Def_maxtrys = MAXTRYS;
return;
}
/*
* Procedure: exec_pass
*
* Notes: This routine forks, changes the uid of the forked process
* to the user logging in, and execs the "/usr/bin/passwd"
* command. It returns the status of the "exec" to the
* parent process. All "working" privileges of the forked
* (child) process are cleared. Also, P_SYSOPS is cleared
* from the maximum set to indicate to "passwd" that this
* "exec" originated from the login scheme.
*/
static int
exec_pass(char *usernam)
{
int status, w;
pid_t pid;
cap_t ocap;
cap_value_t capv;
if ((pid = fork()) == 0) {
if (ia_uid > 0) {
cap_value_t cv[] = {CAP_SETUID, CAP_SETGID};
ocap = cap_acquire (ia_gid > 0 ? 2 : 1, cv);
if (ia_gid > 0 && setgid(ia_gid) == -1) {
cap_surrender (ocap);
exit(127);
}
if (setuid(ia_uid) == -1) {
cap_surrender (ocap);
exit(127);
}
cap_surrender (ocap);
}
(void) execl(ia_pwdpgm, ia_pwdpgm, usernam, (char *)NULL);
exit(127);
}
while ((w = (int) wait(&status)) != pid && w != -1)
;
if (w != -1 && status > 0) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "password program returned error");
cap_surrender(ocap);
}
if (w < 0 || status < 0) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "error execing password program");
cap_surrender(ocap);
}
return (w == -1) ? w : status;
}
#ifdef EXECSH
extern int _getpwent_no_shadow;
static int
doremotelogin(char *host)
{
struct passwd *pw;
#ifdef sgi
(void) alarm(Def_timeout);
#endif
/* caller already read remote and local usernames */
getstr(terminal, sizeof(terminal)-1, "Terminal type");
#ifdef sgi
(void) alarm(0);
#endif
/*
* handle args to login name
*/
if( !(remenvp = getargs2(luser)))
lusername[0] = 0;
else
(void)strncpy(lusername, *remenvp++, sizeof(lusername)-1);
(void)set_lang(remenvp);
SCPYN(u_name, lusername);
if (getuid())
return(-1);
_getpwent_no_shadow = 1;
pw = getpwnam(lusername);
_getpwent_no_shadow = 0;
if (pw == NULL)
return -1;
return(__ruserok_x(host, (pw->pw_uid == 0), rusername, lusername,
pw->pw_uid, pw->pw_dir));
}
/* ARGSUSED */
static void
getstr(char *buf, int cnt, char *err)
{
char c;
do {
if (read(0, &c, 1) != 1)
exit(1);
if (--cnt < 0) {
buf[-1] = '\0';
return;
}
*buf++ = c;
} while (c != 0);
}
static int
doremoteterm(char *term)
{
struct termio tp;
register char *cp = strchr(term, '/');
char *speed;
ioctl(0, TCGETA, &tp);
if (cp) {
*cp++ = '\0';
speed = cp;
cp = strchr(speed, '/');
if (cp)
*cp++ = '\0';
tp.c_ospeed = atoi(speed);
}
tp.c_lflag |= ISIG|ICANON|ECHO|ECHOE|ECHOK;
tp.c_oflag |= OPOST|ONLCR;
tp.c_iflag |= BRKINT|IGNPAR|ISTRIP|ICRNL|IXON;
tp.c_cc[VEOL] = CEOL;
tp.c_cc[VEOF] = CEOF;
return ioctl(0, TCSETA, &tp);
}
#endif /* EXECSH */
/*
* Procedure: get_options
*
* Notes: get_options parses the command line. It returns 0
* if successful, -1 if failed.
*/
extern int _check_rhosts_file; /* used by ruserok */
static int
get_options(int argc, char **argv)
{
int c;
int errflg = 0;
while ((c = getopt(argc, argv, "d:r:R:h:t:u:l:s:M:U:S:p")) != -1) {
switch (c) {
#ifdef EXECSH
/*
* XXX The (IRIX/BSD login) -p option tells login not to
* destroy the environment. But SVR4 login has a -p option
* for calling "/usr/bin/passwd". Currently the SVR4 -p
* option is not supported on the commmand line. If and
* when support is added/required, the current -p should
* be changed to something like "-P" and client applications
* (telnetd, 4DDN sethostd) must be changed.
*/
case 'p':
pflag++;
break;
case 't':
strncpy(terminal, optarg, sizeof(terminal)-1);
break;
case 'r':
case 'R':
_check_rhosts_file = c == 'r' ? 1 : 0;
if (hflag || rflag) {
pfmt(stderr, MM_ERROR,
":310:Only one of -r and -h allowed\n");
exit(1);
}
rflag_set = ++rflag;
strncpy (remotehost, optarg, sizeof(remotehost)-1);
break;
case 'h':
if (hflag || rflag) {
pfmt(stderr, MM_ERROR,
":310:Only one of -r and -h allowed\n");
exit(1);
}
hflag++;
strncpy (remotehost, optarg, sizeof(remotehost)-1);
break;
#else /* !EXECSH */
/*
* no need to continue login since the -r option
* is not allowed.
*/
case 'r':
return -1;
/*
* the ability to specify the -d option at the "login: "
* prompt with an argument is still supported however it
* has no effect.
*/
#endif /* EXECSH */
case 'd':
/* ignore the following options for IAF reqts */
case 'u':
case 'l':
case 's':
case 'M':
case 'U':
case 'S':
break;
default:
errflg++;
break;
} /* end switch */
} /* end while */
if (errflg)
return -1;
return 0;
}
/*
* Procedure: usage
*
* Notes: prints the usage message.
*/
static void
usage(void)
{
pfmt(stderr, MM_ACTION,
":670:Usage: login [[ -p ] name [ env-var ... ]]\n");
}
/*
* Procedure: do_lastlog
*
* Notes: gets the information for the last time the user logged
* on and also sets up the information for this login
* session so it can be reported at a subsequent login.
* The original code does this in a file indexed by uid.
* This works badly on efs since hole-y files are not supported,
* so the cypress scheme is implemented by default.
*/
static int
do_lastlog(struct utmpx *utmp)
{
int fd1,
lastlogok = 0,
exists;
long ia_inact;
struct stat f_buf;
struct lastlog newll;
char *fname;
cap_t ocap;
cap_value_t cap_mac_grade[] = {CAP_MAC_DOWNGRADE, CAP_MAC_UPGRADE};
cap_value_t capv;
exists = stat(LASTLOG, &f_buf) == 0;
if (exists && !S_ISDIR(f_buf.st_mode)) {
unlink(LASTLOG);
exists = 0;
}
if (!exists) {
(void) mkdir(LASTLOG, 0);
(void) chmod(LASTLOG, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
(void) stat(LASTLOG, &f_buf);
}
if (!S_ISDIR(f_buf.st_mode))
return 0;
fname = (char *)malloc(strlen(LASTLOG) + strlen(utmp->ut_user) + 1);
if (!fname)
return 0;
sprintf(fname, "%s/%s", LASTLOG, utmp->ut_user);
if (stat(fname, &f_buf) < 0) {
capv = CAP_MAC_WRITE, ocap = cap_acquire (1, &capv);
(void) close(creat(fname, (mode_t) 0));
cap_surrender (ocap);
(void) chmod(fname, (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH));
if (sysconf (_SC_MAC) > 0) {
mac_t dblow_label = mac_from_text ("dblow");
if (dblow_label == NULL) {
free(fname);
return 0;
}
ocap = cap_acquire(2, cap_mac_grade);
if (mac_set_file (fname, dblow_label) == -1) {
cap_surrender(ocap);
mac_free(dblow_label);
free(fname);
return 0;
}
cap_surrender(ocap);
mac_free(dblow_label);
}
}
capv = CAP_MAC_WRITE, ocap = cap_acquire (1, &capv);
if ((fd1 = open(fname, O_RDWR)) < 0) {
cap_surrender (ocap);
free(fname);
return 0;
}
cap_surrender (ocap);
free(fname);
(void) fstat(fd1, &f_buf);
if (!S_ISREG(f_buf.st_mode))
return 0;
switch (f_buf.st_size) {
case 0:
break;
case sizeof(ll):
if (read(fd1, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
ll.ll_time != 0)
lastlogok = 1;
break;
case sizeof(struct lastlog4): {
struct lastlog4 ll4;
if (read(fd1, (char *)&ll4, sizeof(ll4)) == sizeof(ll4) &&
ll4.ll_time != 0 && ll4.ll_status) {
lastlogok = 1;
ll.ll_time = ll4.ll_time;
/* line & host smaller than current */
strncpy(ll.ll_line, ll4.ll_line, sizeof(ll4.ll_line)-1);
strncpy(ll.ll_host, ll4.ll_host, sizeof(ll4.ll_host)-1);
ll.ll_level = 0;
}
ftruncate(fd1, 0L);
break;
}
case sizeof(struct lastlog5a): {
struct lastlog5a ll5a;
if (read(fd1, (char *)&ll5a, sizeof(ll5a)) == sizeof(ll5a) &&
ll5a.ll_time != 0) {
lastlogok = 1;
ll.ll_time = ll5a.ll_time;
/* line & host smaller than current */
strncpy(ll.ll_line, ll5a.ll_line, sizeof(ll5a.ll_line)-1);
strncpy(ll.ll_host, ll5a.ll_host, sizeof(ll5a.ll_host)-1);
ll.ll_level = ll5a.ll_level;
}
ftruncate(fd1, 0L);
break;
}
default:
ftruncate(fd1, 0L);
break;
}
(void) lseek(fd1, 0, 0);
(void) time(&newll.ll_time);
if (utmp->ut_host[0])
SCPYN(newll.ll_line, rusername);
else
SCPYN(newll.ll_line, (rttyn + sizeof("/dev/")-1));
SCPYN(newll.ll_host, remotehost);
/* Check for login inactivity */
ia_get_loginact(uinfo, &ia_inact);
if ((ia_inact > 0) && ll.ll_time)
if((( ll.ll_time / DAY ) + ia_inact) < DAY_NOW ) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"Account inactive too long");
cap_surrender(ocap);
pfmt(stderr, MM_ERROR|MM_NOGET,
gettxt(incorrectmsgid, incorrectmsg));
(void) close(fd1);
exit(1);
}
(void) write(fd1, (char * )&newll, sizeof(newll));
(void) close(fd1);
return lastlogok;
}
/*
* Procedure: setup_environ
*
* Restrictions:
* access(2): None
*
* Notes: Set up the basic environment for the exec. This
* includes HOME, PATH, LOGNAME, SHELL, TERM, HZ, TZ,
* and MAIL.
*/
static void
setup_environ(char **envp, char **renvp, char *dirp, char **shellp)
{
static int basicenv;
static char envblk[MAXENV];
register int i, j, k,
l_index, length;
char *ptr, *endptr;
/*
* login will only set the environment variable "TERM" if it
* already exists in the environment. This allows features
* such as doconfig with the port monitor to work correctly
* if an administrator specifies a particular terminal for a
* particular port.
*
*/
#ifdef EXECSH
if (terminal[0] != '\0') { /* from -r or -t option */
(void) strcat(term, "TERM=");
ENVSTRNCAT(term, terminal)
} else
#endif /* EXECSH */
{
if (!Def_term || !*Def_term) {
if ((Def_term = getenv("TERM")) != NULL) {
(void) strcpy(term, "TERM=");
ENVSTRNCAT(term, Def_term);
}
} else {
(void) strcpy(term, "TERM=");
ENVSTRNCAT(term, Def_term);
}
}
if (!Def_hertz || !*Def_hertz) {
if ((Def_hertz = getenv("HZ")) != NULL) {
ENVSTRNCAT(hertz, Def_hertz);
} else
(void) strcat(hertz, DEF_HZ);
} else {
ENVSTRNCAT(hertz, Def_hertz);
}
{
int fd;
char *nlp, uhlang[MAXPATHLEN + 7];
if( !Def_lang || !*Def_lang)
(void)strcat(lang, DEF_LANG);
else {
ENVSTRNCAT(lang, Def_lang);
}
/*
* .lang overwrites all LANG settings
* but not those from username args
*/
(void)strncpy(uhlang, dirp, MAXPATHLEN);
(void)strcat(uhlang, LANG_FILE);
if((fd = open(uhlang, O_RDONLY)) >= 0) {
int nrd;
if((nrd=read(fd, uhlang, NL_LANGMAX + 2)) > 0) {
if(nlp = strchr(uhlang, '\n'))
*nlp = 0;
else uhlang[nrd]= 0;
uhlang[NL_LANGMAX] = 0;
(void)strcpy(lang, "LANG=");
ENVSTRNCAT(lang, uhlang);
}
(void)close(fd);
}
}
{
int fd;
char *nlp, uhtz[MAXPATHLEN + 11];
/*
* .timezone overwrites all LANG settings
* but not those from username args
*/
(void)strncpy(uhtz, dirp, MAXPATHLEN);
(void)strcat(uhtz, TZ_FILE);
if((fd = open(uhtz, O_RDONLY)) >= 0) {
int nrd;
/* -4 to leave space for NUL and "TZ=" */
if((nrd=read(fd, uhtz, sizeof(timez) - 4)) > 0) {
if(nlp = strchr(uhtz, '\n'))
*nlp = 0;
else uhtz[nrd]= 0;
uhtz[sizeof(timez)-4] = 0;
(void)strcpy(timez, "TZ=");
ENVSTRNCAT(timez, uhtz);
putenv(timez);
}
(void)close(fd);
}
}
if (ia_uid == 0) {
if (!(Def_path = Def_supath) || !*Def_path)
Def_path = DEF_SUPATH;
} else {
if (!Def_path || !*Def_path)
Def_path = DEF_PATH;
}
ENVSTRNCAT(path, Def_path);
ENVSTRNCAT(home, dirp);
ENVSTRNCAT(user, u_name);
ENVSTRNCAT(logname, u_name);
/* Find the end of the basic environment */
for (basicenv = 0; envinit[basicenv] != NULL; basicenv++);
if (*shellp[0] == '\0') {
/*
* If possible, use the primary default shell,
* otherwise, use the secondary one.
*/
if (access(SHELL, X_OK) == 0)
*shellp = SHELL;
else
*shellp = SHELL2;
} else
if (Altshell && *Altshell &&
strncasecmp(Altshell, "YES", 3) == 0)
envinit[basicenv++] = shell;
ENVSTRNCAT(shell, *shellp);
if (remotehost[0] != '\0') { /* install remote host name */
envinit[basicenv++] = env_remotehost;
ENVSTRNCAT(env_remotehost,remotehost);
}
else if (0 != (ptr=getenv("REMOTEHOST")))
envinit[basicenv++]=strncat(env_remotehost,ptr,MAXHOSTNAMELEN);
if (rusername[0] != '\0') { /* and remote user name */
envinit[basicenv++] = env_remotename;
ENVSTRNCAT(env_remotename,rusername);
}
else if (0 != (ptr=getenv("REMOTEUSER")))
envinit[basicenv++] = strncat(env_remotename,ptr,NMAX);
else envinit[basicenv++] = strcat(env_remotename, "UNKNOWN");
#ifndef NO_MAIL
envinit[basicenv++] = mail;
(void) strcat(mail,_PATH_MAILDIR);
ENVSTRNCAT(mail,u_name);
#endif
#ifdef DCE
if (dfs_authentication) {
envinit[basicenv++] = dce_tktfile;
ENVSTRNCAT(dce_tktfile, getenv("KRB5CCNAME"));
}
#endif /* DCE */
#ifdef EXECSH
/*
* Add/replace environment variables from telnetd.
*/
if (pflag && renvp != NULL) {
for (j=0; *renvp && j < MAXARGS-1; j++,renvp++) {
/*
* Ignore if it doesn't have the format xxx=yyy
* or it is not an alterable variable.
*/
if ((endptr = strchr(*renvp,'=')) == NULL ||
!legalenvvar(*renvp))
continue;
/*
* Replace any previously-defined string or
* append it to the list.
*/
length = endptr + 1 - *renvp;
for (i = 0; i < basicenv; i++) {
if (!strncmp(*renvp, envinit[i], length)) {
envinit[i] = *renvp;
break;
}
}
if (i == basicenv)
envinit[basicenv++] = *renvp;
}
}
#endif /* EXECSH */
/*
* Add in all the environment variables picked up from the
* argument list to "login" or from the user response to the
* "login" request.
*/
for (j = 0,k = 0,l_index = 0,ptr = &envblk[0]; *envp && j < (MAXARGS-1);
j++, envp++) {
/* Scan each string provided. If it doesn't have the format */
/* xxx=yyy, then add the string "Ln=" to the beginning. */
if ((endptr = strchr(*envp,'=')) == (char*)NULL) {
envinit[basicenv+k] = ptr;
(void) snprintf(ptr, (int)MAXENV, "L%d=%s",l_index,*envp);
/* Advance "ptr" to the beginning of the next argument. */
while(*ptr++);
k++;
l_index++;
}
/* Is this an environmental variable we permit? */
else if (!legalenvvar(*envp))
continue;
/* Check to see whether this string replaces any previously- */
/* defined string. */
else {
for (i = 0, length = endptr+1-*envp; i < basicenv+k; i++ ) {
if (strncmp(*envp, envinit[i], length) == 0) {
envinit[i] = *envp;
break;
}
}
/* If it doesn't, place it at the end of environment array. */
if (i == basicenv+k) {
envinit[basicenv+k] = *envp;
k++;
}
}
}
#ifdef EXECSH
environ = envinit;
#endif /* EXECSH */
(void)set_lang(envinit);
}
/*
* Procedure: pr_msgs
*
*
* Notes: prints any advisory messages such as the Copyright
*/
static void
pr_msgs(int lastlog_msg)
{
struct utsname un;
#ifndef sgi
(void) alarm(0);
#endif
(void) signal(SIGQUIT, SIG_DFL);
(void) signal(SIGINT, SIG_DFL);
(void) nuname(&un);
pfmt(stdout, MM_NOSTD,
"uxsgicore:704:IRIX Release %s %s %s\n\
Copyright 1987-1999 Silicon Graphics, Inc. All Rights Reserved.\n",
un.release, un.machine, un.nodename);
/*
* Advise the user the time and date that this login-id
* was last used.
*/
if (lastlog_msg && (access(".hushlogin", F_OK) != 0)) {
char timebuf[256];
int size;
struct tm *ltime;
ltime = localtime(&ll.ll_time);
size = strftime(timebuf, sizeof(timebuf), "%KC", ltime);
if (ll.ll_host[0] && ll.ll_line[0])
pfmt(stdout, MM_NOSTD,
":748:Last login: %.*s by %.*s@%.*s\n",
size, timebuf, sizeof(ll.ll_line), ll.ll_line,
sizeof(ll.ll_host), ll.ll_host);
else if (ll.ll_host[0])
pfmt(stdout, MM_NOSTD,
":329:Last login: %.*s from %.*s\n",
size, timebuf, sizeof(ll.ll_host), ll.ll_host);
else
pfmt(stdout, MM_NOSTD,
":330:Last login: %.*s on %.*s\n",
size, timebuf, sizeof(ll.ll_line), ll.ll_line);
}
}
/*
* Procedure: update_utmp
*
* Restrictions:
* pututxline: None
* getutxent: P_MACREAD
* updwtmpx: P_MACREAD
*
* Notes: updates the utmpx and wtmpx files.
*/
static void
update_utmp(struct utmpx *utmp)
{
register struct utmpx *u;
cap_t ocap;
cap_value_t caps[] = {CAP_DAC_WRITE, CAP_MAC_WRITE};
(void) time(&utmp->ut_tv.tv_sec);
#ifdef EXECSH
utmp->ut_pid = getpid();
#else
utmp->ut_pid = getppid();
#endif /* EXECSH */
/*
* Find the entry for this pid in the utmp file.
*/
ocap = cap_acquire (2, caps);
while ((u = getutxent()) != NULL) {
if (((u->ut_type == INIT_PROCESS ||
u->ut_type == LOGIN_PROCESS) &&
(u->ut_pid == utmp->ut_pid)) ||
((u->ut_type == USER_PROCESS) &&
((u->ut_pid == utmp->ut_pid) ||
!strncmp(u->ut_line,basename(ttyn),
sizeof(u->ut_line))))) {
/* Copy in the name of the tty minus the "/dev/", the id, and set */
/* the type of entry to USER_PROCESS. */
SCPYN(utmp->ut_line,(ttyn + sizeof("/dev/")-1));
utmp->ut_id[0] = u->ut_id[0];
utmp->ut_id[1] = u->ut_id[1];
utmp->ut_id[2] = u->ut_id[2];
utmp->ut_id[3] = u->ut_id[3];
utmp->ut_type = USER_PROCESS;
/* Write the new updated utmp file entry. */
pututxline(utmp);
break;
}
}
endutxent(); /* Close utmp file */
cap_surrender (ocap);
if (u == (struct utmpx *)NULL) {
#ifndef EXECSH
cap_value_t capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"cannot execute login at shell level");
cap_surrender(ocap);
pfmt(stderr, MM_ERROR, ":666:cannot execute login at shell level.\n");
exit(1);
#endif /* EXECSH */
}
else {
/*
* Now attempt to write out this entry to the wtmp file
* if we were successful in getting it from the utmp file
* and the wtmp file exists.
*/
ocap = cap_acquire (2, caps);
updwtmpx(WTMPX_FILE, utmp);
cap_surrender (ocap);
}
}
/*
* Procedure: verify_pwd
*
* Restrictions:
* exec_pass(): none
*
* Notes: execute "/usr/bin/passwd" if passwords are required
* for the system, the user does not have a password,
* AND the user's NULL password can be changed accord-
* ing to its password aging information.
*
* It also calls the program "/usr/bin/passwd" if the
* "-p" flag is present on the input line indicating the
* user wishes to modify their password.
*/
static void
verify_pwd(int nopass, uid_t priv_uid)
{
time_t now;
long ia_lstchg, ia_min,
ia_max, ia_warn;
register int n,
paschg = 0;
char *badpasswd = ":148:Cannot execute %s: %s\n";
cap_t ocap;
cap_value_t capv;
#ifndef sgi
(void) alarm(0); /*give user time to come up with new password */
#endif
now = DAY_NOW;
/* get the aging info */
ia_get_logmin(uinfo, &ia_min);
ia_get_logmax(uinfo, &ia_max);
ia_get_logchg(uinfo, &ia_lstchg);
ia_get_logwarn(uinfo, &ia_warn);
if((rflag && (usererr == -1)) || !rflag){
if (nopass && (ia_uid != priv_uid)) {
if (Passreq && *Passreq &&
!strncasecmp("YES", Passreq, 3) &&
((ia_max == -1) || (ia_lstchg > now) ||
((now >= ia_lstchg + ia_min) &&
(ia_max >= ia_min)))) {
pfmt(stderr, MM_ERROR,
":322:You don't have a password.\n");
pfmt(stderr, MM_ACTION, ":323:Choose one.\n");
(void) fflush(stderr);
n = exec_pass(u_name);
if (n > 0) {
exit(9);
}
if (n < 0) {
pfmt(stderr, MM_ERROR, badpasswd,
ia_pwdpgm, strerror(errno));
exit(9);
}
paschg = 1;
ia_lstchg = now;
}
}
}
/* Is the age of the password to be checked? */
if ((ia_lstchg == 0) ||
(ia_lstchg > now) ||
((ia_max >= 0) && (now > (ia_lstchg + ia_max)) && (ia_max >= ia_min))) {
/* don't make the idleweeks tests if last change == 0, which happens when
* the admin expires the passwd via passwd -f (bug fix)
*/
if ((ia_lstchg != 0) &&
((Idleweeks == 0) ||
((Idleweeks > 0) && (now > (ia_lstchg + (7 * Idleweeks)))))) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"password has been expired for too long");
cap_surrender(ocap);
pfmt(stderr, MM_ERROR,
":324:Your password has been expired for too long\n");
pfmt(stderr, MM_ACTION,
":133:Consult your system administrator\n");
exit(1);
}
else {
pfmt(stderr, MM_ERROR, ":325:Your password has expired.\n");
pfmt(stderr, MM_ACTION, ":326:Choose a new one\n");
n = exec_pass(u_name);
if (n > 0) {
exit(9);
}
if (n < 0) {
pfmt(stderr, MM_ERROR, badpasswd, ia_pwdpgm,
strerror(errno));
exit(9);
}
}
paschg = 1;
ia_lstchg = now;
}
if (pwflag) {
if (!paschg) {
n = exec_pass(u_name);
if (n > 0)
pfmt(stderr, MM_WARNING, ":677:Password unchanged\n");
if (n < 0)
pfmt(stderr, MM_WARNING, badpasswd, ia_pwdpgm,
strerror(errno));
}
ia_lstchg = now;
}
/* Warn user that password will expire in n days */
if ((ia_warn > 0) && (ia_max > 0) &&
(now + ia_warn) >= (ia_lstchg + ia_max)) {
int xdays = (ia_lstchg + ia_max) - now;
if (xdays) {
if (xdays == 1)
(void) pfmt(stderr, MM_INFO,
":678:Your password will expire in 1 day\n");
else
(void) pfmt(stderr, MM_INFO,
":327:Your password will expire in %d days\n", xdays);
}
else {
pfmt(stderr, MM_ERROR, ":325:Your password has expired.\n");
pfmt(stderr, MM_ACTION, ":326:Choose a new one\n");
n = exec_pass(u_name);
if (n > 0) {
exit(9);
}
if (n < 0) {
pfmt(stderr, MM_ERROR, badpasswd, ia_pwdpgm,
strerror(errno));
exit(9);
}
paschg = 1;
}
}
}
/*
* Procedure: init_badtry
*
* Restrictions:
* stat(2): none
*
* Notes: if the logfile exist, turn on attempt logging and
* initialize the string storage area.
*/
static int
init_badtry(char **log_entry)
{
register int i, dolog = 0;
struct stat dbuf;
if (stat(LOGINLOG, &dbuf) == 0) {
dolog = 1;
for (i = 0; i < Def_failures; i++) {
if (!(log_entry[i] = (char *) malloc((unsigned)ENT_SIZE))) {
dolog = 0 ;
break ;
}
*log_entry[i] = '\0';
}
}
return dolog;
}
/*
* Procedure: logbadtry
*
* Notes: Writes the failed login attempt to the storage area.
*/
static void
logbadtry(int trys, char **log_entry)
{
long timenow;
if (trys && (trys <= Def_failures)) {
(void) time(&timenow);
(void) strncat(log_entry[trys-1], u_name, LNAME_SIZE);
(void) strncat(log_entry[trys-1], ":", (size_t) 1);
(void) strncat(log_entry[trys-1], rttyn, TTYN_SIZE);
(void) strncat(log_entry[trys-1], ":", (size_t) 1);
(void) strncat(log_entry[trys-1], ctime(&timenow), TIME_SIZE);
}
}
/*
* Procedure: on_console
*
* Notes: If the "priv_uid" is equal to the user's uid, the login
* will be disallowed if the user is NOT on the system
* console.
*/
static int
on_console(uid_t priv_uid)
{
cap_t ocap;
cap_value_t capv;
if (ia_uid == priv_uid) {
if (Console && *Console && (strcmp(rttyn, Console) != 0)) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"Root login on other than system console");
cap_surrender(ocap);
return 0;
}
if (Def_supath && *Def_supath)
Def_path = Def_supath;
else
Def_path = DEF_SUPATH;
}
return 1;
}
/*
* Procedure: read_pass
*
* Notes: gets user password and checks if MANDPASS is required.
* returns 1 on failure, 0 on success.
*/
static int
read_pass(uid_t priv_uid, int *nopass)
{
char *ia_pwdp,
*pwdmsgid = ":308",
*pwdmsg = "Password:";
int mandatory = 0;
cap_t ocap;
cap_value_t capv;
ia_get_logpwd(uinfo, &ia_pwdp);
/*
* if the user doesn't have a password check if the privilege
* mechanism is ID-based. If so, its OK for a privileged user
* not to have a password.
*
* If, however, the MANDPASS flag is set and this user doesn't
* have a password, set a flag and continue on to get the
* user's password. Otherwise, return success because its OK
* not to have a password.
*/
if (*ia_pwdp == '\0') {
if (ia_uid == priv_uid)
return 0;
if (Mandpass && *Mandpass &&
!strncasecmp("YES", Mandpass, 3)) {
mandatory = 1;
}
else {
return 0;
}
}
#ifdef DCE
dfs_authentication = 0;
if (strcmp(ia_pwdp,"-DCE-") == 0) {
void *handle=NULL;
/*
* Try to load the DCE library and ensure that
* dce_verify exists.
*/
if (handle=sgidladd("libdce.so",RTLD_LAZY)) {
if (dlsym(handle,"dce_verify")) {
dfs_authentication = 1;
} else {
capv = CAP_AUDIT_WRITE;
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"DCE integrated login failed, incorrect DCE library");
cap_surrender(ocap);
}
} else {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"DCE integrated login failed, unable to load DCE library");
cap_surrender(ocap);
}
}
#endif /* DCE */
/*
* get the user's password, turning off echoing.
*/
if (gpass(gettxt(pwdmsgid, pwdmsg), ia_pwdp, priv_uid)) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "Invalid password");
cap_surrender(ocap);
return 1;
}
/*
* Doesn't matter if the user entered No password. Since MANDPASS
* was set, make it look like a bad login attempt.
*/
if (mandatory) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0,
"Passwords are mandatory, but this account has none");
cap_surrender(ocap);
return 1;
}
/*
* everything went fine, so indicate that the user had a password
* and return success.
*/
else {
*nopass = 0;
return 0;
}
}
/*
* Procedure: get_logoffval
*
* Notes: The following is taken directly from the SVR4.1 require-
* ments relating to the functionality of the MAXTRYS and
* LOGFAILURES tuneables:
*
* 1. Users will be allowed LOGFAILURES (will be set to 5)
* attempts to successfully log in at each invocation of
* login.
*
* 2. If the file LOGINLOG (will be defined to be
* /var/adm/loginlog) exists, all LOGFAILURES consecutive
* unsuccessful login attempts will be logged in
* LOGINLOG. After LOGFAILURES unsuccessful attempts,
* login will sleep for DISABLETIME before dropping the
* line. In other words, if a person tried five times,
* unsuccessfully, to log in at a terminal, all five
* attempts will be logged in /var/adm/loginlog if it
* exists. The login command will then sleep for
* DISABLETIME seconds and drop the line. On the other
* hand, if a person has one or two unsuccessful
* attempts, none of them will be logged.
*
* => Note: Since LOGFAILURES can now be set by the
* administrator, it may be set to 1 so that any number
* of failed login attempts are recorded. When either
* MAXTRYS or LOGFAILURES is reached login will exit
* and the user will be disconnected from the system.
* The difference being that in the case of
* LOGFAILURES, a record of bad login attempts are
* recorded in the system logs.
*
* 3. by default, MAXTRYS and LOGFAILURES will be set to 5,
*
* 4. if set, MAXTRYS must be >= 0. If MAXTRYS=0 and
* LOGFAILURES is not set, then login will not kick the
* user off the system (unlimited attempts will be
* allowed).
*
* 5. if set, LOGFAILURES must be within the range of 0-20.
* If LOGFAILURES is = 0, and MAXTRYS is not set, then
* login will not kick off the user (unlimited attempts
* will be allowed).
*
* 6. if LOGFAILURES or MAXTRYS are not set, then
* respectively, the effect will be as if the item were
* set to 0,
*
* 7. the lowest positive number of MAXTRYS and LOGFAILURES
* will be the number of failed login attempts allowed
* before the appropriate action is taken (e.g. 1,
* MAXTRYS = 3 and LOGFAILURES=6 then the user will be
* kicked off the system after 3 bad login attempts and
* IN NO CASE shall bad login records end up in the
* system log file (var/adm/loginlog). e.g. 2, MAXTRYS=6
* and LOGFAILURES=3, then the user will be kicked off
* the system after 3 bad login attempts and at that
* point in time, 3 records will be recorded in the
* system log file.)
*
* 8. in the case when both values are equal, then the
* action of LOGFAILURES will dominate (i.e., a record of
* the bad login attempts will be recorded in the log
* files).
*/
static long
get_logoffval(void)
{
if (Def_maxtrys == Def_failures) /* #8 */
return Def_failures;
if (!Def_maxtrys && (Def_failures < 2)) /* #4, #5, and #6 */
return 0;
if (Def_maxtrys < Def_failures) { /* #7, example 1 */
Def_failures = 0;
return Def_maxtrys;
}
/*
* Def_maxtrys MUST be greater than Def_failures so
* return Def_failures!
*/
return Def_failures; /* #7, example 2 */
}
/*
* Procedure: at_shell_level
*
* Restrictions:
* getutxent: P_MACREAD
*
* Notes: determines if the login scheme was called by the user at
* the shell level. If so, this is forbidden.
*/
static int
at_shell_level(void)
{
#ifndef EXECSH
register struct utmpx *u;
pid_t tmppid;
tmppid = getppid();
/*
* Find the entry for this pid in the utmp file.
*/
while ((u = getutxent()) != NULL) {
if (((u->ut_type == INIT_PROCESS ||
u->ut_type == LOGIN_PROCESS) &&
(u->ut_pid == tmppid)) ||
((u->ut_type == USER_PROCESS) &&
(u->ut_pid == tmppid) &&
(!strncmp(u->ut_user, ".", 1)))) {
break;
}
}
endutxent(); /* Close utmp file */
if (u == (struct utmpx *)NULL)
return 1;
#endif /* !EXECSH */
return 0;
}
/*
* Put string in SYSLOG on failure
*/
static void
failure_log(void)
{
if (syslog_fail) {
openlog("login", LOG_PID, LOG_AUTH);
if (rflag_set || hflag) {
syslog(LOG_INFO|LOG_AUTH, "failed: %s@%s as %s",
hflag ? "?" : rusername, remotehost, u_name);
} else
syslog(LOG_NOTICE|LOG_AUTH, "failed: %s on %s",
u_name, ttyn);
closelog();
}
}
/*
* display pre-prompt messages
*/
static void
early_advice(int rflag, int hflag)
{
FILE *fp;
char inputline[MAXLINE];
cap_t ocap;
cap_value_t capv;
if (rflag || hflag) {
/* If rlogins are disabled, print out the message. */
if ((fp = fopen(_PATH_NOLOGIN,"r")) != NULL) {
while (fgets(inputline,sizeof(inputline),fp) != NULL) {
fputs(inputline,stdout);
putc('\r',stdout);
}
fflush(stdout);
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "Remote logins disabled");
cap_surrender(ocap);
sleep(5);
exit(1);
}
/* Print out the issue file. */
if ((fp = fopen(_PATH_ISSUE,"r")) != NULL) {
while (fgets(inputline,sizeof(inputline),fp) != NULL) {
fputs(inputline,stdout);
putc('\r',stdout);
}
/*
* Insert extra newline otherwise gpass prints
* passwdmsg and puts cursor at beginning of the line.
*/
putc('\n',stdout);
fclose(fp);
}
}
}
/*
* set_uthost() is called only for remote logins. It constructs the
* ut_host string for the current utmpx entry.
* - If the remote and local usernames are different, it copies the
* remote username followed by an '@', then the remotehostname into
* ut_hostp. The entire remote hostname is saved; the remote user
* is truncated as necessary.
* - if the local and remote usernames match, only the remotehostname
* is copied.
*
* Note that `rusername', `luser', and `remotehost' must be initialized
* before set_uthost() is invoked.
*/
static int
set_uthost(
char *ut_hostp, /* constructed ut_host string is copied here */
int uthlen) /* max strlen(ut_hostp) allowed */
{
int rhostlen, ccnt;
int rumaxlen; /* max # of chars to grab from ruser */
ut_hostp[0] = '\0';
if (remotehost[0] == '\0') { /* ???? */
return(0);
}
rhostlen = strlen(remotehost);
rumaxlen = 0;
if (rhostlen < (uthlen-1) && /* only room for '@' if 1 shorter; */
rusername[0]!='\0' && luser[0]!='\0' && strcmp(rusername,luser)) {
rumaxlen = (uthlen - rhostlen - 1);
}
ccnt = sprintf(ut_hostp, "%.*s%s%.*s", rumaxlen, rusername,
(rumaxlen > 0 ? "@" : ""), uthlen, remotehost);
return(ccnt);
}
#ifdef AUX_SECURITY
static unsigned char
dositecheck(void)
{
auto int wstatus;
struct stat sbuf;
char *newargv[5];
pid_t sitepid, rv;
int i;
cap_t ocap;
cap_value_t capv;
i = 0;
newargv[i++] = Def_sitepath;
newargv[i++] = u_name;
if (remotehost[0] != '\0') {
newargv[i++] = (char *) remotehost;
}
if (rusername[0] != '\0') {
newargv[i++] = (char *) rusername;
}
newargv[i] = NULL;
/*
* Enforce security items. Check that the program is owned by root,
* and is not world-writable. Return SITE_CONTINUE if it is not,
* thus causing a traditional unix authentication.
*/
if (stat(Def_sitepath,&sbuf) != 0) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "cannot access sitecheck");
cap_surrender(ocap);
return SITE_CONTINUE;
}
if (sbuf.st_uid != 0) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "bad sitecheck ownership");
cap_surrender(ocap);
return SITE_CONTINUE;
}
if (sbuf.st_mode & 0022) {
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "bad sitecheck permissions");
cap_surrender(ocap);
return SITE_CONTINUE;
}
/* fork, exec, and wait for return code of siteprog */
switch (sitepid = fork()) {
case -1:
return(SITE_FAIL);
case 0:
signal(SIGALRM, SIG_DFL);
signal(SIGHUP, SIG_DFL);
execv(Def_sitepath, newargv);
capv = CAP_AUDIT_WRITE, ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", u_name, 0, "can't exec sitecheck program");
cap_surrender(ocap);
return(SITE_CONTINUE);
}
rv = waitpid(sitepid, &wstatus, 0);
if (rv < 0)
return SITE_FAIL;
else if (WIFEXITED(wstatus))
return WEXITSTATUS(wstatus);
else {
if (WCOREDUMP(wstatus))
return SITE_CONTINUE;
else
return SITE_FAIL;
}
}
#endif /* AUX_SECURITY */
/*
* Return the value of the requested variable in the environment string.
*/
static char *
getsenv(char *varname, char **envp)
{
int s = strlen(varname);
char *ep;
for (ep = *envp; ep = *envp; envp++) {
if (strncmp(varname, ep, s))
continue;
if (ep[s] != '=')
continue;
return (ep + s + 1);
}
return (NULL);
}
#define BL_WRITE 1
#define BL_LOCK 2
#define BL_BADFILE 3
#define BL_BADLOCK 4
#define BL_LOCK_BADFILE 5
/*
* Call update_count and log result to syslog and system audit trail.
*/
static void
count_badlogins(int outcome, char *username)
{
cap_t ocap;
cap_value_t capv = CAP_AUDIT_WRITE;
if (Lockout <= 0)
return;
switch(update_count(outcome, username)) {
case BL_LOCK:
syslog(LOG_NOTICE|LOG_AUTH, "Locked %s account\n", username);
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", username, 1, "Locked account");
cap_surrender(ocap);
break;
case BL_BADFILE:
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", username, 0, "Can't update badlogin file");
cap_surrender(ocap);
syslog(LOG_ALERT|LOG_AUTH, "Can't update %s badlogin count",
username);
break;
case BL_BADLOCK:
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", username, 0, "Can't lock account");
cap_surrender(ocap);
syslog(LOG_ALERT|LOG_AUTH, "Can't lock %s account", username);
break;
case BL_LOCK_BADFILE:
ocap = cap_acquire (1, &capv);
ia_audit("LOGIN", username, 0,
"Locked account but can't update badlogin file");
cap_surrender(ocap);
syslog(LOG_ALERT|LOG_AUTH,
"Can't update %s badlogin count", username);
break;
default:
break;
}
}
#define MATCH 1
#define NOMATCH 0
#define SKIP_DELIM(a) {while (*a && (*a == NAME_DELIM)) a++;}
static int
notlockout(char *user)
{
char notlockname[LNAME_SIZE+1];
char * name_begin_ptr;
char * name_delim_ptr;
int namelen=0;
if (Def_notlockout == NULL)
return (NOMATCH);
notlockname[0]='\0';
name_begin_ptr = Def_notlockout;
SKIP_DELIM(name_begin_ptr);
/* Iterate through the list of names until we reach \0 */
while (*name_begin_ptr != NULL) {
name_delim_ptr = strchr(name_begin_ptr,NAME_DELIM);
/* strchr didn't find any more delimiters, but we still
* have to deal with the last name in the list
*/
if (name_delim_ptr != NULL)
namelen = name_delim_ptr - name_begin_ptr;
else
namelen = strlen(name_begin_ptr);
/* name must be a valid length <= LNAME_SIZE,
* we don't want to overflow on the stack, and we
* don't want to just chop off the name at LNAME_SIZE
* since that would invalidate the identity.
*/
if ((namelen > 0) && (namelen <=LNAME_SIZE)){
strncpy(notlockname, name_begin_ptr, namelen);
notlockname[namelen]='\0';
if (strcmp(user, notlockname) == 0)
return (MATCH);
}
/* Increment the name_begin_ptr, and handle
* termination of the loop is there isn't
* a match in the list.
*/
if (name_delim_ptr != NULL) {
name_begin_ptr=name_delim_ptr;
SKIP_DELIM(name_begin_ptr);
}
else
name_begin_ptr = name_begin_ptr + namelen;
}
return (NOMATCH);
}
/*
* On successful login (outcome=1), reset count to zero.
* Otherwise, increment count, test whether count has reached
* Lockout value, and lock user's account if it has.
*/
static int
update_count(int outcome, char *user)
{
char *badlogfile;
int fd, retval, wstatus;
short new = 0;
short lockok = 0;
char count;
struct passwd *pw;
struct stat bl_buf;
/* Validate that this user exists, we don't use uinfo (iaf)
* structure here, because we're not guaranteed that it exists
* when this routine is called. BUG #491422
*/
pw = getpwnam(user);
/* Don't process LOCKOUT if the user doesn't exist.
* BUG #491422
*/
if (pw != NULL) {
/*
* Open user's badlog file.
*/
if (access(BADLOGDIR, 0) < 0) {
if (mkdir(BADLOGDIR, 0700) < 0)
return(BL_BADFILE);
}
if ((badlogfile = (char *)malloc(sizeof(BADLOGDIR) +
strlen(user) + 3)) == NULL) {
return(BL_BADFILE);
}
sprintf(badlogfile, "%s/%s", BADLOGDIR, user);
/*
* If file is new, count is zero.
* Use stat instead of access to avoid the real uid/gid
* problems when login not started as root. BUG #506487
*/
if (stat(badlogfile, &bl_buf) < 0) {
new = 1;
count = '\0';
}
fd =open(badlogfile, O_RDWR | O_CREAT, 0600);
free(badlogfile);
if (fd < 0 || flock(fd, LOCK_EX) < 0) {
close(fd);
return(BL_BADFILE);
}
if (outcome == 0) {
/*
* Failed login: read count and seek back to start.
*/
if (!new && read(fd, &count, 1) != 1) {
close(fd);
return(BL_BADFILE);
}
if (lseek(fd, 0, SEEK_SET) < 0) {
close(fd);
return(BL_BADFILE);
}
/*
* If updated count has reached Lockout value,
* invoke passwd -l and reset count to zero - UNLESS
* (UNLESS!) the user is on the LOCKOUTEXEMPT list in
* the options file. Such a user will have his count
* updated when a failed login attempt is made, but
* will not be locked out when the Lockout count is
* exceeded. This feature is the result of Bug 491422,
* to address the denial of service attacks possible
* when the LOCKOUT option is in use.
*/
if ((++count >= Lockout) &&
(notlockout(user) == NULL)) {
retval = fork();
switch (retval) {
case -1:
close(fd);
return (BL_BADLOCK);
case 0:
close(fd);
signal(SIGALRM, SIG_DFL);
signal(SIGHUP, SIG_DFL);
execl("/bin/passwd", "passwd",
"-l", user, (char *)0);
exit(1);
}
if (waitpid(retval, &wstatus, 0) < 0 ||
wstatus != 0) {
close(fd);
return (BL_BADLOCK);
}
count = '\0';
lockok = 1;
}
}
else {
/*
* On successful login, reset count to zero.
*/
count = '\0';
}
/*
* Write back updated count.
*/
if (write(fd, &count, 1) != 1) {
close(fd);
if (lockok)
return(BL_LOCK_BADFILE);
return(BL_BADFILE);
}
else {
close(fd);
if (lockok)
return(BL_LOCK);
return(BL_WRITE);
}
}
return(BL_LOCK);
}
/*
* Determine if `user' is cleared for capability state `cap'.
* This function is dependent upon the internal representation
* of cap_t.
*/
static int
cap_user_cleared (const char *user, const cap_t cap)
{
struct user_cap *clp;
cap_t pcap;
int result;
if ((clp = sgi_getcapabilitybyname(user)) == NULL)
pcap = cap_init();
else
pcap = cap_from_text(clp->ca_allowed);
result = CAP_ID_ISSET(cap->cap_effective, pcap->cap_effective) &&
CAP_ID_ISSET(cap->cap_permitted, pcap->cap_permitted) &&
CAP_ID_ISSET(cap->cap_inheritable, pcap->cap_inheritable);
(void) cap_free(pcap);
return result;
}
|