1e18a033bSKonstantin Ananyev
2e18a033bSKonstantin Ananyev/*
3e18a033bSKonstantin Ananyev * Copyright (C) Igor Sysoev
4e18a033bSKonstantin Ananyev * Copyright (C) Nginx, Inc.
5e18a033bSKonstantin Ananyev */
6e18a033bSKonstantin Ananyev
7e18a033bSKonstantin Ananyev
8e18a033bSKonstantin Ananyev#include <ngx_config.h>
9e18a033bSKonstantin Ananyev#include <ngx_core.h>
10e18a033bSKonstantin Ananyev#include <ngx_event.h>
11e18a033bSKonstantin Ananyev#include <ngx_mail.h>
12e18a033bSKonstantin Ananyev#include <ngx_mail_smtp_module.h>
13e18a033bSKonstantin Ananyev
14e18a033bSKonstantin Ananyev
15e18a033bSKonstantin Ananyevstatic void *ngx_mail_smtp_create_srv_conf(ngx_conf_t *cf);
16e18a033bSKonstantin Ananyevstatic char *ngx_mail_smtp_merge_srv_conf(ngx_conf_t *cf, void *parent,
17e18a033bSKonstantin Ananyev    void *child);
18e18a033bSKonstantin Ananyev
19e18a033bSKonstantin Ananyev
20e18a033bSKonstantin Ananyevstatic ngx_conf_bitmask_t  ngx_mail_smtp_auth_methods[] = {
21e18a033bSKonstantin Ananyev    { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
22e18a033bSKonstantin Ananyev    { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
23e18a033bSKonstantin Ananyev    { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
24e18a033bSKonstantin Ananyev    { ngx_string("external"), NGX_MAIL_AUTH_EXTERNAL_ENABLED },
25e18a033bSKonstantin Ananyev    { ngx_string("none"), NGX_MAIL_AUTH_NONE_ENABLED },
26e18a033bSKonstantin Ananyev    { ngx_null_string, 0 }
27e18a033bSKonstantin Ananyev};
28e18a033bSKonstantin Ananyev
29e18a033bSKonstantin Ananyev
30e18a033bSKonstantin Ananyevstatic ngx_str_t  ngx_mail_smtp_auth_methods_names[] = {
31e18a033bSKonstantin Ananyev    ngx_string("PLAIN"),
32e18a033bSKonstantin Ananyev    ngx_string("LOGIN"),
33e18a033bSKonstantin Ananyev    ngx_null_string,  /* APOP */
34e18a033bSKonstantin Ananyev    ngx_string("CRAM-MD5"),
35e18a033bSKonstantin Ananyev    ngx_string("EXTERNAL"),
36e18a033bSKonstantin Ananyev    ngx_null_string   /* NONE */
37e18a033bSKonstantin Ananyev};
38e18a033bSKonstantin Ananyev
39e18a033bSKonstantin Ananyev
40e18a033bSKonstantin Ananyevstatic ngx_mail_protocol_t  ngx_mail_smtp_protocol = {
41e18a033bSKonstantin Ananyev    ngx_string("smtp"),
42e18a033bSKonstantin Ananyev    { 25, 465, 587, 0 },
43e18a033bSKonstantin Ananyev    NGX_MAIL_SMTP_PROTOCOL,
44e18a033bSKonstantin Ananyev
45e18a033bSKonstantin Ananyev    ngx_mail_smtp_init_session,
46e18a033bSKonstantin Ananyev    ngx_mail_smtp_init_protocol,
47e18a033bSKonstantin Ananyev    ngx_mail_smtp_parse_command,
48e18a033bSKonstantin Ananyev    ngx_mail_smtp_auth_state,
49e18a033bSKonstantin Ananyev
50e18a033bSKonstantin Ananyev    ngx_string("451 4.3.2 Internal server error" CRLF),
51e18a033bSKonstantin Ananyev    ngx_string("421 4.7.1 SSL certificate error" CRLF),
52e18a033bSKonstantin Ananyev    ngx_string("421 4.7.1 No required SSL certificate" CRLF)
53e18a033bSKonstantin Ananyev};
54e18a033bSKonstantin Ananyev
55e18a033bSKonstantin Ananyev
56e18a033bSKonstantin Ananyevstatic ngx_command_t  ngx_mail_smtp_commands[] = {
57e18a033bSKonstantin Ananyev
58e18a033bSKonstantin Ananyev    { ngx_string("smtp_client_buffer"),
59e18a033bSKonstantin Ananyev      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
60e18a033bSKonstantin Ananyev      ngx_conf_set_size_slot,
61e18a033bSKonstantin Ananyev      NGX_MAIL_SRV_CONF_OFFSET,
62e18a033bSKonstantin Ananyev      offsetof(ngx_mail_smtp_srv_conf_t, client_buffer_size),
63e18a033bSKonstantin Ananyev      NULL },
64e18a033bSKonstantin Ananyev
65e18a033bSKonstantin Ananyev    { ngx_string("smtp_greeting_delay"),
66e18a033bSKonstantin Ananyev      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
67e18a033bSKonstantin Ananyev      ngx_conf_set_msec_slot,
68e18a033bSKonstantin Ananyev      NGX_MAIL_SRV_CONF_OFFSET,
69e18a033bSKonstantin Ananyev      offsetof(ngx_mail_smtp_srv_conf_t, greeting_delay),
70e18a033bSKonstantin Ananyev      NULL },
71e18a033bSKonstantin Ananyev
72e18a033bSKonstantin Ananyev    { ngx_string("smtp_capabilities"),
73e18a033bSKonstantin Ananyev      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
74e18a033bSKonstantin Ananyev      ngx_mail_capabilities,
75e18a033bSKonstantin Ananyev      NGX_MAIL_SRV_CONF_OFFSET,
76e18a033bSKonstantin Ananyev      offsetof(ngx_mail_smtp_srv_conf_t, capabilities),
77e18a033bSKonstantin Ananyev      NULL },
78e18a033bSKonstantin Ananyev
79e18a033bSKonstantin Ananyev    { ngx_string("smtp_auth"),
80e18a033bSKonstantin Ananyev      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
81e18a033bSKonstantin Ananyev      ngx_conf_set_bitmask_slot,
82e18a033bSKonstantin Ananyev      NGX_MAIL_SRV_CONF_OFFSET,
83e18a033bSKonstantin Ananyev      offsetof(ngx_mail_smtp_srv_conf_t, auth_methods),
84e18a033bSKonstantin Ananyev      &ngx_mail_smtp_auth_methods },
85e18a033bSKonstantin Ananyev
86e18a033bSKonstantin Ananyev      ngx_null_command
87e18a033bSKonstantin Ananyev};
88e18a033bSKonstantin Ananyev
89e18a033bSKonstantin Ananyev
90e18a033bSKonstantin Ananyevstatic ngx_mail_module_t  ngx_mail_smtp_module_ctx = {
91e18a033bSKonstantin Ananyev    &ngx_mail_smtp_protocol,               /* protocol */
92e18a033bSKonstantin Ananyev
93e18a033bSKonstantin Ananyev    NULL,                                  /* create main configuration */
94e18a033bSKonstantin Ananyev    NULL,                                  /* init main configuration */
95e18a033bSKonstantin Ananyev
96e18a033bSKonstantin Ananyev    ngx_mail_smtp_create_srv_conf,         /* create server configuration */
97e18a033bSKonstantin Ananyev    ngx_mail_smtp_merge_srv_conf           /* merge server configuration */
98e18a033bSKonstantin Ananyev};
99e18a033bSKonstantin Ananyev
100e18a033bSKonstantin Ananyev
101e18a033bSKonstantin Ananyevngx_module_t  ngx_mail_smtp_module = {
102e18a033bSKonstantin Ananyev    NGX_MODULE_V1,
103e18a033bSKonstantin Ananyev    &ngx_mail_smtp_module_ctx,             /* module context */
104e18a033bSKonstantin Ananyev    ngx_mail_smtp_commands,                /* module directives */
105e18a033bSKonstantin Ananyev    NGX_MAIL_MODULE,                       /* module type */
106e18a033bSKonstantin Ananyev    NULL,                                  /* init master */
107e18a033bSKonstantin Ananyev    NULL,                                  /* init module */
108e18a033bSKonstantin Ananyev    NULL,                                  /* init process */
109e18a033bSKonstantin Ananyev    NULL,                                  /* init thread */
110e18a033bSKonstantin Ananyev    NULL,                                  /* exit thread */
111e18a033bSKonstantin Ananyev    NULL,                                  /* exit process */
112e18a033bSKonstantin Ananyev    NULL,                                  /* exit master */
113e18a033bSKonstantin Ananyev    NGX_MODULE_V1_PADDING
114e18a033bSKonstantin Ananyev};
115e18a033bSKonstantin Ananyev
116e18a033bSKonstantin Ananyev
117e18a033bSKonstantin Ananyevstatic void *
118e18a033bSKonstantin Ananyevngx_mail_smtp_create_srv_conf(ngx_conf_t *cf)
119e18a033bSKonstantin Ananyev{
120e18a033bSKonstantin Ananyev    ngx_mail_smtp_srv_conf_t  *sscf;
121e18a033bSKonstantin Ananyev
122e18a033bSKonstantin Ananyev    sscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_smtp_srv_conf_t));
123e18a033bSKonstantin Ananyev    if (sscf == NULL) {
124e18a033bSKonstantin Ananyev        return NULL;
125e18a033bSKonstantin Ananyev    }
126e18a033bSKonstantin Ananyev
127e18a033bSKonstantin Ananyev    sscf->client_buffer_size = NGX_CONF_UNSET_SIZE;
128e18a033bSKonstantin Ananyev    sscf->greeting_delay = NGX_CONF_UNSET_MSEC;
129e18a033bSKonstantin Ananyev
130e18a033bSKonstantin Ananyev    if (ngx_array_init(&sscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
131e18a033bSKonstantin Ananyev        != NGX_OK)
132e18a033bSKonstantin Ananyev    {
133e18a033bSKonstantin Ananyev        return NULL;
134e18a033bSKonstantin Ananyev    }
135e18a033bSKonstantin Ananyev
136e18a033bSKonstantin Ananyev    return sscf;
137e18a033bSKonstantin Ananyev}
138e18a033bSKonstantin Ananyev
139e18a033bSKonstantin Ananyev
140e18a033bSKonstantin Ananyevstatic char *
141e18a033bSKonstantin Ananyevngx_mail_smtp_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
142e18a033bSKonstantin Ananyev{
143e18a033bSKonstantin Ananyev    ngx_mail_smtp_srv_conf_t *prev = parent;
144e18a033bSKonstantin Ananyev    ngx_mail_smtp_srv_conf_t *conf = child;
145e18a033bSKonstantin Ananyev
146e18a033bSKonstantin Ananyev    u_char                    *p, *auth, *last;
147e18a033bSKonstantin Ananyev    size_t                     size;
148e18a033bSKonstantin Ananyev    ngx_str_t                 *c;
149e18a033bSKonstantin Ananyev    ngx_uint_t                 i, m, auth_enabled;
150e18a033bSKonstantin Ananyev    ngx_mail_core_srv_conf_t  *cscf;
151e18a033bSKonstantin Ananyev
152e18a033bSKonstantin Ananyev    ngx_conf_merge_size_value(conf->client_buffer_size,
153e18a033bSKonstantin Ananyev                              prev->client_buffer_size,
154e18a033bSKonstantin Ananyev                              (size_t) ngx_pagesize);
155e18a033bSKonstantin Ananyev
156e18a033bSKonstantin Ananyev    ngx_conf_merge_msec_value(conf->greeting_delay,
157e18a033bSKonstantin Ananyev                              prev->greeting_delay, 0);
158e18a033bSKonstantin Ananyev
159e18a033bSKonstantin Ananyev    ngx_conf_merge_bitmask_value(conf->auth_methods,
160e18a033bSKonstantin Ananyev                              prev->auth_methods,
161e18a033bSKonstantin Ananyev                              (NGX_CONF_BITMASK_SET
162e18a033bSKonstantin Ananyev                               |NGX_MAIL_AUTH_PLAIN_ENABLED
163e18a033bSKonstantin Ananyev                               |NGX_MAIL_AUTH_LOGIN_ENABLED));
164e18a033bSKonstantin Ananyev
165e18a033bSKonstantin Ananyev
166e18a033bSKonstantin Ananyev    cscf = ngx_mail_conf_get_module_srv_conf(cf, ngx_mail_core_module);
167e18a033bSKonstantin Ananyev
168e18a033bSKonstantin Ananyev    size = sizeof("220  ESMTP ready" CRLF) - 1 + cscf->server_name.len;
169e18a033bSKonstantin Ananyev
170e18a033bSKonstantin Ananyev    p = ngx_pnalloc(cf->pool, size);
171e18a033bSKonstantin Ananyev    if (p == NULL) {
172e18a033bSKonstantin Ananyev        return NGX_CONF_ERROR;
173e18a033bSKonstantin Ananyev    }
174e18a033bSKonstantin Ananyev
175e18a033bSKonstantin Ananyev    conf->greeting.len = size;
176e18a033bSKonstantin Ananyev    conf->greeting.data = p;
177e18a033bSKonstantin Ananyev
178e18a033bSKonstantin Ananyev    *p++ = '2'; *p++ = '2'; *p++ = '0'; *p++ = ' ';
179e18a033bSKonstantin Ananyev    p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
180e18a033bSKonstantin Ananyev    ngx_memcpy(p, " ESMTP ready" CRLF, sizeof(" ESMTP ready" CRLF) - 1);
181e18a033bSKonstantin Ananyev
182e18a033bSKonstantin Ananyev
183e18a033bSKonstantin Ananyev    size = sizeof("250 " CRLF) - 1 + cscf->server_name.len;
184e18a033bSKonstantin Ananyev
185e18a033bSKonstantin Ananyev    p = ngx_pnalloc(cf->pool, size);
186e18a033bSKonstantin Ananyev    if (p == NULL) {
187e18a033bSKonstantin Ananyev        return NGX_CONF_ERROR;
188e18a033bSKonstantin Ananyev    }
189e18a033bSKonstantin Ananyev
190e18a033bSKonstantin Ananyev    conf->server_name.len = size;
191e18a033bSKonstantin Ananyev    conf->server_name.data = p;
192e18a033bSKonstantin Ananyev
193e18a033bSKonstantin Ananyev    *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' ';
194e18a033bSKonstantin Ananyev    p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
195e18a033bSKonstantin Ananyev    *p++ = CR; *p = LF;
196e18a033bSKonstantin Ananyev
197e18a033bSKonstantin Ananyev
198e18a033bSKonstantin Ananyev    if (conf->capabilities.nelts == 0) {
199e18a033bSKonstantin Ananyev        conf->capabilities = prev->capabilities;
200e18a033bSKonstantin Ananyev    }
201e18a033bSKonstantin Ananyev
202e18a033bSKonstantin Ananyev    size = sizeof("250-") - 1 + cscf->server_name.len + sizeof(CRLF) - 1;
203e18a033bSKonstantin Ananyev
204e18a033bSKonstantin Ananyev    c = conf->capabilities.elts;
205e18a033bSKonstantin Ananyev    for (i = 0; i < conf->capabilities.nelts; i++) {
206e18a033bSKonstantin Ananyev        size += sizeof("250 ") - 1 + c[i].len + sizeof(CRLF) - 1;
207e18a033bSKonstantin Ananyev    }
208e18a033bSKonstantin Ananyev
209e18a033bSKonstantin Ananyev    auth_enabled = 0;
210e18a033bSKonstantin Ananyev
211e18a033bSKonstantin Ananyev    for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
212e18a033bSKonstantin Ananyev         m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
213e18a033bSKonstantin Ananyev         m <<= 1, i++)
214e18a033bSKonstantin Ananyev    {
215e18a033bSKonstantin Ananyev        if (m & conf->auth_methods) {
216e18a033bSKonstantin Ananyev            size += 1 + ngx_mail_smtp_auth_methods_names[i].len;
217e18a033bSKonstantin Ananyev            auth_enabled = 1;
218e18a033bSKonstantin Ananyev        }
219e18a033bSKonstantin Ananyev    }
220e18a033bSKonstantin Ananyev
221e18a033bSKonstantin Ananyev    if (auth_enabled) {
222e18a033bSKonstantin Ananyev        size += sizeof("250 AUTH") - 1 + sizeof(CRLF) - 1;
223e18a033bSKonstantin Ananyev    }
224e18a033bSKonstantin Ananyev
225e18a033bSKonstantin Ananyev    p = ngx_pnalloc(cf->pool, size);
226e18a033bSKonstantin Ananyev    if (p == NULL) {
227e18a033bSKonstantin Ananyev        return NGX_CONF_ERROR;
228e18a033bSKonstantin Ananyev    }
229e18a033bSKonstantin Ananyev
230e18a033bSKonstantin Ananyev    conf->capability.len = size;
231e18a033bSKonstantin Ananyev    conf->capability.data = p;
232e18a033bSKonstantin Ananyev
233e18a033bSKonstantin Ananyev    last = p;
234e18a033bSKonstantin Ananyev
235e18a033bSKonstantin Ananyev    *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-';
236e18a033bSKonstantin Ananyev    p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
237e18a033bSKonstantin Ananyev    *p++ = CR; *p++ = LF;
238e18a033bSKonstantin Ananyev
239e18a033bSKonstantin Ananyev    for (i = 0; i < conf->capabilities.nelts; i++) {
240e18a033bSKonstantin Ananyev        last = p;
241e18a033bSKonstantin Ananyev        *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-';
242e18a033bSKonstantin Ananyev        p = ngx_cpymem(p, c[i].data, c[i].len);
243e18a033bSKonstantin Ananyev        *p++ = CR; *p++ = LF;
244e18a033bSKonstantin Ananyev    }
245e18a033bSKonstantin Ananyev
246e18a033bSKonstantin Ananyev    auth = p;
247e18a033bSKonstantin Ananyev
248e18a033bSKonstantin Ananyev    if (auth_enabled) {
249e18a033bSKonstantin Ananyev        last = p;
250e18a033bSKonstantin Ananyev
251e18a033bSKonstantin Ananyev        *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' ';
252e18a033bSKonstantin Ananyev        *p++ = 'A'; *p++ = 'U'; *p++ = 'T'; *p++ = 'H';
253e18a033bSKonstantin Ananyev
254e18a033bSKonstantin Ananyev        for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
255e18a033bSKonstantin Ananyev             m <= NGX_MAIL_AUTH_EXTERNAL_ENABLED;
256e18a033bSKonstantin Ananyev             m <<= 1, i++)
257e18a033bSKonstantin Ananyev        {
258e18a033bSKonstantin Ananyev            if (m & conf->auth_methods) {
259e18a033bSKonstantin Ananyev                *p++ = ' ';
260e18a033bSKonstantin Ananyev                p = ngx_cpymem(p, ngx_mail_smtp_auth_methods_names[i].data,
261e18a033bSKonstantin Ananyev                               ngx_mail_smtp_auth_methods_names[i].len);
262e18a033bSKonstantin Ananyev            }
263e18a033bSKonstantin Ananyev        }
264e18a033bSKonstantin Ananyev
265e18a033bSKonstantin Ananyev        *p++ = CR; *p = LF;
266e18a033bSKonstantin Ananyev
267e18a033bSKonstantin Ananyev    } else {
268e18a033bSKonstantin Ananyev        last[3] = ' ';
269e18a033bSKonstantin Ananyev    }
270e18a033bSKonstantin Ananyev
271e18a033bSKonstantin Ananyev    size += sizeof("250 STARTTLS" CRLF) - 1;
272e18a033bSKonstantin Ananyev
273e18a033bSKonstantin Ananyev    p = ngx_pnalloc(cf->pool, size);
274e18a033bSKonstantin Ananyev    if (p == NULL) {
275e18a033bSKonstantin Ananyev        return NGX_CONF_ERROR;
276e18a033bSKonstantin Ananyev    }
277e18a033bSKonstantin Ananyev
278e18a033bSKonstantin Ananyev    conf->starttls_capability.len = size;
279e18a033bSKonstantin Ananyev    conf->starttls_capability.data = p;
280e18a033bSKonstantin Ananyev
281e18a033bSKonstantin Ananyev    p = ngx_cpymem(p, conf->capability.data, conf->capability.len);
282e18a033bSKonstantin Ananyev
283e18a033bSKonstantin Ananyev    ngx_memcpy(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1);
284e18a033bSKonstantin Ananyev
285e18a033bSKonstantin Ananyev    p = conf->starttls_capability.data
286e18a033bSKonstantin Ananyev        + (last - conf->capability.data) + 3;
287e18a033bSKonstantin Ananyev    *p = '-';
288e18a033bSKonstantin Ananyev
289e18a033bSKonstantin Ananyev    size = (auth - conf->capability.data)
290e18a033bSKonstantin Ananyev            + sizeof("250 STARTTLS" CRLF) - 1;
291e18a033bSKonstantin Ananyev
292e18a033bSKonstantin Ananyev    p = ngx_pnalloc(cf->pool, size);
293e18a033bSKonstantin Ananyev    if (p == NULL) {
294e18a033bSKonstantin Ananyev        return NGX_CONF_ERROR;
295e18a033bSKonstantin Ananyev    }
296e18a033bSKonstantin Ananyev
297e18a033bSKonstantin Ananyev    conf->starttls_only_capability.len = size;
298e18a033bSKonstantin Ananyev    conf->starttls_only_capability.data = p;
299e18a033bSKonstantin Ananyev
300e18a033bSKonstantin Ananyev    p = ngx_cpymem(p, conf->capability.data, auth - conf->capability.data);
301e18a033bSKonstantin Ananyev
302e18a033bSKonstantin Ananyev    ngx_memcpy(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1);
303e18a033bSKonstantin Ananyev
304e18a033bSKonstantin Ananyev    if (last < auth) {
305e18a033bSKonstantin Ananyev        p = conf->starttls_only_capability.data
306e18a033bSKonstantin Ananyev            + (last - conf->capability.data) + 3;
307e18a033bSKonstantin Ananyev        *p = '-';
308e18a033bSKonstantin Ananyev    }
309e18a033bSKonstantin Ananyev
310e18a033bSKonstantin Ananyev    return NGX_CONF_OK;
311e18a033bSKonstantin Ananyev}
312