6 | 6 |
|
7 | 7 |
|
8 | 8 |
static void ngx_imap_proxy_block_read(ngx_event_t *rev);
|
9 | |
static void ngx_imap_proxy_greeting_handler(ngx_event_t *rev);
|
|
9 |
static void ngx_imap_proxy_auth_handler(ngx_event_t *rev);
|
10 | 10 |
static void ngx_imap_proxy_init_handler(ngx_event_t *wev);
|
11 | 11 |
static void ngx_imap_proxy_dummy_handler(ngx_event_t *ev);
|
12 | 12 |
static ngx_int_t ngx_imap_proxy_read_response(ngx_imap_session_t *s);
|
|
56 | 56 |
p->upstream.connection->pool = s->connection->pool;
|
57 | 57 |
|
58 | 58 |
s->connection->read->event_handler = ngx_imap_proxy_block_read;
|
59 | |
p->upstream.connection->read->event_handler =
|
60 | |
ngx_imap_proxy_greeting_handler;
|
|
59 |
p->upstream.connection->read->event_handler = ngx_imap_proxy_auth_handler;
|
61 | 60 |
p->upstream.connection->write->event_handler = ngx_imap_proxy_dummy_handler;
|
62 | 61 |
}
|
63 | 62 |
|
|
78 | 77 |
}
|
79 | 78 |
|
80 | 79 |
|
81 | |
static void ngx_imap_proxy_greeting_handler(ngx_event_t *rev)
|
82 | |
{
|
|
80 |
static void ngx_imap_proxy_auth_handler(ngx_event_t *rev)
|
|
81 |
{
|
|
82 |
u_char *p;
|
83 | 83 |
ngx_int_t rc;
|
84 | |
ngx_buf_t *b;
|
|
84 |
ngx_str_t line;
|
85 | 85 |
ngx_connection_t *c;
|
86 | 86 |
ngx_imap_session_t *s;
|
87 | 87 |
|
88 | |
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0,
|
89 | |
"imap proxy greeting handler");
|
|
88 |
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy auth handler");
|
90 | 89 |
|
91 | 90 |
c = rev->data;
|
92 | 91 |
s = c->data;
|
|
105 | 104 |
return;
|
106 | 105 |
}
|
107 | 106 |
|
108 | |
if (rc == NGX_OK) {
|
109 | |
s->connection->read->event_handler = ngx_imap_proxy_handler;
|
110 | |
s->connection->write->event_handler = ngx_imap_proxy_handler;
|
111 | |
rev->event_handler = ngx_imap_proxy_handler;
|
112 | |
c->write->event_handler = ngx_imap_proxy_handler;
|
113 | |
|
114 | |
b = s->proxy->buffer;
|
115 | |
b->pos = b->start;
|
116 | |
b->last = b->start;
|
117 | |
|
118 | |
if (ngx_handle_read_event(s->connection->read, 0) == NGX_ERROR) {
|
|
107 |
if (rc == NGX_ERROR) {
|
|
108 |
/* TODO: ngx_imap_proxy_finalize_session(s, NGX_IMAP_INTERNAL_ERROR) */
|
|
109 |
ngx_imap_proxy_close_session(s);
|
|
110 |
return;
|
|
111 |
}
|
|
112 |
|
|
113 |
if (s->imap_state == ngx_pop3_start) {
|
|
114 |
|
|
115 |
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send user");
|
|
116 |
|
|
117 |
line.len = sizeof("USER ") + s->login.len - 1 + 2;
|
|
118 |
if (!(line.data = ngx_palloc(c->pool, line.len))) {
|
119 | 119 |
ngx_imap_proxy_close_session(s);
|
120 | 120 |
return;
|
121 | 121 |
}
|
122 | 122 |
|
123 | |
if (s->connection->read->ready) {
|
124 | |
ngx_imap_proxy_handler(s->connection->read);
|
125 | |
}
|
126 | |
|
127 | |
return;
|
128 | |
}
|
129 | |
|
130 | |
/* TODO: ngx_imap_proxy_finalize_session(s, NGX_IMAP_INTERNAL_ERROR) */
|
131 | |
ngx_imap_proxy_close_session(s);
|
|
123 |
p = ngx_cpymem(line.data, "USER ", sizeof("USER ") - 1);
|
|
124 |
p = ngx_cpymem(p, s->login.data, s->login.len);
|
|
125 |
*p++ = CR; *p++ = LF;
|
|
126 |
|
|
127 |
if (ngx_send(c, line.data, line.len) < (ssize_t) line.len) {
|
|
128 |
/*
|
|
129 |
* we treat the incomplete sending as NGX_ERROR
|
|
130 |
* because it is very strange here
|
|
131 |
*/
|
|
132 |
ngx_imap_close_connection(c);
|
|
133 |
return;
|
|
134 |
}
|
|
135 |
|
|
136 |
s->imap_state = ngx_pop3_user;
|
|
137 |
|
|
138 |
s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
139 |
s->proxy->buffer->last = s->proxy->buffer->start;
|
|
140 |
|
|
141 |
return;
|
|
142 |
}
|
|
143 |
|
|
144 |
ngx_log_debug0(NGX_LOG_DEBUG_IMAP, rev->log, 0, "imap proxy send pass");
|
|
145 |
|
|
146 |
line.len = sizeof("PASS ") + s->passwd.len - 1 + 2;
|
|
147 |
if (!(line.data = ngx_palloc(c->pool, line.len))) {
|
|
148 |
ngx_imap_proxy_close_session(s);
|
|
149 |
return;
|
|
150 |
}
|
|
151 |
|
|
152 |
p = ngx_cpymem(line.data, "PASS ", sizeof("PASS ") - 1);
|
|
153 |
p = ngx_cpymem(p, s->passwd.data, s->passwd.len);
|
|
154 |
*p++ = CR; *p++ = LF;
|
|
155 |
|
|
156 |
if (ngx_send(c, line.data, line.len) < (ssize_t) line.len) {
|
|
157 |
/*
|
|
158 |
* we treat the incomplete sending as NGX_ERROR
|
|
159 |
* because it is very strange here
|
|
160 |
*/
|
|
161 |
ngx_imap_close_connection(c);
|
|
162 |
return;
|
|
163 |
}
|
|
164 |
|
|
165 |
s->proxy->buffer->pos = s->proxy->buffer->start;
|
|
166 |
s->proxy->buffer->last = s->proxy->buffer->start;
|
|
167 |
|
|
168 |
s->connection->read->event_handler = ngx_imap_proxy_handler;
|
|
169 |
s->connection->write->event_handler = ngx_imap_proxy_handler;
|
|
170 |
rev->event_handler = ngx_imap_proxy_handler;
|
|
171 |
c->write->event_handler = ngx_imap_proxy_handler;
|
132 | 172 |
}
|
133 | 173 |
|
134 | 174 |
|