summaryrefslogtreecommitdiffhomepage
path: root/src/imap
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2007-03-19 13:36:56 +0000
committerIgor Sysoev <igor@sysoev.ru>2007-03-19 13:36:56 +0000
commit02c8d181a4fbdde2f9b1d957526197c421fadbae (patch)
tree0dc87f8b3669f67fae34e739d776571d058e2524 /src/imap
parent4ddeff4956a93efce34105588153f591f3fcfcbc (diff)
downloadnginx-02c8d181a4fbdde2f9b1d957526197c421fadbae.tar.gz
nginx-02c8d181a4fbdde2f9b1d957526197c421fadbae.tar.bz2
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
Diffstat (limited to 'src/imap')
-rw-r--r--src/imap/ngx_imap.c403
-rw-r--r--src/imap/ngx_imap.h278
-rw-r--r--src/imap/ngx_imap_auth_http_module.c1357
-rw-r--r--src/imap/ngx_imap_core_module.c603
-rw-r--r--src/imap/ngx_imap_handler.c1357
-rw-r--r--src/imap/ngx_imap_parse.c587
-rw-r--r--src/imap/ngx_imap_proxy_module.c810
-rw-r--r--src/imap/ngx_imap_ssl_module.c393
-rw-r--r--src/imap/ngx_imap_ssl_module.h47
9 files changed, 0 insertions, 5835 deletions
diff --git a/src/imap/ngx_imap.c b/src/imap/ngx_imap.c
deleted file mode 100644
index 77b605620..000000000
--- a/src/imap/ngx_imap.c
+++ /dev/null
@@ -1,403 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- */
-
-
-#include <ngx_config.h>
-#include <ngx_core.h>
-#include <ngx_event.h>
-#include <ngx_imap.h>
-
-
-static char *ngx_imap_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
-static int ngx_libc_cdecl ngx_imap_cmp_conf_in_addrs(const void *one,
- const void *two);
-
-
-ngx_uint_t ngx_imap_max_module;
-
-
-static ngx_command_t ngx_imap_commands[] = {
-
- { ngx_string("imap"),
- NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
- ngx_imap_block,
- 0,
- 0,
- NULL },
-
- ngx_null_command
-};
-
-
-static ngx_core_module_t ngx_imap_module_ctx = {
- ngx_string("imap"),
- NULL,
- NULL
-};
-
-
-ngx_module_t ngx_imap_module = {
- NGX_MODULE_V1,
- &ngx_imap_module_ctx, /* module context */
- ngx_imap_commands, /* module directives */
- NGX_CORE_MODULE, /* module type */
- NULL, /* init master */
- NULL, /* init module */
- NULL, /* init process */
- NULL, /* init thread */
- NULL, /* exit thread */
- NULL, /* exit process */
- NULL, /* exit master */
- NGX_MODULE_V1_PADDING
-};
-
-
-static char *
-ngx_imap_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
- char *rv;
- u_char *text;
- size_t len;
- ngx_uint_t i, a, l, m, mi, s, p, last, bind_all, done;
- ngx_conf_t pcf;
- ngx_array_t in_ports;
- ngx_listening_t *ls;
- ngx_imap_listen_t *imls;
- ngx_imap_module_t *module;
- ngx_imap_in_port_t *imip;
- ngx_imap_conf_ctx_t *ctx;
- ngx_imap_conf_in_port_t *in_port;
- ngx_imap_conf_in_addr_t *in_addr;
- ngx_imap_core_srv_conf_t **cscfp;
- ngx_imap_core_main_conf_t *cmcf;
-
- /* the main imap context */
-
- ctx = ngx_pcalloc(cf->pool, sizeof(ngx_imap_conf_ctx_t));
- if (ctx == NULL) {
- return NGX_CONF_ERROR;
- }
-
- *(ngx_imap_conf_ctx_t **) conf = ctx;
-
- /* count the number of the http modules and set up their indices */
-
- ngx_imap_max_module = 0;
- for (m = 0; ngx_modules[m]; m++) {
- if (ngx_modules[m]->type != NGX_IMAP_MODULE) {
- continue;
- }
-
- ngx_modules[m]->ctx_index = ngx_imap_max_module++;
- }
-
-
- /* the imap main_conf context, it is the same in the all imap contexts */
-
- ctx->main_conf = ngx_pcalloc(cf->pool,
- sizeof(void *) * ngx_imap_max_module);
- if (ctx->main_conf == NULL) {
- return NGX_CONF_ERROR;
- }
-
-
- /*
- * the imap null srv_conf context, it is used to merge
- * the server{}s' srv_conf's
- */
-
- ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_imap_max_module);
- if (ctx->srv_conf == NULL) {
- return NGX_CONF_ERROR;
- }
-
-
- /*
- * create the main_conf's, the null srv_conf's, and the null loc_conf's
- * of the all imap modules
- */
-
- for (m = 0; ngx_modules[m]; m++) {
- if (ngx_modules[m]->type != NGX_IMAP_MODULE) {
- continue;
- }
-
- module = ngx_modules[m]->ctx;
- mi = ngx_modules[m]->ctx_index;
-
- if (module->create_main_conf) {
- ctx->main_conf[mi] = module->create_main_conf(cf);
- if (ctx->main_conf[mi] == NULL) {
- return NGX_CONF_ERROR;
- }
- }
-
- if (module->create_srv_conf) {
- ctx->srv_conf[mi] = module->create_srv_conf(cf);
- if (ctx->srv_conf[mi] == NULL) {
- return NGX_CONF_ERROR;
- }
- }
- }
-
-
- /* parse inside the imap{} block */
-
- pcf = *cf;
- cf->ctx = ctx;
-
- cf->module_type = NGX_IMAP_MODULE;
- cf->cmd_type = NGX_IMAP_MAIN_CONF;
- rv = ngx_conf_parse(cf, NULL);
-
- if (rv != NGX_CONF_OK) {
- *cf = pcf;
- return rv;
- }
-
-
- /* init imap{} main_conf's, merge the server{}s' srv_conf's */
-
- cmcf = ctx->main_conf[ngx_imap_core_module.ctx_index];
- cscfp = cmcf->servers.elts;
-
- for (m = 0; ngx_modules[m]; m++) {
- if (ngx_modules[m]->type != NGX_IMAP_MODULE) {
- continue;
- }
-
- module = ngx_modules[m]->ctx;
- mi = ngx_modules[m]->ctx_index;
-
- /* init imap{} main_conf's */
-
- if (module->init_main_conf) {
- rv = module->init_main_conf(cf, ctx->main_conf[mi]);
- if (rv != NGX_CONF_OK) {
- *cf = pcf;
- return rv;
- }
- }
-
- for (s = 0; s < cmcf->servers.nelts; s++) {
-
- /* merge the server{}s' srv_conf's */
-
- if (module->merge_srv_conf) {
- rv = module->merge_srv_conf(cf,
- ctx->srv_conf[mi],
- cscfp[s]->ctx->srv_conf[mi]);
- if (rv != NGX_CONF_OK) {
- *cf = pcf;
- return rv;
- }
- }
- }
- }
-
- /* imap{}'s cf->ctx was needed while the configuration merging */
-
- *cf = pcf;
-
-
- if (ngx_array_init(&in_ports, cf->temp_pool, 4,
- sizeof(ngx_imap_conf_in_port_t))
- != NGX_OK)
- {
- return NGX_CONF_ERROR;
- }
-
- imls = cmcf->listen.elts;
-
- for (l = 0; l < cmcf->listen.nelts; l++) {
-
- /* AF_INET only */
-
- in_port = in_ports.elts;
- for (p = 0; p < in_ports.nelts; p++) {
- if (in_port[p].port == imls[l].port) {
- in_port = &in_port[p];
- goto found;
- }
- }
-
- in_port = ngx_array_push(&in_ports);
- if (in_port == NULL) {
- return NGX_CONF_ERROR;
- }
-
- in_port->port = imls[l].port;
-
- if (ngx_array_init(&in_port->addrs, cf->temp_pool, 2,
- sizeof(ngx_imap_conf_in_addr_t))
- != NGX_OK)
- {
- return NGX_CONF_ERROR;
- }
-
- found:
-
- in_addr = ngx_array_push(&in_port->addrs);
- if (in_addr == NULL) {
- return NGX_CONF_ERROR;
- }
-
- in_addr->addr = imls[l].addr;
- in_addr->ctx = imls[l].ctx;
- in_addr->bind = imls[l].bind;
- }
-
- /* optimize the lists of ports and addresses */
-
- /* AF_INET only */
-
- in_port = in_ports.elts;
- for (p = 0; p < in_ports.nelts; p++) {
-
- ngx_qsort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts,
- sizeof(ngx_imap_conf_in_addr_t), ngx_imap_cmp_conf_in_addrs);
-
- in_addr = in_port[p].addrs.elts;
- last = in_port[p].addrs.nelts;
-
- /*
- * if there is the binding to the "*:port" then we need to bind()
- * to the "*:port" only and ignore the other bindings
- */
-
- if (in_addr[last - 1].addr == INADDR_ANY) {
- in_addr[last - 1].bind = 1;
- bind_all = 0;
-
- } else {
- bind_all = 1;
- }
-
- for (a = 0; a < last; /* void */ ) {
-
- if (!bind_all && !in_addr[a].bind) {
- a++;
- continue;
- }
-
- ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr,
- in_port[p].port);
- if (ls == NULL) {
- return NGX_CONF_ERROR;
- }
-
- ls->backlog = -1;
- ls->rcvbuf = -1;
- ls->sndbuf = -1;
-
- ls->addr_ntop = 1;
- ls->handler = ngx_imap_init_connection;
- ls->pool_size = 256;
-
- /* STUB */
- ls->log = *cf->cycle->new_log;
- ls->log.data = &ls->addr_text;
- ls->log.handler = ngx_accept_log_error;
- /**/
-
- imip = ngx_palloc(cf->pool, sizeof(ngx_imap_in_port_t));
- if (imip == NULL) {
- return NGX_CONF_ERROR;
- }
-
- ls->servers = imip;
-
- in_addr = in_port[p].addrs.elts;
-
- if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) {
- imip->naddrs = 1;
- done = 0;
-
- } else if (in_port[p].addrs.nelts > 1
- && in_addr[last - 1].addr == INADDR_ANY)
- {
- imip->naddrs = last;
- done = 1;
-
- } else {
- imip->naddrs = 1;
- done = 0;
- }
-
-#if 0
- ngx_log_error(NGX_LOG_ALERT, cf->log, 0,
- "%ui: %V %d %ui %ui",
- a, &ls->addr_text, in_addr[a].bind,
- imip->naddrs, last);
-#endif
-
- imip->addrs = ngx_pcalloc(cf->pool,
- imip->naddrs * sizeof(ngx_imap_in_addr_t));
- if (imip->addrs == NULL) {
- return NGX_CONF_ERROR;
- }
-
- for (i = 0; i < imip->naddrs; i++) {
- imip->addrs[i].addr = in_addr[i].addr;
- imip->addrs[i].ctx = in_addr[i].ctx;
-
- text = ngx_palloc(cf->pool,
- INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1);
- if (text == NULL) {
- return NGX_CONF_ERROR;
- }
-
- len = ngx_inet_ntop(AF_INET, &in_addr[i].addr, text,
- INET_ADDRSTRLEN);
-
- len = ngx_sprintf(text + len, ":%d", in_port[p].port) - text;
-
- imip->addrs[i].addr_text.len = len;
- imip->addrs[i].addr_text.data = text;
- }
-
- if (done) {
- break;
- }
-
- in_addr++;
- in_port[p].addrs.elts = in_addr;
- last--;
-
- a = 0;
- }
- }
-
- return NGX_CONF_OK;
-}
-
-
-static int ngx_libc_cdecl
-ngx_imap_cmp_conf_in_addrs(const void *one, const void *two)
-{
- ngx_imap_conf_in_addr_t *first, *second;
-
- first = (ngx_imap_conf_in_addr_t *) one;
- second = (ngx_imap_conf_in_addr_t *) two;
-
- if (first->addr == INADDR_ANY) {
- /* the INADDR_ANY must be the last resort, shift it to the end */
- return 1;
- }
-
- if (first->bind && !second->bind) {
- /* shift explicit bind()ed addresses to the start */
- return -1;
- }
-
- if (!first->bind && second->bind) {
- /* shift explicit bind()ed addresses to the start */
- return 1;
- }
-
- /* do not sort by default */
-
- return 0;
-}
diff --git a/src/imap/ngx_imap.h b/src/imap/ngx_imap.h
deleted file mode 100644
index 00ebd3416..000000000
--- a/src/imap/ngx_imap.h
+++ /dev/null
@@ -1,278 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- */
-
-
-#ifndef _NGX_IMAP_H_INCLUDED_
-#define _NGX_IMAP_H_INCLUDED_
-
-
-#include <ngx_config.h>
-#include <ngx_core.h>
-#include <ngx_event.h>
-#include <ngx_event_connect.h>
-
-#if (NGX_IMAP_SSL)
-#include <ngx_imap_ssl_module.h>
-#endif
-
-
-
-typedef struct {
- void **main_conf;
- void **srv_conf;
-} ngx_imap_conf_ctx_t;
-
-
-typedef struct {
- in_addr_t addr;
- in_port_t port;
- int family;
-
- /* server ctx */
- ngx_imap_conf_ctx_t *ctx;
-
- unsigned bind:1;
-} ngx_imap_listen_t;
-
-
-typedef struct {
- in_addr_t addr;
- ngx_imap_conf_ctx_t *ctx;
- ngx_str_t addr_text;
-} ngx_imap_in_addr_t;
-
-
-typedef struct {
- ngx_imap_in_addr_t *addrs; /* array of ngx_imap_in_addr_t */
- ngx_uint_t naddrs;
-} ngx_imap_in_port_t;
-
-
-typedef struct {
- in_port_t port;
- ngx_array_t addrs; /* array of ngx_imap_conf_in_addr_t */
-} ngx_imap_conf_in_port_t;
-
-
-typedef struct {
- in_addr_t addr;
- ngx_imap_conf_ctx_t *ctx;
- unsigned bind:1;
-} ngx_imap_conf_in_addr_t;
-
-
-typedef struct {
- ngx_array_t servers; /* ngx_imap_core_srv_conf_t */
- ngx_array_t listen; /* ngx_imap_listen_t */
-} ngx_imap_core_main_conf_t;
-
-
-#define NGX_IMAP_POP3_PROTOCOL 0
-#define NGX_IMAP_IMAP_PROTOCOL 1
-
-typedef struct {
- ngx_msec_t timeout;
-
- size_t imap_client_buffer_size;
-
- ngx_uint_t protocol;
-
- ngx_flag_t so_keepalive;
-
- ngx_str_t pop3_capability;
- ngx_str_t pop3_starttls_capability;
- ngx_str_t pop3_auth_capability;
-
- ngx_str_t imap_capability;
- ngx_str_t imap_starttls_capability;
- ngx_str_t imap_starttls_only_capability;
-
- ngx_str_t server_name;
-
- ngx_uint_t auth_methods;
-
- ngx_array_t pop3_capabilities;
- ngx_array_t imap_capabilities;
-
- /* server ctx */
- ngx_imap_conf_ctx_t *ctx;
-} ngx_imap_core_srv_conf_t;
-
-
-typedef struct {
- void *(*create_main_conf)(ngx_conf_t *cf);
- char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
-
- void *(*create_srv_conf)(ngx_conf_t *cf);
- char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
- void *conf);
-} ngx_imap_module_t;
-
-
-typedef enum {
- ngx_imap_start = 0,
- ngx_imap_login,
- ngx_imap_user,
- ngx_imap_passwd
-} ngx_imap_state_e;
-
-
-typedef enum {
- ngx_pop3_start = 0,
- ngx_pop3_user,
- ngx_pop3_passwd,
- ngx_pop3_auth_login_username,
- ngx_pop3_auth_login_password,
- ngx_pop3_auth_plain,
- ngx_pop3_auth_cram_md5
-} ngx_po3_state_e;
-
-
-typedef struct {
- ngx_peer_connection_t upstream;
- ngx_buf_t *buffer;
-} ngx_imap_proxy_ctx_t;
-
-
-typedef struct {
- uint32_t signature; /* "IMAP" */
-
- ngx_connection_t *connection;
-
- ngx_str_t out;
- ngx_buf_t *buffer;
-
- void **ctx;
- void **main_conf;
- void **srv_conf;
-
- ngx_imap_proxy_ctx_t *proxy;
-
- ngx_uint_t imap_state;
-
- unsigned blocked:1;
- unsigned quit:1;
- unsigned protocol:1;
- unsigned quoted:1;
- unsigned backslash:1;
- unsigned no_sync_literal:1;
- unsigned starttls:1;
- unsigned auth_method:2;
- unsigned auth_wait:1;
-
- ngx_str_t login;
- ngx_str_t passwd;
-
- ngx_str_t salt;
- ngx_str_t tag;
- ngx_str_t tagged_line;
-
- ngx_str_t *addr_text;
-
- ngx_uint_t command;
- ngx_array_t args;
-
- ngx_uint_t login_attempt;
-
- /* used to parse IMAP/POP3 command */
-
- ngx_uint_t state;
- u_char *cmd_start;
- u_char *arg_start;
- u_char *arg_end;
- ngx_uint_t literal_len;
-} ngx_imap_session_t;
-
-
-typedef struct {
- ngx_str_t *client;
- ngx_imap_session_t *session;
-} ngx_imap_log_ctx_t;
-
-
-#define NGX_POP3_USER 1
-#define NGX_POP3_PASS 2
-#define NGX_POP3_CAPA 3
-#define NGX_POP3_QUIT 4
-#define NGX_POP3_NOOP 5
-#define NGX_POP3_STLS 6
-#define NGX_POP3_APOP 7
-#define NGX_POP3_AUTH 8
-#define NGX_POP3_STAT 9
-#define NGX_POP3_LIST 10
-#define NGX_POP3_RETR 11
-#define NGX_POP3_DELE 12
-#define NGX_POP3_RSET 13
-#define NGX_POP3_TOP 14
-#define NGX_POP3_UIDL 15
-
-
-#define NGX_IMAP_LOGIN 1
-#define NGX_IMAP_LOGOUT 2
-#define NGX_IMAP_CAPABILITY 3
-#define NGX_IMAP_NOOP 4
-#define NGX_IMAP_STARTTLS 5
-
-#define NGX_IMAP_NEXT 6
-
-
-#define NGX_IMAP_AUTH_PLAIN 0
-#define NGX_IMAP_AUTH_APOP 1
-#define NGX_IMAP_AUTH_CRAM_MD5 2
-
-
-#define NGX_IMAP_AUTH_PLAIN_ENABLED 0x0002
-#define NGX_IMAP_AUTH_APOP_ENABLED 0x0004
-#define NGX_IMAP_AUTH_CRAM_MD5_ENABLED 0x0008
-
-
-#define NGX_IMAP_PARSE_INVALID_COMMAND 20
-
-
-#define NGX_IMAP_MODULE 0x50414D49 /* "IMAP" */
-
-#define NGX_IMAP_MAIN_CONF 0x02000000
-#define NGX_IMAP_SRV_CONF 0x04000000
-
-
-#define NGX_IMAP_MAIN_CONF_OFFSET offsetof(ngx_imap_conf_ctx_t, main_conf)
-#define NGX_IMAP_SRV_CONF_OFFSET offsetof(ngx_imap_conf_ctx_t, srv_conf)
-
-
-#define ngx_imap_get_module_ctx(s, module) (s)->ctx[module.ctx_index]
-#define ngx_imap_set_ctx(s, c, module) s->ctx[module.ctx_index] = c;
-#define ngx_imap_delete_ctx(s, module) s->ctx[module.ctx_index] = NULL;
-
-
-#define ngx_imap_get_module_main_conf(s, module) \
- (s)->main_conf[module.ctx_index]
-#define ngx_imap_get_module_srv_conf(s, module) (s)->srv_conf[module.ctx_index]
-
-#define ngx_imap_conf_get_module_main_conf(cf, module) \
- ((ngx_imap_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
-
-
-void ngx_imap_init_connection(ngx_connection_t *c);
-void ngx_imap_send(ngx_event_t *wev);
-void ngx_imap_auth_state(ngx_event_t *rev);
-void ngx_pop3_auth_state(ngx_event_t *rev);
-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_imap_parse_command(ngx_imap_session_t *s);
-ngx_int_t ngx_pop3_parse_command(ngx_imap_session_t *s);
-
-
-/* STUB */
-void ngx_imap_proxy_init(ngx_imap_session_t *s, ngx_peer_addr_t *peer);
-void ngx_imap_auth_http_init(ngx_imap_session_t *s);
-/**/
-
-
-extern ngx_uint_t ngx_imap_max_module;
-extern ngx_module_t ngx_imap_core_module;
-
-
-#endif /* _NGX_IMAP_H_INCLUDED_ */
diff --git a/src/imap/ngx_imap_auth_http_module.c b/src/imap/ngx_imap_auth_http_module.c
deleted file mode 100644
index f3e11e86b..000000000
--- a/src/imap/ngx_imap_auth_http_module.c
+++ /dev/null
@@ -1,1357 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- */
-
-
-#include <ngx_config.h>
-#include <ngx_core.h>
-#include <ngx_event.h>
-#include <ngx_event_connect.h>
-#include <ngx_imap.h>
-
-
-typedef struct {
- ngx_peer_addr_t *peer;
-
- ngx_msec_t timeout;
-
- ngx_str_t host_header;
- ngx_str_t uri;
- ngx_str_t header;
-
- ngx_array_t *headers;
-} ngx_imap_auth_http_conf_t;
-
-
-typedef struct ngx_imap_auth_http_ctx_s ngx_imap_auth_http_ctx_t;
-
-typedef void (*ngx_imap_auth_http_handler_pt)(ngx_imap_session_t *s,
- ngx_imap_auth_http_ctx_t *ctx);
-
-struct ngx_imap_auth_http_ctx_s {
- ngx_buf_t *request;
- ngx_buf_t *response;
- ngx_peer_connection_t peer;
-
- ngx_imap_auth_http_handler_pt handler;
-
- ngx_uint_t state;
- ngx_uint_t hash; /* no needed ? */
-
- u_char *header_name_start;
- u_char *header_name_end;
- u_char *header_start;
- u_char *header_end;
-
- ngx_str_t addr;
- ngx_str_t port;
- ngx_str_t err;
- ngx_str_t errmsg;
-
- time_t sleep;
-
- ngx_pool_t *pool;
-};
-
-
-static void ngx_imap_auth_http_write_handler(ngx_event_t *wev);
-static void ngx_imap_auth_http_read_handler(ngx_event_t *rev);
-static void ngx_imap_auth_http_ignore_status_line(ngx_imap_session_t *s,
- ngx_imap_auth_http_ctx_t *ctx);
-static void ngx_imap_auth_http_process_headers(ngx_imap_session_t *s,
- ngx_imap_auth_http_ctx_t *ctx);
-static void ngx_imap_auth_sleep_handler(ngx_event_t *rev);
-static ngx_int_t ngx_imap_auth_http_parse_header_line(ngx_imap_session_t *s,
- ngx_imap_auth_http_ctx_t *ctx);
-static void ngx_imap_auth_http_block_read(ngx_event_t *rev);
-static void ngx_imap_auth_http_dummy_handler(ngx_event_t *ev);
-static ngx_buf_t *ngx_imap_auth_http_create_request(ngx_imap_session_t *s,
- ngx_pool_t *pool, ngx_imap_auth_http_conf_t *ahcf);
-static ngx_int_t ngx_imap_auth_http_escape(ngx_pool_t *pool, ngx_str_t *text,
- ngx_str_t *escaped);
-
-static void *ngx_imap_auth_http_create_conf(ngx_conf_t *cf);
-static char *ngx_imap_auth_http_merge_conf(ngx_conf_t *cf, void *parent,
- void *child);
-static char *ngx_imap_auth_http(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
-static char *ngx_imap_auth_http_header(ngx_conf_t *cf, ngx_command_t *cmd,
- void *conf);
-
-
-static ngx_command_t ngx_imap_auth_http_commands[] = {
-
- { ngx_string("auth_http"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_imap_auth_http,
- NGX_IMAP_SRV_CONF_OFFSET,
- 0,
- NULL },
-
- { ngx_string("auth_http_timeout"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_msec_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_auth_http_conf_t, timeout),
- NULL },
-
- { ngx_string("auth_http_header"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE2,
- ngx_imap_auth_http_header,
- NGX_IMAP_SRV_CONF_OFFSET,
- 0,
- NULL },
-
- ngx_null_command
-};
-
-
-static ngx_imap_module_t ngx_imap_auth_http_module_ctx = {
- NULL, /* create main configuration */
- NULL, /* init main configuration */
-
- ngx_imap_auth_http_create_conf, /* create server configuration */
- ngx_imap_auth_http_merge_conf /* merge server configuration */
-};
-
-
-ngx_module_t ngx_imap_auth_http_module = {
- NGX_MODULE_V1,
- &ngx_imap_auth_http_module_ctx, /* module context */
- ngx_imap_auth_http_commands, /* module directives */
- NGX_IMAP_MODULE, /* module type */
- NULL, /* init master */
- NULL, /* init module */
- NULL, /* init process */
- NULL, /* init thread */
- NULL, /* exit thread */
- NULL, /* exit process */
- NULL, /* exit master */
- NGX_MODULE_V1_PADDING
-};
-
-
-static char *ngx_imap_auth_http_protocol[] = { "pop3", "imap" };
-static ngx_str_t ngx_imap_auth_http_method[] = {
- ngx_string("plain"),
- ngx_string("apop"),
- ngx_string("cram-md5")
-};
-
-
-void
-ngx_imap_auth_http_init(ngx_imap_session_t *s)
-{
- ngx_int_t rc;
- ngx_pool_t *pool;
- ngx_imap_auth_http_ctx_t *ctx;
- ngx_imap_auth_http_conf_t *ahcf;
-
- s->connection->log->action = "in http auth state";
-
- pool = ngx_create_pool(2048, s->connection->log);
- if (pool == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ctx = ngx_pcalloc(pool, sizeof(ngx_imap_auth_http_ctx_t));
- if (ctx == NULL) {
- ngx_destroy_pool(pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ctx->pool = pool;
-
- ahcf = ngx_imap_get_module_srv_conf(s, ngx_imap_auth_http_module);
-
- ctx->request = ngx_imap_auth_http_create_request(s, pool, ahcf);
- if (ctx->request == NULL) {
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ngx_imap_set_ctx(s, ctx, ngx_imap_auth_http_module);
-
- ctx->peer.sockaddr = ahcf->peer->sockaddr;
- ctx->peer.socklen = ahcf->peer->socklen;
- ctx->peer.name = &ahcf->peer->name;
- ctx->peer.get = ngx_event_get_peer;
- ctx->peer.log = s->connection->log;
- ctx->peer.log_error = NGX_ERROR_ERR;
-
- rc = ngx_event_connect_peer(&ctx->peer);
-
- if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) {
- if (ctx->peer.connection) {
- ngx_close_connection(ctx->peer.connection);
- }
-
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ctx->peer.connection->data = s;
- ctx->peer.connection->pool = s->connection->pool;
-
- s->connection->read->handler = ngx_imap_auth_http_block_read;
- ctx->peer.connection->read->handler = ngx_imap_auth_http_read_handler;
- ctx->peer.connection->write->handler = ngx_imap_auth_http_write_handler;
-
- ctx->handler = ngx_imap_auth_http_ignore_status_line;
-
- ngx_add_timer(ctx->peer.connection->read, ahcf->timeout);
- ngx_add_timer(ctx->peer.connection->write, ahcf->timeout);
-
- if (rc == NGX_OK) {
- ngx_imap_auth_http_write_handler(ctx->peer.connection->write);
- return;
- }
-}
-
-
-static void
-ngx_imap_auth_http_write_handler(ngx_event_t *wev)
-{
- ssize_t n, size;
- ngx_connection_t *c;
- ngx_imap_session_t *s;
- ngx_imap_auth_http_ctx_t *ctx;
- ngx_imap_auth_http_conf_t *ahcf;
-
- c = wev->data;
- s = c->data;
-
- ctx = ngx_imap_get_module_ctx(s, ngx_imap_auth_http_module);
-
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, wev->log, 0,
- "imap auth http write handler");
-
- if (wev->timedout) {
- ngx_log_error(NGX_LOG_ERR, wev->log, NGX_ETIMEDOUT,
- "auth http server %V timed out", ctx->peer.name);
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- size = ctx->request->last - ctx->request->pos;
-
- n = ngx_send(c, ctx->request->pos, size);
-
- if (n == NGX_ERROR) {
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- if (n > 0) {
- ctx->request->pos += n;
-
- if (n == size) {
- wev->handler = ngx_imap_auth_http_dummy_handler;
-
- if (wev->timer_set) {
- ngx_del_timer(wev);
- }
-
- if (ngx_handle_write_event(wev, 0) == NGX_ERROR) {
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- }
-
- return;
- }
- }
-
- if (!wev->timer_set) {
- ahcf = ngx_imap_get_module_srv_conf(s, ngx_imap_auth_http_module);
- ngx_add_timer(wev, ahcf->timeout);
- }
-}
-
-
-static void
-ngx_imap_auth_http_read_handler(ngx_event_t *rev)
-{
- ssize_t n, size;
- ngx_connection_t *c;
- ngx_imap_session_t *s;
- ngx_imap_auth_http_ctx_t *ctx;
-
- c = rev->data;
- s = c->data;
-
- 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 %V timed out", ctx->peer.name);
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- if (ctx->response == NULL) {
- ctx->response = ngx_create_temp_buf(ctx->pool, 1024);
- if (ctx->response == NULL) {
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
- }
-
- size = ctx->response->end - ctx->response->last;
-
- n = ngx_recv(c, ctx->response->pos, size);
-
- if (n > 0) {
- ctx->response->last += n;
-
- ctx->handler(s, ctx);
- return;
- }
-
- if (n == NGX_AGAIN) {
- return;
- }
-
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
-}
-
-
-static void
-ngx_imap_auth_http_ignore_status_line(ngx_imap_session_t *s,
- ngx_imap_auth_http_ctx_t *ctx)
-{
- u_char *p, ch;
- enum {
- sw_start = 0,
- sw_H,
- sw_HT,
- sw_HTT,
- sw_HTTP,
- sw_skip,
- sw_almost_done
- } state;
-
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
- "imap auth http process status line");
-
- state = ctx->state;
-
- for (p = ctx->response->pos; p < ctx->response->last; p++) {
- ch = *p;
-
- switch (state) {
-
- /* "HTTP/" */
- case sw_start:
- if (ch == 'H') {
- state = sw_H;
- break;
- }
- goto next;
-
- case sw_H:
- if (ch == 'T') {
- state = sw_HT;
- break;
- }
- goto next;
-
- case sw_HT:
- if (ch == 'T') {
- state = sw_HTT;
- break;
- }
- goto next;
-
- case sw_HTT:
- if (ch == 'P') {
- state = sw_HTTP;
- break;
- }
- goto next;
-
- case sw_HTTP:
- if (ch == '/') {
- state = sw_skip;
- break;
- }
- goto next;
-
- /* any text until end of line */
- case sw_skip:
- switch (ch) {
- case CR:
- state = sw_almost_done;
-
- break;
- case LF:
- goto done;
- }
- break;
-
- /* end of status line */
- case sw_almost_done:
- if (ch == LF) {
- goto done;
- }
-
- ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
- "auth http server &V sent invalid response",
- ctx->peer.name);
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
- }
-
- ctx->response->pos = p;
- ctx->state = state;
-
- return;
-
-next:
-
- p = ctx->response->start - 1;
-
-done:
-
- ctx->response->pos = p + 1;
- ctx->state = 0;
- ctx->handler = ngx_imap_auth_http_process_headers;
- ctx->handler(s, ctx);
-}
-
-
-static void
-ngx_imap_auth_http_process_headers(ngx_imap_session_t *s,
- ngx_imap_auth_http_ctx_t *ctx)
-{
- u_char *p;
- time_t timer;
- size_t len, size;
- ngx_int_t rc, port, n;
- ngx_peer_addr_t *peer;
- struct sockaddr_in *sin;
-
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
- "imap auth http process headers");
-
- for ( ;; ) {
- rc = ngx_imap_auth_http_parse_header_line(s, ctx);
-
- if (rc == NGX_OK) {
-
-#if (NGX_DEBUG)
- {
- ngx_str_t key, value;
-
- key.len = ctx->header_name_end - ctx->header_name_start;
- key.data = ctx->header_name_start;
- value.len = ctx->header_end - ctx->header_start;
- value.data = ctx->header_start;
-
- ngx_log_debug2(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
- "auth http header: \"%V: %V\"",
- &key, &value);
- }
-#endif
-
- len = ctx->header_name_end - ctx->header_name_start;
-
- if (len == sizeof("Auth-Status") - 1
- && ngx_strncasecmp(ctx->header_name_start,
- (u_char *) "Auth-Status",
- sizeof("Auth-Status") - 1)
- == 0)
- {
- len = ctx->header_end - ctx->header_start;
-
- if (len == 2
- && ctx->header_start[0] == 'O'
- && ctx->header_start[1] == 'K')
- {
- continue;
- }
-
- if (len == 4
- && ctx->header_start[0] == 'W'
- && ctx->header_start[1] == 'A'
- && ctx->header_start[2] == 'I'
- && ctx->header_start[3] == 'T')
- {
- s->auth_wait = 1;
- continue;
- }
-
- ctx->errmsg.len = len;
- ctx->errmsg.data = ctx->header_start;
-
- if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
- size = sizeof("-ERR ") - 1 + len + sizeof(CRLF) - 1;
-
- } else {
- size = s->tag.len + sizeof("NO ") - 1 + len
- + sizeof(CRLF) - 1;
- }
-
- p = ngx_pcalloc(s->connection->pool, size);
- if (p == NULL) {
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ctx->err.data = p;
-
- if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
- *p++ = '-'; *p++ = 'E'; *p++ = 'R'; *p++ = 'R';
-
- } else {
- p = ngx_cpymem(p, s->tag.data, s->tag.len);
- *p++ = 'N'; *p++ = 'O';
- }
-
- *p++ = ' ';
-
- p = ngx_cpymem(p, ctx->header_start, len);
- *p++ = CR; *p++ = LF;
-
- ctx->err.len = p - ctx->err.data;
-
- continue;
- }
-
- if (len == sizeof("Auth-Server") - 1
- && ngx_strncasecmp(ctx->header_name_start,
- (u_char *) "Auth-Server",
- sizeof("Auth-Server") - 1)
- == 0)
- {
- ctx->addr.len = ctx->header_end - ctx->header_start;
- ctx->addr.data = ctx->header_start;
-
- continue;
- }
-
- if (len == sizeof("Auth-Port") - 1
- && ngx_strncasecmp(ctx->header_name_start,
- (u_char *) "Auth-Port",
- sizeof("Auth-Port") - 1)
- == 0)
- {
- ctx->port.len = ctx->header_end - ctx->header_start;
- ctx->port.data = ctx->header_start;
-
- continue;
- }
-
- if (len == sizeof("Auth-User") - 1
- && ngx_strncasecmp(ctx->header_name_start,
- (u_char *) "Auth-User",
- sizeof("Auth-User") - 1)
- == 0)
- {
- s->login.len = ctx->header_end - ctx->header_start;
-
- s->login.data = ngx_palloc(s->connection->pool, s->login.len);
- if (s->login.data == NULL) {
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ngx_memcpy(s->login.data, ctx->header_start, s->login.len);
-
- continue;
- }
-
- if (len == sizeof("Auth-Pass") - 1
- && ngx_strncasecmp(ctx->header_name_start,
- (u_char *) "Auth-Pass",
- sizeof("Auth-Pass") - 1)
- == 0)
- {
- s->passwd.len = ctx->header_end - ctx->header_start;
-
- s->passwd.data = ngx_palloc(s->connection->pool, s->passwd.len);
- if (s->passwd.data == NULL) {
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ngx_memcpy(s->passwd.data, ctx->header_start, s->passwd.len);
-
- continue;
- }
-
- if (len == sizeof("Auth-Wait") - 1
- && ngx_strncasecmp(ctx->header_name_start,
- (u_char *) "Auth-Wait",
- sizeof("Auth-Wait") - 1)
- == 0)
- {
- n = ngx_atoi(ctx->header_start,
- ctx->header_end - ctx->header_start);
-
- if (n != NGX_ERROR) {
- ctx->sleep = n;
- }
-
- continue;
- }
-
- /* ignore other headers */
-
- continue;
- }
-
- if (rc == NGX_DONE) {
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
- "auth http header done");
-
- ngx_close_connection(ctx->peer.connection);
-
- if (ctx->err.len) {
- ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
- "client login failed: \"%V\"", &ctx->errmsg);
-
- s->out = ctx->err;
- timer = ctx->sleep;
-
- ngx_destroy_pool(ctx->pool);
-
- if (timer == 0) {
- s->quit = 1;
- ngx_imap_send(s->connection->write);
- return;
- }
-
- ngx_add_timer(s->connection->read, timer * 1000);
-
- s->connection->read->handler = ngx_imap_auth_sleep_handler;
-
- return;
- }
-
- if (s->auth_wait) {
- timer = ctx->sleep;
-
- ngx_destroy_pool(ctx->pool);
-
- if (timer == 0) {
- ngx_imap_auth_http_init(s);
- return;
- }
-
- ngx_add_timer(s->connection->read, timer * 1000);
-
- s->connection->read->handler = ngx_imap_auth_sleep_handler;
-
- return;
- }
-
- if (ctx->addr.len == 0 || ctx->port.len == 0) {
- ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
- "auth http server %V did not send server or port",
- ctx->peer.name);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- if (s->passwd.data == NULL) {
- ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
- "auth http server %V did not send password",
- ctx->peer.name);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- peer = ngx_pcalloc(s->connection->pool, sizeof(ngx_peer_addr_t));
- if (peer == NULL) {
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- sin = ngx_pcalloc(s->connection->pool, sizeof(struct sockaddr_in));
- if (sin == NULL) {
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- sin->sin_family = AF_INET;
-
- port = ngx_atoi(ctx->port.data, ctx->port.len);
- if (port == NGX_ERROR || port < 1 || port > 65536) {
- ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
- "auth http server %V sent invalid server "
- "port:\"%V\"",
- ctx->peer.name, &ctx->port);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- sin->sin_port = htons((in_port_t) port);
-
- ctx->addr.data[ctx->addr.len] = '\0';
- sin->sin_addr.s_addr = inet_addr((char *) ctx->addr.data);
- if (sin->sin_addr.s_addr == INADDR_NONE) {
- ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
- "auth http server %V sent invalid server "
- "address:\"%V\"",
- ctx->peer.name, &ctx->addr);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- peer->sockaddr = (struct sockaddr *) sin;
- peer->socklen = sizeof(struct sockaddr_in);
-
- len = ctx->addr.len + 1 + ctx->port.len;
-
- peer->name.len = len;
-
- peer->name.data = ngx_palloc(s->connection->pool, len);
- if (peer->name.data == NULL) {
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- len = ctx->addr.len;
-
- ngx_memcpy(peer->name.data, ctx->addr.data, len);
-
- peer->name.data[len++] = ':';
-
- ngx_memcpy(peer->name.data + len, ctx->port.data, ctx->port.len);
-
- ngx_destroy_pool(ctx->pool);
- ngx_imap_proxy_init(s, peer);
-
- return;
- }
-
- if (rc == NGX_AGAIN ) {
- return;
- }
-
- /* rc == NGX_ERROR */
-
- ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
- "auth http server %V sent invalid header in response",
- ctx->peer.name);
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
-
- return;
- }
-}
-
-
-static void
-ngx_imap_auth_sleep_handler(ngx_event_t *rev)
-{
- 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 auth sleep handler");
-
- c = rev->data;
- s = c->data;
-
- if (rev->timedout) {
-
- rev->timedout = 0;
-
- if (s->auth_wait) {
- s->auth_wait = 0;
- ngx_imap_auth_http_init(s);
- return;
- }
-
- if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
- s->imap_state = ngx_pop3_start;
- s->connection->read->handler = ngx_pop3_auth_state;
-
- } else {
- s->imap_state = ngx_imap_start;
- s->connection->read->handler = ngx_imap_auth_state;
- }
-
- s->auth_method = NGX_IMAP_AUTH_PLAIN;
-
- c->log->action = "in auth state";
-
- ngx_imap_send(s->connection->write);
-
- if (c->destroyed) {
- return;
- }
-
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
-
- ngx_add_timer(rev, cscf->timeout);
-
- if (rev->ready) {
- s->connection->read->handler(rev);
- return;
- }
-
- if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
- ngx_imap_close_connection(s->connection);
- }
-
- return;
- }
-
- if (rev->active) {
- if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
- ngx_imap_close_connection(s->connection);
- }
- }
-}
-
-
-static ngx_int_t
-ngx_imap_auth_http_parse_header_line(ngx_imap_session_t *s,
- ngx_imap_auth_http_ctx_t *ctx)
-{
- u_char c, ch, *p;
- ngx_uint_t hash;
- enum {
- sw_start = 0,
- sw_name,
- sw_space_before_value,
- sw_value,
- sw_space_after_value,
- sw_almost_done,
- sw_header_almost_done
- } state;
-
- state = ctx->state;
- hash = ctx->hash;
-
- for (p = ctx->response->pos; p < ctx->response->last; p++) {
- ch = *p;
-
- switch (state) {
-
- /* first char */
- case sw_start:
-
- switch (ch) {
- case CR:
- ctx->header_end = p;
- state = sw_header_almost_done;
- break;
- case LF:
- ctx->header_end = p;
- goto header_done;
- default:
- state = sw_name;
- ctx->header_name_start = p;
-
- c = (u_char) (ch | 0x20);
- if (c >= 'a' && c <= 'z') {
- hash = c;
- break;
- }
-
- if (ch >= '0' && ch <= '9') {
- hash = ch;
- break;
- }
-
- return NGX_ERROR;
- }
- break;
-
- /* header name */
- case sw_name:
- c = (u_char) (ch | 0x20);
- if (c >= 'a' && c <= 'z') {
- hash += c;
- break;
- }
-
- if (ch == ':') {
- ctx->header_name_end = p;
- state = sw_space_before_value;
- break;
- }
-
- if (ch == '-') {
- hash += ch;
- break;
- }
-
- if (ch >= '0' && ch <= '9') {
- hash += ch;
- break;
- }
-
- if (ch == CR) {
- ctx->header_name_end = p;
- ctx->header_start = p;
- ctx->header_end = p;
- state = sw_almost_done;
- break;
- }
-
- if (ch == LF) {
- ctx->header_name_end = p;
- ctx->header_start = p;
- ctx->header_end = p;
- goto done;
- }
-
- return NGX_ERROR;
-
- /* space* before header value */
- case sw_space_before_value:
- switch (ch) {
- case ' ':
- break;
- case CR:
- ctx->header_start = p;
- ctx->header_end = p;
- state = sw_almost_done;
- break;
- case LF:
- ctx->header_start = p;
- ctx->header_end = p;
- goto done;
- default:
- ctx->header_start = p;
- state = sw_value;
- break;
- }
- break;
-
- /* header value */
- case sw_value:
- switch (ch) {
- case ' ':
- ctx->header_end = p;
- state = sw_space_after_value;
- break;
- case CR:
- ctx->header_end = p;
- state = sw_almost_done;
- break;
- case LF:
- ctx->header_end = p;
- goto done;
- }
- break;
-
- /* space* before end of header line */
- case sw_space_after_value:
- switch (ch) {
- case ' ':
- break;
- case CR:
- state = sw_almost_done;
- break;
- case LF:
- goto done;
- default:
- state = sw_value;
- break;
- }
- break;
-
- /* end of header line */
- case sw_almost_done:
- switch (ch) {
- case LF:
- goto done;
- default:
- return NGX_ERROR;
- }
-
- /* end of header */
- case sw_header_almost_done:
- switch (ch) {
- case LF:
- goto header_done;
- default:
- return NGX_ERROR;
- }
- }
- }
-
- ctx->response->pos = p;
- ctx->state = state;
- ctx->hash = hash;
-
- return NGX_AGAIN;
-
-done:
-
- ctx->response->pos = p + 1;
- ctx->state = sw_start;
- ctx->hash = hash;
-
- return NGX_OK;
-
-header_done:
-
- ctx->response->pos = p + 1;
- ctx->state = sw_start;
-
- return NGX_DONE;
-}
-
-
-static void
-ngx_imap_auth_http_block_read(ngx_event_t *rev)
-{
- ngx_connection_t *c;
- ngx_imap_session_t *s;
- ngx_imap_auth_http_ctx_t *ctx;
-
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
- "imap auth http block read");
-
- if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
- c = rev->data;
- s = c->data;
-
- ctx = ngx_imap_get_module_ctx(s, ngx_imap_auth_http_module);
-
- ngx_close_connection(ctx->peer.connection);
- ngx_destroy_pool(ctx->pool);
- ngx_imap_session_internal_server_error(s);
- }
-}
-
-
-static void
-ngx_imap_auth_http_dummy_handler(ngx_event_t *ev)
-{
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, ev->log, 0,
- "imap auth http dummy handler");
-}
-
-
-static ngx_buf_t *
-ngx_imap_auth_http_create_request(ngx_imap_session_t *s, ngx_pool_t *pool,
- ngx_imap_auth_http_conf_t *ahcf)
-{
- size_t len;
- ngx_buf_t *b;
- ngx_str_t login, passwd;
-
- if (ngx_imap_auth_http_escape(pool, &s->login, &login) != NGX_OK) {
- return NULL;
- }
-
- if (ngx_imap_auth_http_escape(pool, &s->passwd, &passwd) != NGX_OK) {
- return NULL;
- }
-
- len = sizeof("GET ") - 1 + ahcf->uri.len + sizeof(" HTTP/1.0" CRLF) - 1
- + sizeof("Host: ") - 1 + ahcf->host_header.len + sizeof(CRLF) - 1
- + sizeof("Auth-Method: ") - 1
- + ngx_imap_auth_http_method[s->auth_method].len
- + sizeof(CRLF) - 1
- + sizeof("Auth-User: ") - 1 + login.len + sizeof(CRLF) - 1
- + sizeof("Auth-Pass: ") - 1 + passwd.len + sizeof(CRLF) - 1
- + sizeof("Auth-Salt: ") - 1 + s->salt.len
- + sizeof("Auth-Protocol: imap" CRLF) - 1
- + sizeof("Auth-Login-Attempt: ") - 1 + NGX_INT_T_LEN
- + sizeof(CRLF) - 1
- + sizeof("Client-IP: ") - 1 + s->connection->addr_text.len
- + sizeof(CRLF) - 1
- + sizeof(CRLF) - 1;
-
- b = ngx_create_temp_buf(pool, len);
- if (b == NULL) {
- return NULL;
- }
-
- b->last = ngx_cpymem(b->last, "GET ", sizeof("GET ") - 1);
- b->last = ngx_copy(b->last, ahcf->uri.data, ahcf->uri.len);
- b->last = ngx_cpymem(b->last, " HTTP/1.0" CRLF,
- sizeof(" HTTP/1.0" CRLF) - 1);
-
- b->last = ngx_cpymem(b->last, "Host: ", sizeof("Host: ") - 1);
- b->last = ngx_copy(b->last, ahcf->host_header.data,
- ahcf->host_header.len);
- *b->last++ = CR; *b->last++ = LF;
-
- b->last = ngx_cpymem(b->last, "Auth-Method: ",
- sizeof("Auth-Method: ") - 1);
- b->last = ngx_cpymem(b->last,
- ngx_imap_auth_http_method[s->auth_method].data,
- ngx_imap_auth_http_method[s->auth_method].len);
- *b->last++ = CR; *b->last++ = LF;
-
- b->last = ngx_cpymem(b->last, "Auth-User: ", sizeof("Auth-User: ") - 1);
- b->last = ngx_copy(b->last, login.data, login.len);
- *b->last++ = CR; *b->last++ = LF;
-
- b->last = ngx_cpymem(b->last, "Auth-Pass: ", sizeof("Auth-Pass: ") - 1);
- b->last = ngx_copy(b->last, passwd.data, passwd.len);
- *b->last++ = CR; *b->last++ = LF;
-
- if (s->auth_method != NGX_IMAP_AUTH_PLAIN && s->salt.len) {
- b->last = ngx_cpymem(b->last, "Auth-Salt: ", sizeof("Auth-Salt: ") - 1);
- b->last = ngx_copy(b->last, s->salt.data, s->salt.len);
-
- s->passwd.data = NULL;
- }
-
- b->last = ngx_cpymem(b->last, "Auth-Protocol: ",
- sizeof("Auth-Protocol: ") - 1);
- b->last = ngx_cpymem(b->last, ngx_imap_auth_http_protocol[s->protocol],
- sizeof("imap") - 1);
- *b->last++ = CR; *b->last++ = LF;
-
- b->last = ngx_sprintf(b->last, "Auth-Login-Attempt: %ui" CRLF,
- s->login_attempt);
-
- b->last = ngx_cpymem(b->last, "Client-IP: ", sizeof("Client-IP: ") - 1);
- b->last = ngx_copy(b->last, s->connection->addr_text.data,
- s->connection->addr_text.len);
- *b->last++ = CR; *b->last++ = LF;
-
- if (ahcf->header.len) {
- b->last = ngx_copy(b->last, ahcf->header.data, ahcf->header.len);
- }
-
- /* add "\r\n" at the header end */
- *b->last++ = CR; *b->last++ = LF;
-
-#if (NGX_DEBUG_IMAP_PASSWD)
- {
- ngx_str_t l;
-
- l.len = b->last - b->pos;
- l.data = b->pos;
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
- "imap auth http header:\n\"%V\"", &l);
- }
-#endif
-
- return b;
-}
-
-
-static ngx_int_t
-ngx_imap_auth_http_escape(ngx_pool_t *pool, ngx_str_t *text, ngx_str_t *escaped)
-{
- u_char ch, *p;
- ngx_uint_t i, n;
-
- n = 0;
-
- for (i = 0; i < text->len; i++) {
- ch = text->data[i];
-
- if (ch == CR || ch == LF) {
- n++;
- }
- }
-
- if (n == 0) {
- *escaped = *text;
- return NGX_OK;
- }
-
- escaped->len = text->len + n * 2;
-
- p = ngx_palloc(pool, escaped->len);
- if (p == NULL) {
- return NGX_ERROR;
- }
-
- escaped->data = p;
-
- for (i = 0; i < text->len; i++) {
- ch = text->data[i];
-
- if (ch == CR) {
- *p++ = '%';
- *p++ = '0';
- *p++ = 'D';
- continue;
- }
-
- if (ch == LF) {
- *p++ = '%';
- *p++ = '0';
- *p++ = 'A';
- continue;
- }
-
- *p++ = ch;
- }
-
- return NGX_OK;
-}
-
-
-static void *
-ngx_imap_auth_http_create_conf(ngx_conf_t *cf)
-{
- ngx_imap_auth_http_conf_t *ahcf;
-
- ahcf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_auth_http_conf_t));
- if (ahcf == NULL) {
- return NGX_CONF_ERROR;
- }
-
- ahcf->timeout = NGX_CONF_UNSET_MSEC;
-
- return ahcf;
-}
-
-
-static char *
-ngx_imap_auth_http_merge_conf(ngx_conf_t *cf, void *parent, void *child)
-{
- ngx_imap_auth_http_conf_t *prev = parent;
- ngx_imap_auth_http_conf_t *conf = child;
-
- u_char *p;
- size_t len;
- ngx_uint_t i;
- ngx_table_elt_t *header;
-
- if (conf->peer == NULL) {
- conf->peer = prev->peer;
- conf->host_header = prev->host_header;
- conf->uri = prev->uri;
- }
-
- ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
-
- if (conf->headers == NULL) {
- conf->headers = prev->headers;
- conf->header = prev->header;
- }
-
- if (conf->headers && conf->header.len == 0) {
- len = 0;
- header = conf->headers->elts;
- for (i = 0; i < conf->headers->nelts; i++) {
- len += header[i].key.len + 2 + header[i].value.len + 2;
- }
-
- p = ngx_palloc(cf->pool, len);
- if (p == NULL) {
- return NGX_CONF_ERROR;
- }
-
- conf->header.len = len;
- conf->header.data = p;
-
- for (i = 0; i < conf->headers->nelts; i++) {
- p = ngx_cpymem(p, header[i].key.data, header[i].key.len);
- *p++ = ':'; *p++ = ' ';
- p = ngx_cpymem(p, header[i].value.data, header[i].value.len);
- *p++ = CR; *p++ = LF;
- }
- }
-
- return NGX_CONF_OK;
-}
-
-
-static char *
-ngx_imap_auth_http(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
- ngx_imap_auth_http_conf_t *ahcf = conf;
-
- ngx_str_t *value;
- ngx_url_t u;
-
- value = cf->args->elts;
-
- ngx_memzero(&u, sizeof(ngx_url_t));
-
- u.url = value[1];
- u.default_port = 80;
- u.uri_part = 1;
- u.one_addr = 1;
-
- if (ngx_parse_url(cf, &u) != NGX_OK) {
- if (u.err) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "%s in auth_http \"%V\"", u.err, &u.url);
- }
- }
-
- ahcf->peer = u.addrs;
-
- ahcf->host_header = u.host;
- ahcf->uri = u.uri;
-
- if (ahcf->uri.len == 0) {
- ahcf->uri.len = sizeof("/") - 1;
- ahcf->uri.data = (u_char *) "/";
- }
-
- return NGX_CONF_OK;
-}
-
-
-static char *
-ngx_imap_auth_http_header(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
- ngx_imap_auth_http_conf_t *ahcf = conf;
-
- ngx_str_t *value;
- ngx_table_elt_t *header;
-
- if (ahcf->headers == NULL) {
- ahcf->headers = ngx_array_create(cf->pool, 1, sizeof(ngx_table_elt_t));
- if (ahcf->headers == NULL) {
- return NGX_CONF_ERROR;
- }
- }
-
- header = ngx_array_push(ahcf->headers);
- if (header == NULL) {
- return NGX_CONF_ERROR;
- }
-
- value = cf->args->elts;
-
- header->key = value[1];
- header->value = value[2];
-
- return NGX_CONF_OK;
-}
diff --git a/src/imap/ngx_imap_core_module.c b/src/imap/ngx_imap_core_module.c
deleted file mode 100644
index 99aa9cc1d..000000000
--- a/src/imap/ngx_imap_core_module.c
+++ /dev/null
@@ -1,603 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- */
-
-
-#include <ngx_config.h>
-#include <ngx_core.h>
-#include <ngx_event.h>
-#include <ngx_imap.h>
-
-
-static void *ngx_imap_core_create_main_conf(ngx_conf_t *cf);
-static void *ngx_imap_core_create_srv_conf(ngx_conf_t *cf);
-static char *ngx_imap_core_merge_srv_conf(ngx_conf_t *cf, void *parent,
- void *child);
-static char *ngx_imap_core_server(ngx_conf_t *cf, ngx_command_t *cmd,
- void *conf);
-static char *ngx_imap_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
- void *conf);
-static char *ngx_imap_core_capability(ngx_conf_t *cf, ngx_command_t *cmd,
- void *conf);
-
-
-static ngx_conf_enum_t ngx_imap_core_procotol[] = {
- { ngx_string("pop3"), NGX_IMAP_POP3_PROTOCOL },
- { ngx_string("imap"), NGX_IMAP_IMAP_PROTOCOL },
- { ngx_null_string, 0 }
-};
-
-
-static ngx_str_t ngx_pop3_default_capabilities[] = {
- ngx_string("TOP"),
- ngx_string("USER"),
- ngx_string("UIDL"),
- ngx_null_string
-};
-
-
-static ngx_str_t ngx_imap_default_capabilities[] = {
- ngx_string("IMAP4"),
- ngx_string("IMAP4rev1"),
- ngx_string("UIDPLUS"),
- ngx_null_string
-};
-
-
-static ngx_conf_bitmask_t ngx_imap_auth_methods[] = {
- { ngx_string("plain"), NGX_IMAP_AUTH_PLAIN_ENABLED },
- { ngx_string("apop"), NGX_IMAP_AUTH_APOP_ENABLED },
- { ngx_string("cram-md5"), NGX_IMAP_AUTH_CRAM_MD5_ENABLED },
- { ngx_null_string, 0 }
-};
-
-
-static ngx_str_t ngx_pop3_auth_plain_capability =
- ngx_string("+OK methods supported:" CRLF
- "LOGIN" CRLF
- "PLAIN" CRLF
- "." CRLF);
-
-
-static ngx_str_t ngx_pop3_auth_cram_md5_capability =
- ngx_string("+OK methods supported:" CRLF
- "LOGIN" CRLF
- "PLAIN" CRLF
- "CRAM-MD5" CRLF
- "." CRLF);
-
-
-
-static ngx_command_t ngx_imap_core_commands[] = {
-
- { ngx_string("server"),
- NGX_IMAP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
- ngx_imap_core_server,
- 0,
- 0,
- NULL },
-
- { ngx_string("listen"),
- NGX_IMAP_SRV_CONF|NGX_CONF_TAKE12,
- ngx_imap_core_listen,
- 0,
- 0,
- NULL },
-
- { ngx_string("protocol"),
- NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_enum_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_core_srv_conf_t, protocol),
- &ngx_imap_core_procotol },
-
- { ngx_string("imap_client_buffer"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_size_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_core_srv_conf_t, imap_client_buffer_size),
- NULL },
-
- { ngx_string("so_keepalive"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_FLAG,
- ngx_conf_set_flag_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_core_srv_conf_t, so_keepalive),
- NULL },
-
- { ngx_string("timeout"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_msec_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_core_srv_conf_t, timeout),
- NULL },
-
- { ngx_string("pop3_capabilities"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_1MORE,
- ngx_imap_core_capability,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_core_srv_conf_t, pop3_capabilities),
- NULL },
-
- { ngx_string("imap_capabilities"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_1MORE,
- ngx_imap_core_capability,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_core_srv_conf_t, imap_capabilities),
- NULL },
-
- { ngx_string("server_name"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_str_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_core_srv_conf_t, server_name),
- NULL },
-
- { ngx_string("auth"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_1MORE,
- ngx_conf_set_bitmask_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_core_srv_conf_t, auth_methods),
- &ngx_imap_auth_methods },
-
- ngx_null_command
-};
-
-
-static ngx_imap_module_t ngx_imap_core_module_ctx = {
- ngx_imap_core_create_main_conf, /* create main configuration */
- NULL, /* init main configuration */
-
- ngx_imap_core_create_srv_conf, /* create server configuration */
- ngx_imap_core_merge_srv_conf /* merge server configuration */
-};
-
-
-ngx_module_t ngx_imap_core_module = {
- NGX_MODULE_V1,
- &ngx_imap_core_module_ctx, /* module context */
- ngx_imap_core_commands, /* module directives */
- NGX_IMAP_MODULE, /* module type */
- NULL, /* init master */
- NULL, /* init module */
- NULL, /* init process */
- NULL, /* init thread */
- NULL, /* exit thread */
- NULL, /* exit process */
- NULL, /* exit master */
- NGX_MODULE_V1_PADDING
-};
-
-
-static void *
-ngx_imap_core_create_main_conf(ngx_conf_t *cf)
-{
- ngx_imap_core_main_conf_t *cmcf;
-
- cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_core_main_conf_t));
- if (cmcf == NULL) {
- return NGX_CONF_ERROR;
- }
-
- if (ngx_array_init(&cmcf->servers, cf->pool, 4,
- sizeof(ngx_imap_core_srv_conf_t *))
- != NGX_OK)
- {
- return NGX_CONF_ERROR;
- }
-
- if (ngx_array_init(&cmcf->listen, cf->pool, 4, sizeof(ngx_imap_listen_t))
- != NGX_OK)
- {
- return NGX_CONF_ERROR;
- }
-
- return cmcf;
-}
-
-
-static void *
-ngx_imap_core_create_srv_conf(ngx_conf_t *cf)
-{
- ngx_imap_core_srv_conf_t *cscf;
-
- cscf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_core_srv_conf_t));
- if (cscf == NULL) {
- return NULL;
- }
-
- cscf->imap_client_buffer_size = NGX_CONF_UNSET_SIZE;
- cscf->protocol = NGX_CONF_UNSET_UINT;
- cscf->timeout = NGX_CONF_UNSET_MSEC;
- cscf->so_keepalive = NGX_CONF_UNSET;
-
- if (ngx_array_init(&cscf->pop3_capabilities, cf->pool, 4, sizeof(ngx_str_t))
- != NGX_OK)
- {
- return NULL;
- }
-
- if (ngx_array_init(&cscf->imap_capabilities, cf->pool, 4, sizeof(ngx_str_t))
- != NGX_OK)
- {
- return NULL;
- }
-
- return cscf;
-}
-
-
-static char *
-ngx_imap_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
-{
- ngx_imap_core_srv_conf_t *prev = parent;
- ngx_imap_core_srv_conf_t *conf = child;
-
- u_char *p;
- size_t size;
- ngx_str_t *c, *d;
- ngx_uint_t i;
-
- ngx_conf_merge_size_value(conf->imap_client_buffer_size,
- prev->imap_client_buffer_size,
- (size_t) ngx_pagesize);
- ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
- ngx_conf_merge_uint_value(conf->protocol, prev->protocol,
- NGX_IMAP_IMAP_PROTOCOL);
- ngx_conf_merge_value(conf->so_keepalive, prev->so_keepalive, 0);
-
-
- ngx_conf_merge_bitmask_value(conf->auth_methods, prev->auth_methods,
- (NGX_CONF_BITMASK_SET|NGX_IMAP_AUTH_PLAIN_ENABLED));
-
-
- ngx_conf_merge_str_value(conf->server_name, prev->server_name, "");
-
- if (conf->server_name.len == 0) {
- conf->server_name.data = ngx_palloc(cf->pool, NGX_MAXHOSTNAMELEN);
- if (conf->server_name.data == NULL) {
- return NGX_CONF_ERROR;
- }
-
- if (gethostname((char *) conf->server_name.data, NGX_MAXHOSTNAMELEN)
- == -1)
- {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, ngx_errno,
- "gethostname() failed");
- return NGX_CONF_ERROR;
- }
-
- conf->server_name.len = ngx_strlen(conf->server_name.data);
- }
-
-
- if (conf->pop3_capabilities.nelts == 0) {
- conf->pop3_capabilities = prev->pop3_capabilities;
- }
-
- if (conf->pop3_capabilities.nelts == 0) {
-
- for (d = ngx_pop3_default_capabilities; d->len; d++) {
- c = ngx_array_push(&conf->pop3_capabilities);
- if (c == NULL) {
- return NGX_CONF_ERROR;
- }
-
- *c = *d;
- }
- }
-
- size = sizeof("+OK Capability list follows" CRLF) - 1
- + sizeof("." CRLF) - 1;
-
- c = conf->pop3_capabilities.elts;
- for (i = 0; i < conf->pop3_capabilities.nelts; i++) {
- size += c[i].len + sizeof(CRLF) - 1;
- }
-
- if (conf->auth_methods & NGX_IMAP_AUTH_CRAM_MD5_ENABLED) {
- size += sizeof("SASL LOGIN PLAIN CRAM-MD5" CRLF) - 1;
-
- } else {
- size += sizeof("SASL LOGIN PLAIN" CRLF) - 1;
- }
-
- p = ngx_palloc(cf->pool, size);
- if (p == NULL) {
- return NGX_CONF_ERROR;
- }
-
- conf->pop3_capability.len = size;
- conf->pop3_capability.data = p;
-
- p = ngx_cpymem(p, "+OK Capability list follows" CRLF,
- sizeof("+OK Capability list follows" CRLF) - 1);
-
- for (i = 0; i < conf->pop3_capabilities.nelts; i++) {
- p = ngx_cpymem(p, c[i].data, c[i].len);
- *p++ = CR; *p++ = LF;
- }
-
- if (conf->auth_methods & NGX_IMAP_AUTH_CRAM_MD5_ENABLED) {
- p = ngx_cpymem(p, "SASL LOGIN PLAIN CRAM-MD5" CRLF,
- sizeof("SASL LOGIN PLAIN CRAM-MD5" CRLF) - 1);
-
- } else {
- p = ngx_cpymem(p, "SASL LOGIN PLAIN" CRLF,
- sizeof("SASL LOGIN PLAIN" CRLF) - 1);
- }
-
- *p++ = '.'; *p++ = CR; *p = LF;
-
-
- size += sizeof("STLS" CRLF) - 1;
-
- p = ngx_palloc(cf->pool, size);
- if (p == NULL) {
- return NGX_CONF_ERROR;
- }
-
- conf->pop3_starttls_capability.len = size;
- conf->pop3_starttls_capability.data = p;
-
- p = ngx_cpymem(p, conf->pop3_capability.data,
- conf->pop3_capability.len - (sizeof("." CRLF) - 1));
-
- p = ngx_cpymem(p, "STLS" CRLF, sizeof("STLS" CRLF) - 1);
- *p++ = '.'; *p++ = CR; *p = LF;
-
-
- if (conf->auth_methods & NGX_IMAP_AUTH_CRAM_MD5_ENABLED) {
- conf->pop3_auth_capability = ngx_pop3_auth_cram_md5_capability;
-
- } else {
- conf->pop3_auth_capability = ngx_pop3_auth_plain_capability;
- }
-
-
- if (conf->imap_capabilities.nelts == 0) {
- conf->imap_capabilities = prev->imap_capabilities;
- }
-
- if (conf->imap_capabilities.nelts == 0) {
-
- for (d = ngx_imap_default_capabilities; d->len; d++) {
- c = ngx_array_push(&conf->imap_capabilities);
- if (c == NULL) {
- return NGX_CONF_ERROR;
- }
-
- *c = *d;
- }
- }
-
- size = sizeof("* CAPABILITY") - 1 + sizeof(CRLF) - 1;
-
- c = conf->imap_capabilities.elts;
- for (i = 0; i < conf->imap_capabilities.nelts; i++) {
- size += 1 + c[i].len;
- }
-
- p = ngx_palloc(cf->pool, size);
- if (p == NULL) {
- return NGX_CONF_ERROR;
- }
-
- conf->imap_capability.len = size;
- conf->imap_capability.data = p;
-
- p = ngx_cpymem(p, "* CAPABILITY", sizeof("* CAPABILITY") - 1);
-
- for (i = 0; i < conf->imap_capabilities.nelts; i++) {
- *p++ = ' ';
- p = ngx_cpymem(p, c[i].data, c[i].len);
- }
-
- *p++ = CR; *p = LF;
-
-
- size += sizeof(" STARTTLS") - 1;
-
- p = ngx_palloc(cf->pool, size);
- if (p == NULL) {
- return NGX_CONF_ERROR;
- }
-
- conf->imap_starttls_capability.len = size;
- conf->imap_starttls_capability.data = p;
-
- p = ngx_cpymem(p, conf->imap_capability.data,
- conf->imap_capability.len - (sizeof(CRLF) - 1));
- p = ngx_cpymem(p, " STARTTLS", sizeof(" STARTTLS") - 1);
- *p++ = CR; *p = LF;
-
-
- size += sizeof(" LOGINDISABLED") - 1;
-
- p = ngx_palloc(cf->pool, size);
- if (p == NULL) {
- return NGX_CONF_ERROR;
- }
-
- conf->imap_starttls_only_capability.len = size;
- conf->imap_starttls_only_capability.data = p;
-
- p = ngx_cpymem(p, conf->imap_starttls_capability.data,
- conf->imap_starttls_capability.len - (sizeof(CRLF) - 1));
- p = ngx_cpymem(p, " LOGINDISABLED", sizeof(" LOGINDISABLED") - 1);
- *p++ = CR; *p = LF;
-
-
- return NGX_CONF_OK;
-}
-
-
-static char *
-ngx_imap_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
- char *rv;
- void *mconf;
- ngx_uint_t m;
- ngx_conf_t pcf;
- ngx_imap_module_t *module;
- ngx_imap_conf_ctx_t *ctx, *imap_ctx;
- ngx_imap_core_srv_conf_t *cscf, **cscfp;
- ngx_imap_core_main_conf_t *cmcf;
-
-
- ctx = ngx_pcalloc(cf->pool, sizeof(ngx_imap_conf_ctx_t));
- if (ctx == NULL) {
- return NGX_CONF_ERROR;
- }
-
- imap_ctx = cf->ctx;
- ctx->main_conf = imap_ctx->main_conf;
-
- /* the server{}'s srv_conf */
-
- ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_imap_max_module);
- if (ctx->srv_conf == NULL) {
- return NGX_CONF_ERROR;
- }
-
- for (m = 0; ngx_modules[m]; m++) {
- if (ngx_modules[m]->type != NGX_IMAP_MODULE) {
- continue;
- }
-
- module = ngx_modules[m]->ctx;
-
- if (module->create_srv_conf) {
- mconf = module->create_srv_conf(cf);
- if (mconf == NULL) {
- return NGX_CONF_ERROR;
- }
-
- ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
- }
- }
-
- /* the server configuration context */
-
- cscf = ctx->srv_conf[ngx_imap_core_module.ctx_index];
- cscf->ctx = ctx;
-
- cmcf = ctx->main_conf[ngx_imap_core_module.ctx_index];
-
- cscfp = ngx_array_push(&cmcf->servers);
- if (cscfp == NULL) {
- return NGX_CONF_ERROR;
- }
-
- *cscfp = cscf;
-
-
- /* parse inside server{} */
-
- pcf = *cf;
- cf->ctx = ctx;
- cf->cmd_type = NGX_IMAP_SRV_CONF;
-
- rv = ngx_conf_parse(cf, NULL);
-
- *cf = pcf;
-
- return rv;
-}
-
-
-/* AF_INET only */
-
-static char *
-ngx_imap_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
- ngx_str_t *value;
- ngx_url_t u;
- ngx_uint_t i;
- ngx_imap_listen_t *imls;
- ngx_imap_core_main_conf_t *cmcf;
-
- value = cf->args->elts;
-
- ngx_memzero(&u, sizeof(ngx_url_t));
-
- u.url = value[1];
- u.listen = 1;
-
- if (ngx_parse_url(cf, &u) != NGX_OK) {
- if (u.err) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "%s in \"%V\" of the \"listen\" directive",
- u.err, &u.url);
- }
-
- return NGX_CONF_ERROR;
- }
-
- cmcf = ngx_imap_conf_get_module_main_conf(cf, ngx_imap_core_module);
-
- imls = cmcf->listen.elts;
-
- for (i = 0; i < cmcf->listen.nelts; i++) {
-
- if (imls[i].addr != u.addr.in_addr || imls[i].port != u.port) {
- continue;
- }
-
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "duplicate \"%V\" address and port pair", &u.url);
- return NGX_CONF_ERROR;
- }
-
- imls = ngx_array_push(&cmcf->listen);
- if (imls == NULL) {
- return NGX_CONF_ERROR;
- }
-
- ngx_memzero(imls, sizeof(ngx_imap_listen_t));
-
- imls->addr = u.addr.in_addr;
- imls->port = u.port;
- imls->family = AF_INET;
- imls->ctx = cf->ctx;
-
- if (cf->args->nelts == 2) {
- return NGX_CONF_OK;
- }
-
- if (ngx_strcmp(value[2].data, "bind") == 0) {
- imls->bind = 1;
- return NGX_CONF_OK;
- }
-
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "the invalid \"%V\" parameter", &value[2]);
- return NGX_CONF_ERROR;
-}
-
-
-static char *
-ngx_imap_core_capability(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
- char *p = conf;
-
- ngx_str_t *c, *value;
- ngx_uint_t i;
- ngx_array_t *a;
-
- a = (ngx_array_t *) (p + cmd->offset);
-
- value = cf->args->elts;
-
- for (i = 1; i < cf->args->nelts; i++) {
- c = ngx_array_push(a);
- if (c == NULL) {
- return NGX_CONF_ERROR;
- }
-
- *c = value[i];
- }
-
- return NGX_CONF_OK;
-}
diff --git a/src/imap/ngx_imap_handler.c b/src/imap/ngx_imap_handler.c
deleted file mode 100644
index a32b10782..000000000
--- a/src/imap/ngx_imap_handler.c
+++ /dev/null
@@ -1,1357 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- */
-
-
-#include <ngx_config.h>
-#include <ngx_core.h>
-#include <ngx_event.h>
-#include <ngx_imap.h>
-
-
-static void ngx_imap_init_session(ngx_connection_t *c);
-static void ngx_imap_init_protocol(ngx_event_t *rev);
-static ngx_int_t ngx_imap_decode_auth_plain(ngx_imap_session_t *s,
- ngx_str_t *encoded);
-static void ngx_imap_do_auth(ngx_imap_session_t *s);
-static ngx_int_t ngx_imap_read_command(ngx_imap_session_t *s);
-static u_char *ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len);
-
-#if (NGX_IMAP_SSL)
-static void ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c);
-static void ngx_imap_ssl_handshake_handler(ngx_connection_t *c);
-#endif
-
-
-static ngx_str_t greetings[] = {
- ngx_string("+OK POP3 ready" CRLF),
- ngx_string("* OK IMAP4 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_next[] = "+ " CRLF;
-static u_char pop3_username[] = "+ VXNlcm5hbWU6" CRLF;
-static u_char pop3_password[] = "+ UGFzc3dvcmQ6" CRLF;
-static u_char pop3_invalid_command[] = "-ERR invalid command" CRLF;
-
-static u_char imap_star[] = "* ";
-static u_char imap_ok[] = "OK completed" CRLF;
-static u_char imap_next[] = "+ OK" CRLF;
-static u_char imap_bye[] = "* BYE" CRLF;
-static u_char imap_invalid_command[] = "BAD invalid command" CRLF;
-
-
-void
-ngx_imap_init_connection(ngx_connection_t *c)
-{
- in_addr_t in_addr;
- socklen_t len;
- ngx_uint_t i;
- struct sockaddr_in sin;
- ngx_imap_log_ctx_t *ctx;
- ngx_imap_in_port_t *imip;
- ngx_imap_in_addr_t *imia;
- ngx_imap_session_t *s;
-#if (NGX_IMAP_SSL)
- ngx_imap_ssl_conf_t *sslcf;
-#endif
-
-
- /* find the server configuration for the address:port */
-
- /* AF_INET only */
-
- imip = c->listening->servers;
- imia = imip->addrs;
-
- i = 0;
-
- if (imip->naddrs > 1) {
-
- /*
- * There are several addresses on this port and one of them
- * is the "*:port" wildcard so getsockname() is needed to determine
- * the server address.
- *
- * AcceptEx() already gave this address.
- */
-
-#if (NGX_WIN32)
- if (c->local_sockaddr) {
- in_addr =
- ((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
-
- } else
-#endif
- {
- len = sizeof(struct sockaddr_in);
- if (getsockname(c->fd, (struct sockaddr *) &sin, &len) == -1) {
- ngx_connection_error(c, ngx_socket_errno,
- "getsockname() failed");
- ngx_imap_close_connection(c);
- return;
- }
-
- in_addr = sin.sin_addr.s_addr;
- }
-
- /* the last address is "*" */
-
- for ( /* void */ ; i < imip->naddrs - 1; i++) {
- if (in_addr == imia[i].addr) {
- break;
- }
- }
- }
-
-
- s = ngx_pcalloc(c->pool, sizeof(ngx_imap_session_t));
- if (s == NULL) {
- ngx_imap_close_connection(c);
- return;
- }
-
- s->main_conf = imia[i].ctx->main_conf;
- s->srv_conf = imia[i].ctx->srv_conf;
-
- s->addr_text = &imia[i].addr_text;
-
- c->data = s;
- s->connection = c;
-
- ngx_log_error(NGX_LOG_INFO, c->log, 0, "*%ui client %V connected to %V",
- c->number, &c->addr_text, s->addr_text);
-
- ctx = ngx_palloc(c->pool, sizeof(ngx_imap_log_ctx_t));
- if (ctx == NULL) {
- ngx_imap_close_connection(c);
- return;
- }
-
- ctx->client = &c->addr_text;
- ctx->session = s;
-
- c->log->connection = c->number;
- c->log->handler = ngx_imap_log_error;
- c->log->data = ctx;
- c->log->action = "sending client greeting line";
-
- c->log_error = NGX_ERROR_INFO;
-
-#if (NGX_IMAP_SSL)
-
- sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
-
- if (sslcf->enable) {
- ngx_imap_ssl_init_connection(&sslcf->ssl, c);
- return;
- }
-
-#endif
-
- ngx_imap_init_session(c);
-}
-
-
-#if (NGX_IMAP_SSL)
-
-static void
-ngx_imap_starttls_handler(ngx_event_t *rev)
-{
- ngx_connection_t *c;
- ngx_imap_session_t *s;
- ngx_imap_ssl_conf_t *sslcf;
-
- c = rev->data;
- s = c->data;
- s->starttls = 1;
-
- c->log->action = "in starttls state";
-
- sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
-
- ngx_imap_ssl_init_connection(&sslcf->ssl, c);
-}
-
-
-static void
-ngx_imap_ssl_init_connection(ngx_ssl_t *ssl, ngx_connection_t *c)
-{
- ngx_imap_session_t *s;
- ngx_imap_core_srv_conf_t *cscf;
-
- if (ngx_ssl_create_connection(ssl, c, 0) == NGX_ERROR) {
- ngx_imap_close_connection(c);
- return;
- }
-
- if (ngx_ssl_handshake(c) == NGX_AGAIN) {
-
- s = c->data;
-
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
-
- ngx_add_timer(c->read, cscf->timeout);
-
- c->ssl->handler = ngx_imap_ssl_handshake_handler;
-
- return;
- }
-
- ngx_imap_ssl_handshake_handler(c);
-}
-
-
-static void
-ngx_imap_ssl_handshake_handler(ngx_connection_t *c)
-{
- ngx_imap_session_t *s;
-
- if (c->ssl->handshaked) {
-
- s = c->data;
-
- if (s->starttls) {
- c->read->handler = ngx_imap_init_protocol;
- c->write->handler = ngx_imap_send;
-
- ngx_imap_init_protocol(c->read);
-
- return;
- }
-
- ngx_imap_init_session(c);
- return;
- }
-
- ngx_imap_close_connection(c);
-}
-
-#endif
-
-
-static void
-ngx_imap_init_session(ngx_connection_t *c)
-{
- u_char *p;
- ngx_imap_session_t *s;
- ngx_imap_core_srv_conf_t *cscf;
-
- c->read->handler = ngx_imap_init_protocol;
- c->write->handler = ngx_imap_send;
-
- s = c->data;
-
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
-
- s->protocol = cscf->protocol;
-
- s->ctx = ngx_pcalloc(c->pool, sizeof(void *) * ngx_imap_max_module);
- if (s->ctx == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- s->out = greetings[s->protocol];
-
- if ((cscf->auth_methods & NGX_IMAP_AUTH_APOP_ENABLED)
- && s->protocol == NGX_IMAP_POP3_PROTOCOL)
- {
- s->salt.data = ngx_palloc(c->pool,
- sizeof(" <18446744073709551616.@>" CRLF) - 1
- + NGX_TIME_T_LEN
- + cscf->server_name.len);
- if (s->salt.data == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- s->salt.len = ngx_sprintf(s->salt.data, "<%ul.%T@%V>" CRLF,
- ngx_random(), ngx_time(), &cscf->server_name)
- - s->salt.data;
-
- s->out.data = ngx_palloc(c->pool, greetings[0].len + 1 + s->salt.len);
- if (s->out.data == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- p = ngx_cpymem(s->out.data, greetings[0].data, greetings[0].len - 2);
- *p++ = ' ';
- p = ngx_cpymem(p, s->salt.data, s->salt.len);
-
- s->out.len = p - s->out.data;
- }
-
- ngx_add_timer(c->read, cscf->timeout);
-
- if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
- ngx_imap_close_connection(c);
- }
-
- ngx_imap_send(c->write);
-}
-
-
-void
-ngx_imap_send(ngx_event_t *wev)
-{
- ngx_int_t n;
- ngx_connection_t *c;
- ngx_imap_session_t *s;
- ngx_imap_core_srv_conf_t *cscf;
-
- c = wev->data;
- s = c->data;
-
- if (wev->timedout) {
- ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
- c->timedout = 1;
- ngx_imap_close_connection(c);
- return;
- }
-
- if (s->out.len == 0) {
- if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
- ngx_imap_close_connection(c);
- }
-
- return;
- }
-
- n = c->send(c, s->out.data, s->out.len);
-
- if (n > 0) {
- s->out.len -= n;
-
- if (wev->timer_set) {
- ngx_del_timer(wev);
- }
-
- if (s->quit) {
- ngx_imap_close_connection(c);
- return;
- }
-
- if (s->blocked) {
- c->read->handler(c->read);
- }
-
- return;
- }
-
- if (n == NGX_ERROR) {
- ngx_imap_close_connection(c);
- return;
- }
-
- /* n == NGX_AGAIN */
-
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
-
- ngx_add_timer(c->write, cscf->timeout);
-
- if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
- ngx_imap_close_connection(c);
- return;
- }
-}
-
-
-static void
-ngx_imap_init_protocol(ngx_event_t *rev)
-{
- size_t size;
- ngx_connection_t *c;
- ngx_imap_session_t *s;
- ngx_imap_core_srv_conf_t *cscf;
-
- c = rev->data;
-
- c->log->action = "in auth state";
-
- if (rev->timedout) {
- ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
- c->timedout = 1;
- ngx_imap_close_connection(c);
- return;
- }
-
- s = c->data;
-
- if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
- size = 128;
- s->imap_state = ngx_pop3_start;
- c->read->handler = ngx_pop3_auth_state;
-
- } else {
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
- size = cscf->imap_client_buffer_size;
- s->imap_state = ngx_imap_start;
- c->read->handler = ngx_imap_auth_state;
- }
-
- if (s->buffer == NULL) {
- if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t))
- == NGX_ERROR)
- {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- s->buffer = ngx_create_temp_buf(c->pool, size);
- if (s->buffer == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
- }
-
- c->read->handler(rev);
-}
-
-
-void
-ngx_imap_auth_state(ngx_event_t *rev)
-{
- u_char *text, *last, *p, *dst, *src, *end;
- ssize_t text_len, last_len;
- ngx_str_t *arg;
- ngx_int_t rc;
- ngx_uint_t tag, i;
- ngx_connection_t *c;
- ngx_imap_session_t *s;
- ngx_imap_core_srv_conf_t *cscf;
-#if (NGX_IMAP_SSL)
- ngx_imap_ssl_conf_t *sslcf;
-#endif
-
- c = rev->data;
- s = c->data;
-
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth state");
-
- if (rev->timedout) {
- ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
- c->timedout = 1;
- ngx_imap_close_connection(c);
- return;
- }
-
- if (s->out.len) {
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap send handler busy");
- s->blocked = 1;
- return;
- }
-
- s->blocked = 0;
-
- rc = ngx_imap_read_command(s);
-
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth: %i", rc);
-
- if (rc == NGX_AGAIN || rc == NGX_ERROR) {
- return;
- }
-
- tag = 1;
-
- text = NULL;
- text_len = 0;
-
- last = imap_ok;
- last_len = sizeof(imap_ok) - 1;
-
- if (rc == NGX_OK) {
-
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap auth command: %i",
- s->command);
-
- if (s->backslash) {
-
- arg = s->args.elts;
-
- for (i = 0; i < s->args.nelts; i++) {
- dst = arg[i].data;
- end = dst + arg[i].len;
-
- for (src = dst; src < end; dst++) {
- *dst = *src;
- if (*src++ == '\\') {
- *dst = *src++;
- }
- }
-
- arg[i].len = dst - arg[i].data;
- }
-
- s->backslash = 0;
- }
-
- switch (s->command) {
-
- case NGX_IMAP_LOGIN:
-
-#if (NGX_IMAP_SSL)
-
- if (c->ssl == NULL) {
- sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
-
- if (sslcf->starttls == NGX_IMAP_STARTTLS_ONLY) {
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
- }
- }
-#endif
-
- arg = s->args.elts;
-
- if (s->args.nelts == 2 && arg[0].len) {
-
- s->login.len = arg[0].len;
- s->login.data = ngx_palloc(c->pool, s->login.len);
- if (s->login.data == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ngx_memcpy(s->login.data, arg[0].data, s->login.len);
-
- s->passwd.len = arg[1].len;
- s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
- if (s->passwd.data == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
-
-#if (NGX_DEBUG_IMAP_PASSWD)
- ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "imap login:\"%V\" passwd:\"%V\"",
- &s->login, &s->passwd);
-#else
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "imap login:\"%V\"", &s->login);
-#endif
-
- ngx_imap_do_auth(s);
- return;
- }
-
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
-
- case NGX_IMAP_CAPABILITY:
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
-
-#if (NGX_IMAP_SSL)
-
- if (c->ssl == NULL) {
- sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
-
- if (sslcf->starttls == NGX_IMAP_STARTTLS_ON) {
- text_len = cscf->imap_starttls_capability.len;
- text = cscf->imap_starttls_capability.data;
- break;
- }
-
- if (sslcf->starttls == NGX_IMAP_STARTTLS_ONLY) {
- text_len = cscf->imap_starttls_only_capability.len;
- text = cscf->imap_starttls_only_capability.data;
- break;
- }
- }
-#endif
-
- text_len = cscf->imap_capability.len;
- text = cscf->imap_capability.data;
- break;
-
- case NGX_IMAP_LOGOUT:
- s->quit = 1;
- text = imap_bye;
- text_len = sizeof(imap_bye) - 1;
- break;
-
- case NGX_IMAP_NOOP:
- break;
-
-#if (NGX_IMAP_SSL)
-
- case NGX_IMAP_STARTTLS:
- if (c->ssl == NULL) {
- sslcf = ngx_imap_get_module_srv_conf(s, ngx_imap_ssl_module);
- if (sslcf->starttls) {
- c->read->handler = ngx_imap_starttls_handler;
- break;
- }
- }
-
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
-#endif
-
- default:
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
- }
-
- } else if (rc == NGX_IMAP_NEXT) {
- last = imap_next;
- last_len = sizeof(imap_next) - 1;
- tag = 0;
- }
-
- if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
- last = imap_invalid_command;
- last_len = sizeof(imap_invalid_command) - 1;
- }
-
- if (tag) {
- if (s->tag.len == 0) {
- s->tag.len = sizeof(imap_star) - 1;
- s->tag.data = (u_char *) imap_star;
- }
-
- if (s->tagged_line.len < s->tag.len + text_len + last_len) {
- s->tagged_line.len = s->tag.len + text_len + last_len;
- s->tagged_line.data = ngx_palloc(c->pool, s->tagged_line.len);
- if (s->tagged_line.data == NULL) {
- ngx_imap_close_connection(c);
- return;
- }
- }
-
- s->out.data = s->tagged_line.data;
- s->out.len = s->tag.len + text_len + last_len;
-
- p = s->out.data;
-
- if (text) {
- p = ngx_cpymem(p, text, text_len);
- }
- p = ngx_cpymem(p, s->tag.data, s->tag.len);
- ngx_memcpy(p, last, last_len);
-
-
- } else {
- s->out.data = last;
- s->out.len = last_len;
- }
-
- if (rc != NGX_IMAP_NEXT) {
- s->args.nelts = 0;
- s->buffer->pos = s->buffer->start;
- s->buffer->last = s->buffer->start;
- s->tag.len = 0;
- }
-
- ngx_imap_send(c->write);
-}
-
-
-void
-ngx_pop3_auth_state(ngx_event_t *rev)
-{
- u_char *text, *p, *last;
- ssize_t size;
- ngx_int_t rc;
- ngx_str_t *arg, salt;
- ngx_connection_t *c;
- ngx_imap_session_t *s;
- ngx_imap_core_srv_conf_t *cscf;
-#if (NGX_IMAP_SSL)
- ngx_imap_ssl_conf_t *sslcf;
-#endif
-
- c = rev->data;
- s = c->data;
-
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "pop3 auth state");
-
- if (rev->timedout) {
- ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
- c->timedout = 1;
- ngx_imap_close_connection(c);
- return;
- }
-
- if (s->out.len) {
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, c->log, 0, "imap send handler busy");
- s->blocked = 1;
- return;
- }
-
- s->blocked = 0;
-
- rc = ngx_imap_read_command(s);
-
- if (rc == NGX_AGAIN || rc == NGX_ERROR) {
- return;
- }
-
- text = pop3_ok;
- size = sizeof(pop3_ok) - 1;
-
- if (rc == NGX_OK) {
- switch (s->imap_state) {
-
- case ngx_pop3_start:
-
- switch (s->command) {
-
- case NGX_POP3_USER:
- if (s->args.nelts == 1) {
- s->imap_state = ngx_pop3_user;
-
- arg = s->args.elts;
- s->login.len = arg[0].len;
- s->login.data = ngx_palloc(c->pool, s->login.len);
- if (s->login.data == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ngx_memcpy(s->login.data, arg[0].data, s->login.len);
-
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "pop3 login: \"%V\"", &s->login);
-
- break;
- }
-
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
-
- case NGX_POP3_CAPA:
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
-
-#if (NGX_IMAP_SSL)
-
- if (c->ssl == NULL) {
- sslcf = ngx_imap_get_module_srv_conf(s,
- ngx_imap_ssl_module);
- if (sslcf->starttls) {
- size = cscf->pop3_starttls_capability.len;
- text = cscf->pop3_starttls_capability.data;
- break;
- }
- }
-#endif
-
- size = cscf->pop3_capability.len;
- text = cscf->pop3_capability.data;
- break;
-
- case NGX_POP3_APOP:
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
-
- if ((cscf->auth_methods & NGX_IMAP_AUTH_APOP_ENABLED)
- && s->args.nelts == 2)
- {
- arg = s->args.elts;
-
- s->login.len = arg[0].len;
- s->login.data = ngx_palloc(c->pool, s->login.len);
- if (s->login.data == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ngx_memcpy(s->login.data, arg[0].data, s->login.len);
-
- s->passwd.len = arg[1].len;
- s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
- if (s->passwd.data == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
-
- ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "pop3 apop: \"%V\" \"%V\"",
- &s->login, &s->passwd);
-
- s->auth_method = NGX_IMAP_AUTH_APOP;
-
- ngx_imap_do_auth(s);
- return;
- }
-
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
-
- case NGX_POP3_AUTH:
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
-
- if (s->args.nelts == 0) {
- size = cscf->pop3_auth_capability.len;
- text = cscf->pop3_auth_capability.data;
- s->state = 0;
- break;
- }
-
- arg = s->args.elts;
-
- if (arg[0].len == 5) {
-
- if (ngx_strncasecmp(arg[0].data, (u_char *) "LOGIN", 5)
- == 0)
- {
-
- if (s->args.nelts != 1) {
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
- }
-
- s->imap_state = ngx_pop3_auth_login_username;
-
- size = sizeof(pop3_username) - 1;
- text = pop3_username;
-
- break;
-
- } else if (ngx_strncasecmp(arg[0].data, (u_char *) "PLAIN",
- 5)
- == 0)
- {
-
- if (s->args.nelts == 1) {
- s->imap_state = ngx_pop3_auth_plain;
-
- size = sizeof(pop3_next) - 1;
- text = pop3_next;
-
- break;
- }
-
- if (s->args.nelts == 2) {
-
- /*
- * workaround for Eudora for Mac: it sends
- * AUTH PLAIN [base64 encoded]
- */
-
- rc = ngx_imap_decode_auth_plain(s, &arg[1]);
-
- if (rc == NGX_OK) {
- ngx_imap_do_auth(s);
- return;
- }
-
- if (rc == NGX_ERROR) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- /* rc == NGX_IMAP_PARSE_INVALID_COMMAND */
-
- break;
- }
-
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
- }
-
- } else if (arg[0].len == 8
- && ngx_strncasecmp(arg[0].data,
- (u_char *) "CRAM-MD5", 8)
- == 0)
- {
- s->imap_state = ngx_pop3_auth_cram_md5;
-
- text = ngx_palloc(c->pool,
- sizeof("+ " CRLF) - 1
- + ngx_base64_encoded_length(s->salt.len));
- if (text == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- text[0] = '+'; text[1]= ' ';
- salt.data = &text[2];
- s->salt.len -= 2;
-
- ngx_encode_base64(&salt, &s->salt);
-
- s->salt.len += 2;
- size = 2 + salt.len;
- text[size++] = CR; text[size++] = LF;
-
- break;
- }
-
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
-
- case NGX_POP3_QUIT:
- s->quit = 1;
- break;
-
- case NGX_POP3_NOOP:
- break;
-
-#if (NGX_IMAP_SSL)
-
- case NGX_POP3_STLS:
- if (c->ssl == NULL) {
- sslcf = ngx_imap_get_module_srv_conf(s,
- ngx_imap_ssl_module);
- if (sslcf->starttls) {
- c->read->handler = ngx_imap_starttls_handler;
- break;
- }
- }
-
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
-#endif
-
- default:
- s->imap_state = ngx_pop3_start;
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
- }
-
- break;
-
- case ngx_pop3_user:
-
- switch (s->command) {
-
- case NGX_POP3_PASS:
- if (s->args.nelts == 1) {
- arg = s->args.elts;
- s->passwd.len = arg[0].len;
- s->passwd.data = ngx_palloc(c->pool, s->passwd.len);
- if (s->passwd.data == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- ngx_memcpy(s->passwd.data, arg[0].data, s->passwd.len);
-
-#if (NGX_DEBUG_IMAP_PASSWD)
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "pop3 passwd: \"%V\"", &s->passwd);
-#endif
-
- ngx_imap_do_auth(s);
- return;
- }
-
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
-
- case NGX_POP3_CAPA:
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
- size = cscf->pop3_capability.len;
- text = cscf->pop3_capability.data;
- break;
-
- case NGX_POP3_QUIT:
- s->quit = 1;
- break;
-
- case NGX_POP3_NOOP:
- break;
-
- default:
- s->imap_state = ngx_pop3_start;
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
- }
-
- break;
-
- /* suppress warinings */
- case ngx_pop3_passwd:
- break;
-
- case ngx_pop3_auth_login_username:
- arg = s->args.elts;
- s->imap_state = ngx_pop3_auth_login_password;
-
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "pop3 auth login username: \"%V\"", &arg[0]);
-
- s->login.data = ngx_palloc(c->pool,
- ngx_base64_decoded_length(arg[0].len));
- if (s->login.data == NULL){
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- if (ngx_decode_base64(&s->login, &arg[0]) != NGX_OK) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent invalid base64 encoding "
- "in AUTH LOGIN command");
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
- }
-
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "pop3 auth login username: \"%V\"", &s->login);
-
- size = sizeof(pop3_password) - 1;
- text = pop3_password;
-
- break;
-
- case ngx_pop3_auth_login_password:
- arg = s->args.elts;
-
-#if (NGX_DEBUG_IMAP_PASSWD)
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "pop3 auth login password: \"%V\"", &arg[0]);
-#endif
-
- s->passwd.data = ngx_palloc(c->pool,
- ngx_base64_decoded_length(arg[0].len));
- if (s->passwd.data == NULL){
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- if (ngx_decode_base64(&s->passwd, &arg[0]) != NGX_OK) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent invalid base64 encoding "
- "in AUTH LOGIN command");
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
- }
-
-#if (NGX_DEBUG_IMAP_PASSWD)
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "pop3 auth login password: \"%V\"", &s->passwd);
-#endif
-
- ngx_imap_do_auth(s);
- return;
-
- case ngx_pop3_auth_plain:
- arg = s->args.elts;
-
- rc = ngx_imap_decode_auth_plain(s, &arg[0]);
-
- if (rc == NGX_OK) {
- ngx_imap_do_auth(s);
- return;
- }
-
- if (rc == NGX_ERROR) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- /* rc == NGX_IMAP_PARSE_INVALID_COMMAND */
-
- break;
-
- case ngx_pop3_auth_cram_md5:
- arg = s->args.elts;
-
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "pop3 auth cram-md5: \"%V\"", &arg[0]);
-
- s->login.data = ngx_palloc(c->pool,
- ngx_base64_decoded_length(arg[0].len));
- if (s->login.data == NULL){
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- if (ngx_decode_base64(&s->login, &arg[0]) != NGX_OK) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent invalid base64 encoding "
- "in AUTH CRAM-MD5 command");
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
- }
-
- p = s->login.data;
- last = p + s->login.len;
-
- while (p < last) {
- if (*p++ == ' ') {
- s->login.len = p - s->login.data - 1;
- s->passwd.len = last - p;
- s->passwd.data = p;
- break;
- }
- }
-
- if (s->passwd.len != 32) {
- ngx_log_error(NGX_LOG_INFO, c->log, 0,
- "client sent invalid CRAM-MD5 hash "
- "in AUTH CRAM-MD5 command");
- rc = NGX_IMAP_PARSE_INVALID_COMMAND;
- break;
- }
-
- ngx_log_debug2(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "pop3 auth cram-md5: \"%V\" \"%V\"",
- &s->login, &s->passwd);
-
- s->auth_method = NGX_IMAP_AUTH_CRAM_MD5;
-
- ngx_imap_do_auth(s);
- return;
- }
- }
-
- if (rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
- s->imap_state = ngx_pop3_start;
- s->state = 0;
- text = pop3_invalid_command;
- size = sizeof(pop3_invalid_command) - 1;
- }
-
- s->args.nelts = 0;
- s->buffer->pos = s->buffer->start;
- s->buffer->last = s->buffer->start;
-
- if (s->state) {
- s->arg_start = s->buffer->start;
- }
-
- s->out.data = text;
- s->out.len = size;
-
- ngx_imap_send(c->write);
-}
-
-
-static ngx_int_t
-ngx_imap_decode_auth_plain(ngx_imap_session_t *s, ngx_str_t *encoded)
-{
- u_char *p, *last;
- ngx_str_t plain;
-
-#if (NGX_DEBUG_IMAP_PASSWD)
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
- "pop3 auth plain: \"%V\"", encoded);
-#endif
-
- plain.data = ngx_palloc(s->connection->pool,
- ngx_base64_decoded_length(encoded->len));
- if (plain.data == NULL){
- return NGX_ERROR;
- }
-
- if (ngx_decode_base64(&plain, encoded) != NGX_OK) {
- ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
- "client sent invalid base64 encoding "
- "in AUTH PLAIN command");
- return NGX_IMAP_PARSE_INVALID_COMMAND;
- }
-
- p = plain.data;
- last = p + plain.len;
-
- while (p < last && *p++) { /* void */ }
-
- if (p == last) {
- ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
- "client sent invalid login in AUTH PLAIN command");
- return NGX_IMAP_PARSE_INVALID_COMMAND;
- }
-
- s->login.data = p;
-
- while (p < last && *p) { p++; }
-
- if (p == last) {
- ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
- "client sent invalid password in AUTH PLAIN command");
- return NGX_IMAP_PARSE_INVALID_COMMAND;
- }
-
- s->login.len = p++ - s->login.data;
-
- s->passwd.len = last - p;
- s->passwd.data = p;
-
-#if (NGX_DEBUG_IMAP_PASSWD)
- ngx_log_debug2(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
- "pop3 auth plain: \"%V\" \"%V\"",
- &s->login, &s->passwd);
-#endif
-
- return NGX_OK;
-}
-
-
-static void
-ngx_imap_do_auth(ngx_imap_session_t *s)
-{
- s->args.nelts = 0;
- s->buffer->pos = s->buffer->start;
- s->buffer->last = s->buffer->start;
- s->state = 0;
-
- if (s->connection->read->timer_set) {
- ngx_del_timer(s->connection->read);
- }
-
- s->login_attempt++;
-
- ngx_imap_auth_http_init(s);
-}
-
-
-static ngx_int_t
-ngx_imap_read_command(ngx_imap_session_t *s)
-{
- ssize_t n;
- ngx_int_t rc;
- ngx_str_t l;
-
- n = s->connection->recv(s->connection, s->buffer->last,
- s->buffer->end - s->buffer->last);
-
- if (n == NGX_ERROR || n == 0) {
- ngx_imap_close_connection(s->connection);
- return NGX_ERROR;
- }
-
- if (n > 0) {
- s->buffer->last += n;
- }
-
- if (n == NGX_AGAIN) {
- if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) {
- ngx_imap_session_internal_server_error(s);
- return NGX_ERROR;
- }
-
- return NGX_AGAIN;
- }
-
- if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
- rc = ngx_pop3_parse_command(s);
- } else {
- rc = ngx_imap_parse_command(s);
- }
-
- if (rc == NGX_AGAIN) {
-
- if (s->buffer->last < s->buffer->end) {
- return rc;
- }
-
- l.len = s->buffer->last - s->buffer->start;
- l.data = s->buffer->start;
-
- ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
- "client sent too long command \"%V\"", &l);
-
- s->quit = 1;
-
- return NGX_IMAP_PARSE_INVALID_COMMAND;
- }
-
- if (rc == NGX_IMAP_NEXT || rc == NGX_IMAP_PARSE_INVALID_COMMAND) {
- return rc;
- }
-
- if (rc == NGX_ERROR) {
- ngx_imap_close_connection(s->connection);
- return NGX_ERROR;
- }
-
- return NGX_OK;
-}
-
-
-void
-ngx_imap_session_internal_server_error(ngx_imap_session_t *s)
-{
- s->out = internal_server_errors[s->protocol];
- s->quit = 1;
-
- ngx_imap_send(s->connection->write);
-}
-
-
-void
-ngx_imap_close_connection(ngx_connection_t *c)
-{
- ngx_pool_t *pool;
-
- ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
- "close imap connection: %d", c->fd);
-
-#if (NGX_IMAP_SSL)
-
- if (c->ssl) {
- if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
- c->ssl->handler = ngx_imap_close_connection;
- return;
- }
- }
-
-#endif
-
- c->destroyed = 1;
-
- pool = c->pool;
-
- ngx_close_connection(c);
-
- ngx_destroy_pool(pool);
-}
-
-
-static u_char *
-ngx_imap_log_error(ngx_log_t *log, u_char *buf, size_t len)
-{
- u_char *p;
- ngx_imap_session_t *s;
- ngx_imap_log_ctx_t *ctx;
-
- if (log->action) {
- p = ngx_snprintf(buf, len, " while %s", log->action);
- len -= p - buf;
- buf = p;
- }
-
- ctx = log->data;
-
- p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
- len -= p - buf;
- buf = p;
-
- s = ctx->session;
-
- if (s == NULL) {
- return p;
- }
-
- p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
- len -= p - buf;
- buf = p;
-
- if (s->login.len == 0) {
- return p;
- }
-
- p = ngx_snprintf(buf, len, ", login: \"%V\"", &s->login);
- len -= p - buf;
- buf = p;
-
- if (s->proxy == NULL) {
- return p;
- }
-
- p = ngx_snprintf(buf, len, ", upstream: %V", s->proxy->upstream.name);
-
- return p;
-}
diff --git a/src/imap/ngx_imap_parse.c b/src/imap/ngx_imap_parse.c
deleted file mode 100644
index fabba26af..000000000
--- a/src/imap/ngx_imap_parse.c
+++ /dev/null
@@ -1,587 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- */
-
-
-#include <ngx_config.h>
-#include <ngx_core.h>
-#include <ngx_event.h>
-#include <ngx_imap.h>
-
-
-ngx_int_t ngx_imap_parse_command(ngx_imap_session_t *s)
-{
- u_char ch, *p, *c;
- ngx_str_t *arg;
- enum {
- sw_start = 0,
- sw_spaces_before_command,
- sw_command,
- sw_spaces_before_argument,
- sw_argument,
- sw_backslash,
- sw_literal,
- sw_no_sync_literal_argument,
- sw_start_literal_argument,
- sw_literal_argument,
- sw_end_literal_argument,
- sw_almost_done
- } state;
-
- state = s->state;
-
- for (p = s->buffer->pos; p < s->buffer->last; p++) {
- ch = *p;
-
- switch (state) {
-
- /* IMAP tag */
- case sw_start:
- switch (ch) {
- case ' ':
- s->tag.len = p - s->buffer->start + 1;
- s->tag.data = s->buffer->start;
- state = sw_spaces_before_command;
- break;
- case CR:
- s->state = sw_start;
- return NGX_IMAP_PARSE_INVALID_COMMAND;
- case LF:
- s->state = sw_start;
- return NGX_IMAP_PARSE_INVALID_COMMAND;
- }
- break;
-
- case sw_spaces_before_command:
- switch (ch) {
- case ' ':
- break;
- case CR:
- s->state = sw_start;
- return NGX_IMAP_PARSE_INVALID_COMMAND;
- case LF:
- s->state = sw_start;
- return NGX_IMAP_PARSE_INVALID_COMMAND;
- default:
- s->cmd_start = p;
- state = sw_command;
- break;
- }
- break;
-
- case sw_command:
- if (ch == ' ' || ch == CR || ch == LF) {
-
- c = s->cmd_start;
-
- switch (p - c) {
-
- case 4:
- if ((c[0] == 'N' || c[0] == 'n')
- && (c[1] == 'O'|| c[1] == 'o')
- && (c[2] == 'O'|| c[2] == 'o')
- && (c[3] == 'P'|| c[3] == 'p'))
- {
- s->command = NGX_IMAP_NOOP;
-
- } else {
- goto invalid;
- }
- break;
-
- case 5:
- if ((c[0] == 'L'|| c[0] == 'l')
- && (c[1] == 'O'|| c[1] == 'o')
- && (c[2] == 'G'|| c[2] == 'g')
- && (c[3] == 'I'|| c[3] == 'i')
- && (c[4] == 'N'|| c[4] == 'n'))
- {
- s->command = NGX_IMAP_LOGIN;
-
- } else {
- goto invalid;
- }
- break;
-
- case 6:
- if ((c[0] == 'L'|| c[0] == 'l')
- && (c[1] == 'O'|| c[1] == 'o')
- && (c[2] == 'G'|| c[2] == 'g')
- && (c[3] == 'O'|| c[3] == 'o')
- && (c[4] == 'U'|| c[4] == 'u')
- && (c[5] == 'T'|| c[5] == 't'))
- {
- s->command = NGX_IMAP_LOGOUT;
-
- } else {
- goto invalid;
- }
- break;
-
-#if (NGX_IMAP_SSL)
- case 8:
- if ((c[0] == 'S'|| c[0] == 's')
- && (c[1] == 'T'|| c[1] == 't')
- && (c[2] == 'A'|| c[2] == 'a')
- && (c[3] == 'R'|| c[3] == 'r')
- && (c[4] == 'T'|| c[4] == 't')
- && (c[5] == 'T'|| c[5] == 't')
- && (c[6] == 'L'|| c[6] == 'l')
- && (c[7] == 'S'|| c[7] == 's'))
- {
- s->command = NGX_IMAP_STARTTLS;
-
- } else {
- goto invalid;
- }
- break;
-#endif
-
- case 10:
- if ((c[0] == 'C'|| c[0] == 'c')
- && (c[1] == 'A'|| c[1] == 'a')
- && (c[2] == 'P'|| c[2] == 'p')
- && (c[3] == 'A'|| c[3] == 'a')
- && (c[4] == 'B'|| c[4] == 'b')
- && (c[5] == 'I'|| c[5] == 'i')
- && (c[6] == 'L'|| c[6] == 'l')
- && (c[7] == 'I'|| c[7] == 'i')
- && (c[8] == 'T'|| c[8] == 't')
- && (c[9] == 'Y'|| c[9] == 'y'))
- {
- s->command = NGX_IMAP_CAPABILITY;
-
- } else {
- goto invalid;
- }
- break;
-
- default:
- goto invalid;
- }
-
- switch (ch) {
- case ' ':
- state = sw_spaces_before_argument;
- break;
- case CR:
- state = sw_almost_done;
- break;
- case LF:
- goto done;
- }
- break;
- }
-
- if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z')) {
- goto invalid;
- }
-
- break;
-
- case sw_spaces_before_argument:
- switch (ch) {
- case ' ':
- break;
- case CR:
- state = sw_almost_done;
- s->arg_end = p;
- break;
- case LF:
- s->arg_end = p;
- goto done;
- case '"':
- if (s->args.nelts <= 2) {
- s->quoted = 1;
- s->arg_start = p + 1;
- state = sw_argument;
- break;
- }
- goto invalid;
- case '{':
- if (s->args.nelts <= 2) {
- state = sw_literal;
- break;
- }
- goto invalid;
- default:
- if (s->args.nelts <= 2) {
- s->arg_start = p;
- state = sw_argument;
- break;
- }
- goto invalid;
- }
- break;
-
- case sw_argument:
- switch (ch) {
- case '"':
- if (!s->quoted) {
- break;
- }
- s->quoted = 0;
- /* fall through */
- case ' ':
- case CR:
- case LF:
- arg = ngx_array_push(&s->args);
- if (arg == NULL) {
- return NGX_ERROR;
- }
- arg->len = p - s->arg_start;
- arg->data = s->arg_start;
- s->arg_start = NULL;
-
- switch (ch) {
- case '"':
- case ' ':
- state = sw_spaces_before_argument;
- break;
- case CR:
- state = sw_almost_done;
- break;
- case LF:
- goto done;
- }
- break;
- case '\\':
- if (s->quoted) {
- s->backslash = 1;
- state = sw_backslash;
- }
- break;
- }
- break;
-
- case sw_backslash:
- switch (ch) {
- case CR:
- case LF:
- goto invalid;
- default:
- state = sw_argument;
- }
- break;
-
- case sw_literal:
- if (ch >= '0' && ch <= '9') {
- s->literal_len = s->literal_len * 10 + (ch - '0');
- break;
- }
- if (ch == '}') {
- state = sw_start_literal_argument;
- break;
- }
- if (ch == '+') {
- state = sw_no_sync_literal_argument;
- break;
- }
- goto invalid;
-
- case sw_no_sync_literal_argument:
- if (ch == '}') {
- s->no_sync_literal = 1;
- state = sw_start_literal_argument;
- break;
- }
- goto invalid;
-
- case sw_start_literal_argument:
- switch (ch) {
- case CR:
- break;
- case LF:
- s->buffer->pos = p + 1;
- s->arg_start = p + 1;
- if (s->no_sync_literal == 0) {
- s->state = sw_literal_argument;
- return NGX_IMAP_NEXT;
- }
- state = sw_literal_argument;
- s->no_sync_literal = 0;
- break;
- default:
- goto invalid;
- }
- break;
-
- case sw_literal_argument:
- if (s->literal_len && --s->literal_len) {
- break;
- }
-
- arg = ngx_array_push(&s->args);
- if (arg == NULL) {
- return NGX_ERROR;
- }
- arg->len = p + 1 - s->arg_start;
- arg->data = s->arg_start;
- s->arg_start = NULL;
- state = sw_end_literal_argument;
-
- break;
-
- case sw_end_literal_argument:
- switch (ch) {
- case '{':
- if (s->args.nelts <= 2) {
- state = sw_literal;
- break;
- }
- goto invalid;
- case CR:
- state = sw_almost_done;
- break;
- case LF:
- goto done;
- default:
- state = sw_spaces_before_argument;
- break;
- }
- break;
-
- case sw_almost_done:
- switch (ch) {
- case LF:
- goto done;
- default:
- goto invalid;
- }
- }
- }
-
- s->buffer->pos = p;
- s->state = state;
-
- return NGX_AGAIN;
-
-done:
-
- s->buffer->pos = p + 1;
-
- if (s->arg_start) {
- arg = ngx_array_push(&s->args);
- if (arg == NULL) {
- return NGX_ERROR;
- }
- arg->len = s->arg_end - s->arg_start;
- arg->data = s->arg_start;
-
- s->arg_start = NULL;
- s->cmd_start = NULL;
- s->quoted = 0;
- s->no_sync_literal = 0;
- s->literal_len = 0;
- }
-
- s->state = sw_start;
-
- return NGX_OK;
-
-invalid:
-
- s->state = sw_start;
- s->quoted = 0;
- s->no_sync_literal = 0;
- s->literal_len = 0;
-
- return NGX_IMAP_PARSE_INVALID_COMMAND;
-}
-
-
-ngx_int_t ngx_pop3_parse_command(ngx_imap_session_t *s)
-{
- u_char ch, *p, *c, c0, c1, c2, c3;
- ngx_str_t *arg;
- enum {
- sw_start = 0,
- sw_spaces_before_argument,
- sw_argument,
- sw_almost_done
- } state;
-
- state = s->state;
-
- for (p = s->buffer->pos; p < s->buffer->last; p++) {
- ch = *p;
-
- switch (state) {
-
- /* POP3 command */
- case sw_start:
- if (ch == ' ' || ch == CR || ch == LF) {
- c = s->buffer->start;
-
- if (p - c == 4) {
-
- c0 = ngx_toupper(c[0]);
- c1 = ngx_toupper(c[1]);
- c2 = ngx_toupper(c[2]);
- c3 = ngx_toupper(c[3]);
-
- if (c0 == 'U' && c1 == 'S' && c2 == 'E' && c3 == 'R')
- {
- s->command = NGX_POP3_USER;
-
- } else if (c0 == 'P' && c1 == 'A' && c2 == 'S' && c3 == 'S')
- {
- s->command = NGX_POP3_PASS;
-
- } else if (c0 == 'A' && c1 == 'P' && c2 == 'O' && c3 == 'P')
- {
- s->command = NGX_POP3_APOP;
-
- } else if (c0 == 'Q' && c1 == 'U' && c2 == 'I' && c3 == 'T')
- {
- s->command = NGX_POP3_QUIT;
-
- } else if (c0 == 'C' && c1 == 'A' && c2 == 'P' && c3 == 'A')
- {
- s->command = NGX_POP3_CAPA;
-
- } else if (c0 == 'A' && c1 == 'U' && c2 == 'T' && c3 == 'H')
- {
- s->command = NGX_POP3_AUTH;
-
- } else if (c0 == 'N' && c1 == 'O' && c2 == 'O' && c3 == 'P')
- {
- s->command = NGX_POP3_NOOP;
-#if (NGX_IMAP_SSL)
- } else if (c0 == 'S' && c1 == 'T' && c2 == 'L' && c3 == 'S')
- {
- s->command = NGX_POP3_STLS;
-#endif
- } else {
- goto invalid;
- }
-
- } else {
- goto invalid;
- }
-
- switch (ch) {
- case ' ':
- state = sw_spaces_before_argument;
- break;
- case CR:
- state = sw_almost_done;
- break;
- case LF:
- goto done;
- }
- break;
- }
-
- if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z')) {
- goto invalid;
- }
-
- break;
-
- case sw_spaces_before_argument:
- switch (ch) {
- case ' ':
- break;
- case CR:
- state = sw_almost_done;
- s->arg_end = p;
- break;
- case LF:
- s->arg_end = p;
- goto done;
- default:
- if (s->args.nelts <= 2) {
- state = sw_argument;
- s->arg_start = p;
- break;
- }
- goto invalid;
- }
- break;
-
- case sw_argument:
- switch (ch) {
-
- case ' ':
-
- /*
- * the space should be considered as part of the at username
- * or password, but not of argument in other commands
- */
-
- if (s->command == NGX_POP3_USER
- || s->command == NGX_POP3_PASS)
- {
- break;
- }
-
- /* fall through */
-
- case CR:
- case LF:
- arg = ngx_array_push(&s->args);
- if (arg == NULL) {
- return NGX_ERROR;
- }
- arg->len = p - s->arg_start;
- arg->data = s->arg_start;
- s->arg_start = NULL;
-
- switch (ch) {
- case ' ':
- state = sw_spaces_before_argument;
- break;
- case CR:
- state = sw_almost_done;
- break;
- case LF:
- goto done;
- }
- break;
-
- default:
- break;
- }
- break;
-
- case sw_almost_done:
- switch (ch) {
- case LF:
- goto done;
- default:
- goto invalid;
- }
- }
- }
-
- s->buffer->pos = p;
- s->state = state;
-
- return NGX_AGAIN;
-
-done:
-
- s->buffer->pos = p + 1;
-
- if (s->arg_start) {
- arg = ngx_array_push(&s->args);
- if (arg == NULL) {
- return NGX_ERROR;
- }
- arg->len = s->arg_end - s->arg_start;
- arg->data = s->arg_start;
- s->arg_start = NULL;
- }
-
- s->state = (s->command != NGX_POP3_AUTH) ? sw_start : sw_argument;
- return NGX_OK;
-
-invalid:
-
- s->state = sw_start;
- s->arg_start = NULL;
-
- return NGX_IMAP_PARSE_INVALID_COMMAND;
-}
diff --git a/src/imap/ngx_imap_proxy_module.c b/src/imap/ngx_imap_proxy_module.c
deleted file mode 100644
index e3cf72e53..000000000
--- a/src/imap/ngx_imap_proxy_module.c
+++ /dev/null
@@ -1,810 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- */
-
-
-#include <ngx_config.h>
-#include <ngx_core.h>
-#include <ngx_event.h>
-#include <ngx_event_connect.h>
-#include <ngx_imap.h>
-
-
-typedef struct {
- ngx_flag_t enable;
- ngx_flag_t pass_error_message;
- size_t buffer_size;
- ngx_msec_t timeout;
-} ngx_imap_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,
- 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,
- void *child);
-
-
-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_string("proxy_buffer"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_size_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_proxy_conf_t, buffer_size),
- NULL },
-
- { ngx_string("proxy_timeout"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_msec_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_proxy_conf_t, timeout),
- NULL },
-
- { ngx_string("proxy_pass_error_message"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_flag_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_proxy_conf_t, pass_error_message),
- 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 */
-};
-
-
-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 master */
- NULL, /* init module */
- NULL, /* init process */
- NULL, /* init thread */
- NULL, /* exit thread */
- NULL, /* exit process */
- NULL, /* exit master */
- NGX_MODULE_V1_PADDING
-};
-
-
-void
-ngx_imap_proxy_init(ngx_imap_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;
-
- s->connection->log->action = "connecting to upstream";
-
- cscf = ngx_imap_get_module_srv_conf(s, ngx_imap_core_module);
-
- if (cscf->so_keepalive) {
- keepalive = 1;
-
- if (setsockopt(s->connection->fd, SOL_SOCKET, SO_KEEPALIVE,
- (const void *) &keepalive, sizeof(int))
- == -1)
- {
- ngx_log_error(NGX_LOG_ALERT, s->connection->log, ngx_socket_errno,
- "setsockopt(SO_KEEPALIVE) failed");
- }
- }
-
- p = ngx_pcalloc(s->connection->pool, sizeof(ngx_imap_proxy_ctx_t));
- if (p == NULL) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- s->proxy = p;
-
- p->upstream.sockaddr = peer->sockaddr;
- p->upstream.socklen = peer->socklen;
- p->upstream.name = &peer->name;
- p->upstream.get = ngx_event_get_peer;
- p->upstream.log = s->connection->log;
- p->upstream.log_error = NGX_ERROR_ERR;
-
- rc = ngx_event_connect_peer(&p->upstream);
-
- if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) {
- ngx_imap_proxy_internal_server_error(s);
- return;
- }
-
- ngx_add_timer(p->upstream.connection->read, cscf->timeout);
-
- 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;
-
- 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;
- }
-}
-
-
-static void
-ngx_imap_proxy_block_read(ngx_event_t *rev)
-{
- 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);
- }
-}
-
-
-static void
-ngx_imap_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_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
- "imap 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_imap_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);
-
- if (rc == NGX_AGAIN) {
- return;
- }
-
- if (rc == NGX_ERROR) {
- ngx_imap_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";
-
- 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");
-
- 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_imap_proxy_internal_server_error(s);
- return;
- }
-
- 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");
-
- s->connection->log->action = "sending password to upstream";
-
- 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);
- return;
- }
-
- p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len);
- *p++ = CR; *p = LF;
-
- s->imap_state = ngx_imap_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;
-
- pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_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);
-
- 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_imap_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_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_proxy_conf_t *pcf;
-
- 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");
- c->timedout = 1;
- ngx_imap_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);
-
- if (rc == NGX_AGAIN) {
- return;
- }
-
- if (rc == NGX_ERROR) {
- ngx_imap_proxy_upstream_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");
-
- s->connection->log->action = "sending user name to upstream";
-
- 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");
-
- s->connection->log->action = "sending password to upstream";
-
- 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;
-
- 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;
-
- pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_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);
-
- 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_imap_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_imap_proxy_dummy_handler(ngx_event_t *wev)
-{
- ngx_connection_t *c;
- ngx_imap_session_t *s;
-
- ngx_log_debug0(NGX_LOG_DEBUG_IMAP, wev->log, 0, "imap proxy dummy handler");
-
- if (ngx_handle_write_event(wev, 0) == NGX_ERROR) {
- c = wev->data;
- s = c->data;
-
- ngx_imap_proxy_close_session(s);
- }
-}
-
-
-static ngx_int_t
-ngx_imap_proxy_read_response(ngx_imap_session_t *s, ngx_uint_t state)
-{
- u_char *p;
- ssize_t n;
- ngx_buf_t *b;
- ngx_imap_proxy_conf_t *pcf;
-
- s->connection->log->action = "reading response from upstream";
-
- b = s->proxy->buffer;
-
- n = s->proxy->upstream.connection->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_ERROR;
- }
-
- return NGX_AGAIN;
- }
-
- p = b->pos;
-
- if (s->protocol == NGX_IMAP_POP3_PROTOCOL) {
- if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
- return NGX_OK;
- }
-
- } else {
- switch (state) {
-
- case ngx_imap_start:
- if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
- return NGX_OK;
- }
- break;
-
- case ngx_imap_login:
- case ngx_imap_user:
- if (p[0] == '+') {
- return NGX_OK;
- }
- 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;
- }
- }
-
- pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
-
- 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;
-
- return NGX_ERROR;
-}
-
-
-static void
-ngx_imap_proxy_handler(ngx_event_t *ev)
-{
- char *action, *recv_action, *send_action;
- size_t size;
- ssize_t n;
- 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;
-
- c = ev->data;
- s = c->data;
-
- if (ev->timedout) {
- c->log->action = "proxying";
-
- if (c == s->connection) {
- ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
- "client timed out");
- c->timedout = 1;
-
- } else {
- ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
- "upstream timed out");
- }
-
- ngx_imap_proxy_close_session(s);
- return;
- }
-
- if (c == s->connection) {
- if (ev->write) {
- recv_action = "proxying and reading from upstream";
- send_action = "proxying and sending to client";
- src = s->proxy->upstream.connection;
- dst = c;
- b = s->proxy->buffer;
-
- } else {
- recv_action = "proxying and reading from client";
- send_action = "proxying and sending to upstream";
- src = c;
- dst = s->proxy->upstream.connection;
- b = s->buffer;
- }
-
- } else {
- if (ev->write) {
- recv_action = "proxying and reading from client";
- send_action = "proxying and sending to upstream";
- src = s->connection;
- dst = c;
- b = s->buffer;
-
- } else {
- recv_action = "proxying and reading from upstream";
- send_action = "proxying and sending to client";
- src = c;
- dst = s->connection;
- b = s->proxy->buffer;
- }
- }
-
- do_write = ev->write ? 1 : 0;
-
- ngx_log_debug3(NGX_LOG_DEBUG_IMAP, ev->log, 0,
- "imap proxy handler: %d, #%d > #%d",
- do_write, src->fd, dst->fd);
-
- for ( ;; ) {
-
- if (do_write) {
-
- size = b->last - b->pos;
-
- if (size && dst->write->ready) {
- c->log->action = send_action;
-
- n = dst->send(dst, b->pos, size);
-
- if (n == NGX_ERROR) {
- ngx_imap_proxy_close_session(s);
- return;
- }
-
- if (n > 0) {
- b->pos += n;
-
- if (b->pos == b->last) {
- b->pos = b->start;
- b->last = b->start;
- }
- }
- }
- }
-
- size = b->end - b->last;
-
- if (size && src->read->ready) {
- c->log->action = recv_action;
-
- n = src->recv(src, b->last, size);
-
- if (n == NGX_AGAIN || n == 0) {
- break;
- }
-
- if (n > 0) {
- do_write = 1;
- b->last += n;
-
- continue;
- }
-
- if (n == NGX_ERROR) {
- src->read->eof = 1;
- }
- }
-
- break;
- }
-
- c->log->action = "proxying";
-
- if ((s->connection->read->eof || s->proxy->upstream.connection->read->eof)
- && s->buffer->pos == s->buffer->last
- && s->proxy->buffer->pos == s->proxy->buffer->last)
- {
- action = c->log->action;
- c->log->action = NULL;
- ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
- c->log->action = action;
-
- ngx_imap_proxy_close_session(s);
- return;
- }
-
- if (ngx_handle_write_event(dst->write, 0) == NGX_ERROR) {
- ngx_imap_proxy_close_session(s);
- return;
- }
-
- if (ngx_handle_read_event(dst->read, 0) == NGX_ERROR) {
- ngx_imap_proxy_close_session(s);
- return;
- }
-
- if (ngx_handle_write_event(src->write, 0) == NGX_ERROR) {
- ngx_imap_proxy_close_session(s);
- return;
- }
-
- if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) {
- ngx_imap_proxy_close_session(s);
- return;
- }
-
- if (c == s->connection) {
- pcf = ngx_imap_get_module_srv_conf(s, ngx_imap_proxy_module);
- ngx_add_timer(c->read, pcf->timeout);
- }
-}
-
-
-static void
-ngx_imap_proxy_upstream_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);
- }
-
- if (s->out.len == 0) {
- ngx_imap_session_internal_server_error(s);
- return;
- }
-
- s->quit = 1;
- ngx_imap_send(s->connection->write);
-}
-
-
-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);
-}
-
-
-static void
-ngx_imap_proxy_close_session(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_close_connection(s->connection);
-}
-
-
-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;
- pcf->pass_error_message = NGX_CONF_UNSET;
- pcf->buffer_size = NGX_CONF_UNSET_SIZE;
- pcf->timeout = NGX_CONF_UNSET_MSEC;
-
- 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_value(conf->enable, prev->enable, 0);
- ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0);
- 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);
-
- return NGX_CONF_OK;
-}
diff --git a/src/imap/ngx_imap_ssl_module.c b/src/imap/ngx_imap_ssl_module.c
deleted file mode 100644
index ac4a08629..000000000
--- a/src/imap/ngx_imap_ssl_module.c
+++ /dev/null
@@ -1,393 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- */
-
-
-#include <ngx_config.h>
-#include <ngx_core.h>
-#include <ngx_imap.h>
-
-
-#define NGX_DEFLAUT_CERTIFICATE "cert.pem"
-#define NGX_DEFLAUT_CERTIFICATE_KEY "cert.pem"
-#define NGX_DEFLAUT_CIPHERS "ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP"
-
-
-static void *ngx_imap_ssl_create_conf(ngx_conf_t *cf);
-static char *ngx_imap_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child);
-static char *ngx_imap_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd,
- void *conf);
-
-#if !defined (SSL_OP_CIPHER_SERVER_PREFERENCE)
-
-static char *ngx_imap_ssl_nosupported(ngx_conf_t *cf, ngx_command_t *cmd,
- void *conf);
-
-static char ngx_imap_ssl_openssl097[] = "OpenSSL 0.9.7 and higher";
-
-#endif
-
-
-static ngx_conf_enum_t ngx_http_starttls_state[] = {
- { ngx_string("off"), NGX_IMAP_STARTTLS_OFF },
- { ngx_string("on"), NGX_IMAP_STARTTLS_ON },
- { ngx_string("only"), NGX_IMAP_STARTTLS_ONLY },
- { ngx_null_string, 0 }
-};
-
-
-
-static ngx_conf_bitmask_t ngx_imap_ssl_protocols[] = {
- { ngx_string("SSLv2"), NGX_SSL_SSLv2 },
- { ngx_string("SSLv3"), NGX_SSL_SSLv3 },
- { ngx_string("TLSv1"), NGX_SSL_TLSv1 },
- { ngx_null_string, 0 }
-};
-
-
-static ngx_command_t ngx_imap_ssl_commands[] = {
-
- { ngx_string("ssl"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_FLAG,
- ngx_conf_set_flag_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_ssl_conf_t, enable),
- NULL },
-
- { ngx_string("starttls"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_enum_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_ssl_conf_t, starttls),
- ngx_http_starttls_state },
-
- { ngx_string("ssl_certificate"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_str_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_ssl_conf_t, certificate),
- NULL },
-
- { ngx_string("ssl_certificate_key"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_str_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_ssl_conf_t, certificate_key),
- NULL },
-
- { ngx_string("ssl_protocols"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_1MORE,
- ngx_conf_set_bitmask_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_ssl_conf_t, protocols),
- &ngx_imap_ssl_protocols },
-
- { ngx_string("ssl_ciphers"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_str_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_ssl_conf_t, ciphers),
- NULL },
-
- { ngx_string("ssl_prefer_server_ciphers"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_FLAG,
-#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
- ngx_conf_set_flag_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_ssl_conf_t, prefer_server_ciphers),
- NULL },
-#else
- ngx_imap_ssl_nosupported, 0, 0, ngx_imap_ssl_openssl097 },
-#endif
-
- { ngx_string("ssl_session_cache"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE12,
- ngx_imap_ssl_session_cache,
- NGX_IMAP_SRV_CONF_OFFSET,
- 0,
- NULL },
-
- { ngx_string("ssl_session_timeout"),
- NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
- ngx_conf_set_sec_slot,
- NGX_IMAP_SRV_CONF_OFFSET,
- offsetof(ngx_imap_ssl_conf_t, session_timeout),
- NULL },
-
- ngx_null_command
-};
-
-
-static ngx_imap_module_t ngx_imap_ssl_module_ctx = {
- NULL, /* create main configuration */
- NULL, /* init main configuration */
-
- ngx_imap_ssl_create_conf, /* create server configuration */
- ngx_imap_ssl_merge_conf /* merge server configuration */
-};
-
-
-ngx_module_t ngx_imap_ssl_module = {
- NGX_MODULE_V1,
- &ngx_imap_ssl_module_ctx, /* module context */
- ngx_imap_ssl_commands, /* module directives */
- NGX_IMAP_MODULE, /* module type */
- NULL, /* init master */
- NULL, /* init module */
- NULL, /* init process */
- NULL, /* init thread */
- NULL, /* exit thread */
- NULL, /* exit process */
- NULL, /* exit master */
- NGX_MODULE_V1_PADDING
-};
-
-
-static ngx_str_t ngx_imap_ssl_sess_id_ctx = ngx_string("IMAP");
-
-
-static void *
-ngx_imap_ssl_create_conf(ngx_conf_t *cf)
-{
- ngx_imap_ssl_conf_t *scf;
-
- scf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_ssl_conf_t));
- if (scf == NULL) {
- return NGX_CONF_ERROR;
- }
-
- /*
- * set by ngx_pcalloc():
- *
- * scf->protocols = 0;
- * scf->certificate.len = 0;
- * scf->certificate.data = NULL;
- * scf->certificate_key.len = 0;
- * scf->certificate_key.data = NULL;
- * scf->ciphers.len = 0;
- * scf->ciphers.data = NULL;
- * scf->shm_zone = NULL;
- */
-
- scf->enable = NGX_CONF_UNSET;
- scf->starttls = NGX_CONF_UNSET;
- scf->prefer_server_ciphers = NGX_CONF_UNSET;
- scf->builtin_session_cache = NGX_CONF_UNSET;
- scf->session_timeout = NGX_CONF_UNSET;
-
- return scf;
-}
-
-
-static char *
-ngx_imap_ssl_merge_conf(ngx_conf_t *cf, void *parent, void *child)
-{
- ngx_imap_ssl_conf_t *prev = parent;
- ngx_imap_ssl_conf_t *conf = child;
-
- ngx_pool_cleanup_t *cln;
-
- ngx_conf_merge_value(conf->enable, prev->enable, 0);
- ngx_conf_merge_value(conf->starttls, prev->starttls, NGX_IMAP_STARTTLS_OFF);
-
- if (conf->enable == 0 && conf->starttls == NGX_IMAP_STARTTLS_OFF) {
- return NGX_CONF_OK;
- }
-
- ngx_conf_merge_value(conf->session_timeout,
- prev->session_timeout, 300);
-
- ngx_conf_merge_value(conf->prefer_server_ciphers,
- prev->prefer_server_ciphers, 0);
-
- ngx_conf_merge_bitmask_value(conf->protocols, prev->protocols,
- (NGX_CONF_BITMASK_SET
- |NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1));
-
- ngx_conf_merge_str_value(conf->certificate, prev->certificate,
- NGX_DEFLAUT_CERTIFICATE);
-
- ngx_conf_merge_str_value(conf->certificate_key, prev->certificate_key,
- NGX_DEFLAUT_CERTIFICATE_KEY);
-
- ngx_conf_merge_str_value(conf->ciphers, prev->ciphers, NGX_DEFLAUT_CIPHERS);
-
-
- conf->ssl.log = cf->log;
-
- if (ngx_ssl_create(&conf->ssl, conf->protocols, NULL) != NGX_OK) {
- return NGX_CONF_ERROR;
- }
-
- cln = ngx_pool_cleanup_add(cf->pool, 0);
- if (cln == NULL) {
- return NGX_CONF_ERROR;
- }
-
- cln->handler = ngx_ssl_cleanup_ctx;
- cln->data = &conf->ssl;
-
- if (ngx_ssl_certificate(cf, &conf->ssl, &conf->certificate,
- &conf->certificate_key)
- != NGX_OK)
- {
- return NGX_CONF_ERROR;
- }
-
- if (conf->ciphers.len) {
- if (SSL_CTX_set_cipher_list(conf->ssl.ctx,
- (const char *) conf->ciphers.data)
- == 0)
- {
- ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0,
- "SSL_CTX_set_cipher_list(\"%V\") failed",
- &conf->ciphers);
- }
- }
-
-#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
-
- if (conf->prefer_server_ciphers) {
- SSL_CTX_set_options(conf->ssl.ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
- }
-
-#endif
-
- if (ngx_ssl_generate_rsa512_key(&conf->ssl) != NGX_OK) {
- return NGX_CONF_ERROR;
- }
-
- ngx_conf_merge_value(conf->builtin_session_cache,
- prev->builtin_session_cache,
- NGX_SSL_DFLT_BUILTIN_SCACHE);
-
- if (conf->shm_zone == NULL) {
- conf->shm_zone = prev->shm_zone;
- }
-
- if (ngx_ssl_session_cache(&conf->ssl, &ngx_imap_ssl_sess_id_ctx,
- conf->builtin_session_cache,
- conf->shm_zone, conf->session_timeout)
- != NGX_OK)
- {
- return NGX_CONF_ERROR;
- }
-
- return NGX_CONF_OK;
-}
-
-
-static char *
-ngx_imap_ssl_session_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
- ngx_imap_ssl_conf_t *scf = conf;
-
- size_t len;
- ngx_str_t *value, name, size;
- ngx_int_t n;
- ngx_uint_t i, j;
-
- value = cf->args->elts;
-
- for (i = 1; i < cf->args->nelts; i++) {
-
- if (ngx_strcmp(value[i].data, "builtin") == 0) {
- scf->builtin_session_cache = NGX_SSL_DFLT_BUILTIN_SCACHE;
- continue;
- }
-
- if (value[i].len > sizeof("builtin:") - 1
- && ngx_strncmp(value[i].data, "builtin:", sizeof("builtin:") - 1)
- == 0)
- {
- n = ngx_atoi(value[i].data + sizeof("builtin:") - 1,
- value[i].len - (sizeof("builtin:") - 1));
-
- if (n == NGX_ERROR) {
- goto invalid;
- }
-
- scf->builtin_session_cache = n;
-
- continue;
- }
-
- if (value[i].len > sizeof("shared:") - 1
- && ngx_strncmp(value[i].data, "shared:", sizeof("shared:") - 1)
- == 0)
- {
- len = 0;
-
- for (j = sizeof("shared:") - 1; j < value[i].len; j++) {
- if (value[i].data[j] == ':') {
- break;
- }
-
- len++;
- }
-
- if (len == 0) {
- goto invalid;
- }
-
- name.len = len;
- name.data = value[i].data + sizeof("shared:") - 1;
-
- size.len = value[i].len - j - 1;
- size.data = name.data + len + 1;
-
- n = ngx_parse_size(&size);
-
- if (n == NGX_ERROR) {
- goto invalid;
- }
-
- if (n < (ngx_int_t) (8 * ngx_pagesize)) {
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "session cache \"%V\" is too small",
- &value[i]);
-
- return NGX_CONF_ERROR;
- }
-
- scf->shm_zone = ngx_shared_memory_add(cf, &name, n,
- &ngx_imap_ssl_module);
- if (scf->shm_zone == NULL) {
- return NGX_CONF_ERROR;
- }
-
- continue;
- }
-
- goto invalid;
- }
-
- if (scf->shm_zone && scf->builtin_session_cache == NGX_CONF_UNSET) {
- scf->builtin_session_cache = NGX_SSL_NO_BUILTIN_SCACHE;
- }
-
- return NGX_CONF_OK;
-
-invalid:
-
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "invalid session cache \"%V\"", &value[i]);
-
- return NGX_CONF_ERROR;
-}
-
-
-#if !defined (SSL_OP_CIPHER_SERVER_PREFERENCE)
-
-static char *
-ngx_imap_ssl_nosupported(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
-{
- ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
- "\"%V\" directive is available only in %s,",
- &cmd->name, cmd->post);
-
- return NGX_CONF_ERROR;
-}
-
-#endif
diff --git a/src/imap/ngx_imap_ssl_module.h b/src/imap/ngx_imap_ssl_module.h
deleted file mode 100644
index 8ab8af287..000000000
--- a/src/imap/ngx_imap_ssl_module.h
+++ /dev/null
@@ -1,47 +0,0 @@
-
-/*
- * Copyright (C) Igor Sysoev
- */
-
-
-#ifndef _NGX_IMAP_SSL_H_INCLUDED_
-#define _NGX_IMAP_SSL_H_INCLUDED_
-
-
-#include <ngx_config.h>
-#include <ngx_core.h>
-#include <ngx_imap.h>
-
-
-#define NGX_IMAP_STARTTLS_OFF 0
-#define NGX_IMAP_STARTTLS_ON 1
-#define NGX_IMAP_STARTTLS_ONLY 2
-
-
-typedef struct {
- ngx_flag_t enable;
-
- ngx_ssl_t ssl;
-
- ngx_flag_t prefer_server_ciphers;
- ngx_flag_t starttls;
-
- ngx_uint_t protocols;
-
- ssize_t builtin_session_cache;
-
- time_t session_timeout;
-
- ngx_str_t certificate;
- ngx_str_t certificate_key;
-
- ngx_str_t ciphers;
-
- ngx_shm_zone_t *shm_zone;
-} ngx_imap_ssl_conf_t;
-
-
-extern ngx_module_t ngx_imap_ssl_module;
-
-
-#endif /* _NGX_IMAP_SSL_H_INCLUDED_ */