2 * Copyright notice from original mutt:
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
21 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
22 #include <openssl/ssl.h>
27 #include <auth-client.h>
30 static char authpass[STRING] = "";
37 #define MSGFAIL(msg) \
39 mutt_error("%s", msg); \
42 #define LIBCFAIL(msg) \
44 mutt_error("%s: %s", msg, strerror(errno)); \
47 #define SMTPFAIL(msg) \
49 _mutt_libesmtp_perror(msg); \
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 * 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 = malloc (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 strcpy (p - 1, "\r\n");
164 octets = p - (char *) *buf;
171 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
172 static int handle_invalid_peer_certificate (long vfy_result) {
173 mutt_error (_("Error verifying certificate: %s"),
174 NONULL (X509_verify_cert_error_string (vfy_result)));
176 return 1; /* Accept the problem */
180 void event_cb (smtp_session_t session, int event_no, void *arg,...)
185 va_start(alist, arg);
187 case SMTP_EV_CONNECT:
188 case SMTP_EV_MAILSTATUS:
189 case SMTP_EV_RCPTSTATUS:
190 case SMTP_EV_MESSAGEDATA:
191 case SMTP_EV_MESSAGESENT:
192 case SMTP_EV_DISCONNECT: break;
193 case SMTP_EV_WEAK_CIPHER: {
195 bits = va_arg(alist, long); ok = va_arg(alist, int*);
196 mutt_message (_("SMTP_EV_WEAK_CIPHER, bits=%d - accepted."), bits);
200 case SMTP_EV_STARTTLS_OK:
201 mutt_message (_("Using TLS"));
204 case SMTP_EV_INVALID_PEER_CERTIFICATE: {
206 vfy_result = va_arg(alist, long); ok = va_arg(alist, int*);
207 *ok = handle_invalid_peer_certificate(vfy_result);
211 case SMTP_EV_NO_PEER_CERTIFICATE: {
212 ok = va_arg(alist, int*);
213 mutt_message (_("SMTP_EV_NO_PEER_CERTIFICATE - accepted."));
217 case SMTP_EV_WRONG_PEER_CERTIFICATE: {
218 ok = va_arg(alist, int*);
219 mutt_message (_("SMTP_EV_WRONG_PEER_CERTIFICATE - accepted."));
223 case SMTP_EV_NO_CLIENT_CERTIFICATE: {
224 ok = va_arg(alist, int*);
225 mutt_message (_("SMTP_EV_NO_CLIENT_CERTIFICATE - accepted."));
230 mutt_message(_("Got event: %d - ignored."), event_no);
237 * mutt_invoke_libesmtp
238 * Sends a mail message to the provided recipients using libesmtp.
239 * Returns 0 upon success, -1 upon failure (and prints an error
242 int mutt_invoke_libesmtp (ADDRESS * from, /* the sender */
243 ADDRESS * to, ADDRESS * cc, ADDRESS * bcc, /* recips */
244 const char *msg, /* file containing message */
246 { /* message contains 8bit chars */
247 int ret = 0; /* return value, default = success */
248 smtp_session_t session;
249 smtp_message_t message;
250 char *hostportstr = NULL;
253 auth_context_t authctx = NULL;
254 const smtp_status_t *status;
256 _mutt_libesmtp_ensure_init ();
258 if ((session = smtp_create_session ()) == NULL)
259 SMTPFAIL ("smtp_create_session");
261 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
262 if (SmtpUseTLS != NULL && ascii_strncasecmp("enabled", SmtpUseTLS, 7) == 0) {
263 smtp_starttls_enable(session, Starttls_ENABLED);
264 } else if (SmtpUseTLS != NULL && ascii_strncasecmp("required", SmtpUseTLS, 8) == 0) {
265 smtp_starttls_enable(session, Starttls_REQUIRED);
269 /* Create hostname:port string and tell libesmtp */
270 /* len = SmtpHost len + colon + max port (65536 => 5 chars) + terminator */
271 hostportlen = str_len (SmtpHost) + 7;
272 hostportstr = mem_malloc (hostportlen);
273 snprintf (hostportstr, hostportlen, "%s:%d", SmtpHost, SmtpPort);
274 if (!smtp_set_server (session, hostportstr))
275 SMTPFAIL ("smtp_set_server");
278 if ((authctx = auth_create_context ()) == NULL)
279 MSGFAIL ("auth_create_context failed");
280 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
281 auth_set_mechanism_flags (authctx, AUTH_PLUGIN_EXTERNAL, 0);
283 auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
285 auth_set_interact_cb (authctx, _mutt_libesmtp_auth_interact, NULL);
287 if (!smtp_auth_set_context (session, authctx))
288 SMTPFAIL ("smtp_auth_set_context");
291 #if defined (USE_SSL) || (defined (USE_GNUTLS) && defined (HAVE_GNUTLS_OPENSSL_H))
292 smtp_starttls_set_ctx (session, NULL);
294 smtp_set_eventcb (session, event_cb, NULL);
296 if ((message = smtp_add_message (session)) == NULL)
297 SMTPFAIL ("smtp_add_message");
298 /* Initialize envelope sender */
299 if (!smtp_set_reverse_path (message, from->mailbox))
300 SMTPFAIL ("smtp_set_reverse_path");
302 if ((fp = fopen (msg, "r")) == NULL)
304 if (!smtp_set_messagecb (message, _mutt_libesmtp_messagefp_cb, fp))
305 SMTPFAIL ("smtp_set_messagecb");
306 if (_mutt_libesmtp_add_recipients (message, to))
308 if (_mutt_libesmtp_add_recipients (message, cc))
310 if (_mutt_libesmtp_add_recipients (message, bcc))
312 if (!smtp_start_session (session))
313 SMTPFAIL ("smtp_start_session");
315 status = smtp_message_transfer_status (message);
316 if (status->code < 200 || status->code > 299) {
319 snprintf (buf, sizeof (buf), "SMTP error while sending: %d %s",
320 status->code, status->text);
327 if (hostportstr != NULL)
330 smtp_destroy_session (session);
332 auth_destroy_context (authctx);
334 /* Forget user-entered SMTP AUTH password if send fails */