Mercurial > hg > nginx-quic
annotate src/mail/ngx_mail_proxy_module.c @ 1908:f2953601ed3c
fix memory leak in long-lived non buffered connections
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 03 Mar 2008 10:42:00 +0000 |
parents | 057d362ee50e |
children | 65581f79e81b |
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> |
1136 | 11 #include <ngx_mail.h> |
418
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; |
1136 | 17 ngx_flag_t xclient; |
539 | 18 size_t buffer_size; |
19 ngx_msec_t timeout; | |
1136 | 20 } ngx_mail_proxy_conf_t; |
521 | 21 |
22 | |
1136 | 23 static void ngx_mail_proxy_block_read(ngx_event_t *rev); |
24 static void ngx_mail_proxy_pop3_handler(ngx_event_t *rev); | |
25 static void ngx_mail_proxy_imap_handler(ngx_event_t *rev); | |
26 static void ngx_mail_proxy_smtp_handler(ngx_event_t *rev); | |
27 static void ngx_mail_proxy_dummy_handler(ngx_event_t *ev); | |
28 static ngx_int_t ngx_mail_proxy_read_response(ngx_mail_session_t *s, | |
663 | 29 ngx_uint_t state); |
1136 | 30 static void ngx_mail_proxy_handler(ngx_event_t *ev); |
31 static void ngx_mail_proxy_upstream_error(ngx_mail_session_t *s); | |
32 static void ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s); | |
33 static void ngx_mail_proxy_close_session(ngx_mail_session_t *s); | |
34 static void *ngx_mail_proxy_create_conf(ngx_conf_t *cf); | |
35 static char *ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, | |
521 | 36 void *child); |
37 | |
38 | |
1136 | 39 static ngx_command_t ngx_mail_proxy_commands[] = { |
539 | 40 |
521 | 41 { ngx_string("proxy"), |
1136 | 42 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG, |
521 | 43 ngx_conf_set_flag_slot, |
1136 | 44 NGX_MAIL_SRV_CONF_OFFSET, |
45 offsetof(ngx_mail_proxy_conf_t, enable), | |
521 | 46 NULL }, |
47 | |
539 | 48 { ngx_string("proxy_buffer"), |
1136 | 49 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
539 | 50 ngx_conf_set_size_slot, |
1136 | 51 NGX_MAIL_SRV_CONF_OFFSET, |
52 offsetof(ngx_mail_proxy_conf_t, buffer_size), | |
539 | 53 NULL }, |
54 | |
55 { ngx_string("proxy_timeout"), | |
1136 | 56 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
539 | 57 ngx_conf_set_msec_slot, |
1136 | 58 NGX_MAIL_SRV_CONF_OFFSET, |
59 offsetof(ngx_mail_proxy_conf_t, timeout), | |
539 | 60 NULL }, |
61 | |
857 | 62 { ngx_string("proxy_pass_error_message"), |
1136 | 63 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
857 | 64 ngx_conf_set_flag_slot, |
1136 | 65 NGX_MAIL_SRV_CONF_OFFSET, |
66 offsetof(ngx_mail_proxy_conf_t, pass_error_message), | |
67 NULL }, | |
68 | |
69 { ngx_string("xclient"), | |
70 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG, | |
71 ngx_conf_set_flag_slot, | |
72 NGX_MAIL_SRV_CONF_OFFSET, | |
73 offsetof(ngx_mail_proxy_conf_t, xclient), | |
857 | 74 NULL }, |
75 | |
521 | 76 ngx_null_command |
77 }; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
78 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
79 |
1136 | 80 static ngx_mail_module_t ngx_mail_proxy_module_ctx = { |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1342
diff
changeset
|
81 NULL, /* protocol */ |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1342
diff
changeset
|
82 |
521 | 83 NULL, /* create main configuration */ |
84 NULL, /* init main configuration */ | |
85 | |
1136 | 86 ngx_mail_proxy_create_conf, /* create server configuration */ |
87 ngx_mail_proxy_merge_conf /* merge server configuration */ | |
521 | 88 }; |
89 | |
90 | |
1136 | 91 ngx_module_t ngx_mail_proxy_module = { |
521 | 92 NGX_MODULE_V1, |
1136 | 93 &ngx_mail_proxy_module_ctx, /* module context */ |
94 ngx_mail_proxy_commands, /* module directives */ | |
95 NGX_MAIL_MODULE, /* module type */ | |
541 | 96 NULL, /* init master */ |
521 | 97 NULL, /* init module */ |
541 | 98 NULL, /* init process */ |
99 NULL, /* init thread */ | |
100 NULL, /* exit thread */ | |
101 NULL, /* exit process */ | |
102 NULL, /* exit master */ | |
103 NGX_MODULE_V1_PADDING | |
521 | 104 }; |
105 | |
106 | |
1136 | 107 static u_char smtp_ok[] = "235 2.0.0 OK" CRLF; |
108 | |
109 | |
521 | 110 void |
1136 | 111 ngx_mail_proxy_init(ngx_mail_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
|
112 { |
587 | 113 int keepalive; |
527 | 114 ngx_int_t rc; |
1136 | 115 ngx_mail_proxy_ctx_t *p; |
116 ngx_mail_proxy_conf_t *pcf; | |
117 ngx_mail_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
|
118 |
587 | 119 s->connection->log->action = "connecting to upstream"; |
120 | |
1136 | 121 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); |
587 | 122 |
123 if (cscf->so_keepalive) { | |
124 keepalive = 1; | |
125 | |
126 if (setsockopt(s->connection->fd, SOL_SOCKET, SO_KEEPALIVE, | |
127 (const void *) &keepalive, sizeof(int)) | |
128 == -1) | |
129 { | |
130 ngx_log_error(NGX_LOG_ALERT, s->connection->log, ngx_socket_errno, | |
131 "setsockopt(SO_KEEPALIVE) failed"); | |
132 } | |
133 } | |
134 | |
1136 | 135 p = ngx_pcalloc(s->connection->pool, sizeof(ngx_mail_proxy_ctx_t)); |
501 | 136 if (p == NULL) { |
1136 | 137 ngx_mail_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
|
138 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
139 } |
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 s->proxy = p; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
142 |
884 | 143 p->upstream.sockaddr = peer->sockaddr; |
144 p->upstream.socklen = peer->socklen; | |
145 p->upstream.name = &peer->name; | |
146 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
|
147 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
|
148 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
|
149 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
150 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
|
151 |
543 | 152 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) { |
1136 | 153 ngx_mail_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
|
154 return; |
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 |
527 | 157 ngx_add_timer(p->upstream.connection->read, cscf->timeout); |
158 | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
159 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
|
160 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
|
161 |
1136 | 162 s->connection->read->handler = ngx_mail_proxy_block_read; |
163 p->upstream.connection->write->handler = ngx_mail_proxy_dummy_handler; | |
164 | |
165 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
166 | |
167 s->proxy->buffer = ngx_create_temp_buf(s->connection->pool, | |
168 pcf->buffer_size); | |
169 if (s->proxy->buffer == NULL) { | |
170 ngx_mail_proxy_internal_server_error(s); | |
171 return; | |
172 } | |
173 | |
174 switch (s->protocol) { | |
527 | 175 |
1136 | 176 case NGX_MAIL_POP3_PROTOCOL: |
177 p->upstream.connection->read->handler = ngx_mail_proxy_pop3_handler; | |
178 s->mail_state = ngx_pop3_start; | |
179 break; | |
527 | 180 |
1136 | 181 case NGX_MAIL_IMAP_PROTOCOL: |
182 p->upstream.connection->read->handler = ngx_mail_proxy_imap_handler; | |
183 s->mail_state = ngx_imap_start; | |
184 break; | |
185 | |
186 default: /* NGX_MAIL_SMTP_PROTOCOL */ | |
187 p->upstream.connection->read->handler = ngx_mail_proxy_smtp_handler; | |
188 s->mail_state = ngx_smtp_start; | |
189 break; | |
527 | 190 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
191 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
192 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
193 |
521 | 194 static void |
1136 | 195 ngx_mail_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
|
196 { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
197 ngx_connection_t *c; |
1136 | 198 ngx_mail_session_t *s; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
199 |
1136 | 200 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy block read"); |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
201 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
202 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
|
203 c = rev->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
204 s = c->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
205 |
1136 | 206 ngx_mail_proxy_close_session(s); |
419
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 } |
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 |
521 | 211 static void |
1136 | 212 ngx_mail_proxy_pop3_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
|
213 { |
539 | 214 u_char *p; |
215 ngx_int_t rc; | |
216 ngx_str_t line; | |
217 ngx_connection_t *c; | |
1136 | 218 ngx_mail_session_t *s; |
219 ngx_mail_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
|
220 |
1136 | 221 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, |
222 "mail proxy pop3 auth handler"); | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
223 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
224 c = rev->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
225 s = c->data; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
226 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
227 if (rev->timedout) { |
527 | 228 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, |
229 "upstream timed out"); | |
577 | 230 c->timedout = 1; |
1136 | 231 ngx_mail_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
|
232 return; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
233 } |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
234 |
1136 | 235 rc = ngx_mail_proxy_read_response(s, 0); |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
236 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
237 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
|
238 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
239 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
240 |
571 | 241 if (rc == NGX_ERROR) { |
1136 | 242 ngx_mail_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
|
243 return; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
244 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
245 |
1136 | 246 switch (s->mail_state) { |
587 | 247 |
1136 | 248 case ngx_pop3_start: |
249 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user"); | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
250 |
587 | 251 s->connection->log->action = "sending user name to upstream"; |
252 | |
1136 | 253 line.len = sizeof("USER ") - 1 + s->login.len + 2; |
527 | 254 line.data = ngx_palloc(c->pool, line.len); |
255 if (line.data == NULL) { | |
1136 | 256 ngx_mail_proxy_internal_server_error(s); |
527 | 257 return; |
258 } | |
259 | |
1136 | 260 p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1); |
261 p = ngx_cpymem(p, s->login.data, s->login.len); | |
262 *p++ = CR; *p = LF; | |
527 | 263 |
1136 | 264 s->mail_state = ngx_pop3_user; |
527 | 265 break; |
266 | |
1136 | 267 case ngx_pop3_user: |
268 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send pass"); | |
527 | 269 |
587 | 270 s->connection->log->action = "sending password to upstream"; |
271 | |
1136 | 272 line.len = sizeof("PASS ") - 1 + s->passwd.len + 2; |
501 | 273 line.data = ngx_palloc(c->pool, line.len); |
274 if (line.data == NULL) { | |
1136 | 275 ngx_mail_proxy_internal_server_error(s); |
527 | 276 return; |
277 } | |
278 | |
1136 | 279 p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1); |
280 p = ngx_cpymem(p, s->passwd.data, s->passwd.len); | |
527 | 281 *p++ = CR; *p = LF; |
282 | |
1136 | 283 s->mail_state = ngx_pop3_passwd; |
527 | 284 break; |
285 | |
1136 | 286 case ngx_pop3_passwd: |
287 s->connection->read->handler = ngx_mail_proxy_handler; | |
288 s->connection->write->handler = ngx_mail_proxy_handler; | |
289 rev->handler = ngx_mail_proxy_handler; | |
290 c->write->handler = ngx_mail_proxy_handler; | |
663 | 291 |
1136 | 292 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); |
663 | 293 ngx_add_timer(s->connection->read, pcf->timeout); |
294 ngx_del_timer(c->read); | |
295 | |
296 c->log->action = NULL; | |
297 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); | |
298 | |
1136 | 299 ngx_mail_proxy_handler(s->connection->write); |
663 | 300 |
301 return; | |
302 | |
527 | 303 default: |
304 #if (NGX_SUPPRESS_WARN) | |
305 line.len = 0; | |
306 line.data = NULL; | |
307 #endif | |
308 break; | |
309 } | |
310 | |
539 | 311 if (c->send(c, line.data, line.len) < (ssize_t) line.len) { |
527 | 312 /* |
313 * we treat the incomplete sending as NGX_ERROR | |
314 * because it is very strange here | |
315 */ | |
1136 | 316 ngx_mail_proxy_internal_server_error(s); |
527 | 317 return; |
318 } | |
319 | |
320 s->proxy->buffer->pos = s->proxy->buffer->start; | |
321 s->proxy->buffer->last = s->proxy->buffer->start; | |
322 } | |
323 | |
324 | |
325 static void | |
1136 | 326 ngx_mail_proxy_imap_handler(ngx_event_t *rev) |
527 | 327 { |
539 | 328 u_char *p; |
329 ngx_int_t rc; | |
330 ngx_str_t line; | |
331 ngx_connection_t *c; | |
1136 | 332 ngx_mail_session_t *s; |
333 ngx_mail_proxy_conf_t *pcf; | |
527 | 334 |
1136 | 335 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, |
336 "mail proxy imap auth handler"); | |
527 | 337 |
338 c = rev->data; | |
339 s = c->data; | |
340 | |
341 if (rev->timedout) { | |
342 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, | |
343 "upstream timed out"); | |
577 | 344 c->timedout = 1; |
1136 | 345 ngx_mail_proxy_internal_server_error(s); |
527 | 346 return; |
347 } | |
348 | |
1136 | 349 rc = ngx_mail_proxy_read_response(s, s->mail_state); |
527 | 350 |
351 if (rc == NGX_AGAIN) { | |
352 return; | |
353 } | |
354 | |
571 | 355 if (rc == NGX_ERROR) { |
1136 | 356 ngx_mail_proxy_upstream_error(s); |
527 | 357 return; |
358 } | |
359 | |
1136 | 360 switch (s->mail_state) { |
527 | 361 |
1136 | 362 case ngx_imap_start: |
363 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
364 "mail proxy send login"); | |
527 | 365 |
1136 | 366 s->connection->log->action = "sending LOGIN command to upstream"; |
587 | 367 |
1136 | 368 line.len = s->tag.len + sizeof("LOGIN ") - 1 |
369 + 1 + NGX_SIZE_T_LEN + 1 + 2; | |
527 | 370 line.data = ngx_palloc(c->pool, line.len); |
371 if (line.data == NULL) { | |
1136 | 372 ngx_mail_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
|
373 return; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
374 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
375 |
1136 | 376 line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF, |
377 &s->tag, s->login.len) | |
378 - line.data; | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
379 |
1136 | 380 s->mail_state = ngx_imap_login; |
527 | 381 break; |
382 | |
1136 | 383 case ngx_imap_login: |
384 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user"); | |
385 | |
386 s->connection->log->action = "sending user name to upstream"; | |
387 | |
388 line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2; | |
389 line.data = ngx_palloc(c->pool, line.len); | |
390 if (line.data == NULL) { | |
391 ngx_mail_proxy_internal_server_error(s); | |
392 return; | |
393 } | |
394 | |
395 line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF, | |
396 &s->login, s->passwd.len) | |
397 - line.data; | |
398 | |
399 s->mail_state = ngx_imap_user; | |
400 break; | |
401 | |
402 case ngx_imap_user: | |
403 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
404 "mail proxy send passwd"); | |
527 | 405 |
587 | 406 s->connection->log->action = "sending password to upstream"; |
407 | |
1136 | 408 line.len = s->passwd.len + 2; |
527 | 409 line.data = ngx_palloc(c->pool, line.len); |
410 if (line.data == NULL) { | |
1136 | 411 ngx_mail_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
|
412 return; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
413 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
414 |
1136 | 415 p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len); |
527 | 416 *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
|
417 |
1136 | 418 s->mail_state = ngx_imap_passwd; |
527 | 419 break; |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
420 |
1136 | 421 case ngx_imap_passwd: |
422 s->connection->read->handler = ngx_mail_proxy_handler; | |
423 s->connection->write->handler = ngx_mail_proxy_handler; | |
424 rev->handler = ngx_mail_proxy_handler; | |
425 c->write->handler = ngx_mail_proxy_handler; | |
663 | 426 |
1136 | 427 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); |
663 | 428 ngx_add_timer(s->connection->read, pcf->timeout); |
429 ngx_del_timer(c->read); | |
430 | |
431 c->log->action = NULL; | |
432 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); | |
433 | |
1136 | 434 ngx_mail_proxy_handler(s->connection->write); |
663 | 435 |
436 return; | |
437 | |
527 | 438 default: |
439 #if (NGX_SUPPRESS_WARN) | |
440 line.len = 0; | |
441 line.data = NULL; | |
442 #endif | |
443 break; | |
422
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
444 } |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
445 |
539 | 446 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
|
447 /* |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
448 * 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
|
449 * 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
|
450 */ |
1136 | 451 ngx_mail_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
|
452 return; |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
453 } |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
454 |
edaefb2a20fc
nginx-0.0.10-2004-09-12-00:22:11 import
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
455 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
|
456 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
|
457 } |
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 |
521 | 460 static void |
1136 | 461 ngx_mail_proxy_smtp_handler(ngx_event_t *rev) |
462 { | |
463 u_char *p; | |
464 ngx_int_t rc; | |
465 ngx_str_t line; | |
466 ngx_connection_t *c; | |
467 ngx_mail_session_t *s; | |
468 ngx_mail_proxy_conf_t *pcf; | |
469 ngx_mail_core_srv_conf_t *cscf; | |
470 | |
471 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
472 "mail proxy smtp auth handler"); | |
473 | |
474 c = rev->data; | |
475 s = c->data; | |
476 | |
477 if (rev->timedout) { | |
478 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, | |
479 "upstream timed out"); | |
480 c->timedout = 1; | |
481 ngx_mail_proxy_internal_server_error(s); | |
482 return; | |
483 } | |
484 | |
485 rc = ngx_mail_proxy_read_response(s, s->mail_state); | |
486 | |
487 if (rc == NGX_AGAIN) { | |
488 return; | |
489 } | |
490 | |
491 if (rc == NGX_ERROR) { | |
492 ngx_mail_proxy_upstream_error(s); | |
493 return; | |
494 } | |
495 | |
496 switch (s->mail_state) { | |
497 | |
498 case ngx_smtp_start: | |
499 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send ehlo"); | |
500 | |
501 s->connection->log->action = "sending HELO/EHLO to upstream"; | |
502 | |
503 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); | |
504 | |
505 line.len = sizeof("HELO ") - 1 + cscf->server_name.len + 2; | |
506 line.data = ngx_palloc(c->pool, line.len); | |
507 if (line.data == NULL) { | |
508 ngx_mail_proxy_internal_server_error(s); | |
509 return; | |
510 } | |
511 | |
512 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
513 | |
514 p = ngx_cpymem(line.data, | |
515 ((s->esmtp || pcf->xclient) ? "EHLO " : "HELO "), | |
516 sizeof("HELO ") - 1); | |
517 | |
518 p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len); | |
519 *p++ = CR; *p = LF; | |
520 | |
521 s->mail_state = pcf->xclient ? ngx_smtp_helo: ngx_smtp_noxclient; | |
522 | |
523 break; | |
524 | |
525 case ngx_smtp_helo: | |
526 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
527 "mail proxy send xclient"); | |
528 | |
529 s->connection->log->action = "sending XCLIENT to upstream"; | |
530 | |
1892
057d362ee50e
resolver in smtp proxy module
Igor Sysoev <igor@sysoev.ru>
parents:
1487
diff
changeset
|
531 line.len = sizeof("XCLIENT PROTO=SMTP HELO= ADDR= LOGIN= NAME=" |
057d362ee50e
resolver in smtp proxy module
Igor Sysoev <igor@sysoev.ru>
parents:
1487
diff
changeset
|
532 CRLF) - 1 |
1136 | 533 + s->esmtp + s->smtp_helo.len |
1892
057d362ee50e
resolver in smtp proxy module
Igor Sysoev <igor@sysoev.ru>
parents:
1487
diff
changeset
|
534 + s->connection->addr_text.len + s->login.len + s->host.len; |
1136 | 535 |
536 line.data = ngx_palloc(c->pool, line.len); | |
537 if (line.data == NULL) { | |
538 ngx_mail_proxy_internal_server_error(s); | |
539 return; | |
540 } | |
541 | |
542 if (s->smtp_helo.len) { | |
543 line.len = ngx_sprintf(line.data, | |
544 "XCLIENT PROTO=%sSMTP HELO=%V ADDR=%V LOGIN=%V " | |
1892
057d362ee50e
resolver in smtp proxy module
Igor Sysoev <igor@sysoev.ru>
parents:
1487
diff
changeset
|
545 "NAME=%V" CRLF, |
1136 | 546 (s->esmtp ? "E" : ""), &s->smtp_helo, |
1892
057d362ee50e
resolver in smtp proxy module
Igor Sysoev <igor@sysoev.ru>
parents:
1487
diff
changeset
|
547 &s->connection->addr_text, &s->login, &s->host) |
1136 | 548 - line.data; |
549 } else { | |
550 line.len = ngx_sprintf(line.data, | |
1892
057d362ee50e
resolver in smtp proxy module
Igor Sysoev <igor@sysoev.ru>
parents:
1487
diff
changeset
|
551 "XCLIENT PROTO=SMTP ADDR=%V LOGIN=%V NAME=%V" CRLF, |
057d362ee50e
resolver in smtp proxy module
Igor Sysoev <igor@sysoev.ru>
parents:
1487
diff
changeset
|
552 &s->connection->addr_text, &s->login, &s->host) |
1136 | 553 - line.data; |
554 } | |
555 | |
556 s->mail_state = ngx_smtp_xclient; | |
557 break; | |
558 | |
559 case ngx_smtp_noxclient: | |
560 case ngx_smtp_xclient: | |
561 | |
562 ngx_memcpy(s->proxy->buffer->start, smtp_ok, sizeof(smtp_ok) - 1); | |
563 | |
564 s->proxy->buffer->pos = s->proxy->buffer->start; | |
565 s->proxy->buffer->last = s->proxy->buffer->start + sizeof(smtp_ok) - 1; | |
566 | |
567 s->connection->read->handler = ngx_mail_proxy_handler; | |
568 s->connection->write->handler = ngx_mail_proxy_handler; | |
569 rev->handler = ngx_mail_proxy_handler; | |
570 c->write->handler = ngx_mail_proxy_handler; | |
571 | |
572 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
573 ngx_add_timer(s->connection->read, pcf->timeout); | |
574 ngx_del_timer(c->read); | |
575 | |
576 c->log->action = NULL; | |
577 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); | |
578 | |
579 ngx_mail_proxy_handler(s->connection->write); | |
580 | |
581 return; | |
582 | |
583 default: | |
584 #if (NGX_SUPPRESS_WARN) | |
585 line.len = 0; | |
586 line.data = NULL; | |
587 #endif | |
588 break; | |
589 } | |
590 | |
591 if (c->send(c, line.data, line.len) < (ssize_t) line.len) { | |
592 /* | |
593 * we treat the incomplete sending as NGX_ERROR | |
594 * because it is very strange here | |
595 */ | |
596 ngx_mail_proxy_internal_server_error(s); | |
597 return; | |
598 } | |
599 | |
600 s->proxy->buffer->pos = s->proxy->buffer->start; | |
601 s->proxy->buffer->last = s->proxy->buffer->start; | |
602 } | |
603 | |
604 | |
605 static void | |
606 ngx_mail_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
|
607 { |
583 | 608 ngx_connection_t *c; |
1136 | 609 ngx_mail_session_t *s; |
583 | 610 |
1136 | 611 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy dummy handler"); |
583 | 612 |
613 if (ngx_handle_write_event(wev, 0) == NGX_ERROR) { | |
614 c = wev->data; | |
615 s = c->data; | |
616 | |
1136 | 617 ngx_mail_proxy_close_session(s); |
583 | 618 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
619 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
620 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
621 |
521 | 622 static ngx_int_t |
1136 | 623 ngx_mail_proxy_read_response(ngx_mail_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
|
624 { |
857 | 625 u_char *p; |
626 ssize_t n; | |
627 ngx_buf_t *b; | |
1136 | 628 ngx_mail_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
|
629 |
587 | 630 s->connection->log->action = "reading response from upstream"; |
631 | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
632 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
|
633 |
539 | 634 n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection, |
635 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
|
636 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
637 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
|
638 return NGX_ERROR; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
639 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
640 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
641 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
|
642 return NGX_AGAIN; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
643 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
644 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
645 b->last += n; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
646 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
647 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
|
648 return NGX_AGAIN; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
649 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
650 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
651 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
|
652 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
|
653 *(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
|
654 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
|
655 "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
|
656 b->pos); |
571 | 657 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
|
658 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
659 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
660 return NGX_AGAIN; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
661 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
662 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
663 p = b->pos; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
664 |
1136 | 665 switch (s->protocol) { |
666 | |
667 case NGX_MAIL_POP3_PROTOCOL: | |
527 | 668 if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') { |
669 return NGX_OK; | |
670 } | |
1136 | 671 break; |
527 | 672 |
1136 | 673 case NGX_MAIL_IMAP_PROTOCOL: |
663 | 674 switch (state) { |
675 | |
676 case ngx_imap_start: | |
527 | 677 if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') { |
678 return NGX_OK; | |
679 } | |
663 | 680 break; |
527 | 681 |
663 | 682 case ngx_imap_login: |
683 case ngx_imap_user: | |
529 | 684 if (p[0] == '+') { |
527 | 685 return NGX_OK; |
686 } | |
663 | 687 break; |
688 | |
689 case ngx_imap_passwd: | |
690 if (ngx_strncmp(p, s->tag.data, s->tag.len) == 0) { | |
691 p += s->tag.len; | |
692 if (p[0] == 'O' && p[1] == 'K') { | |
693 return NGX_OK; | |
694 } | |
695 } | |
696 break; | |
527 | 697 } |
1136 | 698 |
699 break; | |
700 | |
701 default: /* NGX_MAIL_SMTP_PROTOCOL */ | |
702 switch (state) { | |
703 | |
704 case ngx_smtp_helo: | |
705 case ngx_smtp_noxclient: | |
1166 | 706 if (p[0] == '2' && p[1] == '5' && p[2] == '0') { |
707 return NGX_OK; | |
708 } | |
709 break; | |
1136 | 710 |
711 case ngx_smtp_start: | |
712 case ngx_smtp_xclient: | |
1166 | 713 if (p[0] == '2' && p[1] == '2' && p[2] == '0') { |
714 return NGX_OK; | |
715 } | |
716 break; | |
1136 | 717 } |
718 | |
1166 | 719 break; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
720 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
721 |
1136 | 722 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); |
857 | 723 |
724 if (pcf->pass_error_message == 0) { | |
725 *(b->last - 2) = '\0'; | |
726 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
727 "upstream sent invalid response: \"%s\"", p); | |
728 return NGX_ERROR; | |
729 } | |
730 | |
731 s->out.len = b->last - p - 2; | |
732 s->out.data = p; | |
733 | |
734 ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, | |
735 "upstream sent invalid response: \"%V\"", &s->out); | |
736 | |
737 s->out.len = b->last - b->pos; | |
738 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
|
739 |
571 | 740 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
|
741 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
742 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
743 |
521 | 744 static void |
1136 | 745 ngx_mail_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
|
746 { |
583 | 747 char *action, *recv_action, *send_action; |
539 | 748 size_t size; |
749 ssize_t n; | |
750 ngx_buf_t *b; | |
583 | 751 ngx_uint_t do_write; |
539 | 752 ngx_connection_t *c, *src, *dst; |
1136 | 753 ngx_mail_session_t *s; |
754 ngx_mail_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
|
755 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
756 c = ev->data; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
757 s = c->data; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
758 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
759 if (ev->timedout) { |
583 | 760 c->log->action = "proxying"; |
761 | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
762 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
|
763 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
|
764 "client timed out"); |
577 | 765 c->timedout = 1; |
766 | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
767 } else { |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
768 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
|
769 "upstream timed out"); |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
770 } |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
771 |
1136 | 772 ngx_mail_proxy_close_session(s); |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
773 return; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
774 } |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
775 |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
776 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
|
777 if (ev->write) { |
583 | 778 recv_action = "proxying and reading from upstream"; |
779 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
|
780 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
|
781 dst = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
782 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
|
783 |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
784 } else { |
583 | 785 recv_action = "proxying and reading from client"; |
786 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
|
787 src = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
788 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
|
789 b = s->buffer; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
790 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
791 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
792 } else { |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
793 if (ev->write) { |
589 | 794 recv_action = "proxying and reading from client"; |
795 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
|
796 src = s->connection; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
797 dst = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
798 b = s->buffer; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
799 |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
800 } else { |
589 | 801 recv_action = "proxying and reading from upstream"; |
802 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
|
803 src = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
804 dst = s->connection; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
805 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
|
806 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
807 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
808 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
809 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
|
810 |
1136 | 811 ngx_log_debug3(NGX_LOG_DEBUG_MAIL, ev->log, 0, |
812 "mail proxy handler: %d, #%d > #%d", | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
813 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
|
814 |
583 | 815 for ( ;; ) { |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
816 |
583 | 817 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
|
818 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
819 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
|
820 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
821 if (size && dst->write->ready) { |
583 | 822 c->log->action = send_action; |
823 | |
539 | 824 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
|
825 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
826 if (n == NGX_ERROR) { |
1136 | 827 ngx_mail_proxy_close_session(s); |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
828 return; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
829 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
830 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
831 if (n > 0) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
832 b->pos += n; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
833 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
834 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
|
835 b->pos = b->start; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
836 b->last = b->start; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
837 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
838 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
839 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
840 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
841 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
842 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
|
843 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
844 if (size && src->read->ready) { |
583 | 845 c->log->action = recv_action; |
846 | |
539 | 847 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
|
848 |
583 | 849 if (n == NGX_AGAIN || n == 0) { |
850 break; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
851 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
852 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
853 if (n > 0) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
854 do_write = 1; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
855 b->last += n; |
583 | 856 |
857 continue; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
858 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
859 |
583 | 860 if (n == NGX_ERROR) { |
861 src->read->eof = 1; | |
539 | 862 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
863 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
864 |
583 | 865 break; |
866 } | |
867 | |
868 c->log->action = "proxying"; | |
869 | |
1342
be2e13691c60
fix case when client has closed connection but upstream buffer is not empty
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
870 if ((s->connection->read->eof && s->buffer->pos == s->buffer->last) |
be2e13691c60
fix case when client has closed connection but upstream buffer is not empty
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
871 || (s->proxy->upstream.connection->read->eof |
be2e13691c60
fix case when client has closed connection but upstream buffer is not empty
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
872 && s->proxy->buffer->pos == s->proxy->buffer->last) |
be2e13691c60
fix case when client has closed connection but upstream buffer is not empty
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
873 || (s->connection->read->eof |
be2e13691c60
fix case when client has closed connection but upstream buffer is not empty
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
874 && s->proxy->upstream.connection->read->eof)) |
583 | 875 { |
876 action = c->log->action; | |
877 c->log->action = NULL; | |
878 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done"); | |
879 c->log->action = action; | |
880 | |
1136 | 881 ngx_mail_proxy_close_session(s); |
583 | 882 return; |
883 } | |
884 | |
885 if (ngx_handle_write_event(dst->write, 0) == NGX_ERROR) { | |
1136 | 886 ngx_mail_proxy_close_session(s); |
583 | 887 return; |
888 } | |
889 | |
890 if (ngx_handle_read_event(dst->read, 0) == NGX_ERROR) { | |
1136 | 891 ngx_mail_proxy_close_session(s); |
583 | 892 return; |
893 } | |
894 | |
895 if (ngx_handle_write_event(src->write, 0) == NGX_ERROR) { | |
1136 | 896 ngx_mail_proxy_close_session(s); |
583 | 897 return; |
898 } | |
899 | |
900 if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) { | |
1136 | 901 ngx_mail_proxy_close_session(s); |
583 | 902 return; |
903 } | |
904 | |
905 if (c == s->connection) { | |
1136 | 906 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); |
583 | 907 ngx_add_timer(c->read, pcf->timeout); |
908 } | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
909 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
910 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
911 |
521 | 912 static void |
1136 | 913 ngx_mail_proxy_upstream_error(ngx_mail_session_t *s) |
857 | 914 { |
915 if (s->proxy->upstream.connection) { | |
1136 | 916 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
917 "close mail proxy connection: %d", | |
857 | 918 s->proxy->upstream.connection->fd); |
919 | |
920 ngx_close_connection(s->proxy->upstream.connection); | |
921 } | |
922 | |
923 if (s->out.len == 0) { | |
1136 | 924 ngx_mail_session_internal_server_error(s); |
857 | 925 return; |
926 } | |
927 | |
928 s->quit = 1; | |
1136 | 929 ngx_mail_send(s->connection->write); |
857 | 930 } |
931 | |
932 | |
933 static void | |
1136 | 934 ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s) |
527 | 935 { |
936 if (s->proxy->upstream.connection) { | |
1136 | 937 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
938 "close mail proxy connection: %d", | |
527 | 939 s->proxy->upstream.connection->fd); |
940 | |
941 ngx_close_connection(s->proxy->upstream.connection); | |
942 } | |
943 | |
1136 | 944 ngx_mail_session_internal_server_error(s); |
527 | 945 } |
946 | |
947 | |
948 static void | |
1136 | 949 ngx_mail_proxy_close_session(ngx_mail_session_t *s) |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
950 { |
521 | 951 if (s->proxy->upstream.connection) { |
1136 | 952 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
953 "close mail proxy connection: %d", | |
521 | 954 s->proxy->upstream.connection->fd); |
955 | |
956 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
|
957 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
958 |
1136 | 959 ngx_mail_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
|
960 } |
521 | 961 |
962 | |
963 static void * | |
1136 | 964 ngx_mail_proxy_create_conf(ngx_conf_t *cf) |
577 | 965 { |
1136 | 966 ngx_mail_proxy_conf_t *pcf; |
577 | 967 |
1136 | 968 pcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_proxy_conf_t)); |
521 | 969 if (pcf == NULL) { |
970 return NGX_CONF_ERROR; | |
971 } | |
972 | |
973 pcf->enable = NGX_CONF_UNSET; | |
857 | 974 pcf->pass_error_message = NGX_CONF_UNSET; |
1136 | 975 pcf->xclient = NGX_CONF_UNSET; |
539 | 976 pcf->buffer_size = NGX_CONF_UNSET_SIZE; |
977 pcf->timeout = NGX_CONF_UNSET_MSEC; | |
521 | 978 |
979 return pcf; | |
980 } | |
981 | |
982 | |
983 static char * | |
1136 | 984 ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child) |
521 | 985 { |
1136 | 986 ngx_mail_proxy_conf_t *prev = parent; |
987 ngx_mail_proxy_conf_t *conf = child; | |
521 | 988 |
539 | 989 ngx_conf_merge_value(conf->enable, prev->enable, 0); |
857 | 990 ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0); |
1136 | 991 ngx_conf_merge_value(conf->xclient, prev->xclient, 1); |
539 | 992 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, |
993 (size_t) ngx_pagesize); | |
994 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000); | |
521 | 995 |
996 return NGX_CONF_OK; | |
997 } |