e-mail-autoconfig.c 31.1 KB
Newer Older
1 2 3
/*
 * e-mail-autoconfig.c
 *
4 5 6 7 8 9 10 11 12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
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
/* XXX Thoughts on RFC 6186: Use of SRV Records for Locating Email
 *                           Submission/Access Services
 *
 *     RFC 6186 specifies using SRV DNS lookups to aid in automatic
 *     configuration of mail accounts.  While it may be tempting to
 *     implement the RFC here (I was tempted at least), upon closer
 *     examination I find the RFC to be insufficient.
 *
 *     An SRV DNS lookup only provides a host name and port number.
 *     The RFC assumes the account's user name can be derived from
 *     the email address, and suggests probing the mail server for
 *     a valid user name by actually attempting authentication,
 *     first with the user's full email address and then falling
 *     back to only the local part.
 *
 *     I'm uncomfortable with this for a number of reasons:
 *
 *     1) I would prefer the user have a chance to manually review
 *        the settings before transmitting credentials of any kind,
 *        since DNS responses can be spoofed.
 *
 *     2) Authentication at this phase would require asking for
 *        a password either before or during auto-configuration.
 *        Asking before assumes a password-based authentication
 *        mechanism is to be used, which is not always the case,
 *        and asking during may raise the user's suspicion about
 *        what's going on behind the scenes (it would mine).
 *
 *     3) For better or worse, our architecture isn't really built
 *        to handle authentication at this stage.  EMailSession is
 *        wired into too many other areas to be reused here without
 *        risking unwanted side-effects, therefore it would require
 *        a custom CamelSession subclass with an authenticate_sync()
 *        implementation similar to EMailSession.
 *
 * While the technical limitations of (3) could be overcome, my concerns
 * in (1) and (2) still stand.  I think for the time being a better solution
 * is to have an administrator script on api.gnome.org that compares the host
 * and port settings in each clientConfig file to the _imap._tcp, _pop3._tcp,
 * and _submission._tcp SRV records for that service provider (if available)
 * to help ensure the static XML content remains accurate.  It would also be
 * instructive to track how many service providers even implement RFC 6186.
 *
 * Recording my thoughts here for posterity. -- mbarnes
 */

Milan Crha's avatar
Milan Crha committed
64
#include "evolution-config.h"
65 66 67 68 69 70 71

#include <string.h>
#include <glib/gi18n-lib.h>

/* For error codes. */
#include <libsoup/soup.h>

Milan Crha's avatar
Milan Crha committed
72 73
#include "e-mail-autoconfig.h"

74 75 76 77 78
#define E_MAIL_AUTOCONFIG_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), E_TYPE_MAIL_AUTOCONFIG, EMailAutoconfigPrivate))

#define AUTOCONFIG_BASE_URI \
79
	"https://api.gnome.org/evolution/autoconfig/1.1/"
80 81 82 83

#define ERROR_IS_NOT_FOUND(error) \
	(g_error_matches ((error), SOUP_HTTP_ERROR, SOUP_STATUS_NOT_FOUND))

84
typedef struct _EMailAutoconfigResult EMailAutoconfigResult;
85 86
typedef struct _ParserClosure ParserClosure;

87 88 89 90 91
struct _EMailAutoconfigResult {
	gboolean set;
	gchar *user;
	gchar *host;
	guint16 port;
92
	gchar *auth_mechanism;
93 94 95
	CamelNetworkSecurityMethod security_method;
};

96
struct _EMailAutoconfigPrivate {
97
	ESourceRegistry *registry;
98 99 100
	gchar *email_address;
	gchar *email_local_part;
	gchar *email_domain_part;
101 102 103
	EMailAutoconfigResult imap_result;
	EMailAutoconfigResult pop3_result;
	EMailAutoconfigResult smtp_result;
104 105 106
};

struct _ParserClosure {
107 108
	EMailAutoconfig *autoconfig;
	EMailAutoconfigResult *result;
109 110 111 112
};

enum {
	PROP_0,
113 114
	PROP_EMAIL_ADDRESS,
	PROP_REGISTRY
115 116 117
};

/* Forward Declarations */
118
static void	e_mail_autoconfig_initable_init	(GInitableIface *iface);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

/* By default, the GAsyncInitable interface calls GInitable.init()
 * from a separate thread, so we only have to override GInitable. */
G_DEFINE_TYPE_WITH_CODE (
	EMailAutoconfig,
	e_mail_autoconfig,
	G_TYPE_OBJECT,
	G_IMPLEMENT_INTERFACE (
		G_TYPE_INITABLE, e_mail_autoconfig_initable_init)
	G_IMPLEMENT_INTERFACE (
		G_TYPE_ASYNC_INITABLE, NULL))

