summaryrefslogtreecommitdiff
path: root/src/spawn.c
blob: 59ed63e2933d2a6c152f53c70ab63676d591b8a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include "spawn.h"

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "alloc.h"
#include "buffer.h"
#include "byte.h"
#include "error.h"
#include "exit.h"
#include "fd.h"
#include "open.h"
#include "select.h"
#include "sig.h"
#include "str.h"
#include "stralloc.h"
#include "wait.h"

#ifdef USE_CONFIG
  #include "fehsqm-config.h"
#else
  #include "auto_qmail.h"
  #include "auto_spawn.h"
  #include "auto_uids.h"
#endif

struct delivery {
  int used;
  int fdin;  /* pipe input */
  int pid;   /* zero if child is dead */
  int wstat; /* if !pid: status of child */
  int fdout; /* pipe output, -1 if !pid; delays eof until after death */
  stralloc output;
};

struct delivery *d;

static void sigchld()
{
  int wstat;
  int pid;
  int i;

  while ((pid = wait_nohang(&wstat)) > 0)
    for (i = 0; i < auto_spawn; ++i)
      if (d[i].used)
        if (d[i].pid == pid) {
          close(d[i].fdout);
          d[i].fdout = -1;
          d[i].wstat = wstat;
          d[i].pid = 0;
        }
}

int flagwriting = 1;

static ssize_t okwrite(int fd, char *buf, int n)
{
  int w;
  if (!flagwriting) return n;
  w = write(fd, buf, n);
  if (w != -1) return w;
  if (errno == EINTR) return -1;
  flagwriting = 0;
  close(fd);
  return n;
}

int flagreading = 1;
char outbuf[1024];
buffer bo;

int stage = 0;     /* reading 0:delnum 1:messid 2:sender 3:recip */
int flagabort = 0; /* if 1, everything except delnum is garbage */
int delnum;
stralloc messid = {0};
stralloc sender = {0};
stralloc recip = {0};

static void err(char *s)
{
  char ch;

  ch = delnum;
  buffer_put(&bo, &ch, 1);
  buffer_puts(&bo, s);
  buffer_putflush(&bo, "", 1);
}

static void docmd()
{
  int f;
  int i;
  int j;
  int fdmess;
  int pi[2];
  struct stat st;

  if (flagabort) {
    err("Zqmail-spawn: Out of memory. (#4.3.0)\n");
    return;
  }
  if (delnum < 0) {
    err("Zqmail-spawn: Internal error: delnum negative. (#4.3.5)\n");
    return;
  }
  if (delnum >= auto_spawn) {
    err("Zqmail-spawn: Internal error: delnum too big. (#4.3.5)\n");
    return;
  }
  if (d[delnum].used) {
    err("Zqmail-spawn: Internal error: delnum in use. (#4.3.5)\n");
    return;
  }

  for (i = 0; i < messid.len; ++i)
    if (messid.s[i])
      if (!i || (messid.s[i] != '/'))
        if ((unsigned char)(messid.s[i] - '0') > 9) {
          err("Dqmail-spawn: Internal error: messid has nonnumerics. (#5.3.5)\n");
          return;
        }

  if (messid.len > 100) {
    err("Dqmail-spawn: Internal error: messid too long. (#5.3.5)\n");
    return;
  }
  if (!messid.s[0]) {
    err("Dqmail-spawn: Internal error: messid too short. (#5.3.5)\n");
    return;
  }

  if (!stralloc_copys(&d[delnum].output, "")) {
    err("Zqmail-spawn: Out of memory. (#4.3.0)\n");
    return;
  }

  j = byte_rchr(recip.s, recip.len, '@');
  if (j >= recip.len) {
    err("DSorry, address must include host name. (#5.1.3)\n");
    return;
  }

  fdmess = open_read(messid.s);
  if (fdmess == -1) {
    err("Zqmail-spawn: Unable to open message. (#4.3.0)\n");
    return;
  }

  if (fstat(fdmess, &st) == -1) {
    close(fdmess);
    err("Zqmail-spawn: Unable to fstat message. (#4.3.0)\n");
    return;
  }
  if ((st.st_mode & S_IFMT) != S_IFREG) {
    close(fdmess);
    err("ZSorry, message has wrong type. (#4.3.5)\n");
    return;
  }
  if (st.st_uid != auto_uidq) /* aaack! qmailq has to be trusted! */
                              /* your security is already toast at this point. damage control... */
  {
    close(fdmess);
    err("ZSorry, message has wrong owner. (#4.3.5)\n");
    return;
  }

  if (pipe(pi) == -1) {
    if (errno == EFAULT)
      err("Zqmail-spawn: Unable to create pipe (wrong fildes). (#4.3.0)\n");
    else if (errno == EMFILE)
      err("Zqmail-spawn: Unable to create pipe (too many FDS). (#4.3.0)\n");
    else if (errno == ENFILE)
      err("Zqmail-spawn: Unable to create pipe (system file table full). (#4.3.0)\n");
    else if (errno == ENOMEM)
      err("Zqmail-spawn: Unable to create pipe (out of memory). (#4.3.0)\n");
    else
      err("Zqmail-spawn: Unable to create pipe (unkown reason). (#4.3.0)\n");
    close(fdmess);
    return;
  }

  fd_coe(pi[0]);

  f = spawn(fdmess, pi[1], sender.s, recip.s, j);
  close(fdmess);

  if (f == -1) {
    close(pi[0]);
    close(pi[1]);
    err("Zqmail-spawn: Unable to fork. (#4.3.0)\n");
    return;
  }

  d[delnum].fdin = pi[0];
  d[delnum].fdout = pi[1];
  fd_coe(pi[1]);
  d[delnum].pid = f;
  d[delnum].used = 1;
}

