7691b6f827db4aa6a49b24a50604d57009fd9e78
[platform/upstream/glib.git] / gio / gsimpleproxyresolver.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright 2010, 2013 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21
22 #include <stdlib.h>
23
24 #include "gsimpleproxyresolver.h"
25 #include "ginetaddress.h"
26 #include "ginetaddressmask.h"
27 #include "gnetworkingprivate.h"
28 #include "gtask.h"
29
30 #include "glibintl.h"
31
32 /**
33  * SECTION:gsimpleproxyresolver
34  * @short_description: Simple proxy resolver implementation
35  * @include: gio/gio.h
36  * @see_also: g_socket_client_set_proxy_resolver()
37  *
38  * #GSimpleProxyResolver is a simple #GProxyResolver implementation
39  * that handles a single default proxy, multiple URI-scheme-specific
40  * proxies, and a list of hosts that proxies should not be used for.
41  *
42  * #GSimpleProxyResolver is never the default proxy resolver, but it
43  * can be used as the base class for another proxy resolver
44  * implementation, or it can be created and used manually, such as
45  * with g_socket_client_set_proxy_resolver().
46  *
47  * Since: 2.36
48  */
49
50 typedef struct {
51   gchar        *name;
52   gint          length;
53   gushort       port;
54 } GSimpleProxyResolverDomain;
55
56 struct _GSimpleProxyResolverPrivate {
57   gchar *default_proxy, **ignore_hosts;
58   GHashTable *uri_proxies;
59
60   GPtrArray *ignore_ips;
61   GSimpleProxyResolverDomain *ignore_domains;
62 };
63
64 static void g_simple_proxy_resolver_iface_init (GProxyResolverInterface *iface);
65
66 G_DEFINE_TYPE_WITH_CODE (GSimpleProxyResolver, g_simple_proxy_resolver, G_TYPE_OBJECT,
67                          G_IMPLEMENT_INTERFACE (G_TYPE_PROXY_RESOLVER,
68                                                 g_simple_proxy_resolver_iface_init))
69
70 enum
71 {
72   PROP_0,
73   PROP_DEFAULT_PROXY,
74   PROP_IGNORE_HOSTS
75 };
76
77 static void reparse_ignore_hosts (GSimpleProxyResolver *resolver);
78
79 static void
80 g_simple_proxy_resolver_finalize (GObject *object)
81 {
82   GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (object);
83   GSimpleProxyResolverPrivate *priv = resolver->priv;
84
85   g_free (priv->default_proxy);
86   g_hash_table_destroy (priv->uri_proxies);
87
88   g_clear_pointer (&priv->ignore_hosts, g_strfreev);
89   /* This will free ignore_ips and ignore_domains */
90   reparse_ignore_hosts (resolver);
91
92   G_OBJECT_CLASS (g_simple_proxy_resolver_parent_class)->finalize (object);
93 }
94
95 static void
96 g_simple_proxy_resolver_init (GSimpleProxyResolver *resolver)
97 {
98   resolver->priv = G_TYPE_INSTANCE_GET_PRIVATE (resolver,
99                                                 G_TYPE_SIMPLE_PROXY_RESOLVER,
100                                                 GSimpleProxyResolverPrivate);
101   resolver->priv->uri_proxies = g_hash_table_new_full (g_str_hash, g_str_equal,
102                                                        g_free, g_free);
103 }
104
105 static void
106 g_simple_proxy_resolver_set_property (GObject      *object,
107                                       guint         prop_id,
108                                       const GValue *value,
109                                       GParamSpec   *pspec)
110 {
111   GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (object);
112
113   switch (prop_id)
114     {
115       case PROP_DEFAULT_PROXY:
116         g_simple_proxy_resolver_set_default_proxy (resolver, g_value_get_string (value));
117         break;
118
119       case PROP_IGNORE_HOSTS:
120         g_simple_proxy_resolver_set_ignore_hosts (resolver, g_value_get_boxed (value));
121         break;
122
123       default:
124         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125     }
126 }
127
128 static void
129 g_simple_proxy_resolver_get_property (GObject    *object,
130                                       guint       prop_id,
131                                       GValue     *value,
132                                       GParamSpec *pspec)
133 {
134   GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (object);
135
136   switch (prop_id)
137     {
138       case PROP_DEFAULT_PROXY:
139         g_value_set_string (value, resolver->priv->default_proxy);
140         break;
141
142       case PROP_IGNORE_HOSTS:
143         g_value_set_boxed (value, resolver->priv->ignore_hosts);
144         break;
145
146       default:
147         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148     }
149 }
150
151 static void
152 reparse_ignore_hosts (GSimpleProxyResolver *resolver)
153 {
154   GSimpleProxyResolverPrivate *priv = resolver->priv;
155   GPtrArray *ignore_ips;
156   GArray *ignore_domains;
157   gchar *host, *tmp, *colon, *bracket;
158   GInetAddress *iaddr;
159   GInetAddressMask *mask;
160   GSimpleProxyResolverDomain domain;
161   gushort port;
162   int i;
163
164   if (priv->ignore_ips)
165     g_ptr_array_free (priv->ignore_ips, TRUE);
166   if (priv->ignore_domains)
167     {
168       for (i = 0; priv->ignore_domains[i].name; i++)
169         g_free (priv->ignore_domains[i].name);
170       g_free (priv->ignore_domains);
171     }
172   priv->ignore_ips = NULL;
173   priv->ignore_domains = NULL;
174
175   if (!priv->ignore_hosts || !priv->ignore_hosts[0])
176     return;
177
178   ignore_ips = g_ptr_array_new_with_free_func (g_object_unref);
179   ignore_domains = g_array_new (TRUE, FALSE, sizeof (GSimpleProxyResolverDomain));
180
181   for (i = 0; priv->ignore_hosts[i]; i++)
182     {
183       host = g_strchomp (priv->ignore_hosts[i]);
184
185       /* See if it's an IP address or IP/length mask */
186       mask = g_inet_address_mask_new_from_string (host, NULL);
187       if (mask)
188         {
189           g_ptr_array_add (ignore_ips, mask);
190           continue;
191         }
192
193       port = 0;
194
195       if (*host == '[')
196         {
197           /* [IPv6]:port */
198           host++;
199           bracket = strchr (host, ']');
200           if (!bracket || !bracket[1] || bracket[1] != ':')
201             goto bad;
202
203           port = strtoul (bracket + 2, &tmp, 10);
204           if (*tmp)
205             goto bad;
206
207           *bracket = '\0';
208         }
209       else
210         {
211           colon = strchr (host, ':');
212           if (colon && !strchr (colon + 1, ':'))
213             {
214               /* hostname:port or IPv4:port */
215               port = strtoul (colon + 1, &tmp, 10);
216               if (*tmp)
217                 goto bad;
218               *colon = '\0';
219             }
220         }
221
222       iaddr = g_inet_address_new_from_string (host);
223       if (iaddr)
224         g_object_unref (iaddr);
225       else
226         {
227           if (g_str_has_prefix (host, "*."))
228             host += 2;
229           else if (*host == '.')
230             host++;
231         }
232
233       memset (&domain, 0, sizeof (domain));
234       domain.name = g_strdup (host);
235       domain.length = strlen (domain.name);
236       domain.port = port;
237       g_array_append_val (ignore_domains, domain);
238       continue;
239
240     bad:
241       g_warning ("Ignoring invalid ignore_hosts value '%s'", host);
242     }
243
244   if (ignore_ips->len)
245     priv->ignore_ips = ignore_ips;
246   else
247     g_ptr_array_free (ignore_ips, TRUE);
248
249   if (ignore_domains->len)
250     priv->ignore_domains = (GSimpleProxyResolverDomain *)ignore_domains->data;
251   g_array_free (ignore_domains, ignore_domains->len == 0);
252 }
253
254 static gboolean
255 ignore_host (GSimpleProxyResolver *resolver,
256              const gchar          *host,
257              gushort               port)
258 {
259   GSimpleProxyResolverPrivate *priv = resolver->priv;
260   gchar *ascii_host = NULL;
261   gboolean ignore = FALSE;
262   gint i, length, offset;
263
264   if (priv->ignore_ips)
265     {
266       GInetAddress *iaddr;
267
268       iaddr = g_inet_address_new_from_string (host);
269       if (iaddr)
270         {
271           for (i = 0; i < priv->ignore_ips->len; i++)
272             {
273               GInetAddressMask *mask = priv->ignore_ips->pdata[i];
274
275               if (g_inet_address_mask_matches (mask, iaddr))
276                 {
277                   ignore = TRUE;
278                   break;
279                 }
280             }
281
282           g_object_unref (iaddr);
283           if (ignore)
284             return TRUE;
285         }
286     }
287
288   if (priv->ignore_domains)
289     {
290       if (g_hostname_is_non_ascii (host))
291         host = ascii_host = g_hostname_to_ascii (host);
292       length = strlen (host);
293
294       for (i = 0; priv->ignore_domains[i].length; i++)
295         {
296           GSimpleProxyResolverDomain *domain = &priv->ignore_domains[i];
297
298           offset = length - domain->length;
299           if ((domain->port == 0 || domain->port == port) &&
300               (offset == 0 || (offset > 0 && host[offset - 1] == '.')) &&
301               (g_ascii_strcasecmp (domain->name, host + offset) == 0))
302             {
303               ignore = TRUE;
304               break;
305             }
306         }
307
308       g_free (ascii_host);
309     }
310
311   return ignore;
312 }
313
314 static gchar **
315 g_simple_proxy_resolver_lookup (GProxyResolver  *proxy_resolver,
316                                 const gchar     *uri,
317                                 GCancellable    *cancellable,
318                                 GError         **error)
319 {
320   GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (proxy_resolver);
321   GSimpleProxyResolverPrivate *priv = resolver->priv;
322   const gchar *proxy = NULL;
323   gchar **proxies;
324
325   if (priv->ignore_ips || priv->ignore_domains)
326     {
327       gchar *host = NULL;
328       gushort port;
329
330       if (_g_uri_parse_authority (uri, &host, &port, NULL) &&
331           ignore_host (resolver, host, port))
332         proxy = "direct://";
333
334       g_free (host);
335     }
336
337   if (!proxy && g_hash_table_size (priv->uri_proxies))
338     {
339       gchar *scheme = g_ascii_strdown (uri, strcspn (uri, ":"));
340
341       proxy = g_hash_table_lookup (priv->uri_proxies, scheme);
342       g_free (scheme);
343     }
344
345   if (!proxy)
346     proxy = priv->default_proxy;
347   if (!proxy)
348     proxy = "direct://";
349
350   if (!strncmp (proxy, "socks://", 8))
351     {
352       proxies = g_new0 (gchar *, 4);
353       proxies[0] = g_strdup_printf ("socks5://%s", proxy + 8);
354       proxies[1] = g_strdup_printf ("socks4a://%s", proxy + 8);
355       proxies[2] = g_strdup_printf ("socks4://%s", proxy + 8);
356       proxies[3] = NULL;
357     }
358   else
359     {
360       proxies = g_new0 (gchar *, 2);
361       proxies[0] = g_strdup (proxy);
362     }
363
364   return proxies;
365 }
366
367 static void
368 g_simple_proxy_resolver_lookup_async (GProxyResolver      *proxy_resolver,
369                                       const gchar         *uri,
370                                       GCancellable        *cancellable,
371                                       GAsyncReadyCallback  callback,
372                                       gpointer             user_data)
373 {
374   GSimpleProxyResolver *resolver = G_SIMPLE_PROXY_RESOLVER (proxy_resolver);
375   GTask *task;
376   GError *error = NULL;
377   char **proxies;
378
379   task = g_task_new (resolver, cancellable, callback, user_data);
380
381   proxies = g_simple_proxy_resolver_lookup (proxy_resolver, uri,
382                                             cancellable, &error);
383   if (proxies)
384     g_task_return_pointer (task, proxies, (GDestroyNotify)g_strfreev);
385   else
386     g_task_return_error (task, error);
387   g_object_unref (task);
388 }
389
390 static gchar **
391 g_simple_proxy_resolver_lookup_finish (GProxyResolver  *resolver,
392                                        GAsyncResult    *result,
393                                        GError         **error)
394 {
395   g_return_val_if_fail (g_task_is_valid (result, resolver), NULL);
396
397   return g_task_propagate_pointer (G_TASK (result), error);
398 }
399
400 static void
401 g_simple_proxy_resolver_class_init (GSimpleProxyResolverClass *resolver_class)
402 {
403   GObjectClass *object_class = G_OBJECT_CLASS (resolver_class);
404
405   g_type_class_add_private (resolver_class, sizeof (GSimpleProxyResolverPrivate));
406
407   object_class->get_property = g_simple_proxy_resolver_get_property;
408   object_class->set_property = g_simple_proxy_resolver_set_property;
409   object_class->finalize = g_simple_proxy_resolver_finalize;
410
411   /**
412    * GSimpleProxyResolver:default-proxy:
413    *
414    * The default proxy URI that will be used for any URI that doesn't
415    * match #GSimpleProxyResolver:ignore-hosts, and doesn't match any
416    * of the schemes set with g_simple_proxy_resolver_set_uri_proxy().
417    *
418    * Note that as a special case, if this URI starts with
419    * "<literal>socks://</literal>", #GSimpleProxyResolver will treat
420    * it as referring to all three of the <literal>socks5</literal>,
421    * <literal>socks4a</literal>, and <literal>socks4</literal> proxy
422    * types.
423    */
424   g_object_class_install_property (object_class, PROP_DEFAULT_PROXY,
425                                    g_param_spec_string ("default-proxy",
426                                                         P_("Default proxy"),
427                                                         P_("The default proxy URI"),
428                                                         NULL,
429                                                         G_PARAM_READWRITE |
430                                                         G_PARAM_STATIC_STRINGS));
431
432   /**
433    * GSimpleProxyResolver:ignore-hosts:
434    *
435    * A list of hostnames and IP addresses that the resolver should
436    * allow direct connections to.
437    *
438    * Entries can be in one of 4 formats:
439    *
440    * <itemizedlist>
441    *   <listitem>
442    *     A hostname, such as "<literal>example.com</literal>",
443    *     "<literal>.example.com</literal>", or
444    *     "<literal>*.example.com</literal>", any of which match
445    *     "<literal>example.com</literal>" or any subdomain of it.
446    *   </listitem>
447    *   <listitem>
448    *     An IPv4 or IPv6 address, such as
449    *     "<literal>192.168.1.1</literal>", which matches only
450    *     that address.
451    *   </listitem>
452    *   <listitem>
453    *     A hostname or IP address followed by a port, such as
454    *     "<literal>example.com:80</literal>", which matches whatever
455    *     the hostname or IP address would match, but only for URLs
456    *     with the (explicitly) indicated port. In the case of an IPv6
457    *     address, the address part must appear in brackets:
458    *     "<literal>[::1]:443</literal>"
459    *   </listitem>
460    *   <listitem>
461    *     An IP address range, given by a base address and prefix length,
462    *     such as "<literal>fe80::/10</literal>", which matches any
463    *     address in that range.
464    *   </listitem>
465    * </itemizedlist>
466    *
467    * Note that when dealing with Unicode hostnames, the matching is
468    * done against the ASCII form of the name.
469    *
470    * Also note that hostname exclusions apply only to connections made
471    * to hosts identified by name, and IP address exclusions apply only
472    * to connections made to hosts identified by address. That is, if
473    * <literal>example.com</literal> has an address of
474    * <literal>192.168.1.1</literal>, and the :ignore-hosts list
475    * contains only "<literal>192.168.1.1</literal>", then a connection
476    * to "<literal>example.com</literal>" (eg, via a #GNetworkAddress)
477    * will use the proxy, and a connection to
478    * "<literal>192.168.1.1</literal>" (eg, via a #GInetSocketAddress)
479    * will not.
480    *
481    * These rules match the "ignore-hosts"/"noproxy" rules most
482    * commonly used by other applications.
483    */
484   g_object_class_install_property (object_class, PROP_IGNORE_HOSTS,
485                                    g_param_spec_boxed ("ignore-hosts",
486                                                        P_("Ignore hosts"),
487                                                        P_("Hosts that will not use the proxy"),
488                                                        G_TYPE_STRV,
489                                                        G_PARAM_READWRITE |
490                                                        G_PARAM_STATIC_STRINGS));
491
492 }
493
494 static void
495 g_simple_proxy_resolver_iface_init (GProxyResolverInterface *iface)
496 {
497   iface->lookup = g_simple_proxy_resolver_lookup;
498   iface->lookup_async = g_simple_proxy_resolver_lookup_async;
499   iface->lookup_finish = g_simple_proxy_resolver_lookup_finish;
500 }
501
502 /**
503  * g_simple_proxy_resolver_new:
504  * @default_proxy: (allow-none): the default proxy to use, eg
505  *   "socks://192.168.1.1"
506  * @ignore_hosts: (allow-none): an optional list of hosts/IP addresses
507  *   to not use a proxy for.
508  *
509  * Creates a new #GSimpleProxyResolver. See
510  * #GSimpleProxyResolver:default-proxy and
511  * #GSimpleProxyResolver:ignore-hosts for more details on how the
512  * arguments are interpreted.
513  *
514  * Returns: a new #GSimpleProxyResolver
515  *
516  * Since: 2.36
517  */
518 GProxyResolver *
519 g_simple_proxy_resolver_new (const gchar  *default_proxy,
520                              gchar       **ignore_hosts)
521 {
522   return g_object_new (G_TYPE_SIMPLE_PROXY_RESOLVER,
523                        "default-proxy", default_proxy,
524                        "ignore-hosts", ignore_hosts,
525                        NULL);
526 }
527
528 /**
529  * g_simple_proxy_resolver_set_default_proxy:
530  * @resolver: a #GSimpleProxyResolver
531  * @default_proxy: the default proxy to use
532  *
533  * Sets the default proxy on @resolver, to be used for any URIs that
534  * don't match #GSimpleProxyResolver:ignore-hosts or a proxy set
535  * via g_simple_proxy_resolver_set_uri_proxy().
536  *
537  * If @default_proxy starts with "<literal>socks://</literal>",
538  * #GSimpleProxyResolver will treat it as referring to all three of
539  * the <literal>socks5</literal>, <literal>socks4a</literal>, and
540  * <literal>socks4</literal> proxy types.
541  *
542  * Since: 2.36
543  */
544 void
545 g_simple_proxy_resolver_set_default_proxy (GSimpleProxyResolver  *resolver,
546                                            const gchar           *default_proxy)
547 {
548   g_return_if_fail (G_IS_SIMPLE_PROXY_RESOLVER (resolver));
549
550   g_free (resolver->priv->default_proxy);
551   resolver->priv->default_proxy = g_strdup (default_proxy);
552   g_object_notify (G_OBJECT (resolver), "default-proxy");
553 }
554
555 void
556 g_simple_proxy_resolver_set_ignore_hosts  (GSimpleProxyResolver  *resolver,
557                                            gchar                **ignore_hosts)
558 {
559   g_return_if_fail (G_IS_SIMPLE_PROXY_RESOLVER (resolver));
560
561   g_strfreev (resolver->priv->ignore_hosts);
562   resolver->priv->ignore_hosts = g_strdupv (ignore_hosts);
563   reparse_ignore_hosts (resolver);
564   g_object_notify (G_OBJECT (resolver), "ignore-hosts");
565 }
566
567 /**
568  * g_simple_proxy_resolver_set_uri_proxy:
569  * @resolver: a #GSimpleProxyResolver
570  * @uri_scheme: the URI scheme to add a proxy for
571  * @proxy: the proxy to use for @uri_scheme
572  *
573  * Adds a URI-scheme-specific proxy to @resolver; URIs whose scheme
574  * matches @uri_scheme (and which don't match
575  * #GSimpleProxyResolver:ignore-hosts) will be proxied via @proxy.
576  *
577  * As with #GSimpleProxyResolver:default-proxy, if @proxy starts with
578  * "<literal>socks://</literal>", #GSimpleProxyResolver will treat it
579  * as referring to all three of the <literal>socks5</literal>,
580  * <literal>socks4a</literal>, and <literal>socks4</literal> proxy
581  * types.
582  *
583  * Since: 2.36
584  */
585 void
586 g_simple_proxy_resolver_set_uri_proxy (GSimpleProxyResolver *resolver,
587                                        const gchar          *uri_scheme,
588                                        const gchar          *proxy)
589 {
590   g_return_if_fail (G_IS_SIMPLE_PROXY_RESOLVER (resolver));
591
592   g_hash_table_replace (resolver->priv->uri_proxies,
593                         g_ascii_strdown (uri_scheme, -1),
594                         g_strdup (proxy));
595 }