14192
|
1 /**
|
|
2 * @file proxy.c Proxy API
|
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
|
7 * Gaim is the legal property of its developers, whose names are too numerous
|
|
8 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
9 * source distribution.
|
|
10 *
|
|
11 * This program is free software; you can redistribute it and/or modify
|
|
12 * it under the terms of the GNU General Public License as published by
|
|
13 * the Free Software Foundation; either version 2 of the License, or
|
|
14 * (at your option) any later version.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
24 *
|
|
25 */
|
|
26
|
|
27 /* this is a little piece of code to handle proxy connection */
|
|
28 /* it is intended to : 1st handle http proxy, using the CONNECT command
|
|
29 , 2nd provide an easy way to add socks support
|
|
30 , 3rd draw women to it like flies to honey */
|
|
31
|
|
32 #include "internal.h"
|
|
33 #include "cipher.h"
|
|
34 #include "debug.h"
|
|
35 #include "dnsquery.h"
|
|
36 #include "notify.h"
|
|
37 #include "ntlm.h"
|
|
38 #include "prefs.h"
|
|
39 #include "proxy.h"
|
|
40 #include "util.h"
|
|
41
|
|
42 struct _GaimProxyConnectInfo {
|
|
43 GaimProxyConnectFunction connect_cb;
|
|
44 gpointer data;
|
|
45 char *host;
|
|
46 int port;
|
|
47 int fd;
|
|
48 guint inpa;
|
|
49 GaimProxyInfo *gpi;
|
|
50 GaimDnsQueryData *query_data;
|
|
51
|
|
52 /**
|
|
53 * This contains alternating length/char* values. The char*
|
|
54 * values need to be freed when removed from the linked list.
|
|
55 */
|
|
56 GSList *hosts;
|
|
57
|
|
58 /*
|
|
59 * All of the following variables are used when establishing a
|
|
60 * connection through a proxy.
|
|
61 */
|
|
62 guchar *write_buffer;
|
|
63 gsize write_buf_len;
|
|
64 gsize written_len;
|
|
65 GaimInputFunction read_cb;
|
|
66 guchar *read_buffer;
|
|
67 gsize read_buf_len;
|
|
68 gsize read_len;
|
|
69 };
|
|
70
|
|
71 static const char *socks5errors[] = {
|
|
72 "succeeded\n",
|
|
73 "general SOCKS server failure\n",
|
|
74 "connection not allowed by ruleset\n",
|
|
75 "Network unreachable\n",
|
|
76 "Host unreachable\n",
|
|
77 "Connection refused\n",
|
|
78 "TTL expired\n",
|
|
79 "Command not supported\n",
|
|
80 "Address type not supported\n"
|
|
81 };
|
|
82
|
|
83 static GaimProxyInfo *global_proxy_info = NULL;
|
|
84
|
|
85 /*
|
|
86 * TODO: Once all callers of gaim_proxy_connect() are keeping track
|
|
87 * of the return value from that function this linked list
|
|
88 * will no longer be needed.
|
|
89 */
|
|
90 static GSList *connect_infos = NULL;
|
|
91
|
|
92 static void try_connect(GaimProxyConnectInfo *connect_info);
|
|
93
|
|
94 /**************************************************************************
|
|
95 * Proxy structure API
|
|
96 **************************************************************************/
|
|
97 GaimProxyInfo *
|
|
98 gaim_proxy_info_new(void)
|
|
99 {
|
|
100 return g_new0(GaimProxyInfo, 1);
|
|
101 }
|
|
102
|
|
103 void
|
|
104 gaim_proxy_info_destroy(GaimProxyInfo *info)
|
|
105 {
|
|
106 g_return_if_fail(info != NULL);
|
|
107
|
|
108 g_free(info->host);
|
|
109 g_free(info->username);
|
|
110 g_free(info->password);
|
|
111
|
|
112 g_free(info);
|
|
113 }
|
|
114
|
|
115 void
|
|
116 gaim_proxy_info_set_type(GaimProxyInfo *info, GaimProxyType type)
|
|
117 {
|
|
118 g_return_if_fail(info != NULL);
|
|
119
|
|
120 info->type = type;
|
|
121 }
|
|
122
|
|
123 void
|
|
124 gaim_proxy_info_set_host(GaimProxyInfo *info, const char *host)
|
|
125 {
|
|
126 g_return_if_fail(info != NULL);
|
|
127
|
|
128 g_free(info->host);
|
|
129 info->host = g_strdup(host);
|
|
130 }
|
|
131
|
|
132 void
|
|
133 gaim_proxy_info_set_port(GaimProxyInfo *info, int port)
|
|
134 {
|
|
135 g_return_if_fail(info != NULL);
|
|
136
|
|
137 info->port = port;
|
|
138 }
|
|
139
|
|
140 void
|
|
141 gaim_proxy_info_set_username(GaimProxyInfo *info, const char *username)
|
|
142 {
|
|
143 g_return_if_fail(info != NULL);
|
|
144
|
|
145 g_free(info->username);
|
|
146 info->username = g_strdup(username);
|
|
147 }
|
|
148
|
|
149 void
|
|
150 gaim_proxy_info_set_password(GaimProxyInfo *info, const char *password)
|
|
151 {
|
|
152 g_return_if_fail(info != NULL);
|
|
153
|
|
154 g_free(info->password);
|
|
155 info->password = g_strdup(password);
|
|
156 }
|
|
157
|
|
158 GaimProxyType
|
|
159 gaim_proxy_info_get_type(const GaimProxyInfo *info)
|
|
160 {
|
|
161 g_return_val_if_fail(info != NULL, GAIM_PROXY_NONE);
|
|
162
|
|
163 return info->type;
|
|
164 }
|
|
165
|
|
166 const char *
|
|
167 gaim_proxy_info_get_host(const GaimProxyInfo *info)
|
|
168 {
|
|
169 g_return_val_if_fail(info != NULL, NULL);
|
|
170
|
|
171 return info->host;
|
|
172 }
|
|
173
|
|
174 int
|
|
175 gaim_proxy_info_get_port(const GaimProxyInfo *info)
|
|
176 {
|
|
177 g_return_val_if_fail(info != NULL, 0);
|
|
178
|
|
179 return info->port;
|
|
180 }
|
|
181
|
|
182 const char *
|
|
183 gaim_proxy_info_get_username(const GaimProxyInfo *info)
|
|
184 {
|
|
185 g_return_val_if_fail(info != NULL, NULL);
|
|
186
|
|
187 return info->username;
|
|
188 }
|
|
189
|
|
190 const char *
|
|
191 gaim_proxy_info_get_password(const GaimProxyInfo *info)
|
|
192 {
|
|
193 g_return_val_if_fail(info != NULL, NULL);
|
|
194
|
|
195 return info->password;
|
|
196 }
|
|
197
|
|
198 /**************************************************************************
|
|
199 * Global Proxy API
|
|
200 **************************************************************************/
|
|
201 GaimProxyInfo *
|
|
202 gaim_global_proxy_get_info(void)
|
|
203 {
|
|
204 return global_proxy_info;
|
|
205 }
|
|
206
|
|
207 static GaimProxyInfo *
|
|
208 gaim_gnome_proxy_get_info(void)
|
|
209 {
|
|
210 static GaimProxyInfo info = {0, NULL, 0, NULL, NULL};
|
|
211 gchar *path;
|
|
212 if ((path = g_find_program_in_path("gconftool-2"))) {
|
|
213 gchar *tmp;
|
|
214
|
|
215 g_free(path);
|
|
216
|
|
217 /* See whether to use a proxy. */
|
|
218 if (!g_spawn_command_line_sync("gconftool-2 -g /system/http_proxy/use_http_proxy", &tmp,
|
|
219 NULL, NULL, NULL))
|
|
220 return gaim_global_proxy_get_info();
|
|
221 if (!strcmp(tmp, "false\n")) {
|
|
222 info.type = GAIM_PROXY_NONE;
|
|
223 g_free(tmp);
|
|
224 return &info;
|
|
225 } else if (strcmp(tmp, "true\n")) {
|
|
226 g_free(tmp);
|
|
227 return gaim_global_proxy_get_info();
|
|
228 }
|
|
229
|
|
230 g_free(tmp);
|
|
231 info.type = GAIM_PROXY_HTTP;
|
|
232
|
|
233 /* Free the old fields */
|
|
234 if (info.host) {
|
|
235 g_free(info.host);
|
|
236 info.host = NULL;
|
|
237 }
|
|
238 if (info.username) {
|
|
239 g_free(info.username);
|
|
240 info.username = NULL;
|
|
241 }
|
|
242 if (info.password) {
|
|
243 g_free(info.password);
|
|
244 info.password = NULL;
|
|
245 }
|
|
246
|
|
247 /* Get the new ones */
|
|
248 if (!g_spawn_command_line_sync("gconftool-2 -g /system/http_proxy/host", &info.host,
|
|
249 NULL, NULL, NULL))
|
|
250 return gaim_global_proxy_get_info();
|
|
251 g_strchomp(info.host);
|
|
252
|
|
253 if (!g_spawn_command_line_sync("gconftool-2 -g /system/http_proxy/authentication_user", &info.username,
|
|
254 NULL, NULL, NULL))
|
|
255 return gaim_global_proxy_get_info();
|
|
256 g_strchomp(info.username);
|
|
257
|
|
258 if (!g_spawn_command_line_sync("gconftool-2 -g /system/http_proxy/authentication_password", &info.password,
|
|
259 NULL, NULL, NULL))
|
|
260 return gaim_global_proxy_get_info();
|
|
261 g_strchomp(info.password);
|
|
262
|
|
263 if (!g_spawn_command_line_sync("gconftool-2 -g /system/http_proxy/port", &tmp,
|
|
264 NULL, NULL, NULL))
|
|
265 return gaim_global_proxy_get_info();
|
|
266 info.port = atoi(tmp);
|
|
267 g_free(tmp);
|
|
268
|
|
269 return &info;
|
|
270 }
|
|
271 return gaim_global_proxy_get_info();
|
|
272 }
|
|
273 /**************************************************************************
|
|
274 * Proxy API
|
|
275 **************************************************************************/
|
|
276
|
|
277 /*
|
|
278 * This is used when the connection attempt to one particular IP
|
|
279 * address fails. We close the socket, remove the watcher and get
|
|
280 * rid of input and output buffers. Normally try_connect() will
|
|
281 * be called immediately after this.
|
|
282 */
|
|
283 static void
|
|
284 gaim_proxy_connect_info_disconnect(GaimProxyConnectInfo *connect_info)
|
|
285 {
|
|
286 if (connect_info->inpa > 0)
|
|
287 {
|
|
288 gaim_input_remove(connect_info->inpa);
|
|
289 connect_info->inpa = 0;
|
|
290 }
|
|
291
|
|
292 if (connect_info->fd >= 0)
|
|
293 {
|
|
294 close(connect_info->fd);
|
|
295 connect_info->fd = -1;
|
|
296 }
|
|
297
|
|
298 g_free(connect_info->write_buffer);
|
|
299 connect_info->write_buffer = NULL;
|
|
300
|
|
301 g_free(connect_info->read_buffer);
|
|
302 connect_info->read_buffer = NULL;
|
|
303 }
|
|
304
|
|
305 static void
|
|
306 gaim_proxy_connect_info_destroy(GaimProxyConnectInfo *connect_info)
|
|
307 {
|
|
308 gaim_proxy_connect_info_disconnect(connect_info);
|
|
309
|
|
310 connect_infos = g_slist_remove(connect_infos, connect_info);
|
|
311
|
14238
|
312 if (connect_info->query_data != NULL)
|
|
313 gaim_dnsquery_destroy(connect_info->query_data);
|
|
314
|
14192
|
315 while (connect_info->hosts != NULL)
|
|
316 {
|
|
317 /* Discard the length... */
|
|
318 connect_info->hosts = g_slist_remove(connect_info->hosts, connect_info->hosts->data);
|
|
319 /* Free the address... */
|
|
320 g_free(connect_info->hosts->data);
|
|
321 connect_info->hosts = g_slist_remove(connect_info->hosts, connect_info->hosts->data);
|
|
322 }
|
|
323
|
|
324 g_free(connect_info->host);
|
|
325 g_free(connect_info);
|
|
326 }
|
|
327
|
|
328 static void
|
|
329 gaim_proxy_connect_info_connected(GaimProxyConnectInfo *connect_info)
|
|
330 {
|
|
331 connect_info->connect_cb(connect_info->data, connect_info->fd, NULL);
|
|
332
|
|
333 /*
|
|
334 * We've passed the file descriptor to the protocol, so it's no longer
|
|
335 * our responsibility, and we should be careful not to free it when
|
|
336 * we destroy the connect_info.
|
|
337 */
|
|
338 connect_info->fd = -1;
|
|
339
|
|
340 gaim_proxy_connect_info_destroy(connect_info);
|
|
341 }
|
|
342
|
|
343 /**
|
|
344 * @param error An error message explaining why the connection
|
|
345 * failed. This will be passed to the callback function
|
|
346 * specified in the call to gaim_proxy_connect().
|
|
347 */
|
|
348 /*
|
|
349 * TODO: Make sure all callers of this function pass a really really
|
|
350 * good error_message.
|
|
351 */
|
|
352 static void
|
|
353 gaim_proxy_connect_info_error(GaimProxyConnectInfo *connect_info, const gchar *error_message)
|
|
354 {
|
|
355 connect_info->connect_cb(connect_info->data, -1, error_message);
|
|
356 gaim_proxy_connect_info_destroy(connect_info);
|
|
357 }
|
|
358
|
|
359 static void
|
|
360 no_one_calls(gpointer data, gint source, GaimInputCondition cond)
|
|
361 {
|
|
362 GaimProxyConnectInfo *connect_info = data;
|
|
363 socklen_t len;
|
|
364 int error=0, ret;
|
|
365
|
|
366 gaim_debug_info("proxy", "Connected.\n");
|
|
367
|
|
368 len = sizeof(error);
|
|
369
|
|
370 /*
|
|
371 * getsockopt after a non-blocking connect returns -1 if something is
|
|
372 * really messed up (bad descriptor, usually). Otherwise, it returns 0 and
|
|
373 * error holds what connect would have returned if it blocked until now.
|
|
374 * Thus, error == 0 is success, error == EINPROGRESS means "try again",
|
|
375 * and anything else is a real error.
|
|
376 *
|
|
377 * (error == EINPROGRESS can happen after a select because the kernel can
|
|
378 * be overly optimistic sometimes. select is just a hint that you might be
|
|
379 * able to do something.)
|
|
380 */
|
|
381 ret = getsockopt(connect_info->fd, SOL_SOCKET, SO_ERROR, &error, &len);
|
|
382 if (ret == 0 && error == EINPROGRESS)
|
|
383 return; /* we'll be called again later */
|
|
384 if (ret < 0 || error != 0) {
|
|
385 if (ret!=0)
|
|
386 error = errno;
|
|
387
|
|
388 gaim_debug_error("proxy",
|
|
389 "getsockopt SO_ERROR check: %s\n", strerror(error));
|
|
390
|
|
391 gaim_proxy_connect_info_disconnect(connect_info);
|
|
392 try_connect(connect_info);
|
|
393 return;
|
|
394 }
|
|
395
|
|
396 gaim_input_remove(connect_info->inpa);
|
|
397 connect_info->inpa = 0;
|
|
398
|
|
399 gaim_proxy_connect_info_connected(connect_info);
|
|
400 }
|
|
401
|
|
402 static gboolean
|
|
403 clean_connect(gpointer data)
|
|
404 {
|
|
405 GaimProxyConnectInfo *connect_info;
|
|
406
|
|
407 connect_info = data;
|
|
408 gaim_proxy_connect_info_connected(connect_info);
|
|
409
|
|
410 return FALSE;
|
|
411 }
|
|
412
|
|
413 static int
|
|
414 proxy_connect_none(GaimProxyConnectInfo *connect_info, struct sockaddr *addr, socklen_t addrlen)
|
|
415 {
|
|
416 gaim_debug_info("proxy", "Connecting to %s:%d with no proxy\n",
|
|
417 connect_info->host, connect_info->port);
|
|
418
|
|
419 connect_info->fd = socket(addr->sa_family, SOCK_STREAM, 0);
|
|
420 if (connect_info->fd < 0)
|
|
421 {
|
|
422 gaim_debug_error("proxy",
|
|
423 "Unable to create socket: %s\n", strerror(errno));
|
|
424 return -1;
|
|
425 }
|
|
426 fcntl(connect_info->fd, F_SETFL, O_NONBLOCK);
|
|
427 #ifndef _WIN32
|
|
428 fcntl(connect_info->fd, F_SETFD, FD_CLOEXEC);
|
|
429 #endif
|
|
430
|
|
431 if (connect(connect_info->fd, (struct sockaddr *)addr, addrlen) != 0)
|
|
432 {
|
|
433 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
|
434 gaim_debug_info("proxy", "Connection in progress\n");
|
|
435 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_WRITE, no_one_calls, connect_info);
|
|
436 }
|
|
437 else {
|
|
438 gaim_debug_error("proxy",
|
|
439 "Connect failed: %s\n", strerror(errno));
|
|
440 close(connect_info->fd);
|
|
441 connect_info->fd = -1;
|
|
442 return -1;
|
|
443 }
|
|
444 }
|
|
445 else
|
|
446 {
|
|
447 /*
|
|
448 * The connection happened IMMEDIATELY... strange, but whatever.
|
|
449 */
|
|
450 socklen_t len;
|
|
451 int error = ETIMEDOUT;
|
|
452 gaim_debug_info("proxy", "Connected immediately.\n");
|
|
453 len = sizeof(error);
|
|
454 if (getsockopt(connect_info->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0)
|
|
455 {
|
|
456 gaim_debug_error("proxy", "getsockopt failed.\n");
|
|
457 close(connect_info->fd);
|
|
458 connect_info->fd = -1;
|
|
459 return -1;
|
|
460 }
|
|
461
|
|
462 /*
|
|
463 * We want to call the "connected" callback eventually, but we
|
|
464 * don't want to call it before we return, just in case.
|
|
465 */
|
|
466 gaim_timeout_add(10, clean_connect, connect_info);
|
|
467 }
|
|
468
|
|
469 return connect_info->fd;
|
|
470 }
|
|
471
|
|
472 static void
|
|
473 proxy_do_write(gpointer data, gint source, GaimInputCondition cond)
|
|
474 {
|
|
475 GaimProxyConnectInfo *connect_info = data;
|
|
476 const guchar *request = connect_info->write_buffer + connect_info->written_len;
|
|
477 gsize request_len = connect_info->write_buf_len - connect_info->written_len;
|
|
478
|
|
479 int ret = write(connect_info->fd, request, request_len);
|
|
480
|
|
481 if(ret < 0 && errno == EAGAIN)
|
|
482 return;
|
|
483 else if(ret < 0) {
|
|
484 gaim_proxy_connect_info_disconnect(connect_info);
|
|
485 try_connect(connect_info);
|
|
486 return;
|
|
487 } else if (ret < request_len) {
|
|
488 connect_info->written_len += ret;
|
|
489 return;
|
|
490 }
|
|
491
|
|
492 gaim_input_remove(connect_info->inpa);
|
|
493 g_free(connect_info->write_buffer);
|
|
494 connect_info->write_buffer = NULL;
|
|
495
|
|
496 /* register the response handler for the response */
|
|
497 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_READ, connect_info->read_cb, connect_info);
|
|
498 }
|
|
499
|
|
500 #define HTTP_GOODSTRING "HTTP/1.0 200"
|
|
501 #define HTTP_GOODSTRING2 "HTTP/1.1 200"
|
|
502
|
|
503 /* read the response to the CONNECT request, if we are requesting a non-port-80 tunnel */
|
|
504 static void
|
|
505 http_canread(gpointer data, gint source, GaimInputCondition cond)
|
|
506 {
|
|
507 int len, headers_len, status = 0;
|
|
508 gboolean error;
|
|
509 GaimProxyConnectInfo *connect_info = data;
|
|
510 guchar *p;
|
|
511 gsize max_read;
|
|
512 gchar *msg;
|
|
513
|
|
514 if(connect_info->read_buffer == NULL) {
|
|
515 connect_info->read_buf_len = 8192;
|
|
516 connect_info->read_buffer = g_malloc(connect_info->read_buf_len);
|
|
517 connect_info->read_len = 0;
|
|
518 }
|
|
519
|
|
520 p = connect_info->read_buffer + connect_info->read_len;
|
|
521 max_read = connect_info->read_buf_len - connect_info->read_len - 1;
|
|
522
|
|
523 len = read(connect_info->fd, p, max_read);
|
|
524 if(len < 0 && errno == EAGAIN)
|
|
525 return;
|
|
526 else if(len <= 0) {
|
|
527 gaim_proxy_connect_info_error(connect_info, _("Lost connection with server for an unknown reason."));
|
|
528 return;
|
|
529 } else {
|
|
530 connect_info->read_len += len;
|
|
531 }
|
|
532 p[len] = '\0';
|
|
533
|
|
534 if((p = (guchar *)g_strstr_len((const gchar *)connect_info->read_buffer, connect_info->read_len, "\r\n\r\n"))) {
|
|
535 *p = '\0';
|
|
536 headers_len = (p - connect_info->read_buffer) + 4;
|
|
537 } else if(len == max_read)
|
|
538 headers_len = len;
|
|
539 else
|
|
540 return;
|
|
541
|
|
542 error = strncmp((const char *)connect_info->read_buffer, "HTTP/", 5) != 0;
|
|
543 if (!error)
|
|
544 {
|
|
545 int major;
|
|
546 p = connect_info->read_buffer + 5;
|
|
547 major = strtol((const char *)p, (char **)&p, 10);
|
|
548 error = (major == 0) || (*p != '.');
|
|
549 if(!error) {
|
|
550 int minor;
|
|
551 p++;
|
|
552 minor = strtol((const char *)p, (char **)&p, 10);
|
|
553 error = (*p != ' ');
|
|
554 if(!error) {
|
|
555 p++;
|
|
556 status = strtol((const char *)p, (char **)&p, 10);
|
|
557 error = (*p != ' ');
|
|
558 }
|
|
559 }
|
|
560 }
|
|
561
|
|
562 /* Read the contents */
|
|
563 p = (guchar *)g_strrstr((const gchar *)connect_info->read_buffer, "Content-Length: ");
|
|
564 if (p != NULL)
|
|
565 {
|
|
566 gchar *tmp;
|
|
567 int len = 0;
|
|
568 char tmpc;
|
|
569 p += strlen("Content-Length: ");
|
|
570 tmp = strchr((const char *)p, '\r');
|
|
571 if(tmp)
|
|
572 *tmp = '\0';
|
|
573 len = atoi((const char *)p);
|
|
574 if(tmp)
|
|
575 *tmp = '\r';
|
|
576
|
|
577 /* Compensate for what has already been read */
|
|
578 len -= connect_info->read_len - headers_len;
|
|
579 /* I'm assuming that we're doing this to prevent the server from
|
|
580 complaining / breaking since we don't read the whole page */
|
|
581 while(len--) {
|
|
582 /* TODO: deal with EAGAIN (and other errors) better */
|
|
583 if (read(connect_info->fd, &tmpc, 1) < 0 && errno != EAGAIN)
|
|
584 break;
|
|
585 }
|
|
586 }
|
|
587
|
|
588 if (error)
|
|
589 {
|
|
590 msg = g_strdup_printf("Unable to parse response from HTTP proxy: %s\n",
|
|
591 connect_info->read_buffer);
|
|
592 gaim_proxy_connect_info_error(connect_info, msg);
|
|
593 g_free(msg);
|
|
594 return;
|
|
595 }
|
|
596 else if (status != 200)
|
|
597 {
|
|
598 gaim_debug_error("proxy",
|
|
599 "Proxy server replied with:\n%s\n",
|
|
600 connect_info->read_buffer);
|
|
601
|
|
602
|
|
603 if(status == 407 /* Proxy Auth */) {
|
|
604 gchar *ntlm;
|
|
605 if((ntlm = g_strrstr((const gchar *)connect_info->read_buffer, "Proxy-Authenticate: NTLM "))) { /* Check for Type-2 */
|
|
606 gchar *tmp = ntlm;
|
|
607 guint8 *nonce;
|
|
608 gchar *domain = (gchar*)gaim_proxy_info_get_username(connect_info->gpi);
|
|
609 gchar *username;
|
|
610 gchar *request;
|
|
611 gchar *response;
|
|
612 username = strchr(domain, '\\');
|
|
613 if (username == NULL)
|
|
614 {
|
|
615 msg = g_strdup_printf(_("HTTP proxy connection error %d"), status);
|
|
616 gaim_proxy_connect_info_error(connect_info, msg);
|
|
617 g_free(msg);
|
|
618 return;
|
|
619 }
|
|
620 *username = '\0';
|
|
621 username++;
|
|
622 ntlm += strlen("Proxy-Authenticate: NTLM ");
|
|
623 while(*tmp != '\r' && *tmp != '\0') tmp++;
|
|
624 *tmp = '\0';
|
|
625 nonce = gaim_ntlm_parse_type2(ntlm, NULL);
|
|
626 response = gaim_ntlm_gen_type3(username,
|
|
627 (gchar*) gaim_proxy_info_get_password(connect_info->gpi),
|
|
628 (gchar*) gaim_proxy_info_get_host(connect_info->gpi),
|
|
629 domain, nonce, NULL);
|
|
630 username--;
|
|
631 *username = '\\';
|
|
632 request = g_strdup_printf(
|
|
633 "CONNECT %s:%d HTTP/1.1\r\n"
|
|
634 "Host: %s:%d\r\n"
|
|
635 "Proxy-Authorization: NTLM %s\r\n"
|
|
636 "Proxy-Connection: Keep-Alive\r\n\r\n",
|
|
637 connect_info->host, connect_info->port, connect_info->host,
|
|
638 connect_info->port, response);
|
|
639 g_free(response);
|
|
640
|
|
641 gaim_input_remove(connect_info->inpa);
|
|
642 g_free(connect_info->read_buffer);
|
|
643 connect_info->read_buffer = NULL;
|
|
644
|
|
645 connect_info->write_buffer = (guchar *)request;
|
|
646 connect_info->write_buf_len = strlen(request);
|
|
647 connect_info->written_len = 0;
|
|
648
|
|
649 connect_info->read_cb = http_canread;
|
|
650
|
|
651 connect_info->inpa = gaim_input_add(connect_info->fd,
|
|
652 GAIM_INPUT_WRITE, proxy_do_write, connect_info);
|
|
653
|
|
654 proxy_do_write(connect_info, connect_info->fd, cond);
|
|
655 return;
|
|
656 } else if((ntlm = g_strrstr((const char *)connect_info->read_buffer, "Proxy-Authenticate: NTLM"))) { /* Empty message */
|
|
657 gchar request[2048];
|
|
658 gchar *domain = (gchar*) gaim_proxy_info_get_username(connect_info->gpi);
|
|
659 gchar *username;
|
|
660 int request_len;
|
|
661 username = strchr(domain, '\\');
|
|
662 if (username == NULL)
|
|
663 {
|
|
664 msg = g_strdup_printf(_("HTTP proxy connection error %d"), status);
|
|
665 gaim_proxy_connect_info_error(connect_info, msg);
|
|
666 g_free(msg);
|
|
667 return;
|
|
668 }
|
|
669 *username = '\0';
|
|
670
|
|
671 request_len = g_snprintf(request, sizeof(request),
|
|
672 "CONNECT %s:%d HTTP/1.1\r\n"
|
|
673 "Host: %s:%d\r\n",
|
|
674 connect_info->host, connect_info->port,
|
|
675 connect_info->host, connect_info->port);
|
|
676
|
|
677 g_return_if_fail(request_len < sizeof(request));
|
|
678 request_len += g_snprintf(request + request_len,
|
|
679 sizeof(request) - request_len,
|
|
680 "Proxy-Authorization: NTLM %s\r\n"
|
|
681 "Proxy-Connection: Keep-Alive\r\n\r\n",
|
|
682 gaim_ntlm_gen_type1(
|
|
683 (gchar*) gaim_proxy_info_get_host(connect_info->gpi),
|
|
684 domain));
|
|
685 *username = '\\';
|
|
686
|
|
687 gaim_input_remove(connect_info->inpa);
|
|
688 g_free(connect_info->read_buffer);
|
|
689 connect_info->read_buffer = NULL;
|
|
690
|
|
691 connect_info->write_buffer = g_memdup(request, request_len);
|
|
692 connect_info->write_buf_len = request_len;
|
|
693 connect_info->written_len = 0;
|
|
694
|
|
695 connect_info->read_cb = http_canread;
|
|
696
|
|
697 connect_info->inpa = gaim_input_add(connect_info->fd,
|
|
698 GAIM_INPUT_WRITE, proxy_do_write, connect_info);
|
|
699
|
|
700 proxy_do_write(connect_info, connect_info->fd, cond);
|
|
701 return;
|
|
702 } else {
|
|
703 msg = g_strdup_printf(_("HTTP proxy connection error %d"), status);
|
|
704 gaim_proxy_connect_info_error(connect_info, msg);
|
|
705 g_free(msg);
|
|
706 return;
|
|
707 }
|
|
708 }
|
|
709 if(status == 403 /* Forbidden */ ) {
|
|
710 msg = g_strdup_printf(_("Access denied: HTTP proxy server forbids port %d tunnelling."), connect_info->port);
|
|
711 gaim_proxy_connect_info_error(connect_info, msg);
|
|
712 g_free(msg);
|
|
713 } else {
|
|
714 msg = g_strdup_printf(_("HTTP proxy connection error %d"), status);
|
|
715 gaim_proxy_connect_info_error(connect_info, msg);
|
|
716 g_free(msg);
|
|
717 }
|
|
718 } else {
|
|
719 gaim_input_remove(connect_info->inpa);
|
|
720 connect_info->inpa = 0;
|
|
721 g_free(connect_info->read_buffer);
|
|
722 connect_info->read_buffer = NULL;
|
|
723 gaim_debug_info("proxy", "HTTP proxy connection established\n");
|
|
724 gaim_proxy_connect_info_connected(connect_info);
|
|
725 return;
|
|
726 }
|
|
727 }
|
|
728
|
|
729
|
|
730
|
|
731 static void
|
|
732 http_canwrite(gpointer data, gint source, GaimInputCondition cond)
|
|
733 {
|
|
734 char request[8192];
|
|
735 int request_len = 0;
|
|
736 GaimProxyConnectInfo *connect_info = data;
|
|
737 socklen_t len;
|
|
738 int error = ETIMEDOUT;
|
|
739
|
|
740 gaim_debug_info("http proxy", "Connected.\n");
|
|
741
|
|
742 if (connect_info->inpa > 0)
|
|
743 {
|
|
744 gaim_input_remove(connect_info->inpa);
|
|
745 connect_info->inpa = 0;
|
|
746 }
|
|
747
|
|
748 len = sizeof(error);
|
|
749
|
|
750 if (getsockopt(connect_info->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
|
|
751 gaim_proxy_connect_info_disconnect(connect_info);
|
|
752 try_connect(connect_info);
|
|
753 return;
|
|
754 }
|
|
755
|
|
756 gaim_debug_info("proxy", "using CONNECT tunnelling for %s:%d\n",
|
|
757 connect_info->host, connect_info->port);
|
|
758 request_len = g_snprintf(request, sizeof(request),
|
|
759 "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n",
|
|
760 connect_info->host, connect_info->port, connect_info->host, connect_info->port);
|
|
761
|
|
762 if (gaim_proxy_info_get_username(connect_info->gpi) != NULL) {
|
|
763 char *t1, *t2;
|
|
764 t1 = g_strdup_printf("%s:%s",
|
|
765 gaim_proxy_info_get_username(connect_info->gpi),
|
|
766 gaim_proxy_info_get_password(connect_info->gpi) ?
|
|
767 gaim_proxy_info_get_password(connect_info->gpi) : "");
|
|
768 t2 = gaim_base64_encode((const guchar *)t1, strlen(t1));
|
|
769 g_free(t1);
|
|
770 g_return_if_fail(request_len < sizeof(request));
|
|
771
|
|
772 request_len += g_snprintf(request + request_len,
|
|
773 sizeof(request) - request_len,
|
|
774 "Proxy-Authorization: Basic %s\r\n"
|
|
775 "Proxy-Authorization: NTLM %s\r\n"
|
|
776 "Proxy-Connection: Keep-Alive\r\n", t2,
|
|
777 gaim_ntlm_gen_type1(
|
|
778 (gchar*)gaim_proxy_info_get_host(connect_info->gpi),""));
|
|
779 g_free(t2);
|
|
780 }
|
|
781
|
|
782 g_return_if_fail(request_len < sizeof(request));
|
|
783 strcpy(request + request_len, "\r\n");
|
|
784 request_len += 2;
|
|
785 connect_info->write_buffer = g_memdup(request, request_len);
|
|
786 connect_info->write_buf_len = request_len;
|
|
787 connect_info->written_len = 0;
|
|
788
|
|
789 connect_info->read_cb = http_canread;
|
|
790
|
|
791 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_WRITE, proxy_do_write,
|
|
792 connect_info);
|
|
793
|
|
794 proxy_do_write(connect_info, connect_info->fd, cond);
|
|
795 }
|
|
796
|
|
797 static int
|
|
798 proxy_connect_http(GaimProxyConnectInfo *connect_info, struct sockaddr *addr, socklen_t addrlen)
|
|
799 {
|
|
800 gaim_debug_info("http proxy",
|
|
801 "Connecting to %s:%d via %s:%d using HTTP\n",
|
|
802 (connect_info->host ? connect_info->host : "(null)"), connect_info->port,
|
|
803 (gaim_proxy_info_get_host(connect_info->gpi) ? gaim_proxy_info_get_host(connect_info->gpi) : "(null)"),
|
|
804 gaim_proxy_info_get_port(connect_info->gpi));
|
|
805
|
|
806 connect_info->fd = socket(addr->sa_family, SOCK_STREAM, 0);
|
|
807 if (connect_info->fd < 0)
|
|
808 return -1;
|
|
809
|
|
810 fcntl(connect_info->fd, F_SETFL, O_NONBLOCK);
|
|
811 #ifndef _WIN32
|
|
812 fcntl(connect_info->fd, F_SETFD, FD_CLOEXEC);
|
|
813 #endif
|
|
814
|
|
815 if (connect(connect_info->fd, addr, addrlen) != 0)
|
|
816 {
|
|
817 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
|
818 gaim_debug_info("http proxy", "Connection in progress\n");
|
|
819
|
|
820 if (connect_info->port != 80) {
|
|
821 /* we need to do CONNECT first */
|
|
822 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_WRITE,
|
|
823 http_canwrite, connect_info);
|
|
824 } else {
|
|
825 gaim_debug_info("proxy", "HTTP proxy connection established\n");
|
|
826 gaim_proxy_connect_info_connected(connect_info);
|
|
827 }
|
|
828 } else {
|
|
829 close(connect_info->fd);
|
|
830 connect_info->fd = -1;
|
|
831 return -1;
|
|
832 }
|
|
833 }
|
|
834 else {
|
|
835 socklen_t len;
|
|
836 int error = ETIMEDOUT;
|
|
837
|
|
838 gaim_debug_info("http proxy", "Connected immediately.\n");
|
|
839
|
|
840 len = sizeof(error);
|
|
841 if (getsockopt(connect_info->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0)
|
|
842 {
|
|
843 close(connect_info->fd);
|
|
844 connect_info->fd = -1;
|
|
845 return -1;
|
|
846 }
|
|
847 http_canwrite(connect_info, connect_info->fd, GAIM_INPUT_WRITE);
|
|
848 }
|
|
849
|
|
850 return connect_info->fd;
|
|
851 }
|
|
852
|
|
853
|
|
854 static void
|
|
855 s4_canread(gpointer data, gint source, GaimInputCondition cond)
|
|
856 {
|
|
857 GaimProxyConnectInfo *connect_info = data;
|
|
858 guchar *buf;
|
|
859 int len, max_read;
|
|
860
|
|
861 /* This is really not going to block under normal circumstances, but to
|
|
862 * be correct, we deal with the unlikely scenario */
|
|
863
|
|
864 if (connect_info->read_buffer == NULL) {
|
|
865 connect_info->read_buf_len = 12;
|
|
866 connect_info->read_buffer = g_malloc(connect_info->read_buf_len);
|
|
867 connect_info->read_len = 0;
|
|
868 }
|
|
869
|
|
870 buf = connect_info->read_buffer + connect_info->read_len;
|
|
871 max_read = connect_info->read_buf_len - connect_info->read_len;
|
|
872
|
|
873 len = read(connect_info->fd, buf, max_read);
|
|
874
|
|
875 if ((len < 0 && errno == EAGAIN) || (len > 0 && len + connect_info->read_len < 4))
|
|
876 return;
|
|
877 else if (len + connect_info->read_len >= 4) {
|
|
878 if (connect_info->read_buffer[1] == 90) {
|
|
879 gaim_proxy_connect_info_connected(connect_info);
|
|
880 return;
|
|
881 }
|
|
882 }
|
|
883
|
|
884 gaim_proxy_connect_info_disconnect(connect_info);
|
|
885 try_connect(connect_info);
|
|
886 }
|
|
887
|
|
888 static void
|
|
889 s4_canwrite(gpointer data, gint source, GaimInputCondition cond)
|
|
890 {
|
|
891 unsigned char packet[9];
|
|
892 struct hostent *hp;
|
|
893 GaimProxyConnectInfo *connect_info = data;
|
|
894 socklen_t len;
|
|
895 int error = ETIMEDOUT;
|
|
896
|
|
897 gaim_debug_info("socks4 proxy", "Connected.\n");
|
|
898
|
|
899 if (connect_info->inpa > 0)
|
|
900 {
|
|
901 gaim_input_remove(connect_info->inpa);
|
|
902 connect_info->inpa = 0;
|
|
903 }
|
|
904
|
|
905 len = sizeof(error);
|
|
906
|
|
907 if (getsockopt(connect_info->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
|
|
908 gaim_proxy_connect_info_disconnect(connect_info);
|
|
909 try_connect(connect_info);
|
|
910 return;
|
|
911 }
|
|
912
|
|
913 /*
|
|
914 * The socks4 spec doesn't include support for doing host name
|
|
915 * lookups by the proxy. Some socks4 servers do this via
|
|
916 * extensions to the protocol. Since we don't know if a
|
|
917 * server supports this, it would need to be implemented
|
|
918 * with an option, or some detection mechanism - in the
|
|
919 * meantime, stick with plain old SOCKS4.
|
|
920 */
|
|
921 /* TODO: This needs to be non-blocking! */
|
|
922 hp = gethostbyname(connect_info->host);
|
|
923 if (hp == NULL) {
|
|
924 gaim_proxy_connect_info_disconnect(connect_info);
|
|
925 try_connect(connect_info);
|
|
926 return;
|
|
927 }
|
|
928
|
|
929 packet[0] = 4;
|
|
930 packet[1] = 1;
|
|
931 packet[2] = connect_info->port >> 8;
|
|
932 packet[3] = connect_info->port & 0xff;
|
|
933 packet[4] = (unsigned char)(hp->h_addr_list[0])[0];
|
|
934 packet[5] = (unsigned char)(hp->h_addr_list[0])[1];
|
|
935 packet[6] = (unsigned char)(hp->h_addr_list[0])[2];
|
|
936 packet[7] = (unsigned char)(hp->h_addr_list[0])[3];
|
|
937 packet[8] = 0;
|
|
938
|
|
939 connect_info->write_buffer = g_memdup(packet, sizeof(packet));
|
|
940 connect_info->write_buf_len = sizeof(packet);
|
|
941 connect_info->written_len = 0;
|
|
942 connect_info->read_cb = s4_canread;
|
|
943
|
|
944 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_WRITE, proxy_do_write, connect_info);
|
|
945
|
|
946 proxy_do_write(connect_info, connect_info->fd, cond);
|
|
947 }
|
|
948
|
|
949 static int
|
|
950 proxy_connect_socks4(GaimProxyConnectInfo *connect_info, struct sockaddr *addr, socklen_t addrlen)
|
|
951 {
|
|
952 gaim_debug_info("socks4 proxy",
|
|
953 "Connecting to %s:%d via %s:%d using SOCKS4\n",
|
|
954 connect_info->host, connect_info->port,
|
|
955 gaim_proxy_info_get_host(connect_info->gpi),
|
|
956 gaim_proxy_info_get_port(connect_info->gpi));
|
|
957
|
|
958 connect_info->fd = socket(addr->sa_family, SOCK_STREAM, 0);
|
|
959 if (connect_info->fd < 0)
|
|
960 return -1;
|
|
961
|
|
962 fcntl(connect_info->fd, F_SETFL, O_NONBLOCK);
|
|
963 #ifndef _WIN32
|
|
964 fcntl(connect_info->fd, F_SETFD, FD_CLOEXEC);
|
|
965 #endif
|
|
966
|
|
967 if (connect(connect_info->fd, addr, addrlen) != 0)
|
|
968 {
|
|
969 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
|
970 gaim_debug_info("socks4 proxy", "Connection in progress.\n");
|
|
971 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_WRITE, s4_canwrite, connect_info);
|
|
972 }
|
|
973 else {
|
|
974 close(connect_info->fd);
|
|
975 connect_info->fd = -1;
|
|
976 return -1;
|
|
977 }
|
|
978 } else {
|
|
979 socklen_t len;
|
|
980 int error = ETIMEDOUT;
|
|
981
|
|
982 gaim_debug_info("socks4 proxy", "Connected immediately.\n");
|
|
983
|
|
984 len = sizeof(error);
|
|
985
|
|
986 if (getsockopt(connect_info->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0)
|
|
987 {
|
|
988 close(connect_info->fd);
|
|
989 connect_info->fd = -1;
|
|
990 return -1;
|
|
991 }
|
|
992
|
|
993 s4_canwrite(connect_info, connect_info->fd, GAIM_INPUT_WRITE);
|
|
994 }
|
|
995
|
|
996 return connect_info->fd;
|
|
997 }
|
|
998
|
|
999 static void
|
|
1000 s5_canread_again(gpointer data, gint source, GaimInputCondition cond)
|
|
1001 {
|
|
1002 guchar *dest, *buf;
|
|
1003 GaimProxyConnectInfo *connect_info = data;
|
|
1004 int len;
|
|
1005
|
|
1006 if (connect_info->read_buffer == NULL) {
|
|
1007 connect_info->read_buf_len = 512;
|
|
1008 connect_info->read_buffer = g_malloc(connect_info->read_buf_len);
|
|
1009 connect_info->read_len = 0;
|
|
1010 }
|
|
1011
|
|
1012 dest = connect_info->read_buffer + connect_info->read_len;
|
|
1013 buf = connect_info->read_buffer;
|
|
1014
|
|
1015 gaim_debug_info("socks5 proxy", "Able to read again.\n");
|
|
1016
|
|
1017 len = read(connect_info->fd, dest, (connect_info->read_buf_len - connect_info->read_len));
|
|
1018 if(len < 0 && errno == EAGAIN)
|
|
1019 return;
|
|
1020 else if(len <= 0) {
|
|
1021 gaim_debug_warning("socks5 proxy", "or not...\n");
|
|
1022 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1023 try_connect(connect_info);
|
|
1024 return;
|
|
1025 }
|
|
1026 connect_info->read_len += len;
|
|
1027
|
|
1028 if(connect_info->read_len < 4)
|
|
1029 return;
|
|
1030
|
|
1031 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
|
|
1032 if ((buf[0] == 0x05) && (buf[1] < 0x09))
|
|
1033 gaim_debug_error("socks5 proxy", socks5errors[buf[1]]);
|
|
1034 else
|
|
1035 gaim_debug_error("socks5 proxy", "Bad data.\n");
|
|
1036 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1037 try_connect(connect_info);
|
|
1038 return;
|
|
1039 }
|
|
1040
|
|
1041 /* Skip past BND.ADDR */
|
|
1042 switch(buf[3]) {
|
|
1043 case 0x01: /* the address is a version-4 IP address, with a length of 4 octets */
|
|
1044 if(connect_info->read_len < 4 + 4)
|
|
1045 return;
|
|
1046 buf += 4 + 4;
|
|
1047 break;
|
|
1048 case 0x03: /* the address field contains a fully-qualified domain name. The first
|
|
1049 octet of the address field contains the number of octets of name that
|
|
1050 follow, there is no terminating NUL octet. */
|
|
1051 if(connect_info->read_len < 4 + 1)
|
|
1052 return;
|
|
1053 buf += 4 + 1;
|
|
1054 if(connect_info->read_len < 4 + 1 + buf[0])
|
|
1055 return;
|
|
1056 buf += buf[0];
|
|
1057 break;
|
|
1058 case 0x04: /* the address is a version-6 IP address, with a length of 16 octets */
|
|
1059 if(connect_info->read_len < 4 + 16)
|
|
1060 return;
|
|
1061 buf += 4 + 16;
|
|
1062 break;
|
|
1063 }
|
|
1064
|
|
1065 if(connect_info->read_len < (buf - connect_info->read_buffer) + 2)
|
|
1066 return;
|
|
1067
|
|
1068 /* Skip past BND.PORT */
|
|
1069 buf += 2;
|
|
1070
|
|
1071 gaim_proxy_connect_info_connected(connect_info);
|
|
1072 }
|
|
1073
|
|
1074 static void
|
|
1075 s5_sendconnect(gpointer data, int source)
|
|
1076 {
|
|
1077 GaimProxyConnectInfo *connect_info = data;
|
|
1078 int hlen = strlen(connect_info->host);
|
|
1079 connect_info->write_buf_len = 5 + hlen + 2;
|
|
1080 connect_info->write_buffer = g_malloc(connect_info->write_buf_len);
|
|
1081 connect_info->written_len = 0;
|
|
1082
|
|
1083 connect_info->write_buffer[0] = 0x05;
|
|
1084 connect_info->write_buffer[1] = 0x01; /* CONNECT */
|
|
1085 connect_info->write_buffer[2] = 0x00; /* reserved */
|
|
1086 connect_info->write_buffer[3] = 0x03; /* address type -- host name */
|
|
1087 connect_info->write_buffer[4] = hlen;
|
|
1088 memcpy(connect_info->write_buffer + 5, connect_info->host, hlen);
|
|
1089 connect_info->write_buffer[5 + hlen] = connect_info->port >> 8;
|
|
1090 connect_info->write_buffer[5 + hlen + 1] = connect_info->port & 0xff;
|
|
1091
|
|
1092 connect_info->read_cb = s5_canread_again;
|
|
1093
|
|
1094 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_WRITE, proxy_do_write, connect_info);
|
|
1095 proxy_do_write(connect_info, connect_info->fd, GAIM_INPUT_WRITE);
|
|
1096 }
|
|
1097
|
|
1098 static void
|
|
1099 s5_readauth(gpointer data, gint source, GaimInputCondition cond)
|
|
1100 {
|
|
1101 GaimProxyConnectInfo *connect_info = data;
|
|
1102 int len;
|
|
1103
|
|
1104 if (connect_info->read_buffer == NULL) {
|
|
1105 connect_info->read_buf_len = 2;
|
|
1106 connect_info->read_buffer = g_malloc(connect_info->read_buf_len);
|
|
1107 connect_info->read_len = 0;
|
|
1108 }
|
|
1109
|
|
1110 gaim_debug_info("socks5 proxy", "Got auth response.\n");
|
|
1111
|
|
1112 len = read(connect_info->fd, connect_info->read_buffer + connect_info->read_len,
|
|
1113 connect_info->read_buf_len - connect_info->read_len);
|
|
1114 if(len < 0 && errno == EAGAIN)
|
|
1115 return;
|
|
1116 else if(len <= 0) {
|
|
1117 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1118 try_connect(connect_info);
|
|
1119 return;
|
|
1120 }
|
|
1121 connect_info->read_len += len;
|
|
1122
|
|
1123 if (connect_info->read_len < 2)
|
|
1124 return;
|
|
1125
|
|
1126 gaim_input_remove(connect_info->inpa);
|
|
1127 connect_info->inpa = 0;
|
|
1128
|
|
1129 if ((connect_info->read_buffer[0] != 0x01) || (connect_info->read_buffer[1] != 0x00)) {
|
|
1130 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1131 try_connect(connect_info);
|
|
1132 return;
|
|
1133 }
|
|
1134
|
|
1135 g_free(connect_info->read_buffer);
|
|
1136 connect_info->read_buffer = NULL;
|
|
1137
|
|
1138 s5_sendconnect(connect_info, connect_info->fd);
|
|
1139 }
|
|
1140
|
|
1141 static void
|
|
1142 hmacmd5_chap(const unsigned char * challenge, int challen, const char * passwd, unsigned char * response)
|
|
1143 {
|
|
1144 GaimCipher *cipher;
|
|
1145 GaimCipherContext *ctx;
|
|
1146 int i;
|
|
1147 unsigned char Kxoripad[65];
|
|
1148 unsigned char Kxoropad[65];
|
|
1149 int pwlen;
|
|
1150
|
|
1151 cipher = gaim_ciphers_find_cipher("md5");
|
|
1152 ctx = gaim_cipher_context_new(cipher, NULL);
|
|
1153
|
|
1154 memset(Kxoripad,0,sizeof(Kxoripad));
|
|
1155 memset(Kxoropad,0,sizeof(Kxoropad));
|
|
1156
|
|
1157 pwlen=strlen(passwd);
|
|
1158 if (pwlen>64) {
|
|
1159 gaim_cipher_context_append(ctx, (const guchar *)passwd, strlen(passwd));
|
|
1160 gaim_cipher_context_digest(ctx, sizeof(Kxoripad), Kxoripad, NULL);
|
|
1161 pwlen=16;
|
|
1162 } else {
|
|
1163 memcpy(Kxoripad, passwd, pwlen);
|
|
1164 }
|
|
1165 memcpy(Kxoropad,Kxoripad,pwlen);
|
|
1166
|
|
1167 for (i=0;i<64;i++) {
|
|
1168 Kxoripad[i]^=0x36;
|
|
1169 Kxoropad[i]^=0x5c;
|
|
1170 }
|
|
1171
|
|
1172 gaim_cipher_context_reset(ctx, NULL);
|
|
1173 gaim_cipher_context_append(ctx, Kxoripad, 64);
|
|
1174 gaim_cipher_context_append(ctx, challenge, challen);
|
|
1175 gaim_cipher_context_digest(ctx, sizeof(Kxoripad), Kxoripad, NULL);
|
|
1176
|
|
1177 gaim_cipher_context_reset(ctx, NULL);
|
|
1178 gaim_cipher_context_append(ctx, Kxoropad, 64);
|
|
1179 gaim_cipher_context_append(ctx, Kxoripad, 16);
|
|
1180 gaim_cipher_context_digest(ctx, 16, response, NULL);
|
|
1181
|
|
1182 gaim_cipher_context_destroy(ctx);
|
|
1183 }
|
|
1184
|
|
1185 static void
|
|
1186 s5_readchap(gpointer data, gint source, GaimInputCondition cond)
|
|
1187 {
|
|
1188 guchar *cmdbuf, *buf;
|
|
1189 GaimProxyConnectInfo *connect_info = data;
|
|
1190 int len, navas, currentav;
|
|
1191
|
|
1192 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Got CHAP response.\n");
|
|
1193
|
|
1194 if (connect_info->read_buffer == NULL) {
|
|
1195 connect_info->read_buf_len = 20;
|
|
1196 connect_info->read_buffer = g_malloc(connect_info->read_buf_len);
|
|
1197 connect_info->read_len = 0;
|
|
1198 }
|
|
1199
|
|
1200 len = read(connect_info->fd, connect_info->read_buffer + connect_info->read_len,
|
|
1201 connect_info->read_buf_len - connect_info->read_len);
|
|
1202
|
|
1203 if(len < 0 && errno == EAGAIN)
|
|
1204 return;
|
|
1205 else if(len <= 0) {
|
|
1206 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1207 try_connect(connect_info);
|
|
1208 return;
|
|
1209 }
|
|
1210 connect_info->read_len += len;
|
|
1211
|
|
1212 if (connect_info->read_len < 2)
|
|
1213 return;
|
|
1214
|
|
1215 cmdbuf = connect_info->read_buffer;
|
|
1216
|
|
1217 if (*cmdbuf != 0x01) {
|
|
1218 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1219 try_connect(connect_info);
|
|
1220 return;
|
|
1221 }
|
|
1222 cmdbuf++;
|
|
1223
|
|
1224 navas = *cmdbuf;
|
|
1225 cmdbuf++;
|
|
1226
|
|
1227 for (currentav = 0; currentav < navas; currentav++) {
|
|
1228 if (connect_info->read_len - (cmdbuf - connect_info->read_buffer) < 2)
|
|
1229 return;
|
|
1230 if (connect_info->read_len - (cmdbuf - connect_info->read_buffer) < cmdbuf[1])
|
|
1231 return;
|
|
1232 buf = cmdbuf + 2;
|
|
1233 switch (cmdbuf[0]) {
|
|
1234 case 0x00:
|
|
1235 /* Did auth work? */
|
|
1236 if (buf[0] == 0x00) {
|
|
1237 gaim_input_remove(connect_info->inpa);
|
|
1238 connect_info->inpa = 0;
|
|
1239 g_free(connect_info->read_buffer);
|
|
1240 connect_info->read_buffer = NULL;
|
|
1241 /* Success */
|
|
1242 s5_sendconnect(connect_info, connect_info->fd);
|
|
1243 return;
|
|
1244 } else {
|
|
1245 /* Failure */
|
|
1246 gaim_debug_warning("proxy",
|
|
1247 "socks5 CHAP authentication "
|
|
1248 "failed. Disconnecting...");
|
|
1249 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1250 try_connect(connect_info);
|
|
1251 return;
|
|
1252 }
|
|
1253 break;
|
|
1254 case 0x03:
|
|
1255 /* Server wants our credentials */
|
|
1256
|
|
1257 connect_info->write_buf_len = 16 + 4;
|
|
1258 connect_info->write_buffer = g_malloc(connect_info->write_buf_len);
|
|
1259 connect_info->written_len = 0;
|
|
1260
|
|
1261 hmacmd5_chap(buf, cmdbuf[1],
|
|
1262 gaim_proxy_info_get_password(connect_info->gpi),
|
|
1263 connect_info->write_buffer + 4);
|
|
1264 connect_info->write_buffer[0] = 0x01;
|
|
1265 connect_info->write_buffer[1] = 0x01;
|
|
1266 connect_info->write_buffer[2] = 0x04;
|
|
1267 connect_info->write_buffer[3] = 0x10;
|
|
1268
|
|
1269 gaim_input_remove(connect_info->inpa);
|
|
1270 g_free(connect_info->read_buffer);
|
|
1271 connect_info->read_buffer = NULL;
|
|
1272
|
|
1273 connect_info->read_cb = s5_readchap;
|
|
1274
|
|
1275 connect_info->inpa = gaim_input_add(connect_info->fd,
|
|
1276 GAIM_INPUT_WRITE, proxy_do_write, connect_info);
|
|
1277
|
|
1278 proxy_do_write(connect_info, connect_info->fd, GAIM_INPUT_WRITE);
|
|
1279 break;
|
|
1280 case 0x11:
|
|
1281 /* Server wants to select an algorithm */
|
|
1282 if (buf[0] != 0x85) {
|
|
1283 /* Only currently support HMAC-MD5 */
|
|
1284 gaim_debug_warning("proxy",
|
|
1285 "Server tried to select an "
|
|
1286 "algorithm that we did not advertise "
|
|
1287 "as supporting. This is a violation "
|
|
1288 "of the socks5 CHAP specification. "
|
|
1289 "Disconnecting...");
|
|
1290 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1291 try_connect(connect_info);
|
|
1292 return;
|
|
1293 }
|
|
1294 break;
|
|
1295 }
|
|
1296 cmdbuf = buf + cmdbuf[1];
|
|
1297 }
|
|
1298 /* Fell through. We ran out of CHAP events to process, but haven't
|
|
1299 * succeeded or failed authentication - there may be more to come.
|
|
1300 * If this is the case, come straight back here. */
|
|
1301 }
|
|
1302
|
|
1303 static void
|
|
1304 s5_canread(gpointer data, gint source, GaimInputCondition cond)
|
|
1305 {
|
|
1306 GaimProxyConnectInfo *connect_info = data;
|
|
1307 int len;
|
|
1308
|
|
1309 if (connect_info->read_buffer == NULL) {
|
|
1310 connect_info->read_buf_len = 2;
|
|
1311 connect_info->read_buffer = g_malloc(connect_info->read_buf_len);
|
|
1312 connect_info->read_len = 0;
|
|
1313 }
|
|
1314
|
|
1315 gaim_debug_info("socks5 proxy", "Able to read.\n");
|
|
1316
|
|
1317 len = read(connect_info->fd, connect_info->read_buffer + connect_info->read_len,
|
|
1318 connect_info->read_buf_len - connect_info->read_len);
|
|
1319 if(len < 0 && errno == EAGAIN)
|
|
1320 return;
|
|
1321 else if(len <= 0) {
|
|
1322 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1323 try_connect(connect_info);
|
|
1324 return;
|
|
1325 }
|
|
1326 connect_info->read_len += len;
|
|
1327
|
|
1328 if (connect_info->read_len < 2)
|
|
1329 return;
|
|
1330
|
|
1331 gaim_input_remove(connect_info->inpa);
|
|
1332 connect_info->inpa = 0;
|
|
1333
|
|
1334 if ((connect_info->read_buffer[0] != 0x05) || (connect_info->read_buffer[1] == 0xff)) {
|
|
1335 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1336 try_connect(connect_info);
|
|
1337 return;
|
|
1338 }
|
|
1339
|
|
1340 if (connect_info->read_buffer[1] == 0x02) {
|
|
1341 gsize i, j;
|
|
1342 const char *u, *p;
|
|
1343
|
|
1344 u = gaim_proxy_info_get_username(connect_info->gpi);
|
|
1345 p = gaim_proxy_info_get_password(connect_info->gpi);
|
|
1346
|
|
1347 i = (u == NULL) ? 0 : strlen(u);
|
|
1348 j = (p == NULL) ? 0 : strlen(p);
|
|
1349
|
|
1350 connect_info->write_buf_len = 1 + 1 + i + 1 + j;
|
|
1351 connect_info->write_buffer = g_malloc(connect_info->write_buf_len);
|
|
1352 connect_info->written_len = 0;
|
|
1353
|
|
1354 connect_info->write_buffer[0] = 0x01; /* version 1 */
|
|
1355 connect_info->write_buffer[1] = i;
|
|
1356 if (u != NULL)
|
|
1357 memcpy(connect_info->write_buffer + 2, u, i);
|
|
1358 connect_info->write_buffer[2 + i] = j;
|
|
1359 if (p != NULL)
|
|
1360 memcpy(connect_info->write_buffer + 2 + i + 1, p, j);
|
|
1361
|
|
1362 g_free(connect_info->read_buffer);
|
|
1363 connect_info->read_buffer = NULL;
|
|
1364
|
|
1365 connect_info->read_cb = s5_readauth;
|
|
1366
|
|
1367 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_WRITE,
|
|
1368 proxy_do_write, connect_info);
|
|
1369
|
|
1370 proxy_do_write(connect_info, connect_info->fd, GAIM_INPUT_WRITE);
|
|
1371
|
|
1372 return;
|
|
1373 } else if (connect_info->read_buffer[1] == 0x03) {
|
|
1374 gsize userlen;
|
|
1375 userlen = strlen(gaim_proxy_info_get_username(connect_info->gpi));
|
|
1376
|
|
1377 connect_info->write_buf_len = 7 + userlen;
|
|
1378 connect_info->write_buffer = g_malloc(connect_info->write_buf_len);
|
|
1379 connect_info->written_len = 0;
|
|
1380
|
|
1381 connect_info->write_buffer[0] = 0x01;
|
|
1382 connect_info->write_buffer[1] = 0x02;
|
|
1383 connect_info->write_buffer[2] = 0x11;
|
|
1384 connect_info->write_buffer[3] = 0x01;
|
|
1385 connect_info->write_buffer[4] = 0x85;
|
|
1386 connect_info->write_buffer[5] = 0x02;
|
|
1387 connect_info->write_buffer[6] = userlen;
|
|
1388 memcpy(connect_info->write_buffer + 7,
|
|
1389 gaim_proxy_info_get_username(connect_info->gpi), userlen);
|
|
1390
|
|
1391 g_free(connect_info->read_buffer);
|
|
1392 connect_info->read_buffer = NULL;
|
|
1393
|
|
1394 connect_info->read_cb = s5_readchap;
|
|
1395
|
|
1396 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_WRITE,
|
|
1397 proxy_do_write, connect_info);
|
|
1398
|
|
1399 proxy_do_write(connect_info, connect_info->fd, GAIM_INPUT_WRITE);
|
|
1400
|
|
1401 return;
|
|
1402 } else {
|
|
1403 g_free(connect_info->read_buffer);
|
|
1404 connect_info->read_buffer = NULL;
|
|
1405
|
|
1406 s5_sendconnect(connect_info, connect_info->fd);
|
|
1407 }
|
|
1408 }
|
|
1409
|
|
1410 static void
|
|
1411 s5_canwrite(gpointer data, gint source, GaimInputCondition cond)
|
|
1412 {
|
|
1413 unsigned char buf[5];
|
|
1414 int i;
|
|
1415 GaimProxyConnectInfo *connect_info = data;
|
|
1416 socklen_t len;
|
|
1417 int error = ETIMEDOUT;
|
|
1418
|
|
1419 gaim_debug_info("socks5 proxy", "Connected.\n");
|
|
1420
|
|
1421 if (connect_info->inpa > 0)
|
|
1422 {
|
|
1423 gaim_input_remove(connect_info->inpa);
|
|
1424 connect_info->inpa = 0;
|
|
1425 }
|
|
1426
|
|
1427 len = sizeof(error);
|
|
1428 if (getsockopt(connect_info->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
|
|
1429 gaim_proxy_connect_info_disconnect(connect_info);
|
|
1430 try_connect(connect_info);
|
|
1431 return;
|
|
1432 }
|
|
1433
|
|
1434 i = 0;
|
|
1435 buf[0] = 0x05; /* SOCKS version 5 */
|
|
1436
|
|
1437 if (gaim_proxy_info_get_username(connect_info->gpi) != NULL) {
|
|
1438 buf[1] = 0x03; /* three methods */
|
|
1439 buf[2] = 0x00; /* no authentication */
|
|
1440 buf[3] = 0x03; /* CHAP authentication */
|
|
1441 buf[4] = 0x02; /* username/password authentication */
|
|
1442 i = 5;
|
|
1443 }
|
|
1444 else {
|
|
1445 buf[1] = 0x01;
|
|
1446 buf[2] = 0x00;
|
|
1447 i = 3;
|
|
1448 }
|
|
1449
|
|
1450 connect_info->write_buf_len = i;
|
|
1451 connect_info->write_buffer = g_malloc(connect_info->write_buf_len);
|
|
1452 memcpy(connect_info->write_buffer, buf, i);
|
|
1453
|
|
1454 connect_info->read_cb = s5_canread;
|
|
1455
|
|
1456 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_WRITE, proxy_do_write, connect_info);
|
|
1457 proxy_do_write(connect_info, connect_info->fd, GAIM_INPUT_WRITE);
|
|
1458 }
|
|
1459
|
|
1460 static int
|
|
1461 proxy_connect_socks5(GaimProxyConnectInfo *connect_info, struct sockaddr *addr, socklen_t addrlen)
|
|
1462 {
|
|
1463 gaim_debug_info("socks5 proxy",
|
|
1464 "Connecting to %s:%d via %s:%d using SOCKS5\n",
|
|
1465 connect_info->host, connect_info->port,
|
|
1466 gaim_proxy_info_get_host(connect_info->gpi),
|
|
1467 gaim_proxy_info_get_port(connect_info->gpi));
|
|
1468
|
|
1469 connect_info->fd = socket(addr->sa_family, SOCK_STREAM, 0);
|
|
1470 if (connect_info->fd < 0)
|
|
1471 return -1;
|
|
1472
|
|
1473 fcntl(connect_info->fd, F_SETFL, O_NONBLOCK);
|
|
1474 #ifndef _WIN32
|
|
1475 fcntl(connect_info->fd, F_SETFD, FD_CLOEXEC);
|
|
1476 #endif
|
|
1477
|
|
1478 if (connect(connect_info->fd, addr, addrlen) != 0)
|
|
1479 {
|
|
1480 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
|
1481 gaim_debug_info("socks5 proxy", "Connection in progress\n");
|
|
1482 connect_info->inpa = gaim_input_add(connect_info->fd, GAIM_INPUT_WRITE, s5_canwrite, connect_info);
|
|
1483 }
|
|
1484 else {
|
|
1485 close(connect_info->fd);
|
|
1486 connect_info->fd = -1;
|
|
1487 return -1;
|
|
1488 }
|
|
1489 }
|
|
1490 else {
|
|
1491 socklen_t len;
|
|
1492 int error = ETIMEDOUT;
|
|
1493
|
|
1494 gaim_debug_info("socks5 proxy", "Connected immediately.\n");
|
|
1495
|
|
1496 len = sizeof(error);
|
|
1497
|
|
1498 if (getsockopt(connect_info->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0)
|
|
1499 {
|
|
1500 close(connect_info->fd);
|
|
1501 connect_info->fd = -1;
|
|
1502 return -1;
|
|
1503 }
|
|
1504
|
|
1505 s5_canwrite(connect_info, connect_info->fd, GAIM_INPUT_WRITE);
|
|
1506 }
|
|
1507
|
|
1508 return connect_info->fd;
|
|
1509 }
|
|
1510
|
|
1511 /**
|
|
1512 * This function iterates through a list of IP addresses and attempts
|
|
1513 * to connect to each one. This is called after the hostname is
|
|
1514 * resolved, and if a connection attempt fails.
|
|
1515 */
|
|
1516 static void try_connect(GaimProxyConnectInfo *connect_info)
|
|
1517 {
|
|
1518 size_t addrlen;
|
|
1519 struct sockaddr *addr;
|
|
1520 int ret = -1;
|
|
1521
|
|
1522 if (connect_info->hosts == NULL)
|
|
1523 {
|
|
1524 gaim_proxy_connect_info_error(connect_info, _("Could not resolve host name"));
|
|
1525 return;
|
|
1526 }
|
|
1527
|
|
1528 while (connect_info->hosts)
|
|
1529 {
|
|
1530 addrlen = GPOINTER_TO_INT(connect_info->hosts->data);
|
|
1531 connect_info->hosts = g_slist_remove(connect_info->hosts, connect_info->hosts->data);
|
|
1532 addr = connect_info->hosts->data;
|
|
1533 connect_info->hosts = g_slist_remove(connect_info->hosts, connect_info->hosts->data);
|
|
1534
|
|
1535 switch (gaim_proxy_info_get_type(connect_info->gpi)) {
|
|
1536 case GAIM_PROXY_NONE:
|
|
1537 ret = proxy_connect_none(connect_info, addr, addrlen);
|
|
1538 break;
|
|
1539
|
|
1540 case GAIM_PROXY_HTTP:
|
|
1541 ret = proxy_connect_http(connect_info, addr, addrlen);
|
|
1542 break;
|
|
1543
|
|
1544 case GAIM_PROXY_SOCKS4:
|
|
1545 ret = proxy_connect_socks4(connect_info, addr, addrlen);
|
|
1546 break;
|
|
1547
|
|
1548 case GAIM_PROXY_SOCKS5:
|
|
1549 ret = proxy_connect_socks5(connect_info, addr, addrlen);
|
|
1550 break;
|
|
1551
|
|
1552 case GAIM_PROXY_USE_ENVVAR:
|
|
1553 ret = proxy_connect_http(connect_info, addr, addrlen);
|
|
1554 break;
|
|
1555
|
|
1556 default:
|
|
1557 break;
|
|
1558 }
|
|
1559
|
|
1560 g_free(addr);
|
|
1561
|
|
1562 if (ret >= 0)
|
|
1563 break;
|
|
1564 }
|
|
1565
|
|
1566 if (ret < 0) {
|
|
1567 gaim_proxy_connect_info_error(connect_info, _("Unable to establish a connection"));
|
|
1568 }
|
|
1569 }
|
|
1570
|
|
1571 static void
|
|
1572 connection_host_resolved(GSList *hosts, gpointer data,
|
|
1573 const char *error_message)
|
|
1574 {
|
|
1575 GaimProxyConnectInfo *connect_info;
|
|
1576
|
14238
|
1577 connect_info = data;
|
|
1578 connect_info->query_data = NULL;
|
|
1579
|
14192
|
1580 if (error_message != NULL)
|
|
1581 {
|
14238
|
1582 gchar *tmp;
|
|
1583 tmp = g_strdup_printf("Error while resolving hostname: %s\n", error_message);
|
|
1584 gaim_proxy_connect_info_error(connect_info, tmp);
|
|
1585 g_free(tmp);
|
|
1586 return;
|
14192
|
1587 }
|
|
1588
|
|
1589 connect_info->hosts = hosts;
|
|
1590
|
|
1591 try_connect(connect_info);
|
|
1592 }
|
|
1593
|
|
1594 GaimProxyInfo *
|
|
1595 gaim_proxy_get_setup(GaimAccount *account)
|
|
1596 {
|
|
1597 GaimProxyInfo *gpi;
|
|
1598 const gchar *tmp;
|
|
1599
|
|
1600 if (account && gaim_account_get_proxy_info(account) != NULL)
|
|
1601 gpi = gaim_account_get_proxy_info(account);
|
|
1602 else if (gaim_running_gnome())
|
|
1603 gpi = gaim_gnome_proxy_get_info();
|
|
1604 else
|
|
1605 gpi = gaim_global_proxy_get_info();
|
|
1606
|
|
1607 if (gaim_proxy_info_get_type(gpi) == GAIM_PROXY_USE_ENVVAR) {
|
|
1608 if ((tmp = g_getenv("HTTP_PROXY")) != NULL ||
|
|
1609 (tmp = g_getenv("http_proxy")) != NULL ||
|
|
1610 (tmp = g_getenv("HTTPPROXY")) != NULL) {
|
|
1611 char *proxyhost,*proxypath,*proxyuser,*proxypasswd;
|
|
1612 int proxyport;
|
|
1613
|
|
1614 /* http_proxy-format:
|
|
1615 * export http_proxy="http://user:passwd@your.proxy.server:port/"
|
|
1616 */
|
|
1617 if(gaim_url_parse(tmp, &proxyhost, &proxyport, &proxypath, &proxyuser, &proxypasswd)) {
|
|
1618 gaim_proxy_info_set_host(gpi, proxyhost);
|
|
1619 g_free(proxyhost);
|
|
1620 g_free(proxypath);
|
|
1621 if (proxyuser != NULL) {
|
|
1622 gaim_proxy_info_set_username(gpi, proxyuser);
|
|
1623 g_free(proxyuser);
|
|
1624 }
|
|
1625 if (proxypasswd != NULL) {
|
|
1626 gaim_proxy_info_set_password(gpi, proxypasswd);
|
|
1627 g_free(proxypasswd);
|
|
1628 }
|
|
1629
|
|
1630 /* only for backward compatibility */
|
|
1631 if (proxyport == 80 &&
|
|
1632 ((tmp = g_getenv("HTTP_PROXY_PORT")) != NULL ||
|
|
1633 (tmp = g_getenv("http_proxy_port")) != NULL ||
|
|
1634 (tmp = g_getenv("HTTPPROXYPORT")) != NULL))
|
|
1635 proxyport = atoi(tmp);
|
|
1636
|
|
1637 gaim_proxy_info_set_port(gpi, proxyport);
|
|
1638 }
|
|
1639 } else {
|
|
1640 /* no proxy environment variable found, don't use a proxy */
|
|
1641 gaim_debug_info("proxy", "No environment settings found, not using a proxy\n");
|
|
1642 gaim_proxy_info_set_type(gpi, GAIM_PROXY_NONE);
|
|
1643 }
|
|
1644
|
|
1645 /* XXX: Do we want to skip this step if user/password were part of url? */
|
|
1646 if ((tmp = g_getenv("HTTP_PROXY_USER")) != NULL ||
|
|
1647 (tmp = g_getenv("http_proxy_user")) != NULL ||
|
|
1648 (tmp = g_getenv("HTTPPROXYUSER")) != NULL)
|
|
1649 gaim_proxy_info_set_username(gpi, tmp);
|
|
1650
|
|
1651 if ((tmp = g_getenv("HTTP_PROXY_PASS")) != NULL ||
|
|
1652 (tmp = g_getenv("http_proxy_pass")) != NULL ||
|
|
1653 (tmp = g_getenv("HTTPPROXYPASS")) != NULL)
|
|
1654 gaim_proxy_info_set_password(gpi, tmp);
|
|
1655 }
|
|
1656
|
|
1657 return gpi;
|
|
1658 }
|
|
1659
|
|
1660 GaimProxyConnectInfo *
|
|
1661 gaim_proxy_connect(GaimAccount *account, const char *host, int port,
|
|
1662 GaimProxyConnectFunction connect_cb, gpointer data)
|
|
1663 {
|
|
1664 const char *connecthost = host;
|
|
1665 int connectport = port;
|
|
1666 GaimProxyConnectInfo *connect_info;
|
|
1667
|
|
1668 g_return_val_if_fail(host != NULL, NULL);
|
|
1669 g_return_val_if_fail(port > 0, NULL);
|
|
1670 g_return_val_if_fail(connect_cb != NULL, NULL);
|
|
1671
|
|
1672 connect_info = g_new0(GaimProxyConnectInfo, 1);
|
|
1673 connect_info->fd = -1;
|
|
1674 connect_info->connect_cb = connect_cb;
|
|
1675 connect_info->data = data;
|
|
1676 connect_info->host = g_strdup(host);
|
|
1677 connect_info->port = port;
|
|
1678 connect_info->gpi = gaim_proxy_get_setup(account);
|
|
1679
|
|
1680 if ((gaim_proxy_info_get_type(connect_info->gpi) != GAIM_PROXY_NONE) &&
|
|
1681 (gaim_proxy_info_get_host(connect_info->gpi) == NULL ||
|
|
1682 gaim_proxy_info_get_port(connect_info->gpi) <= 0)) {
|
|
1683
|
|
1684 gaim_notify_error(NULL, NULL, _("Invalid proxy settings"), _("Either the host name or port number specified for your given proxy type is invalid."));
|
|
1685 gaim_proxy_connect_info_destroy(connect_info);
|
|
1686 return NULL;
|
|
1687 }
|
|
1688
|
|
1689 switch (gaim_proxy_info_get_type(connect_info->gpi))
|
|
1690 {
|
|
1691 case GAIM_PROXY_NONE:
|
|
1692 break;
|
|
1693
|
|
1694 case GAIM_PROXY_HTTP:
|
|
1695 case GAIM_PROXY_SOCKS4:
|
|
1696 case GAIM_PROXY_SOCKS5:
|
|
1697 case GAIM_PROXY_USE_ENVVAR:
|
|
1698 connecthost = gaim_proxy_info_get_host(connect_info->gpi);
|
|
1699 connectport = gaim_proxy_info_get_port(connect_info->gpi);
|
|
1700 break;
|
|
1701
|
|
1702 default:
|
|
1703 gaim_proxy_connect_info_destroy(connect_info);
|
|
1704 return NULL;
|
|
1705 }
|
|
1706
|
|
1707 connect_info->query_data = gaim_dnsquery_a(connecthost,
|
|
1708 connectport, connection_host_resolved, connect_info);
|
|
1709 if (connect_info->query_data == NULL)
|
|
1710 {
|
|
1711 gaim_proxy_connect_info_destroy(connect_info);
|
|
1712 return NULL;
|
|
1713 }
|
|
1714
|
|
1715 connect_infos = g_slist_prepend(connect_infos, connect_info);
|
|
1716
|
|
1717 return connect_info;
|
|
1718 }
|
|
1719
|
|
1720 /*
|
|
1721 * Combine some of this code with gaim_proxy_connect()
|
|
1722 */
|
|
1723 GaimProxyConnectInfo *
|
|
1724 gaim_proxy_connect_socks5(GaimProxyInfo *gpi, const char *host, int port,
|
|
1725 GaimProxyConnectFunction connect_cb, gpointer data)
|
|
1726 {
|
|
1727 GaimProxyConnectInfo *connect_info;
|
|
1728
|
|
1729 g_return_val_if_fail(host != NULL, NULL);
|
|
1730 g_return_val_if_fail(port > 0, NULL);
|
|
1731 g_return_val_if_fail(connect_cb != NULL, NULL);
|
|
1732
|
|
1733 connect_info = g_new0(GaimProxyConnectInfo, 1);
|
|
1734 connect_info->fd = -1;
|
|
1735 connect_info->connect_cb = connect_cb;
|
|
1736 connect_info->data = data;
|
|
1737 connect_info->host = g_strdup(host);
|
|
1738 connect_info->port = port;
|
|
1739 connect_info->gpi = gpi;
|
|
1740
|
|
1741 connect_info->query_data =
|
|
1742 gaim_dnsquery_a(gaim_proxy_info_get_host(gpi),
|
|
1743 gaim_proxy_info_get_port(gpi),
|
|
1744 connection_host_resolved, connect_info);
|
|
1745 if (connect_info->query_data == NULL)
|
|
1746 {
|
|
1747 gaim_proxy_connect_info_destroy(connect_info);
|
|
1748 return NULL;
|
|
1749 }
|
|
1750
|
|
1751 connect_infos = g_slist_prepend(connect_infos, connect_info);
|
|
1752
|
|
1753 return connect_info;
|
|
1754 }
|
|
1755
|
|
1756 void
|
|
1757 gaim_proxy_connect_cancel(GaimProxyConnectInfo *connect_info)
|
|
1758 {
|
|
1759 gaim_proxy_connect_info_destroy(connect_info);
|
|
1760 }
|
|
1761
|
|
1762 static void
|
|
1763 proxy_pref_cb(const char *name, GaimPrefType type,
|
|
1764 gconstpointer value, gpointer data)
|
|
1765 {
|
|
1766 GaimProxyInfo *info = gaim_global_proxy_get_info();
|
|
1767
|
|
1768 if (!strcmp(name, "/core/proxy/type")) {
|
|
1769 int proxytype;
|
|
1770 const char *type = value;
|
|
1771
|
|
1772 if (!strcmp(type, "none"))
|
|
1773 proxytype = GAIM_PROXY_NONE;
|
|
1774 else if (!strcmp(type, "http"))
|
|
1775 proxytype = GAIM_PROXY_HTTP;
|
|
1776 else if (!strcmp(type, "socks4"))
|
|
1777 proxytype = GAIM_PROXY_SOCKS4;
|
|
1778 else if (!strcmp(type, "socks5"))
|
|
1779 proxytype = GAIM_PROXY_SOCKS5;
|
|
1780 else if (!strcmp(type, "envvar"))
|
|
1781 proxytype = GAIM_PROXY_USE_ENVVAR;
|
|
1782 else
|
|
1783 proxytype = -1;
|
|
1784
|
|
1785 gaim_proxy_info_set_type(info, proxytype);
|
|
1786 } else if (!strcmp(name, "/core/proxy/host"))
|
|
1787 gaim_proxy_info_set_host(info, value);
|
|
1788 else if (!strcmp(name, "/core/proxy/port"))
|
|
1789 gaim_proxy_info_set_port(info, GPOINTER_TO_INT(value));
|
|
1790 else if (!strcmp(name, "/core/proxy/username"))
|
|
1791 gaim_proxy_info_set_username(info, value);
|
|
1792 else if (!strcmp(name, "/core/proxy/password"))
|
|
1793 gaim_proxy_info_set_password(info, value);
|
|
1794 }
|
|
1795
|
|
1796 void *
|
|
1797 gaim_proxy_get_handle()
|
|
1798 {
|
|
1799 static int handle;
|
|
1800
|
|
1801 return &handle;
|
|
1802 }
|
|
1803
|
|
1804 void
|
|
1805 gaim_proxy_init(void)
|
|
1806 {
|
|
1807 void *handle;
|
|
1808
|
|
1809 /* Initialize a default proxy info struct. */
|
|
1810 global_proxy_info = gaim_proxy_info_new();
|
|
1811
|
|
1812 /* Proxy */
|
|
1813 gaim_prefs_add_none("/core/proxy");
|
|
1814 gaim_prefs_add_string("/core/proxy/type", "none");
|
|
1815 gaim_prefs_add_string("/core/proxy/host", "");
|
|
1816 gaim_prefs_add_int("/core/proxy/port", 0);
|
|
1817 gaim_prefs_add_string("/core/proxy/username", "");
|
|
1818 gaim_prefs_add_string("/core/proxy/password", "");
|
|
1819
|
|
1820 /* Setup callbacks for the preferences. */
|
|
1821 handle = gaim_proxy_get_handle();
|
|
1822 gaim_prefs_connect_callback(handle, "/core/proxy/type", proxy_pref_cb,
|
|
1823 NULL);
|
|
1824 gaim_prefs_connect_callback(handle, "/core/proxy/host", proxy_pref_cb,
|
|
1825 NULL);
|
|
1826 gaim_prefs_connect_callback(handle, "/core/proxy/port", proxy_pref_cb,
|
|
1827 NULL);
|
|
1828 gaim_prefs_connect_callback(handle, "/core/proxy/username",
|
|
1829 proxy_pref_cb, NULL);
|
|
1830 gaim_prefs_connect_callback(handle, "/core/proxy/password",
|
|
1831 proxy_pref_cb, NULL);
|
|
1832 }
|
|
1833
|
|
1834 void
|
|
1835 gaim_proxy_uninit(void)
|
|
1836 {
|
|
1837 while (connect_infos != NULL)
|
|
1838 gaim_proxy_connect_info_destroy(connect_infos->data);
|
|
1839 }
|