~ubuntu-branches/debian/squeeze/nginx/squeeze

« back to all changes in this revision

Viewing changes to src/mail/ngx_mail_proxy_module.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella
  • Date: 2009-05-31 18:38:56 UTC
  • mfrom: (1.1.10 upstream) (4.1.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20090531183856-3xhvf6wd0bw5556i
Tags: 0.7.59-1
* New upstream release, first in Debian for the 0.7 branch. Among other
  issues, it also fixes the problem with wildcard dns names used with SSL.
  (Closes: #515904)
* debian/watch: updated.
* debian/postinst: fixed a bashism. (Closes: #507913)
* debian/conf/nginx.conf: removed default_type. (Closes: #509390)
* debian/control: updated Standards-Version to 3.8.1, no changes needed.
* debian/NEWS.Debian: documented the issues with
  server_names_hash_bucket_size. (Closes: #524785)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
108
 
109
109
 
110
110
void
201
201
 
202
202
    ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy block read");
203
203
 
204
 
    if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
 
204
    if (ngx_handle_read_event(rev, 0) != NGX_OK) {
205
205
        c = rev->data;
206
206
        s = c->data;
207
207
 
253
253
        s->connection->log->action = "sending user name to upstream";
254
254
 
255
255
        line.len = sizeof("USER ")  - 1 + s->login.len + 2;
256
 
        line.data = ngx_palloc(c->pool, line.len);
 
256
        line.data = ngx_pnalloc(c->pool, line.len);
257
257
        if (line.data == NULL) {
258
258
            ngx_mail_proxy_internal_server_error(s);
259
259
            return;
272
272
        s->connection->log->action = "sending password to upstream";
273
273
 
274
274
        line.len = sizeof("PASS ")  - 1 + s->passwd.len + 2;
275
 
        line.data = ngx_palloc(c->pool, line.len);
 
275
        line.data = ngx_pnalloc(c->pool, line.len);
276
276
        if (line.data == NULL) {
277
277
            ngx_mail_proxy_internal_server_error(s);
278
278
            return;
369
369
 
370
370
        line.len = s->tag.len + sizeof("LOGIN ") - 1
371
371
                   + 1 + NGX_SIZE_T_LEN + 1 + 2;
372
 
        line.data = ngx_palloc(c->pool, line.len);
 
372
        line.data = ngx_pnalloc(c->pool, line.len);
373
373
        if (line.data == NULL) {
374
374
            ngx_mail_proxy_internal_server_error(s);
375
375
            return;
388
388
        s->connection->log->action = "sending user name to upstream";
389
389
 
390
390
        line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2;
391
 
        line.data = ngx_palloc(c->pool, line.len);
 
391
        line.data = ngx_pnalloc(c->pool, line.len);
392
392
        if (line.data == NULL) {
393
393
            ngx_mail_proxy_internal_server_error(s);
394
394
            return;
408
408
        s->connection->log->action = "sending password to upstream";
409
409
 
410
410
        line.len = s->passwd.len + 2;
411
 
        line.data = ngx_palloc(c->pool, line.len);
 
411
        line.data = ngx_pnalloc(c->pool, line.len);
412
412
        if (line.data == NULL) {
413
413
            ngx_mail_proxy_internal_server_error(s);
414
414
            return;
465
465
    u_char                    *p;
466
466
    ngx_int_t                  rc;
467
467
    ngx_str_t                  line;
 
468
    ngx_buf_t                 *b;
468
469
    ngx_connection_t          *c;
469
470
    ngx_mail_session_t        *s;
470
471
    ngx_mail_proxy_conf_t     *pcf;
505
506
        cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
506
507
 
507
508
        line.len = sizeof("HELO ")  - 1 + cscf->server_name.len + 2;
508
 
        line.data = ngx_palloc(c->pool, line.len);
 
509
        line.data = ngx_pnalloc(c->pool, line.len);
509
510
        if (line.data == NULL) {
510
511
            ngx_mail_proxy_internal_server_error(s);
511
512
            return;
520
521
        p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
521
522
        *p++ = CR; *p = LF;
522
523
 
523
 
        s->mail_state = pcf->xclient ? ngx_smtp_helo: ngx_smtp_noxclient;
 
524
        if (pcf->xclient) {
 
525
            s->mail_state = ngx_smtp_helo_xclient;
 
526
 
 
527
        } else if (s->auth_method == NGX_MAIL_AUTH_NONE) {
 
528
            s->mail_state = ngx_smtp_helo_from;
 
529
 
 
530
        } else {
 
531
            s->mail_state = ngx_smtp_helo;
 
532
        }
524
533
 
525
534
        break;
526
535
 
527
 
    case ngx_smtp_helo:
 
536
    case ngx_smtp_helo_xclient:
528
537
        ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
529
538
                       "mail proxy send xclient");
530
539
 
531
540
        s->connection->log->action = "sending XCLIENT to upstream";
532
541
 
533
 
        line.len = sizeof("XCLIENT PROTO=SMTP HELO= ADDR= LOGIN= NAME="
 
542
        line.len = sizeof("XCLIENT ADDR= LOGIN= NAME="
534
543
                          CRLF) - 1
535
 
                   + s->esmtp + s->smtp_helo.len
536
544
                   + s->connection->addr_text.len + s->login.len + s->host.len;
537
545
 
538
 
        line.data = ngx_palloc(c->pool, line.len);
 
546
        line.data = ngx_pnalloc(c->pool, line.len);
539
547
        if (line.data == NULL) {
540
548
            ngx_mail_proxy_internal_server_error(s);
541
549
            return;
542
550
        }
543
551
 
 
552
        line.len = ngx_sprintf(line.data,
 
553
                       "XCLIENT ADDR=%V%s%V NAME=%V" CRLF,
 
554
                       &s->connection->addr_text,
 
555
                       (s->login.len ? " LOGIN=" : ""), &s->login, &s->host)
 
556
                   - line.data;
 
557
 
544
558
        if (s->smtp_helo.len) {
545
 
            line.len = ngx_sprintf(line.data,
546
 
                           "XCLIENT PROTO=%sSMTP HELO=%V ADDR=%V LOGIN=%V "
547
 
                           "NAME=%V" CRLF,
548
 
                           (s->esmtp ? "E" : ""), &s->smtp_helo,
549
 
                           &s->connection->addr_text, &s->login, &s->host)
550
 
                       - line.data;
 
559
            s->mail_state = ngx_smtp_xclient_helo;
 
560
 
 
561
        } else if (s->auth_method == NGX_MAIL_AUTH_NONE) {
 
562
            s->mail_state = ngx_smtp_xclient_from;
 
563
 
551
564
        } else {
552
 
            line.len = ngx_sprintf(line.data,
553
 
                           "XCLIENT PROTO=SMTP ADDR=%V LOGIN=%V NAME=%V" CRLF,
554
 
                           &s->connection->addr_text, &s->login, &s->host)
555
 
                       - line.data;
556
 
        }
557
 
 
558
 
        s->mail_state = ngx_smtp_xclient;
559
 
        break;
560
 
 
561
 
    case ngx_smtp_noxclient:
 
565
            s->mail_state = ngx_smtp_xclient;
 
566
        }
 
567
 
 
568
        break;
 
569
 
 
570
    case ngx_smtp_xclient_helo:
 
571
        ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
 
572
                       "mail proxy send client ehlo");
 
573
 
 
574
        s->connection->log->action = "sending client HELO/EHLO to upstream";
 
575
 
 
576
        line.len = sizeof("HELO " CRLF) - 1 + s->smtp_helo.len;
 
577
 
 
578
        line.data = ngx_pnalloc(c->pool, line.len);
 
579
        if (line.data == NULL) {
 
580
            ngx_mail_proxy_internal_server_error(s);
 
581
            return;
 
582
        }
 
583
 
 
584
        line.len = ngx_sprintf(line.data,
 
585
                       ((s->esmtp) ? "EHLO %V" CRLF : "HELO %V" CRLF),
 
586
                       &s->smtp_helo)
 
587
                   - line.data;
 
588
 
 
589
        s->mail_state = (s->auth_method == NGX_MAIL_AUTH_NONE) ?
 
590
                            ngx_smtp_helo_from : ngx_smtp_helo;
 
591
 
 
592
        break;
 
593
 
 
594
    case ngx_smtp_helo_from:
 
595
    case ngx_smtp_xclient_from:
 
596
        ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
 
597
                       "mail proxy send mail from");
 
598
 
 
599
        s->connection->log->action = "sending MAIL FROM to upstream";
 
600
 
 
601
        line.len = s->smtp_from.len + sizeof(CRLF) - 1;
 
602
        line.data = ngx_pnalloc(c->pool, line.len);
 
603
        if (line.data == NULL) {
 
604
            ngx_mail_proxy_internal_server_error(s);
 
605
            return;
 
606
        }
 
607
 
 
608
        p = ngx_cpymem(line.data, s->smtp_from.data, s->smtp_from.len);
 
609
        *p++ = CR; *p = LF;
 
610
 
 
611
        s->mail_state = ngx_smtp_from;
 
612
 
 
613
        break;
 
614
 
 
615
    case ngx_smtp_from:
 
616
        ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
 
617
                       "mail proxy send rcpt to");
 
618
 
 
619
        s->connection->log->action = "sending RCPT TO to upstream";
 
620
 
 
621
        line.len = s->smtp_to.len + sizeof(CRLF) - 1;
 
622
        line.data = ngx_pnalloc(c->pool, line.len);
 
623
        if (line.data == NULL) {
 
624
            ngx_mail_proxy_internal_server_error(s);
 
625
            return;
 
626
        }
 
627
 
 
628
        p = ngx_cpymem(line.data, s->smtp_to.data, s->smtp_to.len);
 
629
        *p++ = CR; *p = LF;
 
630
 
 
631
        s->mail_state = ngx_smtp_to;
 
632
 
 
633
        break;
 
634
 
 
635
    case ngx_smtp_helo:
562
636
    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;
 
637
    case ngx_smtp_to:
 
638
 
 
639
        b = s->proxy->buffer;
 
640
 
 
641
        if (s->auth_method == NGX_MAIL_AUTH_NONE) {
 
642
            b->pos = b->start;
 
643
 
 
644
        } else {
 
645
            ngx_memcpy(b->start, smtp_auth_ok, sizeof(smtp_auth_ok) - 1);
 
646
            b->last = b->start + sizeof(smtp_auth_ok) - 1;
 
647
        }
568
648
 
569
649
        s->connection->read->handler = ngx_mail_proxy_handler;
570
650
        s->connection->write->handler = ngx_mail_proxy_handler;
612
692
 
613
693
    ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy dummy handler");
614
694
 
615
 
    if (ngx_handle_write_event(wev, 0) == NGX_ERROR) {
 
695
    if (ngx_handle_write_event(wev, 0) != NGX_OK) {
616
696
        c = wev->data;
617
697
        s = c->data;
618
698
 
703
783
    default: /* NGX_MAIL_SMTP_PROTOCOL */
704
784
        switch (state) {
705
785
 
 
786
        case ngx_smtp_start:
 
787
            if (p[0] == '2' && p[1] == '2' && p[2] == '0') {
 
788
                return NGX_OK;
 
789
            }
 
790
            break;
 
791
 
706
792
        case ngx_smtp_helo:
707
 
        case ngx_smtp_noxclient:
 
793
        case ngx_smtp_helo_xclient:
 
794
        case ngx_smtp_helo_from:
 
795
        case ngx_smtp_from:
708
796
            if (p[0] == '2' && p[1] == '5' && p[2] == '0') {
709
797
                return NGX_OK;
710
798
            }
711
799
            break;
712
800
 
713
 
        case ngx_smtp_start:
714
801
        case ngx_smtp_xclient:
715
 
            if (p[0] == '2' && p[1] == '2' && p[2] == '0') {
 
802
        case ngx_smtp_xclient_from:
 
803
        case ngx_smtp_xclient_helo:
 
804
            if (p[0] == '2' && (p[1] == '2' || p[1] == '5') && p[2] == '0') {
716
805
                return NGX_OK;
717
806
            }
718
807
            break;
 
808
 
 
809
        case ngx_smtp_to:
 
810
            return NGX_OK;
719
811
        }
720
812
 
721
813
        break;
884
976
        return;
885
977
    }
886
978
 
887
 
    if (ngx_handle_write_event(dst->write, 0) == NGX_ERROR) {
888
 
        ngx_mail_proxy_close_session(s);
889
 
        return;
890
 
    }
891
 
 
892
 
    if (ngx_handle_read_event(dst->read, 0) == NGX_ERROR) {
893
 
        ngx_mail_proxy_close_session(s);
894
 
        return;
895
 
    }
896
 
 
897
 
    if (ngx_handle_write_event(src->write, 0) == NGX_ERROR) {
898
 
        ngx_mail_proxy_close_session(s);
899
 
        return;
900
 
    }
901
 
 
902
 
    if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) {
 
979
    if (ngx_handle_write_event(dst->write, 0) != NGX_OK) {
 
980
        ngx_mail_proxy_close_session(s);
 
981
        return;
 
982
    }
 
983
 
 
984
    if (ngx_handle_read_event(dst->read, 0) != NGX_OK) {
 
985
        ngx_mail_proxy_close_session(s);
 
986
        return;
 
987
    }
 
988
 
 
989
    if (ngx_handle_write_event(src->write, 0) != NGX_OK) {
 
990
        ngx_mail_proxy_close_session(s);
 
991
        return;
 
992
    }
 
993
 
 
994
    if (ngx_handle_read_event(src->read, 0) != NGX_OK) {
903
995
        ngx_mail_proxy_close_session(s);
904
996
        return;
905
997
    }