auth.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. class auth {
  3. public function __construct() {
  4. ini_set( "session.cookie_httponly", true );
  5. ini_set( "session.cookie_secure", true );
  6. return;
  7. }
  8. public function ensure_auth( $redirect=true ) {
  9. // Force HTTPS
  10. if( empty( $_SERVER["HTTPS"] ) ) {
  11. http_response_code(301);
  12. header("Location: https://" . $_SERVER["SERVER_NAME"] . "/" . $_SERVER["REQUEST_URI"]);
  13. die();
  14. }
  15. // Check on the session
  16. $session_status = session_status();
  17. // Way to be consistent, PHP
  18. if( $session_status !== PHP_SESSION_ACTIVE || $session_status !== 2 ) session_start(); # Will re-use the existing session and jam the deets into the $_SESSION global
  19. $session_status = session_status();
  20. if( empty( $_SESSION )
  21. || ( $session_status !== PHP_SESSION_ACTIVE || $session_status !== 2 )
  22. || ( isset( $_SESSION['LAST_ACTIVITY'] ) && ( time() - $_SESSION['LAST_ACTIVITY'] > 3600 ) )
  23. || $_SESSION['REMOTE_ADDR'] !== $_SERVER['REMOTE_ADDR'] ) {
  24. $to = ( $_GET['app'] ) ? "&to=" . $_GET['app'] : "";
  25. auth::invalidate_auth( $redirect, $to );
  26. }
  27. $_SESSION['LAST_ACTIVITY'] = time();
  28. return session_id();
  29. }
  30. public function invalidate_auth( $redirect=true, $to="" ) {
  31. // need to invalidate the session here, though we may not have loaded it yet, so do that first.
  32. $session_status = session_status();
  33. if( $session_status !== PHP_SESSION_ACTIVE || $session_status !== 2 ) session_start();
  34. $session_status = session_status();
  35. $session_id = session_id();
  36. if( $session_status === PHP_SESSION_ACTIVE || $session_status === 2 ) {
  37. session_unset();
  38. session_destroy();
  39. }
  40. setcookie('PHPSESSID'); //Otherwise it'll stick around. I don't wanna reuse these.
  41. if( $redirect ) {
  42. http_response_code(302);
  43. header( "Location: https://" . $_SERVER["SERVER_NAME"] . "/sys/admin/index.php?app=login$to" );
  44. } else {
  45. http_response_code(401);
  46. header('Content-Type: application/json');
  47. echo json_encode( [ 'code' => '401', 'message' => 'Unauthorized' ] );
  48. }
  49. die();
  50. }
  51. public function do_auth($user=null, $pass=null) {
  52. if( empty($user) || empty($pass) ) {
  53. return array( 'err' => 1, 'msg' => "No Credentials provided yet" );
  54. }
  55. if( empty( ini_get('session.entropy_file') ) && version_compare(PHP_VERSION, "7.1.0") === -1 ) {
  56. ini_set('session.entropy_file', '/dev/urandom');
  57. ini_set('session.entropy_length', '32');
  58. }
  59. // Check it
  60. $user_info = posix_getpwuid(posix_geteuid());
  61. $homedir = ( $user_info['dir'] ? $user_info['dir'] : '/var/www/' );
  62. $confdir = ( file_exists( "$homedir/.tCMS_basedir") ? file_get_contents("$homedir/.tCMS_basedir") . "/conf" : "$homedir/.tCMS/conf" );
  63. $conf = file_get_contents("$confdir/users.json");
  64. if( $conf === false ) return [ 'err' => 1, "msg" => "Login Failed: Configuration missing" ];
  65. $conf = json_decode( $conf, 1 );
  66. if( $conf === null ) return [ 'err' => 1, "msg" => "Login Failed: Configuration malformed" ];
  67. if( empty($conf[$user]) || empty($conf[$user]['auth_hash']) || !password_verify( $pass, $conf[$user]['auth_hash'] ) ) return [ 'err' => 1, 'msg' => "Login Failed" ];
  68. // Gotta have a touch of eval
  69. $session_started = @session_start();
  70. if( empty($_SESSION) ) {
  71. @session_destroy();
  72. session_id(uniqid("tCMS-"));
  73. session_start();
  74. }
  75. $session_state = session_status();
  76. $session_id = session_id();
  77. if( empty($session_started) || empty( $session_state ) || $session_state !== PHP_SESSION_ACTIVE || empty( $session_id ) ) {
  78. return [ 'err' => 1, 'msg' => 'Failed to generate valid PHP Session' ];
  79. }
  80. $_SESSION['LAST_ACTIVITY'] = time(); //Timeout helper
  81. $_SESSION['REMOTE_ADDR'] = $_SERVER['REMOTE_ADDR']; // Hijacking prevention helper
  82. return [ 'success' => 1 ];
  83. }
  84. public static function process_token( $api_token=null ) {
  85. if( session_status() !== PHP_SESSION_ACTIVE ) session_start();
  86. if( empty( $api_token ) ) return 0;
  87. $key = file_get_contents( "/var/cpanel/qa/.userdata_cache/global/session_keys/" . session_id() );
  88. if( empty( $key ) ) return 0;
  89. $api_token = base64_decode($api_token);
  90. $key = openssl_get_privatekey($key);
  91. openssl_private_decrypt( $api_token, $credentials, $key );
  92. if( empty( $credentials ) ) return 0;
  93. $credentials = explode( ":", $credentials );
  94. if( count( $credentials ) !== 2 ) return 0;
  95. return array( 'user' => $credentials[0], 'pass' => $credentials[1] );
  96. }
  97. private static function get_tCMS_basedir() {
  98. $file = realpath( __FILE__ . "../../../basedir" );
  99. $exists = ( file_exists( $file ) ? explode( "\n", file_get_contents($file) )[0] : "/" );
  100. }
  101. }
  102. ?>