Mercurial > hg > nginx-quic
annotate src/mail/ngx_mail_auth_http_module.c @ 2501:08335cba1146
bump version
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 11 Feb 2009 10:23:06 +0000 |
parents | 722b5aff05ae |
children | 2477b28eaccb |
rev | line source |
---|---|
521 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_event.h> | |
10 #include <ngx_event_connect.h> | |
1136 | 11 #include <ngx_mail.h> |
521 | 12 |
13 | |
14 typedef struct { | |
884 | 15 ngx_peer_addr_t *peer; |
521 | 16 |
527 | 17 ngx_msec_t timeout; |
521 | 18 |
527 | 19 ngx_str_t host_header; |
20 ngx_str_t uri; | |
573 | 21 ngx_str_t header; |
22 | |
23 ngx_array_t *headers; | |
1392 | 24 |
25 u_char *file; | |
26 ngx_uint_t line; | |
1136 | 27 } ngx_mail_auth_http_conf_t; |
521 | 28 |
29 | |
1136 | 30 typedef struct ngx_mail_auth_http_ctx_s ngx_mail_auth_http_ctx_t; |
527 | 31 |
1136 | 32 typedef void (*ngx_mail_auth_http_handler_pt)(ngx_mail_session_t *s, |
33 ngx_mail_auth_http_ctx_t *ctx); | |
527 | 34 |
1136 | 35 struct ngx_mail_auth_http_ctx_s { |
527 | 36 ngx_buf_t *request; |
37 ngx_buf_t *response; | |
38 ngx_peer_connection_t peer; | |
39 | |
1136 | 40 ngx_mail_auth_http_handler_pt handler; |
527 | 41 |
42 ngx_uint_t state; | |
43 | |
44 u_char *header_name_start; | |
45 u_char *header_name_end; | |
46 u_char *header_start; | |
47 u_char *header_end; | |
48 | |
49 ngx_str_t addr; | |
50 ngx_str_t port; | |
51 ngx_str_t err; | |
567 | 52 ngx_str_t errmsg; |
1136 | 53 ngx_str_t errcode; |
527 | 54 |
547 | 55 time_t sleep; |
527 | 56 |
547 | 57 ngx_pool_t *pool; |
527 | 58 }; |
521 | 59 |
60 | |
1136 | 61 static void ngx_mail_auth_http_write_handler(ngx_event_t *wev); |
62 static void ngx_mail_auth_http_read_handler(ngx_event_t *rev); | |
63 static void ngx_mail_auth_http_ignore_status_line(ngx_mail_session_t *s, | |
64 ngx_mail_auth_http_ctx_t *ctx); | |
65 static void ngx_mail_auth_http_process_headers(ngx_mail_session_t *s, | |
66 ngx_mail_auth_http_ctx_t *ctx); | |
67 static void ngx_mail_auth_sleep_handler(ngx_event_t *rev); | |
68 static ngx_int_t ngx_mail_auth_http_parse_header_line(ngx_mail_session_t *s, | |
69 ngx_mail_auth_http_ctx_t *ctx); | |
70 static void ngx_mail_auth_http_block_read(ngx_event_t *rev); | |
71 static void ngx_mail_auth_http_dummy_handler(ngx_event_t *ev); | |
72 static ngx_buf_t *ngx_mail_auth_http_create_request(ngx_mail_session_t *s, | |
73 ngx_pool_t *pool, ngx_mail_auth_http_conf_t *ahcf); | |
74 static ngx_int_t ngx_mail_auth_http_escape(ngx_pool_t *pool, ngx_str_t *text, | |
633 | 75 ngx_str_t *escaped); |
521 | 76 |
1136 | 77 static void *ngx_mail_auth_http_create_conf(ngx_conf_t *cf); |
78 static char *ngx_mail_auth_http_merge_conf(ngx_conf_t *cf, void *parent, | |
521 | 79 void *child); |
1136 | 80 static char *ngx_mail_auth_http(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
81 static char *ngx_mail_auth_http_header(ngx_conf_t *cf, ngx_command_t *cmd, | |
573 | 82 void *conf); |
521 | 83 |
84 | |
1136 | 85 static ngx_command_t ngx_mail_auth_http_commands[] = { |
521 | 86 |
87 { ngx_string("auth_http"), | |
1136 | 88 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
89 ngx_mail_auth_http, | |
90 NGX_MAIL_SRV_CONF_OFFSET, | |
521 | 91 0, |
92 NULL }, | |
93 | |
94 { ngx_string("auth_http_timeout"), | |
1136 | 95 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, |
521 | 96 ngx_conf_set_msec_slot, |
1136 | 97 NGX_MAIL_SRV_CONF_OFFSET, |
98 offsetof(ngx_mail_auth_http_conf_t, timeout), | |
521 | 99 NULL }, |
100 | |
573 | 101 { ngx_string("auth_http_header"), |
1136 | 102 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE2, |
103 ngx_mail_auth_http_header, | |
104 NGX_MAIL_SRV_CONF_OFFSET, | |
573 | 105 0, |
106 NULL }, | |
107 | |
521 | 108 ngx_null_command |
109 }; | |
110 | |
111 | |
1136 | 112 static ngx_mail_module_t ngx_mail_auth_http_module_ctx = { |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
113 NULL, /* protocol */ |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
114 |
521 | 115 NULL, /* create main configuration */ |
116 NULL, /* init main configuration */ | |
117 | |
1136 | 118 ngx_mail_auth_http_create_conf, /* create server configuration */ |
119 ngx_mail_auth_http_merge_conf /* merge server configuration */ | |
521 | 120 }; |
121 | |
122 | |
1136 | 123 ngx_module_t ngx_mail_auth_http_module = { |
521 | 124 NGX_MODULE_V1, |
1136 | 125 &ngx_mail_auth_http_module_ctx, /* module context */ |
126 ngx_mail_auth_http_commands, /* module directives */ | |
127 NGX_MAIL_MODULE, /* module type */ | |
541 | 128 NULL, /* init master */ |
521 | 129 NULL, /* init module */ |
541 | 130 NULL, /* init process */ |
131 NULL, /* init thread */ | |
132 NULL, /* exit thread */ | |
133 NULL, /* exit process */ | |
134 NULL, /* exit master */ | |
135 NGX_MODULE_V1_PADDING | |
521 | 136 }; |
137 | |
138 | |
1136 | 139 static ngx_str_t ngx_mail_auth_http_method[] = { |
140 ngx_string("plain"), | |
809 | 141 ngx_string("plain"), |
142 ngx_string("apop"), | |
2309 | 143 ngx_string("cram-md5"), |
144 ngx_string("none") | |
800 | 145 }; |
521 | 146 |
1136 | 147 static ngx_str_t ngx_mail_smtp_errcode = ngx_string("535 5.7.0"); |
521 | 148 |
1477 | 149 |
521 | 150 void |
1136 | 151 ngx_mail_auth_http_init(ngx_mail_session_t *s) |
521 | 152 { |
153 ngx_int_t rc; | |
547 | 154 ngx_pool_t *pool; |
1136 | 155 ngx_mail_auth_http_ctx_t *ctx; |
156 ngx_mail_auth_http_conf_t *ahcf; | |
521 | 157 |
541 | 158 s->connection->log->action = "in http auth state"; |
159 | |
547 | 160 pool = ngx_create_pool(2048, s->connection->log); |
161 if (pool == NULL) { | |
1136 | 162 ngx_mail_session_internal_server_error(s); |
521 | 163 return; |
164 } | |
165 | |
1136 | 166 ctx = ngx_pcalloc(pool, sizeof(ngx_mail_auth_http_ctx_t)); |
547 | 167 if (ctx == NULL) { |
168 ngx_destroy_pool(pool); | |
1136 | 169 ngx_mail_session_internal_server_error(s); |
547 | 170 return; |
171 } | |
172 | |
173 ctx->pool = pool; | |
174 | |
1136 | 175 ahcf = ngx_mail_get_module_srv_conf(s, ngx_mail_auth_http_module); |
521 | 176 |
1136 | 177 ctx->request = ngx_mail_auth_http_create_request(s, pool, ahcf); |
521 | 178 if (ctx->request == NULL) { |
547 | 179 ngx_destroy_pool(ctx->pool); |
1136 | 180 ngx_mail_session_internal_server_error(s); |
521 | 181 return; |
182 } | |
183 | |
1136 | 184 ngx_mail_set_ctx(s, ctx, ngx_mail_auth_http_module); |
521 | 185 |
884 | 186 ctx->peer.sockaddr = ahcf->peer->sockaddr; |
187 ctx->peer.socklen = ahcf->peer->socklen; | |
188 ctx->peer.name = &ahcf->peer->name; | |
189 ctx->peer.get = ngx_event_get_peer; | |
521 | 190 ctx->peer.log = s->connection->log; |
191 ctx->peer.log_error = NGX_ERROR_ERR; | |
192 | |
193 rc = ngx_event_connect_peer(&ctx->peer); | |
194 | |
543 | 195 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) { |
862
6044cea025fa
fix segfault when connect() failed
Igor Sysoev <igor@sysoev.ru>
parents:
856
diff
changeset
|
196 if (ctx->peer.connection) { |
6044cea025fa
fix segfault when connect() failed
Igor Sysoev <igor@sysoev.ru>
parents:
856
diff
changeset
|
197 ngx_close_connection(ctx->peer.connection); |
6044cea025fa
fix segfault when connect() failed
Igor Sysoev <igor@sysoev.ru>
parents:
856
diff
changeset
|
198 } |
6044cea025fa
fix segfault when connect() failed
Igor Sysoev <igor@sysoev.ru>
parents:
856
diff
changeset
|
199 |
547 | 200 ngx_destroy_pool(ctx->pool); |
1136 | 201 ngx_mail_session_internal_server_error(s); |
521 | 202 return; |
203 } | |
204 | |
205 ctx->peer.connection->data = s; | |
206 ctx->peer.connection->pool = s->connection->pool; | |
207 | |
1136 | 208 s->connection->read->handler = ngx_mail_auth_http_block_read; |
209 ctx->peer.connection->read->handler = ngx_mail_auth_http_read_handler; | |
210 ctx->peer.connection->write->handler = ngx_mail_auth_http_write_handler; | |
521 | 211 |
1136 | 212 ctx->handler = ngx_mail_auth_http_ignore_status_line; |
527 | 213 |
541 | 214 ngx_add_timer(ctx->peer.connection->read, ahcf->timeout); |
215 ngx_add_timer(ctx->peer.connection->write, ahcf->timeout); | |
216 | |
521 | 217 if (rc == NGX_OK) { |
1136 | 218 ngx_mail_auth_http_write_handler(ctx->peer.connection->write); |
521 | 219 return; |
220 } | |
221 } | |
222 | |
223 | |
224 static void | |
1136 | 225 ngx_mail_auth_http_write_handler(ngx_event_t *wev) |
521 | 226 { |
227 ssize_t n, size; | |
228 ngx_connection_t *c; | |
1136 | 229 ngx_mail_session_t *s; |
230 ngx_mail_auth_http_ctx_t *ctx; | |
231 ngx_mail_auth_http_conf_t *ahcf; | |
521 | 232 |
233 c = wev->data; | |
234 s = c->data; | |
235 | |
1136 | 236 ctx = ngx_mail_get_module_ctx(s, ngx_mail_auth_http_module); |
521 | 237 |
1136 | 238 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, |
239 "mail auth http write handler"); | |
521 | 240 |
577 | 241 if (wev->timedout) { |
521 | 242 ngx_log_error(NGX_LOG_ERR, wev->log, NGX_ETIMEDOUT, |
884 | 243 "auth http server %V timed out", ctx->peer.name); |
1478 | 244 ngx_close_connection(c); |
547 | 245 ngx_destroy_pool(ctx->pool); |
1136 | 246 ngx_mail_session_internal_server_error(s); |
521 | 247 return; |
248 } | |
249 | |
250 size = ctx->request->last - ctx->request->pos; | |
251 | |
252 n = ngx_send(c, ctx->request->pos, size); | |
253 | |
254 if (n == NGX_ERROR) { | |
1478 | 255 ngx_close_connection(c); |
547 | 256 ngx_destroy_pool(ctx->pool); |
1136 | 257 ngx_mail_session_internal_server_error(s); |
521 | 258 return; |
259 } | |
260 | |
261 if (n > 0) { | |
262 ctx->request->pos += n; | |
263 | |
264 if (n == size) { | |
1136 | 265 wev->handler = ngx_mail_auth_http_dummy_handler; |
521 | 266 |
267 if (wev->timer_set) { | |
268 ngx_del_timer(wev); | |
269 } | |
270 | |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2310
diff
changeset
|
271 if (ngx_handle_write_event(wev, 0) != NGX_OK) { |
1478 | 272 ngx_close_connection(c); |
799
9737d6fb1ac6
disable write level event while waiting auth server response
Igor Sysoev <igor@sysoev.ru>
parents:
633
diff
changeset
|
273 ngx_destroy_pool(ctx->pool); |
1136 | 274 ngx_mail_session_internal_server_error(s); |
799
9737d6fb1ac6
disable write level event while waiting auth server response
Igor Sysoev <igor@sysoev.ru>
parents:
633
diff
changeset
|
275 } |
9737d6fb1ac6
disable write level event while waiting auth server response
Igor Sysoev <igor@sysoev.ru>
parents:
633
diff
changeset
|
276 |
521 | 277 return; |
278 } | |
279 } | |
280 | |
281 if (!wev->timer_set) { | |
1136 | 282 ahcf = ngx_mail_get_module_srv_conf(s, ngx_mail_auth_http_module); |
521 | 283 ngx_add_timer(wev, ahcf->timeout); |
284 } | |
285 } | |
286 | |
287 | |
288 static void | |
1136 | 289 ngx_mail_auth_http_read_handler(ngx_event_t *rev) |
521 | 290 { |
525 | 291 ssize_t n, size; |
521 | 292 ngx_connection_t *c; |
1136 | 293 ngx_mail_session_t *s; |
294 ngx_mail_auth_http_ctx_t *ctx; | |
521 | 295 |
296 c = rev->data; | |
297 s = c->data; | |
298 | |
1136 | 299 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, |
300 "mail auth http read handler"); | |
521 | 301 |
1136 | 302 ctx = ngx_mail_get_module_ctx(s, ngx_mail_auth_http_module); |
525 | 303 |
577 | 304 if (rev->timedout) { |
525 | 305 ngx_log_error(NGX_LOG_ERR, rev->log, NGX_ETIMEDOUT, |
884 | 306 "auth http server %V timed out", ctx->peer.name); |
1478 | 307 ngx_close_connection(c); |
547 | 308 ngx_destroy_pool(ctx->pool); |
1136 | 309 ngx_mail_session_internal_server_error(s); |
525 | 310 return; |
311 } | |
312 | |
313 if (ctx->response == NULL) { | |
547 | 314 ctx->response = ngx_create_temp_buf(ctx->pool, 1024); |
525 | 315 if (ctx->response == NULL) { |
1478 | 316 ngx_close_connection(c); |
547 | 317 ngx_destroy_pool(ctx->pool); |
1136 | 318 ngx_mail_session_internal_server_error(s); |
525 | 319 return; |
320 } | |
321 } | |
322 | |
527 | 323 size = ctx->response->end - ctx->response->last; |
525 | 324 |
325 n = ngx_recv(c, ctx->response->pos, size); | |
326 | |
527 | 327 if (n > 0) { |
328 ctx->response->last += n; | |
329 | |
330 ctx->handler(s, ctx); | |
331 return; | |
332 } | |
333 | |
334 if (n == NGX_AGAIN) { | |
525 | 335 return; |
336 } | |
337 | |
1478 | 338 ngx_close_connection(c); |
547 | 339 ngx_destroy_pool(ctx->pool); |
1136 | 340 ngx_mail_session_internal_server_error(s); |
527 | 341 } |
525 | 342 |
343 | |
527 | 344 static void |
1136 | 345 ngx_mail_auth_http_ignore_status_line(ngx_mail_session_t *s, |
346 ngx_mail_auth_http_ctx_t *ctx) | |
527 | 347 { |
348 u_char *p, ch; | |
349 enum { | |
350 sw_start = 0, | |
351 sw_H, | |
352 sw_HT, | |
353 sw_HTT, | |
354 sw_HTTP, | |
355 sw_skip, | |
356 sw_almost_done | |
357 } state; | |
358 | |
1136 | 359 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
360 "mail auth http process status line"); | |
527 | 361 |
362 state = ctx->state; | |
363 | |
364 for (p = ctx->response->pos; p < ctx->response->last; p++) { | |
365 ch = *p; | |
366 | |
367 switch (state) { | |
368 | |
369 /* "HTTP/" */ | |
370 case sw_start: | |
371 if (ch == 'H') { | |
372 state = sw_H; | |
373 break; | |
374 } | |
375 goto next; | |
376 | |
377 case sw_H: | |
378 if (ch == 'T') { | |
379 state = sw_HT; | |
380 break; | |
381 } | |
382 goto next; | |
383 | |
384 case sw_HT: | |
385 if (ch == 'T') { | |
386 state = sw_HTT; | |
387 break; | |
388 } | |
389 goto next; | |
390 | |
391 case sw_HTT: | |
392 if (ch == 'P') { | |
393 state = sw_HTTP; | |
394 break; | |
395 } | |
396 goto next; | |
397 | |
398 case sw_HTTP: | |
399 if (ch == '/') { | |
400 state = sw_skip; | |
401 break; | |
402 } | |
403 goto next; | |
404 | |
405 /* any text until end of line */ | |
406 case sw_skip: | |
407 switch (ch) { | |
408 case CR: | |
409 state = sw_almost_done; | |
410 | |
411 break; | |
577 | 412 case LF: |
527 | 413 goto done; |
414 } | |
415 break; | |
416 | |
417 /* end of status line */ | |
418 case sw_almost_done: | |
419 if (ch == LF) { | |
420 goto done; | |
421 } | |
422 | |
423 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
541 | 424 "auth http server &V sent invalid response", |
884 | 425 ctx->peer.name); |
527 | 426 ngx_close_connection(ctx->peer.connection); |
547 | 427 ngx_destroy_pool(ctx->pool); |
1136 | 428 ngx_mail_session_internal_server_error(s); |
527 | 429 return; |
430 } | |
431 } | |
432 | |
433 ctx->response->pos = p; | |
434 ctx->state = state; | |
435 | |
436 return; | |
437 | |
438 next: | |
439 | |
440 p = ctx->response->start - 1; | |
441 | |
442 done: | |
443 | |
444 ctx->response->pos = p + 1; | |
445 ctx->state = 0; | |
1136 | 446 ctx->handler = ngx_mail_auth_http_process_headers; |
527 | 447 ctx->handler(s, ctx); |
448 } | |
525 | 449 |
450 | |
527 | 451 static void |
1136 | 452 ngx_mail_auth_http_process_headers(ngx_mail_session_t *s, |
453 ngx_mail_auth_http_ctx_t *ctx) | |
527 | 454 { |
455 u_char *p; | |
547 | 456 time_t timer; |
527 | 457 size_t len, size; |
458 ngx_int_t rc, port, n; | |
884 | 459 ngx_peer_addr_t *peer; |
527 | 460 struct sockaddr_in *sin; |
525 | 461 |
1136 | 462 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
463 "mail auth http process headers"); | |
527 | 464 |
465 for ( ;; ) { | |
1136 | 466 rc = ngx_mail_auth_http_parse_header_line(s, ctx); |
527 | 467 |
468 if (rc == NGX_OK) { | |
469 | |
470 #if (NGX_DEBUG) | |
471 { | |
472 ngx_str_t key, value; | |
473 | |
474 key.len = ctx->header_name_end - ctx->header_name_start; | |
475 key.data = ctx->header_name_start; | |
476 value.len = ctx->header_end - ctx->header_start; | |
477 value.data = ctx->header_start; | |
478 | |
1136 | 479 ngx_log_debug2(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
480 "mail auth http header: \"%V: %V\"", | |
527 | 481 &key, &value); |
482 } | |
483 #endif | |
484 | |
485 len = ctx->header_name_end - ctx->header_name_start; | |
486 | |
487 if (len == sizeof("Auth-Status") - 1 | |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
488 && ngx_strncasecmp(ctx->header_name_start, |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
489 (u_char *) "Auth-Status", |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
490 sizeof("Auth-Status") - 1) |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
491 == 0) |
527 | 492 { |
493 len = ctx->header_end - ctx->header_start; | |
494 | |
495 if (len == 2 | |
496 && ctx->header_start[0] == 'O' | |
497 && ctx->header_start[1] == 'K') | |
498 { | |
499 continue; | |
500 } | |
501 | |
883 | 502 if (len == 4 |
503 && ctx->header_start[0] == 'W' | |
504 && ctx->header_start[1] == 'A' | |
505 && ctx->header_start[2] == 'I' | |
506 && ctx->header_start[3] == 'T') | |
507 { | |
508 s->auth_wait = 1; | |
509 continue; | |
510 } | |
511 | |
567 | 512 ctx->errmsg.len = len; |
513 ctx->errmsg.data = ctx->header_start; | |
514 | |
1136 | 515 switch (s->protocol) { |
516 | |
517 case NGX_MAIL_POP3_PROTOCOL: | |
854
1673f197bc62
fix segfault when many auth failures occurred
Igor Sysoev <igor@sysoev.ru>
parents:
809
diff
changeset
|
518 size = sizeof("-ERR ") - 1 + len + sizeof(CRLF) - 1; |
1136 | 519 break; |
527 | 520 |
1136 | 521 case NGX_MAIL_IMAP_PROTOCOL: |
854
1673f197bc62
fix segfault when many auth failures occurred
Igor Sysoev <igor@sysoev.ru>
parents:
809
diff
changeset
|
522 size = s->tag.len + sizeof("NO ") - 1 + len |
527 | 523 + sizeof(CRLF) - 1; |
1136 | 524 break; |
525 | |
526 default: /* NGX_MAIL_SMTP_PROTOCOL */ | |
527 ctx->err = ctx->errmsg; | |
528 continue; | |
527 | 529 } |
530 | |
2061
b0a1c84725cf
change useless ngx_pcalloc() to ngx_pnalloc()
Igor Sysoev <igor@sysoev.ru>
parents:
2049
diff
changeset
|
531 p = ngx_pnalloc(s->connection->pool, size); |
527 | 532 if (p == NULL) { |
543 | 533 ngx_close_connection(ctx->peer.connection); |
547 | 534 ngx_destroy_pool(ctx->pool); |
1136 | 535 ngx_mail_session_internal_server_error(s); |
527 | 536 return; |
537 } | |
538 | |
539 ctx->err.data = p; | |
540 | |
1136 | 541 switch (s->protocol) { |
527 | 542 |
1136 | 543 case NGX_MAIL_POP3_PROTOCOL: |
544 *p++ = '-'; *p++ = 'E'; *p++ = 'R'; *p++ = 'R'; *p++ = ' '; | |
545 break; | |
546 | |
547 case NGX_MAIL_IMAP_PROTOCOL: | |
527 | 548 p = ngx_cpymem(p, s->tag.data, s->tag.len); |
1136 | 549 *p++ = 'N'; *p++ = 'O'; *p++ = ' '; |
550 break; | |
551 | |
552 default: /* NGX_MAIL_SMTP_PROTOCOL */ | |
553 break; | |
527 | 554 } |
555 | |
556 p = ngx_cpymem(p, ctx->header_start, len); | |
557 *p++ = CR; *p++ = LF; | |
558 | |
559 ctx->err.len = p - ctx->err.data; | |
560 | |
561 continue; | |
562 } | |
563 | |
564 if (len == sizeof("Auth-Server") - 1 | |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
565 && ngx_strncasecmp(ctx->header_name_start, |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
566 (u_char *) "Auth-Server", |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
567 sizeof("Auth-Server") - 1) |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
568 == 0) |
527 | 569 { |
570 ctx->addr.len = ctx->header_end - ctx->header_start; | |
571 ctx->addr.data = ctx->header_start; | |
572 | |
573 continue; | |
574 } | |
575 | |
576 if (len == sizeof("Auth-Port") - 1 | |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
577 && ngx_strncasecmp(ctx->header_name_start, |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
578 (u_char *) "Auth-Port", |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
579 sizeof("Auth-Port") - 1) |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
580 == 0) |
527 | 581 { |
582 ctx->port.len = ctx->header_end - ctx->header_start; | |
583 ctx->port.data = ctx->header_start; | |
584 | |
585 continue; | |
586 } | |
587 | |
588 if (len == sizeof("Auth-User") - 1 | |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
589 && ngx_strncasecmp(ctx->header_name_start, |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
590 (u_char *) "Auth-User", |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
591 sizeof("Auth-User") - 1) |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
592 == 0) |
527 | 593 { |
594 s->login.len = ctx->header_end - ctx->header_start; | |
567 | 595 |
2049 | 596 s->login.data = ngx_pnalloc(s->connection->pool, s->login.len); |
567 | 597 if (s->login.data == NULL) { |
598 ngx_close_connection(ctx->peer.connection); | |
599 ngx_destroy_pool(ctx->pool); | |
1136 | 600 ngx_mail_session_internal_server_error(s); |
567 | 601 return; |
602 } | |
603 | |
604 ngx_memcpy(s->login.data, ctx->header_start, s->login.len); | |
527 | 605 |
606 continue; | |
607 } | |
608 | |
800 | 609 if (len == sizeof("Auth-Pass") - 1 |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
610 && ngx_strncasecmp(ctx->header_name_start, |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
611 (u_char *) "Auth-Pass", |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
612 sizeof("Auth-Pass") - 1) |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
613 == 0) |
800 | 614 { |
615 s->passwd.len = ctx->header_end - ctx->header_start; | |
616 | |
2049 | 617 s->passwd.data = ngx_pnalloc(s->connection->pool, |
618 s->passwd.len); | |
800 | 619 if (s->passwd.data == NULL) { |
620 ngx_close_connection(ctx->peer.connection); | |
621 ngx_destroy_pool(ctx->pool); | |
1136 | 622 ngx_mail_session_internal_server_error(s); |
800 | 623 return; |
624 } | |
625 | |
626 ngx_memcpy(s->passwd.data, ctx->header_start, s->passwd.len); | |
627 | |
628 continue; | |
629 } | |
630 | |
527 | 631 if (len == sizeof("Auth-Wait") - 1 |
1107
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
632 && ngx_strncasecmp(ctx->header_name_start, |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
633 (u_char *) "Auth-Wait", |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
634 sizeof("Auth-Wait") - 1) |
db7c468c447d
ngx_strcasecmp()/ngx_strncasecmp()
Igor Sysoev <igor@sysoev.ru>
parents:
906
diff
changeset
|
635 == 0) |
527 | 636 { |
637 n = ngx_atoi(ctx->header_start, | |
638 ctx->header_end - ctx->header_start); | |
639 | |
640 if (n != NGX_ERROR) { | |
641 ctx->sleep = n; | |
642 } | |
643 | |
644 continue; | |
645 } | |
646 | |
1136 | 647 if (len == sizeof("Auth-Error-Code") - 1 |
648 && ngx_strncasecmp(ctx->header_name_start, | |
649 (u_char *) "Auth-Error-Code", | |
650 sizeof("Auth-Error-Code") - 1) | |
651 == 0) | |
652 { | |
653 ctx->errcode.len = ctx->header_end - ctx->header_start; | |
654 | |
2049 | 655 ctx->errcode.data = ngx_pnalloc(s->connection->pool, |
656 ctx->errcode.len); | |
1136 | 657 if (ctx->errcode.data == NULL) { |
658 ngx_close_connection(ctx->peer.connection); | |
659 ngx_destroy_pool(ctx->pool); | |
660 ngx_mail_session_internal_server_error(s); | |
661 return; | |
662 } | |
663 | |
664 ngx_memcpy(ctx->errcode.data, ctx->header_start, | |
665 ctx->errcode.len); | |
666 | |
667 continue; | |
668 } | |
669 | |
527 | 670 /* ignore other headers */ |
671 | |
672 continue; | |
673 } | |
674 | |
675 if (rc == NGX_DONE) { | |
1136 | 676 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
677 "mail auth http header done"); | |
527 | 678 |
679 ngx_close_connection(ctx->peer.connection); | |
680 | |
681 if (ctx->err.len) { | |
1136 | 682 |
567 | 683 ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, |
684 "client login failed: \"%V\"", &ctx->errmsg); | |
685 | |
1136 | 686 if (s->protocol == NGX_MAIL_SMTP_PROTOCOL) { |
687 | |
688 if (ctx->errcode.len == 0) { | |
689 ctx->errcode = ngx_mail_smtp_errcode; | |
690 } | |
691 | |
692 ctx->err.len = ctx->errcode.len + ctx->errmsg.len | |
693 + sizeof(" " CRLF) - 1; | |
694 | |
2049 | 695 p = ngx_pnalloc(s->connection->pool, ctx->err.len); |
1166 | 696 if (p == NULL) { |
697 ngx_close_connection(ctx->peer.connection); | |
698 ngx_destroy_pool(ctx->pool); | |
699 ngx_mail_session_internal_server_error(s); | |
700 return; | |
701 } | |
1136 | 702 |
1166 | 703 ctx->err.data = p; |
1136 | 704 |
1166 | 705 p = ngx_cpymem(p, ctx->errcode.data, ctx->errcode.len); |
1136 | 706 *p++ = ' '; |
1166 | 707 p = ngx_cpymem(p, ctx->errmsg.data, ctx->errmsg.len); |
1136 | 708 *p++ = CR; *p = LF; |
709 } | |
710 | |
539 | 711 s->out = ctx->err; |
547 | 712 timer = ctx->sleep; |
527 | 713 |
547 | 714 ngx_destroy_pool(ctx->pool); |
715 | |
716 if (timer == 0) { | |
539 | 717 s->quit = 1; |
1136 | 718 ngx_mail_send(s->connection->write); |
541 | 719 return; |
720 } | |
539 | 721 |
1640 | 722 ngx_add_timer(s->connection->read, (ngx_msec_t) (timer * 1000)); |
527 | 723 |
1136 | 724 s->connection->read->handler = ngx_mail_auth_sleep_handler; |
527 | 725 |
726 return; | |
727 } | |
728 | |
883 | 729 if (s->auth_wait) { |
730 timer = ctx->sleep; | |
731 | |
732 ngx_destroy_pool(ctx->pool); | |
733 | |
734 if (timer == 0) { | |
1136 | 735 ngx_mail_auth_http_init(s); |
883 | 736 return; |
737 } | |
738 | |
1640 | 739 ngx_add_timer(s->connection->read, (ngx_msec_t) (timer * 1000)); |
883 | 740 |
1136 | 741 s->connection->read->handler = ngx_mail_auth_sleep_handler; |
883 | 742 |
743 return; | |
744 } | |
745 | |
527 | 746 if (ctx->addr.len == 0 || ctx->port.len == 0) { |
747 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
541 | 748 "auth http server %V did not send server or port", |
884 | 749 ctx->peer.name); |
547 | 750 ngx_destroy_pool(ctx->pool); |
1136 | 751 ngx_mail_session_internal_server_error(s); |
527 | 752 return; |
753 } | |
754 | |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
755 if (s->passwd.data == NULL |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
756 && s->protocol != NGX_MAIL_SMTP_PROTOCOL) |
1136 | 757 { |
800 | 758 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, |
759 "auth http server %V did not send password", | |
884 | 760 ctx->peer.name); |
800 | 761 ngx_destroy_pool(ctx->pool); |
1136 | 762 ngx_mail_session_internal_server_error(s); |
800 | 763 return; |
764 } | |
765 | |
884 | 766 peer = ngx_pcalloc(s->connection->pool, sizeof(ngx_peer_addr_t)); |
767 if (peer == NULL) { | |
547 | 768 ngx_destroy_pool(ctx->pool); |
1136 | 769 ngx_mail_session_internal_server_error(s); |
527 | 770 return; |
771 } | |
772 | |
773 sin = ngx_pcalloc(s->connection->pool, sizeof(struct sockaddr_in)); | |
774 if (sin == NULL) { | |
547 | 775 ngx_destroy_pool(ctx->pool); |
1136 | 776 ngx_mail_session_internal_server_error(s); |
527 | 777 return; |
778 } | |
779 | |
780 sin->sin_family = AF_INET; | |
781 | |
782 port = ngx_atoi(ctx->port.data, ctx->port.len); | |
783 if (port == NGX_ERROR || port < 1 || port > 65536) { | |
784 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
541 | 785 "auth http server %V sent invalid server " |
786 "port:\"%V\"", | |
884 | 787 ctx->peer.name, &ctx->port); |
547 | 788 ngx_destroy_pool(ctx->pool); |
1136 | 789 ngx_mail_session_internal_server_error(s); |
527 | 790 return; |
791 } | |
792 | |
793 sin->sin_port = htons((in_port_t) port); | |
794 | |
795 ctx->addr.data[ctx->addr.len] = '\0'; | |
796 sin->sin_addr.s_addr = inet_addr((char *) ctx->addr.data); | |
797 if (sin->sin_addr.s_addr == INADDR_NONE) { | |
798 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
541 | 799 "auth http server %V sent invalid server " |
800 "address:\"%V\"", | |
884 | 801 ctx->peer.name, &ctx->addr); |
547 | 802 ngx_destroy_pool(ctx->pool); |
1136 | 803 ngx_mail_session_internal_server_error(s); |
527 | 804 return; |
805 } | |
806 | |
884 | 807 peer->sockaddr = (struct sockaddr *) sin; |
808 peer->socklen = sizeof(struct sockaddr_in); | |
527 | 809 |
810 len = ctx->addr.len + 1 + ctx->port.len; | |
811 | |
884 | 812 peer->name.len = len; |
527 | 813 |
2049 | 814 peer->name.data = ngx_pnalloc(s->connection->pool, len); |
884 | 815 if (peer->name.data == NULL) { |
547 | 816 ngx_destroy_pool(ctx->pool); |
1136 | 817 ngx_mail_session_internal_server_error(s); |
527 | 818 return; |
819 } | |
820 | |
821 len = ctx->addr.len; | |
822 | |
884 | 823 ngx_memcpy(peer->name.data, ctx->addr.data, len); |
527 | 824 |
884 | 825 peer->name.data[len++] = ':'; |
527 | 826 |
884 | 827 ngx_memcpy(peer->name.data + len, ctx->port.data, ctx->port.len); |
527 | 828 |
547 | 829 ngx_destroy_pool(ctx->pool); |
1136 | 830 ngx_mail_proxy_init(s, peer); |
527 | 831 |
832 return; | |
833 } | |
834 | |
835 if (rc == NGX_AGAIN ) { | |
836 return; | |
837 } | |
838 | |
839 /* rc == NGX_ERROR */ | |
840 | |
841 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
541 | 842 "auth http server %V sent invalid header in response", |
884 | 843 ctx->peer.name); |
527 | 844 ngx_close_connection(ctx->peer.connection); |
547 | 845 ngx_destroy_pool(ctx->pool); |
1136 | 846 ngx_mail_session_internal_server_error(s); |
527 | 847 |
848 return; | |
849 } | |
850 } | |
851 | |
521 | 852 |
527 | 853 static void |
1136 | 854 ngx_mail_auth_sleep_handler(ngx_event_t *rev) |
527 | 855 { |
543 | 856 ngx_connection_t *c; |
1136 | 857 ngx_mail_session_t *s; |
858 ngx_mail_core_srv_conf_t *cscf; | |
527 | 859 |
1136 | 860 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail auth sleep handler"); |
527 | 861 |
862 c = rev->data; | |
863 s = c->data; | |
864 | |
865 if (rev->timedout) { | |
866 | |
867 rev->timedout = 0; | |
868 | |
883 | 869 if (s->auth_wait) { |
870 s->auth_wait = 0; | |
1136 | 871 ngx_mail_auth_http_init(s); |
883 | 872 return; |
873 } | |
874 | |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
875 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); |
527 | 876 |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
877 rev->handler = cscf->protocol->auth_state; |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
878 |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
879 s->mail_state = 0; |
1136 | 880 s->auth_method = NGX_MAIL_AUTH_PLAIN; |
800 | 881 |
543 | 882 c->log->action = "in auth state"; |
883 | |
1477 | 884 ngx_mail_send(c->write); |
543 | 885 |
583 | 886 if (c->destroyed) { |
543 | 887 return; |
888 } | |
889 | |
890 ngx_add_timer(rev, cscf->timeout); | |
891 | |
527 | 892 if (rev->ready) { |
1477 | 893 rev->handler(rev); |
527 | 894 return; |
895 } | |
896 | |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2310
diff
changeset
|
897 if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
1477 | 898 ngx_mail_close_connection(c); |
527 | 899 } |
900 | |
901 return; | |
902 } | |
903 | |
904 if (rev->active) { | |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2310
diff
changeset
|
905 if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
1477 | 906 ngx_mail_close_connection(c); |
527 | 907 } |
908 } | |
909 } | |
910 | |
911 | |
912 static ngx_int_t | |
1136 | 913 ngx_mail_auth_http_parse_header_line(ngx_mail_session_t *s, |
914 ngx_mail_auth_http_ctx_t *ctx) | |
527 | 915 { |
916 u_char c, ch, *p; | |
917 enum { | |
918 sw_start = 0, | |
919 sw_name, | |
920 sw_space_before_value, | |
921 sw_value, | |
922 sw_space_after_value, | |
577 | 923 sw_almost_done, |
527 | 924 sw_header_almost_done |
925 } state; | |
926 | |
577 | 927 state = ctx->state; |
527 | 928 |
929 for (p = ctx->response->pos; p < ctx->response->last; p++) { | |
930 ch = *p; | |
931 | |
932 switch (state) { | |
933 | |
934 /* first char */ | |
935 case sw_start: | |
936 | |
937 switch (ch) { | |
938 case CR: | |
577 | 939 ctx->header_end = p; |
527 | 940 state = sw_header_almost_done; |
941 break; | |
577 | 942 case LF: |
527 | 943 ctx->header_end = p; |
944 goto header_done; | |
945 default: | |
946 state = sw_name; | |
947 ctx->header_name_start = p; | |
948 | |
949 c = (u_char) (ch | 0x20); | |
950 if (c >= 'a' && c <= 'z') { | |
951 break; | |
952 } | |
953 | |
954 if (ch >= '0' && ch <= '9') { | |
955 break; | |
956 } | |
957 | |
958 return NGX_ERROR; | |
959 } | |
960 break; | |
961 | |
962 /* header name */ | |
963 case sw_name: | |
964 c = (u_char) (ch | 0x20); | |
965 if (c >= 'a' && c <= 'z') { | |
966 break; | |
967 } | |
968 | |
969 if (ch == ':') { | |
970 ctx->header_name_end = p; | |
971 state = sw_space_before_value; | |
972 break; | |
973 } | |
974 | |
975 if (ch == '-') { | |
976 break; | |
977 } | |
978 | |
979 if (ch >= '0' && ch <= '9') { | |
980 break; | |
981 } | |
982 | |
983 if (ch == CR) { | |
984 ctx->header_name_end = p; | |
985 ctx->header_start = p; | |
986 ctx->header_end = p; | |
987 state = sw_almost_done; | |
988 break; | |
989 } | |
990 | |
991 if (ch == LF) { | |
992 ctx->header_name_end = p; | |
993 ctx->header_start = p; | |
994 ctx->header_end = p; | |
995 goto done; | |
996 } | |
997 | |
998 return NGX_ERROR; | |
999 | |
1000 /* space* before header value */ | |
1001 case sw_space_before_value: | |
1002 switch (ch) { | |
1003 case ' ': | |
1004 break; | |
1005 case CR: | |
1006 ctx->header_start = p; | |
1007 ctx->header_end = p; | |
1008 state = sw_almost_done; | |
1009 break; | |
1010 case LF: | |
1011 ctx->header_start = p; | |
1012 ctx->header_end = p; | |
1013 goto done; | |
1014 default: | |
1015 ctx->header_start = p; | |
1016 state = sw_value; | |
1017 break; | |
1018 } | |
1019 break; | |
1020 | |
1021 /* header value */ | |
1022 case sw_value: | |
1023 switch (ch) { | |
1024 case ' ': | |
1025 ctx->header_end = p; | |
1026 state = sw_space_after_value; | |
1027 break; | |
1028 case CR: | |
1029 ctx->header_end = p; | |
1030 state = sw_almost_done; | |
1031 break; | |
1032 case LF: | |
1033 ctx->header_end = p; | |
1034 goto done; | |
1035 } | |
1036 break; | |
1037 | |
1038 /* space* before end of header line */ | |
1039 case sw_space_after_value: | |
1040 switch (ch) { | |
1041 case ' ': | |
1042 break; | |
1043 case CR: | |
1044 state = sw_almost_done; | |
1045 break; | |
1046 case LF: | |
1047 goto done; | |
1048 default: | |
1049 state = sw_value; | |
1050 break; | |
1051 } | |
1052 break; | |
1053 | |
1054 /* end of header line */ | |
1055 case sw_almost_done: | |
1056 switch (ch) { | |
1057 case LF: | |
1058 goto done; | |
1059 default: | |
1060 return NGX_ERROR; | |
1061 } | |
1062 | |
1063 /* end of header */ | |
1064 case sw_header_almost_done: | |
1065 switch (ch) { | |
1066 case LF: | |
1067 goto header_done; | |
1068 default: | |
1069 return NGX_ERROR; | |
1070 } | |
1071 } | |
1072 } | |
1073 | |
1074 ctx->response->pos = p; | |
1075 ctx->state = state; | |
1076 | |
1077 return NGX_AGAIN; | |
1078 | |
1079 done: | |
1080 | |
1081 ctx->response->pos = p + 1; | |
1082 ctx->state = sw_start; | |
1083 | |
1084 return NGX_OK; | |
1085 | |
1086 header_done: | |
1087 | |
1088 ctx->response->pos = p + 1; | |
1089 ctx->state = sw_start; | |
1090 | |
1091 return NGX_DONE; | |
521 | 1092 } |
1093 | |
1094 | |
1095 static void | |
1136 | 1096 ngx_mail_auth_http_block_read(ngx_event_t *rev) |
521 | 1097 { |
1098 ngx_connection_t *c; | |
1136 | 1099 ngx_mail_session_t *s; |
1100 ngx_mail_auth_http_ctx_t *ctx; | |
521 | 1101 |
1136 | 1102 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, |
1103 "mail auth http block read"); | |
521 | 1104 |
2388
722b5aff05ae
use "!= NGX_OK" instead of "== NGX_ERROR"
Igor Sysoev <igor@sysoev.ru>
parents:
2310
diff
changeset
|
1105 if (ngx_handle_read_event(rev, 0) != NGX_OK) { |
521 | 1106 c = rev->data; |
1107 s = c->data; | |
1108 | |
1136 | 1109 ctx = ngx_mail_get_module_ctx(s, ngx_mail_auth_http_module); |
521 | 1110 |
525 | 1111 ngx_close_connection(ctx->peer.connection); |
547 | 1112 ngx_destroy_pool(ctx->pool); |
1136 | 1113 ngx_mail_session_internal_server_error(s); |
521 | 1114 } |
1115 } | |
1116 | |
1117 | |
1118 static void | |
1136 | 1119 ngx_mail_auth_http_dummy_handler(ngx_event_t *ev) |
521 | 1120 { |
1136 | 1121 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, ev->log, 0, |
1122 "mail auth http dummy handler"); | |
521 | 1123 } |
1124 | |
1125 | |
1126 static ngx_buf_t * | |
1136 | 1127 ngx_mail_auth_http_create_request(ngx_mail_session_t *s, ngx_pool_t *pool, |
1128 ngx_mail_auth_http_conf_t *ahcf) | |
521 | 1129 { |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
1130 size_t len; |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
1131 ngx_buf_t *b; |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
1132 ngx_str_t login, passwd; |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
1133 ngx_mail_core_srv_conf_t *cscf; |
633 | 1134 |
1136 | 1135 if (ngx_mail_auth_http_escape(pool, &s->login, &login) != NGX_OK) { |
633 | 1136 return NULL; |
1137 } | |
1138 | |
1136 | 1139 if (ngx_mail_auth_http_escape(pool, &s->passwd, &passwd) != NGX_OK) { |
633 | 1140 return NULL; |
1141 } | |
521 | 1142 |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
1143 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
1144 |
521 | 1145 len = sizeof("GET ") - 1 + ahcf->uri.len + sizeof(" HTTP/1.0" CRLF) - 1 |
1146 + sizeof("Host: ") - 1 + ahcf->host_header.len + sizeof(CRLF) - 1 | |
856
0197d6aae54e
use correct auth method length
Igor Sysoev <igor@sysoev.ru>
parents:
854
diff
changeset
|
1147 + sizeof("Auth-Method: ") - 1 |
1136 | 1148 + ngx_mail_auth_http_method[s->auth_method].len |
856
0197d6aae54e
use correct auth method length
Igor Sysoev <igor@sysoev.ru>
parents:
854
diff
changeset
|
1149 + sizeof(CRLF) - 1 |
633 | 1150 + sizeof("Auth-User: ") - 1 + login.len + sizeof(CRLF) - 1 |
1151 + sizeof("Auth-Pass: ") - 1 + passwd.len + sizeof(CRLF) - 1 | |
800 | 1152 + sizeof("Auth-Salt: ") - 1 + s->salt.len |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
1153 + sizeof("Auth-Protocol: ") - 1 + cscf->protocol->name.len |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
1154 + sizeof(CRLF) - 1 |
527 | 1155 + sizeof("Auth-Login-Attempt: ") - 1 + NGX_INT_T_LEN |
1156 + sizeof(CRLF) - 1 | |
521 | 1157 + sizeof("Client-IP: ") - 1 + s->connection->addr_text.len |
1158 + sizeof(CRLF) - 1 | |
2309 | 1159 + sizeof("Client-Host: ") - 1 + s->host.len + sizeof(CRLF) - 1 |
1160 + sizeof("Auth-SMTP-Helo: ") - 1 + s->smtp_helo.len | |
1161 + sizeof("Auth-SMTP-From: ") - 1 + s->smtp_from.len | |
1162 + sizeof("Auth-SMTP-To: ") - 1 + s->smtp_to.len | |
1285
0c10dc6a8e74
fix memory allocation for auth_http_header
Igor Sysoev <igor@sysoev.ru>
parents:
1166
diff
changeset
|
1163 + ahcf->header.len |
521 | 1164 + sizeof(CRLF) - 1; |
1165 | |
547 | 1166 b = ngx_create_temp_buf(pool, len); |
521 | 1167 if (b == NULL) { |
1168 return NULL; | |
1169 } | |
1170 | |
1171 b->last = ngx_cpymem(b->last, "GET ", sizeof("GET ") - 1); | |
573 | 1172 b->last = ngx_copy(b->last, ahcf->uri.data, ahcf->uri.len); |
521 | 1173 b->last = ngx_cpymem(b->last, " HTTP/1.0" CRLF, |
1174 sizeof(" HTTP/1.0" CRLF) - 1); | |
1175 | |
1176 b->last = ngx_cpymem(b->last, "Host: ", sizeof("Host: ") - 1); | |
573 | 1177 b->last = ngx_copy(b->last, ahcf->host_header.data, |
521 | 1178 ahcf->host_header.len); |
1179 *b->last++ = CR; *b->last++ = LF; | |
1180 | |
800 | 1181 b->last = ngx_cpymem(b->last, "Auth-Method: ", |
1182 sizeof("Auth-Method: ") - 1); | |
1183 b->last = ngx_cpymem(b->last, | |
1136 | 1184 ngx_mail_auth_http_method[s->auth_method].data, |
1185 ngx_mail_auth_http_method[s->auth_method].len); | |
800 | 1186 *b->last++ = CR; *b->last++ = LF; |
521 | 1187 |
1188 b->last = ngx_cpymem(b->last, "Auth-User: ", sizeof("Auth-User: ") - 1); | |
633 | 1189 b->last = ngx_copy(b->last, login.data, login.len); |
521 | 1190 *b->last++ = CR; *b->last++ = LF; |
1191 | |
1192 b->last = ngx_cpymem(b->last, "Auth-Pass: ", sizeof("Auth-Pass: ") - 1); | |
633 | 1193 b->last = ngx_copy(b->last, passwd.data, passwd.len); |
521 | 1194 *b->last++ = CR; *b->last++ = LF; |
1195 | |
1136 | 1196 if (s->auth_method != NGX_MAIL_AUTH_PLAIN && s->salt.len) { |
800 | 1197 b->last = ngx_cpymem(b->last, "Auth-Salt: ", sizeof("Auth-Salt: ") - 1); |
1198 b->last = ngx_copy(b->last, s->salt.data, s->salt.len); | |
1199 | |
1200 s->passwd.data = NULL; | |
1201 } | |
1202 | |
521 | 1203 b->last = ngx_cpymem(b->last, "Auth-Protocol: ", |
1204 sizeof("Auth-Protocol: ") - 1); | |
1487
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
1205 b->last = ngx_cpymem(b->last, cscf->protocol->name.data, |
f69493e8faab
ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
Igor Sysoev <igor@sysoev.ru>
parents:
1478
diff
changeset
|
1206 cscf->protocol->name.len); |
521 | 1207 *b->last++ = CR; *b->last++ = LF; |
1208 | |
527 | 1209 b->last = ngx_sprintf(b->last, "Auth-Login-Attempt: %ui" CRLF, |
1210 s->login_attempt); | |
1211 | |
521 | 1212 b->last = ngx_cpymem(b->last, "Client-IP: ", sizeof("Client-IP: ") - 1); |
573 | 1213 b->last = ngx_copy(b->last, s->connection->addr_text.data, |
2309 | 1214 s->connection->addr_text.len); |
521 | 1215 *b->last++ = CR; *b->last++ = LF; |
1216 | |
2309 | 1217 if (s->host.len) { |
1218 b->last = ngx_cpymem(b->last, "Client-Host: ", | |
1219 sizeof("Client-Host: ") - 1); | |
1220 b->last = ngx_copy(b->last, s->host.data, s->host.len); | |
1221 *b->last++ = CR; *b->last++ = LF; | |
1222 } | |
1223 | |
1224 if (s->auth_method == NGX_MAIL_AUTH_NONE) { | |
1225 | |
1226 /* HELO, MAIL FROM, and RCPT TO can't contain CRLF, no need to escape */ | |
1227 | |
1228 b->last = ngx_cpymem(b->last, "Auth-SMTP-Helo: ", | |
1229 sizeof("Auth-SMTP-Helo: ") - 1); | |
1230 b->last = ngx_copy(b->last, s->smtp_helo.data, s->smtp_helo.len); | |
1231 *b->last++ = CR; *b->last++ = LF; | |
1232 | |
1233 b->last = ngx_cpymem(b->last, "Auth-SMTP-From: ", | |
1234 sizeof("Auth-SMTP-From: ") - 1); | |
1235 b->last = ngx_copy(b->last, s->smtp_from.data, s->smtp_from.len); | |
1236 *b->last++ = CR; *b->last++ = LF; | |
1237 | |
1238 b->last = ngx_cpymem(b->last, "Auth-SMTP-To: ", | |
1239 sizeof("Auth-SMTP-To: ") - 1); | |
1240 b->last = ngx_copy(b->last, s->smtp_to.data, s->smtp_to.len); | |
1241 *b->last++ = CR; *b->last++ = LF; | |
1242 | |
1243 } | |
1244 | |
573 | 1245 if (ahcf->header.len) { |
1246 b->last = ngx_copy(b->last, ahcf->header.data, ahcf->header.len); | |
1247 } | |
1248 | |
521 | 1249 /* add "\r\n" at the header end */ |
1250 *b->last++ = CR; *b->last++ = LF; | |
1251 | |
1136 | 1252 #if (NGX_DEBUG_MAIL_PASSWD) |
521 | 1253 { |
1254 ngx_str_t l; | |
1255 | |
1256 l.len = b->last - b->pos; | |
1257 l.data = b->pos; | |
1136 | 1258 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, |
1259 "mail auth http header:\n\"%V\"", &l); | |
521 | 1260 } |
1261 #endif | |
1262 | |
1263 return b; | |
1264 } | |
1265 | |
1266 | |
633 | 1267 static ngx_int_t |
1136 | 1268 ngx_mail_auth_http_escape(ngx_pool_t *pool, ngx_str_t *text, ngx_str_t *escaped) |
633 | 1269 { |
1405
fdd064faf26a
escape " ", "%", and %00-%1F in login and password
Igor Sysoev <igor@sysoev.ru>
parents:
1392
diff
changeset
|
1270 u_char *p; |
fdd064faf26a
escape " ", "%", and %00-%1F in login and password
Igor Sysoev <igor@sysoev.ru>
parents:
1392
diff
changeset
|
1271 uintptr_t n; |
633 | 1272 |
1405
fdd064faf26a
escape " ", "%", and %00-%1F in login and password
Igor Sysoev <igor@sysoev.ru>
parents:
1392
diff
changeset
|
1273 n = ngx_escape_uri(NULL, text->data, text->len, NGX_ESCAPE_MAIL_AUTH); |
633 | 1274 |
1275 if (n == 0) { | |
1276 *escaped = *text; | |
1277 return NGX_OK; | |
1278 } | |
1279 | |
1280 escaped->len = text->len + n * 2; | |
1281 | |
2049 | 1282 p = ngx_pnalloc(pool, escaped->len); |
633 | 1283 if (p == NULL) { |
1284 return NGX_ERROR; | |
1285 } | |
1286 | |
1405
fdd064faf26a
escape " ", "%", and %00-%1F in login and password
Igor Sysoev <igor@sysoev.ru>
parents:
1392
diff
changeset
|
1287 (void) ngx_escape_uri(p, text->data, text->len, NGX_ESCAPE_MAIL_AUTH); |
633 | 1288 |
1405
fdd064faf26a
escape " ", "%", and %00-%1F in login and password
Igor Sysoev <igor@sysoev.ru>
parents:
1392
diff
changeset
|
1289 escaped->data = p; |
633 | 1290 |
1291 return NGX_OK; | |
1292 } | |
1293 | |
1294 | |
521 | 1295 static void * |
1136 | 1296 ngx_mail_auth_http_create_conf(ngx_conf_t *cf) |
577 | 1297 { |
1136 | 1298 ngx_mail_auth_http_conf_t *ahcf; |
577 | 1299 |
1136 | 1300 ahcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_auth_http_conf_t)); |
521 | 1301 if (ahcf == NULL) { |
1302 return NGX_CONF_ERROR; | |
1303 } | |
1304 | |
1305 ahcf->timeout = NGX_CONF_UNSET_MSEC; | |
1306 | |
1392 | 1307 ahcf->file = cf->conf_file->file.name.data; |
1308 ahcf->line = cf->conf_file->line; | |
1309 | |
521 | 1310 return ahcf; |
1311 } | |
1312 | |
1313 | |
1314 static char * | |
1136 | 1315 ngx_mail_auth_http_merge_conf(ngx_conf_t *cf, void *parent, void *child) |
521 | 1316 { |
1136 | 1317 ngx_mail_auth_http_conf_t *prev = parent; |
1318 ngx_mail_auth_http_conf_t *conf = child; | |
521 | 1319 |
573 | 1320 u_char *p; |
1321 size_t len; | |
1322 ngx_uint_t i; | |
1323 ngx_table_elt_t *header; | |
1324 | |
884 | 1325 if (conf->peer == NULL) { |
1326 conf->peer = prev->peer; | |
521 | 1327 conf->host_header = prev->host_header; |
1328 conf->uri = prev->uri; | |
1392 | 1329 |
1330 if (conf->peer == NULL) { | |
1331 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
1332 "no \"http_auth\" is defined for server in %s:%ui", | |
1333 conf->file, conf->line); | |
1334 | |
1335 return NGX_CONF_ERROR; | |
1336 } | |
521 | 1337 } |
1338 | |
1339 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000); | |
1340 | |
573 | 1341 if (conf->headers == NULL) { |
1342 conf->headers = prev->headers; | |
1343 conf->header = prev->header; | |
1344 } | |
1345 | |
1346 if (conf->headers && conf->header.len == 0) { | |
1347 len = 0; | |
1348 header = conf->headers->elts; | |
1349 for (i = 0; i < conf->headers->nelts; i++) { | |
1350 len += header[i].key.len + 2 + header[i].value.len + 2; | |
1351 } | |
1352 | |
2049 | 1353 p = ngx_pnalloc(cf->pool, len); |
573 | 1354 if (p == NULL) { |
1355 return NGX_CONF_ERROR; | |
1356 } | |
1357 | |
1358 conf->header.len = len; | |
1359 conf->header.data = p; | |
1360 | |
1361 for (i = 0; i < conf->headers->nelts; i++) { | |
1362 p = ngx_cpymem(p, header[i].key.data, header[i].key.len); | |
1363 *p++ = ':'; *p++ = ' '; | |
1364 p = ngx_cpymem(p, header[i].value.data, header[i].value.len); | |
1365 *p++ = CR; *p++ = LF; | |
1366 } | |
1367 } | |
1368 | |
521 | 1369 return NGX_CONF_OK; |
1370 } | |
1371 | |
1372 | |
1373 static char * | |
1136 | 1374 ngx_mail_auth_http(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
577 | 1375 { |
1136 | 1376 ngx_mail_auth_http_conf_t *ahcf = conf; |
521 | 1377 |
805
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1378 ngx_str_t *value; |
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1379 ngx_url_t u; |
573 | 1380 |
521 | 1381 value = cf->args->elts; |
1382 | |
805
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1383 ngx_memzero(&u, sizeof(ngx_url_t)); |
521 | 1384 |
805
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1385 u.url = value[1]; |
906 | 1386 u.default_port = 80; |
805
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1387 u.uri_part = 1; |
884 | 1388 u.one_addr = 1; |
577 | 1389 |
1391
4eed21047e4d
allow "http://" in auth_http URL
Igor Sysoev <igor@sysoev.ru>
parents:
1390
diff
changeset
|
1390 if (ngx_strncmp(u.url.data, "http://", 7) == 0) { |
4eed21047e4d
allow "http://" in auth_http URL
Igor Sysoev <igor@sysoev.ru>
parents:
1390
diff
changeset
|
1391 u.url.len -= 7; |
4eed21047e4d
allow "http://" in auth_http URL
Igor Sysoev <igor@sysoev.ru>
parents:
1390
diff
changeset
|
1392 u.url.data += 7; |
4eed21047e4d
allow "http://" in auth_http URL
Igor Sysoev <igor@sysoev.ru>
parents:
1390
diff
changeset
|
1393 } |
4eed21047e4d
allow "http://" in auth_http URL
Igor Sysoev <igor@sysoev.ru>
parents:
1390
diff
changeset
|
1394 |
1559
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1487
diff
changeset
|
1395 if (ngx_parse_url(cf->pool, &u) != NGX_OK) { |
805
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1396 if (u.err) { |
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1397 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1398 "%s in auth_http \"%V\"", u.err, &u.url); |
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1399 } |
1390 | 1400 |
1401 return NGX_CONF_ERROR; | |
805
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1402 } |
521 | 1403 |
884 | 1404 ahcf->peer = u.addrs; |
521 | 1405 |
906 | 1406 ahcf->host_header = u.host; |
805
8ee450f30c25
now the "auth_http" directive uses ngx_parse_url()
Igor Sysoev <igor@sysoev.ru>
parents:
800
diff
changeset
|
1407 ahcf->uri = u.uri; |
521 | 1408 |
559 | 1409 if (ahcf->uri.len == 0) { |
555 | 1410 ahcf->uri.len = sizeof("/") - 1; |
1411 ahcf->uri.data = (u_char *) "/"; | |
1412 } | |
1413 | |
521 | 1414 return NGX_CONF_OK; |
1415 } | |
573 | 1416 |
1417 | |
1418 static char * | |
1136 | 1419 ngx_mail_auth_http_header(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
577 | 1420 { |
1136 | 1421 ngx_mail_auth_http_conf_t *ahcf = conf; |
573 | 1422 |
1423 ngx_str_t *value; | |
1424 ngx_table_elt_t *header; | |
1425 | |
1426 if (ahcf->headers == NULL) { | |
1427 ahcf->headers = ngx_array_create(cf->pool, 1, sizeof(ngx_table_elt_t)); | |
1428 if (ahcf->headers == NULL) { | |
1429 return NGX_CONF_ERROR; | |
1430 } | |
1431 } | |
1432 | |
1433 header = ngx_array_push(ahcf->headers); | |
1434 if (header == NULL) { | |
1435 return NGX_CONF_ERROR; | |
1436 } | |
1437 | |
1438 value = cf->args->elts; | |
1439 | |
1440 header->key = value[1]; | |
1441 header->value = value[2]; | |
1442 | |
1443 return NGX_CONF_OK; | |
1444 } |