summaryrefslogtreecommitdiff
path: root/condredirect.c
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-06-01 18:58:45 -0700
committerJohn Denker <jsd@av8n.com>2012-06-01 18:58:45 -0700
commitb732a73bc773789894466b0e5320b2f1fe42c7e9 (patch)
tree385358983f064a1f10a5080b33a3ba13010886db /condredirect.c
parent634d365a03cb0581a062cd3cf4db9ae69f1cde26 (diff)
original, as downloaded from http://www.qmail.org/netqmail-1.06.tar.gz
Diffstat (limited to 'condredirect.c')
-rw-r--r--condredirect.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/condredirect.c b/condredirect.c
new file mode 100644
index 0000000..593d2f5
--- /dev/null
+++ b/condredirect.c
@@ -0,0 +1,85 @@
+#include "sig.h"
+#include "readwrite.h"
+#include "exit.h"
+#include "env.h"
+#include "error.h"
+#include "fork.h"
+#include "wait.h"
+#include "seek.h"
+#include "qmail.h"
+#include "strerr.h"
+#include "substdio.h"
+#include "fmt.h"
+
+#define FATAL "condredirect: fatal: "
+
+struct qmail qqt;
+
+int mywrite(fd,buf,len) int fd; char *buf; int len;
+{
+ qmail_put(&qqt,buf,len);
+ return len;
+}
+
+char inbuf[SUBSTDIO_INSIZE];
+char outbuf[1];
+substdio ssin = SUBSTDIO_FDBUF(read,0,inbuf,sizeof inbuf);
+substdio ssout = SUBSTDIO_FDBUF(mywrite,-1,outbuf,sizeof outbuf);
+
+char num[FMT_ULONG];
+
+void main(argc,argv)
+int argc;
+char **argv;
+{
+ char *sender;
+ char *dtline;
+ int pid;
+ int wstat;
+ char *qqx;
+
+ if (!argv[1] || !argv[2])
+ strerr_die1x(100,"condredirect: usage: condredirect newaddress program [ arg ... ]");
+
+ pid = fork();
+ if (pid == -1)
+ strerr_die2sys(111,FATAL,"unable to fork: ");
+ if (pid == 0) {
+ execvp(argv[2],argv + 2);
+ if (error_temp(errno)) _exit(111);
+ _exit(100);
+ }
+ if (wait_pid(&wstat,pid) == -1)
+ strerr_die2x(111,FATAL,"wait failed");
+ if (wait_crashed(wstat))
+ strerr_die2x(111,FATAL,"child crashed");
+ switch(wait_exitcode(wstat)) {
+ case 0: break;
+ case 111: strerr_die2x(111,FATAL,"temporary child error");
+ default: _exit(0);
+ }
+
+ if (seek_begin(0) == -1)
+ strerr_die2sys(111,FATAL,"unable to rewind: ");
+ sig_pipeignore();
+
+ sender = env_get("SENDER");
+ if (!sender) strerr_die2x(100,FATAL,"SENDER not set");
+ dtline = env_get("DTLINE");
+ if (!dtline) strerr_die2x(100,FATAL,"DTLINE not set");
+
+ if (qmail_open(&qqt) == -1)
+ strerr_die2sys(111,FATAL,"unable to fork: ");
+ qmail_puts(&qqt,dtline);
+ if (substdio_copy(&ssout,&ssin) != 0)
+ strerr_die2sys(111,FATAL,"unable to read message: ");
+ substdio_flush(&ssout);
+
+ num[fmt_ulong(num,qmail_qp(&qqt))] = 0;
+
+ qmail_from(&qqt,sender);
+ qmail_to(&qqt,argv[1]);
+ qqx = qmail_close(&qqt);
+ if (*qqx) strerr_die2x(*qqx == 'D' ? 100 : 111,FATAL,qqx + 1);
+ strerr_die2x(99,"condredirect: qp ",num);
+}