2 * Copyright notice from original mutt:
5 * Parts were written/modified by:
6 * Christian Gall <cg@cgall.de>
7 * Rocco Rutte <pdmef@cs.tu-berlin.de>
9 * This file is part of mutt-ng, see http://www.muttng.org/.
10 * It's licensed under the GNU General Public License,
11 * please see the file GPL in the top level source directory.
25 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
26 #include <openssl/ssl.h>
31 #include <auth-client.h>
34 static char authpass[STRING] = "";
41 #define MSGFAIL(msg) \
43 mutt_error("%s", msg); \
46 #define LIBCFAIL(msg) \
48 mutt_error("%s: %s", msg, strerror(errno)); \
51 #define SMTPFAIL(msg) \
53 _mutt_libesmtp_perror(msg); \
56 #define extna(msg) { mutt_error (_("SMTP Extension '%s' not supported by MTA."), \
60 * _mutt_libesmtp_ensure_init
61 * Make sure the libESMTP support in mutt is initialized at some time.
63 static void _mutt_libesmtp_ensure_init ()
65 static int libesmtp_init = 0;
75 * _mutt_libesmtp_perror
76 * Prints 'msg', a colon, and then a string representation of the
77 * libesmtp errno as a mutt error.
79 static void _mutt_libesmtp_perror (const char *msg)
83 mutt_error ("%s: %s", msg,
84 smtp_strerror (smtp_errno (), buf, sizeof (buf)));
88 * _mutt_libesmtp_add_recipients
89 * Adds every address in 'addr' as a recipient to the smtp message
90 * 'message'. Note that this does not mean that they will necessarily
91 * show up in the mail headers (e.g., when bcc'ing). Returns 0 upon
92 * success, -1 upon failure (and prints an error message).
94 * Very similar to sendlib.c::add_args
97 _mutt_libesmtp_add_recipients (smtp_message_t message, ADDRESS * addr)
101 for (; addr; addr = addr->next) {
102 /* weed out group mailboxes, since those are for display only */
103 if (addr->mailbox && !addr->group) {
104 if (!smtp_add_recipient (message, addr->mailbox))
105 SMTPFAIL ("smtp_add_recipient");
114 _mutt_libesmtp_auth_interact (auth_client_request_t request,
115 char **result, int fields, void *arg)
119 for (i = 0; i < fields; i++) {
120 if (request[i].flags & AUTH_USER) {
121 result[i] = SmtpAuthUser;
123 else if (request[i].flags & AUTH_PASS) {
125 result[i] = SmtpAuthPass;
128 if (authpass[0] == '\0') {
131 snprintf (prompt, sizeof (prompt), "%s%s: ", request[i].prompt,
133 flags & AUTH_CLEARTEXT) ? " (not encrypted)" : "");
134 mutt_get_field_unbuffered (prompt, authpass, sizeof (authpass),
137 result[i] = authpass;
147 static const char *_mutt_libesmtp_messagefp_cb (void **buf, int *len,
153 *buf = malloc (BUFLEN);
156 rewind ((FILE *) arg);
160 if (fgets (*buf, BUFLEN - 2, (FILE *) arg) == NULL) {
164 char *p = strchr (*buf, '\0');
166 if (p[-1] == '\n' && p[-2] != '\r') {
167 strcpy (p - 1, "\r\n");
170 octets = p - (char *) *buf;
177 static int handle_invalid_peer_certificate (long vfy_result) {
178 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
179 mutt_error (_("Error verifying certificate: %s"),
180 NONULL (X509_verify_cert_error_string (vfy_result)));
182 mutt_error (_("Error verifying certificate. Error Code: %lu"), vfy_result);
185 return 1; /* Accept the problem */
188 static void event_cb (smtp_session_t session, int event_no, void *arg,...)
193 va_start(alist, arg);
195 case SMTP_EV_CONNECT:
196 case SMTP_EV_MAILSTATUS:
197 case SMTP_EV_RCPTSTATUS:
198 case SMTP_EV_MESSAGEDATA:
199 case SMTP_EV_MESSAGESENT:
200 case SMTP_EV_DISCONNECT: break;
201 case SMTP_EV_WEAK_CIPHER: {
203 bits = va_arg(alist, long); ok = va_arg(alist, int*);
204 mutt_message (_("SMTP_EV_WEAK_CIPHER, bits=%d - accepted."), bits);
208 case SMTP_EV_STARTTLS_OK:
209 mutt_message (_("Using TLS"));
212 case SMTP_EV_INVALID_PEER_CERTIFICATE: {
214 vfy_result = va_arg(alist, long); ok = va_arg(alist, int*);
215 *ok = handle_invalid_peer_certificate(vfy_result);
219 case SMTP_EV_NO_PEER_CERTIFICATE: {
220 ok = va_arg(alist, int*);
221 mutt_message (_("SMTP_EV_NO_PEER_CERTIFICATE - accepted."));
225 case SMTP_EV_WRONG_PEER_CERTIFICATE: {
226 ok = va_arg(alist, int*);
227 mutt_message (_("SMTP_EV_WRONG_PEER_CERTIFICATE - accepted."));
231 case SMTP_EV_NO_CLIENT_CERTIFICATE: {
232 ok = va_arg(alist, int*);
233 mutt_message (_("SMTP_EV_NO_CLIENT_CERTIFICATE - accepted."));
237 case SMTP_EV_EXTNA_DSN:
240 case SMTP_EV_EXTNA_STARTTLS:
243 case SMTP_EV_EXTNA_8BITMIME:
247 mutt_message(_("Got unhandled event ID = %d - ignored."), event_no);
253 static void do_dsn_notify (smtp_message_t message, const char* from) {
254 int flags = Notify_NOTSET;
255 smtp_recipient_t self = NULL;
257 if (!DsnNotify || !*DsnNotify || !message || !from || !*from ||
258 strstr (DsnNotify, "never") != NULL)
261 if (strstr (DsnNotify, "failure") != NULL)
262 flags |= Notify_FAILURE;
263 if (strstr (DsnNotify, "delay") != NULL)
264 flags |= Notify_DELAY;
265 if (strstr (DsnNotify, "success") != NULL)
266 flags |= Notify_SUCCESS;
268 if (flags != Notify_NOTSET) {
269 if (!(self = smtp_add_recipient (message, from)))
271 smtp_dsn_set_notify (self, flags);
275 static void do_dsn_ret (smtp_message_t message) {
276 if (!DsnReturn || !*DsnReturn || !message)
278 if (ascii_strncasecmp (DsnReturn, "hdrs", 4) == 0)
279 smtp_dsn_set_ret (message, Ret_HDRS);
280 else if (ascii_strncasecmp (DsnReturn, "full", 4) == 0)
281 smtp_dsn_set_ret (message, Ret_FULL);
284 #if defined (USE_LIBESMTP) && (defined (USE_SSL) || defined (USE_GNUTLS))
285 int mutt_libesmtp_check_usetls (const char* option, unsigned long p,
286 char* errbuf, size_t errlen) {
287 char* val = (char*) p;
290 if (str_ncmp (val, "enabled", 7) != 0 &&
291 str_ncmp (val, "required", 8) != 0) {
293 snprintf (errbuf, errlen, _("'%s' is invalid for %s"), val, option);
301 * mutt_libesmtp_invoke
302 * Sends a mail message to the provided recipients using libesmtp.
303 * Returns 0 upon success, -1 upon failure (and prints an error
306 int mutt_libesmtp_invoke (ADDRESS * from, /* the sender */
307 ADDRESS * to, ADDRESS * cc, ADDRESS * bcc, /* recips */
308 const char *msg, /* file containing message */
310 { /* message contains 8bit chars */
311 int ret = 0; /* return value, default = success */
312 smtp_session_t session;
313 smtp_message_t message;
314 char *hostportstr = NULL;
317 auth_context_t authctx = NULL;
318 const smtp_status_t *status;
319 char* envfrom = from->mailbox;
321 _mutt_libesmtp_ensure_init ();
323 if ((session = smtp_create_session ()) == NULL)
324 SMTPFAIL ("smtp_create_session");
326 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
327 if (SmtpUseTLS != NULL && ascii_strncasecmp("enabled", SmtpUseTLS, 7) == 0) {
328 smtp_starttls_enable(session, Starttls_ENABLED);
329 } else if (SmtpUseTLS != NULL && ascii_strncasecmp("required", SmtpUseTLS, 8) == 0) {
330 smtp_starttls_enable(session, Starttls_REQUIRED);
334 /* Create hostname:port string and tell libesmtp */
335 /* len = SmtpHost len + colon + max port (65536 => 5 chars) + terminator */
336 hostportlen = str_len (SmtpHost) + 7;
337 hostportstr = mem_malloc (hostportlen);
338 snprintf (hostportstr, hostportlen, "%s:%d", SmtpHost, SmtpPort);
339 if (!smtp_set_server (session, hostportstr))
340 SMTPFAIL ("smtp_set_server");
343 if ((authctx = auth_create_context ()) == NULL)
344 MSGFAIL ("auth_create_context failed");
345 auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
346 auth_set_interact_cb (authctx, _mutt_libesmtp_auth_interact, NULL);
348 if (!smtp_auth_set_context (session, authctx))
349 SMTPFAIL ("smtp_auth_set_context");
352 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
353 smtp_starttls_set_ctx (session, NULL);
355 smtp_set_eventcb (session, event_cb, NULL);
357 if ((message = smtp_add_message (session)) == NULL)
358 SMTPFAIL ("smtp_add_message");
360 /* Initialize envelope sender */
361 if (SmtpEnvFrom && *SmtpEnvFrom)
362 envfrom = SmtpEnvFrom;
363 if (!smtp_set_reverse_path (message, envfrom))
364 SMTPFAIL ("smtp_set_reverse_path");
366 /* set up DSN for message */
367 do_dsn_notify (message, envfrom);
368 do_dsn_ret (message);
370 /* set up 8bitmime flag */
371 if (eightbit && option (OPTUSE8BITMIME))
372 smtp_8bitmime_set_body (message, E8bitmime_8BITMIME);
374 if ((fp = fopen (msg, "r")) == NULL)
376 if (!smtp_set_messagecb (message, _mutt_libesmtp_messagefp_cb, fp))
377 SMTPFAIL ("smtp_set_messagecb");
378 if (_mutt_libesmtp_add_recipients (message, to))
380 if (_mutt_libesmtp_add_recipients (message, cc))
382 if (_mutt_libesmtp_add_recipients (message, bcc))
384 if (!smtp_start_session (session))
385 SMTPFAIL ("smtp_start_session");
387 status = smtp_message_transfer_status (message);
388 if (status->code < 200 || status->code > 299) {
391 snprintf (buf, sizeof (buf), "SMTP error while sending: %d %s",
392 status->code, status->text);
399 if (hostportstr != NULL)
402 smtp_destroy_session (session);
404 auth_destroy_context (authctx);
406 /* Forget user-entered SMTP AUTH password if send fails */