| 1 |
<?php |
| 2 |
/* |
| 3 |
Example login page for mod_auth_pubtkt |
| 4 |
(https://neon1.net/mod_auth_pubtkt) |
| 5 |
|
| 6 |
written by Manuel Kasper <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
require_once("pubtkt.inc"); |
| 10 |
|
| 11 |
/* Set the parameters relevant to your domain below. |
| 12 |
WARNING: do not use the example keys provided with the distribution |
| 13 |
in production - otherwise, anyone could fake your tickets! Generate |
| 14 |
your own key! |
| 15 |
*/ |
| 16 |
$domain = ".example.com"; |
| 17 |
$secure_cookie = false; /* set to true if all your web servers use HTTPS */ |
| 18 |
|
| 19 |
$logfile = "private/login.log"; |
| 20 |
$privkeyfile = "private/tkt_privkey_dsa.pem"; |
| 21 |
$pubkeyfile = "private/tkt_pubkey_dsa.pem"; |
| 22 |
$keytype = "DSA"; |
| 23 |
$digest = "default"; |
| 24 |
$localuserdb = "private/users.txt"; |
| 25 |
$default_timeout = 86400; |
| 26 |
$default_graceperiod = 3600; |
| 27 |
|
| 28 |
/* authenticates the user with the given password against the local |
| 29 |
user database; returns an array with the following information: |
| 30 |
|
| 31 |
success => true/false, |
| 32 |
tokens => array(tokens that should be given to user), |
| 33 |
timeout => how long the ticket should be valid (in seconds) |
| 34 |
graceperiod => how long the ticket should be refreshed before expiring (in seconds) |
| 35 |
*/ |
| 36 |
function local_login($username, $password) { |
| 37 |
$user_info = get_login_info($username); |
| 38 |
|
| 39 |
if (isset($user_info) && is_array($user_info)) { |
| 40 |
$out_info = $user_info['data']; |
| 41 |
$out_info['success'] = ($user_info['password'] === $password || $user_info['password'] === md5($password)); |
| 42 |
|
| 43 |
return $out_info; |
| 44 |
} |
| 45 |
|
| 46 |
return array('success' => false); |
| 47 |
} |
| 48 |
|
| 49 |
function get_login_info($username) { |
| 50 |
global $localuserdb, $default_timeout, $default_graceperiod; |
| 51 |
|
| 52 |
$fd = @fopen($localuserdb, "r"); |
| 53 |
if ($fd) { |
| 54 |
while (!feof($fd)) { |
| 55 |
$line = trim(fgets($fd)); |
| 56 |
if (preg_match("/^\s*#/", $line)) |
| 57 |
continue; |
| 58 |
if (!$line) |
| 59 |
continue; |
| 60 |
|
| 61 |
list($cusername,$cpassword,$tokens,$timeout,$graceperiod) = explode("\t", $line); |
| 62 |
|
| 63 |
if (!$timeout) |
| 64 |
$timeout = $default_timeout; |
| 65 |
|
| 66 |
if (!$graceperiod) |
| 67 |
$graceperiod = $default_graceperiod; |
| 68 |
|
| 69 |
if ($cusername === $username) { |
| 70 |
fclose($fd); |
| 71 |
return array('login' => $cusername, 'password' => $cpassword, |
| 72 |
'data' => array('tokens' => explode(",", $tokens), 'timeout' => $timeout, 'graceperiod' => $graceperiod)); |
| 73 |
} |
| 74 |
} |
| 75 |
fclose($fd); |
| 76 |
} |
| 77 |
|
| 78 |
return NULL; |
| 79 |
} |
| 80 |
|
| 81 |
/* very simple file-based login auditing */ |
| 82 |
function log_login($ip, $username, $success) { |
| 83 |
global $logfile; |
| 84 |
$fd = @fopen($logfile, "a"); |
| 85 |
if ($fd) { |
| 86 |
fputs($fd, time() . "\t$ip\t$username\t" . ($success ? "1" : "0") . "\n"); |
| 87 |
fclose($fd); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/* use the last username, if known (saves the user from having to type that all the time) */ |
| 92 |
$username = $_COOKIE['sso_lastuser']; |
| 93 |
$password = ""; |
| 94 |
$err = ""; |
| 95 |
$loginsuccess = false; |
| 96 |
|
| 97 |
if ($_GET['back']) { |
| 98 |
/* Extract the host name of the 'back' URL so we can tell the user when |
| 99 |
there will be no point in trying to log in, as the cookie won't be |
| 100 |
available to the target server (e.g. if users try to access a server |
| 101 |
by its IP address instead of by its proper host name), thus |
| 102 |
avoiding confusion. */ |
| 103 |
$urlp = parse_url($_GET['back']); |
| 104 |
$reshost = $urlp['host']; |
| 105 |
$server_allowed = preg_match("/$domain\$/", $reshost); |
| 106 |
} else { |
| 107 |
$server_allowed = true; |
| 108 |
} |
| 109 |
|
| 110 |
if ($_POST) { |
| 111 |
$username = strtolower($_POST['username']); /* always lower-case usernames for easier matching */ |
| 112 |
$password = $_POST['password']; |
| 113 |
|
| 114 |
/* try to authenticate */ |
| 115 |
$res = local_login($username, $password); |
| 116 |
|
| 117 |
if ($res['success']) { |
| 118 |
log_login($_SERVER['REMOTE_ADDR'], $username, true); |
| 119 |
|
| 120 |
$tkt_validuntil = time() + $res['timeout']; |
| 121 |
|
| 122 |
/* generate the ticket now and set a domain cookie */ |
| 123 |
$tkt = pubtkt_generate($privkeyfile, $keytype, $digest, $username, |
| 124 |
$_SERVER['REMOTE_ADDR'], $tkt_validuntil, $res['graceperiod'], join(",", $res['tokens']), ""); |
| 125 |
setcookie("auth_pubtkt", $tkt, 0, "/", $domain, $secure_cookie); |
| 126 |
|
| 127 |
setcookie("sso_lastuser", $username, time()+30*24*60*60); |
| 128 |
|
| 129 |
if ($_GET['back']) { |
| 130 |
header("Location: " . $_GET['back']); |
| 131 |
exit; |
| 132 |
} |
| 133 |
} else { |
| 134 |
log_login($_SERVER['REMOTE_ADDR'], $username, false); |
| 135 |
$loginerr = "Authentication failed. Please try again."; |
| 136 |
} |
| 137 |
} else { |
| 138 |
if ($_COOKIE['auth_pubtkt']) { |
| 139 |
/* Extract data from existing cookie so we can nicely offer the user |
| 140 |
a logout function. No attempt at verifying the ticket is made, |
| 141 |
as that's not necessary at this point. */ |
| 142 |
$ticket = pubtkt_parse($_COOKIE['auth_pubtkt']); |
| 143 |
$tkt_validuntil = $ticket['validuntil']; |
| 144 |
$tkt_graceperiod = $ticket['graceperiod']; |
| 145 |
$tkt_uid = $ticket['uid']; |
| 146 |
|
| 147 |
/* Checking validity of the ticket and if we are between begin of grace |
| 148 |
period and end of ticket validity. If so we can refresh ticket */ |
| 149 |
if (pubtkt_verify($pubkeyfile, $keytype, $digest, $ticket) && isset($tkt_graceperiod) |
| 150 |
&& is_numeric($tkt_graceperiod) && ($tkt_graceperiod <= time()) |
| 151 |
&& (time() <= $tkt_validuntil)) { |
| 152 |
|
| 153 |
/* getting user information */ |
| 154 |
$user_info = get_login_info($tkt_uid); |
| 155 |
|
| 156 |
if (isset($user_info) && is_array($user_info)) { |
| 157 |
|
| 158 |
$tkt_validuntil = time() + $user_info['data']['timeout']; |
| 159 |
|
| 160 |
/* generate the ticket now and set a domain cookie */ |
| 161 |
$tkt = pubtkt_generate($privkeyfile, $keytype, $digest, $tkt_uid, |
| 162 |
$ticket['cip'], $tkt_validuntil, $user_info['data']['graceperiod'], join(",", $user_info['data']['tokens']), ""); |
| 163 |
setcookie("auth_pubtkt", $tkt, 0, "/", $domain, $secure_cookie); |
| 164 |
|
| 165 |
setcookie("sso_lastuser", $tkt_uid, time()+30*24*60*60); |
| 166 |
|
| 167 |
if ($_GET['back']) { |
| 168 |
header("Location: " . $_GET['back']); |
| 169 |
exit; |
| 170 |
} |
| 171 |
} else { |
| 172 |
/* User is not present in user database (anymore) - delete the cookie */ |
| 173 |
setcookie("auth_pubtkt", false, time() - 86400, "/", $domain, $secure_cookie); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
?> |
| 180 |
<html> |
| 181 |
<head> |
| 182 |
<title>mod_auth_pubtkt Single Sign-On</title> |
| 183 |
<link rel="stylesheet" type="text/css" href="style.css"> |
| 184 |
<script type="text/javascript"> |
| 185 |
function dofocus() { |
| 186 |
if (document.loginform.username.value) |
| 187 |
document.loginform.password.focus(); |
| 188 |
else |
| 189 |
document.loginform.username.focus(); |
| 190 |
} |
| 191 |
|
| 192 |
/* The JS code below comes in useful when users open several SSO-protected |
| 193 |
web pages at once without being logged in (e.g. by opening a group of |
| 194 |
bookmarks in tabs. Without this, after logging in on any of the many |
| 195 |
SSO login forms they'd see, they'd manually have to refresh all other |
| 196 |
tabs/windows. The code below checks for changes in the login cookie and |
| 197 |
attempts to direct the browser to the 'back' URL if a change is detected. |
| 198 |
*/ |
| 199 |
|
| 200 |
var initial_cookie; |
| 201 |
|
| 202 |
function checkCookie_initial() { |
| 203 |
initial_cookie = readCookie('auth_pubtkt'); |
| 204 |
setTimeout('checkCookie()', 1000); |
| 205 |
} |
| 206 |
|
| 207 |
function checkCookie() { |
| 208 |
/* look for valid login cookie, and if found, redirect to 'back' URL */ |
| 209 |
var cookie = readCookie('auth_pubtkt'); |
| 210 |
if (cookie != initial_cookie) { |
| 211 |
/* cookie has changed - redirect if back URL provided */ |
| 212 |
var backurl = getArg('back'); |
| 213 |
if (backurl) |
| 214 |
document.location.href = backurl; |
| 215 |
} else { |
| 216 |
setTimeout('checkCookie()', 1000); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
function getArg(argname) { |
| 221 |
var qs = document.location.search.substring(1, document.location.search.length); |
| 222 |
if (qs.length == 0) |
| 223 |
return; |
| 224 |
|
| 225 |
qs = qs.replace(/\+/g, ' '); |
| 226 |
var args = qs.split('&'); |
| 227 |
|
| 228 |
for (var i = 0; i < args.length; i++) { |
| 229 |
var pair = args[i].split('='); |
| 230 |
|
| 231 |
if (pair[0] == argname) |
| 232 |
return unescape(pair[1]); |
| 233 |
} |
| 234 |
|
| 235 |
return null; |
| 236 |
} |
| 237 |
|
| 238 |
function readCookie(cookiename) { |
| 239 |
var nameEQ = cookiename + "="; |
| 240 |
var ca = document.cookie.split(';'); |
| 241 |
for(var i=0;i < ca.length;i++) { |
| 242 |
var c = ca[i]; |
| 243 |
while (c.charAt(0)==' ') c = c.substring(1,c.length); |
| 244 |
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); |
| 245 |
} |
| 246 |
return null; |
| 247 |
} |
| 248 |
</script> |
| 249 |
</head> |
| 250 |
|
| 251 |
<body onload="dofocus(); checkCookie_initial()"> |
| 252 |
|
| 253 |
<table width="100%" height="100%"> |
| 254 |
<tr><td align="center" valign="middle"> |
| 255 |
|
| 256 |
<img src="logo.gif"> |
| 257 |
<h2>Single Sign-On</h2> |
| 258 |
|
| 259 |
<?php if (!$server_allowed): ?> |
| 260 |
|
| 261 |
<p>The server <?php echo htmlspecialchars($reshost); ?> is unknown.</p> |
| 262 |
|
| 263 |
<?php elseif ($loginsuccess): ?> |
| 264 |
|
| 265 |
<p>You have successfully signed on.</p> |
| 266 |
<p class="small">Your login ticket will expire on <?php echo date("d.m.Y H:i:s", $tkt_validuntil); ?>.</p> |
| 267 |
|
| 268 |
<?php else: ?> |
| 269 |
|
| 270 |
<?php if ($_GET['timeout']): ?> |
| 271 |
<p>Your session has ended due to a timeout; please log in again.</p> |
| 272 |
<?php elseif ($_GET['unauth']): ?> |
| 273 |
<p>You don't have permission to access the desired resource on |
| 274 |
<?php echo htmlspecialchars($reshost); ?>;<br>you may try logging in again |
| 275 |
with different credentials.</p> |
| 276 |
<?php elseif ($tkt_uid && $tkt_validuntil >= time() && $ticket['cip'] == $_SERVER['REMOTE_ADDR']): ?> |
| 277 |
<p>You are currently logged on as '<?php echo htmlspecialchars($tkt_uid); ?>'. |
| 278 |
<form action="logout.php" method="POST"> |
| 279 |
<input type="submit" name="logout" value="Logout"> |
| 280 |
</form> |
| 281 |
</p> |
| 282 |
<?php endif; ?> |
| 283 |
|
| 284 |
<?php if ($loginerr): ?> |
| 285 |
<p class="errmsg"><?php echo nl2br(htmlspecialchars($loginerr)); ?></p> |
| 286 |
<?php endif; ?> |
| 287 |
|
| 288 |
<form name="loginform" method="POST" action=""> |
| 289 |
<table class="logintbl"> |
| 290 |
<tr> |
| 291 |
<th>Username:</th> |
| 292 |
<td><input type="text" name="username" size="20" value="<?php echo htmlspecialchars($username); ?>"></td> |
| 293 |
</tr> |
| 294 |
<tr> |
| 295 |
<th>Password:</th> |
| 296 |
<td><input type="password" name="password" size="20" autocomplete="off"></td> |
| 297 |
</tr> |
| 298 |
<tr class="blank"> |
| 299 |
<td></td> |
| 300 |
<td><input type="submit" name="submit" value="Login"></td> |
| 301 |
</tr> |
| 302 |
</table> |
| 303 |
</form> |
| 304 |
|
| 305 |
<?php endif; ?> |
| 306 |
|
| 307 |
</tr></td> |
| 308 |
</table> |
| 309 |
|
| 310 |
</body> |
| 311 |
</html> |