Mercurial > hg > nginx
annotate src/mail/ngx_mail_proxy_module.c @ 3496:fb88801564de
build nginx/Windows against OpenSSL-0.9.8n, since nginx-0.8.35
built against OpenSSL-1.0.0 crashed on Windows XP somewhere
in the NTDLL.DLL on the master process exit
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 21 Apr 2010 15:09:17 +0000 |
parents | f0d596e84634 |
children | c631ef8beaaa |
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 | |
2440
939b40aa9ab4
update r2439: make clear name
Igor Sysoev <igor@sysoev.ru>
parents:
2438
diff
changeset
|
107 static u_char smtp_auth_ok[] = "235 2.0.0 OK" CRLF; |
1136 | 108 |
109 | |
521 | 110 void |
3269
f0d596e84634
rename ngx_peer_addr_t to ngx_addr_t
Igor Sysoev <igor@sysoev.ru>
parents:
2912
diff
changeset
|
111 ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_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 |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2311
diff
changeset
|
204 if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
419
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; | |
2309 | 468 ngx_buf_t *b; |
1136 | 469 ngx_connection_t *c; |
470 ngx_mail_session_t *s; | |
471 ngx_mail_proxy_conf_t *pcf; | |
472 ngx_mail_core_srv_conf_t *cscf; | |
473 | |
474 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
475 "mail proxy smtp auth handler"); | |
476 | |
477 c = rev->data; | |
478 s = c->data; | |
479 | |
480 if (rev->timedout) { | |
481 ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, | |
482 "upstream timed out"); | |
483 c->timedout = 1; | |
484 ngx_mail_proxy_internal_server_error(s); | |
485 return; | |
486 } | |
487 | |
488 rc = ngx_mail_proxy_read_response(s, s->mail_state); | |
489 | |
490 if (rc == NGX_AGAIN) { | |
491 return; | |
492 } | |
493 | |
494 if (rc == NGX_ERROR) { | |
495 ngx_mail_proxy_upstream_error(s); | |
496 return; | |
497 } | |
498 | |
499 switch (s->mail_state) { | |
500 | |
501 case ngx_smtp_start: | |
502 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send ehlo"); | |
503 | |
504 s->connection->log->action = "sending HELO/EHLO to upstream"; | |
505 | |
506 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); | |
507 | |
508 line.len = sizeof("HELO ") - 1 + cscf->server_name.len + 2; | |
2049 | 509 line.data = ngx_pnalloc(c->pool, line.len); |
1136 | 510 if (line.data == NULL) { |
511 ngx_mail_proxy_internal_server_error(s); | |
512 return; | |
513 } | |
514 | |
515 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
516 | |
517 p = ngx_cpymem(line.data, | |
518 ((s->esmtp || pcf->xclient) ? "EHLO " : "HELO "), | |
519 sizeof("HELO ") - 1); | |
520 | |
521 p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len); | |
522 *p++ = CR; *p = LF; | |
523 | |
2309 | 524 if (pcf->xclient) { |
525 s->mail_state = ngx_smtp_helo_xclient; | |
526 | |
527 } else if (s->auth_method == NGX_MAIL_AUTH_NONE) { | |
528 s->mail_state = ngx_smtp_helo_from; | |
529 | |
530 } else { | |
531 s->mail_state = ngx_smtp_helo; | |
532 } | |
1136 | 533 |
534 break; | |
535 | |
2309 | 536 case ngx_smtp_helo_xclient: |
1136 | 537 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, |
538 "mail proxy send xclient"); | |
539 | |
540 s->connection->log->action = "sending XCLIENT to upstream"; | |
541 | |
2497 | 542 line.len = sizeof("XCLIENT ADDR= LOGIN= NAME=" |
1892
057d362ee50e
resolver in smtp proxy module
Igor Sysoev <igor@sysoev.ru>
parents:
1487
diff
changeset
|
543 CRLF) - 1 |
057d362ee50e
resolver in smtp proxy module
Igor Sysoev <igor@sysoev.ru>
parents:
1487
diff
changeset
|
544 + s->connection->addr_text.len + s->login.len + s->host.len; |
1136 | 545 |
2049 | 546 line.data = ngx_pnalloc(c->pool, line.len); |
1136 | 547 if (line.data == NULL) { |
548 ngx_mail_proxy_internal_server_error(s); | |
549 return; | |
550 } | |
551 | |
2309 | 552 line.len = ngx_sprintf(line.data, |
2497 | 553 "XCLIENT ADDR=%V%s%V NAME=%V" CRLF, |
2309 | 554 &s->connection->addr_text, |
555 (s->login.len ? " LOGIN=" : ""), &s->login, &s->host) | |
556 - line.data; | |
557 | |
2497 | 558 if (s->smtp_helo.len) { |
559 s->mail_state = ngx_smtp_xclient_helo; | |
560 | |
561 } else if (s->auth_method == NGX_MAIL_AUTH_NONE) { | |
562 s->mail_state = ngx_smtp_xclient_from; | |
563 | |
564 } else { | |
565 s->mail_state = ngx_smtp_xclient; | |
566 } | |
567 | |
568 break; | |
569 | |
570 case ngx_smtp_xclient_helo: | |
571 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
572 "mail proxy send client ehlo"); | |
573 | |
574 s->connection->log->action = "sending client HELO/EHLO to upstream"; | |
575 | |
576 line.len = sizeof("HELO " CRLF) - 1 + s->smtp_helo.len; | |
577 | |
578 line.data = ngx_pnalloc(c->pool, line.len); | |
579 if (line.data == NULL) { | |
580 ngx_mail_proxy_internal_server_error(s); | |
581 return; | |
582 } | |
583 | |
584 line.len = ngx_sprintf(line.data, | |
585 ((s->esmtp) ? "EHLO %V" CRLF : "HELO %V" CRLF), | |
586 &s->smtp_helo) | |
587 - line.data; | |
588 | |
2309 | 589 s->mail_state = (s->auth_method == NGX_MAIL_AUTH_NONE) ? |
2497 | 590 ngx_smtp_helo_from : ngx_smtp_helo; |
2309 | 591 |
592 break; | |
593 | |
594 case ngx_smtp_helo_from: | |
595 case ngx_smtp_xclient_from: | |
596 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
597 "mail proxy send mail from"); | |
598 | |
599 s->connection->log->action = "sending MAIL FROM to upstream"; | |
600 | |
601 line.len = s->smtp_from.len + sizeof(CRLF) - 1; | |
602 line.data = ngx_pnalloc(c->pool, line.len); | |
603 if (line.data == NULL) { | |
604 ngx_mail_proxy_internal_server_error(s); | |
605 return; | |
1136 | 606 } |
607 | |
2309 | 608 p = ngx_cpymem(line.data, s->smtp_from.data, s->smtp_from.len); |
609 *p++ = CR; *p = LF; | |
610 | |
611 s->mail_state = ngx_smtp_from; | |
612 | |
1136 | 613 break; |
614 | |
2309 | 615 case ngx_smtp_from: |
616 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
617 "mail proxy send rcpt to"); | |
618 | |
619 s->connection->log->action = "sending RCPT TO to upstream"; | |
620 | |
621 line.len = s->smtp_to.len + sizeof(CRLF) - 1; | |
622 line.data = ngx_pnalloc(c->pool, line.len); | |
623 if (line.data == NULL) { | |
624 ngx_mail_proxy_internal_server_error(s); | |
625 return; | |
626 } | |
627 | |
628 p = ngx_cpymem(line.data, s->smtp_to.data, s->smtp_to.len); | |
629 *p++ = CR; *p = LF; | |
630 | |
631 s->mail_state = ngx_smtp_to; | |
1136 | 632 |
2309 | 633 break; |
634 | |
635 case ngx_smtp_helo: | |
636 case ngx_smtp_xclient: | |
637 case ngx_smtp_to: | |
638 | |
639 b = s->proxy->buffer; | |
1136 | 640 |
2309 | 641 if (s->auth_method == NGX_MAIL_AUTH_NONE) { |
2311
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
642 b->pos = b->start; |
2309 | 643 |
644 } else { | |
2440
939b40aa9ab4
update r2439: make clear name
Igor Sysoev <igor@sysoev.ru>
parents:
2438
diff
changeset
|
645 ngx_memcpy(b->start, smtp_auth_ok, sizeof(smtp_auth_ok) - 1); |
939b40aa9ab4
update r2439: make clear name
Igor Sysoev <igor@sysoev.ru>
parents:
2438
diff
changeset
|
646 b->last = b->start + sizeof(smtp_auth_ok) - 1; |
2309 | 647 } |
648 | |
1136 | 649 s->connection->read->handler = ngx_mail_proxy_handler; |
650 s->connection->write->handler = ngx_mail_proxy_handler; | |
651 rev->handler = ngx_mail_proxy_handler; | |
652 c->write->handler = ngx_mail_proxy_handler; | |
653 | |
654 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); | |
655 ngx_add_timer(s->connection->read, pcf->timeout); | |
656 ngx_del_timer(c->read); | |
657 | |
658 c->log->action = NULL; | |
659 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in"); | |
660 | |
661 ngx_mail_proxy_handler(s->connection->write); | |
662 | |
663 return; | |
664 | |
665 default: | |
666 #if (NGX_SUPPRESS_WARN) | |
667 line.len = 0; | |
668 line.data = NULL; | |
669 #endif | |
670 break; | |
671 } | |
672 | |
673 if (c->send(c, line.data, line.len) < (ssize_t) line.len) { | |
674 /* | |
675 * we treat the incomplete sending as NGX_ERROR | |
676 * because it is very strange here | |
677 */ | |
678 ngx_mail_proxy_internal_server_error(s); | |
679 return; | |
680 } | |
681 | |
682 s->proxy->buffer->pos = s->proxy->buffer->start; | |
683 s->proxy->buffer->last = s->proxy->buffer->start; | |
684 } | |
685 | |
686 | |
687 static void | |
688 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
|
689 { |
583 | 690 ngx_connection_t *c; |
1136 | 691 ngx_mail_session_t *s; |
583 | 692 |
1136 | 693 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy dummy handler"); |
583 | 694 |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2311
diff
changeset
|
695 if (ngx_handle_write_event(wev, 0) != NGX_OK) { |
583 | 696 c = wev->data; |
697 s = c->data; | |
698 | |
1136 | 699 ngx_mail_proxy_close_session(s); |
583 | 700 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
701 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
702 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
703 |
521 | 704 static ngx_int_t |
1136 | 705 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
|
706 { |
857 | 707 u_char *p; |
708 ssize_t n; | |
709 ngx_buf_t *b; | |
1136 | 710 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
|
711 |
587 | 712 s->connection->log->action = "reading response from upstream"; |
713 | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
714 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
|
715 |
539 | 716 n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection, |
717 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
|
718 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
719 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
|
720 return NGX_ERROR; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
721 } |
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 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
|
724 return NGX_AGAIN; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
725 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
726 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
727 b->last += n; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
728 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
729 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
|
730 return NGX_AGAIN; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
731 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
732 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
733 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
|
734 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
|
735 *(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
|
736 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
|
737 "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
|
738 b->pos); |
571 | 739 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
|
740 } |
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 return NGX_AGAIN; |
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 p = b->pos; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
746 |
1136 | 747 switch (s->protocol) { |
748 | |
749 case NGX_MAIL_POP3_PROTOCOL: | |
527 | 750 if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') { |
751 return NGX_OK; | |
752 } | |
1136 | 753 break; |
527 | 754 |
1136 | 755 case NGX_MAIL_IMAP_PROTOCOL: |
663 | 756 switch (state) { |
757 | |
758 case ngx_imap_start: | |
527 | 759 if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') { |
760 return NGX_OK; | |
761 } | |
663 | 762 break; |
527 | 763 |
663 | 764 case ngx_imap_login: |
765 case ngx_imap_user: | |
529 | 766 if (p[0] == '+') { |
527 | 767 return NGX_OK; |
768 } | |
663 | 769 break; |
770 | |
771 case ngx_imap_passwd: | |
772 if (ngx_strncmp(p, s->tag.data, s->tag.len) == 0) { | |
773 p += s->tag.len; | |
774 if (p[0] == 'O' && p[1] == 'K') { | |
775 return NGX_OK; | |
776 } | |
777 } | |
778 break; | |
527 | 779 } |
1136 | 780 |
781 break; | |
782 | |
783 default: /* NGX_MAIL_SMTP_PROTOCOL */ | |
784 switch (state) { | |
785 | |
2311
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
786 case ngx_smtp_start: |
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
787 if (p[0] == '2' && p[1] == '2' && p[2] == '0') { |
1166 | 788 return NGX_OK; |
789 } | |
790 break; | |
1136 | 791 |
2311
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
792 case ngx_smtp_helo: |
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
793 case ngx_smtp_helo_xclient: |
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
794 case ngx_smtp_helo_from: |
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
795 case ngx_smtp_from: |
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
796 if (p[0] == '2' && p[1] == '5' && p[2] == '0') { |
2309 | 797 return NGX_OK; |
798 } | |
799 break; | |
800 | |
1136 | 801 case ngx_smtp_xclient: |
2309 | 802 case ngx_smtp_xclient_from: |
2497 | 803 case ngx_smtp_xclient_helo: |
2309 | 804 if (p[0] == '2' && (p[1] == '2' || p[1] == '5') && p[2] == '0') { |
1166 | 805 return NGX_OK; |
806 } | |
807 break; | |
2311
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
808 |
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
809 case ngx_smtp_to: |
6bad42a41dd8
do not close session if SMTP backend returned an error on RCPT TO
Igor Sysoev <igor@sysoev.ru>
parents:
2309
diff
changeset
|
810 return NGX_OK; |
1136 | 811 } |
812 | |
1166 | 813 break; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
814 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
815 |
1136 | 816 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); |
857 | 817 |
818 if (pcf->pass_error_message == 0) { | |
819 *(b->last - 2) = '\0'; | |
820 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
821 "upstream sent invalid response: \"%s\"", p); | |
822 return NGX_ERROR; | |
823 } | |
824 | |
825 s->out.len = b->last - p - 2; | |
826 s->out.data = p; | |
827 | |
828 ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, | |
829 "upstream sent invalid response: \"%V\"", &s->out); | |
830 | |
831 s->out.len = b->last - b->pos; | |
832 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
|
833 |
571 | 834 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
|
835 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
836 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
837 |
521 | 838 static void |
1136 | 839 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
|
840 { |
583 | 841 char *action, *recv_action, *send_action; |
539 | 842 size_t size; |
843 ssize_t n; | |
844 ngx_buf_t *b; | |
583 | 845 ngx_uint_t do_write; |
539 | 846 ngx_connection_t *c, *src, *dst; |
1136 | 847 ngx_mail_session_t *s; |
848 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
|
849 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
850 c = ev->data; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
851 s = c->data; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
852 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
853 if (ev->timedout) { |
583 | 854 c->log->action = "proxying"; |
855 | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
856 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
|
857 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
|
858 "client timed out"); |
577 | 859 c->timedout = 1; |
860 | |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
861 } else { |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
862 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
|
863 "upstream timed out"); |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
864 } |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
865 |
1136 | 866 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
|
867 return; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
868 } |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
869 |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
870 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
|
871 if (ev->write) { |
583 | 872 recv_action = "proxying and reading from upstream"; |
873 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
|
874 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
|
875 dst = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
876 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
|
877 |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
878 } else { |
583 | 879 recv_action = "proxying and reading from client"; |
880 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
|
881 src = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
882 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
|
883 b = s->buffer; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
884 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
885 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
886 } else { |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
887 if (ev->write) { |
589 | 888 recv_action = "proxying and reading from client"; |
889 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
|
890 src = s->connection; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
891 dst = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
892 b = s->buffer; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
893 |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
894 } else { |
589 | 895 recv_action = "proxying and reading from upstream"; |
896 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
|
897 src = c; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
898 dst = s->connection; |
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
899 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
|
900 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
901 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
902 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
903 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
|
904 |
1136 | 905 ngx_log_debug3(NGX_LOG_DEBUG_MAIL, ev->log, 0, |
906 "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
|
907 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
|
908 |
583 | 909 for ( ;; ) { |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
910 |
583 | 911 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
|
912 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
913 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
|
914 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
915 if (size && dst->write->ready) { |
583 | 916 c->log->action = send_action; |
917 | |
539 | 918 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
|
919 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
920 if (n == NGX_ERROR) { |
1136 | 921 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
|
922 return; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
923 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
924 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
925 if (n > 0) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
926 b->pos += n; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
927 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
928 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
|
929 b->pos = b->start; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
930 b->last = b->start; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
931 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
932 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
933 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
934 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
935 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
936 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
|
937 |
423
fda5987b188d
nginx-0.0.10-2004-09-13-20:18:09 import
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
938 if (size && src->read->ready) { |
583 | 939 c->log->action = recv_action; |
940 | |
539 | 941 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
|
942 |
583 | 943 if (n == NGX_AGAIN || n == 0) { |
944 break; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
945 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
946 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
947 if (n > 0) { |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
948 do_write = 1; |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
949 b->last += n; |
583 | 950 |
951 continue; | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
952 } |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
953 |
583 | 954 if (n == NGX_ERROR) { |
955 src->read->eof = 1; | |
539 | 956 } |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
957 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
958 |
583 | 959 break; |
960 } | |
961 | |
962 c->log->action = "proxying"; | |
963 | |
1342
be2e13691c60
fix case when client has closed connection but upstream buffer is not empty
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
964 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
|
965 || (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
|
966 && 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
|
967 || (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
|
968 && s->proxy->upstream.connection->read->eof)) |
583 | 969 { |
970 action = c->log->action; | |
971 c->log->action = NULL; | |
972 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done"); | |
973 c->log->action = action; | |
974 | |
1136 | 975 ngx_mail_proxy_close_session(s); |
583 | 976 return; |
977 } | |
978 | |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2311
diff
changeset
|
979 if (ngx_handle_write_event(dst->write, 0) != NGX_OK) { |
1136 | 980 ngx_mail_proxy_close_session(s); |
583 | 981 return; |
982 } | |
983 | |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2311
diff
changeset
|
984 if (ngx_handle_read_event(dst->read, 0) != NGX_OK) { |
1136 | 985 ngx_mail_proxy_close_session(s); |
583 | 986 return; |
987 } | |
988 | |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2311
diff
changeset
|
989 if (ngx_handle_write_event(src->write, 0) != NGX_OK) { |
1136 | 990 ngx_mail_proxy_close_session(s); |
583 | 991 return; |
992 } | |
993 | |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2311
diff
changeset
|
994 if (ngx_handle_read_event(src->read, 0) != NGX_OK) { |
1136 | 995 ngx_mail_proxy_close_session(s); |
583 | 996 return; |
997 } | |
998 | |
999 if (c == s->connection) { | |
1136 | 1000 pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); |
583 | 1001 ngx_add_timer(c->read, pcf->timeout); |
1002 } | |
418
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1003 } |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1004 |
cf072d26d6d6
nginx-0.0.10-2004-09-08-09:18:51 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
1005 |
521 | 1006 static void |
1136 | 1007 ngx_mail_proxy_upstream_error(ngx_mail_session_t *s) |
857 | 1008 { |
1009 if (s->proxy->upstream.connection) { | |
1136 | 1010 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
1011 "close mail proxy connection: %d", | |
857 | 1012 s->proxy->upstream.connection->fd); |
1013 | |
1014 ngx_close_connection(s->proxy->upstream.connection); | |
1015 } | |
1016 | |
1017 if (s->out.len == 0) { | |
1136 | 1018 ngx_mail_session_internal_server_error(s); |
857 | 1019 return; |
1020 } | |
1021 | |
1022 s->quit = 1; | |
1136 | 1023 ngx_mail_send(s->connection->write); |
857 | 1024 } |
1025 | |
1026 | |
1027 static void | |
1136 | 1028 ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s) |
527 | 1029 { |
1030 if (s->proxy->upstream.connection) { | |
1136 | 1031 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
1032 "close mail proxy connection: %d", | |
527 | 1033 s->proxy->upstream.connection->fd); |
1034 | |
1035 ngx_close_connection(s->proxy->upstream.connection); | |
1036 } | |
1037 | |
1136 | 1038 ngx_mail_session_internal_server_error(s); |
527 | 1039 } |
1040 | |
1041 | |
1042 static void | |
1136 | 1043 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
|
1044 { |
521 | 1045 if (s->proxy->upstream.connection) { |
1136 | 1046 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
1047 "close mail proxy connection: %d", | |
521 | 1048 s->proxy->upstream.connection->fd); |
1049 | |
1050 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
|
1051 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
418
diff
changeset
|
1052 |
1136 | 1053 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
|
1054 } |
521 | 1055 |
1056 | |
1057 static void * | |
1136 | 1058 ngx_mail_proxy_create_conf(ngx_conf_t *cf) |
577 | 1059 { |
1136 | 1060 ngx_mail_proxy_conf_t *pcf; |
577 | 1061 |
1136 | 1062 pcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_proxy_conf_t)); |
521 | 1063 if (pcf == NULL) { |
2912
c7d57b539248
return NULL instead of NGX_CONF_ERROR on a create conf failure
Igor Sysoev <igor@sysoev.ru>
parents:
2497
diff
changeset
|
1064 return NULL; |
521 | 1065 } |
1066 | |
1067 pcf->enable = NGX_CONF_UNSET; | |
857 | 1068 pcf->pass_error_message = NGX_CONF_UNSET; |
1136 | 1069 pcf->xclient = NGX_CONF_UNSET; |
539 | 1070 pcf->buffer_size = NGX_CONF_UNSET_SIZE; |
1071 pcf->timeout = NGX_CONF_UNSET_MSEC; | |
521 | 1072 |
1073 return pcf; | |
1074 } | |
1075 | |
1076 | |
1077 static char * | |
1136 | 1078 ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child) |
521 | 1079 { |
1136 | 1080 ngx_mail_proxy_conf_t *prev = parent; |
1081 ngx_mail_proxy_conf_t *conf = child; | |
521 | 1082 |
539 | 1083 ngx_conf_merge_value(conf->enable, prev->enable, 0); |
857 | 1084 ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0); |
1136 | 1085 ngx_conf_merge_value(conf->xclient, prev->xclient, 1); |
539 | 1086 ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size, |
1087 (size_t) ngx_pagesize); | |
1088 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000); | |
521 | 1089 |
1090 return NGX_CONF_OK; | |
1091 } |