mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-22 23:59:41 +08:00
authcache switched to hashtables, overflow fixed
- authcache switched to use hashtables, size parameter added - overflow fixed on hashinit - hashtable prefers new values on insert if table is full - hashtable is able to compact/grow
This commit is contained in:
1 parent
a3729354b8
commit
7102afe856
6 files changed
+159
-159
No files matched your search
+79
-104
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
#include "proxy.h"
|
||||
#include "libs/blake2.h"
|
||||
|
||||
static FILTER_ACTION (*ext_ssl_parent)(struct clientparam * param) = NULL;
|
||||
|
||||
@@ -776,73 +777,71 @@ int checkACL(struct clientparam * param){
|
||||
}
|
||||
|
||||
struct authcache {
|
||||
char * username;
|
||||
char * password;
|
||||
time_t expires;
|
||||
PROXYSOCKADDRTYPE sa;
|
||||
PROXYSOCKADDRTYPE sinsl;
|
||||
struct ace *acl;
|
||||
struct authcache *next;
|
||||
} *authc = NULL;
|
||||
unsigned char username[64];
|
||||
#ifndef NOIPv6
|
||||
uint8_t sincr_addr[16];
|
||||
uint8_t sinsl_addr[16];
|
||||
#else
|
||||
uint8_t sincr_addr[4];
|
||||
uint8_t sinsl_addr[4];
|
||||
#endif
|
||||
uint16_t sincr_family;
|
||||
uint16_t sinsl_family;
|
||||
};
|
||||
|
||||
void param2hash(const void *index, uint8_t *hash, const unsigned char *rnd){
|
||||
blake2b_state S;
|
||||
const struct clientparam *param = (struct clientparam *)index;
|
||||
|
||||
blake2b_init_key(&S, HASH_SIZE, rnd, sizeof(unsigned)*4);
|
||||
if((conf.authcachetype & 1) && !(conf.authcachetype & 8))blake2b_update(&S, SAADDR(¶m->sincr), SAADDRLEN(¶m->sincr));
|
||||
if((conf.authcachetype & 2) && param->username)blake2b_update(&S, param->username, strlen((const char *)param->username));
|
||||
if((conf.authcachetype & 4) && param->password)blake2b_update(&S, param->password, strlen((const char *)param->password));
|
||||
if((conf.authcachetype & 16))blake2b_update(&S, ¶m->srv->acl, sizeof(param->srv->acl));
|
||||
blake2b_final(&S, hash, HASH_SIZE);
|
||||
}
|
||||
|
||||
struct hashtable auth_table = {0, sizeof(struct authcache), {0,0,0,0}, NULL, NULL, 0, param2hash};
|
||||
|
||||
|
||||
int cacheauth(struct clientparam * param){
|
||||
struct authcache *ac, *last=NULL;
|
||||
|
||||
pthread_mutex_lock(&hash_mutex);
|
||||
for(ac = authc; ac; ){
|
||||
if(ac->expires <= conf.time){
|
||||
if(ac->username)myfree(ac->username);
|
||||
if(ac->password)myfree(ac->password);
|
||||
if(!last){
|
||||
authc = ac->next;
|
||||
myfree(ac);
|
||||
ac = authc;
|
||||
}
|
||||
else {
|
||||
last->next = ac->next;
|
||||
myfree(ac);
|
||||
ac = last->next;
|
||||
}
|
||||
continue;
|
||||
|
||||
}
|
||||
if(
|
||||
(!(conf.authcachetype&2) || (param->username && ac->username && !strcmp(ac->username, (char *)param->username))) &&
|
||||
(!(conf.authcachetype&4) || (ac->password && param->password && !strcmp(ac->password, (char *)param->password))) &&
|
||||
(!(conf.authcachetype&16) || (ac->acl == param->srv->acl))
|
||||
) {
|
||||
|
||||
if(!(conf.authcachetype&1)
|
||||
|| ((*SAFAMILY(&ac->sa) == *SAFAMILY(¶m->sincr)
|
||||
&& !memcmp(SAADDR(&ac->sa), SAADDR(¶m->sincr), SAADDRLEN(&ac->sa))))){
|
||||
|
||||
if(conf.authcachetype&32) {
|
||||
param->sinsl = ac->sinsl;
|
||||
}
|
||||
if(param->username){
|
||||
myfree(param->username);
|
||||
}
|
||||
param->username = (unsigned char *)mystrdup(ac->username);
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
return 0;
|
||||
}
|
||||
else if ((conf.authcachetype&1) && (conf.authcachetype&8)) {
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
return 10;
|
||||
}
|
||||
}
|
||||
last = ac;
|
||||
ac = ac->next;
|
||||
struct authcache ac;
|
||||
uint32_t ttl;
|
||||
|
||||
|
||||
if(
|
||||
((conf.authcachetype & 2) && !param->username) ||
|
||||
((conf.authcachetype & 4) && !param->password) ||
|
||||
(
|
||||
(conf.authcachetype & 1) && *SAFAMILY(¶m->sincr) != AF_INET
|
||||
#ifndef NOIPv6
|
||||
&& *SAFAMILY(¶m->sincr) != AF_INET6
|
||||
#endif
|
||||
) || (!hashresolv(&auth_table, param, &ac, &ttl))) {
|
||||
return 4;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
return 4;
|
||||
if((conf.authcachetype & 1) &&(conf.authcachetype & 8) &&
|
||||
(ac.sincr_family != *SAFAMILY(¶m->sincr) ||
|
||||
memcmp(ac.sincr_addr, SAADDR(¶m->sincr), SAADDRLEN(¶m->sincr))
|
||||
)) {
|
||||
return 10;
|
||||
}
|
||||
|
||||
if(!(conf.authcachetype&2) && *ac.username){
|
||||
if(param->username) myfree(param->username);
|
||||
param->username = (unsigned char *)mystrdup((char *)ac.username);
|
||||
}
|
||||
if((conf.authcachetype & 32)){
|
||||
memset(¶m->sinsl, 0, sizeof(param->sinsl));
|
||||
*(SAFAMILY(¶m->sinsl)) = ac.sinsl_family;
|
||||
memcpy(SAADDR(¶m->sinsl), ac.sinsl_addr, SAADDRLEN(¶m->sinsl));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int doauth(struct clientparam * param){
|
||||
int res = 0;
|
||||
struct auth *authfuncs;
|
||||
struct authcache *ac;
|
||||
char * tmp;
|
||||
int ret = 0;
|
||||
|
||||
@@ -853,51 +852,27 @@ int doauth(struct clientparam * param){
|
||||
(res = (*authfuncs->authorize)(param)))
|
||||
return res;
|
||||
if(conf.authcachetype && authfuncs->authenticate && authfuncs->authenticate != cacheauth && param->username && (!(conf.authcachetype&4) || (!param->pwtype && param->password))){
|
||||
pthread_mutex_lock(&hash_mutex);
|
||||
for(ac = authc; ac; ac = ac->next){
|
||||
if(
|
||||
(!(conf.authcachetype&2) || !strcmp(ac->username, (char *)param->username)) &&
|
||||
(!(conf.authcachetype&1) || (*SAFAMILY(&ac->sa) == *SAFAMILY(¶m->sincr) && !memcmp(SAADDR(&ac->sa), SAADDR(¶m->sincr), SAADDRLEN(&ac->sa)))) &&
|
||||
(!(conf.authcachetype&4) || (ac->password && !strcmp(ac->password, (char *)param->password))) &&
|
||||
(!(conf.authcachetype&16) || (ac->acl == param->srv->acl))
|
||||
) {
|
||||
ac->expires = conf.time + conf.authcachetime;
|
||||
if(strcmp(ac->username, (char *)param->username)){
|
||||
tmp = ac->username;
|
||||
ac->username = mystrdup((char *)param->username);
|
||||
myfree(tmp);
|
||||
}
|
||||
if((conf.authcachetype&4)){
|
||||
tmp = ac->password;
|
||||
ac->password = mystrdup((char *)param->password);
|
||||
myfree(tmp);
|
||||
}
|
||||
ac->sa = param->sincr;
|
||||
if(conf.authcachetype&32) {
|
||||
ac->sinsl = param-> sinsl;
|
||||
*SAPORT(&ac->sinsl) = 0;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!ac){
|
||||
ac = myalloc(sizeof(struct authcache));
|
||||
if(ac){
|
||||
ac->expires = conf.time + conf.authcachetime;
|
||||
ac->username = param->username?mystrdup((char *)param->username):NULL;
|
||||
ac->sa = param->sincr;
|
||||
ac->password = NULL;
|
||||
if((conf.authcachetype&4) && param->password) ac->password = mystrdup((char *)param->password);
|
||||
if(conf.authcachetype&32) {
|
||||
ac->sinsl = param->sinsl;
|
||||
*SAPORT(&ac->sinsl) = 0;
|
||||
}
|
||||
}
|
||||
ac->next = authc;
|
||||
authc = ac;
|
||||
}
|
||||
pthread_mutex_unlock(&hash_mutex);
|
||||
struct authcache ac={};
|
||||
|
||||
if(param->username) strncpy((char *)ac.username, (char *)param->username, 64);
|
||||
if(*SAFAMILY(¶m->sincr) == AF_INET
|
||||
#ifndef NOIPv6
|
||||
|| *SAFAMILY(¶m->sincr) == AF_INET6
|
||||
#endif
|
||||
) {
|
||||
ac.sincr_family = *SAFAMILY(¶m->sincr);
|
||||
memcpy(ac.sincr_addr, SAADDR(¶m->sincr), SAADDRLEN(¶m->sincr));
|
||||
}
|
||||
|
||||
if(*SAFAMILY(¶m->sinsl) == AF_INET
|
||||
#ifndef NOIPv6
|
||||
|| *SAFAMILY(¶m->sinsl) == AF_INET6
|
||||
#endif
|
||||
) {
|
||||
ac.sinsl_family = *SAFAMILY(¶m->sinsl);
|
||||
memcpy(ac.sinsl_addr, SAADDR(¶m->sinsl), SAADDRLEN(¶m->sinsl));
|
||||
}
|
||||
hashadd(&auth_table, param, &ac, conf.time + conf.authcachetime);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user