Home | History | Annotate | Line # | Download | only in 4.3
rexec.c revision 1.10
      1 /*	$NetBSD: rexec.c,v 1.10 1999/05/09 12:28:15 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1980, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 #if 0
     39 static char sccsid[] = "@(#)rexec.c	8.1 (Berkeley) 6/4/93";
     40 #else
     41 __RCSID("$NetBSD: rexec.c,v 1.10 1999/05/09 12:28:15 mycroft Exp $");
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/socket.h>
     47 
     48 #include <netinet/in.h>
     49 
     50 #include <stdio.h>
     51 #include <netdb.h>
     52 #include <string.h>
     53 #include <errno.h>
     54 #include <unistd.h>
     55 #include <err.h>
     56 
     57 int	rexecoptions;
     58 
     59 int	ruserpass __P((const char *, char **, char **));
     60 int	rexec __P((char **, int, char *, char *, char *, int *));
     61 
     62 int
     63 rexec(ahost, rport, name, pass, cmd, fd2p)
     64 	char **ahost;
     65 	int rport;
     66 	char *name, *pass, *cmd;
     67 	int *fd2p;
     68 {
     69 	struct sockaddr_in sin, from;
     70 	socklen_t fromlen;
     71 	struct hostent *hp;
     72 	u_short port;
     73 	size_t len;
     74 	unsigned int timo = 1;
     75 	int s, s3;
     76 	char c;
     77 
     78 	hp = gethostbyname(*ahost);
     79 	if (hp == 0) {
     80 		warnx("Error resolving %s (%s)", *ahost, hstrerror(h_errno));
     81 		return -1;
     82 	}
     83 	*ahost = hp->h_name;
     84 	(void)ruserpass(hp->h_name, &name, &pass);
     85 retry:
     86 	if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
     87 		warn("Cannot create socket");
     88 		return (-1);
     89 	}
     90 	sin.sin_family = hp->h_addrtype;
     91 	sin.sin_len = sizeof(sin);
     92 	sin.sin_port = rport;
     93 	/* Avoid data corruption from bogus DNS results */
     94 	if (hp->h_length > sizeof(sin.sin_addr))
     95 		len = sizeof(sin.sin_addr);
     96 	else
     97 		len = hp->h_length;
     98 	memcpy(&sin.sin_addr, hp->h_addr, len);
     99 	if (connect(s, (struct sockaddr *)(void *)&sin, sizeof(sin)) == -1) {
    100 		if (errno == ECONNREFUSED && timo <= 16) {
    101 			(void)close(s);
    102 			sleep(timo);
    103 			timo *= 2;
    104 			goto retry;
    105 		}
    106 		warn("Cannot connect to %s", hp->h_name);
    107 		return -1;
    108 	}
    109 	port = 0;
    110 	if (fd2p == 0) {
    111 		(void)write(s, "", 1);
    112 	} else {
    113 		char num[8];
    114 		int s2;
    115 		struct sockaddr_in sin2;
    116 		socklen_t sin2len;
    117 
    118 		s2 = socket(AF_INET, SOCK_STREAM, 0);
    119 		if (s2 == -1) {
    120 			warn("Error creating socket");
    121 			(void)close(s);
    122 			return -1;
    123 		}
    124 		listen(s2, 1);
    125 		sin2len = sizeof(sin2);
    126 		if (getsockname(s2, (struct sockaddr *)(void *)&sin2,
    127 		    &sin2len) == -1 || sin2len != sizeof(sin2)) {
    128 			warn("Failed to get socket name");
    129 			(void)close(s2);
    130 			goto bad;
    131 		}
    132 		port = ntohs((u_short)sin2.sin_port);
    133 		(void)snprintf(num, sizeof(num), "%u", port);
    134 		(void)write(s, num, strlen(num)+1);
    135 
    136 		fromlen = sizeof(from);
    137 		s3 = accept(s2, (struct sockaddr *)(void *)&from, &fromlen);
    138 		(void)close(s2);
    139 		if (s3 == -1) {
    140 			warn("Error accepting connection");
    141 			port = 0;
    142 			goto bad;
    143 		}
    144 		*fd2p = s3;
    145 	}
    146 	(void)write(s, name, strlen(name) + 1);
    147 	/* should public key encypt the password here */
    148 	(void)write(s, pass, strlen(pass) + 1);
    149 	(void)write(s, cmd, strlen(cmd) + 1);
    150 	if (read(s, &c, 1) != 1) {
    151 		warn("Error reading from %s", *ahost);
    152 		goto bad;
    153 	}
    154 	if (c != 0) {
    155 		while (read(s, &c, 1) == 1) {
    156 			(void) write(2, &c, 1);
    157 			if (c == '\n')
    158 				break;
    159 		}
    160 		goto bad;
    161 	}
    162 	return s;
    163 bad:
    164 	if (port)
    165 		(void)close(*fd2p);
    166 	(void)close(s);
    167 	return -1;
    168 }
    169