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.
14 #include <lib-lib/lib-lib.h>
16 #include <lib-ui/enter.h>
21 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
22 #include <openssl/ssl.h>
25 #include <auth-client.h>
28 static char authpass[STRING] = "";
35 #define MSGFAIL(msg) \
37 mutt_error("%s", msg); \
40 #define LIBCFAIL(msg) \
42 mutt_error("%s: %s", msg, strerror(errno)); \
45 #define SMTPFAIL(msg) \
47 _mutt_libesmtp_perror(msg); \
50 #define extna(msg) { mutt_error (_("SMTP Extension '%s' not supported by MTA."), \
54 * _mutt_libesmtp_ensure_init
55 * Make sure the libESMTP support in mutt is initialized at some time.
57 static void _mutt_libesmtp_ensure_init ()
59 static int libesmtp_init = 0;
69 * _mutt_libesmtp_perror
70 * Prints 'msg', a colon, and then a string representation of the
71 * libesmtp errno as a mutt error.
73 static void _mutt_libesmtp_perror (const char *msg)
77 mutt_error ("%s: %s", msg,
78 smtp_strerror (smtp_errno (), buf, sizeof (buf)));
82 * _mutt_libesmtp_add_recipients
83 * Adds every address in 'addr' as a recipient to the smtp message
84 * 'message'. Note that this does not mean that they will necessarily
85 * show up in the mail headers (e.g., when bcc'ing). Returns 0 upon
86 * success, -1 upon failure (and prints an error message).
88 * Very similar to sendlib.c::add_args
91 _mutt_libesmtp_add_recipients (smtp_message_t message, address_t * addr)
95 for (; addr; addr = addr->next) {
96 /* weed out group mailboxes, since those are for display only */
97 if (addr->mailbox && !addr->group) {
98 if (!smtp_add_recipient (message, addr->mailbox))
99 SMTPFAIL ("smtp_add_recipient");
108 _mutt_libesmtp_auth_interact (auth_client_request_t request,
109 char **result, int fields, void *arg)
113 for (i = 0; i < fields; i++) {
114 if (request[i].flags & AUTH_USER) {
115 result[i] = SmtpAuthUser;
117 else if (request[i].flags & AUTH_PASS) {
119 result[i] = SmtpAuthPass;
122 if (authpass[0] == '\0') {
125 snprintf (prompt, sizeof (prompt), "%s%s: ", request[i].prompt,
127 flags & AUTH_CLEARTEXT) ? " (not encrypted)" : "");
128 mutt_get_field_unbuffered (prompt, authpass, sizeof (authpass),
131 result[i] = authpass;
141 static const char *_mutt_libesmtp_messagefp_cb (void **buf, int *len,
147 *buf = xmalloc(BUFLEN);
150 rewind ((FILE *) arg);
154 if (fgets (*buf, BUFLEN - 2, (FILE *) arg) == NULL) {
158 char *p = strchr (*buf, '\0');
160 if (p[-1] == '\n' && p[-2] != '\r') {
161 m_strcpy(p - 1, (char *) *buf + BUFLEN - p + 1, "\r\n");
164 octets = p - (char *) *buf;
171 static int handle_invalid_peer_certificate (long vfy_result) {
172 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
173 mutt_error (_("Error verifying certificate: %s"),
174 NONULL (X509_verify_cert_error_string (vfy_result)));
176 mutt_error (_("Error verifying certificate. Error Code: %lu"), vfy_result);
179 return 1; /* Accept the problem */
182 static void event_cb (smtp_session_t session, int event_no, void *arg,...)
187 va_start(alist, arg);
189 case SMTP_EV_CONNECT:
190 case SMTP_EV_MAILSTATUS:
191 case SMTP_EV_RCPTSTATUS:
192 case SMTP_EV_MESSAGEDATA:
193 case SMTP_EV_MESSAGESENT:
194 case SMTP_EV_DISCONNECT: break;
195 case SMTP_EV_WEAK_CIPHER: {
197 bits = va_arg(alist, long); ok = va_arg(alist, int*);
198 mutt_message (_("SMTP_EV_WEAK_CIPHER, bits=%d - accepted."), bits);
202 case SMTP_EV_STARTTLS_OK:
203 mutt_message (_("Using TLS"));
206 case SMTP_EV_INVALID_PEER_CERTIFICATE: {
208 vfy_result = va_arg(alist, long); ok = va_arg(alist, int*);
209 *ok = handle_invalid_peer_certificate(vfy_result);
213 case SMTP_EV_NO_PEER_CERTIFICATE: {
214 ok = va_arg(alist, int*);
215 mutt_message (_("SMTP_EV_NO_PEER_CERTIFICATE - accepted."));
219 case SMTP_EV_WRONG_PEER_CERTIFICATE: {
220 ok = va_arg(alist, int*);
221 mutt_message (_("SMTP_EV_WRONG_PEER_CERTIFICATE - accepted."));
225 case SMTP_EV_NO_CLIENT_CERTIFICATE: {
226 ok = va_arg(alist, int*);
227 mutt_message (_("SMTP_EV_NO_CLIENT_CERTIFICATE - accepted."));
231 case SMTP_EV_EXTNA_DSN:
234 case SMTP_EV_EXTNA_STARTTLS:
237 case SMTP_EV_EXTNA_8BITMIME:
241 mutt_message(_("Got unhandled event ID = %d - ignored."), event_no);
247 static void do_dsn_notify (smtp_message_t message, const char* from) {
248 int flags = Notify_NOTSET;
249 smtp_recipient_t self = NULL;
251 if (m_strisempty(DsnNotify) || !message || m_strisempty(from) ||
252 strstr (DsnNotify, "never") != NULL)
255 if (strstr (DsnNotify, "failure") != NULL)
256 flags |= Notify_FAILURE;
257 if (strstr (DsnNotify, "delay") != NULL)
258 flags |= Notify_DELAY;
259 if (strstr (DsnNotify, "success") != NULL)
260 flags |= Notify_SUCCESS;
262 if (flags != Notify_NOTSET) {
263 if (!(self = smtp_add_recipient (message, from)))
265 smtp_dsn_set_notify (self, flags);
269 static void do_dsn_ret (smtp_message_t message) {
270 if (m_strisempty(DsnReturn) || !message)
272 if (ascii_strncasecmp (DsnReturn, "hdrs", 4) == 0)
273 smtp_dsn_set_ret (message, Ret_HDRS);
274 else if (ascii_strncasecmp (DsnReturn, "full", 4) == 0)
275 smtp_dsn_set_ret (message, Ret_FULL);
278 #if defined (USE_LIBESMTP) && (defined (USE_SSL) || defined (USE_GNUTLS))
279 int mutt_libesmtp_check_usetls (const char* option, unsigned long p,
280 char* errbuf, size_t errlen) {
281 char* val = (char*) p;
282 if (m_strisempty(val))
284 if (m_strncmp(val, "enabled", 7) != 0 &&
285 m_strncmp(val, "required", 8) != 0) {
287 snprintf (errbuf, errlen, _("'%s' is invalid for %s"), val, option);
295 * mutt_libesmtp_invoke
296 * Sends a mail message to the provided recipients using libesmtp.
297 * Returns 0 upon success, -1 upon failure (and prints an error
300 int mutt_libesmtp_invoke (address_t * from, /* the sender */
301 address_t * to, address_t * cc, address_t * bcc, /* recips */
302 const char *msg, /* file containing message */
304 { /* message contains 8bit chars */
305 int ret = 0; /* return value, default = success */
306 smtp_session_t session;
307 smtp_message_t message;
308 char *hostportstr = NULL;
311 auth_context_t authctx = NULL;
312 const smtp_status_t *status;
313 char* envfrom = from->mailbox;
315 _mutt_libesmtp_ensure_init ();
317 if ((session = smtp_create_session ()) == NULL)
318 SMTPFAIL ("smtp_create_session");
320 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
321 if (SmtpUseTLS != NULL && ascii_strncasecmp("enabled", SmtpUseTLS, 7) == 0) {
322 smtp_starttls_enable(session, Starttls_ENABLED);
323 } else if (SmtpUseTLS != NULL && ascii_strncasecmp("required", SmtpUseTLS, 8) == 0) {
324 smtp_starttls_enable(session, Starttls_REQUIRED);
328 /* Create hostname:port string and tell libesmtp */
329 /* len = SmtpHost len + colon + max port (65536 => 5 chars) + terminator */
330 hostportlen = m_strlen(SmtpHost) + 7;
331 hostportstr = p_new(char, hostportlen);
332 snprintf (hostportstr, hostportlen, "%s:%d", SmtpHost, SmtpPort);
333 if (!smtp_set_server (session, hostportstr))
334 SMTPFAIL ("smtp_set_server");
337 if ((authctx = auth_create_context ()) == NULL)
338 MSGFAIL ("auth_create_context failed");
339 auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
340 auth_set_interact_cb (authctx, _mutt_libesmtp_auth_interact, NULL);
342 if (!smtp_auth_set_context (session, authctx))
343 SMTPFAIL ("smtp_auth_set_context");
346 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
347 smtp_starttls_set_ctx (session, NULL);
349 smtp_set_eventcb (session, event_cb, NULL);
351 if ((message = smtp_add_message (session)) == NULL)
352 SMTPFAIL ("smtp_add_message");
354 /* Initialize envelope sender */
355 if (option (OPTENVFROM) && EnvFrom)
356 envfrom = EnvFrom->mailbox;
357 if (!smtp_set_reverse_path (message, envfrom))
358 SMTPFAIL ("smtp_set_reverse_path");
360 /* set up DSN for message */
361 do_dsn_notify (message, envfrom);
362 do_dsn_ret (message);
364 /* set up 8bitmime flag */
365 if (eightbit && option (OPTUSE8BITMIME))
366 smtp_8bitmime_set_body (message, E8bitmime_8BITMIME);
368 if ((fp = fopen (msg, "r")) == NULL)
370 if (!smtp_set_messagecb (message, _mutt_libesmtp_messagefp_cb, fp))
371 SMTPFAIL ("smtp_set_messagecb");
372 if (_mutt_libesmtp_add_recipients (message, to))
374 if (_mutt_libesmtp_add_recipients (message, cc))
376 if (_mutt_libesmtp_add_recipients (message, bcc))
378 if (!smtp_start_session (session))
379 SMTPFAIL ("smtp_start_session");
381 status = smtp_message_transfer_status (message);
382 if (status->code < 200 || status->code > 299) {
385 snprintf (buf, sizeof (buf), "SMTP error while sending: %d %s",
386 status->code, status->text);
392 if (hostportstr != NULL)
393 p_delete(&hostportstr);
395 smtp_destroy_session (session);
397 auth_destroy_context (authctx);
399 /* Forget user-entered SMTP AUTH password if send fails */