1 /* $KAME: rsh.c,v 1.7 2001/09/05 01:10:30 itojun Exp $ */
4 * Copyright (C) 1997 and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * $FreeBSD: src/usr.sbin/faithd/rsh.c,v 1.2.2.4 2002/04/28 05:40:29 suz Exp $
32 * $DragonFly: src/usr.sbin/faithd/rsh.c,v 1.3 2007/05/18 17:05:12 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
56 int s_ctl
, s_ctl6
, s_rcv
, s_snd
;
60 rsh_relay(int s_src
, int s_dst
)
68 FD_SET(s_src
, &readfds
);
69 tv
.tv_sec
= FAITH_TIMEOUT
;
71 error
= select(256, &readfds
, NULL
, NULL
, &tv
);
73 exit_failure("select %d: %s", s_src
, strerror(errno
));
75 exit_failure("connection timeout");
77 n
= read(s_src
, rshbuf
, sizeof(rshbuf
));
79 rsh_dual_relay(s_src
, s_dst
);
82 write(s_dst
, rshbuf
, n
);
83 tcp_relay(s_src
, s_dst
, "rsh");
88 relay(int src
, int dst
)
94 error
= ioctl(s_rcv
, SIOCATMARK
, &atmark
);
95 if (error
!= -1 && atmark
== 1) {
96 n
= read(s_rcv
, rshbuf
, 1);
98 send(s_snd
, rshbuf
, 1, MSG_OOB
);
102 n
= read(s_rcv
, rshbuf
, sizeof(rshbuf
));
106 exit_failure("%s", strerror(errno
));
110 shutdown(dst
, SHUT_WR
);
118 exit_success("terminating rsh/contorol connections");
121 write(s_snd
, rshbuf
, n
);
126 rsh_dual_relay(int s_src
, int s_dst
)
129 int len
, s_wld
, error
;
130 struct sockaddr_storage ctladdr6
;
131 struct sockaddr_storage ctladdr
;
132 int port6
= 0, lport
, lport6
;
140 syslog(LOG_INFO
, "starting rsh connection");
142 for (p
= rshbuf
; *p
; p
++)
143 port6
= port6
* 10 + *p
- '0';
145 len
= sizeof(ctladdr6
);
146 getpeername(s_src
, (struct sockaddr
*)&ctladdr6
, &len
);
147 if (((struct sockaddr
*)&ctladdr6
)->sa_family
== AF_INET6
)
148 ((struct sockaddr_in6
*)&ctladdr6
)->sin6_port
= htons(port6
);
150 ((struct sockaddr_in
*)&ctladdr6
)->sin_port
= htons(port6
);
152 s_wld
= rresvport(&lport
);
153 if (s_wld
== -1) goto bad
;
154 error
= listen(s_wld
, 1);
155 if (error
== -1) goto bad
;
156 snprintf(rshbuf
, sizeof(rshbuf
), "%d", lport
);
157 write(s_dst
, rshbuf
, strlen(rshbuf
)+1);
159 len
= sizeof(ctladdr
);
160 s_ctl
= accept(s_wld
, (struct sockaddr
*)&ctladdr
, &len
);
161 if (s_ctl
== -1) goto bad
;
164 sa
= (struct sockaddr
*)&ctladdr6
;
165 s_ctl6
= rresvport_af(&lport6
, sa
->sa_family
);
166 if (s_ctl6
== -1) goto bad
;
167 error
= connect(s_ctl6
, sa
, sa
->sa_len
);
168 if (error
== -1) goto bad
;
170 syslog(LOG_INFO
, "starting rsh control connection");
175 FD_SET(s_src
, &readfds
);
176 FD_SET(s_dst
, &readfds
);
177 FD_SET(s_ctl
, &readfds
);
178 FD_SET(s_ctl6
, &readfds
);
179 tv
.tv_sec
= FAITH_TIMEOUT
;
182 error
= select(256, &readfds
, NULL
, NULL
, &tv
);
184 exit_failure("select 4 sockets: %s", strerror(errno
));
186 exit_failure("connection timeout");
188 if (half
== NO
&& FD_ISSET(s_src
, &readfds
)) {
193 if (FD_ISSET(s_dst
, &readfds
)) {
198 if (FD_ISSET(s_ctl
, &readfds
)) {
203 if (FD_ISSET(s_ctl6
, &readfds
)) {
212 exit_failure("%s", strerror(errno
));