1ed7b5f09Sandi<?php 215fae107Sandi/** 315fae107Sandi * Authentication library 415fae107Sandi * 515fae107Sandi * Including this file will automatically try to login 615fae107Sandi * a user by calling auth_login() 715fae107Sandi * 815fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1015fae107Sandi */ 1115fae107Sandi 12ed7b5f09Sandi if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 13ed7b5f09Sandi require_once(DOKU_INC.'inc/common.php'); 14ed7b5f09Sandi require_once(DOKU_INC.'inc/io.php'); 15ed7b5f09Sandi require_once(DOKU_INC.'inc/blowfish.php'); 16ed7b5f09Sandi require_once(DOKU_INC.'inc/mail.php'); 1715fae107Sandi // load the the auth functions 18f62ea8a1Sandi require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.php'); 19f3f0262cSandi 2015fae107Sandi // some ACL level defines 21f3f0262cSandi define('AUTH_NONE',0); 22f3f0262cSandi define('AUTH_READ',1); 23f3f0262cSandi define('AUTH_EDIT',2); 24f3f0262cSandi define('AUTH_CREATE',4); 25f3f0262cSandi define('AUTH_UPLOAD',8); 268ef6b7caSandi define('AUTH_DELETE',16); 2710a76f6fSfrank define('AUTH_ADMIN',255); 28f3f0262cSandi 29f3f0262cSandi if($conf['useacl']){ 30132bdbfeSandi auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 3115fae107Sandi //load ACL into a global array 32*11799630Sandi if(is_readable(DOKU_INC.'conf/acl.auth.php')){ 33f62ea8a1Sandi $AUTH_ACL = file(DOKU_INC.'conf/acl.auth.php'); 34*11799630Sandi }else{ 35*11799630Sandi $AUTH_ACL = array(); 36*11799630Sandi } 37f3f0262cSandi } 38f3f0262cSandi 39f3f0262cSandi/** 40f3f0262cSandi * This tries to login the user based on the sent auth credentials 41f3f0262cSandi * 42f3f0262cSandi * The authentication works like this: if a username was given 4315fae107Sandi * a new login is assumed and user/password are checked. If they 4415fae107Sandi * are correct the password is encrypted with blowfish and stored 4515fae107Sandi * together with the username in a cookie - the same info is stored 4615fae107Sandi * in the session, too. Additonally a browserID is stored in the 4715fae107Sandi * session. 4815fae107Sandi * 4915fae107Sandi * If no username was given the cookie is checked: if the username, 5015fae107Sandi * crypted password and browserID match between session and cookie 5115fae107Sandi * no further testing is done and the user is accepted 5215fae107Sandi * 5315fae107Sandi * If a cookie was found but no session info was availabe the 54136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 5515fae107Sandi * together with username rechecked by calling this function again. 56f3f0262cSandi * 57f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 58f3f0262cSandi * are set. 5915fae107Sandi * 6015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 6115fae107Sandi * 6215fae107Sandi * @param string $user Username 6315fae107Sandi * @param string $pass Cleartext Password 6415fae107Sandi * @param bool $sticky Cookie should not expire 6515fae107Sandi * @return bool true on successful auth 66f3f0262cSandi*/ 67132bdbfeSandifunction auth_login($user,$pass,$sticky=false){ 68f3f0262cSandi global $USERINFO; 69f3f0262cSandi global $conf; 70f3f0262cSandi global $lang; 71132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 72f3f0262cSandi 73f3f0262cSandi if(isset($user)){ 74132bdbfeSandi //usual login 75f3f0262cSandi if (auth_checkPass($user,$pass)){ 76132bdbfeSandi // make logininfo globally available 77f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 78132bdbfeSandi $USERINFO = auth_getUserData($user); //FIXME move all references to session 79132bdbfeSandi 80132bdbfeSandi // set cookie 81132bdbfeSandi $pass = PMA_blowfish_encrypt($pass,auth_cookiesalt()); 82132bdbfeSandi $cookie = base64_encode("$user|$sticky|$pass"); 83132bdbfeSandi if($sticky) $time = time()+60*60*24*365; //one year 847009a5a9Sandi setcookie('DokuWikiAUTH',$cookie,$time,'/'); 85132bdbfeSandi 86132bdbfeSandi // set session 87132bdbfeSandi $_SESSION[$conf['title']]['auth']['user'] = $user; 88132bdbfeSandi $_SESSION[$conf['title']]['auth']['pass'] = $pass; 89132bdbfeSandi $_SESSION[$conf['title']]['auth']['buid'] = auth_browseruid(); 90132bdbfeSandi $_SESSION[$conf['title']]['auth']['info'] = $USERINFO; 91132bdbfeSandi return true; 92f3f0262cSandi }else{ 93f3f0262cSandi //invalid credentials - log off 94f3f0262cSandi msg($lang['badlogin'],-1); 95f3f0262cSandi auth_logoff(); 96132bdbfeSandi return false; 97f3f0262cSandi } 98f3f0262cSandi }else{ 99132bdbfeSandi // read cookie information 100132bdbfeSandi $cookie = base64_decode($_COOKIE['DokuWikiAUTH']); 101132bdbfeSandi list($user,$sticky,$pass) = split('\|',$cookie,3); 102132bdbfeSandi // get session info 103132bdbfeSandi $session = $_SESSION[$conf['title']]['auth']; 104132bdbfeSandi 105132bdbfeSandi if($user && $pass){ 106132bdbfeSandi // we got a cookie - see if we can trust it 107132bdbfeSandi if(isset($session) && 108132bdbfeSandi ($session['user'] == $user) && 109132bdbfeSandi ($session['pass'] == $pass) && //still crypted 110132bdbfeSandi ($session['buid'] == auth_browseruid()) ){ 111132bdbfeSandi // he has session, cookie and browser right - let him in 112132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 113132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 114132bdbfeSandi return true; 115132bdbfeSandi } 116132bdbfeSandi // no we don't trust it yet - recheck pass 117132bdbfeSandi $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt()); 118132bdbfeSandi return auth_login($user,$pass,$sticky); 119132bdbfeSandi } 120132bdbfeSandi } 121f3f0262cSandi //just to be sure 122f3f0262cSandi auth_logoff(); 123132bdbfeSandi return false; 124f3f0262cSandi} 125132bdbfeSandi 126132bdbfeSandi/** 127136ce040Sandi * Builds a pseudo UID from browser and IP data 128132bdbfeSandi * 129132bdbfeSandi * This is neither unique nor unfakable - still it adds some 130136ce040Sandi * security. Using the first part of the IP makes sure 131136ce040Sandi * proxy farms like AOLs are stil okay. 13215fae107Sandi * 13315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 13415fae107Sandi * 13515fae107Sandi * @return string a MD5 sum of various browser headers 136132bdbfeSandi */ 137132bdbfeSandifunction auth_browseruid(){ 138132bdbfeSandi $uid = ''; 139132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 140132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 141132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 142132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 143136ce040Sandi $uid .= substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],'.')); 144132bdbfeSandi return md5($uid); 145132bdbfeSandi} 146132bdbfeSandi 147132bdbfeSandi/** 148132bdbfeSandi * Creates a random key to encrypt the password in cookies 14915fae107Sandi * 15015fae107Sandi * This function tries to read the password for encrypting 1519afe4dbfSjan * cookies from $conf['datadir'].'/_cache/_htcookiesalt' 15215fae107Sandi * if no such file is found a random key is created and 15315fae107Sandi * and stored in this file. 15415fae107Sandi * 15515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 15615fae107Sandi * 15715fae107Sandi * @return string 158132bdbfeSandi */ 159132bdbfeSandifunction auth_cookiesalt(){ 160132bdbfeSandi global $conf; 1619afe4dbfSjan $file = $conf['datadir'].'/_cache/_htcookiesalt'; 162132bdbfeSandi $salt = io_readFile($file); 163132bdbfeSandi if(empty($salt)){ 164132bdbfeSandi $salt = uniqid(rand(),true); 165132bdbfeSandi io_saveFile($file,$salt); 166132bdbfeSandi } 167132bdbfeSandi return $salt; 168f3f0262cSandi} 169f3f0262cSandi 170f3f0262cSandi/** 171f3f0262cSandi * This clears all authenticationdata and thus log the user 172f3f0262cSandi * off 17315fae107Sandi * 17415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 175f3f0262cSandi */ 176f3f0262cSandifunction auth_logoff(){ 177f3f0262cSandi global $conf; 178f3f0262cSandi global $USERINFO; 179132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['user']); 180132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['pass']); 181132bdbfeSandi unset($_SESSION[$conf['title']]['auth']['info']); 182f3f0262cSandi unset($_SERVER['REMOTE_USER']); 183132bdbfeSandi $USERINFO=null; //FIXME 1847009a5a9Sandi setcookie('DokuWikiAUTH','',time()-600000,'/'); 185f3f0262cSandi} 186f3f0262cSandi 187f3f0262cSandi/** 18815fae107Sandi * Convinience function for auth_aclcheck() 18915fae107Sandi * 19015fae107Sandi * This checks the permissions for the current user 19115fae107Sandi * 19215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 19315fae107Sandi * 19415fae107Sandi * @param string $id page ID 19515fae107Sandi * @return int permission level 196f3f0262cSandi */ 197f3f0262cSandifunction auth_quickaclcheck($id){ 198f3f0262cSandi global $conf; 199f3f0262cSandi global $USERINFO; 200f3f0262cSandi # if no ACL is used always return upload rights 201f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 202f3f0262cSandi return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']); 203f3f0262cSandi} 204f3f0262cSandi 205f3f0262cSandi/** 206f3f0262cSandi * Returns the maximum rights a user has for 207f3f0262cSandi * the given ID or its namespace 20815fae107Sandi * 20915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 21015fae107Sandi * 21115fae107Sandi * @param string $id page ID 21215fae107Sandi * @param string $user Username 21315fae107Sandi * @param array $groups Array of groups the user is in 21415fae107Sandi * @return int permission level 215f3f0262cSandi */ 216f3f0262cSandifunction auth_aclcheck($id,$user,$groups){ 217f3f0262cSandi global $conf; 218f3f0262cSandi global $AUTH_ACL; 219f3f0262cSandi 220f3f0262cSandi # if no ACL is used always return upload rights 221f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 222f3f0262cSandi 22310a76f6fSfrank //if user is superuser return 255 (acl_admin) 22410a76f6fSfrank if($conf['superuser'] == $user) { return AUTH_ADMIN; } 22510a76f6fSfrank 226074cf26bSandi //make sure groups is an array 227074cf26bSandi if(!is_array($groups)) $groups = array(); 228074cf26bSandi 22910a76f6fSfrank //prepend groups with @ 2302cd2db38Sandi $cnt = count($groups); 2312cd2db38Sandi for($i=0; $i<$cnt; $i++){ 23210a76f6fSfrank $groups[$i] = '@'.$groups[$i]; 23310a76f6fSfrank } 23410a76f6fSfrank //if user is in superuser group return 255 (acl_admin) 23510a76f6fSfrank if(in_array($conf['superuser'], $groups)) { return AUTH_ADMIN; } 23610a76f6fSfrank 237f3f0262cSandi $ns = getNS($id); 238f3f0262cSandi $perm = -1; 239f3f0262cSandi 240f3f0262cSandi if($user){ 241f3f0262cSandi //add ALL group 242f3f0262cSandi $groups[] = '@ALL'; 243f3f0262cSandi //add User 244f3f0262cSandi $groups[] = $user; 245f3f0262cSandi //build regexp 246f3f0262cSandi $regexp = join('|',$groups); 247f3f0262cSandi }else{ 248f3f0262cSandi $regexp = '@ALL'; 249f3f0262cSandi } 250f3f0262cSandi 251f3f0262cSandi //check exact match first 252f3f0262cSandi $matches = preg_grep('/^'.$id.'\s+('.$regexp.')\s+/',$AUTH_ACL); 253f3f0262cSandi if(count($matches)){ 254f3f0262cSandi foreach($matches as $match){ 255f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 256f3f0262cSandi $acl = preg_split('/\s+/',$match); 2578ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 258f3f0262cSandi if($acl[2] > $perm){ 259f3f0262cSandi $perm = $acl[2]; 260f3f0262cSandi } 261f3f0262cSandi } 262f3f0262cSandi if($perm > -1){ 263f3f0262cSandi //we had a match - return it 264f3f0262cSandi return $perm; 265f3f0262cSandi } 266f3f0262cSandi } 267f3f0262cSandi 268f3f0262cSandi //still here? do the namespace checks 269f3f0262cSandi if($ns){ 270f3f0262cSandi $path = $ns.':\*'; 271f3f0262cSandi }else{ 272f3f0262cSandi $path = '\*'; //root document 273f3f0262cSandi } 274f3f0262cSandi 275f3f0262cSandi do{ 276f3f0262cSandi $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/',$AUTH_ACL); 277f3f0262cSandi if(count($matches)){ 278f3f0262cSandi foreach($matches as $match){ 279f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 280f3f0262cSandi $acl = preg_split('/\s+/',$match); 2818ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 282f3f0262cSandi if($acl[2] > $perm){ 283f3f0262cSandi $perm = $acl[2]; 284f3f0262cSandi } 285f3f0262cSandi } 286f3f0262cSandi //we had a match - return it 287f3f0262cSandi return $perm; 288f3f0262cSandi } 289f3f0262cSandi 290f3f0262cSandi //get next higher namespace 291f3f0262cSandi $ns = getNS($ns); 292f3f0262cSandi 293f3f0262cSandi if($path != '\*'){ 294f3f0262cSandi $path = $ns.':\*'; 295f3f0262cSandi if($path == ':\*') $path = '\*'; 296f3f0262cSandi }else{ 297f3f0262cSandi //we did this already 298f3f0262cSandi //looks like there is something wrong with the ACL 299f3f0262cSandi //break here 300f3f0262cSandi return $perm; 301f3f0262cSandi } 302f3f0262cSandi }while(1); //this should never loop endless 30352a5af8dSandi 30452a5af8dSandi //still here? return no permissions 30552a5af8dSandi return AUTH_NONE; 306f3f0262cSandi} 307f3f0262cSandi 308f3f0262cSandi/** 309f3f0262cSandi * Create a pronouncable password 310f3f0262cSandi * 31115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 31215fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 31315fae107Sandi * 31415fae107Sandi * @return string pronouncable password 315f3f0262cSandi */ 316f3f0262cSandifunction auth_pwgen(){ 317f3f0262cSandi $pw = ''; 318f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 319f3f0262cSandi $v = 'aeiou'; //vowels 320f3f0262cSandi $a = $c.$v; //both 321f3f0262cSandi 322f3f0262cSandi //use two syllables... 323f3f0262cSandi for($i=0;$i < 2; $i++){ 324f3f0262cSandi $pw .= $c[rand(0, strlen($c)-1)]; 325f3f0262cSandi $pw .= $v[rand(0, strlen($v)-1)]; 326f3f0262cSandi $pw .= $a[rand(0, strlen($a)-1)]; 327f3f0262cSandi } 328f3f0262cSandi //... and add a nice number 329f3f0262cSandi $pw .= rand(10,99); 330f3f0262cSandi 331f3f0262cSandi return $pw; 332f3f0262cSandi} 333f3f0262cSandi 334f3f0262cSandi/** 335f3f0262cSandi * Sends a password to the given user 336f3f0262cSandi * 33715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 33815fae107Sandi * 33915fae107Sandi * @return bool true on success 340f3f0262cSandi */ 341f3f0262cSandifunction auth_sendPassword($user,$password){ 342f3f0262cSandi global $conf; 343f3f0262cSandi global $lang; 344f3f0262cSandi $hdrs = ''; 34587ddda95Sandi $userinfo = auth_getUserData($user); 346f3f0262cSandi 34787ddda95Sandi if(!$userinfo['mail']) return false; 348f3f0262cSandi 349f3f0262cSandi $text = rawLocale('password'); 350ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 35187ddda95Sandi $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 352f3f0262cSandi $text = str_replace('@LOGIN@',$user,$text); 353f3f0262cSandi $text = str_replace('@PASSWORD@',$password,$text); 354f3f0262cSandi $text = str_replace('@TITLE@',$conf['title'],$text); 355f3f0262cSandi 35644f669e9Sandi return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 35744f669e9Sandi $lang['regpwmail'], 35844f669e9Sandi $text, 35944f669e9Sandi $conf['mailfrom']); 360f3f0262cSandi} 361f3f0262cSandi 362f3f0262cSandi/** 36315fae107Sandi * Register a new user 364f3f0262cSandi * 36515fae107Sandi * This registers a new user - Data is read directly from $_POST 36615fae107Sandi * 36715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 36815fae107Sandi * 36915fae107Sandi * @return bool true on success, false on any error 370f3f0262cSandi */ 371f3f0262cSandifunction register(){ 372f3f0262cSandi global $lang; 373eb5d07e4Sjan global $conf; 374f3f0262cSandi 375f3f0262cSandi if(!$_POST['save']) return false; 376640145a5Sandi 377f3f0262cSandi //clean username 378f3f0262cSandi $_POST['login'] = preg_replace('/.*:/','',$_POST['login']); 379f3f0262cSandi $_POST['login'] = cleanID($_POST['login']); 380f3f0262cSandi //clean fullname and email 381f3f0262cSandi $_POST['fullname'] = trim(str_replace(':','',$_POST['fullname'])); 382f3f0262cSandi $_POST['email'] = trim(str_replace(':','',$_POST['email'])); 383f3f0262cSandi 384f3f0262cSandi if( empty($_POST['login']) || 385f3f0262cSandi empty($_POST['fullname']) || 386f3f0262cSandi empty($_POST['email']) ){ 387f3f0262cSandi msg($lang['regmissing'],-1); 388f3f0262cSandi return false; 389f3f0262cSandi } 390f3f0262cSandi 391cab2716aSmatthias.grimm if ($conf['autopasswd']) { 392cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 393cab2716aSmatthias.grimm } elseif (empty($_POST['pass']) || 394cab2716aSmatthias.grimm empty($_POST['passchk'])) { 395bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 396cab2716aSmatthias.grimm return false; 397cab2716aSmatthias.grimm } elseif ($_POST['pass'] != $_POST['passchk']) { 398bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 399cab2716aSmatthias.grimm return false; 400cab2716aSmatthias.grimm } else { 401cab2716aSmatthias.grimm $pass = $_POST['pass']; // accept checked and valid password 402cab2716aSmatthias.grimm } 403cab2716aSmatthias.grimm 404f3f0262cSandi //check mail 40544f669e9Sandi if(!mail_isvalid($_POST['email'])){ 406f3f0262cSandi msg($lang['regbadmail'],-1); 407f3f0262cSandi return false; 408f3f0262cSandi } 409f3f0262cSandi 410f3f0262cSandi //okay try to create the user 4117c37db8aSmatthias.grimm $pass = auth_createUser($_POST['login'],$pass,$_POST['fullname'],$_POST['email']); 412f3f0262cSandi if(empty($pass)){ 413f3f0262cSandi msg($lang['reguexists'],-1); 414f3f0262cSandi return false; 415f3f0262cSandi } 416f3f0262cSandi 417cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 418cab2716aSmatthias.grimm msg($lang['regsuccess2'],1); 419cab2716aSmatthias.grimm return true; 420cab2716aSmatthias.grimm } 421cab2716aSmatthias.grimm 422cab2716aSmatthias.grimm // autogenerated password? then send him the password 423f3f0262cSandi if (auth_sendPassword($_POST['login'],$pass)){ 424f3f0262cSandi msg($lang['regsuccess'],1); 425f3f0262cSandi return true; 426f3f0262cSandi }else{ 427f3f0262cSandi msg($lang['regmailfail'],-1); 428f3f0262cSandi return false; 429f3f0262cSandi } 430f3f0262cSandi} 431f3f0262cSandi 43210a76f6fSfrank/** 43310a76f6fSfrank * Uses a regular expresion to check if a given mail address is valid 43410a76f6fSfrank * 43510a76f6fSfrank * May not be completly RFC conform! 43610a76f6fSfrank * 43710a76f6fSfrank * @link http://www.webmasterworld.com/forum88/135.htm 43810a76f6fSfrank * 43910a76f6fSfrank * @param string $email the address to check 44010a76f6fSfrank * @return bool true if address is valid 44110a76f6fSfrank */ 44210a76f6fSfrankfunction isvalidemail($email){ 44310a76f6fSfrank return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email); 44410a76f6fSfrank} 44510a76f6fSfrank 446b0855b11Sandi/** 447b0855b11Sandi * Encrypts a password using the given method and salt 448b0855b11Sandi * 449b0855b11Sandi * If the selected method needs a salt and none was given, a random one 450b0855b11Sandi * is chosen. 451b0855b11Sandi * 452b0855b11Sandi * The following methods are understood: 453b0855b11Sandi * 454b0855b11Sandi * smd5 - Salted MD5 hashing 455b0855b11Sandi * md5 - Simple MD5 hashing 456b0855b11Sandi * sha1 - SHA1 hashing 457b0855b11Sandi * ssha - Salted SHA1 hashing 458d7be6245Sandi * crypt - Unix crypt 459d7be6245Sandi * mysql - MySQL password (old method) 460d7be6245Sandi * my411 - MySQL 4.1.1 password 461b0855b11Sandi * 462b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 463b0855b11Sandi * @return string The crypted password 464b0855b11Sandi */ 465b0855b11Sandifunction auth_cryptPassword($clear,$method='',$salt=''){ 466b0855b11Sandi global $conf; 467b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 46810a76f6fSfrank 469b0855b11Sandi //prepare a salt 470b0855b11Sandi if(empty($salt)) $salt = md5(uniqid(rand(), true)); 471b0855b11Sandi 472b0855b11Sandi switch(strtolower($method)){ 473b0855b11Sandi case 'smd5': 474b0855b11Sandi return crypt($clear,'$1$'.substr($salt,0,8).'$'); 475b0855b11Sandi case 'md5': 476b0855b11Sandi return md5($clear); 477b0855b11Sandi case 'sha1': 478b0855b11Sandi return sha1($clear); 479b0855b11Sandi case 'ssha': 480b0855b11Sandi $salt=substr($salt,0,4); 481d6e54e02Smatthiasgrimm return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt); 482b0855b11Sandi case 'crypt': 483b0855b11Sandi return crypt($clear,substr($salt,0,2)); 484d7be6245Sandi case 'mysql': 485d7be6245Sandi //from http://www.php.net/mysql comment by <soren at byu dot edu> 486d7be6245Sandi $nr=0x50305735; 487d7be6245Sandi $nr2=0x12345671; 488d7be6245Sandi $add=7; 489d7be6245Sandi $charArr = preg_split("//", $clear); 490d7be6245Sandi foreach ($charArr as $char) { 491d7be6245Sandi if (($char == '') || ($char == ' ') || ($char == '\t')) continue; 492d7be6245Sandi $charVal = ord($char); 493d7be6245Sandi $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8); 494d7be6245Sandi $nr2 += ($nr2 << 8) ^ $nr; 495d7be6245Sandi $add += $charVal; 496d7be6245Sandi } 497d7be6245Sandi return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff)); 498d7be6245Sandi case 'my411': 499d7be6245Sandi return '*'.sha1(pack("H*", sha1($clear))); 500b0855b11Sandi default: 501b0855b11Sandi msg("Unsupported crypt method $method",-1); 502b0855b11Sandi } 503b0855b11Sandi} 504b0855b11Sandi 505b0855b11Sandi/** 506b0855b11Sandi * Verifies a cleartext password against a crypted hash 507b0855b11Sandi * 508b0855b11Sandi * The method and salt used for the crypted hash is determined automatically 509b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs 510b0855b11Sandi * match true is is returned else false 511b0855b11Sandi * 512b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 513b0855b11Sandi * @return bool 514b0855b11Sandi */ 515b0855b11Sandifunction auth_verifyPassword($clear,$crypt){ 516b0855b11Sandi $method=''; 517b0855b11Sandi $salt=''; 518b0855b11Sandi 519b0855b11Sandi //determine the used method and salt 520d7be6245Sandi $len = strlen($crypt); 521b0855b11Sandi if(substr($crypt,0,3) == '$1$'){ 522b0855b11Sandi $method = 'smd5'; 523b0855b11Sandi $salt = substr($crypt,3,8); 524b0855b11Sandi }elseif(substr($crypt,0,6) == '{SSHA}'){ 525b0855b11Sandi $method = 'ssha'; 526b0855b11Sandi $salt = substr(base64_decode(substr($crypt, 6)),20); 527d7be6245Sandi }elseif($len == 32){ 528b0855b11Sandi $method = 'md5'; 529d7be6245Sandi }elseif($len == 40){ 530b0855b11Sandi $method = 'sha1'; 531d7be6245Sandi }elseif($len == 16){ 532d7be6245Sandi $method = 'mysql'; 533d7be6245Sandi }elseif($len == 41 && $crypt[0] == '*'){ 534d7be6245Sandi $method = 'my411'; 535b0855b11Sandi }else{ 536b0855b11Sandi $method = 'crypt'; 537b0855b11Sandi $salt = substr($crypt,0,2); 538b0855b11Sandi } 539b0855b11Sandi 540b0855b11Sandi //crypt and compare 541b0855b11Sandi if(auth_cryptPassword($clear,$method,$salt) === $crypt){ 542b0855b11Sandi return true; 543b0855b11Sandi } 544b0855b11Sandi return false; 545b0855b11Sandi} 546340756e4Sandi 547340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 548