1*9867Ssam static char *sccsid = "@(#)login.c 4.20 82/12/21"; 21043Sbill /* 31043Sbill * login [ name ] 46005Swnj * login -r 51043Sbill */ 61043Sbill 71043Sbill #include <sys/types.h> 81043Sbill #include <sgtty.h> 91043Sbill #include <utmp.h> 101043Sbill #include <signal.h> 111043Sbill #include <pwd.h> 121043Sbill #include <stdio.h> 131043Sbill #include <sys/stat.h> 141043Sbill #include <lastlog.h> 151043Sbill 162822Swnj #define SCPYN(a, b) strncpy(a, b, sizeof(a)) 172822Swnj 186197Sroot #define NMAX sizeof(utmp.ut_name) 196197Sroot #define LMAX sizeof(utmp.ut_line) 201043Sbill 212822Swnj #define FALSE 0 222822Swnj #define TRUE -1 232822Swnj 242822Swnj char nolog[] = "/etc/nologin"; 252822Swnj char qlog[] = ".hushlogin"; 262822Swnj char securetty[] = "/etc/securetty"; 271043Sbill char maildir[30] = "/usr/spool/mail/"; 281043Sbill char lastlog[] = "/usr/adm/lastlog"; 29*9867Ssam struct passwd nouser = {"", "nope", -1, -1, -1, "", "", "", "" }; 301043Sbill struct sgttyb ttyb; 311043Sbill struct utmp utmp; 321043Sbill char minusnam[16] = "-"; 336005Swnj 341043Sbill char homedir[64] = "HOME="; 351043Sbill char shell[64] = "SHELL="; 361043Sbill char term[64] = "TERM="; 372822Swnj char user[20] = "USER="; 386005Swnj char *speeds[] = 396005Swnj { "0", "50", "75", "110", "134", "150", "200", "300", 406005Swnj "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" }; 416005Swnj #define NSPEEDS (sizeof (speeds) / sizeof (speeds[0])) 426005Swnj 436005Swnj char *envinit[] = 446005Swnj {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user, 0}; 456005Swnj 461043Sbill struct passwd *pwd; 471043Sbill struct passwd *getpwnam(); 486005Swnj char *strcat(), *rindex(), *index(); 491043Sbill int setpwent(); 501043Sbill char *ttyname(); 511043Sbill char *crypt(); 521043Sbill char *getpass(); 531043Sbill char *rindex(); 541043Sbill char *stypeof(); 551043Sbill extern char **environ; 561043Sbill 57*9867Ssam struct ttychars tc = { 58*9867Ssam CERASE, CKILL, CINTR, CQUIT, CSTART, 59*9867Ssam CSTOP, CEOF, CBRK, CSUSP, CDSUSP, 60*9867Ssam CRPRNT, CFLUSH, CWERASE,CLNEXT 611365Sbill }; 621365Sbill 636005Swnj int rflag; 646197Sroot char rusername[NMAX+1], lusername[NMAX+1]; 656005Swnj char rpassword[NMAX+1]; 666878Smckusick char name[NMAX+1]; 676197Sroot char *rhost; 686005Swnj 691043Sbill main(argc, argv) 701043Sbill char **argv; 711043Sbill { 721043Sbill register char *namep; 731043Sbill int t, f, c; 742822Swnj int invalid; 752822Swnj int quietlog; 762822Swnj int i; 772822Swnj FILE *nlfd; 781043Sbill char *ttyn; 796197Sroot int ldisc = 0, zero = 0; 806197Sroot FILE *hostf; int first = 1; 811043Sbill 821043Sbill alarm(60); 831043Sbill signal(SIGQUIT, SIG_IGN); 841043Sbill signal(SIGINT, SIG_IGN); 851043Sbill nice(-100); 861043Sbill nice(20); 871043Sbill nice(0); 886329Swnj if (argc > 1 && !strcmp(argv[1], "-r")) { 896005Swnj rflag++; 906329Swnj rhost = argv[2]; 916197Sroot argc = 1; 926329Swnj getstr(rusername, sizeof (rusername), "remuser"); 936329Swnj getstr(lusername, sizeof (lusername), "locuser"); 946005Swnj getstr(term+5, sizeof(term)-5, "Terminal type"); 956329Swnj if (getuid()) 966329Swnj goto abnormal; 976197Sroot setpwent(); 986197Sroot pwd = getpwnam(lusername); 996329Swnj endpwent(); 1006197Sroot if (pwd == NULL) { 1016329Swnj if (strcmp(rusername, lusername)) 1026329Swnj printf("%s: No such user\r\n", lusername); 1036329Swnj goto abnormal; 1046197Sroot } 1056466Swnj hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0; 1066197Sroot again: 1076197Sroot if (hostf) { 1086197Sroot char ahost[32]; 1096197Sroot while (fgets(ahost, sizeof (ahost), hostf)) { 1106197Sroot char *user; 1116197Sroot if (index(ahost, '\n')) 1126197Sroot *index(ahost, '\n') = 0; 1136197Sroot user = index(ahost, ' '); 1146197Sroot if (user) 1156197Sroot *user++ = 0; 1166197Sroot if (!strcmp(rhost, ahost) && 1176329Swnj !strcmp(rusername, user ? user : lusername)) { 1186329Swnj fclose(hostf); 1196197Sroot goto normal; 1206329Swnj } 1216197Sroot } 1226197Sroot fclose(hostf); 1236197Sroot } 1246197Sroot if (first == 1) { 1256197Sroot first = 0; 1266197Sroot if (chdir(pwd->pw_dir) < 0) 1276197Sroot goto again; 1286197Sroot hostf = fopen(".rhosts", "r"); 1296197Sroot goto again; 1306197Sroot } 1316329Swnj abnormal: 1326197Sroot rhost = 0; 1336197Sroot rflag = -1; 1346005Swnj } 1356197Sroot normal: 136*9867Ssam ioctl(0, TIOCLSET, &zero); /* XXX */ 1371547Sbill ioctl(0, TIOCNXCL, 0); 1386329Swnj ioctl(0, FIONBIO, &zero); 1396329Swnj ioctl(0, FIOASYNC, &zero); 140*9867Ssam ioctl(0, TIOCGETP, &ttyb); /* XXX */ 1416005Swnj if (rflag) { 1426005Swnj char *cp = index(term, '/'); 1436005Swnj if (cp) { 1446005Swnj int i; 1456005Swnj *cp++ = 0; 1466005Swnj for (i = 0; i < NSPEEDS; i++) 1476005Swnj if (!strcmp(speeds[i], cp)) { 1486005Swnj ttyb.sg_ispeed = ttyb.sg_ospeed = i; 1496005Swnj break; 1506005Swnj } 1516005Swnj } 1526005Swnj ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS; 1536005Swnj } 154*9867Ssam ioctl(0, TIOCSETP, &ttyb); /* XXX */ 155*9867Ssam ioctl(0, TIOCCSET, &tc); 1561043Sbill for (t=3; t<20; t++) 1571043Sbill close(t); 1581043Sbill ttyn = ttyname(0); 1592822Swnj if (ttyn==(char *)0) 1601043Sbill ttyn = "/dev/tty??"; 1612822Swnj do { 1622822Swnj ldisc = 0; 1632822Swnj ioctl(0, TIOCSETD, &ldisc); 1642822Swnj invalid = FALSE; 1652822Swnj SCPYN(utmp.ut_name, ""); 1662822Swnj if (argc>1) { 1672822Swnj SCPYN(utmp.ut_name, argv[1]); 1682822Swnj argc = 0; 1691043Sbill } 1706329Swnj if (rflag) { 171*9867Ssam SCPYN(utmp.ut_name, lusername); 1726329Swnj if (rflag == -1) 1736329Swnj rflag = 0; 1746329Swnj } else 1756197Sroot while (utmp.ut_name[0] == '\0') { 1766197Sroot namep = utmp.ut_name; 1776197Sroot { char hostname[32]; 1786197Sroot gethostname(hostname, sizeof (hostname)); 1796197Sroot printf("%s login: ", hostname); } 1806197Sroot while ((c = getchar()) != '\n') { 1816197Sroot if (c == ' ') 1826197Sroot c = '_'; 1836197Sroot if (c == EOF) 1846197Sroot exit(0); 1856197Sroot if (namep < utmp.ut_name+NMAX) 1866197Sroot *namep++ = c; 1876197Sroot } 1882822Swnj } 1896197Sroot if (rhost == 0) { 1906197Sroot setpwent(); 1916197Sroot if ((pwd = getpwnam(utmp.ut_name)) == NULL) 1926197Sroot pwd = &nouser; 1936197Sroot endpwent(); 1942822Swnj } 1952822Swnj if (!strcmp(pwd->pw_shell, "/bin/csh")) { 1962822Swnj ldisc = NTTYDISC; 1972822Swnj ioctl(0, TIOCSETD, &ldisc); 1982822Swnj } 1996197Sroot if (rhost == 0) { 2006197Sroot if (*pwd->pw_passwd != '\0') { 2016197Sroot char *pp; 2026197Sroot nice(-4); 2036197Sroot if (rflag == 0) 2046197Sroot pp = getpass("Password:"); 2056197Sroot else 2066197Sroot pp = rpassword; 2076197Sroot namep = crypt(pp,pwd->pw_passwd); 2086197Sroot nice(4); 2096197Sroot if (strcmp(namep, pwd->pw_passwd)) 2106197Sroot invalid = TRUE; 2116197Sroot } 2122822Swnj } 2132822Swnj if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) { 2142822Swnj /* logins are disabled except for root */ 2152822Swnj while ((c = getc(nlfd)) != EOF) 2162822Swnj putchar(c); 2172822Swnj fflush(stdout); 2182822Swnj sleep(5); 2192822Swnj exit(0); 2202822Swnj } 2212822Swnj if (!invalid && pwd->pw_uid == 0 && 2222822Swnj !rootterm(ttyn+sizeof("/dev/")-1)) { 2236329Swnj logerr("ROOT LOGIN REFUSED %s", 2246329Swnj ttyn+sizeof("/dev/")-1); 2252822Swnj invalid = TRUE; 2262822Swnj } 2272822Swnj if (invalid) { 2281043Sbill printf("Login incorrect\n"); 2296329Swnj if (ttyn[sizeof("/dev/tty")-1] == 'd') 2306329Swnj logerr("BADDIALUP %s %s\n", 2316329Swnj ttyn+sizeof("/dev/")-1, utmp.ut_name); 2321043Sbill } 2332822Swnj if (*pwd->pw_shell == '\0') 2342822Swnj pwd->pw_shell = "/bin/sh"; 2352822Swnj i = strlen(pwd->pw_shell); 2362822Swnj if (chdir(pwd->pw_dir) < 0 && !invalid ) { 2372822Swnj if (chdir("/") < 0) { 2382822Swnj printf("No directory!\n"); 2392822Swnj invalid = TRUE; 2402822Swnj } else { 2412822Swnj printf("No directory! Logging in with home=/\n"); 2422822Swnj pwd->pw_dir = "/"; 2432822Swnj } 2441043Sbill } 2456005Swnj if (rflag && invalid) 2466005Swnj exit(1); 2472822Swnj } while (invalid); 2481043Sbill 2496005Swnj 2501043Sbill time(&utmp.ut_time); 2511043Sbill t = ttyslot(); 2521043Sbill if (t>0 && (f = open("/etc/utmp", 1)) >= 0) { 2531043Sbill lseek(f, (long)(t*sizeof(utmp)), 0); 2541043Sbill SCPYN(utmp.ut_line, rindex(ttyn, '/')+1); 2551043Sbill write(f, (char *)&utmp, sizeof(utmp)); 2561043Sbill close(f); 2571043Sbill } 2581043Sbill if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) { 2591043Sbill lseek(f, 0L, 2); 2601043Sbill write(f, (char *)&utmp, sizeof(utmp)); 2611043Sbill close(f); 2621043Sbill } 2636329Swnj quietlog = 0; 2642822Swnj if (access(qlog, 0) == 0) 2656329Swnj quietlog = 1; 2666329Swnj if ((f = open(lastlog, 2)) >= 0) { 2672822Swnj struct lastlog ll; 2682822Swnj 2692822Swnj lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); 2702822Swnj if (read(f, (char *) &ll, sizeof ll) == sizeof ll && 2712822Swnj ll.ll_time != 0) { 2726329Swnj if (quietlog == 0) 2732822Swnj printf("Last login: %.*s on %.*s\n" 2742822Swnj , 24-5 2752822Swnj , (char *) ctime(&ll.ll_time) 2762822Swnj , sizeof(ll.ll_line) 2772822Swnj , ll.ll_line 2782822Swnj ); 2792822Swnj } 2802822Swnj lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); 2812822Swnj time(&ll.ll_time); 2822822Swnj SCPYN(ll.ll_line, rindex(ttyn, '/')+1); 2832822Swnj write(f, (char *) &ll, sizeof ll); 2842822Swnj close(f); 2852822Swnj } 2861043Sbill chown(ttyn, pwd->pw_uid, pwd->pw_gid); 287*9867Ssam chmod(ttyn, 0622); 2881043Sbill setgid(pwd->pw_gid); 2896878Smckusick strncpy(name, utmp.ut_name, NMAX); 2906878Smckusick name[NMAX] = '\0'; 2919224Ssam initgroups(name, pwd->pw_gid); 2921043Sbill setuid(pwd->pw_uid); 2931043Sbill environ = envinit; 2941043Sbill strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 2951043Sbill strncat(shell, pwd->pw_shell, sizeof(shell)-7); 2966329Swnj if (term[strlen("TERM=")] == 0) 2976005Swnj strncat(term, stypeof(ttyn), sizeof(term)-6); 2982822Swnj strncat(user, pwd->pw_name, sizeof(user)-6); 2991043Sbill if ((namep = rindex(pwd->pw_shell, '/')) == NULL) 3001043Sbill namep = pwd->pw_shell; 3011043Sbill else 3021043Sbill namep++; 3031043Sbill strcat(minusnam, namep); 3041043Sbill alarm(0); 3056197Sroot umask(022); 3066329Swnj if (ttyn[sizeof("/dev/tty")-1] == 'd') 3076329Swnj logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name); 3086329Swnj if (!quietlog) { 3092822Swnj showmotd(); 3102822Swnj strcat(maildir, pwd->pw_name); 3112822Swnj if (access(maildir,4)==0) { 3122822Swnj struct stat statb; 3132822Swnj stat(maildir, &statb); 3142822Swnj if (statb.st_size) 3152822Swnj printf("You have mail.\n"); 3162822Swnj } 3172822Swnj } 3182822Swnj 3191043Sbill signal(SIGQUIT, SIG_DFL); 3201043Sbill signal(SIGINT, SIG_DFL); 3213935Sroot signal(SIGTSTP, SIG_IGN); 3221043Sbill execlp(pwd->pw_shell, minusnam, 0); 3232822Swnj perror(pwd->pw_shell); 3241043Sbill printf("No shell\n"); 3251043Sbill exit(0); 3261043Sbill } 3271043Sbill 3281043Sbill int stopmotd; 3291043Sbill catch() 3301043Sbill { 3316466Swnj 3321043Sbill signal(SIGINT, SIG_IGN); 3331043Sbill stopmotd++; 3341043Sbill } 3351043Sbill 3362822Swnj rootterm(tty) 3376466Swnj char *tty; 3382822Swnj { 3392822Swnj register FILE *fd; 3406466Swnj char buf[100]; 3412822Swnj 3426466Swnj if (rflag) 3436466Swnj return(1); 3442822Swnj if ((fd = fopen(securetty, "r")) == NULL) 3452822Swnj return(1); 3462822Swnj while (fgets(buf, sizeof buf, fd) != NULL) { 3472822Swnj buf[strlen(buf)-1] = '\0'; 3482822Swnj if (strcmp(tty, buf) == 0) { 3492822Swnj fclose(fd); 3502822Swnj return(1); 3512822Swnj } 3522822Swnj } 3532822Swnj fclose(fd); 3542822Swnj return(0); 3552822Swnj } 3562822Swnj 3571043Sbill showmotd() 3581043Sbill { 3591043Sbill FILE *mf; 3601043Sbill register c; 3611043Sbill 3621043Sbill signal(SIGINT, catch); 3632822Swnj if ((mf = fopen("/etc/motd","r")) != NULL) { 3642822Swnj while ((c = getc(mf)) != EOF && stopmotd == 0) 3651043Sbill putchar(c); 3661043Sbill fclose(mf); 3671043Sbill } 3681043Sbill signal(SIGINT, SIG_IGN); 3691043Sbill } 3701043Sbill 3712822Swnj #undef UNKNOWN 3721043Sbill #define UNKNOWN "su" 3731043Sbill 3741043Sbill char * 3751043Sbill stypeof(ttyid) 3761043Sbill char *ttyid; 3771043Sbill { 3781043Sbill static char typebuf[16]; 3791043Sbill char buf[50]; 3801043Sbill register FILE *f; 3811043Sbill register char *p, *t, *q; 3821043Sbill 3831043Sbill if (ttyid == NULL) 3841043Sbill return (UNKNOWN); 3851043Sbill f = fopen("/etc/ttytype", "r"); 3861043Sbill if (f == NULL) 3871043Sbill return (UNKNOWN); 3881043Sbill /* split off end of name */ 3891043Sbill for (p = q = ttyid; *p != 0; p++) 3901043Sbill if (*p == '/') 3911043Sbill q = p + 1; 3921043Sbill 3931043Sbill /* scan the file */ 3941043Sbill while (fgets(buf, sizeof buf, f) != NULL) 3951043Sbill { 3962822Swnj for (t=buf; *t!=' ' && *t != '\t'; t++) 3971043Sbill ; 3981043Sbill *t++ = 0; 3992822Swnj while (*t == ' ' || *t == '\t') 4002822Swnj t++; 4011043Sbill for (p=t; *p>' '; p++) 4021043Sbill ; 4031043Sbill *p = 0; 4041043Sbill if (strcmp(q,t)==0) { 4051043Sbill strcpy(typebuf, buf); 4061043Sbill fclose(f); 4071043Sbill return (typebuf); 4081043Sbill } 4091043Sbill } 4101043Sbill fclose (f); 4111043Sbill return (UNKNOWN); 4121043Sbill } 4136005Swnj 4146005Swnj getstr(buf, cnt, err) 4156005Swnj char *buf; 4166005Swnj int cnt; 4176005Swnj char *err; 4186005Swnj { 4196005Swnj char c; 4206005Swnj 4216005Swnj do { 4226005Swnj if (read(0, &c, 1) != 1) 4236005Swnj exit(1); 4246005Swnj if (--cnt < 0) { 4256005Swnj printf("%s too long\r\n", err); 4266005Swnj exit(1); 4276005Swnj } 4286005Swnj *buf++ = c; 4296005Swnj } while (c != 0); 4306005Swnj } 4316329Swnj 4326329Swnj logerr(fmt, a1, a2, a3) 4336329Swnj char *fmt, *a1, *a2, *a3; 4346329Swnj { 4356329Swnj 4366329Swnj } 437