comparison src/mail/ngx_mail_proxy_module.c @ 2309:8156bc03982a

smtp_auth none patch by Maxim Dounin
author Igor Sysoev <igor@sysoev.ru>
date Thu, 13 Nov 2008 13:25:34 +0000
parents 2a92804f4109
children 6bad42a41dd8
comparison
equal deleted inserted replaced
2308:3f98400e31e9 2309:8156bc03982a
102 NULL, /* exit master */ 102 NULL, /* exit master */
103 NGX_MODULE_V1_PADDING 103 NGX_MODULE_V1_PADDING
104 }; 104 };
105 105
106 106
107 static u_char smtp_ok[] = "235 2.0.0 OK" CRLF; 107 static u_char smtp_auth_ok[] = "235 2.0.0 OK" CRLF;
108 static u_char smtp_ok[] = "250 2.0.0 OK" CRLF;
108 109
109 110
110 void 111 void
111 ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_peer_addr_t *peer) 112 ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_peer_addr_t *peer)
112 { 113 {
463 ngx_mail_proxy_smtp_handler(ngx_event_t *rev) 464 ngx_mail_proxy_smtp_handler(ngx_event_t *rev)
464 { 465 {
465 u_char *p; 466 u_char *p;
466 ngx_int_t rc; 467 ngx_int_t rc;
467 ngx_str_t line; 468 ngx_str_t line;
469 ngx_buf_t *b;
468 ngx_connection_t *c; 470 ngx_connection_t *c;
469 ngx_mail_session_t *s; 471 ngx_mail_session_t *s;
470 ngx_mail_proxy_conf_t *pcf; 472 ngx_mail_proxy_conf_t *pcf;
471 ngx_mail_core_srv_conf_t *cscf; 473 ngx_mail_core_srv_conf_t *cscf;
472 474
518 sizeof("HELO ") - 1); 520 sizeof("HELO ") - 1);
519 521
520 p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len); 522 p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
521 *p++ = CR; *p = LF; 523 *p++ = CR; *p = LF;
522 524
523 s->mail_state = pcf->xclient ? ngx_smtp_helo: ngx_smtp_noxclient; 525 if (pcf->xclient) {
524 526 s->mail_state = ngx_smtp_helo_xclient;
525 break; 527
526 528 } else if (s->auth_method == NGX_MAIL_AUTH_NONE) {
527 case ngx_smtp_helo: 529 s->mail_state = ngx_smtp_helo_from;
530
531 } else {
532 s->mail_state = ngx_smtp_helo;
533 }
534
535 break;
536
537 case ngx_smtp_helo_xclient:
528 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, 538 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
529 "mail proxy send xclient"); 539 "mail proxy send xclient");
530 540
531 s->connection->log->action = "sending XCLIENT to upstream"; 541 s->connection->log->action = "sending XCLIENT to upstream";
532 542
539 if (line.data == NULL) { 549 if (line.data == NULL) {
540 ngx_mail_proxy_internal_server_error(s); 550 ngx_mail_proxy_internal_server_error(s);
541 return; 551 return;
542 } 552 }
543 553
544 if (s->smtp_helo.len) { 554 line.len = ngx_sprintf(line.data,
545 line.len = ngx_sprintf(line.data, 555 "XCLIENT PROTO=%sSMTP%s%V ADDR=%V%s%V NAME=%V" CRLF,
546 "XCLIENT PROTO=%sSMTP HELO=%V ADDR=%V LOGIN=%V " 556 (s->esmtp ? "E" : ""),
547 "NAME=%V" CRLF, 557 (s->smtp_helo.len ? " HELO=" : ""), &s->smtp_helo,
548 (s->esmtp ? "E" : ""), &s->smtp_helo, 558 &s->connection->addr_text,
549 &s->connection->addr_text, &s->login, &s->host) 559 (s->login.len ? " LOGIN=" : ""), &s->login, &s->host)
550 - line.data; 560 - line.data;
561
562 s->mail_state = (s->auth_method == NGX_MAIL_AUTH_NONE) ?
563 ngx_smtp_xclient_from : ngx_smtp_xclient;
564
565 break;
566
567 case ngx_smtp_helo_from:
568 case ngx_smtp_xclient_from:
569 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
570 "mail proxy send mail from");
571
572 s->connection->log->action = "sending MAIL FROM to upstream";
573
574 line.len = s->smtp_from.len + sizeof(CRLF) - 1;
575 line.data = ngx_pnalloc(c->pool, line.len);
576 if (line.data == NULL) {
577 ngx_mail_proxy_internal_server_error(s);
578 return;
579 }
580
581 p = ngx_cpymem(line.data, s->smtp_from.data, s->smtp_from.len);
582 *p++ = CR; *p = LF;
583
584 s->mail_state = ngx_smtp_from;
585
586 break;
587
588 case ngx_smtp_from:
589 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
590 "mail proxy send rcpt to");
591
592 s->connection->log->action = "sending RCPT TO to upstream";
593
594 line.len = s->smtp_to.len + sizeof(CRLF) - 1;
595 line.data = ngx_pnalloc(c->pool, line.len);
596 if (line.data == NULL) {
597 ngx_mail_proxy_internal_server_error(s);
598 return;
599 }
600
601 p = ngx_cpymem(line.data, s->smtp_to.data, s->smtp_to.len);
602 *p++ = CR; *p = LF;
603
604 s->mail_state = ngx_smtp_to;
605
606 break;
607
608 case ngx_smtp_helo:
609 case ngx_smtp_xclient:
610 case ngx_smtp_to:
611
612 b = s->proxy->buffer;
613
614 if (s->auth_method == NGX_MAIL_AUTH_NONE) {
615 ngx_memcpy(b->start, smtp_ok, sizeof(smtp_ok) - 1);
616 b->last = b->start + sizeof(smtp_ok) - 1;
617
551 } else { 618 } else {
552 line.len = ngx_sprintf(line.data, 619 ngx_memcpy(b->start, smtp_auth_ok, sizeof(smtp_auth_ok) - 1);
553 "XCLIENT PROTO=SMTP ADDR=%V LOGIN=%V NAME=%V" CRLF, 620 b->last = b->start + sizeof(smtp_auth_ok) - 1;
554 &s->connection->addr_text, &s->login, &s->host) 621 }
555 - line.data; 622
556 } 623 b->pos = b->start;
557
558 s->mail_state = ngx_smtp_xclient;
559 break;
560
561 case ngx_smtp_noxclient:
562 case ngx_smtp_xclient:
563
564 ngx_memcpy(s->proxy->buffer->start, smtp_ok, sizeof(smtp_ok) - 1);
565
566 s->proxy->buffer->pos = s->proxy->buffer->start;
567 s->proxy->buffer->last = s->proxy->buffer->start + sizeof(smtp_ok) - 1;
568 624
569 s->connection->read->handler = ngx_mail_proxy_handler; 625 s->connection->read->handler = ngx_mail_proxy_handler;
570 s->connection->write->handler = ngx_mail_proxy_handler; 626 s->connection->write->handler = ngx_mail_proxy_handler;
571 rev->handler = ngx_mail_proxy_handler; 627 rev->handler = ngx_mail_proxy_handler;
572 c->write->handler = ngx_mail_proxy_handler; 628 c->write->handler = ngx_mail_proxy_handler;
702 758
703 default: /* NGX_MAIL_SMTP_PROTOCOL */ 759 default: /* NGX_MAIL_SMTP_PROTOCOL */
704 switch (state) { 760 switch (state) {
705 761
706 case ngx_smtp_helo: 762 case ngx_smtp_helo:
707 case ngx_smtp_noxclient: 763 case ngx_smtp_helo_from:
764 case ngx_smtp_helo_xclient:
765 case ngx_smtp_from:
766 case ngx_smtp_to:
708 if (p[0] == '2' && p[1] == '5' && p[2] == '0') { 767 if (p[0] == '2' && p[1] == '5' && p[2] == '0') {
709 return NGX_OK; 768 return NGX_OK;
710 } 769 }
711 break; 770 break;
712 771
713 case ngx_smtp_start: 772 case ngx_smtp_start:
773 if (p[0] == '2' && p[1] == '2' && p[2] == '0') {
774 return NGX_OK;
775 }
776 break;
777
714 case ngx_smtp_xclient: 778 case ngx_smtp_xclient:
715 if (p[0] == '2' && p[1] == '2' && p[2] == '0') { 779 case ngx_smtp_xclient_from:
780 if (p[0] == '2' && (p[1] == '2' || p[1] == '5') && p[2] == '0') {
716 return NGX_OK; 781 return NGX_OK;
717 } 782 }
718 break; 783 break;
719 } 784 }
720 785