3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
11 #include <ngx_mail_imap_module.h>
14 static void *ngx_mail_imap_create_srv_conf(ngx_conf_t *cf);
15 static char *ngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent,
19 static ngx_str_t ngx_mail_imap_default_capabilities[] = {
21 ngx_string("IMAP4rev1"),
22 ngx_string("UIDPLUS"),
27 static ngx_conf_bitmask_t ngx_mail_imap_auth_methods[] = {
28 { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
29 { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
30 { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
31 { ngx_null_string, 0 }
35 static ngx_str_t ngx_mail_imap_auth_methods_names[] = {
36 ngx_string("AUTH=PLAIN"),
37 ngx_string("AUTH=LOGIN"),
38 ngx_null_string, /* APOP */
39 ngx_string("AUTH=CRAM-MD5"),
40 ngx_null_string /* NONE */
44 static ngx_mail_protocol_t ngx_mail_imap_protocol = {
47 NGX_MAIL_IMAP_PROTOCOL,
49 ngx_mail_imap_init_session,
50 ngx_mail_imap_init_protocol,
51 ngx_mail_imap_parse_command,
52 ngx_mail_imap_auth_state,
54 ngx_string("* BAD internal server error" CRLF)
58 static ngx_command_t ngx_mail_imap_commands[] = {
60 { ngx_string("imap_client_buffer"),
61 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
62 ngx_conf_set_size_slot,
63 NGX_MAIL_SRV_CONF_OFFSET,
64 offsetof(ngx_mail_imap_srv_conf_t, client_buffer_size),
67 { ngx_string("imap_capabilities"),
68 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
69 ngx_mail_capabilities,
70 NGX_MAIL_SRV_CONF_OFFSET,
71 offsetof(ngx_mail_imap_srv_conf_t, capabilities),
74 { ngx_string("imap_auth"),
75 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
76 ngx_conf_set_bitmask_slot,
77 NGX_MAIL_SRV_CONF_OFFSET,
78 offsetof(ngx_mail_imap_srv_conf_t, auth_methods),
79 &ngx_mail_imap_auth_methods },
85 static ngx_mail_module_t ngx_mail_imap_module_ctx = {
86 &ngx_mail_imap_protocol, /* protocol */
88 NULL, /* create main configuration */
89 NULL, /* init main configuration */
91 ngx_mail_imap_create_srv_conf, /* create server configuration */
92 ngx_mail_imap_merge_srv_conf /* merge server configuration */
96 ngx_module_t ngx_mail_imap_module = {
98 &ngx_mail_imap_module_ctx, /* module context */
99 ngx_mail_imap_commands, /* module directives */
100 NGX_MAIL_MODULE, /* module type */
101 NULL, /* init master */
102 NULL, /* init module */
103 NULL, /* init process */
104 NULL, /* init thread */
105 NULL, /* exit thread */
106 NULL, /* exit process */
107 NULL, /* exit master */
108 NGX_MODULE_V1_PADDING
113 ngx_mail_imap_create_srv_conf(ngx_conf_t *cf)
115 ngx_mail_imap_srv_conf_t *iscf;
117 iscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_imap_srv_conf_t));
122 iscf->client_buffer_size = NGX_CONF_UNSET_SIZE;
124 if (ngx_array_init(&iscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
135 ngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
137 ngx_mail_imap_srv_conf_t *prev = parent;
138 ngx_mail_imap_srv_conf_t *conf = child;
145 ngx_conf_merge_size_value(conf->client_buffer_size,
146 prev->client_buffer_size,
147 (size_t) ngx_pagesize);
149 ngx_conf_merge_bitmask_value(conf->auth_methods,
151 (NGX_CONF_BITMASK_SET
152 |NGX_MAIL_AUTH_PLAIN_ENABLED));
155 if (conf->capabilities.nelts == 0) {
156 conf->capabilities = prev->capabilities;
159 if (conf->capabilities.nelts == 0) {
161 for (d = ngx_mail_imap_default_capabilities; d->len; d++) {
162 c = ngx_array_push(&conf->capabilities);
164 return NGX_CONF_ERROR;
171 size = sizeof("* CAPABILITY" CRLF) - 1;
173 c = conf->capabilities.elts;
174 for (i = 0; i < conf->capabilities.nelts; i++) {
175 size += 1 + c[i].len;
178 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
179 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
182 if (m & conf->auth_methods) {
183 size += 1 + ngx_mail_imap_auth_methods_names[i].len;
187 p = ngx_pnalloc(cf->pool, size);
189 return NGX_CONF_ERROR;
192 conf->capability.len = size;
193 conf->capability.data = p;
195 p = ngx_cpymem(p, "* CAPABILITY", sizeof("* CAPABILITY") - 1);
197 for (i = 0; i < conf->capabilities.nelts; i++) {
199 p = ngx_cpymem(p, c[i].data, c[i].len);
204 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
205 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
208 if (m & conf->auth_methods) {
210 p = ngx_cpymem(p, ngx_mail_imap_auth_methods_names[i].data,
211 ngx_mail_imap_auth_methods_names[i].len);
218 size += sizeof(" STARTTLS") - 1;
220 p = ngx_pnalloc(cf->pool, size);
222 return NGX_CONF_ERROR;
225 conf->starttls_capability.len = size;
226 conf->starttls_capability.data = p;
228 p = ngx_cpymem(p, conf->capability.data,
229 conf->capability.len - (sizeof(CRLF) - 1));
230 p = ngx_cpymem(p, " STARTTLS", sizeof(" STARTTLS") - 1);
234 size = (auth - conf->capability.data) + sizeof(CRLF) - 1
235 + sizeof(" STARTTLS LOGINDISABLED") - 1;
237 p = ngx_pnalloc(cf->pool, size);
239 return NGX_CONF_ERROR;
242 conf->starttls_only_capability.len = size;
243 conf->starttls_only_capability.data = p;
245 p = ngx_cpymem(p, conf->capability.data,
246 auth - conf->capability.data);
247 p = ngx_cpymem(p, " STARTTLS LOGINDISABLED",
248 sizeof(" STARTTLS LOGINDISABLED") - 1);