updated changelog
[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 (C) 1999-2008 Novell, Inc. (www.novell.com)
6  *
7  * This library is free software you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <string.h>
26
27 #include <glib/gi18n-lib.h>
28
29 #include "camel-internet-address.h"
30 #include "camel-sasl-anonymous.h"
31
32 static CamelServiceAuthType sasl_anonymous_auth_type = {
33         N_("Anonymous"),
34
35         N_("This option will connect to the server using an anonymous login."),
36
37         "ANONYMOUS",
38         FALSE
39 };
40
41 G_DEFINE_TYPE (CamelSaslAnonymous, camel_sasl_anonymous, CAMEL_TYPE_SASL)
42
43 static void
44 sasl_anonymous_finalize (GObject *object)
45 {
46         CamelSaslAnonymous *sasl = CAMEL_SASL_ANONYMOUS (object);
47
48         g_free (sasl->trace_info);
49
50         /* Chain up to parent's finalize() method. */
51         G_OBJECT_CLASS (camel_sasl_anonymous_parent_class)->finalize (object);
52 }
53
54 static GByteArray *
55 sasl_anonymous_challenge_sync (CamelSasl *sasl,
56                                GByteArray *token,
57                                GCancellable *cancellable,
58                                GError **error)
59 {
60         CamelSaslAnonymous *sasl_anon = CAMEL_SASL_ANONYMOUS (sasl);
61         CamelInternetAddress *cia;
62         GByteArray *ret = NULL;
63
64         if (token) {
65                 g_set_error (
66                         error, CAMEL_SERVICE_ERROR,
67                         CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
68                         _("Authentication failed."));
69                 return NULL;
70         }
71
72         switch (sasl_anon->type) {
73         case CAMEL_SASL_ANON_TRACE_EMAIL:
74                 cia = camel_internet_address_new ();
75                 if (camel_internet_address_add (cia, NULL, sasl_anon->trace_info) != 1) {
76                         g_set_error (
77                                 error, CAMEL_SERVICE_ERROR,
78                                 CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
79                                 _("Invalid email address trace information:\n%s"),
80                                 sasl_anon->trace_info);
81                         g_object_unref (cia);
82                         return NULL;
83                 }
84                 g_object_unref (cia);
85                 ret = g_byte_array_new ();
86                 g_byte_array_append (ret, (guint8 *) sasl_anon->trace_info, strlen (sasl_anon->trace_info));
87                 break;
88         case CAMEL_SASL_ANON_TRACE_OPAQUE:
89                 if (strchr (sasl_anon->trace_info, '@')) {
90                         g_set_error (
91                                 error, CAMEL_SERVICE_ERROR,
92                                 CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
93                                 _("Invalid opaque trace information:\n%s"),
94                                 sasl_anon->trace_info);
95                         return NULL;
96                 }
97                 ret = g_byte_array_new ();
98                 g_byte_array_append (ret, (guint8 *) sasl_anon->trace_info, strlen (sasl_anon->trace_info));
99                 break;
100         case CAMEL_SASL_ANON_TRACE_EMPTY:
101                 ret = g_byte_array_new ();
102                 break;
103         default:
104                 g_set_error (
105                         error, CAMEL_SERVICE_ERROR,
106                         CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
107                         _("Invalid trace information:\n%s"),
108                         sasl_anon->trace_info);
109                 return NULL;
110         }
111
112         camel_sasl_set_authenticated (sasl, TRUE);
113         return ret;
114 }
115
116 static void
117 camel_sasl_anonymous_class_init (CamelSaslAnonymousClass *class)
118 {
119         GObjectClass *object_class;
120         CamelSaslClass *sasl_class;
121
122         object_class = G_OBJECT_CLASS (class);
123         object_class->finalize = sasl_anonymous_finalize;
124
125         sasl_class = CAMEL_SASL_CLASS (class);
126         sasl_class->auth_type = &sasl_anonymous_auth_type;
127         sasl_class->challenge_sync = sasl_anonymous_challenge_sync;
128 }
129
130 static void
131 camel_sasl_anonymous_init (CamelSaslAnonymous *sasl_anonymous)
132 {
133 }
134
135 /**
136  * camel_sasl_anonymous_new:
137  * @type: trace type
138  * @trace_info: trace info
139  *
140  * Create a new #CamelSaslAnonymous object.
141  *
142  * Returns: a new #CamelSasl object
143  **/
144 CamelSasl *
145 camel_sasl_anonymous_new (CamelSaslAnonTraceType type,
146                           const gchar *trace_info)
147 {
148         CamelSaslAnonymous *sasl_anon;
149
150         if (!trace_info && type != CAMEL_SASL_ANON_TRACE_EMPTY)
151                 return NULL;
152
153         sasl_anon = g_object_new (CAMEL_TYPE_SASL_ANONYMOUS, NULL);
154         sasl_anon->trace_info = g_strdup (trace_info);
155         sasl_anon->type = type;
156
157         return CAMEL_SASL (sasl_anon);
158 }