ngx_mail_imap_handler.c revision e18a033b
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
10#include <ngx_event.h>
11#include <ngx_mail.h>
12#include <ngx_mail_imap_module.h>
13
14
15static ngx_int_t ngx_mail_imap_login(ngx_mail_session_t *s,
16    ngx_connection_t *c);
17static ngx_int_t ngx_mail_imap_authenticate(ngx_mail_session_t *s,
18    ngx_connection_t *c);
19static ngx_int_t ngx_mail_imap_capability(ngx_mail_session_t *s,
20    ngx_connection_t *c);
21static ngx_int_t ngx_mail_imap_starttls(ngx_mail_session_t *s,
22    ngx_connection_t *c);
23
24
25static u_char  imap_greeting[] = "* OK IMAP4 ready" CRLF;
26static u_char  imap_star[] = "* ";
27static u_char  imap_ok[] = "OK completed" CRLF;
28static u_char  imap_next[] = "+ OK" CRLF;
29static u_char  imap_plain_next[] = "+ " CRLF;
30static u_char  imap_username[] = "+ VXNlcm5hbWU6" CRLF;
31static u_char  imap_password[] = "+ UGFzc3dvcmQ6" CRLF;
32static u_char  imap_bye[] = "* BYE" CRLF;
33static u_char  imap_invalid_command[] = "BAD invalid command" CRLF;
34
35
36void
37ngx_mail_imap_init_session(ngx_mail_session_t *s, ngx_connection_t *c)
38{
39    ngx_mail_core_srv_conf_t  *cscf;
40
41    cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
42
43    ngx_str_set(&s->out, imap_greeting);
44
45    c->read->handler = ngx_mail_imap_init_protocol;
46
47    ngx_add_timer(c->read, cscf->timeout);
48
49    if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
50        ngx_mail_close_connection(c);
51    }
52
53    ngx_mail_send(c->write);
54}
55
56
57void
58ngx_mail_imap_init_protocol(ngx_event_t *rev)
59{
60    ngx_connection_t          *c;
61    ngx_mail_session_t        *s;
62    ngx_mail_imap_srv_conf_t  *iscf;
63
64    c = rev->data;
65
66    c->log->action = "in auth state";
67
68    if (rev->timedout) {
69        ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
70        c->timedout = 1;
71        ngx_mail_close_connection(c);
72        return;
73    }
74
75    s = c->data;
76
77    if (s->buffer == NULL) {
78        if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t))
79            == NGX_ERROR)
80        {
81            ngx_mail_session_internal_server_error(s);
82            return;
83        }
84
85        iscf = ngx_mail_get_module_srv_conf(s, ngx_mail_imap_module);
86
87        s->buffer = ngx_create_temp_buf(c->pool, iscf->client_buffer_size);
88        if (s->buffer == NULL) {
89            ngx_mail_session_internal_server_error(s);
90            return;
91        }
92    }
93
94    s->mail_state = ngx_imap_start;
95    c->read->handler = ngx_mail_imap_auth_state;
96
97    ngx_mail_imap_auth_state(rev);
98}
99
100
101void
102ngx_mail_imap_auth_state(ngx_event_t *rev)
103{
104    u_char              *p, *dst, *src, *end;
105    ngx_str_t           *arg;
106    ngx_int_t            rc;
107    ngx_uint_t           tag, i;
108    ngx_connection_t    *c;
109    ngx_mail_session_t  *s;
110
111    c = rev->data;
112    s = c->data;
113
114    ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "imap auth state");
115
116    if (rev->timedout) {
117        ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
118        c->timedout = 1;
119        ngx_mail_close_connection(c);
120        return;
121    }
122
123    if (s->out.len) {
124        ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "imap send handler busy");
125        s->blocked = 1;
126        return;
127    }
128
129    s->blocked = 0;
130
131    rc = ngx_mail_read_command(s, c);
132
133    if (rc == NGX_AGAIN || rc == NGX_ERROR) {
134        return;
135    }
136
137    tag = 1;
138    s->text.len = 0;
139    ngx_str_set(&s->out, imap_ok);
140
141    if (rc == NGX_OK) {
142
143        ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0, "imap auth command: %i",
144                       s->command);
145
146        if (s->backslash) {
147
148            arg = s->args.elts;
149
150            for (i = 0; i < s->args.nelts; i++) {
151                dst = arg[i].data;
152                end = dst + arg[i].len;
153
154                for (src = dst; src < end; dst++) {
155                    *dst = *src;
156                    if (*src++ == '\\') {
157                        *dst = *src++;
158                    }
159                }
160
161                arg[i].len = dst - arg[i].data;
162            }
163
164            s->backslash = 0;
165        }
166
167        switch (s->mail_state) {
168
169        case ngx_imap_start:
170
171            switch (s->command) {
172
173            case NGX_IMAP_LOGIN:
174                rc = ngx_mail_imap_login(s, c);
175                break;
176
177            case NGX_IMAP_AUTHENTICATE:
178                rc = ngx_mail_imap_authenticate(s, c);
179                tag = (rc != NGX_OK);
180                break;
181
182            case NGX_IMAP_CAPABILITY:
183                rc = ngx_mail_imap_capability(s, c);
184                break;
185
186            case NGX_IMAP_LOGOUT:
187                s->quit = 1;
188                ngx_str_set(&s->text, imap_bye);
189                break;
190
191            case NGX_IMAP_NOOP:
192                break;
193
194            case NGX_IMAP_STARTTLS:
195                rc = ngx_mail_imap_starttls(s, c);
196                break;
197
198            default:
199                rc = NGX_MAIL_PARSE_INVALID_COMMAND;
200                break;
201            }
202
203            break;
204
205        case ngx_imap_auth_login_username:
206            rc = ngx_mail_auth_login_username(s, c, 0);
207
208            tag = 0;
209            ngx_str_set(&s->out, imap_password);
210            s->mail_state = ngx_imap_auth_login_password;
211
212            break;
213
214        case ngx_imap_auth_login_password:
215            rc = ngx_mail_auth_login_password(s, c);
216            break;
217
218        case ngx_imap_auth_plain:
219            rc = ngx_mail_auth_plain(s, c, 0);
220            break;
221
222        case ngx_imap_auth_cram_md5:
223            rc = ngx_mail_auth_cram_md5(s, c);
224            break;
225
226        case ngx_imap_auth_external:
227            rc = ngx_mail_auth_external(s, c, 0);
228            break;
229        }
230
231    } else if (rc == NGX_IMAP_NEXT) {
232        tag = 0;
233        ngx_str_set(&s->out, imap_next);
234    }
235
236    switch (rc) {
237
238    case NGX_DONE:
239        ngx_mail_auth(s, c);
240        return;
241
242    case NGX_ERROR:
243        ngx_mail_session_internal_server_error(s);
244        return;
245
246    case NGX_MAIL_PARSE_INVALID_COMMAND:
247        s->state = 0;
248        ngx_str_set(&s->out, imap_invalid_command);
249        s->mail_state = ngx_imap_start;
250        break;
251    }
252
253    if (tag) {
254        if (s->tag.len == 0) {
255            ngx_str_set(&s->tag, imap_star);
256        }
257
258        if (s->tagged_line.len < s->tag.len + s->text.len + s->out.len) {
259            s->tagged_line.len = s->tag.len + s->text.len + s->out.len;
260            s->tagged_line.data = ngx_pnalloc(c->pool, s->tagged_line.len);
261            if (s->tagged_line.data == NULL) {
262                ngx_mail_close_connection(c);
263                return;
264            }
265        }
266
267        p = s->tagged_line.data;
268
269        if (s->text.len) {
270            p = ngx_cpymem(p, s->text.data, s->text.len);
271        }
272
273        p = ngx_cpymem(p, s->tag.data, s->tag.len);
274        ngx_memcpy(p, s->out.data, s->out.len);
275
276        s->out.len = s->text.len + s->tag.len + s->out.len;
277        s->out.data = s->tagged_line.data;
278    }
279
280    if (rc != NGX_IMAP_NEXT) {
281        s->args.nelts = 0;
282
283        if (s->state) {
284            /* preserve tag */
285            s->arg_start = s->buffer->start + s->tag.len;
286            s->buffer->pos = s->arg_start;
287            s->buffer->last = s->arg_start;
288
289        } else {
290            s->buffer->pos = s->buffer->start;
291            s->buffer->last = s->buffer->start;
292            s->tag.len = 0;
293        }
294    }
295
296    ngx_mail_send(c->write);
297}
298
299
300static ngx_int_t
301ngx_mail_imap_login(ngx_mail_session_t *s, ngx_connection_t *c)
302{
303    ngx_str_t  *arg;
304
305#if (NGX_MAIL_SSL)
306    if (ngx_mail_starttls_only(s, c)) {
307        return NGX_MAIL_PARSE_INVALID_COMMAND;
308    }
309#endif
310
311    arg = s->args.elts;
312
313    if (s->args.nelts != 2 || arg[0].len == 0) {
314        return NGX_MAIL_PARSE_INVALID_COMMAND;
315    }
316
317    s->login.len = arg[0].len;
318    s->login.data = ngx_pnalloc(c->pool, s->login.len);
319    if (s->login.data == NULL) {
320        return NGX_ERROR;
321    }
322
323    ngx_memcpy(s->login.data, arg[0].data, s->login.len);
324
325    s->passwd.len = arg[1].len;
326    s->passwd.data = ngx_pnalloc(c->pool, s->passwd.len);
327    if (s->passwd.data == NULL) {
328        return NGX_ERROR;
329    }
330
331    ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
332
333#if (NGX_DEBUG_MAIL_PASSWD)
334    ngx_log_debug2(NGX_LOG_DEBUG_MAIL, c->log, 0,
335                   "imap login:\"%V\" passwd:\"%V\"",
336                   &s->login, &s->passwd);
337#else
338    ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
339                   "imap login:\"%V\"", &s->login);
340#endif
341
342    return NGX_DONE;
343}
344
345
346static ngx_int_t
347ngx_mail_imap_authenticate(ngx_mail_session_t *s, ngx_connection_t *c)
348{
349    ngx_int_t                  rc;
350    ngx_mail_core_srv_conf_t  *cscf;
351    ngx_mail_imap_srv_conf_t  *iscf;
352
353#if (NGX_MAIL_SSL)
354    if (ngx_mail_starttls_only(s, c)) {
355        return NGX_MAIL_PARSE_INVALID_COMMAND;
356    }
357#endif
358
359    iscf = ngx_mail_get_module_srv_conf(s, ngx_mail_imap_module);
360
361    rc = ngx_mail_auth_parse(s, c);
362
363    switch (rc) {
364
365    case NGX_MAIL_AUTH_LOGIN:
366
367        ngx_str_set(&s->out, imap_username);
368        s->mail_state = ngx_imap_auth_login_username;
369
370        return NGX_OK;
371
372    case NGX_MAIL_AUTH_LOGIN_USERNAME:
373
374        ngx_str_set(&s->out, imap_password);
375        s->mail_state = ngx_imap_auth_login_password;
376
377        return ngx_mail_auth_login_username(s, c, 1);
378
379    case NGX_MAIL_AUTH_PLAIN:
380
381        ngx_str_set(&s->out, imap_plain_next);
382        s->mail_state = ngx_imap_auth_plain;
383
384        return NGX_OK;
385
386    case NGX_MAIL_AUTH_CRAM_MD5:
387
388        if (!(iscf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED)) {
389            return NGX_MAIL_PARSE_INVALID_COMMAND;
390        }
391
392        if (s->salt.data == NULL) {
393            cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
394
395            if (ngx_mail_salt(s, c, cscf) != NGX_OK) {
396                return NGX_ERROR;
397            }
398        }
399
400        if (ngx_mail_auth_cram_md5_salt(s, c, "+ ", 2) == NGX_OK) {
401            s->mail_state = ngx_imap_auth_cram_md5;
402            return NGX_OK;
403        }
404
405        return NGX_ERROR;
406
407    case NGX_MAIL_AUTH_EXTERNAL:
408
409        if (!(iscf->auth_methods & NGX_MAIL_AUTH_EXTERNAL_ENABLED)) {
410            return NGX_MAIL_PARSE_INVALID_COMMAND;
411        }
412
413        ngx_str_set(&s->out, imap_username);
414        s->mail_state = ngx_imap_auth_external;
415
416        return NGX_OK;
417    }
418
419    return rc;
420}
421
422
423static ngx_int_t
424ngx_mail_imap_capability(ngx_mail_session_t *s, ngx_connection_t *c)
425{
426    ngx_mail_imap_srv_conf_t  *iscf;
427
428    iscf = ngx_mail_get_module_srv_conf(s, ngx_mail_imap_module);
429
430#if (NGX_MAIL_SSL)
431
432    if (c->ssl == NULL) {
433        ngx_mail_ssl_conf_t  *sslcf;
434
435        sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
436
437        if (sslcf->starttls == NGX_MAIL_STARTTLS_ON) {
438            s->text = iscf->starttls_capability;
439            return NGX_OK;
440        }
441
442        if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) {
443            s->text = iscf->starttls_only_capability;
444            return NGX_OK;
445        }
446    }
447#endif
448
449    s->text = iscf->capability;
450
451    return NGX_OK;
452}
453
454
455static ngx_int_t
456ngx_mail_imap_starttls(ngx_mail_session_t *s, ngx_connection_t *c)
457{
458#if (NGX_MAIL_SSL)
459    ngx_mail_ssl_conf_t  *sslcf;
460
461    if (c->ssl == NULL) {
462        sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
463        if (sslcf->starttls) {
464            c->read->handler = ngx_mail_starttls_handler;
465            return NGX_OK;
466        }
467    }
468
469#endif
470
471    return NGX_MAIL_PARSE_INVALID_COMMAND;
472}
473