3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 static void *ngx_mail_core_create_main_conf(ngx_conf_t *cf);
14 static void *ngx_mail_core_create_srv_conf(ngx_conf_t *cf);
15 static char *ngx_mail_core_merge_srv_conf(ngx_conf_t *cf, void *parent,
17 static char *ngx_mail_core_server(ngx_conf_t *cf, ngx_command_t *cmd,
19 static char *ngx_mail_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
21 static char *ngx_mail_core_protocol(ngx_conf_t *cf, ngx_command_t *cmd,
23 static char *ngx_mail_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd,
27 static ngx_command_t ngx_mail_core_commands[] = {
29 { ngx_string("server"),
30 NGX_MAIL_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
36 { ngx_string("listen"),
37 NGX_MAIL_SRV_CONF|NGX_CONF_TAKE12,
39 NGX_MAIL_SRV_CONF_OFFSET,
43 { ngx_string("protocol"),
44 NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
45 ngx_mail_core_protocol,
46 NGX_MAIL_SRV_CONF_OFFSET,
50 { ngx_string("so_keepalive"),
51 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
52 ngx_conf_set_flag_slot,
53 NGX_MAIL_SRV_CONF_OFFSET,
54 offsetof(ngx_mail_core_srv_conf_t, so_keepalive),
57 { ngx_string("timeout"),
58 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
59 ngx_conf_set_msec_slot,
60 NGX_MAIL_SRV_CONF_OFFSET,
61 offsetof(ngx_mail_core_srv_conf_t, timeout),
64 { ngx_string("server_name"),
65 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
66 ngx_conf_set_str_slot,
67 NGX_MAIL_SRV_CONF_OFFSET,
68 offsetof(ngx_mail_core_srv_conf_t, server_name),
71 { ngx_string("resolver"),
72 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
73 ngx_mail_core_resolver,
74 NGX_MAIL_SRV_CONF_OFFSET,
78 { ngx_string("resolver_timeout"),
79 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
80 ngx_conf_set_msec_slot,
81 NGX_MAIL_SRV_CONF_OFFSET,
82 offsetof(ngx_mail_core_srv_conf_t, resolver_timeout),
89 static ngx_mail_module_t ngx_mail_core_module_ctx = {
92 ngx_mail_core_create_main_conf, /* create main configuration */
93 NULL, /* init main configuration */
95 ngx_mail_core_create_srv_conf, /* create server configuration */
96 ngx_mail_core_merge_srv_conf /* merge server configuration */
100 ngx_module_t ngx_mail_core_module = {
102 &ngx_mail_core_module_ctx, /* module context */
103 ngx_mail_core_commands, /* module directives */
104 NGX_MAIL_MODULE, /* module type */
105 NULL, /* init master */
106 NULL, /* init module */
107 NULL, /* init process */
108 NULL, /* init thread */
109 NULL, /* exit thread */
110 NULL, /* exit process */
111 NULL, /* exit master */
112 NGX_MODULE_V1_PADDING
117 ngx_mail_core_create_main_conf(ngx_conf_t *cf)
119 ngx_mail_core_main_conf_t *cmcf;
121 cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_core_main_conf_t));
123 return NGX_CONF_ERROR;
126 if (ngx_array_init(&cmcf->servers, cf->pool, 4,
127 sizeof(ngx_mail_core_srv_conf_t *))
130 return NGX_CONF_ERROR;
133 if (ngx_array_init(&cmcf->listen, cf->pool, 4, sizeof(ngx_mail_listen_t))
136 return NGX_CONF_ERROR;
144 ngx_mail_core_create_srv_conf(ngx_conf_t *cf)
146 ngx_mail_core_srv_conf_t *cscf;
148 cscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_core_srv_conf_t));
154 * set by ngx_pcalloc():
156 * cscf->protocol = NULL;
159 cscf->timeout = NGX_CONF_UNSET_MSEC;
160 cscf->resolver_timeout = NGX_CONF_UNSET_MSEC;
161 cscf->so_keepalive = NGX_CONF_UNSET;
163 cscf->resolver = NGX_CONF_UNSET_PTR;
165 cscf->file_name = cf->conf_file->file.name.data;
166 cscf->line = cf->conf_file->line;
173 ngx_mail_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
175 ngx_mail_core_srv_conf_t *prev = parent;
176 ngx_mail_core_srv_conf_t *conf = child;
178 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
179 ngx_conf_merge_msec_value(conf->resolver_timeout, prev->resolver_timeout,
182 ngx_conf_merge_value(conf->so_keepalive, prev->so_keepalive, 0);
185 ngx_conf_merge_str_value(conf->server_name, prev->server_name, "");
187 if (conf->server_name.len == 0) {
188 conf->server_name = cf->cycle->hostname;
191 if (conf->protocol == NULL) {
192 ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
193 "unknown mail protocol for server in %s:%ui",
194 conf->file_name, conf->line);
195 return NGX_CONF_ERROR;
198 ngx_conf_merge_ptr_value(conf->resolver, prev->resolver, NULL);
205 ngx_mail_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
211 ngx_mail_module_t *module;
212 ngx_mail_conf_ctx_t *ctx, *mail_ctx;
213 ngx_mail_core_srv_conf_t *cscf, **cscfp;
214 ngx_mail_core_main_conf_t *cmcf;
216 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_mail_conf_ctx_t));
218 return NGX_CONF_ERROR;
222 ctx->main_conf = mail_ctx->main_conf;
224 /* the server{}'s srv_conf */
226 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_mail_max_module);
227 if (ctx->srv_conf == NULL) {
228 return NGX_CONF_ERROR;
231 for (m = 0; ngx_modules[m]; m++) {
232 if (ngx_modules[m]->type != NGX_MAIL_MODULE) {
236 module = ngx_modules[m]->ctx;
238 if (module->create_srv_conf) {
239 mconf = module->create_srv_conf(cf);
241 return NGX_CONF_ERROR;
244 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
248 /* the server configuration context */
250 cscf = ctx->srv_conf[ngx_mail_core_module.ctx_index];
253 cmcf = ctx->main_conf[ngx_mail_core_module.ctx_index];
255 cscfp = ngx_array_push(&cmcf->servers);
257 return NGX_CONF_ERROR;
263 /* parse inside server{} */
267 cf->cmd_type = NGX_MAIL_SRV_CONF;
269 rv = ngx_conf_parse(cf, NULL);
280 ngx_mail_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
282 ngx_mail_core_srv_conf_t *cscf = conf;
287 ngx_mail_listen_t *imls;
288 ngx_mail_module_t *module;
289 ngx_mail_core_main_conf_t *cmcf;
291 value = cf->args->elts;
293 ngx_memzero(&u, sizeof(ngx_url_t));
298 if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
300 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
301 "%s in \"%V\" of the \"listen\" directive",
305 return NGX_CONF_ERROR;
308 if (u.family != AF_INET) {
309 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "listen supports IPv4 only");
310 return NGX_CONF_ERROR;
313 cmcf = ngx_mail_conf_get_module_main_conf(cf, ngx_mail_core_module);
315 imls = cmcf->listen.elts;
317 for (i = 0; i < cmcf->listen.nelts; i++) {
319 if (imls[i].addr != u.addr.in_addr || imls[i].port != u.port) {
323 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
324 "duplicate \"%V\" address and port pair", &u.url);
325 return NGX_CONF_ERROR;
328 imls = ngx_array_push(&cmcf->listen);
330 return NGX_CONF_ERROR;
333 ngx_memzero(imls, sizeof(ngx_mail_listen_t));
335 imls->addr = u.addr.in_addr;
337 imls->family = u.family;
340 for (m = 0; ngx_modules[m]; m++) {
341 if (ngx_modules[m]->type != NGX_MAIL_MODULE) {
345 module = ngx_modules[m]->ctx;
347 if (module->protocol == NULL) {
351 for (i = 0; module->protocol->port[i]; i++) {
352 if (module->protocol->port[i] == u.port) {
353 cscf->protocol = module->protocol;
359 for (i = 2; i < cf->args->nelts; i++) {
361 if (ngx_strcmp(value[i].data, "bind") == 0) {
366 if (ngx_strcmp(value[i].data, "ssl") == 0) {
371 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
372 "the \"ssl\" parameter requires "
373 "ngx_mail_ssl_module");
374 return NGX_CONF_ERROR;
378 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
379 "the invalid \"%V\" parameter", &value[i]);
380 return NGX_CONF_ERROR;
388 ngx_mail_core_protocol(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
390 ngx_mail_core_srv_conf_t *cscf = conf;
394 ngx_mail_module_t *module;
396 value = cf->args->elts;
398 for (m = 0; ngx_modules[m]; m++) {
399 if (ngx_modules[m]->type != NGX_MAIL_MODULE) {
403 module = ngx_modules[m]->ctx;
406 && ngx_strcmp(module->protocol->name.data, value[1].data) == 0)
408 cscf->protocol = module->protocol;
414 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
415 "unknown protocol \"%V\"", &value[1]);
416 return NGX_CONF_ERROR;
421 ngx_mail_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
423 ngx_mail_core_srv_conf_t *cscf = conf;
428 value = cf->args->elts;
430 if (cscf->resolver != NGX_CONF_UNSET_PTR) {
431 return "is duplicate";
434 if (ngx_strcmp(value[1].data, "off") == 0) {
435 cscf->resolver = NULL;
439 ngx_memzero(&u, sizeof(ngx_url_t));
444 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
445 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V: %s", &u.host, u.err);
446 return NGX_CONF_ERROR;
449 cscf->resolver = ngx_resolver_create(cf, &u.addrs[0]);
450 if (cscf->resolver == NULL) {
459 ngx_mail_capabilities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
463 ngx_str_t *c, *value;
467 a = (ngx_array_t *) (p + cmd->offset);
469 value = cf->args->elts;
471 for (i = 1; i < cf->args->nelts; i++) {
472 c = ngx_array_push(a);
474 return NGX_CONF_ERROR;