src/mail/ngx_mail.h - nginx

Data types defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #ifndef _NGX_MAIL_H_INCLUDED_
  6. #define _NGX_MAIL_H_INCLUDED_


  7. #include <ngx_config.h>
  8. #include <ngx_core.h>
  9. #include <ngx_event.h>
  10. #include <ngx_event_connect.h>

  11. #if (NGX_MAIL_SSL)
  12. #include <ngx_mail_ssl_module.h>
  13. #endif



  14. typedef struct {
  15.     void                  **main_conf;
  16.     void                  **srv_conf;
  17. } ngx_mail_conf_ctx_t;


  18. typedef struct {
  19.     struct sockaddr        *sockaddr;
  20.     socklen_t               socklen;
  21.     ngx_str_t               addr_text;

  22.     /* server ctx */
  23.     ngx_mail_conf_ctx_t    *ctx;

  24.     unsigned                bind:1;
  25.     unsigned                wildcard:1;
  26.     unsigned                ssl:1;
  27. #if (NGX_HAVE_INET6)
  28.     unsigned                ipv6only:1;
  29. #endif
  30.     unsigned                so_keepalive:2;
  31.     unsigned                proxy_protocol:1;
  32. #if (NGX_HAVE_KEEPALIVE_TUNABLE)
  33.     int                     tcp_keepidle;
  34.     int                     tcp_keepintvl;
  35.     int                     tcp_keepcnt;
  36. #endif
  37.     int                     protocol;
  38.     int                     backlog;
  39.     int                     rcvbuf;
  40.     int                     sndbuf;
  41. } ngx_mail_listen_t;


  42. typedef struct {
  43.     ngx_mail_conf_ctx_t    *ctx;
  44.     ngx_str_t               addr_text;
  45.     unsigned                ssl:1;
  46.     unsigned                proxy_protocol:1;
  47. } ngx_mail_addr_conf_t;

  48. typedef struct {
  49.     in_addr_t               addr;
  50.     ngx_mail_addr_conf_t    conf;
  51. } ngx_mail_in_addr_t;


  52. #if (NGX_HAVE_INET6)

  53. typedef struct {
  54.     struct in6_addr         addr6;
  55.     ngx_mail_addr_conf_t    conf;
  56. } ngx_mail_in6_addr_t;

  57. #endif


  58. typedef struct {
  59.     /* ngx_mail_in_addr_t or ngx_mail_in6_addr_t */
  60.     void                   *addrs;
  61.     ngx_uint_t              naddrs;
  62. } ngx_mail_port_t;


  63. typedef struct {
  64.     int                     family;
  65.     in_port_t               port;
  66.     ngx_array_t             addrs;       /* array of ngx_mail_conf_addr_t */
  67. } ngx_mail_conf_port_t;


  68. typedef struct {
  69.     ngx_mail_listen_t       opt;
  70. } ngx_mail_conf_addr_t;


  71. typedef struct {
  72.     ngx_array_t             servers;     /* ngx_mail_core_srv_conf_t */
  73.     ngx_array_t             listen;      /* ngx_mail_listen_t */
  74. } ngx_mail_core_main_conf_t;


  75. #define NGX_MAIL_POP3_PROTOCOL  0
  76. #define NGX_MAIL_IMAP_PROTOCOL  1
  77. #define NGX_MAIL_SMTP_PROTOCOL  2


  78. typedef struct ngx_mail_protocol_s  ngx_mail_protocol_t;


  79. typedef struct {
  80.     ngx_mail_protocol_t    *protocol;

  81.     ngx_msec_t              timeout;
  82.     ngx_msec_t              resolver_timeout;

  83.     ngx_uint_t              max_errors;

  84.     ngx_str_t               server_name;

  85.     u_char                 *file_name;
  86.     ngx_uint_t              line;

  87.     ngx_resolver_t         *resolver;
  88.     ngx_log_t              *error_log;

  89.     /* server ctx */
  90.     ngx_mail_conf_ctx_t    *ctx;

  91.     ngx_uint_t              listen;  /* unsigned  listen:1; */
  92. } ngx_mail_core_srv_conf_t;


  93. typedef enum {
  94.     ngx_pop3_start = 0,
  95.     ngx_pop3_user,
  96.     ngx_pop3_passwd,
  97.     ngx_pop3_auth_login_username,
  98.     ngx_pop3_auth_login_password,
  99.     ngx_pop3_auth_plain,
  100.     ngx_pop3_auth_cram_md5,
  101.     ngx_pop3_auth_external
  102. } ngx_pop3_state_e;


  103. typedef enum {
  104.     ngx_imap_start = 0,
  105.     ngx_imap_auth_login_username,
  106.     ngx_imap_auth_login_password,
  107.     ngx_imap_auth_plain,
  108.     ngx_imap_auth_cram_md5,
  109.     ngx_imap_auth_external,
  110.     ngx_imap_login,
  111.     ngx_imap_user,
  112.     ngx_imap_passwd
  113. } ngx_imap_state_e;


  114. typedef enum {
  115.     ngx_smtp_start = 0,
  116.     ngx_smtp_auth_login_username,
  117.     ngx_smtp_auth_login_password,
  118.     ngx_smtp_auth_plain,
  119.     ngx_smtp_auth_cram_md5,
  120.     ngx_smtp_auth_external,
  121.     ngx_smtp_helo,
  122.     ngx_smtp_helo_xclient,
  123.     ngx_smtp_helo_auth,
  124.     ngx_smtp_helo_from,
  125.     ngx_smtp_xclient,
  126.     ngx_smtp_xclient_from,
  127.     ngx_smtp_xclient_helo,
  128.     ngx_smtp_xclient_auth,
  129.     ngx_smtp_from,
  130.     ngx_smtp_to
  131. } ngx_smtp_state_e;


  132. typedef struct {
  133.     ngx_peer_connection_t   upstream;
  134.     ngx_buf_t              *buffer;
  135.     ngx_uint_t              proxy_protocol;  /* unsigned  proxy_protocol:1; */
  136. } ngx_mail_proxy_ctx_t;


  137. typedef struct {
  138.     uint32_t                signature;         /* "MAIL" */

  139.     ngx_connection_t       *connection;

  140.     ngx_str_t               out;
  141.     ngx_buf_t              *buffer;

  142.     void                  **ctx;
  143.     void                  **main_conf;
  144.     void                  **srv_conf;

  145.     ngx_resolver_ctx_t     *resolver_ctx;

  146.     ngx_mail_proxy_ctx_t   *proxy;

  147.     ngx_uint_t              mail_state;

  148.     unsigned                ssl:1;
  149.     unsigned                protocol:3;
  150.     unsigned                blocked:1;
  151.     unsigned                quit:1;
  152.     unsigned                quoted:1;
  153.     unsigned                backslash:1;
  154.     unsigned                no_sync_literal:1;
  155.     unsigned                starttls:1;
  156.     unsigned                esmtp:1;
  157.     unsigned                auth_method:3;
  158.     unsigned                auth_wait:1;

  159.     ngx_str_t               login;
  160.     ngx_str_t               passwd;

  161.     ngx_str_t               salt;
  162.     ngx_str_t               tag;
  163.     ngx_str_t               tagged_line;
  164.     ngx_str_t               text;

  165.     ngx_str_t              *addr_text;
  166.     ngx_str_t               host;
  167.     ngx_str_t               smtp_helo;
  168.     ngx_str_t               smtp_from;
  169.     ngx_str_t               smtp_to;

  170.     ngx_str_t               cmd;

  171.     ngx_uint_t              command;
  172.     ngx_array_t             args;

  173.     ngx_uint_t              errors;
  174.     ngx_uint_t              login_attempt;

  175.     /* used to parse POP3/IMAP/SMTP command */

  176.     ngx_uint_t              state;
  177.     u_char                 *tag_start;
  178.     u_char                 *cmd_start;
  179.     u_char                 *arg_start;
  180.     ngx_uint_t              literal_len;
  181. } ngx_mail_session_t;


  182. typedef struct {
  183.     ngx_str_t              *client;
  184.     ngx_mail_session_t     *session;
  185. } ngx_mail_log_ctx_t;


  186. #define NGX_POP3_USER          1
  187. #define NGX_POP3_PASS          2
  188. #define NGX_POP3_CAPA          3
  189. #define NGX_POP3_QUIT          4
  190. #define NGX_POP3_NOOP          5
  191. #define NGX_POP3_STLS          6
  192. #define NGX_POP3_APOP          7
  193. #define NGX_POP3_AUTH          8
  194. #define NGX_POP3_STAT          9
  195. #define NGX_POP3_LIST          10
  196. #define NGX_POP3_RETR          11
  197. #define NGX_POP3_DELE          12
  198. #define NGX_POP3_RSET          13
  199. #define NGX_POP3_TOP           14
  200. #define NGX_POP3_UIDL          15


  201. #define NGX_IMAP_LOGIN         1
  202. #define NGX_IMAP_LOGOUT        2
  203. #define NGX_IMAP_CAPABILITY    3
  204. #define NGX_IMAP_NOOP          4
  205. #define NGX_IMAP_STARTTLS      5

  206. #define NGX_IMAP_NEXT          6

  207. #define NGX_IMAP_AUTHENTICATE  7


  208. #define NGX_SMTP_HELO          1
  209. #define NGX_SMTP_EHLO          2
  210. #define NGX_SMTP_AUTH          3
  211. #define NGX_SMTP_QUIT          4
  212. #define NGX_SMTP_NOOP          5
  213. #define NGX_SMTP_MAIL          6
  214. #define NGX_SMTP_RSET          7
  215. #define NGX_SMTP_RCPT          8
  216. #define NGX_SMTP_DATA          9
  217. #define NGX_SMTP_VRFY          10
  218. #define NGX_SMTP_EXPN          11
  219. #define NGX_SMTP_HELP          12
  220. #define NGX_SMTP_STARTTLS      13


  221. #define NGX_MAIL_AUTH_PLAIN             0
  222. #define NGX_MAIL_AUTH_LOGIN             1
  223. #define NGX_MAIL_AUTH_LOGIN_USERNAME    2
  224. #define NGX_MAIL_AUTH_APOP              3
  225. #define NGX_MAIL_AUTH_CRAM_MD5          4
  226. #define NGX_MAIL_AUTH_EXTERNAL          5
  227. #define NGX_MAIL_AUTH_NONE              6


  228. #define NGX_MAIL_AUTH_PLAIN_ENABLED     0x0002
  229. #define NGX_MAIL_AUTH_LOGIN_ENABLED     0x0004
  230. #define NGX_MAIL_AUTH_APOP_ENABLED      0x0008
  231. #define NGX_MAIL_AUTH_CRAM_MD5_ENABLED  0x0010
  232. #define NGX_MAIL_AUTH_EXTERNAL_ENABLED  0x0020
  233. #define NGX_MAIL_AUTH_NONE_ENABLED      0x0040


  234. #define NGX_MAIL_PARSE_INVALID_COMMAND  20


  235. typedef void (*ngx_mail_init_session_pt)(ngx_mail_session_t *s,
  236.     ngx_connection_t *c);
  237. typedef void (*ngx_mail_init_protocol_pt)(ngx_event_t *rev);
  238. typedef void (*ngx_mail_auth_state_pt)(ngx_event_t *rev);
  239. typedef ngx_int_t (*ngx_mail_parse_command_pt)(ngx_mail_session_t *s);


  240. struct ngx_mail_protocol_s {
  241.     ngx_str_t                   name;
  242.     ngx_str_t                   alpn;
  243.     in_port_t                   port[4];
  244.     ngx_uint_t                  type;

  245.     ngx_mail_init_session_pt    init_session;
  246.     ngx_mail_init_protocol_pt   init_protocol;
  247.     ngx_mail_parse_command_pt   parse_command;
  248.     ngx_mail_auth_state_pt      auth_state;

  249.     ngx_str_t                   internal_server_error;
  250.     ngx_str_t                   cert_error;
  251.     ngx_str_t                   no_cert;
  252. };


  253. typedef struct {
  254.     ngx_mail_protocol_t        *protocol;

  255.     void                       *(*create_main_conf)(ngx_conf_t *cf);
  256.     char                       *(*init_main_conf)(ngx_conf_t *cf, void *conf);

  257.     void                       *(*create_srv_conf)(ngx_conf_t *cf);
  258.     char                       *(*merge_srv_conf)(ngx_conf_t *cf, void *prev,
  259.                                                   void *conf);
  260. } ngx_mail_module_t;


  261. #define NGX_MAIL_MODULE         0x4C49414D     /* "MAIL" */

  262. #define NGX_MAIL_MAIN_CONF      0x02000000
  263. #define NGX_MAIL_SRV_CONF       0x04000000


  264. #define NGX_MAIL_MAIN_CONF_OFFSET  offsetof(ngx_mail_conf_ctx_t, main_conf)
  265. #define NGX_MAIL_SRV_CONF_OFFSET   offsetof(ngx_mail_conf_ctx_t, srv_conf)


  266. #define ngx_mail_get_module_ctx(s, module)     (s)->ctx[module.ctx_index]
  267. #define ngx_mail_set_ctx(s, c, module)         s->ctx[module.ctx_index] = c;
  268. #define ngx_mail_delete_ctx(s, module)         s->ctx[module.ctx_index] = NULL;


  269. #define ngx_mail_get_module_main_conf(s, module)                             \
  270.     (s)->main_conf[module.ctx_index]
  271. #define ngx_mail_get_module_srv_conf(s, module)  (s)->srv_conf[module.ctx_index]

  272. #define ngx_mail_conf_get_module_main_conf(cf, module)                       \
  273.     ((ngx_mail_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index]
  274. #define ngx_mail_conf_get_module_srv_conf(cf, module)                        \
  275.     ((ngx_mail_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index]


  276. #if (NGX_MAIL_SSL)
  277. void ngx_mail_starttls_handler(ngx_event_t *rev);
  278. ngx_int_t ngx_mail_starttls_only(ngx_mail_session_t *s, ngx_connection_t *c);
  279. #endif


  280. void ngx_mail_init_connection(ngx_connection_t *c);

  281. ngx_int_t ngx_mail_salt(ngx_mail_session_t *s, ngx_connection_t *c,
  282.     ngx_mail_core_srv_conf_t *cscf);
  283. ngx_int_t ngx_mail_auth_plain(ngx_mail_session_t *s, ngx_connection_t *c,
  284.     ngx_uint_t n);
  285. ngx_int_t ngx_mail_auth_login_username(ngx_mail_session_t *s,
  286.     ngx_connection_t *c, ngx_uint_t n);
  287. ngx_int_t ngx_mail_auth_login_password(ngx_mail_session_t *s,
  288.     ngx_connection_t *c);
  289. ngx_int_t ngx_mail_auth_cram_md5_salt(ngx_mail_session_t *s,
  290.     ngx_connection_t *c, char *prefix, size_t len);
  291. ngx_int_t ngx_mail_auth_cram_md5(ngx_mail_session_t *s, ngx_connection_t *c);
  292. ngx_int_t ngx_mail_auth_external(ngx_mail_session_t *s, ngx_connection_t *c,
  293.     ngx_uint_t n);
  294. ngx_int_t ngx_mail_auth_parse(ngx_mail_session_t *s, ngx_connection_t *c);

  295. void ngx_mail_send(ngx_event_t *wev);
  296. ngx_int_t ngx_mail_read_command(ngx_mail_session_t *s, ngx_connection_t *c);
  297. void ngx_mail_auth(ngx_mail_session_t *s, ngx_connection_t *c);
  298. void ngx_mail_close_connection(ngx_connection_t *c);
  299. void ngx_mail_session_internal_server_error(ngx_mail_session_t *s);
  300. u_char *ngx_mail_log_error(ngx_log_t *log, u_char *buf, size_t len);


  301. char *ngx_mail_capabilities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);


  302. /* STUB */
  303. void ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_addr_t *peer);
  304. void ngx_mail_auth_http_init(ngx_mail_session_t *s);
  305. ngx_int_t ngx_mail_realip_handler(ngx_mail_session_t *s);
  306. /**/


  307. extern ngx_uint_t    ngx_mail_max_module;
  308. extern ngx_module_t  ngx_mail_core_module;


  309. #endif /* _NGX_MAIL_H_INCLUDED_ */