static void
mail_autoconfig_parse_start_element (GMarkupParseContext *context,
                                     const gchar *element_name,
                                     const gchar **attribute_names,
                                     const gchar **attribute_values,
                                     gpointer user_data,
                                     GError **error)
{
	ParserClosure *closure = user_data;
140
	EMailAutoconfigPrivate *priv;
141 142 143
	gboolean is_incoming_server;
	gboolean is_outgoing_server;

144 145
	priv = closure->autoconfig->priv;

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	is_incoming_server = g_str_equal (element_name, "incomingServer");
	is_outgoing_server = g_str_equal (element_name, "outgoingServer");

	if (is_incoming_server || is_outgoing_server) {
		const gchar *type = NULL;

		g_markup_collect_attributes (
			element_name,
			attribute_names,
			attribute_values,
			error,
			G_MARKUP_COLLECT_STRING,
			"type", &type,
			G_MARKUP_COLLECT_INVALID);

161 162 163 164 165 166
		if (g_strcmp0 (type, "imap") == 0)
			closure->result = &priv->imap_result;
		if (g_strcmp0 (type, "pop3") == 0)
			closure->result = &priv->pop3_result;
		if (g_strcmp0 (type, "smtp") == 0)
			closure->result = &priv->smtp_result;
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
	}
}

static void
mail_autoconfig_parse_end_element (GMarkupParseContext *context,
                                   const gchar *element_name,
                                   gpointer user_data,
                                   GError **error)
{
	ParserClosure *closure = user_data;
	gboolean is_incoming_server;
	gboolean is_outgoing_server;

	is_incoming_server = g_str_equal (element_name, "incomingServer");
	is_outgoing_server = g_str_equal (element_name, "outgoingServer");

	if (is_incoming_server || is_outgoing_server)
184
		closure->result = NULL;
185 186 187 188 189 190 191 192 193 194
}

static void
mail_autoconfig_parse_text (GMarkupParseContext *context,
                            const gchar *text,
                            gsize text_length,
                            gpointer user_data,
                            GError **error)
{
	ParserClosure *closure = user_data;
195
	EMailAutoconfigPrivate *priv;
196 197 198
	const gchar *element_name;
	GString *string;

199 200 201
	priv = closure->autoconfig->priv;

	if (closure->result == NULL)
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
		return;

	/* Perform the following text substitutions:
	 *
	 * %EMAILADDRESS%    :  closure->email_address
	 * %EMAILLOCALPART%  :  closure->email_local_part
	 * %EMAILDOMAIN%     :  closure->email_domain_part
	 */
	if (strchr (text, '%') == NULL)
		string = g_string_new (text);
	else {
		const gchar *cp = text;

		string = g_string_sized_new (256);
		while (*cp != '\0') {
			const gchar *variable;
			const gchar *substitute;

			if (*cp != '%') {
				g_string_append_c (string, *cp++);
				continue;
			}

			variable = "%EMAILADDRESS%";
226
			substitute = priv->email_address;
227 228 229 230 231 232 233 234

			if (strncmp (cp, variable, strlen (variable)) == 0) {
				g_string_append (string, substitute);
				cp += strlen (variable);
				continue;
			}

			variable = "%EMAILLOCALPART%";
235
			substitute = priv->email_local_part;
236 237 238 239 240 241 242 243

			if (strncmp (cp, variable, strlen (variable)) == 0) {
				g_string_append (string, substitute);
				cp += strlen (variable);
				continue;
			}

			variable = "%EMAILDOMAIN%";
244
			substitute = priv->email_domain_part;
245 246 247 248 249 250 251 252 253 254 255 256 257 258

			if (strncmp (cp, variable, strlen (variable)) == 0) {
				g_string_append (string, substitute);
				cp += strlen (variable);
				continue;
			}

			g_string_append_c (string, *cp++);
		}
	}

	element_name = g_markup_parse_context_get_element (context);

	if (g_str_equal (element_name, "hostname")) {
259 260
		closure->result->host = g_strdup (string->str);
		closure->result->set = TRUE;
261 262

	} else if (g_str_equal (element_name, "username")) {
263 264
		closure->result->user = g_strdup (string->str);
		closure->result->set = TRUE;
265 266 267 268

	} else if (g_str_equal (element_name, "port")) {
		glong port = strtol (string->str, NULL, 10);
		if (port == CLAMP (port, 1, G_MAXUINT16)) {
269 270
			closure->result->port = (guint16) port;
			closure->result->set = TRUE;
271 272 273 274
		}

	} else if (g_str_equal (element_name, "socketType")) {
		if (g_str_equal (string->str, "plain")) {
275 276 277
			closure->result->security_method =
				CAMEL_NETWORK_SECURITY_METHOD_NONE;
			closure->result->set = TRUE;
278
		} else if (g_str_equal (string->str, "SSL")) {
279 280 281
			closure->result->security_method =
				CAMEL_NETWORK_SECURITY_METHOD_SSL_ON_ALTERNATE_PORT;
			closure->result->set = TRUE;
282
		} else if (g_str_equal (string->str, "STARTTLS")) {
283 284 285
			closure->result->security_method =
				CAMEL_NETWORK_SECURITY_METHOD_STARTTLS_ON_STANDARD_PORT;
			closure->result->set = TRUE;
286 287
		}

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	} else if (g_str_equal (element_name, "authentication")) {
		gboolean use_plain_auth = FALSE;

		/* "password-cleartext" and "plain" are synonymous. */

		if (g_str_equal (string->str, "password-cleartext"))
			use_plain_auth = TRUE;

		if (g_str_equal (string->str, "plain"))
			use_plain_auth = TRUE;

		if (use_plain_auth) {
			gchar *auth_mechanism = NULL;

			/* The exact auth name depends on the protocol. */

			/* Leave this NULL for IMAP so Camel
			 * will issue an IMAP LOGIN command. */
			if (closure->result == &priv->imap_result)
				auth_mechanism = NULL;

			/* Leave this NULL for POP3 so Camel
			 * will issue POP3 USER/PASS commands. */
			if (closure->result == &priv->pop3_result)
				auth_mechanism = NULL;

			if (closure->result == &priv->smtp_result)
				auth_mechanism = g_strdup ("LOGIN");

			closure->result->auth_mechanism = auth_mechanism;
			closure->result->set = TRUE;
		}

321 322 323 324 325 326 327 328
		/* "password-encrypted" apparently maps to CRAM-MD5,
		 * or at least that's how Thunderbird interprets it. */

		if (g_str_equal (string->str, "password-encrypted")) {
			closure->result->auth_mechanism = g_strdup ("CRAM-MD5");
			closure->result->set = TRUE;
		}

329 330 331
		/* XXX Other <authentication> values not handled,
		 *     but they are corner cases for the most part. */
	}
332 333 334 335 336 337 338 339 340 341

	g_string_free (string, TRUE);
}

