rexec.c revision 1.9 1 /* $NetBSD: rexec.c,v 1.9 1999/05/04 17:13:57 christos 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.9 1999/05/04 17:13:57 christos 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, sin2, from;
70 struct hostent *hp;
71 u_short port;
72 size_t len;
73 unsigned int timo = 1;
74 int s, s3;
75 char c;
76
77 hp = gethostbyname(*ahost);
78 if (hp == 0) {
79 warnx("Error resolving %s (%s)", *ahost, hstrerror(h_errno));
80 return -1;
81 }
82 *ahost = hp->h_name;
83 (void)ruserpass(hp->h_name, &name, &pass);
84 retry:
85 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
86 warn("Cannot create socket");
87 return (-1);
88 }
89 sin.sin_family = hp->h_addrtype;
90 sin.sin_len = sizeof(sin);
91 sin.sin_port = rport;
92 /* Avoid data corruption from bogus DNS results */
93 if (hp->h_length > sizeof(sin.sin_addr))
94 len = sizeof(sin.sin_addr);
95 else
96 len = hp->h_length;
97 memcpy(&sin.sin_addr, hp->h_addr, len);
98 if (connect(s, (struct sockaddr *)(void *)&sin, sizeof(sin)) == -1) {
99 if (errno == ECONNREFUSED && timo <= 16) {
100 (void)close(s);
101 sleep(timo);
102 timo *= 2;
103 goto retry;
104 }
105 warn("Cannot connect to %s", hp->h_name);
106 return -1;
107 }
108 port = 0;
109 if (fd2p == 0) {
110 (void)write(s, "", 1);
111 } else {
112 char num[8];
113 int s2;
114 socklen_t sin2len;
115
116 s2 = socket(AF_INET, SOCK_STREAM, 0);
117 if (s2 == -1) {
118 warn("Error creating socket");
119 (void)close(s);
120 return -1;
121 }
122 listen(s2, 1);
123 sin2len = sizeof(sin2);
124 if (getsockname(s2, (struct sockaddr *)(void *)&sin2,
125 &sin2len) == -1 || sin2len != sizeof(sin2)) {
126 warn("Failed to get socket name");
127 (void)close(s2);
128 goto bad;
129 }
130 port = ntohs((u_short)sin2.sin_port);
131 (void)snprintf(num, sizeof(num), "%u", port);
132 (void)write(s, num, strlen(num)+1);
133
134 len = sizeof(from);
135 s3 = accept(s2, (struct sockaddr *)(void *)&from, &len);
136 (void)close(s2);
137 if (s3 == -1) {
138 warn("Error accepting connection");
139 port = 0;
140 goto bad;
141 }
142 *fd2p = s3;
143 }
144 (void)write(s, name, strlen(name) + 1);
145 /* should public key encypt the password here */
146 (void)write(s, pass, strlen(pass) + 1);
147 (void)write(s, cmd, strlen(cmd) + 1);
148 if (read(s, &c, 1) != 1) {
149 warn("Error reading from %s", *ahost);
150 goto bad;
151 }
152 if (c != 0) {
153 while (read(s, &c, 1) == 1) {
154 (void) write(2, &c, 1);
155 if (c == '\n')
156 break;
157 }
158 goto bad;
159 }
160 return s;
161 bad:
162 if (port)
163 (void)close(*fd2p);
164 (void)close(s);
165 return -1;
166 }
167