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.
17 #include <lib-lib/mem.h>
18 #include <lib-lib/str.h>
19 #include <lib-lib/ascii.h>
20 #include <lib-lib/macros.h>
26 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
27 #include <openssl/ssl.h>
32 #include <auth-client.h>
35 static char authpass[STRING] = "";
42 #define MSGFAIL(msg) \
44 mutt_error("%s", msg); \
47 #define LIBCFAIL(msg) \
49 mutt_error("%s: %s", msg, strerror(errno)); \
52 #define SMTPFAIL(msg) \
54 _mutt_libesmtp_perror(msg); \
57 #define extna(msg) { mutt_error (_("SMTP Extension '%s' not supported by MTA."), \
61 * _mutt_libesmtp_ensure_init
62 * Make sure the libESMTP support in mutt is initialized at some time.
64 static void _mutt_libesmtp_ensure_init ()
66 static int libesmtp_init = 0;
76 * _mutt_libesmtp_perror
77 * Prints 'msg', a colon, and then a string representation of the
78 * libesmtp errno as a mutt error.
80 static void _mutt_libesmtp_perror (const char *msg)
84 mutt_error ("%s: %s", msg,
85 smtp_strerror (smtp_errno (), buf, sizeof (buf)));
89 * _mutt_libesmtp_add_recipients
90 * Adds every address in 'addr' as a recipient to the smtp message
91 * 'message'. Note that this does not mean that they will necessarily
92 * show up in the mail headers (e.g., when bcc'ing). Returns 0 upon
93 * success, -1 upon failure (and prints an error message).
95 * Very similar to sendlib.c::add_args
98 _mutt_libesmtp_add_recipients (smtp_message_t message, ADDRESS * addr)
102 for (; addr; addr = addr->next) {
103 /* weed out group mailboxes, since those are for display only */
104 if (addr->mailbox && !addr->group) {
105 if (!smtp_add_recipient (message, addr->mailbox))
106 SMTPFAIL ("smtp_add_recipient");
115 _mutt_libesmtp_auth_interact (auth_client_request_t request,
116 char **result, int fields, void *arg)
120 for (i = 0; i < fields; i++) {
121 if (request[i].flags & AUTH_USER) {
122 result[i] = SmtpAuthUser;
124 else if (request[i].flags & AUTH_PASS) {
126 result[i] = SmtpAuthPass;
129 if (authpass[0] == '\0') {
132 snprintf (prompt, sizeof (prompt), "%s%s: ", request[i].prompt,
134 flags & AUTH_CLEARTEXT) ? " (not encrypted)" : "");
135 mutt_get_field_unbuffered (prompt, authpass, sizeof (authpass),
138 result[i] = authpass;
148 static const char *_mutt_libesmtp_messagefp_cb (void **buf, int *len,
154 *buf = malloc (BUFLEN);
157 rewind ((FILE *) arg);
161 if (fgets (*buf, BUFLEN - 2, (FILE *) arg) == NULL) {
165 char *p = strchr (*buf, '\0');
167 if (p[-1] == '\n' && p[-2] != '\r') {
168 strcpy (p - 1, "\r\n");
171 octets = p - (char *) *buf;
178 static int handle_invalid_peer_certificate (long vfy_result) {
179 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
180 mutt_error (_("Error verifying certificate: %s"),
181 NONULL (X509_verify_cert_error_string (vfy_result)));
183 mutt_error (_("Error verifying certificate. Error Code: %lu"), vfy_result);
186 return 1; /* Accept the problem */
189 static void event_cb (smtp_session_t session, int event_no, void *arg,...)
194 va_start(alist, arg);
196 case SMTP_EV_CONNECT:
197 case SMTP_EV_MAILSTATUS:
198 case SMTP_EV_RCPTSTATUS:
199 case SMTP_EV_MESSAGEDATA:
200 case SMTP_EV_MESSAGESENT:
201 case SMTP_EV_DISCONNECT: break;
202 case SMTP_EV_WEAK_CIPHER: {
204 bits = va_arg(alist, long); ok = va_arg(alist, int*);
205 mutt_message (_("SMTP_EV_WEAK_CIPHER, bits=%d - accepted."), bits);
209 case SMTP_EV_STARTTLS_OK:
210 mutt_message (_("Using TLS"));
213 case SMTP_EV_INVALID_PEER_CERTIFICATE: {
215 vfy_result = va_arg(alist, long); ok = va_arg(alist, int*);
216 *ok = handle_invalid_peer_certificate(vfy_result);
220 case SMTP_EV_NO_PEER_CERTIFICATE: {
221 ok = va_arg(alist, int*);
222 mutt_message (_("SMTP_EV_NO_PEER_CERTIFICATE - accepted."));
226 case SMTP_EV_WRONG_PEER_CERTIFICATE: {
227 ok = va_arg(alist, int*);
228 mutt_message (_("SMTP_EV_WRONG_PEER_CERTIFICATE - accepted."));
232 case SMTP_EV_NO_CLIENT_CERTIFICATE: {
233 ok = va_arg(alist, int*);
234 mutt_message (_("SMTP_EV_NO_CLIENT_CERTIFICATE - accepted."));
238 case SMTP_EV_EXTNA_DSN:
241 case SMTP_EV_EXTNA_STARTTLS:
244 case SMTP_EV_EXTNA_8BITMIME:
248 mutt_message(_("Got unhandled event ID = %d - ignored."), event_no);
254 static void do_dsn_notify (smtp_message_t message, const char* from) {
255 int flags = Notify_NOTSET;
256 smtp_recipient_t self = NULL;
258 if (!DsnNotify || !*DsnNotify || !message || !from || !*from ||
259 strstr (DsnNotify, "never") != NULL)
262 if (strstr (DsnNotify, "failure") != NULL)
263 flags |= Notify_FAILURE;
264 if (strstr (DsnNotify, "delay") != NULL)
265 flags |= Notify_DELAY;
266 if (strstr (DsnNotify, "success") != NULL)
267 flags |= Notify_SUCCESS;
269 if (flags != Notify_NOTSET) {
270 if (!(self = smtp_add_recipient (message, from)))
272 smtp_dsn_set_notify (self, flags);
276 static void do_dsn_ret (smtp_message_t message) {
277 if (!DsnReturn || !*DsnReturn || !message)
279 if (ascii_strncasecmp (DsnReturn, "hdrs", 4) == 0)
280 smtp_dsn_set_ret (message, Ret_HDRS);
281 else if (ascii_strncasecmp (DsnReturn, "full", 4) == 0)
282 smtp_dsn_set_ret (message, Ret_FULL);
285 #if defined (USE_LIBESMTP) && (defined (USE_SSL) || defined (USE_GNUTLS))
286 int mutt_libesmtp_check_usetls (const char* option, unsigned long p,
287 char* errbuf, size_t errlen) {
288 char* val = (char*) p;
291 if (str_ncmp (val, "enabled", 7) != 0 &&
292 str_ncmp (val, "required", 8) != 0) {
294 snprintf (errbuf, errlen, _("'%s' is invalid for %s"), val, option);
302 * mutt_libesmtp_invoke
303 * Sends a mail message to the provided recipients using libesmtp.
304 * Returns 0 upon success, -1 upon failure (and prints an error
307 int mutt_libesmtp_invoke (ADDRESS * from, /* the sender */
308 ADDRESS * to, ADDRESS * cc, ADDRESS * bcc, /* recips */
309 const char *msg, /* file containing message */
311 { /* message contains 8bit chars */
312 int ret = 0; /* return value, default = success */
313 smtp_session_t session;
314 smtp_message_t message;
315 char *hostportstr = NULL;
318 auth_context_t authctx = NULL;
319 const smtp_status_t *status;
320 char* envfrom = from->mailbox;
322 _mutt_libesmtp_ensure_init ();
324 if ((session = smtp_create_session ()) == NULL)
325 SMTPFAIL ("smtp_create_session");
327 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
328 if (SmtpUseTLS != NULL && ascii_strncasecmp("enabled", SmtpUseTLS, 7) == 0) {
329 smtp_starttls_enable(session, Starttls_ENABLED);
330 } else if (SmtpUseTLS != NULL && ascii_strncasecmp("required", SmtpUseTLS, 8) == 0) {
331 smtp_starttls_enable(session, Starttls_REQUIRED);
335 /* Create hostname:port string and tell libesmtp */
336 /* len = SmtpHost len + colon + max port (65536 => 5 chars) + terminator */
337 hostportlen = m_strlen(SmtpHost) + 7;
338 hostportstr = p_new(char, hostportlen);
339 snprintf (hostportstr, hostportlen, "%s:%d", SmtpHost, SmtpPort);
340 if (!smtp_set_server (session, hostportstr))
341 SMTPFAIL ("smtp_set_server");
344 if ((authctx = auth_create_context ()) == NULL)
345 MSGFAIL ("auth_create_context failed");
346 auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
347 auth_set_interact_cb (authctx, _mutt_libesmtp_auth_interact, NULL);
349 if (!smtp_auth_set_context (session, authctx))
350 SMTPFAIL ("smtp_auth_set_context");
353 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
354 smtp_starttls_set_ctx (session, NULL);
356 smtp_set_eventcb (session, event_cb, NULL);
358 if ((message = smtp_add_message (session)) == NULL)
359 SMTPFAIL ("smtp_add_message");
361 /* Initialize envelope sender */
362 if (option (OPTENVFROM) && EnvFrom)
363 envfrom = EnvFrom->mailbox;
364 if (!smtp_set_reverse_path (message, envfrom))
365 SMTPFAIL ("smtp_set_reverse_path");
367 /* set up DSN for message */
368 do_dsn_notify (message, envfrom);
369 do_dsn_ret (message);
371 /* set up 8bitmime flag */
372 if (eightbit && option (OPTUSE8BITMIME))
373 smtp_8bitmime_set_body (message, E8bitmime_8BITMIME);
375 if ((fp = fopen (msg, "r")) == NULL)
377 if (!smtp_set_messagecb (message, _mutt_libesmtp_messagefp_cb, fp))
378 SMTPFAIL ("smtp_set_messagecb");
379 if (_mutt_libesmtp_add_recipients (message, to))
381 if (_mutt_libesmtp_add_recipients (message, cc))
383 if (_mutt_libesmtp_add_recipients (message, bcc))
385 if (!smtp_start_session (session))
386 SMTPFAIL ("smtp_start_session");
388 status = smtp_message_transfer_status (message);
389 if (status->code < 200 || status->code > 299) {
392 snprintf (buf, sizeof (buf), "SMTP error while sending: %d %s",
393 status->code, status->text);
400 if (hostportstr != NULL)
401 p_delete(&hostportstr);
403 smtp_destroy_session (session);
405 auth_destroy_context (authctx);
407 /* Forget user-entered SMTP AUTH password if send fails */