summaryrefslogtreecommitdiffstats
path: root/contrib/pf/ftp-proxy/util.c
blob: 3c8b20eab912ad7fb2a2d0518bcd0d671fa5627d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*	$OpenBSD: util.c,v 1.16 2003/06/28 01:04:57 deraadt Exp $ */

/*
 * Copyright (c) 1996-2001
 *	Obtuse Systems Corporation.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Obtuse Systems nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE OBTUSE SYSTEMS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL OBTUSE
 * SYSTEMS CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <net/if.h>
#include <net/pfvar.h>

#include <arpa/inet.h>

#include <ctype.h>
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sysexits.h>
#include <syslog.h>
#include <unistd.h>

#include "util.h"

int Debug_Level;
int Use_Rdns;

void		debuglog(int debug_level, const char *fmt, ...);

void
debuglog(int debug_level, const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);

	if (Debug_Level >= debug_level)
		vsyslog(LOG_DEBUG, fmt, ap);
	va_end(ap);
}

int
get_proxy_env(int connected_fd, struct sockaddr_in *real_server_sa_ptr,
    struct sockaddr_in *client_sa_ptr)
{
	struct pfioc_natlook natlook;
	int slen, fd;

	slen = sizeof(*real_server_sa_ptr);
	if (getsockname(connected_fd, (struct sockaddr *)real_server_sa_ptr,
	    &slen) != 0) {
		syslog(LOG_ERR, "getsockname() failed (%m)");
		return(-1);
	}
	slen = sizeof(*client_sa_ptr);
	if (getpeername(connected_fd, (struct sockaddr *)client_sa_ptr,
	    &slen) != 0) {
		syslog(LOG_ERR, "getpeername() failed (%m)");
		return(-1);
	}

	/*
	 * Build up the pf natlook structure.
	 * Just for IPv4 right now
	 */
	memset((void *)&natlook, 0, sizeof(natlook));
	natlook.af = AF_INET;
	natlook.saddr.addr32[0] = client_sa_ptr->sin_addr.s_addr;
	natlook.daddr.addr32[0] = real_server_sa_ptr->sin_addr.s_addr;
	natlook.proto = IPPROTO_TCP;
	natlook.sport = client_sa_ptr->sin_port;
	natlook.dport = real_server_sa_ptr->sin_port;
	natlook.direction = PF_OUT;

	/*
	 * Open the pf device and lookup the mapping pair to find
	 * the original address we were supposed to connect to.
	 */
	fd = open("/dev/pf", O_RDWR);
	if (fd == -1) {
		syslog(LOG_ERR, "cannot open /dev/pf (%m)");
		exit(EX_UNAVAILABLE);
	}

	if (ioctl(fd, DIOCNATLOOK, &natlook) == -1) {
		syslog(LOG_INFO,
		    "pf nat lookup failed %s:%hu (%m)",
		    inet_ntoa(client_sa_ptr->sin_addr),
		    ntohs(client_sa_ptr->sin_port));
		close(fd);
		return(-1);
	}
	close(fd);

	/*
	 * Now jam the original address and port back into the into
	 * destination sockaddr_in for the proxy to deal with.
	 */
	memset((void *)real_server_sa_ptr, 0, sizeof(struct sockaddr_in));
	real_server_sa_ptr->sin_port = natlook.rdport;
	real_server_sa_ptr->sin_addr.s_addr = natlook.rdaddr.addr32[0];
	real_server_sa_ptr->sin_len = sizeof(struct sockaddr_in);
	real_server_sa_ptr->sin_family = AF_INET;
	return(0);
}


/*
 * Transfer one unit of data across a pair of sockets
 *
 * A unit of data is as much as we get with a single read(2) call.
 */
int
xfer_data(const char *what_read,int from_fd, int to_fd, struct in_addr from,
    struct in_addr to)
{
	int rlen, offset, xerrno, mark, flags = 0;
	char tbuf[4096];

