290
|
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_event_connect.h>
|
|
11 #include <ngx_mail.h>
|
|
12
|
|
13
|
|
14 typedef struct {
|
|
15 ngx_flag_t enable;
|
|
16 ngx_flag_t pass_error_message;
|
|
17 ngx_flag_t xclient;
|
|
18 size_t buffer_size;
|
|
19 ngx_msec_t timeout;
|
|
20 } ngx_mail_proxy_conf_t;
|
|
21
|
|
22
|
|
23 static void ngx_mail_proxy_block_read(ngx_event_t *rev);
|
|
24 static void ngx_mail_proxy_pop3_handler(ngx_event_t *rev);
|
|
25 static void ngx_mail_proxy_imap_handler(ngx_event_t *rev);
|
|
26 static void ngx_mail_proxy_smtp_handler(ngx_event_t *rev);
|
|
27 static void ngx_mail_proxy_dummy_handler(ngx_event_t *ev);
|
|
28 static ngx_int_t ngx_mail_proxy_read_response(ngx_mail_session_t *s,
|
|
29 ngx_uint_t state);
|
|
30 static void ngx_mail_proxy_handler(ngx_event_t *ev);
|
|
31 static void ngx_mail_proxy_upstream_error(ngx_mail_session_t *s);
|
|
32 static void ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s);
|
|
33 static void ngx_mail_proxy_close_session(ngx_mail_session_t *s);
|
|
34 static void *ngx_mail_proxy_create_conf(ngx_conf_t *cf);
|
|
35 static char *ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent,
|
|
36 void *child);
|
|
37
|
|
38
|
|
39 static ngx_command_t ngx_mail_proxy_commands[] = {
|
|
40
|
|
41 { ngx_string("proxy"),
|
|
42 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
|
|
43 ngx_conf_set_flag_slot,
|
|
44 NGX_MAIL_SRV_CONF_OFFSET,
|
|
45 offsetof(ngx_mail_proxy_conf_t, enable),
|
|
46 NULL },
|
|
47
|
|
48 { ngx_string("proxy_buffer"),
|
|
49 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
|
50 ngx_conf_set_size_slot,
|
|
51 NGX_MAIL_SRV_CONF_OFFSET,
|
|
52 offsetof(ngx_mail_proxy_conf_t, buffer_size),
|
|
53 NULL },
|
|
54
|
|
55 { ngx_string("proxy_timeout"),
|
|
56 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
|
57 ngx_conf_set_msec_slot,
|
|
58 NGX_MAIL_SRV_CONF_OFFSET,
|
|
59 offsetof(ngx_mail_proxy_conf_t, timeout),
|
|
60 NULL },
|
|
61
|
|
62 { ngx_string("proxy_pass_error_message"),
|
|
63 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
|
64 ngx_conf_set_flag_slot,
|
|
65 NGX_MAIL_SRV_CONF_OFFSET,
|
|
66 offsetof(ngx_mail_proxy_conf_t, pass_error_message),
|
|
67 NULL },
|
|
68
|
|
69 { ngx_string("xclient"),
|
|
70 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
|
|
71 ngx_conf_set_flag_slot,
|
|
72 NGX_MAIL_SRV_CONF_OFFSET,
|
|
73 offsetof(ngx_mail_proxy_conf_t, xclient),
|
|
74 NULL },
|
|
75
|
|
76 ngx_null_command
|
|
77 };
|
|
78
|
|
79
|
|
80 static ngx_mail_module_t ngx_mail_proxy_module_ctx = {
|
336
|
81 NULL, /* protocol */
|
|
82
|
290
|
83 NULL, /* create main configuration */
|
|
84 NULL, /* init main configuration */
|
|
85
|
|
86 ngx_mail_proxy_create_conf, /* create server configuration */
|
|
87 ngx_mail_proxy_merge_conf /* merge server configuration */
|
|
88 };
|
|
89
|
|
90
|
|
91 ngx_module_t ngx_mail_proxy_module = {
|
|
92 NGX_MODULE_V1,
|
|
93 &ngx_mail_proxy_module_ctx, /* module context */
|
|
94 ngx_mail_proxy_commands, /* module directives */
|
|
95 NGX_MAIL_MODULE, /* module type */
|
|
96 NULL, /* init master */
|
|
97 NULL, /* init module */
|
|
98 NULL, /* init process */
|
|
99 NULL, /* init thread */
|
|
100 NULL, /* exit thread */
|
|
101 NULL, /* exit process */
|
|
102 NULL, /* exit master */
|
|
103 NGX_MODULE_V1_PADDING
|
|
104 };
|
|
105
|
|
106
|
|
107 static u_char smtp_ok[] = "235 2.0.0 OK" CRLF;
|
|
108
|
|
109
|
|
110 void
|
|
111 ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_peer_addr_t *peer)
|
|
112 {
|
|
113 int keepalive;
|
|
114 ngx_int_t rc;
|
|
115 ngx_mail_proxy_ctx_t *p;
|
|
116 ngx_mail_proxy_conf_t *pcf;
|
|
117 ngx_mail_core_srv_conf_t *cscf;
|
|
118
|
|
119 s->connection->log->action = "connecting to upstream";
|
|
120
|
|
121 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
|
|
122
|
|
123 if (cscf->so_keepalive) {
|
|
124 keepalive = 1;
|
|
125
|
|
126 if (setsockopt(s->connection->fd, SOL_SOCKET, SO_KEEPALIVE,
|
|
127 (const void *) &keepalive, sizeof(int))
|
|
128 == -1)
|
|
129 {
|
|
130 ngx_log_error(NGX_LOG_ALERT, s->connection->log, ngx_socket_errno,
|
|
131 "setsockopt(SO_KEEPALIVE) failed");
|
|
132 }
|
|
133 }
|
|
134
|
|
135 p = ngx_pcalloc(s->connection->pool, sizeof(ngx_mail_proxy_ctx_t));
|
|
136 if (p == NULL) {
|
|
137 ngx_mail_session_internal_server_error(s);
|
|
138 return;
|
|
139 }
|
|
140
|
|
141 s->proxy = p;
|
|
142
|
|
143 p->upstream.sockaddr = peer->sockaddr;
|
|
144 p->upstream.socklen = peer->socklen;
|
|
145 p->upstream.name = &peer->name;
|
|
146 p->upstream.get = ngx_event_get_peer;
|
|
147 p->upstream.log = s->connection->log;
|
|
148 p->upstream.log_error = NGX_ERROR_ERR;
|
|
149
|
|
150 rc = ngx_event_connect_peer(&p->upstream);
|
|
151
|
|
152 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) {
|
|
153 ngx_mail_proxy_internal_server_error(s);
|
|
154 return;
|
|
155 }
|
|
156
|
|
157 ngx_add_timer(p->upstream.connection->read, cscf->timeout);
|
|
158
|
|
159 p->upstream.connection->data = s;
|
|
160 p->upstream.connection->pool = s->connection->pool;
|
|
161
|
|
162 s->connection->read->handler = ngx_mail_proxy_block_read;
|
|
163 p->upstream.connection->write->handler = ngx_mail_proxy_dummy_handler;
|
|
164
|
|
165 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
|
166
|
|
167 s->proxy->buffer = ngx_create_temp_buf(s->connection->pool,
|
|
168 pcf->buffer_size);
|
|
169 if (s->proxy->buffer == NULL) {
|
|
170 ngx_mail_proxy_internal_server_error(s);
|
|
171 return;
|
|
172 }
|
|
173
|
|
174 switch (s->protocol) {
|
|
175
|
|
176 case NGX_MAIL_POP3_PROTOCOL:
|
|
177 p->upstream.connection->read->handler = ngx_mail_proxy_pop3_handler;
|
|
178 s->mail_state = ngx_pop3_start;
|
|
179 break;
|
|
180
|
|
181 case NGX_MAIL_IMAP_PROTOCOL:
|
|
182 p->upstream.connection->read->handler = ngx_mail_proxy_imap_handler;
|
|
183 s->mail_state = ngx_imap_start;
|
|
184 break;
|
|
185
|
|
186 default: /* NGX_MAIL_SMTP_PROTOCOL */
|
|
187 p->upstream.connection->read->handler = ngx_mail_proxy_smtp_handler;
|
|
188 s->mail_state = ngx_smtp_start;
|
|
189 break;
|
|
190 }
|
|
191 }
|
|
192
|
|
193
|
|
194 static void
|
|
195 ngx_mail_proxy_block_read(ngx_event_t *rev)
|
|
196 {
|
|
197 ngx_connection_t *c;
|
|
198 ngx_mail_session_t *s;
|
|
199
|
|
200 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy block read");
|
|
201
|
|
202 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
203 c = rev->data;
|
|
204 s = c->data;
|
|
205
|
|
206 ngx_mail_proxy_close_session(s);
|
|
207 }
|
|
208 }
|
|
209
|
|
210
|
|
211 static void
|
|
212 ngx_mail_proxy_pop3_handler(ngx_event_t *rev)
|
|
213 {
|
|
214 u_char *p;
|
|
215 ngx_int_t rc;
|
|
216 ngx_str_t line;
|
|
217 ngx_connection_t *c;
|
|
218 ngx_mail_session_t *s;
|
|
219 ngx_mail_proxy_conf_t *pcf;
|
|
220
|
|
221 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
222 "mail proxy pop3 auth handler");
|
|
223
|
|
224 c = rev->data;
|
|
225 s = c->data;
|
|
226
|
|
227 if (rev->timedout) {
|
|
228 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
229 "upstream timed out");
|
|
230 c->timedout = 1;
|
|
231 ngx_mail_proxy_internal_server_error(s);
|
|
232 return;
|
|
233 }
|
|
234
|
|
235 rc = ngx_mail_proxy_read_response(s, 0);
|
|
236
|
|
237 if (rc == NGX_AGAIN) {
|
|
238 return;
|
|
239 }
|
|
240
|
|
241 if (rc == NGX_ERROR) {
|
|
242 ngx_mail_proxy_upstream_error(s);
|
|
243 return;
|
|
244 }
|
|
245
|
|
246 switch (s->mail_state) {
|
|
247
|
|
248 case ngx_pop3_start:
|
|
249 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user");
|
|
250
|
|
251 s->connection->log->action = "sending user name to upstream";
|
|
252
|
|
253 line.len = sizeof("USER ") - 1 + s->login.len + 2;
|
|
254 line.data = ngx_palloc(c->pool, line.len);
|
|
255 if (line.data == NULL) {
|
|
256 ngx_mail_proxy_internal_server_error(s);
|
|
257 return;
|
|
258 }
|
|
259
|
|
260 p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1);
|
|
261 p = ngx_cpymem(p, s->login.data, s->login.len);
|
|
262 *p++ = CR; *p = LF;
|
|
263
|
|
264 s->mail_state = ngx_pop3_user;
|
|
265 break;
|
|
266
|
|
267 case ngx_pop3_user:
|
|
268 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send pass");
|
|
269
|
|
270 s->connection->log->action = "sending password to upstream";
|
|
271
|
|
272 line.len = sizeof("PASS ") - 1 + s->passwd.len + 2;
|
|
273 line.data = ngx_palloc(c->pool, line.len);
|
|
274 if (line.data == NULL) {
|
|
275 ngx_mail_proxy_internal_server_error(s);
|
|
276 return;
|
|
277 }
|
|
278
|
|
279 p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1);
|
|
280 p = ngx_cpymem(p, s->passwd.data, s->passwd.len);
|
|
281 *p++ = CR; *p = LF;
|
|
282
|
|
283 s->mail_state = ngx_pop3_passwd;
|
|
284 break;
|
|
285
|
|
286 case ngx_pop3_passwd:
|
|
287 s->connection->read->handler = ngx_mail_proxy_handler;
|
|
288 s->connection->write->handler = ngx_mail_proxy_handler;
|
|
289 rev->handler = ngx_mail_proxy_handler;
|
|
290 c->write->handler = ngx_mail_proxy_handler;
|
|
291
|
|
292 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
|
293 ngx_add_timer(s->connection->read, pcf->timeout);
|
|
294 ngx_del_timer(c->read);
|
|
295
|
|
296 c->log->action = NULL;
|
|
297 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
|
|
298
|
|
299 ngx_mail_proxy_handler(s->connection->write);
|
|
300
|
|
301 return;
|
|
302
|
|
303 default:
|
|
304 #if (NGX_SUPPRESS_WARN)
|
|
305 line.len = 0;
|
|
306 line.data = NULL;
|
|
307 #endif
|
|
308 break;
|
|
309 }
|
|
310
|
|
311 if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
|
|
312 /*
|
|
313 * we treat the incomplete sending as NGX_ERROR
|
|
314 * because it is very strange here
|
|
315 */
|
|
316 ngx_mail_proxy_internal_server_error(s);
|
|
317 return;
|
|
318 }
|
|
319
|
|
320 s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
321 s->proxy->buffer->last = s->proxy->buffer->start;
|
|
322 }
|
|
323
|
|
324
|
|
325 static void
|
|
326 ngx_mail_proxy_imap_handler(ngx_event_t *rev)
|
|
327 {
|
|
328 u_char *p;
|
|
329 ngx_int_t rc;
|
|
330 ngx_str_t line;
|
|
331 ngx_connection_t *c;
|
|
332 ngx_mail_session_t *s;
|
|
333 ngx_mail_proxy_conf_t *pcf;
|
|
334
|
|
335 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
336 "mail proxy imap auth handler");
|
|
337
|
|
338 c = rev->data;
|
|
339 s = c->data;
|
|
340
|
|
341 if (rev->timedout) {
|
|
342 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
343 "upstream timed out");
|
|
344 c->timedout = 1;
|
|
345 ngx_mail_proxy_internal_server_error(s);
|
|
346 return;
|
|
347 }
|
|
348
|
|
349 rc = ngx_mail_proxy_read_response(s, s->mail_state);
|
|
350
|
|
351 if (rc == NGX_AGAIN) {
|
|
352 return;
|
|
353 }
|
|
354
|
|
355 if (rc == NGX_ERROR) {
|
|
356 ngx_mail_proxy_upstream_error(s);
|
|
357 return;
|
|
358 }
|
|
359
|
|
360 switch (s->mail_state) {
|
|
361
|
|
362 case ngx_imap_start:
|
|
363 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
364 "mail proxy send login");
|
|
365
|
|
366 s->connection->log->action = "sending LOGIN command to upstream";
|
|
367
|
|
368 line.len = s->tag.len + sizeof("LOGIN ") - 1
|
|
369 + 1 + NGX_SIZE_T_LEN + 1 + 2;
|
|
370 line.data = ngx_palloc(c->pool, line.len);
|
|
371 if (line.data == NULL) {
|
|
372 ngx_mail_proxy_internal_server_error(s);
|
|
373 return;
|
|
374 }
|
|
375
|
|
376 line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF,
|
|
377 &s->tag, s->login.len)
|
|
378 - line.data;
|
|
379
|
|
380 s->mail_state = ngx_imap_login;
|
|
381 break;
|
|
382
|
|
383 case ngx_imap_login:
|
|
384 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user");
|
|
385
|
|
386 s->connection->log->action = "sending user name to upstream";
|
|
387
|
|
388 line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2;
|
|
389 line.data = ngx_palloc(c->pool, line.len);
|
|
390 if (line.data == NULL) {
|
|
391 ngx_mail_proxy_internal_server_error(s);
|
|
392 return;
|
|
393 }
|
|
394
|
|
395 line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF,
|
|
396 &s->login, s->passwd.len)
|
|
397 - line.data;
|
|
398
|
|
399 s->mail_state = ngx_imap_user;
|
|
400 break;
|
|
401
|
|
402 case ngx_imap_user:
|
|
403 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
404 "mail proxy send passwd");
|
|
405
|
|
406 s->connection->log->action = "sending password to upstream";
|
|
407
|
|
408 line.len = s->passwd.len + 2;
|
|
409 line.data = ngx_palloc(c->pool, line.len);
|
|
410 if (line.data == NULL) {
|
|
411 ngx_mail_proxy_internal_server_error(s);
|
|
412 return;
|
|
413 }
|
|
414
|
|
415 p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len);
|
|
416 *p++ = CR; *p = LF;
|
|
417
|
|
418 s->mail_state = ngx_imap_passwd;
|
|
419 break;
|
|
420
|
|
421 case ngx_imap_passwd:
|
|
422 s->connection->read->handler = ngx_mail_proxy_handler;
|
|
423 s->connection->write->handler = ngx_mail_proxy_handler;
|
|
424 rev->handler = ngx_mail_proxy_handler;
|
|
425 c->write->handler = ngx_mail_proxy_handler;
|
|
426
|
|
427 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
|
428 ngx_add_timer(s->connection->read, pcf->timeout);
|
|
429 ngx_del_timer(c->read);
|
|
430
|
|
431 c->log->action = NULL;
|
|
432 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
|
|
433
|
|
434 ngx_mail_proxy_handler(s->connection->write);
|
|
435
|
|
436 return;
|
|
437
|
|
438 default:
|
|
439 #if (NGX_SUPPRESS_WARN)
|
|
440 line.len = 0;
|
|
441 line.data = NULL;
|
|
442 #endif
|
|
443 break;
|
|
444 }
|
|
445
|
|
446 if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
|
|
447 /*
|
|
448 * we treat the incomplete sending as NGX_ERROR
|
|
449 * because it is very strange here
|
|
450 */
|
|
451 ngx_mail_proxy_internal_server_error(s);
|
|
452 return;
|
|
453 }
|
|
454
|
|
455 s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
456 s->proxy->buffer->last = s->proxy->buffer->start;
|
|
457 }
|
|
458
|
|
459
|
|
460 static void
|
|
461 ngx_mail_proxy_smtp_handler(ngx_event_t *rev)
|
|
462 {
|
|
463 u_char *p;
|
|
464 ngx_int_t rc;
|
|
465 ngx_str_t line;
|
|
466 ngx_connection_t *c;
|
|
467 ngx_mail_session_t *s;
|
|
468 ngx_mail_proxy_conf_t *pcf;
|
|
469 ngx_mail_core_srv_conf_t *cscf;
|
|
470
|
|
471 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
472 "mail proxy smtp auth handler");
|
|
473
|
|
474 c = rev->data;
|
|
475 s = c->data;
|
|
476
|
|
477 if (rev->timedout) {
|
|
478 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
479 "upstream timed out");
|
|
480 c->timedout = 1;
|
|
481 ngx_mail_proxy_internal_server_error(s);
|
|
482 return;
|
|
483 }
|
|
484
|
|
485 rc = ngx_mail_proxy_read_response(s, s->mail_state);
|
|
486
|
|
487 if (rc == NGX_AGAIN) {
|
|
488 return;
|
|
489 }
|
|
490
|
|
491 if (rc == NGX_ERROR) {
|
|
492 ngx_mail_proxy_upstream_error(s);
|
|
493 return;
|
|
494 }
|
|
495
|
|
496 switch (s->mail_state) {
|
|
497
|
|
498 case ngx_smtp_start:
|
|
499 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send ehlo");
|
|
500
|
|
501 s->connection->log->action = "sending HELO/EHLO to upstream";
|
|
502
|
|
503 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
|
|
504
|
|
505 line.len = sizeof("HELO ") - 1 + cscf->server_name.len + 2;
|
|
506 line.data = ngx_palloc(c->pool, line.len);
|
|
507 if (line.data == NULL) {
|
|
508 ngx_mail_proxy_internal_server_error(s);
|
|
509 return;
|
|
510 }
|
|
511
|
|
512 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
|
513
|
|
514 p = ngx_cpymem(line.data,
|
|
515 ((s->esmtp || pcf->xclient) ? "EHLO " : "HELO "),
|
|
516 sizeof("HELO ") - 1);
|
|
517
|
|
518 p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
|
|
519 *p++ = CR; *p = LF;
|
|
520
|
|
521 s->mail_state = pcf->xclient ? ngx_smtp_helo: ngx_smtp_noxclient;
|
|
522
|
|
523 break;
|
|
524
|
|
525 case ngx_smtp_helo:
|
|
526 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
527 "mail proxy send xclient");
|
|
528
|
|
529 s->connection->log->action = "sending XCLIENT to upstream";
|
|
530
|
366
|
531 line.len = sizeof("XCLIENT PROTO=SMTP HELO= ADDR= LOGIN= NAME="
|
|
532 CRLF) - 1
|
290
|
533 + s->esmtp + s->smtp_helo.len
|
366
|
534 + s->connection->addr_text.len + s->login.len + s->host.len;
|
290
|
535
|
|
536 line.data = ngx_palloc(c->pool, line.len);
|
|
537 if (line.data == NULL) {
|
|
538 ngx_mail_proxy_internal_server_error(s);
|
|
539 return;
|
|
540 }
|
|
541
|
|
542 if (s->smtp_helo.len) {
|
|
543 line.len = ngx_sprintf(line.data,
|
|
544 "XCLIENT PROTO=%sSMTP HELO=%V ADDR=%V LOGIN=%V "
|
366
|
545 "NAME=%V" CRLF,
|
290
|
546 (s->esmtp ? "E" : ""), &s->smtp_helo,
|
366
|
547 &s->connection->addr_text, &s->login, &s->host)
|
290
|
548 - line.data;
|
|
549 } else {
|
|
550 line.len = ngx_sprintf(line.data,
|
366
|
551 "XCLIENT PROTO=SMTP ADDR=%V LOGIN=%V NAME=%V" CRLF,
|
|
552 &s->connection->addr_text, &s->login, &s->host)
|
290
|
553 - line.data;
|
|
554 }
|
|
555
|
|
556 s->mail_state = ngx_smtp_xclient;
|
|
557 break;
|
|
558
|
|
559 case ngx_smtp_noxclient:
|
|
560 case ngx_smtp_xclient:
|
|
561
|
|
562 ngx_memcpy(s->proxy->buffer->start, smtp_ok, sizeof(smtp_ok) - 1);
|
|
563
|
|
564 s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
565 s->proxy->buffer->last = s->proxy->buffer->start + sizeof(smtp_ok) - 1;
|
|
566
|
|
567 s->connection->read->handler = ngx_mail_proxy_handler;
|
|
568 s->connection->write->handler = ngx_mail_proxy_handler;
|
|
569 rev->handler = ngx_mail_proxy_handler;
|
|
570 c->write->handler = ngx_mail_proxy_handler;
|
|
571
|
|
572 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
|
573 ngx_add_timer(s->connection->read, pcf->timeout);
|
|
574 ngx_del_timer(c->read);
|
|
575
|
|
576 c->log->action = NULL;
|
|
577 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
|
|
578
|
|
579 ngx_mail_proxy_handler(s->connection->write);
|
|
580
|
|
581 return;
|
|
582
|
|
583 default:
|
|
584 #if (NGX_SUPPRESS_WARN)
|
|
585 line.len = 0;
|
|
586 line.data = NULL;
|
|
587 #endif
|
|
588 break;
|
|
589 }
|
|
590
|
|
591 if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
|
|
592 /*
|
|
593 * we treat the incomplete sending as NGX_ERROR
|
|
594 * because it is very strange here
|
|
595 */
|
|
596 ngx_mail_proxy_internal_server_error(s);
|
|
597 return;
|
|
598 }
|
|
599
|
|
600 s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
601 s->proxy->buffer->last = s->proxy->buffer->start;
|
|
602 }
|
|
603
|
|
604
|
|
605 static void
|
|
606 ngx_mail_proxy_dummy_handler(ngx_event_t *wev)
|
|
607 {
|
|
608 ngx_connection_t *c;
|
|
609 ngx_mail_session_t *s;
|
|
610
|
|
611 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy dummy handler");
|
|
612
|
|
613 if (ngx_handle_write_event(wev, 0) == NGX_ERROR) {
|
|
614 c = wev->data;
|
|
615 s = c->data;
|
|
616
|
|
617 ngx_mail_proxy_close_session(s);
|
|
618 }
|
|
619 }
|
|
620
|
|
621
|
|
622 static ngx_int_t
|
|
623 ngx_mail_proxy_read_response(ngx_mail_session_t *s, ngx_uint_t state)
|
|
624 {
|
|
625 u_char *p;
|
|
626 ssize_t n;
|
|
627 ngx_buf_t *b;
|
|
628 ngx_mail_proxy_conf_t *pcf;
|
|
629
|
|
630 s->connection->log->action = "reading response from upstream";
|
|
631
|
|
632 b = s->proxy->buffer;
|
|
633
|
|
634 n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection,
|
|
635 b->last, b->end - b->last);
|
|
636
|
|
637 if (n == NGX_ERROR || n == 0) {
|
|
638 return NGX_ERROR;
|
|
639 }
|
|
640
|
|
641 if (n == NGX_AGAIN) {
|
|
642 return NGX_AGAIN;
|
|
643 }
|
|
644
|
|
645 b->last += n;
|
|
646
|
|
647 if (b->last - b->pos < 5) {
|
|
648 return NGX_AGAIN;
|
|
649 }
|
|
650
|
|
651 if (*(b->last - 2) != CR || *(b->last - 1) != LF) {
|
|
652 if (b->last == b->end) {
|
|
653 *(b->last - 1) = '\0';
|
|
654 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
|
|
655 "upstream sent too long response line: \"%s\"",
|
|
656 b->pos);
|
|
657 return NGX_ERROR;
|
|
658 }
|
|
659
|
|
660 return NGX_AGAIN;
|
|
661 }
|
|
662
|
|
663 p = b->pos;
|
|
664
|
|
665 switch (s->protocol) {
|
|
666
|
|
667 case NGX_MAIL_POP3_PROTOCOL:
|
|
668 if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
|
|
669 return NGX_OK;
|
|
670 }
|
|
671 break;
|
|
672
|
|
673 case NGX_MAIL_IMAP_PROTOCOL:
|
|
674 switch (state) {
|
|
675
|
|
676 case ngx_imap_start:
|
|
677 if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
|
|
678 return NGX_OK;
|
|
679 }
|
|
680 break;
|
|
681
|
|
682 case ngx_imap_login:
|
|
683 case ngx_imap_user:
|
|
684 if (p[0] == '+') {
|
|
685 return NGX_OK;
|
|
686 }
|
|
687 break;
|
|
688
|
|
689 case ngx_imap_passwd:
|
|
690 if (ngx_strncmp(p, s->tag.data, s->tag.len) == 0) {
|
|
691 p += s->tag.len;
|
|
692 if (p[0] == 'O' && p[1] == 'K') {
|
|
693 return NGX_OK;
|
|
694 }
|
|
695 }
|
|
696 break;
|
|
697 }
|
|
698
|
|
699 break;
|
|
700
|
|
701 default: /* NGX_MAIL_SMTP_PROTOCOL */
|
|
702 switch (state) {
|
|
703
|
|
704 case ngx_smtp_helo:
|
|
705 case ngx_smtp_noxclient:
|
296
|
706 if (p[0] == '2' && p[1] == '5' && p[2] == '0') {
|
|
707 return NGX_OK;
|
|
708 }
|
|
709 break;
|
290
|
710
|
|
711 case ngx_smtp_start:
|
|
712 case ngx_smtp_xclient:
|
296
|
713 if (p[0] == '2' && p[1] == '2' && p[2] == '0') {
|
|
714 return NGX_OK;
|
|
715 }
|
|
716 break;
|
290
|
717 }
|
|
718
|
296
|
719 break;
|
290
|
720 }
|
|
721
|
|
722 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
|
723
|
|
724 if (pcf->pass_error_message == 0) {
|
|
725 *(b->last - 2) = '\0';
|
|
726 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
|
|
727 "upstream sent invalid response: \"%s\"", p);
|
|
728 return NGX_ERROR;
|
|
729 }
|
|
730
|
|
731 s->out.len = b->last - p - 2;
|
|
732 s->out.data = p;
|
|
733
|
|
734 ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
|
|
735 "upstream sent invalid response: \"%V\"", &s->out);
|
|
736
|
|
737 s->out.len = b->last - b->pos;
|
|
738 s->out.data = b->pos;
|
|
739
|
|
740 return NGX_ERROR;
|
|
741 }
|
|
742
|
|
743
|
|
744 static void
|
|
745 ngx_mail_proxy_handler(ngx_event_t *ev)
|
|
746 {
|
|
747 char *action, *recv_action, *send_action;
|
|
748 size_t size;
|
|
749 ssize_t n;
|
|
750 ngx_buf_t *b;
|
|
751 ngx_uint_t do_write;
|
|
752 ngx_connection_t *c, *src, *dst;
|
|
753 ngx_mail_session_t *s;
|
|
754 ngx_mail_proxy_conf_t *pcf;
|
|
755
|
|
756 c = ev->data;
|
|
757 s = c->data;
|
|
758
|
|
759 if (ev->timedout) {
|
|
760 c->log->action = "proxying";
|
|
761
|
|
762 if (c == s->connection) {
|
|
763 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
764 "client timed out");
|
|
765 c->timedout = 1;
|
|
766
|
|
767 } else {
|
|
768 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
769 "upstream timed out");
|
|
770 }
|
|
771
|
|
772 ngx_mail_proxy_close_session(s);
|
|
773 return;
|
|
774 }
|
|
775
|
|
776 if (c == s->connection) {
|
|
777 if (ev->write) {
|
|
778 recv_action = "proxying and reading from upstream";
|
|
779 send_action = "proxying and sending to client";
|
|
780 src = s->proxy->upstream.connection;
|
|
781 dst = c;
|
|
782 b = s->proxy->buffer;
|
|
783
|
|
784 } else {
|
|
785 recv_action = "proxying and reading from client";
|
|
786 send_action = "proxying and sending to upstream";
|
|
787 src = c;
|
|
788 dst = s->proxy->upstream.connection;
|
|
789 b = s->buffer;
|
|
790 }
|
|
791
|
|
792 } else {
|
|
793 if (ev->write) {
|
|
794 recv_action = "proxying and reading from client";
|
|
795 send_action = "proxying and sending to upstream";
|
|
796 src = s->connection;
|
|
797 dst = c;
|
|
798 b = s->buffer;
|
|
799
|
|
800 } else {
|
|
801 recv_action = "proxying and reading from upstream";
|
|
802 send_action = "proxying and sending to client";
|
|
803 src = c;
|
|
804 dst = s->connection;
|
|
805 b = s->proxy->buffer;
|
|
806 }
|
|
807 }
|
|
808
|
|
809 do_write = ev->write ? 1 : 0;
|
|
810
|
|
811 ngx_log_debug3(NGX_LOG_DEBUG_MAIL, ev->log, 0,
|
|
812 "mail proxy handler: %d, #%d > #%d",
|
|
813 do_write, src->fd, dst->fd);
|
|
814
|
|
815 for ( ;; ) {
|
|
816
|
|
817 if (do_write) {
|
|
818
|
|
819 size = b->last - b->pos;
|
|
820
|
|
821 if (size && dst->write->ready) {
|
|
822 c->log->action = send_action;
|
|
823
|
|
824 n = dst->send(dst, b->pos, size);
|
|
825
|
|
826 if (n == NGX_ERROR) {
|
|
827 ngx_mail_proxy_close_session(s);
|
|
828 return;
|
|
829 }
|
|
830
|
|
831 if (n > 0) {
|
|
832 b->pos += n;
|
|
833
|
|
834 if (b->pos == b->last) {
|
|
835 b->pos = b->start;
|
|
836 b->last = b->start;
|
|
837 }
|
|
838 }
|
|
839 }
|
|
840 }
|
|
841
|
|
842 size = b->end - b->last;
|
|
843
|
|
844 if (size && src->read->ready) {
|
|
845 c->log->action = recv_action;
|
|
846
|
|
847 n = src->recv(src, b->last, size);
|
|
848
|
|
849 if (n == NGX_AGAIN || n == 0) {
|
|
850 break;
|
|
851 }
|
|
852
|
|
853 if (n > 0) {
|
|
854 do_write = 1;
|
|
855 b->last += n;
|
|
856
|
|
857 continue;
|
|
858 }
|
|
859
|
|
860 if (n == NGX_ERROR) {
|
|
861 src->read->eof = 1;
|
|
862 }
|
|
863 }
|
|
864
|
|
865 break;
|
|
866 }
|
|
867
|
|
868 c->log->action = "proxying";
|
|
869
|
324
|
870 if ((s->connection->read->eof && s->buffer->pos == s->buffer->last)
|
|
871 || (s->proxy->upstream.connection->read->eof
|
|
872 && s->proxy->buffer->pos == s->proxy->buffer->last)
|
|
873 || (s->connection->read->eof
|
|
874 && s->proxy->upstream.connection->read->eof))
|
290
|
875 {
|
|
876 action = c->log->action;
|
|
877 c->log->action = NULL;
|
|
878 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
|
|
879 c->log->action = action;
|
|
880
|
|
881 ngx_mail_proxy_close_session(s);
|
|
882 return;
|
|
883 }
|
|
884
|
|
885 if (ngx_handle_write_event(dst->write, 0) == NGX_ERROR) {
|
|
886 ngx_mail_proxy_close_session(s);
|
|
887 return;
|
|
888 }
|
|
889
|
|
890 if (ngx_handle_read_event(dst->read, 0) == NGX_ERROR) {
|
|
891 ngx_mail_proxy_close_session(s);
|
|
892 return;
|
|
893 }
|
|
894
|
|
895 if (ngx_handle_write_event(src->write, 0) == NGX_ERROR) {
|
|
896 ngx_mail_proxy_close_session(s);
|
|
897 return;
|
|
898 }
|
|
899
|
|
900 if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) {
|
|
901 ngx_mail_proxy_close_session(s);
|
|
902 return;
|
|
903 }
|
|
904
|
|
905 if (c == s->connection) {
|
|
906 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
|
907 ngx_add_timer(c->read, pcf->timeout);
|
|
908 }
|
|
909 }
|
|
910
|
|
911
|
|
912 static void
|
|
913 ngx_mail_proxy_upstream_error(ngx_mail_session_t *s)
|
|
914 {
|
|
915 if (s->proxy->upstream.connection) {
|
|
916 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
|
|
917 "close mail proxy connection: %d",
|
|
918 s->proxy->upstream.connection->fd);
|
|
919
|
|
920 ngx_close_connection(s->proxy->upstream.connection);
|
|
921 }
|
|
922
|
|
923 if (s->out.len == 0) {
|
|
924 ngx_mail_session_internal_server_error(s);
|
|
925 return;
|
|
926 }
|
|
927
|
|
928 s->quit = 1;
|
|
929 ngx_mail_send(s->connection->write);
|
|
930 }
|
|
931
|
|
932
|
|
933 static void
|
|
934 ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s)
|
|
935 {
|
|
936 if (s->proxy->upstream.connection) {
|
|
937 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
|
|
938 "close mail proxy connection: %d",
|
|
939 s->proxy->upstream.connection->fd);
|
|
940
|
|
941 ngx_close_connection(s->proxy->upstream.connection);
|
|
942 }
|
|
943
|
|
944 ngx_mail_session_internal_server_error(s);
|
|
945 }
|
|
946
|
|
947
|
|
948 static void
|
|
949 ngx_mail_proxy_close_session(ngx_mail_session_t *s)
|
|
950 {
|
|
951 if (s->proxy->upstream.connection) {
|
|
952 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
|
|
953 "close mail proxy connection: %d",
|
|
954 s->proxy->upstream.connection->fd);
|
|
955
|
|
956 ngx_close_connection(s->proxy->upstream.connection);
|
|
957 }
|
|
958
|
|
959 ngx_mail_close_connection(s->connection);
|
|
960 }
|
|
961
|
|
962
|
|
963 static void *
|
|
964 ngx_mail_proxy_create_conf(ngx_conf_t *cf)
|
|
965 {
|
|
966 ngx_mail_proxy_conf_t *pcf;
|
|
967
|
|
968 pcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_proxy_conf_t));
|
|
969 if (pcf == NULL) {
|
|
970 return NGX_CONF_ERROR;
|
|
971 }
|
|
972
|
|
973 pcf->enable = NGX_CONF_UNSET;
|
|
974 pcf->pass_error_message = NGX_CONF_UNSET;
|
|
975 pcf->xclient = NGX_CONF_UNSET;
|
|
976 pcf->buffer_size = NGX_CONF_UNSET_SIZE;
|
|
977 pcf->timeout = NGX_CONF_UNSET_MSEC;
|
|
978
|
|
979 return pcf;
|
|
980 }
|
|
981
|
|
982
|
|
983 static char *
|
|
984 ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
985 {
|
|
986 ngx_mail_proxy_conf_t *prev = parent;
|
|
987 ngx_mail_proxy_conf_t *conf = child;
|
|
988
|
|
989 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
990 ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0);
|
|
991 ngx_conf_merge_value(conf->xclient, prev->xclient, 1);
|
|
992 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
|
|
993 (size_t) ngx_pagesize);
|
|
994 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000);
|
|
995
|
|
996 return NGX_CONF_OK;
|
|
997 }
|