comparison src/imap/ngx_imap_auth_http_module.c @ 96:ca4f70b3ccc6 NGINX_0_2_2

nginx 0.2.2 *) Feature: the "config errmsg" command of the ngx_http_ssi_module. *) Change: the ngx_http_geo_module variables can be overridden by the "set" directive. *) Feature: the "ssl_protocols" and "ssl_prefer_server_ciphers" directives of the ngx_http_ssl_module and ngx_imap_ssl_module. *) Bugfix: the ngx_http_autoindex_module did not show correctly the long file names; *) Bugfix: the ngx_http_autoindex_module now do not show the files starting by dot. *) Bugfix: if the SSL handshake failed then another connection may be closed too. Thanks to Rob Mueller. *) Bugfix: the export versions of MSIE 5.x could not connect via HTTPS.
author Igor Sysoev <http://sysoev.ru>
date Fri, 30 Sep 2005 00:00:00 +0400
parents 45945fa8b8ba
children 146eff53ab60
comparison
equal deleted inserted replaced
95:2f95911bc4b4 96:ca4f70b3ccc6
43 43
44 ngx_str_t addr; 44 ngx_str_t addr;
45 ngx_str_t port; 45 ngx_str_t port;
46 ngx_str_t err; 46 ngx_str_t err;
47 47
48 ngx_msec_t sleep; 48 time_t sleep;
49 49
50 ngx_peers_t *peers; 50 ngx_pool_t *pool;
51 }; 51 };
52 52
53 53
54 static void ngx_imap_auth_http_write_handler(ngx_event_t *wev); 54 static void ngx_imap_auth_http_write_handler(ngx_event_t *wev);
55 static void ngx_imap_auth_http_read_handler(ngx_event_t *rev); 55 static void ngx_imap_auth_http_read_handler(ngx_event_t *rev);
61 static ngx_int_t ngx_imap_auth_http_parse_header_line(ngx_imap_session_t *s, 61 static ngx_int_t ngx_imap_auth_http_parse_header_line(ngx_imap_session_t *s,
62 ngx_imap_auth_http_ctx_t *ctx); 62 ngx_imap_auth_http_ctx_t *ctx);
63 static void ngx_imap_auth_http_block_read(ngx_event_t *rev); 63 static void ngx_imap_auth_http_block_read(ngx_event_t *rev);
64 static void ngx_imap_auth_http_dummy_handler(ngx_event_t *ev); 64 static void ngx_imap_auth_http_dummy_handler(ngx_event_t *ev);
65 static ngx_buf_t *ngx_imap_auth_http_create_request(ngx_imap_session_t *s, 65 static ngx_buf_t *ngx_imap_auth_http_create_request(ngx_imap_session_t *s,
66 ngx_imap_auth_http_conf_t *ahcf); 66 ngx_pool_t *pool, ngx_imap_auth_http_conf_t *ahcf);
67 67
68 static void *ngx_imap_auth_http_create_conf(ngx_conf_t *cf); 68 static void *ngx_imap_auth_http_create_conf(ngx_conf_t *cf);
69 static char *ngx_imap_auth_http_merge_conf(ngx_conf_t *cf, void *parent, 69 static char *ngx_imap_auth_http_merge_conf(ngx_conf_t *cf, void *parent,
70 void *child); 70 void *child);
71 static char *ngx_imap_auth_http(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 71 static char *ngx_imap_auth_http(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
121 121
122 void 122 void
123 ngx_imap_auth_http_init(ngx_imap_session_t *s) 123 ngx_imap_auth_http_init(ngx_imap_session_t *s)
124 { 124 {
125 ngx_int_t rc; 125 ngx_int_t rc;
126 ngx_pool_t *pool;
126 ngx_imap_auth_http_ctx_t *ctx; 127 ngx_imap_auth_http_ctx_t *ctx;
127 ngx_imap_auth_http_conf_t *ahcf; 128 ngx_imap_auth_http_conf_t *ahcf;
128 129
129 s->connection->log->action = "in http auth state"; 130 s->connection->log->action = "in http auth state";
130 131
131 ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_imap_auth_http_ctx_t)); 132 pool = ngx_create_pool(2048, s->connection->log);
132 if (ctx == NULL) { 133 if (pool == NULL) {
133 ngx_imap_session_internal_server_error(s); 134 ngx_imap_session_internal_server_error(s);
134 return; 135 return;
135 } 136 }
136 137
138 ctx = ngx_pcalloc(pool, sizeof(ngx_imap_auth_http_ctx_t));
139 if (ctx == NULL) {
140 ngx_destroy_pool(pool);
141 ngx_imap_session_internal_server_error(s);
142 return;
143 }
144
145 ctx->pool = pool;
146
137 ahcf = ngx_imap_get_module_srv_conf(s, ngx_imap_auth_http_module); 147 ahcf = ngx_imap_get_module_srv_conf(s, ngx_imap_auth_http_module);
138 148
139 ctx->request = ngx_imap_auth_http_create_request(s, ahcf); 149 ctx->request = ngx_imap_auth_http_create_request(s, pool, ahcf);
140 if (ctx->request == NULL) { 150 if (ctx->request == NULL) {
151 ngx_destroy_pool(ctx->pool);
141 ngx_imap_session_internal_server_error(s); 152 ngx_imap_session_internal_server_error(s);
142 return; 153 return;
143 } 154 }
144 155
145 ngx_imap_set_ctx(s, ctx, ngx_imap_auth_http_module); 156 ngx_imap_set_ctx(s, ctx, ngx_imap_auth_http_module);
150 161
151 rc = ngx_event_connect_peer(&ctx->peer); 162 rc = ngx_event_connect_peer(&ctx->peer);
152 163
153 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) { 164 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) {
154 ngx_close_connection(ctx->peer.connection); 165 ngx_close_connection(ctx->peer.connection);
166 ngx_destroy_pool(ctx->pool);
155 ngx_imap_session_internal_server_error(s); 167 ngx_imap_session_internal_server_error(s);
156 return; 168 return;
157 } 169 }
158 170
159 ctx->peer.connection->data = s; 171 ctx->peer.connection->data = s;
195 if (wev->timedout) { 207 if (wev->timedout) {
196 ngx_log_error(NGX_LOG_ERR, wev->log, NGX_ETIMEDOUT, 208 ngx_log_error(NGX_LOG_ERR, wev->log, NGX_ETIMEDOUT,
197 "auth http server %V timed out", 209 "auth http server %V timed out",
198 &ctx->peer.peers->peer[0].name); 210 &ctx->peer.peers->peer[0].name);
199 ngx_close_connection(ctx->peer.connection); 211 ngx_close_connection(ctx->peer.connection);
212 ngx_destroy_pool(ctx->pool);
200 ngx_imap_session_internal_server_error(s); 213 ngx_imap_session_internal_server_error(s);
201 return; 214 return;
202 } 215 }
203 216
204 size = ctx->request->last - ctx->request->pos; 217 size = ctx->request->last - ctx->request->pos;
205 218
206 n = ngx_send(c, ctx->request->pos, size); 219 n = ngx_send(c, ctx->request->pos, size);
207 220
208 if (n == NGX_ERROR) { 221 if (n == NGX_ERROR) {
209 ngx_close_connection(ctx->peer.connection); 222 ngx_close_connection(ctx->peer.connection);
223 ngx_destroy_pool(ctx->pool);
210 ngx_imap_session_internal_server_error(s); 224 ngx_imap_session_internal_server_error(s);
211 return; 225 return;
212 } 226 }
213 227
214 if (n > 0) { 228 if (n > 0) {
251 if (rev->timedout) { 265 if (rev->timedout) {
252 ngx_log_error(NGX_LOG_ERR, rev->log, NGX_ETIMEDOUT, 266 ngx_log_error(NGX_LOG_ERR, rev->log, NGX_ETIMEDOUT,
253 "auth http server %V timed out", 267 "auth http server %V timed out",
254 &ctx->peer.peers->peer[0].name); 268 &ctx->peer.peers->peer[0].name);
255 ngx_close_connection(ctx->peer.connection); 269 ngx_close_connection(ctx->peer.connection);
270 ngx_destroy_pool(ctx->pool);
256 ngx_imap_session_internal_server_error(s); 271 ngx_imap_session_internal_server_error(s);
257 return; 272 return;
258 } 273 }
259 274
260 if (ctx->response == NULL) { 275 if (ctx->response == NULL) {
261 ctx->response = ngx_create_temp_buf(s->connection->pool, 1024); 276 ctx->response = ngx_create_temp_buf(ctx->pool, 1024);
262 if (ctx->response == NULL) { 277 if (ctx->response == NULL) {
263 ngx_close_connection(ctx->peer.connection); 278 ngx_close_connection(ctx->peer.connection);
279 ngx_destroy_pool(ctx->pool);
264 ngx_imap_session_internal_server_error(s); 280 ngx_imap_session_internal_server_error(s);
265 return; 281 return;
266 } 282 }
267 } 283 }
268 284
280 if (n == NGX_AGAIN) { 296 if (n == NGX_AGAIN) {
281 return; 297 return;
282 } 298 }
283 299
284 ngx_close_connection(ctx->peer.connection); 300 ngx_close_connection(ctx->peer.connection);
301 ngx_destroy_pool(ctx->pool);
285 ngx_imap_session_internal_server_error(s); 302 ngx_imap_session_internal_server_error(s);
286 } 303 }
287 304
288 305
289 static void 306 static void
367 384
368 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, 385 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
369 "auth http server &V sent invalid response", 386 "auth http server &V sent invalid response",
370 &ctx->peer.peers->peer[0].name); 387 &ctx->peer.peers->peer[0].name);
371 ngx_close_connection(ctx->peer.connection); 388 ngx_close_connection(ctx->peer.connection);
389 ngx_destroy_pool(ctx->pool);
372 ngx_imap_session_internal_server_error(s); 390 ngx_imap_session_internal_server_error(s);
373 return; 391 return;
374 } 392 }
375 } 393 }
376 394
395 static void 413 static void
396 ngx_imap_auth_http_process_headers(ngx_imap_session_t *s, 414 ngx_imap_auth_http_process_headers(ngx_imap_session_t *s,
397 ngx_imap_auth_http_ctx_t *ctx) 415 ngx_imap_auth_http_ctx_t *ctx)
398 { 416 {
399 u_char *p; 417 u_char *p;
418 time_t timer;
400 size_t len, size; 419 size_t len, size;
401 ngx_int_t rc, port, n; 420 ngx_int_t rc, port, n;
421 ngx_peers_t *peers;
402 struct sockaddr_in *sin; 422 struct sockaddr_in *sin;
403 423
404 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, s->connection->log, 0, 424 ngx_log_debug0(NGX_LOG_DEBUG_IMAP, s->connection->log, 0,
405 "imap auth http process headers"); 425 "imap auth http process headers");
406 426
448 } 468 }
449 469
450 p = ngx_pcalloc(s->connection->pool, size); 470 p = ngx_pcalloc(s->connection->pool, size);
451 if (p == NULL) { 471 if (p == NULL) {
452 ngx_close_connection(ctx->peer.connection); 472 ngx_close_connection(ctx->peer.connection);
473 ngx_destroy_pool(ctx->pool);
453 ngx_imap_session_internal_server_error(s); 474 ngx_imap_session_internal_server_error(s);
454 return; 475 return;
455 } 476 }
456 477
457 ctx->err.data = p; 478 ctx->err.data = p;
528 549
529 ngx_close_connection(ctx->peer.connection); 550 ngx_close_connection(ctx->peer.connection);
530 551
531 if (ctx->err.len) { 552 if (ctx->err.len) {
532 s->out = ctx->err; 553 s->out = ctx->err;
533 554 timer = ctx->sleep;
534 if (ctx->sleep == 0) { 555
556 ngx_destroy_pool(ctx->pool);
557
558 ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
559 "client login failed");
560
561 if (timer == 0) {
535 s->quit = 1; 562 s->quit = 1;
536
537 ngx_imap_send(s->connection->write); 563 ngx_imap_send(s->connection->write);
538
539 return; 564 return;
540 } 565 }
541 566
542 ngx_add_timer(s->connection->read, ctx->sleep * 1000); 567 ngx_add_timer(s->connection->read, timer * 1000);
543 568
544 s->connection->read->handler = ngx_imap_auth_sleep_handler; 569 s->connection->read->handler = ngx_imap_auth_sleep_handler;
545 570
546 return; 571 return;
547 } 572 }
548 573
549 if (ctx->addr.len == 0 || ctx->port.len == 0) { 574 if (ctx->addr.len == 0 || ctx->port.len == 0) {
550 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, 575 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
551 "auth http server %V did not send server or port", 576 "auth http server %V did not send server or port",
552 &ctx->peer.peers->peer[0].name); 577 &ctx->peer.peers->peer[0].name);
578 ngx_destroy_pool(ctx->pool);
553 ngx_imap_session_internal_server_error(s); 579 ngx_imap_session_internal_server_error(s);
554 return; 580 return;
555 } 581 }
556 582
557 ctx->peers = ngx_pcalloc(s->connection->pool, sizeof(ngx_peers_t)); 583 peers = ngx_pcalloc(s->connection->pool, sizeof(ngx_peers_t));
558 if (ctx->peers == NULL) { 584 if (peers == NULL) {
585 ngx_destroy_pool(ctx->pool);
559 ngx_imap_session_internal_server_error(s); 586 ngx_imap_session_internal_server_error(s);
560 return; 587 return;
561 } 588 }
562 589
563 sin = ngx_pcalloc(s->connection->pool, sizeof(struct sockaddr_in)); 590 sin = ngx_pcalloc(s->connection->pool, sizeof(struct sockaddr_in));
564 if (sin == NULL) { 591 if (sin == NULL) {
592 ngx_destroy_pool(ctx->pool);
565 ngx_imap_session_internal_server_error(s); 593 ngx_imap_session_internal_server_error(s);
566 return; 594 return;
567 } 595 }
568 596
569 sin->sin_family = AF_INET; 597 sin->sin_family = AF_INET;
572 if (port == NGX_ERROR || port < 1 || port > 65536) { 600 if (port == NGX_ERROR || port < 1 || port > 65536) {
573 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, 601 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
574 "auth http server %V sent invalid server " 602 "auth http server %V sent invalid server "
575 "port:\"%V\"", 603 "port:\"%V\"",
576 &ctx->peer.peers->peer[0].name, &ctx->port); 604 &ctx->peer.peers->peer[0].name, &ctx->port);
605 ngx_destroy_pool(ctx->pool);
577 ngx_imap_session_internal_server_error(s); 606 ngx_imap_session_internal_server_error(s);
578 return; 607 return;
579 } 608 }
580 609
581 sin->sin_port = htons((in_port_t) port); 610 sin->sin_port = htons((in_port_t) port);
585 if (sin->sin_addr.s_addr == INADDR_NONE) { 614 if (sin->sin_addr.s_addr == INADDR_NONE) {
586 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, 615 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
587 "auth http server %V sent invalid server " 616 "auth http server %V sent invalid server "
588 "address:\"%V\"", 617 "address:\"%V\"",
589 &ctx->peer.peers->peer[0].name, &ctx->addr); 618 &ctx->peer.peers->peer[0].name, &ctx->addr);
619 ngx_destroy_pool(ctx->pool);
590 ngx_imap_session_internal_server_error(s); 620 ngx_imap_session_internal_server_error(s);
591 return; 621 return;
592 } 622 }
593 623
594 ctx->peers->number = 1; 624 peers->number = 1;
595 625
596 ctx->peers->peer[0].sockaddr = (struct sockaddr *) sin; 626 peers->peer[0].sockaddr = (struct sockaddr *) sin;
597 ctx->peers->peer[0].socklen = sizeof(struct sockaddr_in); 627 peers->peer[0].socklen = sizeof(struct sockaddr_in);
598 628
599 len = ctx->addr.len + 1 + ctx->port.len; 629 len = ctx->addr.len + 1 + ctx->port.len;
600 630
601 ctx->peers->peer[0].name.len = len; 631 peers->peer[0].name.len = len;
602 632
603 ctx->peers->peer[0].name.data = ngx_palloc(s->connection->pool, 633 peers->peer[0].name.data = ngx_palloc(s->connection->pool, len);
604 len); 634 if (peers->peer[0].name.data == NULL) {
605 if (ctx->peers->peer[0].name.data == NULL) { 635 ngx_destroy_pool(ctx->pool);
606 ngx_imap_session_internal_server_error(s); 636 ngx_imap_session_internal_server_error(s);
607 return; 637 return;
608 } 638 }
609 639
610 len = ctx->addr.len; 640 len = ctx->addr.len;
611 641
612 ngx_memcpy(ctx->peers->peer[0].name.data, ctx->addr.data, len); 642 ngx_memcpy(peers->peer[0].name.data, ctx->addr.data, len);
613 643
614 ctx->peers->peer[0].name.data[len++] = ':'; 644 peers->peer[0].name.data[len++] = ':';
615 645
616 ngx_memcpy(ctx->peers->peer[0].name.data + len, 646 ngx_memcpy(peers->peer[0].name.data + len,
617 ctx->port.data, ctx->port.len); 647 ctx->port.data, ctx->port.len);
618 648
619 ctx->peers->peer[0].uri_separator = ""; 649 peers->peer[0].uri_separator = "";
620 650
621 ngx_imap_proxy_init(s, ctx->peers); 651 ngx_destroy_pool(ctx->pool);
652 ngx_imap_proxy_init(s, peers);
622 653
623 return; 654 return;
624 } 655 }
625 656
626 if (rc == NGX_AGAIN ) { 657 if (rc == NGX_AGAIN ) {
631 662
632 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, 663 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
633 "auth http server %V sent invalid header in response", 664 "auth http server %V sent invalid header in response",
634 &ctx->peer.peers->peer[0].name); 665 &ctx->peer.peers->peer[0].name);
635 ngx_close_connection(ctx->peer.connection); 666 ngx_close_connection(ctx->peer.connection);
667 ngx_destroy_pool(ctx->pool);
636 ngx_imap_session_internal_server_error(s); 668 ngx_imap_session_internal_server_error(s);
637 669
638 return; 670 return;
639 } 671 }
640 } 672 }
904 s = c->data; 936 s = c->data;
905 937
906 ctx = ngx_imap_get_module_ctx(s, ngx_imap_auth_http_module); 938 ctx = ngx_imap_get_module_ctx(s, ngx_imap_auth_http_module);
907 939
908 ngx_close_connection(ctx->peer.connection); 940 ngx_close_connection(ctx->peer.connection);
941 ngx_destroy_pool(ctx->pool);
909 ngx_imap_session_internal_server_error(s); 942 ngx_imap_session_internal_server_error(s);
910 } 943 }
911 } 944 }
912 945
913 946
918 "imap auth http dummy handler"); 951 "imap auth http dummy handler");
919 } 952 }
920 953
921 954
922 static ngx_buf_t * 955 static ngx_buf_t *
923 ngx_imap_auth_http_create_request(ngx_imap_session_t *s, 956 ngx_imap_auth_http_create_request(ngx_imap_session_t *s, ngx_pool_t *pool,
924 ngx_imap_auth_http_conf_t *ahcf) 957 ngx_imap_auth_http_conf_t *ahcf)
925 { 958 {
926 size_t len; 959 size_t len;
927 ngx_buf_t *b; 960 ngx_buf_t *b;
928 961
936 + sizeof(CRLF) - 1 969 + sizeof(CRLF) - 1
937 + sizeof("Client-IP: ") - 1 + s->connection->addr_text.len 970 + sizeof("Client-IP: ") - 1 + s->connection->addr_text.len
938 + sizeof(CRLF) - 1 971 + sizeof(CRLF) - 1
939 + sizeof(CRLF) - 1; 972 + sizeof(CRLF) - 1;
940 973
941 b = ngx_create_temp_buf(s->connection->pool, len); 974 b = ngx_create_temp_buf(pool, len);
942 if (b == NULL) { 975 if (b == NULL) {
943 return NULL; 976 return NULL;
944 } 977 }
945 978
946 b->last = ngx_cpymem(b->last, "GET ", sizeof("GET ") - 1); 979 b->last = ngx_cpymem(b->last, "GET ", sizeof("GET ") - 1);
979 *b->last++ = CR; *b->last++ = LF; 1012 *b->last++ = CR; *b->last++ = LF;
980 1013
981 /* add "\r\n" at the header end */ 1014 /* add "\r\n" at the header end */
982 *b->last++ = CR; *b->last++ = LF; 1015 *b->last++ = CR; *b->last++ = LF;
983 1016
984 #if (NGX_DEBUG) 1017 #if (NGX_DEBUG_IMAP_PASSWD)
985 { 1018 {
986 ngx_str_t l; 1019 ngx_str_t l;
987 1020
988 l.len = b->last - b->pos; 1021 l.len = b->last - b->pos;
989 l.data = b->pos; 1022 l.data = b->pos;