121174Sdist /* 236517Skarels * Copyright (c) 1983, 1988 The Regents of the University of California. 335441Sbostic * All rights reserved. 435441Sbostic * 535441Sbostic * Redistribution and use in source and binary forms are permitted 635441Sbostic * provided that the above copyright notice and this paragraph are 735441Sbostic * duplicated in all such forms and that any documentation, 835441Sbostic * advertising materials, and other materials related to such 935441Sbostic * distribution and use acknowledge that the software was developed 1035441Sbostic * by the University of California, Berkeley. The name of the 1135441Sbostic * University may not be used to endorse or promote products derived 1235441Sbostic * from this software without specific prior written permission. 1335441Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1435441Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1535441Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621174Sdist */ 1721174Sdist 186446Swnj #ifndef lint 1921174Sdist char copyright[] = 2036517Skarels "@(#) Copyright (c) 1983, 1988 The Regents of the University of California.\n\ 2121174Sdist All rights reserved.\n"; 2235441Sbostic #endif /* not lint */ 236446Swnj 2421174Sdist #ifndef lint 25*36711Skfall static char sccsid[] = "@(#)rlogind.c 5.33 (Berkeley) 02/08/89"; 2635441Sbostic #endif /* not lint */ 2721174Sdist 2816369Skarels /* 2916369Skarels * remote login server: 3036453Skfall * \0 3116369Skarels * remuser\0 3216369Skarels * locuser\0 3336453Skfall * terminal_type/speed\0 3436518Skarels * data 3516369Skarels */ 3616369Skarels 376446Swnj #include <stdio.h> 38*36711Skfall #include <sys/types.h> 396446Swnj #include <sys/stat.h> 406446Swnj #include <sys/socket.h> 4113554Ssam #include <sys/wait.h> 4218357Ssam #include <sys/file.h> 43*36711Skfall #include <sys/param.h> 449208Ssam 459208Ssam #include <netinet/in.h> 469208Ssam 476446Swnj #include <errno.h> 486446Swnj #include <pwd.h> 496446Swnj #include <signal.h> 50*36711Skfall #include <sys/ioctl.h> 51*36711Skfall #include <sys/termios.h> 526446Swnj #include <stdio.h> 538380Ssam #include <netdb.h> 5417187Sralph #include <syslog.h> 5518357Ssam #include <strings.h> 566446Swnj 5736518Skarels #ifndef TIOCPKT_WINDOW 5836518Skarels #define TIOCPKT_WINDOW 0x80 5936518Skarels #endif 6036518Skarels 61*36711Skfall #ifdef KERBEROS 62*36711Skfall #include <kerberos/krb.h> 63*36711Skfall #define SECURE_MESSAGE "This rlogin session is using DES encryption for all transmissions.\r\n" 64*36711Skfall 65*36711Skfall AUTH_DAT *kdata; 66*36711Skfall KTEXT ticket; 67*36711Skfall u_char auth_buf[sizeof(AUTH_DAT)]; 68*36711Skfall u_char tick_buf[sizeof(KTEXT_ST)]; 69*36711Skfall Key_schedule schedule; 70*36711Skfall int encrypt = 0, retval, use_kerberos = 0, vacuous = 0; 71*36711Skfall int do_krb_login(); 72*36711Skfall 73*36711Skfall #define OLD_RCMD 0x00 74*36711Skfall #define KERB_RCMD 0x00 75*36711Skfall #define KERB_RCMD_MUTUAL 0x03 76*36711Skfall 77*36711Skfall #define ARGSTR "lnkv" 78*36711Skfall #else 79*36711Skfall #define ARGSTR "ln" 80*36711Skfall #endif /* KERBEROS */ 81*36711Skfall 8236518Skarels char *env[2]; 8336518Skarels #define NMAX 30 8436517Skarels char lusername[NMAX+1], rusername[NMAX+1]; 8536517Skarels static char term[64] = "TERM="; 8636517Skarels #define ENVSIZE (sizeof("TERM=")-1) /* skip null for concatenation */ 8736517Skarels int keepalive = 1; 8836453Skfall 8936453Skfall #define SUPERUSER(pwd) ((pwd)->pw_uid == 0) 9036453Skfall 9134424Sbostic extern int errno; 9210417Ssam int reapchild(); 9336453Skfall struct passwd *getpwnam(), *pwd; 9424723Smckusick char *malloc(); 9516369Skarels 966446Swnj main(argc, argv) 976446Swnj int argc; 986446Swnj char **argv; 996446Swnj { 10036319Sbostic extern int opterr, optind, _check_rhosts_file; 10136319Sbostic int ch; 10234424Sbostic int on = 1, fromlen; 1036446Swnj struct sockaddr_in from; 1046446Swnj 10536625Skfall openlog("rlogind", LOG_PID | LOG_CONS, LOG_AUTH); 10636319Sbostic 10736319Sbostic opterr = 0; 108*36711Skfall while ((ch = getopt(argc, argv, ARGSTR)) != EOF) 10936517Skarels switch (ch) { 11036319Sbostic case 'l': 11136319Sbostic _check_rhosts_file = 0; 11236319Sbostic break; 11336517Skarels case 'n': 11436517Skarels keepalive = 0; 11536517Skarels break; 116*36711Skfall #ifdef KERBEROS 117*36711Skfall case 'k': 118*36711Skfall use_kerberos = 1; 119*36711Skfall break; 120*36711Skfall case 'v': 121*36711Skfall vacuous = 1; 122*36711Skfall break; 123*36711Skfall #endif 12436319Sbostic case '?': 12536319Sbostic default: 126*36711Skfall usage(); 12736319Sbostic break; 12836319Sbostic } 12936319Sbostic argc -= optind; 13036319Sbostic argv += optind; 13136319Sbostic 132*36711Skfall #ifdef KERBEROS 133*36711Skfall if (use_kerberos && vacuous) { 134*36711Skfall usage(); 135*36711Skfall fatal("only one of -k and -v allowed\n"); 136*36711Skfall } 137*36711Skfall #endif 13816369Skarels fromlen = sizeof (from); 13916369Skarels if (getpeername(0, &from, &fromlen) < 0) { 140*36711Skfall syslog(LOG_ERR,"Can't get peer name of remote host: %m"); 141*36711Skfall fatalperror("Can't get peer name of remote host"); 1428380Ssam } 14336517Skarels if (keepalive && 14436517Skarels setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof (on)) < 0) 14517187Sralph syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m"); 14616369Skarels doit(0, &from); 1476446Swnj } 1486446Swnj 1496446Swnj int child; 1506446Swnj int cleanup(); 1516446Swnj int netf; 1526446Swnj char *line; 15324724Smckusick extern char *inet_ntoa(); 1546446Swnj 15524889Smckusick struct winsize win = { 0, 0, 0, 0 }; 15624723Smckusick 15724889Smckusick 1586446Swnj doit(f, fromp) 1596446Swnj int f; 1606446Swnj struct sockaddr_in *fromp; 1616446Swnj { 16218357Ssam int i, p, t, pid, on = 1; 16336631Skarels int authenticated = 0, hostok = 0; 164*36711Skfall register struct hostent *hp; 16536703Skarels char remotehost[2 * MAXHOSTNAMELEN + 1]; 16624724Smckusick struct hostent hostent; 1678380Ssam char c; 1686446Swnj 1696446Swnj alarm(60); 1706446Swnj read(f, &c, 1); 171*36711Skfall 172*36711Skfall #ifdef KERBEROS 173*36711Skfall /* 174*36711Skfall * XXX: 1st char tells us which client we're talking to 175*36711Skfall */ 176*36711Skfall switch (c) { 177*36711Skfall 178*36711Skfall case OLD_RCMD: /* NB: OLD_RCMD == KERB_RCMD */ 179*36711Skfall if (vacuous) 180*36711Skfall fatal(f, "Remote host requires Kerberos authentication"); 181*36711Skfall break; 182*36711Skfall 183*36711Skfall case KERB_RCMD_MUTUAL: 184*36711Skfall encrypt = 1; 185*36711Skfall break; 186*36711Skfall 187*36711Skfall default: 188*36711Skfall fatal(f, "Remote protocol error"); 189*36711Skfall } 190*36711Skfall #else 1916446Swnj if (c != 0) 1926446Swnj exit(1); 193*36711Skfall #endif 19436453Skfall 1956446Swnj alarm(0); 19616227Skarels fromp->sin_port = ntohs((u_short)fromp->sin_port); 1978380Ssam hp = gethostbyaddr(&fromp->sin_addr, sizeof (struct in_addr), 1988380Ssam fromp->sin_family); 19911345Ssam if (hp == 0) { 20024724Smckusick /* 20124724Smckusick * Only the name is used below. 20224724Smckusick */ 20324724Smckusick hp = &hostent; 20424724Smckusick hp->h_name = inet_ntoa(fromp->sin_addr); 20536635Skarels hostok++; 206*36711Skfall } else if (local_domain(hp->h_name)) { 20736635Skarels /* 20836635Skarels * If name returned by gethostbyaddr is in our domain, 20936635Skarels * attempt to verify that we haven't been fooled by someone 21036635Skarels * in a remote net; look up the name and check that this 21136635Skarels * address corresponds to the name. 21236635Skarels */ 21336635Skarels strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1); 21436635Skarels remotehost[sizeof(remotehost) - 1] = 0; 21536635Skarels hp = gethostbyname(remotehost); 21636635Skarels if (hp) 21736635Skarels for (; hp->h_addr_list[0]; hp->h_addr_list++) 21836635Skarels if (!bcmp(hp->h_addr_list[0], (caddr_t)&fromp->sin_addr, 21936635Skarels sizeof(fromp->sin_addr))) { 22036635Skarels hostok++; 22136635Skarels break; 22236635Skarels } 22336633Skarels } else 22436635Skarels hostok++; 225*36711Skfall 226*36711Skfall #ifdef KERBEROS 227*36711Skfall if (use_kerberos) { 228*36711Skfall retval = do_krb_login(hp->h_name, fromp, encrypt); 229*36711Skfall write(f, &c, 1); 230*36711Skfall if (retval == 0 && hostok) 231*36711Skfall authenticated++; 232*36711Skfall else if (retval > 0) 233*36711Skfall fatal(f, krb_err_txt[retval]); 234*36711Skfall else if(!hostok) 235*36711Skfall fatal(f, "krlogind: Host address mismatch.\r\n"); 236*36711Skfall } else 23736703Skarels #endif 238*36711Skfall { 239*36711Skfall if (fromp->sin_family != AF_INET || 240*36711Skfall fromp->sin_port >= IPPORT_RESERVED || 241*36711Skfall fromp->sin_port < IPPORT_RESERVED/2) { 242*36711Skfall syslog(LOG_NOTICE, "Connection from %s on illegal port", 243*36711Skfall inet_ntoa(fromp->sin_addr)); 244*36711Skfall fatal(f, "Permission denied"); 245*36711Skfall } 24636702Skarels #ifdef IP_OPTIONS 247*36711Skfall { 248*36711Skfall u_char optbuf[BUFSIZ/3], *cp; 249*36711Skfall char lbuf[BUFSIZ], *lp; 250*36711Skfall int optsize = sizeof(optbuf), ipproto; 251*36711Skfall struct protoent *ip; 25236702Skarels 253*36711Skfall if ((ip = getprotobyname("ip")) != NULL) 254*36711Skfall ipproto = ip->p_proto; 255*36711Skfall else 256*36711Skfall ipproto = IPPROTO_IP; 257*36711Skfall if (getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, 258*36711Skfall &optsize) == 0 && optsize != 0) { 259*36711Skfall lp = lbuf; 260*36711Skfall for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3) 261*36711Skfall sprintf(lp, " %2.2x", *cp); 262*36711Skfall syslog(LOG_NOTICE, 263*36711Skfall "Connection received using IP options (ignored):%s", 264*36711Skfall lbuf); 265*36711Skfall if (setsockopt(0, ipproto, IP_OPTIONS, 266*36711Skfall (char *)NULL, &optsize) != 0) { 267*36711Skfall syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m"); 268*36711Skfall exit(1); 269*36711Skfall } 270*36711Skfall } 271*36711Skfall } 27236702Skarels #endif 273*36711Skfall write(f, "", 1); 274*36711Skfall 275*36711Skfall if (do_rlogin(hp->h_name) == 0) { 276*36711Skfall if (hostok) 277*36711Skfall authenticated++; 278*36711Skfall else 279*36711Skfall write(f, "rlogind: Host address mismatch.\r\n", 280*36711Skfall sizeof("rlogind: Host address mismatch.\r\n") - 1); 281*36711Skfall } 28236631Skarels } 28336636Skarels 2846446Swnj for (c = 'p'; c <= 's'; c++) { 2856446Swnj struct stat stb; 2866446Swnj line = "/dev/ptyXX"; 2876446Swnj line[strlen("/dev/pty")] = c; 2886446Swnj line[strlen("/dev/ptyp")] = '0'; 2896446Swnj if (stat(line, &stb) < 0) 2906446Swnj break; 2916446Swnj for (i = 0; i < 16; i++) { 29234424Sbostic line[sizeof("/dev/ptyp") - 1] = "0123456789abcdef"[i]; 29334424Sbostic p = open(line, O_RDWR); 2946446Swnj if (p > 0) 2956446Swnj goto gotpty; 2966446Swnj } 2976446Swnj } 29824723Smckusick fatal(f, "Out of ptys"); 2999242Ssam /*NOTREACHED*/ 3006446Swnj gotpty: 30124889Smckusick (void) ioctl(p, TIOCSWINSZ, &win); 30216227Skarels netf = f; 3036446Swnj line[strlen("/dev/")] = 't'; 30434424Sbostic t = open(line, O_RDWR); 30534424Sbostic if (t < 0) 30634424Sbostic fatalperror(f, line); 30734424Sbostic if (fchmod(t, 0)) 30834424Sbostic fatalperror(f, line); 30934424Sbostic (void)signal(SIGHUP, SIG_IGN); 31034424Sbostic vhangup(); 31134424Sbostic (void)signal(SIGHUP, SIG_DFL); 31234424Sbostic t = open(line, O_RDWR); 31334424Sbostic if (t < 0) 31434424Sbostic fatalperror(f, line); 31536453Skfall setup_term(t); 3166446Swnj #ifdef DEBUG 31734424Sbostic { 31834424Sbostic int tt = open("/dev/tty", O_RDWR); 31934424Sbostic if (tt > 0) { 32034424Sbostic (void)ioctl(tt, TIOCNOTTY, 0); 32134424Sbostic (void)close(tt); 32234424Sbostic } 3236446Swnj } 3246446Swnj #endif 3259242Ssam pid = fork(); 3269242Ssam if (pid < 0) 32734424Sbostic fatalperror(f, ""); 32818357Ssam if (pid == 0) { 329*36711Skfall if (setsid() < 0) 330*36711Skfall fatalperror(f, "setsid"); 331*36711Skfall if (ioctl(t, TIOCSCTTY, 0) < 0) 332*36711Skfall fatalperror(f, "ioctl(sctty)"); 33318357Ssam close(f), close(p); 33418357Ssam dup2(t, 0), dup2(t, 1), dup2(t, 2); 33516227Skarels close(t); 33636631Skarels if (authenticated) 337*36711Skfall execl("/bin/login", "login", "-p", 338*36711Skfall "-h", hp->h_name, "-f", lusername, 0); 33936525Skfall else 340*36711Skfall execl("/bin/login", "login", "-p", 341*36711Skfall "-h", hp->h_name, lusername, 0); 34234424Sbostic fatalperror(2, "/bin/login"); 34318357Ssam /*NOTREACHED*/ 34418357Ssam } 34518357Ssam close(t); 34636453Skfall 347*36711Skfall #ifdef KERBEROS 348*36711Skfall /* 349*36711Skfall * If encrypted, don't turn on NBIO or the des read/write 350*36711Skfall * routines will croak. 351*36711Skfall */ 352*36711Skfall 353*36711Skfall if (encrypt) 354*36711Skfall (void) des_write(f, SECURE_MESSAGE, sizeof(SECURE_MESSAGE)); 355*36711Skfall else 356*36711Skfall #endif 357*36711Skfall ioctl(f, FIONBIO, &on); 35818357Ssam ioctl(p, FIONBIO, &on); 35918357Ssam ioctl(p, TIOCPKT, &on); 36018357Ssam signal(SIGTSTP, SIG_IGN); 36118357Ssam signal(SIGCHLD, cleanup); 36224724Smckusick setpgrp(0, 0); 36318357Ssam protocol(f, p); 36430600Smckusick signal(SIGCHLD, SIG_IGN); 36518357Ssam cleanup(); 36618357Ssam } 3679242Ssam 36818357Ssam char magic[2] = { 0377, 0377 }; 36925423Skarels char oobdata[] = {TIOCPKT_WINDOW}; 37018357Ssam 37118357Ssam /* 37218357Ssam * Handle a "control" request (signaled by magic being present) 37318357Ssam * in the data stream. For now, we are only willing to handle 37418357Ssam * window size changes. 37518357Ssam */ 37618357Ssam control(pty, cp, n) 37718357Ssam int pty; 37818357Ssam char *cp; 37918357Ssam int n; 38018357Ssam { 38128705Smckusick struct winsize w; 38218357Ssam 38328705Smckusick if (n < 4+sizeof (w) || cp[2] != 's' || cp[3] != 's') 38418357Ssam return (0); 38525423Skarels oobdata[0] &= ~TIOCPKT_WINDOW; /* we know he heard */ 38628705Smckusick bcopy(cp+4, (char *)&w, sizeof(w)); 38728705Smckusick w.ws_row = ntohs(w.ws_row); 38828705Smckusick w.ws_col = ntohs(w.ws_col); 38928705Smckusick w.ws_xpixel = ntohs(w.ws_xpixel); 39028705Smckusick w.ws_ypixel = ntohs(w.ws_ypixel); 39128705Smckusick (void)ioctl(pty, TIOCSWINSZ, &w); 39228705Smckusick return (4+sizeof (w)); 39318357Ssam } 39418357Ssam 39518357Ssam /* 39618357Ssam * rlogin "protocol" machine. 39718357Ssam */ 39818357Ssam protocol(f, p) 39918357Ssam int f, p; 40018357Ssam { 40118357Ssam char pibuf[1024], fibuf[1024], *pbp, *fbp; 40218357Ssam register pcc = 0, fcc = 0; 40336517Skarels int cc, nfd, pmask, fmask; 40425740Skarels char cntl; 40518357Ssam 40618482Ssam /* 40718484Ssam * Must ignore SIGTTOU, otherwise we'll stop 40818484Ssam * when we try and set slave pty's window shape 40925423Skarels * (our controlling tty is the master pty). 41018482Ssam */ 41118484Ssam (void) signal(SIGTTOU, SIG_IGN); 41225423Skarels send(f, oobdata, 1, MSG_OOB); /* indicate new rlogin */ 41336517Skarels if (f > p) 41436517Skarels nfd = f + 1; 41536517Skarels else 41636517Skarels nfd = p + 1; 41736517Skarels fmask = 1 << f; 41836517Skarels pmask = 1 << p; 41918357Ssam for (;;) { 42025740Skarels int ibits, obits, ebits; 42118357Ssam 42225740Skarels ibits = 0; 42325740Skarels obits = 0; 42418357Ssam if (fcc) 42536517Skarels obits |= pmask; 42618357Ssam else 42736517Skarels ibits |= fmask; 42818357Ssam if (pcc >= 0) 42918357Ssam if (pcc) 43036517Skarels obits |= fmask; 4319242Ssam else 43236517Skarels ibits |= pmask; 43336517Skarels ebits = pmask; 43436517Skarels if (select(nfd, &ibits, obits ? &obits : (int *)NULL, 43536517Skarels &ebits, 0) < 0) { 43618357Ssam if (errno == EINTR) 4376446Swnj continue; 43834424Sbostic fatalperror(f, "select"); 43918357Ssam } 44025740Skarels if (ibits == 0 && obits == 0 && ebits == 0) { 44118357Ssam /* shouldn't happen... */ 44218357Ssam sleep(5); 44318357Ssam continue; 44418357Ssam } 44525740Skarels #define pkcontrol(c) ((c)&(TIOCPKT_FLUSHWRITE|TIOCPKT_NOSTOP|TIOCPKT_DOSTOP)) 44636517Skarels if (ebits & pmask) { 44725740Skarels cc = read(p, &cntl, 1); 44825740Skarels if (cc == 1 && pkcontrol(cntl)) { 44925740Skarels cntl |= oobdata[0]; 45025740Skarels send(f, &cntl, 1, MSG_OOB); 45125740Skarels if (cntl & TIOCPKT_FLUSHWRITE) { 45225740Skarels pcc = 0; 45336517Skarels ibits &= ~pmask; 45425740Skarels } 45525740Skarels } 45625740Skarels } 45736517Skarels if (ibits & fmask) { 458*36711Skfall #ifdef KERBEROS 459*36711Skfall if (encrypt) 460*36711Skfall fcc = des_read(f, fibuf, sizeof(fibuf)); 461*36711Skfall else 462*36711Skfall #endif 463*36711Skfall fcc = read(f, fibuf, sizeof(fibuf)); 46418357Ssam if (fcc < 0 && errno == EWOULDBLOCK) 46518357Ssam fcc = 0; 46618357Ssam else { 46718357Ssam register char *cp; 46818357Ssam int left, n; 46918357Ssam 47018357Ssam if (fcc <= 0) 47116227Skarels break; 47218357Ssam fbp = fibuf; 47324723Smckusick 47418357Ssam top: 47525423Skarels for (cp = fibuf; cp < fibuf+fcc-1; cp++) 47618357Ssam if (cp[0] == magic[0] && 47718357Ssam cp[1] == magic[1]) { 47818357Ssam left = fcc - (cp-fibuf); 47918357Ssam n = control(p, cp, left); 48018357Ssam if (n) { 48118357Ssam left -= n; 48218357Ssam if (left > 0) 48325423Skarels bcopy(cp+n, cp, left); 48418357Ssam fcc -= n; 48518357Ssam goto top; /* n^2 */ 48625423Skarels } 48725423Skarels } 48836517Skarels obits |= pmask; /* try write */ 48925423Skarels } 49025423Skarels } 49124723Smckusick 49236517Skarels if ((obits & pmask) && fcc > 0) { 49325423Skarels cc = write(p, fbp, fcc); 49424723Smckusick if (cc > 0) { 49524723Smckusick fcc -= cc; 49624723Smckusick fbp += cc; 4976446Swnj } 49818357Ssam } 49924723Smckusick 50036517Skarels if (ibits & pmask) { 50118357Ssam pcc = read(p, pibuf, sizeof (pibuf)); 50218357Ssam pbp = pibuf; 50318357Ssam if (pcc < 0 && errno == EWOULDBLOCK) 50418357Ssam pcc = 0; 50518357Ssam else if (pcc <= 0) 50618357Ssam break; 50736517Skarels else if (pibuf[0] == 0) { 50818357Ssam pbp++, pcc--; 509*36711Skfall #ifdef KERBEROS 510*36711Skfall if (!encrypt) 511*36711Skfall #endif 512*36711Skfall obits |= fmask; /* try a write */ 51336517Skarels } else { 51418357Ssam if (pkcontrol(pibuf[0])) { 51525423Skarels pibuf[0] |= oobdata[0]; 51618357Ssam send(f, &pibuf[0], 1, MSG_OOB); 51716227Skarels } 51818357Ssam pcc = 0; 5196446Swnj } 52018357Ssam } 52136517Skarels if ((obits & fmask) && pcc > 0) { 522*36711Skfall #ifdef KERBEROS 523*36711Skfall if (encrypt) 524*36711Skfall cc = des_write(f, pbp, pcc); 525*36711Skfall else 526*36711Skfall #endif 527*36711Skfall cc = write(f, pbp, pcc); 52825423Skarels if (cc < 0 && errno == EWOULDBLOCK) { 52925423Skarels /* also shouldn't happen */ 53025423Skarels sleep(5); 53125423Skarels continue; 53225423Skarels } 53318357Ssam if (cc > 0) { 53418357Ssam pcc -= cc; 53518357Ssam pbp += cc; 53618357Ssam } 5376446Swnj } 5386446Swnj } 5396446Swnj } 5406446Swnj 5416446Swnj cleanup() 5426446Swnj { 54335440Sbostic char *p; 54435440Sbostic 54535440Sbostic p = line + sizeof("/dev/") - 1; 54635440Sbostic if (logout(p)) 54735440Sbostic logwtmp(p, "", ""); 54835440Sbostic (void)chmod(line, 0666); 54935440Sbostic (void)chown(line, 0, 0); 55035440Sbostic *p = 'p'; 55135440Sbostic (void)chmod(line, 0666); 55235440Sbostic (void)chown(line, 0, 0); 55310192Ssam shutdown(netf, 2); 5546446Swnj exit(1); 5556446Swnj } 5566446Swnj 5579242Ssam fatal(f, msg) 5589242Ssam int f; 5599242Ssam char *msg; 5609242Ssam { 5619242Ssam char buf[BUFSIZ]; 5629242Ssam 5639242Ssam buf[0] = '\01'; /* error indicator */ 56413554Ssam (void) sprintf(buf + 1, "rlogind: %s.\r\n", msg); 5659242Ssam (void) write(f, buf, strlen(buf)); 5669242Ssam exit(1); 5679242Ssam } 5689242Ssam 56934424Sbostic fatalperror(f, msg) 5709242Ssam int f; 5719242Ssam char *msg; 5729242Ssam { 5739242Ssam char buf[BUFSIZ]; 57416227Skarels extern int sys_nerr; 5759242Ssam extern char *sys_errlist[]; 5769242Ssam 57718357Ssam if ((unsigned)errno < sys_nerr) 57816227Skarels (void) sprintf(buf, "%s: %s", msg, sys_errlist[errno]); 57916227Skarels else 58016227Skarels (void) sprintf(buf, "%s: Error %d", msg, errno); 5819242Ssam fatal(f, buf); 5829242Ssam } 58336453Skfall 58436453Skfall do_rlogin(host) 58536518Skarels char *host; 58636453Skfall { 58736453Skfall 58836518Skarels getstr(rusername, sizeof(rusername), "remuser too long"); 58936518Skarels getstr(lusername, sizeof(lusername), "locuser too long"); 59036518Skarels getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type too long"); 59136518Skarels 59236518Skarels if (getuid()) 59336453Skfall return(-1); 59436453Skfall pwd = getpwnam(lusername); 59536518Skarels if (pwd == NULL) 59636453Skfall return(-1); 59736453Skfall return(ruserok(host, SUPERUSER(pwd), rusername, lusername)); 59836453Skfall } 59936453Skfall 60036453Skfall 60136518Skarels getstr(buf, cnt, errmsg) 60236518Skarels char *buf; 60336518Skarels int cnt; 60436518Skarels char *errmsg; 60536453Skfall { 60636518Skarels char c; 60736518Skarels 60836453Skfall do { 60936518Skarels if (read(0, &c, 1) != 1) 61036453Skfall exit(1); 61136518Skarels if (--cnt < 0) 61236518Skarels fatal(1, errmsg); 61336453Skfall *buf++ = c; 61436518Skarels } while (c != 0); 61536453Skfall } 61636453Skfall 61736518Skarels extern char **environ; 61836453Skfall 61936519Skarels setup_term(fd) 62036519Skarels int fd; 62136519Skarels { 622*36711Skfall struct termios tt; 623*36711Skfall register char *cp = index(term+ENVSIZE, '/'); 62436519Skarels char *speed; 62536519Skarels 626*36711Skfall tcgetattr(fd, &tt); 62736519Skarels if (cp) { 62836519Skarels *cp++ = '\0'; 62936519Skarels speed = cp; 63036519Skarels cp = index(speed, '/'); 63136519Skarels if (cp) 63236519Skarels *cp++ = '\0'; 633*36711Skfall cfsetspeed(&tt, atoi(speed)); 63436519Skarels } 635*36711Skfall tt.c_iflag = BRKINT|ICRNL|IXON|ISTRIP|IEXTEN|IMAXBEL; 636*36711Skfall tt.c_oflag = OPOST|ONLCR|OXTABS; 637*36711Skfall tt.c_lflag = ISIG|ICANON|ECHO; 638*36711Skfall tcsetattr(fd, TCSADFLUSH, &tt); 63936519Skarels 64036519Skarels env[0] = term; 64136519Skarels env[1] = 0; 64236519Skarels environ = env; 64336519Skarels } 64436609Skfall 645*36711Skfall #ifdef KERBEROS 646*36711Skfall #define VERSION_SIZE 9 647*36711Skfall 64836609Skfall /* 649*36711Skfall * Do the remote kerberos login to the named host with the 650*36711Skfall * given inet address 651*36711Skfall * 652*36711Skfall * Return 0 on valid authorization 653*36711Skfall * Return -1 on valid authentication, no authorization 654*36711Skfall * Return >0 for error conditions 655*36711Skfall */ 656*36711Skfall do_krb_login(host, dest, encrypt) 657*36711Skfall char *host; 658*36711Skfall struct sockaddr_in *dest; 659*36711Skfall int encrypt; 660*36711Skfall { 661*36711Skfall int rc; 662*36711Skfall char instance[INST_SZ], version[VERSION_SIZE]; 663*36711Skfall long authopts = 0L; /* !mutual */ 664*36711Skfall struct sockaddr_in faddr; 665*36711Skfall 666*36711Skfall if (getuid()) 667*36711Skfall return(KFAILURE); 668*36711Skfall 669*36711Skfall kdata = (AUTH_DAT *) auth_buf; 670*36711Skfall ticket = (KTEXT) tick_buf; 671*36711Skfall strcpy(instance, "*"); 672*36711Skfall 673*36711Skfall if (encrypt) { 674*36711Skfall rc = sizeof(faddr); 675*36711Skfall if (getsockname(0, &faddr, &rc)) 676*36711Skfall return(-1); 677*36711Skfall authopts = KOPT_DO_MUTUAL; 678*36711Skfall rc = krb_recvauth( 679*36711Skfall authopts, 0, 680*36711Skfall ticket, "rcmd", 681*36711Skfall instance, dest, &faddr, 682*36711Skfall kdata, "", schedule, version); 683*36711Skfall des_set_key(kdata->session, schedule); 684*36711Skfall 685*36711Skfall } else { 686*36711Skfall rc = krb_recvauth( 687*36711Skfall authopts, 0, 688*36711Skfall ticket, "rcmd", 689*36711Skfall instance, dest, (struct sockaddr_in *) 0, 690*36711Skfall kdata, "", (bit_64 *) 0, version); 691*36711Skfall } 692*36711Skfall 693*36711Skfall if (rc != KSUCCESS) 694*36711Skfall return(rc); 695*36711Skfall 696*36711Skfall getstr(lusername, sizeof(lusername), "locuser"); 697*36711Skfall /* get the "cmd" in the rcmd protocol */ 698*36711Skfall getstr(term+ENVSIZE, sizeof(term)-ENVSIZE, "Terminal type"); 699*36711Skfall 700*36711Skfall pwd = getpwnam(lusername); 701*36711Skfall if (pwd == NULL) 702*36711Skfall return(-1); 703*36711Skfall 704*36711Skfall /* returns nonzero for no access */ 705*36711Skfall /* return(ruserok(host, SUPERUSER(pwd), rusername, lusername)); */ 706*36711Skfall if (kuserok(kdata,lusername) != 0) 707*36711Skfall return(-1); 708*36711Skfall 709*36711Skfall return(0); 710*36711Skfall 711*36711Skfall } 712*36711Skfall 713*36711Skfall #endif /* KERBEROS */ 714*36711Skfall 715*36711Skfall usage() 716*36711Skfall { 717*36711Skfall #ifdef KERBEROS 718*36711Skfall syslog(LOG_ERR, "usage: rlogind [-k | -v] [-l] [-n]"); 719*36711Skfall #else 720*36711Skfall syslog(LOG_ERR, "usage: rlogind [-l] [-n]"); 721*36711Skfall #endif 722*36711Skfall } 723*36711Skfall 724*36711Skfall /* 72536631Skarels * Check whether host h is in our local domain, 72636631Skarels * as determined by the part of the name following 72736631Skarels * the first '.' in its name and in ours. 72836631Skarels * If either name is unqualified (contains no '.'), 72936631Skarels * assume that the host is local, as it will be 73036631Skarels * interpreted as such. 73136631Skarels */ 73236631Skarels local_domain(h) 73336631Skarels char *h; 73436625Skfall { 73536631Skarels char localhost[MAXHOSTNAMELEN]; 73636631Skarels char *p1, *p2 = index(h, '.'); 73736631Skarels 73836631Skarels (void) gethostname(localhost, sizeof(localhost)); 73936631Skarels p1 = index(localhost, '.'); 74036631Skarels if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2)) 74136625Skfall return(1); 74236625Skfall return(0); 74336625Skfall } 744