char cmdbuf[1024];

static void getcmd()
{
  int i;
  int r;
  char ch;

  r = read(0, cmdbuf, sizeof(cmdbuf));
  if (r == 0) {
    flagreading = 0;
    return;
  }
  if (r == -1) {
    if (errno != EINTR) flagreading = 0;
    return;
  }

  for (i = 0; i < r; ++i) {
    ch = cmdbuf[i];
    switch (stage) {
      case 0:
        delnum = (unsigned int)(unsigned char)ch;
        messid.len = 0;
        stage = 1;
        break;
      case 1:
        if (!stralloc_append(&messid, &ch)) flagabort = 1;
        if (ch) break;
        sender.len = 0;
        stage = 2;
        break;
      case 2:
        if (!stralloc_append(&sender, &ch)) flagabort = 1;
        if (ch) break;
        recip.len = 0;
        stage = 3;
        break;
      case 3:
        if (!stralloc_append(&recip, &ch)) flagabort = 1;
        if (ch) break;
        docmd();
        flagabort = 0;
        stage = 0;
        break;
    }
  }
}

char inbuf[128];

int main(int argc, char **argv)
{
  char ch;
  int i;
  int r;
  fd_set rfds;
  int nfds;

  if (chdir(auto_qmail) == -1) _exit(110);
  if (chdir("queue/mess") == -1) _exit(110);
  if (!stralloc_copys(&messid, "")) _exit(111);
  if (!stralloc_copys(&sender, "")) _exit(111);
  if (!stralloc_copys(&recip, "")) _exit(111);

  d = (struct delivery *)alloc((auto_spawn + 10) * sizeof(struct delivery));
  if (!d) _exit(111);

  buffer_init(&bo, okwrite, 1, outbuf, sizeof(outbuf));

  sig_pipeignore();
  sig_childcatch(sigchld);

  initialize(argc, argv);

  ch = auto_spawn;
  buffer_putflush(&bo, &ch, 1);

  for (i = 0; i < auto_spawn; ++i) {
    d[i].used = 0;
    d[i].output.s = 0;
  }

  for (;;) {
    if (!flagreading) {
      for (i = 0; i < auto_spawn; ++i)
        if (d[i].used) break;
      if (i >= auto_spawn) _exit(0);
    }
    sig_childunblock();

    FD_ZERO(&rfds);
    if (flagreading) FD_SET(0, &rfds);
    nfds = 1;

    for (i = 0; i < auto_spawn; ++i)
      if (d[i].used) {
        FD_SET(d[i].fdin, &rfds);
        if (d[i].fdin >= nfds) nfds = d[i].fdin + 1;
      }

    r = select(nfds, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0);
    sig_childblock();

    if (r != -1) {
      if (flagreading)
        if (FD_ISSET(0, &rfds)) getcmd();
      for (i = 0; i < auto_spawn; ++i)
        if (d[i].used)

          if (FD_ISSET(d[i].fdin, &rfds)) {
            r = read(d[i].fdin, inbuf, 128);
            if (r == -1) continue; /* read error on a readable pipe? be serious */
            if (r == 0) {
              ch = i;
              buffer_put(&bo, &ch, 1);
              report(&bo, d[i].wstat, d[i].output.s, d[i].output.len);
              buffer_put(&bo, "", 1);
              buffer_flush(&bo);
              close(d[i].fdin);
              d[i].used = 0;
              continue;
            }
            while (!stralloc_readyplus(&d[i].output, r)) sleep(10); /*XXX*/
            byte_copy(d[i].output.s + d[i].output.len, r, inbuf);
            d[i].output.len += r;
            if (truncreport > 100)
              if (d[i].output.len > truncreport) {
                char *truncmess = "\nError report too long, sorry.\n";
                d[i].output.len = truncreport - str_len(truncmess) - 3;
                stralloc_cats(&d[i].output, truncmess);
              }
          }
    }
  }
}