index.php 6.7 KB

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