3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
8 #include <ngx_config.h>
10 #include <ngx_event.h>
12 #include <ngx_mail_imap_module.h>
15 static void *ngx_mail_imap_create_srv_conf(ngx_conf_t
*cf
);
16 static char *ngx_mail_imap_merge_srv_conf(ngx_conf_t
*cf
, void *parent
,
20 static ngx_str_t ngx_mail_imap_default_capabilities
[] = {
22 ngx_string("IMAP4rev1"),
23 ngx_string("UIDPLUS"),
28 static 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_null_string
, 0 }
36 static ngx_str_t ngx_mail_imap_auth_methods_names
[] = {
37 ngx_string("AUTH=PLAIN"),
38 ngx_string("AUTH=LOGIN"),
39 ngx_null_string
, /* APOP */
40 ngx_string("AUTH=CRAM-MD5"),
41 ngx_null_string
/* NONE */
45 static ngx_mail_protocol_t ngx_mail_imap_protocol
= {
48 NGX_MAIL_IMAP_PROTOCOL
,
50 ngx_mail_imap_init_session
,
51 ngx_mail_imap_init_protocol
,
52 ngx_mail_imap_parse_command
,
53 ngx_mail_imap_auth_state
,
55 ngx_string("* BAD internal server error" CRLF
)
59 static ngx_command_t ngx_mail_imap_commands
[] = {
61 { ngx_string("imap_client_buffer"),
62 NGX_MAIL_MAIN_CONF
|NGX_MAIL_SRV_CONF
|NGX_CONF_TAKE1
,
63 ngx_conf_set_size_slot
,
64 NGX_MAIL_SRV_CONF_OFFSET
,
65 offsetof(ngx_mail_imap_srv_conf_t
, client_buffer_size
),
68 { ngx_string("imap_capabilities"),
69 NGX_MAIL_MAIN_CONF
|NGX_MAIL_SRV_CONF
|NGX_CONF_1MORE
,
70 ngx_mail_capabilities
,
71 NGX_MAIL_SRV_CONF_OFFSET
,
72 offsetof(ngx_mail_imap_srv_conf_t
, capabilities
),
75 { ngx_string("imap_auth"),
76 NGX_MAIL_MAIN_CONF
|NGX_MAIL_SRV_CONF
|NGX_CONF_1MORE
,
77 ngx_conf_set_bitmask_slot
,
78 NGX_MAIL_SRV_CONF_OFFSET
,
79 offsetof(ngx_mail_imap_srv_conf_t
, auth_methods
),
80 &ngx_mail_imap_auth_methods
},
86 static ngx_mail_module_t ngx_mail_imap_module_ctx
= {
87 &ngx_mail_imap_protocol
, /* protocol */
89 NULL
, /* create main configuration */
90 NULL
, /* init main configuration */
92 ngx_mail_imap_create_srv_conf
, /* create server configuration */
93 ngx_mail_imap_merge_srv_conf
/* merge server configuration */
97 ngx_module_t ngx_mail_imap_module
= {
99 &ngx_mail_imap_module_ctx
, /* module context */
100 ngx_mail_imap_commands
, /* module directives */
101 NGX_MAIL_MODULE
, /* module type */
102 NULL
, /* init master */
103 NULL
, /* init module */
104 NULL
, /* init process */
105 NULL
, /* init thread */
106 NULL
, /* exit thread */
107 NULL
, /* exit process */
108 NULL
, /* exit master */
109 NGX_MODULE_V1_PADDING
114 ngx_mail_imap_create_srv_conf(ngx_conf_t
*cf
)
116 ngx_mail_imap_srv_conf_t
*iscf
;
118 iscf
= ngx_pcalloc(cf
->pool
, sizeof(ngx_mail_imap_srv_conf_t
));
123 iscf
->client_buffer_size
= NGX_CONF_UNSET_SIZE
;
125 if (ngx_array_init(&iscf
->capabilities
, cf
->pool
, 4, sizeof(ngx_str_t
))
136 ngx_mail_imap_merge_srv_conf(ngx_conf_t
*cf
, void *parent
, void *child
)
138 ngx_mail_imap_srv_conf_t
*prev
= parent
;
139 ngx_mail_imap_srv_conf_t
*conf
= child
;
146 ngx_conf_merge_size_value(conf
->client_buffer_size
,
147 prev
->client_buffer_size
,
148 (size_t) ngx_pagesize
);
150 ngx_conf_merge_bitmask_value(conf
->auth_methods
,
152 (NGX_CONF_BITMASK_SET
153 |NGX_MAIL_AUTH_PLAIN_ENABLED
));
156 if (conf
->capabilities
.nelts
== 0) {
157 conf
->capabilities
= prev
->capabilities
;
160 if (conf
->capabilities
.nelts
== 0) {
162 for (d
= ngx_mail_imap_default_capabilities
; d
->len
; d
++) {
163 c
= ngx_array_push(&conf
->capabilities
);
165 return NGX_CONF_ERROR
;
172 size
= sizeof("* CAPABILITY" CRLF
) - 1;
174 c
= conf
->capabilities
.elts
;
175 for (i
= 0; i
< conf
->capabilities
.nelts
; i
++) {
176 size
+= 1 + c
[i
].len
;
179 for (m
= NGX_MAIL_AUTH_PLAIN_ENABLED
, i
= 0;
180 m
<= NGX_MAIL_AUTH_CRAM_MD5_ENABLED
;
183 if (m
& conf
->auth_methods
) {
184 size
+= 1 + ngx_mail_imap_auth_methods_names
[i
].len
;
188 p
= ngx_pnalloc(cf
->pool
, size
);
190 return NGX_CONF_ERROR
;
193 conf
->capability
.len
= size
;
194 conf
->capability
.data
= p
;
196 p
= ngx_cpymem(p
, "* CAPABILITY", sizeof("* CAPABILITY") - 1);
198 for (i
= 0; i
< conf
->capabilities
.nelts
; i
++) {
200 p
= ngx_cpymem(p
, c
[i
].data
, c
[i
].len
);
205 for (m
= NGX_MAIL_AUTH_PLAIN_ENABLED
, i
= 0;
206 m
<= NGX_MAIL_AUTH_CRAM_MD5_ENABLED
;
209 if (m
& conf
->auth_methods
) {
211 p
= ngx_cpymem(p
, ngx_mail_imap_auth_methods_names
[i
].data
,
212 ngx_mail_imap_auth_methods_names
[i
].len
);
219 size
+= sizeof(" STARTTLS") - 1;
221 p
= ngx_pnalloc(cf
->pool
, size
);
223 return NGX_CONF_ERROR
;
226 conf
->starttls_capability
.len
= size
;
227 conf
->starttls_capability
.data
= p
;
229 p
= ngx_cpymem(p
, conf
->capability
.data
,
230 conf
->capability
.len
- (sizeof(CRLF
) - 1));
231 p
= ngx_cpymem(p
, " STARTTLS", sizeof(" STARTTLS") - 1);
235 size
= (auth
- conf
->capability
.data
) + sizeof(CRLF
) - 1
236 + sizeof(" STARTTLS LOGINDISABLED") - 1;
238 p
= ngx_pnalloc(cf
->pool
, size
);
240 return NGX_CONF_ERROR
;
243 conf
->starttls_only_capability
.len
= size
;
244 conf
->starttls_only_capability
.data
= p
;
246 p
= ngx_cpymem(p
, conf
->capability
.data
,
247 auth
- conf
->capability
.data
);
248 p
= ngx_cpymem(p
, " STARTTLS LOGINDISABLED",
249 sizeof(" STARTTLS LOGINDISABLED") - 1);