Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / mail / ngx_mail_imap_module.c
blobdc80b4fb4c13d1220b03d0548fa109cc7e258bf0
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
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>
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,
17 void *child);
20 static ngx_str_t ngx_mail_imap_default_capabilities[] = {
21 ngx_string("IMAP4"),
22 ngx_string("IMAP4rev1"),
23 ngx_string("UIDPLUS"),
24 ngx_null_string
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 = {
46 ngx_string("imap"),
47 { 143, 993, 0, 0 },
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),
66 NULL },
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),
73 NULL },
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 },
82 ngx_null_command
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 = {
98 NGX_MODULE_V1,
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
113 static void *
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));
119 if (iscf == NULL) {
120 return NULL;
123 iscf->client_buffer_size = NGX_CONF_UNSET_SIZE;
125 if (ngx_array_init(&iscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
126 != NGX_OK)
128 return NULL;
131 return iscf;
135 static char *
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;
141 u_char *p, *auth;
142 size_t size;
143 ngx_str_t *c, *d;
144 ngx_uint_t i, m;
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,
151 prev->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);
164 if (c == NULL) {
165 return NGX_CONF_ERROR;
168 *c = *d;
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;
181 m <<= 1, i++)
183 if (m & conf->auth_methods) {
184 size += 1 + ngx_mail_imap_auth_methods_names[i].len;
188 p = ngx_pnalloc(cf->pool, size);
189 if (p == NULL) {
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++) {
199 *p++ = ' ';
200 p = ngx_cpymem(p, c[i].data, c[i].len);
203 auth = p;
205 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
206 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
207 m <<= 1, i++)
209 if (m & conf->auth_methods) {
210 *p++ = ' ';
211 p = ngx_cpymem(p, ngx_mail_imap_auth_methods_names[i].data,
212 ngx_mail_imap_auth_methods_names[i].len);
216 *p++ = CR; *p = LF;
219 size += sizeof(" STARTTLS") - 1;
221 p = ngx_pnalloc(cf->pool, size);
222 if (p == NULL) {
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);
232 *p++ = CR; *p = LF;
235 size = (auth - conf->capability.data) + sizeof(CRLF) - 1
236 + sizeof(" STARTTLS LOGINDISABLED") - 1;
238 p = ngx_pnalloc(cf->pool, size);
239 if (p == NULL) {
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);
250 *p++ = CR; *p = LF;
252 return NGX_CONF_OK;