autofs: Add "noatime" to auto_master
[dragonfly.git] / usr.sbin / faithd / rsh.c
1 /*      $KAME: rsh.c,v 1.7 2001/09/05 01:10:30 itojun Exp $     */
2
3 /*
4  * Copyright (C) 1997 and 1998 WIDE Project.
5  * 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. 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.
18  *
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
29  * SUCH DAMAGE.
30  *
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 $
33  */
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/ioctl.h>
39 #include <sys/time.h>
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <syslog.h>
45 #include <unistd.h>
46 #include <errno.h>
47
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <netdb.h>
51
52 #include "faithd.h"
53
54 char rshbuf[MSS];
55
56 int s_ctl, s_ctl6, s_rcv, s_snd;
57 int half;
58
59 void
60 rsh_relay(int s_src, int s_dst)
61 {
62         ssize_t n;
63         fd_set readfds;
64         int error;
65         struct timeval tv;
66
67         FD_ZERO(&readfds);
68         FD_SET(s_src, &readfds);
69         tv.tv_sec = FAITH_TIMEOUT;
70         tv.tv_usec = 0;
71         error = select(256, &readfds, NULL, NULL, &tv);
72         if (error == -1)
73                 exit_failure("select %d: %s", s_src, strerror(errno));
74         else if (error == 0)
75                 exit_failure("connection timeout");
76
77         n = read(s_src, rshbuf, sizeof(rshbuf));
78         if (rshbuf[0] != 0) {
79                 rsh_dual_relay(s_src, s_dst);
80                 /* NOTREACHED */
81         }
82         write(s_dst, rshbuf, n);
83         tcp_relay(s_src, s_dst, "rsh");
84                 /* NOTREACHED */
85 }
86
87 static void
88 relay(int src, int dst)
89 {
90         int error;
91         ssize_t n;      
92         int atmark;
93
94         error = ioctl(s_rcv, SIOCATMARK, &atmark);
95         if (error != -1 && atmark == 1) {
96                 n = read(s_rcv, rshbuf, 1);
97                 if (n == 1)
98                         send(s_snd, rshbuf, 1, MSG_OOB);
99                 return;
100         }
101
102         n = read(s_rcv, rshbuf, sizeof(rshbuf));
103
104         switch (n) {
105         case -1:
106                 exit_failure("%s", strerror(errno));
107         case 0:
108                 if (s_rcv == src) {
109                         /* half close */
110                         shutdown(dst, SHUT_WR);
111                         half = YES;
112                         break;
113                 }
114                 close(src);
115                 close(dst);
116                 close(s_ctl);
117                 close(s_ctl6);                  
118                 exit_success("terminating rsh/contorol connections");
119                 break;
120         default:
121                 write(s_snd, rshbuf, n);
122         }
123 }
124
125 void
126 rsh_dual_relay(int s_src, int s_dst)
127 {
128         fd_set readfds;
129         int len, s_wld, error;
130         struct sockaddr_storage ctladdr6;
131         struct sockaddr_storage ctladdr;
132         int port6 = 0, lport, lport6;
133         char *p;
134         struct timeval tv;
135         struct sockaddr *sa;
136
137         half = NO;
138         s_rcv = s_src;
139         s_snd = s_dst;
140         syslog(LOG_INFO, "starting rsh connection");
141
142         for (p = rshbuf; *p; p++)
143                 port6 = port6 * 10 + *p - '0';
144
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);
149         else
150                 ((struct sockaddr_in *)&ctladdr6)->sin_port = htons(port6);
151
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);
158
159         len = sizeof(ctladdr);
160         s_ctl = accept(s_wld, (struct sockaddr *)&ctladdr, &len);
161         if (s_ctl == -1) goto bad;
162         close(s_wld);
163         
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;
169         
170         syslog(LOG_INFO, "starting rsh control connection");
171
172         for (;;) {
173                 FD_ZERO(&readfds);
174                 if (half == NO)
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;
180                 tv.tv_usec = 0;
181
182                 error = select(256, &readfds, NULL, NULL, &tv);
183                 if (error == -1)
184                         exit_failure("select 4 sockets: %s", strerror(errno));
185                 else if (error == 0)
186                         exit_failure("connection timeout");
187
188                 if (half == NO && FD_ISSET(s_src, &readfds)) {
189                         s_rcv = s_src;
190                         s_snd = s_dst;
191                         relay(s_src, s_dst);
192                 }
193                 if (FD_ISSET(s_dst, &readfds)) {
194                         s_rcv = s_dst;
195                         s_snd = s_src;
196                         relay(s_src, s_dst);
197                 }
198                 if (FD_ISSET(s_ctl, &readfds)) {
199                         s_rcv = s_ctl;
200                         s_snd = s_ctl6;
201                         relay(s_src, s_dst);
202                 }
203                 if (FD_ISSET(s_ctl6, &readfds)) {
204                         s_rcv = s_ctl6;
205                         s_snd = s_ctl;
206                         relay(s_src, s_dst);
207                 }
208         }
209         /* NOTREACHED */
210
211  bad:
212         exit_failure("%s", strerror(errno));
213 }