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