diff options
Diffstat (limited to 'src/imap')
-rw-r--r-- | src/imap/ngx_imap.h | 1 | ||||
-rw-r--r-- | src/imap/ngx_imap_auth_http_module.c | 60 | ||||
-rw-r--r-- | src/imap/ngx_imap_handler.c | 15 |
3 files changed, 61 insertions, 15 deletions
diff --git a/src/imap/ngx_imap.h b/src/imap/ngx_imap.h index 38f449891..5ba390055 100644 --- a/src/imap/ngx_imap.h +++ b/src/imap/ngx_imap.h @@ -136,6 +136,7 @@ typedef struct { void ngx_imap_init_connection(ngx_connection_t *c); void ngx_imap_close_connection(ngx_connection_t *c); +void ngx_imap_session_internal_server_error(ngx_imap_session_t *s); ngx_int_t ngx_pop3_parse_command(ngx_imap_session_t *s); diff --git a/src/imap/ngx_imap_auth_http_module.c b/src/imap/ngx_imap_auth_http_module.c index 7e6c44b23..fa984b855 100644 --- a/src/imap/ngx_imap_auth_http_module.c +++ b/src/imap/ngx_imap_auth_http_module.c @@ -23,6 +23,7 @@ typedef struct { typedef struct { ngx_buf_t *request; + ngx_buf_t *response; ngx_peer_connection_t peer; } ngx_imap_auth_http_ctx_t; @@ -91,7 +92,7 @@ ngx_imap_auth_http_init(ngx_imap_session_t *s) ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_imap_auth_http_ctx_t)); if (ctx == NULL) { - ngx_imap_close_connection(s->connection); + ngx_imap_session_internal_server_error(s); return; } @@ -99,7 +100,7 @@ ngx_imap_auth_http_init(ngx_imap_session_t *s) ctx->request = ngx_imap_auth_http_create_request(s, ahcf); if (ctx->request == NULL) { - ngx_imap_close_connection(s->connection); + ngx_imap_session_internal_server_error(s); return; } @@ -112,7 +113,7 @@ ngx_imap_auth_http_init(ngx_imap_session_t *s) rc = ngx_event_connect_peer(&ctx->peer); if (rc == NGX_ERROR) { - ngx_imap_close_connection(s->connection); + ngx_imap_session_internal_server_error(s); return; } @@ -153,8 +154,8 @@ ngx_imap_auth_http_write_handler(ngx_event_t *wev) if (wev->timedout) { ngx_log_error(NGX_LOG_ERR, wev->log, NGX_ETIMEDOUT, "auth http server timed out"); - ngx_imap_close_connection(ctx->peer.connection); - ngx_imap_close_connection(s->connection); + ngx_close_connection(ctx->peer.connection); + ngx_imap_session_internal_server_error(s); return; } @@ -163,8 +164,8 @@ ngx_imap_auth_http_write_handler(ngx_event_t *wev) n = ngx_send(c, ctx->request->pos, size); if (n == NGX_ERROR) { - ngx_imap_close_connection(ctx->peer.connection); - ngx_imap_close_connection(s->connection); + ngx_close_connection(ctx->peer.connection); + ngx_imap_session_internal_server_error(s); return; } @@ -192,23 +193,52 @@ ngx_imap_auth_http_write_handler(ngx_event_t *wev) static void ngx_imap_auth_http_read_handler(ngx_event_t *rev) { + ssize_t n, size; ngx_peers_t *peers; ngx_connection_t *c; ngx_imap_session_t *s; -#if 0 ngx_imap_auth_http_ctx_t *ctx; -#endif c = rev->data; s = c->data; -#if 0 - ctx = ngx_imap_get_module_ctx(s, ngx_imap_auth_http_module); -#endif - ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap auth http read handler"); + ctx = ngx_imap_get_module_ctx(s, ngx_imap_auth_http_module); + + if (rev->timedout) { + ngx_log_error(NGX_LOG_ERR, rev->log, NGX_ETIMEDOUT, + "auth http server timed out"); + ngx_close_connection(ctx->peer.connection); + ngx_imap_session_internal_server_error(s); + return; + } + + if (ctx->response == NULL) { + ctx->response = ngx_create_temp_buf(s->connection->pool, 1024); + if (ctx->response == NULL) { + ngx_close_connection(ctx->peer.connection); + ngx_imap_session_internal_server_error(s); + return; + } + } + + size = ctx->response->last - ctx->response->pos; + + n = ngx_recv(c, ctx->response->pos, size); + + if (n == NGX_ERROR || n == 0) { + ngx_close_connection(ctx->peer.connection); + ngx_imap_session_internal_server_error(s); + return; + } + + + + + + peers = NULL; ngx_imap_proxy_init(s, peers); @@ -231,8 +261,8 @@ ngx_imap_auth_http_block_read(ngx_event_t *rev) ctx = ngx_imap_get_module_ctx(s, ngx_imap_auth_http_module); - ngx_imap_close_connection(ctx->peer.connection); - ngx_imap_close_connection(s->connection); + ngx_close_connection(ctx->peer.connection); + ngx_imap_session_internal_server_error(s); } } diff --git a/src/imap/ngx_imap_handler.c b/src/imap/ngx_imap_handler.c index be2c9d1c5..680bf20d0 100644 --- a/src/imap/ngx_imap_handler.c +++ b/src/imap/ngx_imap_handler.c @@ -24,6 +24,11 @@ static ngx_str_t greetings[] = { ngx_string("* OK " NGINX_VER " ready" CRLF) }; +static ngx_str_t internal_server_errors[] = { + ngx_string("-ERR internal server error" CRLF), + ngx_string("* BAD internal server error" CRLF), +}; + static u_char pop3_ok[] = "+OK" CRLF; static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF; @@ -342,6 +347,16 @@ ngx_imap_close_session(ngx_imap_session_t *s) void +ngx_imap_session_internal_server_error(ngx_imap_session_t *s) +{ + (void) ngx_send(s->connection, internal_server_errors[s->protocol].data, + internal_server_errors[s->protocol].len); + + ngx_imap_close_connection(s->connection); +} + + +void ngx_imap_close_connection(ngx_connection_t *c) { ngx_pool_t *pool; |