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