336
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10 #include <ngx_mail.h>
|
|
11 #include <ngx_mail_pop3_module.h>
|
|
12
|
|
13
|
|
14 static ngx_int_t ngx_mail_pop3_user(ngx_mail_session_t *s, ngx_connection_t *c);
|
|
15 static ngx_int_t ngx_mail_pop3_pass(ngx_mail_session_t *s, ngx_connection_t *c);
|
|
16 static ngx_int_t ngx_mail_pop3_capa(ngx_mail_session_t *s, ngx_connection_t *c,
|
|
17 ngx_int_t stls);
|
|
18 static ngx_int_t ngx_mail_pop3_stls(ngx_mail_session_t *s, ngx_connection_t *c);
|
|
19 static ngx_int_t ngx_mail_pop3_apop(ngx_mail_session_t *s, ngx_connection_t *c);
|
|
20 static ngx_int_t ngx_mail_pop3_auth(ngx_mail_session_t *s, ngx_connection_t *c);
|
|
21
|
|
22
|
|
23 static u_char pop3_greeting[] = "+OK POP3 ready" CRLF;
|
|
24 static u_char pop3_ok[] = "+OK" CRLF;
|
|
25 static u_char pop3_next[] = "+ " CRLF;
|
|
26 static u_char pop3_username[] = "+ VXNlcm5hbWU6" CRLF;
|
|
27 static u_char pop3_password[] = "+ UGFzc3dvcmQ6" CRLF;
|
|
28 static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF;
|
|
29
|
|
30
|
|
31 void
|
|
32 ngx_mail_pop3_init_session(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
33 {
|
|
34 u_char *p;
|
|
35 ngx_mail_core_srv_conf_t *cscf;
|
|
36 ngx_mail_pop3_srv_conf_t *pscf;
|
|
37
|
|
38 pscf = ngx_mail_get_module_srv_conf(s, ngx_mail_pop3_module);
|
|
39 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
|
|
40
|
|
41 if (pscf->auth_methods
|
|
42 & (NGX_MAIL_AUTH_APOP_ENABLED|NGX_MAIL_AUTH_CRAM_MD5_ENABLED))
|
|
43 {
|
|
44 if (ngx_mail_salt(s, c, cscf) != NGX_OK) {
|
|
45 ngx_mail_session_internal_server_error(s);
|
|
46 return;
|
|
47 }
|
|
48
|
382
|
49 s->out.data = ngx_pnalloc(c->pool, sizeof(pop3_greeting) + s->salt.len);
|
336
|
50 if (s->out.data == NULL) {
|
|
51 ngx_mail_session_internal_server_error(s);
|
|
52 return;
|
|
53 }
|
|
54
|
|
55 p = ngx_cpymem(s->out.data, pop3_greeting, sizeof(pop3_greeting) - 3);
|
|
56 *p++ = ' ';
|
|
57 p = ngx_cpymem(p, s->salt.data, s->salt.len);
|
|
58
|
|
59 s->out.len = p - s->out.data;
|
|
60
|
|
61 } else {
|
|
62 s->out.len = sizeof(pop3_greeting) - 1;
|
|
63 s->out.data = pop3_greeting;
|
|
64 }
|
|
65
|
|
66 c->read->handler = ngx_mail_pop3_init_protocol;
|
|
67
|
|
68 ngx_add_timer(c->read, cscf->timeout);
|
|
69
|
430
|
70 if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
|
336
|
71 ngx_mail_close_connection(c);
|
|
72 }
|
|
73
|
|
74 ngx_mail_send(c->write);
|
|
75 }
|
|
76
|
|
77
|
|
78 void
|
|
79 ngx_mail_pop3_init_protocol(ngx_event_t *rev)
|
|
80 {
|
|
81 ngx_connection_t *c;
|
|
82 ngx_mail_session_t *s;
|
|
83
|
|
84 c = rev->data;
|
|
85
|
|
86 c->log->action = "in auth state";
|
|
87
|
|
88 if (rev->timedout) {
|
|
89 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
90 c->timedout = 1;
|
|
91 ngx_mail_close_connection(c);
|
|
92 return;
|
|
93 }
|
|
94
|
|
95 s = c->data;
|
|
96
|
|
97 if (s->buffer == NULL) {
|
|
98 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t))
|
|
99 == NGX_ERROR)
|
|
100 {
|
|
101 ngx_mail_session_internal_server_error(s);
|
|
102 return;
|
|
103 }
|
|
104
|
|
105 s->buffer = ngx_create_temp_buf(c->pool, 128);
|
|
106 if (s->buffer == NULL) {
|
|
107 ngx_mail_session_internal_server_error(s);
|
|
108 return;
|
|
109 }
|
|
110 }
|
|
111
|
|
112 s->mail_state = ngx_pop3_start;
|
|
113 c->read->handler = ngx_mail_pop3_auth_state;
|
|
114
|
|
115 ngx_mail_pop3_auth_state(rev);
|
|
116 }
|
|
117
|
|
118
|
|
119 void
|
|
120 ngx_mail_pop3_auth_state(ngx_event_t *rev)
|
|
121 {
|
|
122 ngx_int_t rc;
|
|
123 ngx_connection_t *c;
|
|
124 ngx_mail_session_t *s;
|
|
125
|
|
126 c = rev->data;
|
|
127 s = c->data;
|
|
128
|
|
129 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "pop3 auth state");
|
|
130
|
|
131 if (rev->timedout) {
|
|
132 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
133 c->timedout = 1;
|
|
134 ngx_mail_close_connection(c);
|
|
135 return;
|
|
136 }
|
|
137
|
|
138 if (s->out.len) {
|
|
139 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "pop3 send handler busy");
|
|
140 s->blocked = 1;
|
|
141 return;
|
|
142 }
|
|
143
|
|
144 s->blocked = 0;
|
|
145
|
|
146 rc = ngx_mail_read_command(s, c);
|
|
147
|
|
148 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
149 return;
|
|
150 }
|
|
151
|
|
152 s->out.len = sizeof(pop3_ok) - 1;
|
|
153 s->out.data = pop3_ok;
|
|
154
|
|
155 if (rc == NGX_OK) {
|
|
156 switch (s->mail_state) {
|
|
157
|
|
158 case ngx_pop3_start:
|
|
159
|
|
160 switch (s->command) {
|
|
161
|
|
162 case NGX_POP3_USER:
|
|
163 rc = ngx_mail_pop3_user(s, c);
|
|
164 break;
|
|
165
|
|
166 case NGX_POP3_CAPA:
|
|
167 rc = ngx_mail_pop3_capa(s, c, 1);
|
|
168 break;
|
|
169
|
|
170 case NGX_POP3_APOP:
|
|
171 rc = ngx_mail_pop3_apop(s, c);
|
|
172 break;
|
|
173
|
|
174 case NGX_POP3_AUTH:
|
|
175 rc = ngx_mail_pop3_auth(s, c);
|
|
176 break;
|
|
177
|
|
178 case NGX_POP3_QUIT:
|
|
179 s->quit = 1;
|
|
180 break;
|
|
181
|
|
182 case NGX_POP3_NOOP:
|
|
183 break;
|
|
184
|
|
185 case NGX_POP3_STLS:
|
|
186 rc = ngx_mail_pop3_stls(s, c);
|
|
187 break;
|
|
188
|
|
189 default:
|
|
190 rc = NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
191 break;
|
|
192 }
|
|
193
|
|
194 break;
|
|
195
|
|
196 case ngx_pop3_user:
|
|
197
|
|
198 switch (s->command) {
|
|
199
|
|
200 case NGX_POP3_PASS:
|
|
201 rc = ngx_mail_pop3_pass(s, c);
|
|
202 break;
|
|
203
|
|
204 case NGX_POP3_CAPA:
|
|
205 rc = ngx_mail_pop3_capa(s, c, 0);
|
|
206 break;
|
|
207
|
|
208 case NGX_POP3_QUIT:
|
|
209 s->quit = 1;
|
|
210 break;
|
|
211
|
|
212 case NGX_POP3_NOOP:
|
|
213 break;
|
|
214
|
|
215 default:
|
|
216 rc = NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
217 break;
|
|
218 }
|
|
219
|
|
220 break;
|
|
221
|
|
222 /* suppress warinings */
|
|
223 case ngx_pop3_passwd:
|
|
224 break;
|
|
225
|
|
226 case ngx_pop3_auth_login_username:
|
444
|
227 rc = ngx_mail_auth_login_username(s, c, 0);
|
336
|
228
|
|
229 s->out.len = sizeof(pop3_password) - 1;
|
|
230 s->out.data = pop3_password;
|
|
231 s->mail_state = ngx_pop3_auth_login_password;
|
|
232 break;
|
|
233
|
|
234 case ngx_pop3_auth_login_password:
|
|
235 rc = ngx_mail_auth_login_password(s, c);
|
|
236 break;
|
|
237
|
|
238 case ngx_pop3_auth_plain:
|
|
239 rc = ngx_mail_auth_plain(s, c, 0);
|
|
240 break;
|
|
241
|
|
242 case ngx_pop3_auth_cram_md5:
|
|
243 rc = ngx_mail_auth_cram_md5(s, c);
|
|
244 break;
|
|
245 }
|
|
246 }
|
|
247
|
|
248 switch (rc) {
|
|
249
|
|
250 case NGX_DONE:
|
|
251 ngx_mail_auth(s, c);
|
|
252 return;
|
|
253
|
|
254 case NGX_ERROR:
|
|
255 ngx_mail_session_internal_server_error(s);
|
|
256 return;
|
|
257
|
|
258 case NGX_MAIL_PARSE_INVALID_COMMAND:
|
|
259 s->mail_state = ngx_pop3_start;
|
|
260 s->state = 0;
|
|
261
|
|
262 s->out.len = sizeof(pop3_invalid_command) - 1;
|
|
263 s->out.data = pop3_invalid_command;
|
|
264
|
|
265 /* fall through */
|
|
266
|
|
267 case NGX_OK:
|
|
268
|
|
269 s->args.nelts = 0;
|
|
270 s->buffer->pos = s->buffer->start;
|
|
271 s->buffer->last = s->buffer->start;
|
|
272
|
|
273 if (s->state) {
|
|
274 s->arg_start = s->buffer->start;
|
|
275 }
|
|
276
|
|
277 ngx_mail_send(c->write);
|
|
278 }
|
|
279 }
|
|
280
|
|
281 static ngx_int_t
|
|
282 ngx_mail_pop3_user(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
283 {
|
356
|
284 ngx_str_t *arg;
|
336
|
285
|
|
286 #if (NGX_MAIL_SSL)
|
|
287 if (ngx_mail_starttls_only(s, c)) {
|
|
288 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
289 }
|
|
290 #endif
|
|
291
|
|
292 if (s->args.nelts != 1) {
|
|
293 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
294 }
|
|
295
|
|
296 arg = s->args.elts;
|
|
297 s->login.len = arg[0].len;
|
382
|
298 s->login.data = ngx_pnalloc(c->pool, s->login.len);
|
336
|
299 if (s->login.data == NULL) {
|
|
300 return NGX_ERROR;
|
|
301 }
|
|
302
|
|
303 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
304
|
|
305 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
|
|
306 "pop3 login: \"%V\"", &s->login);
|
|
307
|
|
308 s->mail_state = ngx_pop3_user;
|
|
309
|
|
310 return NGX_OK;
|
|
311 }
|
|
312
|
|
313
|
|
314 static ngx_int_t
|
|
315 ngx_mail_pop3_pass(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
316 {
|
|
317 ngx_str_t *arg;
|
|
318
|
|
319 if (s->args.nelts != 1) {
|
|
320 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
321 }
|
|
322
|
|
323 arg = s->args.elts;
|
|
324 s->passwd.len = arg[0].len;
|
382
|
325 s->passwd.data = ngx_pnalloc(c->pool, s->passwd.len);
|
336
|
326 if (s->passwd.data == NULL) {
|
|
327 return NGX_ERROR;
|
|
328 }
|
|
329
|
|
330 ngx_memcpy(s->passwd.data, arg[0].data, s->passwd.len);
|
|
331
|
|
332 #if (NGX_DEBUG_MAIL_PASSWD)
|
|
333 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
|
|
334 "pop3 passwd: \"%V\"", &s->passwd);
|
|
335 #endif
|
|
336
|
|
337 return NGX_DONE;
|
|
338 }
|
|
339
|
|
340
|
|
341 static ngx_int_t
|
|
342 ngx_mail_pop3_capa(ngx_mail_session_t *s, ngx_connection_t *c, ngx_int_t stls)
|
|
343 {
|
|
344 ngx_mail_pop3_srv_conf_t *pscf;
|
|
345
|
|
346 pscf = ngx_mail_get_module_srv_conf(s, ngx_mail_pop3_module);
|
|
347
|
|
348 #if (NGX_MAIL_SSL)
|
|
349
|
|
350 if (stls && c->ssl == NULL) {
|
356
|
351 ngx_mail_ssl_conf_t *sslcf;
|
|
352
|
336
|
353 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
|
|
354
|
|
355 if (sslcf->starttls == NGX_MAIL_STARTTLS_ON) {
|
|
356 s->out = pscf->starttls_capability;
|
|
357 return NGX_OK;
|
|
358 }
|
|
359
|
|
360 if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) {
|
|
361 s->out = pscf->starttls_only_capability;
|
|
362 return NGX_OK;
|
|
363 }
|
|
364 }
|
|
365
|
|
366 #endif
|
|
367
|
|
368 s->out = pscf->capability;
|
|
369 return NGX_OK;
|
|
370 }
|
|
371
|
|
372
|
|
373 static ngx_int_t
|
|
374 ngx_mail_pop3_stls(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
375 {
|
|
376 #if (NGX_MAIL_SSL)
|
|
377 ngx_mail_ssl_conf_t *sslcf;
|
|
378
|
|
379 if (c->ssl == NULL) {
|
|
380 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
|
|
381 if (sslcf->starttls) {
|
|
382 c->read->handler = ngx_mail_starttls_handler;
|
|
383 return NGX_OK;
|
|
384 }
|
|
385 }
|
|
386
|
|
387 #endif
|
|
388
|
|
389 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
390 }
|
|
391
|
|
392
|
|
393 static ngx_int_t
|
|
394 ngx_mail_pop3_apop(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
395 {
|
|
396 ngx_str_t *arg;
|
|
397 ngx_mail_pop3_srv_conf_t *pscf;
|
|
398
|
|
399 #if (NGX_MAIL_SSL)
|
|
400 if (ngx_mail_starttls_only(s, c)) {
|
|
401 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
402 }
|
|
403 #endif
|
|
404
|
|
405 if (s->args.nelts != 2) {
|
|
406 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
407 }
|
|
408
|
|
409 pscf = ngx_mail_get_module_srv_conf(s, ngx_mail_pop3_module);
|
|
410
|
|
411 if (!(pscf->auth_methods & NGX_MAIL_AUTH_APOP_ENABLED)) {
|
|
412 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
413 }
|
|
414
|
|
415 arg = s->args.elts;
|
|
416
|
|
417 s->login.len = arg[0].len;
|
382
|
418 s->login.data = ngx_pnalloc(c->pool, s->login.len);
|
336
|
419 if (s->login.data == NULL) {
|
|
420 return NGX_ERROR;
|
|
421 }
|
|
422
|
|
423 ngx_memcpy(s->login.data, arg[0].data, s->login.len);
|
|
424
|
|
425 s->passwd.len = arg[1].len;
|
382
|
426 s->passwd.data = ngx_pnalloc(c->pool, s->passwd.len);
|
336
|
427 if (s->passwd.data == NULL) {
|
|
428 return NGX_ERROR;
|
|
429 }
|
|
430
|
|
431 ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
|
|
432
|
|
433 ngx_log_debug2(NGX_LOG_DEBUG_MAIL, c->log, 0,
|
|
434 "pop3 apop: \"%V\" \"%V\"", &s->login, &s->passwd);
|
|
435
|
|
436 s->auth_method = NGX_MAIL_AUTH_APOP;
|
|
437
|
|
438 return NGX_DONE;
|
|
439 }
|
|
440
|
|
441
|
|
442 static ngx_int_t
|
|
443 ngx_mail_pop3_auth(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
444 {
|
|
445 ngx_int_t rc;
|
|
446 ngx_mail_pop3_srv_conf_t *pscf;
|
|
447
|
|
448 #if (NGX_MAIL_SSL)
|
|
449 if (ngx_mail_starttls_only(s, c)) {
|
|
450 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
451 }
|
|
452 #endif
|
|
453
|
|
454 pscf = ngx_mail_get_module_srv_conf(s, ngx_mail_pop3_module);
|
|
455
|
|
456 if (s->args.nelts == 0) {
|
|
457 s->out = pscf->auth_capability;
|
|
458 s->state = 0;
|
|
459
|
|
460 return NGX_OK;
|
|
461 }
|
|
462
|
|
463 rc = ngx_mail_auth_parse(s, c);
|
|
464
|
|
465 switch (rc) {
|
|
466
|
|
467 case NGX_MAIL_AUTH_LOGIN:
|
|
468
|
|
469 s->out.len = sizeof(pop3_username) - 1;
|
|
470 s->out.data = pop3_username;
|
|
471 s->mail_state = ngx_pop3_auth_login_username;
|
|
472
|
|
473 return NGX_OK;
|
|
474
|
444
|
475 case NGX_MAIL_AUTH_LOGIN_USERNAME:
|
|
476
|
|
477 s->out.len = sizeof(pop3_password) - 1;
|
|
478 s->out.data = pop3_password;
|
|
479 s->mail_state = ngx_pop3_auth_login_password;
|
|
480
|
|
481 return ngx_mail_auth_login_username(s, c, 1);
|
|
482
|
336
|
483 case NGX_MAIL_AUTH_PLAIN:
|
|
484
|
|
485 s->out.len = sizeof(pop3_next) - 1;
|
|
486 s->out.data = pop3_next;
|
|
487 s->mail_state = ngx_pop3_auth_plain;
|
|
488
|
|
489 return NGX_OK;
|
|
490
|
|
491 case NGX_MAIL_AUTH_CRAM_MD5:
|
|
492
|
|
493 if (!(pscf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED)) {
|
|
494 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
495 }
|
|
496
|
|
497 if (ngx_mail_auth_cram_md5_salt(s, c, "+ ", 2) == NGX_OK) {
|
|
498 s->mail_state = ngx_pop3_auth_cram_md5;
|
|
499 return NGX_OK;
|
|
500 }
|
|
501
|
|
502 return NGX_ERROR;
|
|
503 }
|
|
504
|
|
505 return rc;
|
|
506 }
|