1 /*- 2 * Copyright (c) 1980, 1987, 1988, 1991 The Regents of the University 3 * of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1980, 1987, 1988, 1991 The Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)login.c 5.68 (Berkeley) 03/29/91"; 16 #endif /* not lint */ 17 18 /* 19 * login [ name ] 20 * login -h hostname (for telnetd, etc.) 21 * login -f name (for pre-authenticated login: datakit, xterm, etc.) 22 */ 23 24 #include <sys/param.h> 25 #include <sys/stat.h> 26 #include <sys/time.h> 27 #include <sys/resource.h> 28 #include <sys/file.h> 29 30 #include <utmp.h> 31 #include <signal.h> 32 #include <errno.h> 33 #include <ttyent.h> 34 #include <syslog.h> 35 #include <grp.h> 36 #include <pwd.h> 37 #include <setjmp.h> 38 #include <stdio.h> 39 #include <string.h> 40 #include <tzfile.h> 41 #include "pathnames.h" 42 43 #define TTYGRPNAME "tty" /* name of group to own ttys */ 44 45 /* 46 * This bounds the time given to login. Not a define so it can 47 * be patched on machines where it's too small. 48 */ 49 int timeout = 300; 50 #ifdef KERBEROS 51 int notickets = 1; 52 int rootlogin = 0; 53 #define ROOTLOGIN (rootlogin || (pwd && pwd->pw_uid == 0)) 54 #else 55 #define ROOTLOGIN (pwd && pwd->pw_uid == 0) 56 #endif 57 58 struct passwd *pwd; 59 int failures; 60 char term[64], *envinit[1], *hostname, *username, *tty; 61 62 main(argc, argv) 63 int argc; 64 char **argv; 65 { 66 extern int optind; 67 extern char *optarg, **environ; 68 struct timeval tp; 69 struct group *gr; 70 register int ch; 71 register char *p; 72 int ask, fflag, hflag, pflag, cnt, uid; 73 int quietlog, rval; 74 char *domain, *salt, *ttyn; 75 char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10]; 76 char localhost[MAXHOSTNAMELEN]; 77 char *ctime(), *ttyname(), *stypeof(), *crypt(), *getpass(); 78 time_t time(); 79 off_t lseek(); 80 void timedout(); 81 82 (void)signal(SIGALRM, timedout); 83 (void)alarm((u_int)timeout); 84 (void)signal(SIGQUIT, SIG_IGN); 85 (void)signal(SIGINT, SIG_IGN); 86 (void)setpriority(PRIO_PROCESS, 0, 0); 87 88 openlog("login", LOG_ODELAY, LOG_AUTH); 89 90 /* 91 * -p is used by getty to tell login not to destroy the environment 92 * -f is used to skip a second login authentication 93 * -h is used by other servers to pass the name of the remote 94 * host to login so that it may be placed in utmp and wtmp 95 */ 96 domain = NULL; 97 if (gethostname(localhost, sizeof(localhost)) < 0) 98 syslog(LOG_ERR, "couldn't get local hostname: %m"); 99 else 100 domain = index(localhost, '.'); 101 102 fflag = hflag = pflag = 0; 103 uid = getuid(); 104 while ((ch = getopt(argc, argv, "fh:p")) != EOF) 105 switch (ch) { 106 case 'f': 107 fflag = 1; 108 break; 109 case 'h': 110 if (uid) { 111 (void)fprintf(stderr, 112 "login: -h for super-user only.\n"); 113 exit(1); 114 } 115 hflag = 1; 116 if (domain && (p = index(optarg, '.')) && 117 strcasecmp(p, domain) == 0) 118 *p = 0; 119 hostname = optarg; 120 break; 121 case 'p': 122 pflag = 1; 123 break; 124 case '?': 125 default: 126 if (!uid) 127 syslog(LOG_ERR, "invalid flag %c", ch); 128 (void)fprintf(stderr, 129 "usage: login [-fp] [username]\n"); 130 exit(1); 131 } 132 argc -= optind; 133 argv += optind; 134 if (*argv) { 135 username = *argv; 136 if (strlen(username) > UT_NAMESIZE) 137 username[UT_NAMESIZE] = '\0'; 138 ask = 0; 139 } else 140 ask = 1; 141 142 for (cnt = getdtablesize(); cnt > 2; cnt--) 143 close(cnt); 144 145 ttyn = ttyname(0); 146 if (ttyn == NULL || *ttyn == '\0') { 147 (void)sprintf(tname, "%s??", _PATH_TTY); 148 ttyn = tname; 149 } 150 if (tty = rindex(ttyn, '/')) 151 ++tty; 152 else 153 tty = ttyn; 154 155 for (cnt = 0;; ask = 1) { 156 if (ask) { 157 fflag = 0; 158 getloginname(); 159 } 160 /* 161 * Note if trying multiple user names; log failures for 162 * previous user name, but don't bother logging one failure 163 * for nonexistent name (mistyped username). 164 */ 165 if (failures && strcmp(tbuf, username)) { 166 if (failures > (pwd ? 0 : 1)) 167 badlogin(tbuf); 168 failures = 0; 169 } 170 (void)strcpy(tbuf, username); 171 172 if (pwd = getpwnam(username)) 173 salt = pwd->pw_passwd; 174 else 175 salt = "xx"; 176 177 /* 178 * if we have a valid account name, and it doesn't have a 179 * password, or the -f option was specified and the caller 180 * is root or the caller isn't changing their uid, don't 181 * authenticate. 182 */ 183 if (pwd && (*pwd->pw_passwd == '\0' || 184 fflag && (uid == 0 || uid == pwd->pw_uid))) 185 break; 186 fflag = 0; 187 188 /* 189 * If trying to log in as root, but with insecure terminal, 190 * refuse the login attempt. 191 */ 192 if (ROOTLOGIN && !rootterm(tty)) { 193 (void)fprintf(stderr, 194 "%s login refused on this terminal.\n", 195 pwd->pw_name); 196 if (hostname) 197 syslog(LOG_NOTICE, 198 "LOGIN %s REFUSED FROM %s ON TTY %s", 199 pwd->pw_name, hostname, tty); 200 else 201 syslog(LOG_NOTICE, 202 "LOGIN %s REFUSED ON TTY %s", 203 pwd->pw_name, tty); 204 continue; 205 } 206 207 (void)setpriority(PRIO_PROCESS, 0, -4); 208 209 p = getpass("Password:"); 210 211 if (pwd) { 212 #ifdef KERBEROS 213 rval = klogin(pwd, localhost, p); 214 if (rval == 1) 215 rval = strcmp(crypt(p, salt), pwd->pw_passwd); 216 #else 217 rval = strcmp(crypt(p, salt), pwd->pw_passwd); 218 #endif 219 } 220 bzero(p, strlen(p)); 221 222 (void)setpriority(PRIO_PROCESS, 0, 0); 223 224 if (pwd && !rval) 225 break; 226 227 (void)printf("Login incorrect\n"); 228 failures++; 229 /* we allow 10 tries, but after 3 we start backing off */ 230 if (++cnt > 3) { 231 if (cnt >= 10) { 232 badlogin(username); 233 sleepexit(1); 234 } 235 sleep((u_int)((cnt - 3) * 5)); 236 } 237 } 238 239 /* committed to login -- turn off timeout */ 240 (void)alarm((u_int)0); 241 242 /* paranoia... */ 243 endpwent(); 244 245 /* if user not super-user, check for disabled logins */ 246 if (!(ROOTLOGIN)) 247 checknologin(); 248 249 if (chdir(pwd->pw_dir) < 0) { 250 (void)printf("No directory %s!\n", pwd->pw_dir); 251 if (chdir("/")) 252 exit(0); 253 pwd->pw_dir = "/"; 254 (void)printf("Logging in with home = \"/\".\n"); 255 } 256 257 quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0; 258 259 if (pwd->pw_change || pwd->pw_expire) 260 (void)gettimeofday(&tp, (struct timezone *)NULL); 261 if (pwd->pw_change) 262 if (tp.tv_sec >= pwd->pw_change) { 263 (void)printf("Sorry -- your password has expired.\n"); 264 sleepexit(1); 265 } 266 else if (pwd->pw_change - tp.tv_sec < 267 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) 268 (void)printf("Warning: your password expires on %s", 269 ctime(&pwd->pw_expire)); 270 if (pwd->pw_expire) 271 if (tp.tv_sec >= pwd->pw_expire) { 272 (void)printf("Sorry -- your account has expired.\n"); 273 sleepexit(1); 274 } 275 else if (pwd->pw_expire - tp.tv_sec < 276 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) 277 (void)printf("Warning: your account expires on %s", 278 ctime(&pwd->pw_expire)); 279 280 /* nothing else left to fail -- really log in */ 281 { 282 struct utmp utmp; 283 284 bzero((void *)&utmp, sizeof(utmp)); 285 (void)time(&utmp.ut_time); 286 strncpy(utmp.ut_name, username, sizeof(utmp.ut_name)); 287 if (hostname) 288 strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host)); 289 strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); 290 login(&utmp); 291 } 292 293 dolastlog(quietlog); 294 295 (void)chown(ttyn, pwd->pw_uid, 296 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid); 297 (void)chmod(ttyn, 0620); 298 (void)setgid(pwd->pw_gid); 299 300 initgroups(username, pwd->pw_gid); 301 302 if (*pwd->pw_shell == '\0') 303 pwd->pw_shell = _PATH_BSHELL; 304 305 /* destroy environment unless user has requested preservation */ 306 if (!pflag) 307 environ = envinit; 308 (void)setenv("HOME", pwd->pw_dir, 1); 309 (void)setenv("SHELL", pwd->pw_shell, 1); 310 if (term[0] == '\0') 311 strncpy(term, stypeof(tty), sizeof(term)); 312 (void)setenv("TERM", term, 0); 313 (void)setenv("USER", pwd->pw_name, 1); 314 (void)setenv("PATH", _PATH_DEFPATH, 0); 315 316 if (tty[sizeof("tty")-1] == 'd') 317 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name); 318 /* if fflag is on, assume caller/authenticator has logged root login */ 319 if (ROOTLOGIN && fflag == 0) 320 if (hostname) 321 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s FROM %s", 322 username, tty, hostname); 323 else 324 syslog(LOG_NOTICE, "ROOT LOGIN (%s) ON %s", username, tty); 325 326 #ifdef KERBEROS 327 if (!quietlog && notickets == 1) 328 (void)printf("Warning: no Kerberos tickets issued.\n"); 329 #endif 330 331 if (!quietlog) { 332 struct stat st; 333 334 printf( 335 "Copyright (c) 1980,1983,1986,1988,1990,1991 The Regents of the University\n%s", 336 "of California. All rights reserved.\n\n"); 337 motd(); 338 (void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 339 if (stat(tbuf, &st) == 0 && st.st_size != 0) 340 (void)printf("You have %smail.\n", 341 (st.st_mtime > st.st_atime) ? "new " : ""); 342 } 343 344 (void)signal(SIGALRM, SIG_DFL); 345 (void)signal(SIGQUIT, SIG_DFL); 346 (void)signal(SIGINT, SIG_DFL); 347 (void)signal(SIGTSTP, SIG_IGN); 348 349 tbuf[0] = '-'; 350 strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ? 351 p + 1 : pwd->pw_shell); 352 353 if (setlogin(pwd->pw_name) < 0) 354 syslog(LOG_ERR, "setlogin() failure: %m"); 355 356 /* discard permissions last so can't get killed and drop core */ 357 if (ROOTLOGIN) 358 (void) setuid(0); 359 else 360 (void)setuid(pwd->pw_uid); 361 362 execlp(pwd->pw_shell, tbuf, 0); 363 (void)fprintf(stderr, "login: no shell: %s.\n", strerror(errno)); 364 exit(0); 365 } 366 367 #ifdef KERBEROS 368 #define NBUFSIZ (UT_NAMESIZE + 1 + 5) /* .root suffix */ 369 #else 370 #define NBUFSIZ (UT_NAMESIZE + 1) 371 #endif 372 373 getloginname() 374 { 375 register int ch; 376 register char *p, *instance; 377 static char nbuf[NBUFSIZ]; 378 379 for (;;) { 380 (void)printf("login: "); 381 for (p = nbuf; (ch = getchar()) != '\n'; ) { 382 if (ch == EOF) { 383 badlogin(username); 384 exit(0); 385 } 386 if (p < nbuf + (NBUFSIZ - 1)) 387 *p++ = ch; 388 } 389 if (p > nbuf) 390 if (nbuf[0] == '-') 391 (void)fprintf(stderr, 392 "login names may not start with '-'.\n"); 393 else { 394 *p = '\0'; 395 username = nbuf; 396 break; 397 } 398 } 399 #ifdef KERBEROS 400 if ((instance = index(nbuf, '.')) != NULL) { 401 if (strncmp(instance, ".root", 5) == 0) { 402 rootlogin++; 403 } 404 *instance = '\0'; 405 if ((instance - nbuf) > UT_NAMESIZE) 406 nbuf[UT_NAMESIZE] = '\0'; 407 } else 408 rootlogin = 0; 409 #endif 410 } 411 412 void 413 timedout() 414 { 415 (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout); 416 exit(0); 417 } 418 419 rootterm(ttyn) 420 char *ttyn; 421 { 422 struct ttyent *t; 423 424 return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE); 425 } 426 427 jmp_buf motdinterrupt; 428 429 motd() 430 { 431 register int fd, nchars; 432 sig_t oldint; 433 void sigint(); 434 char tbuf[8192]; 435 436 if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0) 437 return; 438 oldint = signal(SIGINT, sigint); 439 if (setjmp(motdinterrupt) == 0) 440 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 441 (void)write(fileno(stdout), tbuf, nchars); 442 (void)signal(SIGINT, oldint); 443 (void)close(fd); 444 } 445 446 void 447 sigint() 448 { 449 longjmp(motdinterrupt, 1); 450 } 451 452 checknologin() 453 { 454 register int fd, nchars; 455 char tbuf[8192]; 456 457 if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) { 458 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 459 (void)write(fileno(stdout), tbuf, nchars); 460 sleepexit(0); 461 } 462 } 463 464 dolastlog(quiet) 465 int quiet; 466 { 467 struct lastlog ll; 468 int fd; 469 char *ctime(); 470 471 if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) { 472 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 473 if (!quiet) { 474 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) && 475 ll.ll_time != 0) { 476 (void)printf("Last login: %.*s ", 477 24-5, (char *)ctime(&ll.ll_time)); 478 if (*ll.ll_host != '\0') 479 (void)printf("from %.*s\n", 480 sizeof(ll.ll_host), ll.ll_host); 481 else 482 (void)printf("on %.*s\n", 483 sizeof(ll.ll_line), ll.ll_line); 484 } 485 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 486 } 487 bzero((void *)&ll, sizeof(ll)); 488 (void)time(&ll.ll_time); 489 strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 490 if (hostname) 491 strncpy(ll.ll_host, hostname, sizeof(ll.ll_host)); 492 (void)write(fd, (char *)&ll, sizeof(ll)); 493 (void)close(fd); 494 } 495 } 496 497 badlogin(name) 498 char *name; 499 { 500 if (failures == 0) 501 return; 502 if (hostname) { 503 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s", 504 failures, failures > 1 ? "S" : "", hostname); 505 syslog(LOG_AUTHPRIV|LOG_NOTICE, 506 "%d LOGIN FAILURE%s FROM %s, %s", 507 failures, failures > 1 ? "S" : "", hostname, name); 508 } else { 509 syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s", 510 failures, failures > 1 ? "S" : "", tty); 511 syslog(LOG_AUTHPRIV|LOG_NOTICE, 512 "%d LOGIN FAILURE%s ON %s, %s", 513 failures, failures > 1 ? "S" : "", tty, name); 514 } 515 } 516 517 #undef UNKNOWN 518 #define UNKNOWN "su" 519 520 char * 521 stypeof(ttyid) 522 char *ttyid; 523 { 524 struct ttyent *t; 525 526 return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN); 527 } 528 529 sleepexit(eval) 530 int eval; 531 { 532 sleep((u_int)5); 533 exit(eval); 534 } 535