index.php 5.6 KB

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