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