2 proxy.c -- Proxy handling functions.
3 Copyright (C) 2015-2016 Guus Sliepen <guus@tinc-vpn.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "connection.h"
30 proxytype_t proxytype;
36 static void update_address_ipv4(connection_t *c, void *address, void *port) {
37 sockaddrfree(&c->address);
38 memset(&c->address, 0, sizeof c->address);
39 c->address.sa.sa_family = AF_INET;
41 memcpy(&c->address.in.sin_addr, address, sizeof(ipv4_t));
43 memcpy(&c->address.in.sin_port, port, sizeof(uint16_t));
44 // OpenSSH -D returns all zero address, set it to 0.0.0.1 to prevent spamming ourselves.
45 if(!memcmp(&c->address.in.sin_addr, "\0\0\0\0", 4))
46 memcpy(&c->address.in.sin_addr, "\0\0\0\01", 4);
49 static void update_address_ipv6(connection_t *c, void *address, void *port) {
50 sockaddrfree(&c->address);
51 memset(&c->address, 0, sizeof c->address);
52 c->address.sa.sa_family = AF_INET6;
54 memcpy(&c->address.in6.sin6_addr, address, sizeof(ipv6_t));
56 memcpy(&c->address.in6.sin6_port, port, sizeof(uint16_t));
57 // OpenSSH -D returns all zero address, set it to 0100:: to prevent spamming ourselves.
58 if(!memcmp(&c->address.in6.sin6_addr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
59 memcpy(&c->address.in6.sin6_addr, "\01\0\0\0\0\0\0\0", 8);
62 bool send_proxyrequest(connection_t *c) {
65 if(c->address.sa.sa_family != AF_INET) {
66 logger(LOG_ERR, "Can only connect to numeric IPv4 addresses through a SOCKS 4 proxy!");
70 if(c->address.sa.sa_family != AF_INET && c->address.sa.sa_family != AF_UNKNOWN) {
71 logger(LOG_ERR, "Can only connect to IPv4 addresses or hostnames through a SOCKS 4a proxy!");
76 len += strlen(proxyuser);
77 if(c->address.sa.sa_family == AF_UNKNOWN)
78 len += 1 + strlen(c->address.unknown.address);
82 if(c->address.sa.sa_family == AF_INET) {
83 memcpy(s4req + 2, &c->address.in.sin_port, 2);
84 memcpy(s4req + 4, &c->address.in.sin_addr, 4);
86 uint16_t port = htons(atoi(c->address.unknown.port));
87 memcpy(s4req + 2, &port, 2);
88 memcpy(s4req + 4, "\0\0\0\1", 4);
89 strcpy(s4req + (9 + (proxyuser ? strlen(proxyuser) : 0)), c->address.unknown.address);
92 strcpy(s4req + 8, proxyuser);
95 s4req[sizeof s4req - 1] = 0;
96 c->allow_request = PROXY;
97 return send_meta(c, s4req, sizeof s4req);
102 if(c->address.sa.sa_family == AF_INET) {
104 } else if(c->address.sa.sa_family == AF_INET6) {
106 } else if(c->address.sa.sa_family == AF_UNKNOWN) {
107 len += 1 + strlen(c->address.unknown.address);
109 logger(LOG_ERR, "Address family %x not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
113 len += 3 + strlen(proxyuser) + strlen(proxypass);
121 s5req[i++] = strlen(proxyuser);
122 strcpy(s5req + i, proxyuser);
123 i += strlen(proxyuser);
124 s5req[i++] = strlen(proxypass);
125 strcpy(s5req + i, proxypass);
126 i += strlen(proxypass);
133 if(c->address.sa.sa_family == AF_INET) {
135 memcpy(s5req + i, &c->address.in.sin_addr, 4);
137 memcpy(s5req + i, &c->address.in.sin_port, 2);
139 } else if(c->address.sa.sa_family == AF_INET6) {
141 memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
143 memcpy(s5req + i, &c->address.in6.sin6_port, 2);
145 } else if(c->address.sa.sa_family == AF_UNKNOWN) {
147 int len = strlen(c->address.unknown.address);
149 memcpy(s5req + i, c->address.unknown.address, len);
151 uint16_t port = htons(atoi(c->address.unknown.port));
152 memcpy(s5req + i, &port, 2);
155 logger(LOG_ERR, "Unknown address family while trying to connect to SOCKS5 proxy");
160 c->allow_request = PROXY;
161 return send_meta(c, s5req, sizeof s5req);
168 sockaddr2str(&c->address, &host, &port);
169 send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
172 c->allow_request = PROXY;
180 logger(LOG_ERR, "Unknown proxy type");
185 int receive_proxy_meta(connection_t *c, int start, int lenin) {
191 if(c->buffer[0] == 0 && c->buffer[1] == 0x5a) {
192 if(c->address.sa.sa_family == AF_UNKNOWN)
193 update_address_ipv4(c, c->buffer + 4, c->buffer + 2);
195 ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Proxy request granted");
196 c->allow_request = ID;
197 c->status.proxy_passed = true;
201 logger(LOG_ERR, "Proxy request rejected");
208 if(c->buffer[0] != 0x05 || c->buffer[1] == (char)0xff) {
209 logger(LOG_ERR, "Proxy authentication method rejected");
213 if(c->buffer[1] == 0x02) {
216 if(c->buffer[2] != 0x05 || c->buffer[3] != 0x00) {
217 logger(LOG_ERR, "Proxy username/password rejected");
222 if(c->buflen - offset < 7)
224 if(c->buffer[offset] != 0x05 || c->buffer[offset + 1] != 0x00) {
225 logger(LOG_ERR, "Proxy request rejected");
228 int replen = offset + 6;
229 switch(c->buffer[offset + 3]) {
231 if(c->address.sa.sa_family == AF_UNKNOWN)
232 update_address_ipv4(c, c->buffer + offset + 4, c->buffer + offset + 8);
235 case 0x03: // Hostname
236 if(c->address.sa.sa_family == AF_UNKNOWN)
237 update_address_ipv4(c, "\0\0\0\1", "\0\0");
238 replen += ((uint8_t *)c->buffer)[offset + 4];
241 if(c->address.sa.sa_family == AF_UNKNOWN)
242 update_address_ipv6(c, c->buffer + offset + 4, c->buffer + offset + 20);
246 logger(LOG_ERR, "Proxy reply malformed");
249 if(c->buflen < replen) {
252 ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Proxy request granted");
253 c->allow_request = ID;
254 c->status.proxy_passed = true;
260 char *p = memchr(c->buffer, '\n', c->buflen);
261 if(!p || p - c->buffer >= c->buflen)
264 while((p = memchr(p + 1, '\n', c->buflen - (p + 1 - c->buffer)))) {
265 if(p > c->buffer + 3 && !memcmp(p - 3, "\r\n\r\n", 4))
275 if(!strncasecmp(c->buffer, "HTTP/1.1 ", 9)) {
276 if(!strncmp(c->buffer + 9, "200", 3)) {
277 if(c->address.sa.sa_family == AF_UNKNOWN)
278 update_address_ipv4(c, "\0\0\0\1", "\0\0");
279 logger(LOG_DEBUG, "Proxy request granted");
280 replen = p + 1 - c->buffer;
281 c->allow_request = ID;
282 c->status.proxy_passed = true;
286 p = memchr(c->buffer, '\n', c->buflen);
288 logger(LOG_ERR, "Proxy request rejected: %s", c->buffer + 9);
292 logger(LOG_ERR, "Proxy reply malformed");