You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

krb_authentication.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Kerberos Authentication
  4. *
  5. * Make use of an existing Kerberos authentication and perform login
  6. * with the existing user credentials
  7. *
  8. * For other configuration options, see config.inc.php.dist!
  9. *
  10. * @license GNU GPLv3+
  11. * @author Jeroen van Meeuwen
  12. */
  13. class krb_authentication extends rcube_plugin
  14. {
  15. private $redirect_query;
  16. /**
  17. * Plugin initialization
  18. */
  19. function init()
  20. {
  21. $this->add_hook('startup', array($this, 'startup'));
  22. $this->add_hook('authenticate', array($this, 'authenticate'));
  23. $this->add_hook('login_after', array($this, 'login'));
  24. $this->add_hook('storage_connect', array($this, 'storage_connect'));
  25. }
  26. /**
  27. * Startup hook handler
  28. */
  29. function startup($args)
  30. {
  31. if (!empty($_SERVER['REMOTE_USER']) && !empty($_SERVER['KRB5CCNAME'])) {
  32. // handle login action
  33. if (empty($_SESSION['user_id'])) {
  34. $args['action'] = 'login';
  35. $this->redirect_query = $_SERVER['QUERY_STRING'];
  36. }
  37. else {
  38. $_SESSION['password'] = null;
  39. }
  40. }
  41. return $args;
  42. }
  43. /**
  44. * Authenticate hook handler
  45. */
  46. function authenticate($args)
  47. {
  48. if (!empty($_SERVER['REMOTE_USER']) && !empty($_SERVER['KRB5CCNAME'])) {
  49. // Load plugin's config file
  50. $this->load_config();
  51. $rcmail = rcmail::get_instance();
  52. $host = $rcmail->config->get('krb_authentication_host');
  53. if (is_string($host) && trim($host) !== '' && empty($args['host'])) {
  54. $args['host'] = rcube_utils::idn_to_ascii(rcube_utils::parse_host($host));
  55. }
  56. if (!empty($_SERVER['REMOTE_USER'])) {
  57. $args['user'] = $_SERVER['REMOTE_USER'];
  58. $args['pass'] = null;
  59. }
  60. $args['cookiecheck'] = false;
  61. $args['valid'] = true;
  62. }
  63. return $args;
  64. }
  65. /**
  66. * Storage_connect hook handler
  67. */
  68. function storage_connect($args)
  69. {
  70. if (!empty($_SERVER['REMOTE_USER']) && !empty($_SERVER['KRB5CCNAME'])) {
  71. // Load plugin's config file
  72. $this->load_config();
  73. $rcmail = rcmail::get_instance();
  74. $context = $rcmail->config->get('krb_authentication_context');
  75. $args['gssapi_context'] = $context ?: 'imap/kolab.example.org@EXAMPLE.ORG';
  76. $args['gssapi_cn'] = $_SERVER['KRB5CCNAME'];
  77. $args['auth_type'] = 'GSSAPI';
  78. }
  79. return $args;
  80. }
  81. /**
  82. * login_after hook handler
  83. */
  84. function login($args)
  85. {
  86. // Redirect to the previous QUERY_STRING
  87. if ($this->redirect_query) {
  88. header('Location: ./?' . $this->redirect_query);
  89. exit;
  90. }
  91. return $args;
  92. }
  93. }