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
|
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/* THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T */
/* The copyright notice above does not evidence any */
/* actual or intended publication of such source code. */
#pragma ident "@(#)login.c 1.73 97/11/25 SMI" /* SVr4.0 1.43.6.26 */
/*
PROPRIETARY NOTICE(Combined)
This source code is unpublished proprietary information
constituting, or derived under license from AT&T's UNIX(r) System V.
In addition, portions of such source code were derived from Berkeley
4.3 BSD under license from the Regents of the University of
California.
Copyright Notice
Notice of copyright on this source code product does not indicate
publication.
(c) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1997 Sun Microsystems, Inc
(c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T.
All rights reserved.
*******************************************************************
*/
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
/* This Module contains Proprietary Information of Microsoft */
/* Corporation and should be treated as Confidential. */
/*
* Usage: login [ -d device ] [ name ] [ environment args ]
*
*
*/
/*
*
* *** Header Files ***
*
*
*/
#include <sys/types.h>
#include <sys/param.h>
#include <unistd.h> /* For logfile locking */
#include <signal.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <deflt.h>
#include <grp.h>
#include <fcntl.h>
#include <lastlog.h>
#include <termio.h>
#include <utmpx.h>
#include <dirent.h>
#include <stdlib.h>
#include <wait.h>
#include <errno.h>
#include <ctype.h>
#include <syslog.h>
#include <ulimit.h>
#include <libgen.h>
#include <pwd.h>
#include <security/pam_appl.h>
#include "libcmd.h" /* for defcntl */
/*
*
* *** Defines, Macros, and String Constants ***
*
*
*/
#define ISSUEFILE "/etc/issue" /* file to print before prompt */
#define NOLOGIN "/etc/nologin" /* file to lock users out during shutdown */
/*
* These need to be defined for UTMP management.
* If we add in the utility functions later, we
* can remove them.
*/
#define __UPDATE_ENTRY 1
#define __LOGIN 2
/*
* Intervals to sleep after failed login
*/
#ifndef SLEEPTIME
#define SLEEPTIME 4 /* sleeptime before login incorrect msg */
#endif
static int Sleeptime = SLEEPTIME;
/*
* seconds login disabled after allowable number of unsuccessful attempts
*/
#ifndef DISABLETIME
#define DISABLETIME 20
#endif
#define MAXTRYS 5
static int retry = MAXTRYS;
/*
* Login logging support
*/
#define LOGINLOG "/var/adm/loginlog" /* login log file */
#define LNAME_SIZE 20 /* size of logged logname */
#define TTYN_SIZE 15 /* size of logged tty name */
#define TIME_SIZE 30 /* size of logged time string */
#define ENT_SIZE (LNAME_SIZE + TTYN_SIZE + TIME_SIZE + 3)
#define L_WAITTIME 5 /* waittime for log file to unlock */
#define LOGTRYS 10 /* depth of 'try' logging */
/*
* String manipulation macros: SCPYN, EQN and ENVSTRNCAT
*/
#define SCPYN(a, b) (void) strncpy(a, b, sizeof (a))
#define EQN(a, b) (strncmp(a, b, sizeof (a)-1) == 0)
#define ENVSTRNCAT(to, from) {int deflen; deflen = strlen(to); \
(void) strncpy((to)+ deflen, (from), sizeof (to) - (1 + deflen)); }
/*
* Other macros
*/
#define NMAX sizeof (utmp.ut_name)
#define HMAX sizeof (utmp.ut_host)
#define min(a, b) (((a) < (b)) ? (a) : (b))
/*
* Various useful files and string constants
*/
#define SHELL "/usr/bin/sh"
#define SHELL2 "/sbin/sh"
#define SUBLOGIN "<!sublogin>"
#define LASTLOG "/var/adm/lastlog"
#define PROG_NAME "login"
#define HUSHLOGIN ".hushlogin"
/*
* Array and Buffer sizes
*/
#define PBUFSIZE 8 /* max significant characters in a password */
#define MAXARGS 63
#define MAXENV 1024
#define MAXLINE 2048
/*
* Miscellaneous constants
*/
#define ROOTUID 0
#define ERROR 1
#define OK 0
#define LOG_ERROR 1
#define DONT_LOG_ERROR 0
#define TRUE 1
#define FALSE 0
/*
* Counters for counting the number of failed login attempts
*/
static int trys = 0;
/*
* Externs a plenty
*/
extern int getsecretkey();
/*
* BSM hooks
*/
extern int audit_login_save_flags(int rflag, int hflag);
extern int audit_login_save_host(char *host);
extern int audit_login_save_ttyn(char *ttyn);
extern int audit_login_save_port(void);
extern int audit_login_success(void);
extern int audit_login_save_pw(struct passwd *pwd);
extern int audit_login_bad_pw(void);
extern int audit_login_maxtrys(void);
extern int audit_login_not_console(void);
extern int audit_login_bad_dialup(void);
extern int audit_login_maxtrys(void);
/*
* utmp file variables
*/
static struct utmpx utmp;
/*
* The current user name
*/
static char user_name[64];
static char minusnam[16] = "-";
/*
* locale environments to be passed to shells.
*/
static char *localeenv[] = {
"LANG",
"LC_CTYPE", "LC_NUMERIC", "LC_TIME", "LC_COLLATE",
"LC_MONETARY", "LC_MESSAGES", "LC_ALL", 0};
static int locale_envmatch(char *lenv, char *penv);
/*
* Environment variable support
*/
static char shell[256] = { "SHELL=" };
static char home[MAXPATHLEN] = { "HOME=" };
static char term[64] = { "TERM=" };
static char logname[30] = { "LOGNAME=" };
static char timez[100] = { "TZ=" };
static char hertz[10] = { "HZ=" };
static char path[MAXPATHLEN] = { "PATH=" };
static char *newenv[10+MAXARGS] =
{home, path, logname, hertz, term, 0, 0};
static char **envinit = newenv;
static int basicenv;
static char envblk[MAXENV];
static char *zero = (char *)0;
static char **envp;
#ifndef NO_MAIL
static char mail[30] = { "MAIL=/var/mail/" };
#endif
extern char **environ;
char inputline[MAXLINE];
/*
* Strings used to prompt the user.
*/
static char loginmsg[] = "login: ";
static char passwdmsg[] = "Password:";
static char incorrectmsg[] = "Login incorrect\n";
/*
* Password file support
*/
static struct passwd *pwd;
static char remote_host[HMAX];
/*
* Illegal passwd entries.
*/
static struct passwd nouser = { "", "no:password", ~ROOTUID };
/*
* Log file support
*/
static char *log_entry[LOGTRYS];
static int writelog = 0;
static int lastlogok = 0;
static struct lastlog ll;
static int dosyslog = 0;
/*
* Default file toggles
*/
static char *Pndefault = "/etc/default/login";
static char *Altshell = NULL;
static char *Console = NULL;
static char *Passreq = NULL;
#define DEFUMASK 022
static mode_t Umask = DEFUMASK;
static char *Def_tz = NULL;
static char *tmp_tz = NULL;
static char *Def_hertz = NULL;
#define SET_FSIZ 2 /* ulimit() command arg */
static long Def_ulimit = 0;
#define MAX_TIMEOUT (15 * 60)
#define DEF_TIMEOUT (5 * 60)
static unsigned Def_timeout = DEF_TIMEOUT;
static char *Def_path = NULL;
static char *Def_supath = NULL;
#define DEF_PATH "/usr/bin:" /* same as PATH */
#define DEF_SUPATH "/usr/sbin:/usr/bin" /* same as ROOTPATH */
/*
* ttyprompt will point to the environment variable TTYPROMPT.
* TTYPROMPT is set by ttymon if ttymon already wrote out the prompt.
*/
static char *ttyprompt = NULL;
static char *ttyn = NULL;
static struct group *grpstr;
static char *ttygrp = "tty";
static char *progname = PROG_NAME;
/*
* Pass inherited environment. Used by telnetd in support of the telnet
* ENVIRON option.
*/
static int pflag;
/*
* Remote login support
*/
static int hflag, rflag;
static char rusername[NMAX+1], lusername[NMAX+1];
static char terminal[MAXPATHLEN];
/*
* Pre-authentication flag support
*/
static int fflag;
static int login_conv(int num_msg, struct pam_message **msg,
struct pam_response **response, void *appdata_ptr);
static struct pam_conv pam_conv = {login_conv, NULL};
static pam_handle_t *pamh; /* Authentication handle */
/*
* Function declarations
*/
static void turn_on_logging(void);
static void defaults(void);
static void usage(void);
static void process_rlogin(void);
static void login_authenticate();
static void setup_credentials(void);
static void adjust_nice(void);
static void update_utmp_entry(int sublogin);
static void establish_user_environment(char **renvp);
static void print_banner(void);
static void display_last_login_time(void);
static void exec_the_shell(void);
static int process_chroot_logins(void);
static int chdir_to_dir_root(void);
static void chdir_to_dir_user(void);
static void logindevperm(char *, uid_t, gid_t);
static void dir_dev_acc(char *, uid_t, gid_t, mode_t, char *);
static void check_log(void);
static void validate_account();
static void doremoteterm(char *term);
static int get_options(int argc, char *argv[]);
static void getstr(char *buf, int cnt, char *err);
static int legalenvvar(char *s);
static void check_for_root_user(void);
static void check_for_dueling_unix(char inputline[]);
static void get_user_name(void);
static void login_exit(int exit_code);
static int logins_disabled(char *user_name);
static void log_bad_attempts(void);
/*
* *** main ***
*
* The primary flow of control is directed in this routine.
* Control moves in line from top to bottom calling subfunctions
* which perform the bulk of the work. Many of these calls exit
* when a fatal error is encountered and do not return to main.
*
*
*/
void
main(int argc, char *argv[], char **renvp)
{
int sublogin;
/*
* Set up Defaults and flags
*/
defaults();
/*
* Set up default umask
*/
if (Umask > ((mode_t) 0777))
Umask = DEFUMASK;
(void) umask(Umask);
/*
* Set up default timeouts and delays
*/
if (Def_timeout > MAX_TIMEOUT)
Def_timeout = MAX_TIMEOUT;
if (Sleeptime < 0 || Sleeptime > 5)
Sleeptime = SLEEPTIME;
(void) alarm(Def_timeout);
/*
* Ignore SIGQUIT and SIGINT and set nice to 0
*/
(void) signal(SIGQUIT, SIG_IGN);
(void) signal(SIGINT, SIG_IGN);
(void) nice(0);
/*
* Set flag to disable the pid check if you find that you are
* a subsystem login.
*/
sublogin = 0;
if (*renvp && strcmp(*renvp, SUBLOGIN) == 0)
sublogin = 1;
/*
* Parse Arguments
*/
if (get_options(argc, argv) == -1) {
usage();
login_exit(1);
}
audit_login_save_flags(rflag, hflag);
audit_login_save_host(remote_host);
/*
* if devicename is not passed as argument, call ttyname(0)
*/
if (ttyn == NULL) {
ttyn = ttyname(0);
if (ttyn == NULL)
ttyn = "/dev/???";
}
audit_login_save_ttyn(ttyn);
audit_login_save_port();
/*
* Call pam_start to initiate a PAM authentication operation
*/
if ((pam_start(progname, user_name, &pam_conv, &pamh))
!= PAM_SUCCESS)
login_exit(1);
if ((pam_set_item(pamh, PAM_TTY, ttyn)) != PAM_SUCCESS) {
login_exit(1);
}
if ((pam_set_item(pamh, PAM_RHOST, remote_host)) != PAM_SUCCESS) {
login_exit(1);
}
/*
* Open the log file which contains a record of successful and failed
* login attempts
*/
turn_on_logging();
/*
* say "hi" to syslogd ..
*/
openlog("login", 0, LOG_AUTH);
/*
* Do special processing for -r (rlogin) flag
*/
if (rflag)
process_rlogin();
/*
* validate user
*/
/* we are already authenticated. fill in what we must, then continue */
if (fflag) {
if (pwd = getpwnam(user_name))
audit_login_save_pw(pwd);
else {
audit_login_save_pw(pwd);
audit_login_bad_pw();
log_bad_attempts();
login_exit(1);
}
} else {
/*
* Perform the primary login authentication activity.
*/
login_authenticate();
}
/* change root login, then we exec another login and try again */
if (process_chroot_logins() != OK)
login_exit(1);
/*
* If root login and not on system console then call exit(2)
*/
check_for_root_user();
/*
* Check to see if a shutdown is in progress, if it is and
* we are not root then throw the user off the system
*/
if (logins_disabled(user_name) == TRUE)
login_exit(1);
if (pwd->pw_uid == 0) {
if (Def_supath != NULL)
Def_path = Def_supath;
else
Def_path = DEF_SUPATH;
}
/*
* Check account expiration and passwd aging
*/
validate_account();
/*
* We only get here if we've been authenticated.
*/
update_utmp_entry(sublogin);
/*
* Now we set up the environment for the new user, which includes
* the users ulimit, nice value, ownership of this tty, uid, gid,
* and environment variables.
*/
if (Def_ulimit > 0L && ulimit(SET_FSIZ, Def_ulimit) < 0L)
(void) printf("Could not set ULIMIT to %ld\n", Def_ulimit);
/*
* Set mode to r/w user & w group, owner to user and group to tty
*/
(void) chmod(ttyn, S_IRUSR|S_IWUSR|S_IWGRP);
if ((grpstr = getgrnam(ttygrp)) == NULL)
(void) chown(ttyn, pwd->pw_uid, pwd->pw_gid);
else
(void) chown(ttyn, pwd->pw_uid, grpstr->gr_gid);
logindevperm(ttyn, pwd->pw_uid, pwd->pw_gid);
adjust_nice(); /* passwd file can specify nice value */
/*
* Record successful login and fork process that records logout.
* We have to do this before setting credentials because we need
* to be root in order do a setaudit() and an audit().
*/
audit_login_success();
setup_credentials(); /* Set uid/gid - exits on failure */
/*
* Set up the basic environment for the exec. This includes
* HOME, PATH, LOGNAME, SHELL, TERM, TZ, HZ, and MAIL.
*/
chdir_to_dir_user();
establish_user_environment(renvp);
pam_end(pamh, PAM_SUCCESS); /* Done using PAM */
if (pwd->pw_uid == 0)
if (remote_host[0] && dosyslog)
syslog(LOG_NOTICE, "ROOT LOGIN %s FROM %.*s",
ttyn, HMAX, remote_host);
else if (dosyslog)
syslog(LOG_NOTICE, "ROOT LOGIN %s", ttyn);
closelog();
(void) signal(SIGQUIT, SIG_DFL);
(void) signal(SIGINT, SIG_DFL);
/*
* Display some useful information to the new user like the banner
* and last login time if not a quiet login.
*/
if (access(HUSHLOGIN, F_OK) != 0) {
print_banner();
display_last_login_time();
}
/*
* Set SIGXCPU and SIGXFSZ to default disposition.
* Shells inherit signal disposition from parent.
* And the shells should have default dispositions
* for the two below signals.
*/
(void) signal(SIGXCPU, SIG_DFL);
(void) signal(SIGXFSZ, SIG_DFL);
/*
* Now fire off the shell of choice
*/
exec_the_shell();
/*
* All done
*/
login_exit(1);
/* NOTREACHED */
}
/*
* *** Utility functions ***
*/
/*
* donothing & catch - Signal catching functions
*/
/*ARGSUSED*/
static void
donothing(int sig)
{
if (pamh)
pam_end(pamh, PAM_ABORT);
}
#ifdef notdef
static int intrupt;
/*ARGSUSED*/
static void
catch(int sig)
{
++intrupt;
}
#endif
/*
* *** Bad login logging support ***
*/
/*
* badlogin() - log to the log file 'trys'
* unsuccessful attempts
*/
static void
badlogin(void)
{
int retval, count1, fildes;
/*
* 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) {
(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 (count1 = 0; count1 < trys; count1++)
(void) write(fildes, log_entry[count1],
(unsigned) strlen(log_entry[count1]));
(void) lockf(fildes, F_ULOCK, 0L);
}
(void) close(fildes);
}
}
/*
* log_bad_attempts - log each bad login attempt - called from
* login_authenticate. Exits when the maximum attempt
* count is exceeded.
*/
static void
log_bad_attempts(void)
{
time_t timenow;
if (writelog == 1 && trys < LOGTRYS) {
(void) time(&timenow);
(void) strncat(log_entry[trys], user_name, LNAME_SIZE);
(void) strncat(log_entry[trys], ":", (size_t) 1);
(void) strncat(log_entry[trys], ttyn, TTYN_SIZE);
(void) strncat(log_entry[trys], ":", (size_t) 1);
(void) strncat(log_entry[trys], ctime(&timenow), TIME_SIZE);
trys++;
}
}
/*
* turn_on_logging - if the logfile exist, turn on attempt logging and
* initialize the string storage area
*/
static void
turn_on_logging(void)
{
struct stat dbuf;
int i;
if (stat(LOGINLOG, &dbuf) == 0) {
writelog = 1;
for (i = 0; i < LOGTRYS; i++) {
if (!(log_entry[i] = malloc((size_t) ENT_SIZE))) {
writelog = 0;
break;
}
*log_entry[i] = '\0';
}
}
}
/*
* login_conv():
* This is the conv (conversation) function called from
* a PAM authentication module to print error messages
* or garner information from the user.
*/
static int
login_conv(int num_msg, struct pam_message **msg,
struct pam_response **response, void *appdata_ptr)
{
struct pam_message *m;
struct pam_response *r;
char *temp;
int k, i;
if (num_msg <= 0)
return (PAM_CONV_ERR);
*response = calloc(num_msg, sizeof (struct pam_response));
if (*response == NULL)
return (PAM_BUF_ERR);
k = num_msg;
m = *msg;
r = *response;
while (k--) {
switch (m->msg_style) {
case PAM_PROMPT_ECHO_OFF:
temp = getpassphrase(m->msg);
if (temp != NULL) {
r->resp = strdup(temp);
if (r->resp == NULL) {
/* free responses */
r = *response;
for (i = 0; i < num_msg; i++, r++) {
if (r->resp)
free(r->resp);
}
free(*response);
*response = NULL;
return (PAM_BUF_ERR);
}
}
m++;
r++;
break;
case PAM_PROMPT_ECHO_ON:
if (m->msg != NULL)
(void) fputs(m->msg, stdout);
r->resp = calloc(1, PAM_MAX_RESP_SIZE);
if (r->resp == NULL) {
/* free responses */
r = *response;
for (i = 0; i < num_msg; i++, r++) {
if (r->resp)
free(r->resp);
}
free(*response);
*response = NULL;
return (PAM_BUF_ERR);
}
if (fgets(r->resp, PAM_MAX_RESP_SIZE-1, stdin)) {
int len;
r->resp[PAM_MAX_RESP_SIZE-1] = NULL;
len = strlen(r->resp);
if (r->resp[len-1] == '\n')
r->resp[len-1] = '\0';
} else {
login_exit(1);
}
m++;
r++;
break;
case PAM_ERROR_MSG:
if (m->msg != NULL) {
(void) fputs(m->msg, stderr);
(void) fputs("\n", stderr);
}
m++;
r++;
break;
case PAM_TEXT_INFO:
if (m->msg != NULL) {
(void) fputs(m->msg, stdout);
(void) fputs("\n", stdout);
}
m++;
r++;
break;
default:
break;
}
}
return (PAM_SUCCESS);
}
/*
* verify_passwd - Checks the users password
* Returns: OK if password check successful,
* ERROR if password check fails.
*/
static int
verify_passwd()
{
int flags;
int error;
char *user;
/*
* PAM authenticates the user for us.
*/
error = pam_authenticate(pamh, 0);
/* get the user_name from the pam handle */
pam_get_item(pamh, PAM_USER, (void**)&user);
SCPYN(user_name, user); /*SCUT*/
check_for_dueling_unix(user_name);
if ((pwd = getpwnam(user_name)) == NULL) {
audit_login_save_pw(pwd);
return (PAM_USER_UNKNOWN);
}
audit_login_save_pw(pwd);
return (error);
}
/*
* quotec - Called by 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);
}
/*
* getargs - returns an input line. Exits if EOF encountered.
*/
#define WHITESPACE 0
#define ARGUMENT 1
static char **
getargs(char *inline)
{
static char envbuf[MAXLINE];
static char *args[MAXARGS];
register char *ptr, **answer;
register int c;
int state;
for (ptr = envbuf; ptr < &envbuf[sizeof (envbuf)]; /* cstyle */)
*ptr++ = '\0';
for (answer = args; answer < &args[MAXARGS]; /* cstyle */)
*answer++ = (char *)NULL;
for (ptr = envbuf, answer = &args[0], state = WHITESPACE;
(c = getc(stdin)) != EOF; /* cstyle */) {
*(inline++) = c;
switch (c) {
case '\n':
if (ptr == &envbuf[0])
return ((char **)NULL);
return (&args[0]);
case ' ':
case '\t':
if (state == ARGUMENT) {
*ptr++ = '\0';
state = WHITESPACE;
}
break;
case '\\':
c = quotec();
default:
if (state == WHITESPACE) {
*answer++ = ptr;
state = ARGUMENT;
}
*ptr++ = c;
}
/*
* If the buffer is full, force the next character to be read to
* be a <newline>.
*/
if (ptr == &envbuf[sizeof (envbuf)-1]) {
(void) ungetc('\n', stdin);
(void) putc('\n', stdout);
}
}
/*
* If we left loop because an EOF was received, exit immediately.
*/
login_exit(0);
/* NOTREACHED */
}
/*
* get_user_name - Gets the user name either passed in, or from the
* login: prompt.
*/
static void
get_user_name()
{
FILE *fp;
int gotname = 0;
if ((fp = fopen(ISSUEFILE, "r")) != NULL) {
char *ptr, buffer[BUFSIZ];
while ((ptr = fgets(buffer, sizeof (buffer),
fp)) != NULL) {
(void) fputs(ptr, stdout);
}
(void) fclose(fp);
}
/*
* if TTYPROMPT is not set, use our own prompt
* otherwise, use ttyprompt. We just set PAM_USER_PROMPT
* and let the module do the prompting.
*/
if ((ttyprompt == NULL) || (*ttyprompt == '\0'))
(void) pam_set_item(pamh, PAM_USER_PROMPT, (void *)loginmsg);
else
(void) pam_set_item(pamh, PAM_USER_PROMPT, (void *)ttyprompt);
envp = &zero; /* XXX: is this right? */
}
/*
* Check_for_dueling_unix - Check to see if the another login is talking
* to the line we've got open as a login port
* Exits if we're talking to another unix system
*/
static void
check_for_dueling_unix(char *inputline)
{
if (EQN(loginmsg, inputline) || EQN(passwdmsg, inputline) ||
EQN(incorrectmsg, inputline)) {
(void) printf("Looking at a login line.\n");
login_exit(8);
}
}
/*
* logins_disabled - if the file /etc/nologin exists and the user is not
* root then do not permit them to login
*/
static int
logins_disabled(char *user_name)
{
FILE *nlfd;
int c;
if (!EQN("root", user_name) &&
((nlfd = fopen(NOLOGIN, "r")) != (FILE *)NULL)) {
while ((c = getc(nlfd)) != EOF)
putchar(c);
(void) fflush(stdout);
sleep(5);
return (TRUE);
}
return (FALSE);
}
/*
* check_for_root_user - Checks if we're getting a root login on the console
* Exits if root login not on system console.
*
*/
static void
check_for_root_user(void)
{
if (pwd->pw_uid == 0) {
if ((Console != NULL) && (strcmp(ttyn, Console) != 0)) {
audit_login_not_console();
(void) printf("Not on system console\n");
login_exit(10);
}
}
}
static char *illegal[] = {
"SHELL=",
"HOME=",
"LOGNAME=",
#ifndef NO_MAIL
"MAIL=",
#endif
"CDPATH=",
"IFS=",
"PATH=",
0
};
/*
* legalenvvar - Is it 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)) == 0)
return (0);
if (s[0] == 'L' && s[1] == 'D' && s[2] == '_')
return (0);
return (1);
}
/*
* getstr - Get a string from standard input
* Calls exit if read(2) fails.
*/
static void
getstr(char *buf, int cnt, char *err)
{
char c;
do {
if (read(0, &c, 1) != 1)
login_exit(1);
*buf++ = c;
} while (--cnt > 1 && c != 0);
*buf = 0;
err = err; /* For lint */
}
/*
* defaults - read defaults
*/
static void
defaults(void)
{
register int flags;
register char *ptr;
if (defopen(Pndefault) == 0) {
/*
* ignore case
*/
flags = defcntl(DC_GETFLAGS, 0);
TURNOFF(flags, DC_CASE);
defcntl(DC_SETFLAGS, flags);
if ((Console = defread("CONSOLE=")) != NULL)
Console = strdup(Console);
if ((Altshell = defread("ALTSHELL=")) != NULL)
Altshell = strdup(Altshell);
if ((Passreq = defread("PASSREQ=")) != NULL)
Passreq = strdup(Passreq);
if ((Def_tz = defread("TIMEZONE=")) != NULL)
Def_tz = strdup(Def_tz);
if ((Def_hertz = defread("HZ=")) != NULL)
Def_hertz = strdup(Def_hertz);
if ((Def_path = defread("PATH=")) != NULL)
Def_path = strdup(Def_path);
if ((Def_supath = defread("SUPATH=")) != NULL)
Def_supath = strdup(Def_supath);
if ((ptr = defread("ULIMIT=")) != NULL)
Def_ulimit = atol(ptr);
if ((ptr = defread("TIMEOUT=")) != NULL)
Def_timeout = (unsigned) atoi(ptr);
if ((ptr = defread("UMASK=")) != NULL)
if (sscanf(ptr, "%lo", &Umask) != 1)
Umask = DEFUMASK;
if ((ptr = defread("SLEEPTIME=")) != NULL)
Sleeptime = atoi(ptr);
if ((ptr = defread("SYSLOG=")) != NULL)
dosyslog = strcmp(ptr, "YES") == 0;
if ((ptr = defread("RETRIES=")) != NULL)
retry = atoi(ptr);
(void) defopen((char *)NULL);
}
}
/*
* get_options(argc, argv)
* - parse the cmd line.
* - return 0 if successful, -1 if failed.
* Calls login_exit() on misuse of -r and -h flags
*/
static int
get_options(int argc, char *argv[])
{
int c;
int errflg = 0;
while ((c = getopt(argc, argv, "f:h:r:pad:")) != -1) {
switch (c) {
case 'a':
break;
case 'd':
/*
* Must be root to pass in device name
* otherwise we exit() as punishment for trying.
*/
if (getuid() != 0 || geteuid() != 0) {
login_exit(1); /* sigh */
/*NOTREACHED*/
}
ttyn = optarg;
break;
case 'h':
if (hflag || rflag) {
(void) fprintf(stderr,
"Only one of -r and -h allowed\n");
login_exit(1);
}
hflag++;
SCPYN(remote_host, optarg);
if (argv[optind]) {
if (argv[optind][0] != '-')
SCPYN(terminal, argv[optind]);
optind++;
}
progname = "telnet";
break;
case 'r':
if (hflag || rflag) {
(void) fprintf(stderr,
"Only one of -r and -h allowed\n");
login_exit(1);
}
rflag++;
SCPYN(remote_host, optarg);
progname = "rlogin";
break;
case 'p':
pflag++;
break;
case 'f':
/*
* Must be root to bypass authentication
* otherwise we exit() as punishment for trying.
*/
if (getuid() != 0 || geteuid() != 0) {
login_exit(1); /* sigh */
/*NOTREACHED*/
}
/* save fflag user name for future use */
SCPYN(user_name, optarg);
fflag = 1;
break;
default:
errflg++;
break;
} /* end switch */
} /* end while */
/*
* get the prompt set by ttymon
*/
ttyprompt = getenv("TTYPROMPT");
if ((ttyprompt != NULL) && (*ttyprompt != '\0')) {
/*
* if ttyprompt is set, there should be data on
* the stream already.
*/
if ((envp = getargs(inputline)) != (char **)NULL) {
/*
* don't get name if name passed as argument.
*/
SCPYN(user_name, *envp++);
}
} else if (optind < argc) {
SCPYN(user_name, argv[optind]);
(void) strcpy(inputline, user_name);
(void) strcat(inputline, " \n");
envp = &argv[optind+1];
}
if (errflg)
return (-1);
return (0);
}
/*
* usage - Print usage message
*
*/
static void
usage(void)
{
(void) fprintf(stderr,
"Usage:\tlogin [-h|-r] [ name [ env-var ... ]]\n");
}
/*
* *** Remote login support ***
*
*/
/*
* doremoteterm - Sets the appropriate ioctls for a remote terminal
*/
static char *speeds[] = {
"0", "50", "75", "110", "134", "150", "200", "300",
"600", "1200", "1800", "2400", "4800", "9600", "19200", "38400",
"57600", "76800", "115200", "153600", "230400", "307200", "460800"
};
#define NSPEEDS (sizeof (speeds) / sizeof (speeds[0]))
static void
doremoteterm(char *term)
{
struct termios tp;
register char *cp = strchr(term, '/'), **cpp;
char *speed;
(void) ioctl(0, TCGETS, &tp);
if (cp) {
*cp++ = '\0';
speed = cp;
cp = strchr(speed, '/');
if (cp)
*cp++ = '\0';
for (cpp = speeds; cpp < &speeds[NSPEEDS]; cpp++)
if (strcmp(*cpp, speed) == 0) {
cfsetospeed(&tp, cpp-speeds);
break;
}
}
tp.c_lflag |= ECHO|ICANON;
tp.c_iflag |= IGNPAR|ICRNL;
(void) ioctl(0, TCSETS, &tp);
}
/*
* Process_rlogin - Does the work that rlogin and telnet
* need done
*/
static void
process_rlogin(void)
{
getstr(rusername, sizeof (rusername), "remuser");
getstr(lusername, sizeof (lusername), "locuser");
getstr(terminal, sizeof (terminal), "Terminal type");
/* fflag has precedence over stuff passed by rlogind */
if (fflag || getuid()) {
pwd = &nouser;
doremoteterm(terminal);
return;
} else {
if (pam_set_item(pamh, PAM_USER, lusername) != PAM_SUCCESS)
login_exit(1);
pwd = getpwnam(lusername);
if (pwd == NULL) {
pwd = &nouser;
doremoteterm(terminal);
return;
}
}
doremoteterm(terminal);
/*
* Update PAM on the user name
*/
if (pam_set_item(pamh, PAM_USER, lusername) != PAM_SUCCESS)
login_exit(1);
if (pam_set_item(pamh, PAM_RUSER, rusername) != PAM_SUCCESS)
login_exit(1);
SCPYN(user_name, lusername);
envp = &zero;
lusername[0] = '\0';
}
/*
* *** Account validation routines ***
*
*/
/*
* validate_account - This is the PAM version of validate.
*/
static void
validate_account()
{
int error;
int n;
int flag;
(void) alarm(0); /* give user time to come up with password */
check_log();
if ((Passreq != NULL) && (!strcasecmp("YES", Passreq)))
flag = PAM_DISALLOW_NULL_AUTHTOK;
else
flag = 0;
if ((error = pam_acct_mgmt(pamh, flag)) != PAM_SUCCESS) {
if (error == PAM_NEW_AUTHTOK_REQD) {
(void) printf("Choose a new password.\n");
if ((error = pam_chauthtok(pamh, 0)) != PAM_SUCCESS) {
if (dosyslog)
syslog(LOG_CRIT,
"change password failure: %s",
pam_strerror(pamh, error));
login_exit(1);
}
} else {
(void) printf(incorrectmsg);
if (dosyslog)
syslog(LOG_CRIT,
"login account failure: %s",
pam_strerror(pamh, error));
login_exit(1);
}
}
}
/*
* Check_log - This is really a hack because PAM checks the log, but login
* wants to know if the log is okay and PAM doesn't have
* a module independent way of handing this info back.
*/
static void
check_log(void)
{
int fdl;
long long offset;
offset = (long long) pwd->pw_uid * (long long) sizeof (struct lastlog);
if ((fdl = open(LASTLOG, O_RDWR|O_CREAT, 0444)) >= 0) {
if (llseek(fdl, offset, SEEK_SET) == offset &&
read(fdl, (char *)&ll, sizeof (ll)) == sizeof (ll) &&
ll.ll_time != 0)
lastlogok = 1;
(void) close(fdl);
}
}
/*
* chdir_to_dir_root - Attempts to chdir us to the home directory.
* defaults to "/" if it can't cd to the home
* directory, and returns ERROR if it can't do that.
*/
static int
chdir_to_dir_root(void)
{
if (chdir(pwd->pw_dir) < 0) {
if (chdir("/") < 0) {
(void) printf("No directory!\n");
return (ERROR);
}
}
return (OK);
}
/*
* chdir_to_dir_user - Now chdir after setuid/setgid have happened to
* place us in the user's home directory just in
* case it was protected and the first chdir failed.
* No chdir errors should happen at this point because
* all failures should have happened on the first
* time around.
*/
static void
chdir_to_dir_user(void)
{
if (chdir(pwd->pw_dir) < 0) {
if (chdir("/") < 0) {
(void) printf("No directory!\n");
/*
* This probably won't work since we can't get to /.
*/
if (remote_host[0] && dosyslog) {
syslog(LOG_CRIT,
"REPEATED LOGIN FAILURES ON %s FROM %.*s",
ttyn, HMAX, remote_host);
} else if (dosyslog) {
syslog(LOG_CRIT,
"REPEATED LOGIN FAILURES ON %s", ttyn);
}
closelog();
(void) sleep(DISABLETIME);
exit(1);
} else {
(void) printf("No directory! Logging in with home=/\n");
pwd->pw_dir = "/";
}
}
}
/*
* login_authenticate - Performs the main authentication work
* 1. Prints the login prompt
* 2. Requests and verifys the password
* 3. Checks the port password
*/
static void
login_authenticate()
{
int cnt = 1;
char *user;
int err;
int login_successful = 0;
do {
/* if scheme broken, then nothing to do but quit */
if (pam_get_item(pamh, PAM_USER, (void **)&user)
!= PAM_SUCCESS)
exit(1);
/*
* only get name from utility if it is not already
* supplied by pam_start or a pam_set_item.
*/
if (!user || !user[0]) {
/* use call back to get user name */
get_user_name();
}
err = verify_passwd();
switch (err) {
case PAM_MAXTRIES:
cnt = retry;
case PAM_AUTH_ERR:
case PAM_AUTHINFO_UNAVAIL:
case PAM_USER_UNKNOWN:
audit_login_bad_pw();
log_bad_attempts();
break;
case PAM_SUCCESS:
case PAM_NEW_AUTHTOK_REQD:
/*
* pm_authenticate() shouldn't
* return this but might do in
* some cases.
*/
if (chdir_to_dir_root() == OK) {
cnt = 0;
login_successful = 1;
}
else
audit_login_bad_pw();
break;
case PAM_ABORT:
audit_login_bad_pw();
log_bad_attempts();
(void) sleep(DISABLETIME);
(void) printf(incorrectmsg);
login_exit(1);
/*NOTREACHED*/
default: /* Some other PAM error */
login_exit(1);
/*NOTREACHED*/
}
if (login_successful)
break;
/* only sleep after first bad passwd */
if (cnt)
(void) sleep(Sleeptime);
(void) printf(incorrectmsg);
user_name[0] = '\0'; /* is this needed? */
/* force name to be null in this case */
if (pam_set_item(pamh, PAM_USER, NULL) != PAM_SUCCESS)
login_exit(1);
if (pam_set_item(pamh, PAM_RUSER, NULL) != PAM_SUCCESS)
login_exit(1);
} while (cnt++ < retry);
if (cnt >= retry) {
audit_login_maxtrys();
/*
* If logging is turned on, output the
* string storage area to the log file,
* and sleep for DISABLETIME
* seconds before exiting.
*/
if (writelog)
badlogin();
if (remote_host[0] && dosyslog) {
syslog(LOG_CRIT,
"REPEATED LOGIN FAILURES ON %s FROM %.*s",
ttyn, HMAX, remote_host);
} else if (dosyslog) {
syslog(LOG_CRIT,
"REPEATED LOGIN FAILURES ON %s", ttyn);
}
(void) sleep(DISABLETIME);
exit(1);
}
}
/*
* *** Credential Related routines ***
*
*/
/*
* setup_credentials - sets the group ID, initializes the groups
* and sets up the secretkey.
* Exits if a failure occurrs.
*/
/*
* setup_credentials - PAM does all the work for us on this one.
*/
static void
setup_credentials(void)
{
int error = 0;
int flags;
/* set the real (and effective) GID */
if (setgid(pwd->pw_gid) == -1) {
login_exit(1);
}
/*
* Initialize the supplementary group access list.
*/
if (!user_name)
login_exit(1);
if (initgroups(user_name, pwd->pw_gid) == -1) {
login_exit(1);
}
/* XXX really should be after setgid */
if ((error = pam_setcred(pamh, PAM_ESTABLISH_CRED)) != PAM_SUCCESS) {
login_exit(error);
}
/* set the real (and effective) UID */
if (setuid(pwd->pw_uid) == -1) {
login_exit(1);
}
}
/*
*
* *** Routines to get a new user set up and running ***
*
* Things to do when starting up a new user:
* adjust_nice
* update_utmp_entry
* establish_user_environment
* print_banner
* display_last_login_time
* exec_the_shell
*
*/
/*
* adjust_nice - Set the nice (process priority) value if the
* gecos value contains an appropriate value.
*/
static void
adjust_nice(void)
{
int pri, mflg, i;
if (strncmp("pri=", pwd->pw_gecos, 4) == 0) {
pri = 0;
mflg = 0;
i = 4;
if (pwd->pw_gecos[i] == '-') {
mflg++;
i++;
}
while (pwd->pw_gecos[i] >= '0' && pwd->pw_gecos[i] <= '9')
pri = (pri * 10) + pwd->pw_gecos[i++] - '0';
if (mflg)
pri = -pri;
(void) nice(pri);
}
}
/*
* update_utmp_entry - Searchs for the correct utmp entry, making an
* entry there if it finds one, otherwise exits.
*/
static void
update_utmp_entry(int sublogin)
{
char *user;
int error;
static char *errmsg = "No utmpx entry. "
"You must exec \"login\" from the lowest level \"shell\".";
int tmplen;
struct utmpx *u = (struct utmpx *)0;
struct utmpx utmp;
char *ttyntail;
/*
* If we're not a sublogin then
* we'll get an error back if our PID doesn't match the PID of the
* entry we are updating, otherwise if its a sublogin the flags
* field is set to 0, which means we just write a matching entry
* (without checking the pid), or a new entry if an entry doesn't
* exist.
*/
if (error = pam_open_session(pamh, 0) != PAM_SUCCESS) {
login_exit(1);
}
if ((error = pam_get_item(pamh, PAM_USER, (void **) &user))
!= PAM_SUCCESS) {
login_exit(1);
}
(void) memset((void *)&utmp, 0, sizeof (utmp));
(void) time(&utmp.ut_tv.tv_sec);
utmp.ut_pid = getpid();
if (rflag || hflag) {
SCPYN(utmp.ut_host, remote_host);
tmplen = strlen(remote_host) + 1;
if (tmplen < sizeof (utmp.ut_host))
utmp.ut_syslen = tmplen;
else
utmp.ut_syslen = sizeof (utmp.ut_host);
} else {
utmp.ut_syslen = 0;
}
SCPYN(utmp.ut_user, user);
/* skip over "/dev/" */
ttyntail = basename(ttyn);
while ((u = getutxent()) != NULL) {
if ((u->ut_type == INIT_PROCESS ||
u->ut_type == LOGIN_PROCESS ||
u->ut_type == USER_PROCESS) &&
((sublogin && strncmp(u->ut_line, ttyntail,
sizeof (u->ut_line)) == 0) ||
u->ut_pid == utmp.ut_pid)) {
SCPYN(utmp.ut_line, (ttyn+sizeof ("/dev/")-1));
(void) memcpy(utmp.ut_id, u->ut_id,
sizeof (utmp.ut_id));
utmp.ut_type = USER_PROCESS;
pututxline(&utmp);
break;
}
}
endutxent();
if (u == (struct utmpx *)NULL) {
if (!sublogin) {
/*
* no utmp entry already setup
* (init or rlogind/telnetd)
*/
(void) puts(errmsg);
login_exit(1);
}
} 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. */
updwtmpx(WTMPX_FILE, &utmp);
}
}
/*
* process_chroot_logins - Chroots to the specified subdirectory and
* re executes login.
*/
static int
process_chroot_logins(void)
{
/*
* If the shell field starts with a '*', do a chroot to the home
* directory and perform a new login.
*/
if (*pwd->pw_shell == '*') {
pam_end(pamh, PAM_SUCCESS); /* Done using PAM */
if (chroot(pwd->pw_dir) < 0) {
(void) printf("No Root Directory\n");
return (ERROR);
}
/*
* Set the environment flag <!sublogin> so that the next login
* knows that it is a sublogin.
*/
envinit[0] = SUBLOGIN;
envinit[1] = (char *)NULL;
(void) printf("Subsystem root: %s\n", pwd->pw_dir);
(void) execle("/usr/bin/login", "login", (char *)0,
&envinit[0]);
(void) execle("/etc/login", "login", (char *)0, &envinit[0]);
(void) printf("No /usr/bin/login or /etc/login on root\n");
login_exit(1);
}
return (OK);
}
/*
* establish_user_environment - Set up the new users enviornment
*/
static void
establish_user_environment(char **renvp)
{
int i, j, k, l_index, length, idx = 0;
char *ptr;
char *endptr;
char **lenvp;
char **pam_env;
lenvp = environ;
while (*lenvp++)
;
/* count the number of PAM environment variables set by modules */
if ((pam_env = pam_getenvlist(pamh)) != 0) {
for (idx = 1; pam_env[idx] != 0; idx++)
;
}
envinit = (char **) calloc(lenvp - environ + 10
+ MAXARGS + idx, sizeof (char *));
if (envinit == NULL) {
(void) printf("Calloc failed - out of swap space.\n");
login_exit(8);
}
/*
* add PAM environment variables first so they
* can be overwritten at login's discretion.
* check for illegal environment variables.
*/
idx = 0; basicenv = 0;
if (pam_env != 0) {
while (pam_env[idx] != 0) {
if (legalenvvar(pam_env[idx])) {
envinit[basicenv] = pam_env[idx];
basicenv++;
}
idx++;
}
}
memcpy(&envinit[basicenv], newenv, sizeof (newenv));
/* Set up environment */
if (rflag) {
ENVSTRNCAT(term, terminal);
} else if (hflag) {
if (strlen(terminal)) {
ENVSTRNCAT(term, terminal);
}
} else {
char *tp = getenv("TERM");
if ((tp != NULL) && (*tp != '\0'))
ENVSTRNCAT(term, tp);
}
ENVSTRNCAT(logname, pwd->pw_name);
/*
* There are three places to get timezone info. init.c sets
* TZ if the file /etc/TIMEZONE contains a value for TZ.
* login.c looks in the file /etc/default/login for a
* variable called TIMEZONE being set. If TIMEZONE has a
* value, TZ is set to that value; no environment variable
* TIMEZONE is set, only TZ. If neither of these methods
* work to set TZ, then the library routines will default
* to using the file /usr/lib/locale/TZ/localtime.
*
* There is a priority set up here. If /etc/TIMEZONE has
* a value for TZ, that value remains top priority. If the
* file /etc/default/login has TIMEZONE set, that has second
* highest priority not overriding the value of TZ in
* /etc/TIMEZONE. The reason for this priority is that the
* file /etc/TIMEZONE is supposed to be sourced by
* /etc/profile. We are doing the "sourcing" prematurely in
* init.c. Additionally, a login C shell doesn't source the
* file /etc/profile thus not sourcing /etc/TIMEZONE thus not
* allowing an adminstrator to globally set TZ for all users
*/
if (Def_tz != NULL) /* Is there a TZ from defaults/login? */
tmp_tz = Def_tz;
if ((Def_tz = getenv("TZ")) != NULL) {
ENVSTRNCAT(timez, Def_tz);
} else if (tmp_tz != NULL) {
Def_tz = tmp_tz;
ENVSTRNCAT(timez, Def_tz);
}
if (Def_hertz == NULL)
(void) sprintf(hertz + strlen(hertz), "%u", HZ);
else
ENVSTRNCAT(hertz, Def_hertz);
if (Def_path == NULL)
(void) strcat(path, DEF_PATH);
else
ENVSTRNCAT(path, Def_path);
ENVSTRNCAT(home, pwd->pw_dir);
/*
* Find the end of the basic environment
*/
for (basicenv = 0; envinit[basicenv] != NULL; basicenv++)
;
/*
* If TZ has a value, add it.
*/
if (strcmp(timez, "TZ=") != 0)
envinit[basicenv++] = timez;
if (*pwd->pw_shell == '\0') {
/*
* If possible, use the primary default shell,
* otherwise, use the secondary one.
*/
if (access(SHELL, X_OK) == 0)
pwd->pw_shell = SHELL;
else
pwd->pw_shell = SHELL2;
} else if (Altshell != NULL && strcmp(Altshell, "YES") == 0) {
envinit[basicenv++] = shell;
ENVSTRNCAT(shell, pwd->pw_shell);
}
#ifndef NO_MAIL
envinit[basicenv++] = mail;
(void) strcat(mail, pwd->pw_name);
#endif
/*
* Pick up locale environment variables, if any.
*/
lenvp = renvp;
while (*lenvp != NULL) {
j = 0;
while (localeenv[j] != 0) {
/*
* locale_envmatch() returns 1 if
* *lenvp is localenev[j] and valid.
*/
if (locale_envmatch(localeenv[j], *lenvp) == 1) {
envinit[basicenv++] = *lenvp;
break;
}
j++;
}
lenvp++;
}
/*
* If '-p' flag, then try to pass on allowable environment
* variables. Note that by processing this first, what is
* passed on the final "login:" line may over-ride the invocation
* values. XXX is this correct?
*/
if (pflag) {
for (lenvp = renvp; *lenvp; lenvp++) {
if (!legalenvvar(*lenvp)) {
continue;
}
/*
* If this isn't 'xxx=yyy', skip it. XXX
*/
if ((endptr = strchr(*lenvp, '=')) == NULL) {
continue;
}
length = endptr + 1 - *lenvp;
for (j = 0; j < basicenv; j++) {
if (strncmp(envinit[j], *lenvp, length) == 0) {
/*
* Replace previously established value
*/
envinit[j] = *lenvp;
break;
}
}
if (j == basicenv) {
/*
* It's a new definition, so add it at the end.
*/
envinit[basicenv++] = *lenvp;
}
}
}
/*
* 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, '=')) == NULL) {
envinit[basicenv+k] = ptr;
(void) sprintf(ptr, "L%d=%s", l_index, *envp);
/*
* Advance "ptr" to the beginning of the
* next argument.
*/
while (*ptr++)
;
k++;
l_index++;
} else {
if (!legalenvvar(*envp)) { /* this env var permited? */
continue;
} else {
/*
* Check to see whether this string replaces
* any previously defined string
*/
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++;
}
}
}
} /* for (j = 0 ... ) */
/*
* Switch to the new environment.
*/
environ = envinit;
}
/*
* print_banner - Print the banner at start up
* Do not turn on DOBANNER ifdef. This is not
* relevant to SunOS.
*/
static void
print_banner(void)
{
#ifdef DOBANNER
uname(&un);
#if i386
(void) printf("UNIX System V/386 Release %s\n%s\n"
"Copyright (C) 1984, 1986, 1987, 1988 AT&T\n"
"Copyright (C) 1987, 1988 Microsoft Corp.\nAll Rights Reserved\n",
un.release, un.nodename);
#elif sun
(void) printf("SunOS Release %s Sun Microsystems %s\n%s\n"
"Copyright (c) 1984, 1986, 1987, 1988 AT&T\n"
"Copyright (c) 1988, 1989, 1990, 1991 Sun Microsystems\n"
"All Rights Reserved\n",
un.release, un.machine, un.nodename);
#else
(void) printf("UNIX System V Release %s AT&T %s\n%s\n"
"Copyright (c) 1984, 1986, 1987, 1988 AT&T\nAll Rights Reserved\n",
un.release, un.machine, un.nodename);
#endif /* i386 */
#endif /* DOBANNER */
}
/*
* display_last_login_time - Advise the user the time and date
* that this login-id was last used.
*/
static void
display_last_login_time(void)
{
if (lastlogok) {
(void) printf("Last login: %.*s ", 24-5, ctime(&ll.ll_time));
if (*ll.ll_host != '\0')
(void) printf("from %.*s\n", sizeof (ll.ll_host),
ll.ll_host);
else
(void) printf("on %.*s\n", sizeof (ll.ll_line),
ll.ll_line);
}
}
/*
* exec_the_shell - invoke the specified shell or start up program
*/
static void
exec_the_shell(void)
{
char *endptr;
int i;
(void) strcat(minusnam, basename(pwd->pw_shell));
/*
* Exec the shell
*/
(void) execl(pwd->pw_shell, minusnam, (char *)0);
/*
* pwd->pw_shell 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(pwd->pw_shell, R_OK|X_OK) == 0) {
(void) execl(SHELL, "sh", pwd->pw_shell, (char *)0);
(void) execl(SHELL2, "sh", pwd->pw_shell, (char *)0);
}
(void) printf("No shell\n");
}
/*
* login_exit - Call exit() and terminate.
* This function is here for PAM so cleanup can
* be done before the process exits.
*/
static void
login_exit(int exit_code)
{
if (pamh)
pam_end(pamh, PAM_ABORT);
exit(exit_code);
/*NOTREACHED*/
}
/*
* Check if lenv and penv matches or not.
*/
static int
locale_envmatch(char *lenv, char *penv)
{
while ((*lenv == *penv) && *lenv && *penv != '=') {
lenv++;
penv++;
}
/*
* '/' is eliminated for security reason.
*/
if (*lenv == '\0' && *penv == '=' && *(penv + 1) != '/')
return (1);
return (0);
}
/*
* logindevperm - change owner/group/permissions of devices
* list in /etc/logindevperm. (Code derived from set_fb_attrs()
* in 4.x usr/src/bin/login.c and usr/src/etc/getty/main.c.)
*/
#define MAX_LINELEN 256
#define LOGINDEVPERM "/etc/logindevperm"
#define DIRWILD "/*" /* directory wildcard */
#define DIRWLDLEN 2 /* strlen(DIRWILD) */
static void
logindevperm(char *ttyn, uid_t uid, gid_t gid)
{
char *field_delims = " \t\n";
char *permfile = LOGINDEVPERM;
char line[MAX_LINELEN];
char *console;
char *mode_str;
char *dev_list;
char *device;
char *ptr;
int mode;
FILE *fp;
size_t l;
int lineno;
if ((fp = fopen(permfile, "r")) == NULL)
return;
lineno = 0;
while (fgets(line, MAX_LINELEN, fp) != NULL) {
lineno++;
if ((ptr = strchr(line, '#')) != NULL)
*ptr = '\0'; /* handle comments */
if ((console = strtok(line, field_delims)) == NULL)
continue; /* ignore blank lines */
if (strcmp(console, ttyn) != 0)
continue; /* not our tty, skip */
mode_str = strtok((char *)NULL, field_delims);
if (mode_str == NULL) {
(void) fprintf(stderr,
"%s: line %d, invalid entry -- %s\n", permfile,
lineno, line);
continue;
}
/* convert string to octal value */
mode = strtol(mode_str, &ptr, 8);
if (mode < 0 || mode > 0777 || *ptr != '\0') {
(void) fprintf(stderr,
"%s: line %d, invalid mode -- %s\n", permfile,
lineno, mode_str);
continue;
}
dev_list = strtok((char *)NULL, field_delims);
if (dev_list == NULL) {
(void) fprintf(stderr,
"%s: line %d, %s -- empty device list\n",
permfile, lineno, console);
continue;
}
device = strtok(dev_list, ":");
while (device != NULL) {
l = strlen(device);
ptr = &device[l - DIRWLDLEN];
if ((l > DIRWLDLEN) && (strcmp(ptr, DIRWILD) == 0)) {
*ptr = '\0'; /* chop off wildcard */
dir_dev_acc(device, uid, gid, mode, permfile);
} else {
/*
* change the owner/group/permission;
* nonexistent devices are ignored
*/
if (chown(device, uid, gid) == -1) {
if (errno != ENOENT) {
(void) fprintf(stderr, "%s: ",
permfile);
perror(device);
}
} else {
if ((chmod(device, mode) == -1) &&
(errno != ENOENT)) {
(void) fprintf(stderr, "%s: ",
permfile);
perror(device);
}
}
}
device = strtok((char *)NULL, ":");
}
}
(void) fclose(fp);
}
/*
* Apply owner/group/perms to all files (except "." and "..")
* in a directory.
*/
static void
dir_dev_acc(char *dir, uid_t uid, gid_t gid, mode_t mode, char *permfile)
{
DIR *dirp;
struct dirent *direntp;
char *name, path[MAX_LINELEN + MAXNAMELEN];
dirp = opendir(dir);
if (dirp == NULL)
return;
while ((direntp = readdir(dirp)) != NULL) {
name = direntp->d_name;
if ((strcmp(name, ".") == 0) || (strcmp(name, "..") == 0))
continue;
(void) sprintf(path, "%s/%s", dir, name);
if (chown(path, uid, gid) == -1) {
if (errno != ENOENT) {
(void) fprintf(stderr, "%s: ", permfile);
perror(path);
}
} else {
if ((chmod(path, mode) == -1) && (errno != ENOENT)) {
(void) fprintf(stderr, "%s: ", permfile);
perror(path);
}
}
}
(void) closedir(dirp);
}
|