Back to home page

Nginx displayed by LXR

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