index.php 6.6 KB

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