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 Lesser 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 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.
32 #include <glib/gi18n-lib.h>
34 #include "camel-sasl-popb4smtp.h"
35 #include "camel-service.h"
36 #include "camel-session.h"
38 CamelServiceAuthType camel_sasl_popb4smtp_authtype = {
39 N_("POP before SMTP"),
41 N_("This option will authorise a POP connection before attempting SMTP"),
47 /* last time the pop was accessed (through the auth method anyway), *time_t */
48 static GHashTable *poplast;
50 /* use 1 hour as our pop timeout */
51 #define POPB4SMTP_TIMEOUT (60*60)
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)
57 static CamelSaslClass *parent_class = NULL;
59 /* Returns the class for a CamelSaslPOPB4SMTP */
60 #define CSP_CLASS(so) CAMEL_SASL_POPB4SMTP_CLASS (CAMEL_OBJECT_GET_CLASS (so))
62 static GByteArray *popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);
65 camel_sasl_popb4smtp_class_init (CamelSaslPOPB4SMTPClass *camel_sasl_popb4smtp_class)
67 CamelSaslClass *camel_sasl_class = CAMEL_SASL_CLASS (camel_sasl_popb4smtp_class);
69 parent_class = CAMEL_SASL_CLASS (camel_type_get_global_classfuncs (camel_sasl_get_type ()));
71 /* virtual method overload */
72 camel_sasl_class->challenge = popb4smtp_challenge;
74 poplast = g_hash_table_new(g_str_hash, g_str_equal);
78 camel_sasl_popb4smtp_get_type (void)
80 static CamelType type = CAMEL_INVALID_TYPE;
82 if (type == CAMEL_INVALID_TYPE) {
83 type = camel_type_register (camel_sasl_get_type (),
85 sizeof (CamelSaslPOPB4SMTP),
86 sizeof (CamelSaslPOPB4SMTPClass),
87 (CamelObjectClassInitFunc) camel_sasl_popb4smtp_class_init,
97 popb4smtp_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
100 CamelSession *session = sasl->service->session;
104 sasl->authenticated = FALSE;
106 popuri = camel_session_get_password (session, sasl->service, NULL, _("POP Source URI"), "popb4smtp_uri", 0, ex);
108 if (popuri == NULL) {
109 camel_exception_setv(ex, 1, _("POP Before SMTP auth using an unknown transport"));
113 if (g_ascii_strncasecmp(popuri, "pop:", 4) != 0) {
114 camel_exception_setv(ex, 1, _("POP Before SMTP auth using a non-pop source"));
118 /* check if we've done it before recently in this session */
121 /* need to lock around the whole thing until finished with timep */
123 POPB4SMTP_LOCK(lock);
124 timep = g_hash_table_lookup(poplast, popuri);
126 if ((*timep + POPB4SMTP_TIMEOUT) > now) {
127 sasl->authenticated = TRUE;
128 POPB4SMTP_UNLOCK(lock);
133 timep = g_malloc0(sizeof(*timep));
134 g_hash_table_insert(poplast, g_strdup(popuri), timep);
137 /* connect to pop session */
138 store = camel_session_get_store(session, popuri, ex);
140 sasl->authenticated = TRUE;
141 camel_object_unref((CamelObject *)store);
144 sasl->authenticated = FALSE;
148 POPB4SMTP_UNLOCK(lock);