1 /* $OpenBSD: util.c,v 1.18 2004/01/22 16:10:30 beck Exp $ */
2 /* $DragonFly: src/libexec/ftp-proxy/util.c,v 1.1 2004/09/21 21:25:28 joerg Exp $ */
5 * Copyright (c) 1996-2001
6 * Obtuse Systems Corporation. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the Obtuse Systems nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE OBTUSE SYSTEMS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OBTUSE
24 * SYSTEMS CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
42 #include <net/pf/pfvar.h>
44 #include <arpa/inet.h>
62 in_addr_t Bind_Addr = INADDR_NONE;
64 void debuglog(int debug_level, const char *fmt, ...);
67 debuglog(int debug_level, const char *fmt, ...)
72 if (Debug_Level >= debug_level)
73 vsyslog(LOG_DEBUG, fmt, ap);
78 get_proxy_env(int connected_fd, struct sockaddr_in *real_server_sa_ptr,
79 struct sockaddr_in *client_sa_ptr)
81 struct pfioc_natlook natlook;
85 slen = sizeof(*real_server_sa_ptr);
86 if (getsockname(connected_fd, (struct sockaddr *)real_server_sa_ptr,
88 syslog(LOG_ERR, "getsockname() failed (%m)");
91 slen = sizeof(*client_sa_ptr);
92 if (getpeername(connected_fd, (struct sockaddr *)client_sa_ptr,
94 syslog(LOG_ERR, "getpeername() failed (%m)");
99 * Build up the pf natlook structure.
100 * Just for IPv4 right now
102 memset((void *)&natlook, 0, sizeof(natlook));
103 natlook.af = AF_INET;
104 natlook.saddr.addr32[0] = client_sa_ptr->sin_addr.s_addr;
105 natlook.daddr.addr32[0] = real_server_sa_ptr->sin_addr.s_addr;
106 natlook.proto = IPPROTO_TCP;
107 natlook.sport = client_sa_ptr->sin_port;
108 natlook.dport = real_server_sa_ptr->sin_port;
109 natlook.direction = PF_OUT;
112 * Open the pf device and lookup the mapping pair to find
113 * the original address we were supposed to connect to.
115 fd = open("/dev/pf", O_RDWR);
117 syslog(LOG_ERR, "cannot open /dev/pf (%m)");
118 exit(EX_UNAVAILABLE);
121 if (ioctl(fd, DIOCNATLOOK, &natlook) == -1) {
123 "pf nat lookup failed %s:%hu (%m)",
124 inet_ntoa(client_sa_ptr->sin_addr),
125 ntohs(client_sa_ptr->sin_port));
132 * Now jam the original address and port back into the into
133 * destination sockaddr_in for the proxy to deal with.
135 memset((void *)real_server_sa_ptr, 0, sizeof(struct sockaddr_in));
136 real_server_sa_ptr->sin_port = natlook.rdport;
137 real_server_sa_ptr->sin_addr.s_addr = natlook.rdaddr.addr32[0];
138 real_server_sa_ptr->sin_len = sizeof(struct sockaddr_in);
139 real_server_sa_ptr->sin_family = AF_INET;
145 * Transfer one unit of data across a pair of sockets
147 * A unit of data is as much as we get with a single read(2) call.
150 xfer_data(const char *what_read,int from_fd, int to_fd)
152 int rlen, offset, xerrno, mark, flags = 0;
156 * Are we at the OOB mark?
158 if (ioctl(from_fd, SIOCATMARK, &mark) < 0) {
160 syslog(LOG_ERR, "cannot ioctl(SIOCATMARK) socket from %s (%m)",
166 flags = MSG_OOB; /* Yes - at the OOB mark */
169 rlen = recv(from_fd, tbuf, sizeof(tbuf), flags);
170 if (rlen == -1 && flags == MSG_OOB && errno == EINVAL) {
171 /* OOB didn't work */
173 rlen = recv(from_fd, tbuf, sizeof(tbuf), flags);
176 debuglog(3, "EOF on read socket");
178 } else if (rlen == -1) {
179 if (errno == EAGAIN || errno == EINTR)
182 syslog(LOG_ERR, "xfer_data (%s): failed (%m) with flags 0%o",
188 debuglog(3, "got %d bytes from socket", rlen);
190 while (offset < rlen) {
193 wlen = send(to_fd, &tbuf[offset], rlen - offset,
196 debuglog(3, "zero-length write");
198 } else if (wlen == -1) {
199 if (errno == EAGAIN || errno == EINTR)
202 syslog(LOG_INFO, "write failed (%m)");
206 debuglog(3, "wrote %d bytes to socket",wlen);
215 * get_backchannel_socket gets us a socket bound somewhere in a
216 * particular range of ports
219 get_backchannel_socket(int type, int min_port, int max_port, int start_port,
220 int direction, struct sockaddr_in *sap)
225 * Make sure that direction is 'defined' and that min_port is not
226 * greater than max_port.
231 /* by default we go up by one port until we find one */
232 if (min_port > max_port) {
237 count = 1 + max_port - min_port;
240 * Pick a port we can bind to from within the range we want.
241 * If the caller specifies -1 as the starting port number then
242 * we pick one somewhere in the range to try.
243 * This is an optimization intended to speedup port selection and
244 * has NOTHING to do with security.
246 if (start_port == -1)
247 start_port = (arc4random() % count) + min_port;
249 if (start_port < min_port || start_port > max_port) {
254 while (count-- > 0) {
255 struct sockaddr_in sa;
258 fd = socket(AF_INET, type, 0);
260 bzero(&sa, sizeof sa);
261 sa.sin_family = AF_INET;
262 if (Bind_Addr == INADDR_NONE)
264 sa.sin_addr.s_addr = INADDR_ANY;
266 sa.sin_addr.s_addr = sap->sin_addr.s_addr;
268 sa.sin_addr.s_addr = Bind_Addr;
271 * Indicate that we want to reuse a port if it happens that the
272 * port in question was a listen port recently.
275 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one,
279 sa.sin_port = htons(start_port);
281 if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0) {
287 if (errno != EADDRINUSE)
290 /* if it's in use, try the next port */
293 start_port += direction;
294 if (start_port < min_port)
295 start_port = max_port;
296 else if (start_port > max_port)
297 start_port = min_port;