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