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