forked from jech/polipo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tunnel.c
572 lines (505 loc) · 16.8 KB
/
tunnel.c
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
Copyright (c) 2004-2006 by Juliusz Chroboczek
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "polipo.h"
#ifdef NO_TUNNEL
void
do_tunnel(int fd, char *buf, int offset, int len, AtomPtr url)
{
int n;
assert(buf);
n = httpWriteErrorHeaders(buf, CHUNK_SIZE, 0, 1,
501, internAtom("CONNECT not available "
"in this version."),
1, NULL, url->string, url->length, NULL);
releaseAtom(url);
if(n >= 0) {
/* This is completely wrong. The write is non-blocking, and we
don't reschedule it if it fails. But then, if the write
blocks, we'll simply drop the connection with no error message. */
write(fd, buf, n);
}
dispose_chunk(buf);
lingeringClose(fd);
return;
}
#else
static void tunnelDispatch(TunnelPtr);
static int tunnelRead1Handler(int, FdEventHandlerPtr, StreamRequestPtr);
static int tunnelRead2Handler(int, FdEventHandlerPtr, StreamRequestPtr);
static int tunnelWrite1Handler(int, FdEventHandlerPtr, StreamRequestPtr);
static int tunnelWrite2Handler(int, FdEventHandlerPtr, StreamRequestPtr);
static int tunnelDnsHandler(int, GethostbynameRequestPtr);
static int tunnelConnectionHandler(int, FdEventHandlerPtr, ConnectRequestPtr);
static int tunnelSocksHandler(int, SocksRequestPtr);
static int tunnelHandlerCommon(int, TunnelPtr);
static int tunnelError(TunnelPtr, int, AtomPtr);
static int
circularBufferFull(CircularBufferPtr buf)
{
if(buf->head == buf->tail - 1)
return 1;
if(buf->head == CHUNK_SIZE - 1 && buf->tail == 0)
return 1;
return 0;
}
static int
circularBufferEmpty(CircularBufferPtr buf)
{
return buf->head == buf->tail;
}
static void
logTunnel(TunnelPtr tunnel, int blocked)
{
do_log(L_TUNNEL,"tunnel %s:%d %s\n", tunnel->hostname->string, tunnel->port,
blocked ? "blocked" : "allowed");
}
static TunnelPtr
makeTunnel(int fd, char *buf, int offset, int len)
{
TunnelPtr tunnel;
assert(offset < CHUNK_SIZE);
tunnel = malloc(sizeof(TunnelRec));
if(tunnel == NULL)
return NULL;
tunnel->hostname = NULL;
tunnel->port = -1;
tunnel->flags = 0;
tunnel->fd1 = fd;
tunnel->fd2 = -1;
tunnel->buf1.buf = buf;
if(offset == len) {
tunnel->buf1.tail = 0;
tunnel->buf1.head = 0;
} else {
tunnel->buf1.tail = offset;
tunnel->buf1.head = len;
}
tunnel->buf2.buf = NULL;
tunnel->buf2.tail = 0;
tunnel->buf2.head = 0;
return tunnel;
}
static void
destroyTunnel(TunnelPtr tunnel)
{
assert(tunnel->fd1 < 0 && tunnel->fd2 < 0);
releaseAtom(tunnel->hostname);
if(tunnel->buf1.buf)
dispose_chunk(tunnel->buf1.buf);
if(tunnel->buf2.buf)
dispose_chunk(tunnel->buf2.buf);
free(tunnel);
}
void
do_tunnel(int fd, char *buf, int offset, int len, AtomPtr url)
{
TunnelPtr tunnel;
int port;
char *p, *q;
tunnel = makeTunnel(fd, buf, offset, len);
if(tunnel == NULL) {
do_log(L_ERROR, "Couldn't allocate tunnel.\n");
releaseAtom(url);
dispose_chunk(buf);
CLOSE(fd);
return;
}
if(proxyOffline) {
do_log(L_INFO, "Attemted CONNECT when disconnected.\n");
releaseAtom(url);
tunnelError(tunnel, 502,
internAtom("Cannot CONNECT when disconnected."));
return;
}
p = memrchr(url->string, ':', url->length);
q = NULL;
if(p)
port = strtol(p + 1, &q, 10);
if(!p || q != url->string + url->length) {
do_log(L_ERROR, "Couldn't parse CONNECT.\n");
releaseAtom(url);
tunnelError(tunnel, 400, internAtom("Couldn't parse CONNECT"));
return;
}
tunnel->hostname = internAtomLowerN(url->string, p - url->string);
if(tunnel->hostname == NULL) {
releaseAtom(url);
tunnelError(tunnel, 501, internAtom("Couldn't allocate hostname"));
return;
}
if(!intListMember(port, tunnelAllowedPorts)) {
releaseAtom(url);
tunnelError(tunnel, 403, internAtom("Forbidden port"));
return;
}
tunnel->port = port;
if (tunnelIsMatched(url->string, url->length,
tunnel->hostname->string, tunnel->hostname->length)) {
releaseAtom(url);
tunnelError(tunnel, 404, internAtom("Forbidden tunnel"));
logTunnel(tunnel,1);
return;
}
logTunnel(tunnel,0);
releaseAtom(url);
if(socksParentProxy)
do_socks_connect(parentHost ?
parentHost->string : tunnel->hostname->string,
parentHost ? parentPort : tunnel->port,
tunnelSocksHandler, tunnel);
else
do_gethostbyname(parentHost ?
parentHost->string : tunnel->hostname->string, 0,
tunnelDnsHandler, tunnel);
}
static int
tunnelDnsHandler(int status, GethostbynameRequestPtr request)
{
TunnelPtr tunnel = request->data;
if(status <= 0) {
tunnelError(tunnel, 504,
internAtomError(-status,
"Host %s lookup failed",
atomString(tunnel->hostname)));
return 1;
}
if(request->addr->string[0] == DNS_CNAME) {
if(request->count > 10)
tunnelError(tunnel, 504, internAtom("CNAME loop"));
do_gethostbyname(request->addr->string + 1, request->count + 1,
tunnelDnsHandler, tunnel);
return 1;
}
do_connect(retainAtom(request->addr), 0,
parentHost ? parentPort : tunnel->port,
tunnelConnectionHandler, tunnel);
return 1;
}
static int
tunnelConnectionHandler(int status,
FdEventHandlerPtr event,
ConnectRequestPtr request)
{
TunnelPtr tunnel = request->data;
int rc;
if(status < 0) {
tunnelError(tunnel, 504, internAtomError(-status, "Couldn't connect"));
return 1;
}
rc = setNodelay(request->fd, 1);
if(rc < 0)
do_log_error(L_WARN, errno, "Couldn't disable Nagle's algorithm");
return tunnelHandlerCommon(request->fd, tunnel);
}
static int
tunnelSocksHandler(int status, SocksRequestPtr request)
{
TunnelPtr tunnel = request->data;
if(status < 0) {
tunnelError(tunnel, 504, internAtomError(-status, "Couldn't connect"));
return 1;
}
return tunnelHandlerCommon(request->fd, tunnel);
}
static int
tunnelHandlerParent(int fd, TunnelPtr tunnel)
{
char *message;
int n;
if(tunnel->buf1.buf == NULL)
tunnel->buf1.buf = get_chunk();
if(tunnel->buf1.buf == NULL) {
message = "Couldn't allocate buffer";
goto fail;
}
if(tunnel->buf1.tail != tunnel->buf1.head) {
message = "Pipelined connect to parent proxy not implemented";
goto fail;
}
n = snnprintf(tunnel->buf1.buf, tunnel->buf1.tail, CHUNK_SIZE,
"CONNECT %s:%d HTTP/1.1",
tunnel->hostname->string, tunnel->port);
if (parentAuthCredentials)
n = buildServerAuthHeaders(tunnel->buf1.buf, n, CHUNK_SIZE,
parentAuthCredentials);
n = snnprintf(tunnel->buf1.buf, n, CHUNK_SIZE, "\r\n\r\n");
if(n < 0) {
message = "Buffer overflow";
goto fail;
}
tunnel->buf1.head = n;
tunnelDispatch(tunnel);
return 1;
fail:
CLOSE(fd);
tunnel->fd2 = -1;
tunnelError(tunnel, 501, internAtom(message));
return 1;
}
static int
tunnelHandlerCommon(int fd, TunnelPtr tunnel)
{
const char *message = "HTTP/1.1 200 Tunnel established\r\n\r\n";
tunnel->fd2 = fd;
if(parentHost)
return tunnelHandlerParent(fd, tunnel);
if(tunnel->buf2.buf == NULL)
tunnel->buf2.buf = get_chunk();
if(tunnel->buf2.buf == NULL) {
CLOSE(fd);
tunnelError(tunnel, 501, internAtom("Couldn't allocate buffer"));
return 1;
}
memcpy(tunnel->buf2.buf, message, MIN(CHUNK_SIZE - 1, strlen(message)));
tunnel->buf2.head = MIN(CHUNK_SIZE - 1, strlen(message));
tunnelDispatch(tunnel);
return 1;
}
static void
bufRead(int fd, CircularBufferPtr buf,
int (*handler)(int, FdEventHandlerPtr, StreamRequestPtr),
void *data)
{
int tail;
if(buf->tail == 0)
tail = CHUNK_SIZE - 1;
else
tail = buf->tail - 1;
if(buf->head == 0)
do_stream_buf(IO_READ | IO_NOTNOW,
fd, 0,
&buf->buf, tail,
handler, data);
else if(buf->tail > buf->head)
do_stream(IO_READ | IO_NOTNOW,
fd, buf->head,
buf->buf, tail,
handler, data);
else
do_stream_2(IO_READ | IO_NOTNOW,
fd, buf->head,
buf->buf, CHUNK_SIZE,
buf->buf, tail,
handler, data);
}
static void
bufWrite(int fd, CircularBufferPtr buf,
int (*handler)(int, FdEventHandlerPtr, StreamRequestPtr),
void *data)
{
if(buf->head > buf->tail)
do_stream(IO_WRITE,
fd, buf->tail,
buf->buf, buf->head,
handler, data);
else
do_stream_2(IO_WRITE,
fd, buf->tail,
buf->buf, CHUNK_SIZE,
buf->buf, buf->head,
handler, data);
}
static void
tunnelDispatch(TunnelPtr tunnel)
{
if(circularBufferEmpty(&tunnel->buf1)) {
if(tunnel->buf1.buf &&
!(tunnel->flags & (TUNNEL_READER1 | TUNNEL_WRITER2))) {
dispose_chunk(tunnel->buf1.buf);
tunnel->buf1.buf = NULL;
tunnel->buf1.head = tunnel->buf1.tail = 0;
}
}
if(circularBufferEmpty(&tunnel->buf2)) {
if(tunnel->buf2.buf &&
!(tunnel->flags & (TUNNEL_READER2 | TUNNEL_WRITER1))) {
dispose_chunk(tunnel->buf2.buf);
tunnel->buf2.buf = NULL;
tunnel->buf2.head = tunnel->buf2.tail = 0;
}
}
if(tunnel->fd1 >= 0) {
if(!(tunnel->flags & (TUNNEL_READER1 | TUNNEL_EOF1)) &&
!circularBufferFull(&tunnel->buf1)) {
tunnel->flags |= TUNNEL_READER1;
bufRead(tunnel->fd1, &tunnel->buf1, tunnelRead1Handler, tunnel);
}
if(!(tunnel->flags & (TUNNEL_WRITER1 | TUNNEL_EPIPE1)) &&
!circularBufferEmpty(&tunnel->buf2)) {
tunnel->flags |= TUNNEL_WRITER1;
/* There's no IO_NOTNOW in bufWrite, so it might close the
file descriptor straight away. Wait until we're
rescheduled. */
bufWrite(tunnel->fd1, &tunnel->buf2, tunnelWrite1Handler, tunnel);
return;
}
if(tunnel->fd2 < 0 || (tunnel->flags & TUNNEL_EOF2)) {
if(!(tunnel->flags & TUNNEL_EPIPE1))
shutdown(tunnel->fd1, 1);
tunnel->flags |= TUNNEL_EPIPE1;
} else if(tunnel->fd1 < 0 || (tunnel->flags & TUNNEL_EPIPE2)) {
if(!(tunnel->flags & TUNNEL_EOF1))
shutdown(tunnel->fd1, 0);
tunnel->flags |= TUNNEL_EOF1;
}
if((tunnel->flags & TUNNEL_EOF1) && (tunnel->flags & TUNNEL_EPIPE1)) {
if(!(tunnel->flags & (TUNNEL_READER1 | TUNNEL_WRITER1))) {
CLOSE(tunnel->fd1);
tunnel->fd1 = -1;
}
}
}
if(tunnel->fd2 >= 0) {
if(!(tunnel->flags & (TUNNEL_READER2 | TUNNEL_EOF2)) &&
!circularBufferFull(&tunnel->buf2)) {
tunnel->flags |= TUNNEL_READER2;
bufRead(tunnel->fd2, &tunnel->buf2, tunnelRead2Handler, tunnel);
}
if(!(tunnel->flags & (TUNNEL_WRITER2 | TUNNEL_EPIPE2)) &&
!circularBufferEmpty(&tunnel->buf1)) {
tunnel->flags |= TUNNEL_WRITER2;
bufWrite(tunnel->fd2, &tunnel->buf1, tunnelWrite2Handler, tunnel);
return;
}
if(tunnel->fd1 < 0 || (tunnel->flags & TUNNEL_EOF1)) {
if(!(tunnel->flags & TUNNEL_EPIPE2))
shutdown(tunnel->fd2, 1);
tunnel->flags |= TUNNEL_EPIPE2;
} else if(tunnel->fd1 < 0 || (tunnel->flags & TUNNEL_EPIPE1)) {
if(!(tunnel->flags & TUNNEL_EOF2))
shutdown(tunnel->fd2, 0);
tunnel->flags |= TUNNEL_EOF2;
}
if((tunnel->flags & TUNNEL_EOF2) && (tunnel->flags & TUNNEL_EPIPE2)) {
if(!(tunnel->flags & (TUNNEL_READER2 | TUNNEL_WRITER2))) {
CLOSE(tunnel->fd2);
tunnel->fd2 = -1;
}
}
}
if(tunnel->fd1 < 0 && tunnel->fd2 < 0)
destroyTunnel(tunnel);
else
assert(tunnel->flags & (TUNNEL_READER1 | TUNNEL_WRITER1 |
TUNNEL_READER2 | TUNNEL_WRITER2));
}
static int
tunnelRead1Handler(int status,
FdEventHandlerPtr event, StreamRequestPtr request)
{
TunnelPtr tunnel = request->data;
if(status) {
if(status < 0)
do_log_error(L_ERROR, -status, "Couldn't read from client");
tunnel->flags |= TUNNEL_EOF1;
goto done;
}
tunnel->buf1.head = request->offset % CHUNK_SIZE;
done:
/* Keep buffer empty to avoid a deadlock */
if((tunnel->flags & TUNNEL_EPIPE2))
tunnel->buf1.tail = tunnel->buf1.head;
tunnel->flags &= ~TUNNEL_READER1;
tunnelDispatch(tunnel);
return 1;
}
static int
tunnelRead2Handler(int status,
FdEventHandlerPtr event, StreamRequestPtr request)
{
TunnelPtr tunnel = request->data;
if(status) {
if(status < 0)
do_log_error(L_ERROR, -status, "Couldn't read from server");
tunnel->flags |= TUNNEL_EOF2;
goto done;
}
tunnel->buf2.head = request->offset % CHUNK_SIZE;
done:
/* Keep buffer empty to avoid a deadlock */
if((tunnel->flags & TUNNEL_EPIPE1))
tunnel->buf2.tail = tunnel->buf2.head;
tunnel->flags &= ~TUNNEL_READER2;
tunnelDispatch(tunnel);
return 1;
}
static int
tunnelWrite1Handler(int status,
FdEventHandlerPtr event, StreamRequestPtr request)
{
TunnelPtr tunnel = request->data;
if(status || (tunnel->flags & TUNNEL_EPIPE1)) {
tunnel->flags |= TUNNEL_EPIPE1;
if(status < 0 && status != -EPIPE)
do_log_error(L_ERROR, -status, "Couldn't write to client");
/* Empty the buffer to avoid a deadlock */
tunnel->buf2.tail = tunnel->buf2.head;
goto done;
}
tunnel->buf2.tail = request->offset % CHUNK_SIZE;
done:
tunnel->flags &= ~TUNNEL_WRITER1;
tunnelDispatch(tunnel);
return 1;
}
static int
tunnelWrite2Handler(int status,
FdEventHandlerPtr event, StreamRequestPtr request)
{
TunnelPtr tunnel = request->data;
if(status || (tunnel->flags & TUNNEL_EPIPE2)) {
tunnel->flags |= TUNNEL_EPIPE2;
if(status < 0 && status != -EPIPE)
do_log_error(L_ERROR, -status, "Couldn't write to server");
/* Empty the buffer to avoid a deadlock */
tunnel->buf1.tail = tunnel->buf1.head;
goto done;
}
tunnel->buf1.tail = request->offset % CHUNK_SIZE;
done:
tunnel->flags &= ~TUNNEL_WRITER2;
tunnelDispatch(tunnel);
return 1;
}
static int
tunnelError(TunnelPtr tunnel, int code, AtomPtr message)
{
int n;
if(tunnel->fd2 > 0) {
CLOSE(tunnel->fd2);
tunnel->fd2 = -1;
}
if(tunnel->buf2.buf == NULL)
tunnel->buf2.buf = get_chunk();
if(tunnel->buf2.buf == NULL)
goto fail;
n = httpWriteErrorHeaders(tunnel->buf2.buf, CHUNK_SIZE - 1, 0,
1, code, message, 1, NULL,
NULL, 0, NULL);
if(n <= 0) goto fail;
tunnel->buf2.head = n;
tunnelDispatch(tunnel);
return 1;
fail:
CLOSE(tunnel->fd1);
tunnel->fd1 = -1;
tunnelDispatch(tunnel);
return 1;
}
#endif