aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-sasl.c
blob: dd4da2f081af95ac91113f3fa456dc3b2511aad2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Jeffrey Stedfast <fejj@ximian.com>
 *
 *  Copyright 2001 Ximian, Inc. (www.ximian.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include "camel-sasl.h"
#include "camel-mime-utils.h"
#include "camel-service.h"

#include "camel-sasl-cram-md5.h"
#include "camel-sasl-digest-md5.h"
#include "camel-sasl-kerberos4.h"
#include "camel-sasl-login.h"
#include "camel-sasl-plain.h"

static CamelObjectClass *parent_class = NULL;

/* Returns the class for a CamelSasl */
#define CS_CLASS(so) CAMEL_SASL_CLASS (CAMEL_OBJECT_GET_CLASS (so))

static GByteArray *sasl_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);

static void
camel_sasl_class_init (CamelSaslClass *camel_sasl_class)
{
    parent_class = camel_type_get_global_classfuncs (CAMEL_OBJECT_TYPE);
    
    /* virtual method definition */
    camel_sasl_class->challenge = sasl_challenge;
}

static void
camel_sasl_finalize (CamelSasl *sasl)
{
    g_free (sasl->service_name);
    camel_object_unref (CAMEL_OBJECT (sasl->service));
}

CamelType
camel_sasl_get_type (void)
{
    static CamelType type = CAMEL_INVALID_TYPE;
    
    if (type == CAMEL_INVALID_TYPE) {
        type = camel_type_register (CAMEL_OBJECT_TYPE,
                        "CamelSasl",
                        sizeof (CamelSasl),
                        sizeof (CamelSaslClass),
                        (CamelObjectClassInitFunc) camel_sasl_class_init,
                        NULL,
                        NULL,
                        (CamelObjectFinalizeFunc) camel_sasl_finalize);
    }
    
    return type;
}


static GByteArray *
sasl_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
{
    g_warning ("sasl_challenge: Using default implementation!");
    return NULL;
}

/**
 * camel_sasl_challenge:
 * @sasl: a SASL object
 * @token: a token, or %NULL
 * @ex: exception
 *
 * If @token is %NULL, generate the initial SASL message to send to
 * the server. (This will be %NULL if the client doesn't initiate the
 * exchange.) Otherwise, @token is a challenge from the server, and
 * the return value is the response.
 *
 * Return value: The SASL response or %NULL. If an error occurred, @ex
 * will also be set.
 **/
GByteArray *
camel_sasl_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
{
    g_return_val_if_fail (CAMEL_IS_SASL (sasl), NULL);

    return CS_CLASS (sasl)->challenge (sasl, token, ex);
}

/**
 * camel_sasl_challenge_base64:
 * @sasl: a SASL object
 * @token: a base64-encoded token
 * @ex: exception
 *
 * As with camel_sasl_challenge(), but the challenge @token and the
 * response are both base64-encoded.
 *
 * Return value: As with camel_sasl_challenge(), but base64-encoded.
 **/
char *
camel_sasl_challenge_base64 (CamelSasl *sasl, const char *token, CamelException *ex)
{
    GByteArray *token_binary, *ret_binary;
    char *ret;
    int len;

    g_return_val_if_fail (CAMEL_IS_SASL (sasl), NULL);

    if (token) {
        token_binary = g_byte_array_new ();
        len = strlen (token);
        g_byte_array_append (token_binary, token, len);
        token_binary->len = base64_decode_simple (token_binary->data, len);
    } else
        token_binary = NULL;

    ret_binary = camel_sasl_challenge (sasl, token_binary, ex);
    if (token_binary)
        g_byte_array_free (token_binary, TRUE);
    if (!ret_binary)
        return NULL;

    ret = base64_encode_simple (ret_binary->data, ret_binary->len);
    g_byte_array_free (ret_binary, TRUE);

    return ret;
}

