Commit | Line | Data |
---|---|---|
bcf1365c | 1 | /* |
ede75793 KB |
2 | * Copyright (c) 1980, 1987, 1988 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
bcf1365c DF |
16 | */ |
17 | ||
18 | #ifndef lint | |
19 | char copyright[] = | |
ede75793 | 20 | "@(#) Copyright (c) 1980, 1987, 1988 The Regents of the University of California.\n\ |
bcf1365c | 21 | All rights reserved.\n"; |
ede75793 | 22 | #endif /* not lint */ |
bcf1365c | 23 | |
22d4760e | 24 | #ifndef lint |
9a448eee | 25 | static char sccsid[] = "@(#)login.c 5.36 (Berkeley) %G%"; |
ede75793 | 26 | #endif /* not lint */ |
22d4760e | 27 | |
88a01c09 BJ |
28 | /* |
29 | * login [ name ] | |
5dbe2745 MK |
30 | * login -h hostname (for telnetd, etc.) |
31 | * login -f name (for pre-authenticated login: datakit, xterm, etc.) | |
88a01c09 BJ |
32 | */ |
33 | ||
7a625b73 | 34 | #include <sys/param.h> |
3b8dd95e SL |
35 | #include <sys/quota.h> |
36 | #include <sys/stat.h> | |
37 | #include <sys/time.h> | |
38 | #include <sys/resource.h> | |
9479aa87 | 39 | #include <sys/file.h> |
80f91e3f | 40 | #include <sys/ioctl.h> |
3b8dd95e | 41 | |
88a01c09 BJ |
42 | #include <utmp.h> |
43 | #include <signal.h> | |
88a01c09 | 44 | #include <lastlog.h> |
22d4760e | 45 | #include <errno.h> |
9479aa87 BJ |
46 | #include <ttyent.h> |
47 | #include <syslog.h> | |
c89291e2 | 48 | #include <grp.h> |
80f91e3f | 49 | #include <pwd.h> |
ede75793 | 50 | #include <setjmp.h> |
80f91e3f KB |
51 | #include <stdio.h> |
52 | #include <strings.h> | |
fb6e7f7e | 53 | #include "pathnames.h" |
c89291e2 | 54 | |
80f91e3f | 55 | #define TTYGRPNAME "tty" /* name of group to own ttys */ |
88a01c09 | 56 | |
3b8dd95e | 57 | /* |
80f91e3f KB |
58 | * This bounds the time given to login. Not a define so it can |
59 | * be patched on machines where it's too small. | |
3b8dd95e | 60 | */ |
a9b031fe | 61 | int timeout = 300; |
86eb6c9e | 62 | |
659be7fb MK |
63 | struct passwd *pwd; |
64 | int failures; | |
65 | char term[64], *hostname, *username, *tty; | |
88a01c09 | 66 | |
659be7fb MK |
67 | struct sgttyb sgttyb; |
68 | struct tchars tc = { | |
714accc5 SL |
69 | CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK |
70 | }; | |
659be7fb | 71 | struct ltchars ltc = { |
714accc5 | 72 | CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT |
841d84b0 BJ |
73 | }; |
74 | ||
83cc7253 KB |
75 | char *months[] = |
76 | { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", | |
77 | "Sep", "Oct", "Nov", "Dec" }; | |
78 | ||
c91c86eb KB |
79 | char *months[] = |
80 | { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", | |
81 | "Sep", "Oct", "Nov", "Dec" }; | |
82 | ||
88a01c09 | 83 | main(argc, argv) |
80f91e3f KB |
84 | int argc; |
85 | char **argv; | |
88a01c09 | 86 | { |
80f91e3f KB |
87 | extern int errno, optind; |
88 | extern char *optarg, **environ; | |
c91c86eb KB |
89 | struct timeval tp; |
90 | struct tm *ttp; | |
83cc7253 KB |
91 | struct timeval tp; |
92 | struct tm *ttp; | |
80f91e3f KB |
93 | struct group *gr; |
94 | register int ch; | |
659be7fb | 95 | register char *p; |
f0c0c252 | 96 | int ask, fflag, hflag, pflag, cnt; |
80f91e3f | 97 | int quietlog, passwd_req, ioctlval, timedout(); |
659be7fb | 98 | char *domain, *salt, *envinit[1], *ttyn, *pp; |
80f91e3f | 99 | char tbuf[MAXPATHLEN + 2]; |
c91c86eb | 100 | char *ctime(), *ttyname(), *stypeof(), *crypt(), *getpass(); |
80f91e3f KB |
101 | time_t time(); |
102 | off_t lseek(); | |
103 | ||
104 | (void)signal(SIGALRM, timedout); | |
105 | (void)alarm((u_int)timeout); | |
106 | (void)signal(SIGQUIT, SIG_IGN); | |
107 | (void)signal(SIGINT, SIG_IGN); | |
108 | (void)setpriority(PRIO_PROCESS, 0, 0); | |
109 | (void)quota(Q_SETUID, 0, 0, 0); | |
110 | ||
3b8dd95e | 111 | /* |
d3737d51 | 112 | * -p is used by getty to tell login not to destroy the environment |
5dbe2745 | 113 | * -f is used to skip a second login authentication |
ee1eff74 MK |
114 | * -h is used by other servers to pass the name of the remote |
115 | * host to login so that it may be placed in utmp and wtmp | |
3b8dd95e | 116 | */ |
80f91e3f KB |
117 | (void)gethostname(tbuf, sizeof(tbuf)); |
118 | domain = index(tbuf, '.'); | |
119 | ||
ee1eff74 | 120 | fflag = hflag = pflag = 0; |
80f91e3f | 121 | passwd_req = 1; |
ee1eff74 | 122 | while ((ch = getopt(argc, argv, "fh:p")) != EOF) |
ede75793 | 123 | switch (ch) { |
80f91e3f | 124 | case 'f': |
80f91e3f | 125 | fflag = 1; |
80f91e3f KB |
126 | break; |
127 | case 'h': | |
128 | if (getuid()) { | |
129 | fprintf(stderr, | |
130 | "login: -h for super-user only.\n"); | |
b6f7cade | 131 | exit(1); |
5dbe2745 | 132 | } |
80f91e3f KB |
133 | hflag = 1; |
134 | if (domain && (p = index(optarg, '.')) && | |
abc4056e | 135 | strcasecmp(p, domain) == 0) |
80f91e3f KB |
136 | *p = 0; |
137 | hostname = optarg; | |
138 | break; | |
139 | case 'p': | |
d3737d51 | 140 | pflag = 1; |
80f91e3f | 141 | break; |
80f91e3f KB |
142 | case '?': |
143 | default: | |
ede75793 | 144 | fprintf(stderr, "usage: login [-fp] [username]\n"); |
80f91e3f | 145 | exit(1); |
d3737d51 | 146 | } |
80f91e3f KB |
147 | argc -= optind; |
148 | argv += optind; | |
f0c0c252 | 149 | if (*argv) { |
80f91e3f | 150 | username = *argv; |
659be7fb MK |
151 | ask = 0; |
152 | } else | |
f0c0c252 | 153 | ask = 1; |
80f91e3f KB |
154 | |
155 | ioctlval = 0; | |
156 | (void)ioctl(0, TIOCLSET, &ioctlval); | |
157 | (void)ioctl(0, TIOCNXCL, 0); | |
ede75793 KB |
158 | (void)fcntl(0, F_SETFL, ioctlval); |
159 | (void)ioctl(0, TIOCGETP, &sgttyb); | |
ede75793 KB |
160 | sgttyb.sg_erase = CERASE; |
161 | sgttyb.sg_kill = CKILL; | |
80f91e3f KB |
162 | (void)ioctl(0, TIOCSLTC, <c); |
163 | (void)ioctl(0, TIOCSETC, &tc); | |
ede75793 | 164 | (void)ioctl(0, TIOCSETP, &sgttyb); |
80f91e3f KB |
165 | |
166 | for (cnt = getdtablesize(); cnt > 2; cnt--) | |
167 | close(cnt); | |
168 | ||
88a01c09 | 169 | ttyn = ttyname(0); |
80f91e3f | 170 | if (ttyn == NULL || *ttyn == '\0') |
88a01c09 | 171 | ttyn = "/dev/tty??"; |
80f91e3f KB |
172 | if (tty = rindex(ttyn, '/')) |
173 | ++tty; | |
9479aa87 | 174 | else |
80f91e3f KB |
175 | tty = ttyn; |
176 | ||
076ae92c | 177 | openlog("login", LOG_ODELAY, LOG_AUTH); |
80f91e3f | 178 | |
f0c0c252 | 179 | for (cnt = 0;; ask = 1) { |
80f91e3f KB |
180 | ioctlval = 0; |
181 | (void)ioctl(0, TIOCSETD, &ioctlval); | |
182 | ||
f0c0c252 | 183 | if (ask) { |
ede75793 | 184 | fflag = 0; |
80f91e3f | 185 | getloginname(); |
ede75793 | 186 | } |
659be7fb MK |
187 | /* |
188 | * Note if trying multiple user names; | |
189 | * log failures for previous user name, | |
190 | * but don't bother logging one failure | |
191 | * for nonexistent name (mistyped username). | |
192 | */ | |
193 | if (failures && strcmp(tbuf, username)) { | |
194 | if (failures > (pwd ? 0 : 1)) | |
f0c0c252 | 195 | badlogin(tbuf); |
659be7fb | 196 | failures = 0; |
f0c0c252 | 197 | } |
659be7fb | 198 | (void)strcpy(tbuf, username); |
ede75793 KB |
199 | if (pwd = getpwnam(username)) |
200 | salt = pwd->pw_passwd; | |
201 | else | |
202 | salt = "xx"; | |
80f91e3f KB |
203 | |
204 | /* if user not super-user, check for disabled logins */ | |
ede75793 | 205 | if (pwd == NULL || pwd->pw_uid) |
80f91e3f KB |
206 | checknologin(); |
207 | ||
3b8dd95e | 208 | /* |
ede75793 KB |
209 | * Disallow automatic login to root; if not invoked by |
210 | * root, disallow if the uid's differ. | |
3b8dd95e | 211 | */ |
ede75793 | 212 | if (fflag && pwd) { |
5dbe2745 MK |
213 | int uid = getuid(); |
214 | ||
ede75793 KB |
215 | passwd_req = pwd->pw_uid == 0 || |
216 | (uid && uid != pwd->pw_uid); | |
f570e1ff | 217 | } |
80f91e3f | 218 | |
3b8dd95e | 219 | /* |
ee1eff74 | 220 | * If no pre-authentication and a password exists |
80f91e3f | 221 | * for this user, prompt for one and verify it. |
3b8dd95e | 222 | */ |
9d6a5a69 | 223 | if (!passwd_req || (pwd && !*pwd->pw_passwd)) |
80f91e3f KB |
224 | break; |
225 | ||
226 | setpriority(PRIO_PROCESS, 0, -4); | |
9d6a5a69 KF |
227 | pp = getpass("Password:"); |
228 | p = crypt(pp, salt); | |
80f91e3f | 229 | setpriority(PRIO_PROCESS, 0, 0); |
9d6a5a69 | 230 | |
9d6a5a69 | 231 | (void) bzero(pp, strlen(pp)); |
ede75793 | 232 | if (pwd && !strcmp(p, pwd->pw_passwd)) |
80f91e3f KB |
233 | break; |
234 | ||
235 | printf("Login incorrect\n"); | |
659be7fb | 236 | failures++; |
f0c0c252 KB |
237 | /* we allow 10 tries, but after 3 we start backing off */ |
238 | if (++cnt > 3) { | |
239 | if (cnt >= 10) { | |
240 | badlogin(username); | |
241 | (void)ioctl(0, TIOCHPCL, (struct sgttyb *)NULL); | |
242 | sleepexit(1); | |
243 | } | |
244 | sleep((u_int)((cnt - 3) * 5)); | |
f570e1ff | 245 | } |
80f91e3f | 246 | } |
88a01c09 | 247 | |
80f91e3f KB |
248 | /* committed to login -- turn off timeout */ |
249 | (void)alarm((u_int)0); | |
250 | ||
251 | /* | |
252 | * If valid so far and root is logging in, see if root logins on | |
253 | * this terminal are permitted. | |
254 | */ | |
659be7fb | 255 | if (pwd->pw_uid == 0 && !rootterm(tty)) { |
80f91e3f | 256 | if (hostname) |
659be7fb MK |
257 | syslog(LOG_NOTICE, "ROOT LOGIN REFUSED FROM %s", |
258 | hostname); | |
22d4760e | 259 | else |
659be7fb | 260 | syslog(LOG_NOTICE, "ROOT LOGIN REFUSED ON %s", tty); |
80f91e3f KB |
261 | printf("Login incorrect\n"); |
262 | sleepexit(1); | |
22d4760e | 263 | } |
80f91e3f | 264 | |
80f91e3f KB |
265 | if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0 && errno != EINVAL) { |
266 | switch(errno) { | |
267 | case EUSERS: | |
268 | fprintf(stderr, | |
269 | "Too many users logged on already.\nTry again later.\n"); | |
270 | break; | |
271 | case EPROCLIM: | |
272 | fprintf(stderr, | |
273 | "You have too many processes running.\n"); | |
274 | break; | |
275 | default: | |
276 | perror("quota (Q_SETUID)"); | |
f570e1ff | 277 | } |
80f91e3f KB |
278 | sleepexit(0); |
279 | } | |
280 | ||
ede75793 KB |
281 | if (chdir(pwd->pw_dir) < 0) { |
282 | printf("No directory %s!\n", pwd->pw_dir); | |
283 | if (chdir("/")) | |
284 | exit(0); | |
285 | pwd->pw_dir = "/"; | |
286 | printf("Logging in with home = \"/\".\n"); | |
287 | } | |
288 | ||
83cc7253 KB |
289 | #define TWOWEEKS (14*24*60*60) |
290 | if (pwd->pw_change || pwd->pw_expire) | |
291 | (void)gettimeofday(&tp, (struct timezone *)NULL); | |
292 | if (pwd->pw_change) | |
293 | if (tp.tv_sec >= pwd->pw_change) { | |
294 | printf("Sorry -- your password has expired.\n"); | |
295 | sleepexit(1); | |
296 | } | |
297 | else if (tp.tv_sec - pwd->pw_change < TWOWEEKS) { | |
298 | ttp = localtime(&pwd->pw_change); | |
299 | printf("Warning: your password expires on %s %d, 19%d\n", | |
300 | months[ttp->tm_mon], ttp->tm_mday, ttp->tm_year); | |
301 | } | |
302 | if (pwd->pw_expire) | |
303 | if (tp.tv_sec >= pwd->pw_expire) { | |
304 | printf("Sorry -- your account has expired.\n"); | |
305 | sleepexit(1); | |
306 | } | |
307 | else if (tp.tv_sec - pwd->pw_expire < TWOWEEKS) { | |
308 | ttp = localtime(&pwd->pw_expire); | |
309 | printf("Warning: your account expires on %s %d, 19%d\n", | |
310 | months[ttp->tm_mon], ttp->tm_mday, ttp->tm_year); | |
311 | } | |
312 | ||
c91c86eb KB |
313 | #define TWOWEEKS (14*24*60*60) |
314 | if (pwd->pw_change || pwd->pw_expire) | |
315 | (void)gettimeofday(&tp, (struct timezone *)NULL); | |
316 | if (pwd->pw_change) | |
317 | if (tp.tv_sec >= pwd->pw_change) { | |
318 | printf("Sorry -- your password has expired.\n"); | |
319 | sleepexit(1); | |
320 | } | |
321 | else if (tp.tv_sec - pwd->pw_change < TWOWEEKS) { | |
322 | ttp = localtime(&pwd->pw_change); | |
323 | printf("Warning: your password expires on %s %d, 19%d\n", | |
324 | months[ttp->tm_mon], ttp->tm_mday, ttp->tm_year); | |
325 | } | |
326 | if (pwd->pw_expire) | |
327 | if (tp.tv_sec >= pwd->pw_expire) { | |
328 | printf("Sorry -- your account has expired.\n"); | |
329 | sleepexit(1); | |
330 | } | |
331 | else if (tp.tv_sec - pwd->pw_expire < TWOWEEKS) { | |
332 | ttp = localtime(&pwd->pw_expire); | |
333 | printf("Warning: your account expires on %s %d, 19%d\n", | |
334 | months[ttp->tm_mon], ttp->tm_mday, ttp->tm_year); | |
335 | } | |
336 | ||
ede75793 | 337 | /* nothing else left to fail -- really log in */ |
80f91e3f KB |
338 | { |
339 | struct utmp utmp; | |
340 | ||
659be7fb | 341 | bzero((char *)&utmp, sizeof(utmp)); |
80f91e3f KB |
342 | (void)time(&utmp.ut_time); |
343 | strncpy(utmp.ut_name, username, sizeof(utmp.ut_name)); | |
a829b33b KB |
344 | if (hostname) |
345 | strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host)); | |
80f91e3f KB |
346 | strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); |
347 | login(&utmp); | |
f570e1ff | 348 | } |
80f91e3f | 349 | |
fb6e7f7e | 350 | quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0; |
f0c0c252 | 351 | dolastlog(quietlog); |
80f91e3f | 352 | |
ee1eff74 | 353 | if (!hflag) { /* XXX */ |
80f91e3f KB |
354 | static struct winsize win = { 0, 0, 0, 0 }; |
355 | ||
356 | (void)ioctl(0, TIOCSWINSZ, &win); | |
357 | } | |
358 | ||
359 | (void)chown(ttyn, pwd->pw_uid, | |
360 | (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid); | |
361 | (void)chmod(ttyn, 0620); | |
362 | (void)setgid(pwd->pw_gid); | |
363 | ||
364 | initgroups(username, pwd->pw_gid); | |
365 | ||
22d4760e | 366 | quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0); |
80f91e3f | 367 | (void)setuid(pwd->pw_uid); |
3daca631 | 368 | |
ede75793 | 369 | if (*pwd->pw_shell == '\0') |
fb6e7f7e | 370 | pwd->pw_shell = _PATH_BSHELL; |
ede75793 | 371 | /* turn on new line discipline for the csh */ |
9a448eee | 372 | else if (!strcmp(pwd->pw_shell, _PATH_CSHELL)) { |
ede75793 KB |
373 | ioctlval = NTTYDISC; |
374 | (void)ioctl(0, TIOCSETD, &ioctlval); | |
375 | } | |
376 | ||
80f91e3f | 377 | /* destroy environment unless user has requested preservation */ |
d3737d51 SL |
378 | if (!pflag) |
379 | environ = envinit; | |
ede75793 KB |
380 | (void)setenv("HOME", pwd->pw_dir, 1); |
381 | (void)setenv("SHELL", pwd->pw_shell, 1); | |
d3737d51 | 382 | if (term[0] == '\0') |
659be7fb | 383 | strncpy(term, stypeof(tty), sizeof(term)); |
ede75793 KB |
384 | (void)setenv("TERM", term, 0); |
385 | (void)setenv("USER", pwd->pw_name, 1); | |
386 | (void)setenv("PATH", "/usr/ucb:/bin:/usr/bin:", 0); | |
d3737d51 | 387 | |
9479aa87 | 388 | if (tty[sizeof("tty")-1] == 'd') |
d3737d51 SL |
389 | syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name); |
390 | if (pwd->pw_uid == 0) | |
80f91e3f | 391 | if (hostname) |
659be7fb | 392 | syslog(LOG_NOTICE, "ROOT LOGIN ON %s FROM %s", |
f0c0c252 | 393 | tty, hostname); |
df9d9536 | 394 | else |
659be7fb | 395 | syslog(LOG_NOTICE, "ROOT LOGIN ON %s", tty); |
80f91e3f | 396 | |
4f8d3876 | 397 | if (!quietlog) { |
6821e9c5 | 398 | struct stat st; |
d3737d51 | 399 | |
80f91e3f | 400 | motd(); |
fb6e7f7e | 401 | (void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name); |
80f91e3f | 402 | if (stat(tbuf, &st) == 0 && st.st_size != 0) |
6821e9c5 | 403 | printf("You have %smail.\n", |
80f91e3f | 404 | (st.st_mtime > st.st_atime) ? "new " : ""); |
f570e1ff | 405 | } |
80f91e3f KB |
406 | |
407 | (void)signal(SIGALRM, SIG_DFL); | |
408 | (void)signal(SIGQUIT, SIG_DFL); | |
409 | (void)signal(SIGINT, SIG_DFL); | |
410 | (void)signal(SIGTSTP, SIG_IGN); | |
411 | ||
412 | tbuf[0] = '-'; | |
413 | strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ? | |
414 | p + 1 : pwd->pw_shell); | |
415 | execlp(pwd->pw_shell, tbuf, 0); | |
ede75793 | 416 | fprintf(stderr, "login: no shell: "); |
f570e1ff | 417 | perror(pwd->pw_shell); |
88a01c09 BJ |
418 | exit(0); |
419 | } | |
420 | ||
80f91e3f | 421 | getloginname() |
3b8dd95e | 422 | { |
80f91e3f KB |
423 | register int ch; |
424 | register char *p; | |
425 | static char nbuf[UT_NAMESIZE + 1]; | |
3b8dd95e | 426 | |
80f91e3f | 427 | for (;;) { |
5a786176 | 428 | printf("login: "); |
ede75793 | 429 | for (p = nbuf; (ch = getchar()) != '\n'; ) { |
f0c0c252 KB |
430 | if (ch == EOF) { |
431 | badlogin(username); | |
3b8dd95e | 432 | exit(0); |
f0c0c252 | 433 | } |
ede75793 | 434 | if (p < nbuf + UT_NAMESIZE) |
80f91e3f | 435 | *p++ = ch; |
3b8dd95e | 436 | } |
80f91e3f KB |
437 | if (p > nbuf) |
438 | if (nbuf[0] == '-') | |
439 | fprintf(stderr, | |
440 | "login names may not start with '-'.\n"); | |
441 | else { | |
442 | *p = '\0'; | |
443 | username = nbuf; | |
ede75793 | 444 | break; |
80f91e3f | 445 | } |
3b8dd95e | 446 | } |
3b8dd95e SL |
447 | } |
448 | ||
449 | timedout() | |
450 | { | |
80f91e3f | 451 | fprintf(stderr, "Login timed out after %d seconds\n", timeout); |
3b8dd95e SL |
452 | exit(0); |
453 | } | |
454 | ||
659be7fb MK |
455 | rootterm(ttyn) |
456 | char *ttyn; | |
88a01c09 | 457 | { |
80f91e3f | 458 | struct ttyent *t; |
1886582e | 459 | |
659be7fb | 460 | return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE); |
88a01c09 BJ |
461 | } |
462 | ||
ede75793 KB |
463 | jmp_buf motdinterrupt; |
464 | ||
80f91e3f | 465 | motd() |
f570e1ff | 466 | { |
ede75793 | 467 | register int fd, nchars; |
80f91e3f | 468 | int (*oldint)(), sigint(); |
ede75793 | 469 | char tbuf[8192]; |
80f91e3f | 470 | |
fb6e7f7e | 471 | if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0) |
80f91e3f KB |
472 | return; |
473 | oldint = signal(SIGINT, sigint); | |
ede75793 KB |
474 | if (setjmp(motdinterrupt) == 0) |
475 | while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) | |
476 | (void)write(fileno(stdout), tbuf, nchars); | |
80f91e3f | 477 | (void)signal(SIGINT, oldint); |
ede75793 | 478 | (void)close(fd); |
80f91e3f | 479 | } |
9479aa87 | 480 | |
80f91e3f KB |
481 | sigint() |
482 | { | |
ede75793 | 483 | longjmp(motdinterrupt, 1); |
80f91e3f KB |
484 | } |
485 | ||
486 | checknologin() | |
487 | { | |
488 | register int fd, nchars; | |
ede75793 | 489 | char tbuf[8192]; |
80f91e3f | 490 | |
fb6e7f7e | 491 | if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) { |
80f91e3f KB |
492 | while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) |
493 | (void)write(fileno(stdout), tbuf, nchars); | |
494 | sleepexit(0); | |
f570e1ff | 495 | } |
f570e1ff BJ |
496 | } |
497 | ||
f0c0c252 | 498 | dolastlog(quiet) |
80f91e3f | 499 | int quiet; |
88a01c09 | 500 | { |
80f91e3f KB |
501 | struct lastlog ll; |
502 | int fd; | |
c91c86eb | 503 | char *ctime(); |
83cc7253 | 504 | char *ctime(); |
80f91e3f | 505 | |
fb6e7f7e | 506 | if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) { |
ede75793 | 507 | (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); |
80f91e3f KB |
508 | if (!quiet) { |
509 | if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) && | |
510 | ll.ll_time != 0) { | |
511 | printf("Last login: %.*s ", | |
512 | 24-5, (char *)ctime(&ll.ll_time)); | |
513 | if (*ll.ll_host != '\0') | |
514 | printf("from %.*s\n", | |
515 | sizeof(ll.ll_host), ll.ll_host); | |
516 | else | |
517 | printf("on %.*s\n", | |
518 | sizeof(ll.ll_line), ll.ll_line); | |
519 | } | |
ede75793 | 520 | (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); |
80f91e3f | 521 | } |
659be7fb | 522 | bzero((char *)&ll, sizeof(ll)); |
80f91e3f KB |
523 | (void)time(&ll.ll_time); |
524 | strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); | |
a829b33b KB |
525 | if (hostname) |
526 | strncpy(ll.ll_host, hostname, sizeof(ll.ll_host)); | |
80f91e3f KB |
527 | (void)write(fd, (char *)&ll, sizeof(ll)); |
528 | (void)close(fd); | |
88a01c09 | 529 | } |
88a01c09 BJ |
530 | } |
531 | ||
f0c0c252 KB |
532 | badlogin(name) |
533 | char *name; | |
534 | { | |
659be7fb | 535 | if (failures == 0) |
f0c0c252 KB |
536 | return; |
537 | if (hostname) | |
659be7fb MK |
538 | syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s, %s", |
539 | failures, failures > 1 ? "S" : "", hostname, name); | |
f0c0c252 | 540 | else |
659be7fb MK |
541 | syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s, %s", |
542 | failures, failures > 1 ? "S" : "", tty, name); | |
f0c0c252 KB |
543 | } |
544 | ||
f570e1ff | 545 | #undef UNKNOWN |
80f91e3f | 546 | #define UNKNOWN "su" |
88a01c09 BJ |
547 | |
548 | char * | |
659be7fb MK |
549 | stypeof(ttyid) |
550 | char *ttyid; | |
88a01c09 | 551 | { |
80f91e3f | 552 | struct ttyent *t; |
88a01c09 | 553 | |
659be7fb | 554 | return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN); |
88a01c09 | 555 | } |
86eb6c9e BJ |
556 | |
557 | getstr(buf, cnt, err) | |
80f91e3f | 558 | char *buf, *err; |
86eb6c9e | 559 | int cnt; |
86eb6c9e | 560 | { |
80f91e3f | 561 | char ch; |
86eb6c9e BJ |
562 | |
563 | do { | |
80f91e3f | 564 | if (read(0, &ch, sizeof(ch)) != sizeof(ch)) |
86eb6c9e BJ |
565 | exit(1); |
566 | if (--cnt < 0) { | |
80f91e3f KB |
567 | fprintf(stderr, "%s too long\r\n", err); |
568 | sleepexit(1); | |
86eb6c9e | 569 | } |
80f91e3f KB |
570 | *buf++ = ch; |
571 | } while (ch); | |
86eb6c9e | 572 | } |
4f8d3876 | 573 | |
80f91e3f KB |
574 | sleepexit(eval) |
575 | int eval; | |
c89291e2 | 576 | { |
80f91e3f KB |
577 | sleep((u_int)5); |
578 | exit(eval); | |
c89291e2 | 579 | } |