1e18a033bSKonstantin Ananyev
2e18a033bSKonstantin Ananyev/*
3e18a033bSKonstantin Ananyev * Copyright (C) Igor Sysoev
4e18a033bSKonstantin Ananyev * Copyright (C) Nginx, Inc.
5e18a033bSKonstantin Ananyev */
6e18a033bSKonstantin Ananyev
7e18a033bSKonstantin Ananyev
8e18a033bSKonstantin Ananyev#include <ngx_config.h>
9e18a033bSKonstantin Ananyev#include <ngx_core.h>
10e18a033bSKonstantin Ananyev#include <ngx_event.h>
11e18a033bSKonstantin Ananyev#include <ngx_mail.h>
12e18a033bSKonstantin Ananyev#include <ngx_mail_imap_module.h>
13e18a033bSKonstantin Ananyev
14e18a033bSKonstantin Ananyev
15e18a033bSKonstantin Ananyevstatic void *ngx_mail_imap_create_srv_conf(ngx_conf_t *cf);
16e18a033bSKonstantin Ananyevstatic char *ngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent,
17e18a033bSKonstantin Ananyev    void *child);
18e18a033bSKonstantin Ananyev
19e18a033bSKonstantin Ananyev
20e18a033bSKonstantin Ananyevstatic ngx_str_t  ngx_mail_imap_default_capabilities[] = {
21e18a033bSKonstantin Ananyev    ngx_string("IMAP4"),
22e18a033bSKonstantin Ananyev    ngx_string("IMAP4rev1"),
23e18a033bSKonstantin Ananyev    ngx_string("UIDPLUS"),
24e18a033bSKonstantin Ananyev    ngx_null_string
25e18a033bSKonstantin Ananyev};
26e18a033bSKonstantin Ananyev
27e18a033bSKonstantin Ananyev
28e18a033bSKonstantin Ananyevstatic ngx_conf_bitmask_t  ngx_mail_imap_auth_methods[] = {
29e18a033bSKonstantin Ananyev    { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
30e18a033bSKonstantin Ananyev    { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
31e18a033bSKonstantin Ananyev    { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
32e18a033bSKonstantin Ananyev    { ngx_string("external"), NGX_MAIL_AUTH_EXTERNAL_ENABLED },
33e18a033bSKonstantin Ananyev    { ngx_null_string, 0 }
34e18a033bSKonstantin Ananyev};
35e18a033bSKonstantin Ananyev
36e18a033bSKonstantin Ananyev
37e18a033bSKonstantin Ananyevstatic ngx_str_t  ngx_mail_imap_auth_methods_names[] = {
38e18a033bSKonstantin Ananyev    ngx_string("AUTH=PLAIN"),
39e18a033bSKonstantin Ananyev    ngx_string("AUTH=LOGIN"),
40e18a033bSKonstantin Ananyev    ngx_null_string,  /* APOP */
41e18a033bSKonstantin Ananyev    ngx_string("AUTH=CRAM-MD5"),
42e18a033bSKonstantin Ananyev    ngx_string("AUTH=EXTERNAL"),
43e18a033bSKonstantin Ananyev    ngx_null_string   /* NONE */
44e18a033bSKonstantin Ananyev};
45e18a033bSKonstantin Ananyev
46e18a033bSKonstantin Ananyev
47e18a033bSKonstantin Ananyevstatic ngx_mail_protocol_t  ngx_mail_imap_protocol = {
48e18a033bSKonstantin Ananyev    ngx_string("imap"),
49e18a033bSKonstantin Ananyev    { 143, 993, 0, 0 },
50e18a033bSKonstantin Ananyev    NGX_MAIL_IMAP_PROTOCOL,
51e18a033bSKonstantin Ananyev
52e18a033bSKonstantin Ananyev    ngx_mail_imap_init_session,
53e18a033bSKonstantin Ananyev    ngx_mail_imap_init_protocol,
54e18a033bSKonstantin Ananyev    ngx_mail_imap_parse_command,
55e18a033bSKonstantin Ananyev    ngx_mail_imap_auth_state,
56e18a033bSKonstantin Ananyev
57e18a033bSKonstantin Ananyev    ngx_string("* BAD internal server error" CRLF),
58e18a033bSKonstantin Ananyev    ngx_string("* BYE SSL certificate error" CRLF),
59e18a033bSKonstantin Ananyev    ngx_string("* BYE No required SSL certificate" CRLF)
60e18a033bSKonstantin Ananyev};
61e18a033bSKonstantin Ananyev
62e18a033bSKonstantin Ananyev
63e18a033bSKonstantin Ananyevstatic ngx_command_t  ngx_mail_imap_commands[] = {
64e18a033bSKonstantin Ananyev
65e18a033bSKonstantin Ananyev    { ngx_string("imap_client_buffer"),
66e18a033bSKonstantin Ananyev      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
67e18a033bSKonstantin Ananyev      ngx_conf_set_size_slot,
68e18a033bSKonstantin Ananyev      NGX_MAIL_SRV_CONF_OFFSET,
69e18a033bSKonstantin Ananyev      offsetof(ngx_mail_imap_srv_conf_t, client_buffer_size),
70e18a033bSKonstantin Ananyev      NULL },
71e18a033bSKonstantin Ananyev
72e18a033bSKonstantin Ananyev    { ngx_string("imap_capabilities"),
73e18a033bSKonstantin Ananyev      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
74e18a033bSKonstantin Ananyev      ngx_mail_capabilities,
75e18a033bSKonstantin Ananyev      NGX_MAIL_SRV_CONF_OFFSET,
76e18a033bSKonstantin Ananyev      offsetof(ngx_mail_imap_srv_conf_t, capabilities),
77e18a033bSKonstantin Ananyev      NULL },
78e18a033bSKonstantin Ananyev
79e18a033bSKonstantin Ananyev    { ngx_string("imap_auth"),
80e18a033bSKonstantin Ananyev      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
81e18a033bSKonstantin Ananyev      ngx_conf_set_bitmask_slot,
82e18a033bSKonstantin Ananyev      NGX_MAIL_SRV_CONF_OFFSET,
83e18a033bSKonstantin Ananyev      offsetof(ngx_mail_imap_srv_conf_t, auth_methods),
84e18a033bSKonstantin Ananyev      &ngx_mail_imap_auth_methods },
85e18a033bSKonstantin Ananyev
86e18a033bSKonstantin Ananyev      ngx_null_command
87e18a033bSKonstantin Ananyev};
88e18a033bSKonstantin Ananyev
89e18a033bSKonstantin Ananyev
90e18a033bSKonstantin Ananyevstatic ngx_mail_module_t  ngx_mail_imap_module_ctx = {
91e18a033bSKonstantin Ananyev    &ngx_mail_imap_protocol,               /* protocol */
92e18a033bSKonstantin Ananyev
93e18a033bSKonstantin Ananyev    NULL,                                  /* create main configuration */
94e18a033bSKonstantin Ananyev    NULL,                                  /* init main configuration */
95e18a033bSKonstantin Ananyev
96e18a033bSKonstantin Ananyev    ngx_mail_imap_create_srv_conf,         /* create server configuration */
97e18a033bSKonstantin Ananyev    ngx_mail_imap_merge_srv_conf           /* merge server configuration */
98e18a033bSKonstantin Ananyev};
99e18a033bSKonstantin Ananyev
100e18a033bSKonstantin Ananyev
101e18a033bSKonstantin Ananyevngx_module_t  ngx_mail_imap_module = {
102e18a033bSKonstantin Ananyev    NGX_MODULE_V1,
103e18a033bSKonstantin Ananyev    &ngx_mail_imap_module_ctx,             /* module context */
104e18a033bSKonstantin Ananyev    ngx_mail_imap_commands,                /* module directives */
105e18a033bSKonstantin Ananyev    NGX_MAIL_MODULE,                       /* module type */
106e18a033bSKonstantin Ananyev    NULL,                                  /* init master */
107e18a033bSKonstantin Ananyev    NULL,                                  /* init module */
108e18a033bSKonstantin Ananyev    NULL,                                  /* init process */
109e18a033bSKonstantin Ananyev    NULL,                                  /* init thread */
110e18a033bSKonstantin Ananyev    NULL,                                  /* exit thread */
111e18a033bSKonstantin Ananyev    NULL,                                  /* exit process */
112e18a033bSKonstantin Ananyev    NULL,                                  /* exit master */
113e18a033bSKonstantin Ananyev    NGX_MODULE_V1_PADDING
114e18a033bSKonstantin Ananyev};
115e18a033bSKonstantin Ananyev
116e18a033bSKonstantin Ananyev
117e18a033bSKonstantin Ananyevstatic void *
118e18a033bSKonstantin Ananyevngx_mail_imap_create_srv_conf(ngx_conf_t *cf)
119e18a033bSKonstantin Ananyev{
120e18a033bSKonstantin Ananyev    ngx_mail_imap_srv_conf_t  *iscf;
121e18a033bSKonstantin Ananyev
122e18a033bSKonstantin Ananyev    iscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_imap_srv_conf_t));
123e18a033bSKonstantin Ananyev    if (iscf == NULL) {
124e18a033bSKonstantin Ananyev        return NULL;
125e18a033bSKonstantin Ananyev    }
126e18a033bSKonstantin Ananyev
127e18a033bSKonstantin Ananyev    iscf->client_buffer_size = NGX_CONF_UNSET_SIZE;
128e18a033bSKonstantin Ananyev
129e18a033bSKonstantin Ananyev    if (ngx_array_init(&iscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
130e18a033bSKonstantin Ananyev        != NGX_OK)
131e18a033bSKonstantin Ananyev    {
132e18a033bSKonstantin Ananyev        return NULL;
133e18a033bSKonstantin Ananyev    }
134e18a033bSKonstantin Ananyev
135e18a033bSKonstantin Ananyev    return iscf;
136e18a033bSKonstantin Ananyev}
137e18a033bSKonstantin Ananyev
138e18a033bSKonstantin Ananyev
139e18a033bSKonstantin Ananyevstatic char *
140e18a033bSKonstantin Ananyevngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
141e18a033bSKonstantin Ananyev{
142e18a033bSKonstantin Ananyev    ngx_mail_imap_srv_conf_t *prev = parent;
143e18a033bSKonstantin Ananyev    ngx_mail_imap_srv_conf_t *conf = child;
144e18a033bSKonstantin Ananyev
145e18a033bSKonstantin Ananyev    u_char      *p, *auth;
146e18a033bSKonstantin Ananyev    size_t       size;
147e18a033bSKonstantin Ananyev    ngx_str_t   *c, *d;
148e18a033bSKonstantin Ananyev    ngx_uint_t   i, m;
149e18a033bSKonstantin Ananyev
150e18a033bSKonstantin Ananyev    ngx_conf_merge_size_value(conf->client_buffer_size,
151e18a033bSKonstantin Ananyev                              prev->client_buffer_size,
152e18a033bSKonstantin Ananyev                              (size_t) ngx_pagesize);
153e18a033bSKonstantin Ananyev
154e18a033bSKonstantin Ananyev    ngx_conf_merge_bitmask_value(conf->auth_methods,
155e18a033bSKonstantin Ananyev                              prev->auth_methods,
156e18a033bSKonstantin Ananyev                              (NGX_CONF_BITMASK_SET
157e18a033bSKonstantin Ananyev                               |NGX_MAIL_AUTH_PLAIN_ENABLED));
158e18a033bSKonstantin Ananyev
159e18a033bSKonstantin Ananyev
160e18a033bSKonstantin Ananyev    if (conf->capabilities.nelts == 0) {
161e18a033bSKonstantin Ananyev        conf->capabilities = prev->capabilities;
162e18a033bSKonstantin Ananyev    }
163e18a033bSKonstantin Ananyev
164e18a033bSKonstantin Ananyev    if (conf->capabilities.nelts == 0) {
165e18a033bSKonstantin Ananyev
166e18a033bSKonstantin Ananyev        for (d = ngx_mail_imap_default_capabilities; d->len; d++) {
167e18a033bSKonstantin Ananyev            c = ngx_array_push(&conf->capabilities);
168e18a033bSKonstantin Ananyev            if (c == NULL) {
169e18a033bSKonstantin Ananyev                return NGX_CONF_ERROR;
170e18a033bSKonstantin Ananyev            }
171e18a033bSKonstantin Ananyev
172e18a033bSKonstantin Ananyev            *c = *d;
173e18a033bSKonstantin Ananyev        }
174e18a033bSKonstantin Ananyev    }
175e18a033bSKonstantin Ananyev
176e18a033bSKonstantin Ananyev    size = sizeof("* CAPABILITY" CRLF) - 1;
177e18a033bSKonstantin Ananyev
178e18a033bSKonstantin Ananyev    c = conf->capabilities.elts;
179e18a033bSKonstantin Ananyev    for (i = 0; i < conf->capabilities.nelts; i++) {
180e18a033bSKonstantin Ananyev        size += 1 + c[i].len;
181e18a033bSKonstantin Ananyev    }
182e18a033bSKonstantin Ananyev
183e18a033bSKonstantin Ananyev    for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
184e18a033bSKonstantin Ananyev         m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
185e18a033bSKonstantin Ananyev         m <<= 1, i++)
186e18a033bSKonstantin Ananyev    {
187e18a033bSKonstantin Ananyev        if (m & conf->auth_methods) {
188e18a033bSKonstantin Ananyev            size += 1 + ngx_mail_imap_auth_methods_names[i].len;
189e18a033bSKonstantin Ananyev        }
190e18a033bSKonstantin Ananyev    }
191e18a033bSKonstantin Ananyev
192e18a033bSKonstantin Ananyev    p = ngx_pnalloc(cf->pool, size);
193e18a033bSKonstantin Ananyev    if (p == NULL) {
194e18a033bSKonstantin Ananyev        return NGX_CONF_ERROR;
195e18a033bSKonstantin Ananyev    }
196e18a033bSKonstantin Ananyev
197e18a033bSKonstantin Ananyev    conf->capability.len = size;
198e18a033bSKonstantin Ananyev    conf->capability.data = p;
199e18a033bSKonstantin Ananyev
200e18a033bSKonstantin Ananyev    p = ngx_cpymem(p, "* CAPABILITY", sizeof("* CAPABILITY") - 1);
201e18a033bSKonstantin Ananyev
202e18a033bSKonstantin Ananyev    for (i = 0; i < conf->capabilities.nelts; i++) {
203e18a033bSKonstantin Ananyev        *p++ = ' ';
204e18a033bSKonstantin Ananyev        p = ngx_cpymem(p, c[i].data, c[i].len);
205e18a033bSKonstantin Ananyev    }
206e18a033bSKonstantin Ananyev
207e18a033bSKonstantin Ananyev    auth = p;
208e18a033bSKonstantin Ananyev
209e18a033bSKonstantin Ananyev    for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
210e18a033bSKonstantin Ananyev         m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
211e18a033bSKonstantin Ananyev         m <<= 1, i++)
212e18a033bSKonstantin Ananyev    {
213e18a033bSKonstantin Ananyev        if (m & conf->auth_methods) {
214e18a033bSKonstantin Ananyev            *p++ = ' ';
215e18a033bSKonstantin Ananyev            p = ngx_cpymem(p, ngx_mail_imap_auth_methods_names[i].data,
216e18a033bSKonstantin Ananyev                           ngx_mail_imap_auth_methods_names[i].len);
217e18a033bSKonstantin Ananyev        }
218e18a033bSKonstantin Ananyev    }
219e18a033bSKonstantin Ananyev
220e18a033bSKonstantin Ananyev    *p++ = CR; *p = LF;
221e18a033bSKonstantin Ananyev
222e18a033bSKonstantin Ananyev
223e18a033bSKonstantin Ananyev    size += sizeof(" STARTTLS") - 1;
224e18a033bSKonstantin Ananyev
225e18a033bSKonstantin Ananyev    p = ngx_pnalloc(cf->pool, size);
226e18a033bSKonstantin Ananyev    if (p == NULL) {
227e18a033bSKonstantin Ananyev        return NGX_CONF_ERROR;
228e18a033bSKonstantin Ananyev    }
229e18a033bSKonstantin Ananyev
230e18a033bSKonstantin Ananyev    conf->starttls_capability.len = size;
231e18a033bSKonstantin Ananyev    conf->starttls_capability.data = p;
232e18a033bSKonstantin Ananyev
233e18a033bSKonstantin Ananyev    p = ngx_cpymem(p, conf->capability.data,
234e18a033bSKonstantin Ananyev                   conf->capability.len - (sizeof(CRLF) - 1));
235e18a033bSKonstantin Ananyev    p = ngx_cpymem(p, " STARTTLS", sizeof(" STARTTLS") - 1);
236e18a033bSKonstantin Ananyev    *p++ = CR; *p = LF;
237e18a033bSKonstantin Ananyev
238e18a033bSKonstantin Ananyev
239e18a033bSKonstantin Ananyev    size = (auth - conf->capability.data) + sizeof(CRLF) - 1
240e18a033bSKonstantin Ananyev            + sizeof(" STARTTLS LOGINDISABLED") - 1;
241e18a033bSKonstantin Ananyev
242e18a033bSKonstantin Ananyev    p = ngx_pnalloc(cf->pool, size);
243e18a033bSKonstantin Ananyev    if (p == NULL) {
244e18a033bSKonstantin Ananyev        return NGX_CONF_ERROR;
245e18a033bSKonstantin Ananyev    }
246e18a033bSKonstantin Ananyev
247e18a033bSKonstantin Ananyev    conf->starttls_only_capability.len = size;
248e18a033bSKonstantin Ananyev    conf->starttls_only_capability.data = p;
249e18a033bSKonstantin Ananyev
250e18a033bSKonstantin Ananyev    p = ngx_cpymem(p, conf->capability.data,
251e18a033bSKonstantin Ananyev                   auth - conf->capability.data);
252e18a033bSKonstantin Ananyev    p = ngx_cpymem(p, " STARTTLS LOGINDISABLED",
253e18a033bSKonstantin Ananyev                   sizeof(" STARTTLS LOGINDISABLED") - 1);
254e18a033bSKonstantin Ananyev    *p++ = CR; *p = LF;
255e18a033bSKonstantin Ananyev
256e18a033bSKonstantin Ananyev    return NGX_CONF_OK;
257e18a033bSKonstantin Ananyev}
258