init.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. class Auth_Proxy extends Plugin implements IAuthModule {
  3. private $host;
  4. /* @var Auth_Base $base */
  5. private $base;
  6. function about() {
  7. return array(1.0,
  8. "Trust proxy X-Forwarded-User. May be dangerous, see doc",
  9. "boyska",
  10. true);
  11. }
  12. /* @var PluginHost $host */
  13. function init($host ) {
  14. $this->host = $host;
  15. $this->base = new Auth_Base();
  16. $host->add_hook($host::HOOK_AUTH_USER, $this);
  17. }
  18. /*
  19. * is_whitelisted check if an IP is whitelisted by defined values in config.php
  20. * it will check by-IP and by-NAME
  21. * currently, only exact IP is supported (no cidr, no wildcard); this is a TODO
  22. * check by
  23. */
  24. private function is_whitelisted($client_ip) {
  25. if(!defined('AUTHPROXY_WHITELIST_IP') && !defined('AUTHPROXY_WHITELIST_NAME')) {
  26. // TODO: send a warning: this is a misconfiguration!
  27. return false;
  28. }
  29. if(defined('AUTHPROXY_WHITELIST_IP')) {
  30. $whitelist = explode(' ', AUTHPROXY_WHITELIST_IP);
  31. foreach($whitelist as $w_ip) {
  32. if($client_ip === $w_ip) {
  33. return true;
  34. }
  35. }
  36. }
  37. if(defined('AUTHPROXY_WHITELIST_NAME')) {
  38. $whitelist = explode(' ', AUTHPROXY_WHITELIST_NAME);
  39. foreach($whitelist as $w_name) {
  40. foreach(gethostbynamel($w_name) as $w_ip) {
  41. if($client_ip === $w_ip) {
  42. return true;
  43. }
  44. }
  45. }
  46. }
  47. return false;
  48. }
  49. /**
  50. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  51. */
  52. function authenticate($login, $password) {
  53. $client_ip = $_SERVER['REMOTE_ADDR'];
  54. if($this->is_whitelisted($client_ip) === false) {
  55. return false;
  56. }
  57. if(!array_key_exists("HTTP_X_FORWARDED_USER", $_SERVER)) {
  58. return false;
  59. }
  60. $try_login = $_SERVER["HTTP_X_FORWARDED_USER"];
  61. if ($try_login) {
  62. $user_id = $this->base->auto_create_user($try_login, $password);
  63. if ($user_id) {
  64. $_SESSION["fake_login"] = $try_login;
  65. $_SESSION["fake_password"] = "******";
  66. $_SESSION["hide_hello"] = true;
  67. $_SESSION["hide_logout"] = true;
  68. return $user_id;
  69. }
  70. }
  71. return false;
  72. }
  73. function api_version() {
  74. return 2;
  75. }
  76. }