root / branches / lighttpd-1.4.x / src / http_auth.c @ 1875
History | View | Annotate | Download (28.8 KB)
| 1 | #ifdef HAVE_CONFIG_H
|
|---|---|
| 2 | #include "config.h" |
| 3 | #endif
|
| 4 | |
| 5 | #ifdef HAVE_CRYPT_H
|
| 6 | # include <crypt.h> |
| 7 | #elif defined(__linux__)
|
| 8 | /* linux needs _XOPEN_SOURCE */
|
| 9 | # define _XOPEN_SOURCE
|
| 10 | #endif
|
| 11 | |
| 12 | #ifdef HAVE_LIBCRYPT
|
| 13 | # define HAVE_CRYPT
|
| 14 | #endif
|
| 15 | |
| 16 | #include <sys/types.h> |
| 17 | #include <sys/stat.h> |
| 18 | |
| 19 | #include <fcntl.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <time.h> |
| 24 | #include <errno.h> |
| 25 | #include <unistd.h> |
| 26 | #include <ctype.h> |
| 27 | |
| 28 | #include "server.h" |
| 29 | #include "log.h" |
| 30 | #include "http_auth.h" |
| 31 | #include "http_auth_digest.h" |
| 32 | #include "stream.h" |
| 33 | |
| 34 | #ifdef USE_OPENSSL
|
| 35 | # include <openssl/md5.h> |
| 36 | #else
|
| 37 | # include "md5.h" |
| 38 | #endif
|
| 39 | |
| 40 | /**
|
| 41 | * the $apr1$ handling is taken from apache 1.3.x |
| 42 | */ |
| 43 | |
| 44 | /*
|
| 45 | * The apr_md5_encode() routine uses much code obtained from the FreeBSD 3.0 |
| 46 | * MD5 crypt() function, which is licenced as follows: |
| 47 | * ---------------------------------------------------------------------------- |
| 48 | * "THE BEER-WARE LICENSE" (Revision 42): |
| 49 | * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you |
| 50 | * can do whatever you want with this stuff. If we meet some day, and you think |
| 51 | * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp |
| 52 | * ---------------------------------------------------------------------------- |
| 53 | */ |
| 54 | |
| 55 | handler_t auth_ldap_init(server *srv, mod_auth_plugin_config *s); |
| 56 | |
| 57 | static const char base64_pad = '='; |
| 58 | |
| 59 | static const short base64_reverse_table[256] = { |
| 60 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 61 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 62 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, |
| 63 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, |
| 64 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 65 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
| 66 | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 67 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
| 68 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 69 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 70 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 71 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 72 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 73 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 74 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | static unsigned char * base64_decode(buffer *out, const char *in) { |
| 79 | unsigned char *result; |
| 80 | int ch, j = 0, k; |
| 81 | size_t i; |
| 82 | |
| 83 | size_t in_len = strlen(in); |
| 84 | |
| 85 | buffer_prepare_copy(out, in_len); |
| 86 | |
| 87 | result = (unsigned char *)out->ptr; |
| 88 | |
| 89 | ch = in[0];
|
| 90 | /* run through the whole string, converting as we go */
|
| 91 | for (i = 0; i < in_len; i++) { |
| 92 | ch = in[i]; |
| 93 | |
| 94 | if (ch == '\0') break; |
| 95 | |
| 96 | if (ch == base64_pad) break; |
| 97 | |
| 98 | ch = base64_reverse_table[ch]; |
| 99 | if (ch < 0) continue; |
| 100 | |
| 101 | switch(i % 4) { |
| 102 | case 0: |
| 103 | result[j] = ch << 2;
|
| 104 | break;
|
| 105 | case 1: |
| 106 | result[j++] |= ch >> 4;
|
| 107 | result[j] = (ch & 0x0f) << 4; |
| 108 | break;
|
| 109 | case 2: |
| 110 | result[j++] |= ch >>2;
|
| 111 | result[j] = (ch & 0x03) << 6; |
| 112 | break;
|
| 113 | case 3: |
| 114 | result[j++] |= ch; |
| 115 | break;
|
| 116 | } |
| 117 | } |
| 118 | k = j; |
| 119 | /* mop things up if we ended on a boundary */
|
| 120 | if (ch == base64_pad) {
|
| 121 | switch(i % 4) { |
| 122 | case 0: |
| 123 | case 1: |
| 124 | return NULL; |
| 125 | case 2: |
| 126 | k++; |
| 127 | case 3: |
| 128 | result[k++] = 0;
|
| 129 | } |
| 130 | } |
| 131 | result[k] = '\0';
|
| 132 | |
| 133 | out->used = k; |
| 134 | |
| 135 | return result;
|
| 136 | } |
| 137 | |
| 138 | static int http_auth_get_password(server *srv, mod_auth_plugin_data *p, buffer *username, buffer *realm, buffer *password) { |
| 139 | int ret = -1; |
| 140 | |
| 141 | if (!username->used|| !realm->used) return -1; |
| 142 | |
| 143 | if (p->conf.auth_backend == AUTH_BACKEND_HTDIGEST) {
|
| 144 | stream f; |
| 145 | char * f_line;
|
| 146 | |
| 147 | if (buffer_is_empty(p->conf.auth_htdigest_userfile)) return -1; |
| 148 | |
| 149 | if (0 != stream_open(&f, p->conf.auth_htdigest_userfile)) { |
| 150 | log_error_write(srv, __FILE__, __LINE__, "sbss", "opening digest-userfile", p->conf.auth_htdigest_userfile, "failed:", strerror(errno)); |
| 151 | |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | f_line = f.start; |
| 156 | |
| 157 | while (f_line - f.start != f.size) {
|
| 158 | char *f_user, *f_pwd, *e, *f_realm;
|
| 159 | size_t u_len, pwd_len, r_len; |
| 160 | |
| 161 | f_user = f_line; |
| 162 | |
| 163 | /*
|
| 164 | * htdigest format |
| 165 | * |
| 166 | * user:realm:md5(user:realm:password) |
| 167 | */ |
| 168 | |
| 169 | if (NULL == (f_realm = memchr(f_user, ':', f.size - (f_user - f.start) ))) { |
| 170 | log_error_write(srv, __FILE__, __LINE__, "sbs",
|
| 171 | "parsed error in", p->conf.auth_htdigest_userfile,
|
| 172 | "expected 'username:realm:hashed password'");
|
| 173 | |
| 174 | stream_close(&f); |
| 175 | |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | if (NULL == (f_pwd = memchr(f_realm + 1, ':', f.size - (f_realm + 1 - f.start)))) { |
| 180 | log_error_write(srv, __FILE__, __LINE__, "sbs",
|
| 181 | "parsed error in", p->conf.auth_plain_userfile,
|
| 182 | "expected 'username:realm:hashed password'");
|
| 183 | |
| 184 | stream_close(&f); |
| 185 | |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | /* get pointers to the fields */
|
| 190 | u_len = f_realm - f_user; |
| 191 | f_realm++; |
| 192 | r_len = f_pwd - f_realm; |
| 193 | f_pwd++; |
| 194 | |
| 195 | if (NULL != (e = memchr(f_pwd, '\n', f.size - (f_pwd - f.start)))) { |
| 196 | pwd_len = e - f_pwd; |
| 197 | } else {
|
| 198 | pwd_len = f.size - (f_pwd - f.start); |
| 199 | } |
| 200 | |
| 201 | if (username->used - 1 == u_len && |
| 202 | (realm->used - 1 == r_len) &&
|
| 203 | (0 == strncmp(username->ptr, f_user, u_len)) &&
|
| 204 | (0 == strncmp(realm->ptr, f_realm, r_len))) {
|
| 205 | /* found */
|
| 206 | |
| 207 | buffer_copy_string_len(password, f_pwd, pwd_len); |
| 208 | |
| 209 | ret = 0;
|
| 210 | break;
|
| 211 | } |
| 212 | |
| 213 | /* EOL */
|
| 214 | if (!e) break; |
| 215 | |
| 216 | f_line = e + 1;
|
| 217 | } |
| 218 | |
| 219 | stream_close(&f); |
| 220 | } else if (p->conf.auth_backend == AUTH_BACKEND_HTPASSWD || |
| 221 | p->conf.auth_backend == AUTH_BACKEND_PLAIN) {
|
| 222 | stream f; |
| 223 | char * f_line;
|
| 224 | buffer *auth_fn; |
| 225 | |
| 226 | auth_fn = (p->conf.auth_backend == AUTH_BACKEND_HTPASSWD) ? p->conf.auth_htpasswd_userfile : p->conf.auth_plain_userfile; |
| 227 | |
| 228 | if (buffer_is_empty(auth_fn)) return -1; |
| 229 | |
| 230 | if (0 != stream_open(&f, auth_fn)) { |
| 231 | log_error_write(srv, __FILE__, __LINE__, "sbss",
|
| 232 | "opening plain-userfile", auth_fn, "failed:", strerror(errno)); |
| 233 | |
| 234 | return -1; |
| 235 | } |
| 236 | |
| 237 | f_line = f.start; |
| 238 | |
| 239 | while (f_line - f.start != f.size) {
|
| 240 | char *f_user, *f_pwd, *e;
|
| 241 | size_t u_len, pwd_len; |
| 242 | |
| 243 | f_user = f_line; |
| 244 | |
| 245 | /*
|
| 246 | * htpasswd format |
| 247 | * |
| 248 | * user:crypted passwd |
| 249 | */ |
| 250 | |
| 251 | if (NULL == (f_pwd = memchr(f_user, ':', f.size - (f_user - f.start) ))) { |
| 252 | log_error_write(srv, __FILE__, __LINE__, "sbs",
|
| 253 | "parsed error in", auth_fn,
|
| 254 | "expected 'username:hashed password'");
|
| 255 | |
| 256 | stream_close(&f); |
| 257 | |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | /* get pointers to the fields */
|
| 262 | u_len = f_pwd - f_user; |
| 263 | f_pwd++; |
| 264 | |
| 265 | if (NULL != (e = memchr(f_pwd, '\n', f.size - (f_pwd - f.start)))) { |
| 266 | pwd_len = e - f_pwd; |
| 267 | } else {
|
| 268 | pwd_len = f.size - (f_pwd - f.start); |
| 269 | } |
| 270 | |
| 271 | if (username->used - 1 == u_len && |
| 272 | (0 == strncmp(username->ptr, f_user, u_len))) {
|
| 273 | /* found */
|
| 274 | |
| 275 | buffer_copy_string_len(password, f_pwd, pwd_len); |
| 276 | |
| 277 | ret = 0;
|
| 278 | break;
|
| 279 | } |
| 280 | |
| 281 | /* EOL */
|
| 282 | if (!e) break; |
| 283 | |
| 284 | f_line = e + 1;
|
| 285 | } |
| 286 | |
| 287 | stream_close(&f); |
| 288 | } else if (p->conf.auth_backend == AUTH_BACKEND_LDAP) { |
| 289 | ret = 0;
|
| 290 | } else {
|
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | return ret;
|
| 295 | } |
| 296 | |
| 297 | static int http_auth_match_rules(server *srv, mod_auth_plugin_data *p, const char *url, const char *username, const char *group, const char *host) { |
| 298 | const char *r = NULL, *rules = NULL; |
| 299 | size_t i; |
| 300 | int username_len;
|
| 301 | data_string *require; |
| 302 | array *req; |
| 303 | |
| 304 | UNUSED(group); |
| 305 | UNUSED(host); |
| 306 | |
| 307 | /* check what has to be match to fullfil the request */
|
| 308 | /* search auth-directives for path */
|
| 309 | for (i = 0; i < p->conf.auth_require->used; i++) { |
| 310 | if (p->conf.auth_require->data[i]->key->used == 0) continue; |
| 311 | |
| 312 | if (0 == strncmp(url, p->conf.auth_require->data[i]->key->ptr, p->conf.auth_require->data[i]->key->used - 1)) { |
| 313 | break;
|
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (i == p->conf.auth_require->used) {
|
| 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | req = ((data_array *)(p->conf.auth_require->data[i]))->value; |
| 322 | |
| 323 | require = (data_string *)array_get_element(req, "require");
|
| 324 | |
| 325 | /* if we get here, the user we got a authed user */
|
| 326 | if (0 == strcmp(require->value->ptr, "valid-user")) { |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* user=name1|group=name3|host=name4 */
|
| 331 | |
| 332 | /* seperate the string by | */
|
| 333 | #if 0
|
| 334 | log_error_write(srv, __FILE__, __LINE__, "sb", "rules", require->value); |
| 335 | #endif |
| 336 | |
| 337 | username_len = username ? strlen(username) : 0;
|
| 338 | |
| 339 | r = rules = require->value->ptr; |
| 340 | |
| 341 | while (1) { |
| 342 | const char *eq; |
| 343 | const char *k, *v, *e; |
| 344 | int k_len, v_len, r_len;
|
| 345 | |
| 346 | e = strchr(r, '|');
|
| 347 | |
| 348 | if (e) {
|
| 349 | r_len = e - r; |
| 350 | } else {
|
| 351 | r_len = strlen(rules) - (r - rules); |
| 352 | } |
| 353 | |
| 354 | /* from r to r + r_len is a rule */
|
| 355 | |
| 356 | if (0 == strncmp(r, "valid-user", r_len)) { |
| 357 | log_error_write(srv, __FILE__, __LINE__, "sb",
|
| 358 | "parsing the 'require' section in 'auth.require' failed: valid-user cannot be combined with other require rules",
|
| 359 | require->value); |
| 360 | return -1; |
| 361 | } |
| 362 | |
| 363 | /* search for = in the rules */
|
| 364 | if (NULL == (eq = strchr(r, '='))) { |
| 365 | log_error_write(srv, __FILE__, __LINE__, "sb",
|
| 366 | "parsing the 'require' section in 'auth.require' failed: a = is missing",
|
| 367 | require->value); |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | /* = out of range */
|
| 372 | if (eq > r + r_len) {
|
| 373 | log_error_write(srv, __FILE__, __LINE__, "sb",
|
| 374 | "parsing the 'require' section in 'auth.require' failed: = out of range",
|
| 375 | require->value); |
| 376 | |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | /* the part before the = is user|group|host */
|
| 381 | |
| 382 | k = r; |
| 383 | k_len = eq - r; |
| 384 | v = eq + 1;
|
| 385 | v_len = r_len - k_len - 1;
|
| 386 | |
| 387 | if (k_len == 4) { |
| 388 | if (0 == strncmp(k, "user", k_len)) { |
| 389 | if (username &&
|
| 390 | username_len == v_len && |
| 391 | 0 == strncmp(username, v, v_len)) {
|
| 392 | return 0; |
| 393 | } |
| 394 | } else if (0 == strncmp(k, "host", k_len)) { |
| 395 | log_error_write(srv, __FILE__, __LINE__, "s", "host ... (not implemented)"); |
| 396 | } else {
|
| 397 | log_error_write(srv, __FILE__, __LINE__, "s", "unknown key"); |
| 398 | return -1; |
| 399 | } |
| 400 | } else if (k_len == 5) { |
| 401 | if (0 == strncmp(k, "group", k_len)) { |
| 402 | log_error_write(srv, __FILE__, __LINE__, "s", "group ... (not implemented)"); |
| 403 | } else {
|
| 404 | log_error_write(srv, __FILE__, __LINE__, "ss", "unknown key", k); |
| 405 | return -1; |
| 406 | } |
| 407 | } else {
|
| 408 | log_error_write(srv, __FILE__, __LINE__, "s", "unknown key"); |
| 409 | return -1; |
| 410 | } |
| 411 | |
| 412 | if (!e) break; |
| 413 | r = e + 1;
|
| 414 | } |
| 415 | |
| 416 | log_error_write(srv, __FILE__, __LINE__, "s", "nothing matched"); |
| 417 | |
| 418 | return -1; |
| 419 | } |
| 420 | |
| 421 | #define APR_MD5_DIGESTSIZE 16 |
| 422 | #define APR1_ID "$apr1$" |
| 423 | |
| 424 | /*
|
| 425 | * The following MD5 password encryption code was largely borrowed from |
| 426 | * the FreeBSD 3.0 /usr/src/lib/libcrypt/crypt.c file, which is |
| 427 | * licenced as stated at the top of this file. |
| 428 | */ |
| 429 | |
| 430 | static void to64(char *s, unsigned long v, int n) |
| 431 | {
|
| 432 | static unsigned char itoa64[] = /* 0 ... 63 => ASCII - 64 */ |
| 433 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
| 434 | |
| 435 | while (--n >= 0) { |
| 436 | *s++ = itoa64[v&0x3f];
|
| 437 | v >>= 6;
|
| 438 | } |
| 439 | } |
| 440 | |
| 441 | static void apr_md5_encode(const char *pw, const char *salt, char *result, size_t nbytes) { |
| 442 | /*
|
| 443 | * Minimum size is 8 bytes for salt, plus 1 for the trailing NUL, |
| 444 | * plus 4 for the '$' separators, plus the password hash itself. |
| 445 | * Let's leave a goodly amount of leeway. |
| 446 | */ |
| 447 | |
| 448 | char passwd[120], *p; |
| 449 | const char *sp, *ep; |
| 450 | unsigned char final[APR_MD5_DIGESTSIZE]; |
| 451 | ssize_t sl, pl, i; |
| 452 | MD5_CTX ctx, ctx1; |
| 453 | unsigned long l; |
| 454 | |
| 455 | /*
|
| 456 | * Refine the salt first. It's possible we were given an already-hashed |
| 457 | * string as the salt argument, so extract the actual salt value from it |
| 458 | * if so. Otherwise just use the string up to the first '$' as the salt. |
| 459 | */ |
| 460 | sp = salt; |
| 461 | |
| 462 | /*
|
| 463 | * If it starts with the magic string, then skip that. |
| 464 | */ |
| 465 | if (!strncmp(sp, APR1_ID, strlen(APR1_ID))) {
|
| 466 | sp += strlen(APR1_ID); |
| 467 | } |
| 468 | |
| 469 | /*
|
| 470 | * It stops at the first '$' or 8 chars, whichever comes first |
| 471 | */ |
| 472 | for (ep = sp; (*ep != '\0') && (*ep != '$') && (ep < (sp + 8)); ep++) { |
| 473 | continue;
|
| 474 | } |
| 475 | |
| 476 | /*
|
| 477 | * Get the length of the true salt |
| 478 | */ |
| 479 | sl = ep - sp; |
| 480 | |
| 481 | /*
|
| 482 | * 'Time to make the doughnuts..' |
| 483 | */ |
| 484 | MD5_Init(&ctx); |
| 485 | |
| 486 | /*
|
| 487 | * The password first, since that is what is most unknown |
| 488 | */ |
| 489 | MD5_Update(&ctx, pw, strlen(pw)); |
| 490 | |
| 491 | /*
|
| 492 | * Then our magic string |
| 493 | */ |
| 494 | MD5_Update(&ctx, APR1_ID, strlen(APR1_ID)); |
| 495 | |
| 496 | /*
|
| 497 | * Then the raw salt |
| 498 | */ |
| 499 | MD5_Update(&ctx, sp, sl); |
| 500 | |
| 501 | /*
|
| 502 | * Then just as many characters of the MD5(pw, salt, pw) |
| 503 | */ |
| 504 | MD5_Init(&ctx1); |
| 505 | MD5_Update(&ctx1, pw, strlen(pw)); |
| 506 | MD5_Update(&ctx1, sp, sl); |
| 507 | MD5_Update(&ctx1, pw, strlen(pw)); |
| 508 | MD5_Final(final, &ctx1); |
| 509 | for (pl = strlen(pw); pl > 0; pl -= APR_MD5_DIGESTSIZE) { |
| 510 | MD5_Update(&ctx, final, |
| 511 | (pl > APR_MD5_DIGESTSIZE) ? APR_MD5_DIGESTSIZE : pl); |
| 512 | } |
| 513 | |
| 514 | /*
|
| 515 | * Don't leave anything around in vm they could use. |
| 516 | */ |
| 517 | memset(final, 0, sizeof(final)); |
| 518 | |
| 519 | /*
|
| 520 | * Then something really weird... |
| 521 | */ |
| 522 | for (i = strlen(pw); i != 0; i >>= 1) { |
| 523 | if (i & 1) { |
| 524 | MD5_Update(&ctx, final, 1);
|
| 525 | } |
| 526 | else {
|
| 527 | MD5_Update(&ctx, pw, 1);
|
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /*
|
| 532 | * Now make the output string. We know our limitations, so we |
| 533 | * can use the string routines without bounds checking. |
| 534 | */ |
| 535 | strcpy(passwd, APR1_ID); |
| 536 | strncat(passwd, sp, sl); |
| 537 | strcat(passwd, "$");
|
| 538 | |
| 539 | MD5_Final(final, &ctx); |
| 540 | |
| 541 | /*
|
| 542 | * And now, just to make sure things don't run too fast.. |
| 543 | * On a 60 Mhz Pentium this takes 34 msec, so you would |
| 544 | * need 30 seconds to build a 1000 entry dictionary... |
| 545 | */ |
| 546 | for (i = 0; i < 1000; i++) { |
| 547 | MD5_Init(&ctx1); |
| 548 | if (i & 1) { |
| 549 | MD5_Update(&ctx1, pw, strlen(pw)); |
| 550 | } |
| 551 | else {
|
| 552 | MD5_Update(&ctx1, final, APR_MD5_DIGESTSIZE); |
| 553 | } |
| 554 | if (i % 3) { |
| 555 | MD5_Update(&ctx1, sp, sl); |
| 556 | } |
| 557 | |
| 558 | if (i % 7) { |
| 559 | MD5_Update(&ctx1, pw, strlen(pw)); |
| 560 | } |
| 561 | |
| 562 | if (i & 1) { |
| 563 | MD5_Update(&ctx1, final, APR_MD5_DIGESTSIZE); |
| 564 | } |
| 565 | else {
|
| 566 | MD5_Update(&ctx1, pw, strlen(pw)); |
| 567 | } |
| 568 | MD5_Final(final,&ctx1); |
| 569 | } |
| 570 | |
| 571 | p = passwd + strlen(passwd); |
| 572 | |
| 573 | l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p, l, 4); p += 4; |
| 574 | l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p, l, 4); p += 4; |
| 575 | l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p, l, 4); p += 4; |
| 576 | l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p, l, 4); p += 4; |
| 577 | l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p, l, 4); p += 4; |
| 578 | l = final[11] ; to64(p, l, 2); p += 2; |
| 579 | *p = '\0';
|
| 580 | |
| 581 | /*
|
| 582 | * Don't leave anything around in vm they could use. |
| 583 | */ |
| 584 | memset(final, 0, sizeof(final)); |
| 585 | |
| 586 | /* FIXME
|
| 587 | */ |
| 588 | #define apr_cpystrn strncpy
|
| 589 | apr_cpystrn(result, passwd, nbytes - 1);
|
| 590 | } |
| 591 | |
| 592 | |
| 593 | /**
|
| 594 | * |
| 595 | * |
| 596 | * @param password password-string from the auth-backend |
| 597 | * @param pw password-string from the client |
| 598 | */ |
| 599 | |
| 600 | static int http_auth_basic_password_compare(server *srv, mod_auth_plugin_data *p, array *req, buffer *username, buffer *realm, buffer *password, const char *pw) { |
| 601 | UNUSED(srv); |
| 602 | UNUSED(req); |
| 603 | |
| 604 | if (p->conf.auth_backend == AUTH_BACKEND_HTDIGEST) {
|
| 605 | /*
|
| 606 | * htdigest format |
| 607 | * |
| 608 | * user:realm:md5(user:realm:password) |
| 609 | */ |
| 610 | |
| 611 | MD5_CTX Md5Ctx; |
| 612 | HASH HA1; |
| 613 | char a1[256]; |
| 614 | |
| 615 | MD5_Init(&Md5Ctx); |
| 616 | MD5_Update(&Md5Ctx, (unsigned char *)username->ptr, username->used - 1); |
| 617 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 618 | MD5_Update(&Md5Ctx, (unsigned char *)realm->ptr, realm->used - 1); |
| 619 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 620 | MD5_Update(&Md5Ctx, (unsigned char *)pw, strlen(pw)); |
| 621 | MD5_Final(HA1, &Md5Ctx); |
| 622 | |
| 623 | CvtHex(HA1, a1); |
| 624 | |
| 625 | if (0 == strcmp(password->ptr, a1)) { |
| 626 | return 0; |
| 627 | } |
| 628 | } else if (p->conf.auth_backend == AUTH_BACKEND_HTPASSWD) { |
| 629 | char sample[120]; |
| 630 | if (!strncmp(password->ptr, APR1_ID, strlen(APR1_ID))) {
|
| 631 | /*
|
| 632 | * The hash was created using $apr1$ custom algorithm. |
| 633 | */ |
| 634 | apr_md5_encode(pw, password->ptr, sample, sizeof(sample));
|
| 635 | return (strcmp(sample, password->ptr) == 0) ? 0 : 1; |
| 636 | } else {
|
| 637 | #ifdef HAVE_CRYPT
|
| 638 | char salt[32]; |
| 639 | char *crypted;
|
| 640 | size_t salt_len = 0;
|
| 641 | /*
|
| 642 | * htpasswd format |
| 643 | * |
| 644 | * user:crypted password |
| 645 | */ |
| 646 | |
| 647 | /*
|
| 648 | * Algorithm Salt |
| 649 | * CRYPT_STD_DES 2-character (Default) |
| 650 | * CRYPT_EXT_DES 9-character |
| 651 | * CRYPT_MD5 12-character beginning with $1$ |
| 652 | * CRYPT_BLOWFISH 16-character beginning with $2$ |
| 653 | */ |
| 654 | |
| 655 | if (password->used < 13 + 1) { |
| 656 | fprintf(stderr, "%s.%d\n", __FILE__, __LINE__);
|
| 657 | return -1; |
| 658 | } |
| 659 | |
| 660 | if (password->used == 13 + 1) { |
| 661 | /* a simple DES password is 2 + 11 characters */
|
| 662 | salt_len = 2;
|
| 663 | } else if (password->ptr[0] == '$' && password->ptr[2] == '$') { |
| 664 | char *dollar = NULL; |
| 665 | |
| 666 | if (NULL == (dollar = strchr(password->ptr + 3, '$'))) { |
| 667 | fprintf(stderr, "%s.%d\n", __FILE__, __LINE__);
|
| 668 | return -1; |
| 669 | } |
| 670 | |
| 671 | salt_len = dollar - password->ptr; |
| 672 | } |
| 673 | |
| 674 | if (salt_len > sizeof(salt) - 1) { |
| 675 | fprintf(stderr, "%s.%d\n", __FILE__, __LINE__);
|
| 676 | return -1; |
| 677 | } |
| 678 | |
| 679 | strncpy(salt, password->ptr, salt_len); |
| 680 | |
| 681 | salt[salt_len] = '\0';
|
| 682 | |
| 683 | crypted = crypt(pw, salt); |
| 684 | |
| 685 | if (0 == strcmp(password->ptr, crypted)) { |
| 686 | return 0; |
| 687 | } else {
|
| 688 | fprintf(stderr, "%s.%d\n", __FILE__, __LINE__);
|
| 689 | } |
| 690 | |
| 691 | #endif
|
| 692 | } |
| 693 | } else if (p->conf.auth_backend == AUTH_BACKEND_PLAIN) { |
| 694 | if (0 == strcmp(password->ptr, pw)) { |
| 695 | return 0; |
| 696 | } |
| 697 | } else if (p->conf.auth_backend == AUTH_BACKEND_LDAP) { |
| 698 | #ifdef USE_LDAP
|
| 699 | LDAP *ldap; |
| 700 | LDAPMessage *lm, *first; |
| 701 | char *dn;
|
| 702 | int ret;
|
| 703 | char *attrs[] = { LDAP_NO_ATTRS, NULL }; |
| 704 | size_t i; |
| 705 | |
| 706 | /* for now we stay synchronous */
|
| 707 | |
| 708 | /*
|
| 709 | * 1. connect anonymously (done in plugin init) |
| 710 | * 2. get DN for uid = username |
| 711 | * 3. auth against ldap server |
| 712 | * 4. (optional) check a field |
| 713 | * 5. disconnect |
| 714 | * |
| 715 | */ |
| 716 | |
| 717 | /* check username
|
| 718 | * |
| 719 | * we have to protect us againt username which modifies out filter in |
| 720 | * a unpleasant way |
| 721 | */ |
| 722 | |
| 723 | for (i = 0; i < username->used - 1; i++) { |
| 724 | char c = username->ptr[i];
|
| 725 | |
| 726 | if (!isalpha(c) &&
|
| 727 | !isdigit(c)) {
|
| 728 | |
| 729 | log_error_write(srv, __FILE__, __LINE__, "sbd",
|
| 730 | "ldap: invalid character (a-zA-Z0-9 allowed) in username:", username, i);
|
| 731 | |
| 732 | return -1; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | if (p->conf.auth_ldap_allow_empty_pw != 1 && pw[0] == '\0') |
| 737 | return -1; |
| 738 | |
| 739 | /* build filter */
|
| 740 | buffer_copy_string_buffer(p->ldap_filter, p->conf.ldap_filter_pre); |
| 741 | buffer_append_string_buffer(p->ldap_filter, username); |
| 742 | buffer_append_string_buffer(p->ldap_filter, p->conf.ldap_filter_post); |
| 743 | |
| 744 | |
| 745 | /* 2. */
|
| 746 | if (p->conf.ldap == NULL || |
| 747 | LDAP_SUCCESS != (ret = ldap_search_s(p->conf.ldap, p->conf.auth_ldap_basedn->ptr, LDAP_SCOPE_SUBTREE, p->ldap_filter->ptr, attrs, 0, &lm))) {
|
| 748 | if (auth_ldap_init(srv, &p->conf) != HANDLER_GO_ON)
|
| 749 | return -1; |
| 750 | if (LDAP_SUCCESS != (ret = ldap_search_s(p->conf.ldap, p->conf.auth_ldap_basedn->ptr, LDAP_SCOPE_SUBTREE, p->ldap_filter->ptr, attrs, 0, &lm))) { |
| 751 | |
| 752 | log_error_write(srv, __FILE__, __LINE__, "sssb",
|
| 753 | "ldap:", ldap_err2string(ret), "filter:", p->ldap_filter); |
| 754 | |
| 755 | return -1; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | if (NULL == (first = ldap_first_entry(p->conf.ldap, lm))) { |
| 760 | log_error_write(srv, __FILE__, __LINE__, "s", "ldap ..."); |
| 761 | |
| 762 | ldap_msgfree(lm); |
| 763 | |
| 764 | return -1; |
| 765 | } |
| 766 | |
| 767 | if (NULL == (dn = ldap_get_dn(p->conf.ldap, first))) { |
| 768 | log_error_write(srv, __FILE__, __LINE__, "s", "ldap ..."); |
| 769 | |
| 770 | ldap_msgfree(lm); |
| 771 | |
| 772 | return -1; |
| 773 | } |
| 774 | |
| 775 | ldap_msgfree(lm); |
| 776 | |
| 777 | |
| 778 | /* 3. */
|
| 779 | if (NULL == (ldap = ldap_init(p->conf.auth_ldap_hostname->ptr, LDAP_PORT))) { |
| 780 | log_error_write(srv, __FILE__, __LINE__, "ss", "ldap ...", strerror(errno)); |
| 781 | return -1; |
| 782 | } |
| 783 | |
| 784 | ret = LDAP_VERSION3; |
| 785 | if (LDAP_OPT_SUCCESS != (ret = ldap_set_option(ldap, LDAP_OPT_PROTOCOL_VERSION, &ret))) {
|
| 786 | log_error_write(srv, __FILE__, __LINE__, "ss", "ldap:", ldap_err2string(ret)); |
| 787 | |
| 788 | ldap_unbind_s(ldap); |
| 789 | |
| 790 | return -1; |
| 791 | } |
| 792 | |
| 793 | if (p->conf.auth_ldap_starttls == 1) { |
| 794 | if (LDAP_OPT_SUCCESS != (ret = ldap_start_tls_s(ldap, NULL, NULL))) { |
| 795 | log_error_write(srv, __FILE__, __LINE__, "ss", "ldap startTLS failed:", ldap_err2string(ret)); |
| 796 | |
| 797 | ldap_unbind_s(ldap); |
| 798 | |
| 799 | return -1; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | |
| 804 | if (LDAP_SUCCESS != (ret = ldap_simple_bind_s(ldap, dn, pw))) {
|
| 805 | log_error_write(srv, __FILE__, __LINE__, "ss", "ldap:", ldap_err2string(ret)); |
| 806 | |
| 807 | ldap_unbind_s(ldap); |
| 808 | |
| 809 | return -1; |
| 810 | } |
| 811 | |
| 812 | /* 5. */
|
| 813 | ldap_unbind_s(ldap); |
| 814 | |
| 815 | /* everything worked, good, access granted */
|
| 816 | |
| 817 | return 0; |
| 818 | #endif
|
| 819 | } |
| 820 | return -1; |
| 821 | } |
| 822 | |
| 823 | int http_auth_basic_check(server *srv, connection *con, mod_auth_plugin_data *p, array *req, buffer *url, const char *realm_str) { |
| 824 | buffer *username, *password; |
| 825 | char *pw;
|
| 826 | |
| 827 | data_string *realm; |
| 828 | |
| 829 | realm = (data_string *)array_get_element(req, "realm");
|
| 830 | |
| 831 | username = buffer_init(); |
| 832 | |
| 833 | if (!base64_decode(username, realm_str)) {
|
| 834 | buffer_free(username); |
| 835 | |
| 836 | log_error_write(srv, __FILE__, __LINE__, "sb", "decodeing base64-string failed", username); |
| 837 | |
| 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | /* r2 == user:password */
|
| 842 | if (NULL == (pw = strchr(username->ptr, ':'))) { |
| 843 | buffer_free(username); |
| 844 | |
| 845 | log_error_write(srv, __FILE__, __LINE__, "sb", ": is missing in", username); |
| 846 | |
| 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | *pw++ = '\0';
|
| 851 | |
| 852 | username->used = pw - username->ptr; |
| 853 | |
| 854 | password = buffer_init(); |
| 855 | /* copy password to r1 */
|
| 856 | if (http_auth_get_password(srv, p, username, realm->value, password)) {
|
| 857 | buffer_free(username); |
| 858 | buffer_free(password); |
| 859 | |
| 860 | log_error_write(srv, __FILE__, __LINE__, "s", "get_password failed"); |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | /* password doesn't match */
|
| 866 | if (http_auth_basic_password_compare(srv, p, req, username, realm->value, password, pw)) {
|
| 867 | log_error_write(srv, __FILE__, __LINE__, "sbb", "password doesn't match for", con->uri.path, username); |
| 868 | |
| 869 | buffer_free(username); |
| 870 | buffer_free(password); |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | /* value is our allow-rules */
|
| 876 | if (http_auth_match_rules(srv, p, url->ptr, username->ptr, NULL, NULL)) { |
| 877 | buffer_free(username); |
| 878 | buffer_free(password); |
| 879 | |
| 880 | log_error_write(srv, __FILE__, __LINE__, "s", "rules didn't match"); |
| 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | /* remember the username */
|
| 886 | buffer_copy_string_buffer(p->auth_user, username); |
| 887 | |
| 888 | buffer_free(username); |
| 889 | buffer_free(password); |
| 890 | |
| 891 | return 1; |
| 892 | } |
| 893 | |
| 894 | typedef struct { |
| 895 | const char *key; |
| 896 | int key_len;
|
| 897 | char **ptr;
|
| 898 | } digest_kv; |
| 899 | |
| 900 | int http_auth_digest_check(server *srv, connection *con, mod_auth_plugin_data *p, array *req, buffer *url, const char *realm_str) { |
| 901 | char a1[256]; |
| 902 | char a2[256]; |
| 903 | |
| 904 | char *username;
|
| 905 | char *realm;
|
| 906 | char *nonce;
|
| 907 | char *uri;
|
| 908 | char *algorithm;
|
| 909 | char *qop;
|
| 910 | char *cnonce;
|
| 911 | char *nc;
|
| 912 | char *respons;
|
| 913 | |
| 914 | char *e, *c;
|
| 915 | const char *m = NULL; |
| 916 | int i;
|
| 917 | buffer *password, *b, *username_buf, *realm_buf; |
| 918 | |
| 919 | MD5_CTX Md5Ctx; |
| 920 | HASH HA1; |
| 921 | HASH HA2; |
| 922 | HASH RespHash; |
| 923 | HASHHEX HA2Hex; |
| 924 | |
| 925 | |
| 926 | /* init pointers */
|
| 927 | #define S(x) \
|
| 928 | x, sizeof(x)-1, NULL |
| 929 | digest_kv dkv[10] = {
|
| 930 | { S("username=") },
|
| 931 | { S("realm=") },
|
| 932 | { S("nonce=") },
|
| 933 | { S("uri=") },
|
| 934 | { S("algorithm=") },
|
| 935 | { S("qop=") },
|
| 936 | { S("cnonce=") },
|
| 937 | { S("nc=") },
|
| 938 | { S("response=") },
|
| 939 | |
| 940 | { NULL, 0, NULL }
|
| 941 | }; |
| 942 | #undef S
|
| 943 | |
| 944 | dkv[0].ptr = &username;
|
| 945 | dkv[1].ptr = &realm;
|
| 946 | dkv[2].ptr = &nonce;
|
| 947 | dkv[3].ptr = &uri;
|
| 948 | dkv[4].ptr = &algorithm;
|
| 949 | dkv[5].ptr = &qop;
|
| 950 | dkv[6].ptr = &cnonce;
|
| 951 | dkv[7].ptr = &nc;
|
| 952 | dkv[8].ptr = &respons;
|
| 953 | dkv[9].ptr = NULL; |
| 954 | |
| 955 | UNUSED(req); |
| 956 | |
| 957 | for (i = 0; dkv[i].key; i++) { |
| 958 | *(dkv[i].ptr) = NULL;
|
| 959 | } |
| 960 | |
| 961 | |
| 962 | if (p->conf.auth_backend != AUTH_BACKEND_HTDIGEST &&
|
| 963 | p->conf.auth_backend != AUTH_BACKEND_PLAIN) {
|
| 964 | log_error_write(srv, __FILE__, __LINE__, "s",
|
| 965 | "digest: unsupported backend (only htdigest or plain)");
|
| 966 | |
| 967 | return -1; |
| 968 | } |
| 969 | |
| 970 | b = buffer_init_string(realm_str); |
| 971 | |
| 972 | /* parse credentials from client */
|
| 973 | for (c = b->ptr; *c; c++) {
|
| 974 | /* skip whitespaces */
|
| 975 | while (*c == ' ' || *c == '\t') c++; |
| 976 | if (!*c) break; |
| 977 | |
| 978 | for (i = 0; dkv[i].key; i++) { |
| 979 | if ((0 == strncmp(c, dkv[i].key, dkv[i].key_len))) { |
| 980 | if ((c[dkv[i].key_len] == '"') && |
| 981 | (NULL != (e = strchr(c + dkv[i].key_len + 1, '"')))) { |
| 982 | /* value with "..." */
|
| 983 | *(dkv[i].ptr) = c + dkv[i].key_len + 1;
|
| 984 | c = e; |
| 985 | |
| 986 | *e = '\0';
|
| 987 | } else if (NULL != (e = strchr(c + dkv[i].key_len, ','))) { |
| 988 | /* value without "...", terminated by ',' */
|
| 989 | *(dkv[i].ptr) = c + dkv[i].key_len; |
| 990 | c = e; |
| 991 | |
| 992 | *e = '\0';
|
| 993 | } else {
|
| 994 | /* value without "...", terminated by EOL */
|
| 995 | *(dkv[i].ptr) = c + dkv[i].key_len; |
| 996 | c += strlen(c) - 1;
|
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | if (p->conf.auth_debug > 1) { |
| 1003 | log_error_write(srv, __FILE__, __LINE__, "ss", "username", username); |
| 1004 | log_error_write(srv, __FILE__, __LINE__, "ss", "realm", realm); |
| 1005 | log_error_write(srv, __FILE__, __LINE__, "ss", "nonce", nonce); |
| 1006 | log_error_write(srv, __FILE__, __LINE__, "ss", "uri", uri); |
| 1007 | log_error_write(srv, __FILE__, __LINE__, "ss", "algorigthm", algorithm); |
| 1008 | log_error_write(srv, __FILE__, __LINE__, "ss", "qop", qop); |
| 1009 | log_error_write(srv, __FILE__, __LINE__, "ss", "cnonce", cnonce); |
| 1010 | log_error_write(srv, __FILE__, __LINE__, "ss", "nc", nc); |
| 1011 | log_error_write(srv, __FILE__, __LINE__, "ss", "response", respons); |
| 1012 | } |
| 1013 | |
| 1014 | /* check if everything is transmitted */
|
| 1015 | if (!username ||
|
| 1016 | !realm || |
| 1017 | !nonce || |
| 1018 | !uri || |
| 1019 | (qop && (!nc || !cnonce)) || |
| 1020 | !respons ) {
|
| 1021 | /* missing field */
|
| 1022 | |
| 1023 | log_error_write(srv, __FILE__, __LINE__, "s",
|
| 1024 | "digest: missing field");
|
| 1025 | |
| 1026 | buffer_free(b); |
| 1027 | return -1; |
| 1028 | } |
| 1029 | |
| 1030 | /**
|
| 1031 | * protect the md5-sess against missing cnonce and nonce |
| 1032 | */ |
| 1033 | if (algorithm &&
|
| 1034 | 0 == strcasecmp(algorithm, "md5-sess") && |
| 1035 | (!nonce || !cnonce)) {
|
| 1036 | log_error_write(srv, __FILE__, __LINE__, "s",
|
| 1037 | "digest: (md5-sess: missing field");
|
| 1038 | |
| 1039 | buffer_free(b); |
| 1040 | return -1; |
| 1041 | } |
| 1042 | |
| 1043 | m = get_http_method_name(con->request.http_method); |
| 1044 | |
| 1045 | /* password-string == HA1 */
|
| 1046 | password = buffer_init(); |
| 1047 | username_buf = buffer_init_string(username); |
| 1048 | realm_buf = buffer_init_string(realm); |
| 1049 | if (http_auth_get_password(srv, p, username_buf, realm_buf, password)) {
|
| 1050 | buffer_free(password); |
| 1051 | buffer_free(b); |
| 1052 | buffer_free(username_buf); |
| 1053 | buffer_free(realm_buf); |
| 1054 | return 0; |
| 1055 | } |
| 1056 | |
| 1057 | buffer_free(username_buf); |
| 1058 | buffer_free(realm_buf); |
| 1059 | |
| 1060 | if (p->conf.auth_backend == AUTH_BACKEND_PLAIN) {
|
| 1061 | /* generate password from plain-text */
|
| 1062 | MD5_Init(&Md5Ctx); |
| 1063 | MD5_Update(&Md5Ctx, (unsigned char *)username, strlen(username)); |
| 1064 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1065 | MD5_Update(&Md5Ctx, (unsigned char *)realm, strlen(realm)); |
| 1066 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1067 | MD5_Update(&Md5Ctx, (unsigned char *)password->ptr, password->used - 1); |
| 1068 | MD5_Final(HA1, &Md5Ctx); |
| 1069 | } else if (p->conf.auth_backend == AUTH_BACKEND_HTDIGEST) { |
| 1070 | /* HA1 */
|
| 1071 | /* transform the 32-byte-hex-md5 to a 16-byte-md5 */
|
| 1072 | for (i = 0; i < HASHLEN; i++) { |
| 1073 | HA1[i] = hex2int(password->ptr[i*2]) << 4; |
| 1074 | HA1[i] |= hex2int(password->ptr[i*2+1]); |
| 1075 | } |
| 1076 | } else {
|
| 1077 | /* we already check that above */
|
| 1078 | SEGFAULT(); |
| 1079 | } |
| 1080 | |
| 1081 | buffer_free(password); |
| 1082 | |
| 1083 | if (algorithm &&
|
| 1084 | strcasecmp(algorithm, "md5-sess") == 0) { |
| 1085 | MD5_Init(&Md5Ctx); |
| 1086 | MD5_Update(&Md5Ctx, (unsigned char *)HA1, 16); |
| 1087 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1088 | MD5_Update(&Md5Ctx, (unsigned char *)nonce, strlen(nonce)); |
| 1089 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1090 | MD5_Update(&Md5Ctx, (unsigned char *)cnonce, strlen(cnonce)); |
| 1091 | MD5_Final(HA1, &Md5Ctx); |
| 1092 | } |
| 1093 | |
| 1094 | CvtHex(HA1, a1); |
| 1095 | |
| 1096 | /* calculate H(A2) */
|
| 1097 | MD5_Init(&Md5Ctx); |
| 1098 | MD5_Update(&Md5Ctx, (unsigned char *)m, strlen(m)); |
| 1099 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1100 | MD5_Update(&Md5Ctx, (unsigned char *)uri, strlen(uri)); |
| 1101 | if (qop && strcasecmp(qop, "auth-int") == 0) { |
| 1102 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1103 | MD5_Update(&Md5Ctx, (unsigned char *)"", HASHHEXLEN); |
| 1104 | } |
| 1105 | MD5_Final(HA2, &Md5Ctx); |
| 1106 | CvtHex(HA2, HA2Hex); |
| 1107 | |
| 1108 | /* calculate response */
|
| 1109 | MD5_Init(&Md5Ctx); |
| 1110 | MD5_Update(&Md5Ctx, (unsigned char *)a1, HASHHEXLEN); |
| 1111 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1112 | MD5_Update(&Md5Ctx, (unsigned char *)nonce, strlen(nonce)); |
| 1113 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1114 | if (qop && *qop) {
|
| 1115 | MD5_Update(&Md5Ctx, (unsigned char *)nc, strlen(nc)); |
| 1116 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1117 | MD5_Update(&Md5Ctx, (unsigned char *)cnonce, strlen(cnonce)); |
| 1118 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1119 | MD5_Update(&Md5Ctx, (unsigned char *)qop, strlen(qop)); |
| 1120 | MD5_Update(&Md5Ctx, (unsigned char *)":", 1); |
| 1121 | }; |
| 1122 | MD5_Update(&Md5Ctx, (unsigned char *)HA2Hex, HASHHEXLEN); |
| 1123 | MD5_Final(RespHash, &Md5Ctx); |
| 1124 | CvtHex(RespHash, a2); |
| 1125 | |
| 1126 | if (0 != strcmp(a2, respons)) { |
| 1127 | /* digest not ok */
|
| 1128 | |
| 1129 | if (p->conf.auth_debug) {
|
| 1130 | log_error_write(srv, __FILE__, __LINE__, "sss",
|
| 1131 | "digest: digest mismatch", a2, respons);
|
| 1132 | } |
| 1133 | |
| 1134 | log_error_write(srv, __FILE__, __LINE__, "sss",
|
| 1135 | "digest: auth failed for", username, "wrong password"); |
| 1136 | |
| 1137 | buffer_free(b); |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
| 1141 | /* value is our allow-rules */
|
| 1142 | if (http_auth_match_rules(srv, p, url->ptr, username, NULL, NULL)) { |
| 1143 | buffer_free(b); |
| 1144 | |
| 1145 | log_error_write(srv, __FILE__, __LINE__, "s",
|
| 1146 | "digest: rules did match");
|
| 1147 | |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | /* remember the username */
|
| 1152 | buffer_copy_string(p->auth_user, username); |
| 1153 | |
| 1154 | buffer_free(b); |
| 1155 | |
| 1156 | if (p->conf.auth_debug) {
|
| 1157 | log_error_write(srv, __FILE__, __LINE__, "s",
|
| 1158 | "digest: auth ok");
|
| 1159 | } |
| 1160 | return 1; |
| 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | int http_auth_digest_generate_nonce(server *srv, mod_auth_plugin_data *p, buffer *fn, char out[33]) { |
| 1165 | HASH h; |
| 1166 | MD5_CTX Md5Ctx; |
| 1167 | char hh[32]; |
| 1168 | |
| 1169 | UNUSED(p); |
| 1170 | |
| 1171 | /* generate shared-secret */
|
| 1172 | MD5_Init(&Md5Ctx); |
| 1173 | MD5_Update(&Md5Ctx, (unsigned char *)fn->ptr, fn->used - 1); |
| 1174 | MD5_Update(&Md5Ctx, (unsigned char *)"+", 1); |
| 1175 | |
| 1176 | /* we assume sizeof(time_t) == 4 here, but if not it ain't a problem at all */
|
| 1177 | ltostr(hh, srv->cur_ts); |
| 1178 | MD5_Update(&Md5Ctx, (unsigned char *)hh, strlen(hh)); |
| 1179 | ltostr(hh, rand()); |
| 1180 | MD5_Update(&Md5Ctx, (unsigned char *)hh, strlen(hh)); |
| 1181 | |
| 1182 | MD5_Final(h, &Md5Ctx); |
| 1183 | |
| 1184 | CvtHex(h, out); |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |