sync with 0.8.1cvs12
[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         session->avail_auth_type           = avail_auth_type;
131         session->user                      = user ? g_strdup(user) : NULL;
132         session->pass                      = pass ? g_strdup(pass) :
133                                              user ? g_strdup("") : NULL;
134
135         return SESSION(session);
136 }
137
138 void smtp_session_destroy(SMTPSession *session)
139 {
140         sock_close(SESSION(session)->sock);
141         SESSION(session)->sock = NULL;
142
143         g_free(session->user);
144         g_free(session->pass);
145 }
146
147 gint smtp_from(SMTPSession *session, const gchar *from,
148                SMTPAuthType forced_auth_type)
149 {
150         gchar buf[MSGBUFSIZE];
151
152         g_return_val_if_fail(session != NULL, SM_ERROR);
153         g_return_val_if_fail(from != NULL, SM_ERROR);
154
155         if (session->user) {
156                 if (smtp_auth(session, forced_auth_type) != SM_OK)
157                         return SM_AUTHFAIL;
158         }
159
160         if (strchr(from, '<'))
161                 g_snprintf(buf, sizeof(buf), "MAIL FROM: %s", from);
162         else
163                 g_snprintf(buf, sizeof(buf), "MAIL FROM: <%s>", from);
164
165         sock_printf(SESSION(session)->sock, "%s\r\n", buf);
166         if (verbose)
167                 log_print("SMTP> %s\n", buf);
168
169         return smtp_ok(SESSION(session)->sock, NULL, 0);
170 }
171
172 gint smtp_auth(SMTPSession *session, SMTPAuthType forced_auth_type)
173 {
174         gchar buf[MSGBUFSIZE];
175         SMTPAuthType authtype = 0;
176         guchar hexdigest[33];
177         gchar *challenge, *response, *response64;
178         gint challengelen;
179         SockInfo *sock;
180
181         g_return_val_if_fail(session != NULL, SM_ERROR);
182         g_return_val_if_fail(session->user != NULL, SM_ERROR);
183
184         sock = SESSION(session)->sock;
185
186         if ((forced_auth_type == SMTPAUTH_CRAM_MD5 ||
187              (forced_auth_type == 0 &&
188               (session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0)) &&
189             smtp_auth_cram_md5(sock, buf, sizeof(buf)) == SM_OK)
190                 authtype = SMTPAUTH_CRAM_MD5;
191         else if ((forced_auth_type == SMTPAUTH_LOGIN ||
192                   (forced_auth_type == 0 &&
193                    (session->avail_auth_type & SMTPAUTH_LOGIN) != 0)) &&
194                  smtp_auth_login(sock, buf, sizeof(buf)) == SM_OK)
195                 authtype = SMTPAUTH_LOGIN;
196         else {
197                 log_warning(_("SMTP AUTH not available\n"));
198                 return SM_AUTHFAIL;
199         }
200
201         switch (authtype) {
202         case SMTPAUTH_LOGIN:
203                 if (!strncmp(buf, "334 ", 4))
204                         base64_encode(buf, session->user, strlen(session->user));
205                 else
206                         /* Server rejects AUTH */
207                         g_snprintf(buf, sizeof(buf), "*");
208
209                 sock_printf(sock, "%s\r\n", buf);
210                 if (verbose) log_print("ESMTP> [USERID]\n");
211
212                 smtp_ok(sock, buf, sizeof(buf));
213
214                 if (!strncmp(buf, "334 ", 4))
215                         base64_encode(buf, session->pass, strlen(session->pass));
216                 else
217                         /* Server rejects AUTH */
218                         g_snprintf(buf, sizeof(buf), "*");
219
220                 sock_printf(sock, "%s\r\n", buf);
221                 if (verbose) log_print("ESMTP> [PASSWORD]\n");
222                 break;
223         case SMTPAUTH_CRAM_MD5:
224                 if (!strncmp(buf, "334 ", 4)) {
225                         challenge = g_malloc(strlen(buf + 4) + 1);
226                         challengelen = base64_decode(challenge, buf + 4, -1);
227                         challenge[challengelen] = '\0';
228                         if (verbose)
229                                 log_print("ESMTP< [Decoded: %s]\n", challenge);
230
231                         g_snprintf(buf, sizeof(buf), "%s", session->pass);
232                         md5_hex_hmac(hexdigest, challenge, challengelen,
233                                      buf, strlen(session->pass));
234                         g_free(challenge);
235
236                         response = g_strdup_printf
237                                 ("%s %s", session->user, hexdigest);
238                         if (verbose)
239                                 log_print("ESMTP> [Encoded: %s]\n", response);
240
241                         response64 = g_malloc((strlen(response) + 3) * 2 + 1);
242                         base64_encode(response64, response, strlen(response));
243                         g_free(response);
244
245                         sock_printf(sock, "%s\r\n", response64);
246                         if (verbose) log_print("ESMTP> %s\n", response64);
247                         g_free(response64);
248                 } else {
249                         /* Server rejects AUTH */
250                         g_snprintf(buf, sizeof(buf), "*");
251                         sock_printf(sock, "%s\r\n", buf);
252                         if (verbose)
253                                 log_print("ESMTP> %s\n", buf);
254                 }
255                 break;
256         case SMTPAUTH_DIGEST_MD5:
257         default:
258                 /* stop smtp_auth when no correct authtype */
259                 g_snprintf(buf, sizeof(buf), "*");
260                 sock_printf(sock, "%s\r\n", buf);
261                 if (verbose) log_print("ESMTP> %s\n", buf);
262                 break;
263         }
264
265         return smtp_ok(sock, NULL, 0);
266 }
267
268 gint smtp_ehlo(SockInfo *sock, const gchar *hostname,
269                SMTPAuthType *avail_auth_type)
270 {
271         gchar buf[MSGBUFSIZE];
272
273         *avail_auth_type = 0;
274
275         sock_printf(sock, "EHLO %s\r\n", hostname);
276         if (verbose)
277                 log_print("ESMTP> EHLO %s\n", hostname);
278
279         while ((sock_gets(sock, buf, sizeof(buf) - 1)) != -1) {
280                 if (strlen(buf) < 4)
281                         return SM_ERROR;
282                 strretchomp(buf);
283
284                 if (verbose)
285                         log_print("ESMTP< %s\n", buf);
286
287                 if (strncmp(buf, "250-", 4) == 0) {
288                         gchar *p = buf;
289                         p += 4;
290                         if (g_strncasecmp(p, "AUTH", 4) == 0) {
291                                 p += 5;
292                                 if (strcasestr(p, "LOGIN"))
293                                         *avail_auth_type |= SMTPAUTH_LOGIN;
294                                 if (strcasestr(p, "CRAM-MD5"))
295                                         *avail_auth_type |= SMTPAUTH_CRAM_MD5;
296                                 if (strcasestr(p, "DIGEST-MD5"))
297                                         *avail_auth_type |= SMTPAUTH_DIGEST_MD5;
298                         }
299                 } else if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
300                     (buf[3] == ' ' || buf[3] == '\0'))
301                         return SM_OK;
302                 else if (buf[3] != '-')
303                         return SM_ERROR;
304                 else if (buf[0] == '5' && buf[1] == '0' &&
305                          (buf[2] == '4' || buf[2] == '3' || buf[2] == '1'))
306                         return SM_ERROR;
307         }
308
309         return SM_UNRECOVERABLE;
310 }
311
312 static gint smtp_starttls(SockInfo *sock)
313 {
314         sock_printf(sock, "STARTTLS\r\n");
315         if (verbose)
316                 log_print("ESMTP> STARTTLS\n");
317
318         return smtp_ok(sock, NULL, 0);
319 }
320
321 static gint smtp_auth_cram_md5(SockInfo *sock, gchar *buf, gint len)
322 {
323         sock_printf(sock, "AUTH CRAM-MD5\r\n");
324         if (verbose)
325                 log_print("ESMTP> AUTH CRAM-MD5\n");
326
327         return smtp_ok(sock, buf, len);
328 }
329
330 static gint smtp_auth_login(SockInfo *sock, gchar *buf, gint len)
331 {
332         sock_printf(sock, "AUTH LOGIN\r\n");
333         if (verbose)
334                 log_print("ESMTP> AUTH LOGIN\n");
335
336         return smtp_ok(sock, buf, len);
337 }
338
339 gint smtp_helo(SockInfo *sock, const gchar *hostname)
340 {
341         sock_printf(sock, "HELO %s\r\n", hostname);
342         if (verbose)
343                 log_print("SMTP> HELO %s\n", hostname);
344
345         return smtp_ok(sock, NULL, 0);
346 }
347
348 gint smtp_rcpt(SockInfo *sock, const gchar *to)
349 {
350         gchar buf[MSGBUFSIZE];
351
352         if (strchr(to, '<'))
353                 g_snprintf(buf, sizeof(buf), "RCPT TO: %s", to);
354         else
355                 g_snprintf(buf, sizeof(buf), "RCPT TO: <%s>", to);
356
357         sock_printf(sock, "%s\r\n", buf);
358         if (verbose)
359                 log_print("SMTP> %s\n", buf);
360
361         return smtp_ok(sock, NULL, 0);
362 }
363
364 gint smtp_data(SockInfo *sock)
365 {
366         sock_printf(sock, "DATA\r\n");
367         if (verbose)
368                 log_print("SMTP> DATA\n");
369
370         return smtp_ok(sock, NULL, 0);
371 }
372
373 gint smtp_rset(SockInfo *sock)
374 {
375         sock_printf(sock, "RSET\r\n");
376         if (verbose)
377                 log_print("SMTP> RSET\n");
378
379         return smtp_ok(sock, NULL, 0);
380 }
381
382 gint smtp_quit(SockInfo *sock)
383 {
384         sock_printf(sock, "QUIT\r\n");
385         if (verbose)
386                 log_print("SMTP> QUIT\n");
387
388         return smtp_ok(sock, NULL, 0);
389 }
390
391 gint smtp_eom(SockInfo *sock)
392 {
393         sock_printf(sock, ".\r\n");
394         if (verbose)
395                 log_print("SMTP> . (EOM)\n");
396
397         return smtp_ok(sock, NULL, 0);
398 }
399
400 static gint smtp_ok(SockInfo *sock, gchar *buf, gint len)
401 {
402         gchar tmpbuf[MSGBUFSIZE];
403
404         if (!buf) {
405                 buf = tmpbuf;
406                 len = sizeof(tmpbuf);
407         }
408
409         while ((sock_gets(sock, buf, len - 1)) != -1) {
410                 if (strlen(buf) < 4)
411                         return SM_ERROR;
412                 strretchomp(buf);
413
414                 if (verbose)
415                         log_print("SMTP< %s\n", buf);
416
417                 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
418                     (buf[3] == ' ' || buf[3] == '\0'))
419                         return SM_OK;
420                 else if (buf[3] != '-')
421                         return SM_ERROR;
422                 else if (buf[0] == '5' && buf[1] == '0' &&
423                          (buf[2] == '4' || buf[2] == '3' || buf[2] == '1'))
424                         return SM_ERROR;
425         }
426
427         return SM_UNRECOVERABLE;
428 }