comparison src/mail/ngx_mail_smtp_handler.c @ 420:ad0a34a8efa6 NGINX_0_7_22

nginx 0.7.22 *) Feature: the "none" parameter in the "smtp_auth" directive. Thanks to Maxim Dounin. *) Feature: the "$cookie_..." variables. *) Bugfix: the "directio" directive did not work in XFS filesystem. *) Bugfix: the resolver did not understand big DNS responses. Thanks to Zyb.
author Igor Sysoev <http://sysoev.ru>
date Thu, 20 Nov 2008 00:00:00 +0300
parents 984bb0b1399b
children dac47e9ef0d5
comparison
equal deleted inserted replaced
419:b986babf3f57 420:ad0a34a8efa6
21 static ngx_int_t ngx_mail_smtp_helo(ngx_mail_session_t *s, ngx_connection_t *c); 21 static ngx_int_t ngx_mail_smtp_helo(ngx_mail_session_t *s, ngx_connection_t *c);
22 static ngx_int_t ngx_mail_smtp_auth(ngx_mail_session_t *s, ngx_connection_t *c); 22 static ngx_int_t ngx_mail_smtp_auth(ngx_mail_session_t *s, ngx_connection_t *c);
23 static ngx_int_t ngx_mail_smtp_mail(ngx_mail_session_t *s, ngx_connection_t *c); 23 static ngx_int_t ngx_mail_smtp_mail(ngx_mail_session_t *s, ngx_connection_t *c);
24 static ngx_int_t ngx_mail_smtp_starttls(ngx_mail_session_t *s, 24 static ngx_int_t ngx_mail_smtp_starttls(ngx_mail_session_t *s,
25 ngx_connection_t *c); 25 ngx_connection_t *c);
26 static ngx_int_t ngx_mail_smtp_rset(ngx_mail_session_t *s, ngx_connection_t *c);
27 static ngx_int_t ngx_mail_smtp_rcpt(ngx_mail_session_t *s, ngx_connection_t *c);
26 28
27 static ngx_int_t ngx_mail_smtp_discard_command(ngx_mail_session_t *s, 29 static ngx_int_t ngx_mail_smtp_discard_command(ngx_mail_session_t *s,
28 ngx_connection_t *c, char *err); 30 ngx_connection_t *c, char *err);
29 static void ngx_mail_smtp_log_rejected_command(ngx_mail_session_t *s, 31 static void ngx_mail_smtp_log_rejected_command(ngx_mail_session_t *s,
30 ngx_connection_t *c, char *err); 32 ngx_connection_t *c, char *err);
39 static u_char smtp_invalid_command[] = "500 5.5.1 Invalid command" CRLF; 41 static u_char smtp_invalid_command[] = "500 5.5.1 Invalid command" CRLF;
40 static u_char smtp_invalid_pipelining[] = 42 static u_char smtp_invalid_pipelining[] =
41 "503 5.5.0 Improper use of SMTP command pipelining" CRLF; 43 "503 5.5.0 Improper use of SMTP command pipelining" CRLF;
42 static u_char smtp_invalid_argument[] = "501 5.5.4 Invalid argument" CRLF; 44 static u_char smtp_invalid_argument[] = "501 5.5.4 Invalid argument" CRLF;
43 static u_char smtp_auth_required[] = "530 5.7.1 Authentication required" CRLF; 45 static u_char smtp_auth_required[] = "530 5.7.1 Authentication required" CRLF;
46 static u_char smtp_bad_sequence[] = "503 5.5.1 Bad sequence of commands" CRLF;
44 47
45 48
46 static ngx_str_t smtp_unavailable = ngx_string("[UNAVAILABLE]"); 49 static ngx_str_t smtp_unavailable = ngx_string("[UNAVAILABLE]");
47 static ngx_str_t smtp_tempunavail = ngx_string("[TEMPUNAVAIL]"); 50 static ngx_str_t smtp_tempunavail = ngx_string("[TEMPUNAVAIL]");
48 51
415 418
416 case NGX_SMTP_MAIL: 419 case NGX_SMTP_MAIL:
417 rc = ngx_mail_smtp_mail(s, c); 420 rc = ngx_mail_smtp_mail(s, c);
418 break; 421 break;
419 422
423 case NGX_SMTP_RCPT:
424 rc = ngx_mail_smtp_rcpt(s, c);
425 break;
426
427 case NGX_SMTP_RSET:
428 rc = ngx_mail_smtp_rset(s, c);
429 break;
430
420 case NGX_SMTP_NOOP: 431 case NGX_SMTP_NOOP:
421 case NGX_SMTP_RSET:
422 break; 432 break;
423 433
424 case NGX_SMTP_STARTTLS: 434 case NGX_SMTP_STARTTLS:
425 rc = ngx_mail_smtp_starttls(s, c); 435 rc = ngx_mail_smtp_starttls(s, c);
426 s->out.len = sizeof(smtp_starttls) - 1; 436 s->out.len = sizeof(smtp_starttls) - 1;
511 return NGX_ERROR; 521 return NGX_ERROR;
512 } 522 }
513 523
514 ngx_memcpy(s->smtp_helo.data, arg[0].data, arg[0].len); 524 ngx_memcpy(s->smtp_helo.data, arg[0].data, arg[0].len);
515 525
526 s->smtp_from.len = 0;
527 s->smtp_from.data = NULL;
528 s->smtp_to.len = 0;
529 s->smtp_to.data = NULL;
530
516 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module); 531 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
517 532
518 if (s->command == NGX_SMTP_HELO) { 533 if (s->command == NGX_SMTP_HELO) {
519 s->out = sscf->server_name; 534 s->out = sscf->server_name;
520 535
616 631
617 632
618 static ngx_int_t 633 static ngx_int_t
619 ngx_mail_smtp_mail(ngx_mail_session_t *s, ngx_connection_t *c) 634 ngx_mail_smtp_mail(ngx_mail_session_t *s, ngx_connection_t *c)
620 { 635 {
621 ngx_mail_smtp_log_rejected_command(s, c, "client was rejected: \"%V\""); 636 u_char ch;
622 637 ngx_str_t l;
623 s->out.len = sizeof(smtp_auth_required) - 1; 638 ngx_uint_t i;
624 s->out.data = smtp_auth_required; 639 ngx_mail_smtp_srv_conf_t *sscf;
640
641 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
642
643 if (!(sscf->auth_methods & NGX_MAIL_AUTH_NONE_ENABLED)) {
644 ngx_mail_smtp_log_rejected_command(s, c, "client was rejected: \"%V\"");
645
646 s->out.len = sizeof(smtp_auth_required) - 1;
647 s->out.data = smtp_auth_required;
648
649 return NGX_OK;
650 }
651
652 /* auth none */
653
654 if (s->smtp_from.len) {
655 s->out.len = sizeof(smtp_bad_sequence) - 1;
656 s->out.data = smtp_bad_sequence;
657 return NGX_OK;
658 }
659
660 l.len = s->buffer->last - s->buffer->start;
661 l.data = s->buffer->start;
662
663 for (i = 0; i < l.len; i++) {
664 ch = l.data[i];
665
666 if (ch != CR && ch != LF) {
667 continue;
668 }
669
670 l.data[i] = ' ';
671 }
672
673 while (i) {
674 if (l.data[i - 1] != ' ') {
675 break;
676 }
677
678 i--;
679 }
680
681 l.len = i;
682
683 s->smtp_from.len = l.len;
684
685 s->smtp_from.data = ngx_pnalloc(c->pool, l.len);
686 if (s->smtp_from.data == NULL) {
687 return NGX_ERROR;
688 }
689
690 ngx_memcpy(s->smtp_from.data, l.data, l.len);
691
692 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
693 "smtp mail from:\"%V\"", &s->smtp_from);
694
695 s->out.len = sizeof(smtp_ok) - 1;
696 s->out.data = smtp_ok;
697
698 return NGX_OK;
699 }
700
701
702 static ngx_int_t
703 ngx_mail_smtp_rcpt(ngx_mail_session_t *s, ngx_connection_t *c)
704 {
705 u_char ch;
706 ngx_str_t l;
707 ngx_uint_t i;
708
709 if (s->smtp_from.len == 0) {
710 s->out.len = sizeof(smtp_bad_sequence) - 1;
711 s->out.data = smtp_bad_sequence;
712 return NGX_OK;
713 }
714
715 l.len = s->buffer->last - s->buffer->start;
716 l.data = s->buffer->start;
717
718 for (i = 0; i < l.len; i++) {
719 ch = l.data[i];
720
721 if (ch != CR && ch != LF) {
722 continue;
723 }
724
725 l.data[i] = ' ';
726 }
727
728 while (i) {
729 if (l.data[i - 1] != ' ') {
730 break;
731 }
732
733 i--;
734 }
735
736 l.len = i;
737
738 s->smtp_to.len = l.len;
739
740 s->smtp_to.data = ngx_pnalloc(c->pool, l.len);
741 if (s->smtp_to.data == NULL) {
742 return NGX_ERROR;
743 }
744
745 ngx_memcpy(s->smtp_to.data, l.data, l.len);
746
747 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
748 "smtp rcpt to:\"%V\"", &s->smtp_to);
749
750 s->auth_method = NGX_MAIL_AUTH_NONE;
751
752 return NGX_DONE;
753 }
754
755
756 static ngx_int_t
757 ngx_mail_smtp_rset(ngx_mail_session_t *s, ngx_connection_t *c)
758 {
759 s->smtp_from.len = 0;
760 s->smtp_from.data = NULL;
761 s->smtp_to.len = 0;
762 s->smtp_to.data = NULL;
763
764 s->out.len = sizeof(smtp_ok) - 1;
765 s->out.data = smtp_ok;
625 766
626 return NGX_OK; 767 return NGX_OK;
627 } 768 }
628 769
629 770
642 * obtained from client before STARTTLS. 783 * obtained from client before STARTTLS.
643 */ 784 */
644 785
645 s->smtp_helo.len = 0; 786 s->smtp_helo.len = 0;
646 s->smtp_helo.data = NULL; 787 s->smtp_helo.data = NULL;
788 s->smtp_from.len = 0;
789 s->smtp_from.data = NULL;
790 s->smtp_to.len = 0;
791 s->smtp_to.data = NULL;
647 792
648 c->read->handler = ngx_mail_starttls_handler; 793 c->read->handler = ngx_mail_starttls_handler;
649 return NGX_OK; 794 return NGX_OK;
650 } 795 }
651 } 796 }