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