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