/**
 * camel_sasl_authenticated:
 * @sasl: a SASL object
 *
 * Return value: whether or not @sasl has successfully authenticated
 * the user. This will be %TRUE after it returns the last needed response.
 * The caller must still pass that information on to the server and verify
 * that it has accepted it.
 **/
gboolean
camel_sasl_authenticated (CamelSasl *sasl)
{
    return sasl->authenticated;
}


/**
 * camel_sasl_new:
 * @service_name: the SASL service name
 * @mechanism: the SASL mechanism
 * @service: the CamelService that will be using this SASL
 *
 * Return value: a new CamelSasl for the given @service_name,
 * @mechanism, and @service, or %NULL if the mechanism is not
 * supported.
 **/
CamelSasl *
camel_sasl_new (const char *service_name, const char *mechanism, CamelService *service)
{
    CamelSasl *sasl;

    g_return_val_if_fail (service_name != NULL, NULL);
    g_return_val_if_fail (mechanism != NULL, NULL);
    g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);

    /* We don't do ANONYMOUS here, because it's a little bit weird. */

    if (!strcmp (mechanism, "CRAM-MD5"))
        sasl = (CamelSasl *)camel_object_new (CAMEL_SASL_CRAM_MD5_TYPE);
    else if (!strcmp (mechanism, "DIGEST-MD5"))
        sasl = (CamelSasl *)camel_object_new (CAMEL_SASL_DIGEST_MD5_TYPE);
#ifdef HAVE_KRB4
    else if (!strcmp (mechanism, "KERBEROS_V4"))
        sasl = (CamelSasl *)camel_object_new (CAMEL_SASL_KERBEROS4_TYPE);
#endif
    else if (!strcmp (mechanism, "PLAIN"))
        sasl = (CamelSasl *)camel_object_new (CAMEL_SASL_PLAIN_TYPE);
    else if (!strcmp (mechanism, "LOGIN"))
        sasl = (CamelSasl *)camel_object_new (CAMEL_SASL_LOGIN_TYPE);
    else
        return NULL;

    sasl->service_name = g_strdup (service_name);
    sasl->service = service;
    camel_object_ref (CAMEL_OBJECT (service));

    return sasl;
}

/**
 * camel_sasl_authtype_list:
 * @include_plain: whether or not to include the PLAIN mechanism
 *
 * Return value: a GList of SASL-supported authtypes. The caller must
 * free the list, but not the contents.
 **/
GList *
camel_sasl_authtype_list (gboolean include_plain)
{
    GList *types = NULL;

    types = g_list_prepend (types, &camel_sasl_cram_md5_authtype);
    types = g_list_prepend (types, &camel_sasl_digest_md5_authtype);
#ifdef HAVE_KRB4
    types = g_list_prepend (types, &camel_sasl_kerberos4_authtype);
#endif
    types = g_list_prepend (types, &camel_sasl_login_authtype);
    if (include_plain)
        types = g_list_prepend (types, &camel_sasl_plain_authtype);
    
    return types;
}

/**
 * camel_sasl_authtype:
 * @mechanism: the SASL mechanism to get an authtype for
 *
 * Return value: a CamelServiceAuthType for the given mechanism, if
 * it is supported.
 **/
CamelServiceAuthType *
camel_sasl_authtype (const char *mechanism)
{
    if (!strcmp (mechanism, "CRAM-MD5"))
        return &camel_sasl_cram_md5_authtype;
    else if (!strcmp (mechanism, "DIGEST-MD5"))
        return &camel_sasl_digest_md5_authtype;
#ifdef HAVE_KRB4
    else if (!strcmp (mechanism, "KERBEROS_V4"))
        return &camel_sasl_kerberos4_authtype;
#endif
    else if (!strcmp (mechanism, "PLAIN"))
        return &camel_sasl_plain_authtype;
    else if (!strcmp (mechanism, "LOGIN"))
        return &camel_sasl_login_authtype;
    else
        return NULL;
}