1*12560Sleres static char *sccsid = "@(#)login.c 4.23 83/05/19"; 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 *stypeof(); 541043Sbill extern char **environ; 551043Sbill 569867Ssam struct ttychars tc = { 579867Ssam CERASE, CKILL, CINTR, CQUIT, CSTART, 589867Ssam CSTOP, CEOF, CBRK, CSUSP, CDSUSP, 599867Ssam CRPRNT, CFLUSH, CWERASE,CLNEXT 601365Sbill }; 611365Sbill 626005Swnj int rflag; 636197Sroot char rusername[NMAX+1], lusername[NMAX+1]; 646005Swnj char rpassword[NMAX+1]; 656878Smckusick char name[NMAX+1]; 666197Sroot char *rhost; 676005Swnj 681043Sbill main(argc, argv) 691043Sbill char **argv; 701043Sbill { 711043Sbill register char *namep; 721043Sbill int t, f, c; 732822Swnj int invalid; 742822Swnj int quietlog; 752822Swnj int i; 762822Swnj FILE *nlfd; 771043Sbill char *ttyn; 786197Sroot int ldisc = 0, zero = 0; 796197Sroot FILE *hostf; int first = 1; 801043Sbill 811043Sbill alarm(60); 821043Sbill signal(SIGQUIT, SIG_IGN); 831043Sbill signal(SIGINT, SIG_IGN); 841043Sbill nice(-100); 851043Sbill nice(20); 861043Sbill nice(0); 876329Swnj if (argc > 1 && !strcmp(argv[1], "-r")) { 886005Swnj rflag++; 896329Swnj rhost = argv[2]; 906197Sroot argc = 1; 916329Swnj getstr(rusername, sizeof (rusername), "remuser"); 926329Swnj getstr(lusername, sizeof (lusername), "locuser"); 936005Swnj getstr(term+5, sizeof(term)-5, "Terminal type"); 946329Swnj if (getuid()) 956329Swnj goto abnormal; 966197Sroot setpwent(); 976197Sroot pwd = getpwnam(lusername); 986329Swnj endpwent(); 9911990Sleres if (pwd == NULL) 1006329Swnj goto abnormal; 1016466Swnj hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0; 1026197Sroot again: 1036197Sroot if (hostf) { 10411813Sleres char ahost[32]; 10511813Sleres 10611813Sleres while (fgets(ahost, sizeof (ahost), hostf)) { 10711813Sleres char *user; 10811813Sleres 109*12560Sleres if ((user = index(ahost, '\n')) != 0) 110*12560Sleres *user++ = '\0'; 111*12560Sleres if ((user = index(ahost, ' ')) != 0) 112*12560Sleres *user++ = '\0'; 11311813Sleres if (!strcmp(rhost, ahost) && 11411813Sleres !strcmp(rusername, user ? 11511813Sleres user : lusername)) { 11611813Sleres fclose(hostf); 11711813Sleres goto normal; 11811813Sleres } 1196329Swnj } 12011813Sleres fclose(hostf); 1216197Sroot } 1226197Sroot if (first == 1) { 12311813Sleres char *rhosts = ".rhosts"; 12411813Sleres struct stat sbuf; 12511813Sleres 1266197Sroot first = 0; 1276197Sroot if (chdir(pwd->pw_dir) < 0) 1286197Sroot goto again; 12911813Sleres if (lstat(rhosts, &sbuf) < 0) 13011813Sleres goto again; 13111813Sleres if ((sbuf.st_mode & S_IFMT) == S_IFLNK) { 13211813Sleres printf("login: .rhosts is a soft link.\r\n"); 13311813Sleres goto abnormal; 13411813Sleres } 13511813Sleres hostf = fopen(rhosts, "r"); 13611813Sleres fstat(fileno(hostf), &sbuf); 13711813Sleres if ((int) sbuf.st_uid != pwd->pw_uid && 13811813Sleres (int) sbuf.st_uid != 0) { 13911813Sleres printf("login: Bad .rhosts ownership.\r\n"); 14011813Sleres fclose(hostf); 14111813Sleres goto abnormal; 14211813Sleres } 14311813Sleres goto again; 1446197Sroot } 1456329Swnj abnormal: 1466197Sroot rhost = 0; 1476197Sroot rflag = -1; 1486005Swnj } 1496197Sroot normal: 1509867Ssam ioctl(0, TIOCLSET, &zero); /* XXX */ 1511547Sbill ioctl(0, TIOCNXCL, 0); 1526329Swnj ioctl(0, FIONBIO, &zero); 1536329Swnj ioctl(0, FIOASYNC, &zero); 1549867Ssam ioctl(0, TIOCGETP, &ttyb); /* XXX */ 1556005Swnj if (rflag) { 1566005Swnj char *cp = index(term, '/'); 1576005Swnj if (cp) { 1586005Swnj int i; 1596005Swnj *cp++ = 0; 1606005Swnj for (i = 0; i < NSPEEDS; i++) 1616005Swnj if (!strcmp(speeds[i], cp)) { 1626005Swnj ttyb.sg_ispeed = ttyb.sg_ospeed = i; 1636005Swnj break; 1646005Swnj } 1656005Swnj } 1666005Swnj ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS; 1676005Swnj } 1689867Ssam ioctl(0, TIOCSETP, &ttyb); /* XXX */ 1699867Ssam ioctl(0, TIOCCSET, &tc); 1701043Sbill for (t=3; t<20; t++) 1711043Sbill close(t); 1721043Sbill ttyn = ttyname(0); 1732822Swnj if (ttyn==(char *)0) 1741043Sbill ttyn = "/dev/tty??"; 1752822Swnj do { 1762822Swnj ldisc = 0; 1772822Swnj ioctl(0, TIOCSETD, &ldisc); 1782822Swnj invalid = FALSE; 1792822Swnj SCPYN(utmp.ut_name, ""); 1802822Swnj if (argc>1) { 1812822Swnj SCPYN(utmp.ut_name, argv[1]); 1822822Swnj argc = 0; 1831043Sbill } 1846329Swnj if (rflag) { 1859867Ssam SCPYN(utmp.ut_name, lusername); 1866329Swnj if (rflag == -1) 1876329Swnj rflag = 0; 1886329Swnj } else 1896197Sroot while (utmp.ut_name[0] == '\0') { 1906197Sroot namep = utmp.ut_name; 1916197Sroot { char hostname[32]; 1926197Sroot gethostname(hostname, sizeof (hostname)); 1936197Sroot printf("%s login: ", hostname); } 1946197Sroot while ((c = getchar()) != '\n') { 1956197Sroot if (c == ' ') 1966197Sroot c = '_'; 1976197Sroot if (c == EOF) 1986197Sroot exit(0); 1996197Sroot if (namep < utmp.ut_name+NMAX) 2006197Sroot *namep++ = c; 2016197Sroot } 2022822Swnj } 2036197Sroot if (rhost == 0) { 2046197Sroot setpwent(); 2056197Sroot if ((pwd = getpwnam(utmp.ut_name)) == NULL) 2066197Sroot pwd = &nouser; 2076197Sroot endpwent(); 2082822Swnj } 2092822Swnj if (!strcmp(pwd->pw_shell, "/bin/csh")) { 2102822Swnj ldisc = NTTYDISC; 2112822Swnj ioctl(0, TIOCSETD, &ldisc); 2122822Swnj } 2136197Sroot if (rhost == 0) { 2146197Sroot if (*pwd->pw_passwd != '\0') { 2156197Sroot char *pp; 2166197Sroot nice(-4); 2176197Sroot if (rflag == 0) 2186197Sroot pp = getpass("Password:"); 2196197Sroot else 2206197Sroot pp = rpassword; 2216197Sroot namep = crypt(pp,pwd->pw_passwd); 2226197Sroot nice(4); 2236197Sroot if (strcmp(namep, pwd->pw_passwd)) 2246197Sroot invalid = TRUE; 2256197Sroot } 2262822Swnj } 2272822Swnj if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) { 2282822Swnj /* logins are disabled except for root */ 2292822Swnj while ((c = getc(nlfd)) != EOF) 2302822Swnj putchar(c); 2312822Swnj fflush(stdout); 2322822Swnj sleep(5); 2332822Swnj exit(0); 2342822Swnj } 2352822Swnj if (!invalid && pwd->pw_uid == 0 && 2362822Swnj !rootterm(ttyn+sizeof("/dev/")-1)) { 2376329Swnj logerr("ROOT LOGIN REFUSED %s", 2386329Swnj ttyn+sizeof("/dev/")-1); 2392822Swnj invalid = TRUE; 2402822Swnj } 2412822Swnj if (invalid) { 2421043Sbill printf("Login incorrect\n"); 2436329Swnj if (ttyn[sizeof("/dev/tty")-1] == 'd') 2446329Swnj logerr("BADDIALUP %s %s\n", 2456329Swnj ttyn+sizeof("/dev/")-1, utmp.ut_name); 2461043Sbill } 2472822Swnj if (*pwd->pw_shell == '\0') 2482822Swnj pwd->pw_shell = "/bin/sh"; 2492822Swnj i = strlen(pwd->pw_shell); 2502822Swnj if (chdir(pwd->pw_dir) < 0 && !invalid ) { 2512822Swnj if (chdir("/") < 0) { 2522822Swnj printf("No directory!\n"); 2532822Swnj invalid = TRUE; 2542822Swnj } else { 2552822Swnj printf("No directory! Logging in with home=/\n"); 2562822Swnj pwd->pw_dir = "/"; 2572822Swnj } 2581043Sbill } 2596005Swnj if (rflag && invalid) 2606005Swnj exit(1); 2612822Swnj } while (invalid); 2621043Sbill 2636005Swnj 2641043Sbill time(&utmp.ut_time); 2651043Sbill t = ttyslot(); 2661043Sbill if (t>0 && (f = open("/etc/utmp", 1)) >= 0) { 2671043Sbill lseek(f, (long)(t*sizeof(utmp)), 0); 2681043Sbill SCPYN(utmp.ut_line, rindex(ttyn, '/')+1); 2691043Sbill write(f, (char *)&utmp, sizeof(utmp)); 2701043Sbill close(f); 2711043Sbill } 2721043Sbill if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) { 2731043Sbill lseek(f, 0L, 2); 2741043Sbill write(f, (char *)&utmp, sizeof(utmp)); 2751043Sbill close(f); 2761043Sbill } 2776329Swnj quietlog = 0; 2782822Swnj if (access(qlog, 0) == 0) 2796329Swnj quietlog = 1; 2806329Swnj if ((f = open(lastlog, 2)) >= 0) { 2812822Swnj struct lastlog ll; 2822822Swnj 2832822Swnj lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); 2842822Swnj if (read(f, (char *) &ll, sizeof ll) == sizeof ll && 2852822Swnj ll.ll_time != 0) { 2866329Swnj if (quietlog == 0) 2872822Swnj printf("Last login: %.*s on %.*s\n" 2882822Swnj , 24-5 2892822Swnj , (char *) ctime(&ll.ll_time) 2902822Swnj , sizeof(ll.ll_line) 2912822Swnj , ll.ll_line 2922822Swnj ); 2932822Swnj } 2942822Swnj lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); 2952822Swnj time(&ll.ll_time); 2962822Swnj SCPYN(ll.ll_line, rindex(ttyn, '/')+1); 2972822Swnj write(f, (char *) &ll, sizeof ll); 2982822Swnj close(f); 2992822Swnj } 3001043Sbill chown(ttyn, pwd->pw_uid, pwd->pw_gid); 3019867Ssam chmod(ttyn, 0622); 3021043Sbill setgid(pwd->pw_gid); 3036878Smckusick strncpy(name, utmp.ut_name, NMAX); 3046878Smckusick name[NMAX] = '\0'; 3059224Ssam initgroups(name, pwd->pw_gid); 3061043Sbill setuid(pwd->pw_uid); 3071043Sbill environ = envinit; 3081043Sbill strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); 3091043Sbill strncat(shell, pwd->pw_shell, sizeof(shell)-7); 3106329Swnj if (term[strlen("TERM=")] == 0) 3116005Swnj strncat(term, stypeof(ttyn), sizeof(term)-6); 3122822Swnj strncat(user, pwd->pw_name, sizeof(user)-6); 3131043Sbill if ((namep = rindex(pwd->pw_shell, '/')) == NULL) 3141043Sbill namep = pwd->pw_shell; 3151043Sbill else 3161043Sbill namep++; 3171043Sbill strcat(minusnam, namep); 3181043Sbill alarm(0); 3196197Sroot umask(022); 3206329Swnj if (ttyn[sizeof("/dev/tty")-1] == 'd') 3216329Swnj logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name); 3226329Swnj if (!quietlog) { 3232822Swnj showmotd(); 3242822Swnj strcat(maildir, pwd->pw_name); 3252822Swnj if (access(maildir,4)==0) { 3262822Swnj struct stat statb; 3272822Swnj stat(maildir, &statb); 3282822Swnj if (statb.st_size) 3292822Swnj printf("You have mail.\n"); 3302822Swnj } 3312822Swnj } 3322822Swnj 3331043Sbill signal(SIGQUIT, SIG_DFL); 3341043Sbill signal(SIGINT, SIG_DFL); 3353935Sroot signal(SIGTSTP, SIG_IGN); 3361043Sbill execlp(pwd->pw_shell, minusnam, 0); 3372822Swnj perror(pwd->pw_shell); 3381043Sbill printf("No shell\n"); 3391043Sbill exit(0); 3401043Sbill } 3411043Sbill 3421043Sbill int stopmotd; 3431043Sbill catch() 3441043Sbill { 3456466Swnj 3461043Sbill signal(SIGINT, SIG_IGN); 3471043Sbill stopmotd++; 3481043Sbill } 3491043Sbill 3502822Swnj rootterm(tty) 3516466Swnj char *tty; 3522822Swnj { 3532822Swnj register FILE *fd; 3546466Swnj char buf[100]; 3552822Swnj 3562822Swnj if ((fd = fopen(securetty, "r")) == NULL) 3572822Swnj return(1); 3582822Swnj while (fgets(buf, sizeof buf, fd) != NULL) { 3592822Swnj buf[strlen(buf)-1] = '\0'; 3602822Swnj if (strcmp(tty, buf) == 0) { 3612822Swnj fclose(fd); 3622822Swnj return(1); 3632822Swnj } 3642822Swnj } 3652822Swnj fclose(fd); 3662822Swnj return(0); 3672822Swnj } 3682822Swnj 3691043Sbill showmotd() 3701043Sbill { 3711043Sbill FILE *mf; 3721043Sbill register c; 3731043Sbill 3741043Sbill signal(SIGINT, catch); 3752822Swnj if ((mf = fopen("/etc/motd","r")) != NULL) { 3762822Swnj while ((c = getc(mf)) != EOF && stopmotd == 0) 3771043Sbill putchar(c); 3781043Sbill fclose(mf); 3791043Sbill } 3801043Sbill signal(SIGINT, SIG_IGN); 3811043Sbill } 3821043Sbill 3832822Swnj #undef UNKNOWN 3841043Sbill #define UNKNOWN "su" 3851043Sbill 3861043Sbill char * 3871043Sbill stypeof(ttyid) 3881043Sbill char *ttyid; 3891043Sbill { 3901043Sbill static char typebuf[16]; 3911043Sbill char buf[50]; 3921043Sbill register FILE *f; 3931043Sbill register char *p, *t, *q; 3941043Sbill 3951043Sbill if (ttyid == NULL) 3961043Sbill return (UNKNOWN); 3971043Sbill f = fopen("/etc/ttytype", "r"); 3981043Sbill if (f == NULL) 3991043Sbill return (UNKNOWN); 4001043Sbill /* split off end of name */ 4011043Sbill for (p = q = ttyid; *p != 0; p++) 4021043Sbill if (*p == '/') 4031043Sbill q = p + 1; 4041043Sbill 4051043Sbill /* scan the file */ 4061043Sbill while (fgets(buf, sizeof buf, f) != NULL) 4071043Sbill { 4082822Swnj for (t=buf; *t!=' ' && *t != '\t'; t++) 4091043Sbill ; 4101043Sbill *t++ = 0; 4112822Swnj while (*t == ' ' || *t == '\t') 4122822Swnj t++; 4131043Sbill for (p=t; *p>' '; p++) 4141043Sbill ; 4151043Sbill *p = 0; 4161043Sbill if (strcmp(q,t)==0) { 4171043Sbill strcpy(typebuf, buf); 4181043Sbill fclose(f); 4191043Sbill return (typebuf); 4201043Sbill } 4211043Sbill } 4221043Sbill fclose (f); 4231043Sbill return (UNKNOWN); 4241043Sbill } 4256005Swnj 4266005Swnj getstr(buf, cnt, err) 4276005Swnj char *buf; 4286005Swnj int cnt; 4296005Swnj char *err; 4306005Swnj { 4316005Swnj char c; 4326005Swnj 4336005Swnj do { 4346005Swnj if (read(0, &c, 1) != 1) 4356005Swnj exit(1); 4366005Swnj if (--cnt < 0) { 4376005Swnj printf("%s too long\r\n", err); 4386005Swnj exit(1); 4396005Swnj } 4406005Swnj *buf++ = c; 4416005Swnj } while (c != 0); 4426005Swnj } 4436329Swnj 4446329Swnj logerr(fmt, a1, a2, a3) 4456329Swnj char *fmt, *a1, *a2, *a3; 4466329Swnj { 4476329Swnj 4486329Swnj } 449