camel-mime-part.c camel-mime-utils.c camel-movemail.c
[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 Lesser 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 Lesser 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-i18n.h"
32 #include "camel-sasl-popb4smtp.h"
33 #include "camel-service.h"
34 #include "camel-session.h"
35
36 CamelServiceAuthType camel_sasl_popb4smtp_authtype = {
37         N_("POP before SMTP"),
38
39         N_("This option will authorise a POP connection before attempting SMTP"),
40
41         "POPB4SMTP",
42         FALSE,
43 };
44
45 /* last time the pop was accessed (through the auth method anyway), *time_t */
46 static GHashTable *poplast;
47
48 /* use 1 hour as our pop timeout */
49 #define POPB4SMTP_TIMEOUT (60*60)
50
51 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
52 #define POPB4SMTP_LOCK(l) pthread_mutex_lock(&l)
53 #define POPB4SMTP_UNLOCK(l) pthread_mutex_unlock(&l)
54
55 static CamelSaslClass *parent_class = NULL;
56
57 /* Returns the class for a CamelSaslPOPB4SMTP */
58 #define CSP_CLASS(so) CAMEL_SASL_POPB4SMTP_CLASS (CAMEL_OBJECT_GET_CLASS (so))
59
60 static GByteArray *popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);
61
62 static void
63 camel_sasl_popb4smtp_class_init (CamelSaslPOPB4SMTPClass *camel_sasl_popb4smtp_class)
64 {
65         CamelSaslClass *camel_sasl_class = CAMEL_SASL_CLASS (camel_sasl_popb4smtp_class);
66         
67         parent_class = CAMEL_SASL_CLASS (camel_type_get_global_classfuncs (camel_sasl_get_type ()));
68         
69         /* virtual method overload */
70         camel_sasl_class->challenge = popb4smtp_challenge;
71
72         poplast = g_hash_table_new(g_str_hash, g_str_equal);
73 }
74
75 CamelType
76 camel_sasl_popb4smtp_get_type (void)
77 {
78         static CamelType type = CAMEL_INVALID_TYPE;
79         
80         if (type == CAMEL_INVALID_TYPE) {
81                 type = camel_type_register (camel_sasl_get_type (),
82                                             "CamelSaslPOPB4SMTP",
83                                             sizeof (CamelSaslPOPB4SMTP),
84                                             sizeof (CamelSaslPOPB4SMTPClass),
85                                             (CamelObjectClassInitFunc) camel_sasl_popb4smtp_class_init,
86                                             NULL,
87                                             NULL,
88                                             NULL);
89         }
90         
91         return type;
92 }
93
94 static GByteArray *
95 popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
96 {
97         char *popuri;
98         CamelSession *session = sasl->service->session;
99         CamelStore *store;
100         time_t now, *timep;
101
102         sasl->authenticated = FALSE;
103
104         popuri = camel_session_get_password (session, sasl->service, NULL, _("POP Source URI"), "popb4smtp_uri", 0, 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 (g_ascii_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 }