2009-10-11 14:31:42 +00:00
|
|
|
#include "server.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "http_auth.h"
|
|
|
|
#include "inet_ntop_cache.h"
|
|
|
|
#include "stream.h"
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CRYPT_H
|
|
|
|
# include <crypt.h>
|
|
|
|
#elif defined(__linux__)
|
|
|
|
/* linux needs _XOPEN_SOURCE */
|
|
|
|
# define _XOPEN_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2015-08-29 12:23:45 +00:00
|
|
|
#if defined(HAVE_LIBCRYPT) && !defined(HAVE_CRYPT)
|
|
|
|
/* always assume crypt() is present if we have -lcrypt */
|
2005-02-20 14:27:00 +00:00
|
|
|
# define HAVE_CRYPT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
2005-08-21 23:09:43 +00:00
|
|
|
#include <stdio.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2011-07-30 09:16:03 +00:00
|
|
|
#include "md5.h"
|
2011-04-24 16:02:52 +00:00
|
|
|
|
2013-04-29 13:08:25 +00:00
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
#endif
|
|
|
|
|
2011-07-30 09:16:03 +00:00
|
|
|
#define HASHLEN 16
|
|
|
|
#define HASHHEXLEN 32
|
|
|
|
typedef unsigned char HASH[HASHLEN];
|
|
|
|
typedef char HASHHEX[HASHHEXLEN+1];
|
2011-04-24 16:02:52 +00:00
|
|
|
|
2011-07-30 09:16:03 +00:00
|
|
|
static void CvtHex(const HASH Bin, char Hex[33]) {
|
2015-02-08 19:10:44 +00:00
|
|
|
li_tohex(Hex, (const char*) Bin, 16);
|
2011-07-30 09:16:03 +00:00
|
|
|
}
|
2005-02-20 14:27:00 +00:00
|
|
|
|
2006-10-04 07:46:12 +00:00
|
|
|
/**
|
|
|
|
* the $apr1$ handling is taken from apache 1.3.x
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The apr_md5_encode() routine uses much code obtained from the FreeBSD 3.0
|
|
|
|
* MD5 crypt() function, which is licenced as follows:
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
|
|
|
* <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
|
|
|
|
* can do whatever you want with this stuff. If we meet some day, and you think
|
|
|
|
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
|
|
|
|
* ----------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2005-11-07 09:50:34 +00:00
|
|
|
handler_t auth_ldap_init(server *srv, mod_auth_plugin_config *s);
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
static const char base64_pad = '=';
|
|
|
|
|
2008-09-30 13:48:45 +00:00
|
|
|
/* "A-Z a-z 0-9 + /" maps to 0-63 */
|
2005-02-20 14:27:00 +00:00
|
|
|
static const short base64_reverse_table[256] = {
|
2008-09-30 13:48:45 +00:00
|
|
|
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 - 0x0F */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 - 0x1F */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 - 0x2F */
|
|
|
|
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 - 0x3F */
|
|
|
|
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 - 0x4F */
|
|
|
|
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 - 0x5F */
|
|
|
|
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 - 0x6F */
|
|
|
|
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 0x70 - 0x7F */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80 - 0x8F */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x90 - 0x9F */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xA0 - 0xAF */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xB0 - 0xBF */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xC0 - 0xCF */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xD0 - 0xDF */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xE0 - 0xEF */
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0xF0 - 0xFF */
|
2005-02-20 14:27:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned char * base64_decode(buffer *out, const char *in) {
|
|
|
|
unsigned char *result;
|
2013-05-15 10:31:04 +00:00
|
|
|
unsigned int j = 0; /* current output character (position) that is decoded. can contain partial result */
|
|
|
|
unsigned int group = 0; /* how many base64 digits in the current group were decoded already. each group has up to 4 digits */
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t i;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
size_t in_len = strlen(in);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
result = (unsigned char *) buffer_string_prepare_copy(out, in_len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* run through the whole string, converting as we go */
|
|
|
|
for (i = 0; i < in_len; i++) {
|
2013-05-15 10:31:04 +00:00
|
|
|
unsigned char c = (unsigned char) in[i];
|
|
|
|
short ch;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2013-05-15 10:31:04 +00:00
|
|
|
if (c == '\0') break;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2013-05-15 10:31:04 +00:00
|
|
|
if (c == base64_pad) {
|
|
|
|
/* pad character can only come after 2 base64 digits in a group */
|
|
|
|
if (group < 2) return NULL;
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2013-05-15 10:31:04 +00:00
|
|
|
ch = base64_reverse_table[c];
|
|
|
|
if (ch < 0) continue; /* skip invalid characters */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2013-05-15 10:31:04 +00:00
|
|
|
switch(group) {
|
2005-02-20 14:27:00 +00:00
|
|
|
case 0:
|
|
|
|
result[j] = ch << 2;
|
2013-05-15 10:31:04 +00:00
|
|
|
group = 1;
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
result[j++] |= ch >> 4;
|
|
|
|
result[j] = (ch & 0x0f) << 4;
|
2013-05-15 10:31:04 +00:00
|
|
|
group = 2;
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
result[j++] |= ch >>2;
|
|
|
|
result[j] = (ch & 0x03) << 6;
|
2013-05-15 10:31:04 +00:00
|
|
|
group = 3;
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
result[j++] |= ch;
|
2013-05-15 10:31:04 +00:00
|
|
|
group = 0;
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-05-15 10:31:04 +00:00
|
|
|
|
|
|
|
switch(group) {
|
|
|
|
case 0:
|
|
|
|
/* ended on boundary */
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
/* need at least 2 base64 digits per group */
|
|
|
|
return NULL;
|
|
|
|
case 2:
|
|
|
|
/* have 2 base64 digits in last group => one real octect, two zeroes padded */
|
|
|
|
case 3:
|
|
|
|
/* have 3 base64 digits in last group => two real octects, one zero padded */
|
|
|
|
|
|
|
|
/* for both cases the current index already is on the first zero padded octet
|
|
|
|
* - check it really is zero (overlapping bits) */
|
|
|
|
if (0 != result[j]) return NULL;
|
|
|
|
break;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
buffer_commit(out, j);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int http_auth_get_password(server *srv, mod_auth_plugin_data *p, buffer *username, buffer *realm, buffer *password) {
|
|
|
|
int ret = -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
if (buffer_is_empty(username) || buffer_is_empty(realm)) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (p->conf.auth_backend == AUTH_BACKEND_HTDIGEST) {
|
|
|
|
stream f;
|
|
|
|
char * f_line;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (buffer_string_is_empty(p->conf.auth_htdigest_userfile)) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (0 != stream_open(&f, p->conf.auth_htdigest_userfile)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbss", "opening digest-userfile", p->conf.auth_htdigest_userfile, "failed:", strerror(errno));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
f_line = f.start;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
while (f_line - f.start != f.size) {
|
|
|
|
char *f_user, *f_pwd, *e, *f_realm;
|
|
|
|
size_t u_len, pwd_len, r_len;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
f_user = f_line;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/*
|
2005-02-20 14:27:00 +00:00
|
|
|
* htdigest format
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* user:realm:md5(user:realm:password)
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (NULL == (f_realm = memchr(f_user, ':', f.size - (f_user - f.start) ))) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbs",
|
|
|
|
"parsed error in", p->conf.auth_htdigest_userfile,
|
2005-02-20 14:27:00 +00:00
|
|
|
"expected 'username:realm:hashed password'");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
stream_close(&f);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (NULL == (f_pwd = memchr(f_realm + 1, ':', f.size - (f_realm + 1 - f.start)))) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbs",
|
|
|
|
"parsed error in", p->conf.auth_plain_userfile,
|
2005-02-20 14:27:00 +00:00
|
|
|
"expected 'username:realm:hashed password'");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
stream_close(&f);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* get pointers to the fields */
|
2006-10-04 13:26:23 +00:00
|
|
|
u_len = f_realm - f_user;
|
2005-02-20 14:27:00 +00:00
|
|
|
f_realm++;
|
|
|
|
r_len = f_pwd - f_realm;
|
|
|
|
f_pwd++;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (NULL != (e = memchr(f_pwd, '\n', f.size - (f_pwd - f.start)))) {
|
|
|
|
pwd_len = e - f_pwd;
|
|
|
|
} else {
|
|
|
|
pwd_len = f.size - (f_pwd - f.start);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
if (buffer_string_length(username) == u_len &&
|
|
|
|
(buffer_string_length(realm) == r_len) &&
|
2005-02-20 14:27:00 +00:00
|
|
|
(0 == strncmp(username->ptr, f_user, u_len)) &&
|
|
|
|
(0 == strncmp(realm->ptr, f_realm, r_len))) {
|
|
|
|
/* found */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_copy_string_len(password, f_pwd, pwd_len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* EOL */
|
|
|
|
if (!e) break;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
f_line = e + 1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
stream_close(&f);
|
|
|
|
} else if (p->conf.auth_backend == AUTH_BACKEND_HTPASSWD ||
|
|
|
|
p->conf.auth_backend == AUTH_BACKEND_PLAIN) {
|
|
|
|
stream f;
|
|
|
|
char * f_line;
|
|
|
|
buffer *auth_fn;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
auth_fn = (p->conf.auth_backend == AUTH_BACKEND_HTPASSWD) ? p->conf.auth_htpasswd_userfile : p->conf.auth_plain_userfile;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 12:37:10 +00:00
|
|
|
if (buffer_string_is_empty(auth_fn)) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (0 != stream_open(&f, auth_fn)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbss",
|
2005-02-20 14:27:00 +00:00
|
|
|
"opening plain-userfile", auth_fn, "failed:", strerror(errno));
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
f_line = f.start;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
while (f_line - f.start != f.size) {
|
|
|
|
char *f_user, *f_pwd, *e;
|
|
|
|
size_t u_len, pwd_len;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
f_user = f_line;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
|
|
|
/*
|
2005-02-20 14:27:00 +00:00
|
|
|
* htpasswd format
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* user:crypted passwd
|
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (NULL == (f_pwd = memchr(f_user, ':', f.size - (f_user - f.start) ))) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sbs",
|
|
|
|
"parsed error in", auth_fn,
|
2005-02-20 14:27:00 +00:00
|
|
|
"expected 'username:hashed password'");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
stream_close(&f);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* get pointers to the fields */
|
2006-10-04 13:26:23 +00:00
|
|
|
u_len = f_pwd - f_user;
|
2005-02-20 14:27:00 +00:00
|
|
|
f_pwd++;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (NULL != (e = memchr(f_pwd, '\n', f.size - (f_pwd - f.start)))) {
|
|
|
|
pwd_len = e - f_pwd;
|
|
|
|
} else {
|
|
|
|
pwd_len = f.size - (f_pwd - f.start);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-02-08 19:10:44 +00:00
|
|
|
if (buffer_string_length(username) == u_len &&
|
2005-02-20 14:27:00 +00:00
|
|
|
(0 == strncmp(username->ptr, f_user, u_len))) {
|
|
|
|
/* found */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
buffer_copy_string_len(password, f_pwd, pwd_len);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* EOL */
|
|
|
|
if (!e) break;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
f_line = e + 1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
stream_close(&f);
|
|
|
|
} else if (p->conf.auth_backend == AUTH_BACKEND_LDAP) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-30 13:14:54 +00:00
|
|
|
int http_auth_match_rules(server *srv, array *req, const char *username, const char *group, const char *host) {
|
2005-02-20 14:27:00 +00:00
|
|
|
const char *r = NULL, *rules = NULL;
|
|
|
|
int username_len;
|
|
|
|
data_string *require;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
UNUSED(group);
|
|
|
|
UNUSED(host);
|
|
|
|
|
|
|
|
require = (data_string *)array_get_element(req, "require");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-07-31 11:49:26 +00:00
|
|
|
/* if we get here, the user we got a authed user */
|
|
|
|
if (0 == strcmp(require->value->ptr, "valid-user")) {
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* user=name1|group=name3|host=name4 */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* seperate the string by | */
|
|
|
|
#if 0
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb", "rules", require->value);
|
2006-10-04 13:26:23 +00:00
|
|
|
#endif
|
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
username_len = username ? strlen(username) : 0;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
r = rules = require->value->ptr;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
while (1) {
|
|
|
|
const char *eq;
|
|
|
|
const char *k, *v, *e;
|
|
|
|
int k_len, v_len, r_len;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
e = strchr(r, '|');
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (e) {
|
|
|
|
r_len = e - r;
|
|
|
|
} else {
|
|
|
|
r_len = strlen(rules) - (r - rules);
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* from r to r + r_len is a rule */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-07-31 11:49:26 +00:00
|
|
|
if (0 == strncmp(r, "valid-user", r_len)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb",
|
2005-11-26 12:47:09 +00:00
|
|
|
"parsing the 'require' section in 'auth.require' failed: valid-user cannot be combined with other require rules",
|
|
|
|
require->value);
|
2005-07-31 11:49:26 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* search for = in the rules */
|
|
|
|
if (NULL == (eq = strchr(r, '='))) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb",
|
|
|
|
"parsing the 'require' section in 'auth.require' failed: a = is missing",
|
2005-11-26 12:47:09 +00:00
|
|
|
require->value);
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* = out of range */
|
|
|
|
if (eq > r + r_len) {
|
2006-10-04 13:26:23 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "sb",
|
2005-11-26 12:47:09 +00:00
|
|
|
"parsing the 'require' section in 'auth.require' failed: = out of range",
|
|
|
|
require->value);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/* the part before the = is user|group|host */
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
k = r;
|
|
|
|
k_len = eq - r;
|
|
|
|
v = eq + 1;
|
|
|
|
v_len = r_len - k_len - 1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (k_len == 4) {
|
|
|
|
if (0 == strncmp(k, "user", k_len)) {
|
2006-10-04 13:26:23 +00:00
|
|
|
if (username &&
|
2005-02-20 14:27:00 +00:00
|
|
|
username_len == v_len &&
|
|
|
|
0 == strncmp(username, v, v_len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (0 == strncmp(k, "host", k_len)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "host ... (not implemented)");
|
|
|
|
} else {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "unknown key");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else if (k_len == 5) {
|
|
|
|
if (0 == strncmp(k, "group", k_len)) {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "group ... (not implemented)");
|
|
|
|
} else {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ss", "unknown key", k);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "unknown key");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (!e) break;
|
|
|
|
r = e + 1;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "s", "nothing matched");
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-04 07:46:12 +00:00
|
|
|
#define APR_MD5_DIGESTSIZE 16
|
|
|
|
#define APR1_ID "$apr1$"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following MD5 password encryption code was largely borrowed from
|
|
|
|
* the FreeBSD 3.0 /usr/src/lib/libcrypt/crypt.c file, which is
|
|
|
|
* licenced as stated at the top of this file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void to64(char *s, unsigned long v, int n)
|
|
|
|
{
|
2011-07-30 09:16:03 +00:00
|
|
|
static const unsigned char itoa64[] = /* 0 ... 63 => ASCII - 64 */
|
2006-10-04 07:46:12 +00:00
|
|
|
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
|
while (--n >= 0) {
|
|
|
|
*s++ = itoa64[v&0x3f];
|
|
|
|
v >>= 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void apr_md5_encode(const char *pw, const char *salt, char *result, size_t nbytes) {
|
|
|
|
/*
|
|
|
|
* Minimum size is 8 bytes for salt, plus 1 for the trailing NUL,
|
|
|
|
* plus 4 for the '$' separators, plus the password hash itself.
|
|
|
|
* Let's leave a goodly amount of leeway.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char passwd[120], *p;
|
|
|
|
const char *sp, *ep;
|
|
|
|
unsigned char final[APR_MD5_DIGESTSIZE];
|
|
|
|
ssize_t sl, pl, i;
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_CTX ctx, ctx1;
|
2006-10-04 07:46:12 +00:00
|
|
|
unsigned long l;
|
|
|
|
|
2006-10-04 13:26:23 +00:00
|
|
|
/*
|
2006-10-04 07:46:12 +00:00
|
|
|
* Refine the salt first. It's possible we were given an already-hashed
|
|
|
|
* string as the salt argument, so extract the actual salt value from it
|
|
|
|
* if so. Otherwise just use the string up to the first '$' as the salt.
|
|
|
|
*/
|
|
|
|
sp = salt;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it starts with the magic string, then skip that.
|
|
|
|
*/
|
|
|
|
if (!strncmp(sp, APR1_ID, strlen(APR1_ID))) {
|
|
|
|
sp += strlen(APR1_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It stops at the first '$' or 8 chars, whichever comes first
|
|
|
|
*/
|
|
|
|
for (ep = sp; (*ep != '\0') && (*ep != '$') && (ep < (sp + 8)); ep++) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the length of the true salt
|
|
|
|
*/
|
|
|
|
sl = ep - sp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 'Time to make the doughnuts..'
|
|
|
|
*/
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Init(&ctx);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2006-10-04 07:46:12 +00:00
|
|
|
/*
|
|
|
|
* The password first, since that is what is most unknown
|
|
|
|
*/
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx, pw, strlen(pw));
|
2006-10-04 07:46:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Then our magic string
|
|
|
|
*/
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx, APR1_ID, strlen(APR1_ID));
|
2006-10-04 07:46:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Then the raw salt
|
|
|
|
*/
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx, sp, sl);
|
2006-10-04 07:46:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Then just as many characters of the MD5(pw, salt, pw)
|
|
|
|
*/
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Init(&ctx1);
|
|
|
|
li_MD5_Update(&ctx1, pw, strlen(pw));
|
|
|
|
li_MD5_Update(&ctx1, sp, sl);
|
|
|
|
li_MD5_Update(&ctx1, pw, strlen(pw));
|
|
|
|
li_MD5_Final(final, &ctx1);
|
2006-10-04 07:46:12 +00:00
|
|
|
for (pl = strlen(pw); pl > 0; pl -= APR_MD5_DIGESTSIZE) {
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx, final,
|
2006-10-04 07:46:12 +00:00
|
|
|
(pl > APR_MD5_DIGESTSIZE) ? APR_MD5_DIGESTSIZE : pl);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't leave anything around in vm they could use.
|
|
|
|
*/
|
|
|
|
memset(final, 0, sizeof(final));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Then something really weird...
|
|
|
|
*/
|
|
|
|
for (i = strlen(pw); i != 0; i >>= 1) {
|
|
|
|
if (i & 1) {
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx, final, 1);
|
2006-10-04 07:46:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx, pw, 1);
|
2006-10-04 07:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now make the output string. We know our limitations, so we
|
|
|
|
* can use the string routines without bounds checking.
|
|
|
|
*/
|
|
|
|
strcpy(passwd, APR1_ID);
|
|
|
|
strncat(passwd, sp, sl);
|
|
|
|
strcat(passwd, "$");
|
|
|
|
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Final(final, &ctx);
|
2006-10-04 07:46:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* And now, just to make sure things don't run too fast..
|
|
|
|
* On a 60 Mhz Pentium this takes 34 msec, so you would
|
|
|
|
* need 30 seconds to build a 1000 entry dictionary...
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 1000; i++) {
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Init(&ctx1);
|
2006-10-04 07:46:12 +00:00
|
|
|
if (i & 1) {
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx1, pw, strlen(pw));
|
2006-10-04 07:46:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx1, final, APR_MD5_DIGESTSIZE);
|
2006-10-04 07:46:12 +00:00
|
|
|
}
|
|
|
|
if (i % 3) {
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx1, sp, sl);
|
2006-10-04 07:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i % 7) {
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx1, pw, strlen(pw));
|
2006-10-04 07:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (i & 1) {
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx1, final, APR_MD5_DIGESTSIZE);
|
2006-10-04 07:46:12 +00:00
|
|
|
}
|
|
|
|
else {
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&ctx1, pw, strlen(pw));
|
2006-10-04 07:46:12 +00:00
|
|
|
}
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Final(final,&ctx1);
|
2006-10-04 07:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p = passwd + strlen(passwd);
|
|
|
|
|
|
|
|
l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p, l, 4); p += 4;
|
|
|
|
l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p, l, 4); p += 4;
|
|
|
|
l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p, l, 4); p += 4;
|
|
|
|
l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p, l, 4); p += 4;
|
|
|
|
l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p, l, 4); p += 4;
|
|
|
|
l = final[11] ; to64(p, l, 2); p += 2;
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Don't leave anything around in vm they could use.
|
|
|
|
*/
|
|
|
|
memset(final, 0, sizeof(final));
|
|
|
|
|
|
|
|
/* FIXME
|
|
|
|
*/
|
|
|
|
#define apr_cpystrn strncpy
|
|
|
|
apr_cpystrn(result, passwd, nbytes - 1);
|
|
|
|
}
|
|
|
|
|
2013-04-29 13:08:25 +00:00
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
static void apr_sha_encode(const char *pw, char *result, size_t nbytes) {
|
|
|
|
static const unsigned char base64_data[65] =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
unsigned char digest[21]; /* multiple of 3 for base64 encoding */
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(result, 0, nbytes);
|
|
|
|
|
|
|
|
/* need 5 bytes for "{SHA}", 28 for base64 (3 bytes -> 4 bytes) of SHA1 (20 bytes), 1 terminating */
|
|
|
|
if (nbytes < 5 + 28 + 1) return;
|
|
|
|
|
|
|
|
SHA1((const unsigned char*) pw, strlen(pw), digest);
|
|
|
|
digest[20] = 0;
|
|
|
|
|
|
|
|
strcpy(result, "{SHA}");
|
|
|
|
result = result + 5;
|
|
|
|
for (i = 0; i < 21; i += 3) {
|
|
|
|
unsigned int v = (digest[i] << 16) | (digest[i+1] << 8) | digest[i+2];
|
|
|
|
result[3] = base64_data[v & 0x3f]; v >>= 6;
|
|
|
|
result[2] = base64_data[v & 0x3f]; v >>= 6;
|
|
|
|
result[1] = base64_data[v & 0x3f]; v >>= 6;
|
|
|
|
result[0] = base64_data[v & 0x3f];
|
|
|
|
result += 4;
|
|
|
|
}
|
|
|
|
result[-1] = '='; /* last digest character was already end of string, pad it */
|
|
|
|
*result = '\0';
|
|
|
|
}
|
|
|
|
#endif
|
2006-10-04 07:46:12 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
/**
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
*
|
2005-02-20 14:27:00 +00:00
|
|
|
* @param password password-string from the auth-backend
|
|
|
|
* @param pw password-string from the client
|
|
|
|
*/
|
|
|
|
|
|
|
|
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) {
|
|
|
|
UNUSED(srv);
|
|
|
|
UNUSED(req);
|
|
|
|
|
|
|
|
if (p->conf.auth_backend == AUTH_BACKEND_HTDIGEST) {
|
2006-10-04 13:26:23 +00:00
|
|
|
/*
|
2005-02-20 14:27:00 +00:00
|
|
|
* htdigest format
|
2006-10-04 13:26:23 +00:00
|
|
|
*
|
|
|
|
* user:realm:md5(user:realm:password)
|
2005-02-20 14:27:00 +00:00
|
|
|
*/
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_CTX Md5Ctx;
|
2005-02-20 14:27:00 +00:00
|
|
|
HASH HA1;
|
|
|
|
char a1[256];
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Init(&Md5Ctx);
|
2015-02-08 19:10:44 +00:00
|
|
|
li_MD5_Update(&Md5Ctx, CONST_BUF_LEN(username));
|
|
|
|
li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":"));
|
|
|
|
li_MD5_Update(&Md5Ctx, CONST_BUF_LEN(realm));
|
|
|
|
li_MD5_Update(&Md5Ctx, CONST_STR_LEN(":"));
|
2011-07-30 09:16:03 +00:00
|
|
|
li_MD5_Update(&Md5Ctx, (unsigned char *)pw, strlen(pw));
|
|
|
|
li_MD5_Final(HA1, &Md5Ctx);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
CvtHex(HA1, a1);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2005-02-20 14:27:00 +00:00
|
|
|
if (0 == strcmp(password->ptr, a1)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
} else if (p->conf.auth_backend == AUTH_BACKEND_HTPASSWD) {
|
2006-10-04 07:46:12 +00:00
|
|
|
char sample[120];
|
|
|
|
if (!strncmp(password->ptr, APR1_ID, strlen(APR1_ID))) {
|
|
|
|
/*
|
|
|
|
* The hash was created using $apr1$ custom algorithm.
|
|
|
|
*/
|
|
|
|
apr_md5_encode(pw, password->ptr, sample, sizeof(sample));
|
|
|
|
return (strcmp(sample, password->ptr) == 0) ? 0 : 1;
|
2013-04-29 13:08:25 +00:00
|
|
|
#ifdef USE_OPENSSL
|
|
|
|
} else if (0 == strncmp(password->ptr, "{SHA}", 5)) {
|
|
|
|
apr_sha_encode(pw, sample, sizeof(sample));
|
|
|
|
return (strcmp(sample, password->ptr) == 0) ? 0 : 1;
|
|
|
|
#endif
|
2006-10-04 07:46:12 +00:00
|
|
|
} else {
|
2015-02-12 06:39:39 +00:00
|
|
|
#if defined(HAVE_CRYPT_R) || defined(HAVE_CRYPT)
|
2013-04-29 13:08:23 +00:00
|
|
|
char *crypted;
|
2015-02-12 06:39:39 +00:00
|
|
|
#if defined(HAVE_CRYPT_R)
|
|
|
|
struct crypt_data crypt_tmp_data;
|
|
|
|
crypt_tmp_data.initialized = 0;
|
|
|
|
#endif
|
2005-09-16 10:40:45 +00:00
|
|
|
|
2013-04-29 13:08:23 +00:00
|
|
|
/* a simple DES password is 2 + 11 characters. everything else should be longer. */
|
|