2004-11-26 [colin] 0.9.12cvs173.1
[claws.git] / src / common / smtp.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 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
65         session_init(SESSION(session));
66
67         SESSION(session)->type             = SESSION_SMTP;
68
69         SESSION(session)->recv_msg         = smtp_session_recv_msg;
70
71         SESSION(session)->recv_data_finished = NULL;
72         SESSION(session)->send_data_finished = smtp_session_send_data_finished;
73
74         SESSION(session)->destroy          = smtp_session_destroy;
75
76         session->state                     = SMTP_READY;
77
78 #if USE_OPENSSL
79         session->tls_init_done             = FALSE;
80 #endif
81
82         session->hostname                  = NULL;
83         session->user                      = NULL;
84         session->pass                      = NULL;
85
86         session->from                      = NULL;
87         session->to_list                   = NULL;
88         session->cur_to                    = NULL;
89
90         session->send_data                 = NULL;
91         session->send_data_len             = 0;
92
93         session->max_message_size          = -1;
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         session->error_msg                 = NULL;
101
102         return SESSION(session);
103 }
104
105 static void smtp_session_destroy(Session *session)
106 {
107         SMTPSession *smtp_session = SMTP_SESSION(session);
108
109         g_free(smtp_session->hostname);
110         g_free(smtp_session->user);
111         g_free(smtp_session->pass);
112         g_free(smtp_session->from);
113
114         g_free(smtp_session->send_data);
115
116         g_free(smtp_session->error_msg);
117 }
118
119 static gint smtp_from(SMTPSession *session)
120 {
121         gchar buf[MSGBUFSIZE];
122         gchar *mail_size = NULL;
123
124         g_return_val_if_fail(session->from != NULL, SM_ERROR);
125
126         session->state = SMTP_FROM;
127         
128         if (session->is_esmtp)
129                 mail_size = g_strdup_printf(" SIZE=%d", session->send_data_len);
130         else
131                 mail_size = g_strdup("");
132                 
133
134         if (strchr(session->from, '<'))
135                 g_snprintf(buf, sizeof(buf), "MAIL FROM:%s%s", session->from,
136                            mail_size);
137         else
138                 g_snprintf(buf, sizeof(buf), "MAIL FROM:<%s>%s", session->from,
139                            mail_size);
140
141         g_free(mail_size);
142
143         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
144         log_print("%sSMTP> %s\n", (session->is_esmtp?"E":""), buf);
145
146         return SM_OK;
147 }
148
149 static gint smtp_auth(SMTPSession *session)
150 {
151
152         g_return_val_if_fail(session->user != NULL, SM_ERROR);
153
154         session->state = SMTP_AUTH;
155
156         if (session->forced_auth_type == SMTPAUTH_CRAM_MD5 ||
157             (session->forced_auth_type == 0 &&
158              (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0))
159                 smtp_auth_cram_md5(session);
160         else if (session->forced_auth_type == SMTPAUTH_LOGIN ||
161                  (session->forced_auth_type == 0 &&
162                   (session->avail_auth_type & SMTPAUTH_LOGIN) != 0))
163                 smtp_auth_login(session);
164         else {
165                 log_warning(_("SMTP AUTH not available\n"));
166                 return SM_AUTHFAIL;
167         }
168
169         return SM_OK;
170 }
171
172 static gint smtp_auth_recv(SMTPSession *session, const gchar *msg)
173 {
174         gchar buf[MSGBUFSIZE];
175
176         switch (session->auth_type) {
177         case SMTPAUTH_LOGIN:
178                 session->state = SMTP_AUTH_LOGIN_USER;
179
180                 if (!strncmp(msg, "334 ", 4)) {
181                         base64_encode(buf, session->user, strlen(session->user));
182
183                         session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
184                                          buf);
185                         log_print("ESMTP> [USERID]\n");
186                 } else {
187                         /* Server rejects AUTH */
188                         session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
189                                          "*");
190                         log_print("ESMTP> *\n");
191                 }
192                 break;
193         case SMTPAUTH_CRAM_MD5:
194                 session->state = SMTP_AUTH_CRAM_MD5;
195
196                 if (!strncmp(msg, "334 ", 4)) {
197                         gchar *response;
198                         gchar *response64;
199                         gchar *challenge;
200                         gint challengelen;
201                         guchar hexdigest[33];
202
203                         challenge = g_malloc(strlen(msg + 4) + 1);
204                         challengelen = base64_decode(challenge, msg + 4, -1);
205                         challenge[challengelen] = '\0';
206                         log_print("ESMTP< [Decoded: %s]\n", challenge);
207
208                         g_snprintf(buf, sizeof(buf), "%s", session->pass);
209                         md5_hex_hmac(hexdigest, challenge, challengelen,
210                                      buf, strlen(session->pass));
211                         g_free(challenge);
212
213                         response = g_strdup_printf
214                                 ("%s %s", session->user, hexdigest);
215                         log_print("ESMTP> [Encoded: %s]\n", response);
216
217                         response64 = g_malloc((strlen(response) + 3) * 2 + 1);
218                         base64_encode(response64, response, strlen(response));
219                         g_free(response);
220
221                         session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
222                                          response64);
223                         log_print("ESMTP> %s\n", response64);
224                         g_free(response64);
225                 } else {
226                         /* Server rejects AUTH */
227                         session_send_msg(SESSION(session), SESSION_MSG_NORMAL,
228                                          "*");
229                         log_print("ESMTP> *\n");
230                 }
231                 break;
232         case SMTPAUTH_DIGEST_MD5:
233         default:
234                 /* stop smtp_auth when no correct authtype */
235                 session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "*");
236                 log_print("ESMTP> *\n");
237                 break;
238         }
239
240         return SM_OK;
241 }
242
243 static gint smtp_auth_login_user_recv(SMTPSession *session, const gchar *msg)
244 {
245         gchar buf[MSGBUFSIZE];
246
247         session->state = SMTP_AUTH_LOGIN_PASS;
248
249         if (!strncmp(msg, "334 ", 4))
250                 base64_encode(buf, session->pass, strlen(session->pass));
251         else
252                 /* Server rejects AUTH */
253                 g_snprintf(buf, sizeof(buf), "*");
254
255         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
256         log_print("ESMTP> [PASSWORD]\n");
257
258         return SM_OK;
259 }
260
261 static gint smtp_ehlo(SMTPSession *session)
262 {
263         gchar buf[MSGBUFSIZE];
264
265         session->state = SMTP_EHLO;
266
267         session->avail_auth_type = 0;
268
269         g_snprintf(buf, sizeof(buf), "EHLO %s",
270                    session->hostname ? session->hostname : get_domain_name());
271         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
272         log_print("ESMTP> %s\n", buf);
273
274         return SM_OK;
275 }
276
277 static gint smtp_ehlo_recv(SMTPSession *session, const gchar *msg)
278 {
279         if (strncmp(msg, "250", 3) == 0) {
280                 const gchar *p = msg;
281                 p += 3;
282                 if (*p == '-' || *p == ' ') p++;
283                 if (g_ascii_strncasecmp(p, "AUTH", 4) == 0) {
284                         p += 5;
285                         if (strcasestr(p, "LOGIN"))
286                                 session->avail_auth_type |= SMTPAUTH_LOGIN;
287                         if (strcasestr(p, "CRAM-MD5"))
288                                 session->avail_auth_type |= SMTPAUTH_CRAM_MD5;
289                         if (strcasestr(p, "DIGEST-MD5"))
290                                 session->avail_auth_type |= SMTPAUTH_DIGEST_MD5;
291                 }
292                 if (g_ascii_strncasecmp(p, "SIZE", 4) == 0) {
293                         p += 5;
294                         session->max_message_size = atoi(p);
295                 }
296                 return SM_OK;
297         } else if ((msg[0] == '1' || msg[0] == '2' || msg[0] == '3') &&
298             (msg[3] == ' ' || msg[3] == '\0'))
299                 return SM_OK;
300         else if (msg[0] == '5' && msg[1] == '0' &&
301                  (msg[2] == '4' || msg[2] == '3' || msg[2] == '1'))
302                 return SM_ERROR;
303
304         return SM_ERROR;
305 }
306
307 static gint smtp_starttls(SMTPSession *session)
308 {
309         session->state = SMTP_STARTTLS;
310
311         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "STARTTLS");
312         log_print("ESMTP> STARTTLS\n");
313
314         return SM_OK;
315 }
316
317 static gint smtp_auth_cram_md5(SMTPSession *session)
318 {
319         session->state = SMTP_AUTH;
320         session->auth_type = SMTPAUTH_CRAM_MD5;
321
322         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH CRAM-MD5");
323         log_print("ESMTP> AUTH CRAM-MD5\n");
324
325         return SM_OK;
326 }
327
328 static gint smtp_auth_login(SMTPSession *session)
329 {
330         session->state = SMTP_AUTH;
331         session->auth_type = SMTPAUTH_LOGIN;
332
333         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "AUTH LOGIN");
334         log_print("ESMTP> AUTH LOGIN\n");
335
336         return SM_OK;
337 }
338
339 static gint smtp_helo(SMTPSession *session)
340 {
341         gchar buf[MSGBUFSIZE];
342
343         session->state = SMTP_HELO;
344
345         g_snprintf(buf, sizeof(buf), "HELO %s",
346                    session->hostname ? session->hostname : get_domain_name());
347         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
348         log_print("SMTP> %s\n", buf);
349
350         return SM_OK;
351 }
352
353 static gint smtp_rcpt(SMTPSession *session)
354 {
355         gchar buf[MSGBUFSIZE];
356         gchar *to;
357
358         g_return_val_if_fail(session->cur_to != NULL, SM_ERROR);
359
360         session->state = SMTP_RCPT;
361
362         to = (gchar *)session->cur_to->data;
363
364         if (strchr(to, '<'))
365                 g_snprintf(buf, sizeof(buf), "RCPT TO:%s", to);
366         else
367                 g_snprintf(buf, sizeof(buf), "RCPT TO:<%s>", to);
368         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, buf);
369         log_print("SMTP> %s\n", buf);
370
371         session->cur_to = session->cur_to->next;
372
373         return SM_OK;
374 }
375
376 static gint smtp_data(SMTPSession *session)
377 {
378         session->state = SMTP_DATA;
379
380         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "DATA");
381         log_print("SMTP> DATA\n");
382
383         return SM_OK;
384 }
385
386 static gint smtp_send_data(SMTPSession *session)
387 {
388         session->state = SMTP_SEND_DATA;
389
390         session_send_data(SESSION(session), session->send_data,
391                           session->send_data_len);
392
393         return SM_OK;
394 }
395
396 #if 0
397 static gint smtp_rset(SMTPSession *session)
398 {
399         session->state = SMTP_RSET;
400
401         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "RSET");
402         log_print("SMTP> RSET\n");
403
404         return SM_OK;
405 }
406 #endif
407
408 static gint smtp_quit(SMTPSession *session)
409 {
410         session->state = SMTP_QUIT;
411
412         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, "QUIT");
413         log_print("SMTP> QUIT\n");
414
415         return SM_OK;
416 }
417
418 static gint smtp_eom(SMTPSession *session)
419 {
420         session->state = SMTP_EOM;
421
422         session_send_msg(SESSION(session), SESSION_MSG_NORMAL, ".");
423         log_print("SMTP> . (EOM)\n");
424
425         return SM_OK;
426 }
427
428 static gint smtp_session_recv_msg(Session *session, const gchar *msg)
429 {
430         SMTPSession *smtp_session = SMTP_SESSION(session);
431         gboolean cont = FALSE;
432
433         if (strlen(msg) < 4) {
434                 log_warning(_("bad SMTP response\n"));
435                 return -1;
436         }
437
438         switch (smtp_session->state) {
439         case SMTP_EHLO:
440         case SMTP_STARTTLS:
441         case SMTP_AUTH:
442         case SMTP_AUTH_LOGIN_USER:
443         case SMTP_AUTH_LOGIN_PASS:
444         case SMTP_AUTH_CRAM_MD5:
445                 log_print("ESMTP< %s\n", msg);
446                 break;
447         default:
448                 log_print("SMTP< %s\n", msg);
449                 break;
450         }
451
452         if (msg[0] == '5' && msg[1] == '0' &&
453             (msg[2] == '4' || msg[2] == '3' || msg[2] == '1')) {
454                 log_warning(_("error occurred on SMTP session\n"));
455                 smtp_session->state = SMTP_ERROR;
456                 smtp_session->error_val = SM_ERROR;
457                 g_free(smtp_session->error_msg);
458                 smtp_session->error_msg = g_strdup(msg);
459                 return -1;
460         }
461
462         if (!strncmp(msg, "535", 3)) {
463                 log_warning(_("error occurred on authentication\n"));
464                 smtp_session->state = SMTP_ERROR;
465                 smtp_session->error_val = SM_AUTHFAIL;
466                 g_free(smtp_session->error_msg);
467                 smtp_session->error_msg = g_strdup(msg);
468                 return -1;
469         }
470
471         if (msg[0] != '1' && msg[0] != '2' && msg[0] != '3') {
472                 log_warning(_("error occurred on SMTP session\n"));
473                 smtp_session->state = SMTP_ERROR;
474                 smtp_session->error_val = SM_ERROR;
475                 g_free(smtp_session->error_msg);
476                 smtp_session->error_msg = g_strdup(msg);
477                 return -1;
478         }
479
480         if (msg[3] == '-')
481                 cont = TRUE;
482         else if (msg[3] != ' ' && msg[3] != '\0') {
483                 log_warning(_("bad SMTP response\n"));
484                 smtp_session->state = SMTP_ERROR;
485                 smtp_session->error_val = SM_UNRECOVERABLE;
486                 return -1;
487         }
488
489         /* ignore all multiline responses except for EHLO */
490         if (cont && smtp_session->state != SMTP_EHLO)
491                 return session_recv_msg(session);
492
493         switch (smtp_session->state) {
494         case SMTP_READY:
495                 if (strstr(msg, "ESMTP"))
496                         smtp_session->is_esmtp = TRUE;
497         case SMTP_CONNECTED:
498 #if USE_OPENSSL
499                 if (smtp_session->user || session->ssl_type != SSL_NONE ||
500                     smtp_session->is_esmtp)
501 #else
502                 if (smtp_session->user || smtp_session->is_esmtp)
503 #endif
504                         smtp_ehlo(smtp_session);
505                 else
506                         smtp_helo(smtp_session);
507                 break;
508         case SMTP_HELO:
509                 smtp_from(smtp_session);
510                 break;
511         case SMTP_EHLO:
512                 smtp_ehlo_recv(smtp_session, msg);
513                 if (cont == TRUE)
514                         break;
515                 if (smtp_session->max_message_size > 0
516                 && smtp_session->max_message_size < 
517                    smtp_session->send_data_len) {
518                         log_warning(_("Message is too big "
519                               "(Maximum size is %s)\n"),
520                               to_human_readable(
521                                (off_t)(smtp_session->max_message_size)));
522                         smtp_session->state = SMTP_ERROR;
523                         smtp_session->error_val = SM_ERROR;
524                         return -1;
525                 }
526 #if USE_OPENSSL
527                 if (session->ssl_type == SSL_STARTTLS &&
528                     smtp_session->tls_init_done == FALSE) {
529                         smtp_starttls(smtp_session);
530                         break;
531                 }
532 #endif
533                 if (smtp_session->user) {
534                         if (smtp_auth(smtp_session) != SM_OK)
535                                 smtp_from(smtp_session);
536                 } else
537                         smtp_from(smtp_session);
538                 break;
539         case SMTP_STARTTLS:
540 #if USE_OPENSSL
541                 if (session_start_tls(session) < 0) {
542                         log_warning(_("can't start TLS session\n"));
543                         smtp_session->state = SMTP_ERROR;
544                         smtp_session->error_val = SM_ERROR;
545                         return -1;
546                 }
547                 smtp_session->tls_init_done = TRUE;
548                 smtp_ehlo(smtp_session);
549 #endif
550                 break;
551         case SMTP_AUTH:
552                 smtp_auth_recv(smtp_session, msg);
553                 break;
554         case SMTP_AUTH_LOGIN_USER:
555                 smtp_auth_login_user_recv(smtp_session, msg);
556                 break;
557         case SMTP_AUTH_LOGIN_PASS:
558         case SMTP_AUTH_CRAM_MD5:
559                 smtp_from(smtp_session);
560                 break;
561         case SMTP_FROM:
562                 if (smtp_session->cur_to)
563                         smtp_rcpt(smtp_session);
564                 break;
565         case SMTP_RCPT:
566                 if (smtp_session->cur_to)
567                         smtp_rcpt(smtp_session);
568                 else
569                         smtp_data(smtp_session);
570                 break;
571         case SMTP_DATA:
572                 smtp_send_data(smtp_session);
573                 break;
574         case SMTP_EOM:
575                 smtp_quit(smtp_session);
576                 break;
577         case SMTP_QUIT:
578                 session_disconnect(session);
579                 break;
580         case SMTP_ERROR:
581         default:
582                 log_warning(_("error occurred on SMTP session\n"));
583                 smtp_session->error_val = SM_ERROR;
584                 return -1;
585         }
586
587         if (cont)
588                 return session_recv_msg(session);
589
590         return 0;
591 }
592
593 static gint smtp_session_send_data_finished(Session *session, guint len)
594 {
595         smtp_eom(SMTP_SESSION(session));
596         return 0;
597 }