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 #ifdef SKEY 278 /* 279 * Do not allow user to type in kerberos password 280 * over the net (actually, this is ok for encrypted 281 * links, but we have no way of determining if the 282 * link is encrypted. 283 */ 284 if (!permit_passwd) { 285 rval = 1; /* failed */ 286 } else 287 #endif 288 rval = klogin(pwd, instance, localhost, p); 289 if (rval != 0 && rootlogin && pwd->pw_uid != 0) 290 rootlogin = 0; 291 if (rval == 0) 292 authok = 1; 293 else if (rval == 1) 294 rval = strcmp(ep, pwd->pw_passwd); 295 #else 296 rval = strcmp(ep, pwd->pw_passwd); 297 #endif 298 } 299 memset(p, 0, strlen(p)); 300 301 (void)setpriority(PRIO_PROCESS, 0, 0); 302 303 ttycheck: 304 /* 305 * If trying to log in as root without Kerberos, 306 * but with insecure terminal, refuse the login attempt. 307 */ 308 #ifdef KERBEROS 309 if (authok == 0) 310 #endif 311 if (pwd && !rval && rootlogin && !rootterm(tty)) { 312 (void)fprintf(stderr, 313 "%s login refused on this terminal.\n", 314 pwd->pw_name); 315 if (hostname) 316 syslog(LOG_NOTICE, 317 "LOGIN %s REFUSED FROM %s ON TTY %s", 318 pwd->pw_name, hostname, tty); 319 else 320 syslog(LOG_NOTICE, 321 "LOGIN %s REFUSED ON TTY %s", 322 pwd->pw_name, tty); 323 continue; 324 } 325 326 if (pwd && !rval) 327 break; 328 329 (void)printf("Login incorrect\n"); 330 failures++; 331 /* we allow 10 tries, but after 3 we start backing off */ 332 if (++cnt > 3) { 333 if (cnt >= 10) { 334 badlogin(username); 335 sleepexit(1); 336 } 337 sleep((u_int)((cnt - 3) * 5)); 338 } 339 } 340 341 /* committed to login -- turn off timeout */ 342 (void)alarm((u_int)0); 343 344 endpwent(); 345 346 /* if user not super-user, check for disabled logins */ 347 if (!rootlogin) 348 checknologin(); 349 350 if (chdir(pwd->pw_dir) < 0) { 351 (void)printf("No home directory %s!\n", pwd->pw_dir); 352 if (chdir("/")) 353 exit(0); 354 pwd->pw_dir = "/"; 355 (void)printf("Logging in with home = \"/\".\n"); 356 } 357 358 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0; 359 360 if (pwd->pw_change || pwd->pw_expire) 361 (void)gettimeofday(&tp, (struct timezone *)NULL); 362 363 changepass=0; 364 if (pwd->pw_change) 365 if (tp.tv_sec >= pwd->pw_change) { 366 (void)printf("Sorry -- your password has expired.\n"); 367 changepass=1; 368 } else if (pwd->pw_change - tp.tv_sec < 369 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) 370 (void)printf("Warning: your password expires on %s", 371 ctime(&pwd->pw_change)); 372 if (pwd->pw_expire) 373 if (tp.tv_sec >= pwd->pw_expire) { 374 (void)printf("Sorry -- your account has expired.\n"); 375 sleepexit(1); 376 } else if (pwd->pw_expire - tp.tv_sec < 377 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) 378 (void)printf("Warning: your account expires on %s", 379 ctime(&pwd->pw_expire)); 380 381 /* Nothing else left to fail -- really log in. */ 382 memset((void *)&utmp, 0, sizeof(utmp)); 383 (void)time(&utmp.ut_time); 384 (void)strncpy(utmp.ut_name, username, sizeof(utmp.ut_name)); 385 if (hostname) 386 (void)strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host)); 387 (void)strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); 388 login(&utmp); 389 390 dolastlog(quietlog); 391 392 /* 393 * Set device protections, depending on what terminal the 394 * user is logged in. This feature is used on Suns to give 395 * console users better privacy. 396 */ 397 login_fbtab(tty, pwd->pw_uid, pwd->pw_gid); 398 399 (void)chown(ttyn, pwd->pw_uid, 400 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid); 401 (void)setgid(pwd->pw_gid); 402 403 initgroups(username, pwd->pw_gid); 404 405 if (*pwd->pw_shell == '\0') 406 pwd->pw_shell = _PATH_BSHELL; 407 408 /* Destroy environment unless user has requested its preservation. */ 409 if (!pflag) 410 environ = envinit; 411 (void)setenv("HOME", pwd->pw_dir, 1); 412 (void)setenv("SHELL", pwd->pw_shell, 1); 413 if (term[0] == '\0') 414 (void)strncpy(term, stypeof(tty), sizeof(term)); 415 (void)setenv("TERM", term, 0); 416 (void)setenv("LOGNAME", pwd->pw_name, 1); 417 (void)setenv("USER", pwd->pw_name, 1); 418 (void)setenv("PATH", _PATH_DEFPATH, 0); 419 #ifdef KERBEROS 420 if (krbtkfile_env) 421 (void)setenv("KRBTKFILE", krbtkfile_env, 1); 422 #endif 423 424 if (tty[sizeof("tty")-1] == 'd') 425 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name); 426 427 /* If fflag is on, assume caller/authenticator has logged root login. */ 428 if (rootlogin && fflag == 0) 429 if (hostname) 430 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s", 431 username, tty, hostname); 432 else 433 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty); 434 435 #ifdef KERBEROS 436 if (!quietlog && notickets == 1 && !noticketsdontcomplain) 437 (void)printf("Warning: no Kerberos tickets issued.\n"); 438 #endif 439 440 #ifdef LOGALL 441 /* 442 * Syslog each successful login, so we don't have to watch hundreds 443 * of wtmp or lastlogin files. 444 */ 445 if (hostname) { 446 syslog(LOG_INFO, "login from %s as %s", hostname, pwd->pw_name); 447 } else { 448 syslog(LOG_INFO, "login on %s as %s", tty, pwd->pw_name); 449 } 450 #endif 451 452 if (!quietlog) { 453 (void)printf("%s\n\t%s %s\n\n", 454 "Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994", 455 "The Regents of the University of California. ", 456 "All rights reserved."); 457 motd(); 458 (void)snprintf(tbuf, 459 sizeof(tbuf), "%s/%s", _PATH_MAILDIR, pwd->pw_name); 460 if (stat(tbuf, &st) == 0 && st.st_size != 0) 461 (void)printf("You have %smail.\n", 462 (st.st_mtime > st.st_atime) ? "new " : ""); 463 } 464 465 #ifdef LOGIN_ACCESS 466 if (login_access(pwd->pw_name, hostname ? full_hostname : tty) == 0) { 467 printf("Permission denied\n"); 468 if (hostname) 469 syslog(LOG_NOTICE, "%s LOGIN REFUSED FROM %s", 470 pwd->pw_name, hostname); 471 else 472 syslog(LOG_NOTICE, "%s LOGIN REFUSED ON %s", 473 pwd->pw_name, tty); 474 sleepexit(1); 475 } 476 #endif 477 478 (void)signal(SIGALRM, SIG_DFL); 479 (void)signal(SIGQUIT, SIG_DFL); 480 (void)signal(SIGINT, SIG_DFL); 481 (void)signal(SIGTSTP, SIG_IGN); 482 483 tbuf[0] = '-'; 484 (void)strcpy(tbuf + 1, (p = strrchr(pwd->pw_shell, '/')) ? 485 p + 1 : pwd->pw_shell); 486 487 if (setlogin(pwd->pw_name) < 0) 488 syslog(LOG_ERR, "setlogin() failure: %m"); 489 490 /* Discard permissions last so can't get killed and drop core. */ 491 if (rootlogin) 492 (void) setuid(0); 493 else 494 (void) setuid(pwd->pw_uid); 495 496 if (changepass) { 497 int res; 498 if ((res=system(_PATH_CHPASS))) 499 sleepexit(1); 500 } 501 502 execlp(pwd->pw_shell, tbuf, 0); 503 err(1, "%s", pwd->pw_shell); 504 } 505 506 #ifdef KERBEROS 507 #define NBUFSIZ (UT_NAMESIZE + 1 + 5) /* .root suffix */ 508 #else 509 #define NBUFSIZ (UT_NAMESIZE + 1) 510 #endif 511 512 void 513 getloginname() 514 { 515 int ch; 516 char *p; 517 static char nbuf[NBUFSIZ]; 518 519 for (;;) { 520 (void)printf("login: "); 521 for (p = nbuf; (ch = getchar()) != '\n'; ) { 522 if (ch == EOF) { 523 badlogin(username); 524 exit(0); 525 } 526 if (p < nbuf + (NBUFSIZ - 1)) 527 *p++ = ch; 528 } 529 if (p > nbuf) 530 if (nbuf[0] == '-') 531 (void)fprintf(stderr, 532 "login names may not start with '-'.\n"); 533 else { 534 *p = '\0'; 535 username = nbuf; 536 break; 537 } 538 } 539 } 540 541 int 542 rootterm(ttyn) 543 char *ttyn; 544 { 545 struct ttyent *t; 546 547 return ((t = getttynam(ttyn)) && t->ty_status & TTY_SECURE); 548 } 549 550 jmp_buf motdinterrupt; 551 552 void 553 motd() 554 { 555 int fd, nchars; 556 sig_t oldint; 557 char tbuf[8192]; 558 559 if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0) 560 return; 561 oldint = signal(SIGINT, sigint); 562 if (setjmp(motdinterrupt) == 0) 563 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 564 (void)write(fileno(stdout), tbuf, nchars); 565 (void)signal(SIGINT, oldint); 566 (void)close(fd); 567 } 568 569 /* ARGSUSED */ 570 void 571 sigint(signo) 572 int signo; 573 { 574 575 longjmp(motdinterrupt, 1); 576 } 577 578 /* ARGSUSED */ 579 void 580 timedout(signo) 581 int signo; 582 { 583 584 (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout); 585 exit(0); 586 } 587 588 void 589 checknologin() 590 { 591 int fd, nchars; 592 char tbuf[8192]; 593 594 if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) { 595 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 596 (void)write(fileno(stdout), tbuf, nchars); 597 sleepexit(0); 598 } 599 } 600 601 void 602 dolastlog(quiet) 603 int quiet; 604 { 605 struct lastlog ll; 606 int fd; 607 608 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) { 609 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 610 if (!quiet) { 611 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) && 612 ll.ll_time != 0) { 613 (void)printf("Last login: %.*s ", 614 24-5, (char *)ctime(&ll.ll_time)); 615 if (*ll.ll_host != '\0') 616 (void)printf("from %.*s\n", 617 (int)sizeof(ll.ll_host), 618 ll.ll_host); 619 else 620 (void)printf("on %.*s\n", 621 (int)sizeof(ll.ll_line), 622 ll.ll_line); 623 } 624 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 625 } 626 memset((void *)&ll, 0, sizeof(ll)); 627 (void)time(&ll.ll_time); 628 (void)strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 629 if (hostname) 630 (void)strncpy(ll.ll_host, hostname, sizeof(ll.ll_host)); 631 (void)write(fd, (char *)&ll, sizeof(ll)); 632 (void)close(fd); 633 } 634 } 635 636 void 637 badlogin(name) 638 char *name; 639 { 640 641 if (failures == 0) 642 return; 643 if (hostname) { 644 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s", 645 failures, failures > 1 ? "S" : "", hostname); 646 syslog(LOG_AUTHPRIV|LOG_NOTICE, 647 "%d LOGIN FAILURE%s FROM %s, %s", 648 failures, failures > 1 ? "S" : "", hostname, name); 649 } else { 650 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s", 651 failures, failures > 1 ? "S" : "", tty); 652 syslog(LOG_AUTHPRIV|LOG_NOTICE, 653 "%d LOGIN FAILURE%s ON %s, %s", 654 failures, failures > 1 ? "S" : "", tty, name); 655 } 656 } 657 658 #undef UNKNOWN 659 #define UNKNOWN "su" 660 661 char * 662 stypeof(ttyid) 663 char *ttyid; 664 { 665 struct ttyent *t; 666 667 return (ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN); 668 } 669 670 void 671 sleepexit(eval) 672 int eval; 673 { 674 675 (void)sleep(5); 676 exit(eval); 677 } 678 679 680