Skip to content

Commit 8a3f859

Browse files
committed
sha-512 passwords func
1 parent 1a7612c commit 8a3f859

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

bin/v-add-mail-account

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ is_password_valid
4848
# Action #
4949
#----------------------------------------------------------#
5050

51-
if [ -x '/usr/bin/doveadm' ]; then
52-
md5=$(/usr/bin/doveadm pw -s md5 -p "$password")
53-
else
54-
md5=$(/usr/sbin/dovecotpw -s md5 -p "$password")
55-
fi
51+
# Generating hashed password
52+
salt=$(gen_password "$PW_MATRIX" "8")
53+
md5="{MD5}$($BIN/v-generate-password-hash md5 $salt <<<$password)"
5654

55+
# Adding account info into password file
5756
if [[ "$MAIL_SYSTEM" =~ exim ]]; then
5857
str="$account:$md5:$user:mail::$HOMEDIR/$user:$quota"
5958
echo $str >> $HOMEDIR/$user/conf/mail/$domain/passwd

bin/v-change-mail-account-password

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ is_password_valid
4747
# Action #
4848
#----------------------------------------------------------#
4949

50-
if [ -x '/usr/bin/doveadm' ]; then
51-
md5=$(/usr/bin/doveadm pw -s md5 -p "$password")
52-
else
53-
md5=$(/usr/sbin/dovecotpw -s md5 -p "$password")
54-
fi
50+
# Generating hashed password
51+
salt=$(gen_password "$PW_MATRIX" "8")
52+
md5="{MD5}$($BIN/v-generate-password-hash md5 $salt <<<$password)"
5553

5654
if [[ "$MAIL_SYSTEM" =~ exim ]]; then
5755
sed -i "/^$account:/d" $HOMEDIR/$user/conf/mail/$domain/passwd

bin/v-check-user-password

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,6 @@ if [[ -z "$password" ]]; then
4343
exit 9
4444
fi
4545

46-
# Checking mkpasswd command
47-
which mkpasswd >/dev/null 2>&1
48-
if [ $? -ne 0 ]; then
49-
# Activating fallback procedure
50-
if [ -e "/usr/bin/yum" ]; then
51-
yum install -y expect >/dev/null 2>&1
52-
else
53-
apt-get install -y expect >/dev/null 2>&1
54-
fi
55-
fi
56-
5746

5847
#----------------------------------------------------------#
5948
# Action #
@@ -68,7 +57,7 @@ if [[ -z "$salt" ]] || [[ "${#salt}" -gt 8 ]]; then
6857
fi
6958

7059
# Generating SHA-512
71-
hash=$(mkpasswd -m sha-512 -S $salt -s <<< $password)
60+
hash=$($BIN/v-generate-password-hash sha-512 $salt <<< $password)
7261
if [[ -z "$hash" ]]; then
7362
echo "Error: password missmatch"
7463
echo "$DATE $user $ip failed to login" >> $VESTA/log/auth.log

bin/v-generate-password-hash

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/local/vesta/php/bin/php
2+
<?php
3+
//# info: generate password hash
4+
//# options: HASH-METHOD SALT PASSWORD
5+
//
6+
//# The function generates password hash
7+
8+
// Checking arguments
9+
if ((empty($argv[1])) || (empty($argv[2]))) {
10+
echo "Error: not enought arguments\n";
11+
echo "Usage: " . $argv[0] ." HASH-METHOD SALT PASSWORD\n";
12+
exit(1);
13+
}
14+
15+
$crypt = $argv[1];
16+
$salt = $argv[2];
17+
if (empty($argv[3])) {
18+
$password = file_get_contents("php://stdin");
19+
$password = str_replace("\n",'',$password);
20+
} else {
21+
$password = $argv[3];
22+
}
23+
24+
// Generating MD5 hash
25+
if ($crypt == 'md5' ) {
26+
$hash = crypt($password, '$1$'.$salt.'$');
27+
}
28+
29+
// Generating SHA-512 hash
30+
if ($crypt == 'sha-512' ) {
31+
$hash = crypt($password, '$6$rounds=5000$'.$salt.'$');
32+
$hash = str_replace('$rounds=5000','',$hash);
33+
}
34+
35+
// Printing result
36+
echo $hash . "\n";

0 commit comments

Comments
 (0)