Commit | Line | Data |
---|---|---|
2117e02e MW |
1 | #include "sig.h" |
2 | #include "readwrite.h" | |
3 | #include "exit.h" | |
4 | #include "env.h" | |
5 | #include "error.h" | |
6 | #include "fork.h" | |
7 | #include "wait.h" | |
8 | #include "seek.h" | |
9 | #include "qmail.h" | |
10 | #include "stralloc.h" | |
11 | #include "subfd.h" | |
12 | #include "substdio.h" | |
13 | ||
14 | void die_success() { _exit(0); } | |
15 | void die_99() { _exit(99); } | |
16 | void die_perm(s) char *s; { substdio_putsflush(subfderr,s); _exit(100); } | |
17 | void die_temp(s) char *s; { substdio_putsflush(subfderr,s); _exit(111); } | |
18 | void die_nomem() { die_temp("condredirect: fatal: out of memory\n"); } | |
19 | ||
20 | struct qmail qqt; | |
21 | ||
22 | int mywrite(fd,buf,len) int fd; char *buf; int len; | |
23 | { | |
24 | qmail_put(&qqt,buf,len); | |
25 | return len; | |
26 | } | |
27 | ||
28 | substdio ssin; | |
29 | substdio ssout; | |
30 | char inbuf[SUBSTDIO_INSIZE]; | |
31 | char outbuf[16]; | |
32 | ||
33 | void main(argc,argv) | |
34 | int argc; | |
35 | char **argv; | |
36 | { | |
37 | char *sender; | |
38 | char *dtline; | |
39 | int pid; | |
40 | int wstat; | |
41 | ||
42 | if (!argv[1] || !argv[2]) | |
43 | die_perm("condredirect: usage: condredirect newaddress program arg ...\n"); | |
44 | ||
45 | switch(pid = fork()) | |
46 | { | |
47 | case -1: die_temp("condredirect: fatal: unable to fork\n"); | |
48 | case 0: | |
49 | execvp(argv[2],argv + 2); | |
50 | if (error_temp(errno)) _exit(111); | |
51 | _exit(100); | |
52 | } | |
53 | if (wait_pid(&wstat,pid) != pid) | |
54 | die_perm("condredirect: fatal: internal bug\n"); | |
55 | if (wait_crashed(wstat)) die_temp("condredirect: fatal: child crashed\n"); | |
56 | switch(wait_exitcode(wstat)) | |
57 | { | |
58 | case 0: break; | |
59 | case 111: die_temp("condredirect: fatal: temporary child error\n"); | |
60 | default: die_success(); | |
61 | } | |
62 | ||
63 | if (seek_begin(0) == -1) die_temp("condredirect: fatal: unable to rewind\n"); | |
64 | sig_pipeignore(); | |
65 | ||
66 | sender = env_get("SENDER"); | |
67 | if (!sender) die_perm("condredirect: fatal: SENDER not set\n"); | |
68 | dtline = env_get("DTLINE"); | |
69 | if (!dtline) die_perm("condredirect: fatal: DTLINE not set\n"); | |
70 | ||
71 | if (qmail_open(&qqt) == -1) die_temp("condredirect: fatal: unable to fork\n"); | |
72 | qmail_puts(&qqt,dtline); | |
73 | substdio_fdbuf(&ssin,read,0,inbuf,sizeof(inbuf)); | |
74 | substdio_fdbuf(&ssout,mywrite,-1,outbuf,sizeof(outbuf)); | |
75 | if (substdio_copy(&ssout,&ssin) != 0) | |
76 | die_temp("condredirect: fatal: error while reading message\n"); | |
77 | substdio_flush(&ssout); | |
78 | ||
79 | qmail_from(&qqt,sender); | |
80 | qmail_to(&qqt,argv[1]); | |
81 | switch(qmail_close(&qqt)) | |
82 | { | |
83 | case 0: die_99(); | |
84 | case QMAIL_TOOLONG: die_perm("condredirect: fatal: permanent qmail-queue error\n"); | |
85 | default: die_temp("condredirect: fatal: temporary qmail-queue error\n"); | |
86 | } | |
87 | } |