src/mail/ngx_mail_imap_handler.c - nginx source code

Global variables defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_event.h>
  8. #include <ngx_mail.h>
  9. #include <ngx_mail_imap_module.h>


  10. static ngx_int_t ngx_mail_imap_login(ngx_mail_session_t *s,
  11.     ngx_connection_t *c);
  12. static ngx_int_t ngx_mail_imap_authenticate(ngx_mail_session_t *s,
  13.     ngx_connection_t *c);
  14. static ngx_int_t ngx_mail_imap_capability(ngx_mail_session_t *s,
  15.     ngx_connection_t *c);
  16. static ngx_int_t ngx_mail_imap_starttls(ngx_mail_session_t *s,
  17.     ngx_connection_t *c);


  18. static u_char  imap_greeting[] = "* OK IMAP4 ready" CRLF;
  19. static u_char  imap_star[] = "* ";
  20. static u_char  imap_ok[] = "OK completed" CRLF;
  21. static u_char  imap_next[] = "+ OK" CRLF;
  22. static u_char  imap_plain_next[] = "+ " CRLF;
  23. static u_char  imap_username[] = "+ VXNlcm5hbWU6" CRLF;
  24. static u_char  imap_password[] = "+ UGFzc3dvcmQ6" CRLF;
  25. static u_char  imap_bye[] = "* BYE" CRLF;
  26. static u_char  imap_invalid_command[] = "BAD invalid command" CRLF;


  27. void
  28. ngx_mail_imap_init_session(ngx_mail_session_t *s, ngx_connection_t *c)
  29. {
  30.     ngx_mail_core_srv_conf_t  *cscf;

  31.     cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);

  32.     ngx_str_set(&s->out, imap_greeting);

  33.     c->read->handler = ngx_mail_imap_init_protocol;

  34.     ngx_add_timer(c->read, cscf->timeout);

  35.     if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  36.         ngx_mail_close_connection(c);
  37.     }

  38.     ngx_mail_send(c->write);
  39. }


  40. void
  41. ngx_mail_imap_init_protocol(ngx_event_t *rev)
  42. {
  43.     ngx_connection_t          *c;
  44.     ngx_mail_session_t        *s;
  45.     ngx_mail_imap_srv_conf_t  *iscf;

  46.     c = rev->data;

  47.     c->log->action = "in auth state";

  48.     if (rev->timedout) {
  49.         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
  50.         c->timedout = 1;
  51.         ngx_mail_close_connection(c);
  52.         return;
  53.     }

  54.     s = c->data;

  55.     if (s->buffer == NULL) {
  56.         if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t))
  57.             == NGX_ERROR)
  58.         {
  59.             ngx_mail_session_internal_server_error(s);
  60.             return;
  61.         }

  62.         iscf = ngx_mail_get_module_srv_conf(s, ngx_mail_imap_module);

  63.         s->buffer = ngx_create_temp_buf(c->pool, iscf->client_buffer_size);
  64.         if (s->buffer == NULL) {
  65.             ngx_mail_session_internal_server_error(s);
  66.             return;
  67.         }
  68.     }

  69.     s->mail_state = ngx_imap_start;
  70.     c->read->handler = ngx_mail_imap_auth_state;

  71.     ngx_mail_imap_auth_state(rev);
  72. }


  73. void
  74. ngx_mail_imap_auth_state(ngx_event_t *rev)
  75. {
  76.     u_char              *p;
  77.     ngx_int_t            rc;
  78.     ngx_uint_t           tag;
  79.     ngx_connection_t    *c;
  80.     ngx_mail_session_t  *s;

  81.     c = rev->data;
  82.     s = c->data;

  83.     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "imap auth state");

  84.     if (rev->timedout) {
  85.         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
  86.         c->timedout = 1;
  87.         ngx_mail_close_connection(c);
  88.         return;
  89.     }

  90.     if (s->out.len) {
  91.         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "imap send handler busy");
  92.         s->blocked = 1;

  93.         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  94.             ngx_mail_close_connection(c);
  95.             return;
  96.         }

  97.         return;
  98.     }

  99.     s->blocked = 0;

  100.     rc = ngx_mail_read_command(s, c);

  101.     if (rc == NGX_AGAIN) {
  102.         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  103.             ngx_mail_session_internal_server_error(s);
  104.             return;
  105.         }

  106.         return;
  107.     }

  108.     if (rc == NGX_ERROR) {
  109.         return;
  110.     }

  111.     tag = 1;
  112.     s->text.len = 0;
  113.     ngx_str_set(&s->out, imap_ok);

  114.     if (rc == NGX_OK) {

  115.         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0, "imap auth command: %i",
  116.                        s->command);

  117.         switch (s->mail_state) {

  118.         case ngx_imap_start:

  119.             switch (s->command) {

  120.             case NGX_IMAP_LOGIN:
  121.                 rc = ngx_mail_imap_login(s, c);
  122.                 break;

  123.             case NGX_IMAP_AUTHENTICATE:
  124.                 rc = ngx_mail_imap_authenticate(s, c);
  125.                 tag = (rc != NGX_OK);
  126.                 break;

  127.             case NGX_IMAP_CAPABILITY:
  128.                 rc = ngx_mail_imap_capability(s, c);
  129.                 break;

  130.             case NGX_IMAP_LOGOUT:
  131.                 s->quit = 1;
  132.                 ngx_str_set(&s->text, imap_bye);
  133.                 break;

  134.             case NGX_IMAP_NOOP:
  135.                 break;

  136.             case NGX_IMAP_STARTTLS:
  137.                 rc = ngx_mail_imap_starttls(s, c);
  138.                 break;

  139.             default:
  140.                 rc = NGX_MAIL_PARSE_INVALID_COMMAND;
  141.                 break;
  142.             }

  143.             break;

  144.         case ngx_imap_auth_login_username:
  145.             rc = ngx_mail_auth_login_username(s, c, 0);

  146.             tag = 0;
  147.             ngx_str_set(&s->out, imap_password);
  148.             s->mail_state = ngx_imap_auth_login_password;

  149.             break;

  150.         case ngx_imap_auth_login_password:
  151.             rc = ngx_mail_auth_login_password(s, c);
  152.             break;

  153.         case ngx_imap_auth_plain:
  154.             rc = ngx_mail_auth_plain(s, c, 0);
  155.             break;

  156.         case ngx_imap_auth_cram_md5:
  157.             rc = ngx_mail_auth_cram_md5(s, c);
  158.             break;

  159.         case ngx_imap_auth_external:
  160.             rc = ngx_mail_auth_external(s, c, 0);
  161.             break;
  162.         }

  163.     } else if (rc == NGX_IMAP_NEXT) {
  164.         tag = 0;
  165.         ngx_str_set(&s->out, imap_next);
  166.     }

  167.     if (s->buffer->pos < s->buffer->last) {
  168.         s->blocked = 1;
  169.     }

  170.     switch (rc) {

  171.     case NGX_DONE:
  172.         ngx_mail_auth(s, c);
  173.         return;

  174.     case NGX_ERROR:
  175.         ngx_mail_session_internal_server_error(s);
  176.         return;

  177.     case NGX_MAIL_PARSE_INVALID_COMMAND:
  178.         s->state = 0;
  179.         ngx_str_set(&s->out, imap_invalid_command);
  180.         s->mail_state = ngx_imap_start;
  181.         break;
  182.     }

  183.     if (tag) {
  184.         if (s->tag.len == 0) {
  185.             ngx_str_set(&s->tag, imap_star);
  186.         }

  187.         if (s->tagged_line.len < s->tag.len + s->text.len + s->out.len) {
  188.             s->tagged_line.len = s->tag.len + s->text.len + s->out.len;
  189.             s->tagged_line.data = ngx_pnalloc(c->pool, s->tagged_line.len);
  190.             if (s->tagged_line.data == NULL) {
  191.                 ngx_mail_close_connection(c);
  192.                 return;
  193.             }
  194.         }

  195.         p = s->tagged_line.data;

  196.         if (s->text.len) {
  197.             p = ngx_cpymem(p, s->text.data, s->text.len);
  198.         }

  199.         p = ngx_cpymem(p, s->tag.data, s->tag.len);
  200.         ngx_memcpy(p, s->out.data, s->out.len);

  201.         s->out.len = s->text.len + s->tag.len + s->out.len;
  202.         s->out.data = s->tagged_line.data;
  203.     }

  204.     if (rc != NGX_IMAP_NEXT) {
  205.         s->args.nelts = 0;

  206.         if (s->state) {
  207.             /* preserve tag */
  208.             s->arg_start = s->buffer->pos;

  209.         } else {
  210.             if (s->buffer->pos == s->buffer->last) {
  211.                 s->buffer->pos = s->buffer->start;
  212.                 s->buffer->last = s->buffer->start;
  213.             }

  214.             s->tag.len = 0;
  215.         }
  216.     }

  217.     if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
  218.         ngx_mail_session_internal_server_error(s);
  219.         return;
  220.     }

  221.     ngx_mail_send(c->write);
  222. }


  223. static ngx_int_t
  224. ngx_mail_imap_login(ngx_mail_session_t *s, ngx_connection_t *c)
  225. {
  226.     ngx_str_t  *arg;

  227. #if (NGX_MAIL_SSL)
  228.     if (ngx_mail_starttls_only(s, c)) {
  229.         return NGX_MAIL_PARSE_INVALID_COMMAND;
  230.     }
  231. #endif

  232.     arg = s->args.elts;

  233.     if (s->args.nelts != 2 || arg[0].len == 0) {
  234.         return NGX_MAIL_PARSE_INVALID_COMMAND;
  235.     }

  236.     s->login.len = arg[0].len;
  237.     s->login.data = ngx_pnalloc(c->pool, s->login.len);
  238.     if (s->login.data == NULL) {
  239.         return NGX_ERROR;
  240.     }

  241.     ngx_memcpy(s->login.data, arg[0].data, s->login.len);

  242.     s->passwd.len = arg[1].len;
  243.     s->passwd.data = ngx_pnalloc(c->pool, s->passwd.len);
  244.     if (s->passwd.data == NULL) {
  245.         return NGX_ERROR;
  246.     }

  247.     ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);

  248. #if (NGX_DEBUG_MAIL_PASSWD)
  249.     ngx_log_debug2(NGX_LOG_DEBUG_MAIL, c->log, 0,
  250.                    "imap login:\"%V\" passwd:\"%V\"",
  251.                    &s->login, &s->passwd);
  252. #else
  253.     ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
  254.                    "imap login:\"%V\"", &s->login);
  255. #endif

  256.     return NGX_DONE;
  257. }


  258. static ngx_int_t
  259. ngx_mail_imap_authenticate(ngx_mail_session_t *s, ngx_connection_t *c)
  260. {
  261.     ngx_int_t                  rc;
  262.     ngx_mail_core_srv_conf_t  *cscf;
  263.     ngx_mail_imap_srv_conf_t  *iscf;

  264. #if (NGX_MAIL_SSL)
  265.     if (ngx_mail_starttls_only(s, c)) {
  266.         return NGX_MAIL_PARSE_INVALID_COMMAND;
  267.     }
  268. #endif

  269.     iscf = ngx_mail_get_module_srv_conf(s, ngx_mail_imap_module);

  270.     rc = ngx_mail_auth_parse(s, c);

  271.     switch (rc) {

  272.     case NGX_MAIL_AUTH_LOGIN:

  273.         ngx_str_set(&s->out, imap_username);
  274.         s->mail_state = ngx_imap_auth_login_username;

  275.         return NGX_OK;

  276.     case NGX_MAIL_AUTH_LOGIN_USERNAME:

  277.         ngx_str_set(&s->out, imap_password);
  278.         s->mail_state = ngx_imap_auth_login_password;

  279.         return ngx_mail_auth_login_username(s, c, 1);

  280.     case NGX_MAIL_AUTH_PLAIN:

  281.         ngx_str_set(&s->out, imap_plain_next);
  282.         s->mail_state = ngx_imap_auth_plain;

  283.         return NGX_OK;

  284.     case NGX_MAIL_AUTH_CRAM_MD5:

  285.         if (!(iscf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED)) {
  286.             return NGX_MAIL_PARSE_INVALID_COMMAND;
  287.         }

  288.         if (s->salt.data == NULL) {
  289.             cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);

  290.             if (ngx_mail_salt(s, c, cscf) != NGX_OK) {
  291.                 return NGX_ERROR;
  292.             }
  293.         }

  294.         if (ngx_mail_auth_cram_md5_salt(s, c, "+ ", 2) == NGX_OK) {
  295.             s->mail_state = ngx_imap_auth_cram_md5;
  296.             return NGX_OK;
  297.         }

  298.         return NGX_ERROR;

  299.     case NGX_MAIL_AUTH_EXTERNAL:

  300.         if (!(iscf->auth_methods & NGX_MAIL_AUTH_EXTERNAL_ENABLED)) {
  301.             return NGX_MAIL_PARSE_INVALID_COMMAND;
  302.         }

  303.         ngx_str_set(&s->out, imap_username);
  304.         s->mail_state = ngx_imap_auth_external;

  305.         return NGX_OK;
  306.     }

  307.     return rc;
  308. }


  309. static ngx_int_t
  310. ngx_mail_imap_capability(ngx_mail_session_t *s, ngx_connection_t *c)
  311. {
  312.     ngx_mail_imap_srv_conf_t  *iscf;

  313.     iscf = ngx_mail_get_module_srv_conf(s, ngx_mail_imap_module);

  314. #if (NGX_MAIL_SSL)

  315.     if (c->ssl == NULL) {
  316.         ngx_mail_ssl_conf_t  *sslcf;

  317.         sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);

  318.         if (sslcf->starttls == NGX_MAIL_STARTTLS_ON) {
  319.             s->text = iscf->starttls_capability;
  320.             return NGX_OK;
  321.         }

  322.         if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) {
  323.             s->text = iscf->starttls_only_capability;
  324.             return NGX_OK;
  325.         }
  326.     }
  327. #endif

  328.     s->text = iscf->capability;

  329.     return NGX_OK;
  330. }


  331. static ngx_int_t
  332. ngx_mail_imap_starttls(ngx_mail_session_t *s, ngx_connection_t *c)
  333. {
  334. #if (NGX_MAIL_SSL)
  335.     ngx_mail_ssl_conf_t  *sslcf;

  336.     if (c->ssl == NULL) {
  337.         sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
  338.         if (sslcf->starttls) {
  339.             s->buffer->pos = s->buffer->start;
  340.             s->buffer->last = s->buffer->start;
  341.             c->read->handler = ngx_mail_starttls_handler;
  342.             return NGX_OK;
  343.         }
  344.     }

  345. #endif

  346.     return NGX_MAIL_PARSE_INVALID_COMMAND;
  347. }