~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Nginx/mail/ngx_mail_proxy_module.c

Version: ~ [ nginx-0.6.26 ] ~ [ nginx-0.5.35 ] ~ [ nginx-0.5.20 ] ~ [ nginx-0.5.19 ] ~

  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_flag_t  enable;
 16     ngx_flag_t  pass_error_message;
 17     ngx_flag_t  xclient;
 18     size_t      buffer_size;
 19     ngx_msec_t  timeout;
 20 } ngx_mail_proxy_conf_t;
 21 
 22 
 23 static void ngx_mail_proxy_block_read(ngx_event_t *rev);
 24 static void ngx_mail_proxy_pop3_handler(ngx_event_t *rev);
 25 static void ngx_mail_proxy_imap_handler(ngx_event_t *rev);
 26 static void ngx_mail_proxy_smtp_handler(ngx_event_t *rev);
 27 static void ngx_mail_proxy_dummy_handler(ngx_event_t *ev);
 28 static ngx_int_t ngx_mail_proxy_read_response(ngx_mail_session_t *s,
 29     ngx_uint_t state);
 30 static void ngx_mail_proxy_handler(ngx_event_t *ev);
 31 static void ngx_mail_proxy_upstream_error(ngx_mail_session_t *s);
 32 static void ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s);
 33 static void ngx_mail_proxy_close_session(ngx_mail_session_t *s);
 34 static void *ngx_mail_proxy_create_conf(ngx_conf_t *cf);
 35 static char *ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent,
 36     void *child);
 37 
 38 
 39 static ngx_command_t  ngx_mail_proxy_commands[] = {
 40 
 41     { ngx_string("proxy"),
 42       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
 43       ngx_conf_set_flag_slot,
 44       NGX_MAIL_SRV_CONF_OFFSET,
 45       offsetof(ngx_mail_proxy_conf_t, enable),
 46       NULL },
 47 
 48     { ngx_string("proxy_buffer"),
 49       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
 50       ngx_conf_set_size_slot,
 51       NGX_MAIL_SRV_CONF_OFFSET,
 52       offsetof(ngx_mail_proxy_conf_t, buffer_size),
 53       NULL },
 54 
 55     { ngx_string("proxy_timeout"),
 56       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
 57       ngx_conf_set_msec_slot,
 58       NGX_MAIL_SRV_CONF_OFFSET,
 59       offsetof(ngx_mail_proxy_conf_t, timeout),
 60       NULL },
 61 
 62     { ngx_string("proxy_pass_error_message"),
 63       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_TAKE1,
 64       ngx_conf_set_flag_slot,
 65       NGX_MAIL_SRV_CONF_OFFSET,
 66       offsetof(ngx_mail_proxy_conf_t, pass_error_message),
 67       NULL },
 68 
 69     { ngx_string("xclient"),
 70       NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_FLAG,
 71       ngx_conf_set_flag_slot,
 72       NGX_MAIL_SRV_CONF_OFFSET,
 73       offsetof(ngx_mail_proxy_conf_t, xclient),
 74       NULL },
 75 
 76       ngx_null_command
 77 };
 78 
 79 
 80 static ngx_mail_module_t  ngx_mail_proxy_module_ctx = {
 81     NULL,                                  /* protocol */
 82 
 83     NULL,                                  /* create main configuration */
 84     NULL,                                  /* init main configuration */
 85 
 86     ngx_mail_proxy_create_conf,            /* create server configuration */
 87     ngx_mail_proxy_merge_conf              /* merge server configuration */
 88 };
 89 
 90 
 91 ngx_module_t  ngx_mail_proxy_module = {
 92     NGX_MODULE_V1,
 93     &ngx_mail_proxy_module_ctx,            /* module context */
 94     ngx_mail_proxy_commands,               /* module directives */
 95     NGX_MAIL_MODULE,                       /* module type */
 96     NULL,                                  /* init master */
 97     NULL,                                  /* init module */
 98     NULL,                                  /* init process */
 99     NULL,                                  /* init thread */
100     NULL,                                  /* exit thread */
101     NULL,                                  /* exit process */
102     NULL,                                  /* exit master */
103     NGX_MODULE_V1_PADDING
104 };
105 
106 
107 static u_char  smtp_ok[] = "235 2.0.0 OK" CRLF;
108 
109 
110 void
111 ngx_mail_proxy_init(ngx_mail_session_t *s, ngx_peer_addr_t *peer)
112 {
113     int                        keepalive;
114     ngx_int_t                  rc;
115     ngx_mail_proxy_ctx_t      *p;
116     ngx_mail_proxy_conf_t     *pcf;
117     ngx_mail_core_srv_conf_t  *cscf;
118 
119     s->connection->log->action = "connecting to upstream";
120 
121     cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
122 
123     if (cscf->so_keepalive) {
124         keepalive = 1;
125 
126         if (setsockopt(s->connection->fd, SOL_SOCKET, SO_KEEPALIVE,
127                        (const void *) &keepalive, sizeof(int))
128                 == -1)
129         {
130             ngx_log_error(NGX_LOG_ALERT, s->connection->log, ngx_socket_errno,
131                           "setsockopt(SO_KEEPALIVE) failed");
132         }
133     }
134 
135     p = ngx_pcalloc(s->connection->pool, sizeof(ngx_mail_proxy_ctx_t));
136     if (p == NULL) {
137         ngx_mail_session_internal_server_error(s);
138         return;
139     }
140 
141     s->proxy = p;
142 
143     p->upstream.sockaddr = peer->sockaddr;
144     p->upstream.socklen = peer->socklen;
145     p->upstream.name = &peer->name;
146     p->upstream.get = ngx_event_get_peer;
147     p->upstream.log = s->connection->log;
148     p->upstream.log_error = NGX_ERROR_ERR;
149 
150     rc = ngx_event_connect_peer(&p->upstream);
151 
152     if (rc == NGX_ERROR || rc == NGX_BUSY || rc == NGX_DECLINED) {
153         ngx_mail_proxy_internal_server_error(s);
154         return;
155     }
156 
157     ngx_add_timer(p->upstream.connection->read, cscf->timeout);
158 
159     p->upstream.connection->data = s;
160     p->upstream.connection->pool = s->connection->pool;
161 
162     s->connection->read->handler = ngx_mail_proxy_block_read;
163     p->upstream.connection->write->handler = ngx_mail_proxy_dummy_handler;
164 
165     pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
166 
167     s->proxy->buffer = ngx_create_temp_buf(s->connection->pool,
168                                            pcf->buffer_size);
169     if (s->proxy->buffer == NULL) {
170         ngx_mail_proxy_internal_server_error(s);
171         return;
172     }
173 
174     switch (s->protocol) {
175 
176     case NGX_MAIL_POP3_PROTOCOL:
177         p->upstream.connection->read->handler = ngx_mail_proxy_pop3_handler;
178         s->mail_state = ngx_pop3_start;
179         break;
180 
181     case NGX_MAIL_IMAP_PROTOCOL:
182         p->upstream.connection->read->handler = ngx_mail_proxy_imap_handler;
183         s->mail_state = ngx_imap_start;
184         break;
185 
186     default: /* NGX_MAIL_SMTP_PROTOCOL */
187         p->upstream.connection->read->handler = ngx_mail_proxy_smtp_handler;
188         s->mail_state = ngx_smtp_start;
189         break;
190     }
191 }
192 
193 
194 static void
195 ngx_mail_proxy_block_read(ngx_event_t *rev)
196 {
197     ngx_connection_t    *c;
198     ngx_mail_session_t  *s;
199 
200     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy block read");
201 
202     if (ngx_handle_read_event(rev, 0) == NGX_ERROR) {
203         c = rev->data;
204         s = c->data;
205 
206         ngx_mail_proxy_close_session(s);
207     }
208 }
209 
210 
211 static void
212 ngx_mail_proxy_pop3_handler(ngx_event_t *rev)
213 {
214     u_char                 *p;
215     ngx_int_t               rc;
216     ngx_str_t               line;
217     ngx_connection_t       *c;
218     ngx_mail_session_t     *s;
219     ngx_mail_proxy_conf_t  *pcf;
220 
221     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
222                    "mail proxy pop3 auth handler");
223 
224     c = rev->data;
225     s = c->data;
226 
227     if (rev->timedout) {
228         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
229                       "upstream timed out");
230         c->timedout = 1;
231         ngx_mail_proxy_internal_server_error(s);
232         return;
233     }
234 
235     rc = ngx_mail_proxy_read_response(s, 0);
236 
237     if (rc == NGX_AGAIN) {
238         return;
239     }
240 
241     if (rc == NGX_ERROR) {
242         ngx_mail_proxy_upstream_error(s);
243         return;
244     }
245 
246     switch (s->mail_state) {
247 
248     case ngx_pop3_start:
249         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user");
250 
251         s->connection->log->action = "sending user name to upstream";
252 
253         line.len = sizeof("USER ")  - 1 + s->login.len + 2;
254         line.data = ngx_palloc(c->pool, line.len);
255         if (line.data == NULL) {
256             ngx_mail_proxy_internal_server_error(s);
257             return;
258         }
259 
260         p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1);
261         p = ngx_cpymem(p, s->login.data, s->login.len);
262         *p++ = CR; *p = LF;
263 
264         s->mail_state = ngx_pop3_user;
265         break;
266 
267     case ngx_pop3_user:
268         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send pass");
269 
270         s->connection->log->action = "sending password to upstream";
271 
272         line.len = sizeof("PASS ")  - 1 + s->passwd.len + 2;
273         line.data = ngx_palloc(c->pool, line.len);
274         if (line.data == NULL) {
275             ngx_mail_proxy_internal_server_error(s);
276             return;
277         }
278 
279         p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1);
280         p = ngx_cpymem(p, s->passwd.data, s->passwd.len);
281         *p++ = CR; *p = LF;
282 
283         s->mail_state = ngx_pop3_passwd;
284         break;
285 
286     case ngx_pop3_passwd:
287         s->connection->read->handler = ngx_mail_proxy_handler;
288         s->connection->write->handler = ngx_mail_proxy_handler;
289         rev->handler = ngx_mail_proxy_handler;
290         c->write->handler = ngx_mail_proxy_handler;
291 
292         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
293         ngx_add_timer(s->connection->read, pcf->timeout);
294         ngx_del_timer(c->read);
295 
296         c->log->action = NULL;
297         ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
298 
299         ngx_mail_proxy_handler(s->connection->write);
300 
301         return;
302 
303     default:
304 #if (NGX_SUPPRESS_WARN)
305         line.len = 0;
306         line.data = NULL;
307 #endif
308         break;
309     }
310 
311     if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
312         /*
313          * we treat the incomplete sending as NGX_ERROR
314          * because it is very strange here
315          */
316         ngx_mail_proxy_internal_server_error(s);
317         return;
318     }
319 
320     s->proxy->buffer->pos = s->proxy->buffer->start;
321     s->proxy->buffer->last = s->proxy->buffer->start;
322 }
323 
324 
325 static void
326 ngx_mail_proxy_imap_handler(ngx_event_t *rev)
327 {
328     u_char                 *p;
329     ngx_int_t               rc;
330     ngx_str_t               line;
331     ngx_connection_t       *c;
332     ngx_mail_session_t     *s;
333     ngx_mail_proxy_conf_t  *pcf;
334 
335     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
336                    "mail proxy imap auth handler");
337 
338     c = rev->data;
339     s = c->data;
340 
341     if (rev->timedout) {
342         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
343                       "upstream timed out");
344         c->timedout = 1;
345         ngx_mail_proxy_internal_server_error(s);
346         return;
347     }
348 
349     rc = ngx_mail_proxy_read_response(s, s->mail_state);
350 
351     if (rc == NGX_AGAIN) {
352         return;
353     }
354 
355     if (rc == NGX_ERROR) {
356         ngx_mail_proxy_upstream_error(s);
357         return;
358     }
359 
360     switch (s->mail_state) {
361 
362     case ngx_imap_start:
363         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
364                        "mail proxy send login");
365 
366         s->connection->log->action = "sending LOGIN command to upstream";
367 
368         line.len = s->tag.len + sizeof("LOGIN ") - 1
369                    + 1 + NGX_SIZE_T_LEN + 1 + 2;
370         line.data = ngx_palloc(c->pool, line.len);
371         if (line.data == NULL) {
372             ngx_mail_proxy_internal_server_error(s);
373             return;
374         }
375 
376         line.len = ngx_sprintf(line.data, "%VLOGIN {%uz}" CRLF,
377                                &s->tag, s->login.len)
378                    - line.data;
379 
380         s->mail_state = ngx_imap_login;
381         break;
382 
383     case ngx_imap_login:
384         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send user");
385 
386         s->connection->log->action = "sending user name to upstream";
387 
388         line.len = s->login.len + 1 + 1 + NGX_SIZE_T_LEN + 1 + 2;
389         line.data = ngx_palloc(c->pool, line.len);
390         if (line.data == NULL) {
391             ngx_mail_proxy_internal_server_error(s);
392             return;
393         }
394 
395         line.len = ngx_sprintf(line.data, "%V {%uz}" CRLF,
396                                &s->login, s->passwd.len)
397                    - line.data;
398 
399         s->mail_state = ngx_imap_user;
400         break;
401 
402     case ngx_imap_user:
403         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
404                        "mail proxy send passwd");
405 
406         s->connection->log->action = "sending password to upstream";
407 
408         line.len = s->passwd.len + 2;
409         line.data = ngx_palloc(c->pool, line.len);
410         if (line.data == NULL) {
411             ngx_mail_proxy_internal_server_error(s);
412             return;
413         }
414 
415         p = ngx_cpymem(line.data, s->passwd.data, s->passwd.len);
416         *p++ = CR; *p = LF;
417 
418         s->mail_state = ngx_imap_passwd;
419         break;
420 
421     case ngx_imap_passwd:
422         s->connection->read->handler = ngx_mail_proxy_handler;
423         s->connection->write->handler = ngx_mail_proxy_handler;
424         rev->handler = ngx_mail_proxy_handler;
425         c->write->handler = ngx_mail_proxy_handler;
426 
427         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
428         ngx_add_timer(s->connection->read, pcf->timeout);
429         ngx_del_timer(c->read);
430 
431         c->log->action = NULL;
432         ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
433 
434         ngx_mail_proxy_handler(s->connection->write);
435 
436         return;
437 
438     default:
439 #if (NGX_SUPPRESS_WARN)
440         line.len = 0;
441         line.data = NULL;
442 #endif
443         break;
444     }
445 
446     if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
447         /*
448          * we treat the incomplete sending as NGX_ERROR
449          * because it is very strange here
450          */
451         ngx_mail_proxy_internal_server_error(s);
452         return;
453     }
454 
455     s->proxy->buffer->pos = s->proxy->buffer->start;
456     s->proxy->buffer->last = s->proxy->buffer->start;
457 }
458 
459 
460 static void
461 ngx_mail_proxy_smtp_handler(ngx_event_t *rev)
462 {
463     u_char                    *p;
464     ngx_int_t                  rc;
465     ngx_str_t                  line;
466     ngx_connection_t          *c;
467     ngx_mail_session_t        *s;
468     ngx_mail_proxy_conf_t     *pcf;
469     ngx_mail_core_srv_conf_t  *cscf;
470 
471     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
472                    "mail proxy smtp auth handler");
473 
474     c = rev->data;
475     s = c->data;
476 
477     if (rev->timedout) {
478         ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
479                       "upstream timed out");
480         c->timedout = 1;
481         ngx_mail_proxy_internal_server_error(s);
482         return;
483     }
484 
485     rc = ngx_mail_proxy_read_response(s, s->mail_state);
486 
487     if (rc == NGX_AGAIN) {
488         return;
489     }
490 
491     if (rc == NGX_ERROR) {
492         ngx_mail_proxy_upstream_error(s);
493         return;
494     }
495 
496     switch (s->mail_state) {
497 
498     case ngx_smtp_start:
499         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0, "mail proxy send ehlo");
500 
501         s->connection->log->action = "sending HELO/EHLO to upstream";
502 
503         cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module);
504 
505         line.len = sizeof("HELO ")  - 1 + cscf->server_name.len + 2;
506         line.data = ngx_palloc(c->pool, line.len);
507         if (line.data == NULL) {
508             ngx_mail_proxy_internal_server_error(s);
509             return;
510         }
511 
512         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
513 
514         p = ngx_cpymem(line.data,
515                        ((s->esmtp || pcf->xclient) ? "EHLO " : "HELO "),
516                        sizeof("HELO ") - 1);
517 
518         p = ngx_cpymem(p, cscf->server_name.data, cscf->server_name.len);
519         *p++ = CR; *p = LF;
520 
521         s->mail_state = pcf->xclient ? ngx_smtp_helo: ngx_smtp_noxclient;
522 
523         break;
524 
525     case ngx_smtp_helo:
526         ngx_log_debug0(NGX_LOG_DEBUG_MAIL, rev->log, 0,
527                        "mail proxy send xclient");
528 
529         s->connection->log->action = "sending XCLIENT to upstream";
530 
531         line.len = sizeof("XCLIENT PROTO=SMTP HELO= ADDR= LOGIN= "
532                           "NAME=[UNAVAILABLE]" CRLF) - 1
533                    + s->esmtp + s->smtp_helo.len
534                    + s->connection->addr_text.len + s->login.len;
535 
536         line.data = ngx_palloc(c->pool, line.len);
537         if (line.data == NULL) {
538             ngx_mail_proxy_internal_server_error(s);
539             return;
540         }
541 
542         if (s->smtp_helo.len) {
543             line.len = ngx_sprintf(line.data,
544                            "XCLIENT PROTO=%sSMTP HELO=%V ADDR=%V LOGIN=%V "
545                            "NAME=[UNAVAILABLE]" CRLF,
546                            (s->esmtp ? "E" : ""), &s->smtp_helo,
547                            &s->connection->addr_text, &s->login)
548                        - line.data;
549         } else {
550             line.len = ngx_sprintf(line.data,
551                            "XCLIENT PROTO=SMTP ADDR=%V LOGIN=%V "
552                            "NAME=[UNAVAILABLE]" CRLF,
553                            &s->connection->addr_text, &s->login)
554                        - line.data;
555         }
556 
557         s->mail_state = ngx_smtp_xclient;
558         break;
559 
560     case ngx_smtp_noxclient:
561     case ngx_smtp_xclient:
562 
563         ngx_memcpy(s->proxy->buffer->start, smtp_ok, sizeof(smtp_ok) - 1);
564 
565         s->proxy->buffer->pos = s->proxy->buffer->start;
566         s->proxy->buffer->last = s->proxy->buffer->start + sizeof(smtp_ok) - 1;
567 
568         s->connection->read->handler = ngx_mail_proxy_handler;
569         s->connection->write->handler = ngx_mail_proxy_handler;
570         rev->handler = ngx_mail_proxy_handler;
571         c->write->handler = ngx_mail_proxy_handler;
572 
573         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
574         ngx_add_timer(s->connection->read, pcf->timeout);
575         ngx_del_timer(c->read);
576 
577         c->log->action = NULL;
578         ngx_log_error(NGX_LOG_INFO, c->log, 0, "client logged in");
579 
580         ngx_mail_proxy_handler(s->connection->write);
581 
582         return;
583 
584     default:
585 #if (NGX_SUPPRESS_WARN)
586         line.len = 0;
587         line.data = NULL;
588 #endif
589         break;
590     }
591 
592     if (c->send(c, line.data, line.len) < (ssize_t) line.len) {
593         /*
594          * we treat the incomplete sending as NGX_ERROR
595          * because it is very strange here
596          */
597         ngx_mail_proxy_internal_server_error(s);
598         return;
599     }
600 
601     s->proxy->buffer->pos = s->proxy->buffer->start;
602     s->proxy->buffer->last = s->proxy->buffer->start;
603 }
604 
605 
606 static void
607 ngx_mail_proxy_dummy_handler(ngx_event_t *wev)
608 {
609     ngx_connection_t    *c;
610     ngx_mail_session_t  *s;
611 
612     ngx_log_debug0(NGX_LOG_DEBUG_MAIL, wev->log, 0, "mail proxy dummy handler");
613 
614     if (ngx_handle_write_event(wev, 0) == NGX_ERROR) {
615         c = wev->data;
616         s = c->data;
617 
618         ngx_mail_proxy_close_session(s);
619     }
620 }
621 
622 
623 static ngx_int_t
624 ngx_mail_proxy_read_response(ngx_mail_session_t *s, ngx_uint_t state)
625 {
626     u_char                 *p;
627     ssize_t                 n;
628     ngx_buf_t              *b;
629     ngx_mail_proxy_conf_t  *pcf;
630 
631     s->connection->log->action = "reading response from upstream";
632 
633     b = s->proxy->buffer;
634 
635     n = s->proxy->upstream.connection->recv(s->proxy->upstream.connection,
636                                             b->last, b->end - b->last);
637 
638     if (n == NGX_ERROR || n == 0) {
639         return NGX_ERROR;
640     }
641 
642     if (n == NGX_AGAIN) {
643         return NGX_AGAIN;
644     }
645 
646     b->last += n;
647 
648     if (b->last - b->pos < 5) {
649         return NGX_AGAIN;
650     }
651 
652     if (*(b->last - 2) != CR || *(b->last - 1) != LF) {
653         if (b->last == b->end) {
654             *(b->last - 1) = '\0';
655             ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
656                           "upstream sent too long response line: \"%s\"",
657                           b->pos);
658             return NGX_ERROR;
659         }
660 
661         return NGX_AGAIN;
662     }
663 
664     p = b->pos;
665 
666     switch (s->protocol) {
667 
668     case NGX_MAIL_POP3_PROTOCOL:
669         if (p[0] == '+' && p[1] == 'O' && p[2] == 'K') {
670             return NGX_OK;
671         }
672         break;
673 
674     case NGX_MAIL_IMAP_PROTOCOL:
675         switch (state) {
676 
677         case ngx_imap_start:
678             if (p[0] == '*' && p[1] == ' ' && p[2] == 'O' && p[3] == 'K') {
679                 return NGX_OK;
680             }
681             break;
682 
683         case ngx_imap_login:
684         case ngx_imap_user:
685             if (p[0] == '+') {
686                 return NGX_OK;
687             }
688             break;
689 
690         case ngx_imap_passwd:
691             if (ngx_strncmp(p, s->tag.data, s->tag.len) == 0) {
692                 p += s->tag.len;
693                 if (p[0] == 'O' && p[1] == 'K') {
694                     return NGX_OK;
695                 }
696             }
697             break;
698         }
699 
700         break;
701 
702     default: /* NGX_MAIL_SMTP_PROTOCOL */
703         switch (state) {
704 
705         case ngx_smtp_helo:
706         case ngx_smtp_noxclient:
707             if (p[0] == '2' && p[1] == '5' && p[2] == '') {
708                 return NGX_OK;
709             }
710             break;
711 
712         case ngx_smtp_start:
713         case ngx_smtp_xclient:
714             if (p[0] == '2' && p[1] == '2' && p[2] == '') {
715                 return NGX_OK;
716             }
717             break;
718         }
719 
720         break;
721     }
722 
723     pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
724 
725     if (pcf->pass_error_message == 0) {
726         *(b->last - 2) = '\0';
727         ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
728                       "upstream sent invalid response: \"%s\"", p);
729         return NGX_ERROR;
730     }
731 
732     s->out.len = b->last - p - 2;
733     s->out.data = p;
734 
735     ngx_log_error(NGX_LOG_INFO, s->connection->log, 0,
736                   "upstream sent invalid response: \"%V\"", &s->out);
737 
738     s->out.len = b->last - b->pos;
739     s->out.data = b->pos;
740 
741     return NGX_ERROR;
742 }
743 
744 
745 static void
746 ngx_mail_proxy_handler(ngx_event_t *ev)
747 {
748     char                   *action, *recv_action, *send_action;
749     size_t                  size;
750     ssize_t                 n;
751     ngx_buf_t              *b;
752     ngx_uint_t              do_write;
753     ngx_connection_t       *c, *src, *dst;
754     ngx_mail_session_t     *s;
755     ngx_mail_proxy_conf_t  *pcf;
756 
757     c = ev->data;
758     s = c->data;
759 
760     if (ev->timedout) {
761         c->log->action = "proxying";
762 
763         if (c == s->connection) {
764             ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
765                           "client timed out");
766             c->timedout = 1;
767 
768         } else {
769             ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
770                           "upstream timed out");
771         }
772 
773         ngx_mail_proxy_close_session(s);
774         return;
775     }
776 
777     if (c == s->connection) {
778         if (ev->write) {
779             recv_action = "proxying and reading from upstream";
780             send_action = "proxying and sending to client";
781             src = s->proxy->upstream.connection;
782             dst = c;
783             b = s->proxy->buffer;
784 
785         } else {
786             recv_action = "proxying and reading from client";
787             send_action = "proxying and sending to upstream";
788             src = c;
789             dst = s->proxy->upstream.connection;
790             b = s->buffer;
791         }
792 
793     } else {
794         if (ev->write) {
795             recv_action = "proxying and reading from client";
796             send_action = "proxying and sending to upstream";
797             src = s->connection;
798             dst = c;
799             b = s->buffer;
800 
801         } else {
802             recv_action = "proxying and reading from upstream";
803             send_action = "proxying and sending to client";
804             src = c;
805             dst = s->connection;
806             b = s->proxy->buffer;
807         }
808     }
809 
810     do_write = ev->write ? 1 : 0;
811 
812     ngx_log_debug3(NGX_LOG_DEBUG_MAIL, ev->log, 0,
813                    "mail proxy handler: %d, #%d > #%d",
814                    do_write, src->fd, dst->fd);
815 
816     for ( ;; ) {
817 
818         if (do_write) {
819 
820             size = b->last - b->pos;
821 
822             if (size && dst->write->ready) {
823                 c->log->action = send_action;
824 
825                 n = dst->send(dst, b->pos, size);
826 
827                 if (n == NGX_ERROR) {
828                     ngx_mail_proxy_close_session(s);
829                     return;
830                 }
831 
832                 if (n > 0) {
833                     b->pos += n;
834 
835                     if (b->pos == b->last) {
836                         b->pos = b->start;
837                         b->last = b->start;
838                     }
839                 }
840             }
841         }
842 
843         size = b->end - b->last;
844 
845         if (size && src->read->ready) {
846             c->log->action = recv_action;
847 
848             n = src->recv(src, b->last, size);
849 
850             if (n == NGX_AGAIN || n == 0) {
851                 break;
852             }
853 
854             if (n > 0) {
855                 do_write = 1;
856                 b->last += n;
857 
858                 continue;
859             }
860 
861             if (n == NGX_ERROR) {
862                 src->read->eof = 1;
863             }
864         }
865 
866         break;
867     }
868 
869     c->log->action = "proxying";
870 
871     if ((s->connection->read->eof && s->buffer->pos == s->buffer->last)
872         || (s->proxy->upstream.connection->read->eof
873             && s->proxy->buffer->pos == s->proxy->buffer->last)
874         || (s->connection->read->eof
875             && s->proxy->upstream.connection->read->eof))
876     {
877         action = c->log->action;
878         c->log->action = NULL;
879         ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxied session done");
880         c->log->action = action;
881 
882         ngx_mail_proxy_close_session(s);
883         return;
884     }
885 
886     if (ngx_handle_write_event(dst->write, 0) == NGX_ERROR) {
887         ngx_mail_proxy_close_session(s);
888         return;
889     }
890 
891     if (ngx_handle_read_event(dst->read, 0) == NGX_ERROR) {
892         ngx_mail_proxy_close_session(s);
893         return;
894     }
895 
896     if (ngx_handle_write_event(src->write, 0) == NGX_ERROR) {
897         ngx_mail_proxy_close_session(s);
898         return;
899     }
900 
901     if (ngx_handle_read_event(src->read, 0) == NGX_ERROR) {
902         ngx_mail_proxy_close_session(s);
903         return;
904     }
905 
906     if (c == s->connection) {
907         pcf = ngx_mail_get_module_srv_conf(s, ngx_mail_proxy_module);
908         ngx_add_timer(c->read, pcf->timeout);
909     }
910 }
911 
912 
913 static void
914 ngx_mail_proxy_upstream_error(ngx_mail_session_t *s)
915 {
916     if (s->proxy->upstream.connection) {
917         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
918                        "close mail proxy connection: %d",
919                        s->proxy->upstream.connection->fd);
920 
921         ngx_close_connection(s->proxy->upstream.connection);
922     }
923 
924     if (s->out.len == 0) {
925         ngx_mail_session_internal_server_error(s);
926         return;
927     }
928 
929     s->quit = 1;
930     ngx_mail_send(s->connection->write);
931 }
932 
933 
934 static void
935 ngx_mail_proxy_internal_server_error(ngx_mail_session_t *s)
936 {
937     if (s->proxy->upstream.connection) {
938         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
939                        "close mail proxy connection: %d",
940                        s->proxy->upstream.connection->fd);
941 
942         ngx_close_connection(s->proxy->upstream.connection);
943     }
944 
945     ngx_mail_session_internal_server_error(s);
946 }
947 
948 
949 static void
950 ngx_mail_proxy_close_session(ngx_mail_session_t *s)
951 {
952     if (s->proxy->upstream.connection) {
953         ngx_log_debug1(NGX_LOG_DEBUG_MAIL, s->connection->log, 0,
954                        "close mail proxy connection: %d",
955                        s->proxy->upstream.connection->fd);
956 
957         ngx_close_connection(s->proxy->upstream.connection);
958     }
959 
960     ngx_mail_close_connection(s->connection);
961 }
962 
963 
964 static void *
965 ngx_mail_proxy_create_conf(ngx_conf_t *cf)
966 {
967     ngx_mail_proxy_conf_t  *pcf;
968 
969     pcf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_proxy_conf_t));
970     if (pcf == NULL) {
971         return NGX_CONF_ERROR;
972     }
973 
974     pcf->enable = NGX_CONF_UNSET;
975     pcf->pass_error_message = NGX_CONF_UNSET;
976     pcf->xclient = NGX_CONF_UNSET;
977     pcf->buffer_size = NGX_CONF_UNSET_SIZE;
978     pcf->timeout = NGX_CONF_UNSET_MSEC;
979 
980     return pcf;
981 }
982 
983 
984 static char *
985 ngx_mail_proxy_merge_conf(ngx_conf_t *cf, void *parent, void *child)
986 {
987     ngx_mail_proxy_conf_t *prev = parent;
988     ngx_mail_proxy_conf_t *conf = child;
989 
990     ngx_conf_merge_value(conf->enable, prev->enable, 0);
991     ngx_conf_merge_value(conf->pass_error_message, prev->pass_error_message, 0);
992     ngx_conf_merge_value(conf->xclient, prev->xclient, 1);
993     ngx_conf_merge_size_value(conf->buffer_size, prev->buffer_size,
994                               (size_t) ngx_pagesize);
995     ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 24 * 60 * 60000);
996 
997     return NGX_CONF_OK;
998 }
999 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.