pam.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * Elgg Simple PAM library
  4. * Contains functions for managing authentication.
  5. * This is not a full implementation of PAM. It supports a single facility
  6. * (authentication) and allows multiple policies (user authentication is the
  7. * default). There are two control flags possible for each module: sufficient
  8. * or required. The entire chain for a policy is processed (or until a
  9. * required module fails). A module fails by returning false or throwing an
  10. * exception. The order that modules are processed is determined by the order
  11. * they are registered. For an example of a PAM, see pam_auth_userpass() in
  12. * sessions.php.
  13. *
  14. * For more information on PAMs see:
  15. * http://www.freebsd.org/doc/en/articles/pam/index.html
  16. *
  17. * @see \ElggPAM
  18. *
  19. * @package Elgg.Core
  20. * @subpackage Authentication.PAM
  21. */
  22. global $_PAM_HANDLERS;
  23. $_PAM_HANDLERS = array();
  24. /**
  25. * Register a PAM handler.
  26. *
  27. * A PAM handler should return true if the authentication attempt passed. For a
  28. * failure, return false or throw an exception. Returning nothing indicates that
  29. * the handler wants to be skipped.
  30. *
  31. * Note, $handler must be string callback (not an array/Closure).
  32. *
  33. * @param string $handler Callable global handler function in the format ()
  34. * pam_handler($credentials = null);
  35. * @param string $importance The importance - "sufficient" (default) or "required"
  36. * @param string $policy The policy type, default is "user"
  37. *
  38. * @return bool
  39. */
  40. function register_pam_handler($handler, $importance = "sufficient", $policy = "user") {
  41. global $_PAM_HANDLERS;
  42. // setup array for this type of pam if not already set
  43. if (!isset($_PAM_HANDLERS[$policy])) {
  44. $_PAM_HANDLERS[$policy] = array();
  45. }
  46. // @todo remove requirement that $handle be a global function
  47. if (is_string($handler) && is_callable($handler, true)) {
  48. $_PAM_HANDLERS[$policy][$handler] = new \stdClass;
  49. $_PAM_HANDLERS[$policy][$handler]->handler = $handler;
  50. $_PAM_HANDLERS[$policy][$handler]->importance = strtolower($importance);
  51. return true;
  52. }
  53. return false;
  54. }
  55. /**
  56. * Unregisters a PAM handler.
  57. *
  58. * @param string $handler The PAM handler function name
  59. * @param string $policy The policy type, default is "user"
  60. *
  61. * @return void
  62. * @since 1.7.0
  63. */
  64. function unregister_pam_handler($handler, $policy = "user") {
  65. global $_PAM_HANDLERS;
  66. unset($_PAM_HANDLERS[$policy][$handler]);
  67. }