	/*
	 * Are we at the OOB mark?
	 */
	if (ioctl(from_fd, SIOCATMARK, &mark) < 0) {
		xerrno = errno;
		syslog(LOG_ERR, "cannot ioctl(SIOCATMARK) socket from %s (%m)",
		    what_read);
		errno = xerrno;
		return(-1);
	}
	if (mark)
		flags = MSG_OOB;	/* Yes - at the OOB mark */

snarf:
	rlen = recv(from_fd, tbuf, sizeof(tbuf), flags);
	if (rlen == -1 && flags == MSG_OOB && errno == EINVAL) {
		/* OOB didn't work */
		flags = 0;
		rlen = recv(from_fd, tbuf, sizeof(tbuf), flags);
	}
	if (rlen == 0) {
		debuglog(3, "EOF on read socket");
		return(0);
	} else if (rlen == -1) {
		if (errno == EAGAIN || errno == EINTR)
			goto snarf;
		xerrno = errno;
		syslog(LOG_ERR, "xfer_data (%s): failed (%m) with flags 0%o",
		    what_read, flags);
		errno = xerrno;
		return(-1);
	} else {
		offset = 0;
		debuglog(3, "got %d bytes from socket", rlen);

		while (offset < rlen) {
			int wlen;
		fling:
			wlen = send(to_fd, &tbuf[offset], rlen - offset,
			    flags);
			if (wlen == 0) {
				debuglog(3, "zero-length write");
				goto fling;
			} else if (wlen == -1) {
				if (errno == EAGAIN || errno == EINTR)
					goto fling;
				xerrno = errno;
				syslog(LOG_INFO, "write failed (%m)");
				errno = xerrno;
				return(-1);
			} else {
				debuglog(3, "wrote %d bytes to socket",wlen);
				offset += wlen;
			}
		}
		return(offset);
	}
}

/*
 * get_backchannel_socket gets us a socket bound somewhere in a
 * particular range of ports
 */
int
get_backchannel_socket(int type, int min_port, int max_port, int start_port,
    int direction, struct sockaddr_in *sap)
{
	int count;

	/*
	 * Make sure that direction is 'defined' and that min_port is not
	 * greater than max_port.
	 */
	if (direction != -1)
		direction = 1;

	/* by default we go up by one port until we find one */
	if (min_port > max_port) {
		errno = EINVAL;
		return(-1);
	}

	count = 1 + max_port - min_port;

	/*
	 * Pick a port we can bind to from within the range we want.
	 * If the caller specifies -1 as the starting port number then
	 * we pick one somewhere in the range to try.
	 * This is an optimization intended to speedup port selection and
	 * has NOTHING to do with security.
	 */
	if (start_port == -1)
		start_port = (arc4random() % count) + min_port;

	if (start_port < min_port || start_port > max_port) {
		errno = EINVAL;
		return(-1);
	}

	while (count-- > 0) {
		struct sockaddr_in sa;
		int one, fd;

		fd = socket(AF_INET, type, 0);

		bzero(&sa, sizeof sa);
		sa.sin_family = AF_INET;
		if (sap == NULL)
			sa.sin_addr.s_addr = INADDR_ANY;
		else
			sa.sin_addr.s_addr = sap->sin_addr.s_addr;

		/*
		 * Indicate that we want to reuse a port if it happens that the
		 * port in question was a listen port recently.
		 */
		one = 1;
		if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one,
		    sizeof(one))  == -1)
			return(-1);

		sa.sin_port = htons(start_port);

		if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0) {
			if (sap != NULL)
				*sap = sa;
			return(fd);
		}

		if (errno != EADDRINUSE)
			return(-1);

		/* if it's in use, try the next port */
		close(fd);

		start_port += direction;
		if (start_port < min_port)
			start_port = max_port;
		else if (start_port > max_port)
			start_port = min_port;
	}
	errno = EAGAIN;
	return(-1);
}
OpenPOWER on IntegriCloud