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
|
|
35 #define NGX_IMAP_WAIT_OK 0
|
|
36 #define NGX_IMAP_WAIT_NEXT 1
|
|
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 {
|
96
|
159 char *action;
|
88
|
160 u_char *p;
|
|
161 ngx_int_t rc;
|
|
162 ngx_str_t line;
|
|
163 ngx_connection_t *c;
|
|
164 ngx_imap_session_t *s;
|
|
165 ngx_imap_proxy_conf_t *pcf;
|
76
|
166
|
|
167 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
168 "imap proxy imap auth handler");
|
|
169
|
|
170 c = rev->data;
|
|
171 s = c->data;
|
|
172
|
|
173 if (rev->timedout) {
|
|
174 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
175 "upstream timed out");
|
|
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
|
78
|
197 if (rc == NGX_ERROR || rc == NGX_IMAP_PROXY_INVALID) {
|
76
|
198 ngx_imap_proxy_internal_server_error(s);
|
|
199 return;
|
|
200 }
|
|
201
|
78
|
202 if (rc == NGX_IMAP_PROXY_ERROR) {
|
|
203 s->connection->read->handler = ngx_imap_proxy_handler;
|
|
204 s->connection->write->handler = ngx_imap_proxy_handler;
|
|
205 rev->handler = ngx_imap_proxy_handler;
|
|
206 c->write->handler = ngx_imap_proxy_handler;
|
|
207
|
|
208 ngx_imap_proxy_handler(c->read);
|
|
209 return;
|
|
210 }
|
|
211
|
76
|
212 switch (s->imap_state) {
|
|
213
|
|
214 case ngx_imap_start:
|
|
215 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
216 "imap proxy send login");
|
|
217
|
|
218 line.len = s->tag.len + sizeof("LOGIN ") - 1
|
|
219 + 1 + NGX_SIZE_T_LEN + 1 + 2;
|
|
220 line.data = ngx_palloc(c->pool, line.len);
|
|
221 if (line.data == NULL) {
|
|
222 ngx_imap_proxy_internal_server_error(s);
|
|
223 return;
|
|
224 }
|
|
225
|
|
226 line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF,
|
|
227 &s->tag, s->login.len)
|
|
228 - line.data;
|
|
229
|
|
230 s->imap_state = ngx_imap_login;
|
|
231 break;
|
|
232
|
|
233 case ngx_imap_login:
|
|
234 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
|
|
235
|
78
|
236 line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2;
|
76
|
237 line.data = ngx_palloc(c->pool, line.len);
|
|
238 if (line.data == NULL) {
|
|
239 ngx_imap_proxy_internal_server_error(s);
|
|
240 return;
|
|
241 }
|
|
242
|
78
|
243 line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF,
|
76
|
244 &s->login, s->passwd.len)
|
|
245 - line.data;
|
|
246
|
|
247 s->imap_state = ngx_imap_user;
|
|
248 break;
|
|
249
|
|
250 case ngx_imap_user:
|
|
251 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
252 "imap proxy send passwd");
|
|
253
|
|
254 line.len = s->passwd.len + 2;
|
|
255 line.data = ngx_palloc(c->pool, line.len);
|
|
256 if (line.data == NULL) {
|
|
257 ngx_imap_proxy_internal_server_error(s);
|
|
258 return;
|
|
259 }
|
|
260
|
|
261 p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len);
|
|
262 *p++ = CR; *p = LF;
|
|
263
|
|
264 s->imap_state = ngx_imap_passwd;
|
|
265 break;
|
|
266
|
|
267 default:
|
|
268 #if (NGX_SUPPRESS_WARN)
|
|
269 line.len = 0;
|
|
270 line.data = NULL;
|
|
271 #endif
|
|
272 break;
|
|
273 }
|
|
274
|
88
|
275 if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
|
76
|
276 /*
|
|
277 * we treat the incomplete sending as NGX_ERROR
|
|
278 * because it is very strange here
|
|
279 */
|
|
280 ngx_imap_proxy_internal_server_error(s);
|
|
281 return;
|
|
282 }
|
|
283
|
|
284 s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
285 s->proxy->buffer->last = s->proxy->buffer->start;
|
|
286
|
|
287 if (s->imap_state == ngx_imap_passwd) {
|
|
288 s->connection->read->handler = ngx_imap_proxy_handler;
|
|
289 s->connection->write->handler = ngx_imap_proxy_handler;
|
|
290 rev->handler = ngx_imap_proxy_handler;
|
|
291 c->write->handler = ngx_imap_proxy_handler;
|
80
|
292
|
88
|
293 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
|
|
294 ngx_add_timer(s->connection->read, pcf->timeout);
|
80
|
295 ngx_del_timer(c->read);
|
90
|
296
|
96
|
297 action = c->log->action;
|
|
298 c->log->action = NULL;
|
|
299 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
|
|
300 c->log->action = action;
|
|
301
|
90
|
302 c->log->action = "proxying";
|
76
|
303 }
|
|
304 }
|
|
305
|
|
306
|
|
307 static void
|
|
308 ngx_imap_proxy_pop3_handler(ngx_event_t *rev)
|
|
309 {
|
96
|
310 char *action;
|
88
|
311 u_char *p;
|
|
312 ngx_int_t rc;
|
|
313 ngx_str_t line;
|
|
314 ngx_connection_t *c;
|
|
315 ngx_imap_session_t *s;
|
|
316 ngx_imap_proxy_conf_t *pcf;
|
76
|
317
|
|
318 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
319 "imap proxy pop3 auth handler");
|
|
320
|
|
321 c = rev->data;
|
|
322 s = c->data;
|
|
323
|
|
324 if (rev->timedout) {
|
|
325 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
326 "upstream timed out");
|
|
327 ngx_imap_proxy_internal_server_error(s);
|
|
328 return;
|
|
329 }
|
|
330
|
|
331 if (s->proxy->buffer == NULL) {
|
88
|
332 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
|
76
|
333
|
88
|
334 s->proxy->buffer = ngx_create_temp_buf(c->pool, pcf->buffer_size);
|
76
|
335 if (s->proxy->buffer == NULL) {
|
|
336 ngx_imap_proxy_internal_server_error(s);
|
|
337 return;
|
|
338 }
|
|
339 }
|
|
340
|
|
341 rc = ngx_imap_proxy_read_response(s, NGX_IMAP_WAIT_OK);
|
|
342
|
|
343 if (rc == NGX_AGAIN) {
|
|
344 return;
|
|
345 }
|
|
346
|
78
|
347 if (rc == NGX_ERROR || rc == NGX_IMAP_PROXY_INVALID) {
|
76
|
348 ngx_imap_proxy_internal_server_error(s);
|
|
349 return;
|
|
350 }
|
|
351
|
78
|
352 if (rc == NGX_IMAP_PROXY_ERROR) {
|
|
353 s->connection->read->handler = ngx_imap_proxy_handler;
|
|
354 s->connection->write->handler = ngx_imap_proxy_handler;
|
|
355 rev->handler = ngx_imap_proxy_handler;
|
|
356 c->write->handler = ngx_imap_proxy_handler;
|
|
357
|
|
358 ngx_imap_proxy_handler(c->read);
|
|
359 return;
|
|
360 }
|
|
361
|
76
|
362 switch (s->imap_state) {
|
|
363
|
|
364 case ngx_pop3_start:
|
|
365 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
|
|
366
|
|
367 line.len = sizeof("USER ") - 1 + s->login.len + 2;
|
|
368 line.data = ngx_palloc(c->pool, line.len);
|
|
369 if (line.data == NULL) {
|
|
370 ngx_imap_proxy_internal_server_error(s);
|
|
371 return;
|
|
372 }
|
|
373
|
|
374 p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1);
|
|
375 p = ngx_cpymem(p, s->login.data, s->login.len);
|
|
376 *p++ = CR; *p = LF;
|
|
377
|
|
378 s->imap_state = ngx_pop3_user;
|
|
379 break;
|
|
380
|
|
381 case ngx_pop3_user:
|
|
382 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send pass");
|
|
383
|
|
384 line.len = sizeof("PASS ") - 1 + s->passwd.len + 2;
|
|
385 line.data = ngx_palloc(c->pool, line.len);
|
|
386 if (line.data == NULL) {
|
|
387 ngx_imap_proxy_internal_server_error(s);
|
|
388 return;
|
|
389 }
|
|
390
|
|
391 p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1);
|
|
392 p = ngx_cpymem(p, s->passwd.data, s->passwd.len);
|
|
393 *p++ = CR; *p = LF;
|
|
394
|
|
395 s->imap_state = ngx_pop3_passwd;
|
|
396 break;
|
|
397
|
|
398 default:
|
|
399 #if (NGX_SUPPRESS_WARN)
|
|
400 line.len = 0;
|
|
401 line.data = NULL;
|
|
402 #endif
|
|
403 break;
|
|
404 }
|
|
405
|
88
|
406 if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
|
76
|
407 /*
|
|
408 * we treat the incomplete sending as NGX_ERROR
|
|
409 * because it is very strange here
|
|
410 */
|
|
411 ngx_imap_proxy_internal_server_error(s);
|
|
412 return;
|
|
413 }
|
|
414
|
|
415 s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
416 s->proxy->buffer->last = s->proxy->buffer->start;
|
|
417
|
|
418 if (s->imap_state == ngx_pop3_passwd) {
|
|
419 s->connection->read->handler = ngx_imap_proxy_handler;
|
|
420 s->connection->write->handler = ngx_imap_proxy_handler;
|
|
421 rev->handler = ngx_imap_proxy_handler;
|
|
422 c->write->handler = ngx_imap_proxy_handler;
|
80
|
423
|
88
|
424 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
|
|
425 ngx_add_timer(s->connection->read, pcf->timeout);
|
80
|
426 ngx_del_timer(c->read);
|
90
|
427
|
96
|
428 action = c->log->action;
|
|
429 c->log->action = NULL;
|
|
430 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
|
|
431 c->log->action = action;
|
|
432
|
90
|
433 c->log->action = "proxying";
|
76
|
434 }
|
|
435 }
|
|
436
|
|
437
|
|
438 static void
|
|
439 ngx_imap_proxy_dummy_handler(ngx_event_t *ev)
|
|
440 {
|
|
441 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, ev->log, 0, "imap proxy dummy handler");
|
|
442 }
|
|
443
|
|
444
|
|
445 static ngx_int_t
|
|
446 ngx_imap_proxy_read_response(ngx_imap_session_t *s, ngx_uint_t what)
|
|
447 {
|
|
448 u_char *p;
|
|
449 ssize_t n;
|
|
450 ngx_buf_t *b;
|
|
451
|
|
452 b = s->proxy->buffer;
|
|
453
|
88
|
454 n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection,
|
|
455 b->last, b->end - b->last);
|
76
|
456
|
|
457 if (n == NGX_ERROR || n == 0) {
|
|
458 return NGX_ERROR;
|
|
459 }
|
|
460
|
|
461 if (n == NGX_AGAIN) {
|
|
462 return NGX_AGAIN;
|
|
463 }
|
|
464
|
|
465 b->last += n;
|
|
466
|
|
467 if (b->last - b->pos < 5) {
|
|
468 return NGX_AGAIN;
|
|
469 }
|
|
470
|
|
471 if (*(b->last - 2) != CR || *(b->last - 1) != LF) {
|
|
472 if (b->last == b->end) {
|
|
473 *(b->last - 1) = '\0';
|
|
474 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
|
|
475 "upstream sent too long response line: \"%s\"",
|
|
476 b->pos);
|
|
477 return NGX_IMAP_PROXY_INVALID;
|
|
478 }
|
|
479
|
|
480 return NGX_AGAIN;
|
|
481 }
|
|
482
|
|
483 p = b->pos;
|
|
484
|
|
485 if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
|
486 if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
|
|
487 return NGX_OK;
|
|
488 }
|
|
489
|
|
490 if (p[0] == '-' && p[1] == 'E' && p[2] == 'R' && p[3] == 'R') {
|
|
491 return NGX_IMAP_PROXY_ERROR;
|
|
492 }
|
|
493
|
|
494 } else {
|
78
|
495 if (p[0] == 'N' && p[1] == 'O') {
|
|
496 return NGX_IMAP_PROXY_ERROR;
|
|
497 }
|
|
498
|
76
|
499 if (what == NGX_IMAP_WAIT_OK) {
|
|
500 if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
|
|
501 return NGX_OK;
|
|
502 }
|
|
503
|
|
504 } else {
|
78
|
505 if (p[0] == '+') {
|
76
|
506 return NGX_OK;
|
|
507 }
|
|
508 }
|
|
509 }
|
|
510
|
|
511 *(b->last - 2) = '\0';
|
|
512 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
|
|
513 "upstream sent invalid response: \"%s\"", p);
|
|
514
|
|
515 return NGX_IMAP_PROXY_INVALID;
|
|
516 }
|
|
517
|
|
518
|
|
519 static void
|
|
520 ngx_imap_proxy_handler(ngx_event_t *ev)
|
|
521 {
|
96
|
522 char *action;
|
88
|
523 size_t size;
|
|
524 ssize_t n;
|
|
525 ngx_buf_t *b;
|
|
526 ngx_uint_t again, do_write;
|
|
527 ngx_connection_t *c, *src, *dst;
|
|
528 ngx_imap_session_t *s;
|
|
529 ngx_imap_proxy_conf_t *pcf;
|
76
|
530
|
|
531 c = ev->data;
|
|
532 s = c->data;
|
|
533
|
|
534 if (ev->timedout) {
|
|
535 if (c == s->connection) {
|
|
536 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
537 "client timed out");
|
|
538 } else {
|
|
539 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
540 "upstream timed out");
|
|
541 }
|
|
542
|
|
543 ngx_imap_proxy_close_session(s);
|
|
544 return;
|
|
545 }
|
|
546
|
|
547 if (c == s->connection) {
|
|
548 if (ev->write) {
|
|
549 src = s->proxy->upstream.connection;
|
|
550 dst = c;
|
|
551 b = s->proxy->buffer;
|
|
552
|
|
553 } else {
|
|
554 src = c;
|
|
555 dst = s->proxy->upstream.connection;
|
|
556 b = s->buffer;
|
|
557 }
|
|
558
|
|
559 } else {
|
|
560 if (ev->write) {
|
|
561 src = s->connection;
|
|
562 dst = c;
|
|
563 b = s->buffer;
|
|
564
|
|
565 } else {
|
|
566 src = c;
|
|
567 dst = s->connection;
|
|
568 b = s->proxy->buffer;
|
|
569 }
|
|
570 }
|
|
571
|
|
572 do_write = ev->write ? 1 : 0;
|
|
573
|
|
574 ngx_log_debug3(NGX_LOG_DEBUG_IMAP, ev->log, 0,
|
|
575 "imap proxy handler: %d, #%d > #%d",
|
|
576 do_write, src->fd, dst->fd);
|
|
577
|
|
578 do {
|
|
579 again = 0;
|
|
580
|
|
581 if (do_write == 1) {
|
|
582
|
|
583 size = b->last - b->pos;
|
|
584
|
|
585 if (size && dst->write->ready) {
|
88
|
586 n = dst->send(dst, b->pos, size);
|
76
|
587
|
|
588 if (n == NGX_ERROR) {
|
|
589 ngx_imap_proxy_close_session(s);
|
|
590 return;
|
|
591 }
|
|
592
|
|
593 if (n > 0) {
|
|
594 again = 1;
|
|
595 b->pos += n;
|
|
596
|
|
597 if (b->pos == b->last) {
|
|
598 b->pos = b->start;
|
|
599 b->last = b->start;
|
|
600 }
|
|
601 }
|
|
602
|
|
603 if (n == NGX_AGAIN || n < (ssize_t) size) {
|
|
604 if (ngx_handle_write_event(dst->write, /* TODO: LOWAT */ 0)
|
78
|
605 == NGX_ERROR)
|
76
|
606 {
|
|
607 ngx_imap_proxy_close_session(s);
|
|
608 return;
|
|
609 }
|
|
610 }
|
|
611 }
|
|
612 }
|
|
613
|
|
614 size = b->end - b->last;
|
|
615
|
|
616 if (size && src->read->ready) {
|
88
|
617 n = src->recv(src, b->last, size);
|
76
|
618
|
96
|
619 if (n == NGX_ERROR) {
|
|
620 ngx_imap_proxy_close_session(s);
|
|
621 return;
|
|
622 }
|
|
623
|
|
624 if (n == 0) {
|
|
625 action = c->log->action;
|
|
626 c->log->action = NULL;
|
|
627 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
|
|
628 c->log->action = action;
|
|
629
|
76
|
630 ngx_imap_proxy_close_session(s);
|
|
631 return;
|
|
632 }
|
|
633
|
|
634 if (n > 0) {
|
|
635 again = 1;
|
|
636 do_write = 1;
|
|
637 b->last += n;
|
|
638 }
|
|
639
|
|
640 if (n == NGX_AGAIN || n < (ssize_t) size) {
|
|
641 if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) {
|
|
642 ngx_imap_proxy_close_session(s);
|
|
643 return;
|
|
644 }
|
|
645 }
|
88
|
646
|
|
647 if (c == s->connection) {
|
|
648 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
|
|
649 ngx_add_timer(c->read, pcf->timeout);
|
|
650 }
|
76
|
651 }
|
|
652
|
|
653 } while (again);
|
|
654 }
|
|
655
|
|
656
|
|
657 static void
|
|
658 ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s)
|
|
659 {
|
|
660 if (s->proxy->upstream.connection) {
|
|
661 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
|
|
662 "close imap proxy connection: %d",
|
|
663 s->proxy->upstream.connection->fd);
|
|
664
|
|
665 ngx_close_connection(s->proxy->upstream.connection);
|
|
666 }
|
|
667
|
|
668 ngx_imap_session_internal_server_error(s);
|
|
669 }
|
|
670
|
|
671
|
|
672 static void
|
|
673 ngx_imap_proxy_close_session(ngx_imap_session_t *s)
|
|
674 {
|
|
675 if (s->proxy->upstream.connection) {
|
|
676 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
|
|
677 "close imap proxy connection: %d",
|
|
678 s->proxy->upstream.connection->fd);
|
|
679
|
|
680 ngx_close_connection(s->proxy->upstream.connection);
|
|
681 }
|
|
682
|
|
683 ngx_imap_close_connection(s->connection);
|
|
684 }
|
|
685
|
|
686
|
|
687 static void *
|
|
688 ngx_imap_proxy_create_conf(ngx_conf_t *cf)
|
|
689 {
|
|
690 ngx_imap_proxy_conf_t *pcf;
|
|
691
|
|
692 pcf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_proxy_conf_t));
|
|
693 if (pcf == NULL) {
|
|
694 return NGX_CONF_ERROR;
|
|
695 }
|
|
696
|
|
697 pcf->enable = NGX_CONF_UNSET;
|
88
|
698 pcf->buffer_size = NGX_CONF_UNSET_SIZE;
|
|
699 pcf->timeout = NGX_CONF_UNSET_MSEC;
|
76
|
700
|
|
701 return pcf;
|
|
702 }
|
|
703
|
|
704
|
|
705 static char *
|
|
706 ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
707 {
|
|
708 ngx_imap_proxy_conf_t *prev = parent;
|
|
709 ngx_imap_proxy_conf_t *conf = child;
|
|
710
|
88
|
711 ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
|
712 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
|
|
713 (size_t) ngx_pagesize);
|
|
714 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000);
|
76
|
715
|
|
716 return NGX_CONF_OK;
|
|
717 }
|