ngx_mail_core_module.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 void *ngx_mail_core_create_main_conf(ngx_conf_t *cf);
15static void *ngx_mail_core_create_srv_conf(ngx_conf_t *cf);
16static char *ngx_mail_core_merge_srv_conf(ngx_conf_t *cf, void *parent,
17    void *child);
18static char *ngx_mail_core_server(ngx_conf_t *cf, ngx_command_t *cmd,
19    void *conf);
20static char *ngx_mail_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
21    void *conf);
22static char *ngx_mail_core_protocol(ngx_conf_t *cf, ngx_command_t *cmd,
23    void *conf);
24static char *ngx_mail_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd,
25    void *conf);
26static char *ngx_mail_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
27    void *conf);
28
29
30static ngx_command_t  ngx_mail_core_commands[] = {
31
32    { ngx_string("server"),
33      NGX_MAIL_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
34      ngx_mail_core_server,
35      0,
36      0,
37      NULL },
38
39    { ngx_string("listen"),
40      NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
41      ngx_mail_core_listen,
42      NGX_MAIL_SRV_CONF_OFFSET,
43      0,
44      NULL },
45
46    { ngx_string("protocol"),
47      NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
48      ngx_mail_core_protocol,
49      NGX_MAIL_SRV_CONF_OFFSET,
50      0,
51      NULL },
52
53    { ngx_string("timeout"),
54      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
55      ngx_conf_set_msec_slot,
56      NGX_MAIL_SRV_CONF_OFFSET,
57      offsetof(ngx_mail_core_srv_conf_t, timeout),
58      NULL },
59
60    { ngx_string("server_name"),
61      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
62      ngx_conf_set_str_slot,
63      NGX_MAIL_SRV_CONF_OFFSET,
64      offsetof(ngx_mail_core_srv_conf_t, server_name),
65      NULL },
66
67    { ngx_string("error_log"),
68      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
69      ngx_mail_core_error_log,
70      NGX_MAIL_SRV_CONF_OFFSET,
71      0,
72      NULL },
73
74    { ngx_string("resolver"),
75      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
76      ngx_mail_core_resolver,
77      NGX_MAIL_SRV_CONF_OFFSET,
78      0,
79      NULL },
80
81    { ngx_string("resolver_timeout"),
82      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
83      ngx_conf_set_msec_slot,
84      NGX_MAIL_SRV_CONF_OFFSET,
85      offsetof(ngx_mail_core_srv_conf_t, resolver_timeout),
86      NULL },
87
88      ngx_null_command
89};
90
91
92static ngx_mail_module_t  ngx_mail_core_module_ctx = {
93    NULL,                                  /* protocol */
94
95    ngx_mail_core_create_main_conf,        /* create main configuration */
96    NULL,                                  /* init main configuration */
97
98    ngx_mail_core_create_srv_conf,         /* create server configuration */
99    ngx_mail_core_merge_srv_conf           /* merge server configuration */
100};
101
102
103ngx_module_t  ngx_mail_core_module = {
104    NGX_MODULE_V1,
105    &ngx_mail_core_module_ctx,             /* module context */
106    ngx_mail_core_commands,                /* module directives */
107    NGX_MAIL_MODULE,                       /* module type */
108    NULL,                                  /* init master */
109    NULL,                                  /* init module */
110    NULL,                                  /* init process */
111    NULL,                                  /* init thread */
112    NULL,                                  /* exit thread */
113    NULL,                                  /* exit process */
114    NULL,                                  /* exit master */
115    NGX_MODULE_V1_PADDING
116};
117
118
119static void *
120ngx_mail_core_create_main_conf(ngx_conf_t *cf)
121{
122    ngx_mail_core_main_conf_t  *cmcf;
123
124    cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_core_main_conf_t));
125    if (cmcf == NULL) {
126        return NULL;
127    }
128
129    if (ngx_array_init(&cmcf->servers, cf->pool, 4,
130                       sizeof(ngx_mail_core_srv_conf_t *))
131        != NGX_OK)
132    {
133        return NULL;
134    }
135
136    if (ngx_array_init(&cmcf->listen, cf->pool, 4, sizeof(ngx_mail_listen_t))
137        != NGX_OK)
138    {
139        return NULL;
140    }
141
142    return cmcf;
143}
144
145
146static void *
147ngx_mail_core_create_srv_conf(ngx_conf_t *cf)
148{
149    ngx_mail_core_srv_conf_t  *cscf;
150
151    cscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_core_srv_conf_t));
152    if (cscf == NULL) {
153        return NULL;
154    }
155
156    /*
157     * set by ngx_pcalloc():
158     *
159     *     cscf->protocol = NULL;
160     *     cscf->error_log = NULL;
161     */
162
163    cscf->timeout = NGX_CONF_UNSET_MSEC;
164    cscf->resolver_timeout = NGX_CONF_UNSET_MSEC;
165
166    cscf->resolver = NGX_CONF_UNSET_PTR;
167
168    cscf->file_name = cf->conf_file->file.name.data;
169    cscf->line = cf->conf_file->line;
170
171    return cscf;
172}
173
174
175static char *
176ngx_mail_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
177{
178    ngx_mail_core_srv_conf_t *prev = parent;
179    ngx_mail_core_srv_conf_t *conf = child;
180
181    ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
182    ngx_conf_merge_msec_value(conf->resolver_timeout, prev->resolver_timeout,
183                              30000);
184
185
186    ngx_conf_merge_str_value(conf->server_name, prev->server_name, "");
187
188    if (conf->server_name.len == 0) {
189        conf->server_name = cf->cycle->hostname;
190    }
191
192    if (conf->protocol == NULL) {
193        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
194                      "unknown mail protocol for server in %s:%ui",
195                      conf->file_name, conf->line);
196        return NGX_CONF_ERROR;
197    }
198
199    if (conf->error_log == NULL) {
200        if (prev->error_log) {
201            conf->error_log = prev->error_log;
202        } else {
203            conf->error_log = &cf->cycle->new_log;
204        }
205    }
206
207    ngx_conf_merge_ptr_value(conf->resolver, prev->resolver, NULL);
208
209    return NGX_CONF_OK;
210}
211
212
213static char *
214ngx_mail_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
215{
216    char                       *rv;
217    void                       *mconf;
218    ngx_uint_t                  m;
219    ngx_conf_t                  pcf;
220    ngx_mail_module_t          *module;
221    ngx_mail_conf_ctx_t        *ctx, *mail_ctx;
222    ngx_mail_core_srv_conf_t   *cscf, **cscfp;
223    ngx_mail_core_main_conf_t  *cmcf;
224
225    ctx = ngx_pcalloc(cf->pool, sizeof(ngx_mail_conf_ctx_t));
226    if (ctx == NULL) {
227        return NGX_CONF_ERROR;
228    }
229
230    mail_ctx = cf->ctx;
231    ctx->main_conf = mail_ctx->main_conf;
232
233    /* the server{}'s srv_conf */
234
235    ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_mail_max_module);
236    if (ctx->srv_conf == NULL) {
237        return NGX_CONF_ERROR;
238    }
239
240    for (m = 0; cf->cycle->modules[m]; m++) {
241        if (cf->cycle->modules[m]->type != NGX_MAIL_MODULE) {
242            continue;
243        }
244
245        module = cf->cycle->modules[m]->ctx;
246
247        if (module->create_srv_conf) {
248            mconf = module->create_srv_conf(cf);
249            if (mconf == NULL) {
250                return NGX_CONF_ERROR;
251            }
252
253            ctx->srv_conf[cf->cycle->modules[m]->ctx_index] = mconf;
254        }
255    }
256
257    /* the server configuration context */
258
259    cscf = ctx->srv_conf[ngx_mail_core_module.ctx_index];
260    cscf->ctx = ctx;
261
262    cmcf = ctx->main_conf[ngx_mail_core_module.ctx_index];
263
264    cscfp = ngx_array_push(&cmcf->servers);
265    if (cscfp == NULL) {
266        return NGX_CONF_ERROR;
267    }
268
269    *cscfp = cscf;
270
271
272    /* parse inside server{} */
273
274    pcf = *cf;
275    cf->ctx = ctx;
276    cf->cmd_type = NGX_MAIL_SRV_CONF;
277
278    rv = ngx_conf_parse(cf, NULL);
279
280    *cf = pcf;
281
282    if (rv == NGX_CONF_OK && !cscf->listen) {
283        ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
284                      "no \"listen\" is defined for server in %s:%ui",
285                      cscf->file_name, cscf->line);
286        return NGX_CONF_ERROR;
287    }
288
289    return rv;
290}
291
292
293static char *
294ngx_mail_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
295{
296    ngx_mail_core_srv_conf_t  *cscf = conf;
297
298    ngx_str_t                  *value;
299    ngx_url_t                   u;
300    ngx_uint_t                  i, m;
301    ngx_mail_listen_t          *ls;
302    ngx_mail_module_t          *module;
303    ngx_mail_core_main_conf_t  *cmcf;
304
305    cscf->listen = 1;
306
307    value = cf->args->elts;
308
309    ngx_memzero(&u, sizeof(ngx_url_t));
310
311    u.url = value[1];
312    u.listen = 1;
313
314    if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
315        if (u.err) {
316            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
317                               "%s in \"%V\" of the \"listen\" directive",
318                               u.err, &u.url);
319        }
320
321        return NGX_CONF_ERROR;
322    }
323
324    cmcf = ngx_mail_conf_get_module_main_conf(cf, ngx_mail_core_module);
325
326    ls = cmcf->listen.elts;
327
328    for (i = 0; i < cmcf->listen.nelts; i++) {
329
330        if (ngx_cmp_sockaddr(&ls[i].sockaddr.sockaddr, ls[i].socklen,
331                             (struct sockaddr *) &u.sockaddr, u.socklen, 1)
332            != NGX_OK)
333        {
334            continue;
335        }
336
337        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
338                           "duplicate \"%V\" address and port pair", &u.url);
339        return NGX_CONF_ERROR;
340    }
341
342    ls = ngx_array_push(&cmcf->listen);
343    if (ls == NULL) {
344        return NGX_CONF_ERROR;
345    }
346
347    ngx_memzero(ls, sizeof(ngx_mail_listen_t));
348
349    ngx_memcpy(&ls->sockaddr.sockaddr, &u.sockaddr, u.socklen);
350
351    ls->socklen = u.socklen;
352    ls->backlog = NGX_LISTEN_BACKLOG;
353    ls->wildcard = u.wildcard;
354    ls->ctx = cf->ctx;
355
356#if (NGX_HAVE_INET6)
357    ls->ipv6only = 1;
358#endif
359
360    if (cscf->protocol == NULL) {
361        for (m = 0; cf->cycle->modules[m]; m++) {
362            if (cf->cycle->modules[m]->type != NGX_MAIL_MODULE) {
363                continue;
364            }
365
366            module = cf->cycle->modules[m]->ctx;
367
368            if (module->protocol == NULL) {
369                continue;
370            }
371
372            for (i = 0; module->protocol->port[i]; i++) {
373                if (module->protocol->port[i] == u.port) {
374                    cscf->protocol = module->protocol;
375                    break;
376                }
377            }
378        }
379    }
380
381    for (i = 2; i < cf->args->nelts; i++) {
382
383        if (ngx_strcmp(value[i].data, "bind") == 0) {
384            ls->bind = 1;
385            continue;
386        }
387
388        if (ngx_strncmp(value[i].data, "backlog=", 8) == 0) {
389            ls->backlog = ngx_atoi(value[i].data + 8, value[i].len - 8);
390            ls->bind = 1;
391
392            if (ls->backlog == NGX_ERROR || ls->backlog == 0) {
393                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
394                                   "invalid backlog \"%V\"", &value[i]);
395                return NGX_CONF_ERROR;
396            }
397
398            continue;
399        }
400
401        if (ngx_strncmp(value[i].data, "ipv6only=o", 10) == 0) {
402#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
403            size_t  len;
404            u_char  buf[NGX_SOCKADDR_STRLEN];
405
406            if (ls->sockaddr.sockaddr.sa_family == AF_INET6) {
407
408                if (ngx_strcmp(&value[i].data[10], "n") == 0) {
409                    ls->ipv6only = 1;
410
411                } else if (ngx_strcmp(&value[i].data[10], "ff") == 0) {
412                    ls->ipv6only = 0;
413
414                } else {
415                    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
416                                       "invalid ipv6only flags \"%s\"",
417                                       &value[i].data[9]);
418                    return NGX_CONF_ERROR;
419                }
420
421                ls->bind = 1;
422
423            } else {
424                len = ngx_sock_ntop(&ls->sockaddr.sockaddr, ls->socklen, buf,
425                                    NGX_SOCKADDR_STRLEN, 1);
426
427                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
428                                   "ipv6only is not supported "
429                                   "on addr \"%*s\", ignored", len, buf);
430            }
431
432            continue;
433#else
434            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
435                               "bind ipv6only is not supported "
436                               "on this platform");
437            return NGX_CONF_ERROR;
438#endif
439        }
440
441        if (ngx_strcmp(value[i].data, "ssl") == 0) {
442#if (NGX_MAIL_SSL)
443            ls->ssl = 1;
444            continue;
445#else
446            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
447                               "the \"ssl\" parameter requires "
448                               "ngx_mail_ssl_module");
449            return NGX_CONF_ERROR;
450#endif
451        }
452
453        if (ngx_strncmp(value[i].data, "so_keepalive=", 13) == 0) {
454
455            if (ngx_strcmp(&value[i].data[13], "on") == 0) {
456                ls->so_keepalive = 1;
457
458            } else if (ngx_strcmp(&value[i].data[13], "off") == 0) {
459                ls->so_keepalive = 2;
460
461            } else {
462
463#if (NGX_HAVE_KEEPALIVE_TUNABLE)
464                u_char     *p, *end;
465                ngx_str_t   s;
466
467                end = value[i].data + value[i].len;
468                s.data = value[i].data + 13;
469
470                p = ngx_strlchr(s.data, end, ':');
471                if (p == NULL) {
472                    p = end;
473                }
474
475                if (p > s.data) {
476                    s.len = p - s.data;
477
478                    ls->tcp_keepidle = ngx_parse_time(&s, 1);
479                    if (ls->tcp_keepidle == (time_t) NGX_ERROR) {
480                        goto invalid_so_keepalive;
481                    }
482                }
483
484                s.data = (p < end) ? (p + 1) : end;
485
486                p = ngx_strlchr(s.data, end, ':');
487                if (p == NULL) {
488                    p = end;
489                }
490
491                if (p > s.data) {
492                    s.len = p - s.data;
493
494                    ls->tcp_keepintvl = ngx_parse_time(&s, 1);
495                    if (ls->tcp_keepintvl == (time_t) NGX_ERROR) {
496                        goto invalid_so_keepalive;
497                    }
498                }
499
500                s.data = (p < end) ? (p + 1) : end;
501
502                if (s.data < end) {
503                    s.len = end - s.data;
504
505                    ls->tcp_keepcnt = ngx_atoi(s.data, s.len);
506                    if (ls->tcp_keepcnt == NGX_ERROR) {
507                        goto invalid_so_keepalive;
508                    }
509                }
510
511                if (ls->tcp_keepidle == 0 && ls->tcp_keepintvl == 0
512                    && ls->tcp_keepcnt == 0)
513                {
514                    goto invalid_so_keepalive;
515                }
516
517                ls->so_keepalive = 1;
518
519#else
520
521                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
522                                   "the \"so_keepalive\" parameter accepts "
523                                   "only \"on\" or \"off\" on this platform");
524                return NGX_CONF_ERROR;
525
526#endif
527            }
528
529            ls->bind = 1;
530
531            continue;
532
533#if (NGX_HAVE_KEEPALIVE_TUNABLE)
534        invalid_so_keepalive:
535
536            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
537                               "invalid so_keepalive value: \"%s\"",
538                               &value[i].data[13]);
539            return NGX_CONF_ERROR;
540#endif
541        }
542
543        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
544                           "the invalid \"%V\" parameter", &value[i]);
545        return NGX_CONF_ERROR;
546    }
547
548    return NGX_CONF_OK;
549}
550
551
552static char *
553ngx_mail_core_protocol(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
554{
555    ngx_mail_core_srv_conf_t  *cscf = conf;
556
557    ngx_str_t          *value;
558    ngx_uint_t          m;
559    ngx_mail_module_t  *module;
560
561    value = cf->args->elts;
562
563    for (m = 0; cf->cycle->modules[m]; m++) {
564        if (cf->cycle->modules[m]->type != NGX_MAIL_MODULE) {
565            continue;
566        }
567
568        module = cf->cycle->modules[m]->ctx;
569
570        if (module->protocol
571            && ngx_strcmp(module->protocol->name.data, value[1].data) == 0)
572        {
573            cscf->protocol = module->protocol;
574
575            return NGX_CONF_OK;
576        }
577    }
578
579    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
580                       "unknown protocol \"%V\"", &value[1]);
581    return NGX_CONF_ERROR;
582}
583
584
585static char *
586ngx_mail_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
587{
588    ngx_mail_core_srv_conf_t  *cscf = conf;
589
590    return ngx_log_set_log(cf, &cscf->error_log);
591}
592
593
594static char *
595ngx_mail_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
596{
597    ngx_mail_core_srv_conf_t  *cscf = conf;
598
599    ngx_str_t  *value;
600
601    value = cf->args->elts;
602
603    if (cscf->resolver != NGX_CONF_UNSET_PTR) {
604        return "is duplicate";
605    }
606
607    if (ngx_strcmp(value[1].data, "off") == 0) {
608        cscf->resolver = NULL;
609        return NGX_CONF_OK;
610    }
611
612    cscf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1);
613    if (cscf->resolver == NULL) {
614        return NGX_CONF_ERROR;
615    }
616
617    return NGX_CONF_OK;
618}
619
620
621char *
622ngx_mail_capabilities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
623{
624    char  *p = conf;
625
626    ngx_str_t    *c, *value;
627    ngx_uint_t    i;
628    ngx_array_t  *a;
629
630    a = (ngx_array_t *) (p + cmd->offset);
631
632    value = cf->args->elts;
633
634    for (i = 1; i < cf->args->nelts; i++) {
635        c = ngx_array_push(a);
636        if (c == NULL) {
637            return NGX_CONF_ERROR;
638        }
639
640        *c = value[i];
641    }
642
643    return NGX_CONF_OK;
644}
645