summaryrefslogtreecommitdiff
path: root/src/qmail-lspawn.c
blob: cbf402ee878fa7db280627c78dccd3bab0482a84 (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
#include <unistd.h>

#include "buffer.h"
#include "byte.h"
#include "case.h"
#include "cdbread.h"
#include "error.h"
#include "exit.h"
#include "fd.h"
#include "open.h"
#include "prot.h"
#include "readclose.h"
#include "scan.h"
#include "stralloc.h"
#include "wait.h"

#include "qlx.h"
#include "spawn.h"

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

char *aliasempty;

void initialize(int argc, char **argv)
{
  aliasempty = argv[1];
  if (!aliasempty) _exit(100);
}

int truncreport = 3000;

void report(buffer *log, int wstat, char *s, int len)
{
  int i;
  if (wait_crashed(wstat)) {
    buffer_putsflush(log, "Zqmail-lspawn: qmail-local crashed.\n");
    return;
  }

  switch (wait_exitcode(wstat)) {
    case QLX_CDB:     buffer_putsflush(log, "Zqmail-lspawn: Trouble reading users/assign.cdb.\n"); return;
    case QLX_NOMEM:   buffer_putsflush(log, "Zqmail-lspawn: Out of memory.\n"); return;
    case QLX_SYS:     buffer_putsflush(log, "Zqmail-lspawn: Temporary failure.\n"); return;
    case QLX_NOALIAS: buffer_putsflush(log, "Zqmail-lspawn: Unable to find alias user!\n"); return;
    case QLX_ROOT:
      buffer_putsflush(log, "Zqmail-spawn: Not allowed to perform deliveries as root.\n");
      return;
    case QLX_USAGE:    buffer_putsflush(log, "Zqmail-spawn: Internal bug.\n"); return;
    case QLX_NFS:      buffer_putsflush(log, "Zqmail-spawn: NFS failure in qmail-local.\n"); return;
    case QLX_EXECHARD: buffer_putsflush(log, "Dqmail-spawn: Unable to run qmail-local.\n"); return;
    case QLX_EXECSOFT: buffer_putsflush(log, "Zqmail-spawn: Unable to run qmail-local.\n"); return;
    case QLX_EXECPW:   buffer_putsflush(log, "Zqmail-spawn: Unable to run qmail-getpw.\n"); return;
    case 111:
    case 71:
    case 74:
    case 75:           buffer_put(log, "Z", 1); break;
    case 0:            buffer_put(log, "K", 1); break;
    case 100:
    default:           buffer_put(log, "D", 1); break;
  }

  for (i = 0; i < len; ++i)
    if (!s[i]) break;

  buffer_put(log, s, i);
}

stralloc lower = {0};
stralloc nughde = {0};
stralloc wildchars = {0};

static struct cdb c;

static void nughde_get(char *local)
{
  char *(args[3]);
  int pi[2];
  int gpwpid;
  int gpwstat;
  int r;
  int fd;
  int flagwild;

  if (!stralloc_copys(&lower, "!")) _exit(QLX_NOMEM);
  if (!stralloc_cats(&lower, local)) _exit(QLX_NOMEM);
  if (!stralloc_0(&lower)) _exit(QLX_NOMEM);
  case_lowerb(lower.s, lower.len);

  if (!stralloc_copys(&nughde, "")) _exit(QLX_NOMEM);

  fd = open_read("users/assign.cdb");
  if (fd == -1)
    if (errno != ENOENT) _exit(QLX_CDB);

  if (fd != -1) {
    unsigned int i;

    cdb_init(&c, fd);
    r = cdb_find(&c, "", 0);
    if (r != 1) _exit(QLX_CDB);
    if (!stralloc_ready(&wildchars, cdb_datalen(&c))) _exit(QLX_NOMEM);
    wildchars.len = cdb_datalen(&c);
    if (cdb_read(&c, wildchars.s, wildchars.len, cdb_datapos(&c)) == -1) _exit(QLX_CDB);

    i = lower.len;
    flagwild = 0;

    do {
      /* i > 0 */
      if (!flagwild || (i == 1)
          || (byte_chr(wildchars.s, wildchars.len, lower.s[i - 1]) < wildchars.len))
      {
        r = cdb_find(&c, lower.s, i);
        if (r == -1) _exit(QLX_CDB);
        if (r == 1) {
          if (!stralloc_ready(&nughde, cdb_datalen(&c))) _exit(QLX_NOMEM);
          nughde.len = cdb_datalen(&c);
          if (cdb_read(&c, nughde.s, nughde.len, cdb_datapos(&c)) == -1) _exit(QLX_CDB);
          if (flagwild)
            if (!stralloc_cats(&nughde, local + i - 1)) _exit(QLX_NOMEM);
          if (!stralloc_0(&nughde)) _exit(QLX_NOMEM);
          close(fd);
          return;
        }
      }
      --i;
      flagwild = 1;
    } while (i);

    close(fd);
  }

  if (pipe(pi) == -1) _exit(QLX_SYS);

  args[0] = "bin/qmail-getpw";
  args[1] = local;
  args[2] = 0;
  switch (gpwpid = fork()) {
    case -1: _exit(QLX_SYS);
    case 0:
      if (prot_gid(auto_gidn) == -1) _exit(QLX_USAGE);
      if (prot_uid(auto_uidp) == -1) _exit(QLX_USAGE);
      close(pi[0]);
      if (fd_move(1, pi[1]) == -1) _exit(QLX_SYS);
      execv(*args, args);
      _exit(QLX_EXECPW);
  }
  close(pi[1]);

  if (readclose_append(pi[0], &nughde, 128) == -1) _exit(QLX_SYS);

  if (wait_pid(&gpwstat, gpwpid) != -1) {
    if (wait_crashed(gpwstat)) _exit(QLX_SYS);
    if (wait_exitcode(gpwstat) != 0) _exit(wait_exitcode(gpwstat));
  }
}

int spawn(int fdmess, int fdout, const char *s, char *r, const int at)
{
  int f;

  if (!(f = fork())) {
    char *(args[11]);
    unsigned long u;
    int n;
    int uid;
    int gid;
    char *x;
    unsigned int xlen;

    r[at] = 0;
    if (!r[0]) _exit(0); /* <> */

    if (chdir(auto_qmail) == -1) _exit(QLX_USAGE);

    nughde_get(r);

    x = nughde.s;
    xlen = nughde.len;

    args[0] = "bin/qmail-local";
    args[1] = "--";
    args[2] = x;
    n = byte_chr(x, xlen, 0);
    if (n++ == xlen) _exit(QLX_USAGE);
    x += n;
    xlen -= n;

    scan_ulong(x, &u);
    uid = u;
    n = byte_chr(x, xlen, 0);
    if (n++ == xlen) _exit(QLX_USAGE);
    x += n;
    xlen -= n;

    scan_ulong(x, &u);
    gid = u;
    n = byte_chr(x, xlen, 0);
    if (n++ == xlen) _exit(QLX_USAGE);
    x += n;
    xlen -= n;

    args[3] = x;
    n = byte_chr(x, xlen, 0);
    if (n++ == xlen) _exit(QLX_USAGE);
    x += n;
    xlen -= n;

    args[4] = r;
    args[5] = x;
    n = byte_chr(x, xlen, 0);
    if (n++ == xlen) _exit(QLX_USAGE);
    x += n;
    xlen -= n;

    args[6] = x;
    n = byte_chr(x, xlen, 0);
    if (n++ == xlen) _exit(QLX_USAGE);
    x += n;
    xlen -= n;

    args[7] = r + at + 1;
    args[8] = s;
    args[9] = aliasempty;
    args[10] = 0;

    if (fd_move(0, fdmess) == -1) _exit(QLX_SYS);
    if (fd_move(1, fdout) == -1) _exit(QLX_SYS);
    if (fd_copy(2, 1) == -1) _exit(QLX_SYS);
    if (prot_gid(gid) == -1) _exit(QLX_USAGE);
    if (prot_uid(uid) == -1) _exit(QLX_USAGE);
    if (!getuid()) _exit(QLX_ROOT);

    execv(*args, args);
    if (errno) _exit(QLX_EXECSOFT);
    _exit(QLX_EXECHARD);
  }
  return f;
}