3 * Copyright (C) Igor Sysoev
7 #ifndef _NGX_MAIL_H_INCLUDED_
8 #define _NGX_MAIL_H_INCLUDED_
11 #include <ngx_config.h>
13 #include <ngx_event.h>
14 #include <ngx_event_connect.h>
17 #include <ngx_mail_ssl_module.h>
25 } ngx_mail_conf_ctx_t;
34 ngx_mail_conf_ctx_t *ctx;
45 ngx_mail_conf_ctx_t *ctx;
48 ngx_uint_t ssl; /* unsigned ssl:1; */
54 ngx_mail_in_addr_t *addrs; /* array of ngx_mail_in_addr_t */
61 ngx_array_t addrs; /* array of ngx_mail_conf_in_addr_t */
62 } ngx_mail_conf_in_port_t;
67 ngx_mail_conf_ctx_t *ctx;
72 } ngx_mail_conf_in_addr_t;
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;
81 #define NGX_MAIL_POP3_PROTOCOL 0
82 #define NGX_MAIL_IMAP_PROTOCOL 1
83 #define NGX_MAIL_SMTP_PROTOCOL 2
86 typedef struct ngx_mail_protocol_s ngx_mail_protocol_t;
90 ngx_mail_protocol_t *protocol;
93 ngx_msec_t resolver_timeout;
95 ngx_flag_t so_keepalive;
97 ngx_str_t server_name;
102 ngx_resolver_t *resolver;
105 ngx_mail_conf_ctx_t *ctx;
106 } ngx_mail_core_srv_conf_t;
113 ngx_pop3_auth_login_username,
114 ngx_pop3_auth_login_password,
116 ngx_pop3_auth_cram_md5
122 ngx_imap_auth_login_username,
123 ngx_imap_auth_login_password,
125 ngx_imap_auth_cram_md5,
134 ngx_smtp_auth_login_username,
135 ngx_smtp_auth_login_password,
137 ngx_smtp_auth_cram_md5,
139 ngx_smtp_helo_xclient,
142 ngx_smtp_xclient_from,
143 ngx_smtp_xclient_helo,
150 ngx_peer_connection_t upstream;
152 } ngx_mail_proxy_ctx_t;
156 uint32_t signature; /* "MAIL" */
158 ngx_connection_t *connection;
167 ngx_resolver_ctx_t *resolver_ctx;
169 ngx_mail_proxy_ctx_t *proxy;
171 ngx_uint_t mail_state;
177 unsigned backslash:1;
178 unsigned no_sync_literal:1;
181 unsigned auth_method:3;
182 unsigned auth_wait:1;
189 ngx_str_t tagged_line;
192 ngx_str_t *addr_text;
201 ngx_uint_t login_attempt;
203 /* used to parse POP3/IMAP/SMTP command */
209 ngx_uint_t literal_len;
210 } ngx_mail_session_t;
215 ngx_mail_session_t *session;
216 } ngx_mail_log_ctx_t;
219 #define NGX_POP3_USER 1
220 #define NGX_POP3_PASS 2
221 #define NGX_POP3_CAPA 3
222 #define NGX_POP3_QUIT 4
223 #define NGX_POP3_NOOP 5
224 #define NGX_POP3_STLS 6
225 #define NGX_POP3_APOP 7
226 #define NGX_POP3_AUTH 8
227 #define NGX_POP3_STAT 9
228 #define NGX_POP3_LIST 10
229 #define NGX_POP3_RETR 11
230 #define NGX_POP3_DELE 12
231 #define NGX_POP3_RSET 13
232 #define NGX_POP3_TOP 14
233 #define NGX_POP3_UIDL 15
236 #define NGX_IMAP_LOGIN 1
237 #define NGX_IMAP_LOGOUT 2
238 #define NGX_IMAP_CAPABILITY 3
239 #define NGX_IMAP_NOOP 4
240 #define NGX_IMAP_STARTTLS 5
242 #define NGX_IMAP_NEXT 6
244 #define NGX_IMAP_AUTHENTICATE 7
247 #define NGX_SMTP_HELO 1
248 #define NGX_SMTP_EHLO 2
249 #define NGX_SMTP_AUTH 3
250 #define NGX_SMTP_QUIT 4
251 #define NGX_SMTP_NOOP 5
252 #define NGX_SMTP_MAIL 6
253 #define NGX_SMTP_RSET 7
254 #define NGX_SMTP_RCPT 8
255 #define NGX_SMTP_DATA 9
256 #define NGX_SMTP_VRFY 10
257 #define NGX_SMTP_EXPN 11
258 #define NGX_SMTP_HELP 12
259 #define NGX_SMTP_STARTTLS 13
262 #define NGX_MAIL_AUTH_PLAIN 0
263 #define NGX_MAIL_AUTH_LOGIN 1
264 #define NGX_MAIL_AUTH_LOGIN_USERNAME 2
265 #define NGX_MAIL_AUTH_APOP 3
266 #define NGX_MAIL_AUTH_CRAM_MD5 4
267 #define NGX_MAIL_AUTH_NONE 5
270 #define NGX_MAIL_AUTH_PLAIN_ENABLED 0x0002
271 #define NGX_MAIL_AUTH_LOGIN_ENABLED 0x0004
272 #define NGX_MAIL_AUTH_APOP_ENABLED 0x0008
273 #define NGX_MAIL_AUTH_CRAM_MD5_ENABLED 0x0010
274 #define NGX_MAIL_AUTH_NONE_ENABLED 0x0020
277 #define NGX_MAIL_PARSE_INVALID_COMMAND 20
280 typedef void (*ngx_mail_init_session_pt)(ngx_mail_session_t *s,
281 ngx_connection_t *c);
282 typedef void (*ngx_mail_init_protocol_pt)(ngx_event_t *rev);
283 typedef void (*ngx_mail_auth_state_pt)(ngx_event_t *rev);
284 typedef ngx_int_t (*ngx_mail_parse_command_pt)(ngx_mail_session_t *s);
287 struct ngx_mail_protocol_s {
292 ngx_mail_init_session_pt init_session;
293 ngx_mail_init_protocol_pt init_protocol;
294 ngx_mail_parse_command_pt parse_command;
295 ngx_mail_auth_state_pt auth_state;
297 ngx_str_t internal_server_error;
302 ngx_mail_protocol_t *protocol;
304 void *(*create_main_conf)(ngx_conf_t *cf);
305 char *(*init_main_conf)(ngx_conf_t *cf, void *conf);
307 void *(*create_srv_conf)(ngx_conf_t *cf);
308 char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
313 #define NGX_MAIL_MODULE 0x4C49414D /* "MAIL" */
315 #define NGX_MAIL_MAIN_CONF 0x02000000
316 #define NGX_MAIL_SRV_CONF 0x04000000
319 #define NGX_MAIL_MAIN_CONF_OFFSET offsetof(ngx_mail_conf_ctx_t, main_conf)
320 #define NGX_MAIL_SRV_CONF_OFFSET offsetof(ngx_mail_conf_ctx_t, srv_conf)
323 #define ngx_mail_get_module_ctx(s, module) (s)->ctx[module.ctx_index]
324 #define ngx_mail_set_ctx(s, c, module) s->ctx[module.ctx_index] = c;
325 #define ngx_mail_delete_ctx(s, module) s->ctx[module.ctx_index] = NULL;
328 #define ngx_mail_get_module_main_conf(s, module) \
329 (s)->main_conf[module.ctx_index]
330 #define ngx_mail_get_module_srv_conf(s, module) (s)->srv_conf[module.ctx_index]
332 #define ngx_mail_conf_get_module_main_conf(cf, module) \
333 ((ngx_mail_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
334 #define ngx_mail_conf_get_module_srv_conf(cf, module) \
335 ((ngx_mail_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index]
339 void ngx_mail_starttls_handler(ngx_event_t *rev);
340 ngx_int_t ngx_mail_starttls_only(ngx_mail_session_t *s, ngx_connection_t *c);
344 void ngx_mail_init_connection(ngx_connection_t *c);
346 ngx_int_t ngx_mail_salt(ngx_mail_session_t *s, ngx_connection_t *c,
347 ngx_mail_core_srv_conf_t *cscf);
348 ngx_int_t ngx_mail_auth_plain(ngx_mail_session_t *s, ngx_connection_t *c,
350 ngx_int_t ngx_mail_auth_login_username(ngx_mail_session_t *s,
351 ngx_connection_t *c, ngx_uint_t n);
352 ngx_int_t ngx_mail_auth_login_password(ngx_mail_session_t *s,
353 ngx_connection_t *c);
354 ngx_int_t ngx_mail_auth_cram_md5_salt(ngx_mail_session_t *s,
355 ngx_connection_t *c, char *prefix, size_t len);
356 ngx_int_t ngx_mail_auth_cram_md5(ngx_mail_session_t *s, ngx_connection_t *c);
357 ngx_int_t ngx_mail_auth_parse(ngx_mail_session_t *s, ngx_connection_t *c);
359 void ngx_mail_send(ngx_event_t *wev);
360 ngx_int_t ngx_mail_read_command(ngx_mail_session_t *s, ngx_connection_t *c);
361 void ngx_mail_auth(ngx_mail_session_t *s, ngx_connection_t *c);
362 void ngx_mail_close_connection(ngx_connection_t *c);
363 void ngx_mail_session_internal_server_error(ngx_mail_session_t *s);
364 u_char *ngx_mail_log_error(ngx_log_t *log, u_char *buf, size_t len);
367 char *ngx_mail_capabilities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
371 void ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_peer_addr_t *peer);
372 void ngx_mail_auth_http_init(ngx_mail_session_t *s);
376 extern ngx_uint_t ngx_mail_max_module;
377 extern ngx_module_t ngx_mail_core_module;
380 #endif /* _NGX_MAIL_H_INCLUDED_ */