changeset 1481:b58ce1cf66da

smtp_client_buffer and smtp_greeting_delay
author Igor Sysoev <igor@sysoev.ru>
date Fri, 14 Sep 2007 13:58:49 +0000
parents a231e37a19ab
children 4606dce4f416
files src/mail/ngx_mail.h src/mail/ngx_mail_core_module.c src/mail/ngx_mail_smtp_handler.c
diffstat 3 files changed, 194 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/src/mail/ngx_mail.h
+++ b/src/mail/ngx_mail.h
@@ -75,8 +75,10 @@ typedef struct {
 
 typedef struct {
     ngx_msec_t              timeout;
+    ngx_msec_t              smtp_greeting_delay;
 
     size_t                  imap_client_buffer_size;
+    size_t                  smtp_client_buffer_size;
 
     ngx_uint_t              protocol;
 
--- a/src/mail/ngx_mail_core_module.c
+++ b/src/mail/ngx_mail_core_module.c
@@ -132,6 +132,20 @@ static ngx_command_t  ngx_mail_core_comm
       offsetof(ngx_mail_core_srv_conf_t, imap_client_buffer_size),
       NULL },
 
+    { ngx_string("smtp_client_buffer"),
+      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_size_slot,
+      NGX_MAIL_SRV_CONF_OFFSET,
+      offsetof(ngx_mail_core_srv_conf_t, smtp_client_buffer_size),
+      NULL },
+
+    { ngx_string("smtp_greeting_delay"),
+      NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_msec_slot,
+      NGX_MAIL_SRV_CONF_OFFSET,
+      offsetof(ngx_mail_core_srv_conf_t, smtp_greeting_delay),
+      NULL },
+
     { ngx_string("so_keepalive"),
       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
       ngx_conf_set_flag_slot,
@@ -269,8 +283,10 @@ ngx_mail_core_create_srv_conf(ngx_conf_t
     }
 
     cscf->imap_client_buffer_size = NGX_CONF_UNSET_SIZE;
+    cscf->smtp_client_buffer_size = NGX_CONF_UNSET_SIZE;
     cscf->protocol = NGX_CONF_UNSET_UINT;
     cscf->timeout = NGX_CONF_UNSET_MSEC;
+    cscf->smtp_greeting_delay = NGX_CONF_UNSET_MSEC;
     cscf->so_keepalive = NGX_CONF_UNSET;
 
     if (ngx_array_init(&cscf->pop3_capabilities, cf->pool, 4, sizeof(ngx_str_t))
@@ -309,27 +325,33 @@ ngx_mail_core_merge_srv_conf(ngx_conf_t 
     ngx_conf_merge_size_value(conf->imap_client_buffer_size,
                               prev->imap_client_buffer_size,
                               (size_t) ngx_pagesize);
+    ngx_conf_merge_size_value(conf->smtp_client_buffer_size,
+                              prev->smtp_client_buffer_size,
+                              (size_t) ngx_pagesize);
+
     ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
+    ngx_conf_merge_msec_value(conf->smtp_greeting_delay,
+                              prev->smtp_greeting_delay, 0);
+
     ngx_conf_merge_uint_value(conf->protocol, prev->protocol,
                               NGX_MAIL_IMAP_PROTOCOL);
     ngx_conf_merge_value(conf->so_keepalive, prev->so_keepalive, 0);
 
-
     ngx_conf_merge_bitmask_value(conf->pop3_auth_methods,
-                                 prev->pop3_auth_methods,
-                                 (NGX_CONF_BITMASK_SET
-                                  |NGX_MAIL_AUTH_PLAIN_ENABLED));
+                              prev->pop3_auth_methods,
+                              (NGX_CONF_BITMASK_SET
+                               |NGX_MAIL_AUTH_PLAIN_ENABLED));
 
     ngx_conf_merge_bitmask_value(conf->imap_auth_methods,
-                                 prev->imap_auth_methods,
-                                 (NGX_CONF_BITMASK_SET
-                                  |NGX_MAIL_AUTH_PLAIN_ENABLED));
+                              prev->imap_auth_methods,
+                              (NGX_CONF_BITMASK_SET
+                               |NGX_MAIL_AUTH_PLAIN_ENABLED));
 
     ngx_conf_merge_bitmask_value(conf->smtp_auth_methods,
-                                 prev->smtp_auth_methods,
-                                 (NGX_CONF_BITMASK_SET
-                                  |NGX_MAIL_AUTH_PLAIN_ENABLED
-                                  |NGX_MAIL_AUTH_LOGIN_ENABLED));
+                              prev->smtp_auth_methods,
+                              (NGX_CONF_BITMASK_SET
+                               |NGX_MAIL_AUTH_PLAIN_ENABLED
+                               |NGX_MAIL_AUTH_LOGIN_ENABLED));
 
 
     ngx_conf_merge_str_value(conf->server_name, prev->server_name, "");
--- a/src/mail/ngx_mail_smtp_handler.c
+++ b/src/mail/ngx_mail_smtp_handler.c
@@ -10,12 +10,19 @@
 #include <ngx_mail.h>
 
 
+static void ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev);
+
 static ngx_int_t ngx_mail_smtp_helo(ngx_mail_session_t *s, ngx_connection_t *c);
 static ngx_int_t ngx_mail_smtp_auth(ngx_mail_session_t *s, ngx_connection_t *c);
 static ngx_int_t ngx_mail_smtp_mail(ngx_mail_session_t *s, ngx_connection_t *c);
 static ngx_int_t ngx_mail_smtp_starttls(ngx_mail_session_t *s,
     ngx_connection_t *c);
 
