1 /* mutt - text oriented MIME mail user agent
2 * Copyright (C) 2002 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 2005-9 Brendan Cully <brendan@kublai.com>
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.
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.
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, USA.
20 /* This file contains code for direct SMTP delivery of email messages. */
27 #include "mutt_curses.h"
28 #include "mutt_socket.h"
30 # include "mutt_ssl.h"
33 #include "mutt_sasl.h"
35 #include <sasl/sasl.h>
36 #include <sasl/saslutil.h>
40 #include <netinet/in.h>
44 #define smtp_success(x) ((x)/100 == 2)
45 #define smtp_ready 334
46 #define smtp_continue 354
48 #define smtp_err_read -2
49 #define smtp_err_write -3
50 #define smtp_err_code -4
53 #define SMTPS_PORT 465
55 #define SMTP_AUTH_SUCCESS 0
56 #define SMTP_AUTH_UNAVAIL 1
57 #define SMTP_AUTH_FAIL -1
69 static int smtp_auth (CONNECTION* conn);
70 static int smtp_auth_sasl (CONNECTION* conn, const char* mechanisms);
73 static int smtp_fill_account (ACCOUNT* account);
74 static int smtp_open (CONNECTION* conn);
77 static char* AuthMechs = NULL;
78 static unsigned char Capabilities[(CAPMAX + 7)/ 8];
80 static int smtp_code (char *buf, size_t len, int *n)
90 if (mutt_atoi (code, n) < 0)
95 /* Reads a command response from the SMTP server.
97 * 0 on success (2xx code) or continue (354 code)
98 * -1 write error, or any other response code
101 smtp_get_resp (CONNECTION * conn)
107 n = mutt_socket_readln (buf, sizeof (buf), conn);
109 /* read error, or no response code */
110 return smtp_err_read;
113 if (!ascii_strncasecmp ("8BITMIME", buf + 4, 8))
114 mutt_bit_set (Capabilities, EIGHTBITMIME);
115 else if (!ascii_strncasecmp ("AUTH ", buf + 4, 5))
117 mutt_bit_set (Capabilities, AUTH);
119 AuthMechs = safe_strdup (buf + 9);
121 else if (!ascii_strncasecmp ("DSN", buf + 4, 3))
122 mutt_bit_set (Capabilities, DSN);
123 else if (!ascii_strncasecmp ("STARTTLS", buf + 4, 8))
124 mutt_bit_set (Capabilities, STARTTLS);
126 if (smtp_code (buf, n, &n) < 0)
127 return smtp_err_code;
129 } while (buf[3] == '-');
131 if (smtp_success (n) || n == smtp_continue)
134 mutt_error (_("SMTP session failed: %s"), buf);
139 smtp_rcpt_to (CONNECTION * conn, const ADDRESS * a)
146 /* weed out group mailboxes, since those are for display only */
147 if (!a->mailbox || a->group)
152 if (mutt_bit_isset (Capabilities, DSN) && DsnNotify)
153 snprintf (buf, sizeof (buf), "RCPT TO:<%s> NOTIFY=%s\r\n",
154 a->mailbox, DsnNotify);
156 snprintf (buf, sizeof (buf), "RCPT TO:<%s>\r\n", a->mailbox);
157 if (mutt_socket_write (conn, buf) == -1)
158 return smtp_err_write;
159 if ((r = smtp_get_resp (conn)))
168 smtp_data (CONNECTION * conn, const char *msgfile)
177 fp = fopen (msgfile, "r");
180 mutt_error (_("SMTP session failed: unable to open %s"), msgfile);
185 mutt_progress_init (&progress, _("Sending message..."), M_PROGRESS_SIZE,
188 snprintf (buf, sizeof (buf), "DATA\r\n");
189 if (mutt_socket_write (conn, buf) == -1)
192 return smtp_err_write;
194 if ((r = smtp_get_resp (conn)))
200 while (fgets (buf, sizeof (buf) - 1, fp))
202 buflen = mutt_strlen (buf);
203 term = buf[buflen-1] == '\n';
204 if (buflen && buf[buflen-1] == '\n'
205 && (buflen == 1 || buf[buflen - 2] != '\r'))
206 snprintf (buf + buflen - 1, sizeof (buf) - buflen + 1, "\r\n");
209 if (mutt_socket_write_d (conn, ".", -1, M_SOCK_LOG_FULL) == -1)
212 return smtp_err_write;
215 if (mutt_socket_write_d (conn, buf, -1, M_SOCK_LOG_FULL) == -1)
218 return smtp_err_write;
220 mutt_progress_update (&progress, ftell (fp), -1);
222 if (!term && buflen &&
223 mutt_socket_write_d (conn, "\r\n", -1, M_SOCK_LOG_FULL) == -1)
226 return smtp_err_write;
230 /* terminate the message body */
231 if (mutt_socket_write (conn, ".\r\n") == -1)
232 return smtp_err_write;
234 if ((r = smtp_get_resp (conn)))
241 mutt_smtp_send (const ADDRESS* from, const ADDRESS* to, const ADDRESS* cc,
242 const ADDRESS* bcc, const char *msgfile, int eightbit)
250 /* it might be better to synthesize an envelope from from user and host
251 * but this condition is most likely arrived at accidentally */
253 envfrom = EnvFrom->mailbox;
255 envfrom = from->mailbox;
258 mutt_error (_("No from address given"));
262 if (smtp_fill_account (&account) < 0)
265 if (!(conn = mutt_conn_find (NULL, &account)))
272 /* send our greeting */
273 if (( ret = smtp_open (conn)))
277 /* send the sender's address */
278 ret = snprintf (buf, sizeof (buf), "MAIL FROM:<%s>", envfrom);
279 if (eightbit && mutt_bit_isset (Capabilities, EIGHTBITMIME))
281 safe_strncat (buf, sizeof (buf), " BODY=8BITMIME", 15);
284 if (DsnReturn && mutt_bit_isset (Capabilities, DSN))
285 ret += snprintf (buf + ret, sizeof (buf) - ret, " RET=%s", DsnReturn);
286 safe_strncat (buf, sizeof (buf), "\r\n", 3);
287 if (mutt_socket_write (conn, buf) == -1)
289 ret = smtp_err_write;
292 if ((ret = smtp_get_resp (conn)))
295 /* send the recipient list */
296 if ((ret = smtp_rcpt_to (conn, to)) || (ret = smtp_rcpt_to (conn, cc))
297 || (ret = smtp_rcpt_to (conn, bcc)))
300 /* send the message data */
301 if ((ret = smtp_data (conn, msgfile)))
304 mutt_socket_write (conn, "QUIT\r\n");
311 mutt_socket_close (conn);
313 if (ret == smtp_err_read)
314 mutt_error (_("SMTP session failed: read error"));
315 else if (ret == smtp_err_write)
316 mutt_error (_("SMTP session failed: write error"));
317 else if (ret == smtp_err_code)
318 mutt_error (_("Invalid server response"));
323 static int smtp_fill_account (ACCOUNT* account)
325 static unsigned short SmtpPort = 0;
327 struct servent* service;
333 account->type = M_ACCT_TYPE_SMTP;
335 urlstr = safe_strdup (SmtpUrl);
336 url_parse_ciss (&url, urlstr);
337 if ((url.scheme != U_SMTP && url.scheme != U_SMTPS)
338 || mutt_account_fromurl (account, &url) < 0)
341 mutt_error (_("Invalid SMTP URL: %s"), SmtpUrl);
347 if (url.scheme == U_SMTPS)
348 account->flags |= M_ACCT_SSL;
352 if (account->flags & M_ACCT_SSL)
353 account->port = SMTPS_PORT;
358 service = getservbyname ("smtp", "tcp");
360 SmtpPort = ntohs (service->s_port);
362 SmtpPort = SMTP_PORT;
363 dprint (3, (debugfile, "Using default SMTP port %d\n", SmtpPort));
365 account->port = SmtpPort;
372 static int smtp_helo (CONNECTION* conn)
374 char buf[LONG_STRING];
377 memset (Capabilities, 0, sizeof (Capabilities));
381 /* if TLS or AUTH are requested, use EHLO */
382 if (conn->account.flags & M_ACCT_USER)
385 if (option (OPTSSLFORCETLS) || quadoption (OPT_SSLSTARTTLS) != M_NO)
390 if(!(fqdn = mutt_fqdn (0)))
391 fqdn = NONULL (Hostname);
393 snprintf (buf, sizeof (buf), "%s %s\r\n", Esmtp ? "EHLO" : "HELO", fqdn);
394 /* XXX there should probably be a wrapper in mutt_socket.c that
395 * repeatedly calls conn->write until all data is sent. This
396 * currently doesn't check for a short write.
398 if (mutt_socket_write (conn, buf) == -1)
399 return smtp_err_write;
400 return smtp_get_resp (conn);
403 static int smtp_open (CONNECTION* conn)
407 if (mutt_socket_open (conn))
410 /* get greeting string */
411 if ((rc = smtp_get_resp (conn)))
414 if ((rc = smtp_helo (conn)))
420 else if (option (OPTSSLFORCETLS))
422 else if (mutt_bit_isset (Capabilities, STARTTLS) &&
423 (rc = query_quadoption (OPT_SSLSTARTTLS,
424 _("Secure connection with TLS?"))) == -1)
429 if (mutt_socket_write (conn, "STARTTLS\r\n") < 0)
430 return smtp_err_write;
431 if ((rc = smtp_get_resp (conn)))
434 if (mutt_ssl_starttls (conn))
436 mutt_error (_("Could not negotiate TLS connection"));
441 /* re-EHLO to get authentication mechanisms */
442 if ((rc = smtp_helo (conn)))
447 if (conn->account.flags & M_ACCT_USER)
449 if (!mutt_bit_isset (Capabilities, AUTH))
451 mutt_error (_("SMTP server does not support authentication"));
457 if (!(conn->account.flags & M_ACCT_PASS) && option (OPTNOCURSES))
459 mutt_error (_("Interactive SMTP authentication not supported"));
463 return smtp_auth (conn);
465 mutt_error (_("SMTP authentication requires SASL"));
468 #endif /* USE_SASL */
475 static int smtp_auth (CONNECTION* conn)
477 int r = SMTP_AUTH_UNAVAIL;
479 if (SmtpAuthenticators && *SmtpAuthenticators)
481 char* methods = safe_strdup (SmtpAuthenticators);
485 for (method = methods; method; method = delim)
487 delim = strchr (method, ':');
493 dprint (2, (debugfile, "smtp_authenticate: Trying method %s\n", method));
495 r = smtp_auth_sasl (conn, method);
497 if (r == SMTP_AUTH_FAIL && delim)
499 mutt_error (_("%s authentication failed, trying next method"), method);
502 else if (r != SMTP_AUTH_UNAVAIL)
509 r = smtp_auth_sasl (conn, AuthMechs);
511 if (r != SMTP_AUTH_SUCCESS)
512 mutt_account_unsetpass (&conn->account);
514 if (r == SMTP_AUTH_FAIL)
516 mutt_error (_("SASL authentication failed"));
519 else if (r == SMTP_AUTH_UNAVAIL)
521 mutt_error (_("No authenticators available"));
525 return r == SMTP_AUTH_SUCCESS ? 0 : -1;
528 static int smtp_auth_sasl (CONNECTION* conn, const char* mechlist)
530 sasl_conn_t* saslconn;
531 sasl_interact_t* interaction = NULL;
533 const char* data = NULL;
535 char buf[HUGE_STRING];
538 if (mutt_sasl_client_new (conn, &saslconn) < 0)
539 return SMTP_AUTH_FAIL;
543 rc = sasl_client_start (saslconn, mechlist, &interaction, &data, &len, &mech);
544 if (rc == SASL_INTERACT)
545 mutt_sasl_interact (interaction);
547 while (rc == SASL_INTERACT);
549 if (rc != SASL_OK && rc != SASL_CONTINUE)
551 dprint (2, (debugfile, "smtp_auth_sasl: %s unavailable\n", mech));
552 sasl_dispose (&saslconn);
553 return SMTP_AUTH_UNAVAIL;
556 if (!option(OPTNOCURSES))
557 mutt_message (_("Authenticating (%s)..."), mech);
559 snprintf (buf, sizeof (buf), "AUTH %s", mech);
562 safe_strcat (buf, sizeof (buf), " ");
563 if (sasl_encode64 (data, len, buf + mutt_strlen (buf),
564 sizeof (buf) - mutt_strlen (buf), &len) != SASL_OK)
566 dprint (1, (debugfile, "smtp_auth_sasl: error base64-encoding client response.\n"));
570 safe_strcat (buf, sizeof (buf), "\r\n");
573 if (mutt_socket_write (conn, buf) < 0)
575 if ((rc = mutt_socket_readln (buf, sizeof (buf), conn)) < 0)
577 if (smtp_code (buf, rc, &rc) < 0)
580 if (rc != smtp_ready)
583 if (sasl_decode64 (buf+4, strlen (buf+4), buf, sizeof (buf), &len) != SASL_OK)
585 dprint (1, (debugfile, "smtp_auth_sasl: error base64-decoding server response.\n"));
591 saslrc = sasl_client_step (saslconn, buf, len, &interaction, &data, &len);
592 if (saslrc == SASL_INTERACT)
593 mutt_sasl_interact (interaction);
595 while (saslrc == SASL_INTERACT);
599 if (sasl_encode64 (data, len, buf, sizeof (buf), &len) != SASL_OK)
601 dprint (1, (debugfile, "smtp_auth_sasl: error base64-encoding client response.\n"));
605 strfcpy (buf + len, "\r\n", sizeof (buf) - len);
606 } while (rc == smtp_ready);
608 if (smtp_success (rc))
610 mutt_sasl_setup_conn (conn, saslconn);
611 return SMTP_AUTH_SUCCESS;
615 sasl_dispose (&saslconn);
616 return SMTP_AUTH_FAIL;
618 #endif /* USE_SASL */