2004-09-08 13:18:51 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-09-08 13:18:51 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_event.h>
|
2004-09-09 23:40:48 +08:00
|
|
|
#include <ngx_event_connect.h>
|
2004-09-08 13:18:51 +08:00
|
|
|
#include <ngx_imap.h>
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
typedef struct {
|
|
|
|
ngx_flag_t enable;
|
|
|
|
} ngx_imap_proxy_conf_t;
|
|
|
|
|
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
static void ngx_imap_proxy_block_read(ngx_event_t *rev);
|
2005-07-08 22:34:20 +08:00
|
|
|
static void ngx_imap_proxy_imap_handler(ngx_event_t *rev);
|
|
|
|
static void ngx_imap_proxy_pop3_handler(ngx_event_t *rev);
|
2004-09-09 23:40:48 +08:00
|
|
|
static void ngx_imap_proxy_dummy_handler(ngx_event_t *ev);
|
2005-07-08 22:34:20 +08:00
|
|
|
static ngx_int_t ngx_imap_proxy_read_response(ngx_imap_session_t *s,
|
|
|
|
ngx_uint_t what);
|
2004-09-09 23:40:48 +08:00
|
|
|
static void ngx_imap_proxy_handler(ngx_event_t *ev);
|
2005-07-08 22:34:20 +08:00
|
|
|
static void ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s);
|
2004-09-08 13:18:51 +08:00
|
|
|
static void ngx_imap_proxy_close_session(ngx_imap_session_t *s);
|
2005-06-07 23:56:31 +08:00
|
|
|
static void *ngx_imap_proxy_create_conf(ngx_conf_t *cf);
|
|
|
|
static char *ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent,
|
|
|
|
void *child);
|
|
|
|
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
#define NGX_IMAP_WAIT_OK 0
|
|
|
|
#define NGX_IMAP_WAIT_NEXT 1
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static ngx_command_t ngx_imap_proxy_commands[] = {
|
|
|
|
{ ngx_string("proxy"),
|
|
|
|
NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_FLAG,
|
|
|
|
ngx_conf_set_flag_slot,
|
|
|
|
NGX_IMAP_SRV_CONF_OFFSET,
|
|
|
|
offsetof(ngx_imap_proxy_conf_t, enable),
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_imap_module_t ngx_imap_proxy_module_ctx = {
|
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
ngx_imap_proxy_create_conf, /* create server configuration */
|
|
|
|
ngx_imap_proxy_merge_conf /* merge server configuration */
|
|
|
|
};
|
|
|
|
|
2004-09-08 13:18:51 +08:00
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
ngx_module_t ngx_imap_proxy_module = {
|
|
|
|
NGX_MODULE_V1,
|
|
|
|
&ngx_imap_proxy_module_ctx, /* module context */
|
|
|
|
ngx_imap_proxy_commands, /* module directives */
|
|
|
|
NGX_IMAP_MODULE, /* module type */
|
|
|
|
NULL, /* init module */
|
|
|
|
NULL /* init process */
|
|
|
|
};
|
2004-09-08 13:18:51 +08:00
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
ngx_imap_proxy_init(ngx_imap_session_t *s, ngx_peers_t *peers)
|
2004-09-08 13:18:51 +08:00
|
|
|
{
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_imap_proxy_ctx_t *p;
|
|
|
|
ngx_imap_core_srv_conf_t *cscf;
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-03-19 20:38:37 +08:00
|
|
|
p = ngx_pcalloc(s->connection->pool, sizeof(ngx_imap_proxy_ctx_t));
|
|
|
|
if (p == NULL) {
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_imap_session_internal_server_error(s);
|
2004-09-09 23:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->proxy = p;
|
|
|
|
|
|
|
|
p->upstream.peers = peers;
|
|
|
|
p->upstream.log = s->connection->log;
|
|
|
|
p->upstream.log_error = NGX_ERROR_ERR;
|
|
|
|
|
|
|
|
rc = ngx_event_connect_peer(&p->upstream);
|
|
|
|
|
|
|
|
if (rc == NGX_ERROR) {
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_imap_session_internal_server_error(s);
|
2004-09-09 23:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
|
|
ngx_add_timer(p->upstream.connection->read, cscf->timeout);
|
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
p->upstream.connection->data = s;
|
|
|
|
p->upstream.connection->pool = s->connection->pool;
|
|
|
|
|
nginx-0.1.29-RELEASE import
*) Feature: the ngx_http_ssi_module supports "include virtual" command.
*) Feature: the ngx_http_ssi_module supports the condition command like
'if expr="$NAME"' and "else" and "endif" commands. Only one nested
level is supported.
*) Feature: the ngx_http_ssi_module supports the DATE_LOCAL and
DATE_GMT variables and "config timefmt" command.
*) Feature: the "ssi_ignore_recycled_buffers" directive.
*) Bugfix: the "echo" command did not show the default value for the
empty QUERY_STRING variable.
*) Change: the ngx_http_proxy_module was rewritten.
*) Feature: the "proxy_redirect", "proxy_pass_request_headers",
"proxy_pass_request_body", and "proxy_method" directives.
*) Feature: the "proxy_set_header" directive. The "proxy_x_var" was
canceled and must be replaced with the proxy_set_header directive.
*) Change: the "proxy_preserve_host" is canceled and must be replaced
with the "proxy_set_header Host $host" and the "proxy_redirect off"
directives, the "proxy_set_header Host $host:$proxy_port" directive
and the appropriate proxy_redirect directives.
*) Change: the "proxy_set_x_real_ip" is canceled and must be replaced
with the "proxy_set_header X-Real-IP $remote_addr" directive.
*) Change: the "proxy_add_x_forwarded_for" is canceled and must be
replaced with
the "proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for"
directive.
*) Change: the "proxy_set_x_url" is canceled and must be replaced with
the "proxy_set_header X-URL http://$host:$server_port$request_uri"
directive.
*) Feature: the "fastcgi_param" directive.
*) Change: the "fastcgi_root", "fastcgi_set_var" and "fastcgi_params"
directive are canceled and must be replaced with the fastcgi_param
directives.
*) Feature: the "index" directive can use the variables.
*) Feature: the "index" directive can be used at http and server levels.
*) Change: the last index only in the "index" directive can be absolute.
*) Feature: the "rewrite" directive can use the variables.
*) Feature: the "internal" directive.
*) Feature: the CONTENT_LENGTH, CONTENT_TYPE, REMOTE_PORT, SERVER_ADDR,
SERVER_PORT, SERVER_PROTOCOL, DOCUMENT_ROOT, SERVER_NAME,
REQUEST_METHOD, REQUEST_URI, and REMOTE_USER variables.
*) Change: nginx now passes the invalid lines in a client request
headers or a backend response header.
*) Bugfix: if the backend did not transfer response for a long time and
the "send_timeout" was less than "proxy_read_timeout", then nginx
returned the 408 response.
*) Bugfix: the segmentation fault was occurred if the backend sent an
invalid line in response header; the bug had appeared in 0.1.26.
*) Bugfix: the segmentation fault may occurred in FastCGI fault
tolerance configuration.
*) Bugfix: the "expires" directive did not remove the previous
"Expires" and "Cache-Control" headers.
*) Bugfix: nginx did not take into account trailing dot in "Host"
header line.
*) Bugfix: the ngx_http_auth_module did not work under Linux.
*) Bugfix: the rewrite directive worked incorrectly, if the arguments
were in a request.
*) Bugfix: nginx could not be built on MacOS X.
2005-05-12 22:58:06 +08:00
|
|
|
s->connection->read->handler = ngx_imap_proxy_block_read;
|
|
|
|
p->upstream.connection->write->handler = ngx_imap_proxy_dummy_handler;
|
2005-07-08 22:34:20 +08:00
|
|
|
|
|
|
|
if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
|
|
|
p->upstream.connection->read->handler = ngx_imap_proxy_pop3_handler;
|
|
|
|
s->imap_state = ngx_pop3_start;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
p->upstream.connection->read->handler = ngx_imap_proxy_imap_handler;
|
|
|
|
s->imap_state = ngx_imap_start;
|
|
|
|
}
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static void
|
|
|
|
ngx_imap_proxy_block_read(ngx_event_t *rev)
|
2004-09-09 23:40:48 +08:00
|
|
|
{
|
|
|
|
ngx_connection_t *c;
|
|
|
|
ngx_imap_session_t *s;
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy block read");
|
|
|
|
|
|
|
|
if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
|
|
|
|
c = rev->data;
|
|
|
|
s = c->data;
|
|
|
|
|
|
|
|
ngx_imap_proxy_close_session(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static void
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_imap_proxy_imap_handler(ngx_event_t *rev)
|
2004-09-09 23:40:48 +08:00
|
|
|
{
|
2005-07-08 22:34:20 +08:00
|
|
|
u_char *p;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_str_t line;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
ngx_imap_session_t *s;
|
|
|
|
ngx_imap_core_srv_conf_t *cscf;
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
|
|
"imap proxy imap auth handler");
|
2004-09-09 23:40:48 +08:00
|
|
|
|
|
|
|
c = rev->data;
|
|
|
|
s = c->data;
|
|
|
|
|
2004-09-14 00:18:09 +08:00
|
|
|
if (rev->timedout) {
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
|
|
"upstream timed out");
|
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
2004-09-14 00:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
if (s->proxy->buffer == NULL) {
|
2005-07-08 22:34:20 +08:00
|
|
|
cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
|
|
|
|
|
|
s->proxy->buffer = ngx_create_temp_buf(c->pool,
|
|
|
|
cscf->proxy_buffer_size);
|
2004-09-09 23:40:48 +08:00
|
|
|
if (s->proxy->buffer == NULL) {
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
2004-09-09 23:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
rc = ngx_imap_proxy_read_response(s, s->imap_state == ngx_imap_start ?
|
|
|
|
NGX_IMAP_WAIT_OK : NGX_IMAP_WAIT_NEXT);
|
2004-09-09 23:40:48 +08:00
|
|
|
|
|
|
|
if (rc == NGX_AGAIN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-09-12 04:22:11 +08:00
|
|
|
if (rc == NGX_ERROR) {
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
2004-09-12 04:22:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
switch (s->imap_state) {
|
|
|
|
|
|
|
|
case ngx_imap_start:
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
|
|
"imap proxy send login");
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
line.len = s->tag.len + sizeof("LOGIN ") - 1
|
|
|
|
+ 1 + NGX_SIZE_T_LEN + 1 + 2;
|
|
|
|
line.data = ngx_palloc(c->pool, line.len);
|
|
|
|
if (line.data == NULL) {
|
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF,
|
|
|
|
&s->tag, s->login.len)
|
|
|
|
- line.data;
|
|
|
|
|
|
|
|
s->imap_state = ngx_imap_login;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ngx_imap_login:
|
2004-09-12 04:22:11 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
line.len = s->login.len + 1 + NGX_SIZE_T_LEN + 1 + 2;
|
2005-03-19 20:38:37 +08:00
|
|
|
line.data = ngx_palloc(c->pool, line.len);
|
|
|
|
if (line.data == NULL) {
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
2004-09-09 23:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
line.len = ngx_sprintf(line.data, "%V{%uz}" CRLF,
|
|
|
|
&s->login, s->passwd.len)
|
|
|
|
- line.data;
|
|
|
|
|
|
|
|
s->imap_state = ngx_imap_user;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ngx_imap_user:
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
|
|
"imap proxy send passwd");
|
2004-09-12 04:22:11 +08:00
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
line.len = s->passwd.len + 2;
|
|
|
|
line.data = ngx_palloc(c->pool, line.len);
|
|
|
|
if (line.data == NULL) {
|
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
2004-09-12 04:22:11 +08:00
|
|
|
return;
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len);
|
|
|
|
*p++ = CR; *p = LF;
|
|
|
|
|
|
|
|
s->imap_state = ngx_imap_passwd;
|
|
|
|
break;
|
2004-09-12 04:22:11 +08:00
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
default:
|
|
|
|
#if (NGX_SUPPRESS_WARN)
|
|
|
|
line.len = 0;
|
|
|
|
line.data = NULL;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2004-09-12 04:22:11 +08:00
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
if (ngx_send(c, line.data, line.len) < (ssize_t) line.len) {
|
|
|
|
/*
|
|
|
|
* we treat the incomplete sending as NGX_ERROR
|
|
|
|
* because it is very strange here
|
|
|
|
*/
|
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
2004-09-09 23:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
|
|
s->proxy->buffer->last = s->proxy->buffer->start;
|
2004-09-12 04:22:11 +08:00
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
if (s->imap_state == ngx_imap_passwd) {
|
|
|
|
s->connection->read->handler = ngx_imap_proxy_handler;
|
|
|
|
s->connection->write->handler = ngx_imap_proxy_handler;
|
|
|
|
rev->handler = ngx_imap_proxy_handler;
|
|
|
|
c->write->handler = ngx_imap_proxy_handler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_imap_proxy_pop3_handler(ngx_event_t *rev)
|
|
|
|
{
|
|
|
|
u_char *p;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_str_t line;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
ngx_imap_session_t *s;
|
|
|
|
ngx_imap_core_srv_conf_t *cscf;
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
|
|
|
"imap proxy pop3 auth handler");
|
|
|
|
|
|
|
|
c = rev->data;
|
|
|
|
s = c->data;
|
|
|
|
|
|
|
|
if (rev->timedout) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
|
|
"upstream timed out");
|
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->proxy->buffer == NULL) {
|
|
|
|
cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
|
|
|
|
|
|
|
|
s->proxy->buffer = ngx_create_temp_buf(c->pool,
|
|
|
|
cscf->proxy_buffer_size);
|
|
|
|
if (s->proxy->buffer == NULL) {
|
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ngx_imap_proxy_read_response(s, NGX_IMAP_WAIT_OK);
|
|
|
|
|
|
|
|
if (rc == NGX_AGAIN) {
|
2004-09-12 04:22:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (s->imap_state) {
|
|
|
|
|
|
|
|
case ngx_pop3_start:
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
|
|
|
|
|
|
|
|
line.len = sizeof("USER ") - 1 + s->login.len + 2;
|
|
|
|
line.data = ngx_palloc(c->pool, line.len);
|
|
|
|
if (line.data == NULL) {
|
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1);
|
|
|
|
p = ngx_cpymem(p, s->login.data, s->login.len);
|
|
|
|
*p++ = CR; *p = LF;
|
|
|
|
|
|
|
|
s->imap_state = ngx_pop3_user;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ngx_pop3_user:
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send pass");
|
|
|
|
|
|
|
|
line.len = sizeof("PASS ") - 1 + s->passwd.len + 2;
|
|
|
|
line.data = ngx_palloc(c->pool, line.len);
|
|
|
|
if (line.data == NULL) {
|
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1);
|
|
|
|
p = ngx_cpymem(p, s->passwd.data, s->passwd.len);
|
|
|
|
*p++ = CR; *p = LF;
|
|
|
|
|
|
|
|
s->imap_state = ngx_pop3_passwd;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
#if (NGX_SUPPRESS_WARN)
|
|
|
|
line.len = 0;
|
|
|
|
line.data = NULL;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2004-09-12 04:22:11 +08:00
|
|
|
|
|
|
|
if (ngx_send(c, line.data, line.len) < (ssize_t) line.len) {
|
|
|
|
/*
|
|
|
|
* we treat the incomplete sending as NGX_ERROR
|
|
|
|
* because it is very strange here
|
|
|
|
*/
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_imap_proxy_internal_server_error(s);
|
2004-09-12 04:22:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
|
|
s->proxy->buffer->last = s->proxy->buffer->start;
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
if (s->imap_state == ngx_pop3_passwd) {
|
|
|
|
s->connection->read->handler = ngx_imap_proxy_handler;
|
|
|
|
s->connection->write->handler = ngx_imap_proxy_handler;
|
|
|
|
rev->handler = ngx_imap_proxy_handler;
|
|
|
|
c->write->handler = ngx_imap_proxy_handler;
|
|
|
|
}
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static void
|
|
|
|
ngx_imap_proxy_dummy_handler(ngx_event_t *ev)
|
2004-09-09 23:40:48 +08:00
|
|
|
{
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, ev->log, 0, "imap proxy dummy handler");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static ngx_int_t
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_imap_proxy_read_response(ngx_imap_session_t *s, ngx_uint_t what)
|
2004-09-09 23:40:48 +08:00
|
|
|
{
|
|
|
|
u_char *p;
|
|
|
|
ssize_t n;
|
|
|
|
ngx_buf_t *b;
|
|
|
|
|
|
|
|
b = s->proxy->buffer;
|
|
|
|
|
|
|
|
n = ngx_recv(s->proxy->upstream.connection, b->last, b->end - b->last);
|
|
|
|
|
|
|
|
if (n == NGX_ERROR || n == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == NGX_AGAIN) {
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
b->last += n;
|
|
|
|
|
|
|
|
if (b->last - b->pos < 5) {
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*(b->last - 2) != CR || *(b->last - 1) != LF) {
|
|
|
|
if (b->last == b->end) {
|
|
|
|
*(b->last - 1) = '\0';
|
|
|
|
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
|
|
|
|
"upstream sent too long response line: \"%s\"",
|
|
|
|
b->pos);
|
|
|
|
return NGX_IMAP_PROXY_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = b->pos;
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
|
|
|
|
if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
if (p[0] == '-' && p[1] == 'E' && p[2] == 'R' && p[3] == 'R') {
|
|
|
|
return NGX_IMAP_PROXY_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (what == NGX_IMAP_WAIT_OK) {
|
|
|
|
if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (p[0] == '+' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*(b->last - 2) = '\0';
|
|
|
|
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
|
2005-07-08 22:34:20 +08:00
|
|
|
"upstream sent invalid response: \"%s\"", p);
|
2004-09-09 23:40:48 +08:00
|
|
|
|
|
|
|
return NGX_IMAP_PROXY_INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static void
|
|
|
|
ngx_imap_proxy_handler(ngx_event_t *ev)
|
2004-09-09 23:40:48 +08:00
|
|
|
{
|
|
|
|
size_t size;
|
2004-09-08 13:18:51 +08:00
|
|
|
ssize_t n;
|
|
|
|
ngx_buf_t *b;
|
2004-09-14 00:18:09 +08:00
|
|
|
ngx_uint_t again, do_write;
|
2004-09-08 13:18:51 +08:00
|
|
|
ngx_connection_t *c, *src, *dst;
|
|
|
|
ngx_imap_session_t *s;
|
|
|
|
|
|
|
|
c = ev->data;
|
|
|
|
s = c->data;
|
|
|
|
|
2004-09-14 00:18:09 +08:00
|
|
|
if (ev->timedout) {
|
|
|
|
if (c == s->connection) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
|
|
"client timed out");
|
|
|
|
} else {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
|
|
"upstream timed out");
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_imap_proxy_close_session(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-09-08 13:18:51 +08:00
|
|
|
if (c == s->connection) {
|
2004-09-14 00:18:09 +08:00
|
|
|
if (ev->write) {
|
|
|
|
src = s->proxy->upstream.connection;
|
|
|
|
dst = c;
|
|
|
|
b = s->proxy->buffer;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
src = c;
|
|
|
|
dst = s->proxy->upstream.connection;
|
|
|
|
b = s->buffer;
|
|
|
|
}
|
2004-09-08 13:18:51 +08:00
|
|
|
|
|
|
|
} else {
|
2004-09-14 00:18:09 +08:00
|
|
|
if (ev->write) {
|
|
|
|
src = s->connection;
|
|
|
|
dst = c;
|
|
|
|
b = s->buffer;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
src = c;
|
|
|
|
dst = s->connection;
|
|
|
|
b = s->proxy->buffer;
|
|
|
|
}
|
2004-09-08 13:18:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
do_write = ev->write ? 1 : 0;
|
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
ngx_log_debug3(NGX_LOG_DEBUG_IMAP, ev->log, 0,
|
|
|
|
"imap proxy handler: %d, #%d > #%d",
|
|
|
|
do_write, src->fd, dst->fd);
|
|
|
|
|
2004-09-08 13:18:51 +08:00
|
|
|
do {
|
2004-09-14 00:18:09 +08:00
|
|
|
again = 0;
|
2004-09-08 13:18:51 +08:00
|
|
|
|
|
|
|
if (do_write == 1) {
|
2004-09-09 23:40:48 +08:00
|
|
|
|
|
|
|
size = b->last - b->pos;
|
|
|
|
|
2004-09-14 00:18:09 +08:00
|
|
|
if (size && dst->write->ready) {
|
2004-09-09 23:40:48 +08:00
|
|
|
n = ngx_send(dst, b->pos, size);
|
2004-09-08 13:18:51 +08:00
|
|
|
|
|
|
|
if (n == NGX_ERROR) {
|
|
|
|
ngx_imap_proxy_close_session(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0) {
|
2004-09-14 00:18:09 +08:00
|
|
|
again = 1;
|
2004-09-08 13:18:51 +08:00
|
|
|
b->pos += n;
|
|
|
|
|
|
|
|
if (b->pos == b->last) {
|
|
|
|
b->pos = b->start;
|
|
|
|
b->last = b->start;
|
|
|
|
}
|
|
|
|
}
|
2004-09-09 23:40:48 +08:00
|
|
|
|
|
|
|
if (n == NGX_AGAIN || n < (ssize_t) size) {
|
2004-10-11 23:07:03 +08:00
|
|
|
if (ngx_handle_write_event(dst->write, /* TODO: LOWAT */ 0)
|
2004-09-09 23:40:48 +08:00
|
|
|
== NGX_ERROR)
|
|
|
|
{
|
|
|
|
ngx_imap_proxy_close_session(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-09-08 13:18:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
size = b->end - b->last;
|
|
|
|
|
2004-09-14 00:18:09 +08:00
|
|
|
if (size && src->read->ready) {
|
2004-09-09 23:40:48 +08:00
|
|
|
n = ngx_recv(src, b->last, size);
|
2004-09-08 13:18:51 +08:00
|
|
|
|
|
|
|
if (n == NGX_ERROR || n == 0) {
|
|
|
|
ngx_imap_proxy_close_session(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0) {
|
2004-09-14 00:18:09 +08:00
|
|
|
again = 1;
|
2004-09-08 13:18:51 +08:00
|
|
|
do_write = 1;
|
|
|
|
b->last += n;
|
|
|
|
}
|
2004-09-09 23:40:48 +08:00
|
|
|
|
|
|
|
if (n == NGX_AGAIN || n < (ssize_t) size) {
|
|
|
|
if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) {
|
|
|
|
ngx_imap_proxy_close_session(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2004-09-08 13:18:51 +08:00
|
|
|
}
|
|
|
|
|
2004-09-14 00:18:09 +08:00
|
|
|
} while (again);
|
2004-09-08 13:18:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
static void
|
|
|
|
ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s)
|
|
|
|
{
|
|
|
|
if (s->proxy->upstream.connection) {
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
|
|
|
|
"close imap proxy connection: %d",
|
|
|
|
s->proxy->upstream.connection->fd);
|
|
|
|
|
|
|
|
ngx_close_connection(s->proxy->upstream.connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_imap_session_internal_server_error(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static void
|
|
|
|
ngx_imap_proxy_close_session(ngx_imap_session_t *s)
|
2004-09-08 13:18:51 +08:00
|
|
|
{
|
2005-06-07 23:56:31 +08:00
|
|
|
if (s->proxy->upstream.connection) {
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
|
|
|
|
"close imap proxy connection: %d",
|
|
|
|
s->proxy->upstream.connection->fd);
|
|
|
|
|
|
|
|
ngx_close_connection(s->proxy->upstream.connection);
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ngx_imap_close_connection(s->connection);
|
2004-09-08 13:18:51 +08:00
|
|
|
}
|
2005-06-07 23:56:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ngx_imap_proxy_create_conf(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_imap_proxy_conf_t *pcf;
|
|
|
|
|
|
|
|
pcf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_proxy_conf_t));
|
|
|
|
if (pcf == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
pcf->enable = NGX_CONF_UNSET;
|
|
|
|
|
|
|
|
return pcf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
|
|
{
|
|
|
|
ngx_imap_proxy_conf_t *prev = parent;
|
|
|
|
ngx_imap_proxy_conf_t *conf = child;
|
|
|
|
|
|
|
|
ngx_conf_merge_msec_value(conf->enable, prev->enable, 0);
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|