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