index.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. error_reporting(E_ALL);
  3. $TAB = 'USER';
  4. // Main include
  5. include($_SERVER['DOCUMENT_ROOT']."/inc/main.php");
  6. //check for valid format ssh key. Doesn't check it is working!
  7. //https://gist.github.com/jupeter/3248095
  8. function validateKey($value)
  9. {
  10. $key_parts = explode(' ', $value, 3);
  11. if (count($key_parts) < 2) {
  12. return false;
  13. }
  14. if (count($key_parts) > 3) {
  15. return false;
  16. }
  17. $algorithm = $key_parts[0];
  18. $key = $key_parts[1];
  19. if (!in_array($algorithm, array('ssh-rsa', 'ssh-dss'))) {
  20. return false;
  21. }
  22. $key_base64_decoded = base64_decode($key, true);
  23. if ($key_base64_decoded == FALSE) {
  24. return false;
  25. }
  26. $check = base64_decode(substr($key,0,16));
  27. $check = preg_replace("/[^\w\-]/","", $check);
  28. if((string) $check !== (string) $algorithm) {
  29. return false;
  30. }
  31. return true;
  32. }
  33. // Check POST request
  34. if (!empty($_POST['ok'])) {
  35. // Check token
  36. if ((!isset($_POST['token'])) || ($_SESSION['token'] != $_POST['token'])) {
  37. header('location: /login/');
  38. exit();
  39. }
  40. if (empty($_POST['v_key'])){
  41. $_SESSION['error_msg'] = __('Field SSH_KEY can not be blank.');
  42. }
  43. if(!$_SESSION['error_msg']){
  44. switch ($_POST['v_key']){
  45. default:
  46. //key if key already exisits
  47. exec (HESTIA_CMD . "v-list-user-ssh-key ".$user." json", $output, $return_var);
  48. $data = json_decode(implode('', $output), true);
  49. $keylist = array();
  50. foreach($data as $key => $value){
  51. $idlist[] = trim($data[$key]['ID']);
  52. $keylist[] = trim($data[$key]['KEY']);
  53. }
  54. if(!validateKey($_POST['v_key'])){
  55. $_SESSION['error_msg'] = __('SSH KEY is invalid');
  56. break;
  57. }
  58. $v_key_parts = explode(' ',$_POST['v_key']);
  59. $key_id = trim($v_key_parts[2]);
  60. if($v_key_parts[2] == ''){
  61. $_SESSION['error_msg'] = __('SSH KEY is invalid');
  62. break;
  63. }
  64. //for deleting / revoking key the last part user@domain is used therefore needs to be unique
  65. //maybe consider adding random generated message or even an human read able string set by user?
  66. if(in_array($v_key_parts[2], $idlist)){
  67. $_SESSION['error_msg'] = __('SSH KEY already exists');
  68. break;
  69. }
  70. if(in_array($v_key_parts[1], $keylist)){
  71. $_SESSION['error_msg'] = __('SSH KEY already exists');
  72. break;
  73. }
  74. $v_key = escapeshellarg(trim($_POST['v_key']));
  75. }
  76. }
  77. if (empty($_SESSION['error_msg'])) {
  78. exec (HESTIA_CMD."v-add-user-ssh-key ".$user." ".$v_key, $output, $return_var);
  79. check_return_code($return_var,$output);
  80. }
  81. unset($output);
  82. // Flush field values on success
  83. if (empty($_SESSION['error_msg'])) {
  84. $_SESSION['ok_msg'] = __('SSH KEY created');
  85. }
  86. }
  87. render_page($user, $TAB, 'add_key');
  88. // Flush session messages
  89. unset($_SESSION['error_msg']);
  90. unset($_SESSION['ok_msg']);