cookie options */ function lamDefaultCookieOptions(): array { $secureFlag = false; if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on') { $secureFlag = true; } return [ 'path' => '/', 'domain' => '', 'secure' => $secureFlag, 'httponly' => true, 'samesite' => 'Lax' ]; } /** * Starts a session and checks the environment. * The script is stopped if one of the checks fail (timeout redirection may be overridden). * * @param boolean $redirectToLogin redirect user to login page (default: true) * @param boolean $initSecureData init verification data like session ID and client IP (default: false) * @return boolean true if all ok, false if session expired */ function startSecureSession($redirectToLogin = true, $initSecureData = false) { // start session if (isset($_SESSION)) { unset($_SESSION); } if (isFileBasedSession()) { $sessionDir = __DIR__ . "/../sess"; session_save_path($sessionDir); // enable garbage collection (fix for Debian based systems) if (@ini_get("session.gc_probability") == 0) { @ini_set("session.gc_probability", 1); } } lam_start_session(); // init secure data if needed if ($initSecureData && !isset($_SESSION["sec_session_id"])) { $_SESSION["sec_session_id"] = session_id(); $_SESSION["sec_client_ip"] = $_SERVER['REMOTE_ADDR']; $_SESSION['sec_sessionTime'] = time(); $_SESSION['cfgMain'] = new LAMCfgMain(); } // set error reporting if (empty($_SESSION['cfgMain']) || ($_SESSION['cfgMain']->errorReporting == LAMCfgMain::ERROR_REPORTING_DEFAULT)) { ini_set('error_reporting', (E_ALL & ~E_NOTICE)); } elseif ($_SESSION['cfgMain']->errorReporting == LAMCfgMain::ERROR_REPORTING_ALL) { ini_set('error_reporting', E_ALL); ini_set('display_errors', 'On'); } // check session id if (!isset($_SESSION["sec_session_id"]) || ($_SESSION["sec_session_id"] != session_id())) { // session id is invalid logNewMessage(LOG_WARNING, "Invalid session ID, access denied (" . getClientIPForLogging() . ")"); if ($redirectToLogin) { logoffAndBackToLoginPage(); } else { die(); } } // check if client IP has not changed if (!isset($_SESSION["sec_client_ip"]) || ($_SESSION["sec_client_ip"] != $_SERVER['REMOTE_ADDR'])) { // IP is invalid logNewMessage(LOG_WARNING, "Client IP changed, access denied (" . getClientIPForLogging() . ")"); if ($redirectToLogin) { logoffAndBackToLoginPage(); } else { die(); } } // check if session time has not expired if (($_SESSION['sec_sessionTime'] + (60 * $_SESSION['cfgMain']->sessionTimeout)) > time()) { // ok, update time $_SESSION['sec_sessionTime'] = time(); } elseif ($redirectToLogin) { // session expired, logoff user logoffAndBackToLoginPage(); } else { return false; } setSSLCaCert(); return true; } /** * Returns if the session uses files storage. * * @return bool file based session */ function isFileBasedSession(): bool { return ((session_module_name() !== false) && (strtolower(session_module_name()) === 'files')); } /** * Checks if the client's IP address is on the list of allowed IPs. * The script is stopped if the host is not valid. */ function checkClientIP(): void { $cfg = $_SESSION['cfgMain'] ?? new LAMCfgMain(); $allowedHosts = $cfg->allowedHosts; $url = getCallingURL(); if (($url !== null) && ((str_contains($url, '/selfService/selfService')) || ((str_contains($url, '/misc/ajax.php?')) && str_contains($url, 'selfservice=1')))) { // self-service pages have separate IP list $allowedHosts = $cfg->allowedHostsSelfService; } // skip test if no hosts are defined if (empty($allowedHosts) || empty($_SERVER['REMOTE_ADDR'])) { return; } $allowedHosts = explode(",", $allowedHosts); $grantAccess = false; for ($i = 0; $i < count($allowedHosts); $i++) { $host = $allowedHosts[$i]; $ipRegex = '/^[0-9a-z\\.:\\*]+$/i'; if (!preg_match($ipRegex, $host)) { continue; } $hostRegex = str_replace(".", "\\.", $host); $hostRegex = '/^' . str_replace("*", ".*", $hostRegex) . '$/'; $clientIP = $_SERVER['REMOTE_ADDR']; if (preg_match($hostRegex, $clientIP)) { // client is allowed to access LAM $grantAccess = true; } } // stop script is client may not access LAM if (!$grantAccess) { logNewMessage(LOG_WARNING, "Invalid client IP, access denied (" . getClientIPForLogging() . ")"); die(); } } /** * Logs off the user and displays the login page. * */ function logoffAndBackToLoginPage(): void { // log message if (isset($_SESSION['ldap'])) { $ldapUser = $_SESSION['ldap']->getUserName(); logNewMessage(LOG_WARNING, 'Session of user ' . $ldapUser . ' expired.'); // close LDAP connection @$_SESSION["ldap"]->destroy(); } elseif (isset($_SESSION['selfService_clientDN']) || (str_contains($_SERVER['REQUEST_URI'], '/selfService/'))) { logNewMessage(LOG_WARNING, 'Self-service session of DN ' . lamDecrypt($_SESSION['selfService_clientDN'], 'SelfService') . ' expired.'); } // delete key and iv in cookie $cookieOptions = lamDefaultCookieOptions(); $cookieOptions['expires'] = 0; setcookie("Key", "", $cookieOptions); setcookie("IV", "", $cookieOptions); // link back to login page $paths = ['./', '../', '../../', '../../../', '../../../../']; $page = 'login.php'; $pageSuffix = '?expired=yes'; if (isset($_SESSION['selfService_clientDN']) || (str_contains($_SERVER['REQUEST_URI'], '/selfService/'))) { $scope = $_GET['scope']; $name = $_GET['name']; if (!preg_match('/^[0-9a-zA-Z _-]+$/', $scope) || !preg_match('/^[0-9a-zA-Z _-]+$/', $name)) { logNewMessage(LOG_ERR, 'GET parameters invalid: ' . $name . ' ' . $scope); die(); } $page = 'selfServiceLogin.php'; $pageSuffix = '?expired=yes&scope=' . $scope . '&name=' . $name; } elseif (isset($_SESSION['whitePages_clientDN']) || (str_contains($_SERVER['REQUEST_URI'], '/whitePages/'))) { $name = $_GET['name']; if (!preg_match('/^[0-9a-zA-Z _-]+$/', $name)) { logNewMessage(LOG_ERR, 'GET parameters invalid: ' . $name); die(); } $page = 'whitePagesLogin.php'; $pageSuffix = '?expired=yes&name=' . $name; } for ($i = 0; $i < count($paths); $i++) { if (file_exists($paths[$i] . $page)) { $page = $paths[$i] . $page; break; } } $page .= $pageSuffix; echo $_SESSION['header']; echo "
\n"; echo "" . _("Your session expired, click here to go back to the login page.") . "\n"; echo "
\n"; echo "\n"; echo "