Klaus Demo nginx / a747089
Mail: extensible auth methods in pop3 module. Maxim Dounin 5 years ago
1 changed file(s) with 71 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
3232 };
3333
3434
35 static ngx_str_t ngx_mail_pop3_auth_plain_capability =
36 ngx_string("+OK methods supported:" CRLF
37 "LOGIN" CRLF
38 "PLAIN" CRLF
39 "." CRLF);
40
41
42 static ngx_str_t ngx_mail_pop3_auth_cram_md5_capability =
43 ngx_string("+OK methods supported:" CRLF
44 "LOGIN" CRLF
45 "PLAIN" CRLF
46 "CRAM-MD5" CRLF
47 "." CRLF);
35 static ngx_str_t ngx_mail_pop3_auth_methods_names[] = {
36 ngx_string("PLAIN"),
37 ngx_string("LOGIN"),
38 ngx_null_string, /* APOP */
39 ngx_string("CRAM-MD5"),
40 ngx_null_string /* NONE */
41 };
4842
4943
5044 static ngx_mail_protocol_t ngx_mail_pop3_protocol = {
139133 u_char *p;
140134 size_t size, stls_only_size;
141135 ngx_str_t *c, *d;
142 ngx_uint_t i;
136 ngx_uint_t i, m;
143137
144138 ngx_conf_merge_bitmask_value(conf->auth_methods,
145139 prev->auth_methods,
146140 (NGX_CONF_BITMASK_SET
147141 |NGX_MAIL_AUTH_PLAIN_ENABLED));
142
143 if (conf->auth_methods & NGX_MAIL_AUTH_PLAIN_ENABLED) {
144 conf->auth_methods |= NGX_MAIL_AUTH_LOGIN_ENABLED;
145 }
148146
149147 if (conf->capabilities.nelts == 0) {
150148 conf->capabilities = prev->capabilities;
178176 stls_only_size += c[i].len + sizeof(CRLF) - 1;
179177 }
180178
181 if (conf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED) {
182 size += sizeof("SASL LOGIN PLAIN CRAM-MD5" CRLF) - 1;
183
184 } else {
185 size += sizeof("SASL LOGIN PLAIN" CRLF) - 1;
179 size += sizeof("SASL") - 1 + sizeof(CRLF) - 1;
180
181 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
182 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
183 m <<= 1, i++)
184 {
185 if (m & conf->auth_methods) {
186 size += 1 + ngx_mail_pop3_auth_methods_names[i].len;
187 }
186188 }
187189
188190 p = ngx_pnalloc(cf->pool, size);
201203 *p++ = CR; *p++ = LF;
202204 }
203205
204 if (conf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED) {
205 p = ngx_cpymem(p, "SASL LOGIN PLAIN CRAM-MD5" CRLF,
206 sizeof("SASL LOGIN PLAIN CRAM-MD5" CRLF) - 1);
207
208 } else {
209 p = ngx_cpymem(p, "SASL LOGIN PLAIN" CRLF,
210 sizeof("SASL LOGIN PLAIN" CRLF) - 1);
211 }
206 p = ngx_cpymem(p, "SASL", sizeof("SASL") - 1);
207
208 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
209 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
210 m <<= 1, i++)
211 {
212 if (m & conf->auth_methods) {
213 *p++ = ' ';
214 p = ngx_cpymem(p, ngx_mail_pop3_auth_methods_names[i].data,
215 ngx_mail_pop3_auth_methods_names[i].len);
216 }
217 }
218
219 *p++ = CR; *p++ = LF;
212220
213221 *p++ = '.'; *p++ = CR; *p = LF;
214222
230238 *p++ = '.'; *p++ = CR; *p = LF;
231239
232240
233 if (conf->auth_methods & NGX_MAIL_AUTH_CRAM_MD5_ENABLED) {
234 conf->auth_capability = ngx_mail_pop3_auth_cram_md5_capability;
235
236 } else {
237 conf->auth_capability = ngx_mail_pop3_auth_plain_capability;
238 }
241 size = sizeof("+OK methods supported:" CRLF) - 1
242 + sizeof("." CRLF) - 1;
243
244 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
245 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
246 m <<= 1, i++)
247 {
248 if (m & conf->auth_methods) {
249 size += ngx_mail_pop3_auth_methods_names[i].len
250 + sizeof(CRLF) - 1;
251 }
252 }
253
254 p = ngx_pnalloc(cf->pool, size);
255 if (p == NULL) {
256 return NGX_CONF_ERROR;
257 }
258
259 conf->auth_capability.data = p;
260 conf->auth_capability.len = size;
261
262 p = ngx_cpymem(p, "+OK methods supported:" CRLF,
263 sizeof("+OK methods supported:" CRLF) - 1);
264
265 for (m = NGX_MAIL_AUTH_PLAIN_ENABLED, i = 0;
266 m <= NGX_MAIL_AUTH_CRAM_MD5_ENABLED;
267 m <<= 1, i++)
268 {
269 if (m & conf->auth_methods) {
270 p = ngx_cpymem(p, ngx_mail_pop3_auth_methods_names[i].data,
271 ngx_mail_pop3_auth_methods_names[i].len);
272 *p++ = CR; *p++ = LF;
273 }
274 }
275
276 *p++ = '.'; *p++ = CR; *p = LF;
239277
240278
241279 p = ngx_pnalloc(cf->pool, stls_only_size);