updated changelog
[platform/upstream/evolution-data-server.git] / camel / camel-sasl-popb4smtp.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Authors: Michael Zucchi <notzed@ximian.com>
4  *
5  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
6  *
7  * This library is free software you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <string.h>
26 #include <time.h>
27
28 #include <glib/gi18n-lib.h>
29
30 #include "camel-sasl-popb4smtp.h"
31 #include "camel-service.h"
32 #include "camel-session.h"
33 #include "camel-store.h"
34
35 #define CAMEL_SASL_POPB4SMTP_GET_PRIVATE(obj) \
36         (G_TYPE_INSTANCE_GET_PRIVATE \
37         ((obj), CAMEL_TYPE_SASL_POPB4SMTP, CamelSaslPOPB4SMTPPrivate))
38
39 struct _CamelSaslPOPB4SMTPPrivate {
40         gint placeholder;  /* allow for future expansion */
41 };
42
43 static CamelServiceAuthType sasl_popb4smtp_auth_type = {
44         N_("POP before SMTP"),
45
46         N_("This option will authorise a POP connection before attempting SMTP"),
47
48         "POPB4SMTP",
49         FALSE,
50 };
51
52 /* last time the pop was accessed (through the auth method anyway), *time_t */
53 static GHashTable *poplast;
54
55 /* use 1 hour as our pop timeout */
56 #define POPB4SMTP_TIMEOUT (60*60)
57
58 static GMutex lock;
59 #define POPB4SMTP_LOCK(l) g_mutex_lock(&l)
60 #define POPB4SMTP_UNLOCK(l) g_mutex_unlock(&l)
61
62 G_DEFINE_TYPE (CamelSaslPOPB4SMTP, camel_sasl_popb4smtp, CAMEL_TYPE_SASL)
63
64 static GByteArray *
65 sasl_popb4smtp_challenge_sync (CamelSasl *sasl,
66                                GByteArray *token,
67                                GCancellable *cancellable,
68                                GError **error)
69 {
70         CamelService *service;
71         CamelSession *session;
72         time_t now, *timep;
73         const gchar *type_name;
74         gchar *pop_uid;
75
76         service = camel_sasl_get_service (sasl);
77         session = camel_service_ref_session (service);
78
79         camel_sasl_set_authenticated (sasl, FALSE);
80
81         pop_uid = camel_session_get_password (
82                 session, service, _("POP Source UID"),
83                 "popb4smtp_uid", 0, error);
84
85         if (pop_uid != NULL)
86                 service = camel_session_ref_service (session, pop_uid);
87         else
88                 service = NULL;
89
90         g_object_unref (session);
91
92         if (service == NULL) {
93                 g_set_error (
94                         error, CAMEL_SERVICE_ERROR,
95                         CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
96                         _("POP Before SMTP authentication "
97                         "using an unknown transport"));
98                 g_free (pop_uid);
99                 return NULL;
100         }
101
102         type_name = G_OBJECT_TYPE_NAME (service);
103
104         if (!CAMEL_IS_STORE (service)) {
105                 g_set_error (
106                         error, CAMEL_SERVICE_ERROR,
107                         CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
108                         _("POP Before SMTP authentication attempted "
109                         "with a %s service"), type_name);
110                 goto exit;
111         }
112
113         if (strstr (type_name, "POP") == NULL) {
114                 g_set_error (
115                         error, CAMEL_SERVICE_ERROR,
116                         CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
117                         _("POP Before SMTP authentication attempted "
118                         "with a %s service"), type_name);
119                 goto exit;
120         }
121
122         /* check if we've done it before recently in this session */
123         now = time (NULL);
124
125         /* need to lock around the whole thing until finished with timep */
126
127         POPB4SMTP_LOCK (lock);
128
129         timep = g_hash_table_lookup (poplast, pop_uid);
130         if (timep) {
131                 if ((*timep + POPB4SMTP_TIMEOUT) > now) {
132                         camel_sasl_set_authenticated (sasl, TRUE);
133                         POPB4SMTP_UNLOCK (lock);
134                         goto exit;
135                 }
136         } else {
137                 timep = g_malloc0 (sizeof (*timep));
138                 g_hash_table_insert (poplast, g_strdup (pop_uid), timep);
139         }
140
141         /* connect to pop session */
142         if (camel_service_connect_sync (service, cancellable, error)) {
143                 camel_sasl_set_authenticated (sasl, TRUE);
144                 *timep = now;
145         } else {
146                 camel_sasl_set_authenticated (sasl, FALSE);
147                 *timep = 0;
148         }
149
150         POPB4SMTP_UNLOCK (lock);
151
152 exit:
153         g_object_unref (service);
154         g_free (pop_uid);
155
156         return NULL;
157 }
158
159 static void
160 camel_sasl_popb4smtp_class_init (CamelSaslPOPB4SMTPClass *class)
161 {
162         CamelSaslClass *sasl_class;
163
164         g_type_class_add_private (class, sizeof (CamelSaslPOPB4SMTPPrivate));
165
166         sasl_class = CAMEL_SASL_CLASS (class);
167         sasl_class->auth_type = &sasl_popb4smtp_auth_type;
168         sasl_class->challenge_sync = sasl_popb4smtp_challenge_sync;
169
170         poplast = g_hash_table_new (g_str_hash, g_str_equal);
171 }
172
173 static void
174 camel_sasl_popb4smtp_init (CamelSaslPOPB4SMTP *sasl)
175 {
176         sasl->priv = CAMEL_SASL_POPB4SMTP_GET_PRIVATE (sasl);
177 }