Mercurial > hg > nginx
comparison src/mail/ngx_mail_smtp_module.c @ 2309:8156bc03982a
smtp_auth none
patch by Maxim Dounin
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 13 Nov 2008 13:25:34 +0000 |
parents | 2a92804f4109 |
children | d620f497c50f |
comparison
equal
deleted
inserted
replaced
2308:3f98400e31e9 | 2309:8156bc03982a |
---|---|
18 | 18 |
19 static ngx_conf_bitmask_t ngx_mail_smtp_auth_methods[] = { | 19 static ngx_conf_bitmask_t ngx_mail_smtp_auth_methods[] = { |
20 { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED }, | 20 { ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED }, |
21 { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED }, | 21 { ngx_string("login"), NGX_MAIL_AUTH_LOGIN_ENABLED }, |
22 { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED }, | 22 { ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED }, |
23 { ngx_string("none"), NGX_MAIL_AUTH_NONE_ENABLED }, | |
23 { ngx_null_string, 0 } | 24 { ngx_null_string, 0 } |
24 }; | 25 }; |
25 | 26 |
26 | 27 |
27 static ngx_str_t ngx_mail_smtp_auth_methods_names[] = { | 28 static ngx_str_t ngx_mail_smtp_auth_methods_names[] = { |
28 ngx_string("PLAIN"), | 29 ngx_string("PLAIN"), |
29 ngx_string("LOGIN"), | 30 ngx_string("LOGIN"), |
30 ngx_null_string, /* APOP */ | 31 ngx_null_string, /* APOP */ |
31 ngx_string("CRAM-MD5") | 32 ngx_string("CRAM-MD5"), |
33 ngx_null_string /* NONE */ | |
32 }; | 34 }; |
33 | 35 |
34 | 36 |
35 static ngx_mail_protocol_t ngx_mail_smtp_protocol = { | 37 static ngx_mail_protocol_t ngx_mail_smtp_protocol = { |
36 ngx_string("smtp"), | 38 ngx_string("smtp"), |
134 ngx_mail_smtp_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) | 136 ngx_mail_smtp_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) |
135 { | 137 { |
136 ngx_mail_smtp_srv_conf_t *prev = parent; | 138 ngx_mail_smtp_srv_conf_t *prev = parent; |
137 ngx_mail_smtp_srv_conf_t *conf = child; | 139 ngx_mail_smtp_srv_conf_t *conf = child; |
138 | 140 |
139 u_char *p, *auth; | 141 u_char *p, *auth, *last; |
140 size_t size; | 142 size_t size; |
141 ngx_str_t *c; | 143 ngx_str_t *c; |
142 ngx_uint_t i, m; | 144 ngx_uint_t i, m, auth_enabled; |
143 ngx_mail_core_srv_conf_t *cscf; | 145 ngx_mail_core_srv_conf_t *cscf; |
144 | 146 |
145 ngx_conf_merge_size_value(conf->client_buffer_size, | 147 ngx_conf_merge_size_value(conf->client_buffer_size, |
146 prev->client_buffer_size, | 148 prev->client_buffer_size, |
147 (size_t) ngx_pagesize); | 149 (size_t) ngx_pagesize); |
190 | 192 |
191 if (conf->capabilities.nelts == 0) { | 193 if (conf->capabilities.nelts == 0) { |
192 conf->capabilities = prev->capabilities; | 194 conf->capabilities = prev->capabilities; |
193 } | 195 } |
194 | 196 |
195 size = sizeof("250-") - 1 + cscf->server_name.len + sizeof(CRLF) - 1 | 197 size = sizeof("250-") - 1 + cscf->server_name.len + sizeof(CRLF) - 1; |
196 + sizeof("250 AUTH") - 1 + sizeof(CRLF) - 1; | |
197 | 198 |
198 c = conf->capabilities.elts; | 199 c = conf->capabilities.elts; |
199 for (i = 0; i < conf->capabilities.nelts; i++) { | 200 for (i = 0; i < conf->capabilities.nelts; i++) { |
200 size += sizeof("250 ") - 1 + c[i].len + sizeof(CRLF) - 1; | 201 size += sizeof("250 ") - 1 + c[i].len + sizeof(CRLF) - 1; |
201 } | 202 } |
203 | |
204 auth_enabled = 0; | |
202 | 205 |
203 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0; | 206 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0; |
204 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED; | 207 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED; |
205 m <<= 1, i++) | 208 m <<= 1, i++) |
206 { | 209 { |
207 if (m & conf->auth_methods) { | 210 if (m & conf->auth_methods) { |
208 size += 1 + ngx_mail_smtp_auth_methods_names[i].len; | 211 size += 1 + ngx_mail_smtp_auth_methods_names[i].len; |
212 auth_enabled = 1; | |
209 } | 213 } |
214 } | |
215 | |
216 if (auth_enabled) { | |
217 size += sizeof("250 AUTH") - 1 + sizeof(CRLF) - 1; | |
210 } | 218 } |
211 | 219 |
212 p = ngx_pnalloc(cf->pool, size); | 220 p = ngx_pnalloc(cf->pool, size); |
213 if (p == NULL) { | 221 if (p == NULL) { |
214 return NGX_CONF_ERROR; | 222 return NGX_CONF_ERROR; |
215 } | 223 } |
216 | 224 |
217 conf->capability.len = size; | 225 conf->capability.len = size; |
218 conf->capability.data = p; | 226 conf->capability.data = p; |
227 | |
228 last = p; | |
219 | 229 |
220 *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-'; | 230 *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-'; |
221 p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len); | 231 p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len); |
222 *p++ = CR; *p++ = LF; | 232 *p++ = CR; *p++ = LF; |
223 | 233 |
224 for (i = 0; i < conf->capabilities.nelts; i++) { | 234 for (i = 0; i < conf->capabilities.nelts; i++) { |
235 last = p; | |
225 *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-'; | 236 *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = '-'; |
226 p = ngx_cpymem(p, c[i].data, c[i].len); | 237 p = ngx_cpymem(p, c[i].data, c[i].len); |
227 *p++ = CR; *p++ = LF; | 238 *p++ = CR; *p++ = LF; |
228 } | 239 } |
229 | 240 |
230 auth = p; | 241 auth = p; |
231 | 242 |
232 *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' '; | 243 if (auth_enabled) { |
233 *p++ = 'A'; *p++ = 'U'; *p++ = 'T'; *p++ = 'H'; | 244 last = p; |
234 | 245 |
235 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0; | 246 *p++ = '2'; *p++ = '5'; *p++ = '0'; *p++ = ' '; |
236 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED; | 247 *p++ = 'A'; *p++ = 'U'; *p++ = 'T'; *p++ = 'H'; |
237 m <<= 1, i++) | 248 |
238 { | 249 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0; |
239 if (m & conf->auth_methods) { | 250 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED; |
240 *p++ = ' '; | 251 m <<= 1, i++) |
241 p = ngx_cpymem(p, ngx_mail_smtp_auth_methods_names[i].data, | 252 { |
242 ngx_mail_smtp_auth_methods_names[i].len); | 253 if (m & conf->auth_methods) { |
254 *p++ = ' '; | |
255 p = ngx_cpymem(p, ngx_mail_smtp_auth_methods_names[i].data, | |
256 ngx_mail_smtp_auth_methods_names[i].len); | |
257 } | |
243 } | 258 } |
244 } | 259 |
245 | 260 *p++ = CR; *p = LF; |
246 *p++ = CR; *p = LF; | 261 |
262 } else { | |
263 last[3] = ' '; | |
264 } | |
247 | 265 |
248 size += sizeof("250 STARTTLS" CRLF) - 1; | 266 size += sizeof("250 STARTTLS" CRLF) - 1; |
249 | 267 |
250 p = ngx_pnalloc(cf->pool, size); | 268 p = ngx_pnalloc(cf->pool, size); |
251 if (p == NULL) { | 269 if (p == NULL) { |
253 } | 271 } |
254 | 272 |
255 conf->starttls_capability.len = size; | 273 conf->starttls_capability.len = size; |
256 conf->starttls_capability.data = p; | 274 conf->starttls_capability.data = p; |
257 | 275 |
258 p = ngx_cpymem(p, conf->capability.data, | 276 p = ngx_cpymem(p, conf->capability.data, conf->capability.len); |
259 conf->capability.len); | |
260 | 277 |
261 p = ngx_cpymem(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1); | 278 p = ngx_cpymem(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1); |
262 *p++ = CR; *p = LF; | 279 *p++ = CR; *p = LF; |
263 | 280 |
264 p = conf->starttls_capability.data | 281 p = conf->starttls_capability.data |
265 + (auth - conf->capability.data) + 3; | 282 + (last - conf->capability.data) + 3; |
266 *p = '-'; | 283 *p = '-'; |
267 | 284 |
268 size = (auth - conf->capability.data) | 285 size = (auth - conf->capability.data) |
269 + sizeof("250 STARTTLS" CRLF) - 1; | 286 + sizeof("250 STARTTLS" CRLF) - 1; |
270 | 287 |
274 } | 291 } |
275 | 292 |
276 conf->starttls_only_capability.len = size; | 293 conf->starttls_only_capability.len = size; |
277 conf->starttls_only_capability.data = p; | 294 conf->starttls_only_capability.data = p; |
278 | 295 |
279 p = ngx_cpymem(p, conf->capability.data, | 296 p = ngx_cpymem(p, conf->capability.data, auth - conf->capability.data); |
280 auth - conf->capability.data); | |
281 | 297 |
282 ngx_memcpy(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1); | 298 ngx_memcpy(p, "250 STARTTLS" CRLF, sizeof("250 STARTTLS" CRLF) - 1); |
299 | |
300 if (last < auth) { | |
301 p = conf->starttls_only_capability.data | |
302 + (last - conf->capability.data) + 3; | |
303 *p = '-'; | |
304 } | |
283 | 305 |
284 return NGX_CONF_OK; | 306 return NGX_CONF_OK; |
285 } | 307 } |