119843Sdist /* 236515Sbostic * Copyright (c) 1980, 1987, 1988 The Regents of the University of California. 336515Sbostic * All rights reserved. 436515Sbostic * 536515Sbostic * Redistribution and use in source and binary forms are permitted 636515Sbostic * provided that the above copyright notice and this paragraph are 736515Sbostic * duplicated in all such forms and that any documentation, 836515Sbostic * advertising materials, and other materials related to such 936515Sbostic * distribution and use acknowledge that the software was developed 1036515Sbostic * by the University of California, Berkeley. The name of the 1136515Sbostic * University may not be used to endorse or promote products derived 1236515Sbostic * from this software without specific prior written permission. 1336515Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1436515Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1536515Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1619843Sdist */ 1719843Sdist 1812678Ssam #ifndef lint 1919843Sdist char copyright[] = 2036515Sbostic "@(#) Copyright (c) 1980, 1987, 1988 The Regents of the University of California.\n\ 2119843Sdist All rights reserved.\n"; 2236515Sbostic #endif /* not lint */ 2312678Ssam 2419843Sdist #ifndef lint 25*38755Skfall static char sccsid[] = "@(#)login.c 5.47 (Berkeley) 08/24/89"; 2636515Sbostic #endif /* not lint */ 2719843Sdist 281043Sbill /* 291043Sbill * login [ name ] 3031695Skarels * login -h hostname (for telnetd, etc.) 3131695Skarels * login -f name (for pre-authenticated login: datakit, xterm, etc.) 321043Sbill */ 331043Sbill 3412984Ssam #include <sys/param.h> 3537692Sbostic #include <ufs/quota.h> 3612687Ssam #include <sys/stat.h> 3712687Ssam #include <sys/time.h> 3812687Ssam #include <sys/resource.h> 3916453Sroot #include <sys/file.h> 4036460Sbostic #include <sys/ioctl.h> 4112687Ssam 421043Sbill #include <utmp.h> 431043Sbill #include <signal.h> 4412678Ssam #include <errno.h> 4516453Sroot #include <ttyent.h> 4616453Sroot #include <syslog.h> 4726862Smckusick #include <grp.h> 4836460Sbostic #include <pwd.h> 4936515Sbostic #include <setjmp.h> 5036460Sbostic #include <stdio.h> 5136460Sbostic #include <strings.h> 5237203Sbostic #include <tzfile.h> 5337203Sbostic #include "pathnames.h" 541043Sbill 5537203Sbostic #ifdef KERBEROS 5638726Skfall #include <krb.h> 5738726Skfall #include <sgtty.h> 5838034Skfall #include <netdb.h> 5938034Skfall char realm[REALM_SZ]; 6038034Skfall int kerror = KSUCCESS, notickets = 1; 6138034Skfall KTEXT_ST ticket; 6238034Skfall AUTH_DAT authdata; 6338034Skfall char savehost[MAXHOSTNAMELEN]; 6438726Skfall char tkfile[MAXPATHLEN]; 6538034Skfall unsigned long faddr; 6638034Skfall struct hostent *hp; 6738034Skfall #define PRINCIPAL_NAME pwd->pw_name 6838034Skfall #define PRINCIPAL_INST "" 6938034Skfall #define INITIAL_TICKET "krbtgt" 7038034Skfall #define VERIFY_SERVICE "rcmd" 7137203Sbostic #endif 7237203Sbostic 7336460Sbostic #define TTYGRPNAME "tty" /* name of group to own ttys */ 7426862Smckusick 7512687Ssam /* 7636460Sbostic * This bounds the time given to login. Not a define so it can 7736460Sbostic * be patched on machines where it's too small. 7812687Ssam */ 7931509Sbostic int timeout = 300; 806005Swnj 8136647Skarels struct passwd *pwd; 8236647Skarels int failures; 8336647Skarels char term[64], *hostname, *username, *tty; 846005Swnj 8536647Skarels struct sgttyb sgttyb; 8636647Skarels struct tchars tc = { 8713074Ssam CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK 881365Sbill }; 8936647Skarels struct ltchars ltc = { 9013074Ssam CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT 9113074Ssam }; 921365Sbill 9336880Sbostic char *months[] = 9436880Sbostic { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", 9536880Sbostic "Sep", "Oct", "Nov", "Dec" }; 9636880Sbostic 971043Sbill main(argc, argv) 9836460Sbostic int argc; 9936460Sbostic char **argv; 1001043Sbill { 10136460Sbostic extern int errno, optind; 10236460Sbostic extern char *optarg, **environ; 10336880Sbostic struct timeval tp; 10436880Sbostic struct tm *ttp; 10536460Sbostic struct group *gr; 10636460Sbostic register int ch; 10736647Skarels register char *p; 10837203Sbostic int ask, fflag, hflag, pflag, cnt; 10936460Sbostic int quietlog, passwd_req, ioctlval, timedout(); 11036647Skarels char *domain, *salt, *envinit[1], *ttyn, *pp; 11137692Sbostic char tbuf[MAXPATHLEN + 2], tname[sizeof(_PATH_TTY) + 10]; 11238034Skfall char localhost[MAXHOSTNAMELEN]; 11336880Sbostic char *ctime(), *ttyname(), *stypeof(), *crypt(), *getpass(); 11436460Sbostic time_t time(); 11536460Sbostic off_t lseek(); 1161043Sbill 11736460Sbostic (void)signal(SIGALRM, timedout); 11836460Sbostic (void)alarm((u_int)timeout); 11936460Sbostic (void)signal(SIGQUIT, SIG_IGN); 12036460Sbostic (void)signal(SIGINT, SIG_IGN); 12136460Sbostic (void)setpriority(PRIO_PROCESS, 0, 0); 12236460Sbostic (void)quota(Q_SETUID, 0, 0, 0); 12336460Sbostic 12412687Ssam /* 12518549Ssam * -p is used by getty to tell login not to destroy the environment 12631695Skarels * -f is used to skip a second login authentication 12736523Skarels * -h is used by other servers to pass the name of the remote 12836523Skarels * host to login so that it may be placed in utmp and wtmp 12912687Ssam */ 13038034Skfall domain = NULL; 13138034Skfall if (gethostname(localhost, sizeof(localhost)) < 0) 13238034Skfall syslog(LOG_ERR, "couldn't get local hostname: %m"); 13338034Skfall else 13438034Skfall domain = index(localhost, '.'); 13536460Sbostic 13637203Sbostic fflag = hflag = pflag = 0; 13736460Sbostic passwd_req = 1; 13837203Sbostic while ((ch = getopt(argc, argv, "fh:p")) != EOF) 13936515Sbostic switch (ch) { 14036460Sbostic case 'f': 14136460Sbostic fflag = 1; 14236460Sbostic break; 14336460Sbostic case 'h': 14436460Sbostic if (getuid()) { 14537203Sbostic (void)fprintf(stderr, 14636460Sbostic "login: -h for super-user only.\n"); 14732313Sbostic exit(1); 14831695Skarels } 14936460Sbostic hflag = 1; 15036460Sbostic if (domain && (p = index(optarg, '.')) && 15136553Sbostic strcasecmp(p, domain) == 0) 15236460Sbostic *p = 0; 15336460Sbostic hostname = optarg; 15436460Sbostic break; 15536460Sbostic case 'p': 15618549Ssam pflag = 1; 15736460Sbostic break; 15836460Sbostic case '?': 15936460Sbostic default: 16038726Skfall syslog(LOG_ERR, "invalid flag"); 16137203Sbostic (void)fprintf(stderr, 16237203Sbostic "usage: login [-fp] [username]\n"); 16336460Sbostic exit(1); 16418549Ssam } 16536460Sbostic argc -= optind; 16636460Sbostic argv += optind; 16736549Sbostic if (*argv) { 16836647Skarels username = *argv; 16936549Sbostic ask = 0; 17036647Skarels } else 17136549Sbostic ask = 1; 17236460Sbostic 17336460Sbostic ioctlval = 0; 17436460Sbostic (void)ioctl(0, TIOCLSET, &ioctlval); 17536460Sbostic (void)ioctl(0, TIOCNXCL, 0); 17636515Sbostic (void)fcntl(0, F_SETFL, ioctlval); 17736515Sbostic (void)ioctl(0, TIOCGETP, &sgttyb); 17836515Sbostic sgttyb.sg_erase = CERASE; 17936515Sbostic sgttyb.sg_kill = CKILL; 18036460Sbostic (void)ioctl(0, TIOCSLTC, <c); 18136460Sbostic (void)ioctl(0, TIOCSETC, &tc); 18236515Sbostic (void)ioctl(0, TIOCSETP, &sgttyb); 18336460Sbostic 18436460Sbostic for (cnt = getdtablesize(); cnt > 2; cnt--) 18536460Sbostic close(cnt); 18636460Sbostic 1871043Sbill ttyn = ttyname(0); 18837692Sbostic if (ttyn == NULL || *ttyn == '\0') { 18937692Sbostic (void)sprintf(tname, "%s??", _PATH_TTY); 19037692Sbostic ttyn = tname; 19137692Sbostic } 19236460Sbostic if (tty = rindex(ttyn, '/')) 19336460Sbostic ++tty; 19436460Sbostic else 19516453Sroot tty = ttyn; 19636460Sbostic 19724852Seric openlog("login", LOG_ODELAY, LOG_AUTH); 19836460Sbostic 19936549Sbostic for (cnt = 0;; ask = 1) { 20038602Sbostic ioctlval = TTYDISC; 20136460Sbostic (void)ioctl(0, TIOCSETD, &ioctlval); 20236460Sbostic 20336549Sbostic if (ask) { 20436515Sbostic fflag = 0; 20536460Sbostic getloginname(); 20636515Sbostic } 20736647Skarels /* 20836647Skarels * Note if trying multiple user names; 20936647Skarels * log failures for previous user name, 21036647Skarels * but don't bother logging one failure 21136647Skarels * for nonexistent name (mistyped username). 21236647Skarels */ 21336647Skarels if (failures && strcmp(tbuf, username)) { 21436647Skarels if (failures > (pwd ? 0 : 1)) 21536549Sbostic badlogin(tbuf); 21636647Skarels failures = 0; 21736549Sbostic } 21836647Skarels (void)strcpy(tbuf, username); 21936515Sbostic if (pwd = getpwnam(username)) 22036515Sbostic salt = pwd->pw_passwd; 22136515Sbostic else 22236515Sbostic salt = "xx"; 22336460Sbostic 22436460Sbostic /* if user not super-user, check for disabled logins */ 22536515Sbostic if (pwd == NULL || pwd->pw_uid) 22636460Sbostic checknologin(); 22736460Sbostic 22812687Ssam /* 22936515Sbostic * Disallow automatic login to root; if not invoked by 23036515Sbostic * root, disallow if the uid's differ. 23112687Ssam */ 23236515Sbostic if (fflag && pwd) { 23331695Skarels int uid = getuid(); 23431695Skarels 23538726Skfall passwd_req = 23638726Skfall #ifndef KERBEROS 23738726Skfall pwd->pw_uid == 0 || 23838726Skfall #endif 23936515Sbostic (uid && uid != pwd->pw_uid); 24031695Skarels } 24136460Sbostic 24212687Ssam /* 24336523Skarels * If no pre-authentication and a password exists 24436460Sbostic * for this user, prompt for one and verify it. 24512687Ssam */ 24636608Skfall if (!passwd_req || (pwd && !*pwd->pw_passwd)) 24736460Sbostic break; 24812687Ssam 24936460Sbostic setpriority(PRIO_PROCESS, 0, -4); 25036608Skfall pp = getpass("Password:"); 25136608Skfall p = crypt(pp, salt); 25236460Sbostic setpriority(PRIO_PROCESS, 0, 0); 25336608Skfall 25437203Sbostic #ifdef KERBEROS 25537203Sbostic 25637203Sbostic /* 257*38755Skfall * If not present in pw file, act as old login would. 25837203Sbostic * If we aren't Kerberos-authenticated, try the normal 25937203Sbostic * pw file for a password. If that's ok, log the user 26037203Sbostic * in without issueing any tickets. 26137203Sbostic */ 26237203Sbostic 26338034Skfall if (pwd && (krb_get_lrealm(realm,1) == KSUCCESS)) { 26438726Skfall 26537203Sbostic /* 26638726Skfall * get TGT for local realm 26738726Skfall * convention: store tickets in file 268*38755Skfall * associated with tty name, which should 269*38755Skfall * be available 27037203Sbostic */ 27138726Skfall 27238726Skfall (void)sprintf(tkfile, "%s_%s", TKT_ROOT, tty); 27338726Skfall kerror = INTK_ERR; 27438726Skfall if (setenv("KRBTKFILE", tkfile, 1) < 0) 27538726Skfall syslog(LOG_ERR, "couldn't set tkfile environ"); 27638726Skfall else { 277*38755Skfall (void) unlink(tkfile); 27838726Skfall kerror = krb_get_pw_in_tkt( 279*38755Skfall PRINCIPAL_NAME, /* user */ 280*38755Skfall PRINCIPAL_INST, /* (null) */ 281*38755Skfall realm, 282*38755Skfall INITIAL_TICKET, realm, 283*38755Skfall DEFAULT_TKT_LIFE, 28438726Skfall pp); 285*38755Skfall 286*38755Skfall if (chown(tkfile, pwd->pw_uid, pwd->pw_gid) < 0) 287*38755Skfall syslog(LOG_ERR, "chown tkfile: %m"); 28838726Skfall } 28938034Skfall /* 29038034Skfall * If we got a TGT, get a local "rcmd" ticket and 29138034Skfall * check it so as to ensure that we are not 29238034Skfall * talking to a bogus Kerberos server 29338726Skfall * 29438726Skfall * There are 2 cases where we still allow a login: 29538726Skfall * 1> the VERIFY_SERVICE doesn't exist in the KDC 29638726Skfall * 2> local host has no srvtab, as (hopefully) 29738726Skfall * indicated by a return value of RD_AP_UNDEC 29838726Skfall * from krb_rd_req() 29938034Skfall */ 30037203Sbostic if (kerror == INTK_OK) { 30138034Skfall (void) strncpy(savehost, 30238034Skfall krb_get_phost(localhost), 30338034Skfall sizeof(savehost)); 30438034Skfall savehost[sizeof(savehost)-1] = NULL; 30538034Skfall kerror = krb_mk_req(&ticket, VERIFY_SERVICE, 30638034Skfall savehost, realm, 33); 30738726Skfall /* 308*38755Skfall * if the "VERIFY_SERVICE" doesn't exist in 309*38755Skfall * the KDC for this host, still allow login, 31038726Skfall * but log the error condition 31138726Skfall */ 31238034Skfall if (kerror == KDC_PR_UNKNOWN) { 31338726Skfall syslog(LOG_NOTICE, 31438034Skfall "Warning: TGT not verified (%s)", 31538034Skfall krb_err_txt[kerror]); 31638034Skfall bzero(pp, strlen(pp)); 31738034Skfall notickets = 0; 31838034Skfall break; /* ok */ 31938034Skfall } else if (kerror != KSUCCESS) { 32038034Skfall printf("Unable to use TGT: (%s)\n", 32138034Skfall krb_err_txt[kerror]); 32238726Skfall syslog(LOG_NOTICE, 32338034Skfall "Unable to use TGT: (%s)", 32438034Skfall krb_err_txt[kerror]); 32538034Skfall dest_tkt(); 32638034Skfall /* fall thru: no login */ 32738034Skfall } else { 32838034Skfall if (!(hp = gethostbyname(localhost))) { 329*38755Skfall syslog(LOG_ERR, 330*38755Skfall "couldn't get local host address"); 33138034Skfall } else { 33238034Skfall bcopy((char *) hp->h_addr, 33338034Skfall (char *) &faddr, sizeof(faddr)); 33438034Skfall if ((kerror = krb_rd_req(&ticket, 33538034Skfall VERIFY_SERVICE, savehost, faddr, 33638034Skfall &authdata, "")) != KSUCCESS) { 33738726Skfall 33838726Skfall if (kerror = RD_AP_UNDEC) { 33938726Skfall syslog(LOG_NOTICE, 34038726Skfall "krb_rd_req: (%s)\n", 34138726Skfall krb_err_txt[kerror]); 34238726Skfall bzero(pp, strlen(pp)); 34338726Skfall notickets = 0; 34438726Skfall break; /* ok */ 34538726Skfall } else { 34638726Skfall printf("Unable to verify %s ticket: (%s)\n", 34738726Skfall VERIFY_SERVICE, 34838034Skfall krb_err_txt[kerror]); 34938726Skfall syslog(LOG_NOTICE, 35038726Skfall "couldn't verify %s ticket: %s", 35138726Skfall VERIFY_SERVICE, 35238034Skfall krb_err_txt[kerror]); 35338726Skfall } 35438726Skfall /* fall thru: no login */ 35538034Skfall } else { 35638034Skfall bzero(pp, strlen(pp)); 35738034Skfall notickets = 0; /* got ticket */ 35838034Skfall break; /* ok */ 35938034Skfall } 36038034Skfall } 36138034Skfall } 36238034Skfall 363*38755Skfall } else { 364*38755Skfall (void) unlink(tkfile); 365*38755Skfall if (kerror != INTK_BADPW) 366*38755Skfall syslog(LOG_ERR, 367*38755Skfall "Kerberos intkt error: %s", 368*38755Skfall krb_err_txt[kerror]); 36937203Sbostic } 37037203Sbostic } 37138726Skfall 37237203Sbostic #endif 37336608Skfall (void) bzero(pp, strlen(pp)); 37436515Sbostic if (pwd && !strcmp(p, pwd->pw_passwd)) 37536460Sbostic break; 37636460Sbostic 37737203Sbostic (void)printf("Login incorrect\n"); 37836647Skarels failures++; 37936549Sbostic /* we allow 10 tries, but after 3 we start backing off */ 38036549Sbostic if (++cnt > 3) { 38136549Sbostic if (cnt >= 10) { 38236549Sbostic badlogin(username); 38336549Sbostic (void)ioctl(0, TIOCHPCL, (struct sgttyb *)NULL); 38436549Sbostic sleepexit(1); 38536549Sbostic } 38636549Sbostic sleep((u_int)((cnt - 3) * 5)); 3872822Swnj } 38836460Sbostic } 3891043Sbill 39036460Sbostic /* committed to login -- turn off timeout */ 39136460Sbostic (void)alarm((u_int)0); 39236460Sbostic 39337692Sbostic /* paranoia... */ 39437692Sbostic endpwent(); 39537692Sbostic 39636460Sbostic /* 39736460Sbostic * If valid so far and root is logging in, see if root logins on 39836460Sbostic * this terminal are permitted. 39936460Sbostic */ 40036647Skarels if (pwd->pw_uid == 0 && !rootterm(tty)) { 40136460Sbostic if (hostname) 40236647Skarels syslog(LOG_NOTICE, "ROOT LOGIN REFUSED FROM %s", 40336647Skarels hostname); 40412678Ssam else 40536647Skarels syslog(LOG_NOTICE, "ROOT LOGIN REFUSED ON %s", tty); 40637203Sbostic (void)printf("Login incorrect\n"); 40736460Sbostic sleepexit(1); 40812678Ssam } 4092822Swnj 41036460Sbostic if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0 && errno != EINVAL) { 41136460Sbostic switch(errno) { 41236460Sbostic case EUSERS: 41337203Sbostic (void)fprintf(stderr, 41436460Sbostic "Too many users logged on already.\nTry again later.\n"); 41536460Sbostic break; 41636460Sbostic case EPROCLIM: 41737203Sbostic (void)fprintf(stderr, 41836460Sbostic "You have too many processes running.\n"); 41936460Sbostic break; 42036460Sbostic default: 42136460Sbostic perror("quota (Q_SETUID)"); 4222822Swnj } 42336460Sbostic sleepexit(0); 4242822Swnj } 42536460Sbostic 42636515Sbostic if (chdir(pwd->pw_dir) < 0) { 42737203Sbostic (void)printf("No directory %s!\n", pwd->pw_dir); 42836515Sbostic if (chdir("/")) 42936515Sbostic exit(0); 43036515Sbostic pwd->pw_dir = "/"; 43137203Sbostic (void)printf("Logging in with home = \"/\".\n"); 43236515Sbostic } 43336515Sbostic 43437203Sbostic quietlog = access(_PATH_HUSHLOGIN, F_OK) == 0; 43537203Sbostic 43637203Sbostic #ifdef KERBEROS 43737203Sbostic if (notickets && !quietlog) 43837203Sbostic (void)printf("Warning: no Kerberos tickets issued\n"); 43937203Sbostic #endif 44037203Sbostic 44136880Sbostic if (pwd->pw_change || pwd->pw_expire) 44236880Sbostic (void)gettimeofday(&tp, (struct timezone *)NULL); 44336880Sbostic if (pwd->pw_change) 44436880Sbostic if (tp.tv_sec >= pwd->pw_change) { 44537203Sbostic (void)printf("Sorry -- your password has expired.\n"); 44636880Sbostic sleepexit(1); 44736880Sbostic } 44838216Sbostic else if (pwd->pw_change - tp.tv_sec < 44938216Sbostic 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) { 45036880Sbostic ttp = localtime(&pwd->pw_change); 45137203Sbostic (void)printf("Warning: your password expires on %s %d, %d\n", 45238216Sbostic months[ttp->tm_mon], ttp->tm_mday, 45338216Sbostic TM_YEAR_BASE + ttp->tm_year); 45436880Sbostic } 45536880Sbostic if (pwd->pw_expire) 45636880Sbostic if (tp.tv_sec >= pwd->pw_expire) { 45737203Sbostic (void)printf("Sorry -- your account has expired.\n"); 45836880Sbostic sleepexit(1); 45936880Sbostic } 46038216Sbostic else if (pwd->pw_expire - tp.tv_sec < 46138216Sbostic 2 * DAYSPERWEEK * SECSPERDAY && !quietlog) { 46236880Sbostic ttp = localtime(&pwd->pw_expire); 46337203Sbostic (void)printf("Warning: your account expires on %s %d, %d\n", 46438216Sbostic months[ttp->tm_mon], ttp->tm_mday, 46538216Sbostic TM_YEAR_BASE + ttp->tm_year); 46636880Sbostic } 46736880Sbostic 46836515Sbostic /* nothing else left to fail -- really log in */ 46936460Sbostic { 47036460Sbostic struct utmp utmp; 47136460Sbostic 47236647Skarels bzero((char *)&utmp, sizeof(utmp)); 47336460Sbostic (void)time(&utmp.ut_time); 47436460Sbostic strncpy(utmp.ut_name, username, sizeof(utmp.ut_name)); 47536591Sbostic if (hostname) 47636591Sbostic strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host)); 47736460Sbostic strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line)); 47836460Sbostic login(&utmp); 47936460Sbostic } 48036460Sbostic 48136549Sbostic dolastlog(quietlog); 48236460Sbostic 48337203Sbostic if (!hflag) { /* XXX */ 48436460Sbostic static struct winsize win = { 0, 0, 0, 0 }; 48536460Sbostic 48636460Sbostic (void)ioctl(0, TIOCSWINSZ, &win); 48736460Sbostic } 48836460Sbostic 48936460Sbostic (void)chown(ttyn, pwd->pw_uid, 49036460Sbostic (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid); 49136460Sbostic (void)chmod(ttyn, 0620); 49236460Sbostic (void)setgid(pwd->pw_gid); 49336460Sbostic 49436460Sbostic initgroups(username, pwd->pw_gid); 49536460Sbostic 49612678Ssam quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0); 49730606Sbostic 49836515Sbostic if (*pwd->pw_shell == '\0') 49937203Sbostic pwd->pw_shell = _PATH_BSHELL; 50036515Sbostic 50136460Sbostic /* destroy environment unless user has requested preservation */ 50218549Ssam if (!pflag) 50318549Ssam environ = envinit; 50436515Sbostic (void)setenv("HOME", pwd->pw_dir, 1); 50536515Sbostic (void)setenv("SHELL", pwd->pw_shell, 1); 50618549Ssam if (term[0] == '\0') 50736647Skarels strncpy(term, stypeof(tty), sizeof(term)); 50836515Sbostic (void)setenv("TERM", term, 0); 50936515Sbostic (void)setenv("USER", pwd->pw_name, 1); 51037252Sbostic (void)setenv("PATH", _PATH_DEFPATH, 0); 51118549Ssam 51216453Sroot if (tty[sizeof("tty")-1] == 'd') 51318549Ssam syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name); 51418549Ssam if (pwd->pw_uid == 0) 51536460Sbostic if (hostname) 51636647Skarels syslog(LOG_NOTICE, "ROOT LOGIN ON %s FROM %s", 51736549Sbostic tty, hostname); 51825230Smckusick else 51936647Skarels syslog(LOG_NOTICE, "ROOT LOGIN ON %s", tty); 52036460Sbostic 5216329Swnj if (!quietlog) { 52217664Sserge struct stat st; 52318549Ssam 52436460Sbostic motd(); 52537203Sbostic (void)sprintf(tbuf, "%s/%s", _PATH_MAILDIR, pwd->pw_name); 52636460Sbostic if (stat(tbuf, &st) == 0 && st.st_size != 0) 52737203Sbostic (void)printf("You have %smail.\n", 52836460Sbostic (st.st_mtime > st.st_atime) ? "new " : ""); 5292822Swnj } 53036460Sbostic 53136460Sbostic (void)signal(SIGALRM, SIG_DFL); 53236460Sbostic (void)signal(SIGQUIT, SIG_DFL); 53336460Sbostic (void)signal(SIGINT, SIG_DFL); 53436460Sbostic (void)signal(SIGTSTP, SIG_IGN); 53536460Sbostic 53636460Sbostic tbuf[0] = '-'; 53736460Sbostic strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ? 53836460Sbostic p + 1 : pwd->pw_shell); 53937203Sbostic 54037692Sbostic if (setlogname(pwd->pw_name, strlen(pwd->pw_name)) < 0) 54137692Sbostic syslog(LOG_ERR, "setlogname() failure: %m"); 54237692Sbostic 54337203Sbostic /* discard permissions last so can't get killed and drop core */ 54437203Sbostic (void)setuid(pwd->pw_uid); 54537203Sbostic 54636460Sbostic execlp(pwd->pw_shell, tbuf, 0); 54737203Sbostic (void)fprintf(stderr, "login: no shell: %s.\n", strerror(errno)); 5481043Sbill exit(0); 5491043Sbill } 5501043Sbill 55136460Sbostic getloginname() 55212687Ssam { 55336460Sbostic register int ch; 55436460Sbostic register char *p; 55536460Sbostic static char nbuf[UT_NAMESIZE + 1]; 55612687Ssam 55736460Sbostic for (;;) { 55837203Sbostic (void)printf("login: "); 55936515Sbostic for (p = nbuf; (ch = getchar()) != '\n'; ) { 56036549Sbostic if (ch == EOF) { 56136549Sbostic badlogin(username); 56212687Ssam exit(0); 56336549Sbostic } 56436515Sbostic if (p < nbuf + UT_NAMESIZE) 56536460Sbostic *p++ = ch; 56612687Ssam } 56736460Sbostic if (p > nbuf) 56836460Sbostic if (nbuf[0] == '-') 56937203Sbostic (void)fprintf(stderr, 57036460Sbostic "login names may not start with '-'.\n"); 57136460Sbostic else { 57236460Sbostic *p = '\0'; 57336460Sbostic username = nbuf; 57436515Sbostic break; 57536460Sbostic } 57612687Ssam } 57712687Ssam } 57812687Ssam 57912687Ssam timedout() 58012687Ssam { 58137203Sbostic (void)fprintf(stderr, "Login timed out after %d seconds\n", timeout); 58212687Ssam exit(0); 58312687Ssam } 58412687Ssam 58536647Skarels rootterm(ttyn) 58636647Skarels char *ttyn; 5871043Sbill { 58836460Sbostic struct ttyent *t; 5896466Swnj 59036647Skarels return((t = getttynam(ttyn)) && t->ty_status&TTY_SECURE); 5911043Sbill } 5921043Sbill 59336515Sbostic jmp_buf motdinterrupt; 59436515Sbostic 59536460Sbostic motd() 5962822Swnj { 59736515Sbostic register int fd, nchars; 59838726Skfall sig_t oldint; 59938726Skfall int sigint(); 60036515Sbostic char tbuf[8192]; 6012822Swnj 60237203Sbostic if ((fd = open(_PATH_MOTDFILE, O_RDONLY, 0)) < 0) 60336460Sbostic return; 60436460Sbostic oldint = signal(SIGINT, sigint); 60536515Sbostic if (setjmp(motdinterrupt) == 0) 60636515Sbostic while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 60736515Sbostic (void)write(fileno(stdout), tbuf, nchars); 60836460Sbostic (void)signal(SIGINT, oldint); 60936515Sbostic (void)close(fd); 61036460Sbostic } 61136460Sbostic 61236460Sbostic sigint() 61336460Sbostic { 61436515Sbostic longjmp(motdinterrupt, 1); 61536460Sbostic } 61636460Sbostic 61736460Sbostic checknologin() 61836460Sbostic { 61936460Sbostic register int fd, nchars; 62036515Sbostic char tbuf[8192]; 62136460Sbostic 62237203Sbostic if ((fd = open(_PATH_NOLOGIN, O_RDONLY, 0)) >= 0) { 62336460Sbostic while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 62436460Sbostic (void)write(fileno(stdout), tbuf, nchars); 62536460Sbostic sleepexit(0); 6262822Swnj } 6272822Swnj } 6282822Swnj 62936549Sbostic dolastlog(quiet) 63036460Sbostic int quiet; 6311043Sbill { 63236460Sbostic struct lastlog ll; 63336460Sbostic int fd; 63436880Sbostic char *ctime(); 6351043Sbill 63637203Sbostic if ((fd = open(_PATH_LASTLOG, O_RDWR, 0)) >= 0) { 63736515Sbostic (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 63836460Sbostic if (!quiet) { 63936460Sbostic if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) && 64036460Sbostic ll.ll_time != 0) { 64137203Sbostic (void)printf("Last login: %.*s ", 64236460Sbostic 24-5, (char *)ctime(&ll.ll_time)); 64336460Sbostic if (*ll.ll_host != '\0') 64437203Sbostic (void)printf("from %.*s\n", 64536460Sbostic sizeof(ll.ll_host), ll.ll_host); 64636460Sbostic else 64737203Sbostic (void)printf("on %.*s\n", 64836460Sbostic sizeof(ll.ll_line), ll.ll_line); 64936460Sbostic } 65036515Sbostic (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET); 65136460Sbostic } 65236647Skarels bzero((char *)&ll, sizeof(ll)); 65336460Sbostic (void)time(&ll.ll_time); 65436460Sbostic strncpy(ll.ll_line, tty, sizeof(ll.ll_line)); 65536591Sbostic if (hostname) 65636591Sbostic strncpy(ll.ll_host, hostname, sizeof(ll.ll_host)); 65736460Sbostic (void)write(fd, (char *)&ll, sizeof(ll)); 65836460Sbostic (void)close(fd); 6591043Sbill } 6601043Sbill } 6611043Sbill 66236549Sbostic badlogin(name) 66336549Sbostic char *name; 66436549Sbostic { 66536647Skarels if (failures == 0) 66636549Sbostic return; 66736549Sbostic if (hostname) 66836647Skarels syslog(LOG_NOTICE, "%d LOGIN FAILURE%s FROM %s, %s", 66936647Skarels failures, failures > 1 ? "S" : "", hostname, name); 67036549Sbostic else 67136647Skarels syslog(LOG_NOTICE, "%d LOGIN FAILURE%s ON %s, %s", 67236647Skarels failures, failures > 1 ? "S" : "", tty, name); 67336549Sbostic } 67436549Sbostic 6752822Swnj #undef UNKNOWN 67636460Sbostic #define UNKNOWN "su" 6771043Sbill 6781043Sbill char * 67936647Skarels stypeof(ttyid) 68036647Skarels char *ttyid; 6811043Sbill { 68236460Sbostic struct ttyent *t; 6831043Sbill 68436647Skarels return(ttyid && (t = getttynam(ttyid)) ? t->ty_type : UNKNOWN); 6851043Sbill } 6866005Swnj 6876005Swnj getstr(buf, cnt, err) 68836460Sbostic char *buf, *err; 6896005Swnj int cnt; 6906005Swnj { 69136460Sbostic char ch; 6926005Swnj 6936005Swnj do { 69436460Sbostic if (read(0, &ch, sizeof(ch)) != sizeof(ch)) 6956005Swnj exit(1); 6966005Swnj if (--cnt < 0) { 69737203Sbostic (void)fprintf(stderr, "%s too long\r\n", err); 69836460Sbostic sleepexit(1); 6996005Swnj } 70036460Sbostic *buf++ = ch; 70136460Sbostic } while (ch); 7026005Swnj } 7036329Swnj 70436460Sbostic sleepexit(eval) 70536460Sbostic int eval; 70626862Smckusick { 70736460Sbostic sleep((u_int)5); 70836460Sbostic exit(eval); 70926862Smckusick } 710