add password/change account/expiration timeouts
[unix-history] / usr / src / usr.bin / login / login.c.1
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
c91c86eb 25static char sccsid[] = "@(#)login.c.1 5.34 (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>
c89291e2 53
80f91e3f 54#define TTYGRPNAME "tty" /* name of group to own ttys */
88a01c09 55
80f91e3f
KB
56#define MOTDFILE "/etc/motd"
57#define MAILDIR "/usr/spool/mail"
58#define NOLOGIN "/etc/nologin"
59#define HUSHLOGIN ".hushlogin"
60#define LASTLOG "/usr/adm/lastlog"
61#define BSHELL "/bin/sh"
f570e1ff 62
3b8dd95e 63/*
80f91e3f
KB
64 * This bounds the time given to login. Not a define so it can
65 * be patched on machines where it's too small.
3b8dd95e 66 */
a9b031fe 67int timeout = 300;
86eb6c9e 68
659be7fb
MK
69struct passwd *pwd;
70int failures;
71char term[64], *hostname, *username, *tty;
88a01c09 72
659be7fb
MK
73struct sgttyb sgttyb;
74struct tchars tc = {
714accc5
SL
75 CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
76};
659be7fb 77struct ltchars ltc = {
714accc5 78 CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
841d84b0
BJ
79};
80
c91c86eb
KB
81char *months[] =
82 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
83 "Sep", "Oct", "Nov", "Dec" };
84
88a01c09 85main(argc, argv)
80f91e3f
KB
86 int argc;
87 char **argv;
88a01c09 88{
80f91e3f
KB
89 extern int errno, optind;
90 extern char *optarg, **environ;
c91c86eb
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, &ltc);
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
c91c86eb
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
ede75793 313 /* nothing else left to fail -- really log in */
80f91e3f
KB
314 {
315 struct utmp utmp;
316
659be7fb 317 bzero((char *)&utmp, sizeof(utmp));
80f91e3f
KB
318 (void)time(&utmp.ut_time);
319 strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
a829b33b
KB
320 if (hostname)
321 strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
80f91e3f
KB
322 strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
323 login(&utmp);
f570e1ff 324 }
80f91e3f
KB
325
326 quietlog = access(HUSHLOGIN, F_OK) == 0;
f0c0c252 327 dolastlog(quietlog);
80f91e3f 328
ee1eff74 329 if (!hflag) { /* XXX */
80f91e3f
KB
330 static struct winsize win = { 0, 0, 0, 0 };
331
332 (void)ioctl(0, TIOCSWINSZ, &win);
333 }
334
335 (void)chown(ttyn, pwd->pw_uid,
336 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
337 (void)chmod(ttyn, 0620);
338 (void)setgid(pwd->pw_gid);
339
340 initgroups(username, pwd->pw_gid);
341
22d4760e 342 quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0);
80f91e3f 343 (void)setuid(pwd->pw_uid);
3daca631 344
ede75793
KB
345 if (*pwd->pw_shell == '\0')
346 pwd->pw_shell = BSHELL;
347 /* turn on new line discipline for the csh */
348 else if (!strcmp(pwd->pw_shell, "/bin/csh")) {
349 ioctlval = NTTYDISC;
350 (void)ioctl(0, TIOCSETD, &ioctlval);
351 }
352
80f91e3f 353 /* destroy environment unless user has requested preservation */
d3737d51
SL
354 if (!pflag)
355 environ = envinit;
ede75793
KB
356 (void)setenv("HOME", pwd->pw_dir, 1);
357 (void)setenv("SHELL", pwd->pw_shell, 1);
d3737d51 358 if (term[0] == '\0')
659be7fb 359 strncpy(term, stypeof(tty), sizeof(term));
ede75793
KB
360 (void)setenv("TERM", term, 0);
361 (void)setenv("USER", pwd->pw_name, 1);
362 (void)setenv("PATH", "/usr/ucb:/bin:/usr/bin:", 0);
d3737d51 363
9479aa87 364 if (tty[sizeof("tty")-1] == 'd')
d3737d51
SL
365 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
366 if (pwd->pw_uid == 0)
80f91e3f 367 if (hostname)
659be7fb 368 syslog(LOG_NOTICE, "ROOT LOGIN ON %s FROM %s",
f0c0c252 369 tty, hostname);
df9d9536 370 else
659be7fb 371 syslog(LOG_NOTICE, "ROOT LOGIN ON %s", tty);
80f91e3f 372
4f8d3876 373 if (!quietlog) {
6821e9c5 374 struct stat st;
d3737d51 375
80f91e3f
KB
376 motd();
377 (void)sprintf(tbuf, "%s/%s", MAILDIR, pwd->pw_name);
378 if (stat(tbuf, &st) == 0 && st.st_size != 0)
6821e9c5 379 printf("You have %smail.\n",
80f91e3f 380 (st.st_mtime > st.st_atime) ? "new " : "");
f570e1ff 381 }
80f91e3f
KB
382
383 (void)signal(SIGALRM, SIG_DFL);
384 (void)signal(SIGQUIT, SIG_DFL);
385 (void)signal(SIGINT, SIG_DFL);
386 (void)signal(SIGTSTP, SIG_IGN);
387
388 tbuf[0] = '-';
389 strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
390 p + 1 : pwd->pw_shell);
391 execlp(pwd->pw_shell, tbuf, 0);
ede75793 392 fprintf(stderr, "login: no shell: ");
f570e1ff 393 perror(pwd->pw_shell);
88a01c09
BJ
394 exit(0);
395}
396
80f91e3f 397getloginname()
3b8dd95e 398{
80f91e3f
KB
399 register int ch;
400 register char *p;
401 static char nbuf[UT_NAMESIZE + 1];
3b8dd95e 402
80f91e3f 403 for (;;) {
5a786176 404 printf("login: ");
ede75793 405 for (p = nbuf; (ch = getchar()) != '\n'; ) {
f0c0c252
KB
406 if (ch == EOF) {
407 badlogin(username);
3b8dd95e 408 exit(0);
f0c0c252 409 }
ede75793 410 if (p < nbuf + UT_NAMESIZE)
80f91e3f 411 *p++ = ch;
3b8dd95e 412 }
80f91e3f
KB
413 if (p > nbuf)
414 if (nbuf[0] == '-')
415 fprintf(stderr,
416 "login names may not start with '-'.\n");
417 else {
418 *p = '\0';
419 username = nbuf;
ede75793 420 break;
80f91e3f 421 }
3b8dd95e 422 }
3b8dd95e
SL
423}
424
425timedout()
426{
80f91e3f 427 fprintf(stderr, "Login timed out after %d seconds\n", timeout);
3b8dd95e
SL
428 exit(0);
429}
430
659be7fb
MK
431rootterm(ttyn)
432 char *ttyn;
88a01c09 433{
80f91e3f 434 struct ttyent *t;
1886582e 435
659be7fb 436 return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE);
88a01c09
BJ
437}
438
ede75793
KB
439jmp_buf motdinterrupt;
440
80f91e3f 441motd()
f570e1ff 442{
ede75793 443 register int fd, nchars;
80f91e3f 444 int (*oldint)(), sigint();
ede75793 445 char tbuf[8192];
80f91e3f 446
ede75793 447 if ((fd = open(MOTDFILE, O_RDONLY, 0)) < 0)
80f91e3f
KB
448 return;
449 oldint = signal(SIGINT, sigint);
ede75793
KB
450 if (setjmp(motdinterrupt) == 0)
451 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
452 (void)write(fileno(stdout), tbuf, nchars);
80f91e3f 453 (void)signal(SIGINT, oldint);
ede75793 454 (void)close(fd);
80f91e3f 455}
9479aa87 456
80f91e3f
KB
457sigint()
458{
ede75793 459 longjmp(motdinterrupt, 1);
80f91e3f
KB
460}
461
462checknologin()
463{
464 register int fd, nchars;
ede75793 465 char tbuf[8192];
80f91e3f
KB
466
467 if ((fd = open(NOLOGIN, O_RDONLY, 0)) >= 0) {
468 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
469 (void)write(fileno(stdout), tbuf, nchars);
470 sleepexit(0);
f570e1ff 471 }
f570e1ff
BJ
472}
473
f0c0c252 474dolastlog(quiet)
80f91e3f 475 int quiet;
88a01c09 476{
80f91e3f
KB
477 struct lastlog ll;
478 int fd;
c91c86eb 479 char *ctime();
80f91e3f
KB
480
481 if ((fd = open(LASTLOG, O_RDWR, 0)) >= 0) {
ede75793 482 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
80f91e3f
KB
483 if (!quiet) {
484 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
485 ll.ll_time != 0) {
486 printf("Last login: %.*s ",
487 24-5, (char *)ctime(&ll.ll_time));
488 if (*ll.ll_host != '\0')
489 printf("from %.*s\n",
490 sizeof(ll.ll_host), ll.ll_host);
491 else
492 printf("on %.*s\n",
493 sizeof(ll.ll_line), ll.ll_line);
494 }
ede75793 495 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
80f91e3f 496 }
659be7fb 497 bzero((char *)&ll, sizeof(ll));
80f91e3f
KB
498 (void)time(&ll.ll_time);
499 strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
a829b33b
KB
500 if (hostname)
501 strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
80f91e3f
KB
502 (void)write(fd, (char *)&ll, sizeof(ll));
503 (void)close(fd);
88a01c09 504 }
88a01c09
BJ
505}
506
f0c0c252
KB
507badlogin(name)
508 char *name;
509{
659be7fb 510 if (failures == 0)
f0c0c252
KB
511 return;
512 if (hostname)
659be7fb
MK
513 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s, %s",
514 failures, failures > 1 ? "S" : "", hostname, name);
f0c0c252 515 else
659be7fb
MK
516 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s, %s",
517 failures, failures > 1 ? "S" : "", tty, name);
f0c0c252
KB
518}
519
f570e1ff 520#undef UNKNOWN
80f91e3f 521#define UNKNOWN "su"
88a01c09
BJ
522
523char *
659be7fb
MK
524stypeof(ttyid)
525 char *ttyid;
88a01c09 526{
80f91e3f 527 struct ttyent *t;
88a01c09 528
659be7fb 529 return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN);
88a01c09 530}
86eb6c9e
BJ
531
532getstr(buf, cnt, err)
80f91e3f 533 char *buf, *err;
86eb6c9e 534 int cnt;
86eb6c9e 535{
80f91e3f 536 char ch;
86eb6c9e
BJ
537
538 do {
80f91e3f 539 if (read(0, &ch, sizeof(ch)) != sizeof(ch))
86eb6c9e
BJ
540 exit(1);
541 if (--cnt < 0) {
80f91e3f
KB
542 fprintf(stderr, "%s too long\r\n", err);
543 sleepexit(1);
86eb6c9e 544 }
80f91e3f
KB
545 *buf++ = ch;
546 } while (ch);
86eb6c9e 547}
4f8d3876 548
80f91e3f
KB
549sleepexit(eval)
550 int eval;
c89291e2 551{
80f91e3f
KB
552 sleep((u_int)5);
553 exit(eval);
c89291e2 554}