|
4 | 4 | if (isset($_POST['user']) || isset($_POST['hash'])) { |
5 | 5 |
|
6 | 6 | // Authentication |
7 | | - $auth_code = 1; |
8 | 7 | if (empty($_POST['hash'])) { |
9 | | - // Check user permission to use API |
10 | 8 | if ($_POST['user'] != 'admin') { |
11 | | - echo 'Error: only admin is allowed to use API'; |
| 9 | + echo 'Error: authentication failed'; |
12 | 10 | exit; |
13 | 11 | } |
14 | 12 |
|
15 | | - $v_user = escapeshellarg($_POST['user']); |
16 | | - $v_password = tempnam("/tmp","vst"); |
17 | | - $fp = fopen($v_password, "w"); |
18 | | - fwrite($fp, $_POST['password']."\n"); |
| 13 | + $password = $_POST['password']; |
| 14 | + $v_ip = escapeshellarg($_SERVER['REMOTE_ADDR']); |
| 15 | + $output = ''; |
| 16 | + exec (VESTA_CMD."v-get-user-salt admin ".$v_ip." json" , $output, $return_var); |
| 17 | + $pam = json_decode(implode('', $output), true); |
| 18 | + $salt = $pam['admin']['SALT']; |
| 19 | + $method = $pam['admin']['METHOD']; |
| 20 | + |
| 21 | + if ($method == 'md5' ) { |
| 22 | + $hash = crypt($password, '$1$'.$salt.'$'); |
| 23 | + } |
| 24 | + if ($method == 'sha-512' ) { |
| 25 | + $hash = crypt($password, '$6$rounds=5000$'.$salt.'$'); |
| 26 | + $hash = str_replace('$rounds=5000','',$hash); |
| 27 | + } |
| 28 | + if ($method == 'des' ) { |
| 29 | + $hash = crypt($password, $salt); |
| 30 | + } |
| 31 | + |
| 32 | + // Send hash via tmp file |
| 33 | + $v_hash = exec('mktemp -p /tmp'); |
| 34 | + $fp = fopen($v_hash, "w"); |
| 35 | + fwrite($fp, $hash."\n"); |
19 | 36 | fclose($fp); |
20 | | - $v_ip_addr = escapeshellarg($_SERVER["REMOTE_ADDR"]); |
21 | | - exec(VESTA_CMD ."v-check-user-password ".$v_user." ".escapeshellarg($v_password)." '".$v_ip_addr."'", $output, $auth_code); |
22 | | - unlink($v_password); |
23 | | - /* No hash auth for security reason |
| 37 | + |
| 38 | + // Check user hash |
| 39 | + exec(VESTA_CMD ."v-check-user-hash admin ".$v_hash." ".$v_ip, $output, $return_var); |
| 40 | + unset($output); |
| 41 | + |
| 42 | + // Remove tmp file |
| 43 | + unlink($v_hash); |
| 44 | + |
| 45 | + // Check API answer |
| 46 | + if ( $return_var > 0 ) { |
| 47 | + echo 'Error: authentication failed'; |
| 48 | + exit; |
| 49 | + } |
24 | 50 | } else { |
25 | 51 | $key = '/usr/local/vesta/data/keys/' . basename($_POST['hash']); |
26 | 52 | if (file_exists($key) && is_file($key)) { |
27 | | - $auth_code = '0'; |
| 53 | + exec(VESTA_CMD ."v-check-api-key ".escapeshellarg($key)." ".$v_ip, $output, $return_var); |
| 54 | + unset($output); |
| 55 | + |
| 56 | + // Check API answer |
| 57 | + if ( $return_var > 0 ) { |
| 58 | + echo 'Error: authentication failed'; |
| 59 | + exit; |
| 60 | + } |
28 | 61 | } |
29 | | - */ |
30 | 62 | } |
31 | 63 |
|
32 | | - if ($auth_code != 0 ) { |
| 64 | + if ( $return_var > 0 ) { |
33 | 65 | echo 'Error: authentication failed'; |
34 | 66 | exit; |
35 | 67 | } |
|
0 commit comments