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