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