Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-sasl-anonymous.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-2003 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
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <string.h>
29
30 #include <glib.h>
31 #include <glib/gi18n-lib.h>
32
33 #include "camel-internet-address.h"
34 #include "camel-sasl-anonymous.h"
35
36 CamelServiceAuthType camel_sasl_anonymous_authtype = {
37         N_("Anonymous"),
38         
39         N_("This option will connect to the server using an anonymous login."),
40         
41         "ANONYMOUS",
42         FALSE
43 };
44
45 static CamelSaslClass *parent_class = NULL;
46
47 /* Returns the class for a CamelSaslAnonymous */
48 #define CSA_CLASS(so) CAMEL_SASL_ANONYMOUS_CLASS (CAMEL_OBJECT_GET_CLASS (so))
49
50 static GByteArray *anon_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex);
51
52 static void
53 camel_sasl_anonymous_class_init (CamelSaslAnonymousClass *camel_sasl_anonymous_class)
54 {
55         CamelSaslClass *camel_sasl_class = CAMEL_SASL_CLASS (camel_sasl_anonymous_class);
56         
57         parent_class = CAMEL_SASL_CLASS (camel_type_get_global_classfuncs (camel_sasl_get_type ()));
58         
59         /* virtual method overload */
60         camel_sasl_class->challenge = anon_challenge;
61 }
62
63 static void
64 camel_sasl_anonymous_finalize (CamelObject *object)
65 {
66         CamelSaslAnonymous *sasl = CAMEL_SASL_ANONYMOUS (object);
67         
68         g_free (sasl->trace_info);
69 }
70
71
72 CamelType
73 camel_sasl_anonymous_get_type (void)
74 {
75         static CamelType type = CAMEL_INVALID_TYPE;
76         
77         if (type == CAMEL_INVALID_TYPE) {
78                 type = camel_type_register (camel_sasl_get_type (),
79                                             "CamelSaslAnonymous",
80                                             sizeof (CamelSaslAnonymous),
81                                             sizeof (CamelSaslAnonymousClass),
82                                             (CamelObjectClassInitFunc) camel_sasl_anonymous_class_init,
83                                             NULL,
84                                             NULL,
85                                             (CamelObjectFinalizeFunc) camel_sasl_anonymous_finalize);
86         }
87         
88         return type;
89 }
90
91
92 /**
93  * camel_sasl_anonymous_new:
94  * @type: trace type
95  * @trace_info: trace info
96  *
97  * Create a new #CamelSaslAnonymous object.
98  *
99  * Returns a new #CamelSasl object
100  **/
101 CamelSasl *
102 camel_sasl_anonymous_new (CamelSaslAnonTraceType type, const char *trace_info)
103 {
104         CamelSaslAnonymous *sasl_anon;
105         
106         if (!trace_info && type != CAMEL_SASL_ANON_TRACE_EMPTY) return NULL;
107         
108         sasl_anon = CAMEL_SASL_ANONYMOUS (camel_object_new (camel_sasl_anonymous_get_type ()));
109         sasl_anon->trace_info = g_strdup (trace_info);
110         sasl_anon->type = type;
111         
112         return CAMEL_SASL (sasl_anon);
113 }
114
115 static GByteArray *
116 anon_challenge (CamelSasl *sasl, GByteArray *token, CamelException *ex)
117 {
118         CamelSaslAnonymous *sasl_anon = CAMEL_SASL_ANONYMOUS (sasl);
119         CamelInternetAddress *cia;
120         GByteArray *ret = NULL;
121
122         if (token) {
123                 camel_exception_set (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
124                                      _("Authentication failed."));
125                 return NULL;
126         }
127
128         switch (sasl_anon->type) {
129         case CAMEL_SASL_ANON_TRACE_EMAIL:
130                 cia = camel_internet_address_new ();
131                 if (camel_internet_address_add (cia, NULL, sasl_anon->trace_info) != 1) {
132                         camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
133                                               _("Invalid email address trace information:\n%s"),
134                                               sasl_anon->trace_info);
135                         camel_object_unref (cia);
136                         return NULL;
137                 }
138                 camel_object_unref (cia);
139                 ret = g_byte_array_new ();
140                 g_byte_array_append (ret, sasl_anon->trace_info, strlen (sasl_anon->trace_info));
141                 break;
142         case CAMEL_SASL_ANON_TRACE_OPAQUE:
143                 if (strchr (sasl_anon->trace_info, '@')) {
144                         camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
145                                               _("Invalid opaque trace information:\n%s"),
146                                               sasl_anon->trace_info);
147                         return NULL;
148                 }
149                 ret = g_byte_array_new ();
150                 g_byte_array_append (ret, sasl_anon->trace_info, strlen (sasl_anon->trace_info));
151                 break;
152         case CAMEL_SASL_ANON_TRACE_EMPTY:
153                 ret = g_byte_array_new ();
154                 break;
155         default:
156                 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_CANT_AUTHENTICATE,
157                                       _("Invalid trace information:\n%s"),
158                                       sasl_anon->trace_info);
159                 return NULL;
160         }
161
162         sasl->authenticated = TRUE;
163         return ret;
164 }