+static ngx_int_t ngx_mail_smtp_discard_command(ngx_mail_session_t *s,
+    ngx_connection_t *c, char *err);
+static void ngx_mail_smtp_log_rejected_command(ngx_mail_session_t *s,
+    ngx_connection_t *c, char *err);
+
 
 static u_char  smtp_ok[] = "250 2.0.0 OK" CRLF;
 static u_char  smtp_bye[] = "221 2.0.0 Bye" CRLF;
@@ -23,6 +30,8 @@ static u_char  smtp_next[] = "334 " CRLF
 static u_char  smtp_username[] = "334 VXNlcm5hbWU6" CRLF;
 static u_char  smtp_password[] = "334 UGFzc3dvcmQ6" CRLF;
 static u_char  smtp_invalid_command[] = "500 5.5.1 Invalid command" CRLF;
+static u_char  smtp_invalid_pipelining[] =
+   "503 5.5.0 Improper use of SMTP command pipelining" CRLF;
 static u_char  smtp_invalid_argument[] = "501 5.5.4 Invalid argument" CRLF;
 static u_char  smtp_auth_required[] = "530 5.7.1 Authentication required" CRLF;
 
@@ -30,6 +39,7 @@ static u_char  smtp_auth_required[] = "5
 void
 ngx_mail_smtp_init_session(ngx_mail_session_t *s, ngx_connection_t *c)
 {
+    ngx_msec_t                 timeout;
     ngx_mail_core_srv_conf_t  *cscf;
 
     cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
@@ -41,14 +51,84 @@ ngx_mail_smtp_init_session(ngx_mail_sess
         }
     }
 
-    s->out = cscf->smtp_greeting;
+    if (s->buffer == NULL) {
+        if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t))
+            == NGX_ERROR)
+        {
+            ngx_mail_session_internal_server_error(s);
+            return;
+        }
+
+        s->buffer = ngx_create_temp_buf(c->pool, cscf->smtp_client_buffer_size);
+        if (s->buffer == NULL) {
+            ngx_mail_session_internal_server_error(s);
+            return;
+        }
+    }
+
+    timeout = cscf->smtp_greeting_delay ? cscf->smtp_greeting_delay:
+                                          cscf->timeout;
+    ngx_add_timer(c->read, timeout); 
+
+    if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
+        ngx_mail_close_connection(c);
+    }
+
+    if (cscf->smtp_greeting_delay) {
+         c->read->handler = ngx_mail_smtp_invalid_pipelining;
+         return;
+    }
 
     c->read->handler = ngx_mail_smtp_init_protocol;
 
-    ngx_add_timer(c->read, cscf->timeout); 
+    s->out = cscf->smtp_greeting;
+
+    ngx_mail_send(c->write);
+}
+
+
+static void
+ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev)
+{
+    ngx_connection_t          *c;
+    ngx_mail_session_t        *s;
+    ngx_mail_core_srv_conf_t  *cscf;
+
+    c = rev->data;
+    s = c->data;
+
+    c->log->action = "in delay pipelining state";
+
+    if (rev->timedout) {
+
+        ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "delay greeting");
+
+        cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
 
-    if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
-        ngx_mail_close_connection(c);
+        c->read->handler = ngx_mail_smtp_init_protocol;
+
+        ngx_add_timer(c->read, cscf->timeout); 
+
+        if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
+            ngx_mail_close_connection(c);
+            return;
+        }
+
+        s->out = cscf->smtp_greeting;
+
+    } else {
+
+        ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "invalid pipelining");
+
+        if (ngx_mail_smtp_discard_command(s, c,
+                                "client was rejected before greeting: \"%V\"")
+            != NGX_OK)
+        {
+            return;
+        }
+
+        s->out.len = sizeof(smtp_invalid_pipelining) - 1;
+        s->out.data = smtp_invalid_pipelining;
     }
 
     ngx_mail_send(c->write);
