Back to index

glibc  2.9
rexec.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1980, 1993
00003  *     The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #if defined(LIBC_SCCS) && !defined(lint)
00031 static char sccsid[] = "@(#)rexec.c       8.1 (Berkeley) 6/4/93";
00032 #endif /* LIBC_SCCS and not lint */
00033 
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 
00037 #include <netinet/in.h>
00038 
00039 #include <alloca.h>
00040 #include <stdio.h>
00041 #include <netdb.h>
00042 #include <errno.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <unistd.h>
00046 #include <sys/uio.h>
00047 
00048 int    rexecoptions;
00049 libc_freeres_ptr (static char *ahostbuf);
00050 
00051 int
00052 rexec_af(ahost, rport, name, pass, cmd, fd2p, af)
00053        char **ahost;
00054        int rport;
00055        const char *name, *pass, *cmd;
00056        int *fd2p;
00057        sa_family_t af;
00058 {
00059        struct sockaddr_storage sa2, from;
00060        struct addrinfo hints, *res0;
00061        const char *orig_name = name;
00062        const char *orig_pass = pass;
00063        u_short port = 0;
00064        int s, timo = 1, s3;
00065        char c;
00066        int gai;
00067        char servbuff[NI_MAXSERV];
00068 
00069        __snprintf(servbuff, sizeof(servbuff), "%d", ntohs(rport));
00070        servbuff[sizeof(servbuff) - 1] = '\0';
00071 
00072        memset(&hints, '\0', sizeof(hints));
00073        hints.ai_family = af;
00074        hints.ai_socktype = SOCK_STREAM;
00075        hints.ai_flags = AI_CANONNAME;
00076        gai = getaddrinfo(*ahost, servbuff, &hints, &res0);
00077        if (gai){
00078               /* XXX: set errno? */
00079               return -1;
00080        }
00081 
00082        if (res0->ai_canonname){
00083               free (ahostbuf);
00084               ahostbuf = strdup (res0->ai_canonname);
00085               if (ahostbuf == NULL) {
00086                      perror ("rexec: strdup");
00087                      return (-1);
00088               }
00089               *ahost = ahostbuf;
00090        } else {
00091               *ahost = NULL;
00092               __set_errno (ENOENT);
00093               return -1;
00094        }
00095        ruserpass(res0->ai_canonname, &name, &pass);
00096 retry:
00097        s = __socket(res0->ai_family, res0->ai_socktype, 0);
00098        if (s < 0) {
00099               perror("rexec: socket");
00100               return (-1);
00101        }
00102        if (__connect(s, res0->ai_addr, res0->ai_addrlen) < 0) {
00103               if (errno == ECONNREFUSED && timo <= 16) {
00104                      (void) __close(s);
00105                      __sleep(timo);
00106                      timo *= 2;
00107                      goto retry;
00108               }
00109               perror(res0->ai_canonname);
00110               return (-1);
00111        }
00112        if (fd2p == 0) {
00113               (void) __write(s, "", 1);
00114               port = 0;
00115        } else {
00116               char num[32];
00117               int s2;
00118               socklen_t sa2len;
00119 
00120               s2 = __socket(res0->ai_family, res0->ai_socktype, 0);
00121               if (s2 < 0) {
00122                      (void) __close(s);
00123                      return (-1);
00124               }
00125               __listen(s2, 1);
00126               sa2len = sizeof (sa2);
00127               if (__getsockname(s2, (struct sockaddr *)&sa2, &sa2len) < 0) {
00128                      perror("getsockname");
00129                      (void) __close(s2);
00130                      goto bad;
00131               } else if (sa2len != SA_LEN((struct sockaddr *)&sa2)) {
00132                      __set_errno(EINVAL);
00133                      (void) __close(s2);
00134                      goto bad;
00135               }
00136               port = 0;
00137               if (!getnameinfo((struct sockaddr *)&sa2, sa2len,
00138                              NULL, 0, servbuff, sizeof(servbuff),
00139                              NI_NUMERICSERV))
00140                      port = atoi(servbuff);
00141               (void) sprintf(num, "%u", port);
00142               (void) __write(s, num, strlen(num)+1);
00143               { socklen_t len = sizeof (from);
00144                 s3 = TEMP_FAILURE_RETRY (accept(s2, (struct sockaddr *)&from,
00145                                             &len));
00146                 __close(s2);
00147                 if (s3 < 0) {
00148                      perror("accept");
00149                      port = 0;
00150                      goto bad;
00151                 }
00152               }
00153               *fd2p = s3;
00154        }
00155 
00156        struct iovec iov[3] =
00157          {
00158            [0] = { .iov_base = (void *) name, .iov_len = strlen (name) + 1 },
00159            /* should public key encypt the password here */
00160            [1] = { .iov_base = (void *) pass, .iov_len = strlen (pass) + 1 },
00161            [2] = { .iov_base = (void *) cmd, .iov_len = strlen (cmd) + 1 }
00162          };
00163        (void) TEMP_FAILURE_RETRY (__writev (s, iov, 3));
00164 
00165        /* We don't need the memory allocated for the name and the password
00166           in ruserpass anymore.  */
00167        if (name != orig_name)
00168          free ((char *) name);
00169        if (pass != orig_pass)
00170          free ((char *) pass);
00171 
00172        if (__read(s, &c, 1) != 1) {
00173               perror(*ahost);
00174               goto bad;
00175        }
00176        if (c != 0) {
00177               while (__read(s, &c, 1) == 1) {
00178                      (void) __write(2, &c, 1);
00179                      if (c == '\n')
00180                             break;
00181               }
00182               goto bad;
00183        }
00184        freeaddrinfo(res0);
00185        return (s);
00186 bad:
00187        if (port)
00188               (void) __close(*fd2p);
00189        (void) __close(s);
00190        freeaddrinfo(res0);
00191        return (-1);
00192 }
00193 libc_hidden_def (rexec_af)
00194 
00195 int
00196 rexec(ahost, rport, name, pass, cmd, fd2p)
00197        char **ahost;
00198        int rport;
00199        const char *name, *pass, *cmd;
00200        int *fd2p;
00201 {
00202        return rexec_af(ahost, rport, name, pass, cmd, fd2p, AF_INET);
00203 }