1 /*- 2 * Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static char copyright[] = 36 "@(#) Copyright (c) 1980, 1987, 1988, 1991, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)login.c 8.4 (Berkeley) 4/2/94"; 42 #endif /* not lint */ 43 44 /* 45 * login [ name ] 46 * login -h hostname (for telnetd, etc.) 47 * login -f name (for pre-authenticated login: datakit, xterm, etc.) 48 */ 49 50 #include <sys/param.h> 51 #include <sys/stat.h> 52 #include <sys/time.h> 53 #include <sys/resource.h> 54 #include <sys/file.h> 55 56 #include <err.h> 57 #include <errno.h> 58 #include <grp.h> 59 #include <pwd.h> 60 #include <setjmp.h> 61 #include <signal.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <syslog.h> 66 #include <ttyent.h> 67 #include <tzfile.h> 68 #include <unistd.h> 69 #include <utmp.h> 70 71 #ifdef SKEY 72 #include <skey.h> 73 #endif 74 75 #include "pathnames.h" 76 77 void badlogin __P((char *)); 78 void checknologin __P((void)); 79 void dolastlog __P((int)); 80 void getloginname __P((void)); 81 void motd __P((void)); 82 int rootterm __P((char *)); 83 void sigint __P((int)); 84 void sleepexit __P((int)); 85 char *stypeof __P((char *)); 86 void timedout __P((int)); 87 void login_fbtab __P((char *, uid_t, gid_t)); 88 #ifdef KERBEROS 89 int klogin __P((struct passwd *, char *, char *, char *)); 90 #endif 91 92 extern void login __P((struct utmp *)); 93 94 #define TTYGRPNAME "tty" /* name of group to own ttys */ 95 96 /* 97 * This bounds the time given to login. Not a define so it can 98 * be patched on machines where it's too small. 99 */ 100 u_int timeout = 300; 101 102 #ifdef KERBEROS 103 int notickets = 1; 104 int noticketsdontcomplain = 1; 105 char *instance; 106 char *krbtkfile_env; 107 int authok; 108 #endif 109 110 struct passwd *pwd; 111 int failures; 112 char term[64], *envinit[1], *hostname, *username, *tty; 113 114 int 115 main(argc, argv) 116 int argc; 117 char *argv[]; 118 { 119 extern char **environ; 120 struct group *gr; 121 struct stat st; 122 struct timeval tp; 123 struct utmp utmp; 124 int ask, ch, cnt, fflag, hflag, pflag, quietlog, rootlogin, rval; 125 int changepass; 126 uid_t uid; 127 char *domain, *p, *ep, *salt, *ttyn; 128 char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10]; 129 char localhost[MAXHOSTNAMELEN]; 130 char full_hostname[MAXHOSTNAMELEN]; 131 #ifdef SKEY 132 int permit_passwd = 0; 133 #endif 134 135 (void)signal(SIGALRM, timedout); 136 (void)alarm(timeout); 137 (void)signal(SIGQUIT, SIG_IGN); 138 (void)signal(SIGINT, SIG_IGN); 139 (void)setpriority(PRIO_PROCESS, 0, 0); 140 141 openlog("login", LOG_ODELAY, LOG_AUTH); 142 143 /* 144 * -p is used by getty to tell login not to destroy the environment 145 * -f is used to skip a second login authentication 146 * -h is used by other servers to pass the name of the remote 147 * host to login so that it may be placed in utmp and wtmp 148 */ 149 *full_hostname = '\0'; 150 domain = NULL; 151 if (gethostname(localhost, sizeof(localhost)) < 0) 152 syslog(LOG_ERR, "couldn't get local hostname: %m"); 153 else 154 domain = strchr(localhost, '.'); 155 156 fflag = hflag = pflag = 0; 157 uid = getuid(); 158 while ((ch = getopt(argc, argv, "fh:p")) != EOF) 159 switch (ch) { 160 case 'f': 161 fflag = 1; 162 break; 163 case 'h': 164 if (uid) 165 errx(1, "-h option: %s", strerror(EPERM)); 166 hflag = 1; 167 strncpy(full_hostname, optarg, sizeof(full_hostname)-1); 168 if (domain && (p = strchr(optarg, '.')) && 169 strcasecmp(p, domain) == 0) 170 *p = 0; 171 hostname = optarg; 172 break; 173 case 'p': 174 pflag = 1; 175 break; 176 case '?': 177 default: 178 if (!uid) 179 syslog(LOG_ERR, "invalid flag %c", ch); 180 (void)fprintf(stderr, 181 "usage: login [-fp] [-h hostname] [username]\n"); 182 exit(1); 183 } 184 argc -= optind; 185 argv += optind; 186 187 if (*argv) { 188 username = *argv; 189 ask = 0; 190 } else 191 ask = 1; 192 193 for (cnt = getdtablesize(); cnt > 2; cnt--) 194 (void)close(cnt); 195 196 ttyn = ttyname(STDIN_FILENO); 197 if (ttyn == NULL || *ttyn == '\0') { 198 (void)snprintf(tname, sizeof(tname), "%s??", _PATH_TTY); 199 ttyn = tname; 200 } 201 if (tty = strrchr(ttyn, '/')) 202 ++tty; 203 else 204 tty = ttyn; 205 206 for (cnt = 0;; ask = 1) { 207 if (ask) { 208 fflag = 0; 209 getloginname(); 210 } 211 rootlogin = 0; 212 #ifdef KERBEROS 213 if ((instance = strchr(username, '.')) != NULL) { 214 if (strncmp(instance, ".root", 5) == 0) 215 rootlogin = 1; 216 *instance++ = '\0'; 217 } else 218 instance = ""; 219 #endif 220 if (strlen(username) > UT_NAMESIZE) 221 username[UT_NAMESIZE] = '\0'; 222 223 /* 224 * Note if trying multiple user names; log failures for 225 * previous user name, but don't bother logging one failure 226 * for nonexistent name (mistyped username). 227 */ 228 if (failures && strcmp(tbuf, username)) { 229 if (failures > (pwd ? 0 : 1)) 230 badlogin(tbuf); 231 failures = 0; 232 } 233 (void)strcpy(tbuf, username); 234 235 if (pwd = getpwnam(username)) 236 salt = pwd->pw_passwd; 237 else 238 salt = "xx"; 239 240 /* 241 * if we have a valid account name, and it doesn't have a 242 * password, or the -f option was specified and the caller 243 * is root or the caller isn't changing their uid, don't 244 * authenticate. 245 */ 246 if (pwd) { 247 if (pwd->pw_uid == 0) 248 rootlogin = 1; 249 250 if (fflag && (uid == 0 || uid == pwd->pw_uid)) { 251 /* already authenticated */ 252 break; 253 } else if (pwd->pw_passwd[0] == '\0') { 254 /* pretend password okay */ 255 rval = 0; 256 goto ttycheck; 257 } 258 } 259 260 fflag = 0; 261 262 (void)setpriority(PRIO_PROCESS, 0, -4); 263 264 #ifdef SKEY 265 permit_passwd = skeyaccess(username, tty, 266 hostname ? full_hostname : NULL, 267 NULL); 268 p = skey_getpass("Password:", pwd, permit_passwd); 269 ep = skey_crypt(p, salt, pwd, permit_passwd); 270 #else 271 p = getpass("Password:"); 272 ep = crypt(p, salt); 273 #endif 274 275 if (pwd) { 276 #ifdef KERBEROS 277 rval = klogin(pwd, instance, localhost, p); 278 if (rval != 0 && rootlogin && pwd->pw_uid != 0) 279 rootlogin = 0; 280 if (rval == 0) 281 authok = 1; 282 else if (rval == 1) 283 rval = strcmp(ep, pwd->pw_passwd); 284 #else 285 rval = strcmp(ep, pwd->pw_passwd); 286 #endif 287 } 288 memset(p, 0, strlen(p)); 289 290 (void)setpriority(PRIO_PROCESS, 0, 0); 291 292 ttycheck: 293 /* 294 * If trying to log in as root without Kerberos, 295 * but with insecure terminal, refuse the login attempt. 296 */ 297 #ifdef KERBEROS 298 if (authok == 0) 299 #endif 300 if (pwd && !rval && rootlogin && !rootterm(tty)) { 301 (void)fprintf(stderr, 302 "%s login refused on this terminal.\n", 303 pwd->pw_name); 304 if (hostname) 305 syslog(LOG_NOTICE, 306 "LOGIN %s REFUSED FROM %s ON TTY %s", 307 pwd->pw_name, hostname, tty); 308 else 309 syslog(LOG_NOTICE, 310 "LOGIN %s REFUSED ON TTY %s", 311 pwd->pw_name, tty); 312 continue; 313 } 314 315 if (pwd && !rval) 316 break; 317 318 (void)printf("Login incorrect\n"); 319 failures++; 320 /* we allow 10 tries, but after 3 we start backing off */ 321 if (++cnt > 3) { 322 if (cnt >= 10) { 323 badlogin(username); 324 sleepexit(1); 325 } 326 sleep((u_int)((cnt - 3) * 5)); 327 } 328 } 329 330 /* committed to login -- turn off timeout */ 331 (void)alarm((u_int)0); 332 333 endpwent(); 334 335 /* if user not super-user, check for disabled logins */ 336 if (!rootlogin) 337 checknologin(); 338 339 if (chdir(pwd->pw_dir) < 0) { 340 (void)printf("No home directory %s!\n", pwd->pw_dir); 341 if (chdir("/")) 342 exit(0); 343 pwd->pw_dir = "/"; 344 (void)printf("Logging in with home = \"/\".\n"); 345 } 346 347 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0; 348 349 if (pwd->pw_change || pwd->pw_expire) 350 (void)gettimeofday(&tp, (struct timezone *)NULL); 351 352 changepass=0; 353 if (pwd->pw_change) 354 if (tp.tv_sec >= pwd->pw_change) { 355 (void)printf("Sorry -- your password has expired.\n"); 356 changepass=1; 357 } else if (pwd->pw_change - tp.tv_sec < 358 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) 359 (void)printf("Warning: your password expires on %s", 360 ctime(&pwd->pw_change)); 361 if (pwd->pw_expire) 362 if (tp.tv_sec >= pwd->pw_expire) { 363 (void)printf("Sorry -- your account has expired.\n"); 364 sleepexit(1); 365 } else if (pwd->pw_expire - tp.tv_sec < 366 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) 367 (void)printf("Warning: your account expires on %s", 368 ctime(&pwd->pw_expire)); 369 370 /* Nothing else left to fail -- really log in. */ 371 memset((void *)&utmp, 0, sizeof(utmp)); 372 (void)time(&utmp.ut_time); 373 (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name)); 374 if (hostname) 375 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host)); 376 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); 377 login(&utmp); 378 379 dolastlog(quietlog); 380 381 /* 382 * Set device protections, depending on what terminal the 383 * user is logged in. This feature is used on Suns to give 384 * console users better privacy. 385 */ 386 login_fbtab(tty, pwd->pw_uid, pwd->pw_gid); 387 388 (void)chown(ttyn, pwd->pw_uid, 389 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid); 390 (void)setgid(pwd->pw_gid); 391 392 initgroups(username, pwd->pw_gid); 393 394 if (*pwd->pw_shell == '\0') 395 pwd->pw_shell = _PATH_BSHELL; 396 397 /* Destroy environment unless user has requested its preservation. */ 398 if (!pflag) 399 environ = envinit; 400 (void)setenv("HOME", pwd->pw_dir, 1); 401 (void)setenv("SHELL", pwd->pw_shell, 1); 402 if (term[0] == '\0') 403 (void)strncpy(term, stypeof(tty), sizeof(term)); 404 (void)setenv("TERM", term, 0); 405 (void)setenv("LOGNAME", pwd->pw_name, 1); 406 (void)setenv("USER", pwd->pw_name, 1); 407 (void)setenv("PATH", _PATH_DEFPATH, 0); 408 #ifdef KERBEROS 409 if (krbtkfile_env) 410 (void)setenv("KRBTKFILE", krbtkfile_env, 1); 411 #endif 412 413 if (tty[sizeof("tty")-1] == 'd') 414 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name); 415 416 /* If fflag is on, assume caller/authenticator has logged root login. */ 417 if (rootlogin && fflag == 0) 418 if (hostname) 419 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s", 420 username, tty, hostname); 421 else 422 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty); 423 424 #ifdef KERBEROS 425 if (!quietlog && notickets == 1 && !noticketsdontcomplain) 426 (void)printf("Warning: no Kerberos tickets issued.\n"); 427 #endif 428 429 #ifdef LOGALL 430 /* 431 * Syslog each successful login, so we don't have to watch hundreds 432 * of wtmp or lastlogin files. 433 */ 434 if (hostname) { 435 syslog(LOG_INFO, "login from %s as %s", hostname, pwd->pw_name); 436 } else { 437 syslog(LOG_INFO, "login on %s as %s", tty, pwd->pw_name); 438 } 439 #endif 440 441 if (!quietlog) { 442 (void)printf("%s\n\t%s %s\n\n", 443 "Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994", 444 "The Regents of the University of California. ", 445 "All rights reserved."); 446 motd(); 447 (void)snprintf(tbuf, 448 sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 449 if (stat(tbuf, &st) == 0 && st.st_size != 0) 450 (void)printf("You have %smail.\n", 451 (st.st_mtime > st.st_atime) ? "new " : ""); 452 } 453 454 #ifdef LOGIN_ACCESS 455 if (login_access(pwd->pw_name, hostname ? full_hostname : tty) == 0) { 456 printf("Permission denied\n"); 457 if (hostname) 458 syslog(LOG_NOTICE, "%s LOGIN REFUSED FROM %s", 459 pwd->pw_name, hostname); 460 else 461 syslog(LOG_NOTICE, "%s LOGIN REFUSED ON %s", 462 pwd->pw_name, tty); 463 sleepexit(1); 464 } 465 #endif 466 467 (void)signal(SIGALRM, SIG_DFL); 468 (void)signal(SIGQUIT, SIG_DFL); 469 (void)signal(SIGINT, SIG_DFL); 470 (void)signal(SIGTSTP, SIG_IGN); 471 472 tbuf[0] = '-'; 473 (void)strcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ? 474 p + 1 : pwd->pw_shell); 475 476 if (setlogin(pwd->pw_name) < 0) 477 syslog(LOG_ERR, "setlogin() failure: %m"); 478 479 /* Discard permissions last so can't get killed and drop core. */ 480 if (rootlogin) 481 (void) setuid(0); 482 else 483 (void) setuid(pwd->pw_uid); 484 485 if (changepass) { 486 int res; 487 if ((res=system(_PATH_CHPASS))) 488 sleepexit(1); 489 } 490 491 execlp(pwd->pw_shell, tbuf, 0); 492 err(1, "%s", pwd->pw_shell); 493 } 494 495 #ifdef KERBEROS 496 #define NBUFSIZ (UT_NAMESIZE + 1 + 5) /* .root suffix */ 497 #else 498 #define NBUFSIZ (UT_NAMESIZE + 1) 499 #endif 500 501 void 502 getloginname() 503 { 504 int ch; 505 char *p; 506 static char nbuf[NBUFSIZ]; 507 508 for (;;) { 509 (void)printf("login: "); 510 for (p = nbuf; (ch = getchar()) != '\n'; ) { 511 if (ch == EOF) { 512 badlogin(username); 513 exit(0); 514 } 515 if (p < nbuf + (NBUFSIZ - 1)) 516 *p++ = ch; 517 } 518 if (p > nbuf) 519 if (nbuf[0] == '-') 520 (void)fprintf(stderr, 521 "login names may not start with '-'.\n"); 522 else { 523 *p = '\0'; 524 username = nbuf; 525 break; 526 } 527 } 528 } 529 530 int 531 rootterm(ttyn) 532 char *ttyn; 533 { 534 struct ttyent *t; 535 536 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE); 537 } 538 539 jmp_buf motdinterrupt; 540 541 void 542 motd() 543 { 544 int fd, nchars; 545 sig_t oldint; 546 char tbuf[8192]; 547 548 if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0) 549 return; 550 oldint = signal(SIGINT, sigint); 551 if (setjmp(motdinterrupt) == 0) 552 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 553 (void)write(fileno(stdout), tbuf, nchars); 554 (void)signal(SIGINT, oldint); 555 (void)close(fd); 556 } 557 558 /* ARGSUSED */ 559 void 560 sigint(signo) 561 int signo; 562 { 563 564 longjmp(motdinterrupt, 1); 565 } 566 567 /* ARGSUSED */ 568 void 569 timedout(signo) 570 int signo; 571 { 572 573 (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout); 574 exit(0); 575 } 576 577 void 578 checknologin() 579 { 580 int fd, nchars; 581 char tbuf[8192]; 582 583 if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) { 584 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 585 (void)write(fileno(stdout), tbuf, nchars); 586 sleepexit(0); 587 } 588 } 589 590 void 591 dolastlog(quiet) 592 int quiet; 593 { 594 struct lastlog ll; 595 int fd; 596 597 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) { 598 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 599 if (!quiet) { 600 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) && 601 ll.ll_time != 0) { 602 (void)printf("Last login: %.*s ", 603 24-5, (char *)ctime(&ll.ll_time)); 604 if (*ll.ll_host != '\0') 605 (void)printf("from %.*s\n", 606 (int)sizeof(ll.ll_host), 607 ll.ll_host); 608 else 609 (void)printf("on %.*s\n", 610 (int)sizeof(ll.ll_line), 611 ll.ll_line); 612 } 613 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 614 } 615 memset((void *)&ll, 0, sizeof(ll)); 616 (void)time(&ll.ll_time); 617 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 618 if (hostname) 619 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host)); 620 (void)write(fd, (char *)&ll, sizeof(ll)); 621 (void)close(fd); 622 } 623 } 624 625 void 626 badlogin(name) 627 char *name; 628 { 629 630 if (failures == 0) 631 return; 632 if (hostname) { 633 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s", 634 failures, failures > 1 ? "S" : "", hostname); 635 syslog(LOG_AUTHPRIV|LOG_NOTICE, 636 "%d LOGIN FAILURE%s FROM %s, %s", 637 failures, failures > 1 ? "S" : "", hostname, name); 638 } else { 639 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s", 640 failures, failures > 1 ? "S" : "", tty); 641 syslog(LOG_AUTHPRIV|LOG_NOTICE, 642 "%d LOGIN FAILURE%s ON %s, %s", 643 failures, failures > 1 ? "S" : "", tty, name); 644 } 645 } 646 647 #undef UNKNOWN 648 #define UNKNOWN "su" 649 650 char * 651 stypeof(ttyid) 652 char *ttyid; 653 { 654 struct ttyent *t; 655 656 return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN); 657 } 658 659 void 660 sleepexit(eval) 661 int eval; 662 { 663 664 (void)sleep(5); 665 exit(eval); 666 } 667 668 669