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