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