pthread_mutex_t ActiveQItemsLock;
HashList *ActiveQItems = NULL;
HashList *QItemHandlers = NULL;
+const unsigned short DefaultMXPort = 25;
int max_sessions_for_outbound_smtp = 500; /* how many sessions might be active till we stop adding more smtp jobs */
int ndelay_count = 50; /* every n queued messages we will sleep... */
int delay_msec = 5000; /* this many seconds. */
return 0;
}
-int CountActiveQueueEntries(OneQueItem *MyQItem)
+int CountActiveQueueEntries(OneQueItem *MyQItem, int before)
{
HashPos *It;
long len;
It = GetNewHashPos(MyQItem->MailQEntries, 0);
while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
{
+ int Active;
MailQEntry *ThisItem = vQE;
if (CheckQEntryActive(ThisItem))
{
ActiveDeliveries++;
- ThisItem->Active = 1;
+ Active = 1;
}
else
- ThisItem->Active = 0;
+ Active = 0;
+ if (before)
+ ThisItem->Active = Active;
+ else
+ ThisItem->StillActive = Active;
}
DeleteHashPos(&It);
return ActiveDeliveries;
/**
* this one has to have the context for loading the message via the redirect buffer...
*/
-StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n)
+StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n, char **Author, char **Address)
{
CitContext *CCC=CC;
StrBuf *SendMsg;
CtdlOutputMsg(MyQItem->MessageID,
MT_RFC822, HEADERS_ALL,
0, 1, NULL,
- (ESC_DOT|SUPPRESS_ENV_TO) );
+ (ESC_DOT|SUPPRESS_ENV_TO),
+ Author,
+ Address);
SendMsg = CCC->redirect_buffer;
CCC->redirect_buffer = NULL;
* instructions for "5" codes (permanent fatal errors) and produce/deliver
* a "bounce" message (delivery status notification).
*/
-void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt)
+void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt, ParsedURL *Relay)
{
static int seq = 0;
{
++num_bounces;
+ StrBufAppendBufPlain(Msg, HKEY(" "), 0);
StrBufAppendBuf(Msg, ThisItem->Recipient, 0);
StrBufAppendBufPlain(Msg, HKEY(": "), 0);
StrBufAppendBuf(Msg, ThisItem->StatusMessage, 0);
return;
}
+ if ((StrLength(MyQItem->SenderRoom) == 0) && MyQItem->HaveRelay) {
+ const char *RelayUrlStr = "[not found]";
+ /* one message that relaying is broken is enough; no extra room error message. */
+ StrBuf *RelayDetails = NewStrBuf();
+
+ if (Relay != NULL)
+ RelayUrlStr = ChrPtr(Relay->URL);
+
+ StrBufPrintf(RelayDetails,
+ "Relaying via %s failed permanently. \n Reason:\n%s\n Revalidate your relay configuration.",
+ RelayUrlStr,
+ ChrPtr(Msg));
+ CtdlAideMessage(ChrPtr(RelayDetails), "Relaying Failed");
+ FreeStrBuf(&RelayDetails);
+ }
+
boundary = NewStrBufPlain(HKEY("=_Citadel_Multipart_"));
StrBufAppendPrintf(boundary,
"%s_%04x%04x",
SMTPCM_syslog(LOG_DEBUG, "Done processing bounces\n");
}
+ParsedURL *LoadRelayUrls(OneQueItem *MyQItem,
+ char *Author,
+ char *Address)
+{
+ int nRelays = 0;
+ ParsedURL *RelayUrls = NULL;
+ char mxbuf[SIZ];
+ ParsedURL **Url = &MyQItem->URL;
+
+ nRelays = get_hosts(mxbuf, "fallbackhost");
+ if (nRelays > 0) {
+ StrBuf *All;
+ StrBuf *One;
+ const char *Pos = NULL;
+ All = NewStrBufPlain(mxbuf, -1);
+ One = NewStrBufPlain(NULL, StrLength(All) + 1);
+
+ while ((Pos != StrBufNOTNULL) &&
+ ((Pos == NULL) ||
+ !IsEmptyStr(Pos)))
+ {
+ StrBufExtract_NextToken(One, All, &Pos, '|');
+ if (!ParseURL(Url, One, DefaultMXPort)) {
+ SMTPC_syslog(LOG_DEBUG,
+ "Failed to parse: %s\n",
+ ChrPtr(One));
+ }
+ else {
+ (*Url)->IsRelay = 1;
+ MyQItem->HaveRelay = 1;
+ }
+ }
+ FreeStrBuf(&All);
+ FreeStrBuf(&One);
+ }
+ nRelays = get_hosts(mxbuf, "smarthost");
+ if (nRelays > 0) {
+ char *User;
+ StrBuf *All;
+ StrBuf *One;
+ const char *Pos = NULL;
+ All = NewStrBufPlain(mxbuf, -1);
+ One = NewStrBufPlain(NULL, StrLength(All) + 1);
+
+ while ((Pos != StrBufNOTNULL) &&
+ ((Pos == NULL) ||
+ !IsEmptyStr(Pos)))
+ {
+ StrBufExtract_NextToken(One, All, &Pos, '|');
+ User = strchr(ChrPtr(One), ' ');
+ if (User != NULL) {
+ if (!strcmp(User + 1, Author) ||
+ !strcmp(User + 1, Address))
+ StrBufCutAt(One, 0, User);
+ else {
+ MyQItem->HaveRelay = 1;
+ continue;
+ }
+ }
+ if (!ParseURL(Url, One, DefaultMXPort)) {
+ SMTPC_syslog(LOG_DEBUG,
+ "Failed to parse: %s\n",
+ ChrPtr(One));
+ }
+ else {
+ ///if (!Url->IsIP)) // todo dupe me fork ipv6
+ (*Url)->IsRelay = 1;
+ MyQItem->HaveRelay = 1;
+ }
+ }
+ FreeStrBuf(&All);
+ FreeStrBuf(&One);
+ }
+ return RelayUrls;
+}
/*
* smtp_do_procmsg()
*
time_t now;
int mynumsessions = num_sessions;
struct CtdlMessage *msg = NULL;
+ char *Author = NULL;
+ char *Address = NULL;
char *instr = NULL;
StrBuf *PlainQItem;
OneQueItem *MyQItem;
void *vQE;
long len;
const char *Key;
- int nRelays = 0;
- ParsedURL *RelayUrls = NULL;
int HaveBuffers = 0;
StrBuf *Msg =NULL;
return;
}
- {
- char mxbuf[SIZ];
- ParsedURL **Url = &MyQItem->URL;
- nRelays = get_hosts(mxbuf, "smarthost");
- if (nRelays > 0) {
- StrBuf *All;
- StrBuf *One;
- const char *Pos = NULL;
- All = NewStrBufPlain(mxbuf, -1);
- One = NewStrBufPlain(NULL, StrLength(All) + 1);
-
- while ((Pos != StrBufNOTNULL) &&
- ((Pos == NULL) ||
- !IsEmptyStr(Pos)))
- {
- StrBufExtract_NextToken(One, All, &Pos, '|');
- if (!ParseURL(Url, One, 25)) {
- SMTPC_syslog(LOG_DEBUG,
- "Failed to parse: %s\n",
- ChrPtr(One));
- }
- else {
- ///if (!Url->IsIP)) // todo dupe me fork ipv6
- Url = &(*Url)->Next;
- }
- }
- FreeStrBuf(&All);
- FreeStrBuf(&One);
- }
-
- Url = &MyQItem->FallBackHost;
- nRelays = get_hosts(mxbuf, "fallbackhost");
- if (nRelays > 0) {
- StrBuf *All;
- StrBuf *One;
- const char *Pos = NULL;
- All = NewStrBufPlain(mxbuf, -1);
- One = NewStrBufPlain(NULL, StrLength(All) + 1);
-
- while ((Pos != StrBufNOTNULL) &&
- ((Pos == NULL) ||
- !IsEmptyStr(Pos)))
- {
- StrBufExtract_NextToken(One, All, &Pos, '|');
- if (!ParseURL(Url, One, 25)) {
- SMTPC_syslog(LOG_DEBUG,
- "Failed to parse: %s\n",
- ChrPtr(One));
- }
- else
- Url = &(*Url)->Next;
- }
- FreeStrBuf(&All);
- FreeStrBuf(&One);
- }
- }
It = GetNewHashPos(MyQItem->MailQEntries, 0);
while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
DeleteHashPos(&It);
MyQItem->NotYetShutdownDeliveries =
- MyQItem->ActiveDeliveries = CountActiveQueueEntries(MyQItem);
+ MyQItem->ActiveDeliveries = CountActiveQueueEntries(MyQItem, 1);
/* failsafe against overload:
* will we exceed the limit set?
if (MyQItem->ActiveDeliveries > 0)
{
+ ParsedURL *RelayUrls = NULL;
int nActivated = 0;
int n = MsgCount++;
int m = MyQItem->ActiveDeliveries;
int i = 1;
- Msg = smtp_load_msg(MyQItem, n);
+
It = GetNewHashPos(MyQItem->MailQEntries, 0);
+
+ Msg = smtp_load_msg(MyQItem, n, &Author, &Address);
+ RelayUrls = LoadRelayUrls(MyQItem, Author, Address);
+ if ((RelayUrls == NULL) && MyQItem->HaveRelay) {
+
+ while ((i <= m) &&
+ (GetNextHashPos(MyQItem->MailQEntries,
+ It, &len, &Key, &vQE)))
+ {
+ int KeepBuffers = (i == m);
+ MailQEntry *ThisItem = vQE;
+ StrBufPrintf(ThisItem->StatusMessage,
+ "No relay configured matching %s / %s",
+ (Author != NULL)? Author : "",
+ (Address != NULL)? Address : "");
+ ThisItem->Status = 5;
+
+ nActivated++;
+
+ if (i > 1) n = MsgCount++;
+ syslog(LOG_INFO,
+ "SMTPC: giving up on <%ld> <%s> %d / %d \n",
+ MyQItem->MessageID,
+ ChrPtr(ThisItem->Recipient),
+ i,
+ m);
+ (*((int*) userdata)) ++;
+ smtp_try_one_queue_entry(MyQItem,
+ ThisItem,
+ Msg,
+ KeepBuffers,
+ n,
+ RelayUrls);
+
+ if (KeepBuffers) HaveBuffers = 1;
+
+ i++;
+ }
+ DeleteHashPos(&It);
+
+ return;
+ }
+ if (Author != NULL) free (Author);
+ if (Address != NULL) free (Address);
+
while ((i <= m) &&
(GetNextHashPos(MyQItem->MailQEntries,
It, &len, &Key, &vQE)))
* Run through the queue sending out messages.
*/
void smtp_do_queue(void) {
- static int is_running = 0;
int num_processed = 0;
int num_activated = 0;
- if (is_running)
- return; /* Concurrency check - only one can run */
- is_running = 1;
-
pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
SMTPCM_syslog(LOG_INFO, "processing outbound queue");
"queue run completed; %d messages processed %d activated",
num_processed, num_activated);
- run_queue_now = 0;
- is_running = 0;
}
CtdlRegisterEVCleanupHook(smtp_evq_cleanup);
CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
- CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
+ CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER, PRIO_SEND + 10);
}
/* return our Subversion id for the Log */