Debianization and various other fixes.
[ezmlm] / qmail.c
CommitLineData
25a55efe 1#include <stdlib.h>
5b62e993
MW
2#include "substdio.h"
3#include "readwrite.h"
4#include "wait.h"
5#include "exit.h"
6#include "fork.h"
7#include "fd.h"
8#include "qmail.h"
9#include "auto_qmail.h"
f8beb284
MW
10#include "alloc.h"
11#include "stralloc.h"
12#include "idx.h"
5b62e993 13
25a55efe
MW
14static char *binqqargs[2] = { 0, 0 } ;
15
16static void setup_qqargs()
17{
18 if(!binqqargs[0])
19 binqqargs[0] = getenv("QMAILQUEUE");
20 if(!binqqargs[0])
21 binqqargs[0] = "/usr/sbin/qmail-queue";
22}
5b62e993 23
f8beb284 24int qmail_open(qq,sa)
5b62e993 25struct qmail *qq;
f8beb284 26stralloc *sa;
5b62e993
MW
27{
28 int pim[2];
29 int pie[2];
f8beb284
MW
30 unsigned i,j;
31 char **cpp;
5b62e993 32
25a55efe
MW
33 setup_qqargs();
34
f8beb284 35 qq->msgbytes = 0L;
5b62e993
MW
36 if (pipe(pim) == -1) return -1;
37 if (pipe(pie) == -1) { close(pim[0]); close(pim[1]); return -1; }
f8beb284 38
5b62e993
MW
39 switch(qq->pid = vfork()) {
40 case -1:
41 close(pim[0]); close(pim[1]);
42 close(pie[0]); close(pie[1]);
43 return -1;
44 case 0:
45 close(pim[1]);
46 close(pie[1]);
47 if (fd_move(0,pim[0]) == -1) _exit(120);
48 if (fd_move(1,pie[0]) == -1) _exit(120);
f8beb284
MW
49 if (chdir(auto_qmail) == -1) _exit(61);
50 j = 2; /* empty sa - qmqpc c control args */
51 if (sa) { /* count args */
52 for (i = 0; i + 1 < sa->len; i++) {
53 if (sa->s[i] == '\0') j++;
54 } /* make space */
55 if (!(cpp = (char **) alloc(j * sizeof (char *)))) _exit(51);
56 cpp[0] = PROG_QMAIL_QMQPC;
57 cpp[j - 1] = (char *) 0;
58 if (sa->len) cpp[1] = sa->s;
59 j = 2;
60 for (i = 0; i + 1 < sa->len; i++) {
61 if (sa->s[i] == '\0')
62 cpp[j++] = sa->s + i + 1; /* build args */
63 }
64 execv(*cpp,cpp);
65 } else
66 execv(*binqqargs,binqqargs);
5b62e993
MW
67 _exit(120);
68 }
69
70 qq->fdm = pim[1]; close(pim[0]);
71 qq->fde = pie[1]; close(pie[0]);
72 substdio_fdbuf(&qq->ss,write,qq->fdm,qq->buf,sizeof(qq->buf));
73 qq->flagerr = 0;
74 return 0;
75}
76
77unsigned long qmail_qp(qq) struct qmail *qq;
78{
79 return qq->pid;
80}
81
82void qmail_fail(qq) struct qmail *qq;
83{
84 qq->flagerr = 1;
85}
86
87void qmail_put(qq,s,len) struct qmail *qq; char *s; int len;
88{
89 if (!qq->flagerr) if (substdio_put(&qq->ss,s,len) == -1) qq->flagerr = 1;
f8beb284 90 qq->msgbytes += len;
5b62e993
MW
91}
92
93void qmail_puts(qq,s) struct qmail *qq; char *s;
94{
f8beb284
MW
95 register int len;
96 if (!qq->flagerr) {
97 len = str_len(s);
98 if (substdio_put(&qq->ss,s,len) == -1) qq->flagerr = 1;
99 }
100 qq->msgbytes += len;
5b62e993
MW
101}
102
103void qmail_from(qq,s) struct qmail *qq; char *s;
104{
105 if (substdio_flush(&qq->ss) == -1) qq->flagerr = 1;
106 close(qq->fdm);
107 substdio_fdbuf(&qq->ss,write,qq->fde,qq->buf,sizeof(qq->buf));
108 qmail_put(qq,"F",1);
109 qmail_puts(qq,s);
110 qmail_put(qq,"",1);
111}
112
113void qmail_to(qq,s) struct qmail *qq; char *s;
114{
115 qmail_put(qq,"T",1);
116 qmail_puts(qq,s);
117 qmail_put(qq,"",1);
118}
119
f8beb284 120char *qmail_close(qq)
5b62e993
MW
121struct qmail *qq;
122{
123 int wstat;
f8beb284 124 int exitcode;
5b62e993
MW
125
126 qmail_put(qq,"",1);
127 if (!qq->flagerr) if (substdio_flush(&qq->ss) == -1) qq->flagerr = 1;
128 close(qq->fde);
129
f8beb284
MW
130 if (wait_pid(&wstat,qq->pid) != qq->pid)
131 return "Zqq waitpid surprise (#4.3.0)";
132 if (wait_crashed(wstat))
133 return "Zqq crashed (#4.3.0)";
134 exitcode = wait_exitcode(wstat);
135
136 switch(exitcode) {
137 case 115: /* compatibility */
138 case 11: return "Denvelope address too long for qq (#5.1.3)";
139 case 31: return "Dmail server permanently rejected message (#5.3.0)";
140 case 51: return "Zqq out of memory (#4.3.0)";
141 case 52: return "Zqq timeout (#4.3.0)";
142 case 53: return "Zqq write error or disk full (#4.3.0)";
143 case 0: if (!qq->flagerr) return ""; /* fall through */
144 case 54: return "Zqq read error (#4.3.0)";
145 case 55: return "Zqq unable to read configuration (#4.3.0)";
146 case 56: return "Zqq trouble making network connection (#4.3.0)";
147 case 61: return "Zqq trouble in home directory (#4.3.0)";
148 case 63:
149 case 64:
150 case 65:
151 case 66:
152 case 62: return "Zqq trouble creating files in queue (#4.3.0)";
153 case 71: return "Zmail server temporarily rejected message (#4.3.0)";
154 case 72: return "Zconnection to mail server timed out (#4.4.1)";
155 case 73: return "Zconnection to mail server rejected (#4.4.1)";
156 case 74: return "Zcommunication with mail server failed (#4.4.2)";
157 case 91: /* fall through */
158 case 81: return "Zqq internal bug (#4.3.0)";
159 case 120: return "Zunable to exec qq (#4.3.0)";
160 default:
161 if ((exitcode >= 11) && (exitcode <= 40))
162 return "Dqq permanent problem (#5.3.0)";
163 return "Zqq temporary problem (#4.3.0)";
5b62e993
MW
164 }
165}