- apply and adapt Bram's patch which allows user to set SMTP AUTH
[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                           SMTPAuthType enable_auth_type)
48 #else
49 Session *smtp_session_new(const gchar *server, gushort port,
50                           const gchar *domain,
51                           const gchar *user, const gchar *pass,
52                           SMTPAuthType enable_auth_type)
53 #endif
54 {
55         SMTPSession *session;
56         SockInfo *sock;
57         gboolean use_esmtp;
58         SMTPAuthType avail_auth_type = 0;
59         gint val;
60
61         g_return_val_if_fail(server != NULL, NULL);
62
63 #if USE_SSL
64         use_esmtp = user != NULL || ssl_type == SSL_STARTTLS;
65 #else
66         use_esmtp = user != NULL;
67 #endif
68
69         if ((sock = sock_connect(server, port)) == NULL) {
70                 log_warning(_("Can't connect to SMTP server: %s:%d\n"),
71                             server, port);
72                 return NULL;
73         }
74
75 #if USE_SSL
76         if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
77                 log_warning(_("SSL connection failed"));
78                 sock_close(sock);
79                 return NULL;
80         }
81 #endif
82
83         if (smtp_ok(sock, NULL, 0) != SM_OK) {
84                 log_warning(_("Error occurred while connecting to %s:%d\n"),
85                             server, port);
86                 sock_close(sock);
87                 return NULL;
88         }
89
90         if (!domain)
91                 domain = get_domain_name();
92
93         if (use_esmtp)
94                 val = smtp_ehlo(sock, domain, &avail_auth_type, enable_auth_type);
95         else
96                 val = smtp_helo(sock, domain);
97         if (val != SM_OK) {
98                 log_warning(_("Error occurred while sending HELO\n"));
99                 sock_close(sock);
100                 return NULL;
101         }
102
103 #if USE_SSL
104         if (ssl_type == SSL_STARTTLS) {
105                 val = smtp_starttls(sock);
106                 if (val != SM_OK) {
107                         log_warning(_("Error occurred while sending STARTTLS\n"));
108                         sock_close(sock);
109                         return NULL;
110                 }
111                 if (!ssl_init_socket_with_method(sock, SSL_METHOD_TLSv1)) {
112                         sock_close(sock);
113                         return NULL;
114                 }
115                 val = smtp_ehlo(sock, domain, &avail_auth_type, enable_auth_type);
116                 if (val != SM_OK) {
117                         log_warning(_("Error occurred while sending EHLO\n"));
118                         sock_close(sock);
119                         return NULL;
120                 }
121         }
122 #endif
123
124         session = g_new(SMTPSession, 1);
125         SESSION(session)->type             = SESSION_SMTP;
126         SESSION(session)->server           = g_strdup(server);
127         SESSION(session)->sock             = sock;
128         SESSION(session)->connected        = TRUE;
129         SESSION(session)->phase            = SESSION_READY;
130         SESSION(session)->last_access_time = 0;
131         SESSION(session)->data             = NULL;
132         session->avail_auth_type           = avail_auth_type;
133         session->user                      = user ? g_strdup(user) : NULL;
134         session->pass                      = pass ? g_strdup(pass) :
135                                              user ? g_strdup("") : NULL;
136
137         return SESSION(session);
138 }
139
140 void smtp_session_destroy(SMTPSession *session)
141 {
142         sock_close(SESSION(session)->sock);
143         SESSION(session)->sock = NULL;
144
145         g_free(session->user);
146         g_free(session->pass);
147 }
148
149 gint smtp_from(SMTPSession *session, const gchar *from)
150 {
151         gchar buf[MSGBUFSIZE];
152
153         g_return_val_if_fail(session != NULL, SM_ERROR);
154         g_return_val_if_fail(from != NULL, SM_ERROR);
155
156         if (session->user) {
157                 if (smtp_auth(session) != SM_OK)
158                         return SM_AUTHFAIL;
159         }
160
161         if (strchr(from, '<'))
162                 g_snprintf(buf, sizeof(buf), "MAIL FROM: %s", from);
163         else
164                 g_snprintf(buf, sizeof(buf), "MAIL FROM: <%s>", from);
165
166         sock_printf(SESSION(session)->sock, "%s\r\n", buf);
167         if (verbose)
168                 log_print("SMTP> %s\n", buf);
169
170         return smtp_ok(SESSION(session)->sock, NULL, 0);
171 }
172
173 gint smtp_auth(SMTPSession *session)
174 {
175         gchar buf[MSGBUFSIZE];
176         SMTPAuthType authtype = 0;
177         guchar hexdigest[33];
178         gchar *challenge, *response, *response64;
179         gint challengelen;
180         SockInfo *sock;
181
182         g_return_val_if_fail(session != NULL, SM_ERROR);
183         g_return_val_if_fail(session->user != NULL, SM_ERROR);
184
185         sock = SESSION(session)->sock;
186
187         if ((session->avail_auth_type & SMTPAUTH_CRAM_MD5) != 0 &&
188             smtp_auth_cram_md5(sock, buf, sizeof(buf)) == SM_OK)
189                 authtype = SMTPAUTH_CRAM_MD5;
190         else if ((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                         to64frombits(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                         to64frombits(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 = from64tobits(challenge, buf + 4);
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                         to64frombits(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                SMTPAuthType  enable_auth_type)
268 {
269         gchar buf[MSGBUFSIZE];
270
271         *avail_auth_type = 0;
272
273         sock_printf(sock, "EHLO %s\r\n", hostname);
274         if (verbose)
275                 log_print("ESMTP> EHLO %s\n", hostname);
276
277         while ((sock_gets(sock, buf, sizeof(buf) - 1)) != -1) {
278                 if (strlen(buf) < 4)
279                         return SM_ERROR;
280                 strretchomp(buf);
281
282                 if (verbose)
283                         log_print("ESMTP< %s\n", buf);
284
285                 if (strncmp(buf, "250-", 4) == 0) {
286                         gchar *p = buf;
287                         p += 4;
288                         
289                         if (g_strncasecmp(p, "AUTH ", 5) == 0 ||
290                            (g_strncasecmp(p, "AUTH=", 5) == 0)) {       /* CLAWS: qmail */
291                                 p += 5;
292                                 if (strcasestr(p, "LOGIN") && (enable_auth_type & SMTPAUTH_LOGIN))
293                                         *avail_auth_type |= SMTPAUTH_LOGIN;
294                                 if (strcasestr(p, "CRAM-MD5") && (enable_auth_type & SMTPAUTH_CRAM_MD5))
295                                         *avail_auth_type |= SMTPAUTH_CRAM_MD5;
296                                 if (strcasestr(p, "DIGEST-MD5") && (enable_auth_type & SMTPAUTH_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 }