index.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. // Define the command to use
  31. if (isset($_POST['cmd']))
  32. {
  33. $cmd = escapeshellarg($_POST['cmd']);
  34. } else
  35. {
  36. // If there's no command, just exit.
  37. echo 'No command specified.';
  38. exit;
  39. }
  40. // Prepare for iteration
  41. $args = [];
  42. $i = 0;
  43. // Loop through args until there isn't another.
  44. while (true)
  45. {
  46. $i++;
  47. if (!empty($_POST['arg' . $i]))
  48. {
  49. $args[] = escapeshellarg($_POST['arg' . $i]);
  50. continue;
  51. }
  52. break;
  53. }
  54. // Build query
  55. $cmdquery = VESTA_CMD . $cmd . " " . implode(" ", $args);
  56. // Check command
  57. if ($cmd == "'v-make-tmp-file'") {
  58. // Used in DNS Cluster
  59. $fp = fopen($_POST['arg2'], 'w');
  60. fwrite($fp, $_POST['arg1']."\n");
  61. fclose($fp);
  62. $return_var = 0;
  63. } else {
  64. // Run normal cmd query
  65. exec ($cmdquery, $output, $return_var);
  66. }
  67. if ((!empty($_POST['returncode'])) && ($_POST['returncode'] == 'yes')) {
  68. echo $return_var;
  69. } else {
  70. if (($return_var == 0) && (empty($output))) {
  71. echo "OK";
  72. } else {
  73. echo implode("\n",$output)."\n";
  74. }
  75. }
  76. }