comparison src/mail/ngx_mail_smtp_handler.c @ 1487:f69493e8faab

ngx_mail_pop3_module, ngx_mail_imap_module, and ngx_mail_smtp_module
author Igor Sysoev <igor@sysoev.ru>
date Sat, 15 Sep 2007 16:51:16 +0000
parents 0e7074ef7303
children 3c4111e07b58
comparison
equal deleted inserted replaced
1486:0e7074ef7303 1487:f69493e8faab
6 6
7 #include <ngx_config.h> 7 #include <ngx_config.h>
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 #include <ngx_event.h> 9 #include <ngx_event.h>
10 #include <ngx_mail.h> 10 #include <ngx_mail.h>
11 #include <ngx_mail_smtp_module.h>
11 12
12 13
13 static void ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev); 14 static void ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev);
14 static ngx_int_t ngx_mail_smtp_create_buffer(ngx_mail_session_t *s, 15 static ngx_int_t ngx_mail_smtp_create_buffer(ngx_mail_session_t *s,
15 ngx_connection_t *c); 16 ngx_connection_t *c);
41 void 42 void
42 ngx_mail_smtp_init_session(ngx_mail_session_t *s, ngx_connection_t *c) 43 ngx_mail_smtp_init_session(ngx_mail_session_t *s, ngx_connection_t *c)
43 { 44 {
44 ngx_msec_t timeout; 45 ngx_msec_t timeout;
45 ngx_mail_core_srv_conf_t *cscf; 46 ngx_mail_core_srv_conf_t *cscf;
47 ngx_mail_smtp_srv_conf_t *sscf;
46 48
47 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); 49 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
48 50 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
49 timeout = cscf->smtp_greeting_delay ? cscf->smtp_greeting_delay: 51
50 cscf->timeout; 52 timeout = sscf->greeting_delay ? sscf->greeting_delay : cscf->timeout;
51 ngx_add_timer(c->read, timeout); 53 ngx_add_timer(c->read, timeout);
52 54
53 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { 55 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
54 ngx_mail_close_connection(c); 56 ngx_mail_close_connection(c);
55 } 57 }
56 58
57 if (cscf->smtp_greeting_delay) { 59 if (sscf->greeting_delay) {
58 c->read->handler = ngx_mail_smtp_invalid_pipelining; 60 c->read->handler = ngx_mail_smtp_invalid_pipelining;
59 return; 61 return;
60 } 62 }
61 63
62 c->read->handler = ngx_mail_smtp_init_protocol; 64 c->read->handler = ngx_mail_smtp_init_protocol;
63 65
64 s->out = cscf->smtp_greeting; 66 s->out = sscf->greeting;
65 67
66 ngx_mail_send(c->write); 68 ngx_mail_send(c->write);
67 } 69 }
68 70
69 71
71 ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev) 73 ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev)
72 { 74 {
73 ngx_connection_t *c; 75 ngx_connection_t *c;
74 ngx_mail_session_t *s; 76 ngx_mail_session_t *s;
75 ngx_mail_core_srv_conf_t *cscf; 77 ngx_mail_core_srv_conf_t *cscf;
78 ngx_mail_smtp_srv_conf_t *sscf;
76 79
77 c = rev->data; 80 c = rev->data;
78 s = c->data; 81 s = c->data;
79 82
80 c->log->action = "in delay pipelining state"; 83 c->log->action = "in delay pipelining state";
94 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) { 97 if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
95 ngx_mail_close_connection(c); 98 ngx_mail_close_connection(c);
96 return; 99 return;
97 } 100 }
98 101
99 s->out = cscf->smtp_greeting; 102 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
103
104 s->out = sscf->greeting;
100 105
101 } else { 106 } else {
102 107
103 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "invalid pipelining"); 108 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, c->log, 0, "invalid pipelining");
104 109
156 161
157 162
158 static ngx_int_t 163 static ngx_int_t
159 ngx_mail_smtp_create_buffer(ngx_mail_session_t *s, ngx_connection_t *c) 164 ngx_mail_smtp_create_buffer(ngx_mail_session_t *s, ngx_connection_t *c)
160 { 165 {
161 ngx_mail_core_srv_conf_t *cscf; 166 ngx_mail_smtp_srv_conf_t *sscf;
162 167
163 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t)) == NGX_ERROR) { 168 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t)) == NGX_ERROR) {
164 ngx_mail_session_internal_server_error(s); 169 ngx_mail_session_internal_server_error(s);
165 return NGX_ERROR; 170 return NGX_ERROR;
166 } 171 }
167 172
168 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); 173 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
169 174
170 s->buffer = ngx_create_temp_buf(c->pool, cscf->smtp_client_buffer_size); 175 s->buffer = ngx_create_temp_buf(c->pool, sscf->client_buffer_size);
171 if (s->buffer == NULL) { 176 if (s->buffer == NULL) {
172 ngx_mail_session_internal_server_error(s); 177 ngx_mail_session_internal_server_error(s);
173 return NGX_ERROR; 178 return NGX_ERROR;
174 } 179 }
175 180
311 316
312 static ngx_int_t 317 static ngx_int_t
313 ngx_mail_smtp_helo(ngx_mail_session_t *s, ngx_connection_t *c) 318 ngx_mail_smtp_helo(ngx_mail_session_t *s, ngx_connection_t *c)
314 { 319 {
315 ngx_str_t *arg; 320 ngx_str_t *arg;
316 ngx_mail_core_srv_conf_t *cscf; 321 ngx_mail_smtp_srv_conf_t *sscf;
317 #if (NGX_MAIL_SSL) 322 #if (NGX_MAIL_SSL)
318 ngx_mail_ssl_conf_t *sslcf; 323 ngx_mail_ssl_conf_t *sslcf;
319 #endif 324 #endif
320
321 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
322 325
323 if (s->args.nelts != 1) { 326 if (s->args.nelts != 1) {
324 s->out.len = sizeof(smtp_invalid_argument) - 1; 327 s->out.len = sizeof(smtp_invalid_argument) - 1;
325 s->out.data = smtp_invalid_argument; 328 s->out.data = smtp_invalid_argument;
326 s->state = 0; 329 s->state = 0;
336 return NGX_ERROR; 339 return NGX_ERROR;
337 } 340 }
338 341
339 ngx_memcpy(s->smtp_helo.data, arg[0].data, arg[0].len); 342 ngx_memcpy(s->smtp_helo.data, arg[0].data, arg[0].len);
340 343
344 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
345
341 if (s->command == NGX_SMTP_HELO) { 346 if (s->command == NGX_SMTP_HELO) {
342 s->out = cscf->smtp_server_name; 347 s->out = sscf->server_name;
343 348
344 } else { 349 } else {
345 s->esmtp = 1; 350 s->esmtp = 1;
346 351
347 #if (NGX_MAIL_SSL) 352 #if (NGX_MAIL_SSL)
348 353
349 if (c->ssl == NULL) { 354 if (c->ssl == NULL) {
350 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module); 355 sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
351 356
352 if (sslcf->starttls == NGX_MAIL_STARTTLS_ON) { 357 if (sslcf->starttls == NGX_MAIL_STARTTLS_ON) {
353 s->out = cscf->smtp_starttls_capability; 358 s->out = sscf->starttls_capability;
354 return NGX_OK; 359 return NGX_OK;
355 } 360 }
356 361
357 if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) { 362 if (sslcf->starttls == NGX_MAIL_STARTTLS_ONLY) {
358 s->out = cscf->smtp_starttls_only_capability; 363 s->out = sscf->starttls_only_capability;
359 return NGX_OK; 364 return NGX_OK;
360 } 365 }
361 } 366 }
362 #endif 367 #endif
363 368
364 s->out = cscf->smtp_capability; 369 s->out = sscf->capability;
365 } 370 }
366 371
367 return NGX_OK; 372 return NGX_OK;
368 } 373 }
369 374
371 static ngx_int_t 376 static ngx_int_t
372 ngx_mail_smtp_auth(ngx_mail_session_t *s, ngx_connection_t *c) 377 ngx_mail_smtp_auth(ngx_mail_session_t *s, ngx_connection_t *c)
373 { 378 {
374 ngx_int_t rc; 379 ngx_int_t rc;
375 ngx_mail_core_srv_conf_t *cscf; 380 ngx_mail_core_srv_conf_t *cscf;
381 ngx_mail_smtp_srv_conf_t *sscf;
376 382
377 #if (NGX_MAIL_SSL) 383 #if (NGX_MAIL_SSL)
378 if (ngx_mail_starttls_only(s, c)) { 384 if (ngx_mail_starttls_only(s, c)) {
379 return NGX_MAIL_PARSE_INVALID_COMMAND; 385 return NGX_MAIL_PARSE_INVALID_COMMAND;
380 } 386 }
407 413
408 return NGX_OK; 414 return NGX_OK;
409 415
410 case NGX_MAIL_AUTH_CRAM_MD5: 416 case NGX_MAIL_AUTH_CRAM_MD5:
411 417
412 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); 418 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
413 419
414 if (!(cscf->smtp_auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED)) { 420 if (!(sscf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED)) {
415 return NGX_MAIL_PARSE_INVALID_COMMAND; 421 return NGX_MAIL_PARSE_INVALID_COMMAND;
416 } 422 }
417 423
418 if (s->salt.data == NULL) { 424 if (s->salt.data == NULL) {
425 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
426
419 if (ngx_mail_salt(s, c, cscf) != NGX_OK) { 427 if (ngx_mail_salt(s, c, cscf) != NGX_OK) {
420 return NGX_ERROR; 428 return NGX_ERROR;
421 } 429 }
422 } 430 }
423 431