comparison src/imap/ngx_imap_core_module.c @ 76:da9a3b14312d NGINX_0_1_38

nginx 0.1.38 *) Feature: the "limit_rate" directive is supported in in proxy and FastCGI mode. *) Feature: the "X-Accel-Limit-Rate" response header line is supported in proxy and FastCGI mode. *) Feature: the "break" directive. *) Feature: the "log_not_found" directive. *) Bugfix: the response status code was not changed when request was redirected by the ""X-Accel-Redirect" header line. *) Bugfix: the variables set by the "set" directive could not be used in SSI. *) Bugfix: the segmentation fault may occurred if the SSI page has more than one remote subrequest. *) Bugfix: nginx treated the backend response as invalid if the status line in the header was transferred in two packets; bug appeared in 0.1.29. *) Feature: the "ssi_types" directive. *) Feature: the "autoindex_exact_size" directive. *) Bugfix: the ngx_http_autoindex_module did not support the long file names in UTF-8. *) Feature: the IMAP/POP3 proxy.
author Igor Sysoev <http://sysoev.ru>
date Fri, 08 Jul 2005 00:00:00 +0400
parents
children e916a291e9aa
comparison
equal deleted inserted replaced
75:985847bb65f9 76:da9a3b14312d
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_imap.h>
11
12
13 static void *ngx_imap_core_create_main_conf(ngx_conf_t *cf);
14 static void *ngx_imap_core_create_srv_conf(ngx_conf_t *cf);
15 static char *ngx_imap_core_merge_srv_conf(ngx_conf_t *cf, void *parent,
16 void *child);
17 static char *ngx_imap_core_server(ngx_conf_t *cf, ngx_command_t *cmd,
18 void *conf);
19 static char *ngx_imap_core_listen(ngx_conf_t *cf, ngx_command_t *cmd,
20 void *conf);
21 static char *ngx_imap_core_capability(ngx_conf_t *cf, ngx_command_t *cmd,
22 void *conf);
23
24
25 static ngx_conf_enum_t ngx_imap_core_procotol[] = {
26 { ngx_string("pop3"), NGX_IMAP_POP3_PROTOCOL },
27 { ngx_string("imap"), NGX_IMAP_IMAP_PROTOCOL },
28 { ngx_null_string, 0 }
29 };
30
31
32 static ngx_str_t ngx_pop3_default_capabilities[] = {
33 ngx_string("TOP"),
34 ngx_string("USER"),
35 ngx_string("UIDL"),
36 ngx_null_string
37 };
38
39
40 static ngx_str_t ngx_imap_default_capabilities[] = {
41 ngx_string("IMAP4"),
42 ngx_string("IMAP4rev1"),
43 ngx_string("UIDPLUS"),
44 ngx_null_string
45 };
46
47
48 static ngx_command_t ngx_imap_core_commands[] = {
49
50 { ngx_string("server"),
51 NGX_IMAP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
52 ngx_imap_core_server,
53 0,
54 0,
55 NULL },
56
57 { ngx_string("listen"),
58 NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
59 ngx_imap_core_listen,
60 0,
61 0,
62 NULL },
63
64 { ngx_string("protocol"),
65 NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
66 ngx_conf_set_enum_slot,
67 NGX_IMAP_SRV_CONF_OFFSET,
68 offsetof(ngx_imap_core_srv_conf_t, protocol),
69 &ngx_imap_core_procotol },
70
71 { ngx_string("imap_client_buffer"),
72 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
73 ngx_conf_set_size_slot,
74 NGX_IMAP_SRV_CONF_OFFSET,
75 offsetof(ngx_imap_core_srv_conf_t, imap_client_buffer_size),
76 NULL },
77
78 { ngx_string("proxy_buffer"),
79 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
80 ngx_conf_set_size_slot,
81 NGX_IMAP_SRV_CONF_OFFSET,
82 offsetof(ngx_imap_core_srv_conf_t, proxy_buffer_size),
83 NULL },
84
85 { ngx_string("timeout"),
86 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_TAKE1,
87 ngx_conf_set_msec_slot,
88 NGX_IMAP_SRV_CONF_OFFSET,
89 offsetof(ngx_imap_core_srv_conf_t, timeout),
90 NULL },
91
92 { ngx_string("pop3_capabilities"),
93 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_1MORE,
94 ngx_imap_core_capability,
95 NGX_IMAP_SRV_CONF_OFFSET,
96 offsetof(ngx_imap_core_srv_conf_t, pop3_capabilities),
97 NULL },
98
99 { ngx_string("imap_capabilities"),
100 NGX_IMAP_MAIN_CONF|NGX_IMAP_SRV_CONF|NGX_CONF_1MORE,
101 ngx_imap_core_capability,
102 NGX_IMAP_SRV_CONF_OFFSET,
103 offsetof(ngx_imap_core_srv_conf_t, imap_capabilities),
104 NULL },
105
106 ngx_null_command
107 };
108
109
110 static ngx_imap_module_t ngx_imap_core_module_ctx = {
111 ngx_imap_core_create_main_conf, /* create main configuration */
112 NULL, /* init main configuration */
113
114 ngx_imap_core_create_srv_conf, /* create server configuration */
115 ngx_imap_core_merge_srv_conf /* merge server configuration */
116 };
117
118
119 ngx_module_t ngx_imap_core_module = {
120 NGX_MODULE_V1,
121 &ngx_imap_core_module_ctx, /* module context */
122 ngx_imap_core_commands, /* module directives */
123 NGX_IMAP_MODULE, /* module type */
124 NULL, /* init module */
125 NULL /* init process */
126 };
127
128
129 static void *
130 ngx_imap_core_create_main_conf(ngx_conf_t *cf)
131 {
132 ngx_imap_core_main_conf_t *cmcf;
133
134 cmcf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_core_main_conf_t));
135 if (cmcf == NULL) {
136 return NGX_CONF_ERROR;
137 }
138
139 if (ngx_array_init(&cmcf->servers, cf->pool, 4,
140 sizeof(ngx_imap_core_srv_conf_t *)) == NGX_ERROR)
141 {
142 return NGX_CONF_ERROR;
143 }
144
145 return cmcf;
146 }
147
148
149 static void *
150 ngx_imap_core_create_srv_conf(ngx_conf_t *cf)
151 {
152 ngx_imap_core_srv_conf_t *cscf;
153
154 cscf = ngx_pcalloc(cf->pool, sizeof(ngx_imap_core_srv_conf_t));
155 if (cscf == NULL) {
156 return NULL;
157 }
158
159 cscf->imap_client_buffer_size = NGX_CONF_UNSET_SIZE;
160 cscf->proxy_buffer_size = NGX_CONF_UNSET_SIZE;
161 cscf->timeout = NGX_CONF_UNSET_MSEC;
162 cscf->protocol = NGX_CONF_UNSET_UINT;
163
164 if (ngx_array_init(&cscf->pop3_capabilities, cf->pool, 4, sizeof(ngx_str_t))
165 != NGX_OK)
166 {
167 return NULL;
168 }
169
170 if (ngx_array_init(&cscf->imap_capabilities, cf->pool, 4, sizeof(ngx_str_t))
171 != NGX_OK)
172 {
173 return NULL;
174 }
175
176 return cscf;
177 }
178
179
180 static char *
181 ngx_imap_core_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
182 {
183 ngx_imap_core_srv_conf_t *prev = parent;
184 ngx_imap_core_srv_conf_t *conf = child;
185
186 size_t size;
187 ngx_buf_t *b;
188 ngx_str_t *c, *d;
189 ngx_uint_t i;
190
191 ngx_conf_merge_size_value(conf->imap_client_buffer_size,
192 prev->imap_client_buffer_size,
193 (size_t) ngx_pagesize);
194 ngx_conf_merge_size_value(conf->proxy_buffer_size, prev->proxy_buffer_size,
195 (size_t) ngx_pagesize);
196 ngx_conf_merge_msec_value(conf->timeout, prev->timeout, 60000);
197 ngx_conf_merge_unsigned_value(conf->protocol, prev->protocol,
198 NGX_IMAP_IMAP_PROTOCOL);
199
200
201 if (conf->pop3_capabilities.nelts == 0) {
202 conf->pop3_capabilities = prev->pop3_capabilities;
203 }
204
205 if (conf->pop3_capabilities.nelts == 0) {
206
207 for (d = ngx_pop3_default_capabilities; d->len; d++) {
208 c = ngx_array_push(&conf->pop3_capabilities);
209 if (c == NULL) {
210 return NGX_CONF_ERROR;
211 }
212
213 *c = *d;
214 }
215 }
216
217 size = sizeof("+OK Capability list follows" CRLF) - 1
218 + sizeof("." CRLF) - 1;
219
220 c = conf->pop3_capabilities.elts;
221 for (i = 0; i < conf->pop3_capabilities.nelts; i++) {
222 size += c[i].len + sizeof(CRLF) - 1;
223 }
224
225 b = ngx_create_temp_buf(cf->pool, size);
226 if (b == NULL) {
227 return NGX_CONF_ERROR;
228 }
229
230 b->last = ngx_cpymem(b->last, "+OK Capability list follows" CRLF,
231 sizeof("+OK Capability list follows" CRLF) - 1);
232
233 for (i = 0; i < conf->pop3_capabilities.nelts; i++) {
234 b->last = ngx_cpymem(b->last, c[i].data, c[i].len);
235 *b->last++ = CR; *b->last++ = LF;
236 }
237
238 *b->last++ = '.'; *b->last++ = CR; *b->last++ = LF;
239
240 conf->pop3_capability = b;
241
242
243 if (conf->imap_capabilities.nelts == 0) {
244 conf->imap_capabilities = prev->imap_capabilities;
245 }
246
247 if (conf->imap_capabilities.nelts == 0) {
248
249 for (d = ngx_imap_default_capabilities; d->len; d++) {
250 c = ngx_array_push(&conf->imap_capabilities);
251 if (c == NULL) {
252 return NGX_CONF_ERROR;
253 }
254
255 *c = *d;
256 }
257 }
258
259 size = sizeof("* CAPABILITY") - 1 + sizeof(CRLF) - 1;
260
261 c = conf->imap_capabilities.elts;
262 for (i = 0; i < conf->imap_capabilities.nelts; i++) {
263 size += 1 + c[i].len;
264 }
265
266 b = ngx_create_temp_buf(cf->pool, size);
267 if (b == NULL) {
268 return NGX_CONF_ERROR;
269 }
270
271 b->last = ngx_cpymem(b->last, "* CAPABILITY", sizeof("* CAPABILITY") - 1);
272
273 for (i = 0; i < conf->imap_capabilities.nelts; i++) {
274 *b->last++ = ' ';
275 b->last = ngx_cpymem(b->last, c[i].data, c[i].len);
276 }
277
278 *b->last++ = CR; *b->last++ = LF;
279
280 conf->imap_capability = b;
281
282 return NGX_CONF_OK;
283 }
284
285
286 static char *
287 ngx_imap_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
288 {
289 char *rv;
290 void *mconf;
291 ngx_uint_t m;
292 ngx_conf_t pcf;
293 ngx_imap_module_t *module;
294 ngx_imap_conf_ctx_t *ctx, *imap_ctx;
295 ngx_imap_core_srv_conf_t *cscf, **cscfp;
296 ngx_imap_core_main_conf_t *cmcf;
297
298
299 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_imap_conf_ctx_t));
300 if (ctx == NULL) {
301 return NGX_CONF_ERROR;
302 }
303
304 imap_ctx = cf->ctx;
305 ctx->main_conf = imap_ctx->main_conf;
306
307 /* the server{}'s srv_conf */
308
309 ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_imap_max_module);
310 if (ctx->srv_conf == NULL) {
311 return NGX_CONF_ERROR;
312 }
313
314 for (m = 0; ngx_modules[m]; m++) {
315 if (ngx_modules[m]->type != NGX_IMAP_MODULE) {
316 continue;
317 }
318
319 module = ngx_modules[m]->ctx;
320
321 if (module->create_srv_conf) {
322 mconf = module->create_srv_conf(cf);
323 if (mconf == NULL) {
324 return NGX_CONF_ERROR;
325 }
326
327 ctx->srv_conf[ngx_modules[m]->ctx_index] = mconf;
328 }
329 }
330
331 /* the server configuration context */
332
333 cscf = ctx->srv_conf[ngx_imap_core_module.ctx_index];
334 cscf->ctx = ctx;
335
336 cmcf = ctx->main_conf[ngx_imap_core_module.ctx_index];
337
338 cscfp = ngx_array_push(&cmcf->servers);
339 if (cscfp == NULL) {
340 return NGX_CONF_ERROR;
341 }
342
343 *cscfp = cscf;
344
345
346 /* parse inside server{} */
347
348 pcf = *cf;
349 cf->ctx = ctx;
350 cf->cmd_type = NGX_IMAP_SRV_CONF;
351
352 rv = ngx_conf_parse(cf, NULL);
353
354 *cf = pcf;
355
356 return rv;
357 }
358
359
360 /* AF_INET only */
361
362 static char *
363 ngx_imap_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
364 {
365 char *err;
366 ngx_str_t *value;
367 in_addr_t in_addr;
368 struct hostent *h;
369 ngx_listening_t *ls;
370 ngx_inet_upstream_t inet_upstream;
371
372 value = cf->args->elts;
373
374 ngx_memzero(&inet_upstream, sizeof(ngx_inet_upstream_t));
375
376 inet_upstream.url = value[1];
377 inet_upstream.port_only = 1;
378
379 err = ngx_inet_parse_host_port(&inet_upstream);
380
381 if (err) {
382 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
383 "%s in \"%V\" of the \"listen\" directive",
384 err, &inet_upstream.url);
385 return NGX_CONF_ERROR;
386 }
387
388 if (inet_upstream.host.len) {
389 inet_upstream.host.data[inet_upstream.host.len] = '\0';
390
391 in_addr = inet_addr((const char *) inet_upstream.host.data);
392
393 if (in_addr == INADDR_NONE) {
394 h = gethostbyname((const char *) inet_upstream.host.data);
395
396 if (h == NULL || h->h_addr_list[0] == NULL) {
397 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
398 "can not resolve host \"%s\" "
399 "in the \"listen\" directive",
400 inet_upstream.host.data);
401 return NGX_CONF_ERROR;
402 }
403
404 in_addr = *(in_addr_t *)(h->h_addr_list[0]);
405 }
406
407 } else {
408 in_addr = INADDR_ANY;
409 }
410
411
412 ls = ngx_listening_inet_stream_socket(cf, in_addr, inet_upstream.port);
413 if (ls == NULL) {
414 return NGX_CONF_ERROR;
415 }
416
417 ls->backlog = -1;
418 ls->addr_ntop = 1;
419 ls->handler = ngx_imap_init_connection;
420 ls->pool_size = 256;
421
422 ls->ctx = cf->ctx;
423
424 /* STUB */
425 ls->log = cf->cycle->new_log;
426 /**/
427
428 return NGX_CONF_OK;
429 }
430
431
432 static char *
433 ngx_imap_core_capability(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
434 {
435 char *p = conf;
436
437 ngx_str_t *c, *value;
438 ngx_uint_t i;
439 ngx_array_t *a;
440
441 a = (ngx_array_t *) (p + cmd->offset);
442
443 value = cf->args->elts;
444
445 for (i = 1; i < cf->args->nelts; i++) {
446 c = ngx_array_push(a);
447 if (c == NULL) {
448 return NGX_CONF_ERROR;
449 }
450
451 *c = value[i];
452 }
453
454 return NGX_CONF_OK;
455 }