0
|
1 |
/* |
|
2 |
* Copyright (c) 2014 Sunil Nimmagadda <sunil@nimmagadda.net> |
|
3 |
* |
|
4 |
* Permission to use, copy, modify, and distribute this software for any |
|
5 |
* purpose with or without fee is hereby granted, provided that the above |
|
6 |
* copyright notice and this permission notice appear in all copies. |
|
7 |
* |
|
8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
|
9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
|
11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
|
14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
15 |
*/ |
|
16 |
|
|
17 |
#include <sys/types.h> |
|
18 |
#include <sys/socket.h> |
|
19 |
|
|
20 |
#include <ctype.h> |
|
21 |
#include <err.h> |
|
22 |
#include <limits.h> |
|
23 |
#include <stdio.h> |
|
24 |
#include <stdlib.h> |
|
25 |
#include <syslog.h> |
|
26 |
#include <unistd.h> |
|
27 |
|
|
28 |
#include "imsgev.h" |
|
29 |
#include "pop3d.h" |
|
30 |
#include "ssl.h" |
|
31 |
|
|
32 |
#define MAXLINESIZE 2048 |
|
33 |
#define TIMEOUT 600000 |
|
34 |
|
|
35 |
enum pop_command { |
|
36 |
CMD_STLS = 0, |
|
37 |
CMD_CAPA, |
|
38 |
CMD_USER, |
|
39 |
CMD_PASS, |
|
40 |
CMD_QUIT, |
|
41 |
CMD_STAT, |
|
42 |
CMD_RETR, |
|
43 |
CMD_LIST, |
|
44 |
CMD_DELE, |
|
45 |
CMD_RSET, |
|
46 |
CMD_TOP, |
|
47 |
CMD_UIDL, |
|
48 |
CMD_NOOP |
|
49 |
}; |
|
50 |
|
|
51 |
enum arg_constraint { |
|
52 |
OPTIONAL = 1, |
|
53 |
PROHIBITED, |
|
54 |
REQUIRED |
|
55 |
}; |
|
56 |
|
|
57 |
static struct {int code; enum arg_constraint c; const char *cmd;} commands[] = { |
|
58 |
{CMD_STLS, PROHIBITED, "STLS"}, |
|
59 |
{CMD_CAPA, PROHIBITED, "CAPA"}, |
|
60 |
{CMD_USER, REQUIRED, "USER"}, |
|
61 |
{CMD_PASS, REQUIRED, "PASS"}, |
|
62 |
{CMD_QUIT, PROHIBITED, "QUIT"}, |
|
63 |
{CMD_STAT, PROHIBITED, "STAT"}, |
|
64 |
{CMD_RETR, REQUIRED, "RETR"}, |
|
65 |
{CMD_LIST, OPTIONAL, "LIST"}, |
|
66 |
{CMD_DELE, REQUIRED, "DELE"}, |
|
67 |
{CMD_RSET, PROHIBITED, "RSET"}, |
|
68 |
{CMD_TOP, REQUIRED, "TOP"}, |
|
69 |
{CMD_UIDL, OPTIONAL, "UIDL"}, |
|
70 |
{CMD_NOOP, PROHIBITED, "NOOP"}, |
|
71 |
{-1, OPTIONAL, NULL} |
|
72 |
}; |
|
73 |
|
|
74 |
static void auth_request(struct session *); |
|
75 |
static void capa(struct session *); |
|
76 |
static void command(struct session *, int, char *); |
|
77 |
static void session_io(struct io *, int); |
|
78 |
static void parse(struct session *, char *); |
|
79 |
static void auth_command(struct session *, int, char *); |
|
80 |
static void trans_command(struct session *, int, char *); |
|
81 |
static void get_list_all(struct session *, int); |
|
82 |
static void get_list(struct session *, unsigned int, int); |
|
83 |
static void maildrop_imsgev(struct imsgev *, int, struct imsg *); |
|
84 |
static void handle_init(struct session *, struct imsg *); |
|
85 |
static void handle_retr(struct session *, struct imsg *); |
|
86 |
static void handle_dele(struct session *, struct imsg *); |
|
87 |
static void handle_list(struct session *, struct imsg *); |
|
88 |
static void handle_list_all(struct session *, struct imsg *, int); |
|
89 |
static void handle_update(struct session *, struct imsg *); |
|
90 |
static void needfd(struct imsgev *); |
|
91 |
static void pop3_debug(char *, ...); |
|
92 |
static void session_write(struct session *, const char *, size_t); |
|
93 |
static const char *strstate(enum state); |
|
94 |
|
|
95 |
struct session_tree sessions; |
|
96 |
static int _pop3_debug = 1; |
|
97 |
|
|
98 |
void |
|
99 |
session_init(struct listener *l, int fd) |
|
100 |
{ |
|
101 |
struct session *s; |
|
102 |
void *ssl; |
|
103 |
extern void *ssl_ctx; |
|
104 |
|
|
105 |
s = xcalloc(1, sizeof(*s), "session_init"); |
|
106 |
s->l = l; |
|
107 |
if (iobuf_init(&s->iobuf, 0, 0) == -1) |
|
108 |
fatal("iobuf_init"); |
|
109 |
|
|
110 |
io_init(&s->io, fd, s, session_io, &s->iobuf); |
|
111 |
io_set_timeout(&s->io, TIMEOUT); |
|
112 |
s->id = arc4random(); |
|
113 |
s->sock = fd; |
|
114 |
s->state = AUTH; |
|
115 |
if (s->l->flags & POP3S) { |
|
116 |
s->flags |= POP3S; |
|
117 |
ssl = pop3s_init(ssl_ctx, s->sock); |
|
118 |
io_set_read(&s->io); |
|
119 |
io_start_tls(&s->io, ssl); |
|
120 |
return; |
|
121 |
} |
|
122 |
|
|
123 |
log_connect(s->id, &l->ss, l->ss.ss_len); |
|
124 |
SPLAY_INSERT(session_tree, &sessions, s); |
|
125 |
session_reply(s, "%s", "+OK pop3d ready"); |
|
126 |
io_set_write(&s->io); |
|
127 |
} |
|
128 |
|
|
129 |
void |
|
130 |
session_close(struct session *s, int flush) |
|
131 |
{ |
|
132 |
struct session *entry; |
|
133 |
|
|
134 |
entry = SPLAY_REMOVE(session_tree, &sessions, s); |
|
135 |
if (entry == NULL) { |
|
136 |
/* STARTTLS session was in progress and got interrupted */ |
|
137 |
logit(LOG_DEBUG, "%u: not in tree", s->id); |
|
138 |
entry = s; |
|
139 |
} |
|
140 |
|
|
141 |
if (flush) { |
|
142 |
if (entry->flags & POP3S) |
|
143 |
iobuf_flush_ssl(&entry->iobuf, entry->io.ssl); |
|
144 |
else |
|
145 |
iobuf_flush(&entry->iobuf, entry->io.sock); |
|
146 |
} |
|
147 |
|
|
148 |
iobuf_clear(&entry->iobuf); |
|
149 |
io_clear(&entry->io); |
|
150 |
imsgev_clear(&entry->iev_maildrop); |
|
151 |
imsgev_close(&entry->iev_maildrop); |
|
152 |
logit(LOG_INFO, "%u: session closed", entry->id); |
|
153 |
free(entry); |
|
154 |
} |
|
155 |
|
|
156 |
static void |
|
157 |
session_io(struct io *io, int evt) |
|
158 |
{ |
|
159 |
struct session *s = io->arg; |
|
160 |
char *line; |
|
161 |
size_t len; |
|
162 |
|
|
163 |
pop3_debug("%u: %s", s->id, io_strevent(evt)); |
|
164 |
switch (evt) { |
|
165 |
case IO_DATAIN: |
|
166 |
line = iobuf_getline(&s->iobuf, &len); |
|
167 |
if (line == NULL) { |
|
168 |
iobuf_normalize(&s->iobuf); |
|
169 |
break; |
|
170 |
} |
|
171 |
if (strncasecmp(line, "PASS", 4) == 0) |
|
172 |
pop3_debug(">>> PASS"); |
|
173 |
else |
|
174 |
pop3_debug(">>> %s", line); |
|
175 |
parse(s, line); |
|
176 |
break; |
|
177 |
case IO_LOWAT: |
|
178 |
if (iobuf_queued(&s->iobuf) == 0) |
|
179 |
io_set_read(io); |
|
180 |
break; |
|
181 |
case IO_TLSREADY: |
|
182 |
/* greet only for pop3s, STLS already greeted */ |
|
183 |
if (s->flags & POP3S) { |
|
184 |
log_connect(s->id, &s->l->ss, s->l->ss.ss_len); |
|
185 |
session_reply(s, "%s", "+OK pop3 ready"); |
|
186 |
io_set_write(&s->io); |
|
187 |
} |
|
188 |
SPLAY_INSERT(session_tree, &sessions, s); |
|
189 |
/* mark STLS session as secure */ |
|
190 |
s->flags |= POP3S; |
|
191 |
logit(LOG_INFO, "%u: TLS ready", s->id); |
|
192 |
break; |
|
193 |
case IO_DISCONNECTED: |
|
194 |
case IO_TIMEOUT: |
|
195 |
case IO_ERROR: |
|
196 |
session_close(s, 0); |
|
197 |
break; |
|
198 |
default: |
|
199 |
logit(LOG_DEBUG, "unknown event %s", io_strevent(evt)); |
|
200 |
break; |
|
201 |
} |
|
202 |
} |
|
203 |
|
|
204 |
static void |
|
205 |
parse(struct session *s, char *line) |
|
206 |
{ |
|
207 |
enum arg_constraint c = OPTIONAL; |
|
208 |
int i, cmd = -1; |
|
209 |
char *args; |
|
210 |
|
|
211 |
/* trim newline */ |
|
212 |
line[strcspn(line, "\n")] = '\0'; |
|
213 |
|
|
214 |
args = strchr(line, ' '); |
|
215 |
if (args) { |
|
216 |
*args++ = '\0'; |
|
217 |
while (isspace((unsigned char)*args)) |
|
218 |
args++; |
|
219 |
} |
|
220 |
|
|
221 |
for (i = 0; commands[i].code != -1; i++) { |
|
222 |
if (strcasecmp(line, commands[i].cmd) == 0) { |
|
223 |
cmd = commands[i].code; |
|
224 |
c = commands[i].c; |
|
225 |
break; |
|
226 |
} |
|
227 |
} |
|
228 |
|
|
229 |
if (cmd == -1) { |
|
230 |
logit(LOG_INFO, "%u: invalid command %s", s->id, line); |
|
231 |
session_reply(s, "%s", "-ERR invalid command"); |
|
232 |
io_set_write(&s->io); |
|
233 |
return; |
|
234 |
} |
|
235 |
|
|
236 |
if (c == PROHIBITED && args) { |
|
237 |
session_reply(s, "%s", "-ERR no arguments allowed"); |
|
238 |
io_set_write(&s->io); |
|
239 |
return; |
|
240 |
} else if ((c == REQUIRED) && |
|
241 |
(args == NULL || strlen(args) >= ARGLEN)) { |
|
242 |
session_reply(s, "%s", "-ERR args required or too long"); |
|
243 |
io_set_write(&s->io); |
|
244 |
return; |
|
245 |
} |
|
246 |
|
|
247 |
command(s, cmd, args); |
|
248 |
} |
|
249 |
|
|
250 |
static void |
|
251 |
command(struct session *s, int cmd, char *args) |
|
252 |
{ |
|
253 |
switch (s->state) { |
|
254 |
case AUTH: |
|
255 |
auth_command(s, cmd, args); |
|
256 |
break; |
|
257 |
case TRANSACTION: |
|
258 |
trans_command(s, cmd, args); |
|
259 |
break; |
|
260 |
case UPDATE: |
|
261 |
session_reply(s, "%s", "-ERR commands not allowed"); |
|
262 |
io_set_write(&s->io); |
|
263 |
break; |
|
264 |
default: |
|
265 |
fatalx("Invalid state"); |
|
266 |
} |
|
267 |
} |
|
268 |
|
|
269 |
static void |
|
270 |
auth_command(struct session *s, int cmd, char *args) |
|
271 |
{ |
|
272 |
extern void *ssl_ctx; |
|
273 |
void *ssl; |
|
274 |
|
|
275 |
switch (cmd) { |
|
276 |
case CMD_STLS: |
|
277 |
if (s->flags & POP3S) { |
|
278 |
session_reply(s, "%s", "-ERR already secured"); |
|
279 |
break; |
|
280 |
} |
|
281 |
session_reply(s, "%s", "+OK"); |
|
282 |
io_set_write(&s->io); |
|
283 |
iobuf_flush(&s->iobuf, s->io.sock); |
|
284 |
/* add back when IO_TLSREADY. */ |
|
285 |
SPLAY_REMOVE(session_tree, &sessions, s); |
|
286 |
ssl = pop3s_init(ssl_ctx, s->sock); |
|
287 |
io_set_read(&s->io); |
|
288 |
io_start_tls(&s->io, ssl); |
|
289 |
return; |
|
290 |
case CMD_CAPA: |
|
291 |
capa(s); |
|
292 |
break; |
|
293 |
case CMD_USER: |
|
294 |
strlcpy(s->user, args, sizeof(s->user)); |
|
295 |
session_reply(s, "%s", "+OK"); |
|
296 |
break; |
|
297 |
case CMD_PASS: |
|
298 |
if (s->user[0] == '\0') { |
|
299 |
session_reply(s, "%s", "-ERR no USER specified"); |
|
300 |
break; |
|
301 |
} |
|
302 |
strlcpy(s->pass, args, sizeof(s->pass)); |
|
303 |
auth_request(s); |
|
304 |
return; |
|
305 |
case CMD_QUIT: |
|
306 |
session_reply(s, "%s", "+OK"); |
|
307 |
io_set_write(&s->io); |
|
308 |
session_close(s, 1); |
|
309 |
return; |
|
310 |
default: |
|
311 |
session_reply(s, "%s", "-ERR invalid command"); |
|
312 |
break; |
|
313 |
} |
|
314 |
|
|
315 |
io_set_write(&s->io); |
|
316 |
} |
|
317 |
|
|
318 |
static void |
|
319 |
auth_request(struct session *s) |
|
320 |
{ |
|
321 |
extern struct imsgev iev_pop3d; |
|
322 |
struct auth_req req; |
|
323 |
|
|
324 |
memset(&req, 0, sizeof(req)); |
|
325 |
strlcpy(req.user, s->user, sizeof(req.user)); |
|
326 |
strlcpy(req.pass, s->pass, sizeof(req.pass)); |
|
327 |
imsgev_xcompose(&iev_pop3d, IMSG_AUTH, s->id, 0, -1, |
|
328 |
&req, sizeof(req), "auth_request"); |
|
329 |
} |
|
330 |
|
|
331 |
static void |
|
332 |
capa(struct session *s) |
|
333 |
{ |
|
334 |
session_reply(s, "%s", "+OK"); |
|
335 |
session_reply(s, "%s", "STLS"); |
|
336 |
session_reply(s, "%s", "USER"); |
|
337 |
session_reply(s, "%s", "TOP"); |
|
338 |
session_reply(s, "%s", "UIDL"); |
|
339 |
session_reply(s, "%s", "IMPLEMENTATION pop3d"); |
|
340 |
session_reply(s, "%s", "."); |
|
341 |
} |
|
342 |
|
|
343 |
static void |
|
344 |
trans_command(struct session *s, int cmd, char *args) |
|
345 |
{ |
|
346 |
struct retr_req retr_req; |
|
347 |
unsigned int idx, n; |
|
348 |
char *c; |
|
349 |
const char *errstr; |
|
350 |
int uidl = 0; |
|
351 |
|
|
352 |
memset(&retr_req, 0, sizeof(retr_req)); |
|
353 |
switch (cmd) { |
|
354 |
case CMD_CAPA: |
|
355 |
capa(s); |
|
356 |
break; |
|
357 |
case CMD_STAT: |
|
358 |
session_reply(s, "%s %zu %zu", "+OK", s->nmsgs, s->m_sz); |
|
359 |
break; |
|
360 |
case CMD_TOP: |
|
361 |
if ((c = strchr(args, ' ')) == NULL) { |
|
362 |
session_reply(s, "%s", "-ERR invalid arguments"); |
|
363 |
break; |
|
364 |
} |
|
365 |
*c++ = '\0'; |
|
366 |
n = strtonum(c, 0, UINT_MAX, &errstr); |
|
367 |
if (errstr) { |
|
368 |
session_reply(s, "%s", "-ERR invalid n"); |
|
369 |
break; |
|
370 |
} |
|
371 |
retr_req.top = 1; |
|
372 |
retr_req.ntop = n; |
|
373 |
/* FALLTRHROUGH */ |
|
374 |
case CMD_RETR: |
|
375 |
if (!get_index(s, args, &retr_req.idx)) |
|
376 |
break; |
|
377 |
imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_RETR, |
|
378 |
s->id, 0, -1, &retr_req, sizeof(retr_req), "trans_command"); |
|
379 |
return; |
|
380 |
case CMD_NOOP: |
|
381 |
session_reply(s, "%s", "+OK"); |
|
382 |
break; |
|
383 |
case CMD_DELE: |
|
384 |
if (!get_index(s, args, &idx)) |
|
385 |
break; |
|
386 |
imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_DELE, |
|
387 |
s->id, 0, -1, &idx, sizeof(idx), "trans_command"); |
|
388 |
return; |
|
389 |
case CMD_RSET: |
|
390 |
imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_RSET, |
|
391 |
s->id, 0, -1, NULL, 0, "trans_command"); |
|
392 |
return; |
|
393 |
case CMD_UIDL: |
|
394 |
uidl = 1; |
|
395 |
/* FALLTHROUGH */ |
|
396 |
case CMD_LIST: |
|
397 |
if (args) { |
|
398 |
if (!get_index(s, args, &idx)) |
|
399 |
break; |
|
400 |
get_list(s, idx, uidl); |
|
401 |
} else |
|
402 |
get_list_all(s, uidl); |
|
403 |
return; |
|
404 |
case CMD_QUIT: |
|
405 |
imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_UPDATE, |
|
406 |
s->id, 0, -1, NULL, 0, "trans_command"); |
|
407 |
session_set_state(s, UPDATE); |
|
408 |
return; |
|
409 |
default: |
|
410 |
session_reply(s, "%s", "-ERR invalid command"); |
|
411 |
break; |
|
412 |
} |
|
413 |
|
|
414 |
io_set_write(&s->io); |
|
415 |
} |
|
416 |
|
|
417 |
static void |
|
418 |
get_list_all(struct session *s, int uidl) |
|
419 |
{ |
|
420 |
imsgev_xcompose(&s->iev_maildrop, |
|
421 |
(uidl) ? IMSG_MAILDROP_UIDLALL : IMSG_MAILDROP_LISTALL, |
|
422 |
s->id, 0, -1, NULL, 0, "list_all"); |
|
423 |
} |
|
424 |
|
|
425 |
static void |
|
426 |
get_list(struct session *s, unsigned int i, int uidl) |
|
427 |
{ |
|
428 |
struct list_req req; |
|
429 |
|
|
430 |
req.idx = i; |
|
431 |
req.uidl = uidl; |
|
432 |
imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_LIST, |
|
433 |
s->id, 0, -1, &req, sizeof(req), "list"); |
|
434 |
} |
|
435 |
|
|
436 |
void |
|
437 |
session_imsgev_init(struct session *s, int fd) |
|
438 |
{ |
|
439 |
imsgev_init(&s->iev_maildrop, fd, s, maildrop_imsgev, needfd); |
|
440 |
} |
|
441 |
|
|
442 |
static void |
|
443 |
maildrop_imsgev(struct imsgev *iev, int code, struct imsg *imsg) |
|
444 |
{ |
|
445 |
struct session key, *r; |
|
446 |
int uidl = 0; |
|
447 |
|
|
448 |
switch (code) { |
|
449 |
case IMSGEV_IMSG: |
|
450 |
key.id = imsg->hdr.peerid; |
|
451 |
r = SPLAY_FIND(session_tree, &sessions, &key); |
|
452 |
if (r == NULL) { |
|
453 |
logit(LOG_INFO, "%u: session not found", key.id); |
|
454 |
fatalx("session: session lost"); |
|
455 |
} |
|
456 |
switch (imsg->hdr.type) { |
|
457 |
case IMSG_MAILDROP_INIT: |
|
458 |
handle_init(r, imsg); |
|
459 |
break; |
|
460 |
case IMSG_MAILDROP_RETR: |
|
461 |
handle_retr(r, imsg); |
|
462 |
break; |
|
463 |
case IMSG_MAILDROP_DELE: |
|
464 |
handle_dele(r, imsg); |
|
465 |
break; |
|
466 |
case IMSG_MAILDROP_RSET: |
|
467 |
session_reply(r, "%s", "+OK reset"); |
|
468 |
io_set_write(&r->io); |
|
469 |
break; |
|
470 |
case IMSG_MAILDROP_LIST: |
|
471 |
handle_list(r, imsg); |
|
472 |
break; |
|
473 |
case IMSG_MAILDROP_UIDLALL: |
|
474 |
uidl = 1; |
|
475 |
/* FALLTHROUGH */ |
|
476 |
case IMSG_MAILDROP_LISTALL: |
|
477 |
handle_list_all(r, imsg, uidl); |
|
478 |
break; |
|
479 |
case IMSG_MAILDROP_UPDATE: |
|
480 |
handle_update(r, imsg); |
|
481 |
break; |
|
482 |
default: |
|
483 |
logit(LOG_DEBUG, "%s: unexpected imsg %u", |
|
484 |
__func__, imsg->hdr.type); |
|
485 |
break; |
|
486 |
} |
|
487 |
break; |
|
488 |
case IMSGEV_EREAD: |
|
489 |
case IMSGEV_EWRITE: |
|
490 |
case IMSGEV_EIMSG: |
|
491 |
fatal("session: imsgev read/write error"); |
|
492 |
break; |
|
493 |
} |
|
494 |
} |
|
495 |
|
|
496 |
static void |
|
497 |
handle_init(struct session *s, struct imsg *imsg) |
|
498 |
{ |
|
499 |
size_t datalen; |
|
500 |
struct stats *stats; |
|
501 |
|
|
502 |
datalen = imsg->hdr.len - sizeof(imsg->hdr); |
|
503 |
if (datalen) { |
|
504 |
stats = imsg->data; |
|
505 |
s->m_sz = stats->sz; |
|
506 |
s->nmsgs = stats->nmsgs; |
|
507 |
session_reply(s, "%s", "+OK maildrop ready"); |
|
508 |
io_set_write(&s->io); |
|
509 |
session_set_state(s, TRANSACTION); |
|
510 |
} else { |
|
511 |
session_reply(s, "%s", "-ERR maildrop init failed"); |
|
512 |
io_set_write(&s->io); |
|
513 |
session_close(s, 1); |
|
514 |
} |
|
515 |
} |
|
516 |
|
|
517 |
static void |
|
518 |
handle_retr(struct session *s, struct imsg *imsg) |
|
519 |
{ |
|
520 |
struct retr_res *r = imsg->data; |
|
521 |
FILE *fp; |
|
522 |
char *line; |
|
523 |
size_t len; |
|
524 |
|
|
525 |
if (imsg->fd == -1) { |
|
526 |
session_reply(s, "%s", "-ERR marked for delete"); |
|
527 |
io_set_write(&s->io); |
|
528 |
return; |
|
529 |
} |
|
530 |
|
|
531 |
if ((fp = fdopen(imsg->fd, "r")) == NULL) { |
|
532 |
logit(LOG_INFO, "%zu: retr failed", s->id); |
|
533 |
session_reply(s, "%s", "-ERR RETR failed"); |
|
534 |
io_set_write(&s->io); |
|
535 |
session_close(s, 1); |
|
536 |
return; |
|
537 |
} |
|
538 |
|
|
539 |
if (fseek(fp, r->offset, SEEK_SET) == -1) |
|
540 |
fatal("fseek"); |
|
541 |
|
|
542 |
session_reply(s, "%s", "+OK"); |
|
543 |
/* Ignore "From " line when type is mbox; maildir doesn't have it */ |
|
544 |
if ((line = fgetln(fp, &len)) && strncmp(line, "From ", 5)) |
|
545 |
session_write(s, line, len); |
|
546 |
|
|
547 |
if (r->top) { |
|
548 |
/* print headers regardless of ntop */ |
|
549 |
while ((line = fgetln(fp, &len))) { |
|
550 |
session_write(s, line, len); |
|
551 |
r->nlines -= 1; |
|
552 |
if (strncmp(line , "\n", 1) == 0) |
|
553 |
break; |
|
554 |
} |
|
555 |
|
|
556 |
/* print ntop lines of body */ |
|
557 |
while ((r->ntop-- > 0) && r->nlines-- && |
|
558 |
(line = fgetln(fp, &len))) |
|
559 |
session_write(s, line, len); |
|
560 |
} else |
|
561 |
while (r->nlines-- && (line = fgetln(fp, &len))) |
|
562 |
session_write(s, line, len); |
|
563 |
|
|
564 |
session_reply(s, "%s", "."); |
|
565 |
io_set_write(&s->io); |
|
566 |
fclose(fp); |
|
567 |
close(imsg->fd); |
|
568 |
} |
|
569 |
|
|
570 |
static void |
|
571 |
handle_dele(struct session *s, struct imsg *imsg) |
|
572 |
{ |
|
573 |
int *res = imsg->data; |
|
574 |
|
|
575 |
if (*res == 0) |
|
576 |
session_reply(s, "%s", "+OK marked for delete"); |
|
577 |
else |
|
578 |
session_reply(s, "%s", "+ERR msg already marked delete"); |
|
579 |
|
|
580 |
io_set_write(&s->io); |
|
581 |
} |
|
582 |
|
|
583 |
/* DELEted msg's hash and sz will be zero, ignore them */ |
|
584 |
static void |
|
585 |
handle_list(struct session *s, struct imsg *imsg) |
|
586 |
{ |
|
587 |
struct list_res *res = imsg->data; |
|
588 |
|
|
589 |
res->idx += 1; /* POP3 index is 1 based */ |
|
590 |
if (res->uidl) { |
|
591 |
if (strlen(res->u.hash)) |
|
592 |
session_reply(s, "+OK %zu %s", res->idx, res->u.hash); |
|
593 |
else |
|
594 |
session_reply(s, "-ERR marked for delete"); |
|
595 |
} else { |
|
596 |
if (res->u.sz) |
|
597 |
session_reply(s, "+OK %zu %zu", res->idx, res->u.sz); |
|
598 |
else |
|
599 |
session_reply(s, "-ERR marked for delete"); |
|
600 |
} |
|
601 |
|
|
602 |
io_set_write(&s->io); |
|
603 |
} |
|
604 |
|
|
605 |
/* DELEted msg's hash and sz will be zero, ignore them */ |
|
606 |
static void |
|
607 |
handle_list_all(struct session *s, struct imsg *imsg, int uidl) |
|
608 |
{ |
|
609 |
char *nhash = NULL; |
|
610 |
size_t datalen, i, item_sz, j, nitems, *nsz = NULL; |
|
611 |
|
|
612 |
datalen = imsg->hdr.len - sizeof(imsg->hdr); |
|
613 |
item_sz = (uidl) ? SHA1_DIGEST_STRING_LENGTH : sizeof(size_t); |
|
614 |
nitems = datalen / item_sz; |
|
615 |
if (uidl) |
|
616 |
nhash = imsg->data; |
|
617 |
else |
|
618 |
nsz = imsg->data; |
|
619 |
|
|
620 |
session_reply(s, "+OK"); |
|
621 |
for (i = 0; i < nitems; i++) { |
|
622 |
if (uidl) { |
|
623 |
j = i * SHA1_DIGEST_STRING_LENGTH; |
|
624 |
if (nhash[j]) |
|
625 |
session_reply(s, "%zu %s", i + 1, nhash + j); |
|
626 |
} else { |
|
627 |
if (nsz[i]) |
|
628 |
session_reply(s, "%zu %zu", i + 1, nsz[i]); |
|
629 |
} |
|
630 |
} |
|
631 |
|
|
632 |
session_reply(s, "."); |
|
633 |
io_set_write(&s->io); |
|
634 |
|
|
635 |
} |
|
636 |
|
|
637 |
static void |
|
638 |
handle_update(struct session *s, struct imsg *imsg) |
|
639 |
{ |
|
640 |
int *res = imsg->data; |
|
641 |
|
|
642 |
if (*res == 0) |
|
643 |
session_reply(s, "%s", "+OK maildrop updated"); |
|
644 |
else |
|
645 |
session_reply(s, "%s", "-ERR maildrop update failed"); |
|
646 |
|
|
647 |
io_set_write(&s->io); |
|
648 |
session_close(s, 1); |
|
649 |
} |
|
650 |
|
|
651 |
static void |
|
652 |
needfd(struct imsgev *iev) |
|
653 |
{ |
|
654 |
/* XXX */ |
|
655 |
fatalx("session needs an fd"); |
|
656 |
} |
|
657 |
|
|
658 |
int |
|
659 |
session_cmp(struct session *a, struct session *b) |
|
660 |
{ |
|
661 |
if (a->id < b->id) |
|
662 |
return (-1); |
|
663 |
|
|
664 |
if (a->id > b->id) |
|
665 |
return (1); |
|
666 |
|
|
667 |
return (0); |
|
668 |
} |
|
669 |
|
|
670 |
void |
|
671 |
session_set_state(struct session *s, enum state newstate) |
|
672 |
{ |
|
673 |
pop3_debug("%u: %s -> %s", s->id, strstate(s->state), |
|
674 |
strstate(newstate)); |
|
675 |
s->state = newstate; |
|
676 |
} |
|
677 |
|
|
678 |
#define CASE(x) case x : return #x |
|
679 |
static const char * |
|
680 |
strstate(enum state state) |
|
681 |
{ |
|
682 |
static char buf[32]; |
|
683 |
|
|
684 |
switch (state) { |
|
685 |
CASE(AUTH); |
|
686 |
CASE(TRANSACTION); |
|
687 |
CASE(UPDATE); |
|
688 |
default: |
|
689 |
snprintf(buf, sizeof(buf), "%d ???", state); |
|
690 |
return (buf); |
|
691 |
} |
|
692 |
} |
|
693 |
|
|
694 |
void |
|
695 |
session_reply(struct session *s, char *fmt, ...) |
|
696 |
{ |
|
697 |
va_list ap; |
|
698 |
int n; |
|
699 |
char buf[MAXLINESIZE]; |
|
700 |
|
|
701 |
va_start(ap, fmt); |
|
702 |
n = vsnprintf(buf, sizeof(buf), fmt, ap); |
|
703 |
va_end(ap); |
|
704 |
if (n == -1 || n > MAXLINESIZE) |
|
705 |
fatalx("session_reply: response too long"); |
|
706 |
|
|
707 |
if (buf[0] == '+') |
|
708 |
pop3_debug("<<< +OK"); |
|
709 |
else if (buf[0] == '-') |
|
710 |
pop3_debug("<<< -ERR"); |
|
711 |
|
|
712 |
iobuf_xfqueue(&s->iobuf, "session_reply", "%s\r\n", buf); |
|
713 |
} |
|
714 |
|
|
715 |
static void |
|
716 |
session_write(struct session *s, const char *data, size_t len) |
|
717 |
{ |
|
718 |
/* remove terminating \n or \r\n if any */ |
|
719 |
if (data[len - 1] == '\n') |
|
720 |
len -= 1; |
|
721 |
|
|
722 |
if (data[len - 1] == '\r') |
|
723 |
len -= 1; |
|
724 |
|
|
725 |
/* byte stuff "." if at beginning of line */ |
|
726 |
if (data[0] == '.') |
|
727 |
iobuf_xfqueue(&s->iobuf, "session_write", "."); |
|
728 |
|
|
729 |
iobuf_xqueue(&s->iobuf, "session_write", data, len); |
|
730 |
/* explicitly terminate with CRLF */ |
|
731 |
iobuf_xfqueue(&s->iobuf, "session_write", "\r\n"); |
|
732 |
} |
|
733 |
|
|
734 |
static void |
|
735 |
pop3_debug(char *fmt, ...) |
|
736 |
{ |
|
737 |
va_list ap; |
|
738 |
char buf[MAXLINESIZE]; |
|
739 |
int n; |
|
740 |
|
|
741 |
if (!_pop3_debug) |
|
742 |
return; |
|
743 |
|
|
744 |
va_start(ap, fmt); |
|
745 |
n = vsnprintf(buf, sizeof(buf), fmt, ap); |
|
746 |
va_end(ap); |
|
747 |
if (n == -1 || n > MAXLINESIZE) |
|
748 |
fatalx("pop3_debug: response too long"); |
|
749 |
|
|
750 |
logit(LOG_DEBUG, "%s", buf); |
|
751 |
} |
|
752 |
|
|
753 |
SPLAY_GENERATE(session_tree, session, entry, session_cmp); |
|
754 |
|