index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. use function Hestiacp\quoteshellarg\quoteshellarg;
  3. try {
  4. require_once "../inc/vendor/autoload.php";
  5. } catch (Throwable $ex) {
  6. $errstr =
  7. "Unable to load required libraries. Please run v-add-sys-dependencies in command line. Error: " .
  8. $ex->getMessage();
  9. trigger_error($errstr);
  10. echo $errstr;
  11. exit(1);
  12. }
  13. //die("Error: Disabled");
  14. define("HESTIA_DIR_BIN", "/usr/local/hestia/bin/");
  15. define("HESTIA_CMD", "/usr/bin/sudo /usr/local/hestia/bin/");
  16. include $_SERVER["DOCUMENT_ROOT"] . "/inc/helpers.php";
  17. /**
  18. * Displays the error message, checks the proper code and saves a log if needed.
  19. *
  20. * @param int $exit_code
  21. * @param string $message
  22. * @param bool $add_log
  23. * @param string $user
  24. * @return void
  25. */
  26. function api_error($exit_code, $message, bool $add_log = false, $user = "system") {
  27. $message = trim(is_array($message) ? implode("\n", $message) : $message);
  28. // Add log
  29. if ($add_log) {
  30. $v_real_user_ip = get_real_user_ip();
  31. hst_add_history_log("[$v_real_user_ip] $message", "API", "Error", $user);
  32. }
  33. // Print the message with http_code and exit_code
  34. $http_code = $exit_code >= 100 ? $exit_code : exit_code_to_http_code($exit_code);
  35. header("Hestia-Exit-Code: $exit_code");
  36. http_response_code($http_code);
  37. echo !preg_match("/^Error:/", $message) ? "Error: $message" : $message;
  38. exit();
  39. }
  40. /**
  41. * Legacy connection format using hash or user and password.
  42. *
  43. * @param array{user: string?, pass: string?, hash?: string, cmd: string, arg1?: string, arg2?: string, arg3?: string, arg4?: string, arg5?: string, arg6?: string, arg7?: string, arg8?: string, arg9?: string, returncode?: string} $request_data
  44. * @return void
  45. * @return void
  46. */
  47. function api_legacy(array $request_data) {
  48. exec(HESTIA_CMD . "v-list-sys-config json", $output, $return_var);
  49. $settings = json_decode(implode("", $output), true);
  50. unset($output);
  51. if ($settings["config"]["API"] != "yes") {
  52. echo "Error: API has been disabled";
  53. exit();
  54. }
  55. if ($settings["config"]["API_ALLOWED_IP"] != "allow-all") {
  56. $ip_list = explode(",", $settings["config"]["API_ALLOWED_IP"]);
  57. $ip_list[] = "";
  58. if (!in_array(get_real_user_ip(), $ip_list)) {
  59. echo "Error: IP is not allowed to connect with API";
  60. exit();
  61. }
  62. }
  63. //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
  64. // Authentication
  65. if (empty($request_data["hash"])) {
  66. if ($request_data["user"] != "admin") {
  67. echo "Error: authentication failed";
  68. exit();
  69. }
  70. $password = $request_data["password"];
  71. if (!isset($password)) {
  72. echo "Error: missing authentication";
  73. exit();
  74. }
  75. $v_ip = quoteshellarg(get_real_user_ip());
  76. unset($output);
  77. exec(HESTIA_CMD . "v-get-user-salt admin " . $v_ip . " json", $output, $return_var);
  78. $pam = json_decode(implode("", $output), true);
  79. $salt = $pam["admin"]["SALT"];
  80. $method = $pam["admin"]["METHOD"];
  81. if ($method == "md5") {
  82. $hash = crypt($password, '$1$' . $salt . '$');
  83. }
  84. if ($method == "sha-512") {
  85. $hash = crypt($password, '$6$rounds=5000$' . $salt . '$');
  86. $hash = str_replace('$rounds=5000', "", $hash);
  87. }
  88. if ($method == "yescrypt") {
  89. $fp = tmpfile();
  90. $v_password = stream_get_meta_data($fp)["uri"];
  91. fwrite($fp, $password . "\n");
  92. unset($output);
  93. exec(
  94. HESTIA_CMD .
  95. 'v-check-user-password "admin" ' .
  96. quoteshellarg($v_password) .
  97. " " .
  98. $v_ip .
  99. " yes",
  100. $output,
  101. $return_var,
  102. );
  103. $hash = $output[0];
  104. fclose($fp);
  105. unset($output, $fp, $v_password);
  106. }
  107. if ($method == "des") {
  108. $hash = crypt($password, $salt);
  109. }
  110. // Send hash via tmp file
  111. $v_hash = exec("mktemp -p /tmp");
  112. $fp = fopen($v_hash, "w");
  113. fwrite($fp, $hash . "\n");
  114. fclose($fp);
  115. // Check user hash
  116. exec(HESTIA_CMD . "v-check-user-hash admin " . $v_hash . " " . $v_ip, $output, $return_var);
  117. unset($output);
  118. // Remove tmp file
  119. unlink($v_hash);
  120. // Check API answer
  121. if ($return_var > 0) {
  122. echo "Error: authentication failed";
  123. exit();
  124. }
  125. } else {
  126. $key = "/usr/local/hestia/data/keys/" . basename($request_data["hash"]);
  127. $v_ip = quoteshellarg(get_real_user_ip());
  128. exec(
  129. HESTIA_CMD . "v-check-api-key " . quoteshellarg($key) . " " . $v_ip,
  130. $output,
  131. $return_var,
  132. );
  133. unset($output);
  134. // Check API answer
  135. if ($return_var > 0) {
  136. echo "Error: authentication failed";
  137. exit();
  138. }
  139. }
  140. $hst_return = ($request_data["returncode"] ?? "no") === "yes" ? "code" : "data";
  141. $hst_cmd = trim($request_data["cmd"] ?? "");
  142. $hst_cmd_args = [];
  143. for ($i = 1; $i <= 9; $i++) {
  144. if (isset($request_data["arg{$i}"])) {
  145. $hst_cmd_args["arg{$i}"] = trim($request_data["arg{$i}"]);
  146. }
  147. }
  148. if (empty($hst_cmd)) {
  149. api_error(E_INVALID, "Command not provided");
  150. } elseif (!preg_match('/^[a-zA-Z0-9_-]+$/', $hst_cmd)) {
  151. api_error(E_INVALID, "$hst_cmd command invalid");
  152. }
  153. // Check command
  154. if ($hst_cmd == "v-make-tmp-file") {
  155. // Used in DNS Cluster
  156. $fp = fopen("/tmp/" . basename(escapeshellcmd($hst_cmd_args["arg2"])), "w");
  157. fwrite($fp, $hst_cmd_args["arg1"] . "\n");
  158. fclose($fp);
  159. $return_var = 0;
  160. } else {
  161. // Prepare command
  162. $cmdquery = HESTIA_CMD . escapeshellcmd($hst_cmd);
  163. // Prepare arguments
  164. foreach ($hst_cmd_args as $cmd_arg) {
  165. $cmdquery .= " " . quoteshellarg($cmd_arg);
  166. }
  167. // Run cmd query
  168. exec($cmdquery, $output, $cmd_exit_code);
  169. }
  170. if (!empty($hst_return) && $hst_return == "code") {
  171. echo $cmd_exit_code;
  172. } else {
  173. if ($return_var == 0 && empty($output)) {
  174. echo "OK";
  175. } else {
  176. echo implode("\n", $output) . "\n";
  177. }
  178. }
  179. exit();
  180. }
  181. /**
  182. * Connection using access key.
  183. *
  184. * @param array{access_key: string, secret_key: string, cmd: string, arg1?: string, arg2?: string, arg3?: string, arg4?: string, arg5?: string, arg6?: string, arg7?: string, arg8?: string, arg9?: string, returncode?: string} $request_data
  185. * @return void
  186. */
  187. function api_connection(array $request_data) {
  188. $v_real_user_ip = get_real_user_ip();
  189. exec(HESTIA_CMD . "v-list-sys-config json", $output, $return_var);
  190. $settings = json_decode(implode("", $output), true);
  191. unset($output, $return_var);
  192. // Get the status of api
  193. $api_status =
  194. !empty($settings["config"]["API_SYSTEM"]) && is_numeric($settings["config"]["API_SYSTEM"])
  195. ? $settings["config"]["API_SYSTEM"]
  196. : 0;
  197. if ($api_status == 0) {
  198. // Check if API is disabled for all users
  199. api_error(E_DISABLED, "API has been disabled");
  200. }
  201. // Check if API access is enabled for the user
  202. if ($settings["config"]["API_ALLOWED_IP"] != "allow-all") {
  203. $ip_list = explode(",", $settings["config"]["API_ALLOWED_IP"]);
  204. $ip_list[] = "";
  205. if (!in_array($v_real_user_ip, $ip_list) && !in_array("0.0.0.0", $ip_list)) {
  206. api_error(E_FORBIDDEN, "IP is not allowed to connect with API");
  207. }
  208. }
  209. // Get POST Params
  210. $hst_access_key_id = trim($request_data["access_key"] ?? "");
  211. $hst_secret_access_key = trim($request_data["secret_key"] ?? "");
  212. $hst_return = ($request_data["returncode"] ?? "no") === "yes" ? "code" : "data";
  213. $hst_cmd = trim($request_data["cmd"] ?? "");
  214. $hst_cmd_args = [];
  215. for ($i = 1; $i <= 9; $i++) {
  216. if (isset($request_data["arg{$i}"])) {
  217. $hst_cmd_args["arg{$i}"] = trim($request_data["arg{$i}"]);
  218. }
  219. }
  220. if (empty($hst_cmd)) {
  221. api_error(E_INVALID, "Command not provided");
  222. } elseif (!preg_match('/^[a-zA-Z0-9_-]+$/', $hst_cmd)) {
  223. api_error(E_INVALID, "$hst_cmd command invalid");
  224. }
  225. if (empty($hst_access_key_id) || empty($hst_secret_access_key)) {
  226. api_error(E_PASSWORD, "Authentication failed");
  227. }
  228. // Authenticates the key and checks permission to run the script
  229. exec(
  230. HESTIA_CMD .
  231. "v-check-access-key " .
  232. quoteshellarg($hst_access_key_id) .
  233. " " .
  234. quoteshellarg($hst_secret_access_key) .
  235. " " .
  236. quoteshellarg($hst_cmd) .
  237. " " .
  238. quoteshellarg($v_real_user_ip) .
  239. " json",
  240. $output,
  241. $return_var,
  242. );
  243. if ($return_var > 0) {
  244. //api_error($return_var, "Key $hst_access_key_id - authentication failed");
  245. api_error($return_var, $output);
  246. }
  247. $key_data = json_decode(implode("", $output), true) ?? [];
  248. unset($output, $return_var);
  249. $key_user = $key_data["USER"];
  250. $user_arg_position =
  251. isset($key_data["USER_ARG_POSITION"]) && is_numeric($key_data["USER_ARG_POSITION"])
  252. ? $key_data["USER_ARG_POSITION"]
  253. : -1;
  254. # Check if API access is enabled for nonadmin users
  255. if ($key_user != "admin" && $api_status < 2) {
  256. api_error(E_DISABLED, "API has been disabled");
  257. }
  258. // Checks if the value entered in the "user" argument matches the user of the key
  259. if (
  260. $key_user != "admin" &&
  261. $user_arg_position > 0 &&
  262. $hst_cmd_args["arg{$user_arg_position}"] != $key_user
  263. ) {
  264. api_error(
  265. E_FORBIDDEN,
  266. "Key $hst_access_key_id - the \"user\" argument doesn\'t match the key\'s user",
  267. );
  268. }
  269. // Prepare command
  270. $cmdquery = HESTIA_CMD . escapeshellcmd($hst_cmd);
  271. // Prepare arguments
  272. foreach ($hst_cmd_args as $cmd_arg) {
  273. $cmdquery .= " " . quoteshellarg($cmd_arg);
  274. }
  275. # v-make-temp files is manodory other wise some functions will break
  276. if ($hst_cmd == "v-make-tmp-file") {
  277. $fp = fopen("/tmp/" . basename($hst_cmd_args["arg2"]), "w");
  278. fwrite($fp, $hst_cmd_args["arg1"] . "\n");
  279. fclose($fp);
  280. $cmd_exit_code = 0;
  281. } else {
  282. // Run cmd query
  283. exec($cmdquery, $output, $cmd_exit_code);
  284. $cmd_output = trim(implode("\n", $output));
  285. unset($output);
  286. }
  287. header("Hestia-Exit-Code: $cmd_exit_code");
  288. if ($hst_return == "code") {
  289. echo $cmd_exit_code;
  290. } else {
  291. if ($cmd_exit_code > 0) {
  292. http_response_code(exit_code_to_http_code($cmd_exit_code));
  293. } else {
  294. http_response_code(!empty($cmd_output) ? 200 : 204);
  295. if (!empty($cmd_output) && json_decode($cmd_output, true)) {
  296. header("Content-Type: application/json; charset=utf-8");
  297. }
  298. }
  299. echo $cmd_output;
  300. }
  301. exit();
  302. }
  303. // Get request data
  304. if (isset($_POST["access_key"]) || isset($_POST["user"]) || isset($_POST["hash"])) {
  305. $request_data = $_POST;
  306. } elseif (($json_data = json_decode(file_get_contents("php://input"), true)) != null) {
  307. $request_data = $json_data;
  308. } else {
  309. api_error(
  310. 405,
  311. "Error: data received is null or invalid, check https://docs.hestiacp.com/admin_docs/api.html",
  312. );
  313. }
  314. // Try to get access key in the hash
  315. if (
  316. !isset($request_data["access_key"]) &&
  317. isset($request_data["hash"]) &&
  318. substr_count($request_data["hash"], ":") == 1
  319. ) {
  320. $hash_parts = explode(":", $request_data["hash"]);
  321. if (strlen($hash_parts[0]) == 20 && strlen($hash_parts[1]) == 40) {
  322. $request_data["access_key"] = $hash_parts[0];
  323. $request_data["secret_key"] = $hash_parts[1];
  324. unset($request_data["hash"]);
  325. }
  326. }
  327. // Check data format
  328. if (isset($request_data["access_key"]) && isset($request_data["secret_key"])) {
  329. api_connection($request_data);
  330. } elseif (isset($request_data["user"]) || isset($request_data["hash"])) {
  331. api_legacy($request_data);
  332. } else {
  333. api_error(
  334. 405,
  335. "Error: data received is null or invalid, check https://docs.hestiacp.com/admin_docs/api.html",
  336. );
  337. }