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