comparison src/mail/ngx_mail_pop3_handler.c @ 1479:2647950e047f

optimizations
author Igor Sysoev <igor@sysoev.ru>
date Thu, 13 Sep 2007 21:24:27 +0000
parents 59e1caf2be94
children a231e37a19ab
comparison
equal deleted inserted replaced
1478:d0cce8369848 1479:2647950e047f
273 273
274 static ngx_int_t 274 static ngx_int_t
275 ngx_mail_pop3_user(ngx_mail_session_t *s, ngx_connection_t *c) 275 ngx_mail_pop3_user(ngx_mail_session_t *s, ngx_connection_t *c)
276 { 276 {
277 ngx_str_t *arg; 277 ngx_str_t *arg;
278 #if (NGX_MAIL_SSL) 278
279 ngx_mail_ssl_conf_t *sslcf; 279 #if (NGX_MAIL_SSL)
280 280 if (ngx_mail_starttls_only(s, c)) {
281 if (c->ssl == NULL) { 281 return NGX_MAIL_PARSE_INVALID_COMMAND;
282 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module); 282 }
283
284 if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) {
285 return NGX_MAIL_PARSE_INVALID_COMMAND;
286 }
287 }
288
289 #endif 283 #endif
290 284
291 if (s->args.nelts != 1) { 285 if (s->args.nelts != 1) {
292 return NGX_MAIL_PARSE_INVALID_COMMAND; 286 return NGX_MAIL_PARSE_INVALID_COMMAND;
293 } 287 }
393 static ngx_int_t 387 static ngx_int_t
394 ngx_mail_pop3_apop(ngx_mail_session_t *s, ngx_connection_t *c) 388 ngx_mail_pop3_apop(ngx_mail_session_t *s, ngx_connection_t *c)
395 { 389 {
396 ngx_str_t *arg; 390 ngx_str_t *arg;
397 ngx_mail_core_srv_conf_t *cscf; 391 ngx_mail_core_srv_conf_t *cscf;
398 #if (NGX_MAIL_SSL) 392
399 ngx_mail_ssl_conf_t *sslcf; 393 #if (NGX_MAIL_SSL)
400 394 if (ngx_mail_starttls_only(s, c)) {
401 if (c->ssl == NULL) { 395 return NGX_MAIL_PARSE_INVALID_COMMAND;
402 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module); 396 }
403
404 if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) {
405 return NGX_MAIL_PARSE_INVALID_COMMAND;
406 }
407 }
408
409 #endif 397 #endif
410 398
411 if (s->args.nelts != 2) { 399 if (s->args.nelts != 2) {
412 return NGX_MAIL_PARSE_INVALID_COMMAND; 400 return NGX_MAIL_PARSE_INVALID_COMMAND;
413 } 401 }
446 434
447 435
448 static ngx_int_t 436 static ngx_int_t
449 ngx_mail_pop3_auth(ngx_mail_session_t *s, ngx_connection_t *c) 437 ngx_mail_pop3_auth(ngx_mail_session_t *s, ngx_connection_t *c)
450 { 438 {
451 size_t n; 439 ngx_int_t rc;
452 u_char *p;
453 ngx_str_t *arg, salt;
454 ngx_mail_core_srv_conf_t *cscf; 440 ngx_mail_core_srv_conf_t *cscf;
455 #if (NGX_MAIL_SSL) 441
456 ngx_mail_ssl_conf_t *sslcf; 442 #if (NGX_MAIL_SSL)
457 443 if (ngx_mail_starttls_only(s, c)) {
458 if (c->ssl == NULL) { 444 return NGX_MAIL_PARSE_INVALID_COMMAND;
459 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module); 445 }
460
461 if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) {
462 return NGX_MAIL_PARSE_INVALID_COMMAND;
463 }
464 }
465
466 #endif 446 #endif
467 447
468 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); 448 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
469 449
470 if (s->args.nelts == 0) { 450 if (s->args.nelts == 0) {
472 s->state = 0; 452 s->state = 0;
473 453
474 return NGX_OK; 454 return NGX_OK;
475 } 455 }
476 456
477 arg = s->args.elts; 457 rc = ngx_mail_auth_parse(s, c);
478 458
479 if (arg[0].len == 5) { 459 switch (rc) {
480 460
481 if (ngx_strncasecmp(arg[0].data, (u_char *) "LOGIN", 5) == 0) { 461 case NGX_MAIL_AUTH_LOGIN:
482 462
483 if (s->args.nelts != 1) { 463 s->out.len = sizeof(pop3_username) - 1;
484 return NGX_MAIL_PARSE_INVALID_COMMAND; 464 s->out.data = pop3_username;
485 } 465 s->mail_state = ngx_pop3_auth_login_username;
486 466
487 s->out.len = sizeof(pop3_username) - 1; 467 return NGX_OK;
488 s->out.data = pop3_username; 468
489 s->mail_state = ngx_pop3_auth_login_username; 469 case NGX_MAIL_AUTH_PLAIN:
490 470
491 return NGX_OK; 471 s->out.len = sizeof(pop3_next) - 1;
492 472 s->out.data = pop3_next;
493 } else if (ngx_strncasecmp(arg[0].data, (u_char *) "PLAIN", 5) == 0) { 473 s->mail_state = ngx_pop3_auth_plain;
494 474
495 if (s->args.nelts == 1) { 475 return NGX_OK;
496 476
497 s->out.len = sizeof(pop3_next) - 1; 477 case NGX_MAIL_AUTH_CRAM_MD5:
498 s->out.data = pop3_next;
499 s->mail_state = ngx_pop3_auth_plain;
500
501 return NGX_OK;
502 }
503
504 if (s->args.nelts == 2) {
505
506 /*
507 * workaround for Eudora for Mac: it sends
508 * AUTH PLAIN [base64 encoded]
509 */
510
511 return ngx_mail_auth_plain(s, c, 1);
512 }
513
514 return NGX_MAIL_PARSE_INVALID_COMMAND;
515 }
516
517 } else if (arg[0].len == 8
518 && ngx_strncasecmp(arg[0].data, (u_char *) "CRAM-MD5", 8) == 0)
519 {
520 if (s->args.nelts != 1) {
521 return NGX_MAIL_PARSE_INVALID_COMMAND;
522 }
523 478
524 if (!(cscf->pop3_auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED)) { 479 if (!(cscf->pop3_auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED)) {
525 return NGX_MAIL_PARSE_INVALID_COMMAND; 480 return NGX_MAIL_PARSE_INVALID_COMMAND;
526 } 481 }
527 482
528 p = ngx_palloc(c->pool, 483 if (ngx_mail_auth_cram_md5_salt(s, c, "+ ", 2) == NGX_OK) {
529 sizeof("+ " CRLF) - 1 484 s->mail_state = ngx_pop3_auth_cram_md5;
530 + ngx_base64_encoded_length(s->salt.len)); 485 return NGX_OK;
531 if (p == NULL) { 486 }
532 return NGX_ERROR; 487
533 } 488 return NGX_ERROR;
534 489 }
535 p[0] = '+'; p[1]= ' '; 490
536 salt.data = &p[2]; 491 return rc;
537 s->salt.len -= 2; 492 }
538
539 ngx_encode_base64(&salt, &s->salt);
540
541 s->salt.len += 2;
542 n = 2 + salt.len;
543 p[n++] = CR; p[n++] = LF;
544
545 s->out.len = n;
546 s->out.data = p;
547 s->mail_state = ngx_pop3_auth_cram_md5;
548
549 return NGX_OK;
550 }
551
552 return NGX_MAIL_PARSE_INVALID_COMMAND;
553 }