comparison src/mail/ngx_mail_imap_module.c @ 409:52b28d322d76

Merge with nginx 0.6.12.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 11 Aug 2008 21:22:47 +0400
parents 1c519aff5c0c
children cd9cb7a3ff9e
comparison
equal deleted inserted replaced
408:4243eecbd594 409:52b28d322d76
1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
10 #include <ngx_mail.h>
11 #include <ngx_mail_imap_module.h>
12
13
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,
16 void *child);
17
18
19 static ngx_str_t ngx_mail_imap_default_capabilities[] = {
20 ngx_string("IMAP4"),
21 ngx_string("IMAP4rev1"),
22 ngx_string("UIDPLUS"),
23 ngx_null_string
24 };
25
26
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 }
32 };
33
34
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 */
41 };
42
43
44 static ngx_mail_protocol_t ngx_mail_imap_protocol = {
45 ngx_string("imap"),
46 { 143, 993, 0, 0 },
47 NGX_MAIL_IMAP_PROTOCOL,
48
49 ngx_mail_imap_init_session,
50 ngx_mail_imap_init_protocol,
51 ngx_mail_imap_parse_command,
52 ngx_mail_imap_auth_state,
53
54 ngx_string("* BAD internal server error" CRLF)
55 };
56
57
58 static ngx_command_t ngx_mail_imap_commands[] = {
59
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),
65 NULL },
66
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),
72 NULL },
73
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 },
80
81 ngx_null_command
82 };
83
84
85 static ngx_mail_module_t ngx_mail_imap_module_ctx = {
86 &ngx_mail_imap_protocol, /* protocol */
87
88 NULL, /* create main configuration */
89 NULL, /* init main configuration */
90
91 ngx_mail_imap_create_srv_conf, /* create server configuration */
92 ngx_mail_imap_merge_srv_conf /* merge server configuration */
93 };
94
95
96 ngx_module_t ngx_mail_imap_module = {
97 NGX_MODULE_V1,
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
109 };
110
111
112 static void *
113 ngx_mail_imap_create_srv_conf(ngx_conf_t *cf)
114 {
115 ngx_mail_imap_srv_conf_t *iscf;
116
117 iscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_imap_srv_conf_t));
118 if (iscf == NULL) {
119 return NULL;
120 }
121
122 iscf->client_buffer_size = NGX_CONF_UNSET_SIZE;
123
124 if (ngx_array_init(&iscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
125 != NGX_OK)
126 {
127 return NULL;
128 }
129
130 return iscf;
131 }
132
133
134 static char *
135 ngx_mail_imap_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
136 {
137 ngx_mail_imap_srv_conf_t *prev = parent;
138 ngx_mail_imap_srv_conf_t *conf = child;
139
140 u_char *p, *auth;
141 size_t size;
142 ngx_str_t *c, *d;
143 ngx_uint_t i, m;
144
145 ngx_conf_merge_size_value(conf->client_buffer_size,
146 prev->client_buffer_size,
147 (size_t) ngx_pagesize);
148
149 ngx_conf_merge_bitmask_value(conf->auth_methods,
150 prev->auth_methods,
151 (NGX_CONF_BITMASK_SET
152 |NGX_MAIL_AUTH_PLAIN_ENABLED));
153
154
155 if (conf->capabilities.nelts == 0) {
156 conf->capabilities = prev->capabilities;
157 }
158
159 if (conf->capabilities.nelts == 0) {
160
161 for (d = ngx_mail_imap_default_capabilities; d->len; d++) {
162 c = ngx_array_push(&conf->capabilities);
163 if (c == NULL) {
164 return NGX_CONF_ERROR;
165 }
166
167 *c = *d;
168 }
169 }
170
171 size = sizeof("* CAPABILITY" CRLF) - 1;
172
173 c = conf->capabilities.elts;
174 for (i = 0; i < conf->capabilities.nelts; i++) {
175 size += 1 + c[i].len;
176 }
177
178 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
179 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
180 m <<= 1, i++)
181 {
182 if (m & conf->auth_methods) {
183 size += 1 + ngx_mail_imap_auth_methods_names[i].len;
184 }
185 }
186
187 p = ngx_palloc(cf->pool, size);
188 if (p == NULL) {
189 return NGX_CONF_ERROR;
190 }
191
192 conf->capability.len = size;
193 conf->capability.data = p;
194
195 p = ngx_cpymem(p, "* CAPABILITY", sizeof("* CAPABILITY") - 1);
196
197 for (i = 0; i < conf->capabilities.nelts; i++) {
198 *p++ = ' ';
199 p = ngx_cpymem(p, c[i].data, c[i].len);
200 }
201
202 auth = p;
203
204 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
205 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
206 m <<= 1, i++)
207 {
208 if (m & conf->auth_methods) {
209 *p++ = ' ';
210 p = ngx_cpymem(p, ngx_mail_imap_auth_methods_names[i].data,
211 ngx_mail_imap_auth_methods_names[i].len);
212 }
213 }
214
215 *p++ = CR; *p = LF;
216
217
218 size += sizeof(" STARTTLS") - 1;
219
220 p = ngx_palloc(cf->pool, size);
221 if (p == NULL) {
222 return NGX_CONF_ERROR;
223 }
224
225 conf->starttls_capability.len = size;
226 conf->starttls_capability.data = p;
227
228 p = ngx_cpymem(p, conf->capability.data,
229 conf->capability.len - (sizeof(CRLF) - 1));
230 p = ngx_cpymem(p, " STARTTLS", sizeof(" STARTTLS") - 1);
231 *p++ = CR; *p = LF;
232
233
234 size = (auth - conf->capability.data) + sizeof(CRLF) - 1
235 + sizeof(" STARTTLS LOGINDISABLED") - 1;
236
237 p = ngx_palloc(cf->pool, size);
238 if (p == NULL) {
239 return NGX_CONF_ERROR;
240 }
241
242 conf->starttls_only_capability.len = size;
243 conf->starttls_only_capability.data = p;
244
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);
249 *p++ = CR; *p = LF;
250
251 return NGX_CONF_OK;
252 }