3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 static char *ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
14 static ngx_int_t ngx_mail_cmp_conf_in_addrs(const void *one, const void *two);
17 ngx_uint_t ngx_mail_max_module;
20 static ngx_command_t ngx_mail_commands[] = {
23 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
30 NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
40 static ngx_core_module_t ngx_mail_module_ctx = {
47 ngx_module_t ngx_mail_module = {
49 &ngx_mail_module_ctx, /* module context */
50 ngx_mail_commands, /* module directives */
51 NGX_CORE_MODULE, /* module type */
52 NULL, /* init master */
53 NULL, /* init module */
54 NULL, /* init process */
55 NULL, /* init thread */
56 NULL, /* exit thread */
57 NULL, /* exit process */
58 NULL, /* exit master */
64 ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
69 ngx_uint_t i, a, l, m, mi, s, p, last, bind_all, done;
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;
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");
88 /* the main mail context */
90 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_mail_conf_ctx_t));
92 return NGX_CONF_ERROR;
95 *(ngx_mail_conf_ctx_t **) conf = ctx;
97 /* count the number of the http modules and set up their indices */
99 ngx_mail_max_module = 0;
100 for (m = 0; ngx_modules[m]; m++) {
101 if (ngx_modules[m]->type != NGX_MAIL_MODULE) {
105 ngx_modules[m]->ctx_index = ngx_mail_max_module++;
109 /* the mail main_conf context, it is the same in the all mail contexts */
111 ctx->main_conf = ngx_pcalloc(cf->pool,
112 sizeof(void *) * ngx_mail_max_module);
113 if (ctx->main_conf == NULL) {
114 return NGX_CONF_ERROR;
119 * the mail null srv_conf context, it is used to merge
120 * the server{}s' srv_conf's
123 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_mail_max_module);
124 if (ctx->srv_conf == NULL) {
125 return NGX_CONF_ERROR;
130 * create the main_conf's, the null srv_conf's, and the null loc_conf's
131 * of the all mail modules
134 for (m = 0; ngx_modules[m]; m++) {
135 if (ngx_modules[m]->type != NGX_MAIL_MODULE) {
139 module = ngx_modules[m]->ctx;
140 mi = ngx_modules[m]->ctx_index;
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;
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;
158 /* parse inside the mail{} block */
163 cf->module_type = NGX_MAIL_MODULE;
164 cf->cmd_type = NGX_MAIL_MAIN_CONF;
165 rv = ngx_conf_parse(cf, NULL);
167 if (rv != NGX_CONF_OK) {
173 /* init mail{} main_conf's, merge the server{}s' srv_conf's */
175 cmcf = ctx->main_conf[ngx_mail_core_module.ctx_index];
176 cscfp = cmcf->servers.elts;
178 for (m = 0; ngx_modules[m]; m++) {
179 if (ngx_modules[m]->type != NGX_MAIL_MODULE) {
183 module = ngx_modules[m]->ctx;
184 mi = ngx_modules[m]->ctx_index;
186 /* init mail{} main_conf's */
190 if (module->init_main_conf) {
191 rv = module->init_main_conf(cf, ctx->main_conf[mi]);
192 if (rv != NGX_CONF_OK) {
198 for (s = 0; s < cmcf->servers.nelts; s++) {
200 /* merge the server{}s' srv_conf's */
202 cf->ctx = cscfp[s]->ctx;
204 if (module->merge_srv_conf) {
205 rv = module->merge_srv_conf(cf,
207 cscfp[s]->ctx->srv_conf[mi]);
208 if (rv != NGX_CONF_OK) {
219 if (ngx_array_init(&in_ports, cf->temp_pool, 4,
220 sizeof(ngx_mail_conf_in_port_t))
223 return NGX_CONF_ERROR;
226 imls = cmcf->listen.elts;
228 for (l = 0; l < cmcf->listen.nelts; l++) {
232 in_port = in_ports.elts;
233 for (p = 0; p < in_ports.nelts; p++) {
234 if (in_port[p].port == imls[l].port) {
235 in_port = &in_port[p];
240 in_port = ngx_array_push(&in_ports);
241 if (in_port == NULL) {
242 return NGX_CONF_ERROR;
245 in_port->port = imls[l].port;
247 if (ngx_array_init(&in_port->addrs, cf->temp_pool, 2,
248 sizeof(ngx_mail_conf_in_addr_t))
251 return NGX_CONF_ERROR;
256 in_addr = ngx_array_push(&in_port->addrs);
257 if (in_addr == NULL) {
258 return NGX_CONF_ERROR;
261 in_addr->addr = imls[l].addr;
262 in_addr->ctx = imls[l].ctx;
263 in_addr->bind = imls[l].bind;
265 in_addr->ssl = imls[l].ssl;
269 /* optimize the lists of ports and addresses */
273 in_port = in_ports.elts;
274 for (p = 0; p < in_ports.nelts; p++) {
276 ngx_sort(in_port[p].addrs.elts, (size_t) in_port[p].addrs.nelts,
277 sizeof(ngx_mail_conf_in_addr_t), ngx_mail_cmp_conf_in_addrs);
279 in_addr = in_port[p].addrs.elts;
280 last = in_port[p].addrs.nelts;
283 * if there is the binding to the "*:port" then we need to bind()
284 * to the "*:port" only and ignore the other bindings
287 if (in_addr[last - 1].addr == INADDR_ANY) {
288 in_addr[last - 1].bind = 1;
295 for (a = 0; a < last; /* void */ ) {
297 if (!bind_all && !in_addr[a].bind) {
302 ls = ngx_listening_inet_stream_socket(cf, in_addr[a].addr,
305 return NGX_CONF_ERROR;
308 ls->backlog = NGX_LISTEN_BACKLOG;
313 ls->handler = ngx_mail_init_connection;
317 ls->log = *cf->cycle->new_log;
318 ls->log.data = &ls->addr_text;
319 ls->log.handler = ngx_accept_log_error;
322 imip = ngx_palloc(cf->pool, sizeof(ngx_mail_in_port_t));
324 return NGX_CONF_ERROR;
329 in_addr = in_port[p].addrs.elts;
331 if (in_addr[a].bind && in_addr[a].addr != INADDR_ANY) {
335 } else if (in_port[p].addrs.nelts > 1
336 && in_addr[last - 1].addr == INADDR_ANY)
347 ngx_log_error(NGX_LOG_ALERT, cf->log, 0,
348 "%ui: %V %d %ui %ui",
349 a, &ls->addr_text, in_addr[a].bind,
353 imip->addrs = ngx_pcalloc(cf->pool,
354 imip->naddrs * sizeof(ngx_mail_in_addr_t));
355 if (imip->addrs == NULL) {
356 return NGX_CONF_ERROR;
359 for (i = 0; i < imip->naddrs; i++) {
360 imip->addrs[i].addr = in_addr[i].addr;
361 imip->addrs[i].ctx = in_addr[i].ctx;
363 text = ngx_pnalloc(cf->pool,
364 NGX_INET_ADDRSTRLEN + sizeof(":65535") - 1);
366 return NGX_CONF_ERROR;
369 len = ngx_inet_ntop(AF_INET, &in_addr[i].addr, text,
370 NGX_INET_ADDRSTRLEN);
372 len = ngx_sprintf(text + len, ":%d", in_port[p].port) - text;
374 imip->addrs[i].addr_text.len = len;
375 imip->addrs[i].addr_text.data = text;
378 imip->addrs[i].ssl = in_addr[i].ssl;
387 in_port[p].addrs.elts = in_addr;
399 ngx_mail_cmp_conf_in_addrs(const void *one, const void *two)
401 ngx_mail_conf_in_addr_t *first, *second;
403 first = (ngx_mail_conf_in_addr_t *) one;
404 second = (ngx_mail_conf_in_addr_t *) two;
406 if (first->addr == INADDR_ANY) {
407 /* the INADDR_ANY must be the last resort, shift it to the end */
411 if (first->bind && !second->bind) {
412 /* shift explicit bind()ed addresses to the start */
416 if (!first->bind && second->bind) {
417 /* shift explicit bind()ed addresses to the start */
421 /* do not sort by default */