summaryrefslogtreecommitdiff
path: root/libsoup/soup-tld.c
blob: 2e3da62da9b2718bd89c3b172edb550c3641e09b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * soup-tld.c
 *
 * Copyright (C) 2012 Igalia S.L.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include <glib/gi18n-lib.h>

#include "soup-tld.h"
#include "soup.h"
#include "soup-tld-private.h"

/**
 * SECTION:soup-tld
 * @short_description: Top-Level Domain Utilities
 *
 * These functions can be used to parse hostnames to attempt to determine
 * what part of the name belongs to the domain owner, and what part is
 * simply a "public suffix" such as ".com".
 */

static void soup_tld_ensure_rules_hash_table (void);
static const char *soup_tld_get_base_domain_internal (const char *hostname,
						      guint       additional_domains,
						      GError    **error);

static GHashTable *rules = NULL;
static SoupTLDEntry tld_entries[] = {
#include "tld_data.inc"
};

/* Stores the entries data in a hash table to ease and speed up
 * searches.
 */
static void
soup_tld_ensure_rules_hash_table (void)
{
	static gsize init = 0;

	if (g_once_init_enter (&init)) {
		int i;

		rules = g_hash_table_new (g_str_hash, g_str_equal);
		for (i = 0; i < G_N_ELEMENTS (tld_entries); ++i)
			g_hash_table_insert (rules, tld_entries[i].domain,
					     &(tld_entries[i].flags));
		g_once_init_leave (&init, 1);
	}
}

/**
 * soup_tld_get_base_domain:
 * @hostname: a hostname
 * @error: return location for a #GError, or %NULL to ignore
 *   errors. See #SoupTLDError for the available error codes
 *
 * Finds the base domain for a given @hostname. The base domain is
 * composed by the top level domain (such as .org, .com, .co.uk, etc)
 * plus the second level domain, for example for myhost.mydomain.com
 * it will return mydomain.com.
 *
 * Note that %NULL will be returned for private URLs (those not ending
 * with any well known TLD) because choosing a base domain for them
 * would be totally arbitrary.
 *
 * Prior to libsoup 2.46, this function required that @hostname be in
 * UTF-8 if it was an IDN. From 2.46 on, the name can be in either
 * UTF-8 or ASCII format (and the return value will be in the same
 * format).
 *
 * Returns: a pointer to the start of the base domain in @hostname. If
 * an error occurs, %NULL will be returned and @error set.
 *
 * Since: 2.40
 **/
const char *
soup_tld_get_base_domain (const char *hostname, GError **error)
{
	g_return_val_if_fail (hostname, NULL);

	return soup_tld_get_base_domain_internal (hostname, 1, error);
}

/**
 * soup_tld_domain_is_public_suffix:
 * @domain: a domain name
 *
 * Looks whether the @domain passed as argument is a public domain
 * suffix (.org, .com, .co.uk, etc) or not.
 *
 * Prior to libsoup 2.46, this function required that @domain be in
 * UTF-8 if it was an IDN. From 2.46 on, the name can be in either
 * UTF-8 or ASCII format (and the return value will be in the same
 * format).
 *
 * Returns: %TRUE if it is a public domain, %FALSE otherwise.
 *
 * Since: 2.40
 **/
gboolean
soup_tld_domain_is_public_suffix (const char *domain)
{
	const char *base_domain;
	GError *error = NULL;

	g_return_val_if_fail (domain, FALSE);

	/* Skip the leading '.' if present */
	if (*domain == '.' && !*(++domain))
		g_return_val_if_reached (FALSE);

	base_domain = soup_tld_get_base_domain_internal (domain, 0, &error);
	if (g_strcmp0 (domain, base_domain)) {
		g_clear_error (&error);
		return FALSE;
	}

	if (g_error_matches (error, SOUP_TLD_ERROR, SOUP_TLD_ERROR_NO_BASE_DOMAIN)) {
		g_error_free (error);
		return FALSE;
	}

	if (g_error_matches (error, SOUP_TLD_ERROR, SOUP_TLD_ERROR_IS_IP_ADDRESS) ||
	    g_error_matches (error, SOUP_TLD_ERROR, SOUP_TLD_ERROR_INVALID_HOSTNAME)) {
		g_error_free (error);
		g_return_val_if_reached (FALSE);
	}

	g_clear_error (&error);

	return TRUE;
}

/**
 * SOUP_TLD_ERROR:
 *
 * The #GError domain for soup-tld-related errors.
 *
 * Since: 2.40
 */
