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
|
2012-01-18 23:07:43 +08:00
|
|
|
* Copyright (C) Nginx, Inc.
|
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>
|
2007-03-19 21:36:56 +08:00
|
|
|
#include <ngx_mail.h>
|
2004-09-08 13:18:51 +08:00
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
typedef struct {
|
|
|
|
ngx_flag_t enable;
|
2006-11-17 22:22:51 +08:00
|
|
|
ngx_flag_t pass_error_message;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_flag_t xclient;
|
2005-09-07 00:09:32 +08:00
|
|
|
size_t buffer_size;
|
|
|
|
ngx_msec_t timeout;
|
2007-03-19 21:36:56 +08:00
|
|
|
} ngx_mail_proxy_conf_t;
|
2005-06-07 23:56:31 +08:00
|
|
|
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
static void ngx_mail_proxy_block_read(ngx_event_t *rev);
|
|
|
|
static void ngx_mail_proxy_pop3_handler(ngx_event_t *rev);
|
|
|
|
static void ngx_mail_proxy_imap_handler(ngx_event_t *rev);
|
|
|
|
static void ngx_mail_proxy_smtp_handler(ngx_event_t *rev);
|
|
|
|
static void ngx_mail_proxy_dummy_handler(ngx_event_t *ev);
|
|
|
|
static ngx_int_t ngx_mail_proxy_read_response(ngx_mail_session_t *s,
|
2006-07-08 00:33:19 +08:00
|
|
|
ngx_uint_t state);
|
2007-03-19 21:36:56 +08:00
|
|
|
static void ngx_mail_proxy_handler(ngx_event_t *ev);
|
|
|
|
static void ngx_mail_proxy_upstream_error(ngx_mail_session_t *s);
|
|
|
|
static void ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s);
|
|
|
|
static void ngx_mail_proxy_close_session(ngx_mail_session_t *s);
|
|
|
|
static void *ngx_mail_proxy_create_conf(ngx_conf_t *cf);
|
|
|
|
static char *ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent,
|
2005-06-07 23:56:31 +08:00
|
|
|
void *child);
|
|
|
|
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
static ngx_command_t ngx_mail_proxy_commands[] = {
|
2005-09-07 00:09:32 +08:00
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
{ ngx_string("proxy"),
|
2007-03-19 21:36:56 +08:00
|
|
|
NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
|
2005-06-07 23:56:31 +08:00
|
|
|
ngx_conf_set_flag_slot,
|
2007-03-19 21:36:56 +08:00
|
|
|
NGX_MAIL_SRV_CONF_OFFSET,
|
|
|
|
offsetof(ngx_mail_proxy_conf_t, enable),
|
2005-06-07 23:56:31 +08:00
|
|
|
NULL },
|
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
{ ngx_string("proxy_buffer"),
|
2007-03-19 21:36:56 +08:00
|
|
|
NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
2005-09-07 00:09:32 +08:00
|
|
|
ngx_conf_set_size_slot,
|
2007-03-19 21:36:56 +08:00
|
|
|
NGX_MAIL_SRV_CONF_OFFSET,
|
|
|
|
offsetof(ngx_mail_proxy_conf_t, buffer_size),
|
2005-09-07 00:09:32 +08:00
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ ngx_string("proxy_timeout"),
|
2007-03-19 21:36:56 +08:00
|
|
|
NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
|
2005-09-07 00:09:32 +08:00
|
|
|
ngx_conf_set_msec_slot,
|
2007-03-19 21:36:56 +08:00
|
|
|
NGX_MAIL_SRV_CONF_OFFSET,
|
|
|
|
offsetof(ngx_mail_proxy_conf_t, timeout),
|
2005-09-07 00:09:32 +08:00
|
|
|
NULL },
|
|
|
|
|
2006-11-17 22:22:51 +08:00
|
|
|
{ ngx_string("proxy_pass_error_message"),
|
2011-11-14 17:12:15 +08:00
|
|
|
NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
|
2006-11-17 22:22:51 +08:00
|
|
|
ngx_conf_set_flag_slot,
|
2007-03-19 21:36:56 +08:00
|
|
|
NGX_MAIL_SRV_CONF_OFFSET,
|
|
|
|
offsetof(ngx_mail_proxy_conf_t, pass_error_message),
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ ngx_string("xclient"),
|
|
|
|
NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
|
|
|
|
ngx_conf_set_flag_slot,
|
|
|
|
NGX_MAIL_SRV_CONF_OFFSET,
|
|
|
|
offsetof(ngx_mail_proxy_conf_t, xclient),
|
2006-11-17 22:22:51 +08:00
|
|
|
NULL },
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
static ngx_mail_module_t ngx_mail_proxy_module_ctx = {
|
2007-09-16 00:51:16 +08:00
|
|
|
NULL, /* protocol */
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_create_conf, /* create server configuration */
|
|
|
|
ngx_mail_proxy_merge_conf /* merge server configuration */
|
2005-06-07 23:56:31 +08:00
|
|
|
};
|
|
|
|
|
2004-09-08 13:18:51 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_module_t ngx_mail_proxy_module = {
|
2005-06-07 23:56:31 +08:00
|
|
|
NGX_MODULE_V1,
|
2007-03-19 21:36:56 +08:00
|
|
|
&ngx_mail_proxy_module_ctx, /* module context */
|
|
|
|
ngx_mail_proxy_commands, /* module directives */
|
|
|
|
NGX_MAIL_MODULE, /* module type */
|
2005-09-08 22:36:09 +08:00
|
|
|
NULL, /* init master */
|
2005-06-07 23:56:31 +08:00
|
|
|
NULL, /* init module */
|
2005-09-08 22:36:09 +08:00
|
|
|
NULL, /* init process */
|
|
|
|
NULL, /* init thread */
|
|
|
|
NULL, /* exit thread */
|
|
|
|
NULL, /* exit process */
|
|
|
|
NULL, /* exit master */
|
|
|
|
NGX_MODULE_V1_PADDING
|
2005-06-07 23:56:31 +08:00
|
|
|
};
|
2004-09-08 13:18:51 +08:00
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
|
2009-01-13 14:47:29 +08:00
|
|
|
static u_char smtp_auth_ok[] = "235 2.0.0 OK" CRLF;
|
2007-03-19 21:36:56 +08:00
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
void
|
2009-11-02 23:14:17 +08:00
|
|
|
ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_addr_t *peer)
|
2004-09-08 13:18:51 +08:00
|
|
|
{
|
2005-07-08 22:34:20 +08:00
|
|
|
ngx_int_t rc;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_ctx_t *p;
|
|
|
|
ngx_mail_proxy_conf_t *pcf;
|
|
|
|
ngx_mail_core_srv_conf_t *cscf;
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-12-07 22:51:31 +08:00
|
|
|
s->connection->log->action = "connecting to upstream";
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
|
2005-12-07 22:51:31 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
p = ngx_pcalloc(s->connection->pool, sizeof(ngx_mail_proxy_ctx_t));
|
2005-03-19 20:38:37 +08:00
|
|
|
if (p == NULL) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_session_internal_server_error(s);
|
2004-09-09 23:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->proxy = p;
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
p->upstream.sockaddr = peer->sockaddr;
|
|
|
|
p->upstream.socklen = peer->socklen;
|
|
|
|
p->upstream.name = &peer->name;
|
|
|
|
p->upstream.get = ngx_event_get_peer;
|
2004-09-09 23:40:48 +08:00
|
|
|
p->upstream.log = s->connection->log;
|
|
|
|
p->upstream.log_error = NGX_ERROR_ERR;
|
|
|
|
|
|
|
|
rc = ngx_event_connect_peer(&p->upstream);
|
|
|
|
|
2005-09-23 19:02:22 +08:00
|
|
|
if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
2004-09-09 23:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
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;
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
s->connection->read->handler = ngx_mail_proxy_block_read;
|
|
|
|
p->upstream.connection->write->handler = ngx_mail_proxy_dummy_handler;
|
2005-07-08 22:34:20 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
2005-07-08 22:34:20 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
s->proxy->buffer = ngx_create_temp_buf(s->connection->pool,
|
|
|
|
pcf->buffer_size);
|
|
|
|
if (s->proxy->buffer == NULL) {
|
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-29 17:35:06 +08:00
|
|
|
s->out.len = 0;
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
switch (s->protocol) {
|
|
|
|
|
|
|
|
case NGX_MAIL_POP3_PROTOCOL:
|
|
|
|
p->upstream.connection->read->handler = ngx_mail_proxy_pop3_handler;
|
|
|
|
s->mail_state = ngx_pop3_start;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NGX_MAIL_IMAP_PROTOCOL:
|
|
|
|
p->upstream.connection->read->handler = ngx_mail_proxy_imap_handler;
|
|
|
|
s->mail_state = ngx_imap_start;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* NGX_MAIL_SMTP_PROTOCOL */
|
|
|
|
p->upstream.connection->read->handler = ngx_mail_proxy_smtp_handler;
|
|
|
|
s->mail_state = ngx_smtp_start;
|
|
|
|
break;
|
2005-07-08 22:34:20 +08:00
|
|
|
}
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static void
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_block_read(ngx_event_t *rev)
|
2004-09-09 23:40:48 +08:00
|
|
|
{
|
|
|
|
ngx_connection_t *c;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_session_t *s;
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy block read");
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2008-12-10 01:27:48 +08:00
|
|
|
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
|
2004-09-09 23:40:48 +08:00
|
|
|
c = rev->data;
|
|
|
|
s = c->data;
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_close_session(s);
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static void
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_pop3_handler(ngx_event_t *rev)
|
2004-09-09 23:40:48 +08:00
|
|
|
{
|
2005-09-07 00:09:32 +08:00
|
|
|
u_char *p;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_str_t line;
|
|
|
|
ngx_connection_t *c;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_session_t *s;
|
|
|
|
ngx_mail_proxy_conf_t *pcf;
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
|
|
"mail proxy pop3 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");
|
2005-11-15 21:30:52 +08:00
|
|
|
c->timedout = 1;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
2004-09-14 00:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
rc = ngx_mail_proxy_read_response(s, 0);
|
|
|
|
|
|
|
|
if (rc == NGX_AGAIN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == NGX_ERROR) {
|
|
|
|
ngx_mail_proxy_upstream_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (s->mail_state) {
|
|
|
|
|
|
|
|
case ngx_pop3_start:
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user");
|
|
|
|
|
|
|
|
s->connection->log->action = "sending user name to upstream";
|
|
|
|
|
|
|
|
line.len = sizeof("USER ") - 1 + s->login.len + 2;
|
2008-06-17 23:00:30 +08:00
|
|
|
line.data = ngx_pnalloc(c->pool, line.len);
|
2007-03-19 21:36:56 +08:00
|
|
|
if (line.data == NULL) {
|
|
|
|
ngx_mail_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->mail_state = ngx_pop3_user;
|
|
|
|
break;
|
2005-07-08 22:34:20 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
case ngx_pop3_user:
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send pass");
|
|
|
|
|
|
|
|
s->connection->log->action = "sending password to upstream";
|
|
|
|
|
|
|
|
line.len = sizeof("PASS ") - 1 + s->passwd.len + 2;
|
2008-06-17 23:00:30 +08:00
|
|
|
line.data = ngx_pnalloc(c->pool, line.len);
|
2007-03-19 21:36:56 +08:00
|
|
|
if (line.data == NULL) {
|
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
2004-09-09 23:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
2007-03-19 21:36:56 +08:00
|
|
|
|
|
|
|
p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1);
|
|
|
|
p = ngx_cpymem(p, s->passwd.data, s->passwd.len);
|
|
|
|
*p++ = CR; *p = LF;
|
|
|
|
|
|
|
|
s->mail_state = ngx_pop3_passwd;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ngx_pop3_passwd:
|
|
|
|
s->connection->read->handler = ngx_mail_proxy_handler;
|
|
|
|
s->connection->write->handler = ngx_mail_proxy_handler;
|
|
|
|
rev->handler = ngx_mail_proxy_handler;
|
|
|
|
c->write->handler = ngx_mail_proxy_handler;
|
|
|
|
|
|
|
|
pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
|
|
|
ngx_add_timer(s->connection->read, pcf->timeout);
|
|
|
|
ngx_del_timer(c->read);
|
|
|
|
|
|
|
|
c->log->action = NULL;
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
|
|
|
|
|
|
|
|
ngx_mail_proxy_handler(s->connection->write);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
#if (NGX_SUPPRESS_WARN)
|
2010-05-14 17:56:37 +08:00
|
|
|
ngx_str_null(&line);
|
2007-03-19 21:36:56 +08:00
|
|
|
#endif
|
|
|
|
break;
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
if (c->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_mail_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
|
|
s->proxy->buffer->last = s->proxy->buffer->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_mail_proxy_imap_handler(ngx_event_t *rev)
|
|
|
|
{
|
|
|
|
u_char *p;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_str_t line;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
ngx_mail_session_t *s;
|
|
|
|
ngx_mail_proxy_conf_t *pcf;
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
|
|
"mail proxy imap auth handler");
|
|
|
|
|
|
|
|
c = rev->data;
|
|
|
|
s = c->data;
|
|
|
|
|
|
|
|
if (rev->timedout) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
|
|
"upstream timed out");
|
|
|
|
c->timedout = 1;
|
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ngx_mail_proxy_read_response(s, s->mail_state);
|
2004-09-09 23:40:48 +08:00
|
|
|
|
|
|
|
if (rc == NGX_AGAIN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-10-27 23:46:13 +08:00
|
|
|
if (rc == NGX_ERROR) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_upstream_error(s);
|
2004-09-12 04:22:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
switch (s->mail_state) {
|
2005-07-08 22:34:20 +08:00
|
|
|
|
|
|
|
case ngx_imap_start:
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
|
|
"mail proxy send login");
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-12-07 22:51:31 +08:00
|
|
|
s->connection->log->action = "sending LOGIN command to upstream";
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
line.len = s->tag.len + sizeof("LOGIN ") - 1
|
|
|
|
+ 1 + NGX_SIZE_T_LEN + 1 + 2;
|
2008-06-17 23:00:30 +08:00
|
|
|
line.data = ngx_pnalloc(c->pool, line.len);
|
2005-07-08 22:34:20 +08:00
|
|
|
if (line.data == NULL) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
2005-07-08 22:34:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF,
|
|
|
|
&s->tag, s->login.len)
|
|
|
|
- line.data;
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
s->mail_state = ngx_imap_login;
|
2005-07-08 22:34:20 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ngx_imap_login:
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user");
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-12-07 22:51:31 +08:00
|
|
|
s->connection->log->action = "sending user name to upstream";
|
|
|
|
|
2005-07-14 20:51:53 +08:00
|
|
|
line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2;
|
2008-06-17 23:00:30 +08:00
|
|
|
line.data = ngx_pnalloc(c->pool, line.len);
|
2005-03-19 20:38:37 +08:00
|
|
|
if (line.data == NULL) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
2004-09-09 23:40:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-07-14 20:51:53 +08:00
|
|
|
line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF,
|
2005-07-08 22:34:20 +08:00
|
|
|
&s->login, s->passwd.len)
|
|
|
|
- line.data;
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
s->mail_state = ngx_imap_user;
|
2005-07-08 22:34:20 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ngx_imap_user:
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
|
|
"mail proxy send passwd");
|
2004-09-12 04:22:11 +08:00
|
|
|
|
2005-12-07 22:51:31 +08:00
|
|
|
s->connection->log->action = "sending password to upstream";
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
line.len = s->passwd.len + 2;
|
2008-06-17 23:00:30 +08:00
|
|
|
line.data = ngx_pnalloc(c->pool, line.len);
|
2005-07-08 22:34:20 +08:00
|
|
|
if (line.data == NULL) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_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;
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
s->mail_state = ngx_imap_passwd;
|
2005-07-08 22:34:20 +08:00
|
|
|
break;
|
2004-09-12 04:22:11 +08:00
|
|
|
|
2006-07-08 00:33:19 +08:00
|
|
|
case ngx_imap_passwd:
|
2007-03-19 21:36:56 +08:00
|
|
|
s->connection->read->handler = ngx_mail_proxy_handler;
|
|
|
|
s->connection->write->handler = ngx_mail_proxy_handler;
|
|
|
|
rev->handler = ngx_mail_proxy_handler;
|
|
|
|
c->write->handler = ngx_mail_proxy_handler;
|
2006-07-08 00:33:19 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
2006-07-08 00:33:19 +08:00
|
|
|
ngx_add_timer(s->connection->read, pcf->timeout);
|
|
|
|
ngx_del_timer(c->read);
|
|
|
|
|
|
|
|
c->log->action = NULL;
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_handler(s->connection->write);
|
2006-07-08 00:33:19 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
default:
|
|
|
|
#if (NGX_SUPPRESS_WARN)
|
2010-05-14 17:56:37 +08:00
|
|
|
ngx_str_null(&line);
|
2005-07-08 22:34:20 +08:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2004-09-12 04:22:11 +08:00
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
|
2005-07-08 22:34:20 +08:00
|
|
|
/*
|
|
|
|
* we treat the incomplete sending as NGX_ERROR
|
|
|
|
* because it is very strange here
|
|
|
|
*/
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_smtp_handler(ngx_event_t *rev)
|
2005-07-08 22:34:20 +08:00
|
|
|
{
|
2007-03-19 21:36:56 +08:00
|
|
|
u_char *p;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_str_t line;
|
2008-11-13 21:25:34 +08:00
|
|
|
ngx_buf_t *b;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_connection_t *c;
|
|
|
|
ngx_mail_session_t *s;
|
|
|
|
ngx_mail_proxy_conf_t *pcf;
|
|
|
|
ngx_mail_core_srv_conf_t *cscf;
|
2005-07-08 22:34:20 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
|
|
"mail proxy smtp auth handler");
|
2005-07-08 22:34:20 +08:00
|
|
|
|
|
|
|
c = rev->data;
|
|
|
|
s = c->data;
|
|
|
|
|
|
|
|
if (rev->timedout) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
|
|
"upstream timed out");
|
2005-11-15 21:30:52 +08:00
|
|
|
c->timedout = 1;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
2005-07-08 22:34:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
rc = ngx_mail_proxy_read_response(s, s->mail_state);
|
2005-07-08 22:34:20 +08:00
|
|
|
|
|
|
|
if (rc == NGX_AGAIN) {
|
2004-09-12 04:22:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-10-27 23:46:13 +08:00
|
|
|
if (rc == NGX_ERROR) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_upstream_error(s);
|
2005-07-08 22:34:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
switch (s->mail_state) {
|
2005-07-08 22:34:20 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
case ngx_smtp_start:
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send ehlo");
|
2005-07-08 22:34:20 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
s->connection->log->action = "sending HELO/EHLO to upstream";
|
2005-12-07 22:51:31 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
|
|
|
|
|
|
|
|
line.len = sizeof("HELO ") - 1 + cscf->server_name.len + 2;
|
2008-06-17 23:00:30 +08:00
|
|
|
line.data = ngx_pnalloc(c->pool, line.len);
|
2005-07-08 22:34:20 +08:00
|
|
|
if (line.data == NULL) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
2005-07-08 22:34:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
|
|
|
|
|
|
|
p = ngx_cpymem(line.data,
|
|
|
|
((s->esmtp || pcf->xclient) ? "EHLO " : "HELO "),
|
|
|
|
sizeof("HELO ") - 1);
|
|
|
|
|
|
|
|
p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
|
2005-07-08 22:34:20 +08:00
|
|
|
*p++ = CR; *p = LF;
|
|
|
|
|
2008-11-13 21:25:34 +08:00
|
|
|
if (pcf->xclient) {
|
|
|
|
s->mail_state = ngx_smtp_helo_xclient;
|
|
|
|
|
|
|
|
} else if (s->auth_method == NGX_MAIL_AUTH_NONE) {
|
|
|
|
s->mail_state = ngx_smtp_helo_from;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
s->mail_state = ngx_smtp_helo;
|
|
|
|
}
|
2007-03-19 21:36:56 +08:00
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
break;
|
|
|
|
|
2008-11-13 21:25:34 +08:00
|
|
|
case ngx_smtp_helo_xclient:
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
|
|
"mail proxy send xclient");
|
2005-07-08 22:34:20 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
s->connection->log->action = "sending XCLIENT to upstream";
|
|
|
|
|
2009-02-10 23:19:45 +08:00
|
|
|
line.len = sizeof("XCLIENT ADDR= LOGIN= NAME="
|
2008-02-13 21:50:04 +08:00
|
|
|
CRLF) - 1
|
|
|
|
+ s->connection->addr_text.len + s->login.len + s->host.len;
|
2005-12-07 22:51:31 +08:00
|
|
|
|
2014-01-18 02:06:04 +08:00
|
|
|
#if (NGX_HAVE_INET6)
|
|
|
|
if (s->connection->sockaddr->sa_family == AF_INET6) {
|
|
|
|
line.len += sizeof("IPV6:") - 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-17 23:00:30 +08:00
|
|
|
line.data = ngx_pnalloc(c->pool, line.len);
|
2005-07-08 22:34:20 +08:00
|
|
|
if (line.data == NULL) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
2005-07-08 22:34:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-18 02:06:04 +08:00
|
|
|
p = ngx_cpymem(line.data, "XCLIENT ADDR=", sizeof("XCLIENT ADDR=") - 1);
|
|
|
|
|
|
|
|
#if (NGX_HAVE_INET6)
|
|
|
|
if (s->connection->sockaddr->sa_family == AF_INET6) {
|
|
|
|
p = ngx_cpymem(p, "IPV6:", sizeof("IPV6:") - 1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
p = ngx_copy(p, s->connection->addr_text.data,
|
|
|
|
s->connection->addr_text.len);
|
|
|
|
|
|
|
|
if (s->login.len) {
|
|
|
|
p = ngx_cpymem(p, " LOGIN=", sizeof(" LOGIN=") - 1);
|
|
|
|
p = ngx_copy(p, s->login.data, s->login.len);
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ngx_cpymem(p, " NAME=", sizeof(" NAME=") - 1);
|
|
|
|
p = ngx_copy(p, s->host.data, s->host.len);
|
|
|
|
|
|
|
|
*p++ = CR; *p++ = LF;
|
|
|
|
|
|
|
|
line.len = p - line.data;
|
2008-11-13 21:25:34 +08:00
|
|
|
|
2009-02-10 23:19:45 +08:00
|
|
|
if (s->smtp_helo.len) {
|
|
|
|
s->mail_state = ngx_smtp_xclient_helo;
|
|
|
|
|
|
|
|
} else if (s->auth_method == NGX_MAIL_AUTH_NONE) {
|
|
|
|
s->mail_state = ngx_smtp_xclient_from;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
s->mail_state = ngx_smtp_xclient;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ngx_smtp_xclient_helo:
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
|
|
"mail proxy send client ehlo");
|
|
|
|
|
|
|
|
s->connection->log->action = "sending client HELO/EHLO to upstream";
|
|
|
|
|
|
|
|
line.len = sizeof("HELO " CRLF) - 1 + s->smtp_helo.len;
|
|
|
|
|
|
|
|
line.data = ngx_pnalloc(c->pool, line.len);
|
|
|
|
if (line.data == NULL) {
|
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
line.len = ngx_sprintf(line.data,
|
|
|
|
((s->esmtp) ? "EHLO %V" CRLF : "HELO %V" CRLF),
|
|
|
|
&s->smtp_helo)
|
|
|
|
- line.data;
|
|
|
|
|
2008-11-13 21:25:34 +08:00
|
|
|
s->mail_state = (s->auth_method == NGX_MAIL_AUTH_NONE) ?
|
2009-02-10 23:19:45 +08:00
|
|
|
ngx_smtp_helo_from : ngx_smtp_helo;
|
2008-11-13 21:25:34 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ngx_smtp_helo_from:
|
|
|
|
case ngx_smtp_xclient_from:
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
|
|
"mail proxy send mail from");
|
|
|
|
|
|
|
|
s->connection->log->action = "sending MAIL FROM to upstream";
|
|
|
|
|
|
|
|
line.len = s->smtp_from.len + sizeof(CRLF) - 1;
|
|
|
|
line.data = ngx_pnalloc(c->pool, line.len);
|
|
|
|
if (line.data == NULL) {
|
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ngx_cpymem(line.data, s->smtp_from.data, s->smtp_from.len);
|
|
|
|
*p++ = CR; *p = LF;
|
|
|
|
|
|
|
|
s->mail_state = ngx_smtp_from;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ngx_smtp_from:
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
|
|
|
|
"mail proxy send rcpt to");
|
|
|
|
|
|
|
|
s->connection->log->action = "sending RCPT TO to upstream";
|
|
|
|
|
|
|
|
line.len = s->smtp_to.len + sizeof(CRLF) - 1;
|
|
|
|
line.data = ngx_pnalloc(c->pool, line.len);
|
|
|
|
if (line.data == NULL) {
|
|
|
|
ngx_mail_proxy_internal_server_error(s);
|
|
|
|
return;
|
2007-03-19 21:36:56 +08:00
|
|
|
}
|
2005-07-08 22:34:20 +08:00
|
|
|
|
2008-11-13 21:25:34 +08:00
|
|
|
p = ngx_cpymem(line.data, s->smtp_to.data, s->smtp_to.len);
|
|
|
|
*p++ = CR; *p = LF;
|
|
|
|
|
|
|
|
s->mail_state = ngx_smtp_to;
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
break;
|
|
|
|
|
2008-11-13 21:25:34 +08:00
|
|
|
case ngx_smtp_helo:
|
2007-03-19 21:36:56 +08:00
|
|
|
case ngx_smtp_xclient:
|
2008-11-13 21:25:34 +08:00
|
|
|
case ngx_smtp_to:
|
|
|
|
|
|
|
|
b = s->proxy->buffer;
|
|
|
|
|
|
|
|
if (s->auth_method == NGX_MAIL_AUTH_NONE) {
|
2008-11-13 22:20:54 +08:00
|
|
|
b->pos = b->start;
|
2006-07-08 00:33:19 +08:00
|
|
|
|
2008-11-13 21:25:34 +08:00
|
|
|
} else {
|
2009-01-13 14:47:29 +08:00
|
|
|
ngx_memcpy(b->start, smtp_auth_ok, sizeof(smtp_auth_ok) - 1);
|
|
|
|
b->last = b->start + sizeof(smtp_auth_ok) - 1;
|
2008-11-13 21:25:34 +08:00
|
|
|
}
|
2007-03-19 21:36:56 +08:00
|
|
|
|
|
|
|
s->connection->read->handler = ngx_mail_proxy_handler;
|
|
|
|
s->connection->write->handler = ngx_mail_proxy_handler;
|
|
|
|
rev->handler = ngx_mail_proxy_handler;
|
|
|
|
c->write->handler = ngx_mail_proxy_handler;
|
|
|
|
|
|
|
|
pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
2006-07-08 00:33:19 +08:00
|
|
|
ngx_add_timer(s->connection->read, pcf->timeout);
|
|
|
|
ngx_del_timer(c->read);
|
|
|
|
|
|
|
|
c->log->action = NULL;
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
|
|
|
|
|
2013-10-01 02:09:57 +08:00
|
|
|
if (s->buffer->pos == s->buffer->last) {
|
|
|
|
ngx_mail_proxy_handler(s->connection->write);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_mail_proxy_handler(c->write);
|
|
|
|
}
|
2006-07-08 00:33:19 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
default:
|
|
|
|
#if (NGX_SUPPRESS_WARN)
|
2010-05-14 17:56:37 +08:00
|
|
|
ngx_str_null(&line);
|
2005-07-08 22:34:20 +08:00
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2004-09-12 04:22:11 +08:00
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
|
2004-09-12 04:22:11 +08:00
|
|
|
/*
|
|
|
|
* we treat the incomplete sending as NGX_ERROR
|
|
|
|
* because it is very strange here
|
|
|
|
*/
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_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;
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static void
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_dummy_handler(ngx_event_t *wev)
|
2004-09-09 23:40:48 +08:00
|
|
|
{
|
2005-12-05 21:18:09 +08:00
|
|
|
ngx_connection_t *c;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_session_t *s;
|
2005-12-05 21:18:09 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy dummy handler");
|
2005-12-05 21:18:09 +08:00
|
|
|
|
2008-12-10 01:27:48 +08:00
|
|
|
if (ngx_handle_write_event(wev, 0) != NGX_OK) {
|
2005-12-05 21:18:09 +08:00
|
|
|
c = wev->data;
|
|
|
|
s = c->data;
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_close_session(s);
|
2005-12-05 21:18:09 +08:00
|
|
|
}
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static ngx_int_t
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_read_response(ngx_mail_session_t *s, ngx_uint_t state)
|
2004-09-09 23:40:48 +08:00
|
|
|
{
|
2013-10-01 02:10:03 +08:00
|
|
|
u_char *p, *m;
|
2006-11-17 22:22:51 +08:00
|
|
|
ssize_t n;
|
|
|
|
ngx_buf_t *b;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_conf_t *pcf;
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-12-07 22:51:31 +08:00
|
|
|
s->connection->log->action = "reading response from upstream";
|
|
|
|
|
2004-09-09 23:40:48 +08:00
|
|
|
b = s->proxy->buffer;
|
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection,
|
|
|
|
b->last, b->end - b->last);
|
2004-09-09 23:40:48 +08:00
|
|
|
|
|
|
|
if (n == NGX_ERROR || n == 0) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == NGX_AGAIN) {
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
b->last += n;
|
|
|
|
|
2010-04-23 17:53:52 +08:00
|
|
|
if (b->last - b->pos < 4) {
|
2004-09-09 23:40:48 +08:00
|
|
|
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);
|
2005-10-27 23:46:13 +08:00
|
|
|
return NGX_ERROR;
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = b->pos;
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
switch (s->protocol) {
|
|
|
|
|
|
|
|
case NGX_MAIL_POP3_PROTOCOL:
|
2005-07-08 22:34:20 +08:00
|
|
|
if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
2007-03-19 21:36:56 +08:00
|
|
|
break;
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
case NGX_MAIL_IMAP_PROTOCOL:
|
2006-07-08 00:33:19 +08:00
|
|
|
switch (state) {
|
|
|
|
|
|
|
|
case ngx_imap_start:
|
2005-07-08 22:34:20 +08:00
|
|
|
if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
2006-07-08 00:33:19 +08:00
|
|
|
break;
|
2005-07-08 22:34:20 +08:00
|
|
|
|
2006-07-08 00:33:19 +08:00
|
|
|
case ngx_imap_login:
|
|
|
|
case ngx_imap_user:
|
2005-07-14 20:51:53 +08:00
|
|
|
if (p[0] == '+') {
|
2005-07-08 22:34:20 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
2006-07-08 00:33:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ngx_imap_passwd:
|
|
|
|
if (ngx_strncmp(p, s->tag.data, s->tag.len) == 0) {
|
|
|
|
p += s->tag.len;
|
|
|
|
if (p[0] == 'O' && p[1] == 'K') {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2005-07-08 22:34:20 +08:00
|
|
|
}
|
2007-03-19 21:36:56 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* NGX_MAIL_SMTP_PROTOCOL */
|
2013-10-01 02:10:03 +08:00
|
|
|
|
|
|
|
if (p[3] == '-') {
|
|
|
|
/* multiline reply, check if we got last line */
|
|
|
|
|
|
|
|
m = b->last - (sizeof(CRLF "200" CRLF) - 1);
|
|
|
|
|
|
|
|
while (m > p) {
|
|
|
|
if (m[0] == CR && m[1] == LF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
m--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m <= p || m[5] == '-') {
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
switch (state) {
|
|
|
|
|
2008-11-13 22:20:54 +08:00
|
|
|
case ngx_smtp_start:
|
|
|
|
if (p[0] == '2' && p[1] == '2' && p[2] == '0') {
|
2007-04-18 23:21:28 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
break;
|
2007-03-19 21:36:56 +08:00
|
|
|
|
2008-11-13 22:20:54 +08:00
|
|
|
case ngx_smtp_helo:
|
|
|
|
case ngx_smtp_helo_xclient:
|
|
|
|
case ngx_smtp_helo_from:
|
|
|
|
case ngx_smtp_from:
|
|
|
|
if (p[0] == '2' && p[1] == '5' && p[2] == '0') {
|
2007-04-18 23:21:28 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
break;
|
2008-11-13 21:25:34 +08:00
|
|
|
|
|
|
|
case ngx_smtp_xclient:
|
|
|
|
case ngx_smtp_xclient_from:
|
2009-02-10 23:19:45 +08:00
|
|
|
case ngx_smtp_xclient_helo:
|
2008-11-13 21:25:34 +08:00
|
|
|
if (p[0] == '2' && (p[1] == '2' || p[1] == '5') && p[2] == '0') {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
break;
|
2008-11-13 22:20:54 +08:00
|
|
|
|
|
|
|
case ngx_smtp_to:
|
|
|
|
return NGX_OK;
|
2007-03-19 21:36:56 +08:00
|
|
|
}
|
|
|
|
|
2007-04-18 23:21:28 +08:00
|
|
|
break;
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
2006-11-17 22:22:51 +08:00
|
|
|
|
|
|
|
if (pcf->pass_error_message == 0) {
|
|
|
|
*(b->last - 2) = '\0';
|
|
|
|
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
|
|
|
|
"upstream sent invalid response: \"%s\"", p);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->out.len = b->last - p - 2;
|
|
|
|
s->out.data = p;
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
|
|
|
|
"upstream sent invalid response: \"%V\"", &s->out);
|
|
|
|
|
|
|
|
s->out.len = b->last - b->pos;
|
|
|
|
s->out.data = b->pos;
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-10-27 23:46:13 +08:00
|
|
|
return NGX_ERROR;
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static void
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_handler(ngx_event_t *ev)
|
2004-09-09 23:40:48 +08:00
|
|
|
{
|
2005-12-05 21:18:09 +08:00
|
|
|
char *action, *recv_action, *send_action;
|
2005-09-07 00:09:32 +08:00
|
|
|
size_t size;
|
|
|
|
ssize_t n;
|
|
|
|
ngx_buf_t *b;
|
2005-12-05 21:18:09 +08:00
|
|
|
ngx_uint_t do_write;
|
2005-09-07 00:09:32 +08:00
|
|
|
ngx_connection_t *c, *src, *dst;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_session_t *s;
|
|
|
|
ngx_mail_proxy_conf_t *pcf;
|
2004-09-08 13:18:51 +08:00
|
|
|
|
|
|
|
c = ev->data;
|
|
|
|
s = c->data;
|
|
|
|
|
2004-09-14 00:18:09 +08:00
|
|
|
if (ev->timedout) {
|
2005-12-05 21:18:09 +08:00
|
|
|
c->log->action = "proxying";
|
|
|
|
|
2004-09-14 00:18:09 +08:00
|
|
|
if (c == s->connection) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
|
|
"client timed out");
|
2005-11-15 21:30:52 +08:00
|
|
|
c->timedout = 1;
|
|
|
|
|
2004-09-14 00:18:09 +08:00
|
|
|
} else {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
|
|
|
|
"upstream timed out");
|
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_close_session(s);
|
2004-09-14 00:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-09-08 13:18:51 +08:00
|
|
|
if (c == s->connection) {
|
2004-09-14 00:18:09 +08:00
|
|
|
if (ev->write) {
|
2005-12-05 21:18:09 +08:00
|
|
|
recv_action = "proxying and reading from upstream";
|
|
|
|
send_action = "proxying and sending to client";
|
2004-09-14 00:18:09 +08:00
|
|
|
src = s->proxy->upstream.connection;
|
|
|
|
dst = c;
|
|
|
|
b = s->proxy->buffer;
|
|
|
|
|
|
|
|
} else {
|
2005-12-05 21:18:09 +08:00
|
|
|
recv_action = "proxying and reading from client";
|
|
|
|
send_action = "proxying and sending to upstream";
|
2004-09-14 00:18:09 +08:00
|
|
|
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) {
|
2005-12-16 23:07:08 +08:00
|
|
|
recv_action = "proxying and reading from client";
|
|
|
|
send_action = "proxying and sending to upstream";
|
2004-09-14 00:18:09 +08:00
|
|
|
src = s->connection;
|
|
|
|
dst = c;
|
|
|
|
b = s->buffer;
|
|
|
|
|
|
|
|
} else {
|
2005-12-16 23:07:08 +08:00
|
|
|
recv_action = "proxying and reading from upstream";
|
|
|
|
send_action = "proxying and sending to client";
|
2004-09-14 00:18:09 +08:00
|
|
|
src = c;
|
|
|
|
dst = s->connection;
|
|
|
|
b = s->proxy->buffer;
|
|
|
|
}
|
2004-09-08 13:18:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
do_write = ev->write ? 1 : 0;
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug3(NGX_LOG_DEBUG_MAIL, ev->log, 0,
|
|
|
|
"mail proxy handler: %d, #%d > #%d",
|
2004-09-09 23:40:48 +08:00
|
|
|
do_write, src->fd, dst->fd);
|
|
|
|
|
2005-12-05 21:18:09 +08:00
|
|
|
for ( ;; ) {
|
2004-09-08 13:18:51 +08:00
|
|
|
|
2005-12-05 21:18:09 +08:00
|
|
|
if (do_write) {
|
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) {
|
2005-12-05 21:18:09 +08:00
|
|
|
c->log->action = send_action;
|
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
n = dst->send(dst, b->pos, size);
|
2004-09-08 13:18:51 +08:00
|
|
|
|
|
|
|
if (n == NGX_ERROR) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_close_session(s);
|
2004-09-08 13:18:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
b->pos += n;
|
|
|
|
|
|
|
|
if (b->pos == b->last) {
|
|
|
|
b->pos = b->start;
|
|
|
|
b->last = b->start;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2005-12-05 21:18:09 +08:00
|
|
|
c->log->action = recv_action;
|
2005-09-30 22:41:25 +08:00
|
|
|
|
2005-12-05 21:18:09 +08:00
|
|
|
n = src->recv(src, b->last, size);
|
2005-09-30 22:41:25 +08:00
|
|
|
|
2005-12-05 21:18:09 +08:00
|
|
|
if (n == NGX_AGAIN || n == 0) {
|
|
|
|
break;
|
2004-09-08 13:18:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
do_write = 1;
|
|
|
|
b->last += n;
|
2004-09-09 23:40:48 +08:00
|
|
|
|
2005-12-05 21:18:09 +08:00
|
|
|
continue;
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
2005-09-07 00:09:32 +08:00
|
|
|
|
2005-12-05 21:18:09 +08:00
|
|
|
if (n == NGX_ERROR) {
|
|
|
|
src->read->eof = 1;
|
2005-09-07 00:09:32 +08:00
|
|
|
}
|
2004-09-08 13:18:51 +08:00
|
|
|
}
|
|
|
|
|
2005-12-05 21:18:09 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->log->action = "proxying";
|
|
|
|
|
2007-07-25 02:08:04 +08:00
|
|
|
if ((s->connection->read->eof && s->buffer->pos == s->buffer->last)
|
|
|
|
|| (s->proxy->upstream.connection->read->eof
|
|
|
|
&& s->proxy->buffer->pos == s->proxy->buffer->last)
|
|
|
|
|| (s->connection->read->eof
|
|
|
|
&& s->proxy->upstream.connection->read->eof))
|
2005-12-05 21:18:09 +08:00
|
|
|
{
|
|
|
|
action = c->log->action;
|
|
|
|
c->log->action = NULL;
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
|
|
|
|
c->log->action = action;
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_close_session(s);
|
2005-12-05 21:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-10 01:27:48 +08:00
|
|
|
if (ngx_handle_write_event(dst->write, 0) != NGX_OK) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_close_session(s);
|
2005-12-05 21:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-10 01:27:48 +08:00
|
|
|
if (ngx_handle_read_event(dst->read, 0) != NGX_OK) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_close_session(s);
|
2005-12-05 21:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-10 01:27:48 +08:00
|
|
|
if (ngx_handle_write_event(src->write, 0) != NGX_OK) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_close_session(s);
|
2005-12-05 21:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-10 01:27:48 +08:00
|
|
|
if (ngx_handle_read_event(src->read, 0) != NGX_OK) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_close_session(s);
|
2005-12-05 21:18:09 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == s->connection) {
|
2007-03-19 21:36:56 +08:00
|
|
|
pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
|
2005-12-05 21:18:09 +08:00
|
|
|
ngx_add_timer(c->read, pcf->timeout);
|
|
|
|
}
|
2004-09-08 13:18:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-17 22:22:51 +08:00
|
|
|
static void
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_upstream_error(ngx_mail_session_t *s)
|
2006-11-17 22:22:51 +08:00
|
|
|
{
|
|
|
|
if (s->proxy->upstream.connection) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
|
|
|
|
"close mail proxy connection: %d",
|
2006-11-17 22:22:51 +08:00
|
|
|
s->proxy->upstream.connection->fd);
|
|
|
|
|
|
|
|
ngx_close_connection(s->proxy->upstream.connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->out.len == 0) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_session_internal_server_error(s);
|
2006-11-17 22:22:51 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->quit = 1;
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_send(s->connection->write);
|
2006-11-17 22:22:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-07-08 22:34:20 +08:00
|
|
|
static void
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s)
|
2005-07-08 22:34:20 +08:00
|
|
|
{
|
|
|
|
if (s->proxy->upstream.connection) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
|
|
|
|
"close mail proxy connection: %d",
|
2005-07-08 22:34:20 +08:00
|
|
|
s->proxy->upstream.connection->fd);
|
|
|
|
|
|
|
|
ngx_close_connection(s->proxy->upstream.connection);
|
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_session_internal_server_error(s);
|
2005-07-08 22:34:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-07 23:56:31 +08:00
|
|
|
static void
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_close_session(ngx_mail_session_t *s)
|
2004-09-08 13:18:51 +08:00
|
|
|
{
|
2005-06-07 23:56:31 +08:00
|
|
|
if (s->proxy->upstream.connection) {
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
|
|
|
|
"close mail proxy connection: %d",
|
2005-06-07 23:56:31 +08:00
|
|
|
s->proxy->upstream.connection->fd);
|
|
|
|
|
|
|
|
ngx_close_connection(s->proxy->upstream.connection);
|
2004-09-09 23:40:48 +08:00
|
|
|
}
|
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_close_connection(s->connection);
|
2004-09-08 13:18:51 +08:00
|
|
|
}
|
2005-06-07 23:56:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void *
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_create_conf(ngx_conf_t *cf)
|
2005-11-15 21:30:52 +08:00
|
|
|
{
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_conf_t *pcf;
|
2005-11-15 21:30:52 +08:00
|
|
|
|
2007-03-19 21:36:56 +08:00
|
|
|
pcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_proxy_conf_t));
|
2005-06-07 23:56:31 +08:00
|
|
|
if (pcf == NULL) {
|
2009-06-03 00:09:44 +08:00
|
|
|
return NULL;
|
2005-06-07 23:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pcf->enable = NGX_CONF_UNSET;
|
2006-11-17 22:22:51 +08:00
|
|
|
pcf->pass_error_message = NGX_CONF_UNSET;
|
2007-03-19 21:36:56 +08:00
|
|
|
pcf->xclient = NGX_CONF_UNSET;
|
2005-09-07 00:09:32 +08:00
|
|
|
pcf->buffer_size = NGX_CONF_UNSET_SIZE;
|
|
|
|
pcf->timeout = NGX_CONF_UNSET_MSEC;
|
2005-06-07 23:56:31 +08:00
|
|
|
|
|
|
|
return pcf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
2005-06-07 23:56:31 +08:00
|
|
|
{
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_mail_proxy_conf_t *prev = parent;
|
|
|
|
ngx_mail_proxy_conf_t *conf = child;
|
2005-06-07 23:56:31 +08:00
|
|
|
|
2005-09-07 00:09:32 +08:00
|
|
|
ngx_conf_merge_value(conf->enable, prev->enable, 0);
|
2006-11-17 22:22:51 +08:00
|
|
|
ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0);
|
2007-03-19 21:36:56 +08:00
|
|
|
ngx_conf_merge_value(conf->xclient, prev->xclient, 1);
|
2005-09-07 00:09:32 +08:00
|
|
|
ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
|
|
|
|
(size_t) ngx_pagesize);
|
|
|
|
ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000);
|
2005-06-07 23:56:31 +08:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|