date and time created 90/06/19 19:22:39 by kfall
[unix-history] / usr / src / usr.bin / login / login.c
CommitLineData
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
19char 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
9e76b9f1 25static char sccsid[] = "@(#)login.c 5.55 (Berkeley) %G%";
ede75793 26#endif /* not lint */
22d4760e 27
88a01c09
BJ
28/*
29 * login [ name ]
d0faf133 30 * login -r hostname (for rlogind)
5dbe2745
MK
31 * login -h hostname (for telnetd, etc.)
32 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
88a01c09
BJ
33 */
34
7a625b73 35#include <sys/param.h>
3b8dd95e
SL
36#include <sys/stat.h>
37#include <sys/time.h>
38#include <sys/resource.h>
9479aa87 39#include <sys/file.h>
2d4a74b8 40#include <sgtty.h>
3b8dd95e 41
88a01c09
BJ
42#include <utmp.h>
43#include <signal.h>
22d4760e 44#include <errno.h>
9479aa87
BJ
45#include <ttyent.h>
46#include <syslog.h>
c89291e2 47#include <grp.h>
80f91e3f 48#include <pwd.h>
ede75793 49#include <setjmp.h>
80f91e3f 50#include <stdio.h>
38dde0cd 51#include <string.h>
0e08a2b3 52#include <tzfile.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 61int timeout = 300;
86eb6c9e 62
659be7fb
MK
63struct passwd *pwd;
64int failures;
657e5a3c 65char term[64], *envinit[1], *hostname, *username, *tty;
88a01c09 66
659be7fb
MK
67struct sgttyb sgttyb;
68struct tchars tc = {
714accc5
SL
69 CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
70};
659be7fb 71struct ltchars ltc = {
714accc5 72 CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
841d84b0
BJ
73};
74
83cc7253
KB
75char *months[] =
76 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
77 "Sep", "Oct", "Nov", "Dec" };
78
c91c86eb
KB
79char *months[] =
80 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
81 "Sep", "Oct", "Nov", "Dec" };
82
88a01c09 83main(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;
d0faf133 96 int ask, fflag, hflag, pflag, rflag, cnt;
80f91e3f 97 int quietlog, passwd_req, ioctlval, timedout();
657e5a3c 98 char *domain, *salt, *ttyn, *pp;
a339b173 99 char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10];
e2a7a526 100 char localhost[MAXHOSTNAMELEN];
c91c86eb 101 char *ctime(), *ttyname(), *stypeof(), *crypt(), *getpass();
80f91e3f
KB
102 time_t time();
103 off_t lseek();
104
105 (void)signal(SIGALRM, timedout);
106 (void)alarm((u_int)timeout);
107 (void)signal(SIGQUIT, SIG_IGN);
108 (void)signal(SIGINT, SIG_IGN);
109 (void)setpriority(PRIO_PROCESS, 0, 0);
80f91e3f 110
9e76b9f1
KB
111 openlog("login", LOG_ODELAY, LOG_AUTH);
112
3b8dd95e 113 /*
d3737d51 114 * -p is used by getty to tell login not to destroy the environment
d0faf133 115 * -r is used by rlogind to cause the autologin protocol;
5dbe2745 116 * -f is used to skip a second login authentication
ee1eff74
MK
117 * -h is used by other servers to pass the name of the remote
118 * host to login so that it may be placed in utmp and wtmp
3b8dd95e 119 */
e2a7a526
KF
120 domain = NULL;
121 if (gethostname(localhost, sizeof(localhost)) < 0)
122 syslog(LOG_ERR, "couldn't get local hostname: %m");
123 else
124 domain = index(localhost, '.');
80f91e3f 125
d0faf133 126 fflag = hflag = pflag = rflag = 0;
80f91e3f 127 passwd_req = 1;
2d4a74b8 128 uid = getuid();
d0faf133 129 while ((ch = getopt(argc, argv, "fh:pr:")) != EOF)
ede75793 130 switch (ch) {
80f91e3f 131 case 'f':
d0faf133
KB
132 if (rflag) {
133 fprintf(stderr,
134 "login: only one of -r and -f allowed.\n");
135 exit(1);
136 }
80f91e3f 137 fflag = 1;
80f91e3f
KB
138 break;
139 case 'h':
2d4a74b8 140 if (uid) {
0e08a2b3 141 (void)fprintf(stderr,
80f91e3f 142 "login: -h for super-user only.\n");
b6f7cade 143 exit(1);
5dbe2745 144 }
d0faf133
KB
145 if (rflag) {
146 fprintf(stderr,
147 "login: only one of -r and -h allowed.\n");
148 exit(1);
149 }
80f91e3f
KB
150 hflag = 1;
151 if (domain && (p = index(optarg, '.')) &&
abc4056e 152 strcasecmp(p, domain) == 0)
80f91e3f
KB
153 *p = 0;
154 hostname = optarg;
155 break;
156 case 'p':
d3737d51 157 pflag = 1;
80f91e3f 158 break;
d0faf133
KB
159 case 'r':
160 if (hflag || fflag) {
161 fprintf(stderr,
162 "login: -f and -h not allowed with -r.\n");
163 exit(1);
164 }
165 if (getuid()) {
166 fprintf(stderr,
167 "login: -r for super-user only.\n");
168 exit(1);
169 }
170 /* "-r hostname" must be last args */
171 if (optind != argc) {
172 fprintf(stderr, "Syntax error.\n");
173 exit(1);
174 }
175 rflag = 1;
176 passwd_req = (doremotelogin(optarg) == -1);
177 if (domain && (p = index(optarg, '.')) &&
178 !strcmp(p, domain))
179 *p = '\0';
180 hostname = optarg;
181 break;
80f91e3f
KB
182 case '?':
183 default:
2d4a74b8
KF
184 if (!uid)
185 syslog(LOG_ERR, "invalid flag %c", ch);
0e08a2b3
KB
186 (void)fprintf(stderr,
187 "usage: login [-fp] [username]\n");
80f91e3f 188 exit(1);
d3737d51 189 }
80f91e3f
KB
190 argc -= optind;
191 argv += optind;
f0c0c252 192 if (*argv) {
80f91e3f 193 username = *argv;
659be7fb
MK
194 ask = 0;
195 } else
f0c0c252 196 ask = 1;
d0faf133
KB
197 if (rflag)
198 ask = 0;
80f91e3f
KB
199
200 ioctlval = 0;
201 (void)ioctl(0, TIOCLSET, &ioctlval);
202 (void)ioctl(0, TIOCNXCL, 0);
ede75793
KB
203 (void)fcntl(0, F_SETFL, ioctlval);
204 (void)ioctl(0, TIOCGETP, &sgttyb);
d0faf133
KB
205
206 /*
207 * If talking to an rlogin process, propagate the terminal type and
208 * baud rate across the network.
209 */
210 if (rflag)
211 doremoteterm(&sgttyb);
ede75793
KB
212 sgttyb.sg_erase = CERASE;
213 sgttyb.sg_kill = CKILL;
80f91e3f
KB
214 (void)ioctl(0, TIOCSLTC, &ltc);
215 (void)ioctl(0, TIOCSETC, &tc);
ede75793 216 (void)ioctl(0, TIOCSETP, &sgttyb);
80f91e3f
KB
217
218 for (cnt = getdtablesize(); cnt > 2; cnt--)
219 close(cnt);
220
88a01c09 221 ttyn = ttyname(0);
a339b173
KB
222 if (ttyn == NULL || *ttyn == '\0') {
223 (void)sprintf(tname, "%s??", _PATH_TTY);
224 ttyn = tname;
225 }
80f91e3f
KB
226 if (tty = rindex(ttyn, '/'))
227 ++tty;
9479aa87 228 else
80f91e3f
KB
229 tty = ttyn;
230
f0c0c252 231 for (cnt = 0;; ask = 1) {
7e845b92 232 ioctlval = TTYDISC;
80f91e3f
KB
233 (void)ioctl(0, TIOCSETD, &ioctlval);
234
f0c0c252 235 if (ask) {
ede75793 236 fflag = 0;
80f91e3f 237 getloginname();
ede75793 238 }
659be7fb 239 /*
2d4a74b8
KF
240 * Note if trying multiple user names; log failures for
241 * previous user name, but don't bother logging one failure
659be7fb
MK
242 * for nonexistent name (mistyped username).
243 */
244 if (failures && strcmp(tbuf, username)) {
245 if (failures > (pwd ? 0 : 1))
f0c0c252 246 badlogin(tbuf);
659be7fb 247 failures = 0;
f0c0c252 248 }
659be7fb 249 (void)strcpy(tbuf, username);
ede75793
KB
250 if (pwd = getpwnam(username))
251 salt = pwd->pw_passwd;
252 else
253 salt = "xx";
80f91e3f
KB
254
255 /* if user not super-user, check for disabled logins */
ede75793 256 if (pwd == NULL || pwd->pw_uid)
80f91e3f
KB
257 checknologin();
258
3b8dd95e 259 /*
ede75793
KB
260 * Disallow automatic login to root; if not invoked by
261 * root, disallow if the uid's differ.
3b8dd95e 262 */
ede75793 263 if (fflag && pwd) {
b32e4ad7
KF
264 passwd_req =
265#ifndef KERBEROS
266 pwd->pw_uid == 0 ||
267#endif
ede75793 268 (uid && uid != pwd->pw_uid);
f570e1ff 269 }
80f91e3f 270
3b8dd95e 271 /*
ee1eff74 272 * If no pre-authentication and a password exists
80f91e3f 273 * for this user, prompt for one and verify it.
3b8dd95e 274 */
9d6a5a69 275 if (!passwd_req || (pwd && !*pwd->pw_passwd))
80f91e3f
KB
276 break;
277
2d4a74b8
KF
278 /*
279 * If trying to log in as root, but with insecure terminal,
280 * refuse the login attempt.
281 */
282 if (pwd->pw_uid == 0 && !rootterm(tty)) {
283 (void)fprintf(stderr,
284 "%s login refused on this terminal.\n",
285 pwd->pw_name);
286 if (hostname)
287 syslog(LOG_NOTICE,
288 "LOGIN %s REFUSED FROM %s ON TTY %s",
289 pwd->pw_name, hostname, tty);
290 else
291 syslog(LOG_NOTICE,
292 "LOGIN %s REFUSED ON TTY %s",
293 pwd->pw_name, tty);
294 continue;
295 }
296
80f91e3f 297 setpriority(PRIO_PROCESS, 0, -4);
9d6a5a69
KF
298 pp = getpass("Password:");
299 p = crypt(pp, salt);
80f91e3f 300 setpriority(PRIO_PROCESS, 0, 0);
9d6a5a69 301
9d6a5a69 302 (void) bzero(pp, strlen(pp));
ede75793 303 if (pwd && !strcmp(p, pwd->pw_passwd))
80f91e3f
KB
304 break;
305
0e08a2b3 306 (void)printf("Login incorrect\n");
659be7fb 307 failures++;
f0c0c252
KB
308 /* we allow 10 tries, but after 3 we start backing off */
309 if (++cnt > 3) {
310 if (cnt >= 10) {
311 badlogin(username);
312 (void)ioctl(0, TIOCHPCL, (struct sgttyb *)NULL);
313 sleepexit(1);
314 }
315 sleep((u_int)((cnt - 3) * 5));
f570e1ff 316 }
80f91e3f 317 }
88a01c09 318
80f91e3f
KB
319 /* committed to login -- turn off timeout */
320 (void)alarm((u_int)0);
321
a339b173
KB
322 /* paranoia... */
323 endpwent();
324
ede75793 325 if (chdir(pwd->pw_dir) < 0) {
0e08a2b3 326 (void)printf("No directory %s!\n", pwd->pw_dir);
ede75793
KB
327 if (chdir("/"))
328 exit(0);
329 pwd->pw_dir = "/";
0e08a2b3 330 (void)printf("Logging in with home = \"/\".\n");
ede75793
KB
331 }
332
83cc7253
KB
333#define TWOWEEKS (14*24*60*60)
334 if (pwd->pw_change || pwd->pw_expire)
335 (void)gettimeofday(&tp, (struct timezone *)NULL);
336 if (pwd->pw_change)
337 if (tp.tv_sec >= pwd->pw_change) {
338 printf("Sorry -- your password has expired.\n");
339 sleepexit(1);
340 }
341 else if (tp.tv_sec - pwd->pw_change < TWOWEEKS) {
342 ttp = localtime(&pwd->pw_change);
343 printf("Warning: your password expires on %s %d, 19%d\n",
344 months[ttp->tm_mon], ttp->tm_mday, ttp->tm_year);
345 }
346 if (pwd->pw_expire)
347 if (tp.tv_sec >= pwd->pw_expire) {
348 printf("Sorry -- your account has expired.\n");
349 sleepexit(1);
350 }
351 else if (tp.tv_sec - pwd->pw_expire < TWOWEEKS) {
352 ttp = localtime(&pwd->pw_expire);
353 printf("Warning: your account expires on %s %d, 19%d\n",
354 months[ttp->tm_mon], ttp->tm_mday, ttp->tm_year);
355 }
356
c91c86eb
KB
357 if (pwd->pw_change || pwd->pw_expire)
358 (void)gettimeofday(&tp, (struct timezone *)NULL);
359 if (pwd->pw_change)
360 if (tp.tv_sec >= pwd->pw_change) {
0e08a2b3 361 (void)printf("Sorry -- your password has expired.\n");
c91c86eb
KB
362 sleepexit(1);
363 }
50112e48
KB
364 else if (pwd->pw_change - tp.tv_sec <
365 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) {
c91c86eb 366 ttp = localtime(&pwd->pw_change);
0e08a2b3 367 (void)printf("Warning: your password expires on %s %d, %d\n",
50112e48
KB
368 months[ttp->tm_mon], ttp->tm_mday,
369 TM_YEAR_BASE + ttp->tm_year);
c91c86eb
KB
370 }
371 if (pwd->pw_expire)
372 if (tp.tv_sec >= pwd->pw_expire) {
0e08a2b3 373 (void)printf("Sorry -- your account has expired.\n");
c91c86eb
KB
374 sleepexit(1);
375 }
50112e48
KB
376 else if (pwd->pw_expire - tp.tv_sec <
377 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) {
c91c86eb 378 ttp = localtime(&pwd->pw_expire);
0e08a2b3 379 (void)printf("Warning: your account expires on %s %d, %d\n",
50112e48
KB
380 months[ttp->tm_mon], ttp->tm_mday,
381 TM_YEAR_BASE + ttp->tm_year);
c91c86eb
KB
382 }
383
ede75793 384 /* nothing else left to fail -- really log in */
80f91e3f
KB
385 {
386 struct utmp utmp;
387
659be7fb 388 bzero((char *)&utmp, sizeof(utmp));
80f91e3f
KB
389 (void)time(&utmp.ut_time);
390 strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
a829b33b
KB
391 if (hostname)
392 strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
80f91e3f
KB
393 strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
394 login(&utmp);
f570e1ff 395 }
80f91e3f 396
f0c0c252 397 dolastlog(quietlog);
80f91e3f 398
d0faf133 399 if (!hflag && !rflag) { /* XXX */
80f91e3f
KB
400 static struct winsize win = { 0, 0, 0, 0 };
401
402 (void)ioctl(0, TIOCSWINSZ, &win);
403 }
404
405 (void)chown(ttyn, pwd->pw_uid,
406 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
407 (void)chmod(ttyn, 0620);
408 (void)setgid(pwd->pw_gid);
409
410 initgroups(username, pwd->pw_gid);
411
ede75793 412 if (*pwd->pw_shell == '\0')
fb6e7f7e 413 pwd->pw_shell = _PATH_BSHELL;
ede75793 414
80f91e3f 415 /* destroy environment unless user has requested preservation */
d3737d51
SL
416 if (!pflag)
417 environ = envinit;
ede75793
KB
418 (void)setenv("HOME", pwd->pw_dir, 1);
419 (void)setenv("SHELL", pwd->pw_shell, 1);
d3737d51 420 if (term[0] == '\0')
659be7fb 421 strncpy(term, stypeof(tty), sizeof(term));
ede75793
KB
422 (void)setenv("TERM", term, 0);
423 (void)setenv("USER", pwd->pw_name, 1);
c502712b 424 (void)setenv("PATH", _PATH_DEFPATH, 0);
d3737d51 425
9479aa87 426 if (tty[sizeof("tty")-1] == 'd')
d3737d51
SL
427 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
428 if (pwd->pw_uid == 0)
80f91e3f 429 if (hostname)
659be7fb 430 syslog(LOG_NOTICE, "ROOT LOGIN ON %s FROM %s",
f0c0c252 431 tty, hostname);
df9d9536 432 else
659be7fb 433 syslog(LOG_NOTICE, "ROOT LOGIN ON %s", tty);
80f91e3f 434
4f8d3876 435 if (!quietlog) {
6821e9c5 436 struct stat st;
d3737d51 437
80f91e3f 438 motd();
fb6e7f7e 439 (void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
80f91e3f 440 if (stat(tbuf, &st) == 0 && st.st_size != 0)
0e08a2b3 441 (void)printf("You have %smail.\n",
80f91e3f 442 (st.st_mtime > st.st_atime) ? "new " : "");
f570e1ff 443 }
80f91e3f
KB
444
445 (void)signal(SIGALRM, SIG_DFL);
446 (void)signal(SIGQUIT, SIG_DFL);
447 (void)signal(SIGINT, SIG_DFL);
448 (void)signal(SIGTSTP, SIG_IGN);
449
450 tbuf[0] = '-';
451 strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
452 p + 1 : pwd->pw_shell);
0e08a2b3 453
3d28f6dd
KS
454 if (setlogin(pwd->pw_name) < 0)
455 syslog(LOG_ERR, "setlogin() failure: %m");
a339b173 456
0e08a2b3
KB
457 /* discard permissions last so can't get killed and drop core */
458 (void)setuid(pwd->pw_uid);
459
80f91e3f 460 execlp(pwd->pw_shell, tbuf, 0);
0e08a2b3 461 (void)fprintf(stderr, "login: no shell: %s.\n", strerror(errno));
88a01c09
BJ
462 exit(0);
463}
464
80f91e3f 465getloginname()
3b8dd95e 466{
80f91e3f
KB
467 register int ch;
468 register char *p;
469 static char nbuf[UT_NAMESIZE + 1];
3b8dd95e 470
80f91e3f 471 for (;;) {
0e08a2b3 472 (void)printf("login: ");
ede75793 473 for (p = nbuf; (ch = getchar()) != '\n'; ) {
f0c0c252
KB
474 if (ch == EOF) {
475 badlogin(username);
3b8dd95e 476 exit(0);
f0c0c252 477 }
ede75793 478 if (p < nbuf + UT_NAMESIZE)
80f91e3f 479 *p++ = ch;
3b8dd95e 480 }
80f91e3f
KB
481 if (p > nbuf)
482 if (nbuf[0] == '-')
0e08a2b3 483 (void)fprintf(stderr,
80f91e3f
KB
484 "login names may not start with '-'.\n");
485 else {
486 *p = '\0';
487 username = nbuf;
ede75793 488 break;
80f91e3f 489 }
3b8dd95e 490 }
3b8dd95e
SL
491}
492
493timedout()
494{
0e08a2b3 495 (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout);
3b8dd95e
SL
496 exit(0);
497}
498
659be7fb
MK
499rootterm(ttyn)
500 char *ttyn;
88a01c09 501{
80f91e3f 502 struct ttyent *t;
1886582e 503
659be7fb 504 return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE);
88a01c09
BJ
505}
506
ede75793
KB
507jmp_buf motdinterrupt;
508
80f91e3f 509motd()
f570e1ff 510{
ede75793 511 register int fd, nchars;
b32e4ad7
KF
512 sig_t oldint;
513 int sigint();
ede75793 514 char tbuf[8192];
80f91e3f 515
fb6e7f7e 516 if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0)
80f91e3f
KB
517 return;
518 oldint = signal(SIGINT, sigint);
ede75793
KB
519 if (setjmp(motdinterrupt) == 0)
520 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
521 (void)write(fileno(stdout), tbuf, nchars);
80f91e3f 522 (void)signal(SIGINT, oldint);
ede75793 523 (void)close(fd);
80f91e3f 524}
9479aa87 525
80f91e3f
KB
526sigint()
527{
ede75793 528 longjmp(motdinterrupt, 1);
80f91e3f
KB
529}
530
531checknologin()
532{
533 register int fd, nchars;
ede75793 534 char tbuf[8192];
80f91e3f 535
fb6e7f7e 536 if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) {
80f91e3f
KB
537 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
538 (void)write(fileno(stdout), tbuf, nchars);
539 sleepexit(0);
f570e1ff 540 }
f570e1ff
BJ
541}
542
f0c0c252 543dolastlog(quiet)
80f91e3f 544 int quiet;
88a01c09 545{
80f91e3f
KB
546 struct lastlog ll;
547 int fd;
c91c86eb 548 char *ctime();
83cc7253 549 char *ctime();
80f91e3f 550
fb6e7f7e 551 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) {
ede75793 552 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
80f91e3f
KB
553 if (!quiet) {
554 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
555 ll.ll_time != 0) {
0e08a2b3 556 (void)printf("Last login: %.*s ",
80f91e3f
KB
557 24-5, (char *)ctime(&ll.ll_time));
558 if (*ll.ll_host != '\0')
0e08a2b3 559 (void)printf("from %.*s\n",
80f91e3f
KB
560 sizeof(ll.ll_host), ll.ll_host);
561 else
0e08a2b3 562 (void)printf("on %.*s\n",
80f91e3f
KB
563 sizeof(ll.ll_line), ll.ll_line);
564 }
ede75793 565 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
80f91e3f 566 }
659be7fb 567 bzero((char *)&ll, sizeof(ll));
80f91e3f
KB
568 (void)time(&ll.ll_time);
569 strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
a829b33b
KB
570 if (hostname)
571 strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
80f91e3f
KB
572 (void)write(fd, (char *)&ll, sizeof(ll));
573 (void)close(fd);
88a01c09 574 }
88a01c09
BJ
575}
576
f0c0c252
KB
577badlogin(name)
578 char *name;
579{
659be7fb 580 if (failures == 0)
f0c0c252
KB
581 return;
582 if (hostname)
659be7fb
MK
583 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s, %s",
584 failures, failures > 1 ? "S" : "", hostname, name);
f0c0c252 585 else
659be7fb
MK
586 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s, %s",
587 failures, failures > 1 ? "S" : "", tty, name);
f0c0c252
KB
588}
589
f570e1ff 590#undef UNKNOWN
80f91e3f 591#define UNKNOWN "su"
88a01c09
BJ
592
593char *
659be7fb
MK
594stypeof(ttyid)
595 char *ttyid;
88a01c09 596{
80f91e3f 597 struct ttyent *t;
88a01c09 598
659be7fb 599 return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
88a01c09 600}
86eb6c9e
BJ
601
602getstr(buf, cnt, err)
80f91e3f 603 char *buf, *err;
86eb6c9e 604 int cnt;
86eb6c9e 605{
80f91e3f 606 char ch;
86eb6c9e
BJ
607
608 do {
80f91e3f 609 if (read(0, &ch, sizeof(ch)) != sizeof(ch))
86eb6c9e
BJ
610 exit(1);
611 if (--cnt < 0) {
0e08a2b3 612 (void)fprintf(stderr, "%s too long\r\n", err);
80f91e3f 613 sleepexit(1);
86eb6c9e 614 }
80f91e3f
KB
615 *buf++ = ch;
616 } while (ch);
86eb6c9e 617}
4f8d3876 618
80f91e3f
KB
619sleepexit(eval)
620 int eval;
c89291e2 621{
80f91e3f
KB
622 sleep((u_int)5);
623 exit(eval);
c89291e2 624}
d0faf133
KB
625
626doremotelogin(host)
627 char *host;
628{
629 static char lusername[UT_NAMESIZE+1];
630 char rusername[UT_NAMESIZE+1];
631
632 getstr(rusername, sizeof(rusername), "remuser");
633 getstr(lusername, sizeof(lusername), "locuser");
634 getstr(term, sizeof(term), "Terminal type");
635 username = lusername;
636 pwd = getpwnam(username);
637 if (pwd == NULL)
638 return(-1);
639 return(ruserok(host, (pwd->pw_uid == 0), rusername, username));
640}
641
642char *speeds[] = {
643 "0", "50", "75", "110", "134", "150", "200", "300", "600",
644 "1200", "1800", "2400", "4800", "9600", "19200", "38400",
645};
646#define NSPEEDS (sizeof(speeds) / sizeof(speeds[0]))
647
648doremoteterm(tp)
649 struct sgttyb *tp;
650{
651 register char *cp = index(term, '/'), **cpp;
652 char *speed;
653
654 if (cp) {
655 *cp++ = '\0';
656 speed = cp;
657 cp = index(speed, '/');
658 if (cp)
659 *cp++ = '\0';
660 for (cpp = speeds; cpp < &speeds[NSPEEDS]; cpp++)
661 if (strcmp(*cpp, speed) == 0) {
662 tp->sg_ispeed = tp->sg_ospeed = cpp-speeds;
663 break;
664 }
665 }
666 tp->sg_flags = ECHO|CRMOD|ANYP|XTABS;
667}