Skip to content

Commit 4c6dadd

Browse files
authored
Merge pull request #1679 from pi-hole/new/envvars
Add environmental variables support
2 parents 74432a9 + 6f8bc4e commit 4c6dadd

File tree

8 files changed

+343
-46
lines changed

8 files changed

+343
-46
lines changed

src/api/config.c

+1-19
Original file line numberDiff line numberDiff line change
@@ -281,26 +281,8 @@ static const char *getJSONvalue(struct conf_item *conf_item, cJSON *elem, struct
281281
break;
282282
}
283283

284-
// Get password hash as allocated string (an empty string is hashed to an empty string)
285-
char *pwhash = strlen(elem->valuestring) > 0 ? create_password(elem->valuestring) : strdup("");
286-
287-
// Verify that the password hash is valid
288-
if(verify_password(elem->valuestring, pwhash, false) != PASSWORD_CORRECT)
289-
{
290-
free(pwhash);
284+
if(!set_and_check_password(conf_item, elem->valuestring))
291285
return "Failed to create password hash (verification failed), password remains unchanged";
292-
}
293-
294-
// Get pointer to pwhash instead
295-
conf_item--;
296-
297-
// Free previously allocated memory (if applicable)
298-
if(conf_item->t == CONF_STRING_ALLOCATED)
299-
free(conf_item->v.s);
300-
301-
// Set item
302-
conf_item->v.s = pwhash;
303-
log_debug(DEBUG_CONFIG, "Set %s to \"%s\"", conf_item->k, conf_item->v.s);
304286

305287
break;
306288
}

src/config/password.c

+27
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,30 @@ int run_performance_test(void)
570570

571571
return EXIT_SUCCESS;
572572
}
573+
574+
bool set_and_check_password(struct conf_item *conf_item, const char *password)
575+
{
576+
// Get password hash as allocated string (an empty string is hashed to an empty string)
577+
char *pwhash = strlen(password) > 0 ? create_password(password) : strdup("");
578+
579+
// Verify that the password hash is valid
580+
if(verify_password(password, pwhash, false) != PASSWORD_CORRECT)
581+
{
582+
free(pwhash);
583+
log_warn("Failed to create password hash (verification failed), password remains unchanged");
584+
return false;
585+
}
586+
587+
// Get pointer to pwhash instead
588+
conf_item--;
589+
590+
// Free previously allocated memory (if applicable)
591+
if(conf_item->t == CONF_STRING_ALLOCATED)
592+
free(conf_item->v.s);
593+
594+
// Set item
595+
conf_item->v.s = pwhash;
596+
log_debug(DEBUG_CONFIG, "Set %s to \"%s\"", conf_item->k, conf_item->v.s);
597+
598+
return true;
599+
}

src/config/password.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void sha256_raw_to_hex(uint8_t *data, char *buffer);
1818
char *create_password(const char *password) __attribute__((malloc));
1919
char verify_password(const char *password, const char *pwhash, const bool rate_limiting);
2020
int run_performance_test(void);
21+
bool set_and_check_password(struct conf_item *conf_item, const char *password);
2122

2223
enum password_result {
2324
PASSWORD_INCORRECT = 0,

0 commit comments

Comments
 (0)