index.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. define('HESTIA_CMD', '/usr/bin/sudo /usr/local/hestia/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. if (!isset($password)){
  12. echo 'Error: missing authentication';
  13. exit;
  14. }
  15. $v_ip = escapeshellarg($_SERVER['REMOTE_ADDR']);
  16. $output = '';
  17. exec (HESTIA_CMD."v-get-user-salt admin ".$v_ip." json" , $output, $return_var);
  18. $pam = json_decode(implode('', $output), true);
  19. $salt = $pam['admin']['SALT'];
  20. $method = $pam['admin']['METHOD'];
  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. // Send hash via tmp file
  32. $v_hash = exec('mktemp -p /tmp');
  33. $fp = fopen($v_hash, "w");
  34. fwrite($fp, $hash."\n");
  35. fclose($fp);
  36. // Check user hash
  37. exec(HESTIA_CMD ."v-check-user-hash admin ".$v_hash." ".$v_ip, $output, $return_var);
  38. unset($output);
  39. // Remove tmp file
  40. unlink($v_hash);
  41. // Check API answer
  42. if ( $return_var > 0 ) {
  43. echo 'Error: authentication failed';
  44. exit;
  45. }
  46. } else {
  47. $key = '/usr/local/hestia/data/keys/' . basename($_POST['hash']);
  48. if (file_exists($key) && is_file($key)) {
  49. exec(HESTIA_CMD ."v-check-api-key ".escapeshellarg($key)." ".$v_ip, $output, $return_var);
  50. unset($output);
  51. // Check API answer
  52. if ( $return_var > 0 ) {
  53. echo 'Error: authentication failed';
  54. exit;
  55. }
  56. } else {
  57. echo 'Error: authentication failed';
  58. exit;
  59. }
  60. }
  61. // Prepare arguments
  62. if (isset($_POST['cmd'])) $cmd = escapeshellarg($_POST['cmd']);
  63. if (isset($_POST['arg1'])) $arg1 = escapeshellarg($_POST['arg1']);
  64. if (isset($_POST['arg2'])) $arg2 = escapeshellarg($_POST['arg2']);
  65. if (isset($_POST['arg3'])) $arg3 = escapeshellarg($_POST['arg3']);
  66. if (isset($_POST['arg4'])) $arg4 = escapeshellarg($_POST['arg4']);
  67. if (isset($_POST['arg5'])) $arg5 = escapeshellarg($_POST['arg5']);
  68. if (isset($_POST['arg6'])) $arg6 = escapeshellarg($_POST['arg6']);
  69. if (isset($_POST['arg7'])) $arg7 = escapeshellarg($_POST['arg7']);
  70. if (isset($_POST['arg8'])) $arg8 = escapeshellarg($_POST['arg8']);
  71. if (isset($_POST['arg9'])) $arg9 = escapeshellarg($_POST['arg9']);
  72. // Build query
  73. $cmdquery = HESTIA_CMD.$cmd." ";
  74. if(!empty($arg1)){
  75. $cmdquery = $cmdquery.$arg1." "; }
  76. if(!empty($arg2)){
  77. $cmdquery = $cmdquery.$arg2." "; }
  78. if(!empty($arg3)){
  79. $cmdquery = $cmdquery.$arg3." "; }
  80. if(!empty($arg4)){
  81. $cmdquery = $cmdquery.$arg4." "; }
  82. if(!empty($arg5)){
  83. $cmdquery = $cmdquery.$arg5." "; }
  84. if(!empty($arg6)){
  85. $cmdquery = $cmdquery.$arg6." "; }
  86. if(!empty($arg7)){
  87. $cmdquery = $cmdquery.$arg7." "; }
  88. if(!empty($arg8)){
  89. $cmdquery = $cmdquery.$arg8." "; }
  90. if(!empty($arg9)){
  91. $cmdquery = $cmdquery.$arg9; }
  92. // Check command
  93. if ($cmd == "'v-make-tmp-file'") {
  94. // Used in DNS Cluster
  95. $fp = fopen($_POST['arg2'], 'w');
  96. fwrite($fp, $_POST['arg1']."\n");
  97. fclose($fp);
  98. $return_var = 0;
  99. } else {
  100. // Run normal cmd query
  101. exec ($cmdquery, $output, $return_var);
  102. }
  103. if ((!empty($_POST['returncode'])) && ($_POST['returncode'] == 'yes')) {
  104. echo $return_var;
  105. } else {
  106. if (($return_var == 0) && (empty($output))) {
  107. echo "OK";
  108. } else {
  109. echo implode("\n",$output)."\n";
  110. }
  111. }
  112. } else {
  113. echo "Error: data received is null or invalid, check https://docs.hestiacp.com/admin_docs/rest_api.html";
  114. exit;
  115. }