ngx_mail.c revision e18a033b
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
10#include <ngx_event.h>
11#include <ngx_mail.h>
12
13
14static char *ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
15static ngx_int_t ngx_mail_add_ports(ngx_conf_t *cf, ngx_array_t *ports,
16    ngx_mail_listen_t *listen);
17static char *ngx_mail_optimize_servers(ngx_conf_t *cf, ngx_array_t *ports);
18static ngx_int_t ngx_mail_add_addrs(ngx_conf_t *cf, ngx_mail_port_t *mport,
19    ngx_mail_conf_addr_t *addr);
20#if (NGX_HAVE_INET6)
21static ngx_int_t ngx_mail_add_addrs6(ngx_conf_t *cf, ngx_mail_port_t *mport,
22    ngx_mail_conf_addr_t *addr);
23#endif
24static ngx_int_t ngx_mail_cmp_conf_addrs(const void *one, const void *two);
25
26
27ngx_uint_t  ngx_mail_max_module;
28
29
30static ngx_command_t  ngx_mail_commands[] = {
31
32    { ngx_string("mail"),
33      NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
34      ngx_mail_block,
35      0,
36      0,
37      NULL },
38
39      ngx_null_command
40};
41
42
43static ngx_core_module_t  ngx_mail_module_ctx = {
44    ngx_string("mail"),
45    NULL,
46    NULL
47};
48
49
50ngx_module_t  ngx_mail_module = {
51    NGX_MODULE_V1,
52    &ngx_mail_module_ctx,                  /* module context */
53    ngx_mail_commands,                     /* module directives */
54    NGX_CORE_MODULE,                       /* module type */
55    NULL,                                  /* init master */
56    NULL,                                  /* init module */
57    NULL,                                  /* init process */
58    NULL,                                  /* init thread */
59    NULL,                                  /* exit thread */
60    NULL,                                  /* exit process */
61    NULL,                                  /* exit master */
62    NGX_MODULE_V1_PADDING
63};
64
65
66static char *
67ngx_mail_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
68{
69    char                        *rv;
70    ngx_uint_t                   i, m, mi, s;
71    ngx_conf_t                   pcf;
72    ngx_array_t                  ports;
73    ngx_mail_listen_t           *listen;
74    ngx_mail_module_t           *module;
75    ngx_mail_conf_ctx_t         *ctx;
76    ngx_mail_core_srv_conf_t   **cscfp;
77    ngx_mail_core_main_conf_t   *cmcf;
78
79    if (*(ngx_mail_conf_ctx_t **) conf) {
80        return "is duplicate";
81    }
82
83    /* the main mail context */
84
85    ctx = ngx_pcalloc(cf->pool, sizeof(ngx_mail_conf_ctx_t));
86    if (ctx == NULL) {
87        return NGX_CONF_ERROR;
88    }
89
90    *(ngx_mail_conf_ctx_t **) conf = ctx;
91
92    /* count the number of the mail modules and set up their indices */
93
94    ngx_mail_max_module = ngx_count_modules(cf->cycle, NGX_MAIL_MODULE);
95
96
97    /* the mail main_conf context, it is the same in the all mail contexts */
98
99    ctx->main_conf = ngx_pcalloc(cf->pool,
100                                 sizeof(void *) * ngx_mail_max_module);
101    if (ctx->main_conf == NULL) {
102        return NGX_CONF_ERROR;
103    }
104
105
106    /*
107     * the mail null srv_conf context, it is used to merge
108     * the server{}s' srv_conf's
109     */
110
111    ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_mail_max_module);
112    if (ctx->srv_conf == NULL) {
113        return NGX_CONF_ERROR;
114    }
115
116
117    /*
118     * create the main_conf's and the null srv_conf's of the all mail modules
119     */
120
121    for (m = 0; cf->cycle->modules[m]; m++) {
122        if (cf->cycle->modules[m]->type != NGX_MAIL_MODULE) {
123            continue;
124        }
125
126        module = cf->cycle->modules[m]->ctx;
127        mi = cf->cycle->modules[m]->ctx_index;
128
129        if (module->create_main_conf) {
130            ctx->main_conf[mi] = module->create_main_conf(cf);
131            if (ctx->main_conf[mi] == NULL) {
132                return NGX_CONF_ERROR;
133            }
134        }
135
136        if (module->create_srv_conf) {
137            ctx->srv_conf[mi] = module->create_srv_conf(cf);
138            if (ctx->srv_conf[mi] == NULL) {
139                return NGX_CONF_ERROR;
140            }
141        }
142    }
143
144
145    /* parse inside the mail{} block */
146
147    pcf = *cf;
148    cf->ctx = ctx;
149
150    cf->module_type = NGX_MAIL_MODULE;
151    cf->cmd_type = NGX_MAIL_MAIN_CONF;
152    rv = ngx_conf_parse(cf, NULL);
153
154    if (rv != NGX_CONF_OK) {
155        *cf = pcf;
156        return rv;
157    }
158
159
160    /* init mail{} main_conf's, merge the server{}s' srv_conf's */
161
162    cmcf = ctx->main_conf[ngx_mail_core_module.ctx_index];
163    cscfp = cmcf->servers.elts;
164
165    for (m = 0; cf->cycle->modules[m]; m++) {
166        if (cf->cycle->modules[m]->type != NGX_MAIL_MODULE) {
167            continue;
168        }
169
170        module = cf->cycle->modules[m]->ctx;
171        mi = cf->cycle->modules[m]->ctx_index;
172
173        /* init mail{} main_conf's */
174
175        cf->ctx = ctx;
176
177        if (module->init_main_conf) {
178            rv = module->init_main_conf(cf, ctx->main_conf[mi]);
179            if (rv != NGX_CONF_OK) {
180                *cf = pcf;
181                return rv;
182            }
183        }
184
185        for (s = 0; s < cmcf->servers.nelts; s++) {
186
187            /* merge the server{}s' srv_conf's */
188
189            cf->ctx = cscfp[s]->ctx;
190
191            if (module->merge_srv_conf) {
192                rv = module->merge_srv_conf(cf,
193                                            ctx->srv_conf[mi],
194                                            cscfp[s]->ctx->srv_conf[mi]);
195                if (rv != NGX_CONF_OK) {
196                    *cf = pcf;
197                    return rv;
198                }
199            }
200        }
201    }
202
203    *cf = pcf;
204
205
206    if (ngx_array_init(&ports, cf->temp_pool, 4, sizeof(ngx_mail_conf_port_t))
207        != NGX_OK)
208    {
209        return NGX_CONF_ERROR;
210    }
211
212    listen = cmcf->listen.elts;
213
214    for (i = 0; i < cmcf->listen.nelts; i++) {
215        if (ngx_mail_add_ports(cf, &ports, &listen[i]) != NGX_OK) {
216            return NGX_CONF_ERROR;
217        }
218    }
219
220    return ngx_mail_optimize_servers(cf, &ports);
221}
222
223
224static ngx_int_t
225ngx_mail_add_ports(ngx_conf_t *cf, ngx_array_t *ports,
226    ngx_mail_listen_t *listen)
227{
228    in_port_t              p;
229    ngx_uint_t             i;
230    struct sockaddr       *sa;
231    ngx_mail_conf_port_t  *port;
232    ngx_mail_conf_addr_t  *addr;
233
234    sa = &listen->sockaddr.sockaddr;
235    p = ngx_inet_get_port(sa);
236
237    port = ports->elts;
238    for (i = 0; i < ports->nelts; i++) {
239        if (p == port[i].port && sa->sa_family == port[i].family) {
240
241            /* a port is already in the port list */
242
243            port = &port[i];
244            goto found;
245        }
246    }
247
248    /* add a port to the port list */
249
250    port = ngx_array_push(ports);
251    if (port == NULL) {
252        return NGX_ERROR;
253    }
254
255    port->family = sa->sa_family;
256    port->port = p;
257
258    if (ngx_array_init(&port->addrs, cf->temp_pool, 2,
259                       sizeof(ngx_mail_conf_addr_t))
260        != NGX_OK)
261    {
262        return NGX_ERROR;
263    }
264
265found:
266
267    addr = ngx_array_push(&port->addrs);
268    if (addr == NULL) {
269        return NGX_ERROR;
270    }
271
272    addr->opt = *listen;
273
274    return NGX_OK;
275}
276
277
278static char *
279ngx_mail_optimize_servers(ngx_conf_t *cf, ngx_array_t *ports)
280{
281    ngx_uint_t                 i, p, last, bind_wildcard;
282    ngx_listening_t           *ls;
283    ngx_mail_port_t           *mport;
284    ngx_mail_conf_port_t      *port;
285    ngx_mail_conf_addr_t      *addr;
286    ngx_mail_core_srv_conf_t  *cscf;
287
288    port = ports->elts;
289    for (p = 0; p < ports->nelts; p++) {
290
291        ngx_sort(port[p].addrs.elts, (size_t) port[p].addrs.nelts,
292                 sizeof(ngx_mail_conf_addr_t), ngx_mail_cmp_conf_addrs);
293
294        addr = port[p].addrs.elts;
295        last = port[p].addrs.nelts;
296
297        /*
298         * if there is the binding to the "*:port" then we need to bind()
299         * to the "*:port" only and ignore the other bindings
300         */
301
302        if (addr[last - 1].opt.wildcard) {
303            addr[last - 1].opt.bind = 1;
304            bind_wildcard = 1;
305
306        } else {
307            bind_wildcard = 0;
308        }
309
310        i = 0;
311
312        while (i < last) {
313
314            if (bind_wildcard && !addr[i].opt.bind) {
315                i++;
316                continue;
317            }
318
319            ls = ngx_create_listening(cf, &addr[i].opt.sockaddr.sockaddr,
320                                      addr[i].opt.socklen);
321            if (ls == NULL) {
322                return NGX_CONF_ERROR;
323            }
324
325            ls->addr_ntop = 1;
326            ls->handler = ngx_mail_init_connection;
327            ls->pool_size = 256;
328
329            cscf = addr->opt.ctx->srv_conf[ngx_mail_core_module.ctx_index];
330
331            ls->logp = cscf->error_log;
332            ls->log.data = &ls->addr_text;
333            ls->log.handler = ngx_accept_log_error;
334
335            ls->backlog = addr[i].opt.backlog;
336
337            ls->keepalive = addr[i].opt.so_keepalive;
338#if (NGX_HAVE_KEEPALIVE_TUNABLE)
339            ls->keepidle = addr[i].opt.tcp_keepidle;
340            ls->keepintvl = addr[i].opt.tcp_keepintvl;
341            ls->keepcnt = addr[i].opt.tcp_keepcnt;
342#endif
343
344#if (NGX_HAVE_INET6)
345            ls->ipv6only = addr[i].opt.ipv6only;
346#endif
347
348            mport = ngx_palloc(cf->pool, sizeof(ngx_mail_port_t));
349            if (mport == NULL) {
350                return NGX_CONF_ERROR;
351            }
352
353            ls->servers = mport;
354
355            mport->naddrs = i + 1;
356
357            switch (ls->sockaddr->sa_family) {
358#if (NGX_HAVE_INET6)
359            case AF_INET6:
360                if (ngx_mail_add_addrs6(cf, mport, addr) != NGX_OK) {
361                    return NGX_CONF_ERROR;
362                }
363                break;
364#endif
365            default: /* AF_INET */
366                if (ngx_mail_add_addrs(cf, mport, addr) != NGX_OK) {
367                    return NGX_CONF_ERROR;
368                }
369                break;
370            }
371
372            addr++;
373            last--;
374        }
375    }
376
377    return NGX_CONF_OK;
378}
379
380
381static ngx_int_t
382ngx_mail_add_addrs(ngx_conf_t *cf, ngx_mail_port_t *mport,
383    ngx_mail_conf_addr_t *addr)
384{
385    u_char              *p;
386    size_t               len;
387    ngx_uint_t           i;
388    ngx_mail_in_addr_t  *addrs;
389    struct sockaddr_in  *sin;
390    u_char               buf[NGX_SOCKADDR_STRLEN];
391
392    mport->addrs = ngx_pcalloc(cf->pool,
393                               mport->naddrs * sizeof(ngx_mail_in_addr_t));
394    if (mport->addrs == NULL) {
395        return NGX_ERROR;
396    }
397
398    addrs = mport->addrs;
399
400    for (i = 0; i < mport->naddrs; i++) {
401
402        sin = &addr[i].opt.sockaddr.sockaddr_in;
403        addrs[i].addr = sin->sin_addr.s_addr;
404
405        addrs[i].conf.ctx = addr[i].opt.ctx;
406#if (NGX_MAIL_SSL)
407        addrs[i].conf.ssl = addr[i].opt.ssl;
408#endif
409
410        len = ngx_sock_ntop(&addr[i].opt.sockaddr.sockaddr, addr[i].opt.socklen,
411                            buf, NGX_SOCKADDR_STRLEN, 1);
412
413        p = ngx_pnalloc(cf->pool, len);
414        if (p == NULL) {
415            return NGX_ERROR;
416        }
417
418        ngx_memcpy(p, buf, len);
419
420        addrs[i].conf.addr_text.len = len;
421        addrs[i].conf.addr_text.data = p;
422    }
423
424    return NGX_OK;
425}
426
427
428#if (NGX_HAVE_INET6)
429
430static ngx_int_t
431ngx_mail_add_addrs6(ngx_conf_t *cf, ngx_mail_port_t *mport,
432    ngx_mail_conf_addr_t *addr)
433{
434    u_char               *p;
435    size_t                len;
436    ngx_uint_t            i;
437    ngx_mail_in6_addr_t  *addrs6;
438    struct sockaddr_in6  *sin6;
439    u_char                buf[NGX_SOCKADDR_STRLEN];
440
441    mport->addrs = ngx_pcalloc(cf->pool,
442                               mport->naddrs * sizeof(ngx_mail_in6_addr_t));
443    if (mport->addrs == NULL) {
444        return NGX_ERROR;
445    }
446
447    addrs6 = mport->addrs;
448
449    for (i = 0; i < mport->naddrs; i++) {
450
451        sin6 = &addr[i].opt.sockaddr.sockaddr_in6;
452        addrs6[i].addr6 = sin6->sin6_addr;
453
454        addrs6[i].conf.ctx = addr[i].opt.ctx;
455#if (NGX_MAIL_SSL)
456        addrs6[i].conf.ssl = addr[i].opt.ssl;
457#endif
458
459        len = ngx_sock_ntop(&addr[i].opt.sockaddr.sockaddr, addr[i].opt.socklen,
460                            buf, NGX_SOCKADDR_STRLEN, 1);
461
462        p = ngx_pnalloc(cf->pool, len);
463        if (p == NULL) {
464            return NGX_ERROR;
465        }
466
467        ngx_memcpy(p, buf, len);
468
469        addrs6[i].conf.addr_text.len = len;
470        addrs6[i].conf.addr_text.data = p;
471    }
472
473    return NGX_OK;
474}
475
476#endif
477
478
479static ngx_int_t
480ngx_mail_cmp_conf_addrs(const void *one, const void *two)
481{
482    ngx_mail_conf_addr_t  *first, *second;
483
484    first = (ngx_mail_conf_addr_t *) one;
485    second = (ngx_mail_conf_addr_t *) two;
486
487    if (first->opt.wildcard) {
488        /* a wildcard must be the last resort, shift it to the end */
489        return 1;
490    }
491
492    if (second->opt.wildcard) {
493        /* a wildcard must be the last resort, shift it to the end */
494        return -1;
495    }
496
497    if (first->opt.bind && !second->opt.bind) {
498        /* shift explicit bind()ed addresses to the start */
499        return -1;
500    }
501
502    if (!first->opt.bind && second->opt.bind) {
503        /* shift explicit bind()ed addresses to the start */
504        return 1;
505    }
506
507    /* do not sort by default */
508
509    return 0;
510}
511