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 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
178 static int handle_invalid_peer_certificate (long vfy_result) {
179 mutt_error (_("Error verifying certificate: %s"),
180 NONULL (X509_verify_cert_error_string (vfy_result)));
182 return 1; /* Accept the problem */
186 static void event_cb (smtp_session_t session, int event_no, void *arg,...)
191 va_start(alist, arg);
193 case SMTP_EV_CONNECT:
194 case SMTP_EV_MAILSTATUS:
195 case SMTP_EV_RCPTSTATUS:
196 case SMTP_EV_MESSAGEDATA:
197 case SMTP_EV_MESSAGESENT:
198 case SMTP_EV_DISCONNECT: break;
199 case SMTP_EV_WEAK_CIPHER: {
201 bits = va_arg(alist, long); ok = va_arg(alist, int*);
202 mutt_message (_("SMTP_EV_WEAK_CIPHER, bits=%d - accepted."), bits);
206 case SMTP_EV_STARTTLS_OK:
207 mutt_message (_("Using TLS"));
210 case SMTP_EV_INVALID_PEER_CERTIFICATE: {
212 vfy_result = va_arg(alist, long); ok = va_arg(alist, int*);
213 *ok = handle_invalid_peer_certificate(vfy_result);
217 case SMTP_EV_NO_PEER_CERTIFICATE: {
218 ok = va_arg(alist, int*);
219 mutt_message (_("SMTP_EV_NO_PEER_CERTIFICATE - accepted."));
223 case SMTP_EV_WRONG_PEER_CERTIFICATE: {
224 ok = va_arg(alist, int*);
225 mutt_message (_("SMTP_EV_WRONG_PEER_CERTIFICATE - accepted."));
229 case SMTP_EV_NO_CLIENT_CERTIFICATE: {
230 ok = va_arg(alist, int*);
231 mutt_message (_("SMTP_EV_NO_CLIENT_CERTIFICATE - accepted."));
235 case SMTP_EV_EXTNA_DSN:
238 case SMTP_EV_EXTNA_STARTTLS:
241 case SMTP_EV_EXTNA_8BITMIME:
245 mutt_message(_("Got unhandled event ID = %d - ignored."), event_no);
251 static void do_dsn_notify (smtp_message_t message, const char* from) {
252 int flags = Notify_NOTSET;
253 smtp_recipient_t self = NULL;
255 if (!DsnNotify || !*DsnNotify || !message || !from || !*from ||
256 strstr (DsnNotify, "never") != NULL)
259 if (strstr (DsnNotify, "failure") != NULL)
260 flags |= Notify_FAILURE;
261 if (strstr (DsnNotify, "delay") != NULL)
262 flags |= Notify_DELAY;
263 if (strstr (DsnNotify, "success") != NULL)
264 flags |= Notify_SUCCESS;
266 if (flags != Notify_NOTSET) {
267 if (!(self = smtp_add_recipient (message, from)))
269 smtp_dsn_set_notify (self, flags);
273 static void do_dsn_ret (smtp_message_t message) {
274 if (!DsnReturn || !*DsnReturn || !message)
276 if (ascii_strncasecmp (DsnReturn, "hdrs", 4) == 0)
277 smtp_dsn_set_ret (message, Ret_HDRS);
278 else if (ascii_strncasecmp (DsnReturn, "full", 4) == 0)
279 smtp_dsn_set_ret (message, Ret_FULL);
283 * mutt_invoke_libesmtp
284 * Sends a mail message to the provided recipients using libesmtp.
285 * Returns 0 upon success, -1 upon failure (and prints an error
288 int mutt_invoke_libesmtp (ADDRESS * from, /* the sender */
289 ADDRESS * to, ADDRESS * cc, ADDRESS * bcc, /* recips */
290 const char *msg, /* file containing message */
292 { /* message contains 8bit chars */
293 int ret = 0; /* return value, default = success */
294 smtp_session_t session;
295 smtp_message_t message;
296 char *hostportstr = NULL;
299 auth_context_t authctx = NULL;
300 const smtp_status_t *status;
301 char* envfrom = from->mailbox;
303 _mutt_libesmtp_ensure_init ();
305 if ((session = smtp_create_session ()) == NULL)
306 SMTPFAIL ("smtp_create_session");
308 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
309 if (SmtpUseTLS != NULL && ascii_strncasecmp("enabled", SmtpUseTLS, 7) == 0) {
310 smtp_starttls_enable(session, Starttls_ENABLED);
311 } else if (SmtpUseTLS != NULL && ascii_strncasecmp("required", SmtpUseTLS, 8) == 0) {
312 smtp_starttls_enable(session, Starttls_REQUIRED);
316 /* Create hostname:port string and tell libesmtp */
317 /* len = SmtpHost len + colon + max port (65536 => 5 chars) + terminator */
318 hostportlen = str_len (SmtpHost) + 7;
319 hostportstr = mem_malloc (hostportlen);
320 snprintf (hostportstr, hostportlen, "%s:%d", SmtpHost, SmtpPort);
321 if (!smtp_set_server (session, hostportstr))
322 SMTPFAIL ("smtp_set_server");
325 if ((authctx = auth_create_context ()) == NULL)
326 MSGFAIL ("auth_create_context failed");
327 auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
328 auth_set_interact_cb (authctx, _mutt_libesmtp_auth_interact, NULL);
330 if (!smtp_auth_set_context (session, authctx))
331 SMTPFAIL ("smtp_auth_set_context");
334 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
335 smtp_starttls_set_ctx (session, NULL);
337 smtp_set_eventcb (session, event_cb, NULL);
339 if ((message = smtp_add_message (session)) == NULL)
340 SMTPFAIL ("smtp_add_message");
342 /* Initialize envelope sender */
343 if (SmtpEnvFrom && *SmtpEnvFrom)
344 envfrom = SmtpEnvFrom;
345 if (!smtp_set_reverse_path (message, envfrom))
346 SMTPFAIL ("smtp_set_reverse_path");
348 /* set up DSN for message */
349 do_dsn_notify (message, envfrom);
350 do_dsn_ret (message);
352 /* set up 8bitmime flag */
353 if (eightbit && option (OPTUSE8BITMIME))
354 smtp_8bitmime_set_body (message, E8bitmime_8BITMIME);
356 if ((fp = fopen (msg, "r")) == NULL)
358 if (!smtp_set_messagecb (message, _mutt_libesmtp_messagefp_cb, fp))
359 SMTPFAIL ("smtp_set_messagecb");
360 if (_mutt_libesmtp_add_recipients (message, to))
362 if (_mutt_libesmtp_add_recipients (message, cc))
364 if (_mutt_libesmtp_add_recipients (message, bcc))
366 if (!smtp_start_session (session))
367 SMTPFAIL ("smtp_start_session");
369 status = smtp_message_transfer_status (message);
370 if (status->code < 200 || status->code > 299) {
373 snprintf (buf, sizeof (buf), "SMTP error while sending: %d %s",
374 status->code, status->text);
381 if (hostportstr != NULL)
384 smtp_destroy_session (session);
386 auth_destroy_context (authctx);
388 /* Forget user-entered SMTP AUTH password if send fails */