From 18e79eac22786598ea6222f47a24c1f945ff4c79 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 18 Aug 2020 15:34:57 +0100 Subject: [PATCH] Use hash_find rather than hash_iter for unpwd check. --- src/security_default.c | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/security_default.c b/src/security_default.c index 1056f3cf..3ce90b87 100644 --- a/src/security_default.c +++ b/src/security_default.c @@ -888,7 +888,7 @@ static int mosquitto__memcmp_const(const void *a, const void *b, size_t len) int mosquitto_unpwd_check_default(struct mosquitto_db *db, struct mosquitto *context) { - struct mosquitto__unpwd *u, *tmp; + struct mosquitto__unpwd *u; struct mosquitto__unpwd *unpwd_ref; #ifdef WITH_TLS unsigned char hash[EVP_MAX_MD_SIZE]; @@ -921,32 +921,31 @@ int mosquitto_unpwd_check_default(struct mosquitto_db *db, struct mosquitto *con } } - HASH_ITER(hh, unpwd_ref, u, tmp){ - if(!strcmp(u->username, context->username)){ - if(u->password){ - if(context->password){ + HASH_FIND(hh, unpwd_ref, context->username, strlen(context->username), u); + if(u){ + if(u->password){ + if(context->password){ #ifdef WITH_TLS - rc = pw__digest(context->password, u->salt, u->salt_len, hash, &hash_len); - if(rc == MOSQ_ERR_SUCCESS){ - if(hash_len == u->password_len && !mosquitto__memcmp_const(u->password, hash, hash_len)){ - return MOSQ_ERR_SUCCESS; - }else{ - return MOSQ_ERR_AUTH; - } - }else{ - return rc; - } -#else - if(!strcmp(u->password, context->password)){ + rc = pw__digest(context->password, u->salt, u->salt_len, hash, &hash_len); + if(rc == MOSQ_ERR_SUCCESS){ + if(hash_len == u->password_len && !mosquitto__memcmp_const(u->password, hash, hash_len)){ return MOSQ_ERR_SUCCESS; + }else{ + return MOSQ_ERR_AUTH; } -#endif }else{ - return MOSQ_ERR_AUTH; + return rc; + } +#else + if(!strcmp(u->password, context->password)){ + return MOSQ_ERR_SUCCESS; } +#endif }else{ - return MOSQ_ERR_SUCCESS; + return MOSQ_ERR_AUTH; } + }else{ + return MOSQ_ERR_SUCCESS; } }