1 /*- 2 * Copyright (c) 1980, 1987, 1988, 1991 The Regents of the University 3 * of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980, 1987, 1988, 1991 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)login.c 5.73 (Berkeley) 06/29/91"; 16 #endif /* not lint */ 17 18 /* 19 * login [ name ] 20 * login -h hostname (for telnetd, etc.) 21 * login -f name (for pre-authenticated login: datakit, xterm, etc.) 22 */ 23 24 #include <sys/param.h> 25 #include <sys/stat.h> 26 #include <sys/time.h> 27 #include <sys/resource.h> 28 #include <sys/file.h> 29 30 #include <utmp.h> 31 #include <signal.h> 32 #include <errno.h> 33 #include <ttyent.h> 34 #include <syslog.h> 35 #include <grp.h> 36 #include <pwd.h> 37 #include <setjmp.h> 38 #include <stdio.h> 39 #include <string.h> 40 #include <tzfile.h> 41 #include "pathnames.h" 42 43 #define TTYGRPNAME "tty" /* name of group to own ttys */ 44 45 /* 46 * This bounds the time given to login. Not a define so it can 47 * be patched on machines where it's too small. 48 */ 49 int timeout = 300; 50 int rootlogin; 51 #ifdef KERBEROS 52 int notickets = 1; 53 char *instance; 54 char *krbtkfile_env; 55 int authok; 56 #endif 57 58 struct passwd *pwd; 59 int failures; 60 char term[64], *envinit[1], *hostname, *username, *tty; 61 62 main(argc, argv) 63 int argc; 64 char **argv; 65 { 66 extern int optind; 67 extern char *optarg, **environ; 68 struct timeval tp; 69 struct group *gr; 70 register int ch; 71 register char *p; 72 int ask, fflag, hflag, pflag, cnt, uid; 73 int quietlog, rval; 74 char *domain, *salt, *ttyn; 75 char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10]; 76 char localhost[MAXHOSTNAMELEN]; 77 char *ctime(), *ttyname(), *stypeof(), *crypt(), *getpass(); 78 time_t time(); 79 off_t lseek(); 80 void timedout(); 81 82 (void)signal(SIGALRM, timedout); 83 (void)alarm((u_int)timeout); 84 (void)signal(SIGQUIT, SIG_IGN); 85 (void)signal(SIGINT, SIG_IGN); 86 (void)setpriority(PRIO_PROCESS, 0, 0); 87 88 openlog("login", LOG_ODELAY, LOG_AUTH); 89 90 /* 91 * -p is used by getty to tell login not to destroy the environment 92 * -f is used to skip a second login authentication 93 * -h is used by other servers to pass the name of the remote 94 * host to login so that it may be placed in utmp and wtmp 95 */ 96 domain = NULL; 97 if (gethostname(localhost, sizeof(localhost)) < 0) 98 syslog(LOG_ERR, "couldn't get local hostname: %m"); 99 else 100 domain = index(localhost, '.'); 101 102 fflag = hflag = pflag = 0; 103 uid = getuid(); 104 while ((ch = getopt(argc, argv, "fh:p")) != EOF) 105 switch (ch) { 106 case 'f': 107 fflag = 1; 108 break; 109 case 'h': 110 if (uid) { 111 (void)fprintf(stderr, 112 "login: -h option: %s\n", strerror(EPERM)); 113 exit(1); 114 } 115 hflag = 1; 116 if (domain && (p = index(optarg, '.')) && 117 strcasecmp(p, domain) == 0) 118 *p = 0; 119 hostname = optarg; 120 break; 121 case 'p': 122 pflag = 1; 123 break; 124 case '?': 125 default: 126 if (!uid) 127 syslog(LOG_ERR, "invalid flag %c", ch); 128 (void)fprintf(stderr, 129 "usage: login [-fp] [-h hostname] [username]\n"); 130 exit(1); 131 } 132 argc -= optind; 133 argv += optind; 134 if (*argv) { 135 username = *argv; 136 ask = 0; 137 } else 138 ask = 1; 139 140 for (cnt = getdtablesize(); cnt > 2; cnt--) 141 close(cnt); 142 143 ttyn = ttyname(0); 144 if (ttyn == NULL || *ttyn == '\0') { 145 (void)sprintf(tname, "%s??", _PATH_TTY); 146 ttyn = tname; 147 } 148 if (tty = rindex(ttyn, '/')) 149 ++tty; 150 else 151 tty = ttyn; 152 153 for (cnt = 0;; ask = 1) { 154 if (ask) { 155 fflag = 0; 156 getloginname(); 157 } 158 #ifdef KERBEROS 159 if ((instance = index(username, '.')) != NULL) { 160 if (strncmp(instance, ".root", 5) == 0) 161 rootlogin++; 162 *instance++ = '\0'; 163 } else { 164 rootlogin = 0; 165 instance = ""; 166 } 167 #endif 168 if (strlen(username) > UT_NAMESIZE) 169 username[UT_NAMESIZE] = '\0'; 170 171 /* 172 * Note if trying multiple user names; log failures for 173 * previous user name, but don't bother logging one failure 174 * for nonexistent name (mistyped username). 175 */ 176 if (failures && strcmp(tbuf, username)) { 177 if (failures > (pwd ? 0 : 1)) 178 badlogin(tbuf); 179 failures = 0; 180 } 181 (void)strcpy(tbuf, username); 182 183 if (pwd = getpwnam(username)) 184 salt = pwd->pw_passwd; 185 else 186 salt = "xx"; 187 188 /* 189 * if we have a valid account name, and it doesn't have a 190 * password, or the -f option was specified and the caller 191 * is root or the caller isn't changing their uid, don't 192 * authenticate. 193 */ 194 if (pwd && (*pwd->pw_passwd == '\0' || 195 fflag && (uid == 0 || uid == pwd->pw_uid))) 196 break; 197 fflag = 0; 198 if (pwd && pwd->pw_uid == 0) 199 rootlogin = 1; 200 201 (void)setpriority(PRIO_PROCESS, 0, -4); 202 203 p = getpass("Password:"); 204 205 if (pwd) { 206 #ifdef KERBEROS 207 rval = klogin(pwd, instance, localhost, p); 208 if (rval == 0) 209 authok = 1; 210 else if (rval == 1) { 211 if (pwd->pw_uid != 0) 212 rootlogin = 0; 213 rval = strcmp(crypt(p, salt), pwd->pw_passwd); 214 } 215 #else 216 rval = strcmp(crypt(p, salt), pwd->pw_passwd); 217 #endif 218 } 219 bzero(p, strlen(p)); 220 221 (void)setpriority(PRIO_PROCESS, 0, 0); 222 223 /* 224 * If trying to log in as root without Kerberos, 225 * but with insecure terminal, refuse the login attempt. 226 */ 227 #ifdef KERBEROS 228 if (authok == 0) 229 #endif 230 if (pwd && rootlogin && !rootterm(tty)) { 231 (void)fprintf(stderr, 232 "%s login refused on this terminal.\n", 233 pwd->pw_name); 234 if (hostname) 235 syslog(LOG_NOTICE, 236 "LOGIN %s REFUSED FROM %s ON TTY %s", 237 pwd->pw_name, hostname, tty); 238 else 239 syslog(LOG_NOTICE, 240 "LOGIN %s REFUSED ON TTY %s", 241 pwd->pw_name, tty); 242 continue; 243 } 244 245 if (pwd && !rval) 246 break; 247 248 (void)printf("Login incorrect\n"); 249 failures++; 250 /* we allow 10 tries, but after 3 we start backing off */ 251 if (++cnt > 3) { 252 if (cnt >= 10) { 253 badlogin(username); 254 sleepexit(1); 255 } 256 sleep((u_int)((cnt - 3) * 5)); 257 } 258 } 259 260 /* committed to login -- turn off timeout */ 261 (void)alarm((u_int)0); 262 263 endpwent(); 264 265 /* if user not super-user, check for disabled logins */ 266 if (!rootlogin) 267 checknologin(); 268 269 if (chdir(pwd->pw_dir) < 0) { 270 (void)printf("No home directory %s!\n", pwd->pw_dir); 271 if (chdir("/")) 272 exit(0); 273 pwd->pw_dir = "/"; 274 (void)printf("Logging in with home = \"/\".\n"); 275 } 276 277 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0; 278 279 if (pwd->pw_change || pwd->pw_expire) 280 (void)gettimeofday(&tp, (struct timezone *)NULL); 281 if (pwd->pw_change) 282 if (tp.tv_sec >= pwd->pw_change) { 283 (void)printf("Sorry -- your password has expired.\n"); 284 sleepexit(1); 285 } else if (pwd->pw_change - tp.tv_sec < 286 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) 287 (void)printf("Warning: your password expires on %s", 288 ctime(&pwd->pw_expire)); 289 if (pwd->pw_expire) 290 if (tp.tv_sec >= pwd->pw_expire) { 291 (void)printf("Sorry -- your account has expired.\n"); 292 sleepexit(1); 293 } else if (pwd->pw_expire - tp.tv_sec < 294 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) 295 (void)printf("Warning: your account expires on %s", 296 ctime(&pwd->pw_expire)); 297 298 /* nothing else left to fail -- really log in */ 299 { 300 struct utmp utmp; 301 302 bzero((void *)&utmp, sizeof(utmp)); 303 (void)time(&utmp.ut_time); 304 strncpy(utmp.ut_name, username, sizeof(utmp.ut_name)); 305 if (hostname) 306 strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host)); 307 strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); 308 login(&utmp); 309 } 310 311 dolastlog(quietlog); 312 313 (void)chown(ttyn, pwd->pw_uid, 314 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid); 315 (void)setgid(pwd->pw_gid); 316 317 initgroups(username, pwd->pw_gid); 318 319 if (*pwd->pw_shell == '\0') 320 pwd->pw_shell = _PATH_BSHELL; 321 322 /* destroy environment unless user has requested preservation */ 323 if (!pflag) 324 environ = envinit; 325 (void)setenv("HOME", pwd->pw_dir, 1); 326 (void)setenv("SHELL", pwd->pw_shell, 1); 327 if (term[0] == '\0') 328 strncpy(term, stypeof(tty), sizeof(term)); 329 (void)setenv("TERM", term, 0); 330 (void)setenv("LOGNAME", pwd->pw_name, 1); 331 (void)setenv("USER", pwd->pw_name, 1); 332 (void)setenv("PATH", _PATH_DEFPATH, 0); 333 #ifdef KERBEROS 334 if (krbtkfile_env) 335 (void)setenv("KRBTKFILE", krbtkfile_env, 1); 336 #endif 337 338 if (tty[sizeof("tty")-1] == 'd') 339 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name); 340 /* if fflag is on, assume caller/authenticator has logged root login */ 341 if (rootlogin && fflag == 0) 342 if (hostname) 343 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s", 344 username, tty, hostname); 345 else 346 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty); 347 348 #ifdef KERBEROS 349 if (!quietlog && notickets == 1) 350 (void)printf("Warning: no Kerberos tickets issued.\n"); 351 #endif 352 353 if (!quietlog) { 354 struct stat st; 355 356 printf( 357 "Copyright (c) 1980,1983,1986,1988,1990,1991 The Regents of the University\n%s", 358 "of California. All rights reserved.\n\n"); 359 motd(); 360 (void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 361 if (stat(tbuf, &st) == 0 && st.st_size != 0) 362 (void)printf("You have %smail.\n", 363 (st.st_mtime > st.st_atime) ? "new " : ""); 364 } 365 366 (void)signal(SIGALRM, SIG_DFL); 367 (void)signal(SIGQUIT, SIG_DFL); 368 (void)signal(SIGINT, SIG_DFL); 369 (void)signal(SIGTSTP, SIG_IGN); 370 371 tbuf[0] = '-'; 372 strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ? 373 p + 1 : pwd->pw_shell); 374 375 if (setlogin(pwd->pw_name) < 0) 376 syslog(LOG_ERR, "setlogin() failure: %m"); 377 378 /* discard permissions last so can't get killed and drop core */ 379 if (rootlogin) 380 (void) setuid(0); 381 else 382 (void) setuid(pwd->pw_uid); 383 384 execlp(pwd->pw_shell, tbuf, 0); 385 (void)fprintf(stderr, "%s: %s\n", pwd->pw_shell, strerror(errno)); 386 exit(1); 387 } 388 389 #ifdef KERBEROS 390 #define NBUFSIZ (UT_NAMESIZE + 1 + 5) /* .root suffix */ 391 #else 392 #define NBUFSIZ (UT_NAMESIZE + 1) 393 #endif 394 395 getloginname() 396 { 397 register int ch; 398 register char *p; 399 static char nbuf[NBUFSIZ]; 400 401 for (;;) { 402 (void)printf("login: "); 403 for (p = nbuf; (ch = getchar()) != '\n'; ) { 404 if (ch == EOF) { 405 badlogin(username); 406 exit(0); 407 } 408 if (p < nbuf + (NBUFSIZ - 1)) 409 *p++ = ch; 410 } 411 if (p > nbuf) 412 if (nbuf[0] == '-') 413 (void)fprintf(stderr, 414 "login names may not start with '-'.\n"); 415 else { 416 *p = '\0'; 417 username = nbuf; 418 break; 419 } 420 } 421 } 422 423 void 424 timedout() 425 { 426 (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout); 427 exit(0); 428 } 429 430 rootterm(ttyn) 431 char *ttyn; 432 { 433 struct ttyent *t; 434 435 return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE); 436 } 437 438 jmp_buf motdinterrupt; 439 440 motd() 441 { 442 register int fd, nchars; 443 sig_t oldint; 444 void sigint(); 445 char tbuf[8192]; 446 447 if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0) 448 return; 449 oldint = signal(SIGINT, sigint); 450 if (setjmp(motdinterrupt) == 0) 451 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 452 (void)write(fileno(stdout), tbuf, nchars); 453 (void)signal(SIGINT, oldint); 454 (void)close(fd); 455 } 456 457 void 458 sigint() 459 { 460 longjmp(motdinterrupt, 1); 461 } 462 463 checknologin() 464 { 465 register int fd, nchars; 466 char tbuf[8192]; 467 468 if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) { 469 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 470 (void)write(fileno(stdout), tbuf, nchars); 471 sleepexit(0); 472 } 473 } 474 475 dolastlog(quiet) 476 int quiet; 477 { 478 struct lastlog ll; 479 int fd; 480 char *ctime(); 481 482 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) { 483 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 484 if (!quiet) { 485 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) && 486 ll.ll_time != 0) { 487 (void)printf("Last login: %.*s ", 488 24-5, (char *)ctime(&ll.ll_time)); 489 if (*ll.ll_host != '\0') 490 (void)printf("from %.*s\n", 491 sizeof(ll.ll_host), ll.ll_host); 492 else 493 (void)printf("on %.*s\n", 494 sizeof(ll.ll_line), ll.ll_line); 495 } 496 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 497 } 498 bzero((void *)&ll, sizeof(ll)); 499 (void)time(&ll.ll_time); 500 strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 501 if (hostname) 502 strncpy(ll.ll_host, hostname, sizeof(ll.ll_host)); 503 (void)write(fd, (char *)&ll, sizeof(ll)); 504 (void)close(fd); 505 } 506 } 507 508 badlogin(name) 509 char *name; 510 { 511 if (failures == 0) 512 return; 513 if (hostname) { 514 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s", 515 failures, failures > 1 ? "S" : "", hostname); 516 syslog(LOG_AUTHPRIV|LOG_NOTICE, 517 "%d LOGIN FAILURE%s FROM %s, %s", 518 failures, failures > 1 ? "S" : "", hostname, name); 519 } else { 520 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s", 521 failures, failures > 1 ? "S" : "", tty); 522 syslog(LOG_AUTHPRIV|LOG_NOTICE, 523 "%d LOGIN FAILURE%s ON %s, %s", 524 failures, failures > 1 ? "S" : "", tty, name); 525 } 526 } 527 528 #undef UNKNOWN 529 #define UNKNOWN "su" 530 531 char * 532 stypeof(ttyid) 533 char *ttyid; 534 { 535 struct ttyent *t; 536 537 return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN); 538 } 539 540 sleepexit(eval) 541 int eval; 542 { 543 sleep((u_int)5); 544 exit(eval); 545 } 546