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