1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Authors: Jeffrey Stedfast <fejj@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.
30 #include "camel-mime-utils.h"
31 #include "camel-sasl-cram-md5.h"
32 #include "camel-sasl-digest-md5.h"
33 #include "camel-sasl-gssapi.h"
34 #include "camel-sasl-kerberos4.h"
35 #include "camel-sasl-login.h"
36 #include "camel-sasl-ntlm.h"
37 #include "camel-sasl-plain.h"
38 #include "camel-sasl-popb4smtp.h"
39 #include "camel-sasl.h"
40 #include "camel-service.h"
45 static CamelObjectClass *parent_class = NULL;
47 /* Returns the class for a CamelSasl */
48 #define CS_CLASS(so) CAMEL_SASL_CLASS (CAMEL_OBJECT_GET_CLASS (so))
50 static GByteArray *sasl_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);
53 camel_sasl_class_init (CamelSaslClass *camel_sasl_class)
55 parent_class = camel_type_get_global_classfuncs (CAMEL_OBJECT_TYPE);
57 /* virtual method definition */
58 camel_sasl_class->challenge = sasl_challenge;
62 camel_sasl_finalize (CamelSasl *sasl)
64 g_free (sasl->service_name);
66 camel_object_unref (sasl->service);
70 camel_sasl_get_type (void)
72 static CamelType type = CAMEL_INVALID_TYPE;
74 if (type == CAMEL_INVALID_TYPE) {
75 type = camel_type_register (CAMEL_OBJECT_TYPE,
78 sizeof (CamelSaslClass),
79 (CamelObjectClassInitFunc) camel_sasl_class_init,
82 (CamelObjectFinalizeFunc) camel_sasl_finalize);
90 sasl_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
92 w(g_warning ("sasl_challenge: Using default implementation!"));
97 * camel_sasl_challenge:
98 * @sasl: a #CamelSasl object
99 * @token: a token, or %NULL
100 * @ex: a #CamelException
102 * If @token is %NULL, generate the initial SASL message to send to
103 * the server. (This will be %NULL if the client doesn't initiate the
104 * exchange.) Otherwise, @token is a challenge from the server, and
105 * the return value is the response.
107 * Returns the SASL response or %NULL. If an error occurred, @ex will
111 camel_sasl_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
113 g_return_val_if_fail (CAMEL_IS_SASL (sasl), NULL);
115 return CS_CLASS (sasl)->challenge (sasl, token, ex);
119 * camel_sasl_challenge_base64:
120 * @sasl: a #CamelSasl object
121 * @token: a base64-encoded token
122 * @ex: a #CamelException
124 * As with #camel_sasl_challenge, but the challenge @token and the
125 * response are both base64-encoded.
127 * Returns the base64 encoded challenge string
130 camel_sasl_challenge_base64 (CamelSasl *sasl, const char *token, CamelException *ex)
132 GByteArray *token_binary, *ret_binary;
136 g_return_val_if_fail (CAMEL_IS_SASL (sasl), NULL);
139 token_binary = g_byte_array_new ();
140 len = strlen (token);
141 g_byte_array_append (token_binary, token, len);
142 token_binary->len = camel_base64_decode_simple (token_binary->data, len);
146 ret_binary = camel_sasl_challenge (sasl, token_binary, ex);
148 g_byte_array_free (token_binary, TRUE);
152 ret = camel_base64_encode_simple (ret_binary->data, ret_binary->len);
153 g_byte_array_free (ret_binary, TRUE);
159 * camel_sasl_authenticated:
160 * @sasl: a #CamelSasl object
162 * Returns whether or not @sasl has successfully authenticated the
163 * user. This will be %TRUE after it returns the last needed response.
164 * The caller must still pass that information on to the server and
165 * verify that it has accepted it.
168 camel_sasl_authenticated (CamelSasl *sasl)
170 return sasl->authenticated;
176 * @service_name: the SASL service name
177 * @mechanism: the SASL mechanism
178 * @service: the CamelService that will be using this SASL
180 * Returns a new #CamelSasl object for the given @service_name,
181 * @mechanism, and @service, or %NULL if the mechanism is not
185 camel_sasl_new (const char *service_name, const char *mechanism, CamelService *service)
189 g_return_val_if_fail (service_name != NULL, NULL);
190 g_return_val_if_fail (mechanism != NULL, NULL);
191 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
193 /* We don't do ANONYMOUS here, because it's a little bit weird. */
195 if (!strcmp (mechanism, "CRAM-MD5"))
196 sasl = (CamelSasl *) camel_object_new (CAMEL_SASL_CRAM_MD5_TYPE);
197 else if (!strcmp (mechanism, "DIGEST-MD5"))
198 sasl = (CamelSasl *) camel_object_new (CAMEL_SASL_DIGEST_MD5_TYPE);
200 else if (!strcmp (mechanism, "GSSAPI"))
201 sasl = (CamelSasl *) camel_object_new (CAMEL_SASL_GSSAPI_TYPE);
204 else if (!strcmp (mechanism, "KERBEROS_V4"))
205 sasl = (CamelSasl *) camel_object_new (CAMEL_SASL_KERBEROS4_TYPE);
207 else if (!strcmp (mechanism, "PLAIN"))
208 sasl = (CamelSasl *) camel_object_new (CAMEL_SASL_PLAIN_TYPE);
209 else if (!strcmp (mechanism, "LOGIN"))
210 sasl = (CamelSasl *) camel_object_new (CAMEL_SASL_LOGIN_TYPE);
211 else if (!strcmp (mechanism, "POPB4SMTP"))
212 sasl = (CamelSasl *) camel_object_new (CAMEL_SASL_POPB4SMTP_TYPE);
213 else if (!strcmp (mechanism, "NTLM"))
214 sasl = (CamelSasl *) camel_object_new (CAMEL_SASL_NTLM_TYPE);
218 sasl->mech = g_strdup (mechanism);
219 sasl->service_name = g_strdup (service_name);
220 sasl->service = service;
221 camel_object_ref (service);
227 * camel_sasl_authtype_list:
228 * @include_plain: whether or not to include the PLAIN mechanism
230 * Returns a #GList of SASL-supported authtypes. The caller must
231 * free the list, but not the contents.
234 camel_sasl_authtype_list (gboolean include_plain)
238 types = g_list_prepend (types, &camel_sasl_cram_md5_authtype);
239 types = g_list_prepend (types, &camel_sasl_digest_md5_authtype);
241 types = g_list_prepend (types, &camel_sasl_gssapi_authtype);
244 types = g_list_prepend (types, &camel_sasl_kerberos4_authtype);
246 types = g_list_prepend (types, &camel_sasl_ntlm_authtype);
248 types = g_list_prepend (types, &camel_sasl_plain_authtype);
254 * camel_sasl_authtype:
255 * @mechanism: the SASL mechanism to get an authtype for
257 * Returns a #CamelServiceAuthType for the given mechanism, if
260 CamelServiceAuthType *
261 camel_sasl_authtype (const char *mechanism)
263 if (!strcmp (mechanism, "CRAM-MD5"))
264 return &camel_sasl_cram_md5_authtype;
265 else if (!strcmp (mechanism, "DIGEST-MD5"))
266 return &camel_sasl_digest_md5_authtype;
268 else if (!strcmp (mechanism, "GSSAPI"))
269 return &camel_sasl_gssapi_authtype;
272 else if (!strcmp (mechanism, "KERBEROS_V4"))
273 return &camel_sasl_kerberos4_authtype;
275 else if (!strcmp (mechanism, "PLAIN"))
276 return &camel_sasl_plain_authtype;
277 else if (!strcmp (mechanism, "LOGIN"))
278 return &camel_sasl_login_authtype;
279 else if (!strcmp(mechanism, "POPB4SMTP"))
280 return &camel_sasl_popb4smtp_authtype;
281 else if (!strcmp (mechanism, "NTLM"))
282 return &camel_sasl_ntlm_authtype;