2 * This module is an SMTP and ESMTP implementation for the Citadel system.
3 * It is compliant with all of the following:
5 * RFC 821 - Simple Mail Transfer Protocol
6 * RFC 876 - Survey of SMTP Implementations
7 * RFC 1047 - Duplicate messages and SMTP
8 * RFC 1652 - 8 bit MIME
9 * RFC 1869 - Extended Simple Mail Transfer Protocol
10 * RFC 1870 - SMTP Service Extension for Message Size Declaration
11 * RFC 2033 - Local Mail Transfer Protocol
12 * RFC 2197 - SMTP Service Extension for Command Pipelining
13 * RFC 2476 - Message Submission
14 * RFC 2487 - SMTP Service Extension for Secure SMTP over TLS
15 * RFC 2554 - SMTP Service Extension for Authentication
16 * RFC 2821 - Simple Mail Transfer Protocol
17 * RFC 2822 - Internet Message Format
18 * RFC 2920 - SMTP Service Extension for Command Pipelining
20 * The VRFY and EXPN commands have been removed from this implementation
21 * because nobody uses these commands anymore, except for spammers.
23 * Copyright (c) 1998-2012 by the citadel.org team
25 * This program is open source software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License version 3.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
49 #include <sys/types.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
72 #include "citserver.h"
79 #include "internet_addressing.h"
82 #include "clientsocket.h"
83 #include "locate_host.h"
84 #include "citadel_dirs.h"
86 #include "ctdl_module.h"
88 #include "smtpqueue.h"
89 #include "event_client.h"
92 struct CitContext smtp_queue_CC;
93 pthread_mutex_t ActiveQItemsLock;
94 HashList *ActiveQItems = NULL;
95 HashList *QItemHandlers = NULL;
96 int max_sessions_for_outbound_smtp = 500; /* how many sessions might be active till we stop adding more smtp jobs */
97 int ndelay_count = 50; /* every n queued messages we will sleep... */
98 int delay_msec = 5000; /* this many seconds. */
100 static const long MaxRetry = SMTP_RETRY_INTERVAL * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2;
102 int run_queue_now = 0; /* Set to 1 to ignore SMTP send retry times */
104 void RegisterQItemHandler(const char *Key, long Len, QItemHandler H)
106 QItemHandlerStruct *HS = (QItemHandlerStruct*)malloc(sizeof(QItemHandlerStruct));
108 Put(QItemHandlers, Key, Len, HS, NULL);
113 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
114 MailQEntry *MyQEntry,
116 /* KeepMsgText allows us to use MsgText as ours. */
119 ParsedURL *RelayUrls);
122 void smtp_evq_cleanup(void)
125 pthread_mutex_lock(&ActiveQItemsLock);
126 DeleteHash(&QItemHandlers);
127 DeleteHash(&ActiveQItems);
128 pthread_mutex_unlock(&ActiveQItemsLock);
129 pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
130 /* citthread_mutex_destroy(&ActiveQItemsLock); TODO */
133 int DecreaseQReference(OneQueItem *MyQItem)
135 int IDestructQueItem;
137 pthread_mutex_lock(&ActiveQItemsLock);
138 MyQItem->ActiveDeliveries--;
139 IDestructQueItem = MyQItem->ActiveDeliveries == 0;
140 pthread_mutex_unlock(&ActiveQItemsLock);
141 return IDestructQueItem;
144 void DecreaseShutdownDeliveries(OneQueItem *MyQItem)
146 pthread_mutex_lock(&ActiveQItemsLock);
147 MyQItem->NotYetShutdownDeliveries--;
148 pthread_mutex_unlock(&ActiveQItemsLock);
151 int GetShutdownDeliveries(OneQueItem *MyQItem)
155 pthread_mutex_lock(&ActiveQItemsLock);
156 DestructNow = MyQItem->ActiveDeliveries == 0;
157 pthread_mutex_unlock(&ActiveQItemsLock);
160 void RemoveQItem(OneQueItem *MyQItem)
167 pthread_mutex_lock(&ActiveQItemsLock);
168 It = GetNewHashPos(ActiveQItems, 0);
169 if (GetHashPosFromKey(ActiveQItems, LKEY(MyQItem->MessageID), It))
170 DeleteEntryFromHash(ActiveQItems, It);
174 "SMTP cleanup: unable to find QItem with ID[%ld]",
176 while (GetNextHashPos(ActiveQItems, It, &len, &Key, &VData))
178 "SMTP cleanup: have_: ID[%ld]",
179 ((OneQueItem *)VData)->MessageID);
181 pthread_mutex_unlock(&ActiveQItemsLock);
186 void FreeMailQEntry(void *qv)
190 syslog(LOG_DEBUG, "---------------%s--------------", __FUNCTION__);
193 FreeStrBuf(&Q->Recipient);
194 FreeStrBuf(&Q->StatusMessage);
196 memset(Q, 0, sizeof(MailQEntry));
199 void FreeQueItem(OneQueItem **Item)
202 syslog(LOG_DEBUG, "---------------%s--------------", __FUNCTION__);
205 DeleteHash(&(*Item)->MailQEntries);
206 FreeStrBuf(&(*Item)->EnvelopeFrom);
207 FreeStrBuf(&(*Item)->BounceTo);
208 FreeStrBuf(&(*Item)->SenderRoom);
209 FreeURL(&(*Item)->URL);
210 memset(*Item, 0, sizeof(OneQueItem));
214 void HFreeQueItem(void *Item)
216 FreeQueItem((OneQueItem**)&Item);
219 /* inspect recipients with a status of:
220 * - 0 (no delivery yet attempted)
221 * - 3/4 (transient errors
222 * were experienced and it's time to try again)
224 int CheckQEntryActive(MailQEntry *ThisItem)
226 if ((ThisItem->Status == 0) ||
227 (ThisItem->Status == 3) ||
228 (ThisItem->Status == 4))
235 int CheckQEntryIsBounce(MailQEntry *ThisItem)
237 if ((ThisItem->Status == 3) ||
238 (ThisItem->Status == 4) ||
239 (ThisItem->Status == 5))
247 int CountActiveQueueEntries(OneQueItem *MyQItem)
251 long ActiveDeliveries;
255 ActiveDeliveries = 0;
256 It = GetNewHashPos(MyQItem->MailQEntries, 0);
257 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
259 MailQEntry *ThisItem = vQE;
261 if (CheckQEntryActive(ThisItem))
264 ThisItem->Active = 1;
267 ThisItem->Active = 0;
270 return ActiveDeliveries;
273 OneQueItem *DeserializeQueueItem(StrBuf *RawQItem, long QueMsgID)
276 const char *pLine = NULL;
281 Item = (OneQueItem*)malloc(sizeof(OneQueItem));
282 memset(Item, 0, sizeof(OneQueItem));
283 Item->Retry = SMTP_RETRY_INTERVAL;
284 Item->MessageID = -1;
285 Item->QueMsgID = QueMsgID;
288 Line = NewStrBufPlain(NULL, 128);
289 while (pLine != StrBufNOTNULL) {
290 const char *pItemPart = NULL;
293 StrBufExtract_NextToken(Line, RawQItem, &pLine, '\n');
294 if (StrLength(Line) == 0) continue;
295 StrBufExtract_NextToken(Token, Line, &pItemPart, '|');
296 if (GetHash(QItemHandlers, SKEY(Token), &vHandler))
298 QItemHandlerStruct *HS;
299 HS = (QItemHandlerStruct*) vHandler;
300 HS->H(Item, Line, &pItemPart);
306 if (Item->Retry >= MaxRetry)
309 pthread_mutex_lock(&ActiveQItemsLock);
310 if (GetHash(ActiveQItems,
311 LKEY(Item->MessageID),
314 /* WHOOPS. somebody else is already working on this. */
315 pthread_mutex_unlock(&ActiveQItemsLock);
320 /* mark our claim on this. */
322 LKEY(Item->MessageID),
325 pthread_mutex_unlock(&ActiveQItemsLock);
331 StrBuf *SerializeQueueItem(OneQueItem *MyQItem)
339 QMessage = NewStrBufPlain(NULL, SIZ);
340 StrBufPrintf(QMessage, "Content-type: %s\n", SPOOLMIME);
341 // "attempted|%ld\n" "retry|%ld\n",, (long)time(NULL), (long)retry );
342 StrBufAppendBufPlain(QMessage, HKEY("\nmsgid|"), 0);
343 StrBufAppendPrintf(QMessage, "%ld", MyQItem->MessageID);
345 StrBufAppendBufPlain(QMessage, HKEY("\nsubmitted|"), 0);
346 StrBufAppendPrintf(QMessage, "%ld", MyQItem->Submitted);
348 if (StrLength(MyQItem->BounceTo) > 0) {
349 StrBufAppendBufPlain(QMessage, HKEY("\nbounceto|"), 0);
350 StrBufAppendBuf(QMessage, MyQItem->BounceTo, 0);
353 if (StrLength(MyQItem->EnvelopeFrom) > 0) {
354 StrBufAppendBufPlain(QMessage, HKEY("\nenvelope_from|"), 0);
355 StrBufAppendBuf(QMessage, MyQItem->EnvelopeFrom, 0);
358 if (StrLength(MyQItem->SenderRoom) > 0) {
359 StrBufAppendBufPlain(QMessage, HKEY("\nsource_room|"), 0);
360 StrBufAppendBuf(QMessage, MyQItem->SenderRoom, 0);
363 StrBufAppendBufPlain(QMessage, HKEY("\nretry|"), 0);
364 StrBufAppendPrintf(QMessage, "%ld",
367 StrBufAppendBufPlain(QMessage, HKEY("\nattempted|"), 0);
368 StrBufAppendPrintf(QMessage, "%ld",
369 time(NULL) /*ctdl_ev_now()*/ + MyQItem->Retry);
371 It = GetNewHashPos(MyQItem->MailQEntries, 0);
372 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
374 MailQEntry *ThisItem = vQE;
376 StrBufAppendBufPlain(QMessage, HKEY("\nremote|"), 0);
377 StrBufAppendBuf(QMessage, ThisItem->Recipient, 0);
378 StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
379 StrBufAppendPrintf(QMessage, "%d", ThisItem->Status);
380 StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
381 StrBufAppendBuf(QMessage, ThisItem->StatusMessage, 0);
384 StrBufAppendBufPlain(QMessage, HKEY("\n"), 0);
392 void NewMailQEntry(OneQueItem *Item)
394 Item->Current = (MailQEntry*) malloc(sizeof(MailQEntry));
395 memset(Item->Current, 0, sizeof(MailQEntry));
397 if (Item->MailQEntries == NULL)
398 Item->MailQEntries = NewHash(1, Flathash);
399 Item->Current->StatusMessage = NewStrBuf();
400 Item->Current->n = GetCount(Item->MailQEntries);
401 Put(Item->MailQEntries,
402 IKEY(Item->Current->n),
407 void QItem_Handle_MsgID(OneQueItem *Item, StrBuf *Line, const char **Pos)
409 Item->MessageID = StrBufExtractNext_long(Line, Pos, '|');
412 void QItem_Handle_EnvelopeFrom(OneQueItem *Item, StrBuf *Line, const char **Pos)
414 if (Item->EnvelopeFrom == NULL)
415 Item->EnvelopeFrom = NewStrBufPlain(NULL, StrLength(Line));
416 StrBufExtract_NextToken(Item->EnvelopeFrom, Line, Pos, '|');
419 void QItem_Handle_BounceTo(OneQueItem *Item, StrBuf *Line, const char **Pos)
421 if (Item->BounceTo == NULL)
422 Item->BounceTo = NewStrBufPlain(NULL, StrLength(Line));
423 StrBufExtract_NextToken(Item->BounceTo, Line, Pos, '|');
426 void QItem_Handle_SenderRoom(OneQueItem *Item, StrBuf *Line, const char **Pos)
428 if (Item->SenderRoom == NULL)
429 Item->SenderRoom = NewStrBufPlain(NULL, StrLength(Line));
430 StrBufExtract_NextToken(Item->SenderRoom, Line, Pos, '|');
433 void QItem_Handle_Recipient(OneQueItem *Item, StrBuf *Line, const char **Pos)
435 if (Item->Current == NULL)
437 if (Item->Current->Recipient == NULL)
438 Item->Current->Recipient=NewStrBufPlain(NULL, StrLength(Line));
439 StrBufExtract_NextToken(Item->Current->Recipient, Line, Pos, '|');
440 Item->Current->Status = StrBufExtractNext_int(Line, Pos, '|');
441 StrBufExtract_NextToken(Item->Current->StatusMessage, Line, Pos, '|');
442 Item->Current = NULL; // TODO: is this always right?
446 void QItem_Handle_retry(OneQueItem *Item, StrBuf *Line, const char **Pos)
449 StrBufExtractNext_int(Line, Pos, '|');
450 if (Item->Retry == 0)
451 Item->Retry = SMTP_RETRY_INTERVAL;
457 void QItem_Handle_Submitted(OneQueItem *Item, StrBuf *Line, const char **Pos)
459 Item->Submitted = atol(*Pos);
463 void QItem_Handle_Attempted(OneQueItem *Item, StrBuf *Line, const char **Pos)
465 Item->ReattemptWhen = StrBufExtractNext_int(Line, Pos, '|');
471 * this one has to have the context for loading the message via the redirect buffer...
473 StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n)
478 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
479 CtdlOutputMsg(MyQItem->MessageID,
480 MT_RFC822, HEADERS_ALL,
482 (ESC_DOT|SUPPRESS_ENV_TO) );
484 SendMsg = CCC->redirect_buffer;
485 CCC->redirect_buffer = NULL;
486 if ((StrLength(SendMsg) > 0) &&
487 ChrPtr(SendMsg)[StrLength(SendMsg) - 1] != '\n') {
489 "SMTP client[%d]: Possible problem: message did not "
490 "correctly terminate. (expecting 0x10, got 0x%02x)\n",
491 MsgCount, //yes uncool, but best choice here...
492 ChrPtr(SendMsg)[StrLength(SendMsg) - 1] );
493 StrBufAppendBufPlain(SendMsg, HKEY("\r\n"), 0);
501 * smtp_do_bounce() is caled by smtp_do_procmsg() to scan a set of delivery
502 * instructions for "5" codes (permanent fatal errors) and produce/deliver
503 * a "bounce" message (delivery status notification).
505 void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt)
509 struct CtdlMessage *bmsg = NULL;
513 struct recptypes *valid;
521 int first_attempt = 0;
522 int successful_bounce = 0;
526 syslog(LOG_DEBUG, "smtp_do_bounce() called\n");
528 if (MyQItem->SendBounceMail == 0)
531 now = time (NULL); //ev_time();
533 if ( (now - MyQItem->Submitted) > SMTP_GIVE_UP ) {
537 if (MyQItem->Retry == SMTP_RETRY_INTERVAL) {
542 * Now go through the instructions checking for stuff.
544 Msg = NewStrBufPlain(NULL, 1024);
545 It = GetNewHashPos(MyQItem->MailQEntries, 0);
546 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
548 MailQEntry *ThisItem = vQE;
549 if ((ThisItem->Active && (ThisItem->Status == 5)) || /* failed now? */
550 ((give_up == 1) && (ThisItem->Status != 2)) ||
551 ((first_attempt == 1) && (ThisItem->Status != 2)))
552 /* giving up after failed attempts... */
556 StrBufAppendBuf(Msg, ThisItem->Recipient, 0);
557 StrBufAppendBufPlain(Msg, HKEY(": "), 0);
558 StrBufAppendBuf(Msg, ThisItem->StatusMessage, 0);
559 StrBufAppendBufPlain(Msg, HKEY("\r\n"), 0);
564 /* Deliver the bounce if there's anything worth mentioning */
565 syslog(LOG_DEBUG, "num_bounces = %d\n", num_bounces);
567 if (num_bounces == 0) {
572 boundary = NewStrBufPlain(HKEY("=_Citadel_Multipart_"));
573 StrBufAppendPrintf(boundary,
579 /* Start building our bounce message; go shopping for memory first. */
580 BounceMB = NewStrBufPlain(
582 1024 + /* mime stuff.... */
583 StrLength(Msg) + /* the bounce information... */
584 StrLength(OMsgTxt)); /* the original message */
585 if (BounceMB == NULL) {
586 FreeStrBuf(&boundary);
587 syslog(LOG_ERR, "Failed to alloc() bounce message.\n");
592 bmsg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
594 FreeStrBuf(&boundary);
595 FreeStrBuf(&BounceMB);
596 syslog(LOG_ERR, "Failed to alloc() bounce message.\n");
600 memset(bmsg, 0, sizeof(struct CtdlMessage));
603 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: multipart/mixed; boundary=\""), 0);
604 StrBufAppendBuf(BounceMB, boundary, 0);
605 StrBufAppendBufPlain(BounceMB, HKEY("\"\r\n"), 0);
606 StrBufAppendBufPlain(BounceMB, HKEY("MIME-Version: 1.0\r\n"), 0);
607 StrBufAppendBufPlain(BounceMB, HKEY("X-Mailer: " CITADEL "\r\n"), 0);
608 StrBufAppendBufPlain(BounceMB, HKEY("\r\nThis is a multipart message in MIME format.\r\n\r\n"), 0);
609 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
610 StrBufAppendBuf(BounceMB, boundary, 0);
611 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
612 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: text/plain\r\n\r\n"), 0);
615 StrBufAppendBufPlain(
618 "A message you sent could not be delivered "
619 "to some or all of its recipients\n"
620 "due to prolonged unavailability "
621 "of its destination(s).\n"
622 "Giving up on the following addresses:\n\n"
625 StrBufAppendBufPlain(
628 "A message you sent could not be delivered "
629 "to some or all of its recipients.\n"
630 "The following addresses "
631 "were undeliverable:\n\n"
634 StrBufAppendBuf(BounceMB, Msg, 0);
637 if (StrLength(MyQItem->SenderRoom) > 0)
639 StrBufAppendBufPlain(
641 HKEY("The message was originaly posted in: "), 0);
642 StrBufAppendBuf(BounceMB, MyQItem->SenderRoom, 0);
643 StrBufAppendBufPlain(
648 /* Attach the original message */
649 StrBufAppendBufPlain(BounceMB, HKEY("\r\n--"), 0);
650 StrBufAppendBuf(BounceMB, boundary, 0);
651 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
652 StrBufAppendBufPlain(BounceMB,
653 HKEY("Content-type: message/rfc822\r\n"), 0);
654 StrBufAppendBufPlain(BounceMB,
655 HKEY("Content-Transfer-Encoding: 7bit\r\n"), 0);
656 StrBufAppendBufPlain(BounceMB,
657 HKEY("Content-Disposition: inline\r\n"), 0);
658 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
659 StrBufAppendBuf(BounceMB, OMsgTxt, 0);
661 /* Close the multipart MIME scope */
662 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
663 StrBufAppendBuf(BounceMB, boundary, 0);
664 StrBufAppendBufPlain(BounceMB, HKEY("--\r\n"), 0);
666 bmsg->cm_magic = CTDLMESSAGE_MAGIC;
667 bmsg->cm_anon_type = MES_NORMAL;
668 bmsg->cm_format_type = FMT_RFC822;
670 bmsg->cm_fields['O'] = strdup(MAILROOM);
671 bmsg->cm_fields['A'] = strdup("Citadel");
672 bmsg->cm_fields['N'] = strdup(config.c_nodename);
673 bmsg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
674 bmsg->cm_fields['M'] = SmashStrBuf(&BounceMB);
676 /* First try the user who sent the message */
677 if (StrLength(MyQItem->BounceTo) == 0)
678 syslog(LOG_ERR, "No bounce address specified\n");
680 syslog(LOG_DEBUG, "bounce to user? <%s>\n",
681 ChrPtr(MyQItem->BounceTo));
683 /* Can we deliver the bounce to the original sender? */
684 valid = validate_recipients(ChrPtr(MyQItem->BounceTo), NULL, 0);
685 if ((valid != NULL) && (valid->num_error == 0)) {
686 CtdlSubmitMsg(bmsg, valid, "", QP_EADDR);
687 successful_bounce = 1;
690 /* If not, post it in the Aide> room */
691 if (successful_bounce == 0) {
692 CtdlSubmitMsg(bmsg, NULL, config.c_aideroom, QP_EADDR);
695 /* Free up the memory we used */
696 free_recipients(valid);
697 FreeStrBuf(&boundary);
698 CtdlFreeMessage(bmsg);
699 syslog(LOG_DEBUG, "Done processing bounces\n");
705 * Called by smtp_do_queue() to handle an individual message.
707 void smtp_do_procmsg(long msgnum, void *userdata) {
709 int mynumsessions = num_sessions;
710 struct CtdlMessage *msg = NULL;
720 ParsedURL *RelayUrls = NULL;
724 if (mynumsessions > max_sessions_for_outbound_smtp) {
726 "SMTP Queue: skipping because of num jobs %d > %d max_sessions_for_outbound_smtp",
728 max_sessions_for_outbound_smtp);
731 syslog(LOG_DEBUG, "SMTP Queue: smtp_do_procmsg(%ld)\n", msgnum);
732 ///strcpy(envelope_from, "");
734 msg = CtdlFetchMessage(msgnum, 1);
736 syslog(LOG_ERR, "SMTP Queue: tried %ld but no such message!\n",
741 pch = instr = msg->cm_fields['M'];
743 /* Strip out the headers (no not amd any other non-instruction) line */
744 while (pch != NULL) {
745 pch = strchr(pch, '\n');
746 if ((pch != NULL) && (*(pch + 1) == '\n')) {
751 PlainQItem = NewStrBufPlain(instr, -1);
752 CtdlFreeMessage(msg);
753 MyQItem = DeserializeQueueItem(PlainQItem, msgnum);
754 FreeStrBuf(&PlainQItem);
756 if (MyQItem == NULL) {
758 "SMTP Queue: Msg No %ld: already in progress!\n",
760 return; /* s.b. else is already processing... */
764 * Postpone delivery if we've already tried recently.
767 if ((MyQItem->ReattemptWhen != 0) &&
768 (now < MyQItem->ReattemptWhen) &&
769 (run_queue_now == 0))
771 syslog(LOG_DEBUG, "SMTP client: Retry time not yet reached. %ld seconds left.", MyQItem->ReattemptWhen - now);
773 It = GetNewHashPos(MyQItem->MailQEntries, 0);
774 pthread_mutex_lock(&ActiveQItemsLock);
776 if (GetHashPosFromKey(ActiveQItems,
777 LKEY(MyQItem->MessageID),
780 DeleteEntryFromHash(ActiveQItems, It);
783 pthread_mutex_unlock(&ActiveQItemsLock);
784 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
790 * Bail out if there's no actual message associated with this
792 if (MyQItem->MessageID < 0L) {
793 syslog(LOG_ERR, "SMTP Queue: no 'msgid' directive found!\n");
794 It = GetNewHashPos(MyQItem->MailQEntries, 0);
795 pthread_mutex_lock(&ActiveQItemsLock);
797 if (GetHashPosFromKey(ActiveQItems,
798 LKEY(MyQItem->MessageID),
801 DeleteEntryFromHash(ActiveQItems, It);
804 pthread_mutex_unlock(&ActiveQItemsLock);
806 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
812 ParsedURL **Url = &MyQItem->URL;
813 nRelays = get_hosts(mxbuf, "smarthost");
817 const char *Pos = NULL;
818 All = NewStrBufPlain(mxbuf, -1);
819 One = NewStrBufPlain(NULL, StrLength(All) + 1);
821 while ((Pos != StrBufNOTNULL) &&
825 StrBufExtract_NextToken(One, All, &Pos, '|');
826 if (!ParseURL(Url, One, 25))
828 "Failed to parse: %s\n",
831 ///if (!Url->IsIP)) // todo dupe me fork ipv6
839 Url = &MyQItem->FallBackHost;
840 nRelays = get_hosts(mxbuf, "fallbackhost");
844 const char *Pos = NULL;
845 All = NewStrBufPlain(mxbuf, -1);
846 One = NewStrBufPlain(NULL, StrLength(All) + 1);
848 while ((Pos != StrBufNOTNULL) &&
852 StrBufExtract_NextToken(One, All, &Pos, '|');
853 if (!ParseURL(Url, One, 25))
855 "Failed to parse: %s\n",
865 It = GetNewHashPos(MyQItem->MailQEntries, 0);
866 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
868 MailQEntry *ThisItem = vQE;
869 syslog(LOG_DEBUG, "SMTP Queue: Task: <%s> %d\n",
870 ChrPtr(ThisItem->Recipient),
875 MyQItem->NotYetShutdownDeliveries =
876 MyQItem->ActiveDeliveries = CountActiveQueueEntries(MyQItem);
878 /* failsafe against overload:
879 * will we exceed the limit set?
881 if ((MyQItem->ActiveDeliveries + mynumsessions > max_sessions_for_outbound_smtp) &&
882 /* if yes, did we reach more than half of the quota? */
883 ((mynumsessions * 2) > max_sessions_for_outbound_smtp) &&
884 /* if... would we ever fit into half of the quota?? */
885 (((MyQItem->ActiveDeliveries * 2) < max_sessions_for_outbound_smtp)))
887 /* abort delivery for another time. */
889 "SMTP Queue: skipping because of num jobs %d + %ld > %d max_sessions_for_outbound_smtp",
891 MyQItem->ActiveDeliveries,
892 max_sessions_for_outbound_smtp);
894 FreeQueItem(&MyQItem);
900 if (MyQItem->ActiveDeliveries > 0)
904 int m = MyQItem->ActiveDeliveries;
906 Msg = smtp_load_msg(MyQItem, n);
907 It = GetNewHashPos(MyQItem->MailQEntries, 0);
909 (GetNextHashPos(MyQItem->MailQEntries,
910 It, &len, &Key, &vQE)))
912 MailQEntry *ThisItem = vQE;
914 if (ThisItem->Active == 1)
916 int KeepBuffers = (i == m);
919 if (nActivated % ndelay_count == 0)
922 if (i > 1) n = MsgCount++;
924 "SMTPQ: Trying <%ld> <%s> %d / %d \n",
926 ChrPtr(ThisItem->Recipient),
929 smtp_try_one_queue_entry(MyQItem,
936 if (KeepBuffers) HaveBuffers = 1;
945 It = GetNewHashPos(MyQItem->MailQEntries, 0);
946 pthread_mutex_lock(&ActiveQItemsLock);
948 if (GetHashPosFromKey(ActiveQItems,
949 LKEY(MyQItem->MessageID),
952 DeleteEntryFromHash(ActiveQItems, It);
961 "SMTP cleanup: unable to find "
962 "QItem with ID[%ld]",
964 while (GetNextHashPos(ActiveQItems,
971 "SMTP cleanup: have: ID[%ld]",
972 ((OneQueItem *)VData)->MessageID);
977 pthread_mutex_unlock(&ActiveQItemsLock);
979 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
981 // TODO: bounce & delete?
986 // TODO : free RelayUrls
993 * smtp_queue_thread()
995 * Run through the queue sending out messages.
997 void smtp_do_queue(void) {
998 static int is_running = 0;
999 int num_processed = 0;
1002 return; /* Concurrency check - only one can run */
1005 pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
1006 syslog(LOG_INFO, "SMTP client: processing outbound queue");
1008 if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
1009 syslog(LOG_ERR, "Cannot find room <%s>", SMTP_SPOOLOUT_ROOM);
1012 num_processed = CtdlForEachMessage(MSGS_ALL,
1021 "SMTP client: queue run completed; %d messages processed",
1031 * Initialize the SMTP outbound queue
1033 void smtp_init_spoolout(void) {
1034 struct ctdlroom qrbuf;
1037 * Create the room. This will silently fail if the room already
1038 * exists, and that's perfectly ok, because we want it to exist.
1040 CtdlCreateRoom(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1, 0, VIEW_QUEUE);
1043 * Make sure it's set to be a "system room" so it doesn't show up
1044 * in the <K>nown rooms list for Aides.
1046 if (CtdlGetRoomLock(&qrbuf, SMTP_SPOOLOUT_ROOM) == 0) {
1047 qrbuf.QRflags2 |= QR2_SYSTEM;
1048 CtdlPutRoomLock(&qrbuf);
1055 /*****************************************************************************/
1056 /* SMTP UTILITY COMMANDS */
1057 /*****************************************************************************/
1059 void cmd_smtp(char *argbuf) {
1066 if (CtdlAccessCheck(ac_aide)) return;
1068 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
1070 if (!strcasecmp(cmd, "mx")) {
1071 extract_token(node, argbuf, 1, '|', sizeof node);
1072 num_mxhosts = getmx(buf, node);
1073 cprintf("%d %d MX hosts listed for %s\n",
1074 LISTING_FOLLOWS, num_mxhosts, node);
1075 for (i=0; i<num_mxhosts; ++i) {
1076 extract_token(node, buf, i, '|', sizeof node);
1077 cprintf("%s\n", node);
1083 else if (!strcasecmp(cmd, "runqueue")) {
1085 cprintf("%d All outbound SMTP will be retried now.\n", CIT_OK);
1090 cprintf("%d Invalid command.\n", ERROR + ILLEGAL_VALUE);
1096 CTDL_MODULE_INIT(smtp_queu)
1102 pstr = getenv("CITSERVER_n_session_max");
1103 if ((pstr != NULL) && (*pstr != '\0'))
1104 max_sessions_for_outbound_smtp = atol(pstr); /* how many sessions might be active till we stop adding more smtp jobs */
1106 pstr = getenv("CITSERVER_smtp_n_delay_count");
1107 if ((pstr != NULL) && (*pstr != '\0'))
1108 ndelay_count = atol(pstr); /* every n queued messages we will sleep... */
1110 pstr = getenv("CITSERVER_smtp_delay");
1111 if ((pstr != NULL) && (*pstr != '\0'))
1112 delay_msec = atol(pstr) * 1000; /* this many seconds. */
1117 CtdlFillSystemContext(&smtp_queue_CC, "SMTP_Send");
1118 ActiveQItems = NewHash(1, lFlathash);
1119 pthread_mutex_init(&ActiveQItemsLock, NULL);
1121 QItemHandlers = NewHash(0, NULL);
1123 RegisterQItemHandler(HKEY("msgid"), QItem_Handle_MsgID);
1124 RegisterQItemHandler(HKEY("envelope_from"), QItem_Handle_EnvelopeFrom);
1125 RegisterQItemHandler(HKEY("retry"), QItem_Handle_retry);
1126 RegisterQItemHandler(HKEY("attempted"), QItem_Handle_Attempted);
1127 RegisterQItemHandler(HKEY("remote"), QItem_Handle_Recipient);
1128 RegisterQItemHandler(HKEY("bounceto"), QItem_Handle_BounceTo);
1129 RegisterQItemHandler(HKEY("source_room"), QItem_Handle_SenderRoom);
1130 RegisterQItemHandler(HKEY("submitted"), QItem_Handle_Submitted);
1132 smtp_init_spoolout();
1134 CtdlRegisterEVCleanupHook(smtp_evq_cleanup);
1136 CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
1137 CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
1140 /* return our Subversion id for the Log */
1141 return "smtpeventclient";