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