summaryrefslogtreecommitdiff
path: root/camel/camel-sasl-popb4smtp.c
blob: 0b8f26ca762b156e131344f160c7d5a18fcbf199 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * This library 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 library 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 Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Michael Zucchi <notzed@ximian.com>
 */

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

#include <string.h>
#include <time.h>

#include <glib/gi18n-lib.h>

#include "camel-sasl-popb4smtp.h"
#include "camel-service.h"
#include "camel-session.h"
#include "camel-store.h"

#define CAMEL_SASL_POPB4SMTP_GET_PRIVATE(obj) \
	(G_TYPE_INSTANCE_GET_PRIVATE \
	((obj), CAMEL_TYPE_SASL_POPB4SMTP, CamelSaslPOPB4SMTPPrivate))

struct _CamelSaslPOPB4SMTPPrivate {
	gint placeholder;  /* allow for future expansion */
};

static CamelServiceAuthType sasl_popb4smtp_auth_type = {
	N_("POP before SMTP"),

	N_("This option will authorise a POP connection before attempting SMTP"),

	"POPB4SMTP",
	FALSE,
};

/* last time the pop was accessed (through the auth method anyway), *time_t */
static GHashTable *poplast;

/* use 1 hour as our pop timeout */
#define POPB4SMTP_TIMEOUT (60*60)

static GMutex lock;
#define POPB4SMTP_LOCK(l) g_mutex_lock(&l)
#define POPB4SMTP_UNLOCK(l) g_mutex_unlock(&l)

G_DEFINE_TYPE (CamelSaslPOPB4SMTP, camel_sasl_popb4smtp, CAMEL_TYPE_SASL)

static GByteArray *
sasl_popb4smtp_challenge_sync (CamelSasl *sasl,
                               GByteArray *token,
                               GCancellable *cancellable,
                               GError **error)
{
	CamelService *service;
	CamelSession *session;
	time_t now, *timep;
	const gchar *type_name;
	gchar *pop_uid;

	service = camel_sasl_get_service (sasl);
	session = camel_service_ref_session (service);

	camel_sasl_set_authenticated (sasl, FALSE);

	pop_uid = camel_session_get_password (
		session, service, _("POP Source UID"),
		"popb4smtp_uid", 0, error);

	if (pop_uid != NULL)
		service = camel_session_ref_service (session, pop_uid);
	else
		service = NULL;

	g_object_unref (session);

	if (service == NULL) {
		g_set_error (
			error, CAMEL_SERVICE_ERROR,
			CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
			_("POP Before SMTP authentication "
			"using an unknown transport"));
		g_free (pop_uid);
		return NULL;
	}

	type_name = G_OBJECT_TYPE_NAME (service);

	if (!CAMEL_IS_STORE (service)) {
		g_set_error (
			error, CAMEL_SERVICE_ERROR,
			CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
			_("POP Before SMTP authentication attempted "
			"with a %s service"), type_name);
		goto exit;
	}

	if (strstr (type_name, "POP") == NULL) {
		g_set_error (
			error, CAMEL_SERVICE_ERROR,
			CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
			_("POP Before SMTP authentication attempted "
			"with a %s service"), type_name);
		goto exit;
	}

	/* check if we've done it before recently in this session */
	now = time (NULL);

	/* need to lock around the whole thing until finished with timep */

	POPB4SMTP_LOCK (lock);

	timep = g_hash_table_lookup (poplast, pop_uid);
	if (timep) {
		if ((*timep + POPB4SMTP_TIMEOUT) > now) {
			camel_sasl_set_authenticated (sasl, TRUE);
			POPB4SMTP_UNLOCK (lock);
			goto exit;
		}
	} else {
		timep = g_malloc0 (sizeof (*timep));
		g_hash_table_insert (poplast, g_strdup (pop_uid), timep);
	}

	/* connect to pop session */
	if (camel_service_connect_sync (service, cancellable, error)) {
		camel_sasl_set_authenticated (sasl, TRUE);
		*timep = now;
	} else {
		camel_sasl_set_authenticated (sasl, FALSE);
		*timep = 0;
	}

	POPB4SMTP_UNLOCK (lock);

exit:
	g_object_unref (service);
	g_free (pop_uid);

	return NULL;
}

static void
camel_sasl_popb4smtp_class_init (CamelSaslPOPB4SMTPClass *class)
{
	CamelSaslClass *sasl_class;

	g_type_class_add_private (class, sizeof (CamelSaslPOPB4SMTPPrivate));

	sasl_class = CAMEL_SASL_CLASS (class);
	sasl_class->auth_type = &sasl_popb4smtp_auth_type;
	sasl_class->challenge_sync = sasl_popb4smtp_challenge_sync;

	poplast = g_hash_table_new (g_str_hash, g_str_equal);
}

static void
camel_sasl_popb4smtp_init (CamelSaslPOPB4SMTP *sasl)
{
	sasl->priv = CAMEL_SASL_POPB4SMTP_GET_PRIVATE (sasl);
}