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
|
/* This file is part of vmod_remoteip.
* Copyright (C) 2020 Sergey Poznyakoff
*
* Vmod_remoteip 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 3, or (at your option)
* any later version.
*
* Vmod_remoteip 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 vmod_remoteip. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <ctype.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <cache/cache.h>
#include <vcl.h>
#include <vcc_if.h>
#include <vsa.h>
static int
is_trusted_ip(VRT_CTX, VCL_ACL acl, char const *ipstr)
{
struct addrinfo hints;
struct addrinfo *res;
struct suckaddr *ip;
int rc;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_NUMERICHOST|AI_NUMERICSERV;
rc = getaddrinfo(ipstr, NULL, &hints, &res);
if (rc)
return 0;
ip = VSA_Malloc(res->ai_addr, res->ai_addrlen);
freeaddrinfo(res);
rc = VRT_acl_match(ctx, acl, ip);
free(ip);
return rc;
}
VCL_STRING
vmod_get(VRT_CTX, VCL_ACL acl, VCL_STRING hdr)
{
unsigned u;
char const *end;
char *buf;
int i = 0;
u = WS_ReserveAll(ctx->ws);
buf = ctx->ws->f;
end = hdr + strlen(hdr);
while (end > hdr) {
char const *p;
size_t len, j;
for (p = end - 1; p > hdr && *p != ','; p--)
;
len = end - p;
AN(len + 1 < u);
i = 0;
j = 0;
if (*p == ',')
j++;
while (j < len && isspace(p[j]))
j++;
while (j < len) {
if (isspace(p[j]))
break;
buf[i++] = p[j++];
}
buf[i++] = 0;
if (!is_trusted_ip(ctx, acl, buf)) {
break;
}
end = p;
}
WS_Release(ctx->ws, i);
return buf;
}
|