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.2 2005/02/24 15:38:09 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/types.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>
60 extern int ReverseMode
;
64 in_addr_t Bind_Addr
= INADDR_NONE
;
66 void debuglog(int debug_level
, const char *fmt
, ...);
69 debuglog(int debug_level
, const char *fmt
, ...)
74 if (Debug_Level
>= debug_level
)
75 vsyslog(LOG_DEBUG
, fmt
, ap
);
80 get_proxy_env(int connected_fd
, struct sockaddr_in
*real_server_sa_ptr
,
81 struct sockaddr_in
*client_sa_ptr
, struct sockaddr_in
*proxy_sa_ptr
)
83 struct pfioc_natlook natlook
;
87 slen
= sizeof(*proxy_sa_ptr
);
88 if (getsockname(connected_fd
, (struct sockaddr
*)proxy_sa_ptr
,
90 syslog(LOG_ERR
, "getsockname() failed (%m)");
93 slen
= sizeof(*client_sa_ptr
);
94 if (getpeername(connected_fd
, (struct sockaddr
*)client_sa_ptr
,
96 syslog(LOG_ERR
, "getpeername() failed (%m)");
104 * Build up the pf natlook structure.
105 * Just for IPv4 right now
107 memset((void *)&natlook
, 0, sizeof(natlook
));
108 natlook
.af
= AF_INET
;
109 natlook
.saddr
.addr32
[0] = client_sa_ptr
->sin_addr
.s_addr
;
110 natlook
.daddr
.addr32
[0] = proxy_sa_ptr
->sin_addr
.s_addr
;
111 natlook
.proto
= IPPROTO_TCP
;
112 natlook
.sport
= client_sa_ptr
->sin_port
;
113 natlook
.dport
= proxy_sa_ptr
->sin_port
;
114 natlook
.direction
= PF_OUT
;
117 * Open the pf device and lookup the mapping pair to find
118 * the original address we were supposed to connect to.
120 fd
= open("/dev/pf", O_RDWR
);
122 syslog(LOG_ERR
, "cannot open /dev/pf (%m)");
123 exit(EX_UNAVAILABLE
);
126 if (ioctl(fd
, DIOCNATLOOK
, &natlook
) == -1) {
128 "pf nat lookup failed %s:%hu (%m)",
129 inet_ntoa(client_sa_ptr
->sin_addr
),
130 ntohs(client_sa_ptr
->sin_port
));
137 * Now jam the original address and port back into the into
138 * destination sockaddr_in for the proxy to deal with.
140 memset((void *)real_server_sa_ptr
, 0, sizeof(struct sockaddr_in
));
141 real_server_sa_ptr
->sin_port
= natlook
.rdport
;
142 real_server_sa_ptr
->sin_addr
.s_addr
= natlook
.rdaddr
.addr32
[0];
143 real_server_sa_ptr
->sin_len
= sizeof(struct sockaddr_in
);
144 real_server_sa_ptr
->sin_family
= AF_INET
;
150 * Transfer one unit of data across a pair of sockets
152 * A unit of data is as much as we get with a single read(2) call.
155 xfer_data(const char *what_read
,int from_fd
, int to_fd
,
156 struct in_addr from __unused
, struct in_addr to __unused
)
158 int rlen
, offset
, xerrno
, mark
, flags
= 0;
162 * Are we at the OOB mark?
164 if (ioctl(from_fd
, SIOCATMARK
, &mark
) < 0) {
166 syslog(LOG_ERR
, "cannot ioctl(SIOCATMARK) socket from %s (%m)",
172 flags
= MSG_OOB
; /* Yes - at the OOB mark */
175 rlen
= recv(from_fd
, tbuf
, sizeof(tbuf
), flags
);
176 if (rlen
== -1 && flags
== MSG_OOB
&& errno
== EINVAL
) {
177 /* OOB didn't work */
179 rlen
= recv(from_fd
, tbuf
, sizeof(tbuf
), flags
);
182 debuglog(3, "EOF on read socket");
184 } else if (rlen
== -1) {
185 if (errno
== EAGAIN
|| errno
== EINTR
)
188 syslog(LOG_ERR
, "xfer_data (%s): failed (%m) with flags 0%o",
194 debuglog(3, "got %d bytes from socket", rlen
);
196 while (offset
< rlen
) {
199 wlen
= send(to_fd
, &tbuf
[offset
], rlen
- offset
,
202 debuglog(3, "zero-length write");
204 } else if (wlen
== -1) {
205 if (errno
== EAGAIN
|| errno
== EINTR
)
208 syslog(LOG_INFO
, "write failed (%m)");
212 debuglog(3, "wrote %d bytes to socket",wlen
);
221 * get_backchannel_socket gets us a socket bound somewhere in a
222 * particular range of ports
225 get_backchannel_socket(int type
, int min_port
, int max_port
, int start_port
,
226 int direction
, struct sockaddr_in
*sap
)
231 * Make sure that direction is 'defined' and that min_port is not
232 * greater than max_port.
237 /* by default we go up by one port until we find one */
238 if (min_port
> max_port
) {
243 count
= 1 + max_port
- min_port
;
246 * Pick a port we can bind to from within the range we want.
247 * If the caller specifies -1 as the starting port number then
248 * we pick one somewhere in the range to try.
249 * This is an optimization intended to speedup port selection and
250 * has NOTHING to do with security.
252 if (start_port
== -1)
253 start_port
= (arc4random() % count
) + min_port
;
255 if (start_port
< min_port
|| start_port
> max_port
) {
260 while (count
-- > 0) {
261 struct sockaddr_in sa
;
264 fd
= socket(AF_INET
, type
, 0);
266 bzero(&sa
, sizeof sa
);
267 sa
.sin_family
= AF_INET
;
268 if (Bind_Addr
== INADDR_NONE
)
270 sa
.sin_addr
.s_addr
= INADDR_ANY
;
272 sa
.sin_addr
.s_addr
= sap
->sin_addr
.s_addr
;
274 sa
.sin_addr
.s_addr
= Bind_Addr
;
277 * Indicate that we want to reuse a port if it happens that the
278 * port in question was a listen port recently.
281 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &one
,
285 sa
.sin_port
= htons(start_port
);
287 if (bind(fd
, (struct sockaddr
*)&sa
, sizeof(sa
)) == 0) {
293 if (errno
!= EADDRINUSE
)
296 /* if it's in use, try the next port */
299 start_port
+= direction
;
300 if (start_port
< min_port
)
301 start_port
= max_port
;
302 else if (start_port
> max_port
)
303 start_port
= min_port
;