Debian Codegraph @

src/goabackend/goasmtpauth.c

raw symbols SCIP
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright © 2011 – 2017 Red Hat, Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "config.h"
20
21 #include <string.h>
22
23 #include <glib/gi18n-lib.h>
24
25 #include "goasmtpauth.h"
26 #include "goautils.h"
27
28 /*
29 * SECTION:goasmtpauth
30 * @title: GoaSmtpAuth
31 * @short_description: Authentication mechanisms for SMTP
32 *
33 * #GoaSmtpAuth implements the <ulink
34 * url="http://tools.ietf.org/html/rfc4616">PLAIN</ulink> and <ulink
35 * url="http://msdn.microsoft.com/en-us/library/cc433484(v=EXCHG.80).aspx">LOGIN</ulink>
36 * SASL mechanisms (e.g. using usernames / passwords) for SMTP.
37 */
38
39 /*
40 * GoaSmtpAuth:
41 *
42 * The #GoaSmtpAuth structure contains only private data and should
43 * only be accessed using the provided API.
44 */
45 struct _GoaSmtpAuth
46 {
47 GoaMailAuth parent_instance;
48
49 gboolean auth_supported;
50 gboolean greeting_absent;
51 gboolean login_supported;
52 gboolean plain_supported;
53 gchar *domain;
54 gchar *username;
55 gchar *password;
56 };
57
58 enum
59 {
60 PROP_0,
61 PROP_DOMAIN,
62 PROP_USERNAME,
63 PROP_PASSWORD
64 };
65
66 static gboolean goa_smtp_auth_is_needed (GoaMailAuth *auth);
67 static gboolean goa_smtp_auth_run_sync (GoaMailAuth *auth,
68 GCancellable *cancellable,
69 GError **error);
70 static gboolean goa_smtp_auth_starttls_sync (GoaMailAuth *auth,
71 GCancellable *cancellable,
72 GError **error);
73
74 G_DEFINE_TYPE (GoaSmtpAuth, goa_smtp_auth, GOA_TYPE_MAIL_AUTH);
75
76 /* ---------------------------------------------------------------------------------------------------- */
77
78 static gboolean
79 smtp_auth_check_not_220 (const gchar *response, GError **error)
80 {
81 if (!g_str_has_prefix (response, "220"))
82 {
83 g_set_error (error,
84 GOA_ERROR,
85 GOA_ERROR_FAILED, /* TODO: more specific */
86 "Unexpected response `%s'",
87 response);
88 return TRUE;
89 }
90
91 return FALSE;
92 }
93
94 static gboolean
95 smtp_auth_check_not_235 (const gchar *response, GError **error)
96 {
97 if (!g_str_has_prefix (response, "235"))
98 {
99 g_set_error (error,
100 GOA_ERROR,
101 GOA_ERROR_FAILED, /* TODO: more specific */
102 _("Authentication failed"));
103 return TRUE;
104 }
105
106 return FALSE;
107 }
108
109 static gboolean
110 smtp_auth_check_not_250 (const gchar *response, GError **error)
111 {
112 if (!g_str_has_prefix (response, "250") || strlen (response) < 4)
113 {
114 g_set_error (error,
115 GOA_ERROR,
116 GOA_ERROR_FAILED, /* TODO: more specific */
117 "Unexpected response `%s'",
118 response);
119 return TRUE;
120 }
121
122 return FALSE;
123 }
124
125 static gboolean
126 smtp_auth_check_not_334_login_password (const gchar *response, GError **error)
127 {
128 if (!g_str_has_prefix (response, "334 UGFzc3dvcmQ6"))
129 {
130 g_set_error (error,
131 GOA_ERROR,
132 GOA_ERROR_FAILED, /* TODO: more specific */
133 "Unexpected response `%s'",
134 response);
135 return TRUE;
136 }
137
138 return FALSE;
139 }
140
141 static gboolean
142 smtp_auth_check_421 (const gchar *response, GError **error)
143 {
144 if (g_str_has_prefix (response, "421"))
145 {
146 g_set_error (error,
147 GOA_ERROR,
148 GOA_ERROR_FAILED, /* TODO: more specific */
149 _("Service not available"));
150 return TRUE;
151 }
152
153 return FALSE;
154 }
155
156 static gboolean
157 smtp_auth_check_454 (const gchar *response, GError **error)
158 {
159 if (g_str_has_prefix (response, "454"))
160 {
161 g_set_error (error,
162 GOA_ERROR,
163 GOA_ERROR_FAILED, /* TODO: more specific */
164 _("TLS not available"));
165 return TRUE;
166 }
167
168 return FALSE;
169 }
170
171 /* ---------------------------------------------------------------------------------------------------- */
172
173 static gboolean
174 smtp_auth_check_greeting (GDataInputStream *input, GCancellable *cancellable, GError **error)
175 {
176 gboolean ret = FALSE;
177 gchar *response = NULL;
178
179 greeting_again:
180 response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
181 if (response == NULL)
182 goto out;
183 g_debug ("< %s", response);
184 if (smtp_auth_check_421 (response, error))
185 goto out;
186 if (smtp_auth_check_not_220 (response, error))
187 goto out;
188
189 if (response[3] == '-')
190 {
191 g_clear_pointer (&response, g_free);
192 goto greeting_again;
193 }
194
195 ret = TRUE;
196
197 out:
198 g_free (response);
199 return ret;
200 }
201
202 /* ---------------------------------------------------------------------------------------------------- */
203
204 static void
205 goa_smtp_auth_finalize (GObject *object)
206 {
207 GoaSmtpAuth *self = GOA_SMTP_AUTH (object);
208
209 g_free (self->domain);
210 g_free (self->username);
211 g_free (self->password);
212
213 G_OBJECT_CLASS (goa_smtp_auth_parent_class)->finalize (object);
214 }
215
216 static void
217 goa_smtp_auth_get_property (GObject *object,
218 guint prop_id,
219 GValue *value,
220 GParamSpec *pspec)
221 {
222 GoaSmtpAuth *self = GOA_SMTP_AUTH (object);
223
224 switch (prop_id)
225 {
226 case PROP_DOMAIN:
227 g_value_set_string (value, self->domain);
228 break;
229
230 case PROP_USERNAME:
231 g_value_set_string (value, self->username);
232 break;
233
234 case PROP_PASSWORD:
235 g_value_set_string (value, self->password);
236 break;
237
238 default:
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 break;
241 }
242 }
243
244 static void
245 goa_smtp_auth_set_property (GObject *object,
246 guint prop_id,
247 const GValue *value,
248 GParamSpec *pspec)
249 {
250 GoaSmtpAuth *self = GOA_SMTP_AUTH (object);
251
252 switch (prop_id)
253 {
254 case PROP_DOMAIN:
255 self->domain = g_value_dup_string (value);
256 break;
257
258 case PROP_USERNAME:
259 self->username = g_value_dup_string (value);
260 break;
261
262 case PROP_PASSWORD:
263 self->password = g_value_dup_string (value);
264 break;
265
266 default:
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
268 break;
269 }
270 }
271
272 /* ---------------------------------------------------------------------------------------------------- */
273
274
275 static void
276 goa_smtp_auth_init (GoaSmtpAuth *self)
277 {
278 }
279
280 static void
281 goa_smtp_auth_class_init (GoaSmtpAuthClass *klass)
282 {
283 GObjectClass *gobject_class;
284 GoaMailAuthClass *auth_class;
285
286 gobject_class = G_OBJECT_CLASS (klass);
287 gobject_class->finalize = goa_smtp_auth_finalize;
288 gobject_class->get_property = goa_smtp_auth_get_property;
289 gobject_class->set_property = goa_smtp_auth_set_property;
290
291 auth_class = GOA_MAIL_AUTH_CLASS (klass);
292 auth_class->is_needed = goa_smtp_auth_is_needed;
293 auth_class->run_sync = goa_smtp_auth_run_sync;
294 auth_class->starttls_sync = goa_smtp_auth_starttls_sync;
295
296 /**
297 * GoaSmtpAuth:domain:
298 *
299 * The domain.
300 */
301 g_object_class_install_property (gobject_class,
302 PROP_DOMAIN,
303 g_param_spec_string ("domain",
304 "domain",
305 "domain",
306 NULL,
307 G_PARAM_READABLE |
308 G_PARAM_WRITABLE |
309 G_PARAM_CONSTRUCT_ONLY |
310 G_PARAM_STATIC_STRINGS));
311
312 /**
313 * GoaSmtpAuth:user-name:
314 *
315 * The user name.
316 */
317 g_object_class_install_property (gobject_class,
318 PROP_USERNAME,
319 g_param_spec_string ("user-name",
320 "user-name",
321 "user-name",
322 NULL,
323 G_PARAM_READABLE |
324 G_PARAM_WRITABLE |
325 G_PARAM_CONSTRUCT_ONLY |
326 G_PARAM_STATIC_STRINGS));
327
328 /**
329 * GoaSmtpAuth:password:
330 *
331 * The password.
332 */
333 g_object_class_install_property (gobject_class,
334 PROP_PASSWORD,
335 g_param_spec_string ("password",
336 "password",
337 "password",
338 NULL,
339 G_PARAM_READABLE |
340 G_PARAM_WRITABLE |
341 G_PARAM_CONSTRUCT_ONLY |
342 G_PARAM_STATIC_STRINGS));
343 }
344
345 /* ---------------------------------------------------------------------------------------------------- */
346
347 /**
348 * goa_smtp_auth_new:
349 * @domain: The domain to use.
350 * @username: (nullable): The user name to use.
351 * @password: (nullable): The password to use.
352 *
353 * Creates a new #GoaMailAuth to be used for username/password
354 * authentication using LOGIN or PLAIN over SMTP.
355 *
356 * If either @username or @password are %NULL, authentication will only
357 * succeed if the service supports it.
358 *
359 * Returns: (type GoaSmtpAuth): A #GoaSmtpAuth. Free with
360 * g_object_unref().
361 */
362 GoaMailAuth *
363 goa_smtp_auth_new (const gchar *domain,
364 const gchar *username,
365 const gchar *password)
366 {
367 g_return_val_if_fail (domain != NULL && domain[0] != '\0', NULL);
368 g_return_val_if_fail (username != NULL || (username == NULL && password == NULL), NULL);
369 g_return_val_if_fail (password == NULL || password[0] != '\0', NULL);
370
371 return GOA_MAIL_AUTH (g_object_new (GOA_TYPE_SMTP_AUTH,
372 "domain", domain,
373 "user-name", username,
374 "password", password,
375 NULL));
376 }
377
378 /* ---------------------------------------------------------------------------------------------------- */
379
380 gboolean
381 goa_smtp_auth_is_login (GoaSmtpAuth *self)
382 {
383 return self->login_supported;
384 }
385
386 gboolean
387 goa_smtp_auth_is_plain (GoaSmtpAuth *self)
388 {
389 return self->plain_supported;
390 }
391
392 /* ---------------------------------------------------------------------------------------------------- */
393
394 static gboolean
395 goa_smtp_auth_is_needed (GoaMailAuth *auth)
396 {
397 GoaSmtpAuth *self = GOA_SMTP_AUTH (auth);
398 return self->auth_supported;
399 }
400
401 /* ---------------------------------------------------------------------------------------------------- */
402
403 static gboolean
404 goa_smtp_auth_run_sync (GoaMailAuth *auth,
405 GCancellable *cancellable,
406 GError **error)
407 {
408 GoaSmtpAuth *self = GOA_SMTP_AUTH (auth);
409 GDataInputStream *input;
410 GDataOutputStream *output;
411 gboolean ret = FALSE;
412 gchar *auth_arg_base64 = NULL;
413 gchar *auth_arg_plain = NULL;
414 gchar *request = NULL;
415 gchar *response = NULL;
416 gsize auth_arg_plain_len;
417
418 input = goa_mail_auth_get_input (auth);
419 output = goa_mail_auth_get_output (auth);
420
421 /* Check the greeting, if there is one */
422
423 if (!self->greeting_absent)
424 {
425 if (!smtp_auth_check_greeting (input, cancellable, error))
426 goto out;
427 }
428
429 /* Send EHLO */
430
431 request = g_strdup_printf ("EHLO %s\r\n", self->domain);
432 g_debug ("> %s", request);
433 if (!g_data_output_stream_put_string (output, request, cancellable, error))
434 goto out;
435 g_clear_pointer (&request, g_free);
436
437 /* Check which SASL mechanisms are supported */
438
439 ehlo_again:
440 response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
441 if (response == NULL)
442 goto out;
443 g_debug ("< %s", response);
444 if (smtp_auth_check_421 (response, error))
445 goto out;
446 if (smtp_auth_check_not_250 (response, error))
447 goto out;
448
449 if (g_str_has_prefix (response + 4, "AUTH"))
450 {
451 self->auth_supported = TRUE;
452 if (strstr (response, "PLAIN") != NULL)
453 self->plain_supported = TRUE;
454 else if (strstr (response, "LOGIN") != NULL)
455 self->login_supported = TRUE;
456 }
457
458 if (response[3] == '-')
459 {
460 g_free (response);
461 goto ehlo_again;
462 }
463 else if (!self->auth_supported)
464 {
465 ret = TRUE;
466 goto out;
467 }
468 else if (self->username == NULL && self->password == NULL)
469 {
470 g_set_error (error,
471 GOA_ERROR,
472 GOA_ERROR_NOT_AUTHORIZED,
473 _("Authentication failed"));
474 goto out;
475 }
476 else if (!self->login_supported && !self->plain_supported)
477 {
478 g_set_error (error,
479 GOA_ERROR,
480 GOA_ERROR_NOT_SUPPORTED,
481 _("Unknown authentication mechanism"));
482 goto out;
483 }
484 g_clear_pointer (&response, g_free);
485
486 /* Try different SASL mechanisms */
487 if (self->plain_supported)
488 {
489 size_t username_len, password_len;
490
491 /* AUTH PLAIN */
492
493 username_len = self->username ? strlen (self->username) : 0;
494 password_len = self->password ? strlen (self->password) : 0;
495 auth_arg_plain = g_strdup_printf ("%s%c%s%c%s",
496 self->username ? self->username : "",
497 '\0',
498 self->username ? self->username : "",
499 '\0',
500 self->password ? self->password : "");
501 auth_arg_plain_len = 2 * username_len + 2 + password_len;
502 auth_arg_base64 = g_base64_encode ((guchar *) auth_arg_plain, auth_arg_plain_len);
503
504 request = g_strdup_printf ("AUTH PLAIN %s\r\n", auth_arg_base64);
505 g_debug ("> AUTH PLAIN ********************");
506 if (!g_data_output_stream_put_string (output, request, cancellable, error))
507 goto out;
508 g_clear_pointer (&request, g_free);
509 }
510 else
511 {
512 /* AUTH LOGIN */
513
514 auth_arg_base64 = g_base64_encode ((guchar *) self->username,
515 self->username ? strlen (self->username) : 0);
516
517 request = g_strdup_printf ("AUTH LOGIN %s\r\n", auth_arg_base64);
518 g_debug ("> AUTH LOGIN ********************");
519 if (!g_data_output_stream_put_string (output, request, cancellable, error))
520 goto out;
521 g_clear_pointer (&request, g_free);
522
523 response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
524 if (response == NULL)
525 goto out;
526 g_debug ("< %s", response);
527 if (smtp_auth_check_not_334_login_password (response, error))
528 goto out;
529
530 g_free (auth_arg_base64);
531
532 auth_arg_base64 = g_base64_encode ((guchar *) self->password,
533 self->password ? strlen (self->password) : 0);
534
535 request = g_strdup_printf ("%s\r\n", auth_arg_base64);
536 g_debug ("> ********************");
537 if (!g_data_output_stream_put_string (output, request, cancellable, error))
538 goto out;
539 g_clear_pointer (&request, g_free);
540 }
541
542 response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
543 if (response == NULL)
544 goto out;
545 g_debug ("< %s", response);
546 if (smtp_auth_check_not_235 (response, error))
547 goto out;
548 g_clear_pointer (&response, g_free);
549
550 ret = TRUE;
551
552 out:
553 g_free (auth_arg_base64);
554 g_free (auth_arg_plain);
555 g_free (response);
556 g_free (request);
557 return ret;
558 }
559
560 /* ---------------------------------------------------------------------------------------------------- */
561
562 static gboolean
563 goa_smtp_auth_starttls_sync (GoaMailAuth *auth,
564 GCancellable *cancellable,
565 GError **error)
566 {
567 GoaSmtpAuth *self = GOA_SMTP_AUTH (auth);
568 GDataInputStream *input;
569 GDataOutputStream *output;
570 gboolean ret = FALSE;
571 gboolean starttls_supported = FALSE;
572 gchar *request = NULL;
573 gchar *response = NULL;
574
575 input = goa_mail_auth_get_input (auth);
576 output = goa_mail_auth_get_output (auth);
577
578 /* Check the greeting */
579
580 if (!smtp_auth_check_greeting (input, cancellable, error))
581 goto out;
582
583 /* Send EHLO */
584
585 request = g_strdup_printf ("EHLO %s\r\n", self->domain);
586 g_debug ("> %s", request);
587 if (!g_data_output_stream_put_string (output, request, cancellable, error))
588 goto out;
589 g_clear_pointer (&request, g_free);
590
591 /* Check if STARTTLS is supported or not */
592
593 ehlo_again:
594 response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
595 if (response == NULL)
596 goto out;
597 g_debug ("< %s", response);
598 if (smtp_auth_check_421 (response, error))
599 goto out;
600 if (smtp_auth_check_not_250 (response, error))
601 goto out;
602
603 if (g_str_has_prefix (response + 4, "STARTTLS"))
604 starttls_supported = TRUE;
605
606 if (response[3] == '-')
607 {
608 g_free (response);
609 goto ehlo_again;
610 }
611 else if (!starttls_supported)
612 {
613 g_set_error (error,
614 GOA_ERROR,
615 GOA_ERROR_NOT_SUPPORTED,
616 _("Server does not support STARTTLS"));
617 goto out;
618 }
619 g_clear_pointer (&response, g_free);
620
621 /* Send STARTTLS */
622
623 request = g_strdup ("STARTTLS\r\n");
624 g_debug ("> %s", request);
625 if (!g_data_output_stream_put_string (output, request, cancellable, error))
626 goto out;
627 g_clear_pointer (&request, g_free);
628
629 response = goa_utils_data_input_stream_read_line (input, NULL, cancellable, error);
630 if (response == NULL)
631 goto out;
632 g_debug ("< %s", response);
633 if (smtp_auth_check_454 (response, error))
634 goto out;
635 if (smtp_auth_check_not_220 (response, error))
636 goto out;
637 g_clear_pointer (&response, g_free);
638
639 /* There won't be a greeting after this */
640 self->greeting_absent = TRUE;
641
642 ret = TRUE;
643
644 out:
645 g_free (response);
646 g_free (request);
647 return ret;
648 }