Klaus Demo nginx / 32e5771
nginx-0.0.10-2004-09-12-00:22:11 import Igor Sysoev 17 years ago
5 changed file(s) with 113 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
219219
220220 /*
221221 * The event filter is deleted after a notification without an additional
222 * syscall - select, poll, kqueue, epoll.
222 * syscall - select, poll, kqueue, epoll, Solaris 10's event ports.
223223 */
224224 #define NGX_USE_ONESHOT_EVENT 0x00000002
225225
2929 ngx_imap_state_e imap_state;
3030
3131 ngx_imap_proxy_ctx_t *proxy;
32
33 ngx_str_t login;
34 ngx_str_t passwd;
3235
3336 ngx_uint_t command;
3437 ngx_array_t args;
6868 s->connection = c;
6969
7070 if (ngx_array_init(&s->args, c->pool, 2, sizeof(ngx_str_t)) == NGX_ERROR) {
71 ngx_imap_close_connection(s->connection);
71 ngx_imap_close_connection(c);
7272 return;
7373 }
7474
7575 size = /* STUB: pop3: 128, imap: configurable 4K default */ 128;
7676
77 s->buffer = ngx_create_temp_buf(s->connection->pool, size);
77 s->buffer = ngx_create_temp_buf(c->pool, size);
7878 if (s->buffer == NULL) {
79 ngx_imap_close_connection(s->connection);
79 ngx_imap_close_connection(c);
8080 return;
8181 }
8282
8888
8989 static void ngx_pop3_auth_state(ngx_event_t *rev)
9090 {
91 ngx_uint_t quit;
9291 u_char *text;
9392 ssize_t size;
9493 ngx_int_t rc;
94 ngx_uint_t quit;
95 ngx_str_t *arg;
9596 ngx_connection_t *c;
9697 ngx_imap_session_t *s;
9798
122123 case NGX_POP3_USER:
123124 if (s->args.nelts == 1) {
124125 s->imap_state = ngx_pop3_user;
126
127 arg = s->args.elts;
128 s->login.len = arg[0].len;
129 s->login.data = ngx_palloc(c->pool, s->login.len + 1);
130 if (s->login.data == NULL) {
131 ngx_imap_close_connection(c);
132 return;
133 }
134
135 ngx_cpystrn(s->login.data, arg[0].data, s->login.len + 1);
136
137 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
138 "pop3 login: \"%s\"", s->login.data);
139
125140 } else {
126141 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
127142 }
147162 case NGX_POP3_PASS:
148163 if (s->args.nelts == 1) {
149164 /* STUB */ s->imap_state = ngx_pop3_start;
165
166 arg = s->args.elts;
167 s->passwd.len = arg[0].len;
168 s->passwd.data = ngx_palloc(c->pool, s->passwd.len + 1);
169 if (s->passwd.data == NULL) {
170 ngx_imap_close_connection(c);
171 return;
172 }
173
174 ngx_cpystrn(s->passwd.data, arg[0].data, s->passwd.len + 1);
175
176 ngx_log_debug1(NGX_LOG_DEBUG_IMAP, c->log, 0,
177 "pop3 passwd: \"%s\"", s->passwd.data);
178
179 s->buffer->pos = s->buffer->start;
180 s->buffer->last = s->buffer->start;
181
182 ngx_imap_proxy_init(s);
183
184 return;
185
150186 } else {
151187 rc = NGX_IMAP_PARSE_INVALID_COMMAND;
152188 }
119119 if (!(arg = ngx_array_push(&s->args))) {
120120 return NGX_ERROR;
121121 }
122 arg->len = p - s->arg_start;
122 arg->len = p - 1 - s->arg_start;
123123 arg->data = s->arg_start;
124124 s->arg_start = NULL;
125125
66
77
88 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);
1010 static void ngx_imap_proxy_init_handler(ngx_event_t *wev);
1111 static void ngx_imap_proxy_dummy_handler(ngx_event_t *ev);
1212 static ngx_int_t ngx_imap_proxy_read_response(ngx_imap_session_t *s);
5656 p->upstream.connection->pool = s->connection->pool;
5757
5858 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;
6160 p->upstream.connection->write->event_handler = ngx_imap_proxy_dummy_handler;
6261 }
6362
7877 }
7978
8079
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;
8383 ngx_int_t rc;
84 ngx_buf_t *b;
84 ngx_str_t line;
8585 ngx_connection_t *c;
8686 ngx_imap_session_t *s;
8787
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");
9089
9190 c = rev->data;
9291 s = c->data;
105104 return;
106105 }
107106
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))) {
119119 ngx_imap_proxy_close_session(s);
120120 return;
121121 }
122122
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;
132172 }
133173
134174