2 * Transmit outbound SMTP mail to the big wide world of the Internet
4 * This is the new, exciting, clever version that makes libcurl do all the work :)
6 * Copyright (c) 1997-2017 by the citadel.org team
8 * This program is open source software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
22 #include <sysconfig.h>
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
29 # include <sys/time.h>
38 #include <sys/types.h>
40 #include <libcitadel.h>
41 #include <curl/curl.h>
44 #include "citserver.h"
47 #include "ctdl_module.h"
48 #include "clientsocket.h"
51 #include "internet_addressing.h"
52 #include "citadel_dirs.h"
53 #include "modules/smtp/smtp_util.h"
55 struct smtpmsgsrc { // Data passed in and out of libcurl for message upload
61 struct CitContext smtp_client_CC;
62 static int doing_smtpclient = 0;
63 long *smtpq = NULL; // array of msgnums containing queue instructions
64 int smtpq_count = 0; // number of queue messages in smtpq
65 int smtpq_alloc = 0; // current allocation size for smtpq
69 * Initialize the SMTP outbound queue
71 void smtp_init_spoolout(void) {
72 struct ctdlroom qrbuf;
75 * Create the room. This will silently fail if the room already
76 * exists, and that's perfectly ok, because we want it to exist.
78 CtdlCreateRoom(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1, 0, VIEW_QUEUE);
81 * Make sure it's set to be a "system room" so it doesn't show up
82 * in the <K>nown rooms list for administrators.
84 if (CtdlGetRoomLock(&qrbuf, SMTP_SPOOLOUT_ROOM) == 0) {
85 qrbuf.QRflags2 |= QR2_SYSTEM;
86 CtdlPutRoomLock(&qrbuf);
91 /* For internet mail, generate delivery instructions.
92 * Yes, this is recursive. Deal with it. Infinite recursion does
93 * not happen because the delivery instructions message does not
94 * contain a recipient.
96 int smtp_aftersave(struct CtdlMessage *msg, recptypes *recps)
98 if ((recps != NULL) && (recps->num_internet > 0)) {
99 struct CtdlMessage *imsg = NULL;
101 StrBuf *SpoolMsg = NewStrBuf();
105 syslog(LOG_DEBUG, "smtpclient: generating delivery instructions");
107 StrBufPrintf(SpoolMsg,
108 "Content-type: "SPOOLMIME"\n"
113 msg->cm_fields[eVltMsgNum],
117 if (recps->envelope_from != NULL) {
118 StrBufAppendBufPlain(SpoolMsg, HKEY("envelope_from|"), 0);
119 StrBufAppendBufPlain(SpoolMsg, recps->envelope_from, -1, 0);
120 StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
122 if (recps->sending_room != NULL) {
123 StrBufAppendBufPlain(SpoolMsg, HKEY("source_room|"), 0);
124 StrBufAppendBufPlain(SpoolMsg, recps->sending_room, -1, 0);
125 StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
128 nTokens = num_tokens(recps->recp_internet, '|');
129 for (i = 0; i < nTokens; i++) {
131 len = extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
133 StrBufAppendBufPlain(SpoolMsg, HKEY("remote|"), 0);
134 StrBufAppendBufPlain(SpoolMsg, recipient, len, 0);
135 StrBufAppendBufPlain(SpoolMsg, HKEY("|0||\n"), 0);
139 imsg = malloc(sizeof(struct CtdlMessage));
140 memset(imsg, 0, sizeof(struct CtdlMessage));
141 imsg->cm_magic = CTDLMESSAGE_MAGIC;
142 imsg->cm_anon_type = MES_NORMAL;
143 imsg->cm_format_type = FMT_RFC822;
144 CM_SetField(imsg, eMsgSubject, HKEY("QMSG"));
145 CM_SetField(imsg, eAuthor, HKEY("Citadel"));
146 CM_SetField(imsg, eJournal, HKEY("do not journal"));
147 CM_SetAsFieldSB(imsg, eMesageText, &SpoolMsg);
148 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
156 * Callback for smtp_attempt_delivery() to supply libcurl with upload data.
158 static size_t upload_source(void *ptr, size_t size, size_t nmemb, void *userp)
160 struct smtpmsgsrc *s = (struct smtpmsgsrc *) userp;
162 const char *send_this = NULL;
164 sendbytes = (size * nmemb);
166 if (s->bytes_sent >= s->bytes_total) {
167 return(0); // we are donez0r
170 if (sendbytes > (s->bytes_total - s->bytes_sent)) {
171 sendbytes = s->bytes_total - s->bytes_sent; // can't send more than we have
174 send_this = ChrPtr(s->TheMessage);
175 send_this += s->bytes_sent; // start where we last left off
177 memcpy(ptr, send_this, sendbytes);
178 s->bytes_sent += sendbytes;
179 return(sendbytes); // return the number of bytes _actually_ copied
184 * Attempt a delivery to one recipient.
185 * Returns a three-digit SMTP status code.
187 int smtp_attempt_delivery(long msgid, char *recp, char *envelope_from)
190 char *fromaddr = NULL;
192 CURLcode res = CURLE_OK;
193 struct curl_slist *recipients = NULL;
194 long response_code = 421;
200 char try_this_mx[256];
204 syslog(LOG_DEBUG, "smtpclient: smtp_attempt_delivery(%ld, %s)", msgid, recp);
206 process_rfc822_addr(recp, user, node, name); // split recipient address into username, hostname, displayname
207 num_mx = getmx(mxes, node);
212 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
213 CtdlOutputMsg(msgid, MT_RFC822, HEADERS_ALL, 0, 1, NULL, 0, NULL, &fromaddr, NULL);
214 s.TheMessage = CC->redirect_buffer;
215 s.bytes_total = StrLength(CC->redirect_buffer);
217 CC->redirect_buffer = NULL;
220 for (i=0; ((i<num_mx)&&((response_code/100)==4)); ++i) { // keep trying MXes until one works or we run out
221 response_code = 421; // default 421 makes non-protocol errors transient
222 s.bytes_sent = 0; // rewind our buffer in case we try multiple MXes
224 curl = curl_easy_init();
227 if (!IsEmptyStr(envelope_from)) {
228 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, envelope_from);
231 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, fromaddr);
234 recipients = curl_slist_append(recipients, recp);
235 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
236 curl_easy_setopt(curl, CURLOPT_READFUNCTION, upload_source);
237 curl_easy_setopt(curl, CURLOPT_READDATA, &s);
238 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1); // tell libcurl we are uploading
239 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 20L); // Time out after 20 seconds
240 if (CtdlGetConfigInt("c_smtpclient_try_starttls") != 0) {
241 curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY); // Attempt STARTTLS if offered
243 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
244 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
245 // curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_error_buffer);
247 // Construct an SMTP URL in the form of:
248 // smtp[s]://target_host/source_host
249 // This looks weird but libcurl uses that last part to set our name for EHLO or HELO.
250 // We check for "smtp://" and "smtps://" because the admin may have put those prefixes in a smart-host entry.
251 // If there is no prefix we add "smtp://"
252 extract_token(try_this_mx, mxes, i, '|', (sizeof try_this_mx - 7));
253 snprintf(smtp_url, sizeof smtp_url,
255 (((!strncasecmp(try_this_mx, HKEY("smtp://"))) || (!strncasecmp(try_this_mx, HKEY("smtps://")))) ? "" : "smtp://"),
257 CtdlGetConfigStr("c_fqdn")
259 curl_easy_setopt(curl, CURLOPT_URL, smtp_url);
260 syslog(LOG_DEBUG, "smtpclient: trying %s", smtp_url); // send the message
261 res = curl_easy_perform(curl);
262 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
263 syslog(LOG_DEBUG, "smtpclient: libcurl returned %d (%s) , SMTP response %ld",
264 res, curl_easy_strerror(res), response_code
267 if ((res != CURLE_OK) && (response_code == 0)) { // check for errors
271 curl_slist_free_all(recipients);
272 recipients = NULL; // this gets reused; avoid double-free
273 curl_easy_cleanup(curl);
274 curl = NULL; // this gets reused; avoid double-free
278 FreeStrBuf(&s.TheMessage);
279 if (fromaddr) free(fromaddr);
280 return((int)response_code);
285 * Process one outbound message.
287 void smtp_process_one_msg(long qmsgnum)
289 struct CtdlMessage *msg = NULL;
296 int delete_this_queue = 0;
298 msg = CtdlFetchMessage(qmsgnum, 1, 1);
300 syslog(LOG_WARNING, "smtpclient: %ld does not exist", qmsgnum);
304 instr = msg->cm_fields[eMesageText];
305 msg->cm_fields[eMesageText] = NULL;
308 // if the queue message has any CRLF's convert them to LF's
310 while (crlf = strstr(instr, "\r\n"), crlf != NULL) {
311 strcpy(crlf, crlf+1);
314 // Strip out the headers and we are now left with just the instructions.
315 char *soi = strstr(instr, "\n\n");
317 strcpy(instr, soi+2);
321 time_t submitted = time(NULL);
322 time_t attempted = 0;
323 char *bounceto = NULL;
324 char *envelope_from = NULL;
327 for (i=0; i<num_tokens(instr, '\n'); ++i) {
328 extract_token(cfgline, instr, i, '\n', sizeof cfgline);
329 if (!strncasecmp(cfgline, HKEY("msgid|"))) msgid = atol(&cfgline[6]);
330 if (!strncasecmp(cfgline, HKEY("submitted|"))) submitted = atol(&cfgline[10]);
331 if (!strncasecmp(cfgline, HKEY("attempted|"))) attempted = atol(&cfgline[10]);
332 if (!strncasecmp(cfgline, HKEY("bounceto|"))) bounceto = strdup(&cfgline[9]);
333 if (!strncasecmp(cfgline, HKEY("envelope_from|"))) envelope_from = strdup(&cfgline[14]);
336 int should_try_now = 0;
337 if (attempted < submitted) { // If no attempts have been made yet, try now
340 else if ((attempted - submitted) <= 14400) {
341 if ((time(NULL) - attempted) > 1800) { // First four hours, retry every 30 minutes
346 if ((time(NULL) - attempted) > 14400) { // After that, retry once every 4 hours
351 if (should_try_now) {
352 syslog(LOG_DEBUG, "smtpclient: %ld attempting delivery now", qmsgnum);
353 StrBuf *NewInstr = NewStrBuf();
354 StrBufAppendPrintf(NewInstr, "Content-type: "SPOOLMIME"\n\n");
355 StrBufAppendPrintf(NewInstr, "msgid|%ld\n", msgid);
356 StrBufAppendPrintf(NewInstr, "submitted|%ld\n", submitted);
357 if (bounceto) StrBufAppendPrintf(NewInstr, "bounceto|%s\n", bounceto);
358 if (envelope_from) StrBufAppendPrintf(NewInstr, "envelope_from|%s\n", envelope_from);
360 for (i=0; i<num_tokens(instr, '\n'); ++i) {
361 extract_token(cfgline, instr, i, '\n', sizeof cfgline);
362 if (!strncasecmp(cfgline, HKEY("remote|"))) {
364 int previous_result = extract_int(cfgline, 2);
365 if ((previous_result == 0) || (previous_result == 4)) {
366 int new_result = 421;
367 extract_token(recp, cfgline, 1, '|', sizeof recp);
368 new_result = smtp_attempt_delivery(msgid, recp, envelope_from);
369 syslog(LOG_DEBUG, "smtpclient: recp: <%s> , result: %d (%s)", recp, new_result, smtpstatus(new_result));
370 if ((new_result / 100) == 2) {
374 if ((new_result / 100) == 5) {
380 StrBufAppendPrintf(NewInstr, "remote|%s|%ld|%ld (%s)\n",
381 recp, (new_result / 100) , new_result, smtpstatus(new_result)
388 StrBufAppendPrintf(NewInstr, "attempted|%ld\n", time(NULL));
390 // All deliveries have now been attempted. Now determine the disposition of this queue entry.
392 time_t age = time(NULL) - submitted;
393 syslog(LOG_DEBUG, "smtpclient: submission age: %ldd%ldh%ldm%lds", (age/86400) , ((age%86400)/3600) , ((age%3600)/60) , (age%60));
394 syslog(LOG_DEBUG, "smtpclient: num_success=%d , num_fail=%d , num_delayed=%d", num_success, num_fail, num_delayed);
396 // If there are permanent fails on this attempt, deliver a bounce to the user.
397 // The 5XX fails will be recorded in the rewritten queue, but they will be removed before the next attempt.
399 smtp_do_bounce(ChrPtr(NewInstr), SDB_BOUNCE_FATALS);
402 // If all deliveries have either succeeded or failed, we are finished with this queue entry.
404 if (num_delayed == 0) {
405 delete_this_queue = 1;
408 // If it's been more than five days, give up and tell the sender we #failed
410 else if ((time(NULL) - submitted) > SMTP_DELIVER_FAIL) {
411 smtp_do_bounce(ChrPtr(NewInstr), SDB_BOUNCE_ALL);
412 delete_this_queue = 1;
415 // If it's been more than four hours but less than five days, warn the sender that I've Been Delayed
417 else if ( ((attempted - submitted) < SMTP_DELIVER_WARN) && ((time(NULL) - submitted) >= SMTP_DELIVER_WARN) ) {
418 smtp_do_bounce(ChrPtr(NewInstr), SDB_WARN);
421 if (delete_this_queue) {
422 syslog(LOG_DEBUG, "smtpclient: %ld deleting", qmsgnum);
423 deletes[0] = qmsgnum;
425 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, deletes, 2, "");
426 FreeStrBuf(&NewInstr); // We have to free NewInstr here, no longer needed
429 // replace the old queue entry with the new one
430 syslog(LOG_DEBUG, "smtpclient: %ld rewriting", qmsgnum);
431 msg = convert_internet_message_buf(&NewInstr); // This function will free NewInstr for us
432 CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM, 0);
434 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, &qmsgnum, 1, "");
438 syslog(LOG_DEBUG, "smtpclient: %ld retry time not reached", qmsgnum);
441 if (bounceto != NULL) free(bounceto);
442 if (envelope_from != NULL) free(envelope_from);
448 * Callback for smtp_do_queue()
450 void smtp_add_msg(long msgnum, void *userdata) {
455 smtpq = malloc(smtpq_alloc * sizeof(long));
458 if (smtpq_alloc >= smtpq_count) {
460 smtpq = realloc(smtpq, (smtpq_alloc * sizeof(long)));
463 smtpq[smtpq_count++] = msgnum;
468 * Run through the queue sending out messages.
470 void smtp_do_queue(void) {
474 * This is a simple concurrency check to make sure only one smtpclient
475 * run is done at a time. We could do this with a mutex, but since we
476 * don't really require extremely fine granularity here, we'll do it
477 * with a static variable instead.
479 if (doing_smtpclient) return;
480 doing_smtpclient = 1;
482 syslog(LOG_DEBUG, "smtpclient: start queue run");
483 pthread_setspecific(MyConKey, (void *)&smtp_client_CC);
485 if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
486 syslog(LOG_WARNING, "Cannot find room <%s>", SMTP_SPOOLOUT_ROOM);
487 doing_smtpclient = 0;
491 // Put the queue in memory so we can close the db cursor
492 CtdlForEachMessage(MSGS_ALL, 0L, NULL, SPOOLMIME, NULL, smtp_add_msg, NULL);
494 // We are ready to run through the queue now.
495 for (i=0; i<smtpq_count; ++i) {
496 smtp_process_one_msg(smtpq[i]);
499 smtpq_count = 0; // don't free it, we will use this memory on the next run
500 doing_smtpclient = 0;
501 syslog(LOG_DEBUG, "smtpclient: end queue run");
508 CTDL_MODULE_INIT(smtpclient)
512 CtdlFillSystemContext(&smtp_client_CC, "SMTP_Send");
513 CtdlRegisterMessageHook(smtp_aftersave, EVT_AFTERSAVE);
514 CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER, PRIO_AGGR + 50);
515 smtp_init_spoolout();
518 /* return our module id for the log */