2 * Copyright (C) 1997 and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
53 int s_ctl, s_ctl6, s_rcv, s_snd;
57 rsh_relay(int s_src, int s_dst)
65 FD_SET(s_src, &readfds);
66 tv.tv_sec = FAITH_TIMEOUT;
68 error = select(256, &readfds, NULL, NULL, &tv);
70 exit_failure("select %d: %s", s_src, ERRSTR);
72 exit_failure("connection timeout");
74 n = read(s_src, rshbuf, sizeof(rshbuf));
76 rsh_dual_relay(s_src, s_dst);
79 write(s_dst, rshbuf, n);
80 tcp_relay(s_src, s_dst, "rsh");
85 relay(int src, int dst)
91 error = ioctl(s_rcv, SIOCATMARK, &atmark);
92 if (error != -1 && atmark == 1) {
93 n = read(s_rcv, rshbuf, 1);
95 send(s_snd, rshbuf, 1, MSG_OOB);
99 n = read(s_rcv, rshbuf, sizeof(rshbuf));
103 exit_failure(ERRSTR);
115 exit_success("terminating rsh/control connections");
118 write(s_snd, rshbuf, n);
123 rsh_dual_relay(int s_src, int s_dst)
126 int len, s_wld, error;
127 struct sockaddr_storage ctladdr6;
128 struct sockaddr_storage ctladdr;
129 int port6 = 0, lport, lport6;
137 syslog(LOG_INFO, "starting rsh connection");
139 for (p = rshbuf; *p; p++)
140 port6 = port6 * 10 + *p - '0';
142 len = sizeof(ctladdr6);
143 getpeername(s_src, (struct sockaddr *)&ctladdr6, &len);
144 if (((struct sockaddr *)&ctladdr6)->sa_family == AF_INET6)
145 ((struct sockaddr_in6 *)&ctladdr6)->sin6_port = htons(port6);
147 ((struct sockaddr_in *)&ctladdr6)->sin_port = htons(port6);
149 s_wld = rresvport(&lport);
150 if (s_wld == -1) goto bad;
151 error = listen(s_wld, 1);
152 if (error == -1) goto bad;
153 snprintf(rshbuf, sizeof(rshbuf), "%d", lport);
154 write(s_dst, rshbuf, strlen(rshbuf)+1);
156 len = sizeof(ctladdr);
157 s_ctl = accept(s_wld, (struct sockaddr *)&ctladdr, &len);
158 if (s_ctl == -1) goto bad;
161 sa = (struct sockaddr *)&ctladdr6;
162 s_ctl6 = rresvport_af(&lport6, sa->sa_family);
163 if (s_ctl6 == -1) goto bad;
164 error = connect(s_ctl6, sa, sa->sa_len);
165 if (error == -1) goto bad;
167 syslog(LOG_INFO, "starting rsh control connection");
172 FD_SET(s_src, &readfds);
173 FD_SET(s_dst, &readfds);
174 FD_SET(s_ctl, &readfds);
175 FD_SET(s_ctl6, &readfds);
176 tv.tv_sec = FAITH_TIMEOUT;
179 error = select(256, &readfds, NULL, NULL, &tv);
181 exit_failure("select 4 sockets: %s", ERRSTR);
183 exit_failure("connection timeout");
185 if (half == NO && FD_ISSET(s_src, &readfds)) {
190 if (FD_ISSET(s_dst, &readfds)) {
195 if (FD_ISSET(s_ctl, &readfds)) {
200 if (FD_ISSET(s_ctl6, &readfds)) {
209 exit_failure(ERRSTR);