static GMarkupParser mail_autoconfig_parser = {
	mail_autoconfig_parse_start_element,
	mail_autoconfig_parse_end_element,
	mail_autoconfig_parse_text
};

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
static gchar *
mail_autoconfig_resolve_name_server (const gchar *domain,
                                     GCancellable *cancellable,
                                     GError **error)
{
	GResolver *resolver;
	GList *records;
	gchar *name_server = NULL;

	resolver = g_resolver_get_default ();

	records = g_resolver_lookup_records (
		resolver, domain, G_RESOLVER_RECORD_NS, cancellable, error);

	/* This list is sorted per RFC 2782, so use the first item. */
	if (records != NULL) {
		GVariant *variant = records->data;
		g_variant_get_child (variant, 0, "s", &name_server);
	}

	g_list_free_full (records, (GDestroyNotify) g_variant_unref);

	g_object_unref (resolver);

	return name_server;
}

static void
mail_autoconfig_abort_soup_session_cb (GCancellable *cancellable,
                                       SoupSession *soup_session)
{
	soup_session_abort (soup_session);
}

static gboolean
mail_autoconfig_lookup (EMailAutoconfig *autoconfig,
                        const gchar *domain,
                        GCancellable *cancellable,
                        GError **error)
{
	GMarkupParseContext *context;
383 384
	ESourceRegistry *registry;
	ESource *proxy_source;
385 386 387 388 389 390 391
	SoupMessage *soup_message;
	SoupSession *soup_session;
	ParserClosure closure;
	gulong cancel_id = 0;
	gboolean success;
	guint status;
	gchar *uri;
392

393 394
	registry = e_mail_autoconfig_get_registry (autoconfig);
	proxy_source = e_source_registry_ref_builtin_proxy (registry);
395

396 397 398 399
	soup_session = soup_session_new_with_options (
		SOUP_SESSION_PROXY_RESOLVER,
		G_PROXY_RESOLVER (proxy_source),
		NULL);
400

401
	g_object_unref (proxy_source);
402

403
	uri = g_strconcat (AUTOCONFIG_BASE_URI, domain, NULL);
404

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	soup_message = soup_message_new (SOUP_METHOD_GET, uri);
	g_free (uri);

	if (G_IS_CANCELLABLE (cancellable))
		cancel_id = g_cancellable_connect (
			cancellable,
			G_CALLBACK (mail_autoconfig_abort_soup_session_cb),
			g_object_ref (soup_session),
			(GDestroyNotify) g_object_unref);

	status = soup_session_send_message (soup_session, soup_message);

	if (cancel_id > 0)
		g_cancellable_disconnect (cancellable, cancel_id);

	success = SOUP_STATUS_IS_SUCCESSFUL (status);

	if (!success) {
		g_set_error_literal (
			error, SOUP_HTTP_ERROR,
			soup_message->status_code,
			soup_message->reason_phrase);
		goto exit;
	}

	closure.autoconfig = autoconfig;
	closure.result = NULL;

	context = g_markup_parse_context_new (
		&mail_autoconfig_parser, 0,
		&closure, (GDestroyNotify) NULL);

	success = g_markup_parse_context_parse (
		context,
		soup_message->response_body->data,
		soup_message->response_body->length,
		error);

	if (success)
		success = g_markup_parse_context_end_parse (context, error);

	g_markup_parse_context_free (context);

exit:
	g_object_unref (soup_message);
	g_object_unref (soup_session);

	return success;
}

