home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / inet / rexec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-14  |  4.6 KB  |  172 lines

  1. /*
  2.  * Copyright (c) 1980, 1993
  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.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)rexec.c    8.1 (Berkeley) 6/4/93";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include "inetprivate.h"
  39.  
  40. #if NLS
  41. #include "nl_types.h"
  42. #endif
  43.  
  44. int    rexecoptions;
  45.  
  46. int
  47. rexec(ahost, rport, name, pass, cmd, fd2p)
  48.     char **ahost;
  49.     int rport;
  50.     const char *name, *pass, *cmd;
  51.     int *fd2p;
  52. {
  53.     int s, timo = 1, s3;
  54.     struct sockaddr_in sin, sin2, from;
  55.     char c;
  56.     u_short port;
  57.     struct hostent *hp;
  58.  
  59. #if NLS
  60.     libc_nls_init();
  61. #endif
  62.     hp = gethostbyname(*ahost);
  63.     if (hp == 0) {
  64. #if NLS
  65.         fprintf(stderr, "%s: %s\n", *ahost,
  66.                               catgets(_libc_cat, HerrorListSet,
  67.                       2, "unknown host"));
  68. #else
  69.         fprintf(stderr, "%s: unknown host\n", *ahost);
  70. #endif
  71.         return (-1);
  72.     }
  73.     *ahost = hp->h_name;
  74.     ruserpass(hp->h_name, &name, &pass);
  75. retry:
  76.     s = socket(AF_INET, SOCK_STREAM, 0);
  77.     if (s < 0) {
  78. #if NLS
  79.         perror(catgets(_libc_cat, NetMiscSet,
  80.                    NetMiscRexecSocket,
  81.                    "rcmd: socket"));
  82. #else
  83.         perror("rexec: socket");
  84. #endif
  85.         return (-1);
  86.     }
  87.     sin.sin_family = hp->h_addrtype;
  88.     sin.sin_port = rport;
  89.     bcopy(hp->h_addr, (caddr_t)&sin.sin_addr, hp->h_length);
  90.     if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  91.         if (errno == ECONNREFUSED && timo <= 16) {
  92.             (void) close(s);
  93.             sleep(timo);
  94.             timo *= 2;
  95.             goto retry;
  96.         }
  97.         perror(hp->h_name);
  98.         return (-1);
  99.     }
  100.     if (fd2p == 0) {
  101.         (void) write(s, "", 1);
  102.         port = 0;
  103.     } else {
  104.         char num[8];
  105.         int s2, sin2len;
  106.         
  107.         s2 = socket(AF_INET, SOCK_STREAM, 0);
  108.         if (s2 < 0) {
  109.             (void) close(s);
  110.             return (-1);
  111.         }
  112.         sin2len = sizeof (sin2);
  113.         sin2.sin_addr.s_addr = INADDR_ANY;
  114.         sin2.sin_family = sin.sin_family;
  115.         bind(s2, &sin2, sin2len);
  116.         listen(s2, 1);
  117.         if (getsockname(s2, (struct sockaddr *)&sin2, &sin2len) < 0 ||
  118.           sin2len != sizeof (sin2)) {
  119. #if NLS
  120.             perror(catgets(_libc_cat, NetMiscSet,
  121.                        NetMiscGetsockname,
  122.                        "getsockname"));
  123. #else
  124.             perror("getsockname");
  125. #endif
  126.             (void) close(s2);
  127.             goto bad;
  128.         }
  129.         port = ntohs((u_short)sin2.sin_port);
  130.         (void) sprintf(num, "%u", port);
  131.         (void) write(s, num, strlen(num)+1);
  132.         { int len = sizeof (from);
  133.           s3 = accept(s2, (struct sockaddr *)&from, &len);
  134.           close(s2);
  135.           if (s3 < 0) {
  136. #if NLS
  137.             perror(catgets(_libc_cat, NetMiscSet,
  138.                        NetMiscAccept,
  139.                        "accept"));
  140. #else
  141.             perror("accept");
  142. #endif
  143.             port = 0;
  144.             goto bad;
  145.           }
  146.         }
  147.         *fd2p = s3;
  148.     }
  149.     (void) write(s, name, strlen(name) + 1);
  150.     /* should public key encypt the password here */
  151.     (void) write(s, pass, strlen(pass) + 1);
  152.     (void) write(s, cmd, strlen(cmd) + 1);
  153.     if (read(s, &c, 1) != 1) {
  154.         perror(*ahost);
  155.         goto bad;
  156.     }
  157.     if (c != 0) {
  158.         while (read(s, &c, 1) == 1) {
  159.             (void) write(2, &c, 1);
  160.             if (c == '\n')
  161.                 break;
  162.         }
  163.         goto bad;
  164.     }
  165.     return (s);
  166. bad:
  167.     if (port)
  168.         (void) close(*fd2p);
  169.     (void) close(s);
  170.     return (-1);
  171. }
  172.