source: lib/proxy.c @ 12f041d

Last change on this file since 12f041d was 12f041d, checked in by dequis <dx@…>, at 2015-10-21T13:14:17Z

socks4a proxy support (like socks4 with remote DNS)

Fixes trac ticket 995 https://bugs.bitlbee.org/bitlbee/ticket/995

This is slightly pointless for the suggested use case (tor), since with
socks5 we already send a hostname instead of an IP address.

Either way, it was easy to implement, so I hope it helps.

  • Property mode set to 100644
File size: 12.2 KB
Line 
1/*
2 * gaim
3 *
4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
5 * Copyright (C) 2002-2004, Wilmer van der Gaast, Jelmer Vernooij
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 *
21 */
22
23#define BITLBEE_CORE
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/types.h>
28#include <sys/socket.h>
29#include <netdb.h>
30#include <netinet/in.h>
31#include <arpa/inet.h>
32#include <unistd.h>
33#include <fcntl.h>
34#include <errno.h>
35#include "nogaim.h"
36#include "proxy.h"
37#include "base64.h"
38
39char proxyhost[128] = "";
40int proxyport = 0;
41int proxytype = PROXY_NONE;
42char proxyuser[128] = "";
43char proxypass[128] = "";
44
45/* Some systems don't know this one. It's not essential, so set it to 0 then. */
46#ifndef AI_NUMERICSERV
47#define AI_NUMERICSERV 0
48#endif
49#ifndef AI_ADDRCONFIG
50#define AI_ADDRCONFIG 0
51#endif
52
53struct PHB {
54        b_event_handler func, proxy_func;
55        gpointer data, proxy_data;
56        char *host;
57        int port;
58        int fd;
59        gint inpa;
60        struct addrinfo *gai, *gai_cur;
61};
62
63static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb);
64
65static gboolean phb_close(struct PHB *phb)
66{
67        close(phb->fd);
68        phb->func(phb->data, -1, B_EV_IO_READ);
69        g_free(phb->host);
70        g_free(phb);
71        return FALSE;
72}
73
74static gboolean proxy_connected(gpointer data, gint source, b_input_condition cond)
75{
76        struct PHB *phb = data;
77        socklen_t len;
78        int error = ETIMEDOUT;
79
80        len = sizeof(error);
81
82        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) {
83                if ((phb->gai_cur = phb->gai_cur->ai_next)) {
84                        int new_fd;
85                        b_event_remove(phb->inpa);
86                        if ((new_fd = proxy_connect_none(NULL, 0, phb))) {
87                                b_event_remove(phb->inpa);
88                                closesocket(source);
89                                dup2(new_fd, source);
90                                closesocket(new_fd);
91                                phb->inpa = b_input_add(source, B_EV_IO_WRITE, proxy_connected, phb);
92                                return FALSE;
93                        }
94                }
95                closesocket(source);
96                source = -1;
97                /* socket is dead, but continue to clean up */
98        } else {
99                sock_make_blocking(source);
100        }
101
102        freeaddrinfo(phb->gai);
103        b_event_remove(phb->inpa);
104        phb->inpa = 0;
105        if (phb->proxy_func) {
106                phb->proxy_func(phb->proxy_data, source, B_EV_IO_READ);
107        } else {
108                phb->func(phb->data, source, B_EV_IO_READ);
109                g_free(phb);
110        }
111
112        return FALSE;
113}
114
115static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb)
116{
117        struct sockaddr_in me;
118        int fd = -1;
119
120        if (phb->gai_cur == NULL) {
121                int ret;
122                char port[6];
123                struct addrinfo hints;
124
125                g_snprintf(port, sizeof(port), "%d", port_);
126
127                memset(&hints, 0, sizeof(struct addrinfo));
128                hints.ai_family = AF_UNSPEC;
129                hints.ai_socktype = SOCK_STREAM;
130                hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
131
132                if (!(ret = getaddrinfo(host, port, &hints, &phb->gai))) {
133                        phb->gai_cur = phb->gai;
134                } else {
135                        event_debug("gai(): %s\n", gai_strerror(ret));
136                }
137        }
138
139        for (; phb->gai_cur; phb->gai_cur = phb->gai_cur->ai_next) {
140                if ((fd = socket(phb->gai_cur->ai_family, phb->gai_cur->ai_socktype, phb->gai_cur->ai_protocol)) < 0) {
141                        event_debug("socket failed: %d\n", errno);
142                        continue;
143                }
144
145                sock_make_nonblocking(fd);
146
147                if (global.conf->iface_out) {
148                        me.sin_family = AF_INET;
149                        me.sin_port = 0;
150                        me.sin_addr.s_addr = inet_addr(global.conf->iface_out);
151
152                        if (bind(fd, (struct sockaddr *) &me, sizeof(me)) != 0) {
153                                event_debug("bind( %d, \"%s\" ) failure\n", fd, global.conf->iface_out);
154                        }
155                }
156
157                event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port_, fd);
158
159                if (connect(fd, phb->gai_cur->ai_addr, phb->gai_cur->ai_addrlen) < 0 && !sockerr_again()) {
160                        event_debug("connect failed: %s\n", strerror(errno));
161                        closesocket(fd);
162                        fd = -1;
163                        continue;
164                } else {
165                        phb->inpa = b_input_add(fd, B_EV_IO_WRITE, proxy_connected, phb);
166                        phb->fd = fd;
167
168                        break;
169                }
170        }
171
172        if (fd < 0 && host) {
173                g_free(phb);
174        }
175
176        return fd;
177}
178
179
180/* Connecting to HTTP proxies */
181
182#define HTTP_GOODSTRING "HTTP/1.0 200"
183#define HTTP_GOODSTRING2 "HTTP/1.1 200"
184
185static gboolean http_canread(gpointer data, gint source, b_input_condition cond)
186{
187        int nlc = 0;
188        int pos = 0;
189        struct PHB *phb = data;
190        char inputline[8192];
191
192        b_event_remove(phb->inpa);
193
194        while ((pos < sizeof(inputline) - 1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) {
195                if (inputline[pos - 1] == '\n') {
196                        nlc++;
197                } else if (inputline[pos - 1] != '\r') {
198                        nlc = 0;
199                }
200        }
201        inputline[pos] = '\0';
202
203        if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) ||
204            (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) {
205                phb->func(phb->data, source, B_EV_IO_READ);
206                g_free(phb->host);
207                g_free(phb);
208                return FALSE;
209        }
210
211        return phb_close(phb);
212}
213
214static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond)
215{
216        char cmd[384];
217        struct PHB *phb = data;
218        socklen_t len;
219        int error = ETIMEDOUT;
220
221        if (phb->inpa > 0) {
222                b_event_remove(phb->inpa);
223        }
224        len = sizeof(error);
225        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
226                return phb_close(phb);
227        }
228        sock_make_blocking(source);
229
230        g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port,
231                   phb->host, phb->port);
232        if (send(source, cmd, strlen(cmd), 0) < 0) {
233                return phb_close(phb);
234        }
235
236        if (strlen(proxyuser) > 0) {
237                char *t1, *t2;
238                t1 = g_strdup_printf("%s:%s", proxyuser, proxypass);
239                t2 = tobase64(t1);
240                g_free(t1);
241                g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2);
242                g_free(t2);
243                if (send(source, cmd, strlen(cmd), 0) < 0) {
244                        return phb_close(phb);
245                }
246        }
247
248        g_snprintf(cmd, sizeof(cmd), "\r\n");
249        if (send(source, cmd, strlen(cmd), 0) < 0) {
250                return phb_close(phb);
251        }
252
253        phb->inpa = b_input_add(source, B_EV_IO_READ, http_canread, phb);
254
255        return FALSE;
256}
257
258static int proxy_connect_http(const char *host, unsigned short port, struct PHB *phb)
259{
260        phb->host = g_strdup(host);
261        phb->port = port;
262        phb->proxy_func = http_canwrite;
263        phb->proxy_data = phb;
264
265        return(proxy_connect_none(proxyhost, proxyport, phb));
266}
267
268
269/* Connecting to SOCKS4 proxies */
270
271static gboolean s4_canread(gpointer data, gint source, b_input_condition cond)
272{
273        unsigned char packet[12];
274        struct PHB *phb = data;
275
276        b_event_remove(phb->inpa);
277
278        memset(packet, 0, sizeof(packet));
279        if (read(source, packet, 9) >= 4 && packet[1] == 90) {
280                phb->func(phb->data, source, B_EV_IO_READ);
281                g_free(phb->host);
282                g_free(phb);
283                return FALSE;
284        }
285
286        return phb_close(phb);
287}
288
289static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond)
290{
291        unsigned char packet[12];
292        struct hostent *hp;
293        struct PHB *phb = data;
294        socklen_t len;
295        int error = ETIMEDOUT;
296        gboolean is_socks4a = (proxytype == PROXY_SOCKS4A);
297
298        if (phb->inpa > 0) {
299                b_event_remove(phb->inpa);
300        }
301        len = sizeof(error);
302        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
303                return phb_close(phb);
304        }
305        sock_make_blocking(source);
306
307        if (!is_socks4a && !(hp = gethostbyname(phb->host))) {
308                return phb_close(phb);
309        }
310
311        packet[0] = 4;
312        packet[1] = 1;
313        packet[2] = phb->port >> 8;
314        packet[3] = phb->port & 0xff;
315        if (is_socks4a) {
316                packet[4] = 0;
317                packet[5] = 0;
318                packet[6] = 0;
319                packet[7] = 1;
320        } else {
321                packet[4] = (unsigned char) (hp->h_addr_list[0])[0];
322                packet[5] = (unsigned char) (hp->h_addr_list[0])[1];
323                packet[6] = (unsigned char) (hp->h_addr_list[0])[2];
324                packet[7] = (unsigned char) (hp->h_addr_list[0])[3];
325        }
326        packet[8] = 0;
327        if (write(source, packet, 9) != 9) {
328                return phb_close(phb);
329        }
330
331        if (is_socks4a) {
332                size_t host_len = strlen(phb->host) + 1; /* include the \0 */
333
334                if (write(source, phb->host, host_len) != host_len) {
335                        return phb_close(phb);
336                }
337        }
338
339        phb->inpa = b_input_add(source, B_EV_IO_READ, s4_canread, phb);
340
341        return FALSE;
342}
343
344static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb)
345{
346        phb->host = g_strdup(host);
347        phb->port = port;
348        phb->proxy_func = s4_canwrite;
349        phb->proxy_data = phb;
350
351        return(proxy_connect_none(proxyhost, proxyport, phb));
352}
353
354
355/* Connecting to SOCKS5 proxies */
356
357static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond)
358{
359        unsigned char buf[512];
360        struct PHB *phb = data;
361
362        b_event_remove(phb->inpa);
363
364        if (read(source, buf, 10) < 10) {
365                return phb_close(phb);
366        }
367        if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
368                return phb_close(phb);
369        }
370
371        phb->func(phb->data, source, B_EV_IO_READ);
372        g_free(phb->host);
373        g_free(phb);
374
375        return FALSE;
376}
377
378static void s5_sendconnect(gpointer data, gint source)
379{
380        unsigned char buf[512];
381        struct PHB *phb = data;
382        int hlen = strlen(phb->host);
383
384        buf[0] = 0x05;
385        buf[1] = 0x01;          /* CONNECT */
386        buf[2] = 0x00;          /* reserved */
387        buf[3] = 0x03;          /* address type -- host name */
388        buf[4] = hlen;
389        memcpy(buf + 5, phb->host, hlen);
390        buf[5 + strlen(phb->host)] = phb->port >> 8;
391        buf[5 + strlen(phb->host) + 1] = phb->port & 0xff;
392
393        if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) {
394                phb_close(phb);
395                return;
396        }
397
398        phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread_again, phb);
399}
400
401static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond)
402{
403        unsigned char buf[512];
404        struct PHB *phb = data;
405
406        b_event_remove(phb->inpa);
407
408        if (read(source, buf, 2) < 2) {
409                return phb_close(phb);
410        }
411
412        if ((buf[0] != 0x01) || (buf[1] != 0x00)) {
413                return phb_close(phb);
414        }
415
416        s5_sendconnect(phb, source);
417
418        return FALSE;
419}
420
421static gboolean s5_canread(gpointer data, gint source, b_input_condition cond)
422{
423        unsigned char buf[512];
424        struct PHB *phb = data;
425
426        b_event_remove(phb->inpa);
427
428        if (read(source, buf, 2) < 2) {
429                return phb_close(phb);
430        }
431
432        if ((buf[0] != 0x05) || (buf[1] == 0xff)) {
433                return phb_close(phb);
434        }
435
436        if (buf[1] == 0x02) {
437                unsigned int i = strlen(proxyuser), j = strlen(proxypass);
438                buf[0] = 0x01;  /* version 1 */
439                buf[1] = i;
440                memcpy(buf + 2, proxyuser, i);
441                buf[2 + i] = j;
442                memcpy(buf + 2 + i + 1, proxypass, j);
443                if (write(source, buf, 3 + i + j) < 3 + i + j) {
444                        return phb_close(phb);
445                }
446
447                phb->inpa = b_input_add(source, B_EV_IO_READ, s5_readauth, phb);
448        } else {
449                s5_sendconnect(phb, source);
450        }
451
452        return FALSE;
453}
454
455static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond)
456{
457        unsigned char buf[512];
458        int i;
459        struct PHB *phb = data;
460        socklen_t len;
461        int error = ETIMEDOUT;
462
463        if (phb->inpa > 0) {
464                b_event_remove(phb->inpa);
465        }
466        len = sizeof(error);
467        if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
468                return phb_close(phb);
469        }
470        sock_make_blocking(source);
471
472        i = 0;
473        buf[0] = 0x05;          /* SOCKS version 5 */
474        if (proxyuser[0]) {
475                buf[1] = 0x02;  /* two methods */
476                buf[2] = 0x00;  /* no authentication */
477                buf[3] = 0x02;  /* username/password authentication */
478                i = 4;
479        } else {
480                buf[1] = 0x01;
481                buf[2] = 0x00;
482                i = 3;
483        }
484
485        if (write(source, buf, i) < i) {
486                return phb_close(phb);
487        }
488
489        phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread, phb);
490
491        return FALSE;
492}
493
494static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb)
495{
496        phb->host = g_strdup(host);
497        phb->port = port;
498        phb->proxy_func = s5_canwrite;
499        phb->proxy_data = phb;
500
501        return(proxy_connect_none(proxyhost, proxyport, phb));
502}
503
504
505/* Export functions */
506
507int proxy_connect(const char *host, int port, b_event_handler func, gpointer data)
508{
509        struct PHB *phb;
510
511        if (!host || port <= 0 || !func || strlen(host) > 128) {
512                return -1;
513        }
514
515        phb = g_new0(struct PHB, 1);
516        phb->func = func;
517        phb->data = data;
518
519        if (proxytype == PROXY_NONE || !proxyhost[0] || proxyport <= 0) {
520                return proxy_connect_none(host, port, phb);
521        } else if (proxytype == PROXY_HTTP) {
522                return proxy_connect_http(host, port, phb);
523        } else if (proxytype == PROXY_SOCKS4 || proxytype == PROXY_SOCKS4A) {
524                return proxy_connect_socks4(host, port, phb);
525        } else if (proxytype == PROXY_SOCKS5) {
526                return proxy_connect_socks5(host, port, phb);
527        }
528
529        g_free(phb);
530        return -1;
531}
Note: See TracBrowser for help on using the repository browser.