76
|
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_imap.h>
|
|
12
|
|
13
|
|
14 typedef struct {
|
|
15 ngx_flag_t enable;
|
88
|
16 size_t buffer_size;
|
|
17 ngx_msec_t timeout;
|
76
|
18 } ngx_imap_proxy_conf_t;
|
|
19
|
|
20
|
|
21 static void ngx_imap_proxy_block_read(ngx_event_t *rev);
|
|
22 static void ngx_imap_proxy_imap_handler(ngx_event_t *rev);
|
|
23 static void ngx_imap_proxy_pop3_handler(ngx_event_t *rev);
|
|
24 static void ngx_imap_proxy_dummy_handler(ngx_event_t *ev);
|
|
25 static ngx_int_t ngx_imap_proxy_read_response(ngx_imap_session_t *s,
|
|
26 ngx_uint_t what);
|
|
27 static void ngx_imap_proxy_handler(ngx_event_t *ev);
|
|
28 static void ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s);
|
|
29 static void ngx_imap_proxy_close_session(ngx_imap_session_t *s);
|
|
30 static void *ngx_imap_proxy_create_conf(ngx_conf_t *cf);
|
|
31 static char *ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent,
|
|
32 void *child);
|
|
33
|
|
34
|
120
|
35 #define NGX_IMAP_WAIT_OK 0
|
|
36 #define NGX_IMAP_WAIT_NEXT 1
|
76
|
37
|
|
38
|
|
39 static ngx_command_t ngx_imap_proxy_commands[] = {
|
88
|
40
|
76
|
41 { ngx_string("proxy"),
|
|
42 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_FLAG,
|
|
43 ngx_conf_set_flag_slot,
|
|
44 NGX_IMAP_SRV_CONF_OFFSET,
|
|
45 offsetof(ngx_imap_proxy_conf_t, enable),
|
|
46 NULL },
|
|
47
|
88
|
48 { ngx_string("proxy_buffer"),
|
|
49 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
|
|
50 ngx_conf_set_size_slot,
|
|
51 NGX_IMAP_SRV_CONF_OFFSET,
|
|
52 offsetof(ngx_imap_proxy_conf_t, buffer_size),
|
|
53 NULL },
|
|
54
|
|
55 { ngx_string("proxy_timeout"),
|
|
56 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
|
|
57 ngx_conf_set_msec_slot,
|
|
58 NGX_IMAP_SRV_CONF_OFFSET,
|
|
59 offsetof(ngx_imap_proxy_conf_t, timeout),
|
|
60 NULL },
|
|
61
|
76
|
62 ngx_null_command
|
|
63 };
|
|
64
|
|
65
|
|
66 static ngx_imap_module_t ngx_imap_proxy_module_ctx = {
|
|
67 NULL, /* create main configuration */
|
|
68 NULL, /* init main configuration */
|
|
69
|
|
70 ngx_imap_proxy_create_conf, /* create server configuration */
|
|
71 ngx_imap_proxy_merge_conf /* merge server configuration */
|
|
72 };
|
|
73
|
|
74
|
|
75 ngx_module_t ngx_imap_proxy_module = {
|
|
76 NGX_MODULE_V1,
|
|
77 &ngx_imap_proxy_module_ctx, /* module context */
|
|
78 ngx_imap_proxy_commands, /* module directives */
|
|
79 NGX_IMAP_MODULE, /* module type */
|
90
|
80 NULL, /* init master */
|
76
|
81 NULL, /* init module */
|
90
|
82 NULL, /* init process */
|
|
83 NULL, /* init thread */
|
|
84 NULL, /* exit thread */
|
|
85 NULL, /* exit process */
|
|
86 NULL, /* exit master */
|
|
87 NGX_MODULE_V1_PADDING
|
76
|
88 };
|
|
89
|
|
90
|
|
91 void
|
|
92 ngx_imap_proxy_init(ngx_imap_session_t *s, ngx_peers_t *peers)
|
|
93 {
|
|
94 ngx_int_t rc;
|
|
95 ngx_imap_proxy_ctx_t *p;
|
|
96 ngx_imap_core_srv_conf_t *cscf;
|
|
97
|
|
98 p = ngx_pcalloc(s->connection->pool, sizeof(ngx_imap_proxy_ctx_t));
|
|
99 if (p == NULL) {
|
|
100 ngx_imap_session_internal_server_error(s);
|
|
101 return;
|
|
102 }
|
|
103
|
|
104 s->proxy = p;
|
|
105
|
|
106 p->upstream.peers = peers;
|
|
107 p->upstream.log = s->connection->log;
|
|
108 p->upstream.log_error = NGX_ERROR_ERR;
|
|
109
|
90
|
110 s->connection->log->action = "in upstream auth state";
|
|
111
|
76
|
112 rc = ngx_event_connect_peer(&p->upstream);
|
|
113
|
92
|
114 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) {
|
|
115 ngx_imap_proxy_internal_server_error(s);
|
76
|
116 return;
|
|
117 }
|
|
118
|
|
119 cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
120 ngx_add_timer(p->upstream.connection->read, cscf->timeout);
|
|
121
|
|
122 p->upstream.connection->data = s;
|
|
123 p->upstream.connection->pool = s->connection->pool;
|
|
124
|
|
125 s->connection->read->handler = ngx_imap_proxy_block_read;
|
|
126 p->upstream.connection->write->handler = ngx_imap_proxy_dummy_handler;
|
|
127
|
|
128 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
|
129 p->upstream.connection->read->handler = ngx_imap_proxy_pop3_handler;
|
|
130 s->imap_state = ngx_pop3_start;
|
|
131
|
|
132 } else {
|
|
133 p->upstream.connection->read->handler = ngx_imap_proxy_imap_handler;
|
|
134 s->imap_state = ngx_imap_start;
|
|
135 }
|
|
136 }
|
|
137
|
|
138
|
|
139 static void
|
|
140 ngx_imap_proxy_block_read(ngx_event_t *rev)
|
|
141 {
|
|
142 ngx_connection_t *c;
|
|
143 ngx_imap_session_t *s;
|
|
144
|
|
145 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy block read");
|
|
146
|
|
147 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
148 c = rev->data;
|
|
149 s = c->data;
|
|
150
|
|
151 ngx_imap_proxy_close_session(s);
|
|
152 }
|
|
153 }
|
|
154
|
|
155
|
|
156 static void
|
|
157 ngx_imap_proxy_imap_handler(ngx_event_t *rev)
|
|
158 {
|
88
|
159 u_char *p;
|
|
160 ngx_int_t rc;
|
|
161 ngx_str_t line;
|
|
162 ngx_connection_t *c;
|
|
163 ngx_imap_session_t *s;
|
|
164 ngx_imap_proxy_conf_t *pcf;
|
76
|
165
|
|
166 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
167 "imap proxy imap auth handler");
|
|
168
|
|
169 c = rev->data;
|
|
170 s = c->data;
|
|
171
|
|
172 if (rev->timedout) {
|
|
173 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
174 "upstream timed out");
|
126
|
175 c->timedout = 1;
|
76
|
176 ngx_imap_proxy_internal_server_error(s);
|
|
177 return;
|
|
178 }
|
|
179
|
|
180 if (s->proxy->buffer == NULL) {
|
88
|
181 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
|
76
|
182
|
88
|
183 s->proxy->buffer = ngx_create_temp_buf(c->pool, pcf->buffer_size);
|
76
|
184 if (s->proxy->buffer == NULL) {
|
|
185 ngx_imap_proxy_internal_server_error(s);
|
|
186 return;
|
|
187 }
|
|
188 }
|
|
189
|
|
190 rc = ngx_imap_proxy_read_response(s, s->imap_state == ngx_imap_start ?
|
|
191 NGX_IMAP_WAIT_OK : NGX_IMAP_WAIT_NEXT);
|
|
192
|
|
193 if (rc == NGX_AGAIN) {
|
|
194 return;
|
|
195 }
|
|
196
|
120
|
197 if (rc == NGX_ERROR) {
|
76
|
198 ngx_imap_proxy_internal_server_error(s);
|
|
199 return;
|
|
200 }
|
|
201
|
|
202 switch (s->imap_state) {
|
|
203
|
|
204 case ngx_imap_start:
|
|
205 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
206 "imap proxy send login");
|
|
207
|
|
208 line.len = s->tag.len + sizeof("LOGIN ") - 1
|
|
209 + 1 + NGX_SIZE_T_LEN + 1 + 2;
|
|
210 line.data = ngx_palloc(c->pool, line.len);
|
|
211 if (line.data == NULL) {
|
|
212 ngx_imap_proxy_internal_server_error(s);
|
|
213 return;
|
|
214 }
|
|
215
|
|
216 line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF,
|
|
217 &s->tag, s->login.len)
|
|
218 - line.data;
|
|
219
|
|
220 s->imap_state = ngx_imap_login;
|
|
221 break;
|
|
222
|
|
223 case ngx_imap_login:
|
|
224 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
|
|
225
|
78
|
226 line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2;
|
76
|
227 line.data = ngx_palloc(c->pool, line.len);
|
|
228 if (line.data == NULL) {
|
|
229 ngx_imap_proxy_internal_server_error(s);
|
|
230 return;
|
|
231 }
|
|
232
|
78
|
233 line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF,
|
76
|
234 &s->login, s->passwd.len)
|
|
235 - line.data;
|
|
236
|
|
237 s->imap_state = ngx_imap_user;
|
|
238 break;
|
|
239
|
|
240 case ngx_imap_user:
|
|
241 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
242 "imap proxy send passwd");
|
|
243
|
|
244 line.len = s->passwd.len + 2;
|
|
245 line.data = ngx_palloc(c->pool, line.len);
|
|
246 if (line.data == NULL) {
|
|
247 ngx_imap_proxy_internal_server_error(s);
|
|
248 return;
|
|
249 }
|
|
250
|
|
251 p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len);
|
|
252 *p++ = CR; *p = LF;
|
|
253
|
|
254 s->imap_state = ngx_imap_passwd;
|
|
255 break;
|
|
256
|
|
257 default:
|
|
258 #if (NGX_SUPPRESS_WARN)
|
|
259 line.len = 0;
|
|
260 line.data = NULL;
|
|
261 #endif
|
|
262 break;
|
|
263 }
|
|
264
|
88
|
265 if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
|
76
|
266 /*
|
|
267 * we treat the incomplete sending as NGX_ERROR
|
|
268 * because it is very strange here
|
|
269 */
|
|
270 ngx_imap_proxy_internal_server_error(s);
|
|
271 return;
|
|
272 }
|
|
273
|
|
274 s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
275 s->proxy->buffer->last = s->proxy->buffer->start;
|
|
276
|
|
277 if (s->imap_state == ngx_imap_passwd) {
|
|
278 s->connection->read->handler = ngx_imap_proxy_handler;
|
|
279 s->connection->write->handler = ngx_imap_proxy_handler;
|
|
280 rev->handler = ngx_imap_proxy_handler;
|
|
281 c->write->handler = ngx_imap_proxy_handler;
|
80
|
282
|
88
|
283 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
|
|
284 ngx_add_timer(s->connection->read, pcf->timeout);
|
80
|
285 ngx_del_timer(c->read);
|
90
|
286
|
96
|
287 c->log->action = NULL;
|
|
288 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
|
76
|
289 }
|
|
290 }
|
|
291
|
|
292
|
|
293 static void
|
|
294 ngx_imap_proxy_pop3_handler(ngx_event_t *rev)
|
|
295 {
|
88
|
296 u_char *p;
|
|
297 ngx_int_t rc;
|
|
298 ngx_str_t line;
|
|
299 ngx_connection_t *c;
|
|
300 ngx_imap_session_t *s;
|
|
301 ngx_imap_proxy_conf_t *pcf;
|
76
|
302
|
|
303 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
304 "imap proxy pop3 auth handler");
|
|
305
|
|
306 c = rev->data;
|
|
307 s = c->data;
|
|
308
|
|
309 if (rev->timedout) {
|
|
310 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
311 "upstream timed out");
|
126
|
312 c->timedout = 1;
|
76
|
313 ngx_imap_proxy_internal_server_error(s);
|
|
314 return;
|
|
315 }
|
|
316
|
|
317 if (s->proxy->buffer == NULL) {
|
88
|
318 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
|
76
|
319
|
88
|
320 s->proxy->buffer = ngx_create_temp_buf(c->pool, pcf->buffer_size);
|
76
|
321 if (s->proxy->buffer == NULL) {
|
|
322 ngx_imap_proxy_internal_server_error(s);
|
|
323 return;
|
|
324 }
|
|
325 }
|
|
326
|
|
327 rc = ngx_imap_proxy_read_response(s, NGX_IMAP_WAIT_OK);
|
|
328
|
|
329 if (rc == NGX_AGAIN) {
|
|
330 return;
|
|
331 }
|
|
332
|
120
|
333 if (rc == NGX_ERROR) {
|
76
|
334 ngx_imap_proxy_internal_server_error(s);
|
|
335 return;
|
|
336 }
|
|
337
|
|
338 switch (s->imap_state) {
|
|
339
|
|
340 case ngx_pop3_start:
|
|
341 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
|
|
342
|
|
343 line.len = sizeof("USER ") - 1 + s->login.len + 2;
|
|
344 line.data = ngx_palloc(c->pool, line.len);
|
|
345 if (line.data == NULL) {
|
|
346 ngx_imap_proxy_internal_server_error(s);
|
|
347 return;
|
|
348 }
|
|
349
|
|
350 p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1);
|
|
351 p = ngx_cpymem(p, s->login.data, s->login.len);
|
|
352 *p++ = CR; *p = LF;
|
|
353
|
|
354 s->imap_state = ngx_pop3_user;
|
|
355 break;
|
|
356
|
|
357 case ngx_pop3_user:
|
|
358 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send pass");
|
|
359
|
|
360 line.len = sizeof("PASS ") - 1 + s->passwd.len + 2;
|
|
361 line.data = ngx_palloc(c->pool, line.len);
|
|
362 if (line.data == NULL) {
|
|
363 ngx_imap_proxy_internal_server_error(s);
|
|
364 return;
|
|
365 }
|
|
366
|
|
367 p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1);
|
|
368 p = ngx_cpymem(p, s->passwd.data, s->passwd.len);
|
|
369 *p++ = CR; *p = LF;
|
|
370
|
|
371 s->imap_state = ngx_pop3_passwd;
|
|
372 break;
|
|
373
|
|
374 default:
|
|
375 #if (NGX_SUPPRESS_WARN)
|
|
376 line.len = 0;
|
|
377 line.data = NULL;
|
|
378 #endif
|
|
379 break;
|
|
380 }
|
|
381
|
88
|
382 if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
|
76
|
383 /*
|
|
384 * we treat the incomplete sending as NGX_ERROR
|
|
385 * because it is very strange here
|
|
386 */
|
|
387 ngx_imap_proxy_internal_server_error(s);
|
|
388 return;
|
|
389 }
|
|
390
|
|
391 s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
392 s->proxy->buffer->last = s->proxy->buffer->start;
|
|
393
|
|
394 if (s->imap_state == ngx_pop3_passwd) {
|
|
395 s->connection->read->handler = ngx_imap_proxy_handler;
|
|
396 s->connection->write->handler = ngx_imap_proxy_handler;
|
|
397 rev->handler = ngx_imap_proxy_handler;
|
|
398 c->write->handler = ngx_imap_proxy_handler;
|
80
|
399
|
88
|
400 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
|
|
401 ngx_add_timer(s->connection->read, pcf->timeout);
|
80
|
402 ngx_del_timer(c->read);
|
90
|
403
|
96
|
404 c->log->action = NULL;
|
|
405 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
|
76
|
406 }
|
|
407 }
|
|
408
|
|
409
|
|
410 static void
|
132
|
411 ngx_imap_proxy_dummy_handler(ngx_event_t *wev)
|
76
|
412 {
|
132
|
413 ngx_connection_t *c;
|
|
414 ngx_imap_session_t *s;
|
|
415
|
|
416 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, wev->log, 0, "imap proxy dummy handler");
|
|
417
|
|
418 if (ngx_handle_write_event(wev, 0) == NGX_ERROR) {
|
|
419 c = wev->data;
|
|
420 s = c->data;
|
|
421
|
|
422 ngx_imap_proxy_close_session(s);
|
|
423 }
|
76
|
424 }
|
|
425
|
|
426
|
|
427 static ngx_int_t
|
|
428 ngx_imap_proxy_read_response(ngx_imap_session_t *s, ngx_uint_t what)
|
|
429 {
|
|
430 u_char *p;
|
|
431 ssize_t n;
|
|
432 ngx_buf_t *b;
|
|
433
|
|
434 b = s->proxy->buffer;
|
|
435
|
88
|
436 n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection,
|
|
437 b->last, b->end - b->last);
|
76
|
438
|
|
439 if (n == NGX_ERROR || n == 0) {
|
|
440 return NGX_ERROR;
|
|
441 }
|
|
442
|
|
443 if (n == NGX_AGAIN) {
|
|
444 return NGX_AGAIN;
|
|
445 }
|
|
446
|
|
447 b->last += n;
|
|
448
|
|
449 if (b->last - b->pos < 5) {
|
|
450 return NGX_AGAIN;
|
|
451 }
|
|
452
|
|
453 if (*(b->last - 2) != CR || *(b->last - 1) != LF) {
|
|
454 if (b->last == b->end) {
|
|
455 *(b->last - 1) = '\0';
|
|
456 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
|
|
457 "upstream sent too long response line: \"%s\"",
|
|
458 b->pos);
|
120
|
459 return NGX_ERROR;
|
76
|
460 }
|
|
461
|
|
462 return NGX_AGAIN;
|
|
463 }
|
|
464
|
|
465 p = b->pos;
|
|
466
|
|
467 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
|
468 if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
|
|
469 return NGX_OK;
|
|
470 }
|
|
471
|
|
472 } else {
|
|
473 if (what == NGX_IMAP_WAIT_OK) {
|
|
474 if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
|
|
475 return NGX_OK;
|
|
476 }
|
|
477
|
|
478 } else {
|
78
|
479 if (p[0] == '+') {
|
76
|
480 return NGX_OK;
|
|
481 }
|
|
482 }
|
|
483 }
|
|
484
|
|
485 *(b->last - 2) = '\0';
|
|
486 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
|
|
487 "upstream sent invalid response: \"%s\"", p);
|
|
488
|
120
|
489 return NGX_ERROR;
|
76
|
490 }
|
|
491
|
|
492
|
|
493 static void
|
|
494 ngx_imap_proxy_handler(ngx_event_t *ev)
|
|
495 {
|
132
|
496 char *action, *recv_action, *send_action;
|
88
|
497 size_t size;
|
|
498 ssize_t n;
|
|
499 ngx_buf_t *b;
|
132
|
500 ngx_uint_t do_write;
|
88
|
501 ngx_connection_t *c, *src, *dst;
|
|
502 ngx_imap_session_t *s;
|
|
503 ngx_imap_proxy_conf_t *pcf;
|
76
|
504
|
|
505 c = ev->data;
|
|
506 s = c->data;
|
|
507
|
|
508 if (ev->timedout) {
|
132
|
509 c->log->action = "proxying";
|
|
510
|
76
|
511 if (c == s->connection) {
|
|
512 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
513 "client timed out");
|
126
|
514 c->timedout = 1;
|
|
515
|
76
|
516 } else {
|
|
517 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
518 "upstream timed out");
|
|
519 }
|
|
520
|
|
521 ngx_imap_proxy_close_session(s);
|
|
522 return;
|
|
523 }
|
|
524
|
|
525 if (c == s->connection) {
|
|
526 if (ev->write) {
|
132
|
527 recv_action = "proxying and reading from upstream";
|
|
528 send_action = "proxying and sending to client";
|
76
|
529 src = s->proxy->upstream.connection;
|
|
530 dst = c;
|
|
531 b = s->proxy->buffer;
|
|
532
|
|
533 } else {
|
132
|
534 recv_action = "proxying and reading from client";
|
|
535 send_action = "proxying and sending to upstream";
|
76
|
536 src = c;
|
|
537 dst = s->proxy->upstream.connection;
|
|
538 b = s->buffer;
|
|
539 }
|
|
540
|
|
541 } else {
|
|
542 if (ev->write) {
|
132
|
543 recv_action = "proxying and reading from upstream";
|
|
544 send_action = "proxying and sending to client";
|
76
|
545 src = s->connection;
|
|
546 dst = c;
|
|
547 b = s->buffer;
|
|
548
|
|
549 } else {
|
132
|
550 recv_action = "proxying and reading from client";
|
|
551 send_action = "proxying and sending to upstream";
|
76
|
552 src = c;
|
|
553 dst = s->connection;
|
|
554 b = s->proxy->buffer;
|
|
555 }
|
|
556 }
|
|
557
|
|
558 do_write = ev->write ? 1 : 0;
|
|
559
|
|
560 ngx_log_debug3(NGX_LOG_DEBUG_IMAP, ev->log, 0,
|
|
561 "imap proxy handler: %d, #%d > #%d",
|
|
562 do_write, src->fd, dst->fd);
|
|
563
|
132
|
564 for ( ;; ) {
|
76
|
565
|
132
|
566 if (do_write) {
|
76
|
567
|
|
568 size = b->last - b->pos;
|
|
569
|
|
570 if (size && dst->write->ready) {
|
132
|
571 c->log->action = send_action;
|
|
572
|
88
|
573 n = dst->send(dst, b->pos, size);
|
76
|
574
|
|
575 if (n == NGX_ERROR) {
|
|
576 ngx_imap_proxy_close_session(s);
|
|
577 return;
|
|
578 }
|
|
579
|
|
580 if (n > 0) {
|
|
581 b->pos += n;
|
|
582
|
|
583 if (b->pos == b->last) {
|
|
584 b->pos = b->start;
|
|
585 b->last = b->start;
|
|
586 }
|
|
587 }
|
|
588 }
|
|
589 }
|
|
590
|
|
591 size = b->end - b->last;
|
|
592
|
|
593 if (size && src->read->ready) {
|
132
|
594 c->log->action = recv_action;
|
|
595
|
88
|
596 n = src->recv(src, b->last, size);
|
76
|
597
|
132
|
598 if (n == NGX_AGAIN || n == 0) {
|
|
599 break;
|
76
|
600 }
|
|
601
|
|
602 if (n > 0) {
|
|
603 do_write = 1;
|
|
604 b->last += n;
|
132
|
605
|
|
606 continue;
|
76
|
607 }
|
|
608
|
132
|
609 if (n == NGX_ERROR) {
|
|
610 src->read->eof = 1;
|
88
|
611 }
|
76
|
612 }
|
|
613
|
132
|
614 break;
|
|
615 }
|
|
616
|
|
617 c->log->action = "proxying";
|
|
618
|
|
619 if ((s->connection->read->eof || s->proxy->upstream.connection->read->eof)
|
|
620 && s->buffer->pos == s->buffer->last
|
|
621 && s->proxy->buffer->pos == s->proxy->buffer->last)
|
|
622 {
|
|
623 action = c->log->action;
|
|
624 c->log->action = NULL;
|
|
625 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
|
|
626 c->log->action = action;
|
|
627
|
|
628 ngx_imap_proxy_close_session(s);
|
|
629 return;
|
|
630 }
|
|
631
|
|
632 if (ngx_handle_write_event(dst->write, 0) == NGX_ERROR) {
|
|
633 ngx_imap_proxy_close_session(s);
|
|
634 return;
|
|
635 }
|
|
636
|
|
637 if (ngx_handle_read_event(dst->read, 0) == NGX_ERROR) {
|
|
638 ngx_imap_proxy_close_session(s);
|
|
639 return;
|
|
640 }
|
|
641
|
|
642 if (ngx_handle_write_event(src->write, 0) == NGX_ERROR) {
|
|
643 ngx_imap_proxy_close_session(s);
|
|
644 return;
|
|
645 }
|
|
646
|
|
647 if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) {
|
|
648 ngx_imap_proxy_close_session(s);
|
|
649 return;
|
|
650 }
|
|
651
|
|
652 if (c == s->connection) {
|
|
653 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
|
|
654 ngx_add_timer(c->read, pcf->timeout);
|
|
655 }
|
76
|
656 }
|
|
657
|
|
658
|
|
659 static void
|
|
660 ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s)
|
|
661 {
|
|
662 if (s->proxy->upstream.connection) {
|
|
663 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
|
|
664 "close imap proxy connection: %d",
|
|
665 s->proxy->upstream.connection->fd);
|
|
666
|
|
667 ngx_close_connection(s->proxy->upstream.connection);
|
|
668 }
|
|
669
|
|
670 ngx_imap_session_internal_server_error(s);
|
|
671 }
|
|
672
|
|
673
|
|
674 static void
|
|
675 ngx_imap_proxy_close_session(ngx_imap_session_t *s)
|
|
676 {
|
|
677 if (s->proxy->upstream.connection) {
|
|
678 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
|
|
679 "close imap proxy connection: %d",
|
|
680 s->proxy->upstream.connection->fd);
|
|
681
|
|
682 ngx_close_connection(s->proxy->upstream.connection);
|
|
683 }
|
|
684
|
|
685 ngx_imap_close_connection(s->connection);
|
|
686 }
|
|
687
|
|
688
|
|
689 static void *
|
|
690 ngx_imap_proxy_create_conf(ngx_conf_t *cf)
|
126
|
691 {
|
76
|
692 ngx_imap_proxy_conf_t *pcf;
|
126
|
693
|
76
|
694 pcf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_proxy_conf_t));
|
|
695 if (pcf == NULL) {
|
|
696 return NGX_CONF_ERROR;
|
|
697 }
|
|
698
|
|
699 pcf->enable = NGX_CONF_UNSET;
|
88
|
700 pcf->buffer_size = NGX_CONF_UNSET_SIZE;
|
|
701 pcf->timeout = NGX_CONF_UNSET_MSEC;
|
76
|
702
|
|
703 return pcf;
|
|
704 }
|
|
705
|
|
706
|
|
707 static char *
|
|
708 ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
709 {
|
|
710 ngx_imap_proxy_conf_t *prev = parent;
|
|
711 ngx_imap_proxy_conf_t *conf = child;
|
|
712
|
88
|
713 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
714 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
|
|
715 (size_t) ngx_pagesize);
|
|
716 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000);
|
76
|
717
|
|
718 return NGX_CONF_OK;
|
|
719 }
|