author | Sunil Nimmagadda <sunil@nimmagadda.net> |
Wed, 20 Aug 2014 21:46:02 +0500 | |
changeset 27 | 6a8a845df07c |
parent 26 | b20d78c55e91 |
child 29 | 9ced98f59ea6 |
permissions | -rw-r--r-- |
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
ce31cbe9193a
Fix a crash during concurrent sessions TIMEOUT reported by James
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
23
diff
changeset
|
25 |
#include <string.h> |
0 | 26 |
#include <syslog.h> |
27 |
#include <unistd.h> |
|
28 |
||
29 |
#include "imsgev.h" |
|
30 |
#include "pop3d.h" |
|
31 |
#include "ssl.h" |
|
32 |
||
33 |
#define MAXLINESIZE 2048 |
|
34 |
#define TIMEOUT 600000 |
|
35 |
||
36 |
enum pop_command { |
|
37 |
CMD_STLS = 0, |
|
38 |
CMD_CAPA, |
|
39 |
CMD_USER, |
|
40 |
CMD_PASS, |
|
41 |
CMD_QUIT, |
|
42 |
CMD_STAT, |
|
43 |
CMD_RETR, |
|
44 |
CMD_LIST, |
|
45 |
CMD_DELE, |
|
46 |
CMD_RSET, |
|
47 |
CMD_TOP, |
|
48 |
CMD_UIDL, |
|
49 |
CMD_NOOP |
|
50 |
}; |
|
51 |
||
52 |
enum arg_constraint { |
|
53 |
OPTIONAL = 1, |
|
54 |
PROHIBITED, |
|
55 |
REQUIRED |
|
56 |
}; |
|
57 |
||
58 |
static struct {int code; enum arg_constraint c; const char *cmd;} commands[] = { |
|
59 |
{CMD_STLS, PROHIBITED, "STLS"}, |
|
60 |
{CMD_CAPA, PROHIBITED, "CAPA"}, |
|
61 |
{CMD_USER, REQUIRED, "USER"}, |
|
62 |
{CMD_PASS, REQUIRED, "PASS"}, |
|
63 |
{CMD_QUIT, PROHIBITED, "QUIT"}, |
|
64 |
{CMD_STAT, PROHIBITED, "STAT"}, |
|
65 |
{CMD_RETR, REQUIRED, "RETR"}, |
|
66 |
{CMD_LIST, OPTIONAL, "LIST"}, |
|
67 |
{CMD_DELE, REQUIRED, "DELE"}, |
|
68 |
{CMD_RSET, PROHIBITED, "RSET"}, |
|
69 |
{CMD_TOP, REQUIRED, "TOP"}, |
|
70 |
{CMD_UIDL, OPTIONAL, "UIDL"}, |
|
71 |
{CMD_NOOP, PROHIBITED, "NOOP"}, |
|
72 |
{-1, OPTIONAL, NULL} |
|
73 |
}; |
|
74 |
||
75 |
static void auth_request(struct session *); |
|
76 |
static void capa(struct session *); |
|
77 |
static void command(struct session *, int, char *); |
|
78 |
static void session_io(struct io *, int); |
|
79 |
static void parse(struct session *, char *); |
|
80 |
static void auth_command(struct session *, int, char *); |
|
81 |
static void trans_command(struct session *, int, char *); |
|
82 |
static void get_list_all(struct session *, int); |
|
83 |
static void get_list(struct session *, unsigned int, int); |
|
84 |
static void maildrop_imsgev(struct imsgev *, int, struct imsg *); |
|
85 |
static void handle_init(struct session *, struct imsg *); |
|
86 |
static void handle_retr(struct session *, struct imsg *); |
|
87 |
static void handle_dele(struct session *, struct imsg *); |
|
88 |
static void handle_list(struct session *, struct imsg *); |
|
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
89 |
static void handle_list_all(struct session *, struct imsg *); |
0 | 90 |
static void handle_update(struct session *, struct imsg *); |
91 |
static void needfd(struct imsgev *); |
|
92 |
static void pop3_debug(char *, ...); |
|
93 |
static void session_write(struct session *, const char *, size_t); |
|
94 |
static const char *strstate(enum state); |
|
95 |
||
96 |
struct session_tree sessions; |
|
97 |
static int _pop3_debug = 1; |
|
98 |
||
99 |
void |
|
100 |
session_init(struct listener *l, int fd) |
|
101 |
{ |
|
102 |
struct session *s; |
|
103 |
void *ssl; |
|
104 |
extern void *ssl_ctx; |
|
105 |
||
106 |
s = xcalloc(1, sizeof(*s), "session_init"); |
|
107 |
s->l = l; |
|
108 |
if (iobuf_init(&s->iobuf, 0, 0) == -1) |
|
109 |
fatal("iobuf_init"); |
|
110 |
||
111 |
io_init(&s->io, fd, s, session_io, &s->iobuf); |
|
112 |
io_set_timeout(&s->io, TIMEOUT); |
|
113 |
s->id = arc4random(); |
|
114 |
s->state = AUTH; |
|
115 |
if (s->l->flags & POP3S) { |
|
116 |
s->flags |= POP3S; |
|
17
596dec2978b2
Remove redundant sock member in session. io already holds the sock for each session.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
8
diff
changeset
|
117 |
ssl = pop3s_init(ssl_ctx, fd); |
0 | 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 |
logit(LOG_INFO, "%u: session closed", entry->id); |
|
152 |
free(entry); |
|
153 |
} |
|
154 |
||
155 |
static void |
|
156 |
session_io(struct io *io, int evt) |
|
157 |
{ |
|
158 |
struct session *s = io->arg; |
|
159 |
char *line; |
|
160 |
size_t len; |
|
161 |
||
162 |
pop3_debug("%u: %s", s->id, io_strevent(evt)); |
|
163 |
switch (evt) { |
|
164 |
case IO_DATAIN: |
|
165 |
line = iobuf_getline(&s->iobuf, &len); |
|
166 |
if (line == NULL) { |
|
167 |
iobuf_normalize(&s->iobuf); |
|
168 |
break; |
|
169 |
} |
|
170 |
if (strncasecmp(line, "PASS", 4) == 0) |
|
171 |
pop3_debug(">>> PASS"); |
|
172 |
else |
|
173 |
pop3_debug(">>> %s", line); |
|
174 |
parse(s, line); |
|
175 |
break; |
|
176 |
case IO_LOWAT: |
|
177 |
if (iobuf_queued(&s->iobuf) == 0) |
|
178 |
io_set_read(io); |
|
179 |
break; |
|
180 |
case IO_TLSREADY: |
|
181 |
/* greet only for pop3s, STLS already greeted */ |
|
182 |
if (s->flags & POP3S) { |
|
183 |
log_connect(s->id, &s->l->ss, s->l->ss.ss_len); |
|
184 |
session_reply(s, "%s", "+OK pop3 ready"); |
|
185 |
io_set_write(&s->io); |
|
186 |
} |
|
187 |
SPLAY_INSERT(session_tree, &sessions, s); |
|
188 |
/* mark STLS session as secure */ |
|
189 |
s->flags |= POP3S; |
|
190 |
logit(LOG_INFO, "%u: TLS ready", s->id); |
|
191 |
break; |
|
192 |
case IO_DISCONNECTED: |
|
193 |
case IO_TIMEOUT: |
|
194 |
case IO_ERROR: |
|
195 |
session_close(s, 0); |
|
196 |
break; |
|
197 |
default: |
|
198 |
logit(LOG_DEBUG, "unknown event %s", io_strevent(evt)); |
|
199 |
break; |
|
200 |
} |
|
201 |
} |
|
202 |
||
203 |
static void |
|
204 |
parse(struct session *s, char *line) |
|
205 |
{ |
|
206 |
enum arg_constraint c = OPTIONAL; |
|
207 |
int i, cmd = -1; |
|
208 |
char *args; |
|
209 |
||
210 |
/* trim newline */ |
|
211 |
line[strcspn(line, "\n")] = '\0'; |
|
212 |
||
213 |
args = strchr(line, ' '); |
|
214 |
if (args) { |
|
215 |
*args++ = '\0'; |
|
216 |
while (isspace((unsigned char)*args)) |
|
217 |
args++; |
|
218 |
} |
|
219 |
||
220 |
for (i = 0; commands[i].code != -1; i++) { |
|
221 |
if (strcasecmp(line, commands[i].cmd) == 0) { |
|
222 |
cmd = commands[i].code; |
|
223 |
c = commands[i].c; |
|
224 |
break; |
|
225 |
} |
|
226 |
} |
|
227 |
||
228 |
if (cmd == -1) { |
|
229 |
logit(LOG_INFO, "%u: invalid command %s", s->id, line); |
|
230 |
session_reply(s, "%s", "-ERR invalid command"); |
|
231 |
io_set_write(&s->io); |
|
232 |
return; |
|
233 |
} |
|
234 |
||
235 |
if (c == PROHIBITED && args) { |
|
236 |
session_reply(s, "%s", "-ERR no arguments allowed"); |
|
237 |
io_set_write(&s->io); |
|
238 |
return; |
|
239 |
} else if ((c == REQUIRED) && |
|
240 |
(args == NULL || strlen(args) >= ARGLEN)) { |
|
241 |
session_reply(s, "%s", "-ERR args required or too long"); |
|
242 |
io_set_write(&s->io); |
|
243 |
return; |
|
244 |
} |
|
245 |
||
246 |
command(s, cmd, args); |
|
247 |
} |
|
248 |
||
249 |
static void |
|
250 |
command(struct session *s, int cmd, char *args) |
|
251 |
{ |
|
252 |
switch (s->state) { |
|
253 |
case AUTH: |
|
254 |
auth_command(s, cmd, args); |
|
255 |
break; |
|
256 |
case TRANSACTION: |
|
257 |
trans_command(s, cmd, args); |
|
258 |
break; |
|
259 |
case UPDATE: |
|
260 |
session_reply(s, "%s", "-ERR commands not allowed"); |
|
261 |
io_set_write(&s->io); |
|
262 |
break; |
|
263 |
default: |
|
264 |
fatalx("Invalid state"); |
|
265 |
} |
|
266 |
} |
|
267 |
||
268 |
static void |
|
269 |
auth_command(struct session *s, int cmd, char *args) |
|
270 |
{ |
|
271 |
extern void *ssl_ctx; |
|
272 |
void *ssl; |
|
273 |
||
274 |
switch (cmd) { |
|
275 |
case CMD_STLS: |
|
276 |
if (s->flags & POP3S) { |
|
277 |
session_reply(s, "%s", "-ERR already secured"); |
|
278 |
break; |
|
279 |
} |
|
280 |
session_reply(s, "%s", "+OK"); |
|
281 |
io_set_write(&s->io); |
|
282 |
iobuf_flush(&s->iobuf, s->io.sock); |
|
283 |
/* add back when IO_TLSREADY. */ |
|
284 |
SPLAY_REMOVE(session_tree, &sessions, s); |
|
17
596dec2978b2
Remove redundant sock member in session. io already holds the sock for each session.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
8
diff
changeset
|
285 |
ssl = pop3s_init(ssl_ctx, s->io.sock); |
0 | 286 |
io_set_read(&s->io); |
287 |
io_start_tls(&s->io, ssl); |
|
288 |
return; |
|
289 |
case CMD_CAPA: |
|
290 |
capa(s); |
|
291 |
break; |
|
292 |
case CMD_USER: |
|
21
a591c0872243
args are checked for size in parse function. Cast to void as no truncation check required here.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
17
diff
changeset
|
293 |
(void)strlcpy(s->user, args, sizeof(s->user)); |
0 | 294 |
session_reply(s, "%s", "+OK"); |
295 |
break; |
|
296 |
case CMD_PASS: |
|
297 |
if (s->user[0] == '\0') { |
|
298 |
session_reply(s, "%s", "-ERR no USER specified"); |
|
299 |
break; |
|
300 |
} |
|
21
a591c0872243
args are checked for size in parse function. Cast to void as no truncation check required here.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
17
diff
changeset
|
301 |
(void)strlcpy(s->pass, args, sizeof(s->pass)); |
0 | 302 |
auth_request(s); |
303 |
return; |
|
304 |
case CMD_QUIT: |
|
305 |
session_reply(s, "%s", "+OK"); |
|
306 |
io_set_write(&s->io); |
|
307 |
session_close(s, 1); |
|
308 |
return; |
|
309 |
default: |
|
310 |
session_reply(s, "%s", "-ERR invalid command"); |
|
311 |
break; |
|
312 |
} |
|
313 |
||
314 |
io_set_write(&s->io); |
|
315 |
} |
|
316 |
||
317 |
static void |
|
318 |
auth_request(struct session *s) |
|
319 |
{ |
|
320 |
extern struct imsgev iev_pop3d; |
|
321 |
struct auth_req req; |
|
322 |
||
323 |
memset(&req, 0, sizeof(req)); |
|
22
5529e269a78c
Equal sized buffer copy, no truncation happens.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
21
diff
changeset
|
324 |
(void)strlcpy(req.user, s->user, sizeof(req.user)); |
5529e269a78c
Equal sized buffer copy, no truncation happens.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
21
diff
changeset
|
325 |
(void)strlcpy(req.pass, s->pass, sizeof(req.pass)); |
0 | 326 |
imsgev_xcompose(&iev_pop3d, IMSG_AUTH, s->id, 0, -1, |
327 |
&req, sizeof(req), "auth_request"); |
|
328 |
} |
|
329 |
||
330 |
static void |
|
331 |
capa(struct session *s) |
|
332 |
{ |
|
333 |
session_reply(s, "%s", "+OK"); |
|
334 |
session_reply(s, "%s", "STLS"); |
|
335 |
session_reply(s, "%s", "USER"); |
|
336 |
session_reply(s, "%s", "TOP"); |
|
337 |
session_reply(s, "%s", "UIDL"); |
|
338 |
session_reply(s, "%s", "IMPLEMENTATION pop3d"); |
|
339 |
session_reply(s, "%s", "."); |
|
340 |
} |
|
341 |
||
342 |
static void |
|
343 |
trans_command(struct session *s, int cmd, char *args) |
|
344 |
{ |
|
345 |
struct retr_req retr_req; |
|
346 |
unsigned int idx, n; |
|
347 |
char *c; |
|
348 |
const char *errstr; |
|
349 |
int uidl = 0; |
|
350 |
||
351 |
memset(&retr_req, 0, sizeof(retr_req)); |
|
352 |
switch (cmd) { |
|
353 |
case CMD_CAPA: |
|
354 |
capa(s); |
|
355 |
break; |
|
356 |
case CMD_STAT: |
|
357 |
session_reply(s, "%s %zu %zu", "+OK", s->nmsgs, s->m_sz); |
|
358 |
break; |
|
359 |
case CMD_TOP: |
|
360 |
if ((c = strchr(args, ' ')) == NULL) { |
|
361 |
session_reply(s, "%s", "-ERR invalid arguments"); |
|
362 |
break; |
|
363 |
} |
|
364 |
*c++ = '\0'; |
|
365 |
n = strtonum(c, 0, UINT_MAX, &errstr); |
|
366 |
if (errstr) { |
|
367 |
session_reply(s, "%s", "-ERR invalid n"); |
|
368 |
break; |
|
369 |
} |
|
370 |
retr_req.top = 1; |
|
371 |
retr_req.ntop = n; |
|
372 |
/* FALLTRHROUGH */ |
|
373 |
case CMD_RETR: |
|
374 |
if (!get_index(s, args, &retr_req.idx)) |
|
375 |
break; |
|
376 |
imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_RETR, |
|
377 |
s->id, 0, -1, &retr_req, sizeof(retr_req), "trans_command"); |
|
378 |
return; |
|
379 |
case CMD_NOOP: |
|
380 |
session_reply(s, "%s", "+OK"); |
|
381 |
break; |
|
382 |
case CMD_DELE: |
|
383 |
if (!get_index(s, args, &idx)) |
|
384 |
break; |
|
385 |
imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_DELE, |
|
386 |
s->id, 0, -1, &idx, sizeof(idx), "trans_command"); |
|
387 |
return; |
|
388 |
case CMD_RSET: |
|
389 |
imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_RSET, |
|
390 |
s->id, 0, -1, NULL, 0, "trans_command"); |
|
391 |
return; |
|
392 |
case CMD_UIDL: |
|
393 |
uidl = 1; |
|
394 |
/* FALLTHROUGH */ |
|
395 |
case CMD_LIST: |
|
396 |
if (args) { |
|
397 |
if (!get_index(s, args, &idx)) |
|
398 |
break; |
|
399 |
get_list(s, idx, uidl); |
|
400 |
} else |
|
401 |
get_list_all(s, uidl); |
|
402 |
return; |
|
403 |
case CMD_QUIT: |
|
404 |
imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_UPDATE, |
|
405 |
s->id, 0, -1, NULL, 0, "trans_command"); |
|
406 |
session_set_state(s, UPDATE); |
|
407 |
return; |
|
408 |
default: |
|
409 |
session_reply(s, "%s", "-ERR invalid command"); |
|
410 |
break; |
|
411 |
} |
|
412 |
||
413 |
io_set_write(&s->io); |
|
414 |
} |
|
415 |
||
416 |
static void |
|
417 |
get_list_all(struct session *s, int uidl) |
|
418 |
{ |
|
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
419 |
io_pause(&s->io, IO_PAUSE_IN); |
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
420 |
session_reply(s, "+OK"); |
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
421 |
imsgev_xcompose(&s->iev_maildrop, IMSG_MAILDROP_LISTALL, |
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
422 |
s->id, 0, -1, &uidl, sizeof(uidl), "list_all"); |
0 | 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 |
||
447 |
switch (code) { |
|
448 |
case IMSGEV_IMSG: |
|
449 |
key.id = imsg->hdr.peerid; |
|
450 |
r = SPLAY_FIND(session_tree, &sessions, &key); |
|
451 |
if (r == NULL) { |
|
452 |
logit(LOG_INFO, "%u: session not found", key.id); |
|
453 |
fatalx("session: session lost"); |
|
454 |
} |
|
455 |
switch (imsg->hdr.type) { |
|
456 |
case IMSG_MAILDROP_INIT: |
|
457 |
handle_init(r, imsg); |
|
458 |
break; |
|
459 |
case IMSG_MAILDROP_RETR: |
|
460 |
handle_retr(r, imsg); |
|
461 |
break; |
|
462 |
case IMSG_MAILDROP_DELE: |
|
463 |
handle_dele(r, imsg); |
|
464 |
break; |
|
465 |
case IMSG_MAILDROP_RSET: |
|
466 |
session_reply(r, "%s", "+OK reset"); |
|
467 |
io_set_write(&r->io); |
|
468 |
break; |
|
469 |
case IMSG_MAILDROP_LIST: |
|
470 |
handle_list(r, imsg); |
|
471 |
break; |
|
472 |
case IMSG_MAILDROP_LISTALL: |
|
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
473 |
handle_list_all(r, imsg); |
0 | 474 |
break; |
475 |
case IMSG_MAILDROP_UPDATE: |
|
476 |
handle_update(r, imsg); |
|
477 |
break; |
|
478 |
default: |
|
479 |
logit(LOG_DEBUG, "%s: unexpected imsg %u", |
|
480 |
__func__, imsg->hdr.type); |
|
481 |
break; |
|
482 |
} |
|
483 |
break; |
|
484 |
case IMSGEV_EREAD: |
|
485 |
case IMSGEV_EWRITE: |
|
486 |
case IMSGEV_EIMSG: |
|
487 |
fatal("session: imsgev read/write error"); |
|
488 |
break; |
|
489 |
} |
|
490 |
} |
|
491 |
||
492 |
static void |
|
493 |
handle_init(struct session *s, struct imsg *imsg) |
|
494 |
{ |
|
495 |
size_t datalen; |
|
496 |
struct stats *stats; |
|
497 |
||
498 |
datalen = imsg->hdr.len - sizeof(imsg->hdr); |
|
499 |
if (datalen) { |
|
500 |
stats = imsg->data; |
|
501 |
s->m_sz = stats->sz; |
|
502 |
s->nmsgs = stats->nmsgs; |
|
503 |
session_reply(s, "%s", "+OK maildrop ready"); |
|
504 |
io_set_write(&s->io); |
|
505 |
session_set_state(s, TRANSACTION); |
|
506 |
} else { |
|
507 |
session_reply(s, "%s", "-ERR maildrop init failed"); |
|
508 |
io_set_write(&s->io); |
|
509 |
session_close(s, 1); |
|
510 |
} |
|
511 |
} |
|
512 |
||
513 |
static void |
|
514 |
handle_retr(struct session *s, struct imsg *imsg) |
|
515 |
{ |
|
516 |
struct retr_res *r = imsg->data; |
|
517 |
FILE *fp; |
|
518 |
char *line; |
|
519 |
size_t len; |
|
520 |
||
521 |
if (imsg->fd == -1) { |
|
522 |
session_reply(s, "%s", "-ERR marked for delete"); |
|
523 |
io_set_write(&s->io); |
|
524 |
return; |
|
525 |
} |
|
526 |
||
527 |
if ((fp = fdopen(imsg->fd, "r")) == NULL) { |
|
528 |
logit(LOG_INFO, "%zu: retr failed", s->id); |
|
529 |
session_reply(s, "%s", "-ERR RETR failed"); |
|
530 |
io_set_write(&s->io); |
|
531 |
session_close(s, 1); |
|
532 |
return; |
|
533 |
} |
|
534 |
||
535 |
if (fseek(fp, r->offset, SEEK_SET) == -1) |
|
536 |
fatal("fseek"); |
|
537 |
||
538 |
session_reply(s, "%s", "+OK"); |
|
539 |
/* Ignore "From " line when type is mbox; maildir doesn't have it */ |
|
540 |
if ((line = fgetln(fp, &len)) && strncmp(line, "From ", 5)) |
|
541 |
session_write(s, line, len); |
|
542 |
||
543 |
if (r->top) { |
|
544 |
/* print headers regardless of ntop */ |
|
545 |
while ((line = fgetln(fp, &len))) { |
|
546 |
session_write(s, line, len); |
|
547 |
r->nlines -= 1; |
|
548 |
if (strncmp(line , "\n", 1) == 0) |
|
549 |
break; |
|
550 |
} |
|
551 |
||
552 |
/* print ntop lines of body */ |
|
553 |
while ((r->ntop-- > 0) && r->nlines-- && |
|
554 |
(line = fgetln(fp, &len))) |
|
555 |
session_write(s, line, len); |
|
556 |
} else |
|
557 |
while (r->nlines-- && (line = fgetln(fp, &len))) |
|
558 |
session_write(s, line, len); |
|
559 |
||
560 |
session_reply(s, "%s", "."); |
|
561 |
io_set_write(&s->io); |
|
562 |
fclose(fp); |
|
563 |
close(imsg->fd); |
|
564 |
} |
|
565 |
||
566 |
static void |
|
567 |
handle_dele(struct session *s, struct imsg *imsg) |
|
568 |
{ |
|
569 |
int *res = imsg->data; |
|
570 |
||
571 |
if (*res == 0) |
|
572 |
session_reply(s, "%s", "+OK marked for delete"); |
|
573 |
else |
|
574 |
session_reply(s, "%s", "+ERR msg already marked delete"); |
|
575 |
||
576 |
io_set_write(&s->io); |
|
577 |
} |
|
578 |
||
579 |
/* DELEted msg's hash and sz will be zero, ignore them */ |
|
580 |
static void |
|
581 |
handle_list(struct session *s, struct imsg *imsg) |
|
582 |
{ |
|
583 |
struct list_res *res = imsg->data; |
|
584 |
||
585 |
res->idx += 1; /* POP3 index is 1 based */ |
|
586 |
if (res->uidl) { |
|
587 |
if (strlen(res->u.hash)) |
|
588 |
session_reply(s, "+OK %zu %s", res->idx, res->u.hash); |
|
589 |
else |
|
590 |
session_reply(s, "-ERR marked for delete"); |
|
591 |
} else { |
|
592 |
if (res->u.sz) |
|
593 |
session_reply(s, "+OK %zu %zu", res->idx, res->u.sz); |
|
594 |
else |
|
595 |
session_reply(s, "-ERR marked for delete"); |
|
596 |
} |
|
597 |
||
598 |
io_set_write(&s->io); |
|
599 |
} |
|
600 |
||
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
601 |
/* List terminal is indicated by hash being empty string or sz = 0 */ |
0 | 602 |
static void |
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
603 |
handle_list_all(struct session *s, struct imsg *imsg) |
0 | 604 |
{ |
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
605 |
struct list_res *res = imsg->data; |
0 | 606 |
|
8
dd898f214ed7
Use correct format of listing.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
2
diff
changeset
|
607 |
res->idx += 1; /* POP3 index is 1 based */ |
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
608 |
if (res->uidl) |
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
609 |
if (strlen(res->u.hash)) |
8
dd898f214ed7
Use correct format of listing.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
2
diff
changeset
|
610 |
session_reply(s, "%zu %s", res->idx, res->u.hash); |
26
b20d78c55e91
knf and whitespace fixes.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
25
diff
changeset
|
611 |
else |
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
612 |
goto end; |
0 | 613 |
else |
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
614 |
if (res->u.sz) |
8
dd898f214ed7
Use correct format of listing.
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
2
diff
changeset
|
615 |
session_reply(s, "%zu %zu", res->idx, res->u.sz); |
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
616 |
else |
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
617 |
goto end; |
0 | 618 |
|
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
619 |
return; |
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
620 |
end: |
0 | 621 |
session_reply(s, "."); |
622 |
io_set_write(&s->io); |
|
2
6e7b98264ea2
Simplify list_all handling. Send an imsg for each msg instead of
Sunil Nimmagadda <sunil@nimmagadda.net>
parents:
0
diff
changeset
|
623 |
io_resume(&s->io, IO_PAUSE_IN); |
0 | 624 |
} |
625 |
||
626 |
static void |
|
627 |
handle_update(struct session *s, struct imsg *imsg) |
|
628 |
{ |
|
629 |
int *res = imsg->data; |
|
630 |
||
631 |
if (*res == 0) |
|
632 |
session_reply(s, "%s", "+OK maildrop updated"); |
|
633 |
else |
|
634 |
session_reply(s, "%s", "-ERR maildrop update failed"); |
|
635 |
||
636 |
io_set_write(&s->io); |
|
637 |
session_close(s, 1); |
|
638 |
} |
|
639 |
||
640 |
static void |
|
641 |
needfd(struct imsgev *iev) |
|
642 |
{ |
|
643 |
/* XXX */ |
|
644 |
fatalx("session needs an fd"); |
|
645 |
} |
|
646 |
||
647 |
int |
|
648 |
session_cmp(struct session *a, struct session *b) |
|
649 |
{ |
|
650 |
if (a->id < b->id) |
|
651 |
return (-1); |
|
652 |
||
653 |
if (a->id > b->id) |
|
654 |
return (1); |
|
655 |
||
656 |
return (0); |
|
657 |
} |
|
658 |
||
659 |
void |
|
660 |
session_set_state(struct session *s, enum state newstate) |
|
661 |
{ |
|
662 |
pop3_debug("%u: %s -> %s", s->id, strstate(s->state), |
|
663 |
strstate(newstate)); |
|
664 |
s->state = newstate; |
|
665 |
} |
|
666 |
||
667 |
#define CASE(x) case x : return #x |
|
668 |
static const char * |
|
669 |
strstate(enum state state) |
|
670 |
{ |
|
671 |
static char buf[32]; |
|
672 |
||
673 |
switch (state) { |
|
674 |
CASE(AUTH); |
|
675 |
CASE(TRANSACTION); |
|
676 |
CASE(UPDATE); |
|
677 |
default: |
|
23
a65ab8b73e52
This cannot truncate unless new states are added.
Sunil Nimmagadda <sunil@sunilnimmagadda.com>
parents:
22
diff
changeset
|
678 |
(void)snprintf(buf, sizeof(buf), "%d ???", state); |
0 | 679 |
return (buf); |
680 |
} |
|
681 |
} |
|
682 |
||
683 |
void |
|
684 |
session_reply(struct session *s, char *fmt, ...) |
|
685 |
{ |
|
686 |
va_list ap; |
|
687 |
int n; |
|
688 |
char buf[MAXLINESIZE]; |
|
689 |
||
690 |
va_start(ap, fmt); |
|
691 |
n = vsnprintf(buf, sizeof(buf), fmt, ap); |
|
692 |
va_end(ap); |
|
693 |
if (n == -1 || n > MAXLINESIZE) |
|
694 |
fatalx("session_reply: response too long"); |
|
695 |
||
696 |
if (buf[0] == '+') |
|
697 |
pop3_debug("<<< +OK"); |
|
698 |
else if (buf[0] == '-') |
|
699 |
pop3_debug("<<< -ERR"); |
|
700 |
||
701 |
iobuf_xfqueue(&s->iobuf, "session_reply", "%s\r\n", buf); |
|
702 |
} |
|
703 |
||
704 |
static void |
|
705 |
session_write(struct session *s, const char *data, size_t len) |
|
706 |
{ |
|
707 |
/* remove terminating \n or \r\n if any */ |
|
708 |
if (data[len - 1] == '\n') |
|
709 |
len -= 1; |
|
710 |
||
711 |
if (data[len - 1] == '\r') |
|
712 |
len -= 1; |
|
713 |
||
714 |
/* byte stuff "." if at beginning of line */ |
|
715 |
if (data[0] == '.') |
|
716 |
iobuf_xfqueue(&s->iobuf, "session_write", "."); |
|
717 |
||
718 |
iobuf_xqueue(&s->iobuf, "session_write", data, len); |
|
719 |
/* explicitly terminate with CRLF */ |
|
720 |
iobuf_xfqueue(&s->iobuf, "session_write", "\r\n"); |
|
721 |
} |
|
722 |
||
723 |
static void |
|
724 |
pop3_debug(char *fmt, ...) |
|
725 |
{ |
|
726 |
va_list ap; |
|
727 |
char buf[MAXLINESIZE]; |
|
728 |
int n; |
|
729 |
||
730 |
if (!_pop3_debug) |
|
731 |
return; |
|
732 |
||
733 |
va_start(ap, fmt); |
|
734 |
n = vsnprintf(buf, sizeof(buf), fmt, ap); |
|
735 |
va_end(ap); |
|
736 |
if (n == -1 || n > MAXLINESIZE) |
|
737 |
fatalx("pop3_debug: response too long"); |
|
738 |
||
739 |
logit(LOG_DEBUG, "%s", buf); |
|
740 |
} |
|
741 |
||
742 |
SPLAY_GENERATE(session_tree, session, entry, session_cmp); |
|
743 |