455
static gboolean
456
mail_autoconfig_set_details (EMailAutoconfigResult *result,
457 458 459 460 461 462 463
                             ESource *source,
                             const gchar *extension_name)
{
	ESourceCamel *camel_ext;
	ESourceBackend *backend_ext;
	CamelSettings *settings;
	const gchar *backend_name;
464 465 466 467 468

	g_return_val_if_fail (result != NULL, FALSE);

	if (!result->set)
		return FALSE;
469 470 471 472 473 474 475 476 477 478 479 480

	if (!e_source_has_extension (source, extension_name))
		return FALSE;

	backend_ext = e_source_get_extension (source, extension_name);
	backend_name = e_source_backend_get_backend_name (backend_ext);
	extension_name = e_source_camel_get_extension_name (backend_name);
	camel_ext = e_source_get_extension (source, extension_name);

	settings = e_source_camel_get_settings (camel_ext);
	g_return_val_if_fail (CAMEL_IS_NETWORK_SETTINGS (settings), FALSE);

481 482 483 484 485
	g_object_set (
		settings,
		"user", result->user,
		"host", result->host,
		"port", result->port,
486
		"auth-mechanism", result->auth_mechanism,
487 488
		"security-method", result->security_method,
		NULL);
489

490
	return TRUE;
491 492
}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
#define E_TYPE_MAIL_CONFIG_LOOKUP_RESULT \
	(e_mail_config_lookup_result_get_type ())
#define E_MAIL_CONFIG_LOOKUP_RESULT(obj) \
	(G_TYPE_CHECK_INSTANCE_CAST \
	((obj), E_TYPE_MAIL_CONFIG_LOOKUP_RESULT, EMailConfigLookupResult))
#define E_IS_MAIL_CONFIG_LOOKUP_RESULT(obj) \
	(G_TYPE_CHECK_INSTANCE_TYPE \
	((obj), E_TYPE_MAIL_CONFIG_LOOKUP_RESULT))

typedef struct _EMailConfigLookupResult EMailConfigLookupResult;
typedef struct _EMailConfigLookupResultClass EMailConfigLookupResultClass;

struct _EMailConfigLookupResult {
	/*< private >*/
	EConfigLookupResultSimple parent;

	EMailAutoconfigResult result;
	gchar *extension_name;
};

struct _EMailConfigLookupResultClass {
	/*< private >*/
	EConfigLookupResultSimpleClass parent_class;
};

GType e_mail_config_lookup_result_get_type (void) G_GNUC_CONST;

G_DEFINE_TYPE (EMailConfigLookupResult, e_mail_config_lookup_result, E_TYPE_CONFIG_LOOKUP_RESULT_SIMPLE)

static gboolean
mail_config_lookup_result_configure_source (EConfigLookupResult *lookup_result,
					    ESource *source)
{
	EMailConfigLookupResult *mail_result;

	g_return_val_if_fail (E_IS_MAIL_CONFIG_LOOKUP_RESULT (lookup_result), FALSE);

	mail_result = E_MAIL_CONFIG_LOOKUP_RESULT (lookup_result);

	/* No chain up to parent method, not needed here, because not used */
	return mail_autoconfig_set_details (&mail_result->result, source, mail_result->extension_name);
}

static void
mail_config_lookup_result_finalize (GObject *object)
{
	EMailConfigLookupResult *mail_result = E_MAIL_CONFIG_LOOKUP_RESULT (object);

	g_free (mail_result->result.user);
	g_free (mail_result->result.host);
	g_free (mail_result->result.auth_mechanism);
	g_free (mail_result->extension_name);

	/* Chain up to parent's method. */
	G_OBJECT_CLASS (e_mail_config_lookup_result_parent_class)->finalize (object);
}

