s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
spawn.c
Go to the documentation of this file.
1#include <sys/types.h>
2#include <sys/stat.h>
3#include <unistd.h>
4#include "alloc.h"
5#include "sig.h"
6#include "wait.h"
7#include "buffer.h"
8#include "byte.h"
9#include "str.h"
10#include "stralloc.h"
11#include "select.h"
12#include "exit.h"
13#include "fd.h"
14#include "open.h"
15#include "error.h"
16#include "auto_qmail.h"
17#include "auto_uids.h"
18#include "auto_spawn.h"
19
20extern int truncreport;
21extern int spawn();
22extern void report();
23extern void initialize();
24
26 {
27 int used;
28 int fdin; /* pipe input */
29 int pid; /* zero if child is dead */
30 int wstat; /* if !pid: status of child */
31 int fdout; /* pipe output, -1 if !pid; delays eof until after death */
32 stralloc output;
33 }
34;
35
36struct delivery *d;
37
38void sigchld()
39{
40 int wstat;
41 int pid;
42 int i;
43
44 while ((pid = wait_nohang(&wstat)) > 0)
45 for (i = 0; i < auto_spawn; ++i)
46 if (d[i].used)
47 if (d[i].pid == pid) {
48 close(d[i].fdout); d[i].fdout = -1;
49 d[i].wstat = wstat; d[i].pid = 0;
50 }
51}
52
54
55ssize_t okwrite(int fd,char *buf,int n)
56{
57 int w;
58 if (!flagwriting) return n;
59 w = write(fd,buf,n);
60 if (w != -1) return w;
61 if (errno == EINTR) return -1;
62 flagwriting = 0; close(fd);
63 return n;
64}
65
67char outbuf[1024];
68buffer bo;
69
70int stage = 0; /* reading 0:delnum 1:messid 2:sender 3:recip */
71int flagabort = 0; /* if 1, everything except delnum is garbage */
73stralloc messid = {0};
74stralloc sender = {0};
75stralloc recip = {0};
76
77void err(char *s)
78{
79 char ch;
80
81 ch = delnum;
82 buffer_put(&bo,&ch,1);
83 buffer_puts(&bo,s);
84 buffer_putflush(&bo,"",1);
85}
86
87void docmd()
88{
89 int f;
90 int i;
91 int j;
92 int fdmess;
93 int pi[2];
94 struct stat st;
95
96 if (flagabort) { err("Zqmail-spawn: Out of memory. (#4.3.0)\n"); return; }
97 if (delnum < 0) { err("Zqmail-spawn: Internal error: delnum negative. (#4.3.5)\n"); return; }
98 if (delnum >= auto_spawn) { err("Zqmail-spawn: Internal error: delnum too big. (#4.3.5)\n"); return; }
99 if (d[delnum].used) { err("Zqmail-spawn: Internal error: delnum in use. (#4.3.5)\n"); return; }
100
101 for (i = 0; i < messid.len; ++i)
102 if (messid.s[i])
103 if (!i || (messid.s[i] != '/'))
104 if ((unsigned char) (messid.s[i] - '0') > 9)
105 { err("Dqmail-spawn: Internal error: messid has nonnumerics. (#5.3.5)\n"); return; }
106
107 if (messid.len > 100) { err("Dqmail-spawn: Internal error: messid too long. (#5.3.5)\n"); return; }
108 if (!messid.s[0]) { err("Dqmail-spawn: Internal error: messid too short. (#5.3.5)\n"); return; }
109
110 if (!stralloc_copys(&d[delnum].output,""))
111 { err("Zqmail-spawn: Out of memory. (#4.3.0)\n"); return; }
112
113 j = byte_rchr(recip.s,recip.len,'@');
114 if (j >= recip.len) { err("DSorry, address must include host name. (#5.1.3)\n"); return; }
115
116 fdmess = open_read(messid.s);
117 if (fdmess == -1) { err("Zqmail-spawn: Unable to open message. (#4.3.0)\n"); return; }
118
119 if (fstat(fdmess,&st) == -1)
120 { close(fdmess); err("Zqmail-spawn: Unable to fstat message. (#4.3.0)\n"); return; }
121 if ((st.st_mode & S_IFMT) != S_IFREG)
122 { close(fdmess); err("ZSorry, message has wrong type. (#4.3.5)\n"); return; }
123 if (st.st_uid != auto_uidq) /* aaack! qmailq has to be trusted! */
124 /* your security is already toast at this point. damage control... */
125 { close(fdmess); err("ZSorry, message has wrong owner. (#4.3.5)\n"); return; }
126
127 if (pipe(pi) == -1) {
128 if (errno == EFAULT) err("Zqmail-spawn: Unable to create pipe (wrong fildes). (#4.3.0)\n");
129 else if (errno == EMFILE) err("Zqmail-spawn: Unable to create pipe (too many FDS). (#4.3.0)\n");
130 else if (errno == ENFILE) err("Zqmail-spawn: Unable to create pipe (system file table full). (#4.3.0)\n");
131 else if (errno == ENOMEM) err("Zqmail-spawn: Unable to create pipe (out of memory). (#4.3.0)\n");
132 else err("Zqmail-spawn: Unable to create pipe (unkown reason). (#4.3.0)\n");
133 close(fdmess);
134 return;
135 }
136
137 fd_coe(pi[0]);
138
139 f = spawn(fdmess,pi[1],sender.s,recip.s,j);
140 close(fdmess);
141
142 if (f == -1)
143 { close(pi[0]); close(pi[1]); err("Zqmail-spawn: Unable to fork. (#4.3.0)\n"); return; }
144
145 d[delnum].fdin = pi[0];
146 d[delnum].fdout = pi[1]; fd_coe(pi[1]);
147 d[delnum].pid = f;
148 d[delnum].used = 1;
149}
150
151char cmdbuf[1024];
152
153void getcmd()
154{
155 int i;
156 int r;
157 char ch;
158
159 r = read(0,cmdbuf,sizeof(cmdbuf));
160 if (r == 0)
161 { flagreading = 0; return; }
162 if (r == -1) {
163 if (errno != EINTR)
164 flagreading = 0;
165 return;
166 }
167
168 for (i = 0; i < r; ++i) {
169 ch = cmdbuf[i];
170 switch (stage) {
171 case 0:
172 delnum = (unsigned int) (unsigned char) ch;
173 messid.len = 0; stage = 1; break;
174 case 1:
175 if (!stralloc_append(&messid,&ch)) flagabort = 1;
176 if (ch) break;
177 sender.len = 0; stage = 2; break;
178 case 2:
179 if (!stralloc_append(&sender,&ch)) flagabort = 1;
180 if (ch) break;
181 recip.len = 0; stage = 3; break;
182 case 3:
183 if (!stralloc_append(&recip,&ch)) flagabort = 1;
184 if (ch) break;
185 docmd();
186 flagabort = 0; stage = 0; break;
187 }
188 }
189}
190
191char inbuf[128];
192
193int main(int argc,char **argv)
194{
195 char ch;
196 int i;
197 int r;
198 fd_set rfds;
199 int nfds;
200
201 if (chdir(auto_qmail) == -1) _exit(110);
202 if (chdir("queue/mess") == -1) _exit(110);
203 if (!stralloc_copys(&messid,"")) _exit(111);
204 if (!stralloc_copys(&sender,"")) _exit(111);
205 if (!stralloc_copys(&recip,"")) _exit(111);
206
207 d = (struct delivery *) alloc((auto_spawn + 10) * sizeof(struct delivery));
208 if (!d) _exit(111);
209
210 buffer_init(&bo,okwrite,1,outbuf,sizeof(outbuf));
211
212 sig_pipeignore();
213 sig_childcatch(sigchld);
214
215 initialize(argc,argv);
216
217 ch = auto_spawn;
218 buffer_putflush(&bo,&ch,1);
219
220 for (i = 0; i < auto_spawn; ++i)
221 { d[i].used = 0; d[i].output.s = 0; }
222
223 for (;;) {
224 if (!flagreading) {
225 for (i = 0; i < auto_spawn; ++i) if (d[i].used) break;
226 if (i >= auto_spawn) _exit(0);
227 }
228 sig_childunblock();
229
230 FD_ZERO(&rfds);
231 if (flagreading) FD_SET(0,&rfds);
232 nfds = 1;
233
234 for (i = 0; i < auto_spawn; ++i)
235 if (d[i].used) {
236 FD_SET(d[i].fdin,&rfds);
237 if (d[i].fdin >= nfds)
238 nfds = d[i].fdin + 1;
239 }
240
241 r = select(nfds,&rfds,(fd_set *) 0,(fd_set *) 0,(struct timeval *) 0);
242 sig_childblock();
243
244 if (r != -1) {
245 if (flagreading)
246 if (FD_ISSET(0,&rfds)) getcmd();
247 for (i = 0; i < auto_spawn; ++i)
248 if (d[i].used)
249
250 if (FD_ISSET(d[i].fdin,&rfds)) {
251 r = read(d[i].fdin,inbuf,128);
252 if (r == -1)
253 continue; /* read error on a readable pipe? be serious */
254 if (r == 0) {
255 ch = i;
256 buffer_put(&bo,&ch,1);
257 report(&bo,d[i].wstat,d[i].output.s,d[i].output.len);
258 buffer_put(&bo,"",1);
259 buffer_flush(&bo);
260 close(d[i].fdin); d[i].used = 0;
261 continue;
262 }
263 while (!stralloc_readyplus(&d[i].output,r))
264 sleep(10); /*XXX*/
265 byte_copy(d[i].output.s + d[i].output.len,r,inbuf);
266 d[i].output.len += r;
267 if (truncreport > 100)
268 if (d[i].output.len > truncreport) {
269 char *truncmess = "\nError report too long, sorry.\n";
270 d[i].output.len = truncreport - str_len(truncmess) - 3;
271 stralloc_cats(&d[i].output,truncmess);
272 }
273 }
274 }
275 }
276}
char auto_qmail[]
int auto_spawn
int auto_uidq
int main()
Definition: chkshsgr.c:6
int stralloc_copys(stralloc *, char const *)
void _exit()
char buf[100+FMT_ULONG]
Definition: hier.c:10
int fd
int
Definition: qmail-mrtg.c:26
int j
Definition: qmail-send.c:920
int fdin
Definition: qmail-todo.c:144
int fdout
Definition: qmail-todo.c:143
stralloc recip
Definition: spawn.c:75
int stage
Definition: spawn.c:70
void initialize()
char inbuf[128]
Definition: spawn.c:191
int delnum
Definition: spawn.c:72
int truncreport
Definition: qmail-lspawn.c:27
char cmdbuf[1024]
Definition: spawn.c:151
stralloc sender
Definition: spawn.c:74
void err(char *s)
Definition: spawn.c:77
char outbuf[1024]
Definition: spawn.c:67
struct delivery * d
Definition: spawn.c:36
void sigchld()
Definition: spawn.c:38
int flagabort
Definition: spawn.c:71
void getcmd()
Definition: spawn.c:153
int flagwriting
Definition: spawn.c:53
stralloc messid
Definition: spawn.c:73
int spawn()
buffer bo
Definition: spawn.c:68
ssize_t okwrite(int fd, char *buf, int n)
Definition: spawn.c:55
void report()
int flagreading
Definition: spawn.c:66
void docmd()
Definition: spawn.c:87
Definition: spawn.c:26
stralloc output
Definition: spawn.c:32
int used
Definition: spawn.c:27
int fdin
Definition: spawn.c:28
int fdout
Definition: spawn.c:31
int pid
Definition: spawn.c:29
int wstat
Definition: spawn.c:30
void write()