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 | static int proxy_connect_none(const char *host, unsigned short port_, struct PHB *phb); |
---|
64 | |
---|
65 | static 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 | |
---|
74 | static 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 | |
---|
115 | static 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 | |
---|
185 | static 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 | |
---|
214 | static 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 | |
---|
258 | static 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 | |
---|
271 | static 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 | |
---|
289 | static 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 | |
---|
297 | if (phb->inpa > 0) { |
---|
298 | b_event_remove(phb->inpa); |
---|
299 | } |
---|
300 | len = sizeof(error); |
---|
301 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
302 | return phb_close(phb); |
---|
303 | } |
---|
304 | sock_make_blocking(source); |
---|
305 | |
---|
306 | /* XXX does socks4 not support host name lookups by the proxy? */ |
---|
307 | if (!(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 | packet[4] = (unsigned char) (hp->h_addr_list[0])[0]; |
---|
316 | packet[5] = (unsigned char) (hp->h_addr_list[0])[1]; |
---|
317 | packet[6] = (unsigned char) (hp->h_addr_list[0])[2]; |
---|
318 | packet[7] = (unsigned char) (hp->h_addr_list[0])[3]; |
---|
319 | packet[8] = 0; |
---|
320 | if (write(source, packet, 9) != 9) { |
---|
321 | return phb_close(phb); |
---|
322 | } |
---|
323 | |
---|
324 | phb->inpa = b_input_add(source, B_EV_IO_READ, s4_canread, phb); |
---|
325 | |
---|
326 | return FALSE; |
---|
327 | } |
---|
328 | |
---|
329 | static int proxy_connect_socks4(const char *host, unsigned short port, struct PHB *phb) |
---|
330 | { |
---|
331 | phb->host = g_strdup(host); |
---|
332 | phb->port = port; |
---|
333 | phb->proxy_func = s4_canwrite; |
---|
334 | phb->proxy_data = phb; |
---|
335 | |
---|
336 | return(proxy_connect_none(proxyhost, proxyport, phb)); |
---|
337 | } |
---|
338 | |
---|
339 | |
---|
340 | /* Connecting to SOCKS5 proxies */ |
---|
341 | |
---|
342 | static gboolean s5_canread_again(gpointer data, gint source, b_input_condition cond) |
---|
343 | { |
---|
344 | unsigned char buf[512]; |
---|
345 | struct PHB *phb = data; |
---|
346 | |
---|
347 | b_event_remove(phb->inpa); |
---|
348 | |
---|
349 | if (read(source, buf, 10) < 10) { |
---|
350 | return phb_close(phb); |
---|
351 | } |
---|
352 | if ((buf[0] != 0x05) || (buf[1] != 0x00)) { |
---|
353 | return phb_close(phb); |
---|
354 | } |
---|
355 | |
---|
356 | phb->func(phb->data, source, B_EV_IO_READ); |
---|
357 | g_free(phb->host); |
---|
358 | g_free(phb); |
---|
359 | |
---|
360 | return FALSE; |
---|
361 | } |
---|
362 | |
---|
363 | static void s5_sendconnect(gpointer data, gint source) |
---|
364 | { |
---|
365 | unsigned char buf[512]; |
---|
366 | struct PHB *phb = data; |
---|
367 | int hlen = strlen(phb->host); |
---|
368 | |
---|
369 | buf[0] = 0x05; |
---|
370 | buf[1] = 0x01; /* CONNECT */ |
---|
371 | buf[2] = 0x00; /* reserved */ |
---|
372 | buf[3] = 0x03; /* address type -- host name */ |
---|
373 | buf[4] = hlen; |
---|
374 | memcpy(buf + 5, phb->host, hlen); |
---|
375 | buf[5 + strlen(phb->host)] = phb->port >> 8; |
---|
376 | buf[5 + strlen(phb->host) + 1] = phb->port & 0xff; |
---|
377 | |
---|
378 | if (write(source, buf, (5 + strlen(phb->host) + 2)) < (5 + strlen(phb->host) + 2)) { |
---|
379 | phb_close(phb); |
---|
380 | return; |
---|
381 | } |
---|
382 | |
---|
383 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread_again, phb); |
---|
384 | } |
---|
385 | |
---|
386 | static gboolean s5_readauth(gpointer data, gint source, b_input_condition cond) |
---|
387 | { |
---|
388 | unsigned char buf[512]; |
---|
389 | struct PHB *phb = data; |
---|
390 | |
---|
391 | b_event_remove(phb->inpa); |
---|
392 | |
---|
393 | if (read(source, buf, 2) < 2) { |
---|
394 | return phb_close(phb); |
---|
395 | } |
---|
396 | |
---|
397 | if ((buf[0] != 0x01) || (buf[1] != 0x00)) { |
---|
398 | return phb_close(phb); |
---|
399 | } |
---|
400 | |
---|
401 | s5_sendconnect(phb, source); |
---|
402 | |
---|
403 | return FALSE; |
---|
404 | } |
---|
405 | |
---|
406 | static gboolean s5_canread(gpointer data, gint source, b_input_condition cond) |
---|
407 | { |
---|
408 | unsigned char buf[512]; |
---|
409 | struct PHB *phb = data; |
---|
410 | |
---|
411 | b_event_remove(phb->inpa); |
---|
412 | |
---|
413 | if (read(source, buf, 2) < 2) { |
---|
414 | return phb_close(phb); |
---|
415 | } |
---|
416 | |
---|
417 | if ((buf[0] != 0x05) || (buf[1] == 0xff)) { |
---|
418 | return phb_close(phb); |
---|
419 | } |
---|
420 | |
---|
421 | if (buf[1] == 0x02) { |
---|
422 | unsigned int i = strlen(proxyuser), j = strlen(proxypass); |
---|
423 | buf[0] = 0x01; /* version 1 */ |
---|
424 | buf[1] = i; |
---|
425 | memcpy(buf + 2, proxyuser, i); |
---|
426 | buf[2 + i] = j; |
---|
427 | memcpy(buf + 2 + i + 1, proxypass, j); |
---|
428 | if (write(source, buf, 3 + i + j) < 3 + i + j) { |
---|
429 | return phb_close(phb); |
---|
430 | } |
---|
431 | |
---|
432 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_readauth, phb); |
---|
433 | } else { |
---|
434 | s5_sendconnect(phb, source); |
---|
435 | } |
---|
436 | |
---|
437 | return FALSE; |
---|
438 | } |
---|
439 | |
---|
440 | static gboolean s5_canwrite(gpointer data, gint source, b_input_condition cond) |
---|
441 | { |
---|
442 | unsigned char buf[512]; |
---|
443 | int i; |
---|
444 | struct PHB *phb = data; |
---|
445 | socklen_t len; |
---|
446 | int error = ETIMEDOUT; |
---|
447 | |
---|
448 | if (phb->inpa > 0) { |
---|
449 | b_event_remove(phb->inpa); |
---|
450 | } |
---|
451 | len = sizeof(error); |
---|
452 | if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { |
---|
453 | return phb_close(phb); |
---|
454 | } |
---|
455 | sock_make_blocking(source); |
---|
456 | |
---|
457 | i = 0; |
---|
458 | buf[0] = 0x05; /* SOCKS version 5 */ |
---|
459 | if (proxyuser[0]) { |
---|
460 | buf[1] = 0x02; /* two methods */ |
---|
461 | buf[2] = 0x00; /* no authentication */ |
---|
462 | buf[3] = 0x02; /* username/password authentication */ |
---|
463 | i = 4; |
---|
464 | } else { |
---|
465 | buf[1] = 0x01; |
---|
466 | buf[2] = 0x00; |
---|
467 | i = 3; |
---|
468 | } |
---|
469 | |
---|
470 | if (write(source, buf, i) < i) { |
---|
471 | return phb_close(phb); |
---|
472 | } |
---|
473 | |
---|
474 | phb->inpa = b_input_add(source, B_EV_IO_READ, s5_canread, phb); |
---|
475 | |
---|
476 | return FALSE; |
---|
477 | } |
---|
478 | |
---|
479 | static int proxy_connect_socks5(const char *host, unsigned short port, struct PHB *phb) |
---|
480 | { |
---|
481 | phb->host = g_strdup(host); |
---|
482 | phb->port = port; |
---|
483 | phb->proxy_func = s5_canwrite; |
---|
484 | phb->proxy_data = phb; |
---|
485 | |
---|
486 | return(proxy_connect_none(proxyhost, proxyport, phb)); |
---|
487 | } |
---|
488 | |
---|
489 | |
---|
490 | /* Export functions */ |
---|
491 | |
---|
492 | int proxy_connect(const char *host, int port, b_event_handler func, gpointer data) |
---|
493 | { |
---|
494 | struct PHB *phb; |
---|
495 | |
---|
496 | if (!host || port <= 0 || !func || strlen(host) > 128) { |
---|
497 | return -1; |
---|
498 | } |
---|
499 | |
---|
500 | phb = g_new0(struct PHB, 1); |
---|
501 | phb->func = func; |
---|
502 | phb->data = data; |
---|
503 | |
---|
504 | if (proxytype == PROXY_NONE || !proxyhost[0] || proxyport <= 0) { |
---|
505 | return proxy_connect_none(host, port, phb); |
---|
506 | } else if (proxytype == PROXY_HTTP) { |
---|
507 | return proxy_connect_http(host, port, phb); |
---|
508 | } else if (proxytype == PROXY_SOCKS4) { |
---|
509 | return proxy_connect_socks4(host, port, phb); |
---|
510 | } else if (proxytype == PROXY_SOCKS5) { |
---|
511 | return proxy_connect_socks5(host, port, phb); |
---|
512 | } |
---|
513 | |
---|
514 | g_free(phb); |
---|
515 | return -1; |
---|
516 | } |
---|