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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
#include <unistd.h>
#include "alloc.h"
#include "buffer.h"
#include "byte.h"
#include "case.h"
#include "env.h"
#include "exit.h"
#include "fd.h"
#include "fmt.h"
#include "ip.h"
#include "sig.h"
#include "str.h"
#include "stralloc.h"
#include "timeout.h"
#include "wait.h"
#include "commands.h"
#include "now.h"
#include "tls_start.h"
#define PORT_POP3S "995"
#define FDIN 0
#define FDOUT 1
#define FDAUTH 3
#define FDLOG 5
void die()
{
_exit(1);
}
ssize_t saferead(int fd, char *buf, int len)
{
int r;
r = timeoutread(1200, fd, buf, len);
if (r <= 0) die();
return r;
}
ssize_t safewrite(int fd, char *buf, int len)
{
int r;
r = timeoutwrite(1200, fd, buf, len);
if (r <= 0) die();
return r;
}
char outbuf[128];
buffer bo = BUFFER_INIT(safewrite, FDOUT, outbuf, sizeof(outbuf));
char inbuf[128];
buffer bi = BUFFER_INIT(saferead, FDIN, inbuf, sizeof(inbuf));
void outs(char *s)
{
buffer_puts(&bo, s);
}
void flush()
{
buffer_flush(&bo);
}
void err(char *s)
{
outs("-ERR ");
outs(s);
outs("\r\n");
flush();
}
/* Logging */
stralloc protocol = {0};
stralloc auth = {0};
char *localport;
char *remoteip;
char *remotehost;
char strnum[FMT_ULONG];
char logbuf[512];
buffer bl = BUFFER_INIT(safewrite, FDLOG, logbuf, sizeof(logbuf));
void logs(char *s)
{
if (buffer_puts(&bl, s) == -1) _exit(1);
}
void logp(char *s)
{
logs(" P:");
logs(s);
}
void logh(char *s1, char *s2)
{
logs(" S:");
logs(s1);
logs(":");
logs(s2);
}
void logu(char *s)
{
logs(" ?~ '");
logs(s);
logs("'");
}
void logn(char *s)
{
if (buffer_puts(&bl, s) == -1) _exit(1);
if (buffer_flush(&bl) == -1) _exit(1);
}
void logpid()
{
strnum[fmt_ulong(strnum, getpid())] = 0;
logs("qmail-popup: pid ");
logs(strnum);
logs(" ");
}
void log_pop(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6)
{
logpid();
logs(s1);
logs(s2);
logp(s3);
logh(s4, s5), logu(s6), logn("\n");
}
void die_usage()
{
err("usage: popup hostname subprogram");
die();
}
void die_nomem()
{
err("out of memory");
die();
}
void die_pipe()
{
err("unable to open pipe");
die();
}
void die_write()
{
err("unable to write pipe");
die();
}
void die_fork()
{
err("unable to fork");
die();
}
void die_childcrashed()
{
err("aack, child crashed");
}
void die_badauth()
{
err("authorization failed");
}
void die_tls()
{
err("TLS startup failed");
die();
}
void die_notls()
{
err("TLS required but not negotiated");
log_pop("Reject::STLS::", "Any", "POP3", remoteip, remotehost, "unknown");
die();
}
void err_syntax()
{
err("syntax error");
}
void err_wantuser()
{
err("USER first");
}
void err_authoriz()
{
err("authorization first");
}
void okay()
{
outs("+OK \r\n");
flush();
}
void pop3_quit()
{
okay();
die();
}
void poplog_init()
{
if (!stralloc_copys(&protocol, "POP3")) die_nomem();
localport = env_get("TCP6LOCALPORT");
if (!localport) localport = env_get("TCPLOCALPORT");
if (!localport) localport = "unknown";
if (!case_diffs(localport, PORT_POP3S))
if (!stralloc_cats(&protocol, "S")) die_nomem();
remoteip = env_get("TCP6REMOTEIP");
if (remoteip && byte_equal(remoteip, 7, V4MAPPREFIX)) remoteip = remoteip + 7;
if (!remoteip) remoteip = env_get("TCPREMOTEIP");
if (!remoteip) remoteip = "unknown";
remotehost = env_get("TCP6REMOTEHOST");
if (!remotehost) remotehost = env_get("TCPREMOTEHOST");
if (!remotehost) remotehost = "unknown";
}
char unique[FMT_ULONG + FMT_ULONG + 3];
char *hostname;
stralloc username = {0};
int seenuser = 0;
char **childargs;
buffer ba;
char authbuf[128];
int stls = 0;
int seenstls = 0;
int apop = 0;
void doanddie(char *user, unsigned int userlen, char *pass) /* userlen: including 0 byte */
{
int child;
int wstat;
int pi[2];
if (fd_copy(2, 1) == -1) die_pipe();
close(FDAUTH);
if (pipe(pi) == -1) die_pipe();
if (pi[0] != FDAUTH) die_pipe();
switch (child = fork()) {
case -1: die_fork();
case 0:
close(pi[1]);
sig_pipedefault();
execvp(*childargs, childargs);
_exit(1);
}
close(pi[0]);
buffer_init(&ba, write, pi[1], authbuf, sizeof(authbuf));
if (buffer_put(&ba, user, userlen) == -1) die_write();
if (buffer_put(&ba, pass, str_len(pass) + 1) == -1) die_write();
if (buffer_puts(&ba, "<") == -1) die_write();
if (buffer_puts(&ba, unique) == -1) die_write();
if (buffer_puts(&ba, hostname) == -1) die_write();
if (buffer_put(&ba, ">", 2) == -1) die_write();
if (buffer_flush(&ba) == -1) die_write();
close(pi[1]);
byte_zero(pass, str_len(pass));
byte_zero(authbuf, sizeof(authbuf));
if (wait_pid(&wstat, child) == -1) die();
if (wait_crashed(wstat)) die_childcrashed();
if (!stralloc_0(&auth)) die_nomem();
if (!stralloc_0(&protocol)) die_nomem();
if (wait_exitcode(wstat)) {
die_badauth();
log_pop("Reject::AUTH::", auth.s, protocol.s, remoteip, remotehost, user);
} else
log_pop("Accept::AUTH::", auth.s, protocol.s, remoteip, remotehost, user);
die();
}
void pop3_greet()
{
char *s;
s = unique;
s += fmt_uint(s, getpid());
*s++ = '.';
s += fmt_ulong(s, (unsigned long)now());
*s++ = '@';
*s++ = 0;
if (!apop)
outs("+OK\r\n");
else {
outs("+OK <");
outs(unique);
outs(hostname);
outs(">\r\n");
}
flush();
}
void pop3_user(char *arg)
{
if (stls == 2 && !seenstls) die_notls();
if (!*arg) {
err_syntax();
return;
}
okay();
seenuser = 1;
if (!stralloc_copys(&username, arg)) die_nomem();
if (!stralloc_0(&username)) die_nomem();
}
void pop3_pass(char *arg)
{
if (!seenuser) {
err_wantuser();
return;
}
if (!*arg) {
err_syntax();
return;
}
if (!stralloc_copys(&auth, "User")) die_nomem();
doanddie(username.s, username.len, arg);
}
void pop3_apop(char *arg)
{
char *space;
if (stls == 2 && !seenstls) die_notls();
space = arg + str_chr(arg, ' ');
if (!*space) {
err_syntax();
return;
}
*space++ = 0;
if (!stralloc_copys(&auth, "Apop")) die_nomem();
doanddie(arg, space - arg, space);
}
void pop3_capa(char *arg)
{
outs("+OK capability list follows\r\n");
outs("TOP\r\n");
outs("USER\r\n");
outs("UIDL\r\n");
if (apop) outs("APOP\r\n");
if (stls > 0) outs("STLS\r\n");
outs(".\r\n");
flush();
}
void pop3_stls(char *arg)
{
if (stls == 0 || seenstls == 1) return err("STLS not available");
outs("+OK starting TLS negotiation\r\n");
flush();
if (!starttls_init()) die_tls();
buffer_init(&bi, saferead, FDIN, inbuf, sizeof(inbuf));
seenstls = 1;
/* reset state */
seenuser = 0;
if (!stralloc_cats(&protocol, "S")) die_nomem();
}
struct commands pop3commands[] = {
{"user", pop3_user, 0},
{"pass", pop3_pass, 0},
{"apop", pop3_apop, 0},
{"quit", pop3_quit, 0},
{"capa", pop3_capa, 0},
{"stls", pop3_stls, 0},
{"noop", okay, 0},
{ 0, err_authoriz, 0}
};
int main(int argc, char **argv)
{
char *pop3auth;
char *ucspitls;
sig_alarmcatch(die);
sig_pipeignore();
hostname = argv[1];
if (!hostname) die_usage();
childargs = argv + 2;
if (!*childargs) die_usage();
ucspitls = env_get("UCSPITLS");
if (ucspitls) {
stls = 1;
if (!case_diffs(ucspitls, "-")) stls = 0;
if (!case_diffs(ucspitls, "!")) stls = 2;
}
pop3auth = env_get("POP3AUTH");
if (pop3auth) {
if (case_starts(pop3auth, "apop")) apop = 2;
if (case_starts(pop3auth, "+apop")) apop = 1;
}
poplog_init();
pop3_greet();
commands(&bi, pop3commands);
die();
}
|