home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-B.05 / NETKIT-B / NetKit-B-0.05 / rshd / rshd.c < prev   
Encoding:
C/C++ Source or Header  |  1994-05-23  |  17.0 KB  |  764 lines

  1. /*-
  2.  * Copyright (c) 1988, 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1988, 1989 The Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. /*static char sccsid[] = "from: @(#)rshd.c    5.38 (Berkeley) 3/2/91";*/
  42. static char rcsid[] = "$Id: rshd.c,v 1.1 1994/05/23 09:09:44 rzsfl Exp rzsfl $";
  43. #endif /* not lint */
  44.  
  45. /*
  46.  * From:
  47.  *    $Source: /hda4/ftp/source/networking/NetBSD/new/rshd/rshd.c,v $
  48.  *    $Header: /mit/kerberos/ucb/mit/rshd/RCS/rshd.c,v 
  49.  *        5.2 89/07/31 19:30:04 kfall Exp $
  50.  */
  51.  
  52. /*
  53.  * remote shell server:
  54.  *    [port]\0
  55.  *    remuser\0
  56.  *    locuser\0
  57.  *    command\0
  58.  *    data
  59.  */
  60. #include <sys/param.h>
  61. #include <sys/ioctl.h>
  62. #include <sys/time.h>
  63. #include <fcntl.h>
  64. #include <signal.h>
  65.  
  66. #include <sys/socket.h>
  67. #include <netinet/in.h>
  68. #include <arpa/inet.h>
  69. #include <netdb.h>
  70.  
  71. #include <pwd.h>
  72. #include <syslog.h>
  73. #include <arpa/nameser.h>
  74. #include <resolv.h>
  75. #include <unistd.h>
  76. #include <errno.h>
  77. #include <stdio.h>
  78. #include <stdlib.h>
  79. #include <string.h>
  80. #include <paths.h>
  81.  
  82. int    keepalive = 1;
  83. int    check_all = 0;
  84. int    paranoid = 0;
  85. char    *index(), *rindex(), *strncat();
  86. /*VARARGS1*/
  87. int    error();
  88. int    sent_null;
  89.  
  90. #ifdef    KERBEROS
  91. #include <kerberosIV/des.h>
  92. #include <kerberosIV/krb.h>
  93. #define    VERSION_SIZE    9
  94. #define SECURE_MESSAGE  "This rsh session is using DES encryption for all transmissions.\r\n"
  95. #define    OPTIONS        "alknvxL"
  96. char    authbuf[sizeof(AUTH_DAT)];
  97. char    tickbuf[sizeof(KTEXT_ST)];
  98. int    doencrypt, use_kerberos, vacuous;
  99. Key_schedule    schedule;
  100. #else
  101. #define    OPTIONS    "alnL"
  102. #endif
  103.  
  104. /*ARGSUSED*/
  105. main(argc, argv)
  106.     int argc;
  107.     char **argv;
  108. {
  109.     extern int opterr, optind;
  110.     extern int _check_rhosts_file;
  111.     struct linger linger;
  112.     int ch, on = 1, fromlen;
  113.     struct sockaddr_in from;
  114.  
  115.     openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
  116.  
  117.     opterr = 0;
  118.     while ((ch = getopt(argc, argv, OPTIONS)) != EOF)
  119.         switch (ch) {
  120.         case 'a':
  121.             check_all = 1;
  122.             break;
  123.  
  124.         case 'l':
  125.             _check_rhosts_file = 0;
  126.             break;
  127.  
  128.         case 'n':
  129.             keepalive = 0;
  130.             break;
  131. #ifdef    KERBEROS
  132.         case 'k':
  133.             use_kerberos = 1;
  134.             break;
  135.  
  136.         case 'v':
  137.             vacuous = 1;
  138.             break;
  139.  
  140. #ifdef CRYPT
  141.         case 'x':
  142.             doencrypt = 1;
  143.             break;
  144. #endif
  145. #endif
  146.  
  147.         case 'L':
  148.             paranoid = 1;
  149.             break;
  150.  
  151.         case '?':
  152.         default:
  153.             usage();
  154.             exit(2);
  155.         }
  156.  
  157.     argc -= optind;
  158.     argv += optind;
  159.  
  160. #ifdef    KERBEROS
  161.     if (use_kerberos && vacuous) {
  162.         syslog(LOG_ERR, "only one of -k and -v allowed");
  163.         exit(2);
  164.     }
  165. #ifdef CRYPT
  166.     if (doencrypt && !use_kerberos) {
  167.         syslog(LOG_ERR, "-k is required for -x");
  168.         exit(2);
  169.     }
  170. #endif
  171. #endif
  172.  
  173.     fromlen = sizeof (from);
  174.     if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
  175.         syslog(LOG_ERR, "getpeername: %m");
  176.         _exit(1);
  177.     }
  178.     if (keepalive &&
  179.         setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
  180.         sizeof(on)) < 0)
  181.         syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
  182.     linger.l_onoff = 1;
  183.     linger.l_linger = 60;            /* XXX */
  184.     if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
  185.         sizeof (linger)) < 0)
  186.         syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
  187.     doit(&from);
  188. }
  189.  
  190. char    username[20] = "USER=";
  191. char    homedir[64] = "HOME=";
  192. char    shell[64] = "SHELL=";
  193. char    path[100] = "PATH=";
  194. char    *envinit[] =
  195.         {homedir, shell, path, username, 0};
  196. extern    char    **environ;
  197.  
  198. doit(fromp)
  199.     struct sockaddr_in *fromp;
  200. {
  201.     char cmdbuf[NCARGS+1], *cp;
  202.     char locuser[16], remuser[16];
  203.     struct passwd *pwd;
  204.     int s;
  205.     struct hostent *hp;
  206.     const char *hostname, *errorhost;
  207.     char *errorstr = NULL;
  208.     u_short port;
  209.     int pv[2], pid, cc;
  210.     int nfd;
  211.     fd_set ready, readfrom;
  212.     char buf[BUFSIZ], sig;
  213.     int one = 1;
  214.     char remotehost[2 * MAXHOSTNAMELEN + 1];
  215.  
  216. #ifdef    KERBEROS
  217.     AUTH_DAT    *kdata = (AUTH_DAT *) NULL;
  218.     KTEXT        ticket = (KTEXT) NULL;
  219.     char        instance[INST_SZ], version[VERSION_SIZE];
  220.     struct        sockaddr_in    fromaddr;
  221.     int        rc;
  222.     long        authopts;
  223.     int        pv1[2], pv2[2];
  224.     fd_set        wready, writeto;
  225.  
  226.     fromaddr = *fromp;
  227. #endif
  228.  
  229.     (void) signal(SIGINT, SIG_DFL);
  230.     (void) signal(SIGQUIT, SIG_DFL);
  231.     (void) signal(SIGTERM, SIG_DFL);
  232. #ifdef DEBUG
  233.     { int t = open(_PATH_TTY, 2);
  234.       if (t >= 0) {
  235.         ioctl(t, TIOCNOTTY, (char *)0);
  236.         (void) close(t);
  237.       }
  238.     }
  239. #endif
  240.     fromp->sin_port = ntohs((u_short)fromp->sin_port);
  241.     if (fromp->sin_family != AF_INET) {
  242.         syslog(LOG_ERR, "malformed \"from\" address (af %d)\n",
  243.             fromp->sin_family);
  244.         exit(1);
  245.     }
  246. #ifdef IP_OPTIONS
  247.       {
  248.     u_char optbuf[BUFSIZ/3], *cp;
  249.     char lbuf[BUFSIZ], *lp;
  250.     int optsize = sizeof(optbuf), ipproto;
  251.     struct protoent *ip;
  252.  
  253.     if ((ip = getprotobyname("ip")) != NULL)
  254.         ipproto = ip->p_proto;
  255.     else
  256.         ipproto = IPPROTO_IP;
  257.     if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
  258.         optsize != 0) {
  259.         lp = lbuf;
  260.         for (cp = optbuf; optsize > 0; cp++, optsize--, lp += 3)
  261.             sprintf(lp, " %2.2x", *cp);
  262.         syslog(LOG_NOTICE,
  263.             "Connection received from %s using IP options (ignored):%s",
  264.             inet_ntoa(fromp->sin_addr), lbuf);
  265.         if (setsockopt(0, ipproto, IP_OPTIONS,
  266.             (char *)NULL, optsize) != 0) {
  267.             syslog(LOG_ERR, "setsockopt IP_OPTIONS NULL: %m");
  268.             exit(1);
  269.         }
  270.     }
  271.       }
  272. #endif
  273.  
  274. #ifdef    KERBEROS
  275.     if (!use_kerberos)
  276. #endif
  277.         if (fromp->sin_port >= IPPORT_RESERVED ||
  278.             fromp->sin_port < IPPORT_RESERVED/2) {
  279.             syslog(LOG_NOTICE|LOG_AUTH,
  280.                 "Connection from %s on illegal port",
  281.                 inet_ntoa(fromp->sin_addr));
  282.             exit(1);
  283.         }
  284.  
  285.     (void) alarm(60);
  286.     port = 0;
  287.     for (;;) {
  288.         char c;
  289.         if ((cc = read(0, &c, 1)) != 1) {
  290.             if (cc < 0)
  291.                 syslog(LOG_NOTICE, "read: %m");
  292.             shutdown(0, 1+1);
  293.             exit(1);
  294.         }
  295.         if (c== 0)
  296.             break;
  297.         port = port * 10 + c - '0';
  298.     }
  299.  
  300.     (void) alarm(0);
  301.     if (port != 0) {
  302.         int lport = IPPORT_RESERVED - 1;
  303.         s = rresvport(&lport);
  304.         if (s < 0) {
  305.             syslog(LOG_ERR, "can't get stderr port: %m");
  306.             exit(1);
  307.         }
  308. #ifdef    KERBEROS
  309.         if (!use_kerberos)
  310. #endif
  311.             if (port >= IPPORT_RESERVED) {
  312.                 syslog(LOG_ERR, "2nd port not reserved\n");
  313.                 exit(1);
  314.             }
  315.         fromp->sin_port = htons(port);
  316.         if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
  317.             syslog(LOG_INFO, "connect second port: %m");
  318.             exit(1);
  319.         }
  320.     }
  321.  
  322. #ifdef    KERBEROS
  323.     if (vacuous) {
  324.         error("rshd: remote host requires Kerberos authentication\n");
  325.         exit(1);
  326.     }
  327. #endif
  328.  
  329. #ifdef notdef
  330.     /* from inetd, socket is already on 0, 1, 2 */
  331.     dup2(f, 0);
  332.     dup2(f, 1);
  333.     dup2(f, 2);
  334. #endif
  335.     hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
  336.         fromp->sin_family);
  337.     if (hp) {
  338.         /*
  339.          * If name returned by gethostbyaddr is in our domain,
  340.          * attempt to verify that we haven't been fooled by someone
  341.          * in a remote net; look up the name and check that this
  342.          * address corresponds to the name.
  343.          */
  344.         hostname = hp->h_name;
  345. #ifdef    KERBEROS
  346.         if (!use_kerberos)
  347. #endif
  348.         if (check_all || local_domain(hp->h_name)) {
  349.             strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
  350.             remotehost[sizeof(remotehost) - 1] = 0;
  351.             errorhost = remotehost;
  352. #ifdef    RES_DNSRCH
  353.             _res.options &= ~RES_DNSRCH;
  354. #endif
  355.             hp = gethostbyname(remotehost);
  356.             if (hp == NULL) {
  357.                 syslog(LOG_INFO,
  358.                     "Couldn't look up address for %s",
  359.                     remotehost);
  360.                 errorstr =
  361.                 "Couldn't look up address for your host (%s)\n";
  362.                 hostname = inet_ntoa(fromp->sin_addr);
  363.             } else for (; ; hp->h_addr_list++) {
  364.                 if (hp->h_addr_list[0] == NULL) {
  365.                     syslog(LOG_NOTICE,
  366.                       "Host addr %s not listed for host %s",
  367.                         inet_ntoa(fromp->sin_addr),
  368.                         hp->h_name);
  369.                     errorstr =
  370.                         "Host address mismatch for %s\n";
  371.                     hostname = inet_ntoa(fromp->sin_addr);
  372.                     break;
  373.                 }
  374.                 if (!bcmp(hp->h_addr_list[0],
  375.                     (caddr_t)&fromp->sin_addr,
  376.                     sizeof(fromp->sin_addr))) {
  377.                     hostname = hp->h_name;
  378.                     break;
  379.                 }
  380.             }
  381.         }
  382.     } else
  383.         errorhost = hostname = inet_ntoa(fromp->sin_addr);
  384.  
  385. #ifdef    KERBEROS
  386.     if (use_kerberos) {
  387.         kdata = (AUTH_DAT *) authbuf;
  388.         ticket = (KTEXT) tickbuf;
  389.         authopts = 0L;
  390.         strcpy(instance, "*");
  391.         version[VERSION_SIZE - 1] = '\0';
  392. #ifdef CRYPT
  393.         if (doencrypt) {
  394.             struct sockaddr_in local_addr;
  395.             rc = sizeof(local_addr);
  396.             if (getsockname(0, (struct sockaddr *)&local_addr,
  397.                 &rc) < 0) {
  398.                 syslog(LOG_ERR, "getsockname: %m");
  399.                 error("rlogind: getsockname: %m");
  400.                 exit(1);
  401.             }
  402.             authopts = KOPT_DO_MUTUAL;
  403.             rc = krb_recvauth(authopts, 0, ticket,
  404.                 "rcmd", instance, &fromaddr,
  405.                 &local_addr, kdata, "", schedule,
  406.                 version);
  407.             des_set_key(kdata->session, schedule);
  408.         } else
  409. #endif
  410.             rc = krb_recvauth(authopts, 0, ticket, "rcmd",
  411.                 instance, &fromaddr,
  412.                 (struct sockaddr_in *) 0,
  413.                 kdata, "", (bit_64 *) 0, version);
  414.         if (rc != KSUCCESS) {
  415.             error("Kerberos authentication failure: %s\n",
  416.                   krb_err_txt[rc]);
  417.             exit(1);
  418.         }
  419.     } else
  420. #endif
  421.         getstr(remuser, sizeof(remuser), "remuser");
  422.  
  423.     getstr(locuser, sizeof(locuser), "locuser");
  424.     getstr(cmdbuf, sizeof(cmdbuf), "command");
  425.     setpwent();
  426.     pwd = getpwnam(locuser);
  427.     if (pwd == NULL) {
  428.         if (errorstr == NULL)
  429.             errorstr = "Login incorrect.\n";
  430.         goto fail;
  431.     }
  432.     if (chdir(pwd->pw_dir) < 0) {
  433.         (void) chdir("/");
  434. #ifdef notdef
  435.         error("No remote directory.\n");
  436.         exit(1);
  437. #endif
  438.     }
  439.  
  440. #ifdef    KERBEROS
  441.     if (use_kerberos) {
  442.         if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') {
  443.             if (kuserok(kdata, locuser) != 0) {
  444.                 syslog(LOG_NOTICE|LOG_AUTH,
  445.                     "Kerberos shell denied to %s.%s@%s on %s as %s: cmd='%s'; %s",
  446.                     kdata->pname, kdata->pinst, kdata->prealm,
  447.                                 hostname, locuser, cmdbuf, errorstr);
  448.                 error("Permission denied.\n");
  449.                 exit(1);
  450.             }
  451.         }
  452.     } else
  453. #endif
  454.  
  455.         if (errorstr ||
  456.             ruserok(hostname, pwd->pw_uid == 0, remuser, locuser) < 0) {
  457. fail:
  458.             if (errorstr == NULL)
  459.                 errorstr = "Permission denied.\n";
  460.  
  461.             /* log the (failed) rsh request, if paranoid */
  462.             if (paranoid || pwd->pw_uid == 0)
  463.                     syslog(LOG_INFO|LOG_AUTH,
  464.                     "rsh denied to %s@%s as %s: cmd='%s'; %s",
  465.                     remuser, hostname, locuser, cmdbuf,
  466.                     errorstr);
  467.  
  468.             error(errorstr, errorhost);
  469.             exit(1);
  470.         }
  471.  
  472.     if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
  473.         error("Logins currently disabled.\n");
  474.         exit(1);
  475.     }
  476.  
  477.     (void) write(2, "\0", 1);
  478.     sent_null = 1;
  479.  
  480.     if (port) {
  481.         if (pipe(pv) < 0) {
  482.             error("Can't make pipe.\n");
  483.             exit(1);
  484.         }
  485. #ifdef CRYPT
  486. #ifdef KERBEROS
  487.         if (doencrypt) {
  488.             if (pipe(pv1) < 0) {
  489.                 error("Can't make 2nd pipe.\n");
  490.                 exit(1);
  491.             }
  492.             if (pipe(pv2) < 0) {
  493.                 error("Can't make 3rd pipe.\n");
  494.                 exit(1);
  495.             }
  496.         }
  497. #endif
  498. #endif
  499.         pid = fork();
  500.         if (pid == -1)  {
  501.             error("Can't fork; try again.\n");
  502.             exit(1);
  503.         }
  504.         if (pid) {
  505. #ifdef CRYPT
  506. #ifdef KERBEROS
  507.             if (doencrypt) {
  508.                 static char msg[] = SECURE_MESSAGE;
  509.                 (void) close(pv1[1]);
  510.                 (void) close(pv2[1]);
  511.                 des_write(s, msg, sizeof(msg));
  512.  
  513.             } else
  514. #endif
  515. #endif
  516.             {
  517.                 (void) close(0); (void) close(1);
  518.             }
  519.             (void) close(2); (void) close(pv[1]);
  520.  
  521.             FD_ZERO(&readfrom);
  522.             FD_SET(s, &readfrom);
  523.             FD_SET(pv[0], &readfrom);
  524.             if (pv[0] > s)
  525.                 nfd = pv[0];
  526.             else
  527.                 nfd = s;
  528. #ifdef CRYPT
  529. #ifdef KERBEROS
  530.             if (doencrypt) {
  531.                 FD_ZERO(&writeto);
  532.                 FD_SET(pv2[0], &writeto);
  533.                 FD_SET(pv1[0], &readfrom);
  534.  
  535.                 nfd = MAX(nfd, pv2[0]);
  536.                 nfd = MAX(nfd, pv1[0]);
  537.             } else
  538. #endif
  539. #endif
  540.                 ioctl(pv[0], FIONBIO, (char *)&one);
  541.  
  542.             /* should set s nbio! */
  543.             nfd++;
  544.             do {
  545.                 ready = readfrom;
  546. #ifdef CRYPT
  547. #ifdef KERBEROS
  548.                 if (doencrypt) {
  549.                     wready = writeto;
  550.                     if (select(nfd, &ready,
  551.                         &wready, (fd_set *) 0,
  552.                         (struct timeval *) 0) < 0)
  553.                         break;
  554.                 } else
  555. #endif
  556. #endif
  557.                     if (select(nfd, &ready, (fd_set *)0,
  558.                       (fd_set *)0, (struct timeval *)0) < 0)
  559.                         break;
  560.                 if (FD_ISSET(s, &ready)) {
  561.                     int    ret;
  562. #ifdef CRYPT
  563. #ifdef KERBEROS
  564.                     if (doencrypt)
  565.                         ret = des_read(s, &sig, 1);
  566.                     else
  567. #endif
  568. #endif
  569.                         ret = read(s, &sig, 1);
  570.                     if (ret <= 0)
  571.                         FD_CLR(s, &readfrom);
  572.                     else
  573.                         killpg(pid, sig);
  574.                 }
  575.                 if (FD_ISSET(pv[0], &ready)) {
  576.                     errno = 0;
  577.                     cc = read(pv[0], buf, sizeof(buf));
  578.                     if (cc <= 0) {
  579.                         shutdown(s, 1+1);
  580.                         FD_CLR(pv[0], &readfrom);
  581.                     } else {
  582. #ifdef CRYPT
  583. #ifdef KERBEROS
  584.                         if (doencrypt)
  585.                             (void)
  586.                               des_write(s, buf, cc);
  587.                         else
  588. #endif
  589. #endif
  590.                             (void)
  591.                               write(s, buf, cc);
  592.                     }
  593.                 }
  594. #ifdef CRYPT
  595. #ifdef KERBEROS
  596.                 if (doencrypt && FD_ISSET(pv1[0], &ready)) {
  597.                     errno = 0;
  598.                     cc = read(pv1[0], buf, sizeof(buf));
  599.                     if (cc <= 0) {
  600.                         shutdown(pv1[0], 1+1);
  601.                         FD_CLR(pv1[0], &readfrom);
  602.                     } else
  603.                         (void) des_write(1, buf, cc);
  604.                 }
  605.  
  606.                 if (doencrypt && FD_ISSET(pv2[0], &wready)) {
  607.                     errno = 0;
  608.                     cc = des_read(0, buf, sizeof(buf));
  609.                     if (cc <= 0) {
  610.                         shutdown(pv2[0], 1+1);
  611.                         FD_CLR(pv2[0], &writeto);
  612.                     } else
  613.                         (void) write(pv2[0], buf, cc);
  614.                 }
  615. #endif
  616. #endif
  617.  
  618.             } while (FD_ISSET(s, &readfrom) ||
  619. #ifdef CRYPT
  620. #ifdef KERBEROS
  621.                 (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
  622. #endif
  623. #endif
  624.                 FD_ISSET(pv[0], &readfrom));
  625.             exit(0);
  626.         }
  627.         setpgrp();
  628.         (void) close(s); (void) close(pv[0]);
  629. #ifdef CRYPT
  630. #ifdef KERBEROS
  631.         if (doencrypt) {
  632.             close(pv1[0]); close(pv2[0]);
  633.             dup2(pv1[1], 1);
  634.             dup2(pv2[1], 0);
  635.             close(pv1[1]);
  636.             close(pv2[1]);
  637.         }
  638. #endif
  639. #endif
  640.         dup2(pv[1], 2);
  641.         close(pv[1]);
  642.     }
  643.     if (*pwd->pw_shell == '\0')
  644.         pwd->pw_shell = _PATH_BSHELL;
  645. #if    BSD > 43
  646.     if (setlogin(pwd->pw_name) < 0)
  647.         syslog(LOG_ERR, "setlogin() failed: %m");
  648. #endif
  649.     (void) setgid((gid_t)pwd->pw_gid);
  650.     initgroups(pwd->pw_name, pwd->pw_gid);
  651.     (void) setuid((uid_t)pwd->pw_uid);
  652.     environ = envinit;
  653.     strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
  654.     strcat(path, _PATH_DEFPATH);
  655.     strncat(shell, pwd->pw_shell, sizeof(shell)-7);
  656.     strncat(username, pwd->pw_name, sizeof(username)-6);
  657.     cp = rindex(pwd->pw_shell, '/');
  658.     if (cp)
  659.         cp++;
  660.     else
  661.         cp = pwd->pw_shell;
  662.     endpwent();
  663.     if (paranoid || pwd->pw_uid == 0) {
  664. #ifdef    KERBEROS
  665.         if (use_kerberos)
  666.             syslog(LOG_INFO|LOG_AUTH,
  667.                         "Kerberos shell from %s.%s@%s on %s as %s: cmd='%s'",
  668.                         kdata->pname, kdata->pinst, kdata->prealm,
  669.                         hostname, locuser, cmdbuf);
  670.         else
  671. #endif
  672.             syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%s'",
  673.             remuser, hostname, locuser, cmdbuf);
  674.     }
  675.  
  676.  
  677.     execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
  678.     perror(pwd->pw_shell);
  679.     exit(1);
  680. }
  681.  
  682. /*
  683.  * Report error to client.
  684.  * Note: can't be used until second socket has connected
  685.  * to client, or older clients will hang waiting
  686.  * for that connection first.
  687.  */
  688. /*VARARGS1*/
  689. error(fmt, a1, a2, a3)
  690.     char *fmt;
  691.     int a1, a2, a3;
  692. {
  693.     char buf[BUFSIZ], *bp = buf;
  694.  
  695.     if (sent_null == 0)
  696.         *bp++ = 1;
  697.     (void) sprintf(bp, fmt, a1, a2, a3);
  698.     (void) write(2, buf, strlen(buf));
  699. }
  700.  
  701. getstr(buf, cnt, err)
  702.     char *buf;
  703.     int cnt;
  704.     char *err;
  705. {
  706.     char c;
  707.  
  708.     do {
  709.         if (read(0, &c, 1) != 1)
  710.             exit(1);
  711.         *buf++ = c;
  712.         if (--cnt == 0) {
  713.             error("%s too long\n", err);
  714.             exit(1);
  715.         }
  716.     } while (c != 0);
  717. }
  718.  
  719. /*
  720.  * Check whether host h is in our local domain,
  721.  * defined as sharing the last two components of the domain part,
  722.  * or the entire domain part if the local domain has only one component.
  723.  * If either name is unqualified (contains no '.'),
  724.  * assume that the host is local, as it will be
  725.  * interpreted as such.
  726.  */
  727. local_domain(h)
  728.     char *h;
  729. {
  730.     char localhost[MAXHOSTNAMELEN];
  731.     char *p1, *p2, *topdomain();
  732.  
  733.     localhost[0] = 0;
  734.     (void) gethostname(localhost, sizeof(localhost));
  735.     p1 = topdomain(localhost);
  736.     p2 = topdomain(h);
  737.     if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
  738.         return(1);
  739.     return(0);
  740. }
  741.  
  742. char *
  743. topdomain(h)
  744.     char *h;
  745. {
  746.     register char *p;
  747.     char *maybe = NULL;
  748.     int dots = 0;
  749.  
  750.     for (p = h + strlen(h); p >= h; p--) {
  751.         if (*p == '.') {
  752.             if (++dots == 2)
  753.                 return (p);
  754.             maybe = p;
  755.         }
  756.     }
  757.     return (maybe);
  758. }
  759.  
  760. usage()
  761. {
  762.     syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
  763. }
  764.