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);
282 #if defined (USE_LIBESMTP) && (defined (USE_SSL) || defined (USE_GNUTLS))
283 int mutt_libesmtp_check_usetls (const char* option, unsigned long p,
284 char* errbuf, size_t errlen) {
285 char* val = (char*) p;
288 if (str_ncmp (val, "enabled", 7) != 0 &&
289 str_ncmp (val, "required", 8) != 0) {
291 snprintf (errbuf, errlen, _("'%s' is invalid for %s"), val, option);
299 * mutt_libesmtp_invoke
300 * Sends a mail message to the provided recipients using libesmtp.
301 * Returns 0 upon success, -1 upon failure (and prints an error
304 int mutt_libesmtp_invoke (ADDRESS * from, /* the sender */
305 ADDRESS * to, ADDRESS * cc, ADDRESS * bcc, /* recips */
306 const char *msg, /* file containing message */
308 { /* message contains 8bit chars */
309 int ret = 0; /* return value, default = success */
310 smtp_session_t session;
311 smtp_message_t message;
312 char *hostportstr = NULL;
315 auth_context_t authctx = NULL;
316 const smtp_status_t *status;
317 char* envfrom = from->mailbox;
319 _mutt_libesmtp_ensure_init ();
321 if ((session = smtp_create_session ()) == NULL)
322 SMTPFAIL ("smtp_create_session");
324 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
325 if (SmtpUseTLS != NULL && ascii_strncasecmp("enabled", SmtpUseTLS, 7) == 0) {
326 smtp_starttls_enable(session, Starttls_ENABLED);
327 } else if (SmtpUseTLS != NULL && ascii_strncasecmp("required", SmtpUseTLS, 8) == 0) {
328 smtp_starttls_enable(session, Starttls_REQUIRED);
332 /* Create hostname:port string and tell libesmtp */
333 /* len = SmtpHost len + colon + max port (65536 => 5 chars) + terminator */
334 hostportlen = str_len (SmtpHost) + 7;
335 hostportstr = mem_malloc (hostportlen);
336 snprintf (hostportstr, hostportlen, "%s:%d", SmtpHost, SmtpPort);
337 if (!smtp_set_server (session, hostportstr))
338 SMTPFAIL ("smtp_set_server");
341 if ((authctx = auth_create_context ()) == NULL)
342 MSGFAIL ("auth_create_context failed");
343 auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
344 auth_set_interact_cb (authctx, _mutt_libesmtp_auth_interact, NULL);
346 if (!smtp_auth_set_context (session, authctx))
347 SMTPFAIL ("smtp_auth_set_context");
350 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
351 smtp_starttls_set_ctx (session, NULL);
353 smtp_set_eventcb (session, event_cb, NULL);
355 if ((message = smtp_add_message (session)) == NULL)
356 SMTPFAIL ("smtp_add_message");
358 /* Initialize envelope sender */
359 if (SmtpEnvFrom && *SmtpEnvFrom)
360 envfrom = SmtpEnvFrom;
361 if (!smtp_set_reverse_path (message, envfrom))
362 SMTPFAIL ("smtp_set_reverse_path");
364 /* set up DSN for message */
365 do_dsn_notify (message, envfrom);
366 do_dsn_ret (message);
368 /* set up 8bitmime flag */
369 if (eightbit && option (OPTUSE8BITMIME))
370 smtp_8bitmime_set_body (message, E8bitmime_8BITMIME);
372 if ((fp = fopen (msg, "r")) == NULL)
374 if (!smtp_set_messagecb (message, _mutt_libesmtp_messagefp_cb, fp))
375 SMTPFAIL ("smtp_set_messagecb");
376 if (_mutt_libesmtp_add_recipients (message, to))
378 if (_mutt_libesmtp_add_recipients (message, cc))
380 if (_mutt_libesmtp_add_recipients (message, bcc))
382 if (!smtp_start_session (session))
383 SMTPFAIL ("smtp_start_session");
385 status = smtp_message_transfer_status (message);
386 if (status->code < 200 || status->code > 299) {
389 snprintf (buf, sizeof (buf), "SMTP error while sending: %d %s",
390 status->code, status->text);
397 if (hostportstr != NULL)
400 smtp_destroy_session (session);
402 auth_destroy_context (authctx);
404 /* Forget user-entered SMTP AUTH password if send fails */