index.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. define('HESTIA_CMD', '/usr/bin/sudo /usr/local/hestia/bin/');
  3. function api($hst_hash, $hst_user, $hst_password, $hst_returncode, $hst_cmd, $hst_arg1, $hst_arg2, $hst_arg3, $hst_arg4, $hst_arg5, $hst_arg6, $hst_arg7, $hst_arg8, $hst_arg9){
  4. //This exists, so native JSON can be used without the repeating the code twice, so future code changes are easier and dont need to be replicated twice
  5. // Authentication
  6. if (empty($hst_hash)) {
  7. if ($hst_user != 'admin') {
  8. echo 'Error: authentication failed';
  9. exit;
  10. }
  11. $password = $hst_password;
  12. if (!isset($password)){
  13. echo 'Error: missing authentication';
  14. exit;
  15. }
  16. $v_ip = escapeshellarg($_SERVER['REMOTE_ADDR']);
  17. $output = '';
  18. exec (HESTIA_CMD."v-get-user-salt admin ".$v_ip." json" , $output, $return_var);
  19. $pam = json_decode(implode('', $output), true);
  20. $salt = $pam['admin']['SALT'];
  21. $method = $pam['admin']['METHOD'];
  22. if ($method == 'md5' ) {
  23. $hash = crypt($password, '$1$'.$salt.'$');
  24. }
  25. if ($method == 'sha-512' ) {
  26. $hash = crypt($password, '$6$rounds=5000$'.$salt.'$');
  27. $hash = str_replace('$rounds=5000','',$hash);
  28. }
  29. if ($method == 'des' ) {
  30. $hash = crypt($password, $salt);
  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");
  36. fclose($fp);
  37. // Check user hash
  38. exec(HESTIA_CMD ."v-check-user-hash admin ".$v_hash." ".$v_ip, $output, $return_var);
  39. unset($output);
  40. // Remove tmp file
  41. unlink($v_hash);
  42. // Check API answer
  43. if ( $return_var > 0 ) {
  44. echo 'Error: authentication failed';
  45. exit;
  46. }
  47. } else {
  48. $key = '/usr/local/hestia/data/keys/' . basename($hst_hash);
  49. if (file_exists($key) && is_file($key)) {
  50. exec(HESTIA_CMD ."v-check-api-key ".escapeshellarg($key)." ".$v_ip, $output, $return_var);
  51. unset($output);
  52. // Check API answer
  53. if ( $return_var > 0 ) {
  54. echo 'Error: authentication failed';
  55. exit;
  56. }
  57. } else {
  58. echo 'Error: authentication failed';
  59. exit;
  60. }
  61. }
  62. // Prepare arguments
  63. if (isset($hst_cmd)) $cmd = escapeshellarg($hst_cmd);
  64. if (isset($hst_arg1)) $arg1 = escapeshellarg($hst_arg1);
  65. if (isset($hst_arg2)) $arg2 = escapeshellarg($hst_arg2);
  66. if (isset($hst_arg3)) $arg3 = escapeshellarg($hst_arg3);
  67. if (isset($hst_arg4)) $arg4 = escapeshellarg($hst_arg4);
  68. if (isset($hst_arg5)) $arg5 = escapeshellarg($hst_arg5);
  69. if (isset($hst_arg6)) $arg6 = escapeshellarg($hst_arg6);
  70. if (isset($hst_arg7)) $arg7 = escapeshellarg($hst_arg7);
  71. if (isset($hst_arg8)) $arg8 = escapeshellarg($hst_arg8);
  72. if (isset($hst_arg9)) $arg9 = escapeshellarg($hst_arg9);
  73. // Build query
  74. $cmdquery = HESTIA_CMD.$cmd." ";
  75. if(!empty($arg1)){
  76. $cmdquery = $cmdquery.$arg1." "; }
  77. if(!empty($arg2)){
  78. $cmdquery = $cmdquery.$arg2." "; }
  79. if(!empty($arg3)){
  80. $cmdquery = $cmdquery.$arg3." "; }
  81. if(!empty($arg4)){
  82. $cmdquery = $cmdquery.$arg4." "; }
  83. if(!empty($arg5)){
  84. $cmdquery = $cmdquery.$arg5." "; }
  85. if(!empty($arg6)){
  86. $cmdquery = $cmdquery.$arg6." "; }
  87. if(!empty($arg7)){
  88. $cmdquery = $cmdquery.$arg7." "; }
  89. if(!empty($arg8)){
  90. $cmdquery = $cmdquery.$arg8." "; }
  91. if(!empty($arg9)){
  92. $cmdquery = $cmdquery.$arg9; }
  93. // Check command
  94. if ($cmd == "'v-make-tmp-file'") {
  95. // Used in DNS Cluster
  96. $fp = fopen($hst_arg2, 'w');
  97. fwrite($fp, $hst_arg1."\n");
  98. fclose($fp);
  99. $return_var = 0;
  100. } else {
  101. // Run normal cmd query
  102. exec ($cmdquery, $output, $return_var);
  103. }
  104. if ((!empty($hst_returncode)) && ($hst_returncode == 'yes')) {
  105. echo $return_var;
  106. } else {
  107. if (($return_var == 0) && (empty($output))) {
  108. echo "OK";
  109. } else {
  110. echo implode("\n",$output)."\n";
  111. }
  112. }
  113. }
  114. if (isset($_POST['user']) || isset($_POST['hash'])) {
  115. api($_POST['hash'], $_POST['user'], $_POST['password'], $_POST['returncode'], $_POST['cmd'], $_POST['arg1'], $_POST['arg2'], $_POST['arg3'], $_POST['arg4'], $_POST['arg5'], $_POST['arg6'], $_POST['arg7'], $_POST['arg8'], $_POST['arg9']);
  116. } else if (json_decode(file_get_contents("php://input"), true) != NULL){ //JSON POST support
  117. $json_data = json_decode(file_get_contents("php://input"), true);
  118. api($json_data['hash'], $json_data['user'], $json_data['password'], $json_data['returncode'], $json_data['cmd'], $json_data['arg1'], $json_data['arg2'], $json_data['arg3'], $json_data['arg4'], $json_data['arg5'], $json_data['arg6'], $json_data['arg7'], $json_data['arg8'], $json_data['arg9']);
  119. } else {
  120. echo "Error: data received is null or invalid, check https://docs.hestiacp.com/admin_docs/rest_api.html";
  121. exit;
  122. }
  123. ?>