sync with 0.8.6cvs3
[claws.git] / src / smtp.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 "socket.h"
31 #include "md5.h"
32 #include "base64.h"
33 #include "utils.h"
34
35 static gint verbose = 1;
36
37 static gint smtp_starttls(SockInfo *sock);
38 static gint smtp_auth_cram_md5(SockInfo *sock, gchar *buf, gint len);
39 static gint smtp_auth_login(SockInfo *sock, gchar *buf, gint len);
40 static gint smtp_ok(SockInfo *sock, gchar *buf, gint len);
41
42 #if USE_SSL
43 Session *smtp_session_new(const gchar *server, gushort port,
44                           const gchar *domain,
45                           const gchar *user, const gchar *pass,
46                           SSLType ssl_type)
47 #else
48 Session *smtp_session_new(const gchar *server, gushort port,
49                           const gchar *domain,
50                           const gchar *user, const gchar *pass)
51 #endif
52 {
53         SMTPSession *session;
54         SockInfo *sock;
55         gboolean use_esmtp;
56         SMTPAuthType avail_auth_type = 0;
57         gint val;
58
59         g_return_val_if_fail(server != NULL, NULL);
60
61 #if USE_SSL
62         use_esmtp = user != NULL || ssl_type == SSL_STARTTLS;
63 #else
64         use_esmtp = user != NULL;
65 #endif
66
67         if ((sock = sock_connect(server, port)) == NULL) {
68                 log_warning(_("Can't connect to SMTP server: %s:%d\n"),
69                             server, port);
70                 return NULL;
71         }
72
73 #if USE_SSL
74         if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
75                 log_warning(_("SSL connection failed"));
76                 sock_close(sock);
77                 return NULL;
78         }
79 #endif
80
81         if (smtp_ok(sock, NULL, 0) != SM_OK) {
82                 log_warning(_("Error occurred while connecting to %s:%d\n"),
83                             server, port);
84                 sock_close(sock);
85                 return NULL;
86         }
87
88         if (!domain)
89                 domain = get_domain_name();
90
91         if (use_esmtp)
92                 val = smtp_ehlo(sock, domain, &avail_auth_type);
93         else
94                 val = smtp_helo(sock, domain);
95         if (val != SM_OK) {
96                 log_warning(_("Error occurred while sending HELO\n"));
97                 sock_close(sock);
98                 return NULL;
99         }
100
101 #if USE_SSL
102         if (ssl_type == SSL_STARTTLS) {
103                 val = smtp_starttls(sock);
104                 if (val != SM_OK) {
105                         log_warning(_("Error occurred while sending STARTTLS\n"));
106                         sock_close(sock);
107                         return NULL;
108                 }
109                 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1)) {
110                         sock_close(sock);
111                         return NULL;
112                 }
113                 val = smtp_ehlo(sock, domain, &avail_auth_type);
114                 if (val != SM_OK) {
115                         log_warning(_("Error occurred while sending EHLO\n"));
116                         sock_close(sock);
117                         return NULL;
118                 }
119         }
120 #endif
121
122         session = g_new(SMTPSession, 1);
123         SESSION(session)->type             = SESSION_SMTP;
124         SESSION(session)->server           = g_strdup(server);
125         SESSION(session)->sock             = sock;
126         SESSION(session)->connected        = TRUE;
127         SESSION(session)->phase            = SESSION_READY;
128         SESSION(session)->last_access_time = 0;
129         SESSION(session)->data             = NULL;
130
131         SESSION(session)->destroy          = smtp_session_destroy;
132
133         session->avail_auth_type           = avail_auth_type;
134         session->user                      = user ? g_strdup(user) : NULL;
135         session->pass                      = pass ? g_strdup(pass) :
136                                              user ? g_strdup("") : NULL;
137
138         return SESSION(session);
139 }
140
141 void smtp_session_destroy(Session *session)
142 {
143         sock_close(session->sock);
144         session->sock = NULL;
145
146         g_free(SMTP_SESSION(session)->user);
147         g_free(SMTP_SESSION(session)->pass);
148 }
149
150 gint smtp_from(SMTPSession *session, const gchar *from)
151 {
152         gchar buf[MSGBUFSIZE];
153
154         g_return_val_if_fail(session != NULL, SM_ERROR);
155         g_return_val_if_fail(from != NULL, SM_ERROR);
156
157         if (strchr(from, '<'))
158                 g_snprintf(buf, sizeof(buf), "MAIL FROM: %s", from);
159         else
160                 g_snprintf(buf, sizeof(buf), "MAIL FROM: <%s>", from);
161
162         sock_printf(SESSION(session)->sock, "%s\r\n", buf);
163         if (verbose)
164                 log_print("SMTP> %s\n", buf);
165
166         return smtp_ok(SESSION(session)->sock, NULL, 0);
167 }
168
169 gint smtp_auth(SMTPSession *session, SMTPAuthType forced_auth_type)
170 {
171         gchar buf[MSGBUFSIZE];
172         SMTPAuthType authtype = 0;
173         guchar hexdigest[33];
174         gchar *challenge, *response, *response64;
175         gint challengelen;
176         SockInfo *sock;
177
178         g_return_val_if_fail(session != NULL, SM_ERROR);
179         g_return_val_if_fail(session->user != NULL, SM_ERROR);
180
181         sock = SESSION(session)->sock;
182
183         if ((forced_auth_type == SMTPAUTH_CRAM_MD5 ||
184              (forced_auth_type == 0 &&
185               (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0)) &&
186             smtp_auth_cram_md5(sock, buf, sizeof(buf)) == SM_OK)
187                 authtype = SMTPAUTH_CRAM_MD5;
188         else if ((forced_auth_type == SMTPAUTH_LOGIN ||
189                   (forced_auth_type == 0 &&
190                    (session->avail_auth_type & SMTPAUTH_LOGIN) != 0)) &&
191                  smtp_auth_login(sock, buf, sizeof(buf)) == SM_OK)
192                 authtype = SMTPAUTH_LOGIN;
193         else {
194                 log_warning(_("SMTP AUTH not available\n"));
195                 return SM_AUTHFAIL;
196         }
197
198         switch (authtype) {
199         case SMTPAUTH_LOGIN:
200                 if (!strncmp(buf, "334 ", 4))
201                         base64_encode(buf, session->user, strlen(session->user));
202                 else
203                         /* Server rejects AUTH */
204                         g_snprintf(buf, sizeof(buf), "*");
205
206                 sock_printf(sock, "%s\r\n", buf);
207                 if (verbose) log_print("ESMTP> [USERID]\n");
208
209                 smtp_ok(sock, buf, sizeof(buf));
210
211                 if (!strncmp(buf, "334 ", 4))
212                         base64_encode(buf, session->pass, strlen(session->pass));
213                 else
214                         /* Server rejects AUTH */
215                         g_snprintf(buf, sizeof(buf), "*");
216
217                 sock_printf(sock, "%s\r\n", buf);
218                 if (verbose) log_print("ESMTP> [PASSWORD]\n");
219                 break;
220         case SMTPAUTH_CRAM_MD5:
221                 if (!strncmp(buf, "334 ", 4)) {
222                         challenge = g_malloc(strlen(buf + 4) + 1);
223                         challengelen = base64_decode(challenge, buf + 4, -1);
224                         challenge[challengelen] = '\0';
225                         if (verbose)
226                                 log_print("ESMTP< [Decoded: %s]\n", challenge);
227
228                         g_snprintf(buf, sizeof(buf), "%s", session->pass);
229                         md5_hex_hmac(hexdigest, challenge, challengelen,
230                                      buf, strlen(session->pass));
231                         g_free(challenge);
232
233                         response = g_strdup_printf
234                                 ("%s %s", session->user, hexdigest);
235                         if (verbose)
236                                 log_print("ESMTP> [Encoded: %s]\n", response);
237
238                         response64 = g_malloc((strlen(response) + 3) * 2 + 1);
239                         base64_encode(response64, response, strlen(response));
240                         g_free(response);
241
242                         sock_printf(sock, "%s\r\n", response64);
243                         if (verbose) log_print("ESMTP> %s\n", response64);
244                         g_free(response64);
245                 } else {
246                         /* Server rejects AUTH */
247                         g_snprintf(buf, sizeof(buf), "*");
248                         sock_printf(sock, "%s\r\n", buf);
249                         if (verbose)
250                                 log_print("ESMTP> %s\n", buf);
251                 }
252                 break;
253         case SMTPAUTH_DIGEST_MD5:
254         default:
255                 /* stop smtp_auth when no correct authtype */
256                 g_snprintf(buf, sizeof(buf), "*");
257                 sock_printf(sock, "%s\r\n", buf);
258                 if (verbose) log_print("ESMTP> %s\n", buf);
259                 break;
260         }
261
262         return smtp_ok(sock, NULL, 0);
263 }
264
265 gint smtp_ehlo(SockInfo *sock, const gchar *hostname,
266                SMTPAuthType *avail_auth_type)
267 {
268         gchar buf[MSGBUFSIZE];
269
270         *avail_auth_type = 0;
271
272         sock_printf(sock, "EHLO %s\r\n", hostname);
273         if (verbose)
274                 log_print("ESMTP> EHLO %s\n", hostname);
275
276         while ((sock_gets(sock, buf, sizeof(buf) - 1)) != -1) {
277                 if (strlen(buf) < 4)
278                         return SM_ERROR;
279                 strretchomp(buf);
280
281                 if (verbose)
282                         log_print("ESMTP< %s\n", buf);
283
284                 if (strncmp(buf, "250-", 4) == 0) {
285                         gchar *p = buf;
286                         p += 4;
287                         if (g_strncasecmp(p, "AUTH", 4) == 0) {
288                                 p += 5;
289                                 if (strcasestr(p, "LOGIN"))
290                                         *avail_auth_type |= SMTPAUTH_LOGIN;
291                                 if (strcasestr(p, "CRAM-MD5"))
292                                         *avail_auth_type |= SMTPAUTH_CRAM_MD5;
293                                 if (strcasestr(p, "DIGEST-MD5"))
294                                         *avail_auth_type |= SMTPAUTH_DIGEST_MD5;
295                         }
296                 } else if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
297                     (buf[3] == ' ' || buf[3] == '\0'))
298                         return SM_OK;
299                 else if (buf[3] != '-')
300                         return SM_ERROR;
301                 else if (buf[0] == '5' && buf[1] == '0' &&
302                          (buf[2] == '4' || buf[2] == '3' || buf[2] == '1'))
303                         return SM_ERROR;
304         }
305
306         return SM_UNRECOVERABLE;
307 }
308
309 static gint smtp_starttls(SockInfo *sock)
310 {
311         sock_printf(sock, "STARTTLS\r\n");
312         if (verbose)
313                 log_print("ESMTP> STARTTLS\n");
314
315         return smtp_ok(sock, NULL, 0);
316 }
317
318 static gint smtp_auth_cram_md5(SockInfo *sock, gchar *buf, gint len)
319 {
320         sock_printf(sock, "AUTH CRAM-MD5\r\n");
321         if (verbose)
322                 log_print("ESMTP> AUTH CRAM-MD5\n");
323
324         return smtp_ok(sock, buf, len);
325 }
326
327 static gint smtp_auth_login(SockInfo *sock, gchar *buf, gint len)
328 {
329         sock_printf(sock, "AUTH LOGIN\r\n");
330         if (verbose)
331                 log_print("ESMTP> AUTH LOGIN\n");
332
333         return smtp_ok(sock, buf, len);
334 }
335
336 gint smtp_helo(SockInfo *sock, const gchar *hostname)
337 {
338         sock_printf(sock, "HELO %s\r\n", hostname);
339         if (verbose)
340                 log_print("SMTP> HELO %s\n", hostname);
341
342         return smtp_ok(sock, NULL, 0);
343 }
344
345 gint smtp_rcpt(SockInfo *sock, const gchar *to)
346 {
347         gchar buf[MSGBUFSIZE];
348
349         if (strchr(to, '<'))
350                 g_snprintf(buf, sizeof(buf), "RCPT TO: %s", to);
351         else
352                 g_snprintf(buf, sizeof(buf), "RCPT TO: <%s>", to);
353
354         sock_printf(sock, "%s\r\n", buf);
355         if (verbose)
356                 log_print("SMTP> %s\n", buf);
357
358         return smtp_ok(sock, NULL, 0);
359 }
360
361 gint smtp_data(SockInfo *sock)
362 {
363         sock_printf(sock, "DATA\r\n");
364         if (verbose)
365                 log_print("SMTP> DATA\n");
366
367         return smtp_ok(sock, NULL, 0);
368 }
369
370 gint smtp_rset(SockInfo *sock)
371 {
372         sock_printf(sock, "RSET\r\n");
373         if (verbose)
374                 log_print("SMTP> RSET\n");
375
376         return smtp_ok(sock, NULL, 0);
377 }
378
379 gint smtp_quit(SockInfo *sock)
380 {
381         sock_printf(sock, "QUIT\r\n");
382         if (verbose)
383                 log_print("SMTP> QUIT\n");
384
385         return smtp_ok(sock, NULL, 0);
386 }
387
388 gint smtp_eom(SockInfo *sock)
389 {
390         sock_printf(sock, ".\r\n");
391         if (verbose)
392                 log_print("SMTP> . (EOM)\n");
393
394         return smtp_ok(sock, NULL, 0);
395 }
396
397 static gint smtp_ok(SockInfo *sock, gchar *buf, gint len)
398 {
399         gchar tmpbuf[MSGBUFSIZE];
400
401         if (!buf) {
402                 buf = tmpbuf;
403                 len = sizeof(tmpbuf);
404         }
405
406         while ((sock_gets(sock, buf, len - 1)) != -1) {
407                 if (strlen(buf) < 4)
408                         return SM_ERROR;
409                 strretchomp(buf);
410
411                 if (verbose)
412                         log_print("SMTP< %s\n", buf);
413
414                 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
415                     (buf[3] == ' ' || buf[3] == '\0'))
416                         return SM_OK;
417                 else if (buf[3] != '-')
418                         return SM_ERROR;
419                 else if (buf[0] == '5' && buf[1] == '0' &&
420                          (buf[2] == '4' || buf[2] == '3' || buf[2] == '1'))
421                         return SM_ERROR;
422         }
423
424         return SM_UNRECOVERABLE;
425 }