Mercurial > hg > nginx
annotate src/mail/ngx_mail.c @ 1233:754f3648642d
fix ssl_verify_client for HTTP/0.9
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 04 Jun 2007 20:40:03 +0000 |
parents | c6c33f81fd79 |
children | 0d57c150115b |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
419
diff
changeset
|
5 |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_event.h> |
1136 | 10 #include <ngx_mail.h> |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
11 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
12 |
1136 | 13 static char *ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
1209
c6c33f81fd79
use stable search where it is required
Igor Sysoev <igor@sysoev.ru>
parents:
1136
diff
changeset
|
14 static int ngx_mail_cmp_conf_in_addrs(const void *one, const void *two); |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
15 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
16 |
1136 | 17 ngx_uint_t ngx_mail_max_module; |
521 | 18 |
19 | |
1136 | 20 static ngx_command_t ngx_mail_commands[] = { |
21 | |
22 { ngx_string("mail"), | |
23 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, | |
24 ngx_mail_block, | |
25 0, | |
26 0, | |
27 NULL }, | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
28 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
29 { ngx_string("imap"), |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
30 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, |
1136 | 31 ngx_mail_block, |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
32 0, |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
33 0, |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
34 NULL }, |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
35 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
36 ngx_null_command |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
37 }; |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
38 |
577 | 39 |
1136 | 40 static ngx_core_module_t ngx_mail_module_ctx = { |
41 ngx_string("mail"), | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
42 NULL, |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
43 NULL |
577 | 44 }; |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
45 |
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
46 |
1136 | 47 ngx_module_t ngx_mail_module = { |
509 | 48 NGX_MODULE_V1, |
1136 | 49 &ngx_mail_module_ctx, /* module context */ |
50 ngx_mail_commands, /* module directives */ | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
51 NGX_CORE_MODULE, /* module type */ |
541 | 52 NULL, /* init master */ |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
53 NULL, /* init module */ |
541 | 54 NULL, /* init process */ |
55 NULL, /* init thread */ | |
56 NULL, /* exit thread */ | |
57 NULL, /* exit process */ | |
58 NULL, /* exit master */ | |
59 NGX_MODULE_V1_PADDING | |
413
de9d4726e28a
nginx-0.0.10-2004-08-31-23:05:39 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
60 }; |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
61 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
62 |
521 | 63 static char * |
1136 | 64 ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
65 { |
521 | 66 char *rv; |
641 | 67 u_char *text; |
68 size_t len; | |
69 ngx_uint_t i, a, l, m, mi, s, p, last, bind_all, done; | |
521 | 70 ngx_conf_t pcf; |
641 | 71 ngx_array_t in_ports; |
72 ngx_listening_t *ls; | |
1136 | 73 ngx_mail_listen_t *imls; |
74 ngx_mail_module_t *module; | |
75 ngx_mail_in_port_t *imip; | |
76 ngx_mail_conf_ctx_t *ctx; | |
77 ngx_mail_conf_in_port_t *in_port; | |
78 ngx_mail_conf_in_addr_t *in_addr; | |
79 ngx_mail_core_srv_conf_t **cscfp; | |
80 ngx_mail_core_main_conf_t *cmcf; | |
521 | 81 |
1136 | 82 if (cmd->name.data[0] == 'i') { |
83 ngx_conf_log_error(NGX_LOG_WARN, cf, 0, | |
84 "the \"imap\" directive is deprecated, " | |
85 "use the \"mail\" directive instead"); | |
86 } | |
521 | 87 |
1136 | 88 /* the main mail context */ |
89 | |
90 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_mail_conf_ctx_t)); | |
521 | 91 if (ctx == NULL) { |
92 return NGX_CONF_ERROR; | |
93 } | |
94 | |
1136 | 95 *(ngx_mail_conf_ctx_t **) conf = ctx; |
521 | 96 |
97 /* count the number of the http modules and set up their indices */ | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
98 |
1136 | 99 ngx_mail_max_module = 0; |
521 | 100 for (m = 0; ngx_modules[m]; m++) { |
1136 | 101 if (ngx_modules[m]->type != NGX_MAIL_MODULE) { |
521 | 102 continue; |
103 } | |
104 | |
1136 | 105 ngx_modules[m]->ctx_index = ngx_mail_max_module++; |
521 | 106 } |
107 | |
108 | |
1136 | 109 /* the mail main_conf context, it is the same in the all mail contexts */ |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
110 |
521 | 111 ctx->main_conf = ngx_pcalloc(cf->pool, |
1136 | 112 sizeof(void *) * ngx_mail_max_module); |
521 | 113 if (ctx->main_conf == NULL) { |
114 return NGX_CONF_ERROR; | |
115 } | |
116 | |
117 | |
118 /* | |
1136 | 119 * the mail null srv_conf context, it is used to merge |
521 | 120 * the server{}s' srv_conf's |
121 */ | |
122 | |
1136 | 123 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_mail_max_module); |
521 | 124 if (ctx->srv_conf == NULL) { |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
125 return NGX_CONF_ERROR; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
126 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
127 |
521 | 128 |
129 /* | |
130 * create the main_conf's, the null srv_conf's, and the null loc_conf's | |
1136 | 131 * of the all mail modules |
521 | 132 */ |
133 | |
134 for (m = 0; ngx_modules[m]; m++) { | |
1136 | 135 if (ngx_modules[m]->type != NGX_MAIL_MODULE) { |
521 | 136 continue; |
137 } | |
138 | |
139 module = ngx_modules[m]->ctx; | |
140 mi = ngx_modules[m]->ctx_index; | |
141 | |
142 if (module->create_main_conf) { | |
143 ctx->main_conf[mi] = module->create_main_conf(cf); | |
144 if (ctx->main_conf[mi] == NULL) { | |
145 return NGX_CONF_ERROR; | |
146 } | |
147 } | |
148 | |
149 if (module->create_srv_conf) { | |
150 ctx->srv_conf[mi] = module->create_srv_conf(cf); | |
151 if (ctx->srv_conf[mi] == NULL) { | |
152 return NGX_CONF_ERROR; | |
153 } | |
154 } | |
155 } | |
156 | |
157 | |
1136 | 158 /* parse inside the mail{} block */ |
521 | 159 |
160 pcf = *cf; | |
161 cf->ctx = ctx; | |
162 | |
1136 | 163 cf->module_type = NGX_MAIL_MODULE; |
164 cf->cmd_type = NGX_MAIL_MAIN_CONF; | |
521 | 165 rv = ngx_conf_parse(cf, NULL); |
166 | |
167 if (rv != NGX_CONF_OK) { | |
168 *cf = pcf; | |
169 return rv; | |
170 } | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
171 |
521 | 172 |
1136 | 173 /* init mail{} main_conf's, merge the server{}s' srv_conf's */ |
521 | 174 |
1136 | 175 cmcf = ctx->main_conf[ngx_mail_core_module.ctx_index]; |
521 | 176 cscfp = cmcf->servers.elts; |
177 | |
178 for (m = 0; ngx_modules[m]; m++) { | |
1136 | 179 if (ngx_modules[m]->type != NGX_MAIL_MODULE) { |
521 | 180 continue; |
181 } | |
182 | |
183 module = ngx_modules[m]->ctx; | |
184 mi = ngx_modules[m]->ctx_index; | |
185 | |
1136 | 186 /* init mail{} main_conf's */ |
521 | 187 |
188 if (module->init_main_conf) { | |
577 | 189 rv = module->init_main_conf(cf, ctx->main_conf[mi]); |
521 | 190 if (rv != NGX_CONF_OK) { |
191 *cf = pcf; | |
192 return rv; | |
193 } | |
194 } | |
195 | |
196 for (s = 0; s < cmcf->servers.nelts; s++) { | |
577 | 197 |
521 | 198 /* merge the server{}s' srv_conf's */ |
577 | 199 |
521 | 200 if (module->merge_srv_conf) { |
201 rv = module->merge_srv_conf(cf, | |
202 ctx->srv_conf[mi], | |
203 cscfp[s]->ctx->srv_conf[mi]); | |
204 if (rv != NGX_CONF_OK) { | |
205 *cf = pcf; | |
206 return rv; | |
207 } | |
208 } | |
209 } | |
210 } | |
211 | |
1136 | 212 /* mail{}'s cf->ctx was needed while the configuration merging */ |
577 | 213 |
214 *cf = pcf; | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
215 |
641 | 216 |
217 if (ngx_array_init(&in_ports, cf->temp_pool, 4, | |
1136 | 218 sizeof(ngx_mail_conf_in_port_t)) |
641 | 219 != NGX_OK) |
220 { | |
221 return NGX_CONF_ERROR; | |
222 } | |
223 | |
224 imls = cmcf->listen.elts; | |
225 | |
226 for (l = 0; l < cmcf->listen.nelts; l++) { | |
227 | |
228 /* AF_INET only */ | |
229 | |
230 in_port = in_ports.elts; | |
231 for (p = 0; p < in_ports.nelts; p++) { | |
232 if (in_port[p].port == imls[l].port) { | |
233 in_port = &in_port[p]; | |
234 goto found; | |
235 } | |
236 } | |
237 | |
238 in_port = ngx_array_push(&in_ports); | |
239 if (in_port == NULL) { | |
240 return NGX_CONF_ERROR; | |
241 } | |
242 | |
243 in_port->port = imls[l].port; | |
244 | |
245 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 2, | |
1136 | 246 sizeof(ngx_mail_conf_in_addr_t)) |
641 | 247 != NGX_OK) |
248 { | |
249 return NGX_CONF_ERROR; | |
250 } | |
251 | |
252 found: | |
253 | |
254 in_addr = ngx_array_push(&in_port->addrs); | |
255 if (in_addr == NULL) { | |
256 return NGX_CONF_ERROR; | |
257 } | |
258 | |
259 in_addr->addr = imls[l].addr; | |
260 in_addr->ctx = imls[l].ctx; | |
261 in_addr->bind = imls[l].bind; | |
262 } | |
263 | |
264 /* optimize the lists of ports and addresses */ | |
265 | |
266 /* AF_INET only */ | |
267 | |
268 in_port = in_ports.elts; | |
269 for (p = 0; p < in_ports.nelts; p++) { | |
270 | |
1209
c6c33f81fd79
use stable search where it is required
Igor Sysoev <igor@sysoev.ru>
parents:
1136
diff
changeset
|
271 ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts, |
c6c33f81fd79
use stable search where it is required
Igor Sysoev <igor@sysoev.ru>
parents:
1136
diff
changeset
|
272 sizeof(ngx_mail_conf_in_addr_t), ngx_mail_cmp_conf_in_addrs); |
641 | 273 |
274 in_addr = in_port[p].addrs.elts; | |
275 last = in_port[p].addrs.nelts; | |
276 | |
277 /* | |
278 * if there is the binding to the "*:port" then we need to bind() | |
279 * to the "*:port" only and ignore the other bindings | |
280 */ | |
281 | |
282 if (in_addr[last - 1].addr == INADDR_ANY) { | |
283 in_addr[last - 1].bind = 1; | |
284 bind_all = 0; | |
285 | |
286 } else { | |
287 bind_all = 1; | |
288 } | |
289 | |
290 for (a = 0; a < last; /* void */ ) { | |
291 | |
292 if (!bind_all && !in_addr[a].bind) { | |
293 a++; | |
294 continue; | |
295 } | |
296 | |
297 ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr, | |
298 in_port[p].port); | |
299 if (ls == NULL) { | |
300 return NGX_CONF_ERROR; | |
301 } | |
302 | |
303 ls->backlog = -1; | |
304 ls->rcvbuf = -1; | |
305 ls->sndbuf = -1; | |
306 | |
307 ls->addr_ntop = 1; | |
1136 | 308 ls->handler = ngx_mail_init_connection; |
641 | 309 ls->pool_size = 256; |
310 | |
311 /* STUB */ | |
312 ls->log = *cf->cycle->new_log; | |
313 ls->log.data = &ls->addr_text; | |
314 ls->log.handler = ngx_accept_log_error; | |
315 /**/ | |
316 | |
1136 | 317 imip = ngx_palloc(cf->pool, sizeof(ngx_mail_in_port_t)); |
641 | 318 if (imip == NULL) { |
319 return NGX_CONF_ERROR; | |
320 } | |
321 | |
322 ls->servers = imip; | |
323 | |
324 in_addr = in_port[p].addrs.elts; | |
325 | |
326 if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) { | |
327 imip->naddrs = 1; | |
328 done = 0; | |
329 | |
330 } else if (in_port[p].addrs.nelts > 1 | |
331 && in_addr[last - 1].addr == INADDR_ANY) | |
332 { | |
333 imip->naddrs = last; | |
334 done = 1; | |
335 | |
336 } else { | |
337 imip->naddrs = 1; | |
338 done = 0; | |
339 } | |
340 | |
341 #if 0 | |
342 ngx_log_error(NGX_LOG_ALERT, cf->log, 0, | |
343 "%ui: %V %d %ui %ui", | |
344 a, &ls->addr_text, in_addr[a].bind, | |
345 imip->naddrs, last); | |
346 #endif | |
347 | |
348 imip->addrs = ngx_pcalloc(cf->pool, | |
1136 | 349 imip->naddrs * sizeof(ngx_mail_in_addr_t)); |
641 | 350 if (imip->addrs == NULL) { |
351 return NGX_CONF_ERROR; | |
352 } | |
353 | |
354 for (i = 0; i < imip->naddrs; i++) { | |
355 imip->addrs[i].addr = in_addr[i].addr; | |
356 imip->addrs[i].ctx = in_addr[i].ctx; | |
357 | |
358 text = ngx_palloc(cf->pool, | |
359 INET_ADDRSTRLEN - 1 + sizeof(":65535") - 1); | |
360 if (text == NULL) { | |
361 return NGX_CONF_ERROR; | |
362 } | |
363 | |
364 len = ngx_inet_ntop(AF_INET, &in_addr[i].addr, text, | |
365 INET_ADDRSTRLEN); | |
366 | |
367 len = ngx_sprintf(text + len, ":%d", in_port[p].port) - text; | |
368 | |
369 imip->addrs[i].addr_text.len = len; | |
370 imip->addrs[i].addr_text.data = text; | |
371 } | |
372 | |
373 if (done) { | |
374 break; | |
375 } | |
376 | |
377 in_addr++; | |
378 in_port[p].addrs.elts = in_addr; | |
379 last--; | |
380 | |
381 a = 0; | |
382 } | |
383 } | |
384 | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
385 return NGX_CONF_OK; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
415
diff
changeset
|
386 } |
641 | 387 |
388 | |
1209
c6c33f81fd79
use stable search where it is required
Igor Sysoev <igor@sysoev.ru>
parents:
1136
diff
changeset
|
389 static int |
1136 | 390 ngx_mail_cmp_conf_in_addrs(const void *one, const void *two) |
641 | 391 { |
1136 | 392 ngx_mail_conf_in_addr_t *first, *second; |
641 | 393 |
1136 | 394 first = (ngx_mail_conf_in_addr_t *) one; |
395 second = (ngx_mail_conf_in_addr_t *) two; | |
641 | 396 |
397 if (first->addr == INADDR_ANY) { | |
398 /* the INADDR_ANY must be the last resort, shift it to the end */ | |
399 return 1; | |
400 } | |
401 | |
402 if (first->bind && !second->bind) { | |
403 /* shift explicit bind()ed addresses to the start */ | |
404 return -1; | |
405 } | |
406 | |
407 if (!first->bind && second->bind) { | |
408 /* shift explicit bind()ed addresses to the start */ | |
409 return 1; | |
410 } | |
411 | |
412 /* do not sort by default */ | |
413 | |
414 return 0; | |
415 } |