comparison src/mail/ngx_mail_core_module.c @ 322:d16d691432c9 NGINX_0_6_5

nginx 0.6.5 *) Feature: $nginx_version variable. Thanks to Nick S. Grechukh. *) Feature: the mail proxy supports AUTHENTICATE in IMAP mode. Thanks to Maxim Dounin. *) Feature: the mail proxy supports STARTTLS in SMTP mode. Thanks to Maxim Dounin. *) Bugfix: now nginx escapes space in $memcached_key variable. *) Bugfix: nginx was incorrectly built by Sun Studio on Solaris/amd64. Thanks to Jiang Hong. *) Bugfix: of minor potential bugs. Thanks to Coverity's Scan.
author Igor Sysoev <http://sysoev.ru>
date Mon, 23 Jul 2007 00:00:00 +0400
parents 2ceaee987f37
children 1c519aff5c0c
comparison
equal deleted inserted replaced
321:a87830ef6fdd 322:d16d691432c9
52 { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED }, 52 { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
53 { ngx_null_string, 0 } 53 { ngx_null_string, 0 }
54 }; 54 };
55 55
56 56
57 static ngx_conf_bitmask_t ngx_imap_auth_methods[] = {
58 { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
59 { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
60 { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
61 { ngx_null_string, 0 }
62 };
63
64
57 static ngx_conf_bitmask_t ngx_smtp_auth_methods[] = { 65 static ngx_conf_bitmask_t ngx_smtp_auth_methods[] = {
58 { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED }, 66 { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
59 { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED }, 67 { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED },
60 { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED }, 68 { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
61 { ngx_null_string, 0 } 69 { ngx_null_string, 0 }
70 };
71
72
73 static ngx_str_t ngx_imap_auth_methods_names[] = {
74 ngx_string("AUTH=PLAIN"),
75 ngx_string("AUTH=LOGIN"),
76 ngx_null_string, /* APOP */
77 ngx_string("AUTH=CRAM-MD5")
62 }; 78 };
63 79
64 80
65 static ngx_str_t ngx_smtp_auth_methods_names[] = { 81 static ngx_str_t ngx_smtp_auth_methods_names[] = {
66 ngx_string("PLAIN"), 82 ngx_string("PLAIN"),
169 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE, 185 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
170 ngx_conf_set_bitmask_slot, 186 ngx_conf_set_bitmask_slot,
171 NGX_MAIL_SRV_CONF_OFFSET, 187 NGX_MAIL_SRV_CONF_OFFSET,
172 offsetof(ngx_mail_core_srv_conf_t, pop3_auth_methods), 188 offsetof(ngx_mail_core_srv_conf_t, pop3_auth_methods),
173 &ngx_pop3_auth_methods }, 189 &ngx_pop3_auth_methods },
190
191 { ngx_string("imap_auth"),
192 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
193 ngx_conf_set_bitmask_slot,
194 NGX_MAIL_SRV_CONF_OFFSET,
195 offsetof(ngx_mail_core_srv_conf_t, imap_auth_methods),
196 &ngx_imap_auth_methods },
174 197
175 { ngx_string("smtp_auth"), 198 { ngx_string("smtp_auth"),
176 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE, 199 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
177 ngx_conf_set_bitmask_slot, 200 ngx_conf_set_bitmask_slot,
178 NGX_MAIL_SRV_CONF_OFFSET, 201 NGX_MAIL_SRV_CONF_OFFSET,
276 ngx_mail_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) 299 ngx_mail_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
277 { 300 {
278 ngx_mail_core_srv_conf_t *prev = parent; 301 ngx_mail_core_srv_conf_t *prev = parent;
279 ngx_mail_core_srv_conf_t *conf = child; 302 ngx_mail_core_srv_conf_t *conf = child;
280 303
281 u_char *p; 304 u_char *p, *auth;
282 size_t size, stls_only_size; 305 size_t size, stls_only_size;
283 ngx_str_t *c, *d; 306 ngx_str_t *c, *d;
284 ngx_uint_t i, m; 307 ngx_uint_t i, m;
285 308
286 ngx_conf_merge_size_value(conf->imap_client_buffer_size, 309 ngx_conf_merge_size_value(conf->imap_client_buffer_size,
295 ngx_conf_merge_bitmask_value(conf->pop3_auth_methods, 318 ngx_conf_merge_bitmask_value(conf->pop3_auth_methods,
296 prev->pop3_auth_methods, 319 prev->pop3_auth_methods,
297 (NGX_CONF_BITMASK_SET 320 (NGX_CONF_BITMASK_SET
298 |NGX_MAIL_AUTH_PLAIN_ENABLED)); 321 |NGX_MAIL_AUTH_PLAIN_ENABLED));
299 322
323 ngx_conf_merge_bitmask_value(conf->imap_auth_methods,
324 prev->imap_auth_methods,
325 (NGX_CONF_BITMASK_SET
326 |NGX_MAIL_AUTH_PLAIN_ENABLED));
327
300 ngx_conf_merge_bitmask_value(conf->smtp_auth_methods, 328 ngx_conf_merge_bitmask_value(conf->smtp_auth_methods,
301 prev->smtp_auth_methods, 329 prev->smtp_auth_methods,
302 (NGX_CONF_BITMASK_SET 330 (NGX_CONF_BITMASK_SET
303 |NGX_MAIL_AUTH_PLAIN_ENABLED 331 |NGX_MAIL_AUTH_PLAIN_ENABLED
304 |NGX_MAIL_AUTH_LOGIN_ENABLED)); 332 |NGX_MAIL_AUTH_LOGIN_ENABLED));
461 c = conf->imap_capabilities.elts; 489 c = conf->imap_capabilities.elts;
462 for (i = 0; i < conf->imap_capabilities.nelts; i++) { 490 for (i = 0; i < conf->imap_capabilities.nelts; i++) {
463 size += 1 + c[i].len; 491 size += 1 + c[i].len;
464 } 492 }
465 493
494 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
495 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
496 m <<= 1, i++)
497 {
498 if (m & conf->imap_auth_methods) {
499 size += 1 + ngx_imap_auth_methods_names[i].len;
500 }
501 }
502
466 p = ngx_palloc(cf->pool, size); 503 p = ngx_palloc(cf->pool, size);
467 if (p == NULL) { 504 if (p == NULL) {
468 return NGX_CONF_ERROR; 505 return NGX_CONF_ERROR;
469 } 506 }
470 507
474 p = ngx_cpymem(p, "* CAPABILITY", sizeof("* CAPABILITY") - 1); 511 p = ngx_cpymem(p, "* CAPABILITY", sizeof("* CAPABILITY") - 1);
475 512
476 for (i = 0; i < conf->imap_capabilities.nelts; i++) { 513 for (i = 0; i < conf->imap_capabilities.nelts; i++) {
477 *p++ = ' '; 514 *p++ = ' ';
478 p = ngx_cpymem(p, c[i].data, c[i].len); 515 p = ngx_cpymem(p, c[i].data, c[i].len);
516 }
517
518 auth = p;
519
520 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
521 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
522 m <<= 1, i++)
523 {
524 if (m & conf->imap_auth_methods) {
525 *p++ = ' ';
526 p = ngx_cpymem(p, ngx_imap_auth_methods_names[i].data,
527 ngx_imap_auth_methods_names[i].len);
528 }
479 } 529 }
480 530
481 *p++ = CR; *p = LF; 531 *p++ = CR; *p = LF;
482 532
483 533
495 conf->imap_capability.len - (sizeof(CRLF) - 1)); 545 conf->imap_capability.len - (sizeof(CRLF) - 1));
496 p = ngx_cpymem(p, " STARTTLS", sizeof(" STARTTLS") - 1); 546 p = ngx_cpymem(p, " STARTTLS", sizeof(" STARTTLS") - 1);
497 *p++ = CR; *p = LF; 547 *p++ = CR; *p = LF;
498 548
499 549
500 size += sizeof(" LOGINDISABLED") - 1; 550 size = (auth - conf->imap_capability.data) + sizeof(CRLF) - 1
551 + sizeof(" STARTTLS LOGINDISABLED") - 1;
501 552
502 p = ngx_palloc(cf->pool, size); 553 p = ngx_palloc(cf->pool, size);
503 if (p == NULL) { 554 if (p == NULL) {
504 return NGX_CONF_ERROR; 555 return NGX_CONF_ERROR;
505 } 556 }
506 557
507 conf->imap_starttls_only_capability.len = size; 558 conf->imap_starttls_only_capability.len = size;
508 conf->imap_starttls_only_capability.data = p; 559 conf->imap_starttls_only_capability.data = p;
509 560
510 p = ngx_cpymem(p, conf->imap_starttls_capability.data, 561 p = ngx_cpymem(p, conf->imap_capability.data,
511 conf->imap_starttls_capability.len - (sizeof(CRLF) - 1)); 562 auth - conf->imap_capability.data);
512 p = ngx_cpymem(p, " LOGINDISABLED", sizeof(" LOGINDISABLED") - 1); 563 p = ngx_cpymem(p, " STARTTLS LOGINDISABLED",
564 sizeof(" STARTTLS LOGINDISABLED") - 1);
513 *p++ = CR; *p = LF; 565 *p++ = CR; *p = LF;
514 566
515 567
516 size = sizeof("220 ESMTP ready" CRLF) - 1 + conf->server_name.len; 568 size = sizeof("220 ESMTP ready" CRLF) - 1 + conf->server_name.len;
517 569
579 for (i = 0; i < conf->smtp_capabilities.nelts; i++) { 631 for (i = 0; i < conf->smtp_capabilities.nelts; i++) {
580 *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-'; 632 *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-';
581 p = ngx_cpymem(p, c[i].data, c[i].len); 633 p = ngx_cpymem(p, c[i].data, c[i].len);
582 *p++ = CR; *p++ = LF; 634 *p++ = CR; *p++ = LF;
583 } 635 }
636
637 auth = p;
584 638
585 *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' '; 639 *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' ';
586 *p++ = 'A'; *p++ = 'U'; *p++ = 'T'; *p++ = 'H'; 640 *p++ = 'A'; *p++ = 'U'; *p++ = 'T'; *p++ = 'H';
587 641
588 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0; 642 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
595 ngx_smtp_auth_methods_names[i].len); 649 ngx_smtp_auth_methods_names[i].len);
596 } 650 }
597 } 651 }
598 652
599 *p++ = CR; *p = LF; 653 *p++ = CR; *p = LF;
654
655 size += sizeof("250 STARTTLS" CRLF) - 1;
656
657 p = ngx_palloc(cf->pool, size);
658 if (p == NULL) {
659 return NGX_CONF_ERROR;
660 }
661
662 conf->smtp_starttls_capability.len = size;
663 conf->smtp_starttls_capability.data = p;
664
665 p = ngx_cpymem(p, conf->smtp_capability.data,
666 conf->smtp_capability.len);
667
668 p = ngx_cpymem(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1);
669 *p++ = CR; *p = LF;
670
671 p = conf->smtp_starttls_capability.data
672 + (auth - conf->smtp_capability.data) + 3;
673 *p = '-';
674
675 size = (auth - conf->smtp_capability.data)
676 + sizeof("250 STARTTLS" CRLF) - 1;
677
678 p = ngx_palloc(cf->pool, size);
679 if (p == NULL) {
680 return NGX_CONF_ERROR;
681 }
682
683 conf->smtp_starttls_only_capability.len = size;
684 conf->smtp_starttls_only_capability.data = p;
685
686 p = ngx_cpymem(p, conf->smtp_capability.data,
687 auth - conf->smtp_capability.data);
688
689 ngx_memcpy(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1);
600 690
601 return NGX_CONF_OK; 691 return NGX_CONF_OK;
602 } 692 }
603 693
604 694