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