sync with 0.8.11cvs16
[claws.git] / src / common / smtp.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "intl.h"
29 #include "smtp.h"
30 #include "md5.h"
31 #include "base64.h"
32 #include "utils.h"
33 #include "log.h"
34
35 static void smtp_session_destroy(Session *session);
36
37 static gint smtp_from(SMTPSession *session);
38
39 static gint smtp_auth(SMTPSession *session);
40 static gint smtp_starttls(SMTPSession *session);
41 static gint smtp_auth_cram_md5(SMTPSession *session);
42 static gint smtp_auth_login(SMTPSession *session);
43
44 static gint smtp_ehlo(SMTPSession *session);
45 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg);
46
47 static gint smtp_helo(SMTPSession *session);
48 static gint smtp_rcpt(SMTPSession *session);
49 static gint smtp_data(SMTPSession *session);
50 static gint smtp_send_data(SMTPSession *session);
51 static gint smtp_rset(SMTPSession *session);
52 static gint smtp_quit(SMTPSession *session);
53 static gint smtp_eom(SMTPSession *session);
54
55 static gint smtp_session_recv_msg(Session *session, const gchar *msg);
56 static gint smtp_session_send_data_finished(Session *session, guint len);
57
58
59 Session *smtp_session_new(void)
60 {
61         SMTPSession *session;
62
63         session = g_new0(SMTPSession, 1);
64         SESSION(session)->type             = SESSION_SMTP;
65         SESSION(session)->server           = NULL;
66         SESSION(session)->port             = 0;
67         SESSION(session)->sock             = NULL;
68         SESSION(session)->state            = SESSION_READY;
69         SESSION(session)->data             = NULL;
70
71         SESSION(session)->recv_msg         = smtp_session_recv_msg;
72
73         SESSION(session)->recv_data_finished = NULL;
74         SESSION(session)->send_data_finished = smtp_session_send_data_finished;
75
76         SESSION(session)->destroy          = smtp_session_destroy;
77
78         session->state                     = SMTP_READY;
79
80 #if USE_OPENSSL
81         session->tls_init_done             = FALSE;
82 #endif
83
84         session->hostname                  = NULL;
85         session->user                      = NULL;
86         session->pass                      = NULL;
87
88         session->from                      = NULL;
89         session->to_list                   = NULL;
90         session->cur_to                    = NULL;
91
92         session->send_data                 = NULL;
93         session->send_data_len             = 0;
94
95         session->avail_auth_type           = 0;
96         session->forced_auth_type          = 0;
97         session->auth_type                 = 0;
98
99         session->error_val                 = SM_OK;
100
101         return SESSION(session);
102 }
103
104 static void smtp_session_destroy(Session *session)
105 {
106         SMTPSession *smtp_session = SMTP_SESSION(session);
107
108         g_free(smtp_session->hostname);
109         g_free(smtp_session->user);
110         g_free(smtp_session->pass);
111         g_free(smtp_session->from);
112
113         g_free(smtp_session->send_data);
114 }
115
116 static gint smtp_from(SMTPSession *session)
117 {
118         gchar buf[MSGBUFSIZE];
119
120         g_return_val_if_fail(session->from != NULL, SM_ERROR);
121
122         session->state = SMTP_FROM;
123
124         if (strchr(session->from, '<'))
125                 g_snprintf(buf, sizeof(buf), "MAIL FROM: %s", session->from);
126         else
127                 g_snprintf(buf, sizeof(buf), "MAIL FROM: <%s>", session->from);
128
129         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
130         log_print("SMTP> %s\n", buf);
131
132         return SM_OK;
133 }
134
135 static gint smtp_auth(SMTPSession *session)
136 {
137
138         g_return_val_if_fail(session->user != NULL, SM_ERROR);
139
140         session->state = SMTP_AUTH;
141
142         if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
143             (session->forced_auth_type == 0 &&
144              (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
145                 smtp_auth_cram_md5(session);
146         else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
147                  (session->forced_auth_type == 0 &&
148                   (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
149                 smtp_auth_login(session);
150         else {
151                 log_warning(_("SMTP AUTH not available\n"));
152                 return SM_AUTHFAIL;
153         }
154
155         return SM_OK;
156 }
157
158 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
159 {
160         gchar buf[MSGBUFSIZE];
161
162         switch (session->auth_type) {
163         case SMTPAUTH_LOGIN:
164                 session->state = SMTP_AUTH_LOGIN_USER;
165
166                 if (!strncmp(msg, "334 ", 4)) {
167                         base64_encode(buf, session->user, strlen(session->user));
168
169                         session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
170                                          buf);
171                         log_print("ESMTP> [USERID]\n");
172                 } else {
173                         /* Server rejects AUTH */
174                         session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
175                                          "*");
176                         log_print("ESMTP> *\n");
177                 }
178                 break;
179         case SMTPAUTH_CRAM_MD5:
180                 session->state = SMTP_AUTH_CRAM_MD5;
181
182                 if (!strncmp(msg, "334 ", 4)) {
183                         gchar *response;
184                         gchar *response64;
185                         gchar *challenge;
186                         gint challengelen;
187                         guchar hexdigest[33];
188
189                         challenge = g_malloc(strlen(msg + 4) + 1);
190                         challengelen = base64_decode(challenge, msg + 4, -1);
191                         challenge[challengelen] = '\0';
192                         log_print("ESMTP< [Decoded: %s]\n", challenge);
193
194                         g_snprintf(buf, sizeof(buf), "%s", session->pass);
195                         md5_hex_hmac(hexdigest, challenge, challengelen,
196                                      buf, strlen(session->pass));
197                         g_free(challenge);
198
199                         response = g_strdup_printf
200                                 ("%s %s", session->user, hexdigest);
201                         log_print("ESMTP> [Encoded: %s]\n", response);
202
203                         response64 = g_malloc((strlen(response) + 3) * 2 + 1);
204                         base64_encode(response64, response, strlen(response));
205                         g_free(response);
206
207                         session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
208                                          response64);
209                         log_print("ESMTP> %s\n", response64);
210                         g_free(response64);
211                 } else {
212                         /* Server rejects AUTH */
213                         session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
214                                          "*");
215                         log_print("ESMTP> *\n");
216                 }
217                 break;
218         case SMTPAUTH_DIGEST_MD5:
219         default:
220                 /* stop smtp_auth when no correct authtype */
221                 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*");
222                 log_print("ESMTP> *\n");
223                 break;
224         }
225
226         return SM_OK;
227 }
228
229 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
230 {
231         gchar buf[MSGBUFSIZE];
232
233         session->state = SMTP_AUTH_LOGIN_PASS;
234
235         if (!strncmp(msg, "334 ", 4))
236                 base64_encode(buf, session->pass, strlen(session->pass));
237         else
238                 /* Server rejects AUTH */
239                 g_snprintf(buf, sizeof(buf), "*");
240
241         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
242         log_print("ESMTP> [PASSWORD]\n");
243
244         return SM_OK;
245 }
246
247 static gint smtp_ehlo(SMTPSession *session)
248 {
249         gchar buf[MSGBUFSIZE];
250
251         session->state = SMTP_EHLO;
252
253         session->avail_auth_type = 0;
254
255         g_snprintf(buf, sizeof(buf), "EHLO %s",
256                    session->hostname ? session->hostname : get_domain_name());
257         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
258         log_print("ESMTP> %s\n", buf);
259
260         return SM_OK;
261 }
262
263 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
264 {
265         if (strncmp(msg, "250", 3) == 0) {
266                 const gchar *p = msg;
267                 p += 3;
268                 if (*p == '-' || *p == ' ') p++;
269                 if (g_strncasecmp(p, "AUTH", 4) == 0) {
270                         p += 5;
271                         if (strcasestr(p, "LOGIN"))
272                                 session->avail_auth_type |= SMTPAUTH_LOGIN;
273                         if (strcasestr(p, "CRAM-MD5"))
274                                 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
275                         if (strcasestr(p, "DIGEST-MD5"))
276                                 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
277                 }
278                 return SM_OK;
279         } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
280             (msg[3] == ' ' || msg[3] == '\0'))
281                 return SM_OK;
282         else if (msg[0] == '5' && msg[1] == '0' &&
283                  (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
284                 return SM_ERROR;
285
286         return SM_ERROR;
287 }
288
289 static gint smtp_starttls(SMTPSession *session)
290 {
291         session->state = SMTP_STARTTLS;
292
293         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS");
294         log_print("ESMTP> STARTTLS\n");
295
296         return SM_OK;
297 }
298
299 static gint smtp_auth_cram_md5(SMTPSession *session)
300 {
301         session->state = SMTP_AUTH;
302         session->auth_type = SMTPAUTH_CRAM_MD5;
303
304         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5");
305         log_print("ESMTP> AUTH CRAM-MD5\n");
306
307         return SM_OK;
308 }
309
310 static gint smtp_auth_login(SMTPSession *session)
311 {
312         session->state = SMTP_AUTH;
313         session->auth_type = SMTPAUTH_LOGIN;
314
315         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN");
316         log_print("ESMTP> AUTH LOGIN\n");
317
318         return SM_OK;
319 }
320
321 static gint smtp_helo(SMTPSession *session)
322 {
323         gchar buf[MSGBUFSIZE];
324
325         session->state = SMTP_HELO;
326
327         g_snprintf(buf, sizeof(buf), "HELO %s",
328                    session->hostname ? session->hostname : get_domain_name());
329         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
330         log_print("SMTP> %s\n", buf);
331
332         return SM_OK;
333 }
334
335 static gint smtp_rcpt(SMTPSession *session)
336 {
337         gchar buf[MSGBUFSIZE];
338         gchar *to;
339
340         g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
341
342         session->state = SMTP_RCPT;
343
344         to = (gchar *)session->cur_to->data;
345
346         if (strchr(to, '<'))
347                 g_snprintf(buf, sizeof(buf), "RCPT TO: %s", to);
348         else
349                 g_snprintf(buf, sizeof(buf), "RCPT TO: <%s>", to);
350         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
351         log_print("SMTP> %s\n", buf);
352
353         session->cur_to = session->cur_to->next;
354
355         return SM_OK;
356 }
357
358 static gint smtp_data(SMTPSession *session)
359 {
360         session->state = SMTP_DATA;
361
362         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA");
363         log_print("SMTP> DATA\n");
364
365         return SM_OK;
366 }
367
368 static gint smtp_send_data(SMTPSession *session)
369 {
370         session->state = SMTP_SEND_DATA;
371
372         session_send_data(SESSION(session), session->send_data,
373                           session->send_data_len);
374
375         return SM_OK;
376 }
377
378 static gint smtp_rset(SMTPSession *session)
379 {
380         session->state = SMTP_RSET;
381
382         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "RSET");
383         log_print("SMTP> RSET\n");
384
385         return SM_OK;
386 }
387
388 static gint smtp_quit(SMTPSession *session)
389 {
390         session->state = SMTP_QUIT;
391
392         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
393         log_print("SMTP> QUIT\n");
394
395         return SM_OK;
396 }
397
398 static gint smtp_eom(SMTPSession *session)
399 {
400         session->state = SMTP_EOM;
401
402         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".");
403         log_print("SMTP> . (EOM)\n");
404
405         return SM_OK;
406 }
407
408 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
409 {
410         SMTPSession *smtp_session = SMTP_SESSION(session);
411         gboolean cont = FALSE;
412
413         if (strlen(msg) < 4) {
414                 log_warning(_("bad SMTP response\n"));
415                 return -1;
416         }
417
418         switch (smtp_session->state) {
419         case SMTP_EHLO:
420         case SMTP_STARTTLS:
421         case SMTP_AUTH:
422         case SMTP_AUTH_LOGIN_USER:
423         case SMTP_AUTH_LOGIN_PASS:
424         case SMTP_AUTH_CRAM_MD5:
425                 log_print("ESMTP< %s\n", msg);
426                 break;
427         default:
428                 log_print("SMTP< %s\n", msg);
429                 break;
430         }
431
432         if (msg[0] == '5' && msg[1] == '0' &&
433             (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
434                 log_warning(_("error occurred on SMTP session\n"));
435                 smtp_session->state = SMTP_ERROR;
436                 smtp_session->error_val = SM_ERROR;
437                 return -1;
438         }
439
440         if (msg[0] == '5' && msg[1] == '3' && msg[2] == '5') {
441                 smtp_session->state = SMTP_AUTH_FAILED;
442                 return -1;
443         }
444
445         if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
446                 log_warning(_("error occurred on SMTP session\n"));
447                 smtp_session->state = SMTP_ERROR;
448                 smtp_session->error_val = SM_ERROR;
449                 return -1;
450         }
451
452         if (msg[3] == '-')
453                 cont = TRUE;
454         else if (msg[3] != ' ' && msg[3] != '\0') {
455                 log_warning(_("bad SMTP response\n"));
456                 smtp_session->state = SMTP_ERROR;
457                 smtp_session->error_val = SM_UNRECOVERABLE;
458                 return -1;
459         }
460
461         /* ignore all multiline responses except for EHLO */
462         if (cont && smtp_session->state != SMTP_EHLO)
463                 return 1;
464
465         switch (smtp_session->state) {
466         case SMTP_READY:
467         case SMTP_CONNECTED:
468 #if USE_OPENSSL
469                 if (smtp_session->user || session->ssl_type != SSL_NONE)
470 #else
471                 if (smtp_session->user)
472 #endif
473                         smtp_ehlo(smtp_session);
474                 else
475                         smtp_helo(smtp_session);
476                 break;
477         case SMTP_HELO:
478                 smtp_from(smtp_session);
479                 break;
480         case SMTP_EHLO:
481                 smtp_ehlo_recv(smtp_session, msg);
482                 if (cont == TRUE)
483                         break;
484 #if USE_OPENSSL
485                 if (session->ssl_type == SSL_STARTTLS &&
486                     smtp_session->tls_init_done == FALSE) {
487                         smtp_starttls(smtp_session);
488                         break;
489                 }
490 #endif
491                 if (smtp_session->user) {
492                         if (smtp_auth(smtp_session) != SM_OK)
493                                 smtp_from(smtp_session);
494                 } else
495                         smtp_from(smtp_session);
496                 break;
497         case SMTP_STARTTLS:
498 #if USE_OPENSSL
499                 if (session_start_tls(session) < 0) {
500                         log_warning(_("can't start TLS session\n"));
501                         smtp_session->state = SMTP_ERROR;
502                         smtp_session->error_val = SM_ERROR;
503                         return -1;
504                 }
505                 smtp_session->tls_init_done = TRUE;
506                 smtp_ehlo(smtp_session);
507 #endif
508                 break;
509         case SMTP_AUTH:
510                 smtp_auth_recv(smtp_session, msg);
511                 break;
512         case SMTP_AUTH_LOGIN_USER:
513                 smtp_auth_login_user_recv(smtp_session, msg);
514                 break;
515         case SMTP_AUTH_LOGIN_PASS:
516         case SMTP_AUTH_CRAM_MD5:
517                 smtp_from(smtp_session);
518                 break;
519         case SMTP_FROM:
520                 if (smtp_session->cur_to)
521                         smtp_rcpt(smtp_session);
522                 break;
523         case SMTP_RCPT:
524                 if (smtp_session->cur_to)
525                         smtp_rcpt(smtp_session);
526                 else
527                         smtp_data(smtp_session);
528                 break;
529         case SMTP_DATA:
530                 smtp_send_data(smtp_session);
531                 break;
532         case SMTP_EOM:
533                 smtp_quit(smtp_session);
534                 break;
535         case SMTP_QUIT:
536                 session_disconnect(session);
537                 break;
538         case SMTP_ERROR:
539         case SMTP_AUTH_FAILED:
540         default:
541                 log_warning(_("error occurred on SMTP session\n"));
542                 smtp_session->error_val = SM_ERROR;
543                 return -1;
544         }
545
546         if (cont)
547                 return 1;
548
549         return 0;
550 }
551
552 static gint smtp_session_send_data_finished(Session *session, guint len)
553 {
554         smtp_eom(SMTP_SESSION(session));
555         return 0;
556 }