rexec.c revision 1.12 1 /* $NetBSD: rexec.c,v 1.12 1999/09/16 11:45:47 lukem 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.12 1999/09/16 11:45:47 lukem 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 <assert.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <netdb.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 int rexecoptions;
59
60 int ruserpass __P((const char *, char **, char **));
61 int rexec __P((char **, int, char *, char *, char *, int *));
62
63 int
64 rexec(ahost, rport, name, pass, cmd, fd2p)
65 char **ahost;
66 int rport;
67 char *name, *pass, *cmd;
68 int *fd2p;
69 {
70 struct sockaddr_in sin, from;
71 socklen_t fromlen;
72 struct hostent *hp;
73 u_short port;
74 size_t len;
75 unsigned int timo = 1;
76 int s, s3;
77 char c;
78
79 _DIAGASSERT(ahost != NULL);
80 _DIAGASSERT(name != NULL);
81 _DIAGASSERT(pass != NULL);
82 _DIAGASSERT(cmd != NULL);
83 /* fd2p may be NULL */
84 #ifdef _DIAGNOSTIC
85 if (ahost == NULL || name == NULL || pass == NULL || cmd == NULL)
86 return (-1);
87 #endif
88
89 hp = gethostbyname(*ahost);
90 if (hp == 0) {
91 warnx("Error resolving %s (%s)", *ahost, hstrerror(h_errno));
92 return -1;
93 }
94 *ahost = hp->h_name;
95 (void)ruserpass(hp->h_name, &name, &pass);
96 retry:
97 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
98 warn("Cannot create socket");
99 return (-1);
100 }
101 sin.sin_family = hp->h_addrtype;
102 sin.sin_len = sizeof(sin);
103 sin.sin_port = rport;
104 /* Avoid data corruption from bogus DNS results */
105 if (hp->h_length > sizeof(sin.sin_addr))
106 len = sizeof(sin.sin_addr);
107 else
108 len = hp->h_length;
109 memcpy(&sin.sin_addr, hp->h_addr, len);
110 if (connect(s, (struct sockaddr *)(void *)&sin, sizeof(sin)) == -1) {
111 if (errno == ECONNREFUSED && timo <= 16) {
112 (void)close(s);
113 sleep(timo);
114 timo *= 2;
115 goto retry;
116 }
117 warn("Cannot connect to %s", hp->h_name);
118 return -1;
119 }
120 port = 0;
121 if (fd2p == 0) {
122 (void)write(s, "", 1);
123 } else {
124 char num[8];
125 int s2;
126 struct sockaddr_in sin2;
127 socklen_t sin2len;
128
129 s2 = socket(AF_INET, SOCK_STREAM, 0);
130 if (s2 == -1) {
131 warn("Error creating socket");
132 (void)close(s);
133 return -1;
134 }
135 listen(s2, 1);
136 sin2len = sizeof(sin2);
137 if (getsockname(s2, (struct sockaddr *)(void *)&sin2,
138 &sin2len) == -1 || sin2len != sizeof(sin2)) {
139 warn("Failed to get socket name");
140 (void)close(s2);
141 goto bad;
142 }
143 port = ntohs((u_short)sin2.sin_port);
144 (void)snprintf(num, sizeof(num), "%u", port);
145 (void)write(s, num, strlen(num)+1);
146
147 fromlen = sizeof(from);
148 s3 = accept(s2, (struct sockaddr *)(void *)&from, &fromlen);
149 (void)close(s2);
150 if (s3 == -1) {
151 warn("Error accepting connection");
152 port = 0;
153 goto bad;
154 }
155 *fd2p = s3;
156 }
157 (void)write(s, name, strlen(name) + 1);
158 /* should public key encypt the password here */
159 (void)write(s, pass, strlen(pass) + 1);
160 (void)write(s, cmd, strlen(cmd) + 1);
161 if (read(s, &c, 1) != 1) {
162 warn("Error reading from %s", *ahost);
163 goto bad;
164 }
165 if (c != 0) {
166 while (read(s, &c, 1) == 1) {
167 (void) write(2, &c, 1);
168 if (c == '\n')
169 break;
170 }
171 goto bad;
172 }
173 return s;
174 bad:
175 if (port)
176 (void)close(*fd2p);
177 (void)close(s);
178 return -1;
179 }
180