112678Ssam #ifndef lint 2*13074Ssam static char *sccsid = "@(#)login.c 4.30 (Berkeley) 83/06/13"; 312678Ssam #endif 412678Ssam 51043Sbill /* 61043Sbill * login [ name ] 712687Ssam * login -r hostname (for rlogind) 812687Ssam * login -h hostname (for telnetd, etc.) 91043Sbill */ 101043Sbill 1112984Ssam #include <sys/param.h> 1212687Ssam #include <sys/quota.h> 1312687Ssam #include <sys/stat.h> 1412687Ssam #include <sys/time.h> 1512687Ssam #include <sys/resource.h> 1612687Ssam 171043Sbill #include <sgtty.h> 181043Sbill #include <utmp.h> 191043Sbill #include <signal.h> 201043Sbill #include <pwd.h> 211043Sbill #include <stdio.h> 221043Sbill #include <lastlog.h> 2312678Ssam #include <errno.h> 241043Sbill 252822Swnj #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 262822Swnj 276197Sroot #define NMAX sizeof(utmp.ut_name) 281043Sbill 292822Swnj #define FALSE 0 302822Swnj #define TRUE -1 312822Swnj 322822Swnj char nolog[] = "/etc/nologin"; 332822Swnj char qlog[] = ".hushlogin"; 342822Swnj char securetty[] = "/etc/securetty"; 351043Sbill char maildir[30] = "/usr/spool/mail/"; 361043Sbill char lastlog[] = "/usr/adm/lastlog"; 379867Ssam struct passwd nouser = {"", "nope", -1, -1, -1, "", "", "", "" }; 381043Sbill struct sgttyb ttyb; 391043Sbill struct utmp utmp; 401043Sbill char minusnam[16] = "-"; 4112687Ssam /* 4212687Ssam * This bounds the time given to login. We initialize it here 4312687Ssam * so it can be patched on machines where it's too small. 4412687Ssam */ 4512687Ssam int timeout = 60; 466005Swnj 471043Sbill char homedir[64] = "HOME="; 481043Sbill char shell[64] = "SHELL="; 491043Sbill char term[64] = "TERM="; 502822Swnj char user[20] = "USER="; 516005Swnj 526005Swnj char *envinit[] = 5312687Ssam { homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user, 0 }; 546005Swnj 551043Sbill struct passwd *pwd; 561043Sbill struct passwd *getpwnam(); 576005Swnj char *strcat(), *rindex(), *index(); 581043Sbill int setpwent(); 5912687Ssam int timedout(); 601043Sbill char *ttyname(); 611043Sbill char *crypt(); 621043Sbill char *getpass(); 631043Sbill char *stypeof(); 641043Sbill extern char **environ; 6512678Ssam extern int errno; 661043Sbill 67*13074Ssam struct tchars tc = { 68*13074Ssam CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK 691365Sbill }; 70*13074Ssam struct ltchars ltc = { 71*13074Ssam CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT 72*13074Ssam }; 731365Sbill 746005Swnj int rflag; 756197Sroot char rusername[NMAX+1], lusername[NMAX+1]; 766005Swnj char rpassword[NMAX+1]; 776878Smckusick char name[NMAX+1]; 786197Sroot char *rhost; 796005Swnj 801043Sbill main(argc, argv) 8112687Ssam char *argv[]; 821043Sbill { 831043Sbill register char *namep; 8412687Ssam int t, f, c, i; 8512687Ssam int invalid, quietlog; 862822Swnj FILE *nlfd; 871043Sbill char *ttyn; 886197Sroot int ldisc = 0, zero = 0; 891043Sbill 9012687Ssam signal(SIGALRM, timedout); 9112687Ssam alarm(timeout); 921043Sbill signal(SIGQUIT, SIG_IGN); 931043Sbill signal(SIGINT, SIG_IGN); 9412687Ssam setpriority(PRIO_PROCESS, 0, 0); 9512678Ssam quota(Q_SETUID, 0, 0, 0); 9612687Ssam /* 9712687Ssam * -r is used by rlogind to cause the autologin protocol; 9812687Ssam * -h is used by other servers to pass the name of the 9912687Ssam * remote host to login so that it may be placed in utmp and wtmp 10012687Ssam */ 10112687Ssam if (argc > 1) { 10212687Ssam if (strcmp(argv[1], "-r") == 0) { 10312687Ssam rflag = doremotelogin(argv[2]); 10412687Ssam SCPYN(utmp.ut_host, argv[2]); 10512687Ssam argc = 0; 1066197Sroot } 10712687Ssam if (strcmp(argv[1], "-h") == 0 && getuid() == 0) { 10812687Ssam SCPYN(utmp.ut_host, argv[2]); 10912687Ssam argc = 0; 1106197Sroot } 1116005Swnj } 112*13074Ssam ioctl(0, TIOCLSET, &zero); 1131547Sbill ioctl(0, TIOCNXCL, 0); 1146329Swnj ioctl(0, FIONBIO, &zero); 1156329Swnj ioctl(0, FIOASYNC, &zero); 116*13074Ssam ioctl(0, TIOCGETP, &ttyb); 11712687Ssam /* 11812687Ssam * If talking to an rlogin process, 11912687Ssam * propagate the terminal type and 12012687Ssam * baud rate across the network. 12112687Ssam */ 12212687Ssam if (rflag) 12312687Ssam doremoteterm(term, &ttyb); 124*13074Ssam ioctl(0, TIOCSLTC, <c); 125*13074Ssam ioctl(0, TIOCSETC, &tc); 126*13074Ssam ioctl(0, TIOCSETP, &ttyb); 12712687Ssam for (t = getdtablesize(); t > 3; t--) 1281043Sbill close(t); 1291043Sbill ttyn = ttyname(0); 1302822Swnj if (ttyn==(char *)0) 1311043Sbill ttyn = "/dev/tty??"; 1322822Swnj do { 1332822Swnj ldisc = 0; 1342822Swnj ioctl(0, TIOCSETD, &ldisc); 1352822Swnj invalid = FALSE; 1362822Swnj SCPYN(utmp.ut_name, ""); 13712687Ssam /* 13812687Ssam * Name specified, take it. 13912687Ssam */ 14012687Ssam if (argc > 1) { 1412822Swnj SCPYN(utmp.ut_name, argv[1]); 1422822Swnj argc = 0; 1431043Sbill } 14412687Ssam /* 14512687Ssam * If remote login take given name, 14612687Ssam * otherwise prompt user for something. 14712687Ssam */ 1486329Swnj if (rflag) { 1499867Ssam SCPYN(utmp.ut_name, lusername); 15012687Ssam /* autologin failed, prompt for passwd */ 1516329Swnj if (rflag == -1) 1526329Swnj rflag = 0; 15312687Ssam } else { 15412687Ssam getloginname(&utmp); 1552822Swnj } 1562822Swnj if (!strcmp(pwd->pw_shell, "/bin/csh")) { 1572822Swnj ldisc = NTTYDISC; 1582822Swnj ioctl(0, TIOCSETD, &ldisc); 1592822Swnj } 16012687Ssam /* 16112687Ssam * If no remote login authentication and 16212687Ssam * a password exists for this user, prompt 16312687Ssam * for one and verify it. 16412687Ssam */ 16512687Ssam if (!rflag && *pwd->pw_passwd != '\0') { 16612687Ssam char *pp; 16712687Ssam 16812687Ssam setpriority(PRIO_PROCESS, 0, -4); 16912687Ssam pp = getpass("Password:"); 17012687Ssam namep = crypt(pp, pwd->pw_passwd); 17112687Ssam setpriority(PRIO_PROCESS, 0, 0); 17212687Ssam if (strcmp(namep, pwd->pw_passwd)) 17312687Ssam invalid = TRUE; 1742822Swnj } 17512687Ssam /* 17612687Ssam * If user not super-user, check for logins disabled. 17712687Ssam */ 1782822Swnj if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) { 1792822Swnj while ((c = getc(nlfd)) != EOF) 1802822Swnj putchar(c); 1812822Swnj fflush(stdout); 1822822Swnj sleep(5); 1832822Swnj exit(0); 1842822Swnj } 18512687Ssam /* 18612687Ssam * If valid so far and root is logging in, 18712687Ssam * see if root logins on this terminal are permitted. 18812687Ssam */ 1892822Swnj if (!invalid && pwd->pw_uid == 0 && 1902822Swnj !rootterm(ttyn+sizeof("/dev/")-1)) { 1916329Swnj logerr("ROOT LOGIN REFUSED %s", 1926329Swnj ttyn+sizeof("/dev/")-1); 1932822Swnj invalid = TRUE; 1942822Swnj } 1952822Swnj if (invalid) { 1961043Sbill printf("Login incorrect\n"); 1976329Swnj if (ttyn[sizeof("/dev/tty")-1] == 'd') 1986329Swnj logerr("BADDIALUP %s %s\n", 1996329Swnj ttyn+sizeof("/dev/")-1, utmp.ut_name); 2001043Sbill } 2012822Swnj if (*pwd->pw_shell == '\0') 2022822Swnj pwd->pw_shell = "/bin/sh"; 2032822Swnj i = strlen(pwd->pw_shell); 2042822Swnj if (chdir(pwd->pw_dir) < 0 && !invalid ) { 2052822Swnj if (chdir("/") < 0) { 2062822Swnj printf("No directory!\n"); 2072822Swnj invalid = TRUE; 2082822Swnj } else { 20912687Ssam printf("No directory! %s\n", 21012687Ssam "Logging in with home=/"); 2112822Swnj pwd->pw_dir = "/"; 2122822Swnj } 2131043Sbill } 21412687Ssam /* 21512687Ssam * Remote login invalid must have been because 21612687Ssam * of a restriction of some sort, no extra chances. 21712687Ssam */ 2186005Swnj if (rflag && invalid) 2196005Swnj exit(1); 2202822Swnj } while (invalid); 22112687Ssam /* committed to login turn off timeout */ 22212687Ssam alarm(0); 2231043Sbill 22412678Ssam if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0) { 22512678Ssam if (errno == EUSERS) 22612678Ssam printf("%s.\n%s.\n", 22712678Ssam "Too many users logged on already", 22812678Ssam "Try again later"); 22912678Ssam else if (errno == EPROCLIM) 23012678Ssam printf("You have too many processes running.\n"); 23112678Ssam else 23212678Ssam perror("setuid"); 23312678Ssam sleep(5); 23412678Ssam exit(0); 23512678Ssam } 2361043Sbill time(&utmp.ut_time); 2371043Sbill t = ttyslot(); 23812687Ssam if (t > 0 && (f = open("/etc/utmp", 1)) >= 0) { 2391043Sbill lseek(f, (long)(t*sizeof(utmp)), 0); 2401043Sbill SCPYN(utmp.ut_line, rindex(ttyn, '/')+1); 2411043Sbill write(f, (char *)&utmp, sizeof(utmp)); 2421043Sbill close(f); 2431043Sbill } 24412687Ssam if (t > 0 && (f = open("/usr/adm/wtmp", 1)) >= 0) { 2451043Sbill lseek(f, 0L, 2); 2461043Sbill write(f, (char *)&utmp, sizeof(utmp)); 2471043Sbill close(f); 2481043Sbill } 24912687Ssam quietlog = access(qlog, 0) == 0; 2506329Swnj if ((f = open(lastlog, 2)) >= 0) { 2512822Swnj struct lastlog ll; 2522822Swnj 2532822Swnj lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); 2542822Swnj if (read(f, (char *) &ll, sizeof ll) == sizeof ll && 25512687Ssam ll.ll_time != 0 && !quietlog) { 25612687Ssam printf("Last login: %.*s ", 25712687Ssam 24-5, (char *)ctime(&ll.ll_time)); 25812687Ssam if (*ll.ll_host != '\0') 25912687Ssam printf("from %.*s\n", 26012687Ssam sizeof (ll.ll_host), ll.ll_host); 26112687Ssam else 26212687Ssam printf("on %.*s\n", 26312687Ssam sizeof (ll.ll_line), ll.ll_line); 2642822Swnj } 2652822Swnj lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); 2662822Swnj time(&ll.ll_time); 2672822Swnj SCPYN(ll.ll_line, rindex(ttyn, '/')+1); 26812687Ssam SCPYN(ll.ll_host, utmp.ut_host); 2692822Swnj write(f, (char *) &ll, sizeof ll); 2702822Swnj close(f); 2712822Swnj } 2721043Sbill chown(ttyn, pwd->pw_uid, pwd->pw_gid); 2739867Ssam chmod(ttyn, 0622); 2741043Sbill setgid(pwd->pw_gid); 2756878Smckusick strncpy(name, utmp.ut_name, NMAX); 2766878Smckusick name[NMAX] = '\0'; 2779224Ssam initgroups(name, pwd->pw_gid); 27812678Ssam quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0); 2791043Sbill setuid(pwd->pw_uid); 2801043Sbill environ = envinit; 2811043Sbill strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 2821043Sbill strncat(shell, pwd->pw_shell, sizeof(shell)-7); 2836329Swnj if (term[strlen("TERM=")] == 0) 2846005Swnj strncat(term, stypeof(ttyn), sizeof(term)-6); 2852822Swnj strncat(user, pwd->pw_name, sizeof(user)-6); 2861043Sbill if ((namep = rindex(pwd->pw_shell, '/')) == NULL) 2871043Sbill namep = pwd->pw_shell; 2881043Sbill else 2891043Sbill namep++; 2901043Sbill strcat(minusnam, namep); 2916197Sroot umask(022); 2926329Swnj if (ttyn[sizeof("/dev/tty")-1] == 'd') 2936329Swnj logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name); 2946329Swnj if (!quietlog) { 2952822Swnj showmotd(); 2962822Swnj strcat(maildir, pwd->pw_name); 2972822Swnj if (access(maildir,4)==0) { 2982822Swnj struct stat statb; 2992822Swnj stat(maildir, &statb); 3002822Swnj if (statb.st_size) 3012822Swnj printf("You have mail.\n"); 3022822Swnj } 3032822Swnj } 30412687Ssam signal(SIGALRM, SIG_DFL); 3051043Sbill signal(SIGQUIT, SIG_DFL); 3061043Sbill signal(SIGINT, SIG_DFL); 3073935Sroot signal(SIGTSTP, SIG_IGN); 3081043Sbill execlp(pwd->pw_shell, minusnam, 0); 3092822Swnj perror(pwd->pw_shell); 3101043Sbill printf("No shell\n"); 3111043Sbill exit(0); 3121043Sbill } 3131043Sbill 31412687Ssam getloginname(up) 31512687Ssam register struct utmp *up; 31612687Ssam { 31712687Ssam register char *namep; 31812712Ssam char c; 31912687Ssam 32012687Ssam while (up->ut_name[0] == '\0') { 32112687Ssam namep = utmp.ut_name; 32212712Ssam printf("login: "); 32312687Ssam while ((c = getchar()) != '\n') { 32412687Ssam if (c == ' ') 32512687Ssam c = '_'; 32612687Ssam if (c == EOF) 32712687Ssam exit(0); 32812687Ssam if (namep < up->ut_name+NMAX) 32912687Ssam *namep++ = c; 33012687Ssam } 33112687Ssam } 33212687Ssam setpwent(); 33312687Ssam if ((pwd = getpwnam(utmp.ut_name)) == NULL) 33412687Ssam pwd = &nouser; 33512687Ssam endpwent(); 33612687Ssam } 33712687Ssam 33812687Ssam timedout() 33912687Ssam { 34012687Ssam 34112687Ssam printf("Login timed out after %d seconds\n", timeout); 34212687Ssam exit(0); 34312687Ssam } 34412687Ssam 3451043Sbill int stopmotd; 3461043Sbill catch() 3471043Sbill { 3486466Swnj 3491043Sbill signal(SIGINT, SIG_IGN); 3501043Sbill stopmotd++; 3511043Sbill } 3521043Sbill 3532822Swnj rootterm(tty) 3546466Swnj char *tty; 3552822Swnj { 3562822Swnj register FILE *fd; 3576466Swnj char buf[100]; 3582822Swnj 3592822Swnj if ((fd = fopen(securetty, "r")) == NULL) 3602822Swnj return(1); 3612822Swnj while (fgets(buf, sizeof buf, fd) != NULL) { 3622822Swnj buf[strlen(buf)-1] = '\0'; 3632822Swnj if (strcmp(tty, buf) == 0) { 3642822Swnj fclose(fd); 3652822Swnj return(1); 3662822Swnj } 3672822Swnj } 3682822Swnj fclose(fd); 3692822Swnj return(0); 3702822Swnj } 3712822Swnj 3721043Sbill showmotd() 3731043Sbill { 3741043Sbill FILE *mf; 3751043Sbill register c; 3761043Sbill 3771043Sbill signal(SIGINT, catch); 3782822Swnj if ((mf = fopen("/etc/motd","r")) != NULL) { 3792822Swnj while ((c = getc(mf)) != EOF && stopmotd == 0) 3801043Sbill putchar(c); 3811043Sbill fclose(mf); 3821043Sbill } 3831043Sbill signal(SIGINT, SIG_IGN); 3841043Sbill } 3851043Sbill 3862822Swnj #undef UNKNOWN 3871043Sbill #define UNKNOWN "su" 3881043Sbill 3891043Sbill char * 3901043Sbill stypeof(ttyid) 39112687Ssam char *ttyid; 3921043Sbill { 39312687Ssam static char typebuf[16]; 39412687Ssam char buf[50]; 39512687Ssam register FILE *f; 39612687Ssam register char *p, *t, *q; 3971043Sbill 3981043Sbill if (ttyid == NULL) 3991043Sbill return (UNKNOWN); 4001043Sbill f = fopen("/etc/ttytype", "r"); 4011043Sbill if (f == NULL) 4021043Sbill return (UNKNOWN); 4031043Sbill /* split off end of name */ 4041043Sbill for (p = q = ttyid; *p != 0; p++) 4051043Sbill if (*p == '/') 4061043Sbill q = p + 1; 4071043Sbill 4081043Sbill /* scan the file */ 40912687Ssam while (fgets(buf, sizeof buf, f) != NULL) { 41012687Ssam for (t = buf; *t != ' ' && *t != '\t'; t++) 4111043Sbill ; 4121043Sbill *t++ = 0; 4132822Swnj while (*t == ' ' || *t == '\t') 4142822Swnj t++; 41512687Ssam for (p = t; *p > ' '; p++) 4161043Sbill ; 4171043Sbill *p = 0; 41812687Ssam if (strcmp(q,t) == 0) { 4191043Sbill strcpy(typebuf, buf); 4201043Sbill fclose(f); 4211043Sbill return (typebuf); 4221043Sbill } 4231043Sbill } 4241043Sbill fclose (f); 4251043Sbill return (UNKNOWN); 4261043Sbill } 4276005Swnj 42812687Ssam doremotelogin(host) 42912687Ssam char *host; 43012687Ssam { 43112687Ssam FILE *hostf; 43212687Ssam int first = 1; 43312687Ssam 43412687Ssam getstr(rusername, sizeof (rusername), "remuser"); 43512687Ssam getstr(lusername, sizeof (lusername), "locuser"); 43612687Ssam getstr(term+5, sizeof(term)-5, "Terminal type"); 43712687Ssam if (getuid()) 43812687Ssam goto bad; 43912687Ssam setpwent(); 44012687Ssam pwd = getpwnam(lusername); 44112687Ssam endpwent(); 44212687Ssam if (pwd == NULL) 44312687Ssam goto bad; 44412687Ssam hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0; 44512687Ssam again: 44612687Ssam if (hostf) { 44712687Ssam char ahost[32]; 44812687Ssam 44912687Ssam while (fgets(ahost, sizeof (ahost), hostf)) { 45012687Ssam char *user; 45112687Ssam 45212687Ssam if ((user = index(ahost, '\n')) != 0) 45312687Ssam *user++ = '\0'; 45412687Ssam if ((user = index(ahost, ' ')) != 0) 45512687Ssam *user++ = '\0'; 45612687Ssam if (!strcmp(host, ahost) && 45712687Ssam !strcmp(rusername, user ? user : lusername)) { 45812687Ssam fclose(hostf); 45912687Ssam return (1); 46012687Ssam } 46112687Ssam } 46212687Ssam fclose(hostf); 46312687Ssam } 46412687Ssam if (first == 1) { 46512687Ssam char *rhosts = ".rhosts"; 46612687Ssam struct stat sbuf; 46712687Ssam 46812687Ssam first = 0; 46912687Ssam if (chdir(pwd->pw_dir) < 0) 47012687Ssam goto again; 47112687Ssam if (lstat(rhosts, &sbuf) < 0) 47212687Ssam goto again; 47312687Ssam if ((sbuf.st_mode & S_IFMT) == S_IFLNK) { 47412687Ssam printf("login: .rhosts is a soft link.\r\n"); 47512687Ssam goto bad; 47612687Ssam } 47712687Ssam hostf = fopen(rhosts, "r"); 47812687Ssam fstat(fileno(hostf), &sbuf); 47912687Ssam if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) { 48012687Ssam printf("login: Bad .rhosts ownership.\r\n"); 48112687Ssam fclose(hostf); 48212687Ssam goto bad; 48312687Ssam } 48412687Ssam goto again; 48512687Ssam } 48612687Ssam bad: 48712687Ssam return (-1); 48812687Ssam } 48912687Ssam 4906005Swnj getstr(buf, cnt, err) 4916005Swnj char *buf; 4926005Swnj int cnt; 4936005Swnj char *err; 4946005Swnj { 4956005Swnj char c; 4966005Swnj 4976005Swnj do { 4986005Swnj if (read(0, &c, 1) != 1) 4996005Swnj exit(1); 5006005Swnj if (--cnt < 0) { 5016005Swnj printf("%s too long\r\n", err); 5026005Swnj exit(1); 5036005Swnj } 5046005Swnj *buf++ = c; 5056005Swnj } while (c != 0); 5066005Swnj } 5076329Swnj 50812687Ssam char *speeds[] = 50912687Ssam { "0", "50", "75", "110", "134", "150", "200", "300", 51012687Ssam "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" }; 51112687Ssam #define NSPEEDS (sizeof (speeds) / sizeof (speeds[0])) 51212687Ssam 51312687Ssam doremoteterm(term, tp) 51412687Ssam char *term; 51512687Ssam struct sgttyb *tp; 51612687Ssam { 51712687Ssam char *cp = index(term, '/'); 51812687Ssam register int i; 51912687Ssam 52012687Ssam if (cp) { 52112687Ssam *cp++ = 0; 52212687Ssam for (i = 0; i < NSPEEDS; i++) 52312687Ssam if (!strcmp(speeds[i], cp)) { 52412687Ssam tp->sg_ispeed = tp->sg_ospeed = i; 52512687Ssam break; 52612687Ssam } 52712687Ssam } 52812687Ssam tp->sg_flags = ECHO|CRMOD|ANYP|XTABS; 52912687Ssam } 53012687Ssam 5316329Swnj logerr(fmt, a1, a2, a3) 5326329Swnj char *fmt, *a1, *a2, *a3; 5336329Swnj { 53412690Ssam #ifdef LOGERR 53512690Ssam FILE *cons = fopen("/dev/console", "w"); 5366329Swnj 53712690Ssam if (cons != NULL) { 53812690Ssam fprintf(cons, fmt, a1, a2, a3); 53912690Ssam fputc('\r', cons); 54012690Ssam fclose(cons); 54112690Ssam } 54212690Ssam #endif 5436329Swnj } 544