Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #ifndef _NGX_MAIL_H_INCLUDED_
0009 #define _NGX_MAIL_H_INCLUDED_
0010 
0011 
0012 #include <ngx_config.h>
0013 #include <ngx_core.h>
0014 #include <ngx_event.h>
0015 #include <ngx_event_connect.h>
0016 
0017 #if (NGX_MAIL_SSL)
0018 #include <ngx_mail_ssl_module.h>
0019 #endif
0020 
0021 
0022 
0023 typedef struct {
0024     void                  **main_conf;
0025     void                  **srv_conf;
0026 } ngx_mail_conf_ctx_t;
0027 
0028 
0029 typedef struct {
0030     ngx_sockaddr_t          sockaddr;
0031     socklen_t               socklen;
0032 
0033     /* server ctx */
0034     ngx_mail_conf_ctx_t    *ctx;
0035 
0036     unsigned                bind:1;
0037     unsigned                wildcard:1;
0038     unsigned                ssl:1;
0039 #if (NGX_HAVE_INET6)
0040     unsigned                ipv6only:1;
0041 #endif
0042     unsigned                so_keepalive:2;
0043 #if (NGX_HAVE_KEEPALIVE_TUNABLE)
0044     int                     tcp_keepidle;
0045     int                     tcp_keepintvl;
0046     int                     tcp_keepcnt;
0047 #endif
0048     int                     backlog;
0049 } ngx_mail_listen_t;
0050 
0051 
0052 typedef struct {
0053     ngx_mail_conf_ctx_t    *ctx;
0054     ngx_str_t               addr_text;
0055     ngx_uint_t              ssl;    /* unsigned   ssl:1; */
0056 } ngx_mail_addr_conf_t;
0057 
0058 typedef struct {
0059     in_addr_t               addr;
0060     ngx_mail_addr_conf_t    conf;
0061 } ngx_mail_in_addr_t;
0062 
0063 
0064 #if (NGX_HAVE_INET6)
0065 
0066 typedef struct {
0067     struct in6_addr         addr6;
0068     ngx_mail_addr_conf_t    conf;
0069 } ngx_mail_in6_addr_t;
0070 
0071 #endif
0072 
0073 
0074 typedef struct {
0075     /* ngx_mail_in_addr_t or ngx_mail_in6_addr_t */
0076     void                   *addrs;
0077     ngx_uint_t              naddrs;
0078 } ngx_mail_port_t;
0079 
0080 
0081 typedef struct {
0082     int                     family;
0083     in_port_t               port;
0084     ngx_array_t             addrs;       /* array of ngx_mail_conf_addr_t */
0085 } ngx_mail_conf_port_t;
0086 
0087 
0088 typedef struct {
0089     ngx_mail_listen_t       opt;
0090 } ngx_mail_conf_addr_t;
0091 
0092 
0093 typedef struct {
0094     ngx_array_t             servers;     /* ngx_mail_core_srv_conf_t */
0095     ngx_array_t             listen;      /* ngx_mail_listen_t */
0096 } ngx_mail_core_main_conf_t;
0097 
0098 
0099 #define NGX_MAIL_POP3_PROTOCOL  0
0100 #define NGX_MAIL_IMAP_PROTOCOL  1
0101 #define NGX_MAIL_SMTP_PROTOCOL  2
0102 
0103 
0104 typedef struct ngx_mail_protocol_s  ngx_mail_protocol_t;
0105 
0106 
0107 typedef struct {
0108     ngx_mail_protocol_t    *protocol;
0109 
0110     ngx_msec_t              timeout;
0111     ngx_msec_t              resolver_timeout;
0112 
0113     ngx_str_t               server_name;
0114 
0115     u_char                 *file_name;
0116     ngx_uint_t              line;
0117 
0118     ngx_resolver_t         *resolver;
0119     ngx_log_t              *error_log;
0120 
0121     /* server ctx */
0122     ngx_mail_conf_ctx_t    *ctx;
0123 
0124     ngx_uint_t              listen;  /* unsigned  listen:1; */
0125 } ngx_mail_core_srv_conf_t;
0126 
0127 
0128 typedef enum {
0129     ngx_pop3_start = 0,
0130     ngx_pop3_user,
0131     ngx_pop3_passwd,
0132     ngx_pop3_auth_login_username,
0133     ngx_pop3_auth_login_password,
0134     ngx_pop3_auth_plain,
0135     ngx_pop3_auth_cram_md5,
0136     ngx_pop3_auth_external
0137 } ngx_pop3_state_e;
0138 
0139 
0140 typedef enum {
0141     ngx_imap_start = 0,
0142     ngx_imap_auth_login_username,
0143     ngx_imap_auth_login_password,
0144     ngx_imap_auth_plain,
0145     ngx_imap_auth_cram_md5,
0146     ngx_imap_auth_external,
0147     ngx_imap_login,
0148     ngx_imap_user,
0149     ngx_imap_passwd
0150 } ngx_imap_state_e;
0151 
0152 
0153 typedef enum {
0154     ngx_smtp_start = 0,
0155     ngx_smtp_auth_login_username,
0156     ngx_smtp_auth_login_password,
0157     ngx_smtp_auth_plain,
0158     ngx_smtp_auth_cram_md5,
0159     ngx_smtp_auth_external,
0160     ngx_smtp_helo,
0161     ngx_smtp_helo_xclient,
0162     ngx_smtp_helo_from,
0163     ngx_smtp_xclient,
0164     ngx_smtp_xclient_from,
0165     ngx_smtp_xclient_helo,
0166     ngx_smtp_from,
0167     ngx_smtp_to
0168 } ngx_smtp_state_e;
0169 
0170 
0171 typedef struct {
0172     ngx_peer_connection_t   upstream;
0173     ngx_buf_t              *buffer;
0174 } ngx_mail_proxy_ctx_t;
0175 
0176 
0177 typedef struct {
0178     uint32_t                signature;         /* "MAIL" */
0179 
0180     ngx_connection_t       *connection;
0181 
0182     ngx_str_t               out;
0183     ngx_buf_t              *buffer;
0184 
0185     void                  **ctx;
0186     void                  **main_conf;
0187     void                  **srv_conf;
0188 
0189     ngx_resolver_ctx_t     *resolver_ctx;
0190 
0191     ngx_mail_proxy_ctx_t   *proxy;
0192 
0193     ngx_uint_t              mail_state;
0194 
0195     unsigned                protocol:3;
0196     unsigned                blocked:1;
0197     unsigned                quit:1;
0198     unsigned                quoted:1;
0199     unsigned                backslash:1;
0200     unsigned                no_sync_literal:1;
0201     unsigned                starttls:1;
0202     unsigned                esmtp:1;
0203     unsigned                auth_method:3;
0204     unsigned                auth_wait:1;
0205 
0206     ngx_str_t               login;
0207     ngx_str_t               passwd;
0208 
0209     ngx_str_t               salt;
0210     ngx_str_t               tag;
0211     ngx_str_t               tagged_line;
0212     ngx_str_t               text;
0213 
0214     ngx_str_t              *addr_text;
0215     ngx_str_t               host;
0216     ngx_str_t               smtp_helo;
0217     ngx_str_t               smtp_from;
0218     ngx_str_t               smtp_to;
0219 
0220     ngx_str_t               cmd;
0221 
0222     ngx_uint_t              command;
0223     ngx_array_t             args;
0224 
0225     ngx_uint_t              login_attempt;
0226 
0227     /* used to parse POP3/IMAP/SMTP command */
0228 
0229     ngx_uint_t              state;
0230     u_char                 *cmd_start;
0231     u_char                 *arg_start;
0232     u_char                 *arg_end;
0233     ngx_uint_t              literal_len;
0234 } ngx_mail_session_t;
0235 
0236 
0237 typedef struct {
0238     ngx_str_t              *client;
0239     ngx_mail_session_t     *session;
0240 } ngx_mail_log_ctx_t;
0241 
0242 
0243 #define NGX_POP3_USER          1
0244 #define NGX_POP3_PASS          2
0245 #define NGX_POP3_CAPA          3
0246 #define NGX_POP3_QUIT          4
0247 #define NGX_POP3_NOOP          5
0248 #define NGX_POP3_STLS          6
0249 #define NGX_POP3_APOP          7
0250 #define NGX_POP3_AUTH          8
0251 #define NGX_POP3_STAT          9
0252 #define NGX_POP3_LIST          10
0253 #define NGX_POP3_RETR          11
0254 #define NGX_POP3_DELE          12
0255 #define NGX_POP3_RSET          13
0256 #define NGX_POP3_TOP           14
0257 #define NGX_POP3_UIDL          15
0258 
0259 
0260 #define NGX_IMAP_LOGIN         1
0261 #define NGX_IMAP_LOGOUT        2
0262 #define NGX_IMAP_CAPABILITY    3
0263 #define NGX_IMAP_NOOP          4
0264 #define NGX_IMAP_STARTTLS      5
0265 
0266 #define NGX_IMAP_NEXT          6
0267 
0268 #define NGX_IMAP_AUTHENTICATE  7
0269 
0270 
0271 #define NGX_SMTP_HELO          1
0272 #define NGX_SMTP_EHLO          2
0273 #define NGX_SMTP_AUTH          3
0274 #define NGX_SMTP_QUIT          4
0275 #define NGX_SMTP_NOOP          5
0276 #define NGX_SMTP_MAIL          6
0277 #define NGX_SMTP_RSET          7
0278 #define NGX_SMTP_RCPT          8
0279 #define NGX_SMTP_DATA          9
0280 #define NGX_SMTP_VRFY          10
0281 #define NGX_SMTP_EXPN          11
0282 #define NGX_SMTP_HELP          12
0283 #define NGX_SMTP_STARTTLS      13
0284 
0285 
0286 #define NGX_MAIL_AUTH_PLAIN             0
0287 #define NGX_MAIL_AUTH_LOGIN             1
0288 #define NGX_MAIL_AUTH_LOGIN_USERNAME    2
0289 #define NGX_MAIL_AUTH_APOP              3
0290 #define NGX_MAIL_AUTH_CRAM_MD5          4
0291 #define NGX_MAIL_AUTH_EXTERNAL          5
0292 #define NGX_MAIL_AUTH_NONE              6
0293 
0294 
0295 #define NGX_MAIL_AUTH_PLAIN_ENABLED     0x0002
0296 #define NGX_MAIL_AUTH_LOGIN_ENABLED     0x0004
0297 #define NGX_MAIL_AUTH_APOP_ENABLED      0x0008
0298 #define NGX_MAIL_AUTH_CRAM_MD5_ENABLED  0x0010
0299 #define NGX_MAIL_AUTH_EXTERNAL_ENABLED  0x0020
0300 #define NGX_MAIL_AUTH_NONE_ENABLED      0x0040
0301 
0302 
0303 #define NGX_MAIL_PARSE_INVALID_COMMAND  20
0304 
0305 
0306 typedef void (*ngx_mail_init_session_pt)(ngx_mail_session_t *s,
0307     ngx_connection_t *c);
0308 typedef void (*ngx_mail_init_protocol_pt)(ngx_event_t *rev);
0309 typedef void (*ngx_mail_auth_state_pt)(ngx_event_t *rev);
0310 typedef ngx_int_t (*ngx_mail_parse_command_pt)(ngx_mail_session_t *s);
0311 
0312 
0313 struct ngx_mail_protocol_s {
0314     ngx_str_t                   name;
0315     in_port_t                   port[4];
0316     ngx_uint_t                  type;
0317 
0318     ngx_mail_init_session_pt    init_session;
0319     ngx_mail_init_protocol_pt   init_protocol;
0320     ngx_mail_parse_command_pt   parse_command;
0321     ngx_mail_auth_state_pt      auth_state;
0322 
0323     ngx_str_t                   internal_server_error;
0324     ngx_str_t                   cert_error;
0325     ngx_str_t                   no_cert;
0326 };
0327 
0328 
0329 typedef struct {
0330     ngx_mail_protocol_t        *protocol;
0331 
0332     void                       *(*create_main_conf)(ngx_conf_t *cf);
0333     char                       *(*init_main_conf)(ngx_conf_t *cf, void *conf);
0334 
0335     void                       *(*create_srv_conf)(ngx_conf_t *cf);
0336     char                       *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
0337                                                   void *conf);
0338 } ngx_mail_module_t;
0339 
0340 
0341 #define NGX_MAIL_MODULE         0x4C49414D     /* "MAIL" */
0342 
0343 #define NGX_MAIL_MAIN_CONF      0x02000000
0344 #define NGX_MAIL_SRV_CONF       0x04000000
0345 
0346 
0347 #define NGX_MAIL_MAIN_CONF_OFFSET  offsetof(ngx_mail_conf_ctx_t, main_conf)
0348 #define NGX_MAIL_SRV_CONF_OFFSET   offsetof(ngx_mail_conf_ctx_t, srv_conf)
0349 
0350 
0351 #define ngx_mail_get_module_ctx(s, module)     (s)->ctx[module.ctx_index]
0352 #define ngx_mail_set_ctx(s, c, module)         s->ctx[module.ctx_index] = c;
0353 #define ngx_mail_delete_ctx(s, module)         s->ctx[module.ctx_index] = NULL;
0354 
0355 
0356 #define ngx_mail_get_module_main_conf(s, module)                             \
0357     (s)->main_conf[module.ctx_index]
0358 #define ngx_mail_get_module_srv_conf(s, module)  (s)->srv_conf[module.ctx_index]
0359 
0360 #define ngx_mail_conf_get_module_main_conf(cf, module)                       \
0361     ((ngx_mail_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
0362 #define ngx_mail_conf_get_module_srv_conf(cf, module)                        \
0363     ((ngx_mail_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index]
0364 
0365 
0366 #if (NGX_MAIL_SSL)
0367 void ngx_mail_starttls_handler(ngx_event_t *rev);
0368 ngx_int_t ngx_mail_starttls_only(ngx_mail_session_t *s, ngx_connection_t *c);
0369 #endif
0370 
0371 
0372 void ngx_mail_init_connection(ngx_connection_t *c);
0373 
0374 ngx_int_t ngx_mail_salt(ngx_mail_session_t *s, ngx_connection_t *c,
0375     ngx_mail_core_srv_conf_t *cscf);
0376 ngx_int_t ngx_mail_auth_plain(ngx_mail_session_t *s, ngx_connection_t *c,
0377     ngx_uint_t n);
0378 ngx_int_t ngx_mail_auth_login_username(ngx_mail_session_t *s,
0379     ngx_connection_t *c, ngx_uint_t n);
0380 ngx_int_t ngx_mail_auth_login_password(ngx_mail_session_t *s,
0381     ngx_connection_t *c);
0382 ngx_int_t ngx_mail_auth_cram_md5_salt(ngx_mail_session_t *s,
0383     ngx_connection_t *c, char *prefix, size_t len);
0384 ngx_int_t ngx_mail_auth_cram_md5(ngx_mail_session_t *s, ngx_connection_t *c);
0385 ngx_int_t ngx_mail_auth_external(ngx_mail_session_t *s, ngx_connection_t *c,
0386     ngx_uint_t n);
0387 ngx_int_t ngx_mail_auth_parse(ngx_mail_session_t *s, ngx_connection_t *c);
0388 
0389 void ngx_mail_send(ngx_event_t *wev);
0390 ngx_int_t ngx_mail_read_command(ngx_mail_session_t *s, ngx_connection_t *c);
0391 void ngx_mail_auth(ngx_mail_session_t *s, ngx_connection_t *c);
0392 void ngx_mail_close_connection(ngx_connection_t *c);
0393 void ngx_mail_session_internal_server_error(ngx_mail_session_t *s);
0394 u_char *ngx_mail_log_error(ngx_log_t *log, u_char *buf, size_t len);
0395 
0396 
0397 char *ngx_mail_capabilities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
0398 
0399 
0400 /* STUB */
0401 void ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_addr_t *peer);
0402 void ngx_mail_auth_http_init(ngx_mail_session_t *s);
0403 /**/
0404 
0405 
0406 extern ngx_uint_t    ngx_mail_max_module;
0407 extern ngx_module_t  ngx_mail_core_module;
0408 
0409 
0410 #endif /* _NGX_MAIL_H_INCLUDED_ */