Mercurial > hg > nginx-quic
annotate src/imap/ngx_imap_proxy_module.c @ 1111:b0fc4af1f196
close connection for too long commands
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 15 Feb 2007 13:30:19 +0000 |
parents | 4d68c486fcb0 |
children |
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 | |
884 | 97 ngx_imap_proxy_init(ngx_imap_session_t *s, ngx_peer_addr_t *peer) |
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 |
884 | 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; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
132 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
|
133 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
|
134 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
135 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
|
136 |
543 | 137 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) { |
138 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
|
139 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
140 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
141 |
527 | 142 ngx_add_timer(p->upstream.connection->read, cscf->timeout); |
143 | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
144 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
|
145 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
|
146 |
509 | 147 s->connection->read->handler = ngx_imap_proxy_block_read; |
148 p->upstream.connection->write->handler = ngx_imap_proxy_dummy_handler; | |
527 | 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 } | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
158 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
159 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
160 |
521 | 161 static void |
162 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
|
163 { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
164 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
|
165 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
|
166 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
167 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
|
168 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
169 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
|
170 c = rev->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
171 s = c->data; |
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 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
|
174 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
175 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
176 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
177 |
521 | 178 static void |
527 | 179 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
|
180 { |
539 | 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; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
187 |
527 | 188 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, |
189 "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
|
190 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
191 c = rev->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
192 s = c->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
193 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
194 if (rev->timedout) { |
527 | 195 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, |
196 "upstream timed out"); | |
577 | 197 c->timedout = 1; |
527 | 198 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
|
199 return; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
200 } |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
201 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
202 if (s->proxy->buffer == NULL) { |
539 | 203 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); |
527 | 204 |
539 | 205 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
|
206 if (s->proxy->buffer == NULL) { |
527 | 207 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
|
208 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
209 } |
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 |
663 | 212 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
|
213 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
214 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
|
215 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
216 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
217 |
571 | 218 if (rc == NGX_ERROR) { |
857 | 219 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
|
220 return; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
221 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
222 |
527 | 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"); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
228 |
587 | 229 s->connection->log->action = "sending LOGIN command to upstream"; |
230 | |
527 | 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: | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
247 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
|
248 |
587 | 249 s->connection->log->action = "sending user name to upstream"; |
250 | |
529 | 251 line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2; |
527 | 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 | |
529 | 258 line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF, |
527 | 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 | |
587 | 269 s->connection->log->action = "sending password to upstream"; |
270 | |
527 | 271 line.len = s->passwd.len + 2; |
501 | 272 line.data = ngx_palloc(c->pool, line.len); |
273 if (line.data == NULL) { | |
527 | 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 | |
663 | 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 | |
527 | 301 default: |
302 #if (NGX_SUPPRESS_WARN) | |
303 line.len = 0; | |
304 line.data = NULL; | |
305 #endif | |
306 break; | |
307 } | |
308 | |
539 | 309 if (c->send(c, line.data, line.len) < (ssize_t) line.len) { |
527 | 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 { | |
539 | 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; | |
527 | 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"); | |
577 | 342 c->timedout = 1; |
527 | 343 ngx_imap_proxy_internal_server_error(s); |
344 return; | |
345 } | |
346 | |
347 if (s->proxy->buffer == NULL) { | |
539 | 348 pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); |
527 | 349 |
539 | 350 s->proxy->buffer = ngx_create_temp_buf(c->pool, pcf->buffer_size); |
527 | 351 if (s->proxy->buffer == NULL) { |
352 ngx_imap_proxy_internal_server_error(s); | |
353 return; | |
354 } | |
355 } | |
356 | |
663 | 357 rc = ngx_imap_proxy_read_response(s, 0); |
527 | 358 |
359 if (rc == NGX_AGAIN) { | |
360 return; | |
361 } | |
362 | |
571 | 363 if (rc == NGX_ERROR) { |
857 | 364 ngx_imap_proxy_upstream_error(s); |
527 | 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 | |
587 | 373 s->connection->log->action = "sending user name to upstream"; |
374 | |
527 | 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); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
379 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
380 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
381 |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
382 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
|
383 p = ngx_cpymem(p, s->login.data, s->login.len); |
501 | 384 *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
|
385 |
527 | 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 | |
587 | 392 s->connection->log->action = "sending password to upstream"; |
393 | |
527 | 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); | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
398 return; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
399 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
400 |
527 | 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; | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
404 |
527 | 405 s->imap_state = ngx_pop3_passwd; |
406 break; | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
407 |
663 | 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 | |
527 | 425 default: |
426 #if (NGX_SUPPRESS_WARN) | |
427 line.len = 0; | |
428 line.data = NULL; | |
429 #endif | |
430 break; | |
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 |
539 | 433 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
|
434 /* |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
435 * 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
|
436 * 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
|
437 */ |
527 | 438 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
|
439 return; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
440 } |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
441 |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
442 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
|
443 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
|
444 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
445 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
446 |
521 | 447 static void |
583 | 448 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
|
449 { |
583 | 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 } | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
461 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
462 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
463 |
521 | 464 static ngx_int_t |
663 | 465 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
|
466 { |
857 | 467 u_char *p; |
468 ssize_t n; | |
469 ngx_buf_t *b; | |
470 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
|
471 |
587 | 472 s->connection->log->action = "reading response from upstream"; |
473 | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
474 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
|
475 |
539 | 476 n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection, |
477 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
|
478 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
479 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
|
480 return NGX_ERROR; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
481 } |
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 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
|
484 return NGX_AGAIN; |
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 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
487 b->last += n; |
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 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
|
490 return NGX_AGAIN; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
491 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
492 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
493 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
|
494 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
|
495 *(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
|
496 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
|
497 "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
|
498 b->pos); |
571 | 499 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
|
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 return NGX_AGAIN; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
503 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
504 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
505 p = b->pos; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
506 |
527 | 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 { | |
663 | 513 switch (state) { |
514 | |
515 case ngx_imap_start: | |
527 | 516 if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') { |
517 return NGX_OK; | |
518 } | |
663 | 519 break; |
527 | 520 |
663 | 521 case ngx_imap_login: |
522 case ngx_imap_user: | |
529 | 523 if (p[0] == '+') { |
527 | 524 return NGX_OK; |
525 } | |
663 | 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; | |
527 | 536 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
537 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
538 |
857 | 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; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
556 |
571 | 557 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
|
558 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
559 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
560 |
521 | 561 static void |
562 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
|
563 { |
583 | 564 char *action, *recv_action, *send_action; |
539 | 565 size_t size; |
566 ssize_t n; | |
567 ngx_buf_t *b; | |
583 | 568 ngx_uint_t do_write; |
539 | 569 ngx_connection_t *c, *src, *dst; |
570 ngx_imap_session_t *s; | |
571 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
|
572 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
573 c = ev->data; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
574 s = c->data; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
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 (ev->timedout) { |
583 | 577 c->log->action = "proxying"; |
578 | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
579 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
|
580 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
|
581 "client timed out"); |
577 | 582 c->timedout = 1; |
583 | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
584 } else { |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
585 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
|
586 "upstream timed out"); |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
587 } |
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 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
|
590 return; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
591 } |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
592 |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
593 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
|
594 if (ev->write) { |
583 | 595 recv_action = "proxying and reading from upstream"; |
596 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
|
597 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
|
598 dst = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
599 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
|
600 |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
601 } else { |
583 | 602 recv_action = "proxying and reading from client"; |
603 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
|
604 src = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
605 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
|
606 b = s->buffer; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
607 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
608 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
609 } else { |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
610 if (ev->write) { |
589 | 611 recv_action = "proxying and reading from client"; |
612 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
|
613 src = s->connection; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
614 dst = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
615 b = s->buffer; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
616 |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
617 } else { |
589 | 618 recv_action = "proxying and reading from upstream"; |
619 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
|
620 src = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
621 dst = s->connection; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
622 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
|
623 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
624 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
625 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
626 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
|
627 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
628 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
|
629 "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
|
630 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
|
631 |
583 | 632 for ( ;; ) { |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
633 |
583 | 634 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
|
635 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
636 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
|
637 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
638 if (size && dst->write->ready) { |
583 | 639 c->log->action = send_action; |
640 | |
539 | 641 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
|
642 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
643 if (n == NGX_ERROR) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
644 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
|
645 return; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
646 } |
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 (n > 0) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
649 b->pos += n; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
650 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
651 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
|
652 b->pos = b->start; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
653 b->last = b->start; |
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 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
656 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
657 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
658 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
659 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
|
660 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
661 if (size && src->read->ready) { |
583 | 662 c->log->action = recv_action; |
663 | |
539 | 664 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
|
665 |
583 | 666 if (n == NGX_AGAIN || n == 0) { |
667 break; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
668 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
669 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
670 if (n > 0) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
671 do_write = 1; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
672 b->last += n; |
583 | 673 |
674 continue; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
675 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
676 |
583 | 677 if (n == NGX_ERROR) { |
678 src->read->eof = 1; | |
539 | 679 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
680 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
681 |
583 | 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 } | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
724 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
725 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
726 |
521 | 727 static void |
857 | 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 | |
527 | 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 | |
521 | 764 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
|
765 { |
521 | 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); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
772 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
773 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
774 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
|
775 } |
521 | 776 |
777 | |
778 static void * | |
779 ngx_imap_proxy_create_conf(ngx_conf_t *cf) | |
577 | 780 { |
521 | 781 ngx_imap_proxy_conf_t *pcf; |
577 | 782 |
521 | 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; | |
857 | 789 pcf->pass_error_message = NGX_CONF_UNSET; |
539 | 790 pcf->buffer_size = NGX_CONF_UNSET_SIZE; |
791 pcf->timeout = NGX_CONF_UNSET_MSEC; | |
521 | 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 | |
539 | 803 ngx_conf_merge_value(conf->enable, prev->enable, 0); |
857 | 804 ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0); |
539 | 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); | |
521 | 808 |
809 return NGX_CONF_OK; | |
810 } |