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