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