handler = $handler; $_PAM_HANDLERS[$policy][$handler]->importance = strtolower($importance); return true; } return false; } /** * Unregisters a PAM handler. * * @param string $handler The PAM handler function name * @param string $policy - the policy type, default is "user" */ function unregister_pam_handler($handler, $policy = "user") { global $_PAM_HANDLERS; unset($_PAM_HANDLERS[$policy][$handler]); } /** * Attempt to authenticate. * This function will process all registered PAM handlers or stop when the first * handler fails. A handler fails by either returning false or throwing an * exception. The advantage of throwing an exception is that it returns a message * through the global $_PAM_HANDLERS_MSG which can be used in communication with * a user. The order that handlers are processed is determined by the order that * they were registered. * * If $credentials are provided the PAM handler should authenticate using the * provided credentials, if not then credentials should be prompted for or * otherwise retrieved (eg from the HTTP header or $_SESSION). * * @param mixed $credentials Mixed PAM handler specific credentials (e.g. username, password) * @param string $policy - the policy type, default is "user" * @return bool true if authenticated, false if not. */ function pam_authenticate($credentials = NULL, $policy = "user") { global $_PAM_HANDLERS, $_PAM_HANDLERS_MSG; $_PAM_HANDLERS_MSG = array(); $authenticated = false; foreach ($_PAM_HANDLERS[$policy] as $k => $v) { $handler = $v->handler; $importance = $v->importance; try { // Execute the handler if ($handler($credentials)) { // Explicitly returned true $_PAM_HANDLERS_MSG[$k] = "Authenticated!"; $authenticated = true; } else { $_PAM_HANDLERS_MSG[$k] = "Not Authenticated."; // If this is required then abort. if ($importance == 'required') { return false; } } } catch (Exception $e) { $_PAM_HANDLERS_MSG[$k] = "$e"; // If this is required then abort. if ($importance == 'required') { return false; } } } return $authenticated; }