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