Fix FSF address (Tobias Mueller, #470445)
[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., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, 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 <glib.h>
32 #include <glib/gi18n-lib.h>
33
34 #include "camel-sasl-popb4smtp.h"
35 #include "camel-service.h"
36 #include "camel-session.h"
37
38 CamelServiceAuthType camel_sasl_popb4smtp_authtype = {
39         N_("POP before SMTP"),
40
41         N_("This option will authorise a POP connection before attempting SMTP"),
42
43         "POPB4SMTP",
44         FALSE,
45 };
46
47 /* last time the pop was accessed (through the auth method anyway), *time_t */
48 static GHashTable *poplast;
49
50 /* use 1 hour as our pop timeout */
51 #define POPB4SMTP_TIMEOUT (60*60)
52
53 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
54 #define POPB4SMTP_LOCK(l) pthread_mutex_lock(&l)
55 #define POPB4SMTP_UNLOCK(l) pthread_mutex_unlock(&l)
56
57 static CamelSaslClass *parent_class = NULL;
58
59 /* Returns the class for a CamelSaslPOPB4SMTP */
60 #define CSP_CLASS(so) CAMEL_SASL_POPB4SMTP_CLASS (CAMEL_OBJECT_GET_CLASS (so))
61
62 static GByteArray *popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);
63
64 static void
65 camel_sasl_popb4smtp_class_init (CamelSaslPOPB4SMTPClass *camel_sasl_popb4smtp_class)
66 {
67         CamelSaslClass *camel_sasl_class = CAMEL_SASL_CLASS (camel_sasl_popb4smtp_class);
68         
69         parent_class = CAMEL_SASL_CLASS (camel_type_get_global_classfuncs (camel_sasl_get_type ()));
70         
71         /* virtual method overload */
72         camel_sasl_class->challenge = popb4smtp_challenge;
73
74         poplast = g_hash_table_new(g_str_hash, g_str_equal);
75 }
76
77 CamelType
78 camel_sasl_popb4smtp_get_type (void)
79 {
80         static CamelType type = CAMEL_INVALID_TYPE;
81         
82         if (type == CAMEL_INVALID_TYPE) {
83                 type = camel_type_register (camel_sasl_get_type (),
84                                             "CamelSaslPOPB4SMTP",
85                                             sizeof (CamelSaslPOPB4SMTP),
86                                             sizeof (CamelSaslPOPB4SMTPClass),
87                                             (CamelObjectClassInitFunc) camel_sasl_popb4smtp_class_init,
88                                             NULL,
89                                             NULL,
90                                             NULL);
91         }
92         
93         return type;
94 }
95
96 static GByteArray *
97 popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
98 {
99         char *popuri;
100         CamelSession *session = sasl->service->session;
101         CamelStore *store;
102         time_t now, *timep;
103
104         sasl->authenticated = FALSE;
105
106         popuri = camel_session_get_password (session, sasl->service, NULL, _("POP Source URI"), "popb4smtp_uri", 0, ex);
107
108         if (popuri == NULL) {
109                 camel_exception_setv(ex, 1, _("POP Before SMTP auth using an unknown transport"));
110                 return NULL;
111         }
112
113         if (g_ascii_strncasecmp(popuri, "pop:", 4) != 0) {
114                 camel_exception_setv(ex, 1, _("POP Before SMTP auth using a non-pop source"));
115                 return NULL;
116         }
117
118         /* check if we've done it before recently in this session */
119         now = time(0);
120
121         /* need to lock around the whole thing until finished with timep */
122
123         POPB4SMTP_LOCK(lock);
124         timep = g_hash_table_lookup(poplast, popuri);
125         if (timep) {
126                 if ((*timep + POPB4SMTP_TIMEOUT) > now) {
127                         sasl->authenticated = TRUE;
128                         POPB4SMTP_UNLOCK(lock);
129                         g_free(popuri);
130                         return NULL;
131                 }
132         } else {
133                 timep = g_malloc0(sizeof(*timep));
134                 g_hash_table_insert(poplast, g_strdup(popuri), timep);
135         }
136
137         /* connect to pop session */
138         store = camel_session_get_store(session, popuri, ex);
139         if (store) {
140                 sasl->authenticated = TRUE;
141                 camel_object_unref((CamelObject *)store);
142                 *timep = now;
143         } else {
144                 sasl->authenticated = FALSE;
145                 *timep = 0;
146         }
147
148         POPB4SMTP_UNLOCK(lock);
149
150         g_free(popuri);
151         
152         return NULL;
153 }