Skip to content

Commit e77c9d9

Browse files
authored
Nitpicking bin/v-generate-password-hash (hestiacp#3874)
* nitpicking v-generate-password-hash hestiacp#1: some errors were sent in stdout, this is a problem because in other parts of HestiaCP, for example in v-check-user-password, we just check if it printed anything at all in stdout, like hash=$($BIN/v-generate-password-hash "$method" "$salt" <<< "$password") if [[ -z "$hash" ]]; then echo "Error: password missmatch" here $hash is not supposed to be "Error: not enough arguments" also made sure we return exit code 1 (error) if the HASH_METHOD is invalid
1 parent 61bca69 commit e77c9d9

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

bin/v-generate-password-hash

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,34 @@
1010

1111
// Checking arguments
1212
if ((empty($argv[1])) || (empty($argv[2]))) {
13-
echo "Error: not enought arguments\n";
14-
echo "Usage: " . $argv[0] ." HASH_METHOD SALT PASSWORD\n";
13+
$errstr = "Error: not enought arguments\n";
14+
$errstr .= "Usage: " . $argv[0] ." HASH_METHOD SALT PASSWORD (or the password can be sent in STDIN)\n";
15+
fwrite(STDERR, $errstr);
1516
exit(1);
1617
}
1718

1819
$crypt = $argv[1];
1920
$salt = $argv[2];
2021
if (empty($argv[3])) {
2122
$password = file_get_contents("php://stdin");
22-
$password = str_replace("\n",'',$password);
23+
$password = str_replace("\n", '', $password);
2324
} else {
2425
$password = $argv[3];
2526
}
2627

2728
// Generating MD5 hash
2829
if ($crypt == 'md5' ) {
2930
$hash = crypt($password, '$1$'.$salt.'$');
30-
}
31-
32-
// Generating SHA-512 hash
33-
if ($crypt == 'sha-512' ) {
31+
} elseif ($crypt == 'sha-512' ) {
3432
$hash = crypt($password, '$6$rounds=5000$'.$salt.'$');
3533
$hash = str_replace('$rounds=5000','',$hash);
36-
}
37-
38-
// Generating base64 hash
39-
if ($crypt == 'htpasswd' ) {
34+
} elseif ($crypt == 'htpasswd' ) {
4035
$hash = crypt($password, base64_encode($password));
41-
}
42-
43-
// Generating DES hash
44-
if ($crypt == 'des' ) {
36+
} elseif ($crypt == 'des' ) {
4537
$hash = crypt($password, $salt);
38+
} else {
39+
fwrite(STDERR, "Error: argument #1 (HASH_METHOD) is invalid. It must be one of 'md5', 'sha-512', 'htpasswd', 'des'\n");
40+
exit(1);
4641
}
4742

4843
// Printing result

0 commit comments

Comments
 (0)