1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Michael Zucchi <notzed@ximian.com>
5 * Copyright 2001 Ximian, Inc. (www.ximian.com)
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.
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.
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.
29 #include "camel-sasl-popb4smtp.h"
30 #include "camel-service.h"
31 #include "camel-session.h"
33 CamelServiceAuthType camel_sasl_popb4smtp_authtype = {
34 N_("POP before SMTP"),
36 N_("This option will authorise a POP connection before attempting SMTP"),
42 /* last time the pop was accessed (through the auth method anyway), *time_t */
43 static GHashTable *poplast;
45 /* use 1 hour as our pop timeout */
46 #define POPB4SMTP_TIMEOUT (60*60)
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)
54 #define POPB4SMTP_LOCK(l)
55 #define POPB4SMTP_UNLOCK(l)
58 static CamelSaslClass *parent_class = NULL;
60 /* Returns the class for a CamelSaslPOPB4SMTP */
61 #define CSP_CLASS(so) CAMEL_SASL_POPB4SMTP_CLASS (CAMEL_OBJECT_GET_CLASS (so))
63 static GByteArray *popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);
66 camel_sasl_popb4smtp_class_init (CamelSaslPOPB4SMTPClass *camel_sasl_popb4smtp_class)
68 CamelSaslClass *camel_sasl_class = CAMEL_SASL_CLASS (camel_sasl_popb4smtp_class);
70 parent_class = CAMEL_SASL_CLASS (camel_type_get_global_classfuncs (camel_sasl_get_type ()));
72 /* virtual method overload */
73 camel_sasl_class->challenge = popb4smtp_challenge;
75 poplast = g_hash_table_new(g_str_hash, g_str_equal);
79 camel_sasl_popb4smtp_get_type (void)
81 static CamelType type = CAMEL_INVALID_TYPE;
83 if (type == CAMEL_INVALID_TYPE) {
84 type = camel_type_register (camel_sasl_get_type (),
86 sizeof (CamelSaslPOPB4SMTP),
87 sizeof (CamelSaslPOPB4SMTPClass),
88 (CamelObjectClassInitFunc) camel_sasl_popb4smtp_class_init,
98 popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
101 CamelSession *session = sasl->service->session;
105 sasl->authenticated = FALSE;
107 popuri = camel_session_get_password (session, _("POP Source URI"), FALSE, FALSE,
108 sasl->service, "popb4smtp_uri", ex);
110 if (popuri == NULL) {
111 camel_exception_setv(ex, 1, _("POP Before SMTP auth using an unknown transport"));
115 if (strncasecmp(popuri, "pop:", 4) != 0) {
116 camel_exception_setv(ex, 1, _("POP Before SMTP auth using a non-pop source"));
120 /* check if we've done it before recently in this session */
123 /* need to lock around the whole thing until finished with timep */
125 POPB4SMTP_LOCK(lock);
126 timep = g_hash_table_lookup(poplast, popuri);
128 if ((*timep + POPB4SMTP_TIMEOUT) > now) {
129 sasl->authenticated = TRUE;
130 POPB4SMTP_UNLOCK(lock);
135 timep = g_malloc0(sizeof(*timep));
136 g_hash_table_insert(poplast, g_strdup(popuri), timep);
139 /* connect to pop session */
140 store = camel_session_get_store(session, popuri, ex);
142 sasl->authenticated = TRUE;
143 camel_object_unref((CamelObject *)store);
146 sasl->authenticated = FALSE;
150 POPB4SMTP_UNLOCK(lock);