@@ -74,21 +154,6 @@ ngx_mail_smtp_init_protocol(ngx_event_t 
 
     s = c->data;
 
-    if (s->buffer == NULL) {
-        if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t))
-            == NGX_ERROR)
-        {
-            ngx_mail_session_internal_server_error(s);
-            return;
-        }
-
-        s->buffer = ngx_create_temp_buf(c->pool, 512);
-        if (s->buffer == NULL) {
-            ngx_mail_session_internal_server_error(s);
-            return;
-        }
-    }
-
     s->mail_state = ngx_smtp_start;
     c->read->handler = ngx_mail_smtp_auth_state;
 
@@ -349,37 +414,7 @@ ngx_mail_smtp_auth(ngx_mail_session_t *s
 static ngx_int_t
 ngx_mail_smtp_mail(ngx_mail_session_t *s, ngx_connection_t *c)
 {
-    u_char      ch;
-    ngx_str_t   mail;
-    ngx_uint_t  i;
-
-    if (c->log->log_level >= NGX_LOG_INFO) {
-        mail.len = s->buffer->last - s->buffer->start;
-        mail.data = s->buffer->start;
-
-        for (i = 0; i < mail.len; i++) {
-            ch = mail.data[i];
-
-            if (ch != CR && ch != LF) {
-                continue;
-            }
-
-            mail.data[i] = ' ';
-        }
-
-        while (i) {
-            if (mail.data[i - 1] != ' ') {
-                break;
-            }
-
-            i--;
-        }
-
-        mail.len = i;
-
-        ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
-                      "client was rejected: \"%V\"", &mail);
-    }
+    ngx_mail_smtp_log_rejected_command(s, c, "client was rejected: \"%V\"");
 
     s->out.len = sizeof(smtp_auth_required) - 1;
     s->out.data = smtp_auth_required;
@@ -415,3 +450,77 @@ ngx_mail_smtp_starttls(ngx_mail_session_
 
     return NGX_MAIL_PARSE_INVALID_COMMAND;
 }
+
+
+static ngx_int_t
+ngx_mail_smtp_discard_command(ngx_mail_session_t *s, ngx_connection_t *c,
+    char *err)
+{
+    ssize_t    n;
+
+    n = c->recv(c, s->buffer->last, s->buffer->end - s->buffer->last);
+
+    if (n == NGX_ERROR || n == 0) {
+        ngx_mail_close_connection(c);
+        return NGX_ERROR;
+    }
+
+    if (n > 0) {
+        s->buffer->last += n;
+    }
+    
+    if (n == NGX_AGAIN) {
+        if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
+            ngx_mail_session_internal_server_error(s);
+            return NGX_ERROR;
+        }
+
+        return NGX_AGAIN;
+    }
+
+    ngx_mail_smtp_log_rejected_command(s, c, err);
+
+    s->buffer->pos = s->buffer->start;
+    s->buffer->last = s->buffer->start;
+
+    return NGX_OK;
+}
+
+
+static void
+ngx_mail_smtp_log_rejected_command(ngx_mail_session_t *s, ngx_connection_t *c,
+    char *err)
+{
+    u_char      ch;
+    ngx_str_t   cmd;
+    ngx_uint_t  i;
+
+    if (c->log->log_level < NGX_LOG_INFO) {
+        return;
+    }
+
+    cmd.len = s->buffer->last - s->buffer->start;
+    cmd.data = s->buffer->start;
+
+    for (i = 0; i < cmd.len; i++) {
+        ch = cmd.data[i];
+
+        if (ch != CR && ch != LF) {
+            continue;
+        }
+
+        cmd.data[i] = ' ';
+    }
+
+    while (i) {
+        if (cmd.data[i - 1] != ' ') {
+            break;
+        }
+
+        i--;
+    }
+
+    cmd.len = i;
+
+    ngx_log_error(NGX_LOG_INFO, c->log, 0, err, &cmd);
+}