index.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. define('VESTA_CMD', '/usr/bin/sudo /usr/local/vesta/bin/');
  3. if (isset($_POST['user']) || isset($_POST['hash'])) {
  4. // Authentication
  5. if (empty($_POST['hash'])) {
  6. if ($_POST['user'] != 'admin') {
  7. echo 'Error: authentication failed';
  8. exit;
  9. }
  10. $password = $_POST['password'];
  11. $v_ip = escapeshellarg($_SERVER['REMOTE_ADDR']);
  12. $output = '';
  13. exec (VESTA_CMD."v-get-user-salt admin ".$v_ip." json" , $output, $return_var);
  14. $pam = json_decode(implode('', $output), true);
  15. $salt = $pam['admin']['SALT'];
  16. $method = $pam['admin']['METHOD'];
  17. if ($method == 'md5' ) {
  18. $hash = crypt($password, '$1$'.$salt.'$');
  19. }
  20. if ($method == 'sha-512' ) {
  21. $hash = crypt($password, '$6$rounds=5000$'.$salt.'$');
  22. $hash = str_replace('$rounds=5000','',$hash);
  23. }
  24. if ($method == 'des' ) {
  25. $hash = crypt($password, $salt);
  26. }
  27. // Send hash via tmp file
  28. $v_hash = exec('mktemp -p /tmp');
  29. $fp = fopen($v_hash, "w");
  30. fwrite($fp, $hash."\n");
  31. fclose($fp);
  32. // Check user hash
  33. exec(VESTA_CMD ."v-check-user-hash admin ".$v_hash." ".$v_ip, $output, $return_var);
  34. unset($output);
  35. // Remove tmp file
  36. unlink($v_hash);
  37. // Check API answer
  38. if ( $return_var > 0 ) {
  39. echo 'Error: authentication failed';
  40. exit;
  41. }
  42. } else {
  43. $key = '/usr/local/vesta/data/keys/' . basename($_POST['hash']);
  44. if (file_exists($key) && is_file($key)) {
  45. exec(VESTA_CMD ."v-check-api-key ".escapeshellarg($key)." ".$v_ip, $output, $return_var);
  46. unset($output);
  47. // Check API answer
  48. if ( $return_var > 0 ) {
  49. echo 'Error: authentication failed';
  50. exit;
  51. }
  52. } else {
  53. $return_var = 1;
  54. }
  55. }
  56. if ( $return_var > 0 ) {
  57. echo 'Error: authentication failed';
  58. exit;
  59. }
  60. // Prepare arguments
  61. if (isset($_POST['cmd'])) $cmd = escapeshellarg($_POST['cmd']);
  62. if (isset($_POST['arg1'])) $arg1 = escapeshellarg($_POST['arg1']);
  63. if (isset($_POST['arg2'])) $arg2 = escapeshellarg($_POST['arg2']);
  64. if (isset($_POST['arg3'])) $arg3 = escapeshellarg($_POST['arg3']);
  65. if (isset($_POST['arg4'])) $arg4 = escapeshellarg($_POST['arg4']);
  66. if (isset($_POST['arg5'])) $arg5 = escapeshellarg($_POST['arg5']);
  67. if (isset($_POST['arg6'])) $arg6 = escapeshellarg($_POST['arg6']);
  68. if (isset($_POST['arg7'])) $arg7 = escapeshellarg($_POST['arg7']);
  69. if (isset($_POST['arg8'])) $arg8 = escapeshellarg($_POST['arg8']);
  70. if (isset($_POST['arg9'])) $arg9 = escapeshellarg($_POST['arg9']);
  71. // Build query
  72. $cmdquery = VESTA_CMD.$cmd." ";
  73. if(!empty($arg1)){
  74. $cmdquery = $cmdquery.$arg1." "; }
  75. if(!empty($arg2)){
  76. $cmdquery = $cmdquery.$arg2." "; }
  77. if(!empty($arg3)){
  78. $cmdquery = $cmdquery.$arg3." "; }
  79. if(!empty($arg4)){
  80. $cmdquery = $cmdquery.$arg4." "; }
  81. if(!empty($arg5)){
  82. $cmdquery = $cmdquery.$arg5." "; }
  83. if(!empty($arg6)){
  84. $cmdquery = $cmdquery.$arg6." "; }
  85. if(!empty($arg7)){
  86. $cmdquery = $cmdquery.$arg7." "; }
  87. if(!empty($arg8)){
  88. $cmdquery = $cmdquery.$arg8." "; }
  89. if(!empty($arg9)){
  90. $cmdquery = $cmdquery.$arg9; }
  91. // Check command
  92. if ($cmd == "'v-make-tmp-file'") {
  93. // Used in DNS Cluster
  94. $fp = fopen($_POST['arg2'], 'w');
  95. fwrite($fp, $_POST['arg1']."\n");
  96. fclose($fp);
  97. $return_var = 0;
  98. } else {
  99. // Run normal cmd query
  100. exec ($cmdquery, $output, $return_var);
  101. }
  102. if ((!empty($_POST['returncode'])) && ($_POST['returncode'] == 'yes')) {
  103. echo $return_var;
  104. } else {
  105. if (($return_var == 0) && (empty($output))) {
  106. echo "OK";
  107. } else {
  108. echo implode("\n",$output)."\n";
  109. }
  110. }
  111. }