1*11990Sleres static char *sccsid = "@(#)login.c 4.22 83/04/18"; 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"; 299867Ssam 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 579867Ssam struct ttychars tc = { 589867Ssam CERASE, CKILL, CINTR, CQUIT, CSTART, 599867Ssam CSTOP, CEOF, CBRK, CSUSP, CDSUSP, 609867Ssam 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(); 100*11990Sleres if (pwd == NULL) 1016329Swnj goto abnormal; 1026466Swnj hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0; 1036197Sroot again: 1046197Sroot if (hostf) { 10511813Sleres char ahost[32]; 10611813Sleres 10711813Sleres while (fgets(ahost, sizeof (ahost), hostf)) { 10811813Sleres char *user; 10911813Sleres 11011813Sleres if (index(ahost, '\n')) 11111813Sleres *index(ahost, '\n') = 0; 11211813Sleres user = index(ahost, ' '); 11311813Sleres if (user) 11411813Sleres *user++ = 0; 11511813Sleres if (!strcmp(rhost, ahost) && 11611813Sleres !strcmp(rusername, user ? 11711813Sleres user : lusername)) { 11811813Sleres fclose(hostf); 11911813Sleres goto normal; 12011813Sleres } 1216329Swnj } 12211813Sleres fclose(hostf); 1236197Sroot } 1246197Sroot if (first == 1) { 12511813Sleres char *rhosts = ".rhosts"; 12611813Sleres struct stat sbuf; 12711813Sleres 1286197Sroot first = 0; 1296197Sroot if (chdir(pwd->pw_dir) < 0) 1306197Sroot goto again; 13111813Sleres if (lstat(rhosts, &sbuf) < 0) 13211813Sleres goto again; 13311813Sleres if ((sbuf.st_mode & S_IFMT) == S_IFLNK) { 13411813Sleres printf("login: .rhosts is a soft link.\r\n"); 13511813Sleres fclose(hostf); 13611813Sleres goto abnormal; 13711813Sleres } 13811813Sleres hostf = fopen(rhosts, "r"); 13911813Sleres fstat(fileno(hostf), &sbuf); 14011813Sleres if ((int) sbuf.st_uid != pwd->pw_uid && 14111813Sleres (int) sbuf.st_uid != 0) { 14211813Sleres printf("login: Bad .rhosts ownership.\r\n"); 14311813Sleres fclose(hostf); 14411813Sleres goto abnormal; 14511813Sleres } 14611813Sleres goto again; 1476197Sroot } 1486329Swnj abnormal: 1496197Sroot rhost = 0; 1506197Sroot rflag = -1; 1516005Swnj } 1526197Sroot normal: 1539867Ssam ioctl(0, TIOCLSET, &zero); /* XXX */ 1541547Sbill ioctl(0, TIOCNXCL, 0); 1556329Swnj ioctl(0, FIONBIO, &zero); 1566329Swnj ioctl(0, FIOASYNC, &zero); 1579867Ssam ioctl(0, TIOCGETP, &ttyb); /* XXX */ 1586005Swnj if (rflag) { 1596005Swnj char *cp = index(term, '/'); 1606005Swnj if (cp) { 1616005Swnj int i; 1626005Swnj *cp++ = 0; 1636005Swnj for (i = 0; i < NSPEEDS; i++) 1646005Swnj if (!strcmp(speeds[i], cp)) { 1656005Swnj ttyb.sg_ispeed = ttyb.sg_ospeed = i; 1666005Swnj break; 1676005Swnj } 1686005Swnj } 1696005Swnj ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS; 1706005Swnj } 1719867Ssam ioctl(0, TIOCSETP, &ttyb); /* XXX */ 1729867Ssam ioctl(0, TIOCCSET, &tc); 1731043Sbill for (t=3; t<20; t++) 1741043Sbill close(t); 1751043Sbill ttyn = ttyname(0); 1762822Swnj if (ttyn==(char *)0) 1771043Sbill ttyn = "/dev/tty??"; 1782822Swnj do { 1792822Swnj ldisc = 0; 1802822Swnj ioctl(0, TIOCSETD, &ldisc); 1812822Swnj invalid = FALSE; 1822822Swnj SCPYN(utmp.ut_name, ""); 1832822Swnj if (argc>1) { 1842822Swnj SCPYN(utmp.ut_name, argv[1]); 1852822Swnj argc = 0; 1861043Sbill } 1876329Swnj if (rflag) { 1889867Ssam SCPYN(utmp.ut_name, lusername); 1896329Swnj if (rflag == -1) 1906329Swnj rflag = 0; 1916329Swnj } else 1926197Sroot while (utmp.ut_name[0] == '\0') { 1936197Sroot namep = utmp.ut_name; 1946197Sroot { char hostname[32]; 1956197Sroot gethostname(hostname, sizeof (hostname)); 1966197Sroot printf("%s login: ", hostname); } 1976197Sroot while ((c = getchar()) != '\n') { 1986197Sroot if (c == ' ') 1996197Sroot c = '_'; 2006197Sroot if (c == EOF) 2016197Sroot exit(0); 2026197Sroot if (namep < utmp.ut_name+NMAX) 2036197Sroot *namep++ = c; 2046197Sroot } 2052822Swnj } 2066197Sroot if (rhost == 0) { 2076197Sroot setpwent(); 2086197Sroot if ((pwd = getpwnam(utmp.ut_name)) == NULL) 2096197Sroot pwd = &nouser; 2106197Sroot endpwent(); 2112822Swnj } 2122822Swnj if (!strcmp(pwd->pw_shell, "/bin/csh")) { 2132822Swnj ldisc = NTTYDISC; 2142822Swnj ioctl(0, TIOCSETD, &ldisc); 2152822Swnj } 2166197Sroot if (rhost == 0) { 2176197Sroot if (*pwd->pw_passwd != '\0') { 2186197Sroot char *pp; 2196197Sroot nice(-4); 2206197Sroot if (rflag == 0) 2216197Sroot pp = getpass("Password:"); 2226197Sroot else 2236197Sroot pp = rpassword; 2246197Sroot namep = crypt(pp,pwd->pw_passwd); 2256197Sroot nice(4); 2266197Sroot if (strcmp(namep, pwd->pw_passwd)) 2276197Sroot invalid = TRUE; 2286197Sroot } 2292822Swnj } 2302822Swnj if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) { 2312822Swnj /* logins are disabled except for root */ 2322822Swnj while ((c = getc(nlfd)) != EOF) 2332822Swnj putchar(c); 2342822Swnj fflush(stdout); 2352822Swnj sleep(5); 2362822Swnj exit(0); 2372822Swnj } 2382822Swnj if (!invalid && pwd->pw_uid == 0 && 2392822Swnj !rootterm(ttyn+sizeof("/dev/")-1)) { 2406329Swnj logerr("ROOT LOGIN REFUSED %s", 2416329Swnj ttyn+sizeof("/dev/")-1); 2422822Swnj invalid = TRUE; 2432822Swnj } 2442822Swnj if (invalid) { 2451043Sbill printf("Login incorrect\n"); 2466329Swnj if (ttyn[sizeof("/dev/tty")-1] == 'd') 2476329Swnj logerr("BADDIALUP %s %s\n", 2486329Swnj ttyn+sizeof("/dev/")-1, utmp.ut_name); 2491043Sbill } 2502822Swnj if (*pwd->pw_shell == '\0') 2512822Swnj pwd->pw_shell = "/bin/sh"; 2522822Swnj i = strlen(pwd->pw_shell); 2532822Swnj if (chdir(pwd->pw_dir) < 0 && !invalid ) { 2542822Swnj if (chdir("/") < 0) { 2552822Swnj printf("No directory!\n"); 2562822Swnj invalid = TRUE; 2572822Swnj } else { 2582822Swnj printf("No directory! Logging in with home=/\n"); 2592822Swnj pwd->pw_dir = "/"; 2602822Swnj } 2611043Sbill } 2626005Swnj if (rflag && invalid) 2636005Swnj exit(1); 2642822Swnj } while (invalid); 2651043Sbill 2666005Swnj 2671043Sbill time(&utmp.ut_time); 2681043Sbill t = ttyslot(); 2691043Sbill if (t>0 && (f = open("/etc/utmp", 1)) >= 0) { 2701043Sbill lseek(f, (long)(t*sizeof(utmp)), 0); 2711043Sbill SCPYN(utmp.ut_line, rindex(ttyn, '/')+1); 2721043Sbill write(f, (char *)&utmp, sizeof(utmp)); 2731043Sbill close(f); 2741043Sbill } 2751043Sbill if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) { 2761043Sbill lseek(f, 0L, 2); 2771043Sbill write(f, (char *)&utmp, sizeof(utmp)); 2781043Sbill close(f); 2791043Sbill } 2806329Swnj quietlog = 0; 2812822Swnj if (access(qlog, 0) == 0) 2826329Swnj quietlog = 1; 2836329Swnj if ((f = open(lastlog, 2)) >= 0) { 2842822Swnj struct lastlog ll; 2852822Swnj 2862822Swnj lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); 2872822Swnj if (read(f, (char *) &ll, sizeof ll) == sizeof ll && 2882822Swnj ll.ll_time != 0) { 2896329Swnj if (quietlog == 0) 2902822Swnj printf("Last login: %.*s on %.*s\n" 2912822Swnj , 24-5 2922822Swnj , (char *) ctime(&ll.ll_time) 2932822Swnj , sizeof(ll.ll_line) 2942822Swnj , ll.ll_line 2952822Swnj ); 2962822Swnj } 2972822Swnj lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); 2982822Swnj time(&ll.ll_time); 2992822Swnj SCPYN(ll.ll_line, rindex(ttyn, '/')+1); 3002822Swnj write(f, (char *) &ll, sizeof ll); 3012822Swnj close(f); 3022822Swnj } 3031043Sbill chown(ttyn, pwd->pw_uid, pwd->pw_gid); 3049867Ssam chmod(ttyn, 0622); 3051043Sbill setgid(pwd->pw_gid); 3066878Smckusick strncpy(name, utmp.ut_name, NMAX); 3076878Smckusick name[NMAX] = '\0'; 3089224Ssam initgroups(name, pwd->pw_gid); 3091043Sbill setuid(pwd->pw_uid); 3101043Sbill environ = envinit; 3111043Sbill strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 3121043Sbill strncat(shell, pwd->pw_shell, sizeof(shell)-7); 3136329Swnj if (term[strlen("TERM=")] == 0) 3146005Swnj strncat(term, stypeof(ttyn), sizeof(term)-6); 3152822Swnj strncat(user, pwd->pw_name, sizeof(user)-6); 3161043Sbill if ((namep = rindex(pwd->pw_shell, '/')) == NULL) 3171043Sbill namep = pwd->pw_shell; 3181043Sbill else 3191043Sbill namep++; 3201043Sbill strcat(minusnam, namep); 3211043Sbill alarm(0); 3226197Sroot umask(022); 3236329Swnj if (ttyn[sizeof("/dev/tty")-1] == 'd') 3246329Swnj logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name); 3256329Swnj if (!quietlog) { 3262822Swnj showmotd(); 3272822Swnj strcat(maildir, pwd->pw_name); 3282822Swnj if (access(maildir,4)==0) { 3292822Swnj struct stat statb; 3302822Swnj stat(maildir, &statb); 3312822Swnj if (statb.st_size) 3322822Swnj printf("You have mail.\n"); 3332822Swnj } 3342822Swnj } 3352822Swnj 3361043Sbill signal(SIGQUIT, SIG_DFL); 3371043Sbill signal(SIGINT, SIG_DFL); 3383935Sroot signal(SIGTSTP, SIG_IGN); 3391043Sbill execlp(pwd->pw_shell, minusnam, 0); 3402822Swnj perror(pwd->pw_shell); 3411043Sbill printf("No shell\n"); 3421043Sbill exit(0); 3431043Sbill } 3441043Sbill 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 3596466Swnj if (rflag) 3606466Swnj return(1); 3612822Swnj if ((fd = fopen(securetty, "r")) == NULL) 3622822Swnj return(1); 3632822Swnj while (fgets(buf, sizeof buf, fd) != NULL) { 3642822Swnj buf[strlen(buf)-1] = '\0'; 3652822Swnj if (strcmp(tty, buf) == 0) { 3662822Swnj fclose(fd); 3672822Swnj return(1); 3682822Swnj } 3692822Swnj } 3702822Swnj fclose(fd); 3712822Swnj return(0); 3722822Swnj } 3732822Swnj 3741043Sbill showmotd() 3751043Sbill { 3761043Sbill FILE *mf; 3771043Sbill register c; 3781043Sbill 3791043Sbill signal(SIGINT, catch); 3802822Swnj if ((mf = fopen("/etc/motd","r")) != NULL) { 3812822Swnj while ((c = getc(mf)) != EOF && stopmotd == 0) 3821043Sbill putchar(c); 3831043Sbill fclose(mf); 3841043Sbill } 3851043Sbill signal(SIGINT, SIG_IGN); 3861043Sbill } 3871043Sbill 3882822Swnj #undef UNKNOWN 3891043Sbill #define UNKNOWN "su" 3901043Sbill 3911043Sbill char * 3921043Sbill stypeof(ttyid) 3931043Sbill char *ttyid; 3941043Sbill { 3951043Sbill static char typebuf[16]; 3961043Sbill char buf[50]; 3971043Sbill register FILE *f; 3981043Sbill register char *p, *t, *q; 3991043Sbill 4001043Sbill if (ttyid == NULL) 4011043Sbill return (UNKNOWN); 4021043Sbill f = fopen("/etc/ttytype", "r"); 4031043Sbill if (f == NULL) 4041043Sbill return (UNKNOWN); 4051043Sbill /* split off end of name */ 4061043Sbill for (p = q = ttyid; *p != 0; p++) 4071043Sbill if (*p == '/') 4081043Sbill q = p + 1; 4091043Sbill 4101043Sbill /* scan the file */ 4111043Sbill while (fgets(buf, sizeof buf, f) != NULL) 4121043Sbill { 4132822Swnj for (t=buf; *t!=' ' && *t != '\t'; t++) 4141043Sbill ; 4151043Sbill *t++ = 0; 4162822Swnj while (*t == ' ' || *t == '\t') 4172822Swnj t++; 4181043Sbill for (p=t; *p>' '; p++) 4191043Sbill ; 4201043Sbill *p = 0; 4211043Sbill if (strcmp(q,t)==0) { 4221043Sbill strcpy(typebuf, buf); 4231043Sbill fclose(f); 4241043Sbill return (typebuf); 4251043Sbill } 4261043Sbill } 4271043Sbill fclose (f); 4281043Sbill return (UNKNOWN); 4291043Sbill } 4306005Swnj 4316005Swnj getstr(buf, cnt, err) 4326005Swnj char *buf; 4336005Swnj int cnt; 4346005Swnj char *err; 4356005Swnj { 4366005Swnj char c; 4376005Swnj 4386005Swnj do { 4396005Swnj if (read(0, &c, 1) != 1) 4406005Swnj exit(1); 4416005Swnj if (--cnt < 0) { 4426005Swnj printf("%s too long\r\n", err); 4436005Swnj exit(1); 4446005Swnj } 4456005Swnj *buf++ = c; 4466005Swnj } while (c != 0); 4476005Swnj } 4486329Swnj 4496329Swnj logerr(fmt, a1, a2, a3) 4506329Swnj char *fmt, *a1, *a2, *a3; 4516329Swnj { 4526329Swnj 4536329Swnj } 454