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 | |
---|
39 | char proxyhost[128] = ""; |
---|
40 | int proxyport = 0; |
---|
41 | int proxytype = PROXY_NONE; |
---|
42 | char proxyuser[128] = ""; |
---|
43 | char 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 | |
---|
53 | struct 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 | |
---|
63 | typedef int (*proxy_connect_func)(const char *host, unsigned short port_, struct PHB *phb); |
---|
64 | |
---|
65 | static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb); |
---|
66 | |
---|
67 | static gboolean phb_close(struct PHB *phb) |
---|
68 | { |
---|
69 | close(phb->fd); |
---|
70 | phb->func(phb->data, -1, B_EV_IO_READ); |
---|
71 | g_free(phb->host); |
---|
72 | g_free(phb); |
---|
73 | return FALSE; |
---|
74 | } |
---|
75 | |
---|
76 | static gboolean proxy_connected(gpointer data, gint source, b_input_condition cond) |
---|
77 | { |
---|
78 | struct PHB *phb = data; |
---|
79 | socklen_t len; |
---|
80 | int error = ETIMEDOUT; |
---|
81 | |
---|
82 | len = sizeof(error); |
---|
83 | |
---|
84 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0 || error) { |
---|
85 | if ((phb->gai_cur = phb->gai_cur->ai_next)) { |
---|
86 | int new_fd; |
---|
87 | b_event_remove(phb->inpa); |
---|
88 | if ((new_fd = proxy_connect_none(NULL, 0, phb))) { |
---|
89 | b_event_remove(phb->inpa); |
---|
90 | closesocket(source); |
---|
91 | dup2(new_fd, source); |
---|
92 | closesocket(new_fd); |
---|
93 | phb->inpa = b_input_add(source, B_EV_IO_WRITE, proxy_connected, phb); |
---|
94 | return FALSE; |
---|
95 | } |
---|
96 | } |
---|
97 | closesocket(source); |
---|
98 | source = -1; |
---|
99 | /* socket is dead, but continue to clean up */ |
---|
100 | } else { |
---|
101 | sock_make_blocking(source); |
---|
102 | } |
---|
103 | |
---|
104 | freeaddrinfo(phb->gai); |
---|
105 | b_event_remove(phb->inpa); |
---|
106 | phb->inpa = 0; |
---|
107 | if (phb->proxy_func) { |
---|
108 | phb->proxy_func(phb->proxy_data, source, B_EV_IO_READ); |
---|
109 | } else { |
---|
110 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
111 | g_free(phb); |
---|
112 | } |
---|
113 | |
---|
114 | return FALSE; |
---|
115 | } |
---|
116 | |
---|
117 | static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb) |
---|
118 | { |
---|
119 | struct sockaddr_in me; |
---|
120 | int fd = -1; |
---|
121 | |
---|
122 | if (phb->gai_cur == NULL) { |
---|
123 | int ret; |
---|
124 | char port[6]; |
---|
125 | struct addrinfo hints; |
---|
126 | |
---|
127 | g_snprintf(port, sizeof(port), "%d", port_); |
---|
128 | |
---|
129 | memset(&hints, 0, sizeof(struct addrinfo)); |
---|
130 | hints.ai_family = AF_UNSPEC; |
---|
131 | hints.ai_socktype = SOCK_STREAM; |
---|
132 | hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; |
---|
133 | |
---|
134 | if (!(ret = getaddrinfo(host, port, &hints, &phb->gai))) { |
---|
135 | phb->gai_cur = phb->gai; |
---|
136 | } else { |
---|
137 | event_debug("gai(): %s\n", gai_strerror(ret)); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | for (; phb->gai_cur; phb->gai_cur = phb->gai_cur->ai_next) { |
---|
142 | if ((fd = socket(phb->gai_cur->ai_family, phb->gai_cur->ai_socktype, phb->gai_cur->ai_protocol)) < 0) { |
---|
143 | event_debug("socket failed: %d\n", errno); |
---|
144 | continue; |
---|
145 | } |
---|
146 | |
---|
147 | sock_make_nonblocking(fd); |
---|
148 | |
---|
149 | if (global.conf->iface_out) { |
---|
150 | me.sin_family = AF_INET; |
---|
151 | me.sin_port = 0; |
---|
152 | me.sin_addr.s_addr = inet_addr(global.conf->iface_out); |
---|
153 | |
---|
154 | if (bind(fd, (struct sockaddr *) &me, sizeof(me)) != 0) { |
---|
155 | event_debug("bind( %d, \"%s\" ) failure\n", fd, global.conf->iface_out); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | event_debug("proxy_connect_none( \"%s\", %d ) = %d\n", host, port_, fd); |
---|
160 | |
---|
161 | if (connect(fd, phb->gai_cur->ai_addr, phb->gai_cur->ai_addrlen) < 0 && !sockerr_again()) { |
---|
162 | event_debug("connect failed: %s\n", strerror(errno)); |
---|
163 | closesocket(fd); |
---|
164 | fd = -1; |
---|
165 | continue; |
---|
166 | } else { |
---|
167 | phb->inpa = b_input_add(fd, B_EV_IO_WRITE, proxy_connected, phb); |
---|
168 | phb->fd = fd; |
---|
169 | |
---|
170 | break; |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | if (fd < 0 && host) { |
---|
175 | g_free(phb); |
---|
176 | } |
---|
177 | |
---|
178 | return fd; |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | /* Connecting to HTTP proxies */ |
---|
183 | |
---|
184 | #define HTTP_GOODSTRING "HTTP/1.0 200" |
---|
185 | #define HTTP_GOODSTRING2 "HTTP/1.1 200" |
---|
186 | |
---|
187 | static gboolean http_canread(gpointer data, gint source, b_input_condition cond) |
---|
188 | { |
---|
189 | int nlc = 0; |
---|
190 | int pos = 0; |
---|
191 | struct PHB *phb = data; |
---|
192 | char inputline[8192]; |
---|
193 | |
---|
194 | b_event_remove(phb->inpa); |
---|
195 | |
---|
196 | while ((pos < sizeof(inputline) - 1) && (nlc != 2) && (read(source, &inputline[pos++], 1) == 1)) { |
---|
197 | if (inputline[pos - 1] == '\n') { |
---|
198 | nlc++; |
---|
199 | } else if (inputline[pos - 1] != '\r') { |
---|
200 | nlc = 0; |
---|
201 | } |
---|
202 | } |
---|
203 | inputline[pos] = '\0'; |
---|
204 | |
---|
205 | if ((memcmp(HTTP_GOODSTRING, inputline, strlen(HTTP_GOODSTRING)) == 0) || |
---|
206 | (memcmp(HTTP_GOODSTRING2, inputline, strlen(HTTP_GOODSTRING2)) == 0)) { |
---|
207 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
208 | g_free(phb->host); |
---|
209 | g_free(phb); |
---|
210 | return FALSE; |
---|
211 | } |
---|
212 | |
---|
213 | return phb_close(phb); |
---|
214 | } |
---|
215 | |
---|
216 | static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
217 | { |
---|
218 | char cmd[384]; |
---|
219 | struct PHB *phb = data; |
---|
220 | socklen_t len; |
---|
221 | int error = ETIMEDOUT; |
---|
222 | |
---|
223 | if (phb->inpa > 0) { |
---|
224 | b_event_remove(phb->inpa); |
---|
225 | } |
---|
226 | len = sizeof(error); |
---|
227 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
228 | return phb_close(phb); |
---|
229 | } |
---|
230 | sock_make_blocking(source); |
---|
231 | |
---|
232 | g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port, |
---|
233 | phb->host, phb->port); |
---|
234 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
235 | return phb_close(phb); |
---|
236 | } |
---|
237 | |
---|
238 | if (strlen(proxyuser) > 0) { |
---|
239 | char *t1, *t2; |
---|
240 | t1 = g_strdup_printf("%s:%s", proxyuser, proxypass); |
---|
241 | t2 = tobase64(t1); |
---|
242 | g_free(t1); |
---|
243 | g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2); |
---|
244 | g_free(t2); |
---|
245 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
246 | return phb_close(phb); |
---|
247 | } |
---|
248 | } |
---|
249 | |
---|
250 | g_snprintf(cmd, sizeof(cmd), "\r\n"); |
---|
251 | if (send(source, cmd, strlen(cmd), 0) < 0) { |
---|
252 | return phb_close(phb); |
---|
253 | } |
---|
254 | |
---|
255 | phb->inpa = b_input_add(source, B_EV_IO_READ, http_canread, phb); |
---|
256 | |
---|
257 | return FALSE; |
---|
258 | } |
---|
259 | |
---|
260 | static int proxy_connect_http(const char *host, unsigned short port, struct PHB *phb) |
---|
261 | { |
---|
262 | phb->host = g_strdup(host); |
---|
263 | phb->port = port; |
---|
264 | phb->proxy_func = http_canwrite; |
---|
265 | phb->proxy_data = phb; |
---|
266 | |
---|
267 | return(proxy_connect_none(proxyhost, proxyport, phb)); |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | /* Connecting to SOCKS4 proxies */ |
---|
272 | |
---|
273 | static gboolean s4_canread(gpointer data, gint source, b_input_condition cond) |
---|
274 | { |
---|
275 | unsigned char packet[12]; |
---|
276 | struct PHB *phb = data; |
---|
277 | |
---|
278 | b_event_remove(phb->inpa); |
---|
279 | |
---|
280 | memset(packet, 0, sizeof(packet)); |
---|
281 | if (read(source, packet, 9) >= 4 && packet[1] == 90) { |
---|
282 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
283 | g_free(phb->host); |
---|
284 | g_free(phb); |
---|
285 | return FALSE; |
---|
286 | } |
---|
287 | |
---|
288 | return phb_close(phb); |
---|
289 | } |
---|
290 | |
---|
291 | static gboolean s4_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
292 | { |
---|
293 | unsigned char packet[12]; |
---|
294 | struct hostent *hp; |
---|
295 | struct PHB *phb = data; |
---|
296 | socklen_t len; |
---|
297 | int error = ETIMEDOUT; |
---|
298 | gboolean is_socks4a = (proxytype == PROXY_SOCKS4A); |
---|
299 | |
---|
300 | if (phb->inpa > 0) { |
---|
301 | b_event_remove(phb->inpa); |
---|
302 | } |
---|
303 | len = sizeof(error); |
---|
304 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
305 | return phb_close(phb); |
---|
306 | } |
---|
307 | sock_make_blocking(source); |
---|
308 | |
---|
309 | if (!is_socks4a && !(hp = gethostbyname(phb->host))) { |
---|
310 | return phb_close(phb); |
---|
311 | } |
---|
312 | |
---|
313 | packet[0] = 4; |
---|
314 | packet[1] = 1; |
---|
315 | packet[2] = phb->port >> 8; |
---|
316 | packet[3] = phb->port & 0xff; |
---|
317 | if (is_socks4a) { |
---|
318 | packet[4] = 0; |
---|
319 | packet[5] = 0; |
---|
320 | packet[6] = 0; |
---|
321 | packet[7] = 1; |
---|
322 | } else { |
---|
323 | packet[4] = (unsigned char) (hp->h_addr_list[0])[0]; |
---|
324 | packet[5] = (unsigned char) (hp->h_addr_list[0])[1]; |
---|
325 | packet[6] = (unsigned char) (hp->h_addr_list[0])[2]; |
---|
326 | packet[7] = (unsigned char) (hp->h_addr_list[0])[3]; |
---|
327 | } |
---|
328 | packet[8] = 0; |
---|
329 | if (write(source, packet, 9) != 9) { |
---|
330 | return phb_close(phb); |
---|
331 | } |
---|
332 | |
---|
333 | if (is_socks4a) { |
---|
334 | size_t host_len = strlen(phb->host) + 1; /* include the \0 */ |
---|
335 | |
---|
336 | if (write(source, phb->host, host_len) != host_len) { |
---|
337 | return phb_close(phb); |
---|
338 | } |
---|
339 | } |
---|
340 | |
---|
341 | phb->inpa = b_input_add(source, B_EV_IO_READ, s4_canread, phb); |
---|
342 | |
---|
343 | return FALSE; |
---|
344 | } |
---|
345 | |
---|
346 | static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb) |
---|
347 | { |
---|
348 | phb->host = g_strdup(host); |
---|
349 | phb->port = port; |
---|
350 | phb->proxy_func = s4_canwrite; |
---|
351 | phb->proxy_data = phb; |
---|
352 | |
---|
353 | return(proxy_connect_none(proxyhost, proxyport, phb)); |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | /* Connecting to SOCKS5 proxies */ |
---|
358 | |
---|
359 | static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond) |
---|
360 | { |
---|
361 | unsigned char buf[512]; |
---|
362 | struct PHB *phb = data; |
---|
363 | |
---|
364 | b_event_remove(phb->inpa); |
---|
365 | |
---|
366 | if (read(source, buf, 10) < 10) { |
---|
367 | return phb_close(phb); |
---|
368 | } |
---|
369 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
370 | return phb_close(phb); |
---|
371 | } |
---|
372 | |
---|
373 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
374 | g_free(phb->host); |
---|
375 | g_free(phb); |
---|
376 | |
---|
377 | return FALSE; |
---|
378 | } |
---|
379 | |
---|
380 | static void s5_sendconnect(gpointer data, gint source) |
---|
381 | { |
---|
382 | unsigned char buf[512]; |
---|
383 | struct PHB *phb = data; |
---|
384 | int hlen = strlen(phb->host); |
---|
385 | |
---|
386 | buf[0] = 0x05; |
---|
387 | buf[1] = 0x01; /* CONNECT */ |
---|
388 | buf[2] = 0x00; /* reserved */ |
---|
389 | buf[3] = 0x03; /* address type -- host name */ |
---|
390 | buf[4] = hlen; |
---|
391 | memcpy(buf + 5, phb->host, hlen); |
---|
392 | buf[5 + strlen(phb->host)] = phb->port >> 8; |
---|
393 | buf[5 + strlen(phb->host) + 1] = phb->port & 0xff; |
---|
394 | |
---|
395 | if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) { |
---|
396 | phb_close(phb); |
---|
397 | return; |
---|
398 | } |
---|
399 | |
---|
400 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread_again, phb); |
---|
401 | } |
---|
402 | |
---|
403 | static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond) |
---|
404 | { |
---|
405 | unsigned char buf[512]; |
---|
406 | struct PHB *phb = data; |
---|
407 | |
---|
408 | b_event_remove(phb->inpa); |
---|
409 | |
---|
410 | if (read(source, buf, 2) < 2) { |
---|
411 | return phb_close(phb); |
---|
412 | } |
---|
413 | |
---|
414 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
415 | return phb_close(phb); |
---|
416 | } |
---|
417 | |
---|
418 | s5_sendconnect(phb, source); |
---|
419 | |
---|
420 | return FALSE; |
---|
421 | } |
---|
422 | |
---|
423 | static gboolean s5_canread(gpointer data, gint source, b_input_condition cond) |
---|
424 | { |
---|
425 | unsigned char buf[512]; |
---|
426 | struct PHB *phb = data; |
---|
427 | |
---|
428 | b_event_remove(phb->inpa); |
---|
429 | |
---|
430 | if (read(source, buf, 2) < 2) { |
---|
431 | return phb_close(phb); |
---|
432 | } |
---|
433 | |
---|
434 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
435 | return phb_close(phb); |
---|
436 | } |
---|
437 | |
---|
438 | if (buf[1] == 0x02) { |
---|
439 | unsigned int i = strlen(proxyuser), j = strlen(proxypass); |
---|
440 | buf[0] = 0x01; /* version 1 */ |
---|
441 | buf[1] = i; |
---|
442 | memcpy(buf + 2, proxyuser, i); |
---|
443 | buf[2 + i] = j; |
---|
444 | memcpy(buf + 2 + i + 1, proxypass, j); |
---|
445 | if (write(source, buf, 3 + i + j) < 3 + i + j) { |
---|
446 | return phb_close(phb); |
---|
447 | } |
---|
448 | |
---|
449 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_readauth, phb); |
---|
450 | } else { |
---|
451 | s5_sendconnect(phb, source); |
---|
452 | } |
---|
453 | |
---|
454 | return FALSE; |
---|
455 | } |
---|
456 | |
---|
457 | static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
458 | { |
---|
459 | unsigned char buf[512]; |
---|
460 | int i; |
---|
461 | struct PHB *phb = data; |
---|
462 | socklen_t len; |
---|
463 | int error = ETIMEDOUT; |
---|
464 | |
---|
465 | if (phb->inpa > 0) { |
---|
466 | b_event_remove(phb->inpa); |
---|
467 | } |
---|
468 | len = sizeof(error); |
---|
469 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
470 | return phb_close(phb); |
---|
471 | } |
---|
472 | sock_make_blocking(source); |
---|
473 | |
---|
474 | i = 0; |
---|
475 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
476 | if (proxyuser[0]) { |
---|
477 | buf[1] = 0x02; /* two methods */ |
---|
478 | buf[2] = 0x00; /* no authentication */ |
---|
479 | buf[3] = 0x02; /* username/password authentication */ |
---|
480 | i = 4; |
---|
481 | } else { |
---|
482 | buf[1] = 0x01; |
---|
483 | buf[2] = 0x00; |
---|
484 | i = 3; |
---|
485 | } |
---|
486 | |
---|
487 | if (write(source, buf, i) < i) { |
---|
488 | return phb_close(phb); |
---|
489 | } |
---|
490 | |
---|
491 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread, phb); |
---|
492 | |
---|
493 | return FALSE; |
---|
494 | } |
---|
495 | |
---|
496 | static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb) |
---|
497 | { |
---|
498 | phb->host = g_strdup(host); |
---|
499 | phb->port = port; |
---|
500 | phb->proxy_func = s5_canwrite; |
---|
501 | phb->proxy_data = phb; |
---|
502 | |
---|
503 | return(proxy_connect_none(proxyhost, proxyport, phb)); |
---|
504 | } |
---|
505 | |
---|
506 | static const proxy_connect_func proxy_connect_funcs_array[] = { |
---|
507 | proxy_connect_none, /* PROXY_NONE */ |
---|
508 | proxy_connect_http, /* PROXY_HTTP */ |
---|
509 | proxy_connect_socks4, /* PROXY_SOCKS4 */ |
---|
510 | proxy_connect_socks5, /* PROXY_SOCKS5 */ |
---|
511 | proxy_connect_socks4, /* PROXY_SOCKS4A */ |
---|
512 | }; |
---|
513 | |
---|
514 | /* Export functions */ |
---|
515 | |
---|
516 | int proxy_connect(const char *host, int port, b_event_handler func, gpointer data) |
---|
517 | { |
---|
518 | struct PHB *phb; |
---|
519 | proxy_connect_func fun; |
---|
520 | |
---|
521 | if (!host || port <= 0 || !func || strlen(host) > 128) { |
---|
522 | return -1; |
---|
523 | } |
---|
524 | |
---|
525 | phb = g_new0(struct PHB, 1); |
---|
526 | phb->func = func; |
---|
527 | phb->data = data; |
---|
528 | |
---|
529 | if (proxyhost[0] && proxyport > 0 && proxytype >= 0 && proxytype <= G_N_ELEMENTS(proxy_connect_funcs_array)) { |
---|
530 | fun = proxy_connect_funcs_array[proxytype]; |
---|
531 | } else { |
---|
532 | fun = proxy_connect_none; |
---|
533 | } |
---|
534 | |
---|
535 | return fun(host, port, phb); |
---|
536 | } |
---|