1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Michael Zucchi <notzed@ximian.com>
5 * Copyright 2001-2003 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.
31 #include "camel-sasl-popb4smtp.h"
32 #include "camel-service.h"
33 #include "camel-session.h"
35 CamelServiceAuthType camel_sasl_popb4smtp_authtype = {
36 N_("POP before SMTP"),
38 N_("This option will authorise a POP connection before attempting SMTP"),
44 /* last time the pop was accessed (through the auth method anyway), *time_t */
45 static GHashTable *poplast;
47 /* use 1 hour as our pop timeout */
48 #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 static CamelSaslClass *parent_class = NULL;
56 /* Returns the class for a CamelSaslPOPB4SMTP */
57 #define CSP_CLASS(so) CAMEL_SASL_POPB4SMTP_CLASS (CAMEL_OBJECT_GET_CLASS (so))
59 static GByteArray *popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);
62 camel_sasl_popb4smtp_class_init (CamelSaslPOPB4SMTPClass *camel_sasl_popb4smtp_class)
64 CamelSaslClass *camel_sasl_class = CAMEL_SASL_CLASS (camel_sasl_popb4smtp_class);
66 parent_class = CAMEL_SASL_CLASS (camel_type_get_global_classfuncs (camel_sasl_get_type ()));
68 /* virtual method overload */
69 camel_sasl_class->challenge = popb4smtp_challenge;
71 poplast = g_hash_table_new(g_str_hash, g_str_equal);
75 camel_sasl_popb4smtp_get_type (void)
77 static CamelType type = CAMEL_INVALID_TYPE;
79 if (type == CAMEL_INVALID_TYPE) {
80 type = camel_type_register (camel_sasl_get_type (),
82 sizeof (CamelSaslPOPB4SMTP),
83 sizeof (CamelSaslPOPB4SMTPClass),
84 (CamelObjectClassInitFunc) camel_sasl_popb4smtp_class_init,
94 popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
97 CamelSession *session = sasl->service->session;
101 sasl->authenticated = FALSE;
103 popuri = camel_session_get_password (session, _("POP Source URI"), FALSE, FALSE,
104 sasl->service, "popb4smtp_uri", ex);
106 if (popuri == NULL) {
107 camel_exception_setv(ex, 1, _("POP Before SMTP auth using an unknown transport"));
111 if (strncasecmp(popuri, "pop:", 4) != 0) {
112 camel_exception_setv(ex, 1, _("POP Before SMTP auth using a non-pop source"));
116 /* check if we've done it before recently in this session */
119 /* need to lock around the whole thing until finished with timep */
121 POPB4SMTP_LOCK(lock);
122 timep = g_hash_table_lookup(poplast, popuri);
124 if ((*timep + POPB4SMTP_TIMEOUT) > now) {
125 sasl->authenticated = TRUE;
126 POPB4SMTP_UNLOCK(lock);
131 timep = g_malloc0(sizeof(*timep));
132 g_hash_table_insert(poplast, g_strdup(popuri), timep);
135 /* connect to pop session */
136 store = camel_session_get_store(session, popuri, ex);
138 sasl->authenticated = TRUE;
139 camel_object_unref((CamelObject *)store);
142 sasl->authenticated = FALSE;
146 POPB4SMTP_UNLOCK(lock);