static void
e_mail_config_lookup_result_class_init (EMailConfigLookupResultClass *klass)
{
	EConfigLookupResultSimpleClass *simple_result_class;
	GObjectClass *object_class;

	object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = mail_config_lookup_result_finalize;

	simple_result_class = E_CONFIG_LOOKUP_RESULT_SIMPLE_CLASS (klass);
	simple_result_class->configure_source = mail_config_lookup_result_configure_source;
}

static void
e_mail_config_lookup_result_init (EMailConfigLookupResult *mail_result)
{
}

static EConfigLookupResult *
e_mail_config_lookup_result_new (EConfigLookupResultKind kind,
				 gint priority,
				 const gchar *protocol,
				 const gchar *display_name,
				 const gchar *description,
				 const EMailAutoconfigResult *result,
				 const gchar *extension_name)
{
	EMailConfigLookupResult *mail_result;

	g_return_val_if_fail (protocol != NULL, NULL);
	g_return_val_if_fail (display_name != NULL, NULL);
	g_return_val_if_fail (description != NULL, NULL);
	g_return_val_if_fail (result != NULL, NULL);
	g_return_val_if_fail (extension_name != NULL, NULL);

	mail_result = g_object_new (E_TYPE_MAIL_CONFIG_LOOKUP_RESULT,
		"kind", kind,
		"priority", priority,
588
		"is-complete", TRUE,
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
		"protocol", protocol,
		"display-name", display_name,
		"description", description,
		NULL);

	mail_result->result.set = result->set;
	mail_result->result.user = g_strdup (result->user);
	mail_result->result.host = g_strdup (result->host);
	mail_result->result.port = result->port;
	mail_result->result.auth_mechanism = g_strdup (result->auth_mechanism);
	mail_result->result.security_method = result->security_method;
	mail_result->extension_name = g_strdup (extension_name);

	return E_CONFIG_LOOKUP_RESULT (mail_result);
}

static void
mail_autoconfig_result_to_config_lookup (EMailAutoconfig *mail_autoconfig,
					 EConfigLookup *config_lookup,
					 EMailAutoconfigResult *result,
					 gint priority,
					 const gchar *protocol,
					 const gchar *display_name,
					 const gchar *extension_name)
{
	EConfigLookupResult *lookup_result;
	EConfigLookupResultKind kind;
	GString *description;

	g_return_if_fail (E_IS_MAIL_AUTOCONFIG (mail_autoconfig));
	g_return_if_fail (E_IS_CONFIG_LOOKUP (config_lookup));
	g_return_if_fail (result != NULL);
	g_return_if_fail (protocol != NULL);
	g_return_if_fail (display_name != NULL);
	g_return_if_fail (extension_name != NULL);

	if (!result->set)
		return;

	kind = E_CONFIG_LOOKUP_RESULT_MAIL_RECEIVE;
	if (g_strcmp0 (extension_name, E_SOURCE_EXTENSION_MAIL_TRANSPORT) == 0)
		kind = E_CONFIG_LOOKUP_RESULT_MAIL_SEND;

	description = g_string_new ("");

	g_string_append_printf (description, _("Host: %s:%d"), result->host, result->port);

	if (result->user && *result->user) {
		g_string_append_c (description, '\n');
		g_string_append_printf (description, _("User: %s"), result->user);
	}

	g_string_append_c (description, '\n');
	g_string_append_printf (description, _("Security method: %s"),
		result->security_method == CAMEL_NETWORK_SECURITY_METHOD_SSL_ON_ALTERNATE_PORT ?  _("TLS") :
		result->security_method == CAMEL_NETWORK_SECURITY_METHOD_STARTTLS_ON_STANDARD_PORT ? _("STARTTLS") : _("None"));

	if (result->auth_mechanism && *result->auth_mechanism) {
		g_string_append_c (description, '\n');
		g_string_append_printf (description, _("Authentication mechanism: %s"), result->auth_mechanism);
	}

	lookup_result = e_mail_config_lookup_result_new (kind, priority, protocol, display_name, description->str, result, extension_name);
	e_config_lookup_add_result (config_lookup, lookup_result);

	g_string_free (description, TRUE);
}

657 658 659 660 661 662 663 664 665 666
static void
mail_autoconfig_set_email_address (EMailAutoconfig *autoconfig,
                                   const gchar *email_address)
{
	g_return_if_fail (email_address != NULL);
	g_return_if_fail (autoconfig->priv->email_address == NULL);

	autoconfig->priv->email_address = g_strdup (email_address);
}

667 668 669 670 671 672 673 674 675 676
static void
mail_autoconfig_set_registry (EMailAutoconfig *autoconfig,
                              ESourceRegistry *registry)
{
	g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
	g_return_if_fail (autoconfig->priv->registry == NULL);

	autoconfig->priv->registry = g_object_ref (registry);
}

