forked from Castaglia/proftpd-mod_proxy_protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_proxy_protocol.c
More file actions
1185 lines (953 loc) · 33.4 KB
/
mod_proxy_protocol.c
File metadata and controls
1185 lines (953 loc) · 33.4 KB
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ProFTPD - mod_proxy_protocol
* Copyright (c) 2013-2017 TJ Saunders
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*/
#include "conf.h"
#include "privs.h"
#ifdef HAVE_SYS_UIO_H
# include <sys/uio.h>
#endif /* HAVE_SYS_UIO_H */
#define MOD_PROXY_PROTOCOL_VERSION "mod_proxy_protocol/0.1"
/* Make sure the version of proftpd is as necessary. */
#if PROFTPD_VERSION_NUMBER < 0x0001030504
# error "ProFTPD 1.3.5rc4 or later required"
#endif
module proxy_protocol_module;
#define PROXY_PROTOCOL_BUFSZ 128
#define PROXY_PROTOCOL_TIMEOUT_DEFAULT 3
static int proxy_protocol_timeout = PROXY_PROTOCOL_TIMEOUT_DEFAULT;
#define PROXY_PROTOCOL_VERSION_HAPROXY_V1 1
#define PROXY_PROTOCOL_VERSION_HAPROXY_V2 2
static unsigned int proxy_protocol_version = PROXY_PROTOCOL_VERSION_HAPROXY_V1;
static const char *trace_channel = "proxy_protocol";
static int poll_sock(int sockfd) {
fd_set rfds;
int res = 0;
struct timeval tv;
memset(&tv, 0, sizeof(tv));
tv.tv_sec = proxy_protocol_timeout;
tv.tv_usec = 0;
pr_trace_msg(trace_channel, 19,
"waiting for max of %lu secs while polling socket %d using select(2)",
(unsigned long) tv.tv_sec, sockfd);
while (TRUE) {
pr_signals_handle();
FD_ZERO(&rfds);
FD_SET(sockfd, &rfds);
res = select(sockfd + 1, &rfds, NULL, NULL, &tv);
if (res < 0) {
int xerrno = errno;
if (xerrno == EINTR) {
pr_signals_handle();
continue;
}
pr_trace_msg(trace_channel, 18, "error calling select(2) on fd %d: %s",
sockfd, strerror(xerrno));
errno = xerrno;
return -1;
} else if (res == 0) {
memset(&tv, 0, sizeof(tv));
tv.tv_sec = proxy_protocol_timeout;
tv.tv_usec = 0;
pr_trace_msg(trace_channel, 18,
"polling on socket %d timed out after %lu sec, trying again", sockfd,
(unsigned long) tv.tv_sec);
continue;
}
break;
}
return 0;
}
static int read_sock(int sockfd, void *buf, size_t reqlen) {
void *ptr = NULL;
size_t remainlen = 0;
if (reqlen == 0) {
return 0;
}
errno = 0;
ptr = buf;
remainlen = reqlen;
while (remainlen > 0) {
int res, xerrno = 0;
if (poll_sock(sockfd) < 0) {
return -1;
}
res = read(sockfd, ptr, remainlen);
xerrno = errno;
while (res <= 0) {
if (res < 0) {
if (xerrno == EINTR) {
pr_signals_handle();
continue;
}
pr_trace_msg(trace_channel, 16,
"error reading from client (fd %d): %s", sockfd, strerror(xerrno));
pr_log_debug(DEBUG5, MOD_PROXY_PROTOCOL_VERSION
": error reading from client (fd %d): %s", sockfd, strerror(xerrno));
/* We explicitly disconnect the client here because the errors below
* all indicate a problem with the TCP connection.
*/
if (xerrno == ECONNRESET ||
xerrno == ECONNABORTED ||
#ifdef ETIMEDOUT
xerrno == ETIMEDOUT ||
#endif /* ETIMEDOUT */
#ifdef ENOTCONN
xerrno == ENOTCONN ||
#endif /* ENOTCONN */
#ifdef ESHUTDOWN
xerrno == ESHUTDOWN ||
#endif /* ESHUTDOWNN */
xerrno == EPIPE) {
errno = xerrno;
pr_trace_msg(trace_channel, 16,
"disconnecting client (%s)", strerror(xerrno));
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": disconnecting client (%s)", strerror(xerrno));
pr_session_disconnect(&proxy_protocol_module,
PR_SESS_DISCONNECT_CLIENT_EOF, strerror(xerrno));
}
return -1;
} else {
/* If we read zero bytes here, treat it as an EOF and hang up on
* the uncommunicative client.
*/
pr_trace_msg(trace_channel, 16, "%s",
"disconnecting client (received EOF)");
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": disconnecting client (received EOF)");
pr_session_disconnect(&proxy_protocol_module,
PR_SESS_DISCONNECT_CLIENT_EOF, NULL);
}
}
/* Generate an event for any interested listeners. */
pr_event_generate("core.ctrl-read", buf);
session.total_raw_in += reqlen;
if (res == remainlen) {
break;
}
pr_trace_msg(trace_channel, 20, "read %lu bytes, expected %lu bytes; "
"reading more", (unsigned long) res, (unsigned long) remainlen);
ptr = ((char *) ptr + res);
remainlen -= res;
}
return reqlen;
}
static int readv_sock(int sockfd, const struct iovec *iov, int count) {
int res, xerrno = 0;
if (poll_sock(sockfd) < 0) {
return -1;
}
res = readv(sockfd, iov, count);
xerrno = errno;
while (res <= 0) {
if (res < 0) {
if (xerrno == EINTR) {
pr_signals_handle();
if (poll_sock(sockfd) < 0) {
return -1;
}
res = readv(sockfd, iov, count);
xerrno = errno;
continue;
}
pr_trace_msg(trace_channel, 16,
"error reading from client (fd %d): %s", sockfd, strerror(xerrno));
pr_log_debug(DEBUG5, MOD_PROXY_PROTOCOL_VERSION
": error reading from client (fd %d): %s", sockfd, strerror(xerrno));
/* We explicitly disconnect the client here because the errors below
* all indicate a problem with the TCP connection.
*/
if (xerrno == ECONNRESET ||
xerrno == ECONNABORTED ||
#ifdef ETIMEDOUT
xerrno == ETIMEDOUT ||
#endif /* ETIMEDOUT */
#ifdef ENOTCONN
xerrno == ENOTCONN ||
#endif /* ENOTCONN */
#ifdef ESHUTDOWN
xerrno == ESHUTDOWN ||
#endif /* ESHUTDOWNN */
xerrno == EPIPE) {
pr_trace_msg(trace_channel, 16,
"disconnecting client (%s)", strerror(xerrno));
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": disconnecting client (%s)", strerror(xerrno));
pr_session_disconnect(&proxy_protocol_module,
PR_SESS_DISCONNECT_CLIENT_EOF, strerror(xerrno));
return -1;
}
/* If we read zero bytes here, treat it as an EOF and hang up on
* the uncommunicative client.
*/
pr_trace_msg(trace_channel, 16, "%s",
"disconnecting client (received EOF)");
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": disconnecting client (received EOF)");
pr_session_disconnect(&proxy_protocol_module,
PR_SESS_DISCONNECT_CLIENT_EOF, NULL);
errno = ENOENT;
return -1;
}
}
session.total_raw_in += res;
return res;
}
static unsigned int strtou(const char **str, const char *last) {
const char *ptr = *str;
unsigned int i = 0, j, k;
while (ptr < last) {
pr_signals_handle();
j = *ptr - '0';
k = i * 10;
if (j > 9) {
break;
}
i = k + j;
ptr++;
}
*str = ptr;
return i;
}
/* This function waits a PROXY protocol header at the beginning of the
* raw data stream. The header looks like this :
*
* "PROXY" <sp> PROTO <sp> SRC3 <sp> DST3 <sp> SRC4 <sp> <DST4> "\r\n"
*
* There must be exactly one space between each field. Fields are :
*
* - PROTO: layer 4 protocol, which must be "TCP4" or "TCP6".
* - SRC3: layer 3 (e.g. IP) source address in standard text form
* - DST3: layer 3 (e.g. IP) destination address in standard text form
* - SRC4: layer 4 (e.g. TCP port) source address in standard text form
* - DST4: layer 4 (e.g. TCP port) destination address in standard text form
*/
static int read_haproxy_v1(pool *p, conn_t *conn,
const pr_netaddr_t **proxied_addr, unsigned int *proxied_port) {
register unsigned int i;
char buf[PROXY_PROTOCOL_BUFSZ], *last = NULL, *ptr = NULL;
int have_cr = FALSE, have_nl = FALSE, have_tcp4 = FALSE, have_tcp6 = FALSE;
size_t buflen = 0;
/* Read until we find the expected PROXY string. */
memset(buf, '\0', sizeof(buf));
ptr = buf;
for (i = 0; i < sizeof(buf)-1; i++) {
int res, xerrno;
pr_signals_handle();
res = read_sock(conn->rfd, &buf[i], 1);
xerrno = errno;
while (res <= 0) {
if (xerrno == EINTR) {
pr_signals_handle();
res = read_sock(conn->rfd, &buf[i], 1);
xerrno = errno;
continue;
}
if (res < 0) {
pr_log_debug(DEBUG5, MOD_PROXY_PROTOCOL_VERSION
": error reading from client socket: %s", strerror(xerrno));
errno = xerrno;
return -1;
}
}
/* Decode a possible PROXY request as early as we can, and fail
* early if it does not match.
*/
if (i == 6) {
if (strncmp(ptr, "PROXY ", 6) != 0) {
goto bad_proto;
}
ptr += 6;
}
/* We continue reading until the client has sent the terminating
* CRLF sequence.
*/
if (buf[i] == '\r') {
have_cr = TRUE;
buf[i] = '\0';
continue;
}
if (have_cr == TRUE &&
buf[i] == '\n') {
have_nl = TRUE;
buf[i] = '\0';
break;
}
buflen++;
}
buf[sizeof(buf)-1] = '\0';
pr_trace_msg(trace_channel, 7,
"read %lu bytes of proxy data (minus CRLF): '%.100s'",
(unsigned long) buflen, buf);
if (have_nl == FALSE) {
pr_log_debug(DEBUG5, MOD_PROXY_PROTOCOL_VERSION
": missing expected CRLF termination");
goto bad_proto;
}
if (buflen == 0) {
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": missing expected PROXY protocol data");
goto bad_proto;
}
last = buf + buflen;
/* Check the PROTO field: "TCP4" or "TCP6" are supported. */
if (strncmp(ptr, "TCP4 ", 5) == 0) {
have_tcp4 = TRUE;
#ifdef PR_USE_IPV6
} else if (strncmp(ptr, "TCP6 ", 5) == 0) {
if (pr_netaddr_use_ipv6()) {
have_tcp6 = TRUE;
}
#endif /* PR_USE_IPV6 */
}
if (have_tcp4 || have_tcp6) {
const pr_netaddr_t *src_addr = NULL, *dst_addr = NULL;
char *ptr2 = NULL;
unsigned int src_port, dst_port;
int flags = PR_NETADDR_GET_ADDR_FL_EXCL_DNS;
ptr += 5;
ptr2 = strchr(ptr, ' ');
if (ptr2 == NULL) {
goto bad_proto;
}
*ptr2 = '\0';
pr_trace_msg(trace_channel, 9,
"resolving source address field '%s'", ptr);
src_addr = pr_netaddr_get_addr2(p, ptr, NULL, flags);
if (src_addr == NULL) {
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": unable to resolve source address '%s': %s", ptr, strerror(errno));
*ptr2 = ' ';
goto bad_proto;
} else {
*ptr2 = ' ';
pr_trace_msg(trace_channel, 9, "resolve source address '%s': %s",
ptr, pr_netaddr_get_ipstr(src_addr));
}
ptr = ptr2 + 1;
ptr2 = strchr(ptr, ' ');
if (ptr2 == NULL) {
goto bad_proto;
}
*ptr2 = '\0';
pr_trace_msg(trace_channel, 9,
"resolving destination address field '%s'", ptr);
dst_addr = pr_netaddr_get_addr2(p, ptr, NULL, flags);
if (dst_addr == NULL) {
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": unable to resolve destination address '%s': %s", ptr,
strerror(errno));
*ptr2 = ' ';
goto bad_proto;
} else {
*ptr2 = ' ';
pr_trace_msg(trace_channel, 9, "resolve destination address '%s': %s",
ptr, pr_netaddr_get_ipstr(dst_addr));
}
/* Check the address family against what the PROTO field says it should
* be. This is to pedantically guard against IPv6 addresses in a
* "TCP4" PROXY line, or IPv4 addresses in a "TCP6" line.
*/
/* TODO: Technically, it's possible that the remote client sent us DNS
* names, rather than IP addresses, and we resolved them. To pedantically
* check for this, scan the given address fields for illegal (e.g.
* alphabetic) characters, keeping in mind that IPv6 addresses can use
* hex.
*/
if (have_tcp4) {
if (pr_netaddr_get_family(src_addr) != AF_INET) {
pr_log_debug(DEBUG8, MOD_PROXY_PROTOCOL_VERSION
": expected IPv4 source address for '%s', got IPv6",
pr_netaddr_get_ipstr(src_addr));
errno = EINVAL;
return -1;
}
if (pr_netaddr_get_family(dst_addr) != AF_INET) {
pr_log_debug(DEBUG8, MOD_PROXY_PROTOCOL_VERSION
": expected IPv4 destination address for '%s', got IPv6",
pr_netaddr_get_ipstr(dst_addr));
errno = EINVAL;
return -1;
}
#ifdef PR_USE_IPV6
} else {
if (pr_netaddr_get_family(src_addr) != AF_INET6) {
pr_log_debug(DEBUG8, MOD_PROXY_PROTOCOL_VERSION
": expected IPv6 source address for '%s', got IPv4",
pr_netaddr_get_ipstr(src_addr));
errno = EINVAL;
return -1;
}
if (pr_netaddr_get_family(dst_addr) != AF_INET6) {
pr_log_debug(DEBUG8, MOD_PROXY_PROTOCOL_VERSION
": expected IPv6 destination address for '%s', got IPv4",
pr_netaddr_get_ipstr(dst_addr));
errno = EINVAL;
return -1;
}
/* Handle IPv4-mapped IPv6 addresses as IPv4 addresses. */
if (pr_netaddr_is_v4mappedv6(src_addr) == TRUE) {
src_addr = pr_netaddr_v6tov4(p, src_addr);
}
if (pr_netaddr_is_v4mappedv6(dst_addr) == TRUE) {
dst_addr = pr_netaddr_v6tov4(p, dst_addr);
}
#endif /* PR_USE_IPV6 */
}
ptr = ptr2 + 1;
ptr2 = strchr(ptr, ' ');
if (ptr2 == NULL) {
goto bad_proto;
}
*ptr2 = '\0';
pr_trace_msg(trace_channel, 9,
"resolving source port field '%s'", ptr);
src_port = strtou((const char **) &ptr, last);
if (src_port == 0) {
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": invalid source port '%s' provided", ptr);
*ptr2 = ' ';
goto bad_proto;
} else {
*ptr2 = ' ';
pr_trace_msg(trace_channel, 9, "resolved source port: %u", src_port);
}
if (src_port > 65535) {
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": out-of-range source port provided: %u", src_port);
goto bad_proto;
}
ptr = ptr2 + 1;
pr_trace_msg(trace_channel, 9,
"resolving destination port field '%s'", ptr);
dst_port = strtou((const char **) &ptr, last);
if (dst_port == 0) {
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": invalid destination port '%s' provided", ptr);
*ptr2 = ' ';
goto bad_proto;
} else {
*ptr2 = ' ';
pr_trace_msg(trace_channel, 9, "resolved destination port: %u", dst_port);
}
if (dst_port > 65535) {
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": out-of-range destination port provided: %u", dst_port);
goto bad_proto;
}
if (ptr > last) {
goto bad_proto;
}
/* Paranoidly check the given source address/port against the
* destination address/port. If the two tuples match, then the remote
* client is lying to us (it's not possible to have a TCP connection
* FROM an address/port tuple which is identical to the destination
* address/port tuple).
*/
if (pr_netaddr_cmp(src_addr, dst_addr) == 0 &&
src_port == dst_port) {
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": source/destination address/port are IDENTICAL: %s#%u",
pr_netaddr_get_ipstr(src_addr), src_port);
goto bad_proto;
}
/* Set the source port for the source address. */
pr_netaddr_set_port((pr_netaddr_t *) src_addr, htons(src_port));
*proxied_addr = src_addr;
*proxied_port = src_port;
} else if (strncmp(ptr, "UNKNOWN", 7) == 0) {
pr_log_debug(DEBUG5, MOD_PROXY_PROTOCOL_VERSION
": client cannot provide proxied address: '%.100s'", buf);
errno = ENOENT;
return 0;
} else {
pr_log_debug(DEBUG5, MOD_PROXY_PROTOCOL_VERSION
": unknown/unsupported PROTO field");
goto bad_proto;
}
return 1;
bad_proto:
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": Bad/unsupported proxy protocol data '%.100s' from %s", buf,
pr_netaddr_get_ipstr(conn->remote_addr));
errno = EINVAL;
return -1;
}
static const char haproxy_v2_sig[12] = "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A";
static int read_haproxy_v2(pool *p, conn_t *conn,
const pr_netaddr_t **proxied_addr, unsigned int *proxied_port) {
int res = 0;
uint8_t v2_sig[12], ver_cmd, trans_fam;
uint16_t v2_len;
struct iovec v2_hdr[4];
const pr_netaddr_t *src_addr = NULL, *dst_addr = NULL;
v2_hdr[0].iov_base = (void *) v2_sig;
v2_hdr[0].iov_len = sizeof(v2_sig);
v2_hdr[1].iov_base = (void *) &ver_cmd;
v2_hdr[1].iov_len = sizeof(ver_cmd);
v2_hdr[2].iov_base = (void *) &trans_fam;
v2_hdr[2].iov_len = sizeof(trans_fam);
v2_hdr[3].iov_base = (void *) &v2_len;
v2_hdr[3].iov_len = sizeof(v2_len);
res = readv_sock(conn->rfd, v2_hdr, 4);
if (res < 0) {
return -1;
}
if (res != 16) {
pr_trace_msg(trace_channel, 20, "read %lu V2 bytes, expected %lu bytes",
(unsigned long) res, (unsigned long) 16);
errno = EPERM;
return -1;
}
/* Validate the obtained data. */
if (memcmp(v2_sig, haproxy_v2_sig, sizeof(haproxy_v2_sig)) != 0) {
pr_trace_msg(trace_channel, 3,
"invalid proxy protocol V2 signature, rejecting");
errno = EINVAL;
return -1;
}
if ((ver_cmd & 0xF0) != 0x20) {
errno = EINVAL;
return -1;
}
switch (ver_cmd & 0xF) {
/* PROXY command */
case 0x01:
switch (trans_fam) {
/* TCP, IPv4 */
case 0x11: {
uint32_t src_ipv4, dst_ipv4;
uint16_t src_port, dst_port;
struct iovec ipv4[4];
struct sockaddr_in *saddr;
pr_trace_msg(trace_channel, 17,
"received proxy protocol V2 TCP/IPv4 transport family (%lu bytes)",
(unsigned long) ntohs(v2_len));
if (ntohs(v2_len) != 12) {
pr_trace_msg(trace_channel, 3,
"proxy protocol V2 TCP/IPv4 transport family sent %lu bytes, "
"expected %lu bytes", (unsigned long) ntohs(v2_len),
(unsigned long) 12);
errno = EINVAL;
return -1;
}
ipv4[0].iov_base = (void *) &src_ipv4;
ipv4[0].iov_len = sizeof(src_ipv4);
ipv4[1].iov_base = (void *) &dst_ipv4;
ipv4[1].iov_len = sizeof(dst_ipv4);
ipv4[2].iov_base = (void *) &src_port;
ipv4[2].iov_len = sizeof(src_port);
ipv4[3].iov_base = (void *) &dst_port;
ipv4[3].iov_len = sizeof(dst_port);
res = readv_sock(conn->rfd, ipv4, 4);
if (res < 0) {
return -1;
}
src_addr = pr_netaddr_alloc(p);
pr_netaddr_set_family((pr_netaddr_t *) src_addr, AF_INET);
saddr = (struct sockaddr_in *) pr_netaddr_get_sockaddr(src_addr);
saddr->sin_family = AF_INET;
saddr->sin_addr.s_addr = src_ipv4;
saddr->sin_port = src_port;
pr_netaddr_set_port((pr_netaddr_t *) src_addr, src_port);
dst_addr = pr_netaddr_alloc(p);
pr_netaddr_set_family((pr_netaddr_t *) dst_addr, AF_INET);
saddr = (struct sockaddr_in *) pr_netaddr_get_sockaddr(dst_addr);
saddr->sin_family = AF_INET;
saddr->sin_addr.s_addr = dst_ipv4;
saddr->sin_port = dst_port;
pr_netaddr_set_port((pr_netaddr_t *) dst_addr, dst_port);
pr_trace_msg(trace_channel, 17,
"received proxy protocol V2 TCP/IPv4 transport family: "
"source address %s#%d, destination address %s#%d",
pr_netaddr_get_ipstr(src_addr),
ntohs(pr_netaddr_get_port(src_addr)),
pr_netaddr_get_ipstr(dst_addr),
ntohs(pr_netaddr_get_port(dst_addr)));
break;
}
/* TCP, IPv6 */
case 0x21: {
uint8_t src_ipv6[16], dst_ipv6[16];
uint16_t src_port, dst_port;
struct iovec ipv6[4];
struct sockaddr_in6 *saddr;
pr_trace_msg(trace_channel, 17,
"received proxy protocol V2 TCP/IPv6 transport family (%lu bytes)",
(unsigned long) ntohs(v2_len));
if (ntohs(v2_len) != 36) {
pr_trace_msg(trace_channel, 3,
"proxy protocol V2 TCP/IPv6 transport family sent %lu bytes, "
"expected %lu bytes", (unsigned long) ntohs(v2_len),
(unsigned long) 36);
errno = EINVAL;
return -1;
}
#ifdef PR_USE_IPV6
ipv6[0].iov_base = (void *) &src_ipv6;
ipv6[0].iov_len = sizeof(src_ipv6);
ipv6[1].iov_base = (void *) &dst_ipv6;
ipv6[1].iov_len = sizeof(dst_ipv6);
ipv6[2].iov_base = (void *) &src_port;
ipv6[2].iov_len = sizeof(src_port);
ipv6[3].iov_base = (void *) &dst_port;
ipv6[3].iov_len = sizeof(dst_port);
res = readv_sock(conn->rfd, ipv6, 4);
if (res < 0) {
return -1;
}
src_addr = pr_netaddr_alloc(p);
pr_netaddr_set_family((pr_netaddr_t *) src_addr, AF_INET6);
saddr = (struct sockaddr_in6 *) pr_netaddr_get_sockaddr(src_addr);
saddr->sin6_family = AF_INET6;
memcpy(&(saddr->sin6_addr), src_ipv6, sizeof(src_ipv6));
saddr->sin6_port = src_port;
pr_netaddr_set_port((pr_netaddr_t *) src_addr, src_port);
dst_addr = pr_netaddr_alloc(p);
pr_netaddr_set_family((pr_netaddr_t *) dst_addr, AF_INET6);
saddr = (struct sockaddr_in6 *) pr_netaddr_get_sockaddr(dst_addr);
saddr->sin6_family = AF_INET6;
memcpy(&(saddr->sin6_addr), dst_ipv6, sizeof(dst_ipv6));
saddr->sin6_port = dst_port;
pr_netaddr_set_port((pr_netaddr_t *) dst_addr, dst_port);
/* Handle IPv4-mapped IPv6 addresses as IPv4 addresses. */
if (pr_netaddr_is_v4mappedv6(src_addr) == TRUE) {
src_addr = pr_netaddr_v6tov4(p, src_addr);
}
if (pr_netaddr_is_v4mappedv6(dst_addr) == TRUE) {
dst_addr = pr_netaddr_v6tov4(p, dst_addr);
}
pr_trace_msg(trace_channel, 17,
"received proxy protocol V2 TCP/IPv6 transport family: "
"source address %s#%d, destination address %s#%d",
pr_netaddr_get_ipstr(src_addr),
ntohs(pr_netaddr_get_port(src_addr)),
pr_netaddr_get_ipstr(dst_addr),
ntohs(pr_netaddr_get_port(dst_addr)));
#else
/* Avoid compiler warnings about unused variables. */
(void) src_ipv6;
(void) dst_ipv6;
(void) src_port;
(void) dst_port;
(void) ipv6;
(void) saddr;
pr_trace_msg(trace_channel, 3,
"IPv6 support disabled, ignoring proxy protocol V2 data");
#endif /* PR_USE_IPV6 */
break;
}
/* Unix */
case 0x31: {
unsigned char src_path[108];
unsigned char dst_path[108];
pr_trace_msg(trace_channel, 17,
"received proxy protocol V2 Unix transport family "
"(%lu bytes), ignoring", (unsigned long) ntohs(v2_len));
if (ntohs(v2_len) != 216) {
pr_trace_msg(trace_channel, 3,
"proxy protocol V2 Unix transport family sent %lu bytes, "
"expected %lu bytes", (unsigned long) ntohs(v2_len),
(unsigned long) 216);
errno = EINVAL;
return -1;
}
res = read_sock(conn->rfd, src_path, sizeof(src_path));
if (res > 0) {
pr_trace_msg(trace_channel, 15,
"received proxy protocol V2 Unix source path: '%s'", src_path);
}
res = read_sock(conn->rfd, dst_path, sizeof(dst_path));
if (res > 0) {
pr_trace_msg(trace_channel, 15,
"received proxy protocol V2 Unix destination path: '%s'",
dst_path);
}
break;
}
/* Unspecified */
case 0x00: {
pool *tmp_pool;
unsigned char *buf;
size_t buflen;
buflen = ntohs(v2_len);
pr_trace_msg(trace_channel, 17,
"received proxy protocol V2 unspecified transport family "
"(%lu bytes), ignoring", (unsigned long) buflen);
tmp_pool = make_sub_pool(p);
buf = palloc(tmp_pool, buflen);
(void) read_sock(conn->rfd, buf, buflen);
destroy_pool(tmp_pool);
break;
}
default:
pr_trace_msg(trace_channel, 3,
"unsupported proxy protocol V2 transport family: %u", trans_fam);
errno = EINVAL;
return -1;
}
break;
/* LOCAL command */
case 0x00:
/* Keep local connection address for LOCAL commands. */
pr_trace_msg(trace_channel, 17,
"received proxy protocol V2 LOCAL command, ignoring");
return 0;
default:
pr_trace_msg(trace_channel, 3,
"unsupported proxy protocol V2 command: %u", ver_cmd);
errno = EINVAL;
return -1;
}
if (src_addr == NULL &&
dst_addr == NULL) {
return 0;
}
if (ntohs(pr_netaddr_get_port(dst_addr)) > 65535) {
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": out-of-range destination port provided: %u",
ntohs(pr_netaddr_get_port(dst_addr)));
errno = EINVAL;
return -1;
}
/* Paranoidly check the given source address/port against the
* destination address/port. If the two tuples match, then the remote
* client is lying to us (it's not possible to have a TCP connection
* FROM an address/port tuple which is identical to the destination
* address/port tuple).
*/
if (pr_netaddr_cmp(src_addr, dst_addr) == 0 &&
pr_netaddr_get_port(src_addr) == pr_netaddr_get_port(dst_addr)) {
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": source/destination address/port are IDENTICAL: %s#%u",
pr_netaddr_get_ipstr(src_addr), ntohs(pr_netaddr_get_port(src_addr)));
errno = EPERM;
return -1;
}
*proxied_addr = src_addr;
*proxied_port = ntohs(pr_netaddr_get_port(src_addr));
return 0;
}
static int read_proxied_addr(pool *p, conn_t *conn,
const pr_netaddr_t **proxied_addr, unsigned int *proxied_port) {
int res;
/* Note that in theory, we could auto-detect the protocol version. */
switch (proxy_protocol_version) {
case PROXY_PROTOCOL_VERSION_HAPROXY_V1:
res = read_haproxy_v1(p, conn, proxied_addr, proxied_port);
break;
case PROXY_PROTOCOL_VERSION_HAPROXY_V2:
res = read_haproxy_v2(p, conn, proxied_addr, proxied_port);
break;
default:
errno = ENOSYS;
res = -1;
}
return res;
}
static int proxy_protocol_timeout_cb(CALLBACK_FRAME) {
pr_event_generate("proxy_protocol.timeout", NULL);
pr_log_debug(DEBUG0, MOD_PROXY_PROTOCOL_VERSION
": proxy protocol timeout (%d %s) reached, disconnecting client",
proxy_protocol_timeout, proxy_protocol_timeout != 1 ? "secs" : "sec");
pr_session_disconnect(&proxy_protocol_module, PR_SESS_DISCONNECT_TIMEOUT,
"ProxyProtocolTimeout");
return 0;
}
/* Configuration handlers
*/
/* usage: ProxyProtocolEngine on|off */
MODRET set_proxyprotocolengine(cmd_rec *cmd) {
int engine = 0;
config_rec *c;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
engine = get_boolean(cmd, 1);
if (engine == -1) {
CONF_ERROR(cmd, "expected Boolean parameter");
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = pcalloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = engine;
return PR_HANDLED(cmd);
}
/* usage: ProxyProtocolTimeout nsecs */
MODRET set_proxyprotocoltimeout(cmd_rec *cmd) {
int timeout = -1;
config_rec *c = NULL;
CHECK_ARGS(cmd, 1);
CHECK_CONF(cmd, CONF_ROOT|CONF_VIRTUAL|CONF_GLOBAL);
if (pr_str_get_duration(cmd->argv[1], &timeout) < 0) {
CONF_ERROR(cmd, pstrcat(cmd->tmp_pool, "error parsing timeout value '",
cmd->argv[1], "': ", strerror(errno), NULL));
}
c = add_config_param(cmd->argv[0], 1, NULL);
c->argv[0] = pcalloc(c->pool, sizeof(int));
*((int *) c->argv[0]) = timeout;
return PR_HANDLED(cmd);
}
/* usage: ProxyProtocolVersion protocol */
MODRET set_proxyprotocolversion(cmd_rec *cmd) {
int proto_version = -1;
config_rec *c = NULL;
CHECK_ARGS(cmd, 1);