3
* Copyright (C) Igor Sysoev
7
#include <ngx_config.h>
11
#include <ngx_mail_pop3_module.h>
14
static void *ngx_mail_pop3_create_srv_conf(ngx_conf_t *cf);
15
static char *ngx_mail_pop3_merge_srv_conf(ngx_conf_t *cf, void *parent,
19
static ngx_str_t ngx_mail_pop3_default_capabilities[] = {
27
static ngx_conf_bitmask_t ngx_mail_pop3_auth_methods[] = {
28
{ ngx_string("plain"), NGX_MAIL_AUTH_PLAIN_ENABLED },
29
{ ngx_string("apop"), NGX_MAIL_AUTH_APOP_ENABLED },
30
{ ngx_string("cram-md5"), NGX_MAIL_AUTH_CRAM_MD5_ENABLED },
31
{ ngx_null_string, 0 }
35
static ngx_str_t ngx_mail_pop3_auth_plain_capability =
36
ngx_string("+OK methods supported:" CRLF
42
static ngx_str_t ngx_mail_pop3_auth_cram_md5_capability =
43
ngx_string("+OK methods supported:" CRLF
50
static ngx_mail_protocol_t ngx_mail_pop3_protocol = {
53
NGX_MAIL_POP3_PROTOCOL,
55
ngx_mail_pop3_init_session,
56
ngx_mail_pop3_init_protocol,
57
ngx_mail_pop3_parse_command,
58
ngx_mail_pop3_auth_state,
60
ngx_string("-ERR internal server error" CRLF)
64
static ngx_command_t ngx_mail_pop3_commands[] = {
66
{ ngx_string("pop3_capabilities"),
67
NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
68
ngx_mail_capabilities,
69
NGX_MAIL_SRV_CONF_OFFSET,
70
offsetof(ngx_mail_pop3_srv_conf_t, capabilities),
73
{ ngx_string("pop3_auth"),
74
NGX_MAIL_MAIN_CONF|NGX_MAIL_SRV_CONF|NGX_CONF_1MORE,
75
ngx_conf_set_bitmask_slot,
76
NGX_MAIL_SRV_CONF_OFFSET,
77
offsetof(ngx_mail_pop3_srv_conf_t, auth_methods),
78
&ngx_mail_pop3_auth_methods },
84
static ngx_mail_module_t ngx_mail_pop3_module_ctx = {
85
&ngx_mail_pop3_protocol, /* protocol */
87
NULL, /* create main configuration */
88
NULL, /* init main configuration */
90
ngx_mail_pop3_create_srv_conf, /* create server configuration */
91
ngx_mail_pop3_merge_srv_conf /* merge server configuration */
95
ngx_module_t ngx_mail_pop3_module = {
97
&ngx_mail_pop3_module_ctx, /* module context */
98
ngx_mail_pop3_commands, /* module directives */
99
NGX_MAIL_MODULE, /* module type */
100
NULL, /* init master */
101
NULL, /* init module */
102
NULL, /* init process */
103
NULL, /* init thread */
104
NULL, /* exit thread */
105
NULL, /* exit process */
106
NULL, /* exit master */
107
NGX_MODULE_V1_PADDING
112
ngx_mail_pop3_create_srv_conf(ngx_conf_t *cf)
114
ngx_mail_pop3_srv_conf_t *pscf;
116
pscf = ngx_pcalloc(cf->pool, sizeof(ngx_mail_pop3_srv_conf_t));
121
if (ngx_array_init(&pscf->capabilities, cf->pool, 4, sizeof(ngx_str_t))
132
ngx_mail_pop3_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
134
ngx_mail_pop3_srv_conf_t *prev = parent;
135
ngx_mail_pop3_srv_conf_t *conf = child;
138
size_t size, stls_only_size;
142
ngx_conf_merge_bitmask_value(conf->auth_methods,
144
(NGX_CONF_BITMASK_SET
145
|NGX_MAIL_AUTH_PLAIN_ENABLED));
147
if (conf->capabilities.nelts == 0) {
148
conf->capabilities = prev->capabilities;
151
if (conf->capabilities.nelts == 0) {
153
for (d = ngx_mail_pop3_default_capabilities; d->len; d++) {
154
c = ngx_array_push(&conf->capabilities);
156
return NGX_CONF_ERROR;
163
size = sizeof("+OK Capability list follows" CRLF) - 1
164
+ sizeof("." CRLF) - 1;
166
stls_only_size = size + sizeof("STLS" CRLF) - 1;
168
c = conf->capabilities.elts;
169
for (i = 0; i < conf->capabilities.nelts; i++) {
170
size += c[i].len + sizeof(CRLF) - 1;
172
if (ngx_strcasecmp(c[i].data, (u_char *) "USER") == 0) {
176
stls_only_size += c[i].len + sizeof(CRLF) - 1;
179
if (conf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED) {
180
size += sizeof("SASL LOGIN PLAIN CRAM-MD5" CRLF) - 1;
183
size += sizeof("SASL LOGIN PLAIN" CRLF) - 1;
186
p = ngx_palloc(cf->pool, size);
188
return NGX_CONF_ERROR;
191
conf->capability.len = size;
192
conf->capability.data = p;
194
p = ngx_cpymem(p, "+OK Capability list follows" CRLF,
195
sizeof("+OK Capability list follows" CRLF) - 1);
197
for (i = 0; i < conf->capabilities.nelts; i++) {
198
p = ngx_cpymem(p, c[i].data, c[i].len);
199
*p++ = CR; *p++ = LF;
202
if (conf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED) {
203
p = ngx_cpymem(p, "SASL LOGIN PLAIN CRAM-MD5" CRLF,
204
sizeof("SASL LOGIN PLAIN CRAM-MD5" CRLF) - 1);
207
p = ngx_cpymem(p, "SASL LOGIN PLAIN" CRLF,
208
sizeof("SASL LOGIN PLAIN" CRLF) - 1);
211
*p++ = '.'; *p++ = CR; *p = LF;
214
size += sizeof("STLS" CRLF) - 1;
216
p = ngx_palloc(cf->pool, size);
218
return NGX_CONF_ERROR;
221
conf->starttls_capability.len = size;
222
conf->starttls_capability.data = p;
224
p = ngx_cpymem(p, conf->capability.data,
225
conf->capability.len - (sizeof("." CRLF) - 1));
227
p = ngx_cpymem(p, "STLS" CRLF, sizeof("STLS" CRLF) - 1);
228
*p++ = '.'; *p++ = CR; *p = LF;
231
if (conf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED) {
232
conf->auth_capability = ngx_mail_pop3_auth_cram_md5_capability;
235
conf->auth_capability = ngx_mail_pop3_auth_plain_capability;
239
p = ngx_palloc(cf->pool, stls_only_size);
241
return NGX_CONF_ERROR;
244
conf->starttls_only_capability.len = stls_only_size;
245
conf->starttls_only_capability.data = p;
247
p = ngx_cpymem(p, "+OK Capability list follows" CRLF,
248
sizeof("+OK Capability list follows" CRLF) - 1);
250
for (i = 0; i < conf->capabilities.nelts; i++) {
251
if (ngx_strcasecmp(c[i].data, (u_char *) "USER") == 0) {
255
p = ngx_cpymem(p, c[i].data, c[i].len);
256
*p++ = CR; *p++ = LF;
259
p = ngx_cpymem(p, "STLS" CRLF, sizeof("STLS" CRLF) - 1);
260
*p++ = '.'; *p++ = CR; *p = LF;