build: Add an --enable-code-coverage configure option to enable gcov support
[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 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/gi18n-lib.h>
30
31 #include "camel-internet-address.h"
32 #include "camel-sasl-anonymous.h"
33
34 static CamelServiceAuthType sasl_anonymous_auth_type = {
35         N_("Anonymous"),
36
37         N_("This option will connect to the server using an anonymous login."),
38
39         "ANONYMOUS",
40         FALSE
41 };
42
43 G_DEFINE_TYPE (CamelSaslAnonymous, camel_sasl_anonymous, CAMEL_TYPE_SASL)
44
45 static void
46 sasl_anonymous_finalize (GObject *object)
47 {
48         CamelSaslAnonymous *sasl = CAMEL_SASL_ANONYMOUS (object);
49
50         g_free (sasl->trace_info);
51
52         /* Chain up to parent's finalize() method. */
53         G_OBJECT_CLASS (camel_sasl_anonymous_parent_class)->finalize (object);
54 }
55
56 static GByteArray *
57 sasl_anonymous_challenge_sync (CamelSasl *sasl,
58                                GByteArray *token,
59                                GCancellable *cancellable,
60                                GError **error)
61 {
62         CamelSaslAnonymous *sasl_anon = CAMEL_SASL_ANONYMOUS (sasl);
63         CamelInternetAddress *cia;
64         GByteArray *ret = NULL;
65
66         if (token) {
67                 g_set_error (
68                         error, CAMEL_SERVICE_ERROR,
69                         CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
70                         _("Authentication failed."));
71                 return NULL;
72         }
73
74         switch (sasl_anon->type) {
75         case CAMEL_SASL_ANON_TRACE_EMAIL:
76                 cia = camel_internet_address_new ();
77                 if (camel_internet_address_add (cia, NULL, sasl_anon->trace_info) != 1) {
78                         g_set_error (
79                                 error, CAMEL_SERVICE_ERROR,
80                                 CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
81                                 _("Invalid email address trace information:\n%s"),
82                                 sasl_anon->trace_info);
83                         g_object_unref (cia);
84                         return NULL;
85                 }
86                 g_object_unref (cia);
87                 ret = g_byte_array_new ();
88                 g_byte_array_append (ret, (guint8 *) sasl_anon->trace_info, strlen (sasl_anon->trace_info));
89                 break;
90         case CAMEL_SASL_ANON_TRACE_OPAQUE:
91                 if (strchr (sasl_anon->trace_info, '@')) {
92                         g_set_error (
93                                 error, CAMEL_SERVICE_ERROR,
94                                 CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
95                                 _("Invalid opaque trace information:\n%s"),
96                                 sasl_anon->trace_info);
97                         return NULL;
98                 }
99                 ret = g_byte_array_new ();
100                 g_byte_array_append (ret, (guint8 *) sasl_anon->trace_info, strlen (sasl_anon->trace_info));
101                 break;
102         case CAMEL_SASL_ANON_TRACE_EMPTY:
103                 ret = g_byte_array_new ();
104                 break;
105         default:
106                 g_set_error (
107                         error, CAMEL_SERVICE_ERROR,
108                         CAMEL_SERVICE_ERROR_CANT_AUTHENTICATE,
109                         _("Invalid trace information:\n%s"),
110                         sasl_anon->trace_info);
111                 return NULL;
112         }
113
114         camel_sasl_set_authenticated (sasl, TRUE);
115         return ret;
116 }
117
118 static void
119 camel_sasl_anonymous_class_init (CamelSaslAnonymousClass *class)
120 {
121         GObjectClass *object_class;
122         CamelSaslClass *sasl_class;
123
124         object_class = G_OBJECT_CLASS (class);
125         object_class->finalize = sasl_anonymous_finalize;
126
127         sasl_class = CAMEL_SASL_CLASS (class);
128         sasl_class->auth_type = &sasl_anonymous_auth_type;
129         sasl_class->challenge_sync = sasl_anonymous_challenge_sync;
130 }
131
132 static void
133 camel_sasl_anonymous_init (CamelSaslAnonymous *sasl_anonymous)
134 {
135 }
136
137 /**
138  * camel_sasl_anonymous_new:
139  * @type: trace type
140  * @trace_info: trace info
141  *
142  * Create a new #CamelSaslAnonymous object.
143  *
144  * Returns: a new #CamelSasl object
145  **/
146 CamelSasl *
147 camel_sasl_anonymous_new (CamelSaslAnonTraceType type,
148                           const gchar *trace_info)
149 {
150         CamelSaslAnonymous *sasl_anon;
151
152         if (!trace_info && type != CAMEL_SASL_ANON_TRACE_EMPTY)
153                 return NULL;
154
155         sasl_anon = g_object_new (CAMEL_TYPE_SASL_ANONYMOUS, NULL);
156         sasl_anon->trace_info = g_strdup (trace_info);
157         sasl_anon->type = type;
158
159         return CAMEL_SASL (sasl_anon);
160 }