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