1 2 /* 3 * Copyright (C) Igor Sysoev 4 * Copyright (C) Nginx, Inc. 5 */ 6 7 8 #ifndef _NGX_MAIL_H_INCLUDED_ 9 #define _NGX_MAIL_H_INCLUDED_ 10 11 12 #include <ngx_config.h> 13 #include <ngx_core.h> 14 #include <ngx_event.h> 15 #include <ngx_event_connect.h> 16 17 #if (NGX_MAIL_SSL) 18 #include <ngx_mail_ssl_module.h> 19 #endif 20 21 22 23 typedef struct { 24 void **main_conf; 25 void **srv_conf; 26 } ngx_mail_conf_ctx_t; 27 28 29 typedef struct { 30 struct sockaddr *sockaddr; 31 socklen_t socklen; 32 ngx_str_t addr_text; 33 34 /* server ctx */ 35 ngx_mail_conf_ctx_t *ctx; 36 37 unsigned bind:1; 38 unsigned wildcard:1; 39 unsigned ssl:1; 40 #if (NGX_HAVE_INET6) 41 unsigned ipv6only:1; 42 #endif 43 unsigned so_keepalive:2; 44 #if (NGX_HAVE_KEEPALIVE_TUNABLE) 45 int tcp_keepidle; 46 int tcp_keepintvl; 47 int tcp_keepcnt; 48 #endif 49 int backlog; 50 int rcvbuf; 51 int sndbuf; 52 } ngx_mail_listen_t; 53 54 55 typedef struct { 56 ngx_mail_conf_ctx_t *ctx; 57 ngx_str_t addr_text; 58 ngx_uint_t ssl; /* unsigned ssl:1; */ 59 } ngx_mail_addr_conf_t; 60 61 typedef struct { 62 in_addr_t addr; 63 ngx_mail_addr_conf_t conf; 64 } ngx_mail_in_addr_t; 65 66 67 #if (NGX_HAVE_INET6) 68 69 typedef struct { 70 struct in6_addr addr6; 71 ngx_mail_addr_conf_t conf; 72 } ngx_mail_in6_addr_t; 73 74 #endif 75 76 77 typedef struct { 78 /* ngx_mail_in_addr_t or ngx_mail_in6_addr_t */ 79 void *addrs; 80 ngx_uint_t naddrs; 81 } ngx_mail_port_t; 82 83 84 typedef struct { 85 int family; 86 in_port_t port; 87 ngx_array_t addrs; /* array of ngx_mail_conf_addr_t */ 88 } ngx_mail_conf_port_t; 89 90 91 typedef struct { 92 ngx_mail_listen_t opt; 93 } ngx_mail_conf_addr_t; 94 95 96 typedef struct { 97 ngx_array_t servers; /* ngx_mail_core_srv_conf_t */ 98 ngx_array_t listen; /* ngx_mail_listen_t */ 99 } ngx_mail_core_main_conf_t; 100 101 102 #define NGX_MAIL_POP3_PROTOCOL 0 103 #define NGX_MAIL_IMAP_PROTOCOL 1 104 #define NGX_MAIL_SMTP_PROTOCOL 2 105 106 107 typedef struct ngx_mail_protocol_s ngx_mail_protocol_t; 108 109 110 typedef struct { 111 ngx_mail_protocol_t *protocol; 112 113 ngx_msec_t timeout; 114 ngx_msec_t resolver_timeout; 115 116 ngx_str_t server_name; 117 118 #if (NGX_HAVE_FSTACK) 119 ngx_flag_t kernel_network_stack; /* kernel_network_stack */ 120 #endif 121 122 u_char *file_name; 123 ngx_uint_t line; 124 125 ngx_resolver_t *resolver; 126 ngx_log_t *error_log; 127 128 /* server ctx */ 129 ngx_mail_conf_ctx_t *ctx; 130 131 ngx_uint_t listen; /* unsigned listen:1; */ 132 } ngx_mail_core_srv_conf_t; 133 134 135 typedef enum { 136 ngx_pop3_start = 0, 137 ngx_pop3_user, 138 ngx_pop3_passwd, 139 ngx_pop3_auth_login_username, 140 ngx_pop3_auth_login_password, 141 ngx_pop3_auth_plain, 142 ngx_pop3_auth_cram_md5, 143 ngx_pop3_auth_external 144 } ngx_pop3_state_e; 145 146 147 typedef enum { 148 ngx_imap_start = 0, 149 ngx_imap_auth_login_username, 150 ngx_imap_auth_login_password, 151 ngx_imap_auth_plain, 152 ngx_imap_auth_cram_md5, 153 ngx_imap_auth_external, 154 ngx_imap_login, 155 ngx_imap_user, 156 ngx_imap_passwd 157 } ngx_imap_state_e; 158 159 160 typedef enum { 161 ngx_smtp_start = 0, 162 ngx_smtp_auth_login_username, 163 ngx_smtp_auth_login_password, 164 ngx_smtp_auth_plain, 165 ngx_smtp_auth_cram_md5, 166 ngx_smtp_auth_external, 167 ngx_smtp_helo, 168 ngx_smtp_helo_xclient, 169 ngx_smtp_helo_from, 170 ngx_smtp_xclient, 171 ngx_smtp_xclient_from, 172 ngx_smtp_xclient_helo, 173 ngx_smtp_from, 174 ngx_smtp_to 175 } ngx_smtp_state_e; 176 177 178 typedef struct { 179 ngx_peer_connection_t upstream; 180 ngx_buf_t *buffer; 181 } ngx_mail_proxy_ctx_t; 182 183 184 typedef struct { 185 uint32_t signature; /* "MAIL" */ 186 187 ngx_connection_t *connection; 188 189 ngx_str_t out; 190 ngx_buf_t *buffer; 191 192 void **ctx; 193 void **main_conf; 194 void **srv_conf; 195 196 ngx_resolver_ctx_t *resolver_ctx; 197 198 ngx_mail_proxy_ctx_t *proxy; 199 200 ngx_uint_t mail_state; 201 202 unsigned protocol:3; 203 unsigned blocked:1; 204 unsigned quit:1; 205 unsigned quoted:1; 206 unsigned backslash:1; 207 unsigned no_sync_literal:1; 208 unsigned starttls:1; 209 unsigned esmtp:1; 210 unsigned auth_method:3; 211 unsigned auth_wait:1; 212 213 ngx_str_t login; 214 ngx_str_t passwd; 215 216 ngx_str_t salt; 217 ngx_str_t tag; 218 ngx_str_t tagged_line; 219 ngx_str_t text; 220 221 ngx_str_t *addr_text; 222 ngx_str_t host; 223 ngx_str_t smtp_helo; 224 ngx_str_t smtp_from; 225 ngx_str_t smtp_to; 226 227 ngx_str_t cmd; 228 229 ngx_uint_t command; 230 ngx_array_t args; 231 232 ngx_uint_t login_attempt; 233 234 /* used to parse POP3/IMAP/SMTP command */ 235 236 ngx_uint_t state; 237 u_char *cmd_start; 238 u_char *arg_start; 239 u_char *arg_end; 240 ngx_uint_t literal_len; 241 } ngx_mail_session_t; 242 243 244 typedef struct { 245 ngx_str_t *client; 246 ngx_mail_session_t *session; 247 } ngx_mail_log_ctx_t; 248 249 250 #define NGX_POP3_USER 1 251 #define NGX_POP3_PASS 2 252 #define NGX_POP3_CAPA 3 253 #define NGX_POP3_QUIT 4 254 #define NGX_POP3_NOOP 5 255 #define NGX_POP3_STLS 6 256 #define NGX_POP3_APOP 7 257 #define NGX_POP3_AUTH 8 258 #define NGX_POP3_STAT 9 259 #define NGX_POP3_LIST 10 260 #define NGX_POP3_RETR 11 261 #define NGX_POP3_DELE 12 262 #define NGX_POP3_RSET 13 263 #define NGX_POP3_TOP 14 264 #define NGX_POP3_UIDL 15 265 266 267 #define NGX_IMAP_LOGIN 1 268 #define NGX_IMAP_LOGOUT 2 269 #define NGX_IMAP_CAPABILITY 3 270 #define NGX_IMAP_NOOP 4 271 #define NGX_IMAP_STARTTLS 5 272 273 #define NGX_IMAP_NEXT 6 274 275 #define NGX_IMAP_AUTHENTICATE 7 276 277 278 #define NGX_SMTP_HELO 1 279 #define NGX_SMTP_EHLO 2 280 #define NGX_SMTP_AUTH 3 281 #define NGX_SMTP_QUIT 4 282 #define NGX_SMTP_NOOP 5 283 #define NGX_SMTP_MAIL 6 284 #define NGX_SMTP_RSET 7 285 #define NGX_SMTP_RCPT 8 286 #define NGX_SMTP_DATA 9 287 #define NGX_SMTP_VRFY 10 288 #define NGX_SMTP_EXPN 11 289 #define NGX_SMTP_HELP 12 290 #define NGX_SMTP_STARTTLS 13 291 292 293 #define NGX_MAIL_AUTH_PLAIN 0 294 #define NGX_MAIL_AUTH_LOGIN 1 295 #define NGX_MAIL_AUTH_LOGIN_USERNAME 2 296 #define NGX_MAIL_AUTH_APOP 3 297 #define NGX_MAIL_AUTH_CRAM_MD5 4 298 #define NGX_MAIL_AUTH_EXTERNAL 5 299 #define NGX_MAIL_AUTH_NONE 6 300 301 302 #define NGX_MAIL_AUTH_PLAIN_ENABLED 0x0002 303 #define NGX_MAIL_AUTH_LOGIN_ENABLED 0x0004 304 #define NGX_MAIL_AUTH_APOP_ENABLED 0x0008 305 #define NGX_MAIL_AUTH_CRAM_MD5_ENABLED 0x0010 306 #define NGX_MAIL_AUTH_EXTERNAL_ENABLED 0x0020 307 #define NGX_MAIL_AUTH_NONE_ENABLED 0x0040 308 309 310 #define NGX_MAIL_PARSE_INVALID_COMMAND 20 311 312 313 typedef void (*ngx_mail_init_session_pt)(ngx_mail_session_t *s, 314 ngx_connection_t *c); 315 typedef void (*ngx_mail_init_protocol_pt)(ngx_event_t *rev); 316 typedef void (*ngx_mail_auth_state_pt)(ngx_event_t *rev); 317 typedef ngx_int_t (*ngx_mail_parse_command_pt)(ngx_mail_session_t *s); 318 319 320 struct ngx_mail_protocol_s { 321 ngx_str_t name; 322 in_port_t port[4]; 323 ngx_uint_t type; 324 325 ngx_mail_init_session_pt init_session; 326 ngx_mail_init_protocol_pt init_protocol; 327 ngx_mail_parse_command_pt parse_command; 328 ngx_mail_auth_state_pt auth_state; 329 330 ngx_str_t internal_server_error; 331 ngx_str_t cert_error; 332 ngx_str_t no_cert; 333 }; 334 335 336 typedef struct { 337 ngx_mail_protocol_t *protocol; 338 339 void *(*create_main_conf)(ngx_conf_t *cf); 340 char *(*init_main_conf)(ngx_conf_t *cf, void *conf); 341 342 void *(*create_srv_conf)(ngx_conf_t *cf); 343 char *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, 344 void *conf); 345 } ngx_mail_module_t; 346 347 348 #define NGX_MAIL_MODULE 0x4C49414D /* "MAIL" */ 349 350 #define NGX_MAIL_MAIN_CONF 0x02000000 351 #define NGX_MAIL_SRV_CONF 0x04000000 352 353 354 #define NGX_MAIL_MAIN_CONF_OFFSET offsetof(ngx_mail_conf_ctx_t, main_conf) 355 #define NGX_MAIL_SRV_CONF_OFFSET offsetof(ngx_mail_conf_ctx_t, srv_conf) 356 357 358 #define ngx_mail_get_module_ctx(s, module) (s)->ctx[module.ctx_index] 359 #define ngx_mail_set_ctx(s, c, module) s->ctx[module.ctx_index] = c; 360 #define ngx_mail_delete_ctx(s, module) s->ctx[module.ctx_index] = NULL; 361 362 363 #define ngx_mail_get_module_main_conf(s, module) \ 364 (s)->main_conf[module.ctx_index] 365 #define ngx_mail_get_module_srv_conf(s, module) (s)->srv_conf[module.ctx_index] 366 367 #define ngx_mail_conf_get_module_main_conf(cf, module) \ 368 ((ngx_mail_conf_ctx_t *) cf->ctx)->main_conf[module.ctx_index] 369 #define ngx_mail_conf_get_module_srv_conf(cf, module) \ 370 ((ngx_mail_conf_ctx_t *) cf->ctx)->srv_conf[module.ctx_index] 371 372 373 #if (NGX_MAIL_SSL) 374 void ngx_mail_starttls_handler(ngx_event_t *rev); 375 ngx_int_t ngx_mail_starttls_only(ngx_mail_session_t *s, ngx_connection_t *c); 376 #endif 377 378 379 void ngx_mail_init_connection(ngx_connection_t *c); 380 381 ngx_int_t ngx_mail_salt(ngx_mail_session_t *s, ngx_connection_t *c, 382 ngx_mail_core_srv_conf_t *cscf); 383 ngx_int_t ngx_mail_auth_plain(ngx_mail_session_t *s, ngx_connection_t *c, 384 ngx_uint_t n); 385 ngx_int_t ngx_mail_auth_login_username(ngx_mail_session_t *s, 386 ngx_connection_t *c, ngx_uint_t n); 387 ngx_int_t ngx_mail_auth_login_password(ngx_mail_session_t *s, 388 ngx_connection_t *c); 389 ngx_int_t ngx_mail_auth_cram_md5_salt(ngx_mail_session_t *s, 390 ngx_connection_t *c, char *prefix, size_t len); 391 ngx_int_t ngx_mail_auth_cram_md5(ngx_mail_session_t *s, ngx_connection_t *c); 392 ngx_int_t ngx_mail_auth_external(ngx_mail_session_t *s, ngx_connection_t *c, 393 ngx_uint_t n); 394 ngx_int_t ngx_mail_auth_parse(ngx_mail_session_t *s, ngx_connection_t *c); 395 396 void ngx_mail_send(ngx_event_t *wev); 397 ngx_int_t ngx_mail_read_command(ngx_mail_session_t *s, ngx_connection_t *c); 398 void ngx_mail_auth(ngx_mail_session_t *s, ngx_connection_t *c); 399 void ngx_mail_close_connection(ngx_connection_t *c); 400 void ngx_mail_session_internal_server_error(ngx_mail_session_t *s); 401 u_char *ngx_mail_log_error(ngx_log_t *log, u_char *buf, size_t len); 402 403 404 char *ngx_mail_capabilities(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 405 406 407 /* STUB */ 408 void ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_addr_t *peer); 409 void ngx_mail_auth_http_init(ngx_mail_session_t *s); 410 /**/ 411 412 413 extern ngx_uint_t ngx_mail_max_module; 414 extern ngx_module_t ngx_mail_core_module; 415 416 417 #endif /* _NGX_MAIL_H_INCLUDED_ */ 418