Mercurial > hg > nginx-mail
annotate src/mail/ngx_mail_auth_http_module.c @ 410:cd9cb7a3ff9e
Merge with nginx 0.7.8.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 11 Aug 2008 22:07:06 +0400 |
parents | 52b28d322d76 12defd37f578 |
children | d67e93e97b4a |
rev | line source |
---|---|
290 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_event.h> | |
10 #include <ngx_event_connect.h> | |
11 #include <ngx_mail.h> | |
12 | |
13 | |
14 typedef struct { | |
15 ngx_peer_addr_t *peer; | |
16 | |
17 ngx_msec_t timeout; | |
18 | |
19 ngx_str_t host_header; | |
20 ngx_str_t uri; | |
21 ngx_str_t header; | |
22 | |
23 ngx_array_t *headers; | |
326 | 24 |
25 u_char *file; | |
26 ngx_uint_t line; | |
290 | 27 } ngx_mail_auth_http_conf_t; |
28 | |
29 | |
30 typedef struct ngx_mail_auth_http_ctx_s ngx_mail_auth_http_ctx_t; | |
31 | |
32 typedef void (*ngx_mail_auth_http_handler_pt)(ngx_mail_session_t *s, | |
33 ngx_mail_auth_http_ctx_t *ctx); | |
34 | |
35 struct ngx_mail_auth_http_ctx_s { | |
36 ngx_buf_t *request; | |
37 ngx_buf_t *response; | |
38 ngx_peer_connection_t peer; | |
39 | |
40 ngx_mail_auth_http_handler_pt handler; | |
41 | |
42 ngx_uint_t state; | |
43 ngx_uint_t hash; /* no needed ? */ | |
44 | |
45 u_char *header_name_start; | |
46 u_char *header_name_end; | |
47 u_char *header_start; | |
48 u_char *header_end; | |
49 | |
50 ngx_str_t addr; | |
51 ngx_str_t port; | |
52 ngx_str_t err; | |
53 ngx_str_t errmsg; | |
54 ngx_str_t errcode; | |
55 | |
56 time_t sleep; | |
57 | |
58 ngx_pool_t *pool; | |
59 }; | |
60 | |
61 | |
62 static void ngx_mail_auth_http_write_handler(ngx_event_t *wev); | |
63 static void ngx_mail_auth_http_read_handler(ngx_event_t *rev); | |
64 static void ngx_mail_auth_http_ignore_status_line(ngx_mail_session_t *s, | |
65 ngx_mail_auth_http_ctx_t *ctx); | |
66 static void ngx_mail_auth_http_process_headers(ngx_mail_session_t *s, | |
67 ngx_mail_auth_http_ctx_t *ctx); | |
68 static void ngx_mail_auth_sleep_handler(ngx_event_t *rev); | |
69 static ngx_int_t ngx_mail_auth_http_parse_header_line(ngx_mail_session_t *s, | |
70 ngx_mail_auth_http_ctx_t *ctx); | |
71 static void ngx_mail_auth_http_block_read(ngx_event_t *rev); | |
72 static void ngx_mail_auth_http_dummy_handler(ngx_event_t *ev); | |
73 static ngx_buf_t *ngx_mail_auth_http_create_request(ngx_mail_session_t *s, | |
74 ngx_pool_t *pool, ngx_mail_auth_http_conf_t *ahcf); | |
75 static ngx_int_t ngx_mail_auth_http_escape(ngx_pool_t *pool, ngx_str_t *text, | |
76 ngx_str_t *escaped); | |
77 | |
78 static void *ngx_mail_auth_http_create_conf(ngx_conf_t *cf); | |
79 static char *ngx_mail_auth_http_merge_conf(ngx_conf_t *cf, void *parent, | |
80 void *child); | |
81 static char *ngx_mail_auth_http(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); | |
82 static char *ngx_mail_auth_http_header(ngx_conf_t *cf, ngx_command_t *cmd, | |
83 void *conf); | |
84 | |
85 | |
86 static ngx_command_t ngx_mail_auth_http_commands[] = { | |
87 | |
88 { ngx_string("auth_http"), | |
89 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, | |
90 ngx_mail_auth_http, | |
91 NGX_MAIL_SRV_CONF_OFFSET, | |
92 0, | |
93 NULL }, | |
94 | |
95 { ngx_string("auth_http_timeout"), | |
96 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1, | |
97 ngx_conf_set_msec_slot, | |
98 NGX_MAIL_SRV_CONF_OFFSET, | |
99 offsetof(ngx_mail_auth_http_conf_t, timeout), | |
100 NULL }, | |
101 | |
102 { ngx_string("auth_http_header"), | |
103 NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE2, | |
104 ngx_mail_auth_http_header, | |
105 NGX_MAIL_SRV_CONF_OFFSET, | |
106 0, | |
107 NULL }, | |
108 | |
109 ngx_null_command | |
110 }; | |
111 | |
112 | |
113 static ngx_mail_module_t ngx_mail_auth_http_module_ctx = { | |
336 | 114 NULL, /* protocol */ |
115 | |
290 | 116 NULL, /* create main configuration */ |
117 NULL, /* init main configuration */ | |
118 | |
119 ngx_mail_auth_http_create_conf, /* create server configuration */ | |
120 ngx_mail_auth_http_merge_conf /* merge server configuration */ | |
121 }; | |
122 | |
123 | |
124 ngx_module_t ngx_mail_auth_http_module = { | |
125 NGX_MODULE_V1, | |
126 &ngx_mail_auth_http_module_ctx, /* module context */ | |
127 ngx_mail_auth_http_commands, /* module directives */ | |
128 NGX_MAIL_MODULE, /* module type */ | |
129 NULL, /* init master */ | |
130 NULL, /* init module */ | |
131 NULL, /* init process */ | |
132 NULL, /* init thread */ | |
133 NULL, /* exit thread */ | |
134 NULL, /* exit process */ | |
135 NULL, /* exit master */ | |
136 NGX_MODULE_V1_PADDING | |
137 }; | |
138 | |
139 | |
140 static ngx_str_t ngx_mail_auth_http_method[] = { | |
141 ngx_string("plain"), | |
142 ngx_string("plain"), | |
143 ngx_string("apop"), | |
400
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
144 ngx_string("cram-md5"), |
404
481e8f936572
Mail: rename "unauth" to "none".
Maxim Dounin <mdounin@mdounin.ru>
parents:
400
diff
changeset
|
145 ngx_string("none") |
290 | 146 }; |
147 | |
148 static ngx_str_t ngx_mail_smtp_errcode = ngx_string("535 5.7.0"); | |
149 | |
336 | 150 |
290 | 151 void |
152 ngx_mail_auth_http_init(ngx_mail_session_t *s) | |
153 { | |
154 ngx_int_t rc; | |
155 ngx_pool_t *pool; | |
156 ngx_mail_auth_http_ctx_t *ctx; | |
157 ngx_mail_auth_http_conf_t *ahcf; | |
158 | |
159 s->connection->log->action = "in http auth state"; | |
160 | |
161 pool = ngx_create_pool(2048, s->connection->log); | |
162 if (pool == NULL) { | |
163 ngx_mail_session_internal_server_error(s); | |
164 return; | |
165 } | |
166 | |
167 ctx = ngx_pcalloc(pool, sizeof(ngx_mail_auth_http_ctx_t)); | |
168 if (ctx == NULL) { | |
169 ngx_destroy_pool(pool); | |
170 ngx_mail_session_internal_server_error(s); | |
171 return; | |
172 } | |
173 | |
174 ctx->pool = pool; | |
175 | |
176 ahcf = ngx_mail_get_module_srv_conf(s, ngx_mail_auth_http_module); | |
177 | |
178 ctx->request = ngx_mail_auth_http_create_request(s, pool, ahcf); | |
179 if (ctx->request == NULL) { | |
180 ngx_destroy_pool(ctx->pool); | |
181 ngx_mail_session_internal_server_error(s); | |
182 return; | |
183 } | |
184 | |
185 ngx_mail_set_ctx(s, ctx, ngx_mail_auth_http_module); | |
186 | |
187 ctx->peer.sockaddr = ahcf->peer->sockaddr; | |
188 ctx->peer.socklen = ahcf->peer->socklen; | |
189 ctx->peer.name = &ahcf->peer->name; | |
190 ctx->peer.get = ngx_event_get_peer; | |
191 ctx->peer.log = s->connection->log; | |
192 ctx->peer.log_error = NGX_ERROR_ERR; | |
193 | |
194 rc = ngx_event_connect_peer(&ctx->peer); | |
195 | |
196 if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) { | |
197 if (ctx->peer.connection) { | |
198 ngx_close_connection(ctx->peer.connection); | |
199 } | |
200 | |
201 ngx_destroy_pool(ctx->pool); | |
202 ngx_mail_session_internal_server_error(s); | |
203 return; | |
204 } | |
205 | |
206 ctx->peer.connection->data = s; | |
207 ctx->peer.connection->pool = s->connection->pool; | |
208 | |
209 s->connection->read->handler = ngx_mail_auth_http_block_read; | |
210 ctx->peer.connection->read->handler = ngx_mail_auth_http_read_handler; | |
211 ctx->peer.connection->write->handler = ngx_mail_auth_http_write_handler; | |
212 | |
213 ctx->handler = ngx_mail_auth_http_ignore_status_line; | |
214 | |
215 ngx_add_timer(ctx->peer.connection->read, ahcf->timeout); | |
216 ngx_add_timer(ctx->peer.connection->write, ahcf->timeout); | |
217 | |
218 if (rc == NGX_OK) { | |
219 ngx_mail_auth_http_write_handler(ctx->peer.connection->write); | |
220 return; | |
221 } | |
222 } | |
223 | |
224 | |
225 static void | |
226 ngx_mail_auth_http_write_handler(ngx_event_t *wev) | |
227 { | |
228 ssize_t n, size; | |
229 ngx_connection_t *c; | |
230 ngx_mail_session_t *s; | |
231 ngx_mail_auth_http_ctx_t *ctx; | |
232 ngx_mail_auth_http_conf_t *ahcf; | |
233 | |
234 c = wev->data; | |
235 s = c->data; | |
236 | |
237 ctx = ngx_mail_get_module_ctx(s, ngx_mail_auth_http_module); | |
238 | |
239 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, | |
240 "mail auth http write handler"); | |
241 | |
242 if (wev->timedout) { | |
243 ngx_log_error(NGX_LOG_ERR, wev->log, NGX_ETIMEDOUT, | |
244 "auth http server %V timed out", ctx->peer.name); | |
336 | 245 ngx_close_connection(c); |
290 | 246 ngx_destroy_pool(ctx->pool); |
247 ngx_mail_session_internal_server_error(s); | |
248 return; | |
249 } | |
250 | |
251 size = ctx->request->last - ctx->request->pos; | |
252 | |
253 n = ngx_send(c, ctx->request->pos, size); | |
254 | |
255 if (n == NGX_ERROR) { | |
336 | 256 ngx_close_connection(c); |
290 | 257 ngx_destroy_pool(ctx->pool); |
258 ngx_mail_session_internal_server_error(s); | |
259 return; | |
260 } | |
261 | |
262 if (n > 0) { | |
263 ctx->request->pos += n; | |
264 | |
265 if (n == size) { | |
266 wev->handler = ngx_mail_auth_http_dummy_handler; | |
267 | |
268 if (wev->timer_set) { | |
269 ngx_del_timer(wev); | |
270 } | |
271 | |
272 if (ngx_handle_write_event(wev, 0) == NGX_ERROR) { | |
336 | 273 ngx_close_connection(c); |
290 | 274 ngx_destroy_pool(ctx->pool); |
275 ngx_mail_session_internal_server_error(s); | |
276 } | |
277 | |
278 return; | |
279 } | |
280 } | |
281 | |
282 if (!wev->timer_set) { | |
283 ahcf = ngx_mail_get_module_srv_conf(s, ngx_mail_auth_http_module); | |
284 ngx_add_timer(wev, ahcf->timeout); | |
285 } | |
286 } | |
287 | |
288 | |
289 static void | |
290 ngx_mail_auth_http_read_handler(ngx_event_t *rev) | |
291 { | |
292 ssize_t n, size; | |
293 ngx_connection_t *c; | |
294 ngx_mail_session_t *s; | |
295 ngx_mail_auth_http_ctx_t *ctx; | |
296 | |
297 c = rev->data; | |
298 s = c->data; | |
299 | |
300 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
301 "mail auth http read handler"); | |
302 | |
303 ctx = ngx_mail_get_module_ctx(s, ngx_mail_auth_http_module); | |
304 | |
305 if (rev->timedout) { | |
306 ngx_log_error(NGX_LOG_ERR, rev->log, NGX_ETIMEDOUT, | |
307 "auth http server %V timed out", ctx->peer.name); | |
336 | 308 ngx_close_connection(c); |
290 | 309 ngx_destroy_pool(ctx->pool); |
310 ngx_mail_session_internal_server_error(s); | |
311 return; | |
312 } | |
313 | |
314 if (ctx->response == NULL) { | |
315 ctx->response = ngx_create_temp_buf(ctx->pool, 1024); | |
316 if (ctx->response == NULL) { | |
336 | 317 ngx_close_connection(c); |
290 | 318 ngx_destroy_pool(ctx->pool); |
319 ngx_mail_session_internal_server_error(s); | |
320 return; | |
321 } | |
322 } | |
323 | |
324 size = ctx->response->end - ctx->response->last; | |
325 | |
326 n = ngx_recv(c, ctx->response->pos, size); | |
327 | |
328 if (n > 0) { | |
329 ctx->response->last += n; | |
330 | |
331 ctx->handler(s, ctx); | |
332 return; | |
333 } | |
334 | |
335 if (n == NGX_AGAIN) { | |
336 return; | |
337 } | |
338 | |
336 | 339 ngx_close_connection(c); |
290 | 340 ngx_destroy_pool(ctx->pool); |
341 ngx_mail_session_internal_server_error(s); | |
342 } | |
343 | |
344 | |
345 static void | |
346 ngx_mail_auth_http_ignore_status_line(ngx_mail_session_t *s, | |
347 ngx_mail_auth_http_ctx_t *ctx) | |
348 { | |
349 u_char *p, ch; | |
350 enum { | |
351 sw_start = 0, | |
352 sw_H, | |
353 sw_HT, | |
354 sw_HTT, | |
355 sw_HTTP, | |
356 sw_skip, | |
357 sw_almost_done | |
358 } state; | |
359 | |
360 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, | |
361 "mail auth http process status line"); | |
362 | |
363 state = ctx->state; | |
364 | |
365 for (p = ctx->response->pos; p < ctx->response->last; p++) { | |
366 ch = *p; | |
367 | |
368 switch (state) { | |
369 | |
370 /* "HTTP/" */ | |
371 case sw_start: | |
372 if (ch == 'H') { | |
373 state = sw_H; | |
374 break; | |
375 } | |
376 goto next; | |
377 | |
378 case sw_H: | |
379 if (ch == 'T') { | |
380 state = sw_HT; | |
381 break; | |
382 } | |
383 goto next; | |
384 | |
385 case sw_HT: | |
386 if (ch == 'T') { | |
387 state = sw_HTT; | |
388 break; | |
389 } | |
390 goto next; | |
391 | |
392 case sw_HTT: | |
393 if (ch == 'P') { | |
394 state = sw_HTTP; | |
395 break; | |
396 } | |
397 goto next; | |
398 | |
399 case sw_HTTP: | |
400 if (ch == '/') { | |
401 state = sw_skip; | |
402 break; | |
403 } | |
404 goto next; | |
405 | |
406 /* any text until end of line */ | |
407 case sw_skip: | |
408 switch (ch) { | |
409 case CR: | |
410 state = sw_almost_done; | |
411 | |
412 break; | |
413 case LF: | |
414 goto done; | |
415 } | |
416 break; | |
417 | |
418 /* end of status line */ | |
419 case sw_almost_done: | |
420 if (ch == LF) { | |
421 goto done; | |
422 } | |
423 | |
424 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
425 "auth http server &V sent invalid response", | |
426 ctx->peer.name); | |
427 ngx_close_connection(ctx->peer.connection); | |
428 ngx_destroy_pool(ctx->pool); | |
429 ngx_mail_session_internal_server_error(s); | |
430 return; | |
431 } | |
432 } | |
433 | |
434 ctx->response->pos = p; | |
435 ctx->state = state; | |
436 | |
437 return; | |
438 | |
439 next: | |
440 | |
441 p = ctx->response->start - 1; | |
442 | |
443 done: | |
444 | |
445 ctx->response->pos = p + 1; | |
446 ctx->state = 0; | |
447 ctx->handler = ngx_mail_auth_http_process_headers; | |
448 ctx->handler(s, ctx); | |
449 } | |
450 | |
451 | |
452 static void | |
453 ngx_mail_auth_http_process_headers(ngx_mail_session_t *s, | |
454 ngx_mail_auth_http_ctx_t *ctx) | |
455 { | |
456 u_char *p; | |
457 time_t timer; | |
458 size_t len, size; | |
459 ngx_int_t rc, port, n; | |
460 ngx_peer_addr_t *peer; | |
461 struct sockaddr_in *sin; | |
462 | |
463 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, | |
464 "mail auth http process headers"); | |
465 | |
466 for ( ;; ) { | |
467 rc = ngx_mail_auth_http_parse_header_line(s, ctx); | |
468 | |
469 if (rc == NGX_OK) { | |
470 | |
471 #if (NGX_DEBUG) | |
472 { | |
473 ngx_str_t key, value; | |
474 | |
475 key.len = ctx->header_name_end - ctx->header_name_start; | |
476 key.data = ctx->header_name_start; | |
477 value.len = ctx->header_end - ctx->header_start; | |
478 value.data = ctx->header_start; | |
479 | |
480 ngx_log_debug2(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, | |
481 "mail auth http header: \"%V: %V\"", | |
482 &key, &value); | |
483 } | |
484 #endif | |
485 | |
486 len = ctx->header_name_end - ctx->header_name_start; | |
487 | |
488 if (len == sizeof("Auth-Status") - 1 | |
489 && ngx_strncasecmp(ctx->header_name_start, | |
490 (u_char *) "Auth-Status", | |
491 sizeof("Auth-Status") - 1) | |
492 == 0) | |
493 { | |
494 len = ctx->header_end - ctx->header_start; | |
495 | |
496 if (len == 2 | |
497 && ctx->header_start[0] == 'O' | |
498 && ctx->header_start[1] == 'K') | |
499 { | |
500 continue; | |
501 } | |
502 | |
503 if (len == 4 | |
504 && ctx->header_start[0] == 'W' | |
505 && ctx->header_start[1] == 'A' | |
506 && ctx->header_start[2] == 'I' | |
507 && ctx->header_start[3] == 'T') | |
508 { | |
509 s->auth_wait = 1; | |
510 continue; | |
511 } | |
512 | |
513 ctx->errmsg.len = len; | |
514 ctx->errmsg.data = ctx->header_start; | |
515 | |
516 switch (s->protocol) { | |
517 | |
518 case NGX_MAIL_POP3_PROTOCOL: | |
519 size = sizeof("-ERR ") - 1 + len + sizeof(CRLF) - 1; | |
520 break; | |
521 | |
522 case NGX_MAIL_IMAP_PROTOCOL: | |
523 size = s->tag.len + sizeof("NO ") - 1 + len | |
524 + sizeof(CRLF) - 1; | |
525 break; | |
526 | |
527 default: /* NGX_MAIL_SMTP_PROTOCOL */ | |
528 ctx->err = ctx->errmsg; | |
529 continue; | |
530 } | |
531 | |
384 | 532 p = ngx_pnalloc(s->connection->pool, size); |
290 | 533 if (p == NULL) { |
534 ngx_close_connection(ctx->peer.connection); | |
535 ngx_destroy_pool(ctx->pool); | |
536 ngx_mail_session_internal_server_error(s); | |
537 return; | |
538 } | |
539 | |
540 ctx->err.data = p; | |
541 | |
542 switch (s->protocol) { | |
543 | |
544 case NGX_MAIL_POP3_PROTOCOL: | |
545 *p++ = '-'; *p++ = 'E'; *p++ = 'R'; *p++ = 'R'; *p++ = ' '; | |
546 break; | |
547 | |
548 case NGX_MAIL_IMAP_PROTOCOL: | |
549 p = ngx_cpymem(p, s->tag.data, s->tag.len); | |
550 *p++ = 'N'; *p++ = 'O'; *p++ = ' '; | |
551 break; | |
552 | |
553 default: /* NGX_MAIL_SMTP_PROTOCOL */ | |
554 break; | |
555 } | |
556 | |
557 p = ngx_cpymem(p, ctx->header_start, len); | |
558 *p++ = CR; *p++ = LF; | |
559 | |
560 ctx->err.len = p - ctx->err.data; | |
561 | |
562 continue; | |
563 } | |
564 | |
565 if (len == sizeof("Auth-Server") - 1 | |
566 && ngx_strncasecmp(ctx->header_name_start, | |
567 (u_char *) "Auth-Server", | |
568 sizeof("Auth-Server") - 1) | |
569 == 0) | |
570 { | |
571 ctx->addr.len = ctx->header_end - ctx->header_start; | |
572 ctx->addr.data = ctx->header_start; | |
573 | |
574 continue; | |
575 } | |
576 | |
577 if (len == sizeof("Auth-Port") - 1 | |
578 && ngx_strncasecmp(ctx->header_name_start, | |
579 (u_char *) "Auth-Port", | |
580 sizeof("Auth-Port") - 1) | |
581 == 0) | |
582 { | |
583 ctx->port.len = ctx->header_end - ctx->header_start; | |
584 ctx->port.data = ctx->header_start; | |
585 | |
586 continue; | |
587 } | |
588 | |
589 if (len == sizeof("Auth-User") - 1 | |
590 && ngx_strncasecmp(ctx->header_name_start, | |
591 (u_char *) "Auth-User", | |
592 sizeof("Auth-User") - 1) | |
593 == 0) | |
594 { | |
595 s->login.len = ctx->header_end - ctx->header_start; | |
596 | |
382 | 597 s->login.data = ngx_pnalloc(s->connection->pool, s->login.len); |
290 | 598 if (s->login.data == NULL) { |
599 ngx_close_connection(ctx->peer.connection); | |
600 ngx_destroy_pool(ctx->pool); | |
601 ngx_mail_session_internal_server_error(s); | |
602 return; | |
603 } | |
604 | |
605 ngx_memcpy(s->login.data, ctx->header_start, s->login.len); | |
606 | |
607 continue; | |
608 } | |
609 | |
610 if (len == sizeof("Auth-Pass") - 1 | |
611 && ngx_strncasecmp(ctx->header_name_start, | |
612 (u_char *) "Auth-Pass", | |
613 sizeof("Auth-Pass") - 1) | |
614 == 0) | |
615 { | |
616 s->passwd.len = ctx->header_end - ctx->header_start; | |
617 | |
382 | 618 s->passwd.data = ngx_pnalloc(s->connection->pool, |
619 s->passwd.len); | |
290 | 620 if (s->passwd.data == NULL) { |
621 ngx_close_connection(ctx->peer.connection); | |
622 ngx_destroy_pool(ctx->pool); | |
623 ngx_mail_session_internal_server_error(s); | |
624 return; | |
625 } | |
626 | |
627 ngx_memcpy(s->passwd.data, ctx->header_start, s->passwd.len); | |
628 | |
629 continue; | |
630 } | |
631 | |
632 if (len == sizeof("Auth-Wait") - 1 | |
633 && ngx_strncasecmp(ctx->header_name_start, | |
634 (u_char *) "Auth-Wait", | |
635 sizeof("Auth-Wait") - 1) | |
636 == 0) | |
637 { | |
638 n = ngx_atoi(ctx->header_start, | |
639 ctx->header_end - ctx->header_start); | |
640 | |
641 if (n != NGX_ERROR) { | |
642 ctx->sleep = n; | |
643 } | |
644 | |
645 continue; | |
646 } | |
647 | |
648 if (len == sizeof("Auth-Error-Code") - 1 | |
649 && ngx_strncasecmp(ctx->header_name_start, | |
650 (u_char *) "Auth-Error-Code", | |
651 sizeof("Auth-Error-Code") - 1) | |
652 == 0) | |
653 { | |
654 ctx->errcode.len = ctx->header_end - ctx->header_start; | |
655 | |
382 | 656 ctx->errcode.data = ngx_pnalloc(s->connection->pool, |
657 ctx->errcode.len); | |
290 | 658 if (ctx->errcode.data == NULL) { |
659 ngx_close_connection(ctx->peer.connection); | |
660 ngx_destroy_pool(ctx->pool); | |
661 ngx_mail_session_internal_server_error(s); | |
662 return; | |
663 } | |
664 | |
665 ngx_memcpy(ctx->errcode.data, ctx->header_start, | |
666 ctx->errcode.len); | |
667 | |
668 continue; | |
669 } | |
670 | |
671 /* ignore other headers */ | |
672 | |
673 continue; | |
674 } | |
675 | |
676 if (rc == NGX_DONE) { | |
677 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, | |
678 "mail auth http header done"); | |
679 | |
680 ngx_close_connection(ctx->peer.connection); | |
681 | |
682 if (ctx->err.len) { | |
683 | |
684 ngx_log_error(NGX_LOG_INFO, s->connection->log, 0, | |
685 "client login failed: \"%V\"", &ctx->errmsg); | |
686 | |
687 if (s->protocol == NGX_MAIL_SMTP_PROTOCOL) { | |
688 | |
689 if (ctx->errcode.len == 0) { | |
690 ctx->errcode = ngx_mail_smtp_errcode; | |
691 } | |
692 | |
693 ctx->err.len = ctx->errcode.len + ctx->errmsg.len | |
694 + sizeof(" " CRLF) - 1; | |
695 | |
382 | 696 p = ngx_pnalloc(s->connection->pool, ctx->err.len); |
296 | 697 if (p == NULL) { |
698 ngx_close_connection(ctx->peer.connection); | |
699 ngx_destroy_pool(ctx->pool); | |
700 ngx_mail_session_internal_server_error(s); | |
701 return; | |
702 } | |
290 | 703 |
296 | 704 ctx->err.data = p; |
290 | 705 |
296 | 706 p = ngx_cpymem(p, ctx->errcode.data, ctx->errcode.len); |
290 | 707 *p++ = ' '; |
296 | 708 p = ngx_cpymem(p, ctx->errmsg.data, ctx->errmsg.len); |
290 | 709 *p++ = CR; *p = LF; |
710 } | |
711 | |
712 s->out = ctx->err; | |
713 timer = ctx->sleep; | |
714 | |
715 ngx_destroy_pool(ctx->pool); | |
716 | |
717 if (timer == 0) { | |
718 s->quit = 1; | |
719 ngx_mail_send(s->connection->write); | |
720 return; | |
721 } | |
722 | |
346 | 723 ngx_add_timer(s->connection->read, (ngx_msec_t) (timer * 1000)); |
290 | 724 |
725 s->connection->read->handler = ngx_mail_auth_sleep_handler; | |
726 | |
727 return; | |
728 } | |
729 | |
730 if (s->auth_wait) { | |
731 timer = ctx->sleep; | |
732 | |
733 ngx_destroy_pool(ctx->pool); | |
734 | |
735 if (timer == 0) { | |
736 ngx_mail_auth_http_init(s); | |
737 return; | |
738 } | |
739 | |
346 | 740 ngx_add_timer(s->connection->read, (ngx_msec_t) (timer * 1000)); |
290 | 741 |
742 s->connection->read->handler = ngx_mail_auth_sleep_handler; | |
743 | |
744 return; | |
745 } | |
746 | |
747 if (ctx->addr.len == 0 || ctx->port.len == 0) { | |
748 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
749 "auth http server %V did not send server or port", | |
750 ctx->peer.name); | |
751 ngx_destroy_pool(ctx->pool); | |
752 ngx_mail_session_internal_server_error(s); | |
753 return; | |
754 } | |
755 | |
336 | 756 if (s->passwd.data == NULL |
757 && s->protocol != NGX_MAIL_SMTP_PROTOCOL) | |
290 | 758 { |
759 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
760 "auth http server %V did not send password", | |
761 ctx->peer.name); | |
762 ngx_destroy_pool(ctx->pool); | |
763 ngx_mail_session_internal_server_error(s); | |
764 return; | |
765 } | |
766 | |
767 peer = ngx_pcalloc(s->connection->pool, sizeof(ngx_peer_addr_t)); | |
768 if (peer == NULL) { | |
769 ngx_destroy_pool(ctx->pool); | |
770 ngx_mail_session_internal_server_error(s); | |
771 return; | |
772 } | |
773 | |
774 sin = ngx_pcalloc(s->connection->pool, sizeof(struct sockaddr_in)); | |
775 if (sin == NULL) { | |
776 ngx_destroy_pool(ctx->pool); | |
777 ngx_mail_session_internal_server_error(s); | |
778 return; | |
779 } | |
780 | |
781 sin->sin_family = AF_INET; | |
782 | |
783 port = ngx_atoi(ctx->port.data, ctx->port.len); | |
784 if (port == NGX_ERROR || port < 1 || port > 65536) { | |
785 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
786 "auth http server %V sent invalid server " | |
787 "port:\"%V\"", | |
788 ctx->peer.name, &ctx->port); | |
789 ngx_destroy_pool(ctx->pool); | |
790 ngx_mail_session_internal_server_error(s); | |
791 return; | |
792 } | |
793 | |
794 sin->sin_port = htons((in_port_t) port); | |
795 | |
796 ctx->addr.data[ctx->addr.len] = '\0'; | |
797 sin->sin_addr.s_addr = inet_addr((char *) ctx->addr.data); | |
798 if (sin->sin_addr.s_addr == INADDR_NONE) { | |
799 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
800 "auth http server %V sent invalid server " | |
801 "address:\"%V\"", | |
802 ctx->peer.name, &ctx->addr); | |
803 ngx_destroy_pool(ctx->pool); | |
804 ngx_mail_session_internal_server_error(s); | |
805 return; | |
806 } | |
807 | |
808 peer->sockaddr = (struct sockaddr *) sin; | |
809 peer->socklen = sizeof(struct sockaddr_in); | |
810 | |
811 len = ctx->addr.len + 1 + ctx->port.len; | |
812 | |
813 peer->name.len = len; | |
814 | |
382 | 815 peer->name.data = ngx_pnalloc(s->connection->pool, len); |
290 | 816 if (peer->name.data == NULL) { |
817 ngx_destroy_pool(ctx->pool); | |
818 ngx_mail_session_internal_server_error(s); | |
819 return; | |
820 } | |
821 | |
822 len = ctx->addr.len; | |
823 | |
824 ngx_memcpy(peer->name.data, ctx->addr.data, len); | |
825 | |
826 peer->name.data[len++] = ':'; | |
827 | |
828 ngx_memcpy(peer->name.data + len, ctx->port.data, ctx->port.len); | |
829 | |
830 ngx_destroy_pool(ctx->pool); | |
831 ngx_mail_proxy_init(s, peer); | |
832 | |
833 return; | |
834 } | |
835 | |
836 if (rc == NGX_AGAIN ) { | |
837 return; | |
838 } | |
839 | |
840 /* rc == NGX_ERROR */ | |
841 | |
842 ngx_log_error(NGX_LOG_ERR, s->connection->log, 0, | |
843 "auth http server %V sent invalid header in response", | |
844 ctx->peer.name); | |
845 ngx_close_connection(ctx->peer.connection); | |
846 ngx_destroy_pool(ctx->pool); | |
847 ngx_mail_session_internal_server_error(s); | |
848 | |
849 return; | |
850 } | |
851 } | |
852 | |
853 | |
854 static void | |
855 ngx_mail_auth_sleep_handler(ngx_event_t *rev) | |
856 { | |
857 ngx_connection_t *c; | |
858 ngx_mail_session_t *s; | |
859 ngx_mail_core_srv_conf_t *cscf; | |
860 | |
861 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail auth sleep handler"); | |
862 | |
863 c = rev->data; | |
864 s = c->data; | |
865 | |
866 if (rev->timedout) { | |
867 | |
868 rev->timedout = 0; | |
869 | |
870 if (s->auth_wait) { | |
871 s->auth_wait = 0; | |
872 ngx_mail_auth_http_init(s); | |
873 return; | |
874 } | |
875 | |
336 | 876 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); |
290 | 877 |
336 | 878 rev->handler = cscf->protocol->auth_state; |
290 | 879 |
336 | 880 s->mail_state = 0; |
290 | 881 s->auth_method = NGX_MAIL_AUTH_PLAIN; |
882 | |
883 c->log->action = "in auth state"; | |
884 | |
336 | 885 ngx_mail_send(c->write); |
290 | 886 |
887 if (c->destroyed) { | |
888 return; | |
889 } | |
890 | |
891 ngx_add_timer(rev, cscf->timeout); | |
892 | |
893 if (rev->ready) { | |
336 | 894 rev->handler(rev); |
290 | 895 return; |
896 } | |
897 | |
898 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { | |
336 | 899 ngx_mail_close_connection(c); |
290 | 900 } |
901 | |
902 return; | |
903 } | |
904 | |
905 if (rev->active) { | |
906 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { | |
336 | 907 ngx_mail_close_connection(c); |
290 | 908 } |
909 } | |
910 } | |
911 | |
912 | |
913 static ngx_int_t | |
914 ngx_mail_auth_http_parse_header_line(ngx_mail_session_t *s, | |
915 ngx_mail_auth_http_ctx_t *ctx) | |
916 { | |
917 u_char c, ch, *p; | |
918 ngx_uint_t hash; | |
919 enum { | |
920 sw_start = 0, | |
921 sw_name, | |
922 sw_space_before_value, | |
923 sw_value, | |
924 sw_space_after_value, | |
925 sw_almost_done, | |
926 sw_header_almost_done | |
927 } state; | |
928 | |
929 state = ctx->state; | |
930 hash = ctx->hash; | |
931 | |
932 for (p = ctx->response->pos; p < ctx->response->last; p++) { | |
933 ch = *p; | |
934 | |
935 switch (state) { | |
936 | |
937 /* first char */ | |
938 case sw_start: | |
939 | |
940 switch (ch) { | |
941 case CR: | |
942 ctx->header_end = p; | |
943 state = sw_header_almost_done; | |
944 break; | |
945 case LF: | |
946 ctx->header_end = p; | |
947 goto header_done; | |
948 default: | |
949 state = sw_name; | |
950 ctx->header_name_start = p; | |
951 | |
952 c = (u_char) (ch | 0x20); | |
953 if (c >= 'a' && c <= 'z') { | |
954 hash = c; | |
955 break; | |
956 } | |
957 | |
958 if (ch >= '0' && ch <= '9') { | |
959 hash = ch; | |
960 break; | |
961 } | |
962 | |
963 return NGX_ERROR; | |
964 } | |
965 break; | |
966 | |
967 /* header name */ | |
968 case sw_name: | |
969 c = (u_char) (ch | 0x20); | |
970 if (c >= 'a' && c <= 'z') { | |
971 hash += c; | |
972 break; | |
973 } | |
974 | |
975 if (ch == ':') { | |
976 ctx->header_name_end = p; | |
977 state = sw_space_before_value; | |
978 break; | |
979 } | |
980 | |
981 if (ch == '-') { | |
982 hash += ch; | |
983 break; | |
984 } | |
985 | |
986 if (ch >= '0' && ch <= '9') { | |
987 hash += ch; | |
988 break; | |
989 } | |
990 | |
991 if (ch == CR) { | |
992 ctx->header_name_end = p; | |
993 ctx->header_start = p; | |
994 ctx->header_end = p; | |
995 state = sw_almost_done; | |
996 break; | |
997 } | |
998 | |
999 if (ch == LF) { | |
1000 ctx->header_name_end = p; | |
1001 ctx->header_start = p; | |
1002 ctx->header_end = p; | |
1003 goto done; | |
1004 } | |
1005 | |
1006 return NGX_ERROR; | |
1007 | |
1008 /* space* before header value */ | |
1009 case sw_space_before_value: | |
1010 switch (ch) { | |
1011 case ' ': | |
1012 break; | |
1013 case CR: | |
1014 ctx->header_start = p; | |
1015 ctx->header_end = p; | |
1016 state = sw_almost_done; | |
1017 break; | |
1018 case LF: | |
1019 ctx->header_start = p; | |
1020 ctx->header_end = p; | |
1021 goto done; | |
1022 default: | |
1023 ctx->header_start = p; | |
1024 state = sw_value; | |
1025 break; | |
1026 } | |
1027 break; | |
1028 | |
1029 /* header value */ | |
1030 case sw_value: | |
1031 switch (ch) { | |
1032 case ' ': | |
1033 ctx->header_end = p; | |
1034 state = sw_space_after_value; | |
1035 break; | |
1036 case CR: | |
1037 ctx->header_end = p; | |
1038 state = sw_almost_done; | |
1039 break; | |
1040 case LF: | |
1041 ctx->header_end = p; | |
1042 goto done; | |
1043 } | |
1044 break; | |
1045 | |
1046 /* space* before end of header line */ | |
1047 case sw_space_after_value: | |
1048 switch (ch) { | |
1049 case ' ': | |
1050 break; | |
1051 case CR: | |
1052 state = sw_almost_done; | |
1053 break; | |
1054 case LF: | |
1055 goto done; | |
1056 default: | |
1057 state = sw_value; | |
1058 break; | |
1059 } | |
1060 break; | |
1061 | |
1062 /* end of header line */ | |
1063 case sw_almost_done: | |
1064 switch (ch) { | |
1065 case LF: | |
1066 goto done; | |
1067 default: | |
1068 return NGX_ERROR; | |
1069 } | |
1070 | |
1071 /* end of header */ | |
1072 case sw_header_almost_done: | |
1073 switch (ch) { | |
1074 case LF: | |
1075 goto header_done; | |
1076 default: | |
1077 return NGX_ERROR; | |
1078 } | |
1079 } | |
1080 } | |
1081 | |
1082 ctx->response->pos = p; | |
1083 ctx->state = state; | |
1084 ctx->hash = hash; | |
1085 | |
1086 return NGX_AGAIN; | |
1087 | |
1088 done: | |
1089 | |
1090 ctx->response->pos = p + 1; | |
1091 ctx->state = sw_start; | |
1092 ctx->hash = hash; | |
1093 | |
1094 return NGX_OK; | |
1095 | |
1096 header_done: | |
1097 | |
1098 ctx->response->pos = p + 1; | |
1099 ctx->state = sw_start; | |
1100 | |
1101 return NGX_DONE; | |
1102 } | |
1103 | |
1104 | |
1105 static void | |
1106 ngx_mail_auth_http_block_read(ngx_event_t *rev) | |
1107 { | |
1108 ngx_connection_t *c; | |
1109 ngx_mail_session_t *s; | |
1110 ngx_mail_auth_http_ctx_t *ctx; | |
1111 | |
1112 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, | |
1113 "mail auth http block read"); | |
1114 | |
1115 if (ngx_handle_read_event(rev, 0) == NGX_ERROR) { | |
1116 c = rev->data; | |
1117 s = c->data; | |
1118 | |
1119 ctx = ngx_mail_get_module_ctx(s, ngx_mail_auth_http_module); | |
1120 | |
1121 ngx_close_connection(ctx->peer.connection); | |
1122 ngx_destroy_pool(ctx->pool); | |
1123 ngx_mail_session_internal_server_error(s); | |
1124 } | |
1125 } | |
1126 | |
1127 | |
1128 static void | |
1129 ngx_mail_auth_http_dummy_handler(ngx_event_t *ev) | |
1130 { | |
1131 ngx_log_debug0(NGX_LOG_DEBUG_MAIL, ev->log, 0, | |
1132 "mail auth http dummy handler"); | |
1133 } | |
1134 | |
1135 | |
1136 static ngx_buf_t * | |
1137 ngx_mail_auth_http_create_request(ngx_mail_session_t *s, ngx_pool_t *pool, | |
1138 ngx_mail_auth_http_conf_t *ahcf) | |
1139 { | |
336 | 1140 size_t len; |
1141 ngx_buf_t *b; | |
1142 ngx_str_t login, passwd; | |
1143 ngx_mail_core_srv_conf_t *cscf; | |
290 | 1144 |
1145 if (ngx_mail_auth_http_escape(pool, &s->login, &login) != NGX_OK) { | |
1146 return NULL; | |
1147 } | |
1148 | |
1149 if (ngx_mail_auth_http_escape(pool, &s->passwd, &passwd) != NGX_OK) { | |
1150 return NULL; | |
1151 } | |
1152 | |
336 | 1153 cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); |
1154 | |
290 | 1155 len = sizeof("GET ") - 1 + ahcf->uri.len + sizeof(" HTTP/1.0" CRLF) - 1 |
1156 + sizeof("Host: ") - 1 + ahcf->host_header.len + sizeof(CRLF) - 1 | |
1157 + sizeof("Auth-Method: ") - 1 | |
1158 + ngx_mail_auth_http_method[s->auth_method].len | |
1159 + sizeof(CRLF) - 1 | |
1160 + sizeof("Auth-User: ") - 1 + login.len + sizeof(CRLF) - 1 | |
1161 + sizeof("Auth-Pass: ") - 1 + passwd.len + sizeof(CRLF) - 1 | |
1162 + sizeof("Auth-Salt: ") - 1 + s->salt.len | |
336 | 1163 + sizeof("Auth-Protocol: ") - 1 + cscf->protocol->name.len |
1164 + sizeof(CRLF) - 1 | |
290 | 1165 + sizeof("Auth-Login-Attempt: ") - 1 + NGX_INT_T_LEN |
1166 + sizeof(CRLF) - 1 | |
1167 + sizeof("Client-IP: ") - 1 + s->connection->addr_text.len | |
1168 + sizeof(CRLF) - 1 | |
400
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1169 + sizeof("Auth-SMTP-Helo: ") - 1 + s->smtp_helo.len |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1170 + sizeof("Auth-SMTP-From: ") - 1 + s->smtp_from.len |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1171 + sizeof("Auth-SMTP-To: ") - 1 + s->smtp_to.len |
318 | 1172 + ahcf->header.len |
290 | 1173 + sizeof(CRLF) - 1; |
1174 | |
1175 b = ngx_create_temp_buf(pool, len); | |
1176 if (b == NULL) { | |
1177 return NULL; | |
1178 } | |
1179 | |
1180 b->last = ngx_cpymem(b->last, "GET ", sizeof("GET ") - 1); | |
1181 b->last = ngx_copy(b->last, ahcf->uri.data, ahcf->uri.len); | |
1182 b->last = ngx_cpymem(b->last, " HTTP/1.0" CRLF, | |
1183 sizeof(" HTTP/1.0" CRLF) - 1); | |
1184 | |
1185 b->last = ngx_cpymem(b->last, "Host: ", sizeof("Host: ") - 1); | |
1186 b->last = ngx_copy(b->last, ahcf->host_header.data, | |
1187 ahcf->host_header.len); | |
1188 *b->last++ = CR; *b->last++ = LF; | |
1189 | |
1190 b->last = ngx_cpymem(b->last, "Auth-Method: ", | |
1191 sizeof("Auth-Method: ") - 1); | |
1192 b->last = ngx_cpymem(b->last, | |
1193 ngx_mail_auth_http_method[s->auth_method].data, | |
1194 ngx_mail_auth_http_method[s->auth_method].len); | |
1195 *b->last++ = CR; *b->last++ = LF; | |
1196 | |
1197 b->last = ngx_cpymem(b->last, "Auth-User: ", sizeof("Auth-User: ") - 1); | |
1198 b->last = ngx_copy(b->last, login.data, login.len); | |
1199 *b->last++ = CR; *b->last++ = LF; | |
1200 | |
1201 b->last = ngx_cpymem(b->last, "Auth-Pass: ", sizeof("Auth-Pass: ") - 1); | |
1202 b->last = ngx_copy(b->last, passwd.data, passwd.len); | |
1203 *b->last++ = CR; *b->last++ = LF; | |
1204 | |
1205 if (s->auth_method != NGX_MAIL_AUTH_PLAIN && s->salt.len) { | |
1206 b->last = ngx_cpymem(b->last, "Auth-Salt: ", sizeof("Auth-Salt: ") - 1); | |
1207 b->last = ngx_copy(b->last, s->salt.data, s->salt.len); | |
1208 | |
1209 s->passwd.data = NULL; | |
1210 } | |
1211 | |
1212 b->last = ngx_cpymem(b->last, "Auth-Protocol: ", | |
1213 sizeof("Auth-Protocol: ") - 1); | |
336 | 1214 b->last = ngx_cpymem(b->last, cscf->protocol->name.data, |
1215 cscf->protocol->name.len); | |
290 | 1216 *b->last++ = CR; *b->last++ = LF; |
1217 | |
1218 b->last = ngx_sprintf(b->last, "Auth-Login-Attempt: %ui" CRLF, | |
1219 s->login_attempt); | |
1220 | |
1221 b->last = ngx_cpymem(b->last, "Client-IP: ", sizeof("Client-IP: ") - 1); | |
1222 b->last = ngx_copy(b->last, s->connection->addr_text.data, | |
1223 s->connection->addr_text.len); | |
1224 *b->last++ = CR; *b->last++ = LF; | |
1225 | |
404
481e8f936572
Mail: rename "unauth" to "none".
Maxim Dounin <mdounin@mdounin.ru>
parents:
400
diff
changeset
|
1226 if (s->auth_method == NGX_MAIL_AUTH_NONE) { |
400
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1227 |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1228 /* HELO / MAIL FROM / RCPT TO can't contain CRLF, no need to escape */ |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1229 |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1230 b->last = ngx_cpymem(b->last, "Auth-SMTP-Helo: ", |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1231 sizeof("Auth-SMTP-Helo: ") - 1); |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1232 b->last = ngx_copy(b->last, s->smtp_helo.data, s->smtp_helo.len); |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1233 *b->last++ = CR; *b->last++ = LF; |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1234 |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1235 b->last = ngx_cpymem(b->last, "Auth-SMTP-From: ", |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1236 sizeof("Auth-SMTP-From: ") - 1); |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1237 b->last = ngx_copy(b->last, s->smtp_from.data, s->smtp_from.len); |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1238 *b->last++ = CR; *b->last++ = LF; |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1239 |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1240 b->last = ngx_cpymem(b->last, "Auth-SMTP-To: ", |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1241 sizeof("Auth-SMTP-To: ") - 1); |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1242 b->last = ngx_copy(b->last, s->smtp_to.data, s->smtp_to.len); |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1243 *b->last++ = CR; *b->last++ = LF; |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1244 |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1245 } |
f1e2fab7a46c
Mail: smtp proxy without authentication.
Maxim Dounin <mdounin@mdounin.ru>
parents:
394
diff
changeset
|
1246 |
290 | 1247 if (ahcf->header.len) { |
1248 b->last = ngx_copy(b->last, ahcf->header.data, ahcf->header.len); | |
1249 } | |
1250 | |
1251 /* add "\r\n" at the header end */ | |
1252 *b->last++ = CR; *b->last++ = LF; | |
1253 | |
1254 #if (NGX_DEBUG_MAIL_PASSWD) | |
1255 { | |
1256 ngx_str_t l; | |
1257 | |
1258 l.len = b->last - b->pos; | |
1259 l.data = b->pos; | |
1260 ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0, | |
1261 "mail auth http header:\n\"%V\"", &l); | |
1262 } | |
1263 #endif | |
1264 | |
1265 return b; | |
1266 } | |
1267 | |
1268 | |
1269 static ngx_int_t | |
1270 ngx_mail_auth_http_escape(ngx_pool_t *pool, ngx_str_t *text, ngx_str_t *escaped) | |
1271 { | |
328 | 1272 u_char *p; |
1273 uintptr_t n; | |
290 | 1274 |
328 | 1275 n = ngx_escape_uri(NULL, text->data, text->len, NGX_ESCAPE_MAIL_AUTH); |
290 | 1276 |
1277 if (n == 0) { | |
1278 *escaped = *text; | |
1279 return NGX_OK; | |
1280 } | |
1281 | |
1282 escaped->len = text->len + n * 2; | |
1283 | |
382 | 1284 p = ngx_pnalloc(pool, escaped->len); |
290 | 1285 if (p == NULL) { |
1286 return NGX_ERROR; | |
1287 } | |
1288 | |
328 | 1289 (void) ngx_escape_uri(p, text->data, text->len, NGX_ESCAPE_MAIL_AUTH); |
290 | 1290 |
328 | 1291 escaped->data = p; |
290 | 1292 |
1293 return NGX_OK; | |
1294 } | |
1295 | |
1296 | |
1297 static void * | |
1298 ngx_mail_auth_http_create_conf(ngx_conf_t *cf) | |
1299 { | |
1300 ngx_mail_auth_http_conf_t *ahcf; | |
1301 | |
1302 ahcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_auth_http_conf_t)); | |
1303 if (ahcf == NULL) { | |
1304 return NGX_CONF_ERROR; | |
1305 } | |
1306 | |
1307 ahcf->timeout = NGX_CONF_UNSET_MSEC; | |
1308 | |
326 | 1309 ahcf->file = cf->conf_file->file.name.data; |
1310 ahcf->line = cf->conf_file->line; | |
1311 | |
290 | 1312 return ahcf; |
1313 } | |
1314 | |
1315 | |
1316 static char * | |
1317 ngx_mail_auth_http_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
1318 { | |
1319 ngx_mail_auth_http_conf_t *prev = parent; | |
1320 ngx_mail_auth_http_conf_t *conf = child; | |
1321 | |
1322 u_char *p; | |
1323 size_t len; | |
1324 ngx_uint_t i; | |
1325 ngx_table_elt_t *header; | |
1326 | |
1327 if (conf->peer == NULL) { | |
1328 conf->peer = prev->peer; | |
1329 conf->host_header = prev->host_header; | |
1330 conf->uri = prev->uri; | |
326 | 1331 |
1332 if (conf->peer == NULL) { | |
1333 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
1334 "no \"http_auth\" is defined for server in %s:%ui", | |
1335 conf->file, conf->line); | |
1336 | |
1337 return NGX_CONF_ERROR; | |
1338 } | |
290 | 1339 } |
1340 | |
1341 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000); | |
1342 | |
1343 if (conf->headers == NULL) { | |
1344 conf->headers = prev->headers; | |
1345 conf->header = prev->header; | |
1346 } | |
1347 | |
1348 if (conf->headers && conf->header.len == 0) { | |
1349 len = 0; | |
1350 header = conf->headers->elts; | |
1351 for (i = 0; i < conf->headers->nelts; i++) { | |
1352 len += header[i].key.len + 2 + header[i].value.len + 2; | |
1353 } | |
1354 | |
382 | 1355 p = ngx_pnalloc(cf->pool, len); |
290 | 1356 if (p == NULL) { |
1357 return NGX_CONF_ERROR; | |
1358 } | |
1359 | |
1360 conf->header.len = len; | |
1361 conf->header.data = p; | |
1362 | |
1363 for (i = 0; i < conf->headers->nelts; i++) { | |
1364 p = ngx_cpymem(p, header[i].key.data, header[i].key.len); | |
1365 *p++ = ':'; *p++ = ' '; | |
1366 p = ngx_cpymem(p, header[i].value.data, header[i].value.len); | |
1367 *p++ = CR; *p++ = LF; | |
1368 } | |
1369 } | |
1370 | |
1371 return NGX_CONF_OK; | |
1372 } | |
1373 | |
1374 | |
1375 static char * | |
1376 ngx_mail_auth_http(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1377 { | |
1378 ngx_mail_auth_http_conf_t *ahcf = conf; | |
1379 | |
1380 ngx_str_t *value; | |
1381 ngx_url_t u; | |
1382 | |
1383 value = cf->args->elts; | |
1384 | |
1385 ngx_memzero(&u, sizeof(ngx_url_t)); | |
1386 | |
1387 u.url = value[1]; | |
1388 u.default_port = 80; | |
1389 u.uri_part = 1; | |
1390 u.one_addr = 1; | |
1391 | |
326 | 1392 if (ngx_strncmp(u.url.data, "http://", 7) == 0) { |
1393 u.url.len -= 7; | |
1394 u.url.data += 7; | |
1395 } | |
1396 | |
340 | 1397 if (ngx_parse_url(cf->pool, &u) != NGX_OK) { |
290 | 1398 if (u.err) { |
1399 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
1400 "%s in auth_http \"%V\"", u.err, &u.url); | |
1401 } | |
326 | 1402 |
1403 return NGX_CONF_ERROR; | |
290 | 1404 } |
1405 | |
1406 ahcf->peer = u.addrs; | |
1407 | |
1408 ahcf->host_header = u.host; | |
1409 ahcf->uri = u.uri; | |
1410 | |
1411 if (ahcf->uri.len == 0) { | |
1412 ahcf->uri.len = sizeof("/") - 1; | |
1413 ahcf->uri.data = (u_char *) "/"; | |
1414 } | |
1415 | |
1416 return NGX_CONF_OK; | |
1417 } | |
1418 | |
1419 | |
1420 static char * | |
1421 ngx_mail_auth_http_header(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1422 { | |
1423 ngx_mail_auth_http_conf_t *ahcf = conf; | |
1424 | |
1425 ngx_str_t *value; | |
1426 ngx_table_elt_t *header; | |
1427 | |
1428 if (ahcf->headers == NULL) { | |
1429 ahcf->headers = ngx_array_create(cf->pool, 1, sizeof(ngx_table_elt_t)); | |
1430 if (ahcf->headers == NULL) { | |
1431 return NGX_CONF_ERROR; | |
1432 } | |
1433 } | |
1434 | |
1435 header = ngx_array_push(ahcf->headers); | |
1436 if (header == NULL) { | |
1437 return NGX_CONF_ERROR; | |
1438 } | |
1439 | |
1440 value = cf->args->elts; | |
1441 | |
1442 header->key = value[1]; | |
1443 header->value = value[2]; | |
1444 | |
1445 return NGX_CONF_OK; | |
1446 } |