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