commit 82ec9787f6d1067bbfc4b3bea59d8ed43c9e44ab
parent e61e9986ca9216168ca1dd1bd14f3963800daaa0
Author: Amelia Bjornsdottir <amelia@chatspeed.net>
Date: Sun, 26 Sep 2021 21:32:23 +0000
applied the IPv6 ipme patch, removed the dead-end libmxf codebase, did some hair splitting for mxf-send and mxf-remote (not building yet)
Diffstat:
29 files changed, 1924 insertions(+), 211 deletions(-)
diff --git a/conf-cc b/conf-cc
@@ -1,3 +1,3 @@
-cc -O3 -Iinclude/ -Iinclude/fmxs
+cc -O3 -Iinclude/ -Iinclude/fmxs -I/package/prog/skalibs/include
This will be used to compile .c files.
diff --git a/conf-ld b/conf-ld
@@ -1,3 +1,3 @@
-cc
+cc /package/prog/skalibs/library/libskarnet.a
This will be used to link .o files into an executable.
diff --git a/include/ip.h b/include/ip.h
@@ -2,6 +2,7 @@
#define IP_H
struct ip_address { unsigned char d[4]; } ;
+struct ip6_address { unsigned char d[16]; } ;
extern unsigned int ip_fmt();
#define IPFMT 19
diff --git a/include/ipalloc.h b/include/ipalloc.h
@@ -4,11 +4,15 @@
#include "ip.h"
struct ip_mx { struct ip_address ip; int pref; } ;
+struct ip6_mx { struct ip6_address ip; int pref; } ;
#include "gen_alloc.h"
GEN_ALLOC_typedef(ipalloc,struct ip_mx,ix,len,a)
+GEN_ALLOC_typedef(ip6alloc,struct ip6_mx,ix,len,a)
extern int ipalloc_readyplus();
extern int ipalloc_append();
+extern int ip6alloc_readyplus();
+extern int ip6alloc_append();
#endif
diff --git a/include/ipme.h b/include/ipme.h
@@ -5,6 +5,7 @@
#include "ipalloc.h"
extern ipalloc ipme;
+extern ip6alloc ip6me;
extern int ipme_init();
extern int ipme_is();
diff --git a/include/stralloc.h b/include/stralloc.h
@@ -4,6 +4,7 @@
#include "gen_alloc.h"
GEN_ALLOC_typedef(stralloc,char,s,len,a)
+#define STRALLOC_ZERO {0, 0, 0}
extern int stralloc_ready();
extern int stralloc_readyplus();
diff --git a/src/commands.c b/src/commands.c
@@ -1,16 +1,34 @@
#include "commands.h"
+#ifndef USING_SKALIBS
#include "substdio.h"
#include "stralloc.h"
+#else
+#include <skalibs/buffer.h>
+#include <skalibs/stralloc.h>
+#endif
#include "str.h"
#include "case.h"
+#ifdef USING_SKALIBS
+static stralloc cmd = STRALLOC_ZERO;
+#else
static stralloc cmd = {0};
+#endif
+// XXX: not instantiable.
+
+#ifdef USING_SKALIBS
+int commands (
+ buffer *bio,
+ struct commands *c
+) {
+#else
int commands (
substdio *ss,
struct commands *c
) {
+#endif
unsigned int i;
char *arg;
@@ -24,13 +42,20 @@ int commands (
that was 1997. This is 2021. We aren't doing that
anymore. smtpd has to do iauth and, if it's running
on the submission port, it has to do SASL too.
+
+ Wait: I misinterpreted. It's totally fine on that
+ front. But the way it re
~ Amelia */
if (!stralloc_copys(&cmd,"")) return -1;
for (;;) {
int j;
if (!stralloc_readyplus(&cmd,1)) return -1;
+#ifdef USING_SKALIBS
+ j = buffer_get(bio,cmd.s + cmd.len,1);
+#else
j = substdio_get(ss,cmd.s + cmd.len,1);
+#endif
if (j != 1) return j;
if (cmd.s[cmd.len] == '\n') break;
++cmd.len;
diff --git a/src/dns.c b/src/dns.c
@@ -130,8 +130,8 @@ int wanttype;
return 0;
}
-static int findip(wanttype)
-int wanttype;
+// XXX A only
+static int findip(int wanttype)
{
unsigned short rrtype;
unsigned short rrdlen;
@@ -147,7 +147,7 @@ int wanttype;
i = responseend - responsepos;
if (i < 4 + 3 * 2) return DNS_SOFT;
-
+
rrtype = getshort(responsepos);
rrdlen = getshort(responsepos + 8);
responsepos += 10;
diff --git a/src/ipalloc.c b/src/ipalloc.c
@@ -5,3 +5,6 @@
GEN_ALLOC_readyplus(ipalloc,struct ip_mx,ix,len,a,10,ipalloc_readyplus)
GEN_ALLOC_append(ipalloc,struct ip_mx,ix,len,a,10,ipalloc_readyplus,ipalloc_append)
+
+GEN_ALLOC_readyplus(ip6alloc,struct ip6_mx,ix,len,a,10,ip6alloc_readyplus)
+GEN_ALLOC_append(ip6alloc,struct ip6_mx,ix,len,a,10,ip6alloc_readyplus,ip6alloc_append)
diff --git a/src/ipme.c b/src/ipme.c
@@ -18,10 +18,11 @@
#include "stralloc.h"
static int ipmeok = 0;
+static int ip6meok = 0;
ipalloc ipme = {0};
+ip6alloc ip6me = {0};
-int ipme_is(ip)
-struct ip_address *ip;
+int ipme_is(struct ip_address *ip)
{
int i;
if (ipme_init() != 1) return -1;
@@ -107,3 +108,87 @@ int ipme_init()
ipmeok = 1;
return 1;
}
+
+int ip6me_is(struct ip6_address *ip)
+{
+ int i;
+ if (ip6me_init() != 1) return -1;
+ for (i = 0;i < ip6me.len;++i)
+ if (byte_equal(&ip6me.ix[i].ip,4,ip))
+ return 1;
+ return 0;
+}
+
+int ip6me_init()
+{
+ struct ifconf ifc;
+ char *x;
+ struct ifreq *ifr;
+ struct sockaddr_in6 *sin;
+ int len;
+ int s;
+ struct ip6_mx ix;
+
+ if (ip6meok) return 1;
+ if (!ipalloc_readyplus(&ip6me,0)) return 0;
+ ip6me.len = 0;
+ ix.pref = 0;
+
+ /* :: is a special address which is specifically unspecified.
+ */
+ byte_copy(&ix.ip,16,"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
+ if (!ip6alloc_append(&ip6me,&ix)) { return 0; }
+ if ((s = socket(AF_INET6,SOCK_STREAM,0)) == -1) return -1;
+
+ ifc.ifc_buf = 0;
+ ifc.ifc_len = 0;
+
+ /* first pass: just ask what the correct length for all addresses is */
+ len = 0;
+ if (ioctl(s,SIOCGIFCONF,&ifc) >= 0 && ifc.ifc_len > 0) { /* > is for System V */
+ if (!stralloc_ready(&buf,ifc.ifc_len)) { close(s); return 0; }
+ ifc.ifc_buf = buf.s;
+ if (ioctl(s,SIOCGIFCONF,&ifc) >= 0)
+ buf.len = ifc.ifc_len;
+ }
+
+ /* check if we have complete length, otherwise try so sort that out */
+ if (buf.len == 0) {
+ len = 256;
+ for (;;) {
+ if (!stralloc_ready(&buf,len)) { close(s); return 0; }
+ buf.len = 0;
+ ifc.ifc_buf = buf.s;
+ ifc.ifc_len = len;
+ if (ioctl(s,SIOCGIFCONF,&ifc) >= 0) /* > is for System V */
+ if (ifc.ifc_len + sizeof(*ifr) + 64 < len) { /* what a stupid interface */
+ buf.len = ifc.ifc_len;
+ break;
+ }
+ if (len > 200000) { close(s); return -1; }
+ len += 100 + (len >> 2);
+ }
+ }
+ x = buf.s;
+ while (x < buf.s + buf.len) {
+ ifr = (struct ifreq *) x;
+#ifdef HASSALEN
+ len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
+ if (len < sizeof(*ifr))
+ len = sizeof(*ifr);
+#else
+ len = sizeof(*ifr);
+#endif
+ if (ifr->ifr_addr.sa_family == AF_INET) {
+ sin = (struct sockaddr_in6 *) &ifr->ifr_addr;
+ byte_copy(&ix.ip,16,&sin->sin6_addr);
+ if (ioctl(s,SIOCGIFFLAGS,x) == 0)
+ if (ifr->ifr_flags & IFF_UP)
+ if (!ip6alloc_append(&ip6me,&ix)) { close(s); return 0; }
+ }
+ x += len;
+ }
+ close(s);
+ ip6meok = 1;
+ return 1;
+}
diff --git a/src/libmxf/Makefile b/src/libmxf/Makefile
diff --git a/src/libmxf/Makefile.common b/src/libmxf/Makefile.common
diff --git a/src/libmxf/include/mxfevent/event.h b/src/libmxf/include/mxfevent/event.h
@@ -1,59 +0,0 @@
-/*
- * libmxf/include/mxfevent/event.h - header for programs relying on mxfevent
- * Copyright (C) 2021 Ellenor Bjornsdottir
- * All rights reserved.
- *
- * Permission to use, modify, redistribute, or incorporate this work
- * is granted, provided that the terms in doc/LICENCE are followed.
- */
-
-#ifndef HAS_MXFEVENT_EVENT_H
-#define HAS_MXFEVENT_EVENT_H
-#include <stddef.h>
-
-#ifndef SYS_POLL_H
-#define BMFLAG(n) (1<<(n-1))
-// only these events/filters are meaningful to us in mxfevent
-#define EVENTREAD BMFLAG(1)
-#define EVENTWRITE BMFLAG(2)
-#define EVENTERR BMFLAG(3)
-#define EVENTHUP BMFLAG(4)
-#define EVENTNVAL BMFLAG(5)
-#else
-#define EVENTREAD POLLIN|POLLRDNORM
-#define EVENTWRITE POLLOUT|POLLWRNORM
-#define EVENTERR POLLERR
-#define EVENTHUP POLLHUP
-#define EVENTNVAL POLLNVAL
-#endif
-typedef struct {
- unsigned int refs; // References
- void *opaque; // pointer to user's opaque
-} rc_ptr_t; // XXX: will we ever use this?
-
-typedef struct {
- unsigned int queue; // kq fd; only used by kq, epoll and illumos ports
-} mxfpq_t; // this is an opaque that'll be used in rc_ptr_t by mxfpoll
-
-// mxfpoll() will be conceptually similar to a bastard hybrid
-// of kqueue() and ppoll and will be implemented using
-// the former or the latter in order if available.
-typedef int event_return_t;
-
-#ifndef SYS_POLL_H
-typedef struct {
- int fd; // File descriptor, as in struct pollfd
- short events; // Events user seeks
- short revents; // Events user got
-} pollfd_t; // low level structure
-typedef unsigned int nfds_t;
-#else
-typedef struct pollfd pollfd_t;
-#endif
-
-// temporary until we import TAI handling
-typedef struct timespec mxftimespec_t;
-
-extern int mxfpoll (rc_ptr_t *mxfpollqueue, pollfd_t fds[], nfds_t nfds, mxftimespec_t *timeout);
-
-#endif
diff --git a/src/libmxf/mxfevent/NOTES.mxfevent b/src/libmxf/mxfevent/NOTES.mxfevent
@@ -1,14 +0,0 @@
- ...:: High level overview
-NightmareMail needs an event loop library. There's nothing sufficiently
-generic to be used by any kind of networked application.
-
-Why does NightmareMail need an event loop library? Surely we already
-have one in qmail-send.c?
-
-Well... qmail-send's event loop is a nightmare of selectitude.
-
- ...:: What should the interface look like?
-Probably broadly related to iopause, which Laurent Bercot of Skarnet uses
-in his 'skalibs.' Perhaps NightmareMail should be constructed so it can
-be linked against libskarnet, or against libmxfevent, with equal
-functionality.
diff --git a/src/libmxf/mxfevent/bsdq.c b/src/libmxf/mxfevent/bsdq.c
@@ -1,14 +0,0 @@
-/*
- * libmxf/mxfevent/bsdq.c - kqueue support for the mxfevent interface
- * Copyright (C) -2021 Ellenor Bjornsdottir
- * All rights reserved.
- *
- * Permission to use, modify, redistribute, or incorporate this work
- * is granted, provided that the terms in doc/LICENCE are followed.
- */
-
-#include <mxfevent/event.h>
-
-// This is safe because this file will not be compiled except on BSD.
-#include <sys/event.h>
-
diff --git a/src/libmxf/mxfevent/common.c b/src/libmxf/mxfevent/common.c
@@ -1,11 +0,0 @@
-/*
- * libmxf/mxfevent/common.c - common code for the mxfevent interface
- * Copyright (C) -2021 Ellenor Bjornsdottir
- * All rights reserved.
- *
- * Permission to use, modify, redistribute, or incorporate this work
- * is granted, provided that the terms in doc/LICENCE are followed.
- */
-
-#include <mxfevent/event.h>
-
diff --git a/src/libmxf/mxfevent/poll.c b/src/libmxf/mxfevent/poll.c
@@ -1,37 +0,0 @@
-/*
- * libmxf/mxfevent/poll.c - poll support for the mxfevent interface
- * Copyright (C) -2021 Ellenor Bjornsdottir
- * All rights reserved.
- *
- * Permission to use, modify, redistribute, or incorporate this work
- * is granted, provided that the terms in doc/LICENCE are followed.
- */
-
-#include <mxfevent/event.h>
-
-// This is safe because this file will only be compiled on platforms
-// that support poll.h.
-#include <poll.h>
-
-// MUST NOT BE NULL PTR! can be 0 refs tho.
-int mxfpoll (
- rc_ptr_t *mxfpollqueue, pollfd_t fds[], nfds_t nfds,
- mxftimespec_t *timeout
-) {
- unsigned int *refc, *queue;
- int ret = 0;
- refc = &((*mxfpollqueue).refs);
- mxfpq_t *mxfpq = (mxfpq_t *)(mxfpollqueue->opaque);
- queue = &(mxfpq->queue); // Not actually used in the poll() variant
- *refc++; // now 1 ref
-
- if (*refc > 0) return -2; // This queue is already in use.
-
-#ifdef USING_PPOLL
- ret = ppoll((struct pollfd *)fds, nfds, timeout, 0);
-#else
- ret = poll((struct pollfd *)fds, nfds, (timeout == NULL) ? 0 : ((timeout->tv_sec * 1000)+((timeout->tv_nsec) / 1000 / 1000)));
-#endif
- *refc--; // back to 0 ref
- return ret;
-}
diff --git a/src/mxf-remote/NOTES.qmailr b/src/mxf-remote/NOTES.qmailr
@@ -1,3 +1,4 @@
== MXF NOTES
-
+qmail-remote.c: This whole thing has to be chunked out. (To be fair, it
+probably doesn't - but a lot needs to be gone.)
diff --git a/src/mxf-remote/qmail-remote.c b/src/mxf-remote/qmail-remote.c
@@ -48,43 +48,74 @@ saa reciplist = {0};
struct ip_address partner;
-void out(s) char *s; { if (substdio_puts(subfdoutsmall,s) == -1) _exit(0); }
-void zero() { if (substdio_put(subfdoutsmall,"\0",1) == -1) _exit(0); }
-void _noreturn_ zerodie() { zero(); substdio_flush(subfdoutsmall); _exit(0); }
-void outsafe(sa) stralloc *sa; { int i; char ch;
-for (i = 0;i < sa->len;++i) {
-ch = sa->s[i]; if (ch < 33) ch = '?'; if (ch > 126) ch = '?';
-if (substdio_put(subfdoutsmall,&ch,1) == -1) _exit(0); } }
-
-void _noreturn_ temp_nomem() { out("ZOut of memory. (#4.3.0)\n"); zerodie(); }
-void _noreturn_ temp_oserr() { out("Z\
-System resources temporarily unavailable. (#4.3.0)\n"); zerodie(); }
-void _noreturn_ temp_noconn() { out("Z\
-Sorry, I wasn't able to establish an SMTP connection. (#4.4.1)\n"); zerodie(); }
-void _noreturn_ temp_read() { out("ZUnable to read message. (#4.3.0)\n"); zerodie(); }
-void _noreturn_ temp_dnscanon() { out("Z\
-CNAME lookup failed temporarily. (#4.4.3)\n"); zerodie(); }
-void _noreturn_ temp_dns() { out("Z\
-Sorry, I couldn't find any host by that name. (#4.1.2)\n"); zerodie(); }
-void _noreturn_ temp_chdir() { out("Z\
-Unable to switch to home directory. (#4.3.0)\n"); zerodie(); }
-void _noreturn_ temp_control() { out("Z\
-Unable to read control files. (#4.3.0)\n"); zerodie(); }
-void _noreturn_ perm_partialline() { out("D\
-SMTP cannot transfer messages with partial final lines. (#5.6.2)\n"); zerodie(); }
-void _noreturn_ perm_usage() { out("D\
-I (qmail-remote) was invoked improperly. (#5.3.5)\n"); zerodie(); }
-void _noreturn_ perm_dns() { out("D\
-Sorry, I couldn't find any host named ");
-outsafe(&host);
-out(". (#5.1.2)\n"); zerodie(); }
-void _noreturn_ perm_nomx() { out("D\
-Sorry, I couldn't find a mail exchanger or IP address. (#5.4.4)\n");
-zerodie(); }
-void _noreturn_ perm_ambigmx() { out("D\
-Sorry. Although I'm listed as a best-preference MX or A for that host,\n\
-it isn't in my control/locals file, so I don't treat it as local. (#5.4.6)\n");
-zerodie(); }
+void out(char *s)
+{
+ if (substdio_puts(subfdoutsmall,s) == -1) _exit(0);
+}
+void zero() {
+ if (substdio_put(subfdoutsmall,"\0",1) == -1) _exit(0);
+}
+void _noreturn_ zerodie() {
+ zero(); substdio_flush(subfdoutsmall); _exit(0);
+}
+void outsafe(stralloc *sa) {
+ int i;
+ char ch;
+ for (i = 0;i < sa->len;++i) {
+ ch = sa->s[i];
+ if (ch < 33) ch = '?';
+ if (ch > 126) ch = '?';
+ if (substdio_put(subfdoutsmall,&ch,1) == -1) _exit(0);
+ }
+}
+
+void _noreturn_ temp_nomem() {
+ out("ZOut of memory. (#4.3.0)\n"); zerodie();
+}
+void _noreturn_ temp_oserr() {
+ out("ZSystem resources temporarily unavailable. (#4.3.0)\n"); zerodie();
+}
+void _noreturn_ temp_noconn() {
+ out("ZSorry, I wasn't able to establish an SMTP connection. (#4.4.1)\n");
+ zerodie();
+}
+void _noreturn_ temp_read() {
+ out("ZUnable to read message. (#4.3.0)\n");
+ zerodie();
+}
+void _noreturn_ temp_dnscanon() {
+ out("ZCNAME lookup failed temporarily. (#4.4.3)\n");
+ zerodie();
+}
+void _noreturn_ temp_dns() {
+ out("ZSorry, I couldn't find any host by that name. (#4.1.2)\n"); zerodie();
+}
+void _noreturn_ temp_chdir() {
+ out("ZUnable to switch to home directory. (#4.3.0)\n"); zerodie();
+}
+void _noreturn_ temp_control() {
+ out("ZUnable to read control files. (#4.3.0)\n"); zerodie();
+}
+void _noreturn_ perm_partialline() {
+ out("DSMTP cannot transfer messages with partial final lines. (#5.6.2)\n"); zerodie();
+}
+void _noreturn_ perm_usage() {
+ out("DI (qmail-remote) was invoked improperly. (#5.3.5)\n"); zerodie();
+}
+void _noreturn_ perm_dns() {
+ out("DSorry, I couldn't find any host named ");
+ outsafe(&host);
+ out(". (#5.1.2)\n"); zerodie();
+}
+void _noreturn_ perm_nomx() {
+ out("DSorry, I couldn't find a mail exchanger or IP address. (#5.4.4)\n");
+ zerodie();
+}
+void _noreturn_ perm_ambigmx() {
+ out("DSorry. Although I'm listed as a best-preference MX or A for that host,\n\
+ it isn't in my control/locals file, so I don't treat it as local. (#5.4.6)\n");
+ zerodie();
+}
void outhost()
{
@@ -206,7 +237,7 @@ void blast()
}
substdio_put(&smtpto,"\r\n",2);
}
-
+
flagcritical = 1;
substdio_put(&smtpto,".\r\n",3);
substdio_flush(&smtpto);
@@ -219,15 +250,15 @@ void smtp()
unsigned long code;
int flagbother;
int i;
-
+
if (smtpcode() != 220) quit("ZConnected to "," but greeting failed");
-
+
substdio_puts(&smtpto,"HELO ");
substdio_put(&smtpto,helohost.s,helohost.len);
substdio_puts(&smtpto,"\r\n");
substdio_flush(&smtpto);
if (smtpcode() != 250) quit("ZConnected to "," but my name was rejected");
-
+
substdio_puts(&smtpto,"MAIL FROM:<");
substdio_put(&smtpto,sender.s,sender.len);
substdio_puts(&smtpto,">\r\n");
@@ -235,7 +266,7 @@ void smtp()
code = smtpcode();
if (code >= 500) quit("DConnected to "," but sender was rejected");
if (code >= 400) quit("ZConnected to "," but sender was rejected");
-
+
flagbother = 0;
for (i = 0;i < reciplist.len;++i) {
substdio_puts(&smtpto,"RCPT TO:<");
@@ -257,12 +288,12 @@ void smtp()
}
}
if (!flagbother) quit("DGiving up on ","");
-
+
substdio_putsflush(&smtpto,"DATA\r\n");
code = smtpcode();
if (code >= 500) quit("D"," failed on DATA command");
if (code >= 400) quit("Z"," failed on DATA command");
-
+
blast();
code = smtpcode();
flagcritical = 0;
@@ -277,7 +308,7 @@ stralloc canonbox = {0};
void addrmangle(stralloc *saout, char *s)
{
int j;
-
+
j = str_rchr(s,'@');
if (!s[j]) {
if (!stralloc_copys(saout,s)) temp_nomem();
@@ -288,7 +319,7 @@ void addrmangle(stralloc *saout, char *s)
/* box has to be quoted */
if (!quote(saout,&canonbox)) temp_nomem();
if (!stralloc_cats(saout,"@")) temp_nomem();
-
+
if (!stralloc_copys(&canonhost,s + j + 1)) temp_nomem();
if (!stralloc_cat(saout,&canonhost)) temp_nomem();
@@ -320,22 +351,22 @@ int main(int argc, char **argv)
char **recips;
unsigned long prefme;
char *relayhost;
-
+
sig_pipeignore();
if (argc < 4) perm_usage();
if (chdir(auto_qmail) == -1) temp_chdir();
getcontrols();
-
-
+
+
if (!stralloc_copys(&host,argv[1])) temp_nomem();
-
+
relayhost = 0;
for (i = 0;i <= host.len;++i)
if ((i == 0) || (i == host.len) || (host.s[i] == '.'))
if ((relayhost = constmap(&maproutes,host.s + i,host.len - i)))
break;
if (relayhost && !*relayhost) relayhost = 0;
-
+
if (relayhost) {
i = str_chr(relayhost,':');
if (relayhost[i]) {
@@ -347,10 +378,11 @@ int main(int argc, char **argv)
addrmangle(&sender,argv[2]);
-
+
if (!saa_readyplus(&reciplist,0)) temp_nomem();
if (ipme_init() != 1) temp_oserr();
-
+ if (ip6me_init() != 1) temp_oserr();
+
recips = argv + 3;
while (*recips) {
if (!saa_readyplus(&reciplist,1)) temp_nomem();
@@ -360,7 +392,7 @@ int main(int argc, char **argv)
++recips;
}
-
+
random = now() + (getpid() << 16);
switch (relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,&host,random)) {
case DNS_MEM: temp_nomem();
@@ -369,30 +401,30 @@ int main(int argc, char **argv)
case 1:
if (ip.len <= 0) temp_dns();
}
-
+
if (ip.len <= 0) perm_nomx();
-
+
prefme = 100000;
for (i = 0;i < ip.len;++i)
if (ipme_is(&ip.ix[i].ip))
if (ip.ix[i].pref < prefme)
prefme = ip.ix[i].pref;
-
+
if (relayhost) prefme = 300000;
-
+
for (i = 0;i < ip.len;++i)
if (ip.ix[i].pref < prefme)
break;
-
+
if (i >= ip.len)
perm_ambigmx();
-
+
for (i = 0;i < ip.len;++i) if (ip.ix[i].pref < prefme) {
if (tcpto(&ip.ix[i].ip)) continue;
-
+
smtpfd = socket(AF_INET,SOCK_STREAM,0);
if (smtpfd == -1) temp_oserr();
-
+
if (timeoutconn(smtpfd,&ip.ix[i].ip,(unsigned int) port,timeoutconnect) == 0) {
tcpto_err(&ip.ix[i].ip,0);
partner = ip.ix[i].ip;
diff --git a/src/mxf-send/cleanup.c b/src/mxf-send/cleanup.c
@@ -0,0 +1,60 @@
+#include "mxf-send.h"
+/* this file is too long ------------------------------------------ CLEANUPS */
+
+int flagcleanup; /* if 1, cleanupdir is initialized and ready */
+readsubdir cleanupdir;
+datetime_sec cleanuptime;
+
+void cleanup_init()
+{
+ flagcleanup = 0;
+ cleanuptime = now();
+}
+
+void cleanup_selprep(datetime_sec *wakeup)
+{
+ if (flagcleanup) *wakeup = 0;
+ if (*wakeup > cleanuptime) *wakeup = cleanuptime;
+}
+
+void cleanup_do()
+{
+ char ch;
+ struct stat st;
+ unsigned long id;
+
+ if (!flagcleanup)
+ {
+ if (recent < cleanuptime) return;
+ readsubdir_init(&cleanupdir,"mess",pausedir);
+ flagcleanup = 1;
+ }
+
+ switch(readsubdir_next(&cleanupdir,&id))
+ {
+ case 1:
+ break;
+ case 0:
+ flagcleanup = 0;
+ cleanuptime = recent + SLEEP_CLEANUP;
+ default:
+ return;
+ }
+
+ fnmake_mess(id);
+ if (stat(fn.s,&st) == -1) return; /* probably qmail-queue deleted it */
+ if (recent <= st.st_atime + OSSIFIED) return;
+
+ fnmake_info(id);
+ if (stat(fn.s,&st) == 0) return;
+ if (errno != error_noent) return;
+ fnmake_todo(id);
+ if (stat(fn.s,&st) == 0) return;
+ if (errno != error_noent) return;
+
+ fnmake_foop(id);
+ if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
+ if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
+ if (ch != '+')
+ log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
+}
diff --git a/src/mxf-send/comm.c b/src/mxf-send/comm.c
@@ -0,0 +1,83 @@
+#include "mxf-send.h"
+/* this file is too long ------------------------------------- COMMUNICATION */
+
+substdio sstoqc; char sstoqcbuf[1024];
+substdio ssfromqc; char ssfromqcbuf[1024];
+stralloc comm_buf[CHANNELS] = { {0}, {0} };
+int comm_pos[CHANNELS];
+
+void comm_init()
+{
+ int c;
+ substdio_fdbuf(&sstoqc,write,5,sstoqcbuf,sizeof(sstoqcbuf));
+ substdio_fdbuf(&ssfromqc,read,6,ssfromqcbuf,sizeof(ssfromqcbuf));
+ for (c = 0;c < CHANNELS;++c)
+ if (ndelay_on(chanfdout[c]) == -1)
+ /* this is so stupid: NDELAY semantics should be default on write */
+ spawndied(c); /* drastic, but better than risking deadlock */
+}
+
+int comm_canwrite(int c)
+{
+ /* XXX: could allow a bigger buffer; say 10 recipients */
+ if (comm_buf[c].s && comm_buf[c].len) return 0;
+ return 1;
+}
+
+void comm_write(int c, int delnum, unsigned long id, char *sender, char *recip)
+{
+ char ch;
+ if (comm_buf[c].s && comm_buf[c].len) return;
+ while (!stralloc_copys(&comm_buf[c],"")) nomem();
+ ch = delnum;
+ while (!stralloc_append(&comm_buf[c],&ch)) nomem();
+ fnmake_split(id);
+ while (!stralloc_cats(&comm_buf[c],fn.s)) nomem();
+ while (!stralloc_0(&comm_buf[c])) nomem();
+ senderadd(&comm_buf[c],sender,recip);
+ while (!stralloc_0(&comm_buf[c])) nomem();
+ while (!stralloc_cats(&comm_buf[c],recip)) nomem();
+ while (!stralloc_0(&comm_buf[c])) nomem();
+ comm_pos[c] = 0;
+}
+
+void comm_selprep(int *nfds, fd_set *wfds)
+{
+ int c;
+ for (c = 0;c < CHANNELS;++c)
+ if (flagspawnalive[c])
+ if (comm_buf[c].s && comm_buf[c].len)
+ {
+ FD_SET(chanfdout[c],wfds);
+ if (*nfds <= chanfdout[c])
+ *nfds = chanfdout[c] + 1;
+ }
+}
+
+void comm_do(fd_set *wfds)
+{
+ int c;
+ for (c = 0;c < CHANNELS;++c)
+ if (flagspawnalive[c])
+ if (comm_buf[c].s && comm_buf[c].len)
+ if (FD_ISSET(chanfdout[c],wfds))
+ {
+ int w;
+ int len;
+ len = comm_buf[c].len;
+ w = write(chanfdout[c],comm_buf[c].s + comm_pos[c],len - comm_pos[c]);
+ if (w == 0 || w == -1)
+ {
+ if ((w == -1) && (errno == error_pipe))
+ spawndied(c);
+ else
+ continue; /* kernel select() bug; can't avoid busy-looping */
+ }
+ else
+ {
+ comm_pos[c] += w;
+ if (comm_pos[c] == len)
+ comm_buf[c].len = 0;
+ }
+ }
+}
diff --git a/src/mxf-send/deliveries.c b/src/mxf-send/deliveries.c
@@ -0,0 +1,201 @@
+#include "mxf-send.h"
+/* this file is too long ---------------------------------------- DELIVERIES */
+
+struct del
+ {
+ int used;
+ int j;
+ unsigned long delid;
+ seek_pos mpos;
+ stralloc recip;
+ }
+;
+
+unsigned long masterdelid = 1;
+unsigned int concurrency[CHANNELS] = { 10, 20 };
+unsigned int concurrencyused[CHANNELS] = { 0, 0 };
+struct del *d[CHANNELS];
+stralloc dline[CHANNELS];
+char delbuf[2048];
+
+void del_status()
+{
+ int c;
+
+ log1("status:");
+ for (c = 0;c < CHANNELS;++c) {
+ strnum2[fmt_ulong(strnum2,(unsigned long) concurrencyused[c])] = 0;
+ strnum3[fmt_ulong(strnum3,(unsigned long) concurrency[c])] = 0;
+ qslog2(chanstatusmsg[c],strnum2);
+ qslog2("/",strnum3);
+ }
+ if (flagexitasap) log1(" exitasap");
+ log1("\n");
+}
+
+void del_init()
+{
+ int c;
+ unsigned int i;
+ for (c = 0;c < CHANNELS;++c)
+ {
+ flagspawnalive[c] = 1;
+ while (!(d[c] = (struct del *) alloc(concurrency[c] * sizeof(struct del))))
+ nomem();
+ for (i = 0;i < concurrency[c];++i)
+ { d[c][i].used = 0; d[c][i].recip.s = 0; }
+ dline[c].s = 0;
+ while (!stralloc_copys(&dline[c],"")) nomem();
+ }
+ del_status();
+}
+
+int del_canexit()
+{
+ int c;
+ for (c = 0;c < CHANNELS;++c)
+ if (flagspawnalive[c]) /* if dead, nothing we can do about its jobs */
+ if (concurrencyused[c]) return 0;
+ return 1;
+}
+
+int del_avail(int c) {
+ return flagspawnalive[c] && comm_canwrite(c) && (concurrencyused[c] < concurrency[c]);
+}
+
+void del_start(int j, seek_pos mpos, char *recip)
+{
+ unsigned int i;
+ int c;
+
+ c = jo[j].channel;
+ if (!flagspawnalive[c]) return;
+ if (!comm_canwrite(c)) return;
+
+ for (i = 0;i < concurrency[c];++i) if (!d[c][i].used) break;
+ if (i == concurrency[c]) return;
+
+ if (!stralloc_copys(&d[c][i].recip,recip)) { nomem(); return; }
+ if (!stralloc_0(&d[c][i].recip)) { nomem(); return; }
+ d[c][i].j = j; ++jo[j].refs;
+ d[c][i].delid = masterdelid++;
+ d[c][i].mpos = mpos;
+ d[c][i].used = 1; ++concurrencyused[c];
+
+ comm_write(c,i,jo[j].id,jo[j].sender.s,recip);
+
+ strnum2[fmt_ulong(strnum2,d[c][i].delid)] = 0;
+ strnum3[fmt_ulong(strnum3,jo[j].id)] = 0;
+ qslog2("starting delivery ",strnum2);
+ log3(": msg ",strnum3,tochan[c]);
+ logsafe(recip);
+ log1("\n");
+ del_status();
+}
+
+void markdone(int c, unsigned long id, seek_pos pos)
+{
+ struct stat st;
+ int fd;
+ fnmake_chanaddr(id,c);
+ for (;;)
+ {
+ fd = open_write(fn.s);
+ if (fd == -1) break;
+ if (fstat(fd,&st) == -1) { close(fd); break; }
+ if (seek_set(fd,pos) == -1) { close(fd); break; }
+ if (write(fd,"D",1) != 1) { close(fd); break; }
+ /* further errors -> double delivery without us knowing about it, oh well */
+ close(fd);
+ return;
+ }
+ log3("warning: trouble marking ",fn.s,"; message will be delivered twice!\n");
+}
+
+void del_dochan(int c)
+{
+ int r;
+ char ch;
+ int i;
+ int delnum;
+ r = read(chanfdin[c],delbuf,sizeof(delbuf));
+ if (r == -1) return;
+ if (r == 0) { spawndied(c); return; }
+ for (i = 0;i < r;++i)
+ {
+ ch = delbuf[i];
+ while (!stralloc_append(&dline[c],&ch)) nomem();
+ if (dline[c].len > REPORTMAX)
+ dline[c].len = REPORTMAX;
+ /* qmail-lspawn and qmail-rspawn are responsible for keeping it short */
+ /* but from a security point of view, we don't trust rspawn */
+ if (!ch && (dline[c].len > 1))
+ {
+ delnum = (unsigned int) (unsigned char) dline[c].s[0];
+ if ((delnum < 0) || (delnum >= concurrency[c]) || !d[c][delnum].used)
+ log1("warning: internal error: delivery report out of range\n");
+ else
+ {
+ strnum3[fmt_ulong(strnum3,d[c][delnum].delid)] = 0;
+ if (dline[c].s[1] == 'Z')
+ if (jo[d[c][delnum].j].flagdying)
+ {
+ dline[c].s[1] = 'D';
+ --dline[c].len;
+ while (!stralloc_cats(&dline[c],"I'm not going to try again; this message has been in the queue too long.\n")) nomem();
+ while (!stralloc_0(&dline[c])) nomem();
+ }
+ switch(dline[c].s[1])
+ {
+ case 'K':
+ log3("delivery ",strnum3,": success: ");
+ logsafe(dline[c].s + 2);
+ log1("\n");
+ markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
+ --jo[d[c][delnum].j].numtodo;
+ break;
+ case 'Z':
+ log3("delivery ",strnum3,": deferral: ");
+ logsafe(dline[c].s + 2);
+ log1("\n");
+ break;
+ case 'D':
+ log3("delivery ",strnum3,": failure: ");
+ logsafe(dline[c].s + 2);
+ log1("\n");
+ addbounce(jo[d[c][delnum].j].id,d[c][delnum].recip.s,dline[c].s + 2);
+ markdone(c,jo[d[c][delnum].j].id,d[c][delnum].mpos);
+ --jo[d[c][delnum].j].numtodo;
+ break;
+ default:
+ log3("delivery ",strnum3,": report mangled, will defer\n");
+ }
+ job_close(d[c][delnum].j);
+ d[c][delnum].used = 0; --concurrencyused[c];
+ del_status();
+ }
+ dline[c].len = 0;
+ }
+ }
+}
+
+void del_selprep(int *nfds, fd_set *rfds)
+{
+ int c;
+ for (c = 0;c < CHANNELS;++c)
+ if (flagspawnalive[c])
+ {
+ FD_SET(chanfdin[c],rfds);
+ if (*nfds <= chanfdin[c])
+ *nfds = chanfdin[c] + 1;
+ }
+}
+
+void del_do(fd_set *rfds)
+{
+ int c;
+ for (c = 0;c < CHANNELS;++c)
+ if (flagspawnalive[c])
+ if (FD_ISSET(chanfdin[c],rfds))
+ del_dochan(c);
+}
diff --git a/src/mxf-send/mxf-send-protos.h b/src/mxf-send/mxf-send-protos.h
@@ -0,0 +1,67 @@
+extern void fnmake_init();
+extern void fnmake_info(unsigned long id);
+extern void fnmake_todo(unsigned long id);
+extern void fnmake_mess(unsigned long id);
+extern void fnmake_foop(unsigned long id);
+extern void fnmake_split(unsigned long id);
+extern void fnmake2_bounce(unsigned long id);
+extern void fnmake_chanaddr(unsigned long id, int c);
+
+extern void cleanup_init();
+extern void cleanup_selprep(datetime_sec *wakeup);
+extern void cleanup_do();
+
+extern void comm_init();
+extern int comm_canwrite(int c);
+extern void comm_write(int c, int delnum, unsigned long id, char *sender, char *recip);
+extern void comm_selprep(int *nfds, fd_set *wfds);
+extern void comm_do(fd_set *wfds);
+
+extern void del_status();
+extern void del_init();
+extern int del_canexit();
+extern int del_avail(int c);
+extern void del_start(int j, seek_pos mpos, char *recip);
+extern void markdone(int c, unsigned long id, seek_pos pos);
+extern void del_dochan(int c);
+extern void del_selprep(int *nfds, fd_set *rfds);
+extern void del_do(fd_set *rfds);
+
+extern void sigterm();
+extern void sigalrm();
+extern void sighup();
+
+extern void cleandied();
+extern void spawndied(int c);
+
+extern int rewrite(char *recip);
+extern void senderadd(stralloc *sa, char *sender, char *recip);
+extern int getinfo(stralloc *sa, datetime_sec *dt, unsigned long id);
+
+extern void pqadd(unsigned long id);
+extern void pqstart();
+extern void pqfinish();
+extern void pqrun();
+
+extern void job_init();
+extern int job_avail();
+extern int job_open(unsigned long id, int channel);
+extern void job_close(int j);
+extern char *stripvdomprepend(char *recip);
+
+extern void addbounce(unsigned long id, char *recip, char *report);
+extern int injectbounce(unsigned long id);
+extern int getcontrols();
+extern void regetcontrols();
+extern void reread();
+
+extern void pass_init();
+extern void pass_selprep(datetime_sec *wakeup);
+extern void pass_dochan(int c);
+extern void messdone(unsigned long id);
+extern void pass_do();
+
+extern void todo_init();
+extern void todo_selprep(int *nfds, fd_set *rfds, datetime_sec *wakeup);
+extern void todo_do(fd_set *rfds);
+extern void todo_dochan(int c);
diff --git a/src/mxf-send/mxf-send.c b/src/mxf-send/mxf-send.c
@@ -0,0 +1,780 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include "readwrite.h"
+#include "sig.h"
+#include "direntry.h"
+#include "control.h"
+#include "select.h"
+#include "open.h"
+#include "seek.h"
+#include "exit.h"
+#include "lock.h"
+#include "ndelay.h"
+#include "now.h"
+#include "getln.h"
+
+#ifdef USING_SKALIBS
+#include <skalibs/buffer.h>
+#include <skalibs/iopause.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/tai.h>
+#else
+#include "substdio.h"
+#include "stralloc.h"
+#endif
+
+#include "error.h"
+
+#include "str.h"
+#include "byte.h"
+
+#include "fmt.h"
+#include "scan.h"
+#include "alloc.h"
+
+#include "case.h"
+#include "auto_qmail.h"
+#include "trigger.h"
+#include "newfield.h"
+#include "quote.h"
+#include "qmail.h"
+#include "qsutil.h"
+#include "prioq.h"
+#include "constmap.h"
+#include "fmtqfn.h"
+#include "readsubdir.h"
+
+/* critical timing feature #1: if not triggered, do not busy-loop */
+/* critical timing feature #2: if triggered, respond within fixed time */
+/* important timing feature: when triggered, respond instantly */
+#define SLEEP_TODO 1500 /* check todo/ every 25 minutes in any case */
+#define SLEEP_FUZZ 1 /* slop a bit on sleeps to avoid zeno effect */
+#define SLEEP_FOREVER 86400 /* absolute maximum time spent in iopause() */
+#define SLEEP_CLEANUP 76431 /* time between cleanups */
+#define SLEEP_SYSFAIL 123
+#define OSSIFIED 129600 /* 36 hours; _must_ exceed q-q's DEATH (24 hours) */
+
+int lifetime = 604800;
+
+stralloc percenthack = {0};
+struct constmap mappercenthack;
+stralloc locals = {0};
+struct constmap maplocals;
+stralloc vdoms = {0};
+struct constmap mapvdoms;
+stralloc envnoathost = {0};
+stralloc bouncefrom = {0};
+stralloc bouncehost = {0};
+stralloc doublebounceto = {0};
+stralloc doublebouncehost = {0};
+
+char strnum2[FMT_ULONG];
+char strnum3[FMT_ULONG];
+
+#define CHANNELS 2
+char *chanaddr[CHANNELS] = { "local/", "remote/" };
+char *chanstatusmsg[CHANNELS] = { " local ", " remote " };
+char *tochan[CHANNELS] = { " to local ", " to remote " };
+int chanfdout[CHANNELS] = { 1, 3 };
+int chanfdin[CHANNELS] = { 2, 4 };
+int chanskip[CHANNELS] = { 10, 20 };
+
+int flagexitasap = 0; void sigterm() { flagexitasap = 1; }
+int flagrunasap = 0; void sigalrm() { flagrunasap = 1; }
+int flagreadasap = 0; void sighup() { flagreadasap = 1; }
+
+void cleandied() { log1("alert: oh no! lost qmail-clean connection! dying...\n");
+ flagexitasap = 1; }
+
+int flagspawnalive[CHANNELS];
+void spawndied(int c) { log1("alert: oh no! lost spawn connection! dying...\n");
+ flagspawnalive[c] = 0; flagexitasap = 1; }
+
+#define REPORTMAX 10000
+
+datetime_sec recent;
+
+
+/* this file is too long ----------------------------------------- FILENAMES */
+
+stralloc fn = {0};
+stralloc fn2 = {0};
+char fnmake_strnum[FMT_ULONG];
+
+void fnmake_init()
+{
+ while (!stralloc_ready(&fn,FMTQFN)) nomem();
+ while (!stralloc_ready(&fn2,FMTQFN)) nomem();
+}
+
+void fnmake_info(unsigned long id) { fn.len = fmtqfn(fn.s,"info/",id,1); }
+void fnmake_todo(unsigned long id) { fn.len = fmtqfn(fn.s,"todo/",id,0); }
+void fnmake_mess(unsigned long id) { fn.len = fmtqfn(fn.s,"mess/",id,1); }
+void fnmake_foop(unsigned long id) { fn.len = fmtqfn(fn.s,"foop/",id,0); }
+void fnmake_split(unsigned long id) { fn.len = fmtqfn(fn.s,"",id,1); }
+void fnmake2_bounce(unsigned long id)
+{ fn2.len = fmtqfn(fn2.s,"bounce/",id,0); }
+void fnmake_chanaddr(unsigned long id, int c)
+{ fn.len = fmtqfn(fn.s,chanaddr[c],id,1); }
+
+
+/* this file is too long ----------------------------------------- REWRITING */
+
+stralloc rwline = {0};
+
+/* 1 if by land, 2 if by sea, 0 if out of memory. not allowed to barf. */
+/* may trash recip. must set up rwline, between a T and a \0. */
+int rewrite(char *recip)
+{
+ unsigned int i;
+ char *x;
+ static stralloc addr = {0};
+ unsigned int at;
+
+ if (!stralloc_copys(&rwline,"T")) return 0;
+ if (!stralloc_copys(&addr,recip)) return 0;
+
+ i = byte_rchr(addr.s,addr.len,'@');
+ if (i == addr.len) {
+ if (!stralloc_cats(&addr,"@")) return 0;
+ if (!stralloc_cat(&addr,&envnoathost)) return 0;
+ }
+
+ while (constmap(&mappercenthack,addr.s + i + 1,addr.len - i - 1)) {
+ unsigned int j = byte_rchr(addr.s,i,'%');
+ if (j == i) break;
+ addr.len = i;
+ i = j;
+ addr.s[i] = '@';
+ }
+
+ at = byte_rchr(addr.s,addr.len,'@');
+
+ if (constmap(&maplocals,addr.s + at + 1,addr.len - at - 1)) {
+ if (!stralloc_cat(&rwline,&addr)) return 0;
+ if (!stralloc_0(&rwline)) return 0;
+ return 1;
+ }
+
+ for (i = 0;i <= addr.len;++i)
+ if (!i || (i == at + 1) || (i == addr.len) || ((i > at) && (addr.s[i] == '.')))
+ if ((x = constmap(&mapvdoms,addr.s + i,addr.len - i))) {
+ if (!*x) break;
+ if (!stralloc_cats(&rwline,x)) return 0;
+ if (!stralloc_cats(&rwline,"-")) return 0;
+ if (!stralloc_cat(&rwline,&addr)) return 0;
+ if (!stralloc_0(&rwline)) return 0;
+ return 1;
+ }
+
+ if (!stralloc_cat(&rwline,&addr)) return 0;
+ if (!stralloc_0(&rwline)) return 0;
+ return 2;
+}
+
+void senderadd(stralloc *sa, char *sender, char *recip)
+{
+ unsigned int i;
+
+ i = str_len(sender);
+ if (i >= 4)
+ if (str_equal(sender + i - 4,"-@[]"))
+ {
+ unsigned int j = byte_rchr(sender,i - 4,'@');
+ unsigned int k = str_rchr(recip,'@');
+ if (recip[k] && (j + 5 <= i))
+ {
+ /* owner-@host-@[] -> owner-recipbox=reciphost@host */
+ while (!stralloc_catb(sa,sender,j)) nomem();
+ while (!stralloc_catb(sa,recip,k)) nomem();
+ while (!stralloc_cats(sa,"=")) nomem();
+ while (!stralloc_cats(sa,recip + k + 1)) nomem();
+ while (!stralloc_cats(sa,"@")) nomem();
+ while (!stralloc_catb(sa,sender + j + 1,i - 5 - j)) nomem();
+ return;
+ }
+ }
+ while (!stralloc_cats(sa,sender)) nomem();
+}
+
+
+/* this file is too long ---------------------------------------------- INFO */
+
+int getinfo(stralloc *sa, datetime_sec *dt, unsigned long id)
+{
+ int fdinfo;
+ struct stat st;
+ static stralloc line = {0};
+ int match;
+ substdio ss;
+ char buf[128];
+
+ fnmake_info(id);
+ fdinfo = open_read(fn.s);
+ if (fdinfo == -1) return 0;
+ if (fstat(fdinfo,&st) == -1) { close(fdinfo); return 0; }
+ substdio_fdbuf(&ss,read,fdinfo,buf,sizeof(buf));
+ if (getln(&ss,&line,&match,'\0') == -1) { close(fdinfo); return 0; }
+ close(fdinfo);
+ if (!match) return 0;
+ if (line.s[0] != 'F') return 0;
+
+ *dt = st.st_mtime;
+ while (!stralloc_copys(sa,line.s + 1)) nomem();
+ while (!stralloc_0(sa)) nomem();
+ return 1;
+}
+
+
+/* this file is too long ----------------------------------- PRIORITY QUEUES */
+
+prioq pqdone = {0}; /* -todo +info; HOPEFULLY -local -remote */
+prioq pqchan[CHANNELS] = { {0}, {0} };
+/* pqchan 0: -todo +info +local ?remote */
+/* pqchan 1: -todo +info ?local +remote */
+prioq pqfail = {0}; /* stat() failure; has to be pqadded again */
+
+void pqadd(unsigned long id)
+{
+ struct prioq_elt pe;
+ struct prioq_elt pechan[CHANNELS];
+ int flagchan[CHANNELS];
+ struct stat st;
+ int c;
+
+#define CHECKSTAT if (errno != error_noent) goto fail;
+
+ fnmake_info(id);
+ if (stat(fn.s,&st) == -1)
+ {
+ CHECKSTAT
+ return; /* someone yanking our chain */
+ }
+
+ fnmake_todo(id);
+ if (stat(fn.s,&st) != -1) return; /* look, ma, dad crashed writing info! */
+ CHECKSTAT
+
+ for (c = 0;c < CHANNELS;++c)
+ {
+ fnmake_chanaddr(id,c);
+ if (stat(fn.s,&st) == -1) { flagchan[c] = 0; CHECKSTAT }
+ else { flagchan[c] = 1; pechan[c].id = id; pechan[c].dt = st.st_mtime; }
+ }
+
+ for (c = 0;c < CHANNELS;++c)
+ if (flagchan[c])
+ while (!prioq_insert(&pqchan[c],&pechan[c])) nomem();
+
+ for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
+ if (c == CHANNELS)
+ {
+ pe.id = id; pe.dt = now();
+ while (!prioq_insert(&pqdone,&pe)) nomem();
+ }
+
+ return;
+
+ fail:
+ log3("warning: unable to stat ",fn.s,"; will try again later\n");
+ pe.id = id; pe.dt = now() + SLEEP_SYSFAIL;
+ while (!prioq_insert(&pqfail,&pe)) nomem();
+}
+
+void pqstart()
+{
+ readsubdir rs;
+ int x;
+ unsigned long id;
+
+ readsubdir_init(&rs,"info",pausedir);
+
+ while ((x = readsubdir_next(&rs,&id)))
+ if (x > 0)
+ pqadd(id);
+}
+
+void pqfinish()
+{
+ int c;
+ struct prioq_elt pe;
+ struct timeval ut[2] = { 0 };
+
+ for (c = 0;c < CHANNELS;++c)
+ while (prioq_min(&pqchan[c],&pe))
+ {
+ prioq_delmin(&pqchan[c]);
+ fnmake_chanaddr(pe.id,c);
+ ut[0].tv_sec = ut[1].tv_sec = pe.dt;
+ if (utimes(fn.s,ut) == -1)
+ log3("warning: unable to utime ",fn.s,"; message will be retried too soon\n");
+ }
+}
+
+void pqrun()
+{
+ int c;
+ unsigned int i;
+ for (c = 0;c < CHANNELS;++c)
+ if (pqchan[c].p)
+ if (pqchan[c].len)
+ for (i = 0;i < pqchan[c].len;++i)
+ pqchan[c].p[i].dt = recent;
+}
+
+
+/* this file is too long ---------------------------------------------- JOBS */
+
+struct job
+ {
+ int refs; /* if 0, this struct is unused */
+ unsigned long id;
+ int channel;
+ datetime_sec retry;
+ stralloc sender;
+ int numtodo;
+ int flaghiteof;
+ int flagdying;
+ }
+;
+
+int numjobs;
+struct job *jo;
+
+void job_init()
+{
+ int j;
+ while (!(jo = (struct job *) alloc(numjobs * sizeof(struct job)))) nomem();
+ for (j = 0;j < numjobs;++j)
+ {
+ jo[j].refs = 0;
+ jo[j].sender.s = 0;
+ }
+}
+
+int job_avail()
+{
+ int j;
+ for (j = 0;j < numjobs;++j) if (!jo[j].refs) return 1;
+ return 0;
+}
+
+int job_open(unsigned long id, int channel)
+{
+ int j;
+ for (j = 0;j < numjobs;++j) if (!jo[j].refs) break;
+ if (j == numjobs) return -1;
+ jo[j].refs = 1;
+ jo[j].id = id;
+ jo[j].channel = channel;
+ jo[j].numtodo = 0;
+ jo[j].flaghiteof = 0;
+ return j;
+}
+
+void job_close(int j)
+{
+ struct prioq_elt pe;
+ struct stat st;
+
+ if (0 < --jo[j].refs) return;
+
+ pe.id = jo[j].id;
+ pe.dt = jo[j].retry;
+ if (jo[j].flaghiteof && !jo[j].numtodo)
+ {
+ fnmake_chanaddr(jo[j].id,jo[j].channel);
+ if (unlink(fn.s) == -1)
+ {
+ log3("warning: unable to unlink ",fn.s,"; will try again later\n");
+ pe.dt = now() + SLEEP_SYSFAIL;
+ }
+ else
+ {
+ int c;
+ for (c = 0;c < CHANNELS;++c) if (c != jo[j].channel)
+ {
+ fnmake_chanaddr(jo[j].id,c);
+ if (stat(fn.s,&st) == 0) return; /* more channels going */
+ if (errno != error_noent)
+ {
+ log3("warning: unable to stat ",fn.s,"\n");
+ break; /* this is the only reason for HOPEFULLY */
+ }
+ }
+ pe.dt = now();
+ while (!prioq_insert(&pqdone,&pe)) nomem();
+ return;
+ }
+ }
+
+ while (!prioq_insert(&pqchan[jo[j].channel],&pe)) nomem();
+}
+
+
+/* this file is too long ------------------------------------------- BOUNCES */
+
+char *stripvdomprepend(char *recip)
+{
+ unsigned int i;
+ char *domain;
+ unsigned int domainlen;
+ char *prepend;
+
+ i = str_rchr(recip,'@');
+ if (!recip[i]) return recip;
+ domain = recip + i + 1;
+ domainlen = str_len(domain);
+
+ for (i = 0;i <= domainlen;++i)
+ if ((i == 0) || (i == domainlen) || (domain[i] == '.'))
+ if ((prepend = constmap(&mapvdoms,domain + i,domainlen - i)))
+ {
+ if (!*prepend) break;
+ i = str_len(prepend);
+ if (str_diffn(recip,prepend,i)) break;
+ if (recip[i] != '-') break;
+ return recip + i + 1;
+ }
+ return recip;
+}
+
+stralloc bouncetext = {0};
+
+void addbounce(unsigned long id, char *recip, char *report)
+{
+ int fd;
+ unsigned int pos;
+ int w;
+ while (!stralloc_copys(&bouncetext,"<")) nomem();
+ while (!stralloc_cats(&bouncetext,stripvdomprepend(recip))) nomem();
+ for (pos = 0;pos < bouncetext.len;++pos)
+ if (bouncetext.s[pos] == '\n')
+ bouncetext.s[pos] = '_';
+ while (!stralloc_cats(&bouncetext,">:\n")) nomem();
+ while (!stralloc_cats(&bouncetext,report)) nomem();
+ if (report[0])
+ if (report[str_len(report) - 1] != '\n')
+ while (!stralloc_cats(&bouncetext,"\n")) nomem();
+ for (pos = bouncetext.len - 2;pos > 0;--pos)
+ if (bouncetext.s[pos] == '\n')
+ if (bouncetext.s[pos - 1] == '\n')
+ bouncetext.s[pos] = '/';
+ while (!stralloc_cats(&bouncetext,"\n")) nomem();
+ fnmake2_bounce(id);
+ for (;;)
+ {
+ fd = open_append(fn2.s);
+ if (fd != -1) break;
+ log1("alert: unable to append to bounce message; HELP! sleeping...\n");
+ sleep(10);
+ }
+ pos = 0;
+ while (pos < bouncetext.len)
+ {
+ w = write(fd,bouncetext.s + pos,bouncetext.len - pos);
+ if (w == 0 || w == -1)
+ {
+ log1("alert: unable to append to bounce message; HELP! sleeping...\n");
+ sleep(10);
+ }
+ else
+ pos += w;
+ }
+ close(fd);
+}
+
+int injectbounce(unsigned long id)
+{
+ struct qmail qqt;
+ struct stat st;
+ char *bouncesender;
+ char *bouncerecip;
+ int r;
+ int fd;
+ substdio ssread;
+ char buf[128];
+ char inbuf[128];
+ static stralloc sender = {0};
+ static stralloc quoted = {0};
+ datetime_sec birth;
+ unsigned long qp;
+
+ if (!getinfo(&sender,&birth,id)) return 0; /* XXX: print warning */
+
+ /* owner-@host-@[] -> owner-@host */
+ if (sender.len >= 5)
+ if (str_equal(sender.s + sender.len - 5,"-@[]"))
+ {
+ sender.len -= 4;
+ sender.s[sender.len - 1] = 0;
+ }
+
+ fnmake2_bounce(id);
+ fnmake_mess(id);
+ if (stat(fn2.s,&st) == -1)
+ {
+ if (errno == error_noent)
+ return 1;
+ log3("warning: unable to stat ",fn2.s,"\n");
+ return 0;
+ }
+ if (str_equal(sender.s,"#@[]"))
+ log3("triple bounce: discarding ",fn2.s,"\n");
+ else
+ {
+ if (qmail_open(&qqt) == -1)
+ { log1("warning: unable to start qmail-queue, will try later\n"); return 0; }
+ qp = qmail_qp(&qqt);
+
+ if (*sender.s) { bouncesender = ""; bouncerecip = sender.s; }
+ else { bouncesender = "#@[]"; bouncerecip = doublebounceto.s; }
+
+ while (!newfield_datemake(now())) nomem();
+ qmail_put(&qqt,newfield_date.s,newfield_date.len);
+ qmail_puts(&qqt,"From: ");
+ while (!quote("ed,&bouncefrom)) nomem();
+ qmail_put(&qqt,quoted.s,quoted.len);
+ qmail_puts(&qqt,"@");
+ qmail_put(&qqt,bouncehost.s,bouncehost.len);
+ qmail_puts(&qqt,"\nTo: ");
+ while (!quote2("ed,bouncerecip)) nomem();
+ qmail_put(&qqt,quoted.s,quoted.len);
+ qmail_puts(&qqt,"\n\
+Subject: Failure Notice\n\
+\n\
+Hi. This is the qmail-send (NightmareMail queue manager) program at ");
+ qmail_put(&qqt,bouncehost.s,bouncehost.len);
+ qmail_puts(&qqt,*sender.s ? ".\n\
+I'm afraid I wasn't able to deliver your message to the following addresses.\n\
+This is a permanent error. I've given up. Sorry it didn't work out.\n\
+For further assistance, mail the postmaster.\n\
+\n\
+" : ".\n\
+I tried to deliver a bounce message to this address, but the bounce bounced!\n\
+\n\
+");
+
+ fd = open_read(fn2.s);
+ if (fd == -1)
+ qmail_fail(&qqt);
+ else
+ {
+ substdio_fdbuf(&ssread,read,fd,inbuf,sizeof(inbuf));
+ while ((r = substdio_get(&ssread,buf,sizeof(buf))) > 0)
+ qmail_put(&qqt,buf,r);
+ close(fd);
+ if (r == -1)
+ qmail_fail(&qqt);
+ }
+
+ qmail_puts(&qqt,*sender.s ? "--- Below this line is a copy of the message.\n\n" : "--- Below this line is the original bounce.\n\n");
+ qmail_puts(&qqt,"Return-Path: <");
+ while (!quote2("ed,sender.s)) nomem();
+ qmail_put(&qqt,quoted.s,quoted.len);
+ qmail_puts(&qqt,">\n");
+
+ fd = open_read(fn.s);
+ if (fd == -1)
+ qmail_fail(&qqt);
+ else
+ {
+ substdio_fdbuf(&ssread,read,fd,inbuf,sizeof(inbuf));
+ while ((r = substdio_get(&ssread,buf,sizeof(buf))) > 0)
+ qmail_put(&qqt,buf,r);
+ close(fd);
+ if (r == -1)
+ qmail_fail(&qqt);
+ }
+
+ qmail_from(&qqt,bouncesender);
+ qmail_to(&qqt,bouncerecip);
+ if (*qmail_close(&qqt))
+ { log1("warning: trouble injecting bounce message, will try later\n"); return 0; }
+
+ strnum2[fmt_ulong(strnum2,id)] = 0;
+ qslog2("bounce msg ",strnum2);
+ strnum2[fmt_ulong(strnum2,qp)] = 0;
+ log3(" qp ",strnum2,"\n");
+ }
+ if (unlink(fn2.s) == -1)
+ {
+ log3("warning: unable to unlink ",fn2.s,"\n");
+ return 0;
+ }
+ return 1;
+}
+
+/* this file is too long ---------------------------------------------- MAIN */
+
+int getcontrols() { if (control_init() == -1) return 0;
+ if (control_readint(&lifetime,"control/queuelifetime") == -1) return 0;
+ if (control_readint(&concurrency[0],"control/concurrencylocal") == -1) return 0;
+ if (control_readint(&concurrency[1],"control/concurrencyremote") == -1) return 0;
+ if (control_rldef(&envnoathost,"control/envnoathost",1,"envnoathost") != 1) return 0;
+ if (control_rldef(&bouncefrom,"control/bouncefrom",0,"MAILER-DAEMON") != 1) return 0;
+ if (control_rldef(&bouncehost,"control/bouncehost",1,"bouncehost") != 1) return 0;
+ if (control_rldef(&doublebouncehost,"control/doublebouncehost",1,"doublebouncehost") != 1) return 0;
+ if (control_rldef(&doublebounceto,"control/doublebounceto",0,"postmaster") != 1) return 0;
+ if (!stralloc_cats(&doublebounceto,"@")) return 0;
+ if (!stralloc_cat(&doublebounceto,&doublebouncehost)) return 0;
+ if (!stralloc_0(&doublebounceto)) return 0;
+ if (control_readfile(&locals,"control/locals",1) != 1) return 0;
+ if (!constmap_init(&maplocals,locals.s,locals.len,0)) return 0;
+ switch(control_readfile(&percenthack,"control/percenthack",0))
+ {
+ case -1: return 0;
+ case 0: if (!constmap_init(&mappercenthack,"",0,0)) return 0; break;
+ case 1: if (!constmap_init(&mappercenthack,percenthack.s,percenthack.len,0)) return 0; break;
+ }
+ switch(control_readfile(&vdoms,"control/virtualdomains",0))
+ {
+ case -1: return 0;
+ case 0: if (!constmap_init(&mapvdoms,"",0,1)) return 0; break;
+ case 1: if (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) return 0; break;
+ }
+ return 1; }
+
+stralloc newlocals = {0};
+stralloc newvdoms = {0};
+
+void regetcontrols()
+{
+ int r;
+
+ if (control_readfile(&newlocals,"control/locals",1) != 1)
+ { log1("alert: unable to reread control/locals\n"); return; }
+ r = control_readfile(&newvdoms,"control/virtualdomains",0);
+ if (r == -1)
+ { log1("alert: unable to reread control/virtualdomains\n"); return; }
+
+ constmap_free(&maplocals);
+ constmap_free(&mapvdoms);
+
+ while (!stralloc_copy(&locals,&newlocals)) nomem();
+ while (!constmap_init(&maplocals,locals.s,locals.len,0)) nomem();
+
+ if (r)
+ {
+ while (!stralloc_copy(&vdoms,&newvdoms)) nomem();
+ while (!constmap_init(&mapvdoms,vdoms.s,vdoms.len,1)) nomem();
+ }
+ else
+ while (!constmap_init(&mapvdoms,"",0,1)) nomem();
+}
+
+void reread()
+{
+ if (chdir(auto_qmail) == -1)
+ {
+ log1("alert: unable to reread controls: unable to switch to home directory\n");
+ return;
+ }
+ regetcontrols();
+ while (chdir("queue") == -1)
+ {
+ log1("alert: unable to switch back to queue directory; HELP! sleeping...\n");
+ sleep(10);
+ }
+}
+
+int main(void)
+{
+ int fd;
+ datetime_sec wakeup;
+ fd_set rfds;
+ fd_set wfds;
+ int nfds;
+ struct timeval tv;
+ int c;
+
+ if (chdir(auto_qmail) == -1)
+ { log1("alert: cannot start: unable to switch to home directory\n"); _exit(111); }
+ if (!getcontrols())
+ { log1("alert: cannot start: unable to read controls\n"); _exit(111); }
+ if (chdir("queue") == -1)
+ { log1("alert: cannot start: unable to switch to queue directory\n"); _exit(111); }
+ sig_pipeignore();
+ sig_termcatch(sigterm);
+ sig_alarmcatch(sigalrm);
+ sig_hangupcatch(sighup);
+ sig_childdefault();
+ umask(077);
+
+ fd = open_write("lock/sendmutex");
+ if (fd == -1)
+ { log1("alert: cannot start: unable to open mutex\n"); _exit(111); }
+ if (lock_exnb(fd) == -1)
+ { log1("alert: cannot start: qmail-send is already running\n"); _exit(111); }
+
+ numjobs = 0;
+ for (c = 0;c < CHANNELS;++c)
+ {
+ char ch;
+ int u;
+ int r;
+ do
+ r = read(chanfdin[c],&ch,1);
+ while ((r == -1) && (errno == error_intr));
+ if (r < 1)
+ { log1("alert: cannot start: hath the daemon spawn no fire?\n"); _exit(111); }
+ u = (unsigned int) (unsigned char) ch;
+ if (concurrency[c] > u) concurrency[c] = u;
+ numjobs += concurrency[c];
+ }
+
+ fnmake_init();
+
+ comm_init();
+
+ pqstart();
+ job_init();
+ del_init();
+ pass_init();
+ todo_init();
+ cleanup_init();
+
+ while (!flagexitasap || !del_canexit())
+ {
+ recent = now();
+
+ if (flagrunasap) { flagrunasap = 0; pqrun(); }
+ if (flagreadasap) { flagreadasap = 0; reread(); }
+
+ wakeup = recent + SLEEP_FOREVER;
+ FD_ZERO(&rfds);
+ FD_ZERO(&wfds);
+ nfds = 1;
+
+ comm_selprep(&nfds,&wfds);
+ del_selprep(&nfds,&rfds);
+ pass_selprep(&wakeup);
+ todo_selprep(&nfds,&rfds,&wakeup);
+ cleanup_selprep(&wakeup);
+
+ if (wakeup <= recent) tv.tv_sec = 0;
+ else tv.tv_sec = wakeup - recent + SLEEP_FUZZ;
+ tv.tv_usec = 0;
+
+ if (select(nfds,&rfds,&wfds,NULL,&tv) == -1)
+ if (errno == error_intr)
+ ;
+ else
+ log1("warning: trouble in select\n");
+ else
+ {
+ recent = now();
+
+ // XXX: should have a queue structure
+ comm_do(&wfds);
+ del_do(&rfds);
+ todo_do(&rfds);
+ pass_do();
+ cleanup_do();
+ }
+ }
+ pqfinish();
+ log1("status: exiting\n");
+ _exit(0);
+};
diff --git a/src/mxf-send/mxf-send.h b/src/mxf-send/mxf-send.h
@@ -0,0 +1,51 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include "readwrite.h"
+#include "sig.h"
+#include "direntry.h"
+#include "control.h"
+#include "select.h"
+#include "open.h"
+#include "seek.h"
+#include "exit.h"
+#include "lock.h"
+#include "ndelay.h"
+#include "now.h"
+#include "getln.h"
+#ifdef USING_SKALIBS
+#include <skalibs/buffer.h>
+#include <skalibs/iopause.h>
+#include <skalibs/stralloc.h>
+#include <skalibs/tai.h>
+#else
+#include "substdio.h"
+#include "stralloc.h"
+#endif
+#include "error.h"
+#include "str.h"
+#include "byte.h"
+#include "fmt.h"
+#include "scan.h"
+#include "alloc.h"
+#include "case.h"
+#include "auto_qmail.h"
+#include "trigger.h"
+#include "newfield.h"
+#include "quote.h"
+#include "qmail.h"
+#include "qsutil.h"
+#include "prioq.h"
+#include "constmap.h"
+#include "fmtqfn.h"
+#include "readsubdir.h"
+
+#define SLEEP_TODO 1500 /* check todo/ every 25 minutes in any case */
+#define SLEEP_FUZZ 1 /* slop a bit on sleeps to avoid zeno effect */
+#define SLEEP_FOREVER 86400 /* absolute maximum time spent in iopause() */
+#define SLEEP_CLEANUP 76431 /* time between cleanups */
+#define SLEEP_SYSFAIL 123
+#define OSSIFIED 129600 /* 36 hours; _must_ exceed q-q's DEATH (24 hours) */
+#define CHANNELS 2
+#define REPORTMAX 10000
+#define CHECKSTAT if (errno != error_noent) goto fail;
diff --git a/src/mxf-send/pass.c b/src/mxf-send/pass.c
@@ -0,0 +1,228 @@
+#include "mxf-send.h"
+
+/* this file is too long -------------------------------------------- PASSES */
+
+struct
+ {
+ unsigned long id; /* if 0, need a new pass */
+ int j; /* defined if id; job number */
+ int fd; /* defined if id; reading from {local,remote} */
+ seek_pos mpos; /* defined if id; mark position */
+ substdio ss;
+ char buf[128];
+ }
+pass[CHANNELS];
+
+void pass_init()
+{
+ int c;
+ for (c = 0;c < CHANNELS;++c) pass[c].id = 0;
+}
+
+void pass_selprep(datetime_sec *wakeup)
+{
+ int c;
+ struct prioq_elt pe;
+ if (flagexitasap) return;
+ for (c = 0;c < CHANNELS;++c)
+ if (pass[c].id)
+ if (del_avail(c))
+ { *wakeup = 0; return; }
+ if (job_avail())
+ for (c = 0;c < CHANNELS;++c)
+ if (!pass[c].id)
+ if (prioq_min(&pqchan[c],&pe))
+ if (*wakeup > pe.dt)
+ *wakeup = pe.dt;
+ if (prioq_min(&pqfail,&pe))
+ if (*wakeup > pe.dt)
+ *wakeup = pe.dt;
+ if (prioq_min(&pqdone,&pe))
+ if (*wakeup > pe.dt)
+ *wakeup = pe.dt;
+}
+
+static datetime_sec squareroot(datetime_sec x)
+/* result^2 <= x < (result + 1)^2 */
+/* assuming: >= 0 */
+{
+ datetime_sec y;
+ datetime_sec yy;
+ datetime_sec y21;
+ int j;
+
+ y = 0; yy = 0;
+ for (j = 15;j >= 0;--j)
+ {
+ y21 = (y << (j + 1)) + (1 << (j + j));
+ if (y21 <= x - yy) { y += (1 << j); yy += y21; }
+ }
+ return y;
+}
+
+datetime_sec nextretry(datetime_sec birth, int c)
+{
+ datetime_sec n;
+
+ if (birth > recent) n = 0;
+ else n = squareroot(recent - birth); /* no need to add fuzz to recent */
+ n += chanskip[c];
+ return birth + n * n;
+}
+
+void pass_dochan(int c)
+{
+ datetime_sec birth;
+ struct prioq_elt pe;
+ static stralloc line = {0};
+ int match;
+
+ if (flagexitasap) return;
+
+ if (!pass[c].id)
+ {
+ if (!job_avail()) return;
+ if (!prioq_min(&pqchan[c],&pe)) return;
+ if (pe.dt > recent) return;
+ fnmake_chanaddr(pe.id,c);
+
+ prioq_delmin(&pqchan[c]);
+ pass[c].mpos = 0;
+ pass[c].fd = open_read(fn.s);
+ if (pass[c].fd == -1) goto trouble;
+ if (!getinfo(&line,&birth,pe.id)) { close(pass[c].fd); goto trouble; }
+ pass[c].id = pe.id;
+ substdio_fdbuf(&pass[c].ss,read,pass[c].fd,pass[c].buf,sizeof(pass[c].buf));
+ pass[c].j = job_open(pe.id,c);
+ jo[pass[c].j].retry = nextretry(birth,c);
+ jo[pass[c].j].flagdying = (recent > birth + lifetime);
+ while (!stralloc_copy(&jo[pass[c].j].sender,&line)) nomem();
+ }
+
+ if (!del_avail(c)) return;
+
+ if (getln(&pass[c].ss,&line,&match,'\0') == -1)
+ {
+ fnmake_chanaddr(pass[c].id,c);
+ log3("warning: trouble reading ",fn.s,"; will try again later\n");
+ close(pass[c].fd);
+ job_close(pass[c].j);
+ pass[c].id = 0;
+ return;
+ }
+ if (!match)
+ {
+ close(pass[c].fd);
+ jo[pass[c].j].flaghiteof = 1;
+ job_close(pass[c].j);
+ pass[c].id = 0;
+ return;
+ }
+ switch(line.s[0])
+ {
+ case 'T':
+ ++jo[pass[c].j].numtodo;
+ del_start(pass[c].j,pass[c].mpos,line.s + 1);
+ break;
+ case 'D':
+ break;
+ default:
+ fnmake_chanaddr(pass[c].id,c);
+ log3("warning: unknown record type in ",fn.s,"!\n");
+ close(pass[c].fd);
+ job_close(pass[c].j);
+ pass[c].id = 0;
+ return;
+ }
+
+ pass[c].mpos += line.len;
+ return;
+
+ trouble:
+ log3("warning: trouble opening ",fn.s,"; will try again later\n");
+ pe.dt = recent + SLEEP_SYSFAIL;
+ while (!prioq_insert(&pqchan[c],&pe)) nomem();
+}
+
+void messdone(unsigned long id)
+{
+ char ch;
+ int c;
+ struct prioq_elt pe;
+ struct stat st;
+
+ for (c = 0;c < CHANNELS;++c)
+ {
+ fnmake_chanaddr(id,c);
+ if (stat(fn.s,&st) == 0) return; /* false alarm; consequence of HOPEFULLY */
+ if (errno != error_noent)
+ {
+ log3("warning: unable to stat ",fn.s,"; will try again later\n");
+ goto fail;
+ }
+ }
+
+ fnmake_todo(id);
+ if (stat(fn.s,&st) == 0) return;
+ if (errno != error_noent)
+ {
+ log3("warning: unable to stat ",fn.s,"; will try again later\n");
+ goto fail;
+ }
+
+ fnmake_info(id);
+ if (stat(fn.s,&st) == -1)
+ {
+ if (errno == error_noent) return;
+ log3("warning: unable to stat ",fn.s,"; will try again later\n");
+ goto fail;
+ }
+
+ /* -todo +info -local -remote ?bounce */
+ if (!injectbounce(id))
+ goto fail; /* injectbounce() produced error message */
+
+ strnum3[fmt_ulong(strnum3,id)] = 0;
+ log3("end msg ",strnum3,"\n");
+
+ /* -todo +info -local -remote -bounce */
+ fnmake_info(id);
+ if (unlink(fn.s) == -1)
+ {
+ log3("warning: unable to unlink ",fn.s,"; will try again later\n");
+ goto fail;
+ }
+
+ /* -todo -info -local -remote -bounce; we can relax */
+ fnmake_foop(id);
+ if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
+ if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
+ if (ch != '+')
+ log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
+
+ return;
+
+ fail:
+ pe.id = id; pe.dt = now() + SLEEP_SYSFAIL;
+ while (!prioq_insert(&pqdone,&pe)) nomem();
+}
+
+void pass_do()
+{
+ int c;
+ struct prioq_elt pe;
+
+ for (c = 0;c < CHANNELS;++c) pass_dochan(c);
+ if (prioq_min(&pqfail,&pe))
+ if (pe.dt <= recent)
+ {
+ prioq_delmin(&pqfail);
+ pqadd(pe.id);
+ }
+ if (prioq_min(&pqdone,&pe))
+ if (pe.dt <= recent)
+ {
+ prioq_delmin(&pqdone);
+ messdone(pe.id);
+ }
+}
diff --git a/src/mxf-send/todo.c b/src/mxf-send/todo.c
@@ -0,0 +1,222 @@
+#include "mxf-send.h"
+/* this file is too long ---------------------------------------------- TODO */
+
+datetime_sec nexttodorun;
+DIR *tododir; /* if 0, have to opendir again */
+stralloc todoline = {0};
+char todobuf[SUBSTDIO_INSIZE];
+char todobufinfo[512];
+char todobufchan[CHANNELS][1024];
+
+void todo_init()
+{
+ tododir = 0;
+ nexttodorun = now();
+ trigger_set();
+}
+
+void todo_selprep(int *nfds, fd_set *rfds, datetime_sec *wakeup)
+{
+ if (flagexitasap) return;
+ trigger_selprep(nfds,rfds);
+ if (tododir) *wakeup = 0;
+ if (*wakeup > nexttodorun) *wakeup = nexttodorun;
+}
+
+// XXX XXX - need to refactor this into a todo_dochan
+void todo_do(fd_set *rfds)
+{
+ struct stat st;
+ substdio ss; int fd;
+ substdio ssinfo; int fdinfo;
+ substdio sschan[CHANNELS];
+ int fdchan[CHANNELS];
+ int flagchan[CHANNELS];
+ struct prioq_elt pe;
+ char ch;
+ int match;
+ unsigned long id;
+ unsigned int len;
+ direntry *dent;
+ int c;
+ unsigned long uid;
+ unsigned long pid;
+
+ fd = -1;
+ fdinfo = -1;
+ for (c = 0;c < CHANNELS;++c) fdchan[c] = -1;
+
+ if (flagexitasap) return;
+
+ if (!tododir)
+ {
+ if (!trigger_pulled(rfds))
+ if (recent < nexttodorun)
+ return;
+ trigger_set();
+ tododir = opendir("todo");
+ if (!tododir)
+ {
+ pausedir("todo");
+ return;
+ }
+ nexttodorun = recent + SLEEP_TODO;
+ }
+
+ dent = readdir(tododir);
+ if (!dent)
+ {
+ closedir(tododir);
+ tododir = 0;
+ return;
+ }
+ if (str_equal(dent->d_name,".")) return;
+ if (str_equal(dent->d_name,"..")) return;
+ len = scan_ulong(dent->d_name,&id);
+ if (!len || dent->d_name[len]) return;
+
+ fnmake_todo(id);
+
+ fd = open_read(fn.s);
+ if (fd == -1) { log3("warning: unable to open ",fn.s,"\n"); return; }
+
+ fnmake_mess(id);
+ /* just for the statistics */
+ if (stat(fn.s,&st) == -1)
+ { log3("warning: unable to stat ",fn.s,"\n"); goto fail; }
+
+ for (c = 0;c < CHANNELS;++c)
+ {
+ fnmake_chanaddr(id,c);
+ if (unlink(fn.s) == -1) if (errno != error_noent)
+ { log3("warning: unable to unlink ",fn.s,"\n"); goto fail; }
+ }
+
+ fnmake_info(id);
+ if (unlink(fn.s) == -1) if (errno != error_noent)
+ { log3("warning: unable to unlink ",fn.s,"\n"); goto fail; }
+
+ fdinfo = open_excl(fn.s);
+ if (fdinfo == -1)
+ { log3("warning: unable to create ",fn.s,"\n"); goto fail; }
+
+ strnum3[fmt_ulong(strnum3,id)] = 0;
+ log3("new msg ",strnum3,"\n");
+
+ for (c = 0;c < CHANNELS;++c) flagchan[c] = 0;
+
+ substdio_fdbuf(&ss,read,fd,todobuf,sizeof(todobuf));
+ substdio_fdbuf(&ssinfo,write,fdinfo,todobufinfo,sizeof(todobufinfo));
+
+ uid = 0;
+ pid = 0;
+
+ for (;;)
+ {
+ if (getln(&ss,&todoline,&match,'\0') == -1)
+ {
+ /* perhaps we're out of memory, perhaps an I/O error */
+ fnmake_todo(id);
+ log3("warning: trouble reading ",fn.s,"\n"); goto fail;
+ }
+ if (!match) break;
+
+ switch(todoline.s[0])
+ {
+ case 'u':
+ scan_ulong(todoline.s + 1,&uid);
+ break;
+ case 'p':
+ scan_ulong(todoline.s + 1,&pid);
+ break;
+ case 'F':
+ if (substdio_putflush(&ssinfo,todoline.s,todoline.len) == -1)
+ {
+ fnmake_info(id);
+ log3("warning: trouble writing to ",fn.s,"\n"); goto fail;
+ }
+ qslog2("info msg ",strnum3);
+ strnum2[fmt_ulong(strnum2,(unsigned long) st.st_size)] = 0;
+ qslog2(": bytes ",strnum2);
+ log1(" from <"); logsafe(todoline.s + 1);
+ strnum2[fmt_ulong(strnum2,pid)] = 0;
+ qslog2("> qp ",strnum2);
+ strnum2[fmt_ulong(strnum2,uid)] = 0;
+ qslog2(" uid ",strnum2);
+ log1("\n");
+ break;
+ case 'T':
+ switch(rewrite(todoline.s + 1))
+ {
+ case 0: nomem(); goto fail;
+ case 2: c = 1; break;
+ default: c = 0; break;
+ }
+ if (fdchan[c] == -1)
+ {
+ fnmake_chanaddr(id,c);
+ fdchan[c] = open_excl(fn.s);
+ if (fdchan[c] == -1)
+ { log3("warning: unable to create ",fn.s,"\n"); goto fail; }
+ substdio_fdbuf(&sschan[c]
+ ,write,fdchan[c],todobufchan[c],sizeof(todobufchan[c]));
+ flagchan[c] = 1;
+ }
+ if (substdio_bput(&sschan[c],rwline.s,rwline.len) == -1)
+ {
+ fnmake_chanaddr(id,c);
+ log3("warning: trouble writing to ",fn.s,"\n"); goto fail;
+ }
+ break;
+ default:
+ fnmake_todo(id);
+ log3("warning: unknown record type in ",fn.s,"\n"); goto fail;
+ }
+ }
+
+ close(fd); fd = -1;
+
+ fnmake_info(id);
+ if (substdio_flush(&ssinfo) == -1)
+ { log3("warning: trouble writing to ",fn.s,"\n"); goto fail; }
+ if (fsync(fdinfo) == -1)
+ { log3("warning: trouble fsyncing ",fn.s,"\n"); goto fail; }
+ close(fdinfo); fdinfo = -1;
+
+ for (c = 0;c < CHANNELS;++c)
+ if (fdchan[c] != -1)
+ {
+ fnmake_chanaddr(id,c);
+ if (substdio_flush(&sschan[c]) == -1)
+ { log3("warning: trouble writing to ",fn.s,"\n"); goto fail; }
+ if (fsync(fdchan[c]) == -1)
+ { log3("warning: trouble fsyncing ",fn.s,"\n"); goto fail; }
+ close(fdchan[c]); fdchan[c] = -1;
+ }
+
+ fnmake_todo(id);
+ if (substdio_putflush(&sstoqc,fn.s,fn.len) == -1) { cleandied(); return; }
+ if (substdio_get(&ssfromqc,&ch,1) != 1) { cleandied(); return; }
+ if (ch != '+')
+ {
+ log3("warning: qmail-clean unable to clean up ",fn.s,"\n");
+ return;
+ }
+
+ pe.id = id; pe.dt = now();
+ for (c = 0;c < CHANNELS;++c)
+ if (flagchan[c])
+ while (!prioq_insert(&pqchan[c],&pe)) nomem();
+
+ for (c = 0;c < CHANNELS;++c) if (flagchan[c]) break;
+ if (c == CHANNELS)
+ while (!prioq_insert(&pqdone,&pe)) nomem();
+
+ return;
+
+ fail:
+ if (fd != -1) close(fd);
+ if (fdinfo != -1) close(fdinfo);
+ for (c = 0;c < CHANNELS;++c)
+ if (fdchan[c] != -1) close(fdchan[c]);
+}
diff --git a/src/qmail-send.c b/src/qmail-send.c
@@ -701,13 +701,14 @@ unsigned long id;
while (!quote2("ed,bouncerecip)) nomem();
qmail_put(&qqt,quoted.s,quoted.len);
qmail_puts(&qqt,"\n\
-Subject: failure notice\n\
+Subject: Failure Notice\n\
\n\
-Hi. This is the qmail-send program at ");
+Hi. This is the qmail-send (NightmareMail queue manager) program at ");
qmail_put(&qqt,bouncehost.s,bouncehost.len);
qmail_puts(&qqt,*sender.s ? ".\n\
I'm afraid I wasn't able to deliver your message to the following addresses.\n\
-This is a permanent error; I've given up. Sorry it didn't work out.\n\
+This is a permanent error. I've given up. Sorry it didn't work out.\n\
+For further assistance, mail the postmaster.\n\
\n\
" : ".\n\
I tried to deliver a bounce message to this address, but the bounce bounced!\n\
diff --git a/src/qmail-smtpd.c b/src/qmail-smtpd.c
@@ -44,6 +44,7 @@ void die_alarm() { out("451 timeout (#4.4.2)\r\n"); flush(); _exit(1); }
void die_nomem() { out("421 out of memory (#4.3.0)\r\n"); flush(); _exit(1); }
void die_control() { out("421 unable to read controls (#4.3.0)\r\n"); flush(); _exit(1); }
void die_ipme() { out("421 unable to figure out my IP addresses (#4.3.0)\r\n"); flush(); _exit(1); }
+void die_ip6me() { out("421 unable to figure out my IPv6 addresses (#4.3.0)\r\n"); flush(); _exit(1); }
void straynewline() { out("451 See https://cr.yp.to/docs/smtplf.html.\r\n"); flush(); _exit(1); }
void err_bmf() { out("553 sorry, your envelope sender is in my badmailfrom list (#5.7.1)\r\n"); }
@@ -150,8 +151,7 @@ extern int realrcptto_deny();
stralloc addr = {0}; /* will be 0-terminated, if addrparse returns 1 */
-int addrparse(arg)
-char *arg;
+int addrparse(char *arg)
{
int i;
char ch;
@@ -159,7 +159,7 @@ char *arg;
struct ip_address ip;
int flagesc;
int flagquoted;
-
+
terminator = '>';
i = str_chr(arg,'<');
if (arg[i])
@@ -238,6 +238,7 @@ int dnsblcheck()
static stralloc dnsblbyte = {0};
static stralloc dnsblrev = {0};
static ipalloc dnsblip = {0};
+ static ip6alloc dnsblip6 = {0};
static stralloc dnsbllist = {0};
ch = remoteip;
@@ -477,6 +478,7 @@ int main(void)
if (chdir(auto_qmail) == -1) die_control();
setup();
if (ipme_init() != 1) die_ipme();
+ if (ip6me_init() != 1) die_ip6me();
smtp_greet("220 ");
out(" ESMTP\r\n");
if (commands(&ssin,&smtpcommands) == 0) die_read();