Mercurial > hg > nginx
comparison src/mail/ngx_mail_imap_handler.c @ 1476:67578e966dcc
split pop3, imap, and smtp handlers
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 13 Sep 2007 20:13:18 +0000 |
parents | src/mail/ngx_mail_handler.c@32450a2bbdf4 |
children | 59e1caf2be94 |
comparison
equal
deleted
inserted
replaced
1475:732fe8258857 | 1476:67578e966dcc |
---|---|
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 | |
12 | |
13 static ngx_int_t ngx_mail_imap_login(ngx_mail_session_t *s, | |
14 ngx_connection_t *c); | |
15 static ngx_int_t ngx_mail_imap_authenticate(ngx_mail_session_t *s, | |
16 ngx_connection_t *c); | |
17 static ngx_int_t ngx_mail_imap_capability(ngx_mail_session_t *s, | |
18 ngx_connection_t *c); | |
19 static ngx_int_t ngx_mail_imap_starttls(ngx_mail_session_t *s, | |
20 ngx_connection_t *c); | |
21 | |
22 | |
23 static u_char imap_greeting[] = "* OK IMAP4 ready" CRLF; | |
24 static u_char imap_star[] = "* "; | |
25 static u_char imap_ok[] = "OK completed" CRLF; | |
26 static u_char imap_next[] = "+ OK" CRLF; | |
27 static u_char imap_plain_next[] = "+ " CRLF; | |
28 static u_char imap_username[] = "+ VXNlcm5hbWU6" CRLF; | |
29 static u_char imap_password[] = "+ UGFzc3dvcmQ6" CRLF; | |
30 static u_char imap_bye[] = "* BYE" CRLF; | |
31 static u_char imap_invalid_command[] = "BAD invalid command" CRLF; | |
32 | |
33 | |
34 void | |
35 ngx_mail_imap_init_session(ngx_mail_session_t *s, ngx_connection_t *c) | |
36 { | |
37 ngx_mail_core_srv_conf_t *cscf; | |
38 | |
39 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); | |
40 | |
41 if (cscf->imap_auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED) { | |
42 if (ngx_mail_salt(s, c, cscf) != NGX_OK) { | |
43 ngx_mail_session_internal_server_error(s); | |
44 return; | |
45 } | |
46 } | |
47 | |
48 c->read->handler = ngx_mail_imap_init_protocol; | |
49 | |
50 s->out.len = sizeof(imap_greeting) - 1; | |
51 s->out.data = imap_greeting; | |
52 | |
53 ngx_mail_send(c->write); | |
54 } | |
55 | |
56 | |
57 void | |
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_core_srv_conf_t *cscf; | |
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 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); | |
86 | |
87 s->buffer = ngx_create_temp_buf(c->pool, cscf->imap_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_imap_auth_state; | |
96 | |
97 ngx_imap_auth_state(rev); | |
98 } | |
99 | |
100 | |
101 void | |
102 ngx_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); | |
132 | |
133 if (rc == NGX_AGAIN || rc == NGX_ERROR) { | |
134 return; | |
135 } | |
136 | |
137 tag = 1; | |
138 s->text.len = 0; | |
139 s->out.len = sizeof(imap_ok) - 1; | |
140 s->out.data = imap_ok; | |
141 | |
142 if (rc == NGX_OK) { | |
143 | |
144 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0, "imap auth command: %i", | |
145 s->command); | |
146 | |
147 if (s->backslash) { | |
148 | |
149 arg = s->args.elts; | |
150 | |
151 for (i = 0; i < s->args.nelts; i++) { | |
152 dst = arg[i].data; | |
153 end = dst + arg[i].len; | |
154 | |
155 for (src = dst; src < end; dst++) { | |
156 *dst = *src; | |
157 if (*src++ == '\\') { | |
158 *dst = *src++; | |
159 } | |
160 } | |
161 | |
162 arg[i].len = dst - arg[i].data; | |
163 } | |
164 | |
165 s->backslash = 0; | |
166 } | |
167 | |
168 switch (s->mail_state) { | |
169 | |
170 case ngx_imap_start: | |
171 | |
172 switch (s->command) { | |
173 | |
174 case NGX_IMAP_LOGIN: | |
175 rc = ngx_mail_imap_login(s, c); | |
176 break; | |
177 | |
178 case NGX_IMAP_AUTHENTICATE: | |
179 rc = ngx_mail_imap_authenticate(s, c); | |
180 | |
181 if (rc == NGX_OK) { | |
182 tag = 0; | |
183 } | |
184 | |
185 break; | |
186 | |
187 case NGX_IMAP_CAPABILITY: | |
188 rc = ngx_mail_imap_capability(s, c); | |
189 break; | |
190 | |
191 case NGX_IMAP_LOGOUT: | |
192 s->quit = 1; | |
193 s->text.len = sizeof(imap_bye) - 1; | |
194 s->text.data = imap_bye; | |
195 break; | |
196 | |
197 case NGX_IMAP_NOOP: | |
198 break; | |
199 | |
200 case NGX_IMAP_STARTTLS: | |
201 rc = ngx_mail_imap_starttls(s, c); | |
202 break; | |
203 | |
204 default: | |
205 rc = NGX_MAIL_PARSE_INVALID_COMMAND; | |
206 break; | |
207 } | |
208 | |
209 break; | |
210 | |
211 case ngx_imap_auth_login_username: | |
212 rc = ngx_mail_auth_login_username(s, c); | |
213 | |
214 tag = 0; | |
215 s->out.len = sizeof(imap_password) - 1; | |
216 s->out.data = imap_password; | |
217 s->mail_state = ngx_imap_auth_login_password; | |
218 | |
219 break; | |
220 | |
221 case ngx_imap_auth_login_password: | |
222 rc = ngx_mail_auth_login_password(s, c); | |
223 break; | |
224 | |
225 case ngx_imap_auth_plain: | |
226 rc = ngx_mail_auth_plain(s, c, 0); | |
227 break; | |
228 | |
229 case ngx_imap_auth_cram_md5: | |
230 rc = ngx_mail_auth_cram_md5(s, c); | |
231 break; | |
232 } | |
233 | |
234 } else if (rc == NGX_IMAP_NEXT) { | |
235 tag = 0; | |
236 s->out.len = sizeof(imap_next) - 1; | |
237 s->out.data = imap_next; | |
238 } | |
239 | |
240 switch (rc) { | |
241 | |
242 case NGX_DONE: | |
243 ngx_mail_auth(s); | |
244 return; | |
245 | |
246 case NGX_ERROR: | |
247 ngx_mail_session_internal_server_error(s); | |
248 return; | |
249 | |
250 case NGX_MAIL_PARSE_INVALID_COMMAND: | |
251 s->state = 0; | |
252 s->out.len = sizeof(imap_invalid_command) - 1; | |
253 s->out.data = imap_invalid_command; | |
254 s->mail_state = ngx_imap_start; | |
255 break; | |
256 } | |
257 | |
258 if (tag) { | |
259 if (s->tag.len == 0) { | |
260 s->tag.len = sizeof(imap_star) - 1; | |
261 s->tag.data = (u_char *) imap_star; | |
262 } | |
263 | |
264 if (s->tagged_line.len < s->tag.len + s->text.len + s->out.len) { | |
265 s->tagged_line.len = s->tag.len + s->text.len + s->out.len; | |
266 s->tagged_line.data = ngx_palloc(c->pool, s->tagged_line.len); | |
267 if (s->tagged_line.data == NULL) { | |
268 ngx_mail_close_connection(c); | |
269 return; | |
270 } | |
271 } | |
272 | |
273 p = s->tagged_line.data; | |
274 | |
275 if (s->text.len) { | |
276 p = ngx_cpymem(p, s->text.data, s->text.len); | |
277 } | |
278 | |
279 p = ngx_cpymem(p, s->tag.data, s->tag.len); | |
280 ngx_memcpy(p, s->out.data, s->out.len); | |
281 | |
282 s->out.len = s->text.len + s->tag.len + s->out.len; | |
283 s->out.data = s->tagged_line.data; | |
284 } | |
285 | |
286 if (rc != NGX_IMAP_NEXT) { | |
287 s->args.nelts = 0; | |
288 | |
289 if (s->state) { | |
290 /* preserve tag */ | |
291 s->arg_start = s->buffer->start + s->tag.len; | |
292 s->buffer->pos = s->arg_start; | |
293 s->buffer->last = s->arg_start; | |
294 | |
295 } else { | |
296 s->buffer->pos = s->buffer->start; | |
297 s->buffer->last = s->buffer->start; | |
298 s->tag.len = 0; | |
299 } | |
300 } | |
301 | |
302 ngx_mail_send(c->write); | |
303 } | |
304 | |
305 | |
306 static ngx_int_t | |
307 ngx_mail_imap_login(ngx_mail_session_t *s, ngx_connection_t *c) | |
308 { | |
309 ngx_str_t *arg; | |
310 #if (NGX_MAIL_SSL) | |
311 ngx_mail_ssl_conf_t *sslcf; | |
312 | |
313 if (c->ssl == NULL) { | |
314 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module); | |
315 | |
316 if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) { | |
317 return NGX_MAIL_PARSE_INVALID_COMMAND; | |
318 } | |
319 } | |
320 #endif | |
321 | |
322 arg = s->args.elts; | |
323 | |
324 if (s->args.nelts != 2 || arg[0].len == 0) { | |
325 return NGX_MAIL_PARSE_INVALID_COMMAND; | |
326 } | |
327 | |
328 s->login.len = arg[0].len; | |
329 s->login.data = ngx_palloc(c->pool, s->login.len); | |
330 if (s->login.data == NULL) { | |
331 return NGX_ERROR; | |
332 } | |
333 | |
334 ngx_memcpy(s->login.data, arg[0].data, s->login.len); | |
335 | |
336 s->passwd.len = arg[1].len; | |
337 s->passwd.data = ngx_palloc(c->pool, s->passwd.len); | |
338 if (s->passwd.data == NULL) { | |
339 return NGX_ERROR; | |
340 } | |
341 | |
342 ngx_memcpy(s->passwd.data, arg[1].data, s->passwd.len); | |
343 | |
344 #if (NGX_DEBUG_MAIL_PASSWD) | |
345 ngx_log_debug2(NGX_LOG_DEBUG_MAIL, c->log, 0, | |
346 "imap login:\"%V\" passwd:\"%V\"", | |
347 &s->login, &s->passwd); | |
348 #else | |
349 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0, | |
350 "imap login:\"%V\"", &s->login); | |
351 #endif | |
352 | |
353 return NGX_DONE; | |
354 } | |
355 | |
356 | |
357 static ngx_int_t | |
358 ngx_mail_imap_authenticate(ngx_mail_session_t *s, ngx_connection_t *c) | |
359 { | |
360 u_char *p; | |
361 ngx_str_t *arg, salt; | |
362 ngx_uint_t n; | |
363 ngx_mail_core_srv_conf_t *cscf; | |
364 #if (NGX_MAIL_SSL) | |
365 ngx_mail_ssl_conf_t *sslcf; | |
366 | |
367 if (c->ssl == NULL) { | |
368 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module); | |
369 | |
370 if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) { | |
371 return NGX_MAIL_PARSE_INVALID_COMMAND; | |
372 } | |
373 } | |
374 #endif | |
375 | |
376 if (s->args.nelts != 1) { | |
377 return NGX_MAIL_PARSE_INVALID_COMMAND; | |
378 } | |
379 | |
380 arg = s->args.elts; | |
381 | |
382 if (arg[0].len == 5) { | |
383 | |
384 if (ngx_strncasecmp(arg[0].data, (u_char *) "LOGIN", 5) == 0) { | |
385 | |
386 s->out.len = sizeof(imap_username) - 1; | |
387 s->out.data = imap_username; | |
388 s->mail_state = ngx_imap_auth_login_username; | |
389 | |
390 return NGX_OK; | |
391 | |
392 } else if (ngx_strncasecmp(arg[0].data, (u_char *) "PLAIN", 5) == 0) { | |
393 | |
394 s->out.len = sizeof(imap_plain_next) - 1; | |
395 s->out.data = imap_plain_next; | |
396 s->mail_state = ngx_imap_auth_plain; | |
397 | |
398 return NGX_OK; | |
399 } | |
400 | |
401 } else if (arg[0].len == 8 | |
402 && ngx_strncasecmp(arg[0].data, (u_char *) "CRAM-MD5", 8) == 0) | |
403 { | |
404 if (s->args.nelts != 1) { | |
405 return NGX_MAIL_PARSE_INVALID_COMMAND; | |
406 } | |
407 | |
408 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); | |
409 | |
410 if (!(cscf->imap_auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED)) { | |
411 return NGX_MAIL_PARSE_INVALID_COMMAND; | |
412 } | |
413 | |
414 p = ngx_palloc(c->pool, | |
415 sizeof("+ " CRLF) - 1 | |
416 + ngx_base64_encoded_length(s->salt.len)); | |
417 if (p == NULL) { | |
418 return NGX_ERROR; | |
419 } | |
420 | |
421 p[0] = '+'; p[1]= ' '; | |
422 salt.data = &p[2]; | |
423 s->salt.len -= 2; | |
424 | |
425 ngx_encode_base64(&salt, &s->salt); | |
426 | |
427 s->salt.len += 2; | |
428 n = 2 + salt.len; | |
429 p[n++] = CR; p[n++] = LF; | |
430 | |
431 s->out.len = n; | |
432 s->out.data = p; | |
433 s->mail_state = ngx_imap_auth_cram_md5; | |
434 | |
435 return NGX_OK; | |
436 } | |
437 | |
438 return NGX_MAIL_PARSE_INVALID_COMMAND; | |
439 } | |
440 | |
441 | |
442 static ngx_int_t | |
443 ngx_mail_imap_capability(ngx_mail_session_t *s, ngx_connection_t *c) | |
444 { | |
445 ngx_mail_core_srv_conf_t *cscf; | |
446 #if (NGX_MAIL_SSL) | |
447 ngx_mail_ssl_conf_t *sslcf; | |
448 #endif | |
449 | |
450 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); | |
451 | |
452 #if (NGX_MAIL_SSL) | |
453 | |
454 if (c->ssl == NULL) { | |
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 = cscf->imap_starttls_capability; | |
459 return NGX_OK; | |
460 } | |
461 | |
462 if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) { | |
463 s->text = cscf->imap_starttls_only_capability; | |
464 return NGX_OK; | |
465 } | |
466 } | |
467 #endif | |
468 | |
469 s->text = cscf->imap_capability; | |
470 | |
471 return NGX_OK; | |
472 } | |
473 | |
474 | |
475 static ngx_int_t | |
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 } |