Annotation of embedaddon/lighttpd/src/http_auth.c, revision 1.1.1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>