Branch data Line data Source code
1 : : /* GIO - GLib Input, Output and Streaming Library
2 : : *
3 : : * Copyright (C) 2010 Collabora, Ltd.
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General
18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 : : *
20 : : * Author: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include "gsocks4aproxy.h"
26 : :
27 : : #include <string.h>
28 : :
29 : : #include "giomodule.h"
30 : : #include "giomodule-priv.h"
31 : : #include "giostream.h"
32 : : #include "ginetaddress.h"
33 : : #include "ginputstream.h"
34 : : #include "glibintl.h"
35 : : #include "goutputstream.h"
36 : : #include "gproxy.h"
37 : : #include "gproxyaddress.h"
38 : : #include "gtask.h"
39 : :
40 : : #define SOCKS4_VERSION 4
41 : :
42 : : #define SOCKS4_CMD_CONNECT 1
43 : : #define SOCKS4_CMD_BIND 2
44 : :
45 : : #define SOCKS4_MAX_LEN 255
46 : :
47 : : #define SOCKS4_REP_VERSION 0
48 : : #define SOCKS4_REP_GRANTED 90
49 : : #define SOCKS4_REP_REJECTED 91
50 : : #define SOCKS4_REP_NO_IDENT 92
51 : : #define SOCKS4_REP_BAD_IDENT 93
52 : :
53 : : static void g_socks4a_proxy_iface_init (GProxyInterface *proxy_iface);
54 : :
55 : : #define g_socks4a_proxy_get_type _g_socks4a_proxy_get_type
56 : 690 : G_DEFINE_TYPE_WITH_CODE (GSocks4aProxy, g_socks4a_proxy, G_TYPE_OBJECT,
57 : 63 : G_IMPLEMENT_INTERFACE (G_TYPE_PROXY,
58 : : g_socks4a_proxy_iface_init)
59 : 63 : _g_io_modules_ensure_extension_points_registered ();
60 : 63 : g_io_extension_point_implement (G_PROXY_EXTENSION_POINT_NAME,
61 : 63 : g_define_type_id,
62 : : "socks4a",
63 : : 0))
64 : :
65 : : static void
66 : 0 : g_socks4a_proxy_finalize (GObject *object)
67 : : {
68 : : /* must chain up */
69 : 0 : G_OBJECT_CLASS (g_socks4a_proxy_parent_class)->finalize (object);
70 : 0 : }
71 : :
72 : : static void
73 : 0 : g_socks4a_proxy_init (GSocks4aProxy *proxy)
74 : : {
75 : 0 : proxy->supports_hostname = TRUE;
76 : 0 : }
77 : :
78 : : /* |-> SOCKSv4a only
79 : : * +----+----+----+----+----+----+----+----+----+----+....+----+------+....+------+
80 : : * | VN | CD | DSTPORT | DSTIP | USERID |NULL| HOST | | NULL |
81 : : * +----+----+----+----+----+----+----+----+----+----+....+----+------+....+------+
82 : : * 1 1 2 4 variable 1 variable 1
83 : : */
84 : : #define SOCKS4_CONN_MSG_LEN (10 + SOCKS4_MAX_LEN * 2)
85 : : static gint
86 : 0 : set_connect_msg (guint8 *msg,
87 : : const gchar *hostname,
88 : : guint16 port,
89 : : const char *username,
90 : : GError **error)
91 : : {
92 : : GInetAddress *addr;
93 : 0 : guint len = 0;
94 : : gsize addr_len;
95 : : gboolean is_ip;
96 : : const gchar *ip;
97 : :
98 : 0 : msg[len++] = SOCKS4_VERSION;
99 : 0 : msg[len++] = SOCKS4_CMD_CONNECT;
100 : :
101 : : {
102 : 0 : guint16 hp = g_htons (port);
103 : 0 : memcpy (msg + len, &hp, 2);
104 : 0 : len += 2;
105 : : }
106 : :
107 : 0 : is_ip = g_hostname_is_ip_address (hostname);
108 : :
109 : 0 : if (is_ip)
110 : 0 : ip = hostname;
111 : : else
112 : 0 : ip = "0.0.0.1";
113 : :
114 : 0 : addr = g_inet_address_new_from_string (ip);
115 : 0 : if (addr == NULL)
116 : : {
117 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
118 : 0 : _("Invalid IP address “%s”"),
119 : 0 : ip);
120 : 0 : return -1;
121 : : }
122 : :
123 : 0 : addr_len = g_inet_address_get_native_size (addr);
124 : 0 : if (addr_len != 4)
125 : : {
126 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
127 : 0 : _("SOCKSv4 does not support IPv6 address “%s”"),
128 : 0 : ip);
129 : 0 : g_object_unref (addr);
130 : 0 : return -1;
131 : : }
132 : :
133 : 0 : memcpy (msg + len, g_inet_address_to_bytes (addr), addr_len);
134 : 0 : len += addr_len;
135 : :
136 : 0 : g_object_unref (addr);
137 : :
138 : 0 : if (username)
139 : : {
140 : 0 : gsize user_len = strlen (username);
141 : :
142 : 0 : if (user_len > SOCKS4_MAX_LEN)
143 : : {
144 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
145 : 0 : _("Username is too long for SOCKSv4 protocol"));
146 : 0 : return -1;
147 : : }
148 : :
149 : 0 : memcpy (msg + len, username, user_len);
150 : 0 : len += user_len;
151 : 0 : }
152 : :
153 : 0 : msg[len++] = '\0';
154 : :
155 : 0 : if (!is_ip)
156 : : {
157 : 0 : gsize host_len = strlen (hostname);
158 : :
159 : 0 : if (host_len > SOCKS4_MAX_LEN)
160 : : {
161 : 0 : g_set_error (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
162 : 0 : _("Hostname “%s” is too long for SOCKSv4 protocol"),
163 : 0 : hostname);
164 : 0 : return -1;
165 : : }
166 : :
167 : 0 : memcpy (msg + len, hostname, host_len);
168 : 0 : len += host_len;
169 : 0 : msg[len++] = '\0';
170 : 0 : }
171 : :
172 : 0 : return len;
173 : 0 : }
174 : :
175 : : /*
176 : : * +----+----+----+----+----+----+----+----+
177 : : * | VN | CD | DSTPORT | DSTIP |
178 : : * +----+----+----+----+----+----+----+----+
179 : : * 1 1 2 4
180 : : */
181 : : #define SOCKS4_CONN_REP_LEN 8
182 : : static gboolean
183 : 0 : parse_connect_reply (const guint8 *data, GError **error)
184 : : {
185 : 0 : if (data[0] != SOCKS4_REP_VERSION)
186 : : {
187 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
188 : 0 : _("The server is not a SOCKSv4 proxy server."));
189 : 0 : return FALSE;
190 : : }
191 : :
192 : 0 : if (data[1] != SOCKS4_REP_GRANTED)
193 : : {
194 : 0 : g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PROXY_FAILED,
195 : 0 : _("Connection through SOCKSv4 server was rejected"));
196 : 0 : return FALSE;
197 : : }
198 : :
199 : 0 : return TRUE;
200 : 0 : }
201 : :
202 : : static GIOStream *
203 : 0 : g_socks4a_proxy_connect (GProxy *proxy,
204 : : GIOStream *io_stream,
205 : : GProxyAddress *proxy_address,
206 : : GCancellable *cancellable,
207 : : GError **error)
208 : : {
209 : : GInputStream *in;
210 : : GOutputStream *out;
211 : : const gchar *hostname;
212 : : guint16 port;
213 : : const gchar *username;
214 : :
215 : 0 : hostname = g_proxy_address_get_destination_hostname (proxy_address);
216 : 0 : port = g_proxy_address_get_destination_port (proxy_address);
217 : 0 : username = g_proxy_address_get_username (proxy_address);
218 : :
219 : 0 : in = g_io_stream_get_input_stream (io_stream);
220 : 0 : out = g_io_stream_get_output_stream (io_stream);
221 : :
222 : : /* Send SOCKS4 connection request */
223 : : {
224 : : guint8 msg[SOCKS4_CONN_MSG_LEN];
225 : : gint len;
226 : :
227 : 0 : len = set_connect_msg (msg, hostname, port, username, error);
228 : :
229 : 0 : if (len < 0)
230 : 0 : goto error;
231 : :
232 : 0 : if (!g_output_stream_write_all (out, msg, len, NULL,
233 : 0 : cancellable, error))
234 : 0 : goto error;
235 : : }
236 : :
237 : : /* Read SOCKS4 response */
238 : : {
239 : : guint8 data[SOCKS4_CONN_REP_LEN];
240 : :
241 : 0 : if (!g_input_stream_read_all (in, data, SOCKS4_CONN_REP_LEN, NULL,
242 : 0 : cancellable, error))
243 : 0 : goto error;
244 : :
245 : 0 : if (!parse_connect_reply (data, error))
246 : 0 : goto error;
247 : : }
248 : :
249 : 0 : return g_object_ref (io_stream);
250 : :
251 : 0 : error:
252 : 0 : return NULL;
253 : 0 : }
254 : :
255 : :
256 : : typedef struct
257 : : {
258 : : GIOStream *io_stream;
259 : :
260 : : /* For connecting */
261 : : guint8 *buffer;
262 : : gssize length;
263 : : gssize offset;
264 : :
265 : : } ConnectAsyncData;
266 : :
267 : : static void connect_msg_write_cb (GObject *source,
268 : : GAsyncResult *result,
269 : : gpointer user_data);
270 : : static void connect_reply_read_cb (GObject *source,
271 : : GAsyncResult *result,
272 : : gpointer user_data);
273 : :
274 : : static void
275 : 0 : free_connect_data (ConnectAsyncData *data)
276 : : {
277 : 0 : g_object_unref (data->io_stream);
278 : 0 : g_free (data->buffer);
279 : 0 : g_slice_free (ConnectAsyncData, data);
280 : 0 : }
281 : :
282 : : static void
283 : 0 : do_read (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
284 : : {
285 : : GInputStream *in;
286 : 0 : in = g_io_stream_get_input_stream (data->io_stream);
287 : 0 : g_input_stream_read_async (in,
288 : 0 : data->buffer + data->offset,
289 : 0 : data->length - data->offset,
290 : 0 : g_task_get_priority (task),
291 : 0 : g_task_get_cancellable (task),
292 : 0 : callback, task);
293 : 0 : }
294 : :
295 : : static void
296 : 0 : do_write (GAsyncReadyCallback callback, GTask *task, ConnectAsyncData *data)
297 : : {
298 : : GOutputStream *out;
299 : 0 : out = g_io_stream_get_output_stream (data->io_stream);
300 : 0 : g_output_stream_write_async (out,
301 : 0 : data->buffer + data->offset,
302 : 0 : data->length - data->offset,
303 : 0 : g_task_get_priority (task),
304 : 0 : g_task_get_cancellable (task),
305 : 0 : callback, task);
306 : 0 : }
307 : :
308 : :
309 : :
310 : : static void
311 : 0 : g_socks4a_proxy_connect_async (GProxy *proxy,
312 : : GIOStream *io_stream,
313 : : GProxyAddress *proxy_address,
314 : : GCancellable *cancellable,
315 : : GAsyncReadyCallback callback,
316 : : gpointer user_data)
317 : : {
318 : 0 : GError *error = NULL;
319 : : GTask *task;
320 : : ConnectAsyncData *data;
321 : : const gchar *hostname;
322 : : guint16 port;
323 : : const gchar *username;
324 : :
325 : 0 : data = g_slice_new0 (ConnectAsyncData);
326 : 0 : data->io_stream = g_object_ref (io_stream);
327 : :
328 : 0 : task = g_task_new (proxy, cancellable, callback, user_data);
329 : 0 : g_task_set_source_tag (task, g_socks4a_proxy_connect_async);
330 : 0 : g_task_set_task_data (task, data, (GDestroyNotify) free_connect_data);
331 : :
332 : 0 : hostname = g_proxy_address_get_destination_hostname (proxy_address);
333 : 0 : port = g_proxy_address_get_destination_port (proxy_address);
334 : 0 : username = g_proxy_address_get_username (proxy_address);
335 : :
336 : 0 : data->buffer = g_malloc0 (SOCKS4_CONN_MSG_LEN);
337 : 0 : data->length = set_connect_msg (data->buffer,
338 : 0 : hostname, port, username,
339 : : &error);
340 : 0 : data->offset = 0;
341 : :
342 : 0 : if (data->length < 0)
343 : : {
344 : 0 : g_task_return_error (task, error);
345 : 0 : g_object_unref (task);
346 : 0 : }
347 : : else
348 : : {
349 : 0 : do_write (connect_msg_write_cb, task, data);
350 : : }
351 : 0 : }
352 : :
353 : : static void
354 : 0 : connect_msg_write_cb (GObject *source,
355 : : GAsyncResult *result,
356 : : gpointer user_data)
357 : : {
358 : 0 : GTask *task = user_data;
359 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
360 : 0 : GError *error = NULL;
361 : : gssize written;
362 : :
363 : 0 : written = g_output_stream_write_finish (G_OUTPUT_STREAM (source),
364 : 0 : result, &error);
365 : :
366 : 0 : if (written < 0)
367 : : {
368 : 0 : g_task_return_error (task, error);
369 : 0 : g_object_unref (task);
370 : 0 : return;
371 : : }
372 : :
373 : 0 : data->offset += written;
374 : :
375 : 0 : if (data->offset == data->length)
376 : : {
377 : 0 : g_free (data->buffer);
378 : :
379 : 0 : data->buffer = g_malloc0 (SOCKS4_CONN_REP_LEN);
380 : 0 : data->length = SOCKS4_CONN_REP_LEN;
381 : 0 : data->offset = 0;
382 : :
383 : 0 : do_read (connect_reply_read_cb, task, data);
384 : 0 : }
385 : : else
386 : : {
387 : 0 : do_write (connect_msg_write_cb, task, data);
388 : : }
389 : 0 : }
390 : :
391 : : static void
392 : 0 : connect_reply_read_cb (GObject *source,
393 : : GAsyncResult *result,
394 : : gpointer user_data)
395 : : {
396 : 0 : GTask *task = user_data;
397 : 0 : ConnectAsyncData *data = g_task_get_task_data (task);
398 : 0 : GError *error = NULL;
399 : : gssize read;
400 : :
401 : 0 : read = g_input_stream_read_finish (G_INPUT_STREAM (source),
402 : 0 : result, &error);
403 : :
404 : 0 : if (read < 0)
405 : : {
406 : 0 : g_task_return_error (task, error);
407 : 0 : g_object_unref (task);
408 : 0 : return;
409 : : }
410 : :
411 : 0 : data->offset += read;
412 : :
413 : 0 : if (data->offset == data->length)
414 : : {
415 : 0 : if (!parse_connect_reply (data->buffer, &error))
416 : : {
417 : 0 : g_task_return_error (task, error);
418 : 0 : g_object_unref (task);
419 : 0 : return;
420 : : }
421 : : else
422 : : {
423 : 0 : g_task_return_pointer (task, g_object_ref (data->io_stream), g_object_unref);
424 : 0 : g_object_unref (task);
425 : 0 : return;
426 : : }
427 : : }
428 : : else
429 : : {
430 : 0 : do_read (connect_reply_read_cb, task, data);
431 : : }
432 : 0 : }
433 : :
434 : : static GIOStream *g_socks4a_proxy_connect_finish (GProxy *proxy,
435 : : GAsyncResult *result,
436 : : GError **error);
437 : :
438 : : static GIOStream *
439 : 0 : g_socks4a_proxy_connect_finish (GProxy *proxy,
440 : : GAsyncResult *result,
441 : : GError **error)
442 : : {
443 : 0 : return g_task_propagate_pointer (G_TASK (result), error);
444 : : }
445 : :
446 : : static gboolean
447 : 0 : g_socks4a_proxy_supports_hostname (GProxy *proxy)
448 : : {
449 : 0 : return G_SOCKS4A_PROXY (proxy)->supports_hostname;
450 : : }
451 : :
452 : : static void
453 : 0 : g_socks4a_proxy_class_init (GSocks4aProxyClass *class)
454 : : {
455 : : GObjectClass *object_class;
456 : :
457 : 0 : object_class = (GObjectClass *) class;
458 : 0 : object_class->finalize = g_socks4a_proxy_finalize;
459 : 0 : }
460 : :
461 : : static void
462 : 0 : g_socks4a_proxy_iface_init (GProxyInterface *proxy_iface)
463 : : {
464 : 0 : proxy_iface->connect = g_socks4a_proxy_connect;
465 : 0 : proxy_iface->connect_async = g_socks4a_proxy_connect_async;
466 : 0 : proxy_iface->connect_finish = g_socks4a_proxy_connect_finish;
467 : 0 : proxy_iface->supports_hostname = g_socks4a_proxy_supports_hostname;
468 : 0 : }
|