Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-sasl-login.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  *  Authors: Jeffrey Stedfast <fejj@ximian.com>
4  *
5  *  Copyright 2001 Ximian, Inc. (www.ximian.com)
6  *
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.
10  *
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.
15  *
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.
20  *
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28
29 #include <glib.h>
30 #include <glib/gi18n-lib.h>
31
32 #include "camel-sasl-login.h"
33 #include "camel-service.h"
34
35 CamelServiceAuthType camel_sasl_login_authtype = {
36         N_("Login"),
37         
38         N_("This option will connect to the server using a "
39            "simple password."),
40         
41         "LOGIN",
42         TRUE
43 };
44
45 enum {
46         LOGIN_USER,
47         LOGIN_PASSWD
48 };
49
50 static CamelSaslClass *parent_class = NULL;
51
52 /* Returns the class for a CamelSaslLogin */
53 #define CSP_CLASS(so) CAMEL_SASL_LOGIN_CLASS (CAMEL_OBJECT_GET_CLASS (so))
54
55 static GByteArray *login_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);
56
57 struct _CamelSaslLoginPrivate {
58         int state;
59 };
60
61 static void
62 camel_sasl_login_class_init (CamelSaslLoginClass *camel_sasl_login_class)
63 {
64         CamelSaslClass *camel_sasl_class = CAMEL_SASL_CLASS (camel_sasl_login_class);
65         
66         parent_class = CAMEL_SASL_CLASS (camel_type_get_global_classfuncs (camel_sasl_get_type ()));
67         
68         /* virtual method overload */
69         camel_sasl_class->challenge = login_challenge;
70 }
71
72 static void
73 camel_sasl_login_init (gpointer object, gpointer klass)
74 {
75         CamelSaslLogin *sasl_login = CAMEL_SASL_LOGIN (object);
76         
77         sasl_login->priv = g_new0 (struct _CamelSaslLoginPrivate, 1);
78 }
79
80 static void
81 camel_sasl_login_finalize (CamelObject *object)
82 {
83         CamelSaslLogin *sasl = CAMEL_SASL_LOGIN (object);
84         
85         g_free (sasl->priv);
86 }
87
88
89 CamelType
90 camel_sasl_login_get_type (void)
91 {
92         static CamelType type = CAMEL_INVALID_TYPE;
93         
94         if (type == CAMEL_INVALID_TYPE) {
95                 type = camel_type_register (camel_sasl_get_type (),
96                                             "CamelSaslLogin",
97                                             sizeof (CamelSaslLogin),
98                                             sizeof (CamelSaslLoginClass),
99                                             (CamelObjectClassInitFunc) camel_sasl_login_class_init,
100                                             NULL,
101                                             (CamelObjectInitFunc) camel_sasl_login_init,
102                                             (CamelObjectFinalizeFunc) camel_sasl_login_finalize);
103         }
104         
105         return type;
106 }
107
108 static GByteArray *
109 login_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
110 {
111         struct _CamelSaslLoginPrivate *priv = CAMEL_SASL_LOGIN (sasl)->priv;
112         GByteArray *buf = NULL;
113         CamelURL *url = sasl->service->url;
114         
115         g_return_val_if_fail (url->passwd != NULL, NULL);
116
117         /* Need to wait for the server */
118         if (!token)
119                 return NULL;
120         
121         switch (priv->state) {
122         case LOGIN_USER:
123                 buf = g_byte_array_new ();
124                 g_byte_array_append (buf, url->user, strlen (url->user));
125                 break;
126         case LOGIN_PASSWD:
127                 buf = g_byte_array_new ();
128                 g_byte_array_append (buf, url->passwd, strlen (url->passwd));
129                 
130                 sasl->authenticated = TRUE;
131                 break;
132         default:
133                 if (!camel_exception_is_set (ex)) {
134                         camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
135                                              _("Unknown authentication state."));
136                 }
137         }
138         
139         priv->state++;
140         
141         return buf;
142 }