Mercurial > hg > nginx-quic
diff src/mail/ngx_mail_proxy_module.c @ 1136:68f30ab68bb7
Many changes:
*) rename imap to mail, sort pop3/imap functions
*) smtp auth support
*) pop3 starttls only
*) fix segfault if cram-md5 was used without apop
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 19 Mar 2007 13:36:56 +0000 |
parents | src/imap/ngx_imap_proxy_module.c@4d68c486fcb0 |
children | bde5e4134759 |
line wrap: on
line diff
copy from src/imap/ngx_imap_proxy_module.c copy to src/mail/ngx_mail_proxy_module.c --- a/src/imap/ngx_imap_proxy_module.c +++ b/src/mail/ngx_mail_proxy_module.c @@ -8,80 +8,89 @@ #include <ngx_core.h> #include <ngx_event.h> #include <ngx_event_connect.h> -#include <ngx_imap.h> +#include <ngx_mail.h> typedef struct { ngx_flag_t enable; ngx_flag_t pass_error_message; + ngx_flag_t xclient; size_t buffer_size; ngx_msec_t timeout; -} ngx_imap_proxy_conf_t; +} ngx_mail_proxy_conf_t; -static void ngx_imap_proxy_block_read(ngx_event_t *rev); -static void ngx_imap_proxy_imap_handler(ngx_event_t *rev); -static void ngx_imap_proxy_pop3_handler(ngx_event_t *rev); -static void ngx_imap_proxy_dummy_handler(ngx_event_t *ev); -static ngx_int_t ngx_imap_proxy_read_response(ngx_imap_session_t *s, +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, ngx_uint_t state); -static void ngx_imap_proxy_handler(ngx_event_t *ev); -static void ngx_imap_proxy_upstream_error(ngx_imap_session_t *s); -static void ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s); -static void ngx_imap_proxy_close_session(ngx_imap_session_t *s); -static void *ngx_imap_proxy_create_conf(ngx_conf_t *cf); -static char *ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent, +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, void *child); -static ngx_command_t ngx_imap_proxy_commands[] = { +static ngx_command_t ngx_mail_proxy_commands[] = { { ngx_string("proxy"), - NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_FLAG, + NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG, ngx_conf_set_flag_slot, - NGX_IMAP_SRV_CONF_OFFSET, - offsetof(ngx_imap_proxy_conf_t, enable), + NGX_MAIL_SRV_CONF_OFFSET, + offsetof(ngx_mail_proxy_conf_t, enable), NULL }, { ngx_string("proxy_buffer"), - NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1, + NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, ngx_conf_set_size_slot, - NGX_IMAP_SRV_CONF_OFFSET, - offsetof(ngx_imap_proxy_conf_t, buffer_size), + NGX_MAIL_SRV_CONF_OFFSET, + offsetof(ngx_mail_proxy_conf_t, buffer_size), NULL }, { ngx_string("proxy_timeout"), - NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1, + NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, ngx_conf_set_msec_slot, - NGX_IMAP_SRV_CONF_OFFSET, - offsetof(ngx_imap_proxy_conf_t, timeout), + NGX_MAIL_SRV_CONF_OFFSET, + offsetof(ngx_mail_proxy_conf_t, timeout), NULL }, { ngx_string("proxy_pass_error_message"), - NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1, + NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, ngx_conf_set_flag_slot, - NGX_IMAP_SRV_CONF_OFFSET, - offsetof(ngx_imap_proxy_conf_t, pass_error_message), + 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), NULL }, ngx_null_command }; -static ngx_imap_module_t ngx_imap_proxy_module_ctx = { +static ngx_mail_module_t ngx_mail_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 */ + ngx_mail_proxy_create_conf, /* create server configuration */ + ngx_mail_proxy_merge_conf /* merge server configuration */ }; -ngx_module_t ngx_imap_proxy_module = { +ngx_module_t ngx_mail_proxy_module = { NGX_MODULE_V1, - &ngx_imap_proxy_module_ctx, /* module context */ - ngx_imap_proxy_commands, /* module directives */ - NGX_IMAP_MODULE, /* module type */ + &ngx_mail_proxy_module_ctx, /* module context */ + ngx_mail_proxy_commands, /* module directives */ + NGX_MAIL_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ NULL, /* init process */ @@ -93,17 +102,21 @@ ngx_module_t ngx_imap_proxy_module = { }; +static u_char smtp_ok[] = "235 2.0.0 OK" CRLF; + + void -ngx_imap_proxy_init(ngx_imap_session_t *s, ngx_peer_addr_t *peer) +ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_peer_addr_t *peer) { int keepalive; ngx_int_t rc; - ngx_imap_proxy_ctx_t *p; - ngx_imap_core_srv_conf_t *cscf; + ngx_mail_proxy_ctx_t *p; + ngx_mail_proxy_conf_t *pcf; + ngx_mail_core_srv_conf_t *cscf; s->connection->log->action = "connecting to upstream"; - cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module); + cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); if (cscf->so_keepalive) { keepalive = 1; @@ -117,9 +130,9 @@ ngx_imap_proxy_init(ngx_imap_session_t * } } - p = ngx_pcalloc(s->connection->pool, sizeof(ngx_imap_proxy_ctx_t)); + p = ngx_pcalloc(s->connection->pool, sizeof(ngx_mail_proxy_ctx_t)); if (p == NULL) { - ngx_imap_session_internal_server_error(s); + ngx_mail_session_internal_server_error(s); return; } @@ -135,7 +148,7 @@ ngx_imap_proxy_init(ngx_imap_session_t * rc = ngx_event_connect_peer(&p->upstream); if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) { - ngx_imap_proxy_internal_server_error(s); + ngx_mail_proxy_internal_server_error(s); return; } @@ -144,49 +157,67 @@ ngx_imap_proxy_init(ngx_imap_session_t * p->upstream.connection->data = s; p->upstream.connection->pool = s->connection->pool; - s->connection->read->handler = ngx_imap_proxy_block_read; - p->upstream.connection->write->handler = ngx_imap_proxy_dummy_handler; + s->connection->read->handler = ngx_mail_proxy_block_read; + p->upstream.connection->write->handler = ngx_mail_proxy_dummy_handler; + + pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); + + 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; + } + + switch (s->protocol) { - if (s->protocol == NGX_IMAP_POP3_PROTOCOL) { - p->upstream.connection->read->handler = ngx_imap_proxy_pop3_handler; - s->imap_state = ngx_pop3_start; + case NGX_MAIL_POP3_PROTOCOL: + p->upstream.connection->read->handler = ngx_mail_proxy_pop3_handler; + s->mail_state = ngx_pop3_start; + break; - } else { - p->upstream.connection->read->handler = ngx_imap_proxy_imap_handler; - s->imap_state = ngx_imap_start; + 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; } } static void -ngx_imap_proxy_block_read(ngx_event_t *rev) +ngx_mail_proxy_block_read(ngx_event_t *rev) { ngx_connection_t *c; - ngx_imap_session_t *s; + ngx_mail_session_t *s; - ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy block read"); + ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy block read"); if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { c = rev->data; s = c->data; - ngx_imap_proxy_close_session(s); + ngx_mail_proxy_close_session(s); } } static void -ngx_imap_proxy_imap_handler(ngx_event_t *rev) +ngx_mail_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_proxy_conf_t *pcf; + ngx_mail_session_t *s; + ngx_mail_proxy_conf_t *pcf; - ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, - "imap proxy imap auth handler"); + ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, + "mail proxy pop3 auth handler"); c = rev->data; s = c->data; @@ -195,106 +226,75 @@ ngx_imap_proxy_imap_handler(ngx_event_t ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "upstream timed out"); c->timedout = 1; - ngx_imap_proxy_internal_server_error(s); + ngx_mail_proxy_internal_server_error(s); return; } - if (s->proxy->buffer == NULL) { - pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); - - s->proxy->buffer = ngx_create_temp_buf(c->pool, pcf->buffer_size); - if (s->proxy->buffer == NULL) { - ngx_imap_proxy_internal_server_error(s); - return; - } - } - - rc = ngx_imap_proxy_read_response(s, s->imap_state); + rc = ngx_mail_proxy_read_response(s, 0); if (rc == NGX_AGAIN) { return; } if (rc == NGX_ERROR) { - ngx_imap_proxy_upstream_error(s); + ngx_mail_proxy_upstream_error(s); return; } - switch (s->imap_state) { - - case ngx_imap_start: - ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, - "imap proxy send login"); - - s->connection->log->action = "sending LOGIN command to upstream"; + switch (s->mail_state) { - 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: - ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user"); + 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 = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2; + 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); + ngx_mail_proxy_internal_server_error(s); return; } - line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF, - &s->login, s->passwd.len) - - line.data; + 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_imap_user; + s->mail_state = ngx_pop3_user; break; - case ngx_imap_user: - ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, - "imap proxy send passwd"); + 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 = s->passwd.len + 2; + 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); + ngx_mail_proxy_internal_server_error(s); return; } - p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len); + 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_imap_passwd; + s->mail_state = ngx_pop3_passwd; break; - case 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; + 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_imap_get_module_srv_conf(s, ngx_imap_proxy_module); + 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_imap_proxy_handler(s->connection->write); + ngx_mail_proxy_handler(s->connection->write); return; @@ -311,7 +311,7 @@ ngx_imap_proxy_imap_handler(ngx_event_t * we treat the incomplete sending as NGX_ERROR * because it is very strange here */ - ngx_imap_proxy_internal_server_error(s); + ngx_mail_proxy_internal_server_error(s); return; } @@ -321,17 +321,17 @@ ngx_imap_proxy_imap_handler(ngx_event_t static void -ngx_imap_proxy_pop3_handler(ngx_event_t *rev) +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_imap_session_t *s; - ngx_imap_proxy_conf_t *pcf; + ngx_mail_session_t *s; + ngx_mail_proxy_conf_t *pcf; - ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, - "imap proxy pop3 auth handler"); + ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, + "mail proxy imap auth handler"); c = rev->data; s = c->data; @@ -340,85 +340,96 @@ ngx_imap_proxy_pop3_handler(ngx_event_t ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "upstream timed out"); c->timedout = 1; - ngx_imap_proxy_internal_server_error(s); + ngx_mail_proxy_internal_server_error(s); return; } - if (s->proxy->buffer == NULL) { - pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); - - s->proxy->buffer = ngx_create_temp_buf(c->pool, pcf->buffer_size); - if (s->proxy->buffer == NULL) { - ngx_imap_proxy_internal_server_error(s); - return; - } - } - - rc = ngx_imap_proxy_read_response(s, 0); + rc = ngx_mail_proxy_read_response(s, s->mail_state); if (rc == NGX_AGAIN) { return; } if (rc == NGX_ERROR) { - ngx_imap_proxy_upstream_error(s); + ngx_mail_proxy_upstream_error(s); return; } - switch (s->imap_state) { + switch (s->mail_state) { - case ngx_pop3_start: - ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user"); + case ngx_imap_start: + ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, + "mail proxy send login"); - s->connection->log->action = "sending user name to upstream"; + s->connection->log->action = "sending LOGIN command to upstream"; - line.len = sizeof("USER ") - 1 + s->login.len + 2; + 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); + 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; + line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF, + &s->tag, s->login.len) + - line.data; - s->imap_state = ngx_pop3_user; + s->mail_state = ngx_imap_login; break; - case ngx_pop3_user: - ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send pass"); + case ngx_imap_login: + 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 = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2; + line.data = ngx_palloc(c->pool, line.len); + if (line.data == NULL) { + ngx_mail_proxy_internal_server_error(s); + return; + } + + line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF, + &s->login, s->passwd.len) + - line.data; + + s->mail_state = ngx_imap_user; + break; + + case ngx_imap_user: + ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, + "mail proxy send passwd"); s->connection->log->action = "sending password to upstream"; - line.len = sizeof("PASS ") - 1 + s->passwd.len + 2; + 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); + ngx_mail_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 = ngx_cpymem(line.data, s->passwd.data, s->passwd.len); *p++ = CR; *p = LF; - s->imap_state = ngx_pop3_passwd; + s->mail_state = ngx_imap_passwd; break; - case 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; + case ngx_imap_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_imap_get_module_srv_conf(s, ngx_imap_proxy_module); + 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_imap_proxy_handler(s->connection->write); + ngx_mail_proxy_handler(s->connection->write); return; @@ -435,7 +446,7 @@ ngx_imap_proxy_pop3_handler(ngx_event_t * we treat the incomplete sending as NGX_ERROR * because it is very strange here */ - ngx_imap_proxy_internal_server_error(s); + ngx_mail_proxy_internal_server_error(s); return; } @@ -445,29 +456,175 @@ ngx_imap_proxy_pop3_handler(ngx_event_t static void -ngx_imap_proxy_dummy_handler(ngx_event_t *wev) +ngx_mail_proxy_smtp_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_mail_core_srv_conf_t *cscf; + + ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, + "mail proxy smtp 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); + + if (rc == NGX_AGAIN) { + return; + } + + if (rc == NGX_ERROR) { + ngx_mail_proxy_upstream_error(s); + return; + } + + switch (s->mail_state) { + + case ngx_smtp_start: + ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send ehlo"); + + s->connection->log->action = "sending HELO/EHLO to upstream"; + + cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); + + line.len = sizeof("HELO ") - 1 + cscf->server_name.len + 2; + line.data = ngx_palloc(c->pool, line.len); + if (line.data == NULL) { + ngx_mail_proxy_internal_server_error(s); + return; + } + + 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); + *p++ = CR; *p = LF; + + s->mail_state = pcf->xclient ? ngx_smtp_helo: ngx_smtp_noxclient; + + break; + + case ngx_smtp_helo: + ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, + "mail proxy send xclient"); + + s->connection->log->action = "sending XCLIENT to upstream"; + + line.len = sizeof("XCLIENT PROTO=SMTP HELO= ADDR= LOGIN= " + "NAME=[UNAVAILABLE]" CRLF) - 1 + + s->esmtp + s->smtp_helo.len + + s->connection->addr_text.len + s->login.len; + + line.data = ngx_palloc(c->pool, line.len); + if (line.data == NULL) { + ngx_mail_proxy_internal_server_error(s); + return; + } + + if (s->smtp_helo.len) { + line.len = ngx_sprintf(line.data, + "XCLIENT PROTO=%sSMTP HELO=%V ADDR=%V LOGIN=%V " + "NAME=[UNAVAILABLE]" CRLF, + (s->esmtp ? "E" : ""), &s->smtp_helo, + &s->connection->addr_text, &s->login) + - line.data; + } else { + line.len = ngx_sprintf(line.data, + "XCLIENT PROTO=SMTP ADDR=%V LOGIN=%V " + "NAME=[UNAVAILABLE]" CRLF, + &s->connection->addr_text, &s->login) + - line.data; + } + + s->mail_state = ngx_smtp_xclient; + break; + + case ngx_smtp_noxclient: + case ngx_smtp_xclient: + + ngx_memcpy(s->proxy->buffer->start, smtp_ok, sizeof(smtp_ok) - 1); + + s->proxy->buffer->pos = s->proxy->buffer->start; + s->proxy->buffer->last = s->proxy->buffer->start + sizeof(smtp_ok) - 1; + + 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) + line.len = 0; + line.data = NULL; +#endif + break; + } + + 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_dummy_handler(ngx_event_t *wev) { ngx_connection_t *c; - ngx_imap_session_t *s; + ngx_mail_session_t *s; - ngx_log_debug0(NGX_LOG_DEBUG_IMAP, wev->log, 0, "imap proxy dummy handler"); + ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy dummy handler"); if (ngx_handle_write_event(wev, 0) == NGX_ERROR) { c = wev->data; s = c->data; - ngx_imap_proxy_close_session(s); + ngx_mail_proxy_close_session(s); } } static ngx_int_t -ngx_imap_proxy_read_response(ngx_imap_session_t *s, ngx_uint_t state) +ngx_mail_proxy_read_response(ngx_mail_session_t *s, ngx_uint_t state) { u_char *p; ssize_t n; ngx_buf_t *b; - ngx_imap_proxy_conf_t *pcf; + ngx_mail_proxy_conf_t *pcf; s->connection->log->action = "reading response from upstream"; @@ -504,12 +661,15 @@ ngx_imap_proxy_read_response(ngx_imap_se p = b->pos; - if (s->protocol == NGX_IMAP_POP3_PROTOCOL) { + switch (s->protocol) { + + case NGX_MAIL_POP3_PROTOCOL: if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') { return NGX_OK; } + break; - } else { + case NGX_MAIL_IMAP_PROTOCOL: switch (state) { case ngx_imap_start: @@ -534,9 +694,31 @@ ngx_imap_proxy_read_response(ngx_imap_se } break; } + + break; + + default: /* NGX_MAIL_SMTP_PROTOCOL */ + switch (state) { + + case ngx_smtp_helo: + case ngx_smtp_noxclient: + if (p[0] == '2' && p[1] == '5' && p[2] == '0') { + return NGX_OK; + } + break; + + case ngx_smtp_start: + case ngx_smtp_xclient: + if (p[0] == '2' && p[1] == '2' && p[2] == '0') { + return NGX_OK; + } + break; + } + + break; } - pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); + pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); if (pcf->pass_error_message == 0) { *(b->last - 2) = '\0'; @@ -559,7 +741,7 @@ ngx_imap_proxy_read_response(ngx_imap_se static void -ngx_imap_proxy_handler(ngx_event_t *ev) +ngx_mail_proxy_handler(ngx_event_t *ev) { char *action, *recv_action, *send_action; size_t size; @@ -567,8 +749,8 @@ ngx_imap_proxy_handler(ngx_event_t *ev) ngx_buf_t *b; ngx_uint_t do_write; ngx_connection_t *c, *src, *dst; - ngx_imap_session_t *s; - ngx_imap_proxy_conf_t *pcf; + ngx_mail_session_t *s; + ngx_mail_proxy_conf_t *pcf; c = ev->data; s = c->data; @@ -586,7 +768,7 @@ ngx_imap_proxy_handler(ngx_event_t *ev) "upstream timed out"); } - ngx_imap_proxy_close_session(s); + ngx_mail_proxy_close_session(s); return; } @@ -625,8 +807,8 @@ ngx_imap_proxy_handler(ngx_event_t *ev) do_write = ev->write ? 1 : 0; - ngx_log_debug3(NGX_LOG_DEBUG_IMAP, ev->log, 0, - "imap proxy handler: %d, #%d > #%d", + ngx_log_debug3(NGX_LOG_DEBUG_MAIL, ev->log, 0, + "mail proxy handler: %d, #%d > #%d", do_write, src->fd, dst->fd); for ( ;; ) { @@ -641,7 +823,7 @@ ngx_imap_proxy_handler(ngx_event_t *ev) n = dst->send(dst, b->pos, size); if (n == NGX_ERROR) { - ngx_imap_proxy_close_session(s); + ngx_mail_proxy_close_session(s); return; } @@ -693,100 +875,101 @@ ngx_imap_proxy_handler(ngx_event_t *ev) ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done"); c->log->action = action; - ngx_imap_proxy_close_session(s); + ngx_mail_proxy_close_session(s); return; } if (ngx_handle_write_event(dst->write, 0) == NGX_ERROR) { - ngx_imap_proxy_close_session(s); + ngx_mail_proxy_close_session(s); return; } if (ngx_handle_read_event(dst->read, 0) == NGX_ERROR) { - ngx_imap_proxy_close_session(s); + ngx_mail_proxy_close_session(s); return; } if (ngx_handle_write_event(src->write, 0) == NGX_ERROR) { - ngx_imap_proxy_close_session(s); + ngx_mail_proxy_close_session(s); return; } if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) { - ngx_imap_proxy_close_session(s); + ngx_mail_proxy_close_session(s); return; } if (c == s->connection) { - pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module); + pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module); ngx_add_timer(c->read, pcf->timeout); } } static void -ngx_imap_proxy_upstream_error(ngx_imap_session_t *s) +ngx_mail_proxy_upstream_error(ngx_mail_session_t *s) { if (s->proxy->upstream.connection) { - ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0, - "close imap proxy connection: %d", + ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, + "close mail proxy connection: %d", s->proxy->upstream.connection->fd); ngx_close_connection(s->proxy->upstream.connection); } if (s->out.len == 0) { - ngx_imap_session_internal_server_error(s); + ngx_mail_session_internal_server_error(s); return; } s->quit = 1; - ngx_imap_send(s->connection->write); + ngx_mail_send(s->connection->write); } static void -ngx_imap_proxy_internal_server_error(ngx_imap_session_t *s) +ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s) { if (s->proxy->upstream.connection) { - ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0, - "close imap proxy connection: %d", + ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, + "close mail proxy connection: %d", s->proxy->upstream.connection->fd); ngx_close_connection(s->proxy->upstream.connection); } - ngx_imap_session_internal_server_error(s); + ngx_mail_session_internal_server_error(s); } static void -ngx_imap_proxy_close_session(ngx_imap_session_t *s) +ngx_mail_proxy_close_session(ngx_mail_session_t *s) { if (s->proxy->upstream.connection) { - ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0, - "close imap proxy connection: %d", + ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, + "close mail proxy connection: %d", s->proxy->upstream.connection->fd); ngx_close_connection(s->proxy->upstream.connection); } - ngx_imap_close_connection(s->connection); + ngx_mail_close_connection(s->connection); } static void * -ngx_imap_proxy_create_conf(ngx_conf_t *cf) +ngx_mail_proxy_create_conf(ngx_conf_t *cf) { - ngx_imap_proxy_conf_t *pcf; + ngx_mail_proxy_conf_t *pcf; - pcf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_proxy_conf_t)); + pcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_proxy_conf_t)); if (pcf == NULL) { return NGX_CONF_ERROR; } pcf->enable = NGX_CONF_UNSET; pcf->pass_error_message = NGX_CONF_UNSET; + pcf->xclient = NGX_CONF_UNSET; pcf->buffer_size = NGX_CONF_UNSET_SIZE; pcf->timeout = NGX_CONF_UNSET_MSEC; @@ -795,13 +978,14 @@ ngx_imap_proxy_create_conf(ngx_conf_t *c static char * -ngx_imap_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child) +ngx_mail_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_mail_proxy_conf_t *prev = parent; + ngx_mail_proxy_conf_t *conf = child; ngx_conf_merge_value(conf->enable, prev->enable, 0); ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0); + ngx_conf_merge_value(conf->xclient, prev->xclient, 1); 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);