index.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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, $hst_return, 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. if ($hst_return == "code") {
  38. echo $exit_code;
  39. } else {
  40. echo !preg_match("/^Error:/", $message) ? "Error: $message" : $message;
  41. }
  42. exit();
  43. }
  44. /**
  45. * Legacy connection format using hash or user and password.
  46. *
  47. * @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, arg10?: string, arg11?: string, arg12?: string, arg13?: string, returncode?: string} $request_data
  48. * @return void
  49. * @return void
  50. */
  51. function api_legacy(array $request_data) {
  52. $hst_return = ($request_data["returncode"] ?? "no") === "yes" ? "code" : "data";
  53. exec(HESTIA_CMD . "v-list-sys-config json", $output, $return_var);
  54. $settings = json_decode(implode("", $output), true);
  55. unset($output);
  56. if ($settings["config"]["API"] != "yes") {
  57. echo "Error: API has been disabled";
  58. api_error(E_DISABLED, "Error: API Disabled", $hst_return);
  59. }
  60. if ($settings["config"]["API_ALLOWED_IP"] != "allow-all") {
  61. $ip_list = explode(",", $settings["config"]["API_ALLOWED_IP"]);
  62. $ip_list[] = "";
  63. if (!in_array(get_real_user_ip(), $ip_list)) {
  64. api_error(E_FORBIDDEN, "Error: IP is not allowed to connect with API", $hst_return);
  65. }
  66. }
  67. //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
  68. // Authentication
  69. if (empty($request_data["hash"])) {
  70. if ($request_data["user"] != "admin") {
  71. api_error(E_FORBIDDEN, "Error: authentication failed", $hst_return);
  72. }
  73. $password = $request_data["password"];
  74. if (!isset($password)) {
  75. api_error(E_PASSWORD, "Error: authentication failed", $hst_return);
  76. }
  77. $v_ip = quoteshellarg(get_real_user_ip());
  78. unset($output);
  79. exec(HESTIA_CMD . "v-get-user-salt admin " . $v_ip . " json", $output, $return_var);
  80. $pam = json_decode(implode("", $output), true);
  81. $salt = $pam["admin"]["SALT"];
  82. $method = $pam["admin"]["METHOD"];
  83. if ($method == "md5") {
  84. $hash = crypt($password, '$1$' . $salt . '$');
  85. }
  86. if ($method == "sha-512") {
  87. $hash = crypt($password, '$6$rounds=5000$' . $salt . '$');
  88. $hash = str_replace('$rounds=5000', "", $hash);
  89. }
  90. if ($method == "yescrypt") {
  91. $fp = tmpfile();
  92. $v_password = stream_get_meta_data($fp)["uri"];
  93. fwrite($fp, $password . "\n");
  94. unset($output);
  95. exec(
  96. HESTIA_CMD .
  97. 'v-check-user-password "admin" ' .
  98. quoteshellarg($v_password) .
  99. " " .
  100. $v_ip .
  101. " yes",
  102. $output,
  103. $return_var,
  104. );
  105. $hash = $output[0];
  106. fclose($fp);
  107. unset($output, $fp, $v_password);
  108. }
  109. if ($method == "des") {
  110. $hash = crypt($password, $salt);
  111. }
  112. // Send hash via tmp file
  113. $v_hash = exec("mktemp -p /tmp");
  114. $fp = fopen($v_hash, "w");
  115. fwrite($fp, $hash . "\n");
  116. fclose($fp);
  117. // Check user hash
  118. exec(HESTIA_CMD . "v-check-user-hash admin " . $v_hash . " " . $v_ip, $output, $return_var);
  119. unset($output);
  120. // Remove tmp file
  121. unlink($v_hash);
  122. // Check API answer
  123. if ($return_var > 0) {
  124. api_error(E_PASSWORD, "Error: authentication failed", $hst_return);
  125. }
  126. } else {
  127. $key = "/usr/local/hestia/data/keys/" . basename($request_data["hash"]);
  128. $v_ip = quoteshellarg(get_real_user_ip());
  129. exec(
  130. HESTIA_CMD . "v-check-api-key " . quoteshellarg($key) . " " . $v_ip,
  131. $output,
  132. $return_var,
  133. );
  134. unset($output);
  135. // Check API answer
  136. if ($return_var > 0) {
  137. api_error(E_PASSWORD, "Error: authentication failed", $hst_return);
  138. }
  139. }
  140. $hst_cmd = trim($request_data["cmd"] ?? "");
  141. $hst_cmd_args = [];
  142. for ($i = 1; $i <= 13; $i++) {
  143. if (isset($request_data["arg{$i}"])) {
  144. $hst_cmd_args["arg{$i}"] = trim($request_data["arg{$i}"]);
  145. }
  146. }
  147. if (empty($hst_cmd)) {
  148. api_error(E_INVALID, "Command not provided", $hst_return);
  149. } elseif (!preg_match('/^[a-zA-Z0-9_-]+$/', $hst_cmd)) {
  150. api_error(E_INVALID, "$hst_cmd command invalid", $hst_return);
  151. }
  152. // Check command
  153. if ($hst_cmd == "v-make-tmp-file") {
  154. // Used in DNS Cluster
  155. $fp = fopen("/tmp/" . basename(escapeshellcmd($hst_cmd_args["arg2"])), "w");
  156. fwrite($fp, $hst_cmd_args["arg1"] . "\n");
  157. fclose($fp);
  158. $return_var = 0;
  159. } else {
  160. // Prepare command
  161. $cmdquery = HESTIA_CMD . escapeshellcmd($hst_cmd);
  162. // Prepare arguments
  163. foreach ($hst_cmd_args as $cmd_arg) {
  164. $cmdquery .= " " . quoteshellarg($cmd_arg);
  165. }
  166. // Run cmd query
  167. exec($cmdquery, $output, $cmd_exit_code);
  168. }
  169. if (!empty($hst_return) && $hst_return == "code") {
  170. echo $cmd_exit_code;
  171. } else {
  172. if ($return_var == 0 && empty($output)) {
  173. echo "OK";
  174. } else {
  175. echo implode("\n", $output) . "\n";
  176. }
  177. }
  178. exit();
  179. }
  180. /**
  181. * Connection using access key.
  182. *
  183. * @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, arg10?: string, arg11?: string, arg12?: string, arg13?: string, returncode?: string} $request_data
  184. * @return void
  185. */
  186. function api_connection(array $request_data) {
  187. $hst_return = ($request_data["returncode"] ?? "no") === "yes" ? "code" : "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", $hst_return);
  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", $hst_return);
  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_cmd = trim($request_data["cmd"] ?? "");
  213. $hst_cmd_args = [];
  214. for ($i = 1; $i <= 13; $i++) {
  215. if (isset($request_data["arg{$i}"])) {
  216. $hst_cmd_args["arg{$i}"] = trim($request_data["arg{$i}"]);
  217. }
  218. }
  219. if (empty($hst_cmd)) {
  220. api_error(E_INVALID, "Command not provided", $hst_return);
  221. } elseif (!preg_match('/^[a-zA-Z0-9_-]+$/', $hst_cmd)) {
  222. api_error(E_INVALID, "$hst_cmd command invalid", $hst_return);
  223. }
  224. if (empty($hst_access_key_id) || empty($hst_secret_access_key)) {
  225. api_error(E_PASSWORD, "Authentication failed", $hst_return);
  226. }
  227. // Authenticates the key and checks permission to run the script
  228. exec(
  229. HESTIA_CMD .
  230. "v-check-access-key " .
  231. quoteshellarg($hst_access_key_id) .
  232. " " .
  233. quoteshellarg($hst_secret_access_key) .
  234. " " .
  235. quoteshellarg($hst_cmd) .
  236. " " .
  237. quoteshellarg($v_real_user_ip) .
  238. " json",
  239. $output,
  240. $return_var,
  241. );
  242. if ($return_var > 0) {
  243. //api_error($return_var, "Key $hst_access_key_id - authentication failed", $hst_return);
  244. api_error($return_var, $output, $hst_return);
  245. }
  246. $key_data = json_decode(implode("", $output), true) ?? [];
  247. unset($output, $return_var);
  248. $key_user = $key_data["USER"];
  249. $user_arg_position =
  250. isset($key_data["USER_ARG_POSITION"]) && is_numeric($key_data["USER_ARG_POSITION"])
  251. ? $key_data["USER_ARG_POSITION"]
  252. : -1;
  253. # Check if API access is enabled for nonadmin users
  254. if ($key_user != "admin" && $api_status < 2) {
  255. api_error(E_API_DISABLED, "API has been disabled", $hst_return);
  256. }
  257. // Checks if the value entered in the "user" argument matches the user of the key
  258. if (
  259. $key_user != "admin" &&
  260. $user_arg_position > 0 &&
  261. $hst_cmd_args["arg{$user_arg_position}"] != $key_user
  262. ) {
  263. api_error(
  264. E_FORBIDDEN,
  265. "Key $hst_access_key_id - the \"user\" argument doesn\'t match the key\'s user",
  266. $hst_return,
  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://hestiacp.com/docs/server-administration/rest-api.html",
  312. "",
  313. );
  314. }
  315. // Try to get access key in the hash
  316. if (
  317. !isset($request_data["access_key"]) &&
  318. isset($request_data["hash"]) &&
  319. substr_count($request_data["hash"], ":") == 1
  320. ) {
  321. $hash_parts = explode(":", $request_data["hash"]);
  322. if (strlen($hash_parts[0]) == 20 && strlen($hash_parts[1]) == 40) {
  323. $request_data["access_key"] = $hash_parts[0];
  324. $request_data["secret_key"] = $hash_parts[1];
  325. unset($request_data["hash"]);
  326. }
  327. }
  328. // Check data format
  329. if (isset($request_data["access_key"]) && isset($request_data["secret_key"])) {
  330. api_connection($request_data);
  331. } elseif (isset($request_data["user"]) || isset($request_data["hash"])) {
  332. api_legacy($request_data);
  333. } else {
  334. api_error(
  335. 405,
  336. "Error: data received is null or invalid, check https://hestiacp.com/docs/server-administration/rest-api.html",
  337. "",
  338. );
  339. }