src/mail/ngx_mail.h - nginx-1.7.10

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.     u_char                  sockaddr[NGX_SOCKADDRLEN];
  20.     socklen_t               socklen;

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

  23.     unsigned                bind:1;
  24.     unsigned                wildcard:1;
  25. #if (NGX_MAIL_SSL)
  26.     unsigned                ssl:1;
  27. #endif
  28. #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
  29.     unsigned                ipv6only:1;
  30. #endif
  31.     unsigned                so_keepalive:2;
  32. #if (NGX_HAVE_KEEPALIVE_TUNABLE)
  33.     int                     tcp_keepidle;
  34.     int                     tcp_keepintvl;
  35.     int                     tcp_keepcnt;
  36. #endif
  37. } ngx_mail_listen_t;


  38. typedef struct {
  39.     ngx_mail_conf_ctx_t    *ctx;
  40.     ngx_str_t               addr_text;
  41. #if (NGX_MAIL_SSL)
  42.     ngx_uint_t              ssl;    /* unsigned   ssl:1; */
  43. #endif
  44. } ngx_mail_addr_conf_t;

  45. typedef struct {
  46.     in_addr_t               addr;
  47.     ngx_mail_addr_conf_t    conf;
  48. } ngx_mail_in_addr_t;


  49. #if (NGX_HAVE_INET6)

  50. typedef struct {
  51.     struct in6_addr         addr6;
  52.     ngx_mail_addr_conf_t    conf;
  53. } ngx_mail_in6_addr_t;

  54. #endif


  55. typedef struct {
  56.     /* ngx_mail_in_addr_t or ngx_mail_in6_addr_t */
  57.     void                   *addrs;
  58.     ngx_uint_t              naddrs;
  59. } ngx_mail_port_t;


  60. typedef struct {
  61.     int                     family;
  62.     in_port_t               port;
  63.     ngx_array_t             addrs;       /* array of ngx_mail_conf_addr_t */
  64. } ngx_mail_conf_port_t;


  65. typedef struct {
  66.     struct sockaddr        *sockaddr;
  67.     socklen_t               socklen;

  68.     ngx_mail_conf_ctx_t    *ctx;

  69.     unsigned                bind:1;
  70.     unsigned                wildcard:1;
  71. #if (NGX_MAIL_SSL)
  72.     unsigned                ssl:1;
  73. #endif
  74. #if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
  75.     unsigned                ipv6only:1;
  76. #endif
  77.     unsigned                so_keepalive:2;
  78. #if (NGX_HAVE_KEEPALIVE_TUNABLE)
  79.     int                     tcp_keepidle;
  80.     int                     tcp_keepintvl;
  81.     int                     tcp_keepcnt;
  82. #endif
  83. } ngx_mail_conf_addr_t;


  84. typedef struct {
  85.     ngx_array_t             servers;     /* ngx_mail_core_srv_conf_t */
  86.     ngx_array_t             listen;      /* ngx_mail_listen_t */
  87. } ngx_mail_core_main_conf_t;


  88. #define NGX_MAIL_POP3_PROTOCOL  0
  89. #define NGX_MAIL_IMAP_PROTOCOL  1
  90. #define NGX_MAIL_SMTP_PROTOCOL  2


  91. typedef struct ngx_mail_protocol_s  ngx_mail_protocol_t;


  92. typedef struct {
  93.     ngx_mail_protocol_t    *protocol;

  94.     ngx_msec_t              timeout;
  95.     ngx_msec_t              resolver_timeout;

  96.     ngx_flag_t              so_keepalive;

  97.     ngx_str_t               server_name;

  98.     u_char                 *file_name;
  99.     ngx_int_t               line;

  100.     ngx_resolver_t         *resolver;

  101.     /* server ctx */
  102.     ngx_mail_conf_ctx_t    *ctx;
  103. } ngx_mail_core_srv_conf_t;


  104. typedef enum {
  105.     ngx_pop3_start = 0,
  106.     ngx_pop3_user,
  107.     ngx_pop3_passwd,
  108.     ngx_pop3_auth_login_username,
  109.     ngx_pop3_auth_login_password,
  110.     ngx_pop3_auth_plain,
  111.     ngx_pop3_auth_cram_md5
  112. } ngx_pop3_state_e;


  113. typedef enum {
  114.     ngx_imap_start = 0,
  115.     ngx_imap_auth_login_username,
  116.     ngx_imap_auth_login_password,
  117.     ngx_imap_auth_plain,
  118.     ngx_imap_auth_cram_md5,
  119.     ngx_imap_login,
  120.     ngx_imap_user,
  121.     ngx_imap_passwd
  122. } ngx_imap_state_e;


  123. typedef enum {
  124.     ngx_smtp_start = 0,
  125.     ngx_smtp_auth_login_username,
  126.     ngx_smtp_auth_login_password,
  127.     ngx_smtp_auth_plain,
  128.     ngx_smtp_auth_cram_md5,
  129.     ngx_smtp_helo,
  130.     ngx_smtp_helo_xclient,
  131.     ngx_smtp_helo_from,
  132.     ngx_smtp_xclient,
  133.     ngx_smtp_xclient_from,
  134.     ngx_smtp_xclient_helo,
  135.     ngx_smtp_from,
  136.     ngx_smtp_to
  137. } ngx_smtp_state_e;


  138. typedef struct {
  139.     ngx_peer_connection_t   upstream;
  140.     ngx_buf_t              *buffer;
  141. } ngx_mail_proxy_ctx_t;


  142. typedef struct {
  143.     uint32_t                signature;         /* "MAIL" */

  144.     ngx_connection_t       *connection;

  145.     ngx_str_t               out;
  146.     ngx_buf_t              *buffer;

  147.     void                  **ctx;
  148.     void                  **main_conf;
  149.     void                  **srv_conf;

  150.     ngx_resolver_ctx_t     *resolver_ctx;

  151.     ngx_mail_proxy_ctx_t   *proxy;

  152.     ngx_uint_t              mail_state;

  153.     unsigned                protocol:3;
  154.     unsigned                blocked:1;
  155.     unsigned                quit:1;
  156.     unsigned                quoted:1;
  157.     unsigned                backslash:1;
  158.     unsigned                no_sync_literal:1;
  159.     unsigned                starttls:1;
  160.     unsigned                esmtp:1;
  161.     unsigned                auth_method:3;
  162.     unsigned                auth_wait:1;

  163.     ngx_str_t               login;
  164.     ngx_str_t               passwd;

  165.     ngx_str_t               salt;
  166.     ngx_str_t               tag;
  167.     ngx_str_t               tagged_line;
  168.     ngx_str_t               text;

  169.     ngx_str_t              *addr_text;
  170.     ngx_str_t               host;
  171.     ngx_str_t               smtp_helo;
  172.     ngx_str_t               smtp_from;
  173.     ngx_str_t               smtp_to;

  174.     ngx_str_t               cmd;

  175.     ngx_uint_t              command;
  176.     ngx_array_t             args;

  177.     ngx_uint_t              login_attempt;

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

  179.     ngx_uint_t              state;
  180.     u_char                 *cmd_start;
  181.     u_char                 *arg_start;
  182.     u_char                 *arg_end;
  183.     ngx_uint_t              literal_len;
  184. } ngx_mail_session_t;


  185. typedef struct {
  186.     ngx_str_t              *client;
  187.     ngx_mail_session_t     *session;
  188. } ngx_mail_log_ctx_t;


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


  204. #define NGX_IMAP_LOGIN         1
  205. #define NGX_IMAP_LOGOUT        2
  206. #define NGX_IMAP_CAPABILITY    3
  207. #define NGX_IMAP_NOOP          4
  208. #define NGX_IMAP_STARTTLS      5

  209. #define NGX_IMAP_NEXT          6

  210. #define NGX_IMAP_AUTHENTICATE  7


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


  224. #define NGX_MAIL_AUTH_PLAIN             0
  225. #define NGX_MAIL_AUTH_LOGIN             1
  226. #define NGX_MAIL_AUTH_LOGIN_USERNAME    2
  227. #define NGX_MAIL_AUTH_APOP              3
  228. #define NGX_MAIL_AUTH_CRAM_MD5          4
  229. #define NGX_MAIL_AUTH_NONE              5


  230. #define NGX_MAIL_AUTH_PLAIN_ENABLED     0x0002
  231. #define NGX_MAIL_AUTH_LOGIN_ENABLED     0x0004
  232. #define NGX_MAIL_AUTH_APOP_ENABLED      0x0008
  233. #define NGX_MAIL_AUTH_CRAM_MD5_ENABLED  0x0010
  234. #define NGX_MAIL_AUTH_NONE_ENABLED      0x0020


  235. #define NGX_MAIL_PARSE_INVALID_COMMAND  20


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


  241. struct ngx_mail_protocol_s {
  242.     ngx_str_t                   name;
  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. };


  251. typedef struct {
  252.     ngx_mail_protocol_t        *protocol;

  253.     void                       *(*create_main_conf)(ngx_conf_t *cf);
  254.     char                       *(*init_main_conf)(ngx_conf_t *cf, void *conf);

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


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

  260. #define NGX_MAIL_MAIN_CONF      0x02000000
  261. #define NGX_MAIL_SRV_CONF       0x04000000


  262. #define NGX_MAIL_MAIN_CONF_OFFSET  offsetof(ngx_mail_conf_ctx_t, main_conf)
  263. #define NGX_MAIL_SRV_CONF_OFFSET   offsetof(ngx_mail_conf_ctx_t, srv_conf)


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


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

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


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


  278. void ngx_mail_init_connection(ngx_connection_t *c);

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

  291. void ngx_mail_send(ngx_event_t *wev);
  292. ngx_int_t ngx_mail_read_command(ngx_mail_session_t *s, ngx_connection_t *c);
  293. void ngx_mail_auth(ngx_mail_session_t *s, ngx_connection_t *c);
  294. void ngx_mail_close_connection(ngx_connection_t *c);
  295. void ngx_mail_session_internal_server_error(ngx_mail_session_t *s);
  296. u_char *ngx_mail_log_error(ngx_log_t *log, u_char *buf, size_t len);


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


  298. /* STUB */
  299. void ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_addr_t *peer);
  300. void ngx_mail_auth_http_init(ngx_mail_session_t *s);
  301. /**/


  302. extern ngx_uint_t    ngx_mail_max_module;
  303. extern ngx_module_t  ngx_mail_core_module;


  304. #endif /* _NGX_MAIL_H_INCLUDED_ */