]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/faithd/rsh.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / usr.sbin / faithd / rsh.c
1 /*
2  * Copyright (C) 1997 and 1998 WIDE Project.
3  * All rights reserved.
4  * 
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  * 
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
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <sys/time.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <syslog.h>
42 #include <unistd.h>
43 #include <errno.h>
44
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <netdb.h>
48
49 #include "faithd.h"
50
51 char rshbuf[MSS];
52
53 int s_ctl, s_ctl6, s_rcv, s_snd;
54 int half;
55
56 void
57 rsh_relay(int s_src, int s_dst)
58 {
59         ssize_t n;
60         fd_set readfds;
61         int error;
62         struct timeval tv;
63
64         FD_ZERO(&readfds);
65         FD_SET(s_src, &readfds);
66         tv.tv_sec = FAITH_TIMEOUT;
67         tv.tv_usec = 0;
68         error = select(256, &readfds, NULL, NULL, &tv);
69         if (error == -1)
70                 exit_failure("select %d: %s", s_src, ERRSTR);
71         else if (error == 0)
72                 exit_failure("connection timeout");
73
74         n = read(s_src, rshbuf, sizeof(rshbuf));
75         if (rshbuf[0] != 0) {
76                 rsh_dual_relay(s_src, s_dst);
77                 /* NOTREACHED */
78         }
79         write(s_dst, rshbuf, n);
80         tcp_relay(s_src, s_dst, "rsh");
81                 /* NOTREACHED */
82 }
83
84 static void
85 relay(int src, int dst)
86 {
87         int error;
88         ssize_t n;      
89         int atmark;
90
91         error = ioctl(s_rcv, SIOCATMARK, &atmark);
92         if (error != -1 && atmark == 1) {
93                 n = read(s_rcv, rshbuf, 1);
94                 if (n == 1)
95                         send(s_snd, rshbuf, 1, MSG_OOB);
96                 return;
97         }
98
99         n = read(s_rcv, rshbuf, sizeof(rshbuf));
100
101         switch (n) {
102         case -1:
103                 exit_failure(ERRSTR);
104         case 0:
105                 if (s_rcv == src) {
106                         /* half close */
107                         shutdown(dst, 1);
108                         half = YES;
109                         break;
110                 }
111                 close(src);
112                 close(dst);
113                 close(s_ctl);
114                 close(s_ctl6);                  
115                 exit_success("terminating rsh/control connections");
116                 break;
117         default:
118                 write(s_snd, rshbuf, n);
119         }
120 }
121
122 void
123 rsh_dual_relay(int s_src, int s_dst)
124 {
125         fd_set readfds;
126         int len, s_wld, error;
127         struct sockaddr_storage ctladdr6;
128         struct sockaddr_storage ctladdr;
129         int port6 = 0, lport, lport6;
130         char *p;
131         struct timeval tv;
132         struct sockaddr *sa;
133
134         half = NO;
135         s_rcv = s_src;
136         s_snd = s_dst;
137         syslog(LOG_INFO, "starting rsh connection");
138
139         for (p = rshbuf; *p; p++)
140                 port6 = port6 * 10 + *p - '0';
141
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);
146         else
147                 ((struct sockaddr_in *)&ctladdr6)->sin_port = htons(port6);
148
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);
155
156         len = sizeof(ctladdr);
157         s_ctl = accept(s_wld, (struct sockaddr *)&ctladdr, &len);
158         if (s_ctl == -1) goto bad;
159         close(s_wld);
160         
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;
166         
167         syslog(LOG_INFO, "starting rsh control connection");
168
169         for (;;) {
170                 FD_ZERO(&readfds);
171                 if (half == NO)
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;
177                 tv.tv_usec = 0;
178
179                 error = select(256, &readfds, NULL, NULL, &tv);
180                 if (error == -1)
181                         exit_failure("select 4 sockets: %s", ERRSTR);
182                 else if (error == 0)
183                         exit_failure("connection timeout");
184
185                 if (half == NO && FD_ISSET(s_src, &readfds)) {
186                         s_rcv = s_src;
187                         s_snd = s_dst;
188                         relay(s_src, s_dst);
189                 }
190                 if (FD_ISSET(s_dst, &readfds)) {
191                         s_rcv = s_dst;
192                         s_snd = s_src;
193                         relay(s_src, s_dst);
194                 }
195                 if (FD_ISSET(s_ctl, &readfds)) {
196                         s_rcv = s_ctl;
197                         s_snd = s_ctl6;
198                         relay(s_src, s_dst);
199                 }
200                 if (FD_ISSET(s_ctl6, &readfds)) {
201                         s_rcv = s_ctl6;
202                         s_snd = s_ctl;
203                         relay(s_src, s_dst);
204                 }
205         }
206         /* NOTREACHED */
207
208  bad:
209         exit_failure(ERRSTR);
210 }