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