index.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 = $_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 = $_SERVER['REMOTE_ADDR'];
  18. $auth_code = v_exec('v-check-user-password', [$v_user, $v_password, $v_ip_addr], false);
  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 arguments
  31. $args = [];
  32. if (isset($_POST['cmd'])) $cmd = $_POST['cmd'];
  33. if (isset($_POST['arg1'])) $args[] = $_POST['arg1'];
  34. if (isset($_POST['arg2'])) $args[] = $_POST['arg2'];
  35. if (isset($_POST['arg3'])) $args[] = $_POST['arg3'];
  36. if (isset($_POST['arg4'])) $args[] = $_POST['arg4'];
  37. if (isset($_POST['arg5'])) $args[] = $_POST['arg5'];
  38. if (isset($_POST['arg6'])) $args[] = $_POST['arg6'];
  39. if (isset($_POST['arg7'])) $args[] = $_POST['arg7'];
  40. if (isset($_POST['arg8'])) $args[] = $_POST['arg8'];
  41. if (isset($_POST['arg9'])) $args[] = $_POST['arg9'];
  42. // Check command
  43. if ($cmd == "'v-make-tmp-file'") {
  44. // Used in DNS Cluster
  45. $fp = fopen($_POST['arg2'], 'w');
  46. fwrite($fp, $_POST['arg1']."\n");
  47. fclose($fp);
  48. $return_var = 0;
  49. } else {
  50. // Run normal cmd query
  51. $return_var = v_exec($cmd, $args, false, $output);
  52. }
  53. if ((!empty($_POST['returncode'])) && ($_POST['returncode'] == 'yes')) {
  54. echo $return_var;
  55. } else {
  56. if (($return_var == 0) && (empty($output))) {
  57. echo "OK";
  58. } else {
  59. echo $output . "\n";
  60. }
  61. }
  62. }