677 678 679 680 681 682 683 684 685 686 687 688
static void
mail_autoconfig_set_property (GObject *object,
                              guint property_id,
                              const GValue *value,
                              GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_EMAIL_ADDRESS:
			mail_autoconfig_set_email_address (
				E_MAIL_AUTOCONFIG (object),
				g_value_get_string (value));
			return;
689 690 691 692 693 694

		case PROP_REGISTRY:
			mail_autoconfig_set_registry (
				E_MAIL_AUTOCONFIG (object),
				g_value_get_object (value));
			return;
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
mail_autoconfig_get_property (GObject *object,
                              guint property_id,
                              GValue *value,
                              GParamSpec *pspec)
{
	switch (property_id) {
		case PROP_EMAIL_ADDRESS:
			g_value_set_string (
				value,
				e_mail_autoconfig_get_email_address (
				E_MAIL_AUTOCONFIG (object)));
			return;
713 714 715 716 717 718 719

		case PROP_REGISTRY:
			g_value_set_object (
				value,
				e_mail_autoconfig_get_registry (
				E_MAIL_AUTOCONFIG (object)));
			return;
720 721 722 723 724
	}

	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

725 726 727 728 729 730 731 732 733 734 735 736 737
static void
mail_autoconfig_dispose (GObject *object)
{
	EMailAutoconfigPrivate *priv;

	priv = E_MAIL_AUTOCONFIG_GET_PRIVATE (object);

	g_clear_object (&priv->registry);

	/* Chain up to parent's dispose() method. */
	G_OBJECT_CLASS (e_mail_autoconfig_parent_class)->dispose (object);
}

738 739 740 741 742 743 744 745 746 747
static void
mail_autoconfig_finalize (GObject *object)
{
	EMailAutoconfigPrivate *priv;

	priv = E_MAIL_AUTOCONFIG_GET_PRIVATE (object);

	g_free (priv->email_address);
	g_free (priv->email_local_part);
	g_free (priv->email_domain_part);
748 749 750

	g_free (priv->imap_result.user);
	g_free (priv->imap_result.host);
751
	g_free (priv->imap_result.auth_mechanism);
752 753
	g_free (priv->pop3_result.user);
	g_free (priv->pop3_result.host);
754
	g_free (priv->pop3_result.auth_mechanism);
755 756
	g_free (priv->smtp_result.user);
	g_free (priv->smtp_result.host);
757
	g_free (priv->smtp_result.auth_mechanism);
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821

	/* Chain up to parent's finalize() method. */
	G_OBJECT_CLASS (e_mail_autoconfig_parent_class)->finalize (object);
}

static gboolean
mail_autoconfig_initable_init (GInitable *initable,
                               GCancellable *cancellable,
                               GError **error)
{
	EMailAutoconfig *autoconfig;
	const gchar *email_address;
	const gchar *domain;
	const gchar *cp;
	gchar *name_server;
	gboolean success = FALSE;
	GError *local_error = NULL;

	autoconfig = E_MAIL_AUTOCONFIG (initable);
	email_address = e_mail_autoconfig_get_email_address (autoconfig);

	if (email_address == NULL) {
		g_set_error_literal (
			error, G_IO_ERROR,
			G_IO_ERROR_INVALID_ARGUMENT,
			_("No email address provided"));
		return FALSE;
	}

	cp = strchr (email_address, '@');
	if (cp == NULL) {
		g_set_error_literal (
			error, G_IO_ERROR,
			G_IO_ERROR_INVALID_ARGUMENT,
			_("Missing domain in email address"));
		return FALSE;
	}

	domain = cp + 1;

	autoconfig->priv->email_local_part =
		g_strndup (email_address, cp - email_address);
	autoconfig->priv->email_domain_part = g_strdup (domain);

	/* First try the email address domain verbatim. */
	success = mail_autoconfig_lookup (
		autoconfig, domain, cancellable, &local_error);

	g_warn_if_fail (
		(success && local_error == NULL) ||
		(!success && local_error != NULL));

	if (success)
		return TRUE;

	/* "404 Not Found" errors are non-fatal this time around. */
	if (ERROR_IS_NOT_FOUND (local_error)) {
		g_clear_error (&local_error);
	} else {
		g_propagate_error (error, local_error);
		return FALSE;
	}

	/* Look up an authoritative name server for the email address
822 823
	 * domain according to its "name server" (NS) DNS record. */
	name_server = mail_autoconfig_resolve_name_server (
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
		domain, cancellable, error);

	if (name_server == NULL)
		return FALSE;

	/* Widdle away segments of the name server domain until
	 * we find a match, or until we widdle down to nothing. */

	cp = name_server;
	while (cp != NULL && strchr (cp, '.') != NULL) {
		g_clear_error (&local_error);

		success = mail_autoconfig_lookup (
			autoconfig, cp, cancellable, &local_error);

		g_warn_if_fail (
			(success && local_error == NULL) ||
			(!success && local_error != NULL));

		if (success || !ERROR_IS_NOT_FOUND (local_error))
			break;

		cp = strchr (cp, '.');
		if (cp != NULL)
			cp++;
	}

	if (local_error != NULL)
		g_propagate_error (error, local_error);

	g_free (name_server);

	return success;
}

static void
e_mail_autoconfig_class_init (EMailAutoconfigClass *class)
{
	GObjectClass *object_class;

	g_type_class_add_private (class, sizeof (EMailAutoconfigPrivate));

	object_class = G_OBJECT_CLASS (class);
	object_class->set_property = mail_autoconfig_set_property;
	object_class->get_property = mail_autoconfig_get_property;
869
	object_class->dispose = mail_autoconfig_dispose;
870 871 872 873 874 875 876 877 878 879 880 881 882
	object_class->finalize = mail_autoconfig_finalize;

	g_object_class_install_property (
		object_class,
		PROP_EMAIL_ADDRESS,
		g_param_spec_string (
			"email-address",
			"Email Address",
			"The address from which to query config data",
			NULL,
			G_PARAM_READWRITE |
			G_PARAM_CONSTRUCT_ONLY |
			G_PARAM_STATIC_STRINGS));
883 884 885 886 887 888 889 890 891 892 893 894

	g_object_class_install_property (
		object_class,
		PROP_REGISTRY,
		g_param_spec_object (
			"registry",
			"Registry",
			"Data source registry",
			E_TYPE_SOURCE_REGISTRY,
			G_PARAM_READWRITE |
			G_PARAM_CONSTRUCT_ONLY |
			G_PARAM_STATIC_STRINGS));
895 896 897
}

static void
898
e_mail_autoconfig_initable_init (GInitableIface *iface)
899
{
900
	iface->init = mail_autoconfig_initable_init;
901 902 903 904 905 906 907 908 909
}

static void
e_mail_autoconfig_init (EMailAutoconfig *autoconfig)
{
	autoconfig->priv = E_MAIL_AUTOCONFIG_GET_PRIVATE (autoconfig);
}

EMailAutoconfig *
910 911
e_mail_autoconfig_new_sync (ESourceRegistry *registry,
                            const gchar *email_address,
912 913 914
                            GCancellable *cancellable,
                            GError **error)
{
915
	g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
916 917 918 919 920
	g_return_val_if_fail (email_address != NULL, NULL);

	return g_initable_new (
		E_TYPE_MAIL_AUTOCONFIG,
		cancellable, error,
921
		"registry", registry,
922 923 924 925 926
		"email-address", email_address,
		NULL);
}

void
927 928
e_mail_autoconfig_new (ESourceRegistry *registry,
                       const gchar *email_address,
929 930 931 932 933
                       gint io_priority,
                       GCancellable *cancellable,
                       GAsyncReadyCallback callback,
                       gpointer user_data)
{
934
	g_return_if_fail (E_IS_SOURCE_REGISTRY (registry));
935 936 937 938 939 940
	g_return_if_fail (email_address != NULL);

	g_async_initable_new_async (
		E_TYPE_MAIL_AUTOCONFIG,
		io_priority, cancellable,
		callback, user_data,
941
		"registry", registry,
942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
		"email-address", email_address,
		NULL);
}

EMailAutoconfig *
e_mail_autoconfig_finish (GAsyncResult *result,
                          GError **error)
{
	GObject *source_object;
	GObject *autoconfig;

	g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);

	source_object = g_async_result_get_source_object (result);
	g_return_val_if_fail (source_object != NULL, NULL);

	autoconfig = g_async_initable_new_finish (
		G_ASYNC_INITABLE (source_object), result, error);

	g_object_unref (source_object);

	if (autoconfig == NULL)
		return NULL;

	return E_MAIL_AUTOCONFIG (autoconfig);
}

969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
/**
 * e_mail_autoconfig_get_registry:
 * @autoconfig: an #EMailAutoconfig
 *
 * Returns the #ESourceRegistry passed to e_mail_autoconfig_new() or
 * e_mail_autoconfig_new_sync().
 *
 * Returns: an #ESourceRegistry
 **/
ESourceRegistry *
e_mail_autoconfig_get_registry (EMailAutoconfig *autoconfig)
{
	g_return_val_if_fail (E_IS_MAIL_AUTOCONFIG (autoconfig), NULL);

	return autoconfig->priv->registry;
}

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
const gchar *
e_mail_autoconfig_get_email_address (EMailAutoconfig *autoconfig)
{
	g_return_val_if_fail (E_IS_MAIL_AUTOCONFIG (autoconfig), NULL);

	return autoconfig->priv->email_address;
}

gboolean
e_mail_autoconfig_set_imap_details (EMailAutoconfig *autoconfig,
                                    ESource *imap_source)
{
	g_return_val_if_fail (E_IS_MAIL_AUTOCONFIG (autoconfig), FALSE);
	g_return_val_if_fail (E_IS_SOURCE (imap_source), FALSE);

	return mail_autoconfig_set_details (
1002
		&autoconfig->priv->imap_result,
1003
		imap_source, E_SOURCE_EXTENSION_MAIL_ACCOUNT);
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
}

gboolean
e_mail_autoconfig_set_pop3_details (EMailAutoconfig *autoconfig,
                                    ESource *pop3_source)
{
	g_return_val_if_fail (E_IS_MAIL_AUTOCONFIG (autoconfig), FALSE);
	g_return_val_if_fail (E_IS_SOURCE (pop3_source), FALSE);

	return mail_autoconfig_set_details (
1014
		&autoconfig->priv->pop3_result,
1015
		pop3_source, E_SOURCE_EXTENSION_MAIL_ACCOUNT);
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
}

gboolean
e_mail_autoconfig_set_smtp_details (EMailAutoconfig *autoconfig,
                                    ESource *smtp_source)
{
	g_return_val_if_fail (E_IS_MAIL_AUTOCONFIG (autoconfig), FALSE);
	g_return_val_if_fail (E_IS_SOURCE (smtp_source), FALSE);

	return mail_autoconfig_set_details (
1026
		&autoconfig->priv->smtp_result,
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
		smtp_source, E_SOURCE_EXTENSION_MAIL_TRANSPORT);
}

void
e_mail_autoconfig_dump_results (EMailAutoconfig *autoconfig)
{
	const gchar *email_address;
	gboolean have_results;

	g_return_if_fail (E_IS_MAIL_AUTOCONFIG (autoconfig));

	email_address = autoconfig->priv->email_address;

	have_results =
		autoconfig->priv->imap_result.set ||
		autoconfig->priv->pop3_result.set ||
		autoconfig->priv->smtp_result.set;

	if (have_results) {
		g_print ("Results for <%s>\n", email_address);

		if (autoconfig->priv->imap_result.set) {
			g_print (
				"IMAP: %s@%s:%u\n",
				autoconfig->priv->imap_result.user,
				autoconfig->priv->imap_result.host,
				autoconfig->priv->imap_result.port);
		}

		if (autoconfig->priv->pop3_result.set) {
			g_print (
				"POP3: %s@%s:%u\n",
				autoconfig->priv->pop3_result.user,
				autoconfig->priv->pop3_result.host,
				autoconfig->priv->pop3_result.port);
		}

		if (autoconfig->priv->smtp_result.set) {
			g_print (
				"SMTP: %s@%s:%u\n",
				autoconfig->priv->smtp_result.user,
				autoconfig->priv->smtp_result.host,
				autoconfig->priv->smtp_result.port);
		}

	} else {
		g_print ("No results for <%s>\n", email_address);
	}
1075 1076
}

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
/**
 * e_mail_autoconfig_copy_results_to_config_lookup:
 * @mail_autoconfig: an #EMailAutoconfig
 * @config_lookup: an #EConfigLookup
 *
 * Copies any valid result from @mail_autoconfig to @config_lookup.
 *
 * Since: 3.26
 **/
void
e_mail_autoconfig_copy_results_to_config_lookup (EMailAutoconfig *mail_autoconfig,
						 EConfigLookup *config_lookup)
{
	g_return_if_fail (E_IS_MAIL_AUTOCONFIG (mail_autoconfig));
	g_return_if_fail (E_IS_CONFIG_LOOKUP (config_lookup));

	mail_autoconfig_result_to_config_lookup (mail_autoconfig, config_lookup,
		&mail_autoconfig->priv->imap_result,
		E_CONFIG_LOOKUP_RESULT_PRIORITY_IMAP,
		"imapx",
		_("IMAP server"),
		E_SOURCE_EXTENSION_MAIL_ACCOUNT);

	mail_autoconfig_result_to_config_lookup (mail_autoconfig, config_lookup,
		&mail_autoconfig->priv->pop3_result,
		E_CONFIG_LOOKUP_RESULT_PRIORITY_POP3,
		"pop",
		_("POP3 server"),
		E_SOURCE_EXTENSION_MAIL_ACCOUNT);

	mail_autoconfig_result_to_config_lookup (mail_autoconfig, config_lookup,
		&mail_autoconfig->priv->smtp_result,
		E_CONFIG_LOOKUP_RESULT_PRIORITY_SMTP,
		"smtp",
		_("SMTP server"),
		E_SOURCE_EXTENSION_MAIL_TRANSPORT);
}