added a 'domain' argument, and rearragned arguments to be prettier and
[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, sasl->service, NULL, _("POP Source URI"), "popb4smtp_uri", 0, ex);
104
105         if (popuri == NULL) {
106                 camel_exception_setv(ex, 1, _("POP Before SMTP auth using an unknown transport"));
107                 return NULL;
108         }
109
110         if (strncasecmp(popuri, "pop:", 4) != 0) {
111                 camel_exception_setv(ex, 1, _("POP Before SMTP auth using a non-pop source"));
112                 return NULL;
113         }
114
115         /* check if we've done it before recently in this session */
116         now = time(0);
117
118         /* need to lock around the whole thing until finished with timep */
119
120         POPB4SMTP_LOCK(lock);
121         timep = g_hash_table_lookup(poplast, popuri);
122         if (timep) {
123                 if ((*timep + POPB4SMTP_TIMEOUT) > now) {
124                         sasl->authenticated = TRUE;
125                         POPB4SMTP_UNLOCK(lock);
126                         g_free(popuri);
127                         return NULL;
128                 }
129         } else {
130                 timep = g_malloc0(sizeof(*timep));
131                 g_hash_table_insert(poplast, g_strdup(popuri), timep);
132         }
133
134         /* connect to pop session */
135         store = camel_session_get_store(session, popuri, ex);
136         if (store) {
137                 sasl->authenticated = TRUE;
138                 camel_object_unref((CamelObject *)store);
139                 *timep = now;
140         } else {
141                 sasl->authenticated = FALSE;
142                 *timep = 0;
143         }
144
145         POPB4SMTP_UNLOCK(lock);
146
147         g_free(popuri);
148         
149         return NULL;
150 }