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
|
14262
|
42 struct _GaimProxyConnectData {
|
14192
|
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 */
|
14262
|
90 static GSList *connect_datas = NULL;
|
14192
|
91
|
14262
|
92 static void try_connect(GaimProxyConnectData *connect_data);
|
14192
|
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
|
14262
|
284 gaim_proxy_connect_data_disconnect(GaimProxyConnectData *connect_data)
|
14192
|
285 {
|
14262
|
286 if (connect_data->inpa > 0)
|
14192
|
287 {
|
14262
|
288 gaim_input_remove(connect_data->inpa);
|
|
289 connect_data->inpa = 0;
|
14192
|
290 }
|
|
291
|
14262
|
292 if (connect_data->fd >= 0)
|
14192
|
293 {
|
14262
|
294 close(connect_data->fd);
|
|
295 connect_data->fd = -1;
|
14192
|
296 }
|
|
297
|
14262
|
298 g_free(connect_data->write_buffer);
|
|
299 connect_data->write_buffer = NULL;
|
14192
|
300
|
14262
|
301 g_free(connect_data->read_buffer);
|
|
302 connect_data->read_buffer = NULL;
|
14192
|
303 }
|
|
304
|
|
305 static void
|
14262
|
306 gaim_proxy_connect_data_destroy(GaimProxyConnectData *connect_data)
|
14192
|
307 {
|
14262
|
308 gaim_proxy_connect_data_disconnect(connect_data);
|
14192
|
309
|
14262
|
310 connect_datas = g_slist_remove(connect_datas, connect_data);
|
14192
|
311
|
14262
|
312 if (connect_data->query_data != NULL)
|
|
313 gaim_dnsquery_destroy(connect_data->query_data);
|
14238
|
314
|
14262
|
315 while (connect_data->hosts != NULL)
|
14192
|
316 {
|
|
317 /* Discard the length... */
|
14262
|
318 connect_data->hosts = g_slist_remove(connect_data->hosts, connect_data->hosts->data);
|
14192
|
319 /* Free the address... */
|
14262
|
320 g_free(connect_data->hosts->data);
|
|
321 connect_data->hosts = g_slist_remove(connect_data->hosts, connect_data->hosts->data);
|
14192
|
322 }
|
|
323
|
14262
|
324 g_free(connect_data->host);
|
|
325 g_free(connect_data);
|
14192
|
326 }
|
|
327
|
|
328 static void
|
14262
|
329 gaim_proxy_connect_data_connected(GaimProxyConnectData *connect_data)
|
14192
|
330 {
|
14262
|
331 connect_data->connect_cb(connect_data->data, connect_data->fd, NULL);
|
14192
|
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
|
14262
|
336 * we destroy the connect_data.
|
14192
|
337 */
|
14262
|
338 connect_data->fd = -1;
|
14192
|
339
|
14262
|
340 gaim_proxy_connect_data_destroy(connect_data);
|
14192
|
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
|
14356
|
353 gaim_proxy_connect_data_error(GaimProxyConnectData *connect_data, const char *format, ...)
|
14192
|
354 {
|
14356
|
355 gchar *error_message;
|
|
356 va_list args;
|
|
357
|
|
358 va_start(args, format);
|
|
359 error_message = g_strdup_vprintf(format, args);
|
|
360 va_end(args);
|
|
361
|
14262
|
362 connect_data->connect_cb(connect_data->data, -1, error_message);
|
14356
|
363 g_free(error_message);
|
|
364
|
14262
|
365 gaim_proxy_connect_data_destroy(connect_data);
|
14192
|
366 }
|
|
367
|
|
368 static void
|
|
369 no_one_calls(gpointer data, gint source, GaimInputCondition cond)
|
|
370 {
|
14262
|
371 GaimProxyConnectData *connect_data = data;
|
14192
|
372 socklen_t len;
|
|
373 int error=0, ret;
|
|
374
|
|
375 gaim_debug_info("proxy", "Connected.\n");
|
|
376
|
|
377 len = sizeof(error);
|
|
378
|
|
379 /*
|
|
380 * getsockopt after a non-blocking connect returns -1 if something is
|
|
381 * really messed up (bad descriptor, usually). Otherwise, it returns 0 and
|
|
382 * error holds what connect would have returned if it blocked until now.
|
|
383 * Thus, error == 0 is success, error == EINPROGRESS means "try again",
|
|
384 * and anything else is a real error.
|
|
385 *
|
|
386 * (error == EINPROGRESS can happen after a select because the kernel can
|
|
387 * be overly optimistic sometimes. select is just a hint that you might be
|
|
388 * able to do something.)
|
|
389 */
|
14262
|
390 ret = getsockopt(connect_data->fd, SOL_SOCKET, SO_ERROR, &error, &len);
|
14192
|
391 if (ret == 0 && error == EINPROGRESS)
|
|
392 return; /* we'll be called again later */
|
|
393 if (ret < 0 || error != 0) {
|
|
394 if (ret!=0)
|
|
395 error = errno;
|
|
396
|
|
397 gaim_debug_error("proxy",
|
|
398 "getsockopt SO_ERROR check: %s\n", strerror(error));
|
|
399
|
14262
|
400 gaim_proxy_connect_data_disconnect(connect_data);
|
|
401 try_connect(connect_data);
|
14192
|
402 return;
|
|
403 }
|
|
404
|
14262
|
405 gaim_input_remove(connect_data->inpa);
|
|
406 connect_data->inpa = 0;
|
14192
|
407
|
14262
|
408 gaim_proxy_connect_data_connected(connect_data);
|
14192
|
409 }
|
|
410
|
|
411 static gboolean
|
|
412 clean_connect(gpointer data)
|
|
413 {
|
14262
|
414 GaimProxyConnectData *connect_data;
|
14192
|
415
|
14262
|
416 connect_data = data;
|
|
417 gaim_proxy_connect_data_connected(connect_data);
|
14192
|
418
|
|
419 return FALSE;
|
|
420 }
|
|
421
|
|
422 static int
|
14262
|
423 proxy_connect_none(GaimProxyConnectData *connect_data, struct sockaddr *addr, socklen_t addrlen)
|
14192
|
424 {
|
|
425 gaim_debug_info("proxy", "Connecting to %s:%d with no proxy\n",
|
14262
|
426 connect_data->host, connect_data->port);
|
14192
|
427
|
14262
|
428 connect_data->fd = socket(addr->sa_family, SOCK_STREAM, 0);
|
|
429 if (connect_data->fd < 0)
|
14192
|
430 {
|
|
431 gaim_debug_error("proxy",
|
|
432 "Unable to create socket: %s\n", strerror(errno));
|
|
433 return -1;
|
|
434 }
|
14262
|
435 fcntl(connect_data->fd, F_SETFL, O_NONBLOCK);
|
14192
|
436 #ifndef _WIN32
|
14262
|
437 fcntl(connect_data->fd, F_SETFD, FD_CLOEXEC);
|
14192
|
438 #endif
|
|
439
|
14262
|
440 if (connect(connect_data->fd, (struct sockaddr *)addr, addrlen) != 0)
|
14192
|
441 {
|
|
442 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
|
443 gaim_debug_info("proxy", "Connection in progress\n");
|
14262
|
444 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_WRITE, no_one_calls, connect_data);
|
14192
|
445 }
|
|
446 else {
|
|
447 gaim_debug_error("proxy",
|
|
448 "Connect failed: %s\n", strerror(errno));
|
14262
|
449 close(connect_data->fd);
|
|
450 connect_data->fd = -1;
|
14192
|
451 return -1;
|
|
452 }
|
|
453 }
|
|
454 else
|
|
455 {
|
|
456 /*
|
|
457 * The connection happened IMMEDIATELY... strange, but whatever.
|
|
458 */
|
|
459 socklen_t len;
|
|
460 int error = ETIMEDOUT;
|
|
461 gaim_debug_info("proxy", "Connected immediately.\n");
|
|
462 len = sizeof(error);
|
14262
|
463 if (getsockopt(connect_data->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0)
|
14192
|
464 {
|
|
465 gaim_debug_error("proxy", "getsockopt failed.\n");
|
14262
|
466 close(connect_data->fd);
|
|
467 connect_data->fd = -1;
|
14192
|
468 return -1;
|
|
469 }
|
|
470
|
|
471 /*
|
|
472 * We want to call the "connected" callback eventually, but we
|
|
473 * don't want to call it before we return, just in case.
|
|
474 */
|
14262
|
475 gaim_timeout_add(10, clean_connect, connect_data);
|
14192
|
476 }
|
|
477
|
14262
|
478 return connect_data->fd;
|
14192
|
479 }
|
|
480
|
|
481 static void
|
|
482 proxy_do_write(gpointer data, gint source, GaimInputCondition cond)
|
|
483 {
|
14262
|
484 GaimProxyConnectData *connect_data = data;
|
|
485 const guchar *request = connect_data->write_buffer + connect_data->written_len;
|
|
486 gsize request_len = connect_data->write_buf_len - connect_data->written_len;
|
14192
|
487
|
14262
|
488 int ret = write(connect_data->fd, request, request_len);
|
14192
|
489
|
|
490 if(ret < 0 && errno == EAGAIN)
|
|
491 return;
|
|
492 else if(ret < 0) {
|
14262
|
493 gaim_proxy_connect_data_disconnect(connect_data);
|
|
494 try_connect(connect_data);
|
14192
|
495 return;
|
|
496 } else if (ret < request_len) {
|
14262
|
497 connect_data->written_len += ret;
|
14192
|
498 return;
|
|
499 }
|
|
500
|
14262
|
501 gaim_input_remove(connect_data->inpa);
|
|
502 g_free(connect_data->write_buffer);
|
|
503 connect_data->write_buffer = NULL;
|
14192
|
504
|
|
505 /* register the response handler for the response */
|
14262
|
506 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_READ, connect_data->read_cb, connect_data);
|
14192
|
507 }
|
|
508
|
|
509 #define HTTP_GOODSTRING "HTTP/1.0 200"
|
|
510 #define HTTP_GOODSTRING2 "HTTP/1.1 200"
|
|
511
|
|
512 /* read the response to the CONNECT request, if we are requesting a non-port-80 tunnel */
|
|
513 static void
|
|
514 http_canread(gpointer data, gint source, GaimInputCondition cond)
|
|
515 {
|
|
516 int len, headers_len, status = 0;
|
|
517 gboolean error;
|
14262
|
518 GaimProxyConnectData *connect_data = data;
|
14192
|
519 guchar *p;
|
|
520 gsize max_read;
|
|
521
|
14262
|
522 if(connect_data->read_buffer == NULL) {
|
|
523 connect_data->read_buf_len = 8192;
|
|
524 connect_data->read_buffer = g_malloc(connect_data->read_buf_len);
|
|
525 connect_data->read_len = 0;
|
14192
|
526 }
|
|
527
|
14262
|
528 p = connect_data->read_buffer + connect_data->read_len;
|
|
529 max_read = connect_data->read_buf_len - connect_data->read_len - 1;
|
14192
|
530
|
14262
|
531 len = read(connect_data->fd, p, max_read);
|
14192
|
532 if(len < 0 && errno == EAGAIN)
|
|
533 return;
|
|
534 else if(len <= 0) {
|
14262
|
535 gaim_proxy_connect_data_error(connect_data, _("Lost connection with server for an unknown reason."));
|
14192
|
536 return;
|
|
537 } else {
|
14262
|
538 connect_data->read_len += len;
|
14192
|
539 }
|
|
540 p[len] = '\0';
|
|
541
|
14262
|
542 if((p = (guchar *)g_strstr_len((const gchar *)connect_data->read_buffer, connect_data->read_len, "\r\n\r\n"))) {
|
14192
|
543 *p = '\0';
|
14262
|
544 headers_len = (p - connect_data->read_buffer) + 4;
|
14192
|
545 } else if(len == max_read)
|
|
546 headers_len = len;
|
|
547 else
|
|
548 return;
|
|
549
|
14262
|
550 error = strncmp((const char *)connect_data->read_buffer, "HTTP/", 5) != 0;
|
14192
|
551 if (!error)
|
|
552 {
|
|
553 int major;
|
14262
|
554 p = connect_data->read_buffer + 5;
|
14192
|
555 major = strtol((const char *)p, (char **)&p, 10);
|
|
556 error = (major == 0) || (*p != '.');
|
|
557 if(!error) {
|
|
558 int minor;
|
|
559 p++;
|
|
560 minor = strtol((const char *)p, (char **)&p, 10);
|
|
561 error = (*p != ' ');
|
|
562 if(!error) {
|
|
563 p++;
|
|
564 status = strtol((const char *)p, (char **)&p, 10);
|
|
565 error = (*p != ' ');
|
|
566 }
|
|
567 }
|
|
568 }
|
|
569
|
|
570 /* Read the contents */
|
14262
|
571 p = (guchar *)g_strrstr((const gchar *)connect_data->read_buffer, "Content-Length: ");
|
14192
|
572 if (p != NULL)
|
|
573 {
|
|
574 gchar *tmp;
|
|
575 int len = 0;
|
|
576 char tmpc;
|
|
577 p += strlen("Content-Length: ");
|
|
578 tmp = strchr((const char *)p, '\r');
|
|
579 if(tmp)
|
|
580 *tmp = '\0';
|
|
581 len = atoi((const char *)p);
|
|
582 if(tmp)
|
|
583 *tmp = '\r';
|
|
584
|
|
585 /* Compensate for what has already been read */
|
14262
|
586 len -= connect_data->read_len - headers_len;
|
14192
|
587 /* I'm assuming that we're doing this to prevent the server from
|
|
588 complaining / breaking since we don't read the whole page */
|
|
589 while(len--) {
|
|
590 /* TODO: deal with EAGAIN (and other errors) better */
|
14262
|
591 if (read(connect_data->fd, &tmpc, 1) < 0 && errno != EAGAIN)
|
14192
|
592 break;
|
|
593 }
|
|
594 }
|
|
595
|
|
596 if (error)
|
|
597 {
|
14356
|
598 gaim_proxy_connect_data_error(connect_data,
|
|
599 _("Unable to parse response from HTTP proxy: %s\n"),
|
14262
|
600 connect_data->read_buffer);
|
14192
|
601 return;
|
|
602 }
|
|
603 else if (status != 200)
|
|
604 {
|
|
605 gaim_debug_error("proxy",
|
|
606 "Proxy server replied with:\n%s\n",
|
14262
|
607 connect_data->read_buffer);
|
14192
|
608
|
|
609
|
|
610 if(status == 407 /* Proxy Auth */) {
|
|
611 gchar *ntlm;
|
14262
|
612 if((ntlm = g_strrstr((const gchar *)connect_data->read_buffer, "Proxy-Authenticate: NTLM "))) { /* Check for Type-2 */
|
14192
|
613 gchar *tmp = ntlm;
|
|
614 guint8 *nonce;
|
14262
|
615 gchar *domain = (gchar*)gaim_proxy_info_get_username(connect_data->gpi);
|
14192
|
616 gchar *username;
|
|
617 gchar *request;
|
|
618 gchar *response;
|
|
619 username = strchr(domain, '\\');
|
|
620 if (username == NULL)
|
|
621 {
|
14356
|
622 gaim_proxy_connect_data_error(connect_data,
|
|
623 _("HTTP proxy connection error %d"), status);
|
14192
|
624 return;
|
|
625 }
|
|
626 *username = '\0';
|
|
627 username++;
|
|
628 ntlm += strlen("Proxy-Authenticate: NTLM ");
|
|
629 while(*tmp != '\r' && *tmp != '\0') tmp++;
|
|
630 *tmp = '\0';
|
|
631 nonce = gaim_ntlm_parse_type2(ntlm, NULL);
|
|
632 response = gaim_ntlm_gen_type3(username,
|
14262
|
633 (gchar*) gaim_proxy_info_get_password(connect_data->gpi),
|
|
634 (gchar*) gaim_proxy_info_get_host(connect_data->gpi),
|
14192
|
635 domain, nonce, NULL);
|
|
636 username--;
|
|
637 *username = '\\';
|
|
638 request = g_strdup_printf(
|
|
639 "CONNECT %s:%d HTTP/1.1\r\n"
|
|
640 "Host: %s:%d\r\n"
|
|
641 "Proxy-Authorization: NTLM %s\r\n"
|
|
642 "Proxy-Connection: Keep-Alive\r\n\r\n",
|
14262
|
643 connect_data->host, connect_data->port, connect_data->host,
|
|
644 connect_data->port, response);
|
14192
|
645 g_free(response);
|
|
646
|
14262
|
647 gaim_input_remove(connect_data->inpa);
|
|
648 g_free(connect_data->read_buffer);
|
|
649 connect_data->read_buffer = NULL;
|
14192
|
650
|
14262
|
651 connect_data->write_buffer = (guchar *)request;
|
|
652 connect_data->write_buf_len = strlen(request);
|
|
653 connect_data->written_len = 0;
|
14192
|
654
|
14262
|
655 connect_data->read_cb = http_canread;
|
14192
|
656
|
14262
|
657 connect_data->inpa = gaim_input_add(connect_data->fd,
|
|
658 GAIM_INPUT_WRITE, proxy_do_write, connect_data);
|
14192
|
659
|
14262
|
660 proxy_do_write(connect_data, connect_data->fd, cond);
|
14192
|
661 return;
|
14262
|
662 } else if((ntlm = g_strrstr((const char *)connect_data->read_buffer, "Proxy-Authenticate: NTLM"))) { /* Empty message */
|
14192
|
663 gchar request[2048];
|
14262
|
664 gchar *domain = (gchar*) gaim_proxy_info_get_username(connect_data->gpi);
|
14192
|
665 gchar *username;
|
|
666 int request_len;
|
|
667 username = strchr(domain, '\\');
|
|
668 if (username == NULL)
|
|
669 {
|
14356
|
670 gaim_proxy_connect_data_error(connect_data,
|
|
671 _("HTTP proxy connection error %d"), status);
|
14192
|
672 return;
|
|
673 }
|
|
674 *username = '\0';
|
|
675
|
|
676 request_len = g_snprintf(request, sizeof(request),
|
|
677 "CONNECT %s:%d HTTP/1.1\r\n"
|
|
678 "Host: %s:%d\r\n",
|
14262
|
679 connect_data->host, connect_data->port,
|
|
680 connect_data->host, connect_data->port);
|
14192
|
681
|
|
682 g_return_if_fail(request_len < sizeof(request));
|
|
683 request_len += g_snprintf(request + request_len,
|
|
684 sizeof(request) - request_len,
|
|
685 "Proxy-Authorization: NTLM %s\r\n"
|
|
686 "Proxy-Connection: Keep-Alive\r\n\r\n",
|
|
687 gaim_ntlm_gen_type1(
|
14262
|
688 (gchar*) gaim_proxy_info_get_host(connect_data->gpi),
|
14192
|
689 domain));
|
|
690 *username = '\\';
|
|
691
|
14262
|
692 gaim_input_remove(connect_data->inpa);
|
|
693 g_free(connect_data->read_buffer);
|
|
694 connect_data->read_buffer = NULL;
|
14192
|
695
|
14262
|
696 connect_data->write_buffer = g_memdup(request, request_len);
|
|
697 connect_data->write_buf_len = request_len;
|
|
698 connect_data->written_len = 0;
|
14192
|
699
|
14262
|
700 connect_data->read_cb = http_canread;
|
14192
|
701
|
14262
|
702 connect_data->inpa = gaim_input_add(connect_data->fd,
|
|
703 GAIM_INPUT_WRITE, proxy_do_write, connect_data);
|
14192
|
704
|
14262
|
705 proxy_do_write(connect_data, connect_data->fd, cond);
|
14192
|
706 return;
|
|
707 } else {
|
14356
|
708 gaim_proxy_connect_data_error(connect_data,
|
|
709 _("HTTP proxy connection error %d"), status);
|
14192
|
710 return;
|
|
711 }
|
|
712 }
|
|
713 if(status == 403 /* Forbidden */ ) {
|
14356
|
714 gaim_proxy_connect_data_error(connect_data,
|
|
715 _("Access denied: HTTP proxy server forbids port %d tunnelling."),
|
|
716 connect_data->port);
|
14192
|
717 } else {
|
14356
|
718 gaim_proxy_connect_data_error(connect_data,
|
|
719 _("HTTP proxy connection error %d"), status);
|
14192
|
720 }
|
|
721 } else {
|
14262
|
722 gaim_input_remove(connect_data->inpa);
|
|
723 connect_data->inpa = 0;
|
|
724 g_free(connect_data->read_buffer);
|
|
725 connect_data->read_buffer = NULL;
|
14192
|
726 gaim_debug_info("proxy", "HTTP proxy connection established\n");
|
14262
|
727 gaim_proxy_connect_data_connected(connect_data);
|
14192
|
728 return;
|
|
729 }
|
|
730 }
|
|
731
|
|
732
|
|
733
|
|
734 static void
|
|
735 http_canwrite(gpointer data, gint source, GaimInputCondition cond)
|
|
736 {
|
|
737 char request[8192];
|
|
738 int request_len = 0;
|
14262
|
739 GaimProxyConnectData *connect_data = data;
|
14192
|
740 socklen_t len;
|
|
741 int error = ETIMEDOUT;
|
|
742
|
|
743 gaim_debug_info("http proxy", "Connected.\n");
|
|
744
|
14262
|
745 if (connect_data->inpa > 0)
|
14192
|
746 {
|
14262
|
747 gaim_input_remove(connect_data->inpa);
|
|
748 connect_data->inpa = 0;
|
14192
|
749 }
|
|
750
|
|
751 len = sizeof(error);
|
|
752
|
14262
|
753 if (getsockopt(connect_data->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
|
|
754 gaim_proxy_connect_data_disconnect(connect_data);
|
|
755 try_connect(connect_data);
|
14192
|
756 return;
|
|
757 }
|
|
758
|
|
759 gaim_debug_info("proxy", "using CONNECT tunnelling for %s:%d\n",
|
14262
|
760 connect_data->host, connect_data->port);
|
14192
|
761 request_len = g_snprintf(request, sizeof(request),
|
|
762 "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n",
|
14262
|
763 connect_data->host, connect_data->port, connect_data->host, connect_data->port);
|
14192
|
764
|
14262
|
765 if (gaim_proxy_info_get_username(connect_data->gpi) != NULL) {
|
14192
|
766 char *t1, *t2;
|
|
767 t1 = g_strdup_printf("%s:%s",
|
14262
|
768 gaim_proxy_info_get_username(connect_data->gpi),
|
|
769 gaim_proxy_info_get_password(connect_data->gpi) ?
|
|
770 gaim_proxy_info_get_password(connect_data->gpi) : "");
|
14192
|
771 t2 = gaim_base64_encode((const guchar *)t1, strlen(t1));
|
|
772 g_free(t1);
|
|
773 g_return_if_fail(request_len < sizeof(request));
|
|
774
|
|
775 request_len += g_snprintf(request + request_len,
|
|
776 sizeof(request) - request_len,
|
|
777 "Proxy-Authorization: Basic %s\r\n"
|
|
778 "Proxy-Authorization: NTLM %s\r\n"
|
|
779 "Proxy-Connection: Keep-Alive\r\n", t2,
|
|
780 gaim_ntlm_gen_type1(
|
14262
|
781 (gchar*)gaim_proxy_info_get_host(connect_data->gpi),""));
|
14192
|
782 g_free(t2);
|
|
783 }
|
|
784
|
|
785 g_return_if_fail(request_len < sizeof(request));
|
|
786 strcpy(request + request_len, "\r\n");
|
|
787 request_len += 2;
|
14262
|
788 connect_data->write_buffer = g_memdup(request, request_len);
|
|
789 connect_data->write_buf_len = request_len;
|
|
790 connect_data->written_len = 0;
|
14192
|
791
|
14262
|
792 connect_data->read_cb = http_canread;
|
14192
|
793
|
14262
|
794 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_WRITE, proxy_do_write,
|
|
795 connect_data);
|
14192
|
796
|
14262
|
797 proxy_do_write(connect_data, connect_data->fd, cond);
|
14192
|
798 }
|
|
799
|
|
800 static int
|
14262
|
801 proxy_connect_http(GaimProxyConnectData *connect_data, struct sockaddr *addr, socklen_t addrlen)
|
14192
|
802 {
|
|
803 gaim_debug_info("http proxy",
|
|
804 "Connecting to %s:%d via %s:%d using HTTP\n",
|
14262
|
805 (connect_data->host ? connect_data->host : "(null)"), connect_data->port,
|
|
806 (gaim_proxy_info_get_host(connect_data->gpi) ? gaim_proxy_info_get_host(connect_data->gpi) : "(null)"),
|
|
807 gaim_proxy_info_get_port(connect_data->gpi));
|
14192
|
808
|
14262
|
809 connect_data->fd = socket(addr->sa_family, SOCK_STREAM, 0);
|
|
810 if (connect_data->fd < 0)
|
14192
|
811 return -1;
|
|
812
|
14262
|
813 fcntl(connect_data->fd, F_SETFL, O_NONBLOCK);
|
14192
|
814 #ifndef _WIN32
|
14262
|
815 fcntl(connect_data->fd, F_SETFD, FD_CLOEXEC);
|
14192
|
816 #endif
|
|
817
|
14262
|
818 if (connect(connect_data->fd, addr, addrlen) != 0)
|
14192
|
819 {
|
|
820 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
|
821 gaim_debug_info("http proxy", "Connection in progress\n");
|
|
822
|
14262
|
823 if (connect_data->port != 80) {
|
14192
|
824 /* we need to do CONNECT first */
|
14262
|
825 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_WRITE,
|
|
826 http_canwrite, connect_data);
|
14192
|
827 } else {
|
|
828 gaim_debug_info("proxy", "HTTP proxy connection established\n");
|
14262
|
829 gaim_proxy_connect_data_connected(connect_data);
|
14192
|
830 }
|
|
831 } else {
|
14262
|
832 close(connect_data->fd);
|
|
833 connect_data->fd = -1;
|
14192
|
834 return -1;
|
|
835 }
|
|
836 }
|
|
837 else {
|
|
838 socklen_t len;
|
|
839 int error = ETIMEDOUT;
|
|
840
|
|
841 gaim_debug_info("http proxy", "Connected immediately.\n");
|
|
842
|
|
843 len = sizeof(error);
|
14262
|
844 if (getsockopt(connect_data->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0)
|
14192
|
845 {
|
14262
|
846 close(connect_data->fd);
|
|
847 connect_data->fd = -1;
|
14192
|
848 return -1;
|
|
849 }
|
14262
|
850 http_canwrite(connect_data, connect_data->fd, GAIM_INPUT_WRITE);
|
14192
|
851 }
|
|
852
|
14262
|
853 return connect_data->fd;
|
14192
|
854 }
|
|
855
|
|
856
|
|
857 static void
|
|
858 s4_canread(gpointer data, gint source, GaimInputCondition cond)
|
|
859 {
|
14262
|
860 GaimProxyConnectData *connect_data = data;
|
14192
|
861 guchar *buf;
|
|
862 int len, max_read;
|
|
863
|
|
864 /* This is really not going to block under normal circumstances, but to
|
|
865 * be correct, we deal with the unlikely scenario */
|
|
866
|
14262
|
867 if (connect_data->read_buffer == NULL) {
|
|
868 connect_data->read_buf_len = 12;
|
|
869 connect_data->read_buffer = g_malloc(connect_data->read_buf_len);
|
|
870 connect_data->read_len = 0;
|
14192
|
871 }
|
|
872
|
14262
|
873 buf = connect_data->read_buffer + connect_data->read_len;
|
|
874 max_read = connect_data->read_buf_len - connect_data->read_len;
|
14192
|
875
|
14262
|
876 len = read(connect_data->fd, buf, max_read);
|
14192
|
877
|
14262
|
878 if ((len < 0 && errno == EAGAIN) || (len > 0 && len + connect_data->read_len < 4))
|
14192
|
879 return;
|
14262
|
880 else if (len + connect_data->read_len >= 4) {
|
|
881 if (connect_data->read_buffer[1] == 90) {
|
|
882 gaim_proxy_connect_data_connected(connect_data);
|
14192
|
883 return;
|
|
884 }
|
|
885 }
|
|
886
|
14262
|
887 gaim_proxy_connect_data_disconnect(connect_data);
|
|
888 try_connect(connect_data);
|
14192
|
889 }
|
|
890
|
|
891 static void
|
|
892 s4_canwrite(gpointer data, gint source, GaimInputCondition cond)
|
|
893 {
|
|
894 unsigned char packet[9];
|
|
895 struct hostent *hp;
|
14262
|
896 GaimProxyConnectData *connect_data = data;
|
14192
|
897 socklen_t len;
|
|
898 int error = ETIMEDOUT;
|
|
899
|
|
900 gaim_debug_info("socks4 proxy", "Connected.\n");
|
|
901
|
14262
|
902 if (connect_data->inpa > 0)
|
14192
|
903 {
|
14262
|
904 gaim_input_remove(connect_data->inpa);
|
|
905 connect_data->inpa = 0;
|
14192
|
906 }
|
|
907
|
|
908 len = sizeof(error);
|
|
909
|
14262
|
910 if (getsockopt(connect_data->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
|
|
911 gaim_proxy_connect_data_disconnect(connect_data);
|
|
912 try_connect(connect_data);
|
14192
|
913 return;
|
|
914 }
|
|
915
|
|
916 /*
|
|
917 * The socks4 spec doesn't include support for doing host name
|
|
918 * lookups by the proxy. Some socks4 servers do this via
|
|
919 * extensions to the protocol. Since we don't know if a
|
|
920 * server supports this, it would need to be implemented
|
|
921 * with an option, or some detection mechanism - in the
|
|
922 * meantime, stick with plain old SOCKS4.
|
|
923 */
|
|
924 /* TODO: This needs to be non-blocking! */
|
14262
|
925 hp = gethostbyname(connect_data->host);
|
14192
|
926 if (hp == NULL) {
|
14262
|
927 gaim_proxy_connect_data_disconnect(connect_data);
|
|
928 try_connect(connect_data);
|
14192
|
929 return;
|
|
930 }
|
|
931
|
|
932 packet[0] = 4;
|
|
933 packet[1] = 1;
|
14262
|
934 packet[2] = connect_data->port >> 8;
|
|
935 packet[3] = connect_data->port & 0xff;
|
14192
|
936 packet[4] = (unsigned char)(hp->h_addr_list[0])[0];
|
|
937 packet[5] = (unsigned char)(hp->h_addr_list[0])[1];
|
|
938 packet[6] = (unsigned char)(hp->h_addr_list[0])[2];
|
|
939 packet[7] = (unsigned char)(hp->h_addr_list[0])[3];
|
|
940 packet[8] = 0;
|
|
941
|
14262
|
942 connect_data->write_buffer = g_memdup(packet, sizeof(packet));
|
|
943 connect_data->write_buf_len = sizeof(packet);
|
|
944 connect_data->written_len = 0;
|
|
945 connect_data->read_cb = s4_canread;
|
14192
|
946
|
14262
|
947 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_WRITE, proxy_do_write, connect_data);
|
14192
|
948
|
14262
|
949 proxy_do_write(connect_data, connect_data->fd, cond);
|
14192
|
950 }
|
|
951
|
|
952 static int
|
14262
|
953 proxy_connect_socks4(GaimProxyConnectData *connect_data, struct sockaddr *addr, socklen_t addrlen)
|
14192
|
954 {
|
|
955 gaim_debug_info("socks4 proxy",
|
|
956 "Connecting to %s:%d via %s:%d using SOCKS4\n",
|
14262
|
957 connect_data->host, connect_data->port,
|
|
958 gaim_proxy_info_get_host(connect_data->gpi),
|
|
959 gaim_proxy_info_get_port(connect_data->gpi));
|
14192
|
960
|
14262
|
961 connect_data->fd = socket(addr->sa_family, SOCK_STREAM, 0);
|
|
962 if (connect_data->fd < 0)
|
14192
|
963 return -1;
|
|
964
|
14262
|
965 fcntl(connect_data->fd, F_SETFL, O_NONBLOCK);
|
14192
|
966 #ifndef _WIN32
|
14262
|
967 fcntl(connect_data->fd, F_SETFD, FD_CLOEXEC);
|
14192
|
968 #endif
|
|
969
|
14262
|
970 if (connect(connect_data->fd, addr, addrlen) != 0)
|
14192
|
971 {
|
|
972 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
|
973 gaim_debug_info("socks4 proxy", "Connection in progress.\n");
|
14262
|
974 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_WRITE, s4_canwrite, connect_data);
|
14192
|
975 }
|
|
976 else {
|
14262
|
977 close(connect_data->fd);
|
|
978 connect_data->fd = -1;
|
14192
|
979 return -1;
|
|
980 }
|
|
981 } else {
|
|
982 socklen_t len;
|
|
983 int error = ETIMEDOUT;
|
|
984
|
|
985 gaim_debug_info("socks4 proxy", "Connected immediately.\n");
|
|
986
|
|
987 len = sizeof(error);
|
|
988
|
14262
|
989 if (getsockopt(connect_data->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0)
|
14192
|
990 {
|
14262
|
991 close(connect_data->fd);
|
|
992 connect_data->fd = -1;
|
14192
|
993 return -1;
|
|
994 }
|
|
995
|
14262
|
996 s4_canwrite(connect_data, connect_data->fd, GAIM_INPUT_WRITE);
|
14192
|
997 }
|
|
998
|
14262
|
999 return connect_data->fd;
|
14192
|
1000 }
|
|
1001
|
|
1002 static void
|
|
1003 s5_canread_again(gpointer data, gint source, GaimInputCondition cond)
|
|
1004 {
|
|
1005 guchar *dest, *buf;
|
14262
|
1006 GaimProxyConnectData *connect_data = data;
|
14192
|
1007 int len;
|
|
1008
|
14262
|
1009 if (connect_data->read_buffer == NULL) {
|
|
1010 connect_data->read_buf_len = 512;
|
|
1011 connect_data->read_buffer = g_malloc(connect_data->read_buf_len);
|
|
1012 connect_data->read_len = 0;
|
14192
|
1013 }
|
|
1014
|
14262
|
1015 dest = connect_data->read_buffer + connect_data->read_len;
|
|
1016 buf = connect_data->read_buffer;
|
14192
|
1017
|
|
1018 gaim_debug_info("socks5 proxy", "Able to read again.\n");
|
|
1019
|
14262
|
1020 len = read(connect_data->fd, dest, (connect_data->read_buf_len - connect_data->read_len));
|
14192
|
1021 if(len < 0 && errno == EAGAIN)
|
|
1022 return;
|
|
1023 else if(len <= 0) {
|
|
1024 gaim_debug_warning("socks5 proxy", "or not...\n");
|
14262
|
1025 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1026 try_connect(connect_data);
|
14192
|
1027 return;
|
|
1028 }
|
14262
|
1029 connect_data->read_len += len;
|
14192
|
1030
|
14262
|
1031 if(connect_data->read_len < 4)
|
14192
|
1032 return;
|
|
1033
|
|
1034 if ((buf[0] != 0x05) || (buf[1] != 0x00)) {
|
|
1035 if ((buf[0] == 0x05) && (buf[1] < 0x09))
|
|
1036 gaim_debug_error("socks5 proxy", socks5errors[buf[1]]);
|
|
1037 else
|
|
1038 gaim_debug_error("socks5 proxy", "Bad data.\n");
|
14262
|
1039 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1040 try_connect(connect_data);
|
14192
|
1041 return;
|
|
1042 }
|
|
1043
|
|
1044 /* Skip past BND.ADDR */
|
|
1045 switch(buf[3]) {
|
|
1046 case 0x01: /* the address is a version-4 IP address, with a length of 4 octets */
|
14262
|
1047 if(connect_data->read_len < 4 + 4)
|
14192
|
1048 return;
|
|
1049 buf += 4 + 4;
|
|
1050 break;
|
|
1051 case 0x03: /* the address field contains a fully-qualified domain name. The first
|
|
1052 octet of the address field contains the number of octets of name that
|
|
1053 follow, there is no terminating NUL octet. */
|
14262
|
1054 if(connect_data->read_len < 4 + 1)
|
14192
|
1055 return;
|
|
1056 buf += 4 + 1;
|
14262
|
1057 if(connect_data->read_len < 4 + 1 + buf[0])
|
14192
|
1058 return;
|
|
1059 buf += buf[0];
|
|
1060 break;
|
|
1061 case 0x04: /* the address is a version-6 IP address, with a length of 16 octets */
|
14262
|
1062 if(connect_data->read_len < 4 + 16)
|
14192
|
1063 return;
|
|
1064 buf += 4 + 16;
|
|
1065 break;
|
|
1066 }
|
|
1067
|
14262
|
1068 if(connect_data->read_len < (buf - connect_data->read_buffer) + 2)
|
14192
|
1069 return;
|
|
1070
|
|
1071 /* Skip past BND.PORT */
|
|
1072 buf += 2;
|
|
1073
|
14262
|
1074 gaim_proxy_connect_data_connected(connect_data);
|
14192
|
1075 }
|
|
1076
|
|
1077 static void
|
|
1078 s5_sendconnect(gpointer data, int source)
|
|
1079 {
|
14262
|
1080 GaimProxyConnectData *connect_data = data;
|
|
1081 int hlen = strlen(connect_data->host);
|
|
1082 connect_data->write_buf_len = 5 + hlen + 2;
|
|
1083 connect_data->write_buffer = g_malloc(connect_data->write_buf_len);
|
|
1084 connect_data->written_len = 0;
|
14192
|
1085
|
14262
|
1086 connect_data->write_buffer[0] = 0x05;
|
|
1087 connect_data->write_buffer[1] = 0x01; /* CONNECT */
|
|
1088 connect_data->write_buffer[2] = 0x00; /* reserved */
|
|
1089 connect_data->write_buffer[3] = 0x03; /* address type -- host name */
|
|
1090 connect_data->write_buffer[4] = hlen;
|
|
1091 memcpy(connect_data->write_buffer + 5, connect_data->host, hlen);
|
|
1092 connect_data->write_buffer[5 + hlen] = connect_data->port >> 8;
|
|
1093 connect_data->write_buffer[5 + hlen + 1] = connect_data->port & 0xff;
|
14192
|
1094
|
14262
|
1095 connect_data->read_cb = s5_canread_again;
|
14192
|
1096
|
14262
|
1097 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_WRITE, proxy_do_write, connect_data);
|
|
1098 proxy_do_write(connect_data, connect_data->fd, GAIM_INPUT_WRITE);
|
14192
|
1099 }
|
|
1100
|
|
1101 static void
|
|
1102 s5_readauth(gpointer data, gint source, GaimInputCondition cond)
|
|
1103 {
|
14262
|
1104 GaimProxyConnectData *connect_data = data;
|
14192
|
1105 int len;
|
|
1106
|
14262
|
1107 if (connect_data->read_buffer == NULL) {
|
|
1108 connect_data->read_buf_len = 2;
|
|
1109 connect_data->read_buffer = g_malloc(connect_data->read_buf_len);
|
|
1110 connect_data->read_len = 0;
|
14192
|
1111 }
|
|
1112
|
|
1113 gaim_debug_info("socks5 proxy", "Got auth response.\n");
|
|
1114
|
14262
|
1115 len = read(connect_data->fd, connect_data->read_buffer + connect_data->read_len,
|
|
1116 connect_data->read_buf_len - connect_data->read_len);
|
14192
|
1117 if(len < 0 && errno == EAGAIN)
|
|
1118 return;
|
|
1119 else if(len <= 0) {
|
14262
|
1120 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1121 try_connect(connect_data);
|
14192
|
1122 return;
|
|
1123 }
|
14262
|
1124 connect_data->read_len += len;
|
14192
|
1125
|
14262
|
1126 if (connect_data->read_len < 2)
|
14192
|
1127 return;
|
|
1128
|
14262
|
1129 gaim_input_remove(connect_data->inpa);
|
|
1130 connect_data->inpa = 0;
|
14192
|
1131
|
14262
|
1132 if ((connect_data->read_buffer[0] != 0x01) || (connect_data->read_buffer[1] != 0x00)) {
|
|
1133 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1134 try_connect(connect_data);
|
14192
|
1135 return;
|
|
1136 }
|
|
1137
|
14262
|
1138 g_free(connect_data->read_buffer);
|
|
1139 connect_data->read_buffer = NULL;
|
14192
|
1140
|
14262
|
1141 s5_sendconnect(connect_data, connect_data->fd);
|
14192
|
1142 }
|
|
1143
|
|
1144 static void
|
|
1145 hmacmd5_chap(const unsigned char * challenge, int challen, const char * passwd, unsigned char * response)
|
|
1146 {
|
|
1147 GaimCipher *cipher;
|
|
1148 GaimCipherContext *ctx;
|
|
1149 int i;
|
|
1150 unsigned char Kxoripad[65];
|
|
1151 unsigned char Kxoropad[65];
|
|
1152 int pwlen;
|
|
1153
|
|
1154 cipher = gaim_ciphers_find_cipher("md5");
|
|
1155 ctx = gaim_cipher_context_new(cipher, NULL);
|
|
1156
|
|
1157 memset(Kxoripad,0,sizeof(Kxoripad));
|
|
1158 memset(Kxoropad,0,sizeof(Kxoropad));
|
|
1159
|
|
1160 pwlen=strlen(passwd);
|
|
1161 if (pwlen>64) {
|
|
1162 gaim_cipher_context_append(ctx, (const guchar *)passwd, strlen(passwd));
|
|
1163 gaim_cipher_context_digest(ctx, sizeof(Kxoripad), Kxoripad, NULL);
|
|
1164 pwlen=16;
|
|
1165 } else {
|
|
1166 memcpy(Kxoripad, passwd, pwlen);
|
|
1167 }
|
|
1168 memcpy(Kxoropad,Kxoripad,pwlen);
|
|
1169
|
|
1170 for (i=0;i<64;i++) {
|
|
1171 Kxoripad[i]^=0x36;
|
|
1172 Kxoropad[i]^=0x5c;
|
|
1173 }
|
|
1174
|
|
1175 gaim_cipher_context_reset(ctx, NULL);
|
|
1176 gaim_cipher_context_append(ctx, Kxoripad, 64);
|
|
1177 gaim_cipher_context_append(ctx, challenge, challen);
|
|
1178 gaim_cipher_context_digest(ctx, sizeof(Kxoripad), Kxoripad, NULL);
|
|
1179
|
|
1180 gaim_cipher_context_reset(ctx, NULL);
|
|
1181 gaim_cipher_context_append(ctx, Kxoropad, 64);
|
|
1182 gaim_cipher_context_append(ctx, Kxoripad, 16);
|
|
1183 gaim_cipher_context_digest(ctx, 16, response, NULL);
|
|
1184
|
|
1185 gaim_cipher_context_destroy(ctx);
|
|
1186 }
|
|
1187
|
|
1188 static void
|
|
1189 s5_readchap(gpointer data, gint source, GaimInputCondition cond)
|
|
1190 {
|
|
1191 guchar *cmdbuf, *buf;
|
14262
|
1192 GaimProxyConnectData *connect_data = data;
|
14192
|
1193 int len, navas, currentav;
|
|
1194
|
|
1195 gaim_debug(GAIM_DEBUG_INFO, "socks5 proxy", "Got CHAP response.\n");
|
|
1196
|
14262
|
1197 if (connect_data->read_buffer == NULL) {
|
|
1198 connect_data->read_buf_len = 20;
|
|
1199 connect_data->read_buffer = g_malloc(connect_data->read_buf_len);
|
|
1200 connect_data->read_len = 0;
|
14192
|
1201 }
|
|
1202
|
14262
|
1203 len = read(connect_data->fd, connect_data->read_buffer + connect_data->read_len,
|
|
1204 connect_data->read_buf_len - connect_data->read_len);
|
14192
|
1205
|
|
1206 if(len < 0 && errno == EAGAIN)
|
|
1207 return;
|
|
1208 else if(len <= 0) {
|
14262
|
1209 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1210 try_connect(connect_data);
|
14192
|
1211 return;
|
|
1212 }
|
14262
|
1213 connect_data->read_len += len;
|
14192
|
1214
|
14262
|
1215 if (connect_data->read_len < 2)
|
14192
|
1216 return;
|
|
1217
|
14262
|
1218 cmdbuf = connect_data->read_buffer;
|
14192
|
1219
|
|
1220 if (*cmdbuf != 0x01) {
|
14262
|
1221 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1222 try_connect(connect_data);
|
14192
|
1223 return;
|
|
1224 }
|
|
1225 cmdbuf++;
|
|
1226
|
|
1227 navas = *cmdbuf;
|
|
1228 cmdbuf++;
|
|
1229
|
|
1230 for (currentav = 0; currentav < navas; currentav++) {
|
14262
|
1231 if (connect_data->read_len - (cmdbuf - connect_data->read_buffer) < 2)
|
14192
|
1232 return;
|
14262
|
1233 if (connect_data->read_len - (cmdbuf - connect_data->read_buffer) < cmdbuf[1])
|
14192
|
1234 return;
|
|
1235 buf = cmdbuf + 2;
|
|
1236 switch (cmdbuf[0]) {
|
|
1237 case 0x00:
|
|
1238 /* Did auth work? */
|
|
1239 if (buf[0] == 0x00) {
|
14262
|
1240 gaim_input_remove(connect_data->inpa);
|
|
1241 connect_data->inpa = 0;
|
|
1242 g_free(connect_data->read_buffer);
|
|
1243 connect_data->read_buffer = NULL;
|
14192
|
1244 /* Success */
|
14262
|
1245 s5_sendconnect(connect_data, connect_data->fd);
|
14192
|
1246 return;
|
|
1247 } else {
|
|
1248 /* Failure */
|
|
1249 gaim_debug_warning("proxy",
|
|
1250 "socks5 CHAP authentication "
|
|
1251 "failed. Disconnecting...");
|
14262
|
1252 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1253 try_connect(connect_data);
|
14192
|
1254 return;
|
|
1255 }
|
|
1256 break;
|
|
1257 case 0x03:
|
|
1258 /* Server wants our credentials */
|
|
1259
|
14262
|
1260 connect_data->write_buf_len = 16 + 4;
|
|
1261 connect_data->write_buffer = g_malloc(connect_data->write_buf_len);
|
|
1262 connect_data->written_len = 0;
|
14192
|
1263
|
|
1264 hmacmd5_chap(buf, cmdbuf[1],
|
14262
|
1265 gaim_proxy_info_get_password(connect_data->gpi),
|
|
1266 connect_data->write_buffer + 4);
|
|
1267 connect_data->write_buffer[0] = 0x01;
|
|
1268 connect_data->write_buffer[1] = 0x01;
|
|
1269 connect_data->write_buffer[2] = 0x04;
|
|
1270 connect_data->write_buffer[3] = 0x10;
|
14192
|
1271
|
14262
|
1272 gaim_input_remove(connect_data->inpa);
|
|
1273 g_free(connect_data->read_buffer);
|
|
1274 connect_data->read_buffer = NULL;
|
14192
|
1275
|
14262
|
1276 connect_data->read_cb = s5_readchap;
|
14192
|
1277
|
14262
|
1278 connect_data->inpa = gaim_input_add(connect_data->fd,
|
|
1279 GAIM_INPUT_WRITE, proxy_do_write, connect_data);
|
14192
|
1280
|
14262
|
1281 proxy_do_write(connect_data, connect_data->fd, GAIM_INPUT_WRITE);
|
14192
|
1282 break;
|
|
1283 case 0x11:
|
|
1284 /* Server wants to select an algorithm */
|
|
1285 if (buf[0] != 0x85) {
|
|
1286 /* Only currently support HMAC-MD5 */
|
|
1287 gaim_debug_warning("proxy",
|
|
1288 "Server tried to select an "
|
|
1289 "algorithm that we did not advertise "
|
|
1290 "as supporting. This is a violation "
|
|
1291 "of the socks5 CHAP specification. "
|
|
1292 "Disconnecting...");
|
14262
|
1293 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1294 try_connect(connect_data);
|
14192
|
1295 return;
|
|
1296 }
|
|
1297 break;
|
|
1298 }
|
|
1299 cmdbuf = buf + cmdbuf[1];
|
|
1300 }
|
|
1301 /* Fell through. We ran out of CHAP events to process, but haven't
|
|
1302 * succeeded or failed authentication - there may be more to come.
|
|
1303 * If this is the case, come straight back here. */
|
|
1304 }
|
|
1305
|
|
1306 static void
|
|
1307 s5_canread(gpointer data, gint source, GaimInputCondition cond)
|
|
1308 {
|
14262
|
1309 GaimProxyConnectData *connect_data = data;
|
14192
|
1310 int len;
|
|
1311
|
14262
|
1312 if (connect_data->read_buffer == NULL) {
|
|
1313 connect_data->read_buf_len = 2;
|
|
1314 connect_data->read_buffer = g_malloc(connect_data->read_buf_len);
|
|
1315 connect_data->read_len = 0;
|
14192
|
1316 }
|
|
1317
|
|
1318 gaim_debug_info("socks5 proxy", "Able to read.\n");
|
|
1319
|
14262
|
1320 len = read(connect_data->fd, connect_data->read_buffer + connect_data->read_len,
|
|
1321 connect_data->read_buf_len - connect_data->read_len);
|
14192
|
1322 if(len < 0 && errno == EAGAIN)
|
|
1323 return;
|
|
1324 else if(len <= 0) {
|
14262
|
1325 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1326 try_connect(connect_data);
|
14192
|
1327 return;
|
|
1328 }
|
14262
|
1329 connect_data->read_len += len;
|
14192
|
1330
|
14262
|
1331 if (connect_data->read_len < 2)
|
14192
|
1332 return;
|
|
1333
|
14262
|
1334 gaim_input_remove(connect_data->inpa);
|
|
1335 connect_data->inpa = 0;
|
14192
|
1336
|
14262
|
1337 if ((connect_data->read_buffer[0] != 0x05) || (connect_data->read_buffer[1] == 0xff)) {
|
|
1338 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1339 try_connect(connect_data);
|
14192
|
1340 return;
|
|
1341 }
|
|
1342
|
14262
|
1343 if (connect_data->read_buffer[1] == 0x02) {
|
14192
|
1344 gsize i, j;
|
|
1345 const char *u, *p;
|
|
1346
|
14262
|
1347 u = gaim_proxy_info_get_username(connect_data->gpi);
|
|
1348 p = gaim_proxy_info_get_password(connect_data->gpi);
|
14192
|
1349
|
|
1350 i = (u == NULL) ? 0 : strlen(u);
|
|
1351 j = (p == NULL) ? 0 : strlen(p);
|
|
1352
|
14262
|
1353 connect_data->write_buf_len = 1 + 1 + i + 1 + j;
|
|
1354 connect_data->write_buffer = g_malloc(connect_data->write_buf_len);
|
|
1355 connect_data->written_len = 0;
|
14192
|
1356
|
14262
|
1357 connect_data->write_buffer[0] = 0x01; /* version 1 */
|
|
1358 connect_data->write_buffer[1] = i;
|
14192
|
1359 if (u != NULL)
|
14262
|
1360 memcpy(connect_data->write_buffer + 2, u, i);
|
|
1361 connect_data->write_buffer[2 + i] = j;
|
14192
|
1362 if (p != NULL)
|
14262
|
1363 memcpy(connect_data->write_buffer + 2 + i + 1, p, j);
|
14192
|
1364
|
14262
|
1365 g_free(connect_data->read_buffer);
|
|
1366 connect_data->read_buffer = NULL;
|
14192
|
1367
|
14262
|
1368 connect_data->read_cb = s5_readauth;
|
14192
|
1369
|
14262
|
1370 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_WRITE,
|
|
1371 proxy_do_write, connect_data);
|
14192
|
1372
|
14262
|
1373 proxy_do_write(connect_data, connect_data->fd, GAIM_INPUT_WRITE);
|
14192
|
1374
|
|
1375 return;
|
14262
|
1376 } else if (connect_data->read_buffer[1] == 0x03) {
|
14192
|
1377 gsize userlen;
|
14262
|
1378 userlen = strlen(gaim_proxy_info_get_username(connect_data->gpi));
|
14192
|
1379
|
14262
|
1380 connect_data->write_buf_len = 7 + userlen;
|
|
1381 connect_data->write_buffer = g_malloc(connect_data->write_buf_len);
|
|
1382 connect_data->written_len = 0;
|
14192
|
1383
|
14262
|
1384 connect_data->write_buffer[0] = 0x01;
|
|
1385 connect_data->write_buffer[1] = 0x02;
|
|
1386 connect_data->write_buffer[2] = 0x11;
|
|
1387 connect_data->write_buffer[3] = 0x01;
|
|
1388 connect_data->write_buffer[4] = 0x85;
|
|
1389 connect_data->write_buffer[5] = 0x02;
|
|
1390 connect_data->write_buffer[6] = userlen;
|
|
1391 memcpy(connect_data->write_buffer + 7,
|
|
1392 gaim_proxy_info_get_username(connect_data->gpi), userlen);
|
14192
|
1393
|
14262
|
1394 g_free(connect_data->read_buffer);
|
|
1395 connect_data->read_buffer = NULL;
|
14192
|
1396
|
14262
|
1397 connect_data->read_cb = s5_readchap;
|
14192
|
1398
|
14262
|
1399 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_WRITE,
|
|
1400 proxy_do_write, connect_data);
|
14192
|
1401
|
14262
|
1402 proxy_do_write(connect_data, connect_data->fd, GAIM_INPUT_WRITE);
|
14192
|
1403
|
|
1404 return;
|
|
1405 } else {
|
14262
|
1406 g_free(connect_data->read_buffer);
|
|
1407 connect_data->read_buffer = NULL;
|
14192
|
1408
|
14262
|
1409 s5_sendconnect(connect_data, connect_data->fd);
|
14192
|
1410 }
|
|
1411 }
|
|
1412
|
|
1413 static void
|
|
1414 s5_canwrite(gpointer data, gint source, GaimInputCondition cond)
|
|
1415 {
|
|
1416 unsigned char buf[5];
|
|
1417 int i;
|
14262
|
1418 GaimProxyConnectData *connect_data = data;
|
14192
|
1419 socklen_t len;
|
|
1420 int error = ETIMEDOUT;
|
|
1421
|
|
1422 gaim_debug_info("socks5 proxy", "Connected.\n");
|
|
1423
|
14262
|
1424 if (connect_data->inpa > 0)
|
14192
|
1425 {
|
14262
|
1426 gaim_input_remove(connect_data->inpa);
|
|
1427 connect_data->inpa = 0;
|
14192
|
1428 }
|
|
1429
|
|
1430 len = sizeof(error);
|
14262
|
1431 if (getsockopt(connect_data->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
|
|
1432 gaim_proxy_connect_data_disconnect(connect_data);
|
|
1433 try_connect(connect_data);
|
14192
|
1434 return;
|
|
1435 }
|
|
1436
|
|
1437 i = 0;
|
|
1438 buf[0] = 0x05; /* SOCKS version 5 */
|
|
1439
|
14262
|
1440 if (gaim_proxy_info_get_username(connect_data->gpi) != NULL) {
|
14192
|
1441 buf[1] = 0x03; /* three methods */
|
|
1442 buf[2] = 0x00; /* no authentication */
|
|
1443 buf[3] = 0x03; /* CHAP authentication */
|
|
1444 buf[4] = 0x02; /* username/password authentication */
|
|
1445 i = 5;
|
|
1446 }
|
|
1447 else {
|
|
1448 buf[1] = 0x01;
|
|
1449 buf[2] = 0x00;
|
|
1450 i = 3;
|
|
1451 }
|
|
1452
|
14262
|
1453 connect_data->write_buf_len = i;
|
|
1454 connect_data->write_buffer = g_malloc(connect_data->write_buf_len);
|
|
1455 memcpy(connect_data->write_buffer, buf, i);
|
14192
|
1456
|
14262
|
1457 connect_data->read_cb = s5_canread;
|
14192
|
1458
|
14262
|
1459 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_WRITE, proxy_do_write, connect_data);
|
|
1460 proxy_do_write(connect_data, connect_data->fd, GAIM_INPUT_WRITE);
|
14192
|
1461 }
|
|
1462
|
|
1463 static int
|
14262
|
1464 proxy_connect_socks5(GaimProxyConnectData *connect_data, struct sockaddr *addr, socklen_t addrlen)
|
14192
|
1465 {
|
|
1466 gaim_debug_info("socks5 proxy",
|
|
1467 "Connecting to %s:%d via %s:%d using SOCKS5\n",
|
14262
|
1468 connect_data->host, connect_data->port,
|
|
1469 gaim_proxy_info_get_host(connect_data->gpi),
|
|
1470 gaim_proxy_info_get_port(connect_data->gpi));
|
14192
|
1471
|
14262
|
1472 connect_data->fd = socket(addr->sa_family, SOCK_STREAM, 0);
|
|
1473 if (connect_data->fd < 0)
|
14192
|
1474 return -1;
|
|
1475
|
14262
|
1476 fcntl(connect_data->fd, F_SETFL, O_NONBLOCK);
|
14192
|
1477 #ifndef _WIN32
|
14262
|
1478 fcntl(connect_data->fd, F_SETFD, FD_CLOEXEC);
|
14192
|
1479 #endif
|
|
1480
|
14262
|
1481 if (connect(connect_data->fd, addr, addrlen) != 0)
|
14192
|
1482 {
|
|
1483 if ((errno == EINPROGRESS) || (errno == EINTR)) {
|
|
1484 gaim_debug_info("socks5 proxy", "Connection in progress\n");
|
14262
|
1485 connect_data->inpa = gaim_input_add(connect_data->fd, GAIM_INPUT_WRITE, s5_canwrite, connect_data);
|
14192
|
1486 }
|
|
1487 else {
|
14262
|
1488 close(connect_data->fd);
|
|
1489 connect_data->fd = -1;
|
14192
|
1490 return -1;
|
|
1491 }
|
|
1492 }
|
|
1493 else {
|
|
1494 socklen_t len;
|
|
1495 int error = ETIMEDOUT;
|
|
1496
|
|
1497 gaim_debug_info("socks5 proxy", "Connected immediately.\n");
|
|
1498
|
|
1499 len = sizeof(error);
|
|
1500
|
14262
|
1501 if (getsockopt(connect_data->fd, SOL_SOCKET, SO_ERROR, &error, &len) != 0)
|
14192
|
1502 {
|
14262
|
1503 close(connect_data->fd);
|
|
1504 connect_data->fd = -1;
|
14192
|
1505 return -1;
|
|
1506 }
|
|
1507
|
14262
|
1508 s5_canwrite(connect_data, connect_data->fd, GAIM_INPUT_WRITE);
|
14192
|
1509 }
|
|
1510
|
14262
|
1511 return connect_data->fd;
|
14192
|
1512 }
|
|
1513
|
|
1514 /**
|
|
1515 * This function iterates through a list of IP addresses and attempts
|
|
1516 * to connect to each one. This is called after the hostname is
|
|
1517 * resolved, and if a connection attempt fails.
|
|
1518 */
|
14262
|
1519 static void try_connect(GaimProxyConnectData *connect_data)
|
14192
|
1520 {
|
|
1521 size_t addrlen;
|
|
1522 struct sockaddr *addr;
|
|
1523 int ret = -1;
|
|
1524
|
14262
|
1525 if (connect_data->hosts == NULL)
|
14192
|
1526 {
|
14262
|
1527 gaim_proxy_connect_data_error(connect_data, _("Could not resolve host name"));
|
14192
|
1528 return;
|
|
1529 }
|
|
1530
|
14262
|
1531 while (connect_data->hosts)
|
14192
|
1532 {
|
14262
|
1533 addrlen = GPOINTER_TO_INT(connect_data->hosts->data);
|
|
1534 connect_data->hosts = g_slist_remove(connect_data->hosts, connect_data->hosts->data);
|
|
1535 addr = connect_data->hosts->data;
|
|
1536 connect_data->hosts = g_slist_remove(connect_data->hosts, connect_data->hosts->data);
|
14192
|
1537
|
14262
|
1538 switch (gaim_proxy_info_get_type(connect_data->gpi)) {
|
14192
|
1539 case GAIM_PROXY_NONE:
|
14262
|
1540 ret = proxy_connect_none(connect_data, addr, addrlen);
|
14192
|
1541 break;
|
|
1542
|
|
1543 case GAIM_PROXY_HTTP:
|
14262
|
1544 ret = proxy_connect_http(connect_data, addr, addrlen);
|
14192
|
1545 break;
|
|
1546
|
|
1547 case GAIM_PROXY_SOCKS4:
|
14262
|
1548 ret = proxy_connect_socks4(connect_data, addr, addrlen);
|
14192
|
1549 break;
|
|
1550
|
|
1551 case GAIM_PROXY_SOCKS5:
|
14262
|
1552 ret = proxy_connect_socks5(connect_data, addr, addrlen);
|
14192
|
1553 break;
|
|
1554
|
|
1555 case GAIM_PROXY_USE_ENVVAR:
|
14262
|
1556 ret = proxy_connect_http(connect_data, addr, addrlen);
|
14192
|
1557 break;
|
|
1558
|
|
1559 default:
|
|
1560 break;
|
|
1561 }
|
|
1562
|
|
1563 g_free(addr);
|
|
1564
|
|
1565 if (ret >= 0)
|
|
1566 break;
|
|
1567 }
|
|
1568
|
|
1569 if (ret < 0) {
|
14262
|
1570 gaim_proxy_connect_data_error(connect_data, _("Unable to establish a connection"));
|
14192
|
1571 }
|
|
1572 }
|
|
1573
|
|
1574 static void
|
|
1575 connection_host_resolved(GSList *hosts, gpointer data,
|
|
1576 const char *error_message)
|
|
1577 {
|
14262
|
1578 GaimProxyConnectData *connect_data;
|
14192
|
1579
|
14262
|
1580 connect_data = data;
|
|
1581 connect_data->query_data = NULL;
|
14238
|
1582
|
14192
|
1583 if (error_message != NULL)
|
|
1584 {
|
14392
|
1585 gaim_proxy_connect_data_error(connect_data, error_message);
|
14238
|
1586 return;
|
14192
|
1587 }
|
|
1588
|
14262
|
1589 connect_data->hosts = hosts;
|
14192
|
1590
|
14262
|
1591 try_connect(connect_data);
|
14192
|
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
|
14262
|
1660 GaimProxyConnectData *
|
14192
|
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;
|
14262
|
1666 GaimProxyConnectData *connect_data;
|
14192
|
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
|
14262
|
1672 connect_data = g_new0(GaimProxyConnectData, 1);
|
|
1673 connect_data->fd = -1;
|
|
1674 connect_data->connect_cb = connect_cb;
|
|
1675 connect_data->data = data;
|
|
1676 connect_data->host = g_strdup(host);
|
|
1677 connect_data->port = port;
|
|
1678 connect_data->gpi = gaim_proxy_get_setup(account);
|
14192
|
1679
|
14262
|
1680 if ((gaim_proxy_info_get_type(connect_data->gpi) != GAIM_PROXY_NONE) &&
|
|
1681 (gaim_proxy_info_get_host(connect_data->gpi) == NULL ||
|
|
1682 gaim_proxy_info_get_port(connect_data->gpi) <= 0)) {
|
14192
|
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."));
|
14262
|
1685 gaim_proxy_connect_data_destroy(connect_data);
|
14192
|
1686 return NULL;
|
|
1687 }
|
|
1688
|
14262
|
1689 switch (gaim_proxy_info_get_type(connect_data->gpi))
|
14192
|
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:
|
14262
|
1698 connecthost = gaim_proxy_info_get_host(connect_data->gpi);
|
|
1699 connectport = gaim_proxy_info_get_port(connect_data->gpi);
|
14192
|
1700 break;
|
|
1701
|
|
1702 default:
|
14262
|
1703 gaim_proxy_connect_data_destroy(connect_data);
|
14192
|
1704 return NULL;
|
|
1705 }
|
|
1706
|
14262
|
1707 connect_data->query_data = gaim_dnsquery_a(connecthost,
|
|
1708 connectport, connection_host_resolved, connect_data);
|
|
1709 if (connect_data->query_data == NULL)
|
14192
|
1710 {
|
14262
|
1711 gaim_proxy_connect_data_destroy(connect_data);
|
14192
|
1712 return NULL;
|
|
1713 }
|
|
1714
|
14262
|
1715 connect_datas = g_slist_prepend(connect_datas, connect_data);
|
14192
|
1716
|
14262
|
1717 return connect_data;
|
14192
|
1718 }
|
|
1719
|
|
1720 /*
|
|
1721 * Combine some of this code with gaim_proxy_connect()
|
|
1722 */
|
14262
|
1723 GaimProxyConnectData *
|
14192
|
1724 gaim_proxy_connect_socks5(GaimProxyInfo *gpi, const char *host, int port,
|
|
1725 GaimProxyConnectFunction connect_cb, gpointer data)
|
|
1726 {
|
14262
|
1727 GaimProxyConnectData *connect_data;
|
14192
|
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
|
14262
|
1733 connect_data = g_new0(GaimProxyConnectData, 1);
|
|
1734 connect_data->fd = -1;
|
|
1735 connect_data->connect_cb = connect_cb;
|
|
1736 connect_data->data = data;
|
|
1737 connect_data->host = g_strdup(host);
|
|
1738 connect_data->port = port;
|
|
1739 connect_data->gpi = gpi;
|
14192
|
1740
|
14262
|
1741 connect_data->query_data =
|
14192
|
1742 gaim_dnsquery_a(gaim_proxy_info_get_host(gpi),
|
|
1743 gaim_proxy_info_get_port(gpi),
|
14262
|
1744 connection_host_resolved, connect_data);
|
|
1745 if (connect_data->query_data == NULL)
|
14192
|
1746 {
|
14262
|
1747 gaim_proxy_connect_data_destroy(connect_data);
|
14192
|
1748 return NULL;
|
|
1749 }
|
|
1750
|
14262
|
1751 connect_datas = g_slist_prepend(connect_datas, connect_data);
|
14192
|
1752
|
14262
|
1753 return connect_data;
|
14192
|
1754 }
|
|
1755
|
|
1756 void
|
14262
|
1757 gaim_proxy_connect_cancel(GaimProxyConnectData *connect_data)
|
14192
|
1758 {
|
14262
|
1759 gaim_proxy_connect_data_destroy(connect_data);
|
14192
|
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 {
|
14262
|
1837 while (connect_datas != NULL)
|
|
1838 gaim_proxy_connect_data_destroy(connect_datas->data);
|
14192
|
1839 }
|