blob: 1a401bc970c395836e3c36e68c048a7d425d8a60 [file] [log] [blame]
Igor Sysoev02c8d182007-03-19 13:36:56 +00001
2/*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7#ifndef _NGX_MAIL_H_INCLUDED_
8#define _NGX_MAIL_H_INCLUDED_
9
10
11#include <ngx_config.h>
12#include <ngx_core.h>
13#include <ngx_event.h>
14#include <ngx_event_connect.h>
15
16#if (NGX_MAIL_SSL)
17#include <ngx_mail_ssl_module.h>
18#endif
19
20
21
22typedef struct {
23 void **main_conf;
24 void **srv_conf;
25} ngx_mail_conf_ctx_t;
26
27
28typedef struct {
29 in_addr_t addr;
30 in_port_t port;
31 int family;
32
33 /* server ctx */
34 ngx_mail_conf_ctx_t *ctx;
35
36 unsigned bind:1;
37} ngx_mail_listen_t;
38
39
40typedef struct {
41 in_addr_t addr;
42 ngx_mail_conf_ctx_t *ctx;
43 ngx_str_t addr_text;
44} ngx_mail_in_addr_t;
45
46
47typedef struct {
48 ngx_mail_in_addr_t *addrs; /* array of ngx_mail_in_addr_t */
49 ngx_uint_t naddrs;
50} ngx_mail_in_port_t;
51
52
53typedef struct {
54 in_port_t port;
55 ngx_array_t addrs; /* array of ngx_mail_conf_in_addr_t */
56} ngx_mail_conf_in_port_t;
57
58
59typedef struct {
60 in_addr_t addr;
61 ngx_mail_conf_ctx_t *ctx;
62 unsigned bind:1;
63} ngx_mail_conf_in_addr_t;
64
65
66typedef struct {
67 ngx_array_t servers; /* ngx_mail_core_srv_conf_t */
68 ngx_array_t listen; /* ngx_mail_listen_t */
69} ngx_mail_core_main_conf_t;
70
71
72#define NGX_MAIL_POP3_PROTOCOL 0
73#define NGX_MAIL_IMAP_PROTOCOL 1
74#define NGX_MAIL_SMTP_PROTOCOL 2
75
Igor Sysoev48714082007-09-15 16:51:16 +000076
77typedef struct ngx_mail_protocol_s ngx_mail_protocol_t;
78
79
Igor Sysoev02c8d182007-03-19 13:36:56 +000080typedef struct {
Igor Sysoev48714082007-09-15 16:51:16 +000081 ngx_mail_protocol_t *protocol;
82
Igor Sysoev02c8d182007-03-19 13:36:56 +000083 ngx_msec_t timeout;
Igor Sysoev02c8d182007-03-19 13:36:56 +000084
85 ngx_flag_t so_keepalive;
86
Igor Sysoev02c8d182007-03-19 13:36:56 +000087 ngx_str_t server_name;
Igor Sysoev02c8d182007-03-19 13:36:56 +000088
Igor Sysoev48714082007-09-15 16:51:16 +000089 u_char *file_name;
90 ngx_int_t line;
Igor Sysoev02c8d182007-03-19 13:36:56 +000091
92 /* server ctx */
93 ngx_mail_conf_ctx_t *ctx;
94} ngx_mail_core_srv_conf_t;
95
96
Igor Sysoev02c8d182007-03-19 13:36:56 +000097typedef enum {
98 ngx_pop3_start = 0,
99 ngx_pop3_user,
100 ngx_pop3_passwd,
101 ngx_pop3_auth_login_username,
102 ngx_pop3_auth_login_password,
103 ngx_pop3_auth_plain,
104 ngx_pop3_auth_cram_md5
105} ngx_po3_state_e;
106
107
108typedef enum {
109 ngx_imap_start = 0,
Igor Sysoevf4283a92007-07-20 19:38:08 +0000110 ngx_imap_auth_login_username,
111 ngx_imap_auth_login_password,
112 ngx_imap_auth_plain,
113 ngx_imap_auth_cram_md5,
Igor Sysoev02c8d182007-03-19 13:36:56 +0000114 ngx_imap_login,
115 ngx_imap_user,
116 ngx_imap_passwd
117} ngx_imap_state_e;
118
119
120typedef enum {
121 ngx_smtp_start = 0,
122 ngx_smtp_auth_login_username,
123 ngx_smtp_auth_login_password,
124 ngx_smtp_auth_plain,
125 ngx_smtp_auth_cram_md5,
126 ngx_smtp_helo,
127 ngx_smtp_noxclient,
128 ngx_smtp_xclient
129} ngx_smtp_state_e;
130
131
132typedef struct {
133 ngx_peer_connection_t upstream;
134 ngx_buf_t *buffer;
135} ngx_mail_proxy_ctx_t;
136
137
138typedef struct {
139 uint32_t signature; /* "MAIL" */
140
141 ngx_connection_t *connection;
142
143 ngx_str_t out;
144 ngx_buf_t *buffer;
145
146 void **ctx;
147 void **main_conf;
148 void **srv_conf;
149
150 ngx_mail_proxy_ctx_t *proxy;
151
152 ngx_uint_t mail_state;
153
Igor Sysoev48714082007-09-15 16:51:16 +0000154 unsigned protocol:3;
Igor Sysoev02c8d182007-03-19 13:36:56 +0000155 unsigned blocked:1;
156 unsigned quit:1;
Igor Sysoev02c8d182007-03-19 13:36:56 +0000157 unsigned quoted:1;
158 unsigned backslash:1;
159 unsigned no_sync_literal:1;
160 unsigned starttls:1;
161 unsigned esmtp:1;
162 unsigned auth_method:2;
163 unsigned auth_wait:1;
164
165 ngx_str_t login;
166 ngx_str_t passwd;
167
168 ngx_str_t salt;
169 ngx_str_t tag;
170 ngx_str_t tagged_line;
Igor Sysoev6cc54cc2007-09-13 20:13:18 +0000171 ngx_str_t text;
Igor Sysoev02c8d182007-03-19 13:36:56 +0000172
173 ngx_str_t *addr_text;
174 ngx_str_t smtp_helo;
175
176 ngx_uint_t command;
177 ngx_array_t args;
178
179 ngx_uint_t login_attempt;
180
Igor Sysoev48714082007-09-15 16:51:16 +0000181 /* used to parse POP3/IMAP/SMTP command */
Igor Sysoev02c8d182007-03-19 13:36:56 +0000182
183 ngx_uint_t state;
184 u_char *cmd_start;
185 u_char *arg_start;
186 u_char *arg_end;
187 ngx_uint_t literal_len;
188} ngx_mail_session_t;
189
190
191typedef struct {
192 ngx_str_t *client;
193 ngx_mail_session_t *session;
194} ngx_mail_log_ctx_t;
195
196
Igor Sysoevf4283a92007-07-20 19:38:08 +0000197#define NGX_POP3_USER 1
198#define NGX_POP3_PASS 2
199#define NGX_POP3_CAPA 3
200#define NGX_POP3_QUIT 4
201#define NGX_POP3_NOOP 5
202#define NGX_POP3_STLS 6
203#define NGX_POP3_APOP 7
204#define NGX_POP3_AUTH 8
205#define NGX_POP3_STAT 9
206#define NGX_POP3_LIST 10
207#define NGX_POP3_RETR 11
208#define NGX_POP3_DELE 12
209#define NGX_POP3_RSET 13
210#define NGX_POP3_TOP 14
211#define NGX_POP3_UIDL 15
Igor Sysoev02c8d182007-03-19 13:36:56 +0000212
213
Igor Sysoevf4283a92007-07-20 19:38:08 +0000214#define NGX_IMAP_LOGIN 1
215#define NGX_IMAP_LOGOUT 2
216#define NGX_IMAP_CAPABILITY 3
217#define NGX_IMAP_NOOP 4
218#define NGX_IMAP_STARTTLS 5
Igor Sysoev02c8d182007-03-19 13:36:56 +0000219
Igor Sysoevf4283a92007-07-20 19:38:08 +0000220#define NGX_IMAP_NEXT 6
221
222#define NGX_IMAP_AUTHENTICATE 7
Igor Sysoev02c8d182007-03-19 13:36:56 +0000223
224
Igor Sysoevf4283a92007-07-20 19:38:08 +0000225#define NGX_SMTP_HELO 1
226#define NGX_SMTP_EHLO 2
227#define NGX_SMTP_AUTH 3
228#define NGX_SMTP_QUIT 4
229#define NGX_SMTP_NOOP 5
230#define NGX_SMTP_MAIL 6
231#define NGX_SMTP_RSET 7
232#define NGX_SMTP_RCPT 8
233#define NGX_SMTP_DATA 9
234#define NGX_SMTP_VRFY 10
235#define NGX_SMTP_EXPN 11
236#define NGX_SMTP_HELP 12
237#define NGX_SMTP_STARTTLS 13
Igor Sysoev02c8d182007-03-19 13:36:56 +0000238
239
240#define NGX_MAIL_AUTH_PLAIN 0
241#define NGX_MAIL_AUTH_LOGIN 1
242#define NGX_MAIL_AUTH_APOP 2
243#define NGX_MAIL_AUTH_CRAM_MD5 3
244
245
246#define NGX_MAIL_AUTH_PLAIN_ENABLED 0x0002
247#define NGX_MAIL_AUTH_LOGIN_ENABLED 0x0004
248#define NGX_MAIL_AUTH_APOP_ENABLED 0x0008
249#define NGX_MAIL_AUTH_CRAM_MD5_ENABLED 0x0010
250
251
252#define NGX_MAIL_PARSE_INVALID_COMMAND 20
253
254
Igor Sysoev48714082007-09-15 16:51:16 +0000255typedef void (*ngx_mail_init_session_pt)(ngx_mail_session_t *s,
256 ngx_connection_t *c);
257typedef void (*ngx_mail_init_protocol_pt)(ngx_event_t *rev);
258typedef void (*ngx_mail_auth_state_pt)(ngx_event_t *rev);
259typedef ngx_int_t (*ngx_mail_parse_command_pt)(ngx_mail_session_t *s);
Igor Sysoev02c8d182007-03-19 13:36:56 +0000260
Igor Sysoev48714082007-09-15 16:51:16 +0000261
262struct ngx_mail_protocol_s {
263 ngx_str_t name;
264 in_port_t port[4];
265 ngx_uint_t type;
266
267 ngx_mail_init_session_pt init_session;
268 ngx_mail_init_protocol_pt init_protocol;
269 ngx_mail_parse_command_pt parse_command;
270 ngx_mail_auth_state_pt auth_state;
271
272 ngx_str_t internal_server_error;
273};
274
275
276typedef struct {
277 ngx_mail_protocol_t *protocol;
278
279 void *(*create_main_conf)(ngx_conf_t *cf);
280 char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
281
282 void *(*create_srv_conf)(ngx_conf_t *cf);
283 char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
284 void *conf);
285} ngx_mail_module_t;
286
287
288#define NGX_MAIL_MODULE 0x4C49414D /* "MAIL" */
289
290#define NGX_MAIL_MAIN_CONF 0x02000000
291#define NGX_MAIL_SRV_CONF 0x04000000
Igor Sysoev02c8d182007-03-19 13:36:56 +0000292
293
294#define NGX_MAIL_MAIN_CONF_OFFSET offsetof(ngx_mail_conf_ctx_t, main_conf)
295#define NGX_MAIL_SRV_CONF_OFFSET offsetof(ngx_mail_conf_ctx_t, srv_conf)
296
297
298#define ngx_mail_get_module_ctx(s, module) (s)->ctx[module.ctx_index]
299#define ngx_mail_set_ctx(s, c, module) s->ctx[module.ctx_index] = c;
300#define ngx_mail_delete_ctx(s, module) s->ctx[module.ctx_index] = NULL;
301
302
303#define ngx_mail_get_module_main_conf(s, module) \
304 (s)->main_conf[module.ctx_index]
305#define ngx_mail_get_module_srv_conf(s, module) (s)->srv_conf[module.ctx_index]
306
307#define ngx_mail_conf_get_module_main_conf(cf, module) \
308 ((ngx_mail_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
Igor Sysoev810dc162007-07-20 18:36:04 +0000309#define ngx_mail_conf_get_module_srv_conf(cf, module) \
310 ((ngx_mail_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index]
Igor Sysoev02c8d182007-03-19 13:36:56 +0000311
Igor Sysoev6cc54cc2007-09-13 20:13:18 +0000312
313#if (NGX_MAIL_SSL)
314void ngx_mail_starttls_handler(ngx_event_t *rev);
Igor Sysoev5fbe1212007-09-13 21:24:27 +0000315ngx_int_t ngx_mail_starttls_only(ngx_mail_session_t *s, ngx_connection_t *c);
Igor Sysoev6cc54cc2007-09-13 20:13:18 +0000316#endif
317
Igor Sysoev02c8d182007-03-19 13:36:56 +0000318
319void ngx_mail_init_connection(ngx_connection_t *c);
Igor Sysoev6cc54cc2007-09-13 20:13:18 +0000320
Igor Sysoev6cc54cc2007-09-13 20:13:18 +0000321ngx_int_t ngx_mail_salt(ngx_mail_session_t *s, ngx_connection_t *c,
322 ngx_mail_core_srv_conf_t *cscf);
323ngx_int_t ngx_mail_auth_plain(ngx_mail_session_t *s, ngx_connection_t *c,
324 ngx_uint_t n);
325ngx_int_t ngx_mail_auth_login_username(ngx_mail_session_t *s,
326 ngx_connection_t *c);
327ngx_int_t ngx_mail_auth_login_password(ngx_mail_session_t *s,
328 ngx_connection_t *c);
Igor Sysoev5fbe1212007-09-13 21:24:27 +0000329ngx_int_t ngx_mail_auth_cram_md5_salt(ngx_mail_session_t *s,
330 ngx_connection_t *c, char *prefix, size_t len);
Igor Sysoev6cc54cc2007-09-13 20:13:18 +0000331ngx_int_t ngx_mail_auth_cram_md5(ngx_mail_session_t *s, ngx_connection_t *c);
Igor Sysoev48714082007-09-15 16:51:16 +0000332ngx_int_t ngx_mail_auth_parse(ngx_mail_session_t *s, ngx_connection_t *c);
Igor Sysoev6cc54cc2007-09-13 20:13:18 +0000333
334void ngx_mail_send(ngx_event_t *wev);
Igor Sysoev24229502007-09-14 14:04:24 +0000335ngx_int_t ngx_mail_read_command(ngx_mail_session_t *s, ngx_connection_t *c);
336void ngx_mail_auth(ngx_mail_session_t *s, ngx_connection_t *c);
Igor Sysoev02c8d182007-03-19 13:36:56 +0000337void ngx_mail_close_connection(ngx_connection_t *c);
338void ngx_mail_session_internal_server_error(ngx_mail_session_t *s);
Igor Sysoev6cc54cc2007-09-13 20:13:18 +0000339u_char *ngx_mail_log_error(ngx_log_t *log, u_char *buf, size_t len);
340
341
Igor Sysoev48714082007-09-15 16:51:16 +0000342char *ngx_mail_capabilities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
Igor Sysoev02c8d182007-03-19 13:36:56 +0000343
344
345/* STUB */
346void ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_peer_addr_t *peer);
347void ngx_mail_auth_http_init(ngx_mail_session_t *s);
348/**/
349
350
351extern ngx_uint_t ngx_mail_max_module;
352extern ngx_module_t ngx_mail_core_module;
353
354
355#endif /* _NGX_MAIL_H_INCLUDED_ */