329151872e4de0d805f12cbbce4e6f1a2668f696
[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 2001-2003 Ximian, Inc. (www.ximian.com)
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of version 2 of the GNU General Public
9  * License as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <pthread.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "camel-sasl-popb4smtp.h"
32 #include "camel-service.h"
33 #include "camel-session.h"
34
35 CamelServiceAuthType camel_sasl_popb4smtp_authtype = {
36         N_("POP before SMTP"),
37
38         N_("This option will authorise a POP connection before attempting SMTP"),
39
40         "POPB4SMTP",
41         FALSE,
42 };
43
44 /* last time the pop was accessed (through the auth method anyway), *time_t */
45 static GHashTable *poplast;
46
47 /* use 1 hour as our pop timeout */
48 #define POPB4SMTP_TIMEOUT (60*60)
49
50 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
51 #define POPB4SMTP_LOCK(l) pthread_mutex_lock(&l)
52 #define POPB4SMTP_UNLOCK(l) pthread_mutex_unlock(&l)
53
54 static CamelSaslClass *parent_class = NULL;
55
56 /* Returns the class for a CamelSaslPOPB4SMTP */
57 #define CSP_CLASS(so) CAMEL_SASL_POPB4SMTP_CLASS (CAMEL_OBJECT_GET_CLASS (so))
58
59 static GByteArray *popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);
60
61 static void
62 camel_sasl_popb4smtp_class_init (CamelSaslPOPB4SMTPClass *camel_sasl_popb4smtp_class)
63 {
64         CamelSaslClass *camel_sasl_class = CAMEL_SASL_CLASS (camel_sasl_popb4smtp_class);
65         
66         parent_class = CAMEL_SASL_CLASS (camel_type_get_global_classfuncs (camel_sasl_get_type ()));
67         
68         /* virtual method overload */
69         camel_sasl_class->challenge = popb4smtp_challenge;
70
71         poplast = g_hash_table_new(g_str_hash, g_str_equal);
72 }
73
74 CamelType
75 camel_sasl_popb4smtp_get_type (void)
76 {
77         static CamelType type = CAMEL_INVALID_TYPE;
78         
79         if (type == CAMEL_INVALID_TYPE) {
80                 type = camel_type_register (camel_sasl_get_type (),
81                                             "CamelSaslPOPB4SMTP",
82                                             sizeof (CamelSaslPOPB4SMTP),
83                                             sizeof (CamelSaslPOPB4SMTPClass),
84                                             (CamelObjectClassInitFunc) camel_sasl_popb4smtp_class_init,
85                                             NULL,
86                                             NULL,
87                                             NULL);
88         }
89         
90         return type;
91 }
92
93 static GByteArray *
94 popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
95 {
96         char *popuri;
97         CamelSession *session = sasl->service->session;
98         CamelStore *store;
99         time_t now, *timep;
100
101         sasl->authenticated = FALSE;
102
103         popuri = camel_session_get_password (session, _("POP Source URI"), FALSE, FALSE,
104                                              sasl->service, "popb4smtp_uri", ex);
105
106         if (popuri == NULL) {
107                 camel_exception_setv(ex, 1, _("POP Before SMTP auth using an unknown transport"));
108                 return NULL;
109         }
110
111         if (strncasecmp(popuri, "pop:", 4) != 0) {
112                 camel_exception_setv(ex, 1, _("POP Before SMTP auth using a non-pop source"));
113                 return NULL;
114         }
115
116         /* check if we've done it before recently in this session */
117         now = time(0);
118
119         /* need to lock around the whole thing until finished with timep */
120
121         POPB4SMTP_LOCK(lock);
122         timep = g_hash_table_lookup(poplast, popuri);
123         if (timep) {
124                 if ((*timep + POPB4SMTP_TIMEOUT) > now) {
125                         sasl->authenticated = TRUE;
126                         POPB4SMTP_UNLOCK(lock);
127                         g_free(popuri);
128                         return NULL;
129                 }
130         } else {
131                 timep = g_malloc0(sizeof(*timep));
132                 g_hash_table_insert(poplast, g_strdup(popuri), timep);
133         }
134
135         /* connect to pop session */
136         store = camel_session_get_store(session, popuri, ex);
137         if (store) {
138                 sasl->authenticated = TRUE;
139                 camel_object_unref((CamelObject *)store);
140                 *timep = now;
141         } else {
142                 sasl->authenticated = FALSE;
143                 *timep = 0;
144         }
145
146         POPB4SMTP_UNLOCK(lock);
147
148         g_free(popuri);
149         
150         return NULL;
151 }