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