comparison src/mail/ngx_mail_smtp_handler.c @ 1892:057d362ee50e

resolver in smtp proxy module
author Igor Sysoev <igor@sysoev.ru>
date Wed, 13 Feb 2008 13:50:04 +0000
parents 7806f453183e
children 4c060e30476b
comparison
equal deleted inserted replaced
1891:782af1038115 1892:057d362ee50e
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 #include <ngx_mail_smtp_module.h>
12 12
13 13
14 static void ngx_mail_smtp_resolve_addr_handler(ngx_resolver_ctx_t *ctx);
15 static void ngx_mail_smtp_resolve_name_handler(ngx_resolver_ctx_t *ctx);
16 static void ngx_mail_smtp_greeting(ngx_mail_session_t *s, ngx_connection_t *c);
14 static void ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev); 17 static void ngx_mail_smtp_invalid_pipelining(ngx_event_t *rev);
15 static ngx_int_t ngx_mail_smtp_create_buffer(ngx_mail_session_t *s, 18 static ngx_int_t ngx_mail_smtp_create_buffer(ngx_mail_session_t *s,
16 ngx_connection_t *c); 19 ngx_connection_t *c);
17 20
18 static ngx_int_t ngx_mail_smtp_helo(ngx_mail_session_t *s, ngx_connection_t *c); 21 static ngx_int_t ngx_mail_smtp_helo(ngx_mail_session_t *s, ngx_connection_t *c);
38 "503 5.5.0 Improper use of SMTP command pipelining" CRLF; 41 "503 5.5.0 Improper use of SMTP command pipelining" CRLF;
39 static u_char smtp_invalid_argument[] = "501 5.5.4 Invalid argument" CRLF; 42 static u_char smtp_invalid_argument[] = "501 5.5.4 Invalid argument" CRLF;
40 static u_char smtp_auth_required[] = "530 5.7.1 Authentication required" CRLF; 43 static u_char smtp_auth_required[] = "530 5.7.1 Authentication required" CRLF;
41 44
42 45
46 static ngx_str_t smtp_unavailable = ngx_string("[UNAVAILABLE]");
47 static ngx_str_t smtp_tempunavail = ngx_string("[TEMPUNAVAIL]");
48
49
43 void 50 void
44 ngx_mail_smtp_init_session(ngx_mail_session_t *s, ngx_connection_t *c) 51 ngx_mail_smtp_init_session(ngx_mail_session_t *s, ngx_connection_t *c)
52 {
53 struct sockaddr_in *sin;
54 ngx_resolver_ctx_t *ctx;
55 ngx_mail_core_srv_conf_t *cscf;
56
57 c->log->action = "in resolving client address";
58
59 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
60
61 ctx = ngx_resolve_start(cscf->resolver, NULL);
62 if (ctx == NULL) {
63 ngx_mail_close_connection(c);
64 return;
65 }
66
67 /* AF_INET only */
68
69 sin = (struct sockaddr_in *) c->sockaddr;
70
71 ctx->addr = sin->sin_addr.s_addr;
72 ctx->handler = ngx_mail_smtp_resolve_addr_handler;
73 ctx->data = s;
74 ctx->timeout = cscf->resolver_timeout;
75
76 if (ngx_resolve_addr(ctx) != NGX_OK) {
77 ngx_mail_close_connection(c);
78 }
79 }
80
81
82 static void
83 ngx_mail_smtp_resolve_addr_handler(ngx_resolver_ctx_t *ctx)
84 {
85 ngx_connection_t *c;
86 ngx_mail_session_t *s;
87 ngx_mail_core_srv_conf_t *cscf;
88
89 s = ctx->data;
90 c = s->connection;
91
92 if (ctx->state) {
93 ngx_log_error(NGX_LOG_ERR, c->log, 0,
94 "%V could not be resolved (%i: %s)",
95 &c->addr_text, ctx->state,
96 ngx_resolver_strerror(ctx->state));
97
98 if (ctx->state == NGX_RESOLVE_NXDOMAIN) {
99 s->host = smtp_unavailable;
100
101 } else {
102 s->host = smtp_tempunavail;
103 }
104
105 ngx_resolve_addr_done(ctx);
106
107 ngx_mail_smtp_greeting(s, s->connection);
108
109 return;
110 }
111
112 c->log->action = "in resolving client hostname";
113
114 s->host.data = ngx_pstrdup(c->pool, &ctx->name);
115 if (s->host.data == NULL) {
116 ngx_resolve_addr_done(ctx);
117 ngx_mail_close_connection(c);
118 return;
119 }
120
121 s->host.len = ctx->name.len;
122
123 ngx_resolve_addr_done(ctx);
124
125 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
126 "address resolved: %V", &s->host);
127
128 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
129
130 ctx = ngx_resolve_start(cscf->resolver, NULL);
131 if (ctx == NULL) {
132 ngx_mail_close_connection(c);
133 return;
134 }
135
136 ctx->name = s->host;
137 ctx->type = NGX_RESOLVE_A;
138 ctx->handler = ngx_mail_smtp_resolve_name_handler;
139 ctx->data = s;
140 ctx->timeout = cscf->resolver_timeout;
141
142 if (ngx_resolve_name(ctx) != NGX_OK) {
143 ngx_mail_close_connection(c);
144 }
145 }
146
147
148 static void
149 ngx_mail_smtp_resolve_name_handler(ngx_resolver_ctx_t *ctx)
150 {
151 in_addr_t addr;
152 ngx_uint_t i;
153 ngx_connection_t *c;
154 struct sockaddr_in *sin;
155 ngx_mail_session_t *s;
156
157 s = ctx->data;
158 c = s->connection;
159
160 if (ctx->state) {
161 ngx_log_error(NGX_LOG_ERR, c->log, 0,
162 "%V could not be resolved (%i: %s)",
163 &ctx->name, ctx->state,
164 ngx_resolver_strerror(ctx->state));
165
166 if (ctx->state == NGX_RESOLVE_NXDOMAIN) {
167 s->host = smtp_unavailable;
168
169 } else {
170 s->host = smtp_tempunavail;
171 }
172
173 } else {
174
175 /* AF_INET only */
176
177 sin = (struct sockaddr_in *) c->sockaddr;
178
179 for (i = 0; i < ctx->naddrs; i++) {
180
181 addr = ctx->addrs[i];
182
183 ngx_log_debug4(NGX_LOG_DEBUG_MAIL, c->log, 0,
184 "name was resolved to %ud.%ud.%ud.%ud",
185 (ntohl(addr) >> 24) & 0xff,
186 (ntohl(addr) >> 16) & 0xff,
187 (ntohl(addr) >> 8) & 0xff,
188 ntohl(addr) & 0xff);
189
190 if (addr == sin->sin_addr.s_addr) {
191 goto found;
192 }
193 }
194
195 s->host = smtp_unavailable;
196 }
197
198 found:
199
200 ngx_resolve_name_done(ctx);
201
202 ngx_mail_smtp_greeting(s, c);
203 }
204
205
206 static void
207 ngx_mail_smtp_greeting(ngx_mail_session_t *s, ngx_connection_t *c)
45 { 208 {
46 ngx_msec_t timeout; 209 ngx_msec_t timeout;
47 ngx_mail_core_srv_conf_t *cscf; 210 ngx_mail_core_srv_conf_t *cscf;
48 ngx_mail_smtp_srv_conf_t *sscf; 211 ngx_mail_smtp_srv_conf_t *sscf;
212
213 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, c->log, 0,
214 "smtp greeting for \"%V\"", &s->host);
49 215
50 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); 216 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
51 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module); 217 sscf = ngx_mail_get_module_srv_conf(s, ngx_mail_smtp_module);
52 218
53 timeout = sscf->greeting_delay ? sscf->greeting_delay : cscf->timeout; 219 timeout = sscf->greeting_delay ? sscf->greeting_delay : cscf->timeout;