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 
15 static ngx_int_t ngx_mail_imap_login(ngx_mail_session_t *s,
16     ngx_connection_t *c);
17 static ngx_int_t ngx_mail_imap_authenticate(ngx_mail_session_t *s,
18     ngx_connection_t *c);
19 static ngx_int_t ngx_mail_imap_capability(ngx_mail_session_t *s,
20     ngx_connection_t *c);
21 static ngx_int_t ngx_mail_imap_starttls(ngx_mail_session_t *s,
22     ngx_connection_t *c);
23 
24 
25 static u_char  imap_greeting[] = "* OK IMAP4 ready" CRLF;
26 static u_char  imap_star[] = "* ";
27 static u_char  imap_ok[] = "OK completed" CRLF;
28 static u_char  imap_next[] = "+ OK" CRLF;
29 static u_char  imap_plain_next[] = "+ " CRLF;
30 static u_char  imap_username[] = "+ VXNlcm5hbWU6" CRLF;
31 static u_char  imap_password[] = "+ UGFzc3dvcmQ6" CRLF;
32 static u_char  imap_bye[] = "* BYE" CRLF;
33 static u_char  imap_invalid_command[] = "BAD invalid command" CRLF;
34 
35 
36 void
ngx_mail_imap_init_session(ngx_mail_session_t * s,ngx_connection_t * c)37 ngx_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 
57 void
ngx_mail_imap_init_protocol(ngx_event_t * rev)58 ngx_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 
101 void
ngx_mail_imap_auth_state(ngx_event_t * rev)102 ngx_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 
127         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
128             ngx_mail_close_connection(c);
129             return;
130         }
131 
132         return;
133     }
134 
135     s->blocked = 0;
136 
137     rc = ngx_mail_read_command(s, c);
138 
139     if (rc == NGX_AGAIN) {
140         if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
141             ngx_mail_session_internal_server_error(s);
142             return;
143         }
144 
145         return;
146     }
147 
148     if (rc == NGX_ERROR) {
149         return;
150     }
151 
152     tag = 1;
153     s->text.len = 0;
154     ngx_str_set(&s->out, imap_ok);
155 
156     if (rc == NGX_OK) {
157 
158         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0, "imap auth command: %i",
159                        s->command);
160 
161         if (s->backslash) {
162 
163             arg = s->args.elts;
164 
165             for (i = 0; i < s->args.nelts; i++) {
166                 dst = arg[i].data;
167                 end = dst + arg[i].len;
168 
169                 for (src = dst; src < end; dst++) {
170                     *dst = *src;
171                     if (*src++ == '\\') {
172                         *dst = *src++;
173                     }
174                 }
175 
176                 arg[i].len = dst - arg[i].data;
177             }
178 
179             s->backslash = 0;
180         }
181 
182         switch (s->mail_state) {
183 
184         case ngx_imap_start:
185 
186             switch (s->command) {
187 
188             case NGX_IMAP_LOGIN:
189                 rc = ngx_mail_imap_login(s, c);
190                 break;
191 
192             case NGX_IMAP_AUTHENTICATE:
193                 rc = ngx_mail_imap_authenticate(s, c);
194                 tag = (rc != NGX_OK);
195                 break;
196 
197             case NGX_IMAP_CAPABILITY:
198                 rc = ngx_mail_imap_capability(s, c);
199                 break;
200 
201             case NGX_IMAP_LOGOUT:
202                 s->quit = 1;
203                 ngx_str_set(&s->text, imap_bye);
204                 break;
205 
206             case NGX_IMAP_NOOP:
207                 break;
208 
209             case NGX_IMAP_STARTTLS:
210                 rc = ngx_mail_imap_starttls(s, c);
211                 break;
212 
213             default:
214                 rc = NGX_MAIL_PARSE_INVALID_COMMAND;
215                 break;
216             }
217 
218             break;
219 
220         case ngx_imap_auth_login_username:
221             rc = ngx_mail_auth_login_username(s, c, 0);
222 
223             tag = 0;
224             ngx_str_set(&s->out, imap_password);
225             s->mail_state = ngx_imap_auth_login_password;
226 
227             break;
228 
229         case ngx_imap_auth_login_password:
230             rc = ngx_mail_auth_login_password(s, c);
231             break;
232 
233         case ngx_imap_auth_plain:
234             rc = ngx_mail_auth_plain(s, c, 0);
235             break;
236 
237         case ngx_imap_auth_cram_md5:
238             rc = ngx_mail_auth_cram_md5(s, c);
239             break;
240 
241         case ngx_imap_auth_external:
242             rc = ngx_mail_auth_external(s, c, 0);
243             break;
244         }
245 
246     } else if (rc == NGX_IMAP_NEXT) {
247         tag = 0;
248         ngx_str_set(&s->out, imap_next);
249     }
250 
251     switch (rc) {
252 
253     case NGX_DONE:
254         ngx_mail_auth(s, c);
255         return;
256 
257     case NGX_ERROR:
258         ngx_mail_session_internal_server_error(s);
259         return;
260 
261     case NGX_MAIL_PARSE_INVALID_COMMAND:
262         s->state = 0;
263         ngx_str_set(&s->out, imap_invalid_command);
264         s->mail_state = ngx_imap_start;
265         break;
266     }
267 
268     if (tag) {
269         if (s->tag.len == 0) {
270             ngx_str_set(&s->tag, imap_star);
271         }
272 
273         if (s->tagged_line.len < s->tag.len + s->text.len + s->out.len) {
274             s->tagged_line.len = s->tag.len + s->text.len + s->out.len;
275             s->tagged_line.data = ngx_pnalloc(c->pool, s->tagged_line.len);
276             if (s->tagged_line.data == NULL) {
277                 ngx_mail_close_connection(c);
278                 return;
279             }
280         }
281 
282         p = s->tagged_line.data;
283 
284         if (s->text.len) {
285             p = ngx_cpymem(p, s->text.data, s->text.len);
286         }
287 
288         p = ngx_cpymem(p, s->tag.data, s->tag.len);
289         ngx_memcpy(p, s->out.data, s->out.len);
290 
291         s->out.len = s->text.len + s->tag.len + s->out.len;
292         s->out.data = s->tagged_line.data;
293     }
294 
295     if (rc != NGX_IMAP_NEXT) {
296         s->args.nelts = 0;
297 
298         if (s->state) {
299             /* preserve tag */
300             s->arg_start = s->buffer->start + s->tag.len;
301             s->buffer->pos = s->arg_start;
302             s->buffer->last = s->arg_start;
303 
304         } else {
305             s->buffer->pos = s->buffer->start;
306             s->buffer->last = s->buffer->start;
307             s->tag.len = 0;
308         }
309     }
310 
311     if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
312         ngx_mail_session_internal_server_error(s);
313         return;
314     }
315 
316     ngx_mail_send(c->write);
317 }
318 
319 
320 static ngx_int_t
ngx_mail_imap_login(ngx_mail_session_t * s,ngx_connection_t * c)321 ngx_mail_imap_login(ngx_mail_session_t *s, ngx_connection_t *c)
322 {
323     ngx_str_t  *arg;
324 
325 #if (NGX_MAIL_SSL)
326     if (ngx_mail_starttls_only(s, c)) {
327         return NGX_MAIL_PARSE_INVALID_COMMAND;
328     }
329 #endif
330 
331     arg = s->args.elts;
332 
333     if (s->args.nelts != 2 || arg[0].len == 0) {
334         return NGX_MAIL_PARSE_INVALID_COMMAND;
335     }
336 
337     s->login.len = arg[0].len;
338     s->login.data = ngx_pnalloc(c->pool, s->login.len);
339     if (s->login.data == NULL) {
340         return NGX_ERROR;
341     }
342 
343     ngx_memcpy(s->login.data, arg[0].data, s->login.len);
344 
345     s->passwd.len = arg[1].len;
346     s->passwd.data = ngx_pnalloc(c->pool, s->passwd.len);
347     if (s->passwd.data == NULL) {
348         return NGX_ERROR;
349     }
350 
351     ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len);
352 
353 #if (NGX_DEBUG_MAIL_PASSWD)
354     ngx_log_debug2(NGX_LOG_DEBUG_MAIL, c->log, 0,
355                    "imap login:\"%V\" passwd:\"%V\"",
356                    &s->login, &s->passwd);
357 #else
358     ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
359                    "imap login:\"%V\"", &s->login);
360 #endif
361 
362     return NGX_DONE;
363 }
364 
365 
366 static ngx_int_t
ngx_mail_imap_authenticate(ngx_mail_session_t * s,ngx_connection_t * c)367 ngx_mail_imap_authenticate(ngx_mail_session_t *s, ngx_connection_t *c)
368 {
369     ngx_int_t                  rc;
370     ngx_mail_core_srv_conf_t  *cscf;
371     ngx_mail_imap_srv_conf_t  *iscf;
372 
373 #if (NGX_MAIL_SSL)
374     if (ngx_mail_starttls_only(s, c)) {
375         return NGX_MAIL_PARSE_INVALID_COMMAND;
376     }
377 #endif
378 
379     iscf = ngx_mail_get_module_srv_conf(s, ngx_mail_imap_module);
380 
381     rc = ngx_mail_auth_parse(s, c);
382 
383     switch (rc) {
384 
385     case NGX_MAIL_AUTH_LOGIN:
386 
387         ngx_str_set(&s->out, imap_username);
388         s->mail_state = ngx_imap_auth_login_username;
389 
390         return NGX_OK;
391 
392     case NGX_MAIL_AUTH_LOGIN_USERNAME:
393 
394         ngx_str_set(&s->out, imap_password);
395         s->mail_state = ngx_imap_auth_login_password;
396 
397         return ngx_mail_auth_login_username(s, c, 1);
398 
399     case NGX_MAIL_AUTH_PLAIN:
400 
401         ngx_str_set(&s->out, imap_plain_next);
402         s->mail_state = ngx_imap_auth_plain;
403 
404         return NGX_OK;
405 
406     case NGX_MAIL_AUTH_CRAM_MD5:
407 
408         if (!(iscf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED)) {
409             return NGX_MAIL_PARSE_INVALID_COMMAND;
410         }
411 
412         if (s->salt.data == NULL) {
413             cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
414 
415             if (ngx_mail_salt(s, c, cscf) != NGX_OK) {
416                 return NGX_ERROR;
417             }
418         }
419 
420         if (ngx_mail_auth_cram_md5_salt(s, c, "+ ", 2) == NGX_OK) {
421             s->mail_state = ngx_imap_auth_cram_md5;
422             return NGX_OK;
423         }
424 
425         return NGX_ERROR;
426 
427     case NGX_MAIL_AUTH_EXTERNAL:
428 
429         if (!(iscf->auth_methods & NGX_MAIL_AUTH_EXTERNAL_ENABLED)) {
430             return NGX_MAIL_PARSE_INVALID_COMMAND;
431         }
432 
433         ngx_str_set(&s->out, imap_username);
434         s->mail_state = ngx_imap_auth_external;
435 
436         return NGX_OK;
437     }
438 
439     return rc;
440 }
441 
442 
443 static ngx_int_t
ngx_mail_imap_capability(ngx_mail_session_t * s,ngx_connection_t * c)444 ngx_mail_imap_capability(ngx_mail_session_t *s, ngx_connection_t *c)
445 {
446     ngx_mail_imap_srv_conf_t  *iscf;
447 
448     iscf = ngx_mail_get_module_srv_conf(s, ngx_mail_imap_module);
449 
450 #if (NGX_MAIL_SSL)
451 
452     if (c->ssl == NULL) {
453         ngx_mail_ssl_conf_t  *sslcf;
454 
455         sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
456 
457         if (sslcf->starttls == NGX_MAIL_STARTTLS_ON) {
458             s->text = iscf->starttls_capability;
459             return NGX_OK;
460         }
461 
462         if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) {
463             s->text = iscf->starttls_only_capability;
464             return NGX_OK;
465         }
466     }
467 #endif
468 
469     s->text = iscf->capability;
470 
471     return NGX_OK;
472 }
473 
474 
475 static ngx_int_t
ngx_mail_imap_starttls(ngx_mail_session_t * s,ngx_connection_t * c)476 ngx_mail_imap_starttls(ngx_mail_session_t *s, ngx_connection_t *c)
477 {
478 #if (NGX_MAIL_SSL)
479     ngx_mail_ssl_conf_t  *sslcf;
480 
481     if (c->ssl == NULL) {
482         sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
483         if (sslcf->starttls) {
484             c->read->handler = ngx_mail_starttls_handler;
485             return NGX_OK;
486         }
487     }
488 
489 #endif
490 
491     return NGX_MAIL_PARSE_INVALID_COMMAND;
492 }
493