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_smtp_module.h>
|
|
12
|
|
13
|
366
|
14 static void ngx_mail_smtp_resolve_addr_handler(ngx_resolver_ctx_t *ctx);
|
384
|
15 static void ngx_mail_smtp_resolve_name(ngx_event_t *rev);
|
366
|
16 static void ngx_mail_smtp_resolve_name_handler(ngx_resolver_ctx_t *ctx);
|
|
17 static void ngx_mail_smtp_greeting(ngx_mail_session_t *s, ngx_connection_t *c);
|
336
|
18 static void ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev);
|
|
19 static ngx_int_t ngx_mail_smtp_create_buffer(ngx_mail_session_t *s,
|
|
20 ngx_connection_t *c);
|
|
21
|
|
22 static ngx_int_t ngx_mail_smtp_helo(ngx_mail_session_t *s, ngx_connection_t *c);
|
|
23 static ngx_int_t ngx_mail_smtp_auth(ngx_mail_session_t *s, ngx_connection_t *c);
|
|
24 static ngx_int_t ngx_mail_smtp_mail(ngx_mail_session_t *s, ngx_connection_t *c);
|
|
25 static ngx_int_t ngx_mail_smtp_starttls(ngx_mail_session_t *s,
|
|
26 ngx_connection_t *c);
|
|
27
|
|
28 static ngx_int_t ngx_mail_smtp_discard_command(ngx_mail_session_t *s,
|
|
29 ngx_connection_t *c, char *err);
|
|
30 static void ngx_mail_smtp_log_rejected_command(ngx_mail_session_t *s,
|
|
31 ngx_connection_t *c, char *err);
|
|
32
|
|
33
|
|
34 static u_char smtp_ok[] = "250 2.0.0 OK" CRLF;
|
|
35 static u_char smtp_bye[] = "221 2.0.0 Bye" CRLF;
|
358
|
36 static u_char smtp_starttls[] = "220 2.0.0 Start TLS" CRLF;
|
336
|
37 static u_char smtp_next[] = "334 " CRLF;
|
|
38 static u_char smtp_username[] = "334 VXNlcm5hbWU6" CRLF;
|
|
39 static u_char smtp_password[] = "334 UGFzc3dvcmQ6" CRLF;
|
|
40 static u_char smtp_invalid_command[] = "500 5.5.1 Invalid command" CRLF;
|
|
41 static u_char smtp_invalid_pipelining[] =
|
|
42 "503 5.5.0 Improper use of SMTP command pipelining" CRLF;
|
|
43 static u_char smtp_invalid_argument[] = "501 5.5.4 Invalid argument" CRLF;
|
|
44 static u_char smtp_auth_required[] = "530 5.7.1 Authentication required" CRLF;
|
|
45
|
|
46
|
366
|
47 static ngx_str_t smtp_unavailable = ngx_string("[UNAVAILABLE]");
|
|
48 static ngx_str_t smtp_tempunavail = ngx_string("[TEMPUNAVAIL]");
|
|
49
|
|
50
|
336
|
51 void
|
|
52 ngx_mail_smtp_init_session(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
53 {
|
366
|
54 struct sockaddr_in *sin;
|
|
55 ngx_resolver_ctx_t *ctx;
|
|
56 ngx_mail_core_srv_conf_t *cscf;
|
|
57
|
|
58 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
|
|
59
|
|
60 if (cscf->resolver == NULL) {
|
|
61 s->host = smtp_unavailable;
|
|
62 ngx_mail_smtp_greeting(s, c);
|
|
63 return;
|
|
64 }
|
|
65
|
|
66 c->log->action = "in resolving client address";
|
|
67
|
|
68 ctx = ngx_resolve_start(cscf->resolver, NULL);
|
|
69 if (ctx == NULL) {
|
|
70 ngx_mail_close_connection(c);
|
|
71 return;
|
|
72 }
|
|
73
|
|
74 /* AF_INET only */
|
|
75
|
|
76 sin = (struct sockaddr_in *) c->sockaddr;
|
|
77
|
|
78 ctx->addr = sin->sin_addr.s_addr;
|
|
79 ctx->handler = ngx_mail_smtp_resolve_addr_handler;
|
|
80 ctx->data = s;
|
|
81 ctx->timeout = cscf->resolver_timeout;
|
|
82
|
|
83 if (ngx_resolve_addr(ctx) != NGX_OK) {
|
|
84 ngx_mail_close_connection(c);
|
|
85 }
|
|
86 }
|
|
87
|
|
88
|
|
89 static void
|
|
90 ngx_mail_smtp_resolve_addr_handler(ngx_resolver_ctx_t *ctx)
|
|
91 {
|
384
|
92 ngx_connection_t *c;
|
|
93 ngx_mail_session_t *s;
|
366
|
94
|
|
95 s = ctx->data;
|
|
96 c = s->connection;
|
|
97
|
|
98 if (ctx->state) {
|
|
99 ngx_log_error(NGX_LOG_ERR, c->log, 0,
|
|
100 "%V could not be resolved (%i: %s)",
|
|
101 &c->addr_text, ctx->state,
|
|
102 ngx_resolver_strerror(ctx->state));
|
|
103
|
|
104 if (ctx->state == NGX_RESOLVE_NXDOMAIN) {
|
|
105 s->host = smtp_unavailable;
|
|
106
|
|
107 } else {
|
|
108 s->host = smtp_tempunavail;
|
|
109 }
|
|
110
|
|
111 ngx_resolve_addr_done(ctx);
|
|
112
|
|
113 ngx_mail_smtp_greeting(s, s->connection);
|
|
114
|
|
115 return;
|
|
116 }
|
|
117
|
|
118 c->log->action = "in resolving client hostname";
|
|
119
|
|
120 s->host.data = ngx_pstrdup(c->pool, &ctx->name);
|
|
121 if (s->host.data == NULL) {
|
|
122 ngx_resolve_addr_done(ctx);
|
|
123 ngx_mail_close_connection(c);
|
|
124 return;
|
|
125 }
|
|
126
|
|
127 s->host.len = ctx->name.len;
|
|
128
|
|
129 ngx_resolve_addr_done(ctx);
|
|
130
|
|
131 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
|
|
132 "address resolved: %V", &s->host);
|
|
133
|
384
|
134 c->read->handler = ngx_mail_smtp_resolve_name;
|
|
135
|
|
136 ngx_post_event(c->read, &ngx_posted_events);
|
|
137 }
|
|
138
|
|
139
|
|
140 static void
|
|
141 ngx_mail_smtp_resolve_name(ngx_event_t *rev)
|
|
142 {
|
|
143 ngx_connection_t *c;
|
|
144 ngx_mail_session_t *s;
|
|
145 ngx_resolver_ctx_t *ctx;
|
|
146 ngx_mail_core_srv_conf_t *cscf;
|
|
147
|
|
148 c = rev->data;
|
|
149 s = c->data;
|
|
150
|
366
|
151 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
|
|
152
|
|
153 ctx = ngx_resolve_start(cscf->resolver, NULL);
|
|
154 if (ctx == NULL) {
|
|
155 ngx_mail_close_connection(c);
|
|
156 return;
|
|
157 }
|
|
158
|
|
159 ctx->name = s->host;
|
|
160 ctx->type = NGX_RESOLVE_A;
|
|
161 ctx->handler = ngx_mail_smtp_resolve_name_handler;
|
|
162 ctx->data = s;
|
|
163 ctx->timeout = cscf->resolver_timeout;
|
|
164
|
|
165 if (ngx_resolve_name(ctx) != NGX_OK) {
|
|
166 ngx_mail_close_connection(c);
|
|
167 }
|
|
168 }
|
|
169
|
|
170
|
|
171 static void
|
|
172 ngx_mail_smtp_resolve_name_handler(ngx_resolver_ctx_t *ctx)
|
|
173 {
|
|
174 in_addr_t addr;
|
|
175 ngx_uint_t i;
|
|
176 ngx_connection_t *c;
|
|
177 struct sockaddr_in *sin;
|
|
178 ngx_mail_session_t *s;
|
|
179
|
|
180 s = ctx->data;
|
|
181 c = s->connection;
|
|
182
|
|
183 if (ctx->state) {
|
|
184 ngx_log_error(NGX_LOG_ERR, c->log, 0,
|
|
185 "%V could not be resolved (%i: %s)",
|
|
186 &ctx->name, ctx->state,
|
|
187 ngx_resolver_strerror(ctx->state));
|
|
188
|
|
189 if (ctx->state == NGX_RESOLVE_NXDOMAIN) {
|
|
190 s->host = smtp_unavailable;
|
|
191
|
|
192 } else {
|
|
193 s->host = smtp_tempunavail;
|
|
194 }
|
|
195
|
|
196 } else {
|
|
197
|
|
198 /* AF_INET only */
|
|
199
|
|
200 sin = (struct sockaddr_in *) c->sockaddr;
|
|
201
|
|
202 for (i = 0; i < ctx->naddrs; i++) {
|
|
203
|
|
204 addr = ctx->addrs[i];
|
|
205
|
|
206 ngx_log_debug4(NGX_LOG_DEBUG_MAIL, c->log, 0,
|
|
207 "name was resolved to %ud.%ud.%ud.%ud",
|
|
208 (ntohl(addr) >> 24) & 0xff,
|
|
209 (ntohl(addr) >> 16) & 0xff,
|
|
210 (ntohl(addr) >> 8) & 0xff,
|
|
211 ntohl(addr) & 0xff);
|
|
212
|
|
213 if (addr == sin->sin_addr.s_addr) {
|
|
214 goto found;
|
|
215 }
|
|
216 }
|
|
217
|
|
218 s->host = smtp_unavailable;
|
|
219 }
|
|
220
|
|
221 found:
|
|
222
|
|
223 ngx_resolve_name_done(ctx);
|
|
224
|
|
225 ngx_mail_smtp_greeting(s, c);
|
|
226 }
|
|
227
|
|
228
|
|
229 static void
|
|
230 ngx_mail_smtp_greeting(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
231 {
|
336
|
232 ngx_msec_t timeout;
|
|
233 ngx_mail_core_srv_conf_t *cscf;
|
|
234 ngx_mail_smtp_srv_conf_t *sscf;
|
|
235
|
366
|
236 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
|
|
237 "smtp greeting for \"%V\"", &s->host);
|
|
238
|
336
|
239 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
|
|
240 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
|
|
241
|
|
242 timeout = sscf->greeting_delay ? sscf->greeting_delay : cscf->timeout;
|
|
243 ngx_add_timer(c->read, timeout);
|
|
244
|
|
245 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
246 ngx_mail_close_connection(c);
|
|
247 }
|
|
248
|
|
249 if (sscf->greeting_delay) {
|
|
250 c->read->handler = ngx_mail_smtp_invalid_pipelining;
|
|
251 return;
|
|
252 }
|
|
253
|
|
254 c->read->handler = ngx_mail_smtp_init_protocol;
|
|
255
|
|
256 s->out = sscf->greeting;
|
|
257
|
|
258 ngx_mail_send(c->write);
|
|
259 }
|
|
260
|
|
261
|
|
262 static void
|
|
263 ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev)
|
|
264 {
|
|
265 ngx_connection_t *c;
|
|
266 ngx_mail_session_t *s;
|
|
267 ngx_mail_core_srv_conf_t *cscf;
|
|
268 ngx_mail_smtp_srv_conf_t *sscf;
|
|
269
|
|
270 c = rev->data;
|
|
271 s = c->data;
|
|
272
|
|
273 c->log->action = "in delay pipelining state";
|
|
274
|
|
275 if (rev->timedout) {
|
|
276
|
|
277 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "delay greeting");
|
|
278
|
|
279 rev->timedout = 0;
|
|
280
|
|
281 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
|
|
282
|
|
283 c->read->handler = ngx_mail_smtp_init_protocol;
|
|
284
|
|
285 ngx_add_timer(c->read, cscf->timeout);
|
|
286
|
|
287 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
288 ngx_mail_close_connection(c);
|
|
289 return;
|
|
290 }
|
|
291
|
|
292 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
|
|
293
|
|
294 s->out = sscf->greeting;
|
|
295
|
|
296 } else {
|
|
297
|
|
298 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "invalid pipelining");
|
|
299
|
|
300 if (s->buffer == NULL) {
|
|
301 if (ngx_mail_smtp_create_buffer(s, c) != NGX_OK) {
|
|
302 return;
|
|
303 }
|
|
304 }
|
|
305
|
|
306 if (ngx_mail_smtp_discard_command(s, c,
|
|
307 "client was rejected before greeting: \"%V\"")
|
|
308 != NGX_OK)
|
|
309 {
|
|
310 return;
|
|
311 }
|
|
312
|
|
313 s->out.len = sizeof(smtp_invalid_pipelining) - 1;
|
|
314 s->out.data = smtp_invalid_pipelining;
|
|
315 }
|
|
316
|
|
317 ngx_mail_send(c->write);
|
|
318 }
|
|
319
|
|
320
|
|
321 void
|
|
322 ngx_mail_smtp_init_protocol(ngx_event_t *rev)
|
|
323 {
|
|
324 ngx_connection_t *c;
|
|
325 ngx_mail_session_t *s;
|
|
326
|
|
327 c = rev->data;
|
|
328
|
|
329 c->log->action = "in auth state";
|
|
330
|
|
331 if (rev->timedout) {
|
|
332 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
333 c->timedout = 1;
|
|
334 ngx_mail_close_connection(c);
|
|
335 return;
|
|
336 }
|
|
337
|
|
338 s = c->data;
|
|
339
|
|
340 if (s->buffer == NULL) {
|
|
341 if (ngx_mail_smtp_create_buffer(s, c) != NGX_OK) {
|
|
342 return;
|
|
343 }
|
|
344 }
|
|
345
|
|
346 s->mail_state = ngx_smtp_start;
|
|
347 c->read->handler = ngx_mail_smtp_auth_state;
|
|
348
|
|
349 ngx_mail_smtp_auth_state(rev);
|
|
350 }
|
|
351
|
|
352
|
|
353 static ngx_int_t
|
|
354 ngx_mail_smtp_create_buffer(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
355 {
|
|
356 ngx_mail_smtp_srv_conf_t *sscf;
|
|
357
|
|
358 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t)) == NGX_ERROR) {
|
|
359 ngx_mail_session_internal_server_error(s);
|
|
360 return NGX_ERROR;
|
|
361 }
|
|
362
|
|
363 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
|
|
364
|
|
365 s->buffer = ngx_create_temp_buf(c->pool, sscf->client_buffer_size);
|
|
366 if (s->buffer == NULL) {
|
|
367 ngx_mail_session_internal_server_error(s);
|
|
368 return NGX_ERROR;
|
|
369 }
|
|
370
|
|
371 return NGX_OK;
|
|
372 }
|
|
373
|
|
374
|
|
375 void
|
|
376 ngx_mail_smtp_auth_state(ngx_event_t *rev)
|
|
377 {
|
|
378 ngx_int_t rc;
|
|
379 ngx_connection_t *c;
|
|
380 ngx_mail_session_t *s;
|
|
381
|
|
382 c = rev->data;
|
|
383 s = c->data;
|
|
384
|
|
385 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "smtp auth state");
|
|
386
|
|
387 if (rev->timedout) {
|
|
388 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
|
|
389 c->timedout = 1;
|
|
390 ngx_mail_close_connection(c);
|
|
391 return;
|
|
392 }
|
|
393
|
|
394 if (s->out.len) {
|
|
395 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "smtp send handler busy");
|
|
396 s->blocked = 1;
|
|
397 return;
|
|
398 }
|
|
399
|
|
400 s->blocked = 0;
|
|
401
|
|
402 rc = ngx_mail_read_command(s, c);
|
|
403
|
|
404 if (rc == NGX_AGAIN || rc == NGX_ERROR) {
|
|
405 return;
|
|
406 }
|
|
407
|
|
408 s->out.len = sizeof(smtp_ok) - 1;
|
|
409 s->out.data = smtp_ok;
|
|
410
|
|
411 if (rc == NGX_OK) {
|
|
412 switch (s->mail_state) {
|
|
413
|
|
414 case ngx_smtp_start:
|
|
415
|
|
416 switch (s->command) {
|
|
417
|
|
418 case NGX_SMTP_HELO:
|
|
419 case NGX_SMTP_EHLO:
|
|
420 rc = ngx_mail_smtp_helo(s, c);
|
|
421 break;
|
|
422
|
|
423 case NGX_SMTP_AUTH:
|
|
424 rc = ngx_mail_smtp_auth(s, c);
|
|
425 break;
|
|
426
|
|
427 case NGX_SMTP_QUIT:
|
|
428 s->quit = 1;
|
|
429 s->out.len = sizeof(smtp_bye) - 1;
|
|
430 s->out.data = smtp_bye;
|
|
431 break;
|
|
432
|
|
433 case NGX_SMTP_MAIL:
|
|
434 rc = ngx_mail_smtp_mail(s, c);
|
|
435 break;
|
|
436
|
|
437 case NGX_SMTP_NOOP:
|
|
438 case NGX_SMTP_RSET:
|
|
439 break;
|
|
440
|
|
441 case NGX_SMTP_STARTTLS:
|
|
442 rc = ngx_mail_smtp_starttls(s, c);
|
358
|
443 s->out.len = sizeof(smtp_starttls) - 1;
|
|
444 s->out.data = smtp_starttls;
|
336
|
445 break;
|
|
446
|
|
447 default:
|
|
448 rc = NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
449 break;
|
|
450 }
|
|
451
|
|
452 break;
|
|
453
|
|
454 case ngx_smtp_auth_login_username:
|
|
455 rc = ngx_mail_auth_login_username(s, c);
|
|
456
|
|
457 s->out.len = sizeof(smtp_password) - 1;
|
|
458 s->out.data = smtp_password;
|
|
459 s->mail_state = ngx_smtp_auth_login_password;
|
|
460 break;
|
|
461
|
|
462 case ngx_smtp_auth_login_password:
|
|
463 rc = ngx_mail_auth_login_password(s, c);
|
|
464 break;
|
|
465
|
|
466 case ngx_smtp_auth_plain:
|
|
467 rc = ngx_mail_auth_plain(s, c, 0);
|
|
468 break;
|
|
469
|
|
470 case ngx_smtp_auth_cram_md5:
|
|
471 rc = ngx_mail_auth_cram_md5(s, c);
|
|
472 break;
|
|
473 }
|
|
474 }
|
|
475
|
|
476 switch (rc) {
|
|
477
|
|
478 case NGX_DONE:
|
|
479 ngx_mail_auth(s, c);
|
|
480 return;
|
|
481
|
|
482 case NGX_ERROR:
|
|
483 ngx_mail_session_internal_server_error(s);
|
|
484 return;
|
|
485
|
|
486 case NGX_MAIL_PARSE_INVALID_COMMAND:
|
|
487 s->mail_state = ngx_smtp_start;
|
|
488 s->state = 0;
|
|
489
|
|
490 s->out.len = sizeof(smtp_invalid_command) - 1;
|
|
491 s->out.data = smtp_invalid_command;
|
|
492
|
|
493 /* fall through */
|
|
494
|
|
495 case NGX_OK:
|
|
496 s->args.nelts = 0;
|
|
497 s->buffer->pos = s->buffer->start;
|
|
498 s->buffer->last = s->buffer->start;
|
|
499
|
|
500 if (s->state) {
|
|
501 s->arg_start = s->buffer->start;
|
|
502 }
|
|
503
|
|
504 ngx_mail_send(c->write);
|
|
505 }
|
|
506 }
|
|
507
|
|
508
|
|
509 static ngx_int_t
|
|
510 ngx_mail_smtp_helo(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
511 {
|
|
512 ngx_str_t *arg;
|
|
513 ngx_mail_smtp_srv_conf_t *sscf;
|
|
514
|
|
515 if (s->args.nelts != 1) {
|
|
516 s->out.len = sizeof(smtp_invalid_argument) - 1;
|
|
517 s->out.data = smtp_invalid_argument;
|
|
518 s->state = 0;
|
|
519 return NGX_OK;
|
|
520 }
|
|
521
|
|
522 arg = s->args.elts;
|
|
523
|
|
524 s->smtp_helo.len = arg[0].len;
|
|
525
|
|
526 s->smtp_helo.data = ngx_palloc(c->pool, arg[0].len);
|
|
527 if (s->smtp_helo.data == NULL) {
|
|
528 return NGX_ERROR;
|
|
529 }
|
|
530
|
|
531 ngx_memcpy(s->smtp_helo.data, arg[0].data, arg[0].len);
|
|
532
|
|
533 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
|
|
534
|
|
535 if (s->command == NGX_SMTP_HELO) {
|
|
536 s->out = sscf->server_name;
|
|
537
|
|
538 } else {
|
|
539 s->esmtp = 1;
|
|
540
|
|
541 #if (NGX_MAIL_SSL)
|
|
542
|
|
543 if (c->ssl == NULL) {
|
356
|
544 ngx_mail_ssl_conf_t *sslcf;
|
|
545
|
336
|
546 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
|
|
547
|
|
548 if (sslcf->starttls == NGX_MAIL_STARTTLS_ON) {
|
|
549 s->out = sscf->starttls_capability;
|
|
550 return NGX_OK;
|
|
551 }
|
|
552
|
|
553 if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) {
|
|
554 s->out = sscf->starttls_only_capability;
|
|
555 return NGX_OK;
|
|
556 }
|
|
557 }
|
|
558 #endif
|
|
559
|
|
560 s->out = sscf->capability;
|
|
561 }
|
|
562
|
|
563 return NGX_OK;
|
|
564 }
|
|
565
|
|
566
|
|
567 static ngx_int_t
|
|
568 ngx_mail_smtp_auth(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
569 {
|
|
570 ngx_int_t rc;
|
|
571 ngx_mail_core_srv_conf_t *cscf;
|
|
572 ngx_mail_smtp_srv_conf_t *sscf;
|
|
573
|
|
574 #if (NGX_MAIL_SSL)
|
|
575 if (ngx_mail_starttls_only(s, c)) {
|
|
576 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
577 }
|
|
578 #endif
|
|
579
|
|
580 if (s->args.nelts == 0) {
|
|
581 s->out.len = sizeof(smtp_invalid_argument) - 1;
|
|
582 s->out.data = smtp_invalid_argument;
|
|
583 s->state = 0;
|
|
584 return NGX_OK;
|
|
585 }
|
|
586
|
|
587 rc = ngx_mail_auth_parse(s, c);
|
|
588
|
|
589 switch (rc) {
|
|
590
|
|
591 case NGX_MAIL_AUTH_LOGIN:
|
|
592
|
|
593 s->out.len = sizeof(smtp_username) - 1;
|
|
594 s->out.data = smtp_username;
|
|
595 s->mail_state = ngx_smtp_auth_login_username;
|
|
596
|
|
597 return NGX_OK;
|
|
598
|
|
599 case NGX_MAIL_AUTH_PLAIN:
|
|
600
|
|
601 s->out.len = sizeof(smtp_next) - 1;
|
|
602 s->out.data = smtp_next;
|
|
603 s->mail_state = ngx_smtp_auth_plain;
|
|
604
|
|
605 return NGX_OK;
|
|
606
|
|
607 case NGX_MAIL_AUTH_CRAM_MD5:
|
|
608
|
|
609 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
|
|
610
|
|
611 if (!(sscf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED)) {
|
|
612 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
613 }
|
|
614
|
|
615 if (s->salt.data == NULL) {
|
|
616 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
|
|
617
|
|
618 if (ngx_mail_salt(s, c, cscf) != NGX_OK) {
|
|
619 return NGX_ERROR;
|
|
620 }
|
|
621 }
|
|
622
|
|
623 if (ngx_mail_auth_cram_md5_salt(s, c, "334 ", 4) == NGX_OK) {
|
|
624 s->mail_state = ngx_smtp_auth_cram_md5;
|
|
625 return NGX_OK;
|
|
626 }
|
|
627
|
|
628 return NGX_ERROR;
|
|
629 }
|
|
630
|
|
631 return rc;
|
|
632 }
|
|
633
|
|
634
|
|
635 static ngx_int_t
|
|
636 ngx_mail_smtp_mail(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
637 {
|
|
638 ngx_mail_smtp_log_rejected_command(s, c, "client was rejected: \"%V\"");
|
|
639
|
|
640 s->out.len = sizeof(smtp_auth_required) - 1;
|
|
641 s->out.data = smtp_auth_required;
|
|
642
|
|
643 return NGX_OK;
|
|
644 }
|
|
645
|
|
646
|
|
647 static ngx_int_t
|
|
648 ngx_mail_smtp_starttls(ngx_mail_session_t *s, ngx_connection_t *c)
|
|
649 {
|
|
650 #if (NGX_MAIL_SSL)
|
|
651 ngx_mail_ssl_conf_t *sslcf;
|
|
652
|
|
653 if (c->ssl == NULL) {
|
|
654 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
|
|
655 if (sslcf->starttls) {
|
|
656
|
|
657 /*
|
|
658 * RFC3207 requires us to discard any knowledge
|
|
659 * obtained from client before STARTTLS.
|
|
660 */
|
|
661
|
|
662 s->smtp_helo.len = 0;
|
|
663 s->smtp_helo.data = NULL;
|
|
664
|
|
665 c->read->handler = ngx_mail_starttls_handler;
|
|
666 return NGX_OK;
|
|
667 }
|
|
668 }
|
|
669
|
|
670 #endif
|
|
671
|
|
672 return NGX_MAIL_PARSE_INVALID_COMMAND;
|
|
673 }
|
|
674
|
|
675
|
|
676 static ngx_int_t
|
|
677 ngx_mail_smtp_discard_command(ngx_mail_session_t *s, ngx_connection_t *c,
|
|
678 char *err)
|
|
679 {
|
|
680 ssize_t n;
|
|
681
|
|
682 n = c->recv(c, s->buffer->last, s->buffer->end - s->buffer->last);
|
|
683
|
|
684 if (n == NGX_ERROR || n == 0) {
|
|
685 ngx_mail_close_connection(c);
|
|
686 return NGX_ERROR;
|
|
687 }
|
|
688
|
|
689 if (n > 0) {
|
|
690 s->buffer->last += n;
|
|
691 }
|
|
692
|
|
693 if (n == NGX_AGAIN) {
|
|
694 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
695 ngx_mail_session_internal_server_error(s);
|
|
696 return NGX_ERROR;
|
|
697 }
|
|
698
|
|
699 return NGX_AGAIN;
|
|
700 }
|
|
701
|
|
702 ngx_mail_smtp_log_rejected_command(s, c, err);
|
|
703
|
|
704 s->buffer->pos = s->buffer->start;
|
|
705 s->buffer->last = s->buffer->start;
|
|
706
|
|
707 return NGX_OK;
|
|
708 }
|
|
709
|
|
710
|
|
711 static void
|
|
712 ngx_mail_smtp_log_rejected_command(ngx_mail_session_t *s, ngx_connection_t *c,
|
|
713 char *err)
|
|
714 {
|
|
715 u_char ch;
|
|
716 ngx_str_t cmd;
|
|
717 ngx_uint_t i;
|
|
718
|
|
719 if (c->log->log_level < NGX_LOG_INFO) {
|
|
720 return;
|
|
721 }
|
|
722
|
|
723 cmd.len = s->buffer->last - s->buffer->start;
|
|
724 cmd.data = s->buffer->start;
|
|
725
|
|
726 for (i = 0; i < cmd.len; i++) {
|
|
727 ch = cmd.data[i];
|
|
728
|
|
729 if (ch != CR && ch != LF) {
|
|
730 continue;
|
|
731 }
|
|
732
|
|
733 cmd.data[i] = '_';
|
|
734 }
|
|
735
|
|
736 cmd.len = i;
|
|
737
|
|
738 ngx_log_error(NGX_LOG_INFO, c->log, 0, err, &cmd);
|
|
739 }
|