/**
 * SoupTLDError:
 * @SOUP_TLD_ERROR_INVALID_HOSTNAME: A hostname was syntactically
 *   invalid.
 * @SOUP_TLD_ERROR_IS_IP_ADDRESS: The passed-in "hostname" was
 *   actually an IP address (and thus has no base domain or
 *   public suffix).
 * @SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS: The passed-in hostname
 *   did not have enough components. Eg, calling
 *   soup_tld_get_base_domain() on <literal>"co.uk"</literal>.
 * @SOUP_TLD_ERROR_NO_BASE_DOMAIN: The passed-in hostname has
 *   no recognized public suffix.
 *
 * Error codes for %SOUP_TLD_ERROR.
 *
 * Since: 2.40
 */

GQuark
soup_tld_error_quark (void)
{
	static GQuark error;
	if (!error)
		error = g_quark_from_static_string ("soup_tld_error_quark");
	return error;
}

static const char *
soup_tld_get_base_domain_internal (const char *hostname, guint additional_domains, GError **error)
{
	char *prev_domain, *cur_domain, *next_dot;
	gint add_domains;
	const char *orig_hostname = NULL, *tld;
	char *utf8_hostname = NULL;

	soup_tld_ensure_rules_hash_table ();

	if (g_hostname_is_ip_address (hostname)) {
		g_set_error_literal (error, SOUP_TLD_ERROR,
				     SOUP_TLD_ERROR_IS_IP_ADDRESS,
				     _("Hostname is an IP address"));
		return NULL;
	}

	if (g_hostname_is_ascii_encoded (hostname)) {
		orig_hostname = hostname;
		hostname = utf8_hostname = g_hostname_to_unicode (hostname);
		if (!hostname) {
			g_set_error_literal (error, SOUP_TLD_ERROR,
					     SOUP_TLD_ERROR_INVALID_HOSTNAME,
					     _("Invalid hostname"));
			return NULL;
		}
	}

	cur_domain = (char *) hostname;
	tld = cur_domain;
	prev_domain = NULL;
	/* Process matching rules from longest to shortest. Logic
	 * based on Mozilla's implementation of nsEffectiveTLDService.
	 */
	while (TRUE) {
		char *orig_domain;
		gboolean domain_found;
		int *flags;

		/* Valid hostnames neither start with a dot nor have more than one
		 * dot together.
		 */
		if (*cur_domain == '.') {
			g_set_error_literal (error, SOUP_TLD_ERROR,
					     SOUP_TLD_ERROR_INVALID_HOSTNAME,
					     _("Invalid hostname"));
			g_free (utf8_hostname);
			return NULL;
		}

		next_dot = strchr (cur_domain, '.');
		domain_found = g_hash_table_lookup_extended (rules, cur_domain, (gpointer *) &orig_domain, (gpointer *) &flags);
		/* We compare the keys just to be sure that we haven't hit a collision */
		if (domain_found && !strncmp (orig_domain, cur_domain, strlen (orig_domain))) {
			if (*flags & SOUP_TLD_RULE_MATCH_ALL) {
				/* If we match a *. rule and there were no previous exceptions
				 * nor previous domains then treat it as an exact match.
				 */
				tld = prev_domain ? prev_domain : cur_domain;
				break;
			} else if (*flags == SOUP_TLD_RULE_NORMAL) {
				tld = cur_domain;
				break;
			} else if (*flags & SOUP_TLD_RULE_EXCEPTION) {
				tld = next_dot + 1;
				break;
			}
		}

		/* If we hit the top and haven't matched yet, then it
		 * has no public suffix.
		 */
		if (!next_dot) {
			g_set_error_literal (error, SOUP_TLD_ERROR,
					     SOUP_TLD_ERROR_NO_BASE_DOMAIN,
					     _("Hostname has no base domain"));
			g_free (utf8_hostname);
			return NULL;
		}

		prev_domain = cur_domain;
		cur_domain = next_dot + 1;
	}

	if (orig_hostname) {
		int dots;
		const char *p;

		/* Count the number of dots that appear after tld in
		 * utf8_hostname, and then find the corresponding spot
		 * in orig_hostname;
		 */
		for (p = tld, dots = 0; *p; p++) {
			if (*p == '.')
				dots++;
		}

		for (p = orig_hostname + strlen (orig_hostname); p > orig_hostname; p--) {
			if (*(p - 1) == '.') {
				if (dots)
					dots--;
				else
					break;
			}
		}
		/* It's not possible for utf8_hostname to have had
		 * more dots than orig_hostname.
		 */
		g_assert (dots == 0);

		tld = p;
		g_free (utf8_hostname);
		hostname = orig_hostname;
	}

	/* Include the additional number of domains requested. */
	add_domains = additional_domains;
	while (tld != hostname) {
		if (*(--tld) == '.' && (!(add_domains--))) {
			++add_domains;
			++tld;
			break;
		}
	}

	/* If additional_domains > 0 then we haven't found enough additional domains. */
	if (add_domains) {
		g_set_error_literal (error, SOUP_TLD_ERROR,
				     SOUP_TLD_ERROR_NOT_ENOUGH_DOMAINS,
				     _("Not enough domains"));
		return NULL;
	}

	return tld;
}