22
23
ngx_mail_init_connection(ngx_connection_t *c)
27
struct sockaddr_in sin;
28
ngx_mail_log_ctx_t *ctx;
29
ngx_mail_in_port_t *imip;
30
ngx_mail_in_addr_t *imia;
31
ngx_mail_session_t *s;
26
ngx_mail_port_t *port;
28
struct sockaddr_in *sin;
29
ngx_mail_log_ctx_t *ctx;
30
ngx_mail_in_addr_t *addr;
31
ngx_mail_session_t *s;
32
ngx_mail_addr_conf_t *addr_conf;
34
struct sockaddr_in6 *sin6;
35
ngx_mail_in6_addr_t *addr6;
33
39
/* find the server configuration for the address:port */
37
imip = c->listening->servers;
42
if (imip->naddrs > 1) {
41
port = c->listening->servers;
43
if (port->naddrs > 1) {
45
46
* There are several addresses on this port and one of them
49
50
* AcceptEx() already gave this address.
53
if (c->local_sockaddr) {
55
((struct sockaddr_in *) c->local_sockaddr)->sin_addr.s_addr;
60
len = sizeof(struct sockaddr_in);
61
if (getsockname(c->fd, (struct sockaddr *) &sin, &len) == -1) {
62
ngx_connection_error(c, ngx_socket_errno,
63
"getsockname() failed");
64
ngx_mail_close_connection(c);
68
in_addr = sin.sin_addr.s_addr;
71
/* the last address is "*" */
73
for ( /* void */ ; i < imip->naddrs - 1; i++) {
74
if (in_addr == imia[i].addr) {
53
if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
54
ngx_mail_close_connection(c);
58
sa = c->local_sockaddr;
60
switch (sa->sa_family) {
64
sin6 = (struct sockaddr_in6 *) sa;
68
/* the last address is "*" */
70
for (i = 0; i < port->naddrs - 1; i++) {
71
if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
76
addr_conf = &addr6[i].conf;
81
default: /* AF_INET */
82
sin = (struct sockaddr_in *) sa;
86
/* the last address is "*" */
88
for (i = 0; i < port->naddrs - 1; i++) {
89
if (addr[i].addr == sin->sin_addr.s_addr) {
94
addr_conf = &addr[i].conf;
100
switch (c->local_sockaddr->sa_family) {
105
addr_conf = &addr6[0].conf;
109
default: /* AF_INET */
111
addr_conf = &addr[0].conf;
81
116
s = ngx_pcalloc(c->pool, sizeof(ngx_mail_session_t));
83
118
ngx_mail_close_connection(c);
87
s->main_conf = imia[i].ctx->main_conf;
88
s->srv_conf = imia[i].ctx->srv_conf;
122
s->main_conf = addr_conf->ctx->main_conf;
123
s->srv_conf = addr_conf->ctx->srv_conf;
90
s->addr_text = &imia[i].addr_text;
125
s->addr_text = &addr_conf->addr_text;
93
128
s->connection = c;
118
153
sslcf = ngx_mail_get_module_srv_conf(s, ngx_mail_ssl_module);
120
155
if (sslcf->enable) {
121
ngx_mail_ssl_init_connection(&sslcf->ssl, c);
156
c->log->action = "SSL handshaking";
158
ngx_mail_ssl_init_connection(&sslcf->ssl, c);
162
if (addr_conf->ssl) {
164
c->log->action = "SSL handshaking";
166
if (sslcf->ssl.ctx == NULL) {
167
ngx_log_error(NGX_LOG_ERR, c->log, 0,
168
"no \"ssl_certificate\" is defined "
169
"in server listening on SSL port");
170
ngx_mail_close_connection(c);
174
ngx_mail_ssl_init_connection(&sslcf->ssl, c);
236
292
ngx_mail_salt(ngx_mail_session_t *s, ngx_connection_t *c,
237
293
ngx_mail_core_srv_conf_t *cscf)
239
s->salt.data = ngx_palloc(c->pool,
240
sizeof(" <18446744073709551616.@>" CRLF) - 1
242
+ cscf->server_name.len);
295
s->salt.data = ngx_pnalloc(c->pool,
296
sizeof(" <18446744073709551616.@>" CRLF) - 1
298
+ cscf->server_name.len);
243
299
if (s->salt.data == NULL) {
244
300
return NGX_ERROR;
288
344
"mail auth plain: \"%V\"", &arg[n]);
291
plain.data = ngx_palloc(c->pool, ngx_base64_decoded_length(arg[n].len));
292
if (plain.data == NULL){
347
plain.data = ngx_pnalloc(c->pool, ngx_base64_decoded_length(arg[n].len));
348
if (plain.data == NULL) {
293
349
return NGX_ERROR;
338
ngx_mail_auth_login_username(ngx_mail_session_t *s, ngx_connection_t *c)
394
ngx_mail_auth_login_username(ngx_mail_session_t *s, ngx_connection_t *c,
342
399
arg = s->args.elts;
344
401
ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
345
"mail auth login username: \"%V\"", &arg[0]);
402
"mail auth login username: \"%V\"", &arg[n]);
347
s->login.data = ngx_palloc(c->pool, ngx_base64_decoded_length(arg[0].len));
348
if (s->login.data == NULL){
404
s->login.data = ngx_pnalloc(c->pool, ngx_base64_decoded_length(arg[n].len));
405
if (s->login.data == NULL) {
349
406
return NGX_ERROR;
352
if (ngx_decode_base64(&s->login, &arg[0]) != NGX_OK) {
409
if (ngx_decode_base64(&s->login, &arg[n]) != NGX_OK) {
353
410
ngx_log_error(NGX_LOG_INFO, c->log, 0,
354
411
"client sent invalid base64 encoding in AUTH LOGIN command");
355
412
return NGX_MAIL_PARSE_INVALID_COMMAND;
374
431
"mail auth login password: \"%V\"", &arg[0]);
377
s->passwd.data = ngx_palloc(c->pool, ngx_base64_decoded_length(arg[0].len));
378
if (s->passwd.data == NULL){
434
s->passwd.data = ngx_pnalloc(c->pool,
435
ngx_base64_decoded_length(arg[0].len));
436
if (s->passwd.data == NULL) {
379
437
return NGX_ERROR;
434
492
ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
435
493
"mail auth cram-md5: \"%V\"", &arg[0]);
437
s->login.data = ngx_palloc(c->pool, ngx_base64_decoded_length(arg[0].len));
438
if (s->login.data == NULL){
495
s->login.data = ngx_pnalloc(c->pool, ngx_base64_decoded_length(arg[0].len));
496
if (s->login.data == NULL) {
439
497
return NGX_ERROR;
531
589
ngx_add_timer(c->write, cscf->timeout);
533
if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
591
if (ngx_handle_write_event(c->write, 0) != NGX_OK) {
534
592
ngx_mail_close_connection(c);