1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#ifndef _NGX_MAIL_H_INCLUDED_
9#define _NGX_MAIL_H_INCLUDED_
10
11
12#include <ngx_config.h>
13#include <ngx_core.h>
14#include <ngx_event.h>
15#include <ngx_event_connect.h>
16
17#if (NGX_MAIL_SSL)
18#include <ngx_mail_ssl_module.h>
19#endif
20
21
22
23typedef struct {
24    void                  **main_conf;
25    void                  **srv_conf;
26} ngx_mail_conf_ctx_t;
27
28
29typedef struct {
30    ngx_sockaddr_t          sockaddr;
31    socklen_t               socklen;
32
33    /* server ctx */
34    ngx_mail_conf_ctx_t    *ctx;
35
36    unsigned                bind:1;
37    unsigned                wildcard:1;
38    unsigned                ssl:1;
39#if (NGX_HAVE_INET6)
40    unsigned                ipv6only:1;
41#endif
42    unsigned                so_keepalive:2;
43#if (NGX_HAVE_KEEPALIVE_TUNABLE)
44    int                     tcp_keepidle;
45    int                     tcp_keepintvl;
46    int                     tcp_keepcnt;
47#endif
48    int                     backlog;
49} ngx_mail_listen_t;
50
51
52typedef struct {
53    ngx_mail_conf_ctx_t    *ctx;
54    ngx_str_t               addr_text;
55    ngx_uint_t              ssl;    /* unsigned   ssl:1; */
56} ngx_mail_addr_conf_t;
57
58typedef struct {
59    in_addr_t               addr;
60    ngx_mail_addr_conf_t    conf;
61} ngx_mail_in_addr_t;
62
63
64#if (NGX_HAVE_INET6)
65
66typedef struct {
67    struct in6_addr         addr6;
68    ngx_mail_addr_conf_t    conf;
69} ngx_mail_in6_addr_t;
70
71#endif
72
73
74typedef struct {
75    /* ngx_mail_in_addr_t or ngx_mail_in6_addr_t */
76    void                   *addrs;
77    ngx_uint_t              naddrs;
78} ngx_mail_port_t;
79
80
81typedef struct {
82    int                     family;
83    in_port_t               port;
84    ngx_array_t             addrs;       /* array of ngx_mail_conf_addr_t */
85} ngx_mail_conf_port_t;
86
87
88typedef struct {
89    ngx_mail_listen_t       opt;
90} ngx_mail_conf_addr_t;
91
92
93typedef struct {
94    ngx_array_t             servers;     /* ngx_mail_core_srv_conf_t */
95    ngx_array_t             listen;      /* ngx_mail_listen_t */
96} ngx_mail_core_main_conf_t;
97
98
99#define NGX_MAIL_POP3_PROTOCOL  0
100#define NGX_MAIL_IMAP_PROTOCOL  1
101#define NGX_MAIL_SMTP_PROTOCOL  2
102
103
104typedef struct ngx_mail_protocol_s  ngx_mail_protocol_t;
105
106
107typedef struct {
108    ngx_mail_protocol_t    *protocol;
109
110    ngx_msec_t              timeout;
111    ngx_msec_t              resolver_timeout;
112
113    ngx_str_t               server_name;
114
115    u_char                 *file_name;
116    ngx_uint_t              line;
117
118    ngx_resolver_t         *resolver;
119    ngx_log_t              *error_log;
120
121    /* server ctx */
122    ngx_mail_conf_ctx_t    *ctx;
123
124    ngx_uint_t              listen;  /* unsigned  listen:1; */
125} ngx_mail_core_srv_conf_t;
126
127
128typedef enum {
129    ngx_pop3_start = 0,
130    ngx_pop3_user,
131    ngx_pop3_passwd,
132    ngx_pop3_auth_login_username,
133    ngx_pop3_auth_login_password,
134    ngx_pop3_auth_plain,
135    ngx_pop3_auth_cram_md5,
136    ngx_pop3_auth_external
137} ngx_pop3_state_e;
138
139
140typedef enum {
141    ngx_imap_start = 0,
142    ngx_imap_auth_login_username,
143    ngx_imap_auth_login_password,
144    ngx_imap_auth_plain,
145    ngx_imap_auth_cram_md5,
146    ngx_imap_auth_external,
147    ngx_imap_login,
148    ngx_imap_user,
149    ngx_imap_passwd
150} ngx_imap_state_e;
151
152
153typedef enum {
154    ngx_smtp_start = 0,
155    ngx_smtp_auth_login_username,
156    ngx_smtp_auth_login_password,
157    ngx_smtp_auth_plain,
158    ngx_smtp_auth_cram_md5,
159    ngx_smtp_auth_external,
160    ngx_smtp_helo,
161    ngx_smtp_helo_xclient,
162    ngx_smtp_helo_from,
163    ngx_smtp_xclient,
164    ngx_smtp_xclient_from,
165    ngx_smtp_xclient_helo,
166    ngx_smtp_from,
167    ngx_smtp_to
168} ngx_smtp_state_e;
169
170
171typedef struct {
172    ngx_peer_connection_t   upstream;
173    ngx_buf_t              *buffer;
174} ngx_mail_proxy_ctx_t;
175
176
177typedef struct {
178    uint32_t                signature;         /* "MAIL" */
179
180    ngx_connection_t       *connection;
181
182    ngx_str_t               out;
183    ngx_buf_t              *buffer;
184
185    void                  **ctx;
186    void                  **main_conf;
187    void                  **srv_conf;
188
189    ngx_resolver_ctx_t     *resolver_ctx;
190
191    ngx_mail_proxy_ctx_t   *proxy;
192
193    ngx_uint_t              mail_state;
194
195    unsigned                protocol:3;
196    unsigned                blocked:1;
197    unsigned                quit:1;
198    unsigned                quoted:1;
199    unsigned                backslash:1;
200    unsigned                no_sync_literal:1;
201    unsigned                starttls:1;
202    unsigned                esmtp:1;
203    unsigned                auth_method:3;
204    unsigned                auth_wait:1;
205
206    ngx_str_t               login;
207    ngx_str_t               passwd;
208
209    ngx_str_t               salt;
210    ngx_str_t               tag;
211    ngx_str_t               tagged_line;
212    ngx_str_t               text;
213
214    ngx_str_t              *addr_text;
215    ngx_str_t               host;
216    ngx_str_t               smtp_helo;
217    ngx_str_t               smtp_from;
218    ngx_str_t               smtp_to;
219
220    ngx_str_t               cmd;
221
222    ngx_uint_t              command;
223    ngx_array_t             args;
224
225    ngx_uint_t              login_attempt;
226
227    /* used to parse POP3/IMAP/SMTP command */
228
229    ngx_uint_t              state;
230    u_char                 *cmd_start;
231    u_char                 *arg_start;
232    u_char                 *arg_end;
233    ngx_uint_t              literal_len;
234} ngx_mail_session_t;
235
236
237typedef struct {
238    ngx_str_t              *client;
239    ngx_mail_session_t     *session;
240} ngx_mail_log_ctx_t;
241
242
243#define NGX_POP3_USER          1
244#define NGX_POP3_PASS          2
245#define NGX_POP3_CAPA          3
246#define NGX_POP3_QUIT          4
247#define NGX_POP3_NOOP          5
248#define NGX_POP3_STLS          6
249#define NGX_POP3_APOP          7
250#define NGX_POP3_AUTH          8
251#define NGX_POP3_STAT          9
252#define NGX_POP3_LIST          10
253#define NGX_POP3_RETR          11
254#define NGX_POP3_DELE          12
255#define NGX_POP3_RSET          13
256#define NGX_POP3_TOP           14
257#define NGX_POP3_UIDL          15
258
259
260#define NGX_IMAP_LOGIN         1
261#define NGX_IMAP_LOGOUT        2
262#define NGX_IMAP_CAPABILITY    3
263#define NGX_IMAP_NOOP          4
264#define NGX_IMAP_STARTTLS      5
265
266#define NGX_IMAP_NEXT          6
267
268#define NGX_IMAP_AUTHENTICATE  7
269
270
271#define NGX_SMTP_HELO          1
272#define NGX_SMTP_EHLO          2
273#define NGX_SMTP_AUTH          3
274#define NGX_SMTP_QUIT          4
275#define NGX_SMTP_NOOP          5
276#define NGX_SMTP_MAIL          6
277#define NGX_SMTP_RSET          7
278#define NGX_SMTP_RCPT          8
279#define NGX_SMTP_DATA          9
280#define NGX_SMTP_VRFY          10
281#define NGX_SMTP_EXPN          11
282#define NGX_SMTP_HELP          12
283#define NGX_SMTP_STARTTLS      13
284
285
286#define NGX_MAIL_AUTH_PLAIN             0
287#define NGX_MAIL_AUTH_LOGIN             1
288#define NGX_MAIL_AUTH_LOGIN_USERNAME    2
289#define NGX_MAIL_AUTH_APOP              3
290#define NGX_MAIL_AUTH_CRAM_MD5          4
291#define NGX_MAIL_AUTH_EXTERNAL          5
292#define NGX_MAIL_AUTH_NONE              6
293
294
295#define NGX_MAIL_AUTH_PLAIN_ENABLED     0x0002
296#define NGX_MAIL_AUTH_LOGIN_ENABLED     0x0004
297#define NGX_MAIL_AUTH_APOP_ENABLED      0x0008
298#define NGX_MAIL_AUTH_CRAM_MD5_ENABLED  0x0010
299#define NGX_MAIL_AUTH_EXTERNAL_ENABLED  0x0020
300#define NGX_MAIL_AUTH_NONE_ENABLED      0x0040
301
302
303#define NGX_MAIL_PARSE_INVALID_COMMAND  20
304
305
306typedef void (*ngx_mail_init_session_pt)(ngx_mail_session_t *s,
307    ngx_connection_t *c);
308typedef void (*ngx_mail_init_protocol_pt)(ngx_event_t *rev);
309typedef void (*ngx_mail_auth_state_pt)(ngx_event_t *rev);
310typedef ngx_int_t (*ngx_mail_parse_command_pt)(ngx_mail_session_t *s);
311
312
313struct ngx_mail_protocol_s {
314    ngx_str_t                   name;
315    in_port_t                   port[4];
316    ngx_uint_t                  type;
317
318    ngx_mail_init_session_pt    init_session;
319    ngx_mail_init_protocol_pt   init_protocol;
320    ngx_mail_parse_command_pt   parse_command;
321    ngx_mail_auth_state_pt      auth_state;
322
323    ngx_str_t                   internal_server_error;
324    ngx_str_t                   cert_error;
325    ngx_str_t                   no_cert;
326};
327
328
329typedef struct {
330    ngx_mail_protocol_t        *protocol;
331
332    void                       *(*create_main_conf)(ngx_conf_t *cf);
333    char                       *(*init_main_conf)(ngx_conf_t *cf, void *conf);
334
335    void                       *(*create_srv_conf)(ngx_conf_t *cf);
336    char                       *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
337                                                  void *conf);
338} ngx_mail_module_t;
339
340
341#define NGX_MAIL_MODULE         0x4C49414D     /* "MAIL" */
342
343#define NGX_MAIL_MAIN_CONF      0x02000000
344#define NGX_MAIL_SRV_CONF       0x04000000
345
346
347#define NGX_MAIL_MAIN_CONF_OFFSET  offsetof(ngx_mail_conf_ctx_t, main_conf)
348#define NGX_MAIL_SRV_CONF_OFFSET   offsetof(ngx_mail_conf_ctx_t, srv_conf)
349
350
351#define ngx_mail_get_module_ctx(s, module)     (s)->ctx[module.ctx_index]
352#define ngx_mail_set_ctx(s, c, module)         s->ctx[module.ctx_index] = c;
353#define ngx_mail_delete_ctx(s, module)         s->ctx[module.ctx_index] = NULL;
354
355
356#define ngx_mail_get_module_main_conf(s, module)                             \
357    (s)->main_conf[module.ctx_index]
358#define ngx_mail_get_module_srv_conf(s, module)  (s)->srv_conf[module.ctx_index]
359
360#define ngx_mail_conf_get_module_main_conf(cf, module)                       \
361    ((ngx_mail_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
362#define ngx_mail_conf_get_module_srv_conf(cf, module)                        \
363    ((ngx_mail_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index]
364
365
366#if (NGX_MAIL_SSL)
367void ngx_mail_starttls_handler(ngx_event_t *rev);
368ngx_int_t ngx_mail_starttls_only(ngx_mail_session_t *s, ngx_connection_t *c);
369#endif
370
371
372void ngx_mail_init_connection(ngx_connection_t *c);
373
374ngx_int_t ngx_mail_salt(ngx_mail_session_t *s, ngx_connection_t *c,
375    ngx_mail_core_srv_conf_t *cscf);
376ngx_int_t ngx_mail_auth_plain(ngx_mail_session_t *s, ngx_connection_t *c,
377    ngx_uint_t n);
378ngx_int_t ngx_mail_auth_login_username(ngx_mail_session_t *s,
379    ngx_connection_t *c, ngx_uint_t n);
380ngx_int_t ngx_mail_auth_login_password(ngx_mail_session_t *s,
381    ngx_connection_t *c);
382ngx_int_t ngx_mail_auth_cram_md5_salt(ngx_mail_session_t *s,
383    ngx_connection_t *c, char *prefix, size_t len);
384ngx_int_t ngx_mail_auth_cram_md5(ngx_mail_session_t *s, ngx_connection_t *c);
385ngx_int_t ngx_mail_auth_external(ngx_mail_session_t *s, ngx_connection_t *c,
386    ngx_uint_t n);
387ngx_int_t ngx_mail_auth_parse(ngx_mail_session_t *s, ngx_connection_t *c);
388
389void ngx_mail_send(ngx_event_t *wev);
390ngx_int_t ngx_mail_read_command(ngx_mail_session_t *s, ngx_connection_t *c);
391void ngx_mail_auth(ngx_mail_session_t *s, ngx_connection_t *c);
392void ngx_mail_close_connection(ngx_connection_t *c);
393void ngx_mail_session_internal_server_error(ngx_mail_session_t *s);
394u_char *ngx_mail_log_error(ngx_log_t *log, u_char *buf, size_t len);
395
396
397char *ngx_mail_capabilities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
398
399
400/* STUB */
401void ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_addr_t *peer);
402void ngx_mail_auth_http_init(ngx_mail_session_t *s);
403/**/
404
405
406extern ngx_uint_t    ngx_mail_max_module;
407extern ngx_module_t  ngx_mail_core_module;
408
409
410#endif /* _NGX_MAIL_H_INCLUDED_ */
411