index.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. define('VESTA_CMD', '/usr/bin/sudo /usr/local/vesta/bin/');
  3. if (isset($_POST['user']) || isset($_POST['hash'])) {
  4. // Authentication
  5. $auth_code = 1;
  6. if (empty($_POST['hash'])) {
  7. // Check user permission to use API
  8. if ($_POST['user'] != 'admin') {
  9. echo 'Error: only admin is allowed to use API';
  10. exit;
  11. }
  12. $v_user = escapeshellarg($_POST['user']);
  13. $v_password = tempnam("/tmp","vst");
  14. $fp = fopen($v_password, "w");
  15. fwrite($fp, $_POST['password']."\n");
  16. fclose($fp);
  17. $v_ip_addr = escapeshellarg($_SERVER["REMOTE_ADDR"]);
  18. exec(VESTA_CMD ."v-check-user-password ".$v_user." ".$v_password." '".$v_ip_addr."'", $output, $auth_code);
  19. unlink($v_password);
  20. } else {
  21. $key = '/usr/local/vesta/data/keys/' . basename($_POST['hash']);
  22. if (file_exists($key) && is_file($key)) {
  23. $auth_code = '0';
  24. }
  25. }
  26. if ($auth_code != 0 ) {
  27. echo 'Error: authentication failed';
  28. exit;
  29. }
  30. // Prepare for iteration
  31. $args = [];
  32. $i = 0;
  33. // Loop through args until there isn't another.
  34. while (true)
  35. {
  36. $i++;
  37. if (!empty($_POST['arg' . $i]))
  38. {
  39. $args[] = $_POST['arg' . $i];
  40. continue;
  41. }
  42. break;
  43. }
  44. // Build query
  45. $cmdquery = VESTA_CMD . $cmd . " " . implode(" ", $args);
  46. // Check command
  47. if ($cmd == "'v-make-tmp-file'") {
  48. // Used in DNS Cluster
  49. $fp = fopen($_POST['arg2'], 'w');
  50. fwrite($fp, $_POST['arg1']."\n");
  51. fclose($fp);
  52. $return_var = 0;
  53. } else {
  54. // Run normal cmd query
  55. exec ($cmdquery, $output, $return_var);
  56. }
  57. if ((!empty($_POST['returncode'])) && ($_POST['returncode'] == 'yes')) {
  58. echo $return_var;
  59. } else {
  60. if (($return_var == 0) && (empty($output))) {
  61. echo "OK";
  62. } else {
  63. echo implode("\n",$output)."\n";
  64. }
  65. }
  66. }