Igor Sysoev | 02c8d18 | 2007-03-19 13:36:56 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) Igor Sysoev |
| 4 | */ |
| 5 | |
| 6 | |
| 7 | #ifndef _NGX_MAIL_H_INCLUDED_ |
| 8 | #define _NGX_MAIL_H_INCLUDED_ |
| 9 | |
| 10 | |
| 11 | #include <ngx_config.h> |
| 12 | #include <ngx_core.h> |
| 13 | #include <ngx_event.h> |
| 14 | #include <ngx_event_connect.h> |
| 15 | |
| 16 | #if (NGX_MAIL_SSL) |
| 17 | #include <ngx_mail_ssl_module.h> |
| 18 | #endif |
| 19 | |
| 20 | |
| 21 | |
| 22 | typedef struct { |
| 23 | void **main_conf; |
| 24 | void **srv_conf; |
| 25 | } ngx_mail_conf_ctx_t; |
| 26 | |
| 27 | |
| 28 | typedef struct { |
| 29 | in_addr_t addr; |
| 30 | in_port_t port; |
| 31 | int family; |
| 32 | |
| 33 | /* server ctx */ |
| 34 | ngx_mail_conf_ctx_t *ctx; |
| 35 | |
| 36 | unsigned bind:1; |
| 37 | } ngx_mail_listen_t; |
| 38 | |
| 39 | |
| 40 | typedef struct { |
| 41 | in_addr_t addr; |
| 42 | ngx_mail_conf_ctx_t *ctx; |
| 43 | ngx_str_t addr_text; |
| 44 | } ngx_mail_in_addr_t; |
| 45 | |
| 46 | |
| 47 | typedef struct { |
| 48 | ngx_mail_in_addr_t *addrs; /* array of ngx_mail_in_addr_t */ |
| 49 | ngx_uint_t naddrs; |
| 50 | } ngx_mail_in_port_t; |
| 51 | |
| 52 | |
| 53 | typedef struct { |
| 54 | in_port_t port; |
| 55 | ngx_array_t addrs; /* array of ngx_mail_conf_in_addr_t */ |
| 56 | } ngx_mail_conf_in_port_t; |
| 57 | |
| 58 | |
| 59 | typedef struct { |
| 60 | in_addr_t addr; |
| 61 | ngx_mail_conf_ctx_t *ctx; |
| 62 | unsigned bind:1; |
| 63 | } ngx_mail_conf_in_addr_t; |
| 64 | |
| 65 | |
| 66 | typedef struct { |
| 67 | ngx_array_t servers; /* ngx_mail_core_srv_conf_t */ |
| 68 | ngx_array_t listen; /* ngx_mail_listen_t */ |
| 69 | } ngx_mail_core_main_conf_t; |
| 70 | |
| 71 | |
| 72 | #define NGX_MAIL_POP3_PROTOCOL 0 |
| 73 | #define NGX_MAIL_IMAP_PROTOCOL 1 |
| 74 | #define NGX_MAIL_SMTP_PROTOCOL 2 |
| 75 | |
| 76 | typedef struct { |
| 77 | ngx_msec_t timeout; |
| 78 | |
| 79 | size_t imap_client_buffer_size; |
| 80 | |
| 81 | ngx_uint_t protocol; |
| 82 | |
| 83 | ngx_flag_t so_keepalive; |
| 84 | |
| 85 | ngx_str_t pop3_capability; |
| 86 | ngx_str_t pop3_starttls_capability; |
| 87 | ngx_str_t pop3_starttls_only_capability; |
| 88 | ngx_str_t pop3_auth_capability; |
| 89 | |
| 90 | ngx_str_t imap_capability; |
| 91 | ngx_str_t imap_starttls_capability; |
| 92 | ngx_str_t imap_starttls_only_capability; |
| 93 | |
| 94 | ngx_str_t smtp_capability; |
| 95 | |
| 96 | ngx_str_t server_name; |
| 97 | ngx_str_t smtp_server_name; |
| 98 | ngx_str_t smtp_greeting; |
| 99 | |
| 100 | ngx_uint_t pop3_auth_methods; |
| 101 | ngx_uint_t smtp_auth_methods; |
| 102 | |
| 103 | ngx_array_t pop3_capabilities; |
| 104 | ngx_array_t imap_capabilities; |
| 105 | ngx_array_t smtp_capabilities; |
| 106 | |
| 107 | /* server ctx */ |
| 108 | ngx_mail_conf_ctx_t *ctx; |
| 109 | } ngx_mail_core_srv_conf_t; |
| 110 | |
| 111 | |
| 112 | typedef struct { |
| 113 | void *(*create_main_conf)(ngx_conf_t *cf); |
| 114 | char *(*init_main_conf)(ngx_conf_t *cf, void *conf); |
| 115 | |
| 116 | void *(*create_srv_conf)(ngx_conf_t *cf); |
| 117 | char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, |
| 118 | void *conf); |
| 119 | } ngx_mail_module_t; |
| 120 | |
| 121 | |
| 122 | typedef enum { |
| 123 | ngx_pop3_start = 0, |
| 124 | ngx_pop3_user, |
| 125 | ngx_pop3_passwd, |
| 126 | ngx_pop3_auth_login_username, |
| 127 | ngx_pop3_auth_login_password, |
| 128 | ngx_pop3_auth_plain, |
| 129 | ngx_pop3_auth_cram_md5 |
| 130 | } ngx_po3_state_e; |
| 131 | |
| 132 | |
| 133 | typedef enum { |
| 134 | ngx_imap_start = 0, |
| 135 | ngx_imap_login, |
| 136 | ngx_imap_user, |
| 137 | ngx_imap_passwd |
| 138 | } ngx_imap_state_e; |
| 139 | |
| 140 | |
| 141 | typedef enum { |
| 142 | ngx_smtp_start = 0, |
| 143 | ngx_smtp_auth_login_username, |
| 144 | ngx_smtp_auth_login_password, |
| 145 | ngx_smtp_auth_plain, |
| 146 | ngx_smtp_auth_cram_md5, |
| 147 | ngx_smtp_helo, |
| 148 | ngx_smtp_noxclient, |
| 149 | ngx_smtp_xclient |
| 150 | } ngx_smtp_state_e; |
| 151 | |
| 152 | |
| 153 | typedef struct { |
| 154 | ngx_peer_connection_t upstream; |
| 155 | ngx_buf_t *buffer; |
| 156 | } ngx_mail_proxy_ctx_t; |
| 157 | |
| 158 | |
| 159 | typedef struct { |
| 160 | uint32_t signature; /* "MAIL" */ |
| 161 | |
| 162 | ngx_connection_t *connection; |
| 163 | |
| 164 | ngx_str_t out; |
| 165 | ngx_buf_t *buffer; |
| 166 | |
| 167 | void **ctx; |
| 168 | void **main_conf; |
| 169 | void **srv_conf; |
| 170 | |
| 171 | ngx_mail_proxy_ctx_t *proxy; |
| 172 | |
| 173 | ngx_uint_t mail_state; |
| 174 | |
| 175 | unsigned blocked:1; |
| 176 | unsigned quit:1; |
| 177 | unsigned protocol:2; |
| 178 | unsigned quoted:1; |
| 179 | unsigned backslash:1; |
| 180 | unsigned no_sync_literal:1; |
| 181 | unsigned starttls:1; |
| 182 | unsigned esmtp:1; |
| 183 | unsigned auth_method:2; |
| 184 | unsigned auth_wait:1; |
| 185 | |
| 186 | ngx_str_t login; |
| 187 | ngx_str_t passwd; |
| 188 | |
| 189 | ngx_str_t salt; |
| 190 | ngx_str_t tag; |
| 191 | ngx_str_t tagged_line; |
| 192 | |
| 193 | ngx_str_t *addr_text; |
| 194 | ngx_str_t smtp_helo; |
| 195 | |
| 196 | ngx_uint_t command; |
| 197 | ngx_array_t args; |
| 198 | |
| 199 | ngx_uint_t login_attempt; |
| 200 | |
| 201 | /* used to parse IMAP/POP3/SMTP command */ |
| 202 | |
| 203 | ngx_uint_t state; |
| 204 | u_char *cmd_start; |
| 205 | u_char *arg_start; |
| 206 | u_char *arg_end; |
| 207 | ngx_uint_t literal_len; |
| 208 | } ngx_mail_session_t; |
| 209 | |
| 210 | |
| 211 | typedef struct { |
| 212 | ngx_str_t *client; |
| 213 | ngx_mail_session_t *session; |
| 214 | } ngx_mail_log_ctx_t; |
| 215 | |
| 216 | |
| 217 | #define NGX_POP3_USER 1 |
| 218 | #define NGX_POP3_PASS 2 |
| 219 | #define NGX_POP3_CAPA 3 |
| 220 | #define NGX_POP3_QUIT 4 |
| 221 | #define NGX_POP3_NOOP 5 |
| 222 | #define NGX_POP3_STLS 6 |
| 223 | #define NGX_POP3_APOP 7 |
| 224 | #define NGX_POP3_AUTH 8 |
| 225 | #define NGX_POP3_STAT 9 |
| 226 | #define NGX_POP3_LIST 10 |
| 227 | #define NGX_POP3_RETR 11 |
| 228 | #define NGX_POP3_DELE 12 |
| 229 | #define NGX_POP3_RSET 13 |
| 230 | #define NGX_POP3_TOP 14 |
| 231 | #define NGX_POP3_UIDL 15 |
| 232 | |
| 233 | |
| 234 | #define NGX_IMAP_LOGIN 1 |
| 235 | #define NGX_IMAP_LOGOUT 2 |
| 236 | #define NGX_IMAP_CAPABILITY 3 |
| 237 | #define NGX_IMAP_NOOP 4 |
| 238 | #define NGX_IMAP_STARTTLS 5 |
| 239 | |
| 240 | #define NGX_IMAP_NEXT 6 |
| 241 | |
| 242 | |
| 243 | #define NGX_SMTP_HELO 1 |
| 244 | #define NGX_SMTP_EHLO 2 |
| 245 | #define NGX_SMTP_AUTH 3 |
| 246 | #define NGX_SMTP_QUIT 4 |
| 247 | #define NGX_SMTP_NOOP 5 |
| 248 | #define NGX_SMTP_MAIL 6 |
| 249 | #define NGX_SMTP_RSET 7 |
| 250 | |
| 251 | |
| 252 | #define NGX_MAIL_AUTH_PLAIN 0 |
| 253 | #define NGX_MAIL_AUTH_LOGIN 1 |
| 254 | #define NGX_MAIL_AUTH_APOP 2 |
| 255 | #define NGX_MAIL_AUTH_CRAM_MD5 3 |
| 256 | |
| 257 | |
| 258 | #define NGX_MAIL_AUTH_PLAIN_ENABLED 0x0002 |
| 259 | #define NGX_MAIL_AUTH_LOGIN_ENABLED 0x0004 |
| 260 | #define NGX_MAIL_AUTH_APOP_ENABLED 0x0008 |
| 261 | #define NGX_MAIL_AUTH_CRAM_MD5_ENABLED 0x0010 |
| 262 | |
| 263 | |
| 264 | #define NGX_MAIL_PARSE_INVALID_COMMAND 20 |
| 265 | |
| 266 | |
| 267 | #define NGX_MAIL_MODULE 0x4C49414D /* "MAIL" */ |
| 268 | |
| 269 | #define NGX_MAIL_MAIN_CONF 0x02000000 |
| 270 | #define NGX_MAIL_SRV_CONF 0x04000000 |
| 271 | |
| 272 | |
| 273 | #define NGX_MAIL_MAIN_CONF_OFFSET offsetof(ngx_mail_conf_ctx_t, main_conf) |
| 274 | #define NGX_MAIL_SRV_CONF_OFFSET offsetof(ngx_mail_conf_ctx_t, srv_conf) |
| 275 | |
| 276 | |
| 277 | #define ngx_mail_get_module_ctx(s, module) (s)->ctx[module.ctx_index] |
| 278 | #define ngx_mail_set_ctx(s, c, module) s->ctx[module.ctx_index] = c; |
| 279 | #define ngx_mail_delete_ctx(s, module) s->ctx[module.ctx_index] = NULL; |
| 280 | |
| 281 | |
| 282 | #define ngx_mail_get_module_main_conf(s, module) \ |
| 283 | (s)->main_conf[module.ctx_index] |
| 284 | #define ngx_mail_get_module_srv_conf(s, module) (s)->srv_conf[module.ctx_index] |
| 285 | |
| 286 | #define ngx_mail_conf_get_module_main_conf(cf, module) \ |
| 287 | ((ngx_mail_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index] |
| 288 | |
| 289 | |
| 290 | void ngx_mail_init_connection(ngx_connection_t *c); |
| 291 | void ngx_mail_send(ngx_event_t *wev); |
| 292 | void ngx_pop3_auth_state(ngx_event_t *rev); |
| 293 | void ngx_imap_auth_state(ngx_event_t *rev); |
| 294 | void ngx_smtp_auth_state(ngx_event_t *rev); |
| 295 | void ngx_mail_close_connection(ngx_connection_t *c); |
| 296 | void ngx_mail_session_internal_server_error(ngx_mail_session_t *s); |
| 297 | |
| 298 | ngx_int_t ngx_pop3_parse_command(ngx_mail_session_t *s); |
| 299 | ngx_int_t ngx_imap_parse_command(ngx_mail_session_t *s); |
| 300 | ngx_int_t ngx_smtp_parse_command(ngx_mail_session_t *s); |
| 301 | |
| 302 | |
| 303 | /* STUB */ |
| 304 | void ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_peer_addr_t *peer); |
| 305 | void ngx_mail_auth_http_init(ngx_mail_session_t *s); |
| 306 | /**/ |
| 307 | |
| 308 | |
| 309 | extern ngx_uint_t ngx_mail_max_module; |
| 310 | extern ngx_module_t ngx_mail_core_module; |
| 311 | |
| 312 | |
| 313 | #endif /* _NGX_MAIL_H_INCLUDED_ */ |