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 1200976812SAndreas Gohr if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../').'/'); 13ed7b5f09Sandi require_once(DOKU_INC.'inc/common.php'); 14ed7b5f09Sandi require_once(DOKU_INC.'inc/io.php'); 151c73890cSAndreas Gohr 16ebf97c8fSAndreas Gohr // some ACL level defines 17ebf97c8fSAndreas Gohr define('AUTH_NONE',0); 18ebf97c8fSAndreas Gohr define('AUTH_READ',1); 19ebf97c8fSAndreas Gohr define('AUTH_EDIT',2); 20ebf97c8fSAndreas Gohr define('AUTH_CREATE',4); 21ebf97c8fSAndreas Gohr define('AUTH_UPLOAD',8); 22ebf97c8fSAndreas Gohr define('AUTH_DELETE',16); 23ebf97c8fSAndreas Gohr define('AUTH_ADMIN',255); 24ebf97c8fSAndreas Gohr 25742c66f8Schris global $conf; 26742c66f8Schris 271c73890cSAndreas Gohr if($conf['useacl']){ 28ed7b5f09Sandi require_once(DOKU_INC.'inc/blowfish.php'); 29ed7b5f09Sandi require_once(DOKU_INC.'inc/mail.php'); 308b06d178Schris 3103c4aec3Schris global $auth; 3203c4aec3Schris 338b06d178Schris // load the the backend auth functions and instantiate the auth object 348b06d178Schris if (@file_exists(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php')) { 358b06d178Schris require_once(DOKU_INC.'inc/auth/basic.class.php'); 368b06d178Schris require_once(DOKU_INC.'inc/auth/'.$conf['authtype'].'.class.php'); 378b06d178Schris 388b06d178Schris $auth_class = "auth_".$conf['authtype']; 39cd52f92dSchris if (class_exists($auth_class)) { 408b06d178Schris $auth = new $auth_class(); 41d2dde4ebSMatthias Grimm if ($auth->success == false) { 420f4f4adfSAndreas Gohr // degrade to unauthenticated user 43d2dde4ebSMatthias Grimm unset($auth); 440f4f4adfSAndreas Gohr auth_logoff(); 45cd52f92dSchris msg($lang['authtempfail'], -1); 46d2dde4ebSMatthias Grimm } 478b06d178Schris } else { 483816dcbcSAndreas Gohr nice_die($lang['authmodfailed']); 49cd52f92dSchris } 50cd52f92dSchris } else { 513816dcbcSAndreas Gohr nice_die($lang['authmodfailed']); 528b06d178Schris } 531c73890cSAndreas Gohr } 54f3f0262cSandi 55f5cb575dSAndreas Gohr // do the login either by cookie or provided credentials 561ec50243SAndreas Gohr if($conf['useacl']){ 571ec50243SAndreas Gohr if($auth){ 58bbbd6568SAndreas Gohr if (!isset($_REQUEST['u'])) $_REQUEST['u'] = ''; 59bbbd6568SAndreas Gohr if (!isset($_REQUEST['p'])) $_REQUEST['p'] = ''; 60bbbd6568SAndreas Gohr if (!isset($_REQUEST['r'])) $_REQUEST['r'] = ''; 61bbbd6568SAndreas Gohr 621e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 634a26ad85Schris if(empty($_REQUEST['u']) && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])){ 641e8c9c90SAndreas Gohr $_REQUEST['u'] = $_SERVER['PHP_AUTH_USER']; 651e8c9c90SAndreas Gohr $_REQUEST['p'] = $_SERVER['PHP_AUTH_PW']; 661e8c9c90SAndreas Gohr } 671e8c9c90SAndreas Gohr 68f5cb575dSAndreas Gohr // external trust mechanism in place? 6982fd59b6SAndreas Gohr if(!is_null($auth) && $auth->canDo('external')){ 70f5cb575dSAndreas Gohr $auth->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 71f5cb575dSAndreas Gohr }else{ 72132bdbfeSandi auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 73f5cb575dSAndreas Gohr } 741ec50243SAndreas Gohr } 75f5cb575dSAndreas Gohr 7615fae107Sandi //load ACL into a global array 7788e6a4f2SAndreas Gohr global $AUTH_ACL; 78e7cb32dcSAndreas Gohr if(is_readable(DOKU_CONF.'acl.auth.php')){ 79e7cb32dcSAndreas Gohr $AUTH_ACL = file(DOKU_CONF.'acl.auth.php'); 80f8cc3354SGuy Brand if(isset($_SERVER['REMOTE_USER'])){ 81a8fe108bSGuy Brand $AUTH_ACL = str_replace('@USER@',$_SERVER['REMOTE_USER'],$AUTH_ACL); 82a8fe108bSGuy Brand } 8311799630Sandi }else{ 8411799630Sandi $AUTH_ACL = array(); 8511799630Sandi } 86f3f0262cSandi } 87f3f0262cSandi 88f3f0262cSandi/** 89f3f0262cSandi * This tries to login the user based on the sent auth credentials 90f3f0262cSandi * 91f3f0262cSandi * The authentication works like this: if a username was given 9215fae107Sandi * a new login is assumed and user/password are checked. If they 9315fae107Sandi * are correct the password is encrypted with blowfish and stored 9415fae107Sandi * together with the username in a cookie - the same info is stored 9515fae107Sandi * in the session, too. Additonally a browserID is stored in the 9615fae107Sandi * session. 9715fae107Sandi * 9815fae107Sandi * If no username was given the cookie is checked: if the username, 9915fae107Sandi * crypted password and browserID match between session and cookie 10015fae107Sandi * no further testing is done and the user is accepted 10115fae107Sandi * 10215fae107Sandi * If a cookie was found but no session info was availabe the 103136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 10415fae107Sandi * together with username rechecked by calling this function again. 105f3f0262cSandi * 106f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 107f3f0262cSandi * are set. 10815fae107Sandi * 10915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 11015fae107Sandi * 11115fae107Sandi * @param string $user Username 11215fae107Sandi * @param string $pass Cleartext Password 11315fae107Sandi * @param bool $sticky Cookie should not expire 114f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 11515fae107Sandi * @return bool true on successful auth 116f3f0262cSandi*/ 117f112c2faSAndreas Gohrfunction auth_login($user,$pass,$sticky=false,$silent=false){ 118f3f0262cSandi global $USERINFO; 119f3f0262cSandi global $conf; 120f3f0262cSandi global $lang; 121cd52f92dSchris global $auth; 122132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 123f3f0262cSandi 124bbbd6568SAndreas Gohr if(!empty($user)){ 125132bdbfeSandi //usual login 126cd52f92dSchris if ($auth->checkPass($user,$pass)){ 127132bdbfeSandi // make logininfo globally available 128f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 1290f4f4adfSAndreas Gohr $USERINFO = $auth->getUserData($user); 130132bdbfeSandi 131132bdbfeSandi // set cookie 132132bdbfeSandi $pass = PMA_blowfish_encrypt($pass,auth_cookiesalt()); 133132bdbfeSandi $cookie = base64_encode("$user|$sticky|$pass"); 134132bdbfeSandi if($sticky) $time = time()+60*60*24*365; //one year 1354b1a4e04SAndreas Gohr setcookie(DOKU_COOKIE,$cookie,$time,DOKU_REL); 136132bdbfeSandi 137132bdbfeSandi // set session 138e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 139e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass; 140e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 141e71ce681SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1424c989037SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1434c989037SChris Smith 144132bdbfeSandi return true; 145f3f0262cSandi }else{ 146f3f0262cSandi //invalid credentials - log off 147f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'],-1); 148f3f0262cSandi auth_logoff(); 149132bdbfeSandi return false; 150f3f0262cSandi } 151f3f0262cSandi }else{ 152132bdbfeSandi // read cookie information 153e65afed4SSameer D. Sahasrabuddhe $cookie = base64_decode($_COOKIE[DOKU_COOKIE]); 154132bdbfeSandi list($user,$sticky,$pass) = split('\|',$cookie,3); 155132bdbfeSandi // get session info 156e71ce681SAndreas Gohr $session = $_SESSION[DOKU_COOKIE]['auth']; 157132bdbfeSandi if($user && $pass){ 158132bdbfeSandi // we got a cookie - see if we can trust it 159132bdbfeSandi if(isset($session) && 1607172dbc0SAndreas Gohr $auth->useSessionCache($user) && 1614c989037SChris Smith ($session['time'] >= time()-$conf['auth_security_timeout']) && 162132bdbfeSandi ($session['user'] == $user) && 163132bdbfeSandi ($session['pass'] == $pass) && //still crypted 164132bdbfeSandi ($session['buid'] == auth_browseruid()) ){ 165132bdbfeSandi // he has session, cookie and browser right - let him in 166132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 167132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 168132bdbfeSandi return true; 169132bdbfeSandi } 170f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 171132bdbfeSandi $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt()); 172f112c2faSAndreas Gohr return auth_login($user,$pass,$sticky,true); 173132bdbfeSandi } 174132bdbfeSandi } 175f3f0262cSandi //just to be sure 176f3f0262cSandi auth_logoff(); 177132bdbfeSandi return false; 178f3f0262cSandi} 179132bdbfeSandi 180132bdbfeSandi/** 181136ce040Sandi * Builds a pseudo UID from browser and IP data 182132bdbfeSandi * 183132bdbfeSandi * This is neither unique nor unfakable - still it adds some 184136ce040Sandi * security. Using the first part of the IP makes sure 185136ce040Sandi * proxy farms like AOLs are stil okay. 18615fae107Sandi * 18715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 18815fae107Sandi * 18915fae107Sandi * @return string a MD5 sum of various browser headers 190132bdbfeSandi */ 191132bdbfeSandifunction auth_browseruid(){ 192132bdbfeSandi $uid = ''; 193132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 194132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 195132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 196132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 197136ce040Sandi $uid .= substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],'.')); 198132bdbfeSandi return md5($uid); 199132bdbfeSandi} 200132bdbfeSandi 201132bdbfeSandi/** 202132bdbfeSandi * Creates a random key to encrypt the password in cookies 20315fae107Sandi * 20415fae107Sandi * This function tries to read the password for encrypting 20598407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 20615fae107Sandi * if no such file is found a random key is created and 20715fae107Sandi * and stored in this file. 20815fae107Sandi * 20915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 21015fae107Sandi * 21115fae107Sandi * @return string 212132bdbfeSandi */ 213132bdbfeSandifunction auth_cookiesalt(){ 214132bdbfeSandi global $conf; 21598407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 216132bdbfeSandi $salt = io_readFile($file); 217132bdbfeSandi if(empty($salt)){ 218132bdbfeSandi $salt = uniqid(rand(),true); 219132bdbfeSandi io_saveFile($file,$salt); 220132bdbfeSandi } 221132bdbfeSandi return $salt; 222f3f0262cSandi} 223f3f0262cSandi 224f3f0262cSandi/** 225f3f0262cSandi * This clears all authenticationdata and thus log the user 226f3f0262cSandi * off 22715fae107Sandi * 22815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 229f3f0262cSandi */ 230f3f0262cSandifunction auth_logoff(){ 231f3f0262cSandi global $conf; 232f3f0262cSandi global $USERINFO; 2338b06d178Schris global $INFO, $ID; 2345298a619SAndreas Gohr global $auth; 23537065e65Sandi 236e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 237e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 238e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 239e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 240e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 241e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 24237065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 243f3f0262cSandi unset($_SERVER['REMOTE_USER']); 244132bdbfeSandi $USERINFO=null; //FIXME 2454b1a4e04SAndreas Gohr setcookie(DOKU_COOKIE,'',time()-600000,DOKU_REL); 2465298a619SAndreas Gohr 2475298a619SAndreas Gohr if($auth && $auth->canDo('logoff')){ 2485298a619SAndreas Gohr $auth->logOff(); 2495298a619SAndreas Gohr } 250f3f0262cSandi} 251f3f0262cSandi 252f3f0262cSandi/** 253f8cc712eSAndreas Gohr * Check if a user is a manager 254f8cc712eSAndreas Gohr * 255f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 256f8cc712eSAndreas Gohr * user. 257f8cc712eSAndreas Gohr * 258f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 259f8cc712eSAndreas Gohr * 260f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 261f8cc712eSAndreas Gohr * @see auth_isadmin 262f8cc712eSAndreas Gohr * @param string user - Username 263f8cc712eSAndreas Gohr * @param array groups - List of groups the user is in 264f8cc712eSAndreas Gohr * @param bool adminonly - when true checks if user is admin 265f8cc712eSAndreas Gohr */ 266f8cc712eSAndreas Gohrfunction auth_ismanager($user=null,$groups=null,$adminonly=false){ 267f8cc712eSAndreas Gohr global $conf; 268f8cc712eSAndreas Gohr global $USERINFO; 269f8cc712eSAndreas Gohr 270f8cc712eSAndreas Gohr if(!$conf['useacl']) return false; 271f8cc712eSAndreas Gohr if(is_null($user)) $user = $_SERVER['REMOTE_USER']; 27290583e9fSAndreas Gohr if(is_null($groups)) $groups = (array) $USERINFO['grps']; 273f8cc712eSAndreas Gohr $user = auth_nameencode($user); 274f8cc712eSAndreas Gohr 275f8cc712eSAndreas Gohr // check username against superuser and manager 2767651d633SGuy Brand $superusers = explode(',', $conf['superuser']); 2777651d633SGuy Brand $superusers = array_unique($superusers); 2787651d633SGuy Brand $superusers = array_map('trim', $superusers); 2797651d633SGuy Brand // prepare an array containing only true values for array_map call 2807651d633SGuy Brand $alltrue = array_fill(0, count($superusers), true); 2817651d633SGuy Brand $superusers = array_map('auth_nameencode', $superusers, $alltrue); 2827651d633SGuy Brand if(in_array($user, $superusers)) return true; 2837651d633SGuy Brand 284f8cc712eSAndreas Gohr if(!$adminonly){ 2857651d633SGuy Brand $managers = explode(',', $conf['manager']); 2867651d633SGuy Brand $managers = array_unique($managers); 2877651d633SGuy Brand $managers = array_map('trim', $managers); 2887651d633SGuy Brand // prepare an array containing only true values for array_map call 2897651d633SGuy Brand $alltrue = array_fill(0, count($managers), true); 2907651d633SGuy Brand $managers = array_map('auth_nameencode', $managers, $alltrue); 2917651d633SGuy Brand if(in_array($user, $managers)) return true; 292f8cc712eSAndreas Gohr } 293f8cc712eSAndreas Gohr 29400ce12daSChris Smith // check user's groups against superuser and manager 29500ce12daSChris Smith if (!empty($groups)) { 29600ce12daSChris Smith 297f8cc712eSAndreas Gohr //prepend groups with @ and nameencode 298f8cc712eSAndreas Gohr $cnt = count($groups); 299f8cc712eSAndreas Gohr for($i=0; $i<$cnt; $i++){ 300f8cc712eSAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 301f8cc712eSAndreas Gohr } 302f8cc712eSAndreas Gohr 303f8cc712eSAndreas Gohr // check groups against superuser and manager 3047651d633SGuy Brand foreach($superusers as $supu) 3057651d633SGuy Brand if(in_array($supu, $groups)) return true; 306f8cc712eSAndreas Gohr if(!$adminonly){ 3077651d633SGuy Brand foreach($managers as $mana) 3087651d633SGuy Brand if(in_array($mana, $groups)) return true; 309f8cc712eSAndreas Gohr } 31000ce12daSChris Smith } 31100ce12daSChris Smith 312f8cc712eSAndreas Gohr return false; 313f8cc712eSAndreas Gohr} 314f8cc712eSAndreas Gohr 315f8cc712eSAndreas Gohr/** 316f8cc712eSAndreas Gohr * Check if a user is admin 317f8cc712eSAndreas Gohr * 318f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 319f8cc712eSAndreas Gohr * 320f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 321f8cc712eSAndreas Gohr * 322f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 323f8cc712eSAndreas Gohr * @see auth_ismanager 324f8cc712eSAndreas Gohr */ 325f8cc712eSAndreas Gohrfunction auth_isadmin($user=null,$groups=null){ 326f8cc712eSAndreas Gohr return auth_ismanager($user,$groups,true); 327f8cc712eSAndreas Gohr} 328f8cc712eSAndreas Gohr 329f8cc712eSAndreas Gohr/** 33015fae107Sandi * Convinience function for auth_aclcheck() 33115fae107Sandi * 33215fae107Sandi * This checks the permissions for the current user 33315fae107Sandi * 33415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 33515fae107Sandi * 33615fae107Sandi * @param string $id page ID 33715fae107Sandi * @return int permission level 338f3f0262cSandi */ 339f3f0262cSandifunction auth_quickaclcheck($id){ 340f3f0262cSandi global $conf; 341f3f0262cSandi global $USERINFO; 342f3f0262cSandi # if no ACL is used always return upload rights 343f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 344f3f0262cSandi return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']); 345f3f0262cSandi} 346f3f0262cSandi 347f3f0262cSandi/** 348f3f0262cSandi * Returns the maximum rights a user has for 349f3f0262cSandi * the given ID or its namespace 35015fae107Sandi * 35115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 35215fae107Sandi * 35315fae107Sandi * @param string $id page ID 35415fae107Sandi * @param string $user Username 35515fae107Sandi * @param array $groups Array of groups the user is in 35615fae107Sandi * @return int permission level 357f3f0262cSandi */ 358f3f0262cSandifunction auth_aclcheck($id,$user,$groups){ 359f3f0262cSandi global $conf; 360f3f0262cSandi global $AUTH_ACL; 361f3f0262cSandi 36285d03f68SAndreas Gohr // if no ACL is used always return upload rights 363f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 364f3f0262cSandi 365074cf26bSandi //make sure groups is an array 366074cf26bSandi if(!is_array($groups)) $groups = array(); 367074cf26bSandi 36885d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 36985d03f68SAndreas Gohr if(auth_isadmin($user,$groups)) { return AUTH_ADMIN; } 37085d03f68SAndreas Gohr 37185d03f68SAndreas Gohr $user = auth_nameencode($user); 37285d03f68SAndreas Gohr 3736c2bb100SAndreas Gohr //prepend groups with @ and nameencode 3742cd2db38Sandi $cnt = count($groups); 3752cd2db38Sandi for($i=0; $i<$cnt; $i++){ 3766c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 37710a76f6fSfrank } 37810a76f6fSfrank 379f3f0262cSandi $ns = getNS($id); 380f3f0262cSandi $perm = -1; 381f3f0262cSandi 382f3f0262cSandi if($user){ 383f3f0262cSandi //add ALL group 384f3f0262cSandi $groups[] = '@ALL'; 385f3f0262cSandi //add User 386f3f0262cSandi $groups[] = $user; 387f3f0262cSandi //build regexp 388f3f0262cSandi $regexp = join('|',$groups); 389f3f0262cSandi }else{ 390f3f0262cSandi $regexp = '@ALL'; 391f3f0262cSandi } 392f3f0262cSandi 393f3f0262cSandi //check exact match first 39442905504SAndreas Gohr $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/',$AUTH_ACL); 395f3f0262cSandi if(count($matches)){ 396f3f0262cSandi foreach($matches as $match){ 397f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 398f3f0262cSandi $acl = preg_split('/\s+/',$match); 3998ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 400f3f0262cSandi if($acl[2] > $perm){ 401f3f0262cSandi $perm = $acl[2]; 402f3f0262cSandi } 403f3f0262cSandi } 404f3f0262cSandi if($perm > -1){ 405f3f0262cSandi //we had a match - return it 406f3f0262cSandi return $perm; 407f3f0262cSandi } 408f3f0262cSandi } 409f3f0262cSandi 410f3f0262cSandi //still here? do the namespace checks 411f3f0262cSandi if($ns){ 412f3f0262cSandi $path = $ns.':\*'; 413f3f0262cSandi }else{ 414f3f0262cSandi $path = '\*'; //root document 415f3f0262cSandi } 416f3f0262cSandi 417f3f0262cSandi do{ 418f3f0262cSandi $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/',$AUTH_ACL); 419f3f0262cSandi if(count($matches)){ 420f3f0262cSandi foreach($matches as $match){ 421f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 422f3f0262cSandi $acl = preg_split('/\s+/',$match); 4238ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 424f3f0262cSandi if($acl[2] > $perm){ 425f3f0262cSandi $perm = $acl[2]; 426f3f0262cSandi } 427f3f0262cSandi } 428f3f0262cSandi //we had a match - return it 429f3f0262cSandi return $perm; 430f3f0262cSandi } 431f3f0262cSandi 432f3f0262cSandi //get next higher namespace 433f3f0262cSandi $ns = getNS($ns); 434f3f0262cSandi 435f3f0262cSandi if($path != '\*'){ 436f3f0262cSandi $path = $ns.':\*'; 437f3f0262cSandi if($path == ':\*') $path = '\*'; 438f3f0262cSandi }else{ 439f3f0262cSandi //we did this already 440f3f0262cSandi //looks like there is something wrong with the ACL 441f3f0262cSandi //break here 442d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 443d5ce66f6SAndreas Gohr return AUTH_NONE; 444f3f0262cSandi } 445f3f0262cSandi }while(1); //this should never loop endless 44652a5af8dSandi 44752a5af8dSandi //still here? return no permissions 44852a5af8dSandi return AUTH_NONE; 449f3f0262cSandi} 450f3f0262cSandi 451f3f0262cSandi/** 4526c2bb100SAndreas Gohr * Encode ASCII special chars 4536c2bb100SAndreas Gohr * 4546c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 4556c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 4566c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 4576c2bb100SAndreas Gohr * urlencoding!). 4586c2bb100SAndreas Gohr * 4596c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 4606c2bb100SAndreas Gohr * 4616c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 4626c2bb100SAndreas Gohr * @see rawurldecode() 4636c2bb100SAndreas Gohr */ 464e838fc2eSAndreas Gohrfunction auth_nameencode($name,$skip_group=false){ 465a424cd8eSchris global $cache_authname; 466a424cd8eSchris $cache =& $cache_authname; 46731784267SAndreas Gohr $name = (string) $name; 468a424cd8eSchris 469a424cd8eSchris if (!isset($cache[$name][$skip_group])) { 470e838fc2eSAndreas Gohr if($skip_group && $name{0} =='@'){ 471a424cd8eSchris $cache[$name][$skip_group] = '@'.preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 4721a9ae8e5SAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))",substr($name,1)); 473e838fc2eSAndreas Gohr }else{ 474a424cd8eSchris $cache[$name][$skip_group] = preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 4751a9ae8e5SAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))",$name); 476e838fc2eSAndreas Gohr } 4776c2bb100SAndreas Gohr } 4786c2bb100SAndreas Gohr 479a424cd8eSchris return $cache[$name][$skip_group]; 480a424cd8eSchris} 481a424cd8eSchris 4826c2bb100SAndreas Gohr/** 483f3f0262cSandi * Create a pronouncable password 484f3f0262cSandi * 48515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 48615fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 48715fae107Sandi * 48815fae107Sandi * @return string pronouncable password 489f3f0262cSandi */ 490f3f0262cSandifunction auth_pwgen(){ 491f3f0262cSandi $pw = ''; 492f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 493f3f0262cSandi $v = 'aeiou'; //vowels 494f3f0262cSandi $a = $c.$v; //both 495f3f0262cSandi 496f3f0262cSandi //use two syllables... 497f3f0262cSandi for($i=0;$i < 2; $i++){ 498f3f0262cSandi $pw .= $c[rand(0, strlen($c)-1)]; 499f3f0262cSandi $pw .= $v[rand(0, strlen($v)-1)]; 500f3f0262cSandi $pw .= $a[rand(0, strlen($a)-1)]; 501f3f0262cSandi } 502f3f0262cSandi //... and add a nice number 503f3f0262cSandi $pw .= rand(10,99); 504f3f0262cSandi 505f3f0262cSandi return $pw; 506f3f0262cSandi} 507f3f0262cSandi 508f3f0262cSandi/** 509f3f0262cSandi * Sends a password to the given user 510f3f0262cSandi * 51115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 51215fae107Sandi * 51315fae107Sandi * @return bool true on success 514f3f0262cSandi */ 515f3f0262cSandifunction auth_sendPassword($user,$password){ 516f3f0262cSandi global $conf; 517f3f0262cSandi global $lang; 518cd52f92dSchris global $auth; 519cd52f92dSchris 520f3f0262cSandi $hdrs = ''; 521cd52f92dSchris $userinfo = $auth->getUserData($user); 522f3f0262cSandi 52387ddda95Sandi if(!$userinfo['mail']) return false; 524f3f0262cSandi 525f3f0262cSandi $text = rawLocale('password'); 526ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 52787ddda95Sandi $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 528f3f0262cSandi $text = str_replace('@LOGIN@',$user,$text); 529f3f0262cSandi $text = str_replace('@PASSWORD@',$password,$text); 530f3f0262cSandi $text = str_replace('@TITLE@',$conf['title'],$text); 531f3f0262cSandi 53244f669e9Sandi return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 53344f669e9Sandi $lang['regpwmail'], 53444f669e9Sandi $text, 53544f669e9Sandi $conf['mailfrom']); 536f3f0262cSandi} 537f3f0262cSandi 538f3f0262cSandi/** 53915fae107Sandi * Register a new user 540f3f0262cSandi * 54115fae107Sandi * This registers a new user - Data is read directly from $_POST 54215fae107Sandi * 54315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 54415fae107Sandi * 54515fae107Sandi * @return bool true on success, false on any error 546f3f0262cSandi */ 547f3f0262cSandifunction register(){ 548f3f0262cSandi global $lang; 549eb5d07e4Sjan global $conf; 550cd52f92dSchris global $auth; 551f3f0262cSandi 552f3f0262cSandi if(!$_POST['save']) return false; 55382fd59b6SAndreas Gohr if(!$auth->canDo('addUser')) return false; 554640145a5Sandi 555f3f0262cSandi //clean username 556f3f0262cSandi $_POST['login'] = preg_replace('/.*:/','',$_POST['login']); 557f3f0262cSandi $_POST['login'] = cleanID($_POST['login']); 558f3f0262cSandi //clean fullname and email 55954f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname'])); 56054f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email'])); 561f3f0262cSandi 562f3f0262cSandi if( empty($_POST['login']) || 563f3f0262cSandi empty($_POST['fullname']) || 564f3f0262cSandi empty($_POST['email']) ){ 565f3f0262cSandi msg($lang['regmissing'],-1); 566f3f0262cSandi return false; 567f3f0262cSandi } 568f3f0262cSandi 569cab2716aSmatthias.grimm if ($conf['autopasswd']) { 570cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 571cab2716aSmatthias.grimm } elseif (empty($_POST['pass']) || 572cab2716aSmatthias.grimm empty($_POST['passchk'])) { 573bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 574cab2716aSmatthias.grimm return false; 575cab2716aSmatthias.grimm } elseif ($_POST['pass'] != $_POST['passchk']) { 576bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 577cab2716aSmatthias.grimm return false; 578cab2716aSmatthias.grimm } else { 579cab2716aSmatthias.grimm $pass = $_POST['pass']; // accept checked and valid password 580cab2716aSmatthias.grimm } 581cab2716aSmatthias.grimm 582f3f0262cSandi //check mail 58344f669e9Sandi if(!mail_isvalid($_POST['email'])){ 584f3f0262cSandi msg($lang['regbadmail'],-1); 585f3f0262cSandi return false; 586f3f0262cSandi } 587f3f0262cSandi 588f3f0262cSandi //okay try to create the user 5891d096a10SAndreas Gohr if(!$auth->createUser($_POST['login'],$pass,$_POST['fullname'],$_POST['email'])){ 590f3f0262cSandi msg($lang['reguexists'],-1); 591f3f0262cSandi return false; 592f3f0262cSandi } 593f3f0262cSandi 59402a498e7Schris // create substitutions for use in notification email 59502a498e7Schris $substitutions = array( 59602a498e7Schris 'NEWUSER' => $_POST['login'], 59702a498e7Schris 'NEWNAME' => $_POST['fullname'], 59802a498e7Schris 'NEWEMAIL' => $_POST['email'], 59902a498e7Schris ); 60002a498e7Schris 601cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 602cab2716aSmatthias.grimm msg($lang['regsuccess2'],1); 60302a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 604cab2716aSmatthias.grimm return true; 605cab2716aSmatthias.grimm } 606cab2716aSmatthias.grimm 607cab2716aSmatthias.grimm // autogenerated password? then send him the password 608f3f0262cSandi if (auth_sendPassword($_POST['login'],$pass)){ 609f3f0262cSandi msg($lang['regsuccess'],1); 61002a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 611f3f0262cSandi return true; 612f3f0262cSandi }else{ 613f3f0262cSandi msg($lang['regmailfail'],-1); 614f3f0262cSandi return false; 615f3f0262cSandi } 616f3f0262cSandi} 617f3f0262cSandi 61810a76f6fSfrank/** 6198b06d178Schris * Update user profile 6208b06d178Schris * 6218b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 6228b06d178Schris */ 6238b06d178Schrisfunction updateprofile() { 6248b06d178Schris global $conf; 6258b06d178Schris global $INFO; 6268b06d178Schris global $lang; 627cd52f92dSchris global $auth; 6288b06d178Schris 629bb4866bdSchris if(empty($_POST['save'])) return false; 6301b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 6318b06d178Schris 63282fd59b6SAndreas Gohr // should not be able to get here without Profile being possible... 63382fd59b6SAndreas Gohr if(!$auth->canDo('Profile')) { 6348b06d178Schris msg($lang['profna'],-1); 6358b06d178Schris return false; 6368b06d178Schris } 6378b06d178Schris 6388b06d178Schris if ($_POST['newpass'] != $_POST['passchk']) { 6398b06d178Schris msg($lang['regbadpass'], -1); // complain about misspelled passwords 6408b06d178Schris return false; 6418b06d178Schris } 6428b06d178Schris 6438b06d178Schris //clean fullname and email 64454f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname'])); 64554f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email'])); 6468b06d178Schris 6478b06d178Schris if (empty($_POST['fullname']) || empty($_POST['email'])) { 6488b06d178Schris msg($lang['profnoempty'],-1); 6498b06d178Schris return false; 6508b06d178Schris } 6518b06d178Schris 6528b06d178Schris if (!mail_isvalid($_POST['email'])){ 6538b06d178Schris msg($lang['regbadmail'],-1); 6548b06d178Schris return false; 6558b06d178Schris } 6568b06d178Schris 6574c21b7eeSAndreas Gohr if ($_POST['fullname'] != $INFO['userinfo']['name'] && $auth->canDo('modName')) $changes['name'] = $_POST['fullname']; 6584c21b7eeSAndreas Gohr if ($_POST['email'] != $INFO['userinfo']['mail'] && $auth->canDo('modMail')) $changes['mail'] = $_POST['email']; 659*cf626a62SAndreas Gohr if (!empty($_POST['newpass']) && $auth->canDo('modPass')) $changes['pass'] = $_POST['newpass']; 6604c21b7eeSAndreas Gohr 6618b06d178Schris 6628b06d178Schris if (!count($changes)) { 6638b06d178Schris msg($lang['profnochange'], -1); 6648b06d178Schris return false; 6658b06d178Schris } 6668b06d178Schris 6678b06d178Schris if ($conf['profileconfirm']) { 668df466c7aSAndreas Gohr if (!$auth->checkPass($_SERVER['REMOTE_USER'], $_POST['oldpass'])) { 6698b06d178Schris msg($lang['badlogin'],-1); 6708b06d178Schris return false; 6718b06d178Schris } 6728b06d178Schris } 6738b06d178Schris 674cd52f92dSchris return $auth->modifyUser($_SERVER['REMOTE_USER'], $changes); 6758b06d178Schris} 6768b06d178Schris 6778b06d178Schris/** 6788b06d178Schris * Send a new password 6798b06d178Schris * 6801d5856cfSAndreas Gohr * This function handles both phases of the password reset: 6811d5856cfSAndreas Gohr * 6821d5856cfSAndreas Gohr * - handling the first request of password reset 6831d5856cfSAndreas Gohr * - validating the password reset auth token 6841d5856cfSAndreas Gohr * 6858b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 6868b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 6871d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 6888b06d178Schris * 6898b06d178Schris * @return bool true on success, false on any error 6908b06d178Schris*/ 6918b06d178Schrisfunction act_resendpwd(){ 6928b06d178Schris global $lang; 6938b06d178Schris global $conf; 694cd52f92dSchris global $auth; 6958b06d178Schris 696409d7af7SAndreas Gohr if(!actionOK('resendpwd')) return false; 6978b06d178Schris 69882fd59b6SAndreas Gohr // should not be able to get here without modPass being possible... 69982fd59b6SAndreas Gohr if(!$auth->canDo('modPass')) { 7008b06d178Schris msg($lang['resendna'],-1); 7018b06d178Schris return false; 7028b06d178Schris } 7038b06d178Schris 7041d5856cfSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/','',$_REQUEST['pwauth']); 7058b06d178Schris 7061d5856cfSAndreas Gohr if($token){ 7071d5856cfSAndreas Gohr // we're in token phase 7081d5856cfSAndreas Gohr 7091d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 7101d5856cfSAndreas Gohr if(!@file_exists($tfile)){ 7111d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'],-1); 7121d5856cfSAndreas Gohr return false; 7131d5856cfSAndreas Gohr } 7141d5856cfSAndreas Gohr $user = io_readfile($tfile); 7151d5856cfSAndreas Gohr @unlink($tfile); 716cd52f92dSchris $userinfo = $auth->getUserData($user); 7178b06d178Schris if(!$userinfo['mail']) { 7188b06d178Schris msg($lang['resendpwdnouser'], -1); 7198b06d178Schris return false; 7208b06d178Schris } 7218b06d178Schris 7228b06d178Schris $pass = auth_pwgen(); 723cd52f92dSchris if (!$auth->modifyUser($user,array('pass' => $pass))) { 7248b06d178Schris msg('error modifying user data',-1); 7258b06d178Schris return false; 7268b06d178Schris } 7278b06d178Schris 7288b06d178Schris if (auth_sendPassword($user,$pass)) { 7298b06d178Schris msg($lang['resendpwdsuccess'],1); 7308b06d178Schris } else { 7318b06d178Schris msg($lang['regmailfail'],-1); 7328b06d178Schris } 7338b06d178Schris return true; 7341d5856cfSAndreas Gohr 7351d5856cfSAndreas Gohr } else { 7361d5856cfSAndreas Gohr // we're in request phase 7371d5856cfSAndreas Gohr 7381d5856cfSAndreas Gohr if(!$_POST['save']) return false; 7391d5856cfSAndreas Gohr 7401d5856cfSAndreas Gohr if (empty($_POST['login'])) { 7411d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 7421d5856cfSAndreas Gohr return false; 7431d5856cfSAndreas Gohr } else { 74416470b1dSchris $_POST['login'] = preg_replace('/.*:/','',$_POST['login']); 74516470b1dSchris $user = cleanID($_POST['login']); 7461d5856cfSAndreas Gohr } 7471d5856cfSAndreas Gohr 7481d5856cfSAndreas Gohr $userinfo = $auth->getUserData($user); 7491d5856cfSAndreas Gohr if(!$userinfo['mail']) { 7501d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 7511d5856cfSAndreas Gohr return false; 7521d5856cfSAndreas Gohr } 7531d5856cfSAndreas Gohr 7541d5856cfSAndreas Gohr // generate auth token 7551d5856cfSAndreas Gohr $token = md5(auth_cookiesalt().$user); //secret but user based 7561d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 7571d5856cfSAndreas Gohr $url = wl('',array('do'=>'resendpwd','pwauth'=>$token),true,'&'); 7581d5856cfSAndreas Gohr 7591d5856cfSAndreas Gohr io_saveFile($tfile,$user); 7601d5856cfSAndreas Gohr 7611d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 7621d5856cfSAndreas Gohr $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 7631d5856cfSAndreas Gohr $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 7641d5856cfSAndreas Gohr $text = str_replace('@LOGIN@',$user,$text); 7651d5856cfSAndreas Gohr $text = str_replace('@TITLE@',$conf['title'],$text); 7661d5856cfSAndreas Gohr $text = str_replace('@CONFIRM@',$url,$text); 7671d5856cfSAndreas Gohr 7681d5856cfSAndreas Gohr if(mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 7691d5856cfSAndreas Gohr $lang['regpwmail'], 7701d5856cfSAndreas Gohr $text, 7711d5856cfSAndreas Gohr $conf['mailfrom'])){ 7721d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'],1); 7731d5856cfSAndreas Gohr }else{ 7741d5856cfSAndreas Gohr msg($lang['regmailfail'],-1); 7751d5856cfSAndreas Gohr } 7761d5856cfSAndreas Gohr return true; 7771d5856cfSAndreas Gohr } 7781d5856cfSAndreas Gohr 7791d5856cfSAndreas Gohr return false; // never reached 7808b06d178Schris} 7818b06d178Schris 7828b06d178Schris/** 78310a76f6fSfrank * Uses a regular expresion to check if a given mail address is valid 78410a76f6fSfrank * 78510a76f6fSfrank * May not be completly RFC conform! 78610a76f6fSfrank * 78710a76f6fSfrank * @link http://www.webmasterworld.com/forum88/135.htm 78810a76f6fSfrank * 78910a76f6fSfrank * @param string $email the address to check 79010a76f6fSfrank * @return bool true if address is valid 79110a76f6fSfrank */ 79210a76f6fSfrankfunction isvalidemail($email){ 79310a76f6fSfrank return eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$", $email); 79410a76f6fSfrank} 79510a76f6fSfrank 796b0855b11Sandi/** 797b0855b11Sandi * Encrypts a password using the given method and salt 798b0855b11Sandi * 799b0855b11Sandi * If the selected method needs a salt and none was given, a random one 800b0855b11Sandi * is chosen. 801b0855b11Sandi * 802b0855b11Sandi * The following methods are understood: 803b0855b11Sandi * 804b0855b11Sandi * smd5 - Salted MD5 hashing 805b0855b11Sandi * md5 - Simple MD5 hashing 806b0855b11Sandi * sha1 - SHA1 hashing 807b0855b11Sandi * ssha - Salted SHA1 hashing 808d7be6245Sandi * crypt - Unix crypt 809d7be6245Sandi * mysql - MySQL password (old method) 810d7be6245Sandi * my411 - MySQL 4.1.1 password 811b0855b11Sandi * 812b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 813b0855b11Sandi * @return string The crypted password 814b0855b11Sandi */ 815b0855b11Sandifunction auth_cryptPassword($clear,$method='',$salt=''){ 816b0855b11Sandi global $conf; 817b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 81810a76f6fSfrank 819b0855b11Sandi //prepare a salt 820b0855b11Sandi if(empty($salt)) $salt = md5(uniqid(rand(), true)); 821b0855b11Sandi 822b0855b11Sandi switch(strtolower($method)){ 823b0855b11Sandi case 'smd5': 824b0855b11Sandi return crypt($clear,'$1$'.substr($salt,0,8).'$'); 825b0855b11Sandi case 'md5': 826b0855b11Sandi return md5($clear); 827b0855b11Sandi case 'sha1': 828b0855b11Sandi return sha1($clear); 829b0855b11Sandi case 'ssha': 830b0855b11Sandi $salt=substr($salt,0,4); 831d6e54e02Smatthiasgrimm return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt); 832b0855b11Sandi case 'crypt': 833b0855b11Sandi return crypt($clear,substr($salt,0,2)); 834d7be6245Sandi case 'mysql': 835d7be6245Sandi //from http://www.php.net/mysql comment by <soren at byu dot edu> 836d7be6245Sandi $nr=0x50305735; 837d7be6245Sandi $nr2=0x12345671; 838d7be6245Sandi $add=7; 839d7be6245Sandi $charArr = preg_split("//", $clear); 840d7be6245Sandi foreach ($charArr as $char) { 841d7be6245Sandi if (($char == '') || ($char == ' ') || ($char == '\t')) continue; 842d7be6245Sandi $charVal = ord($char); 843d7be6245Sandi $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8); 844d7be6245Sandi $nr2 += ($nr2 << 8) ^ $nr; 845d7be6245Sandi $add += $charVal; 846d7be6245Sandi } 847d7be6245Sandi return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff)); 848d7be6245Sandi case 'my411': 849d7be6245Sandi return '*'.sha1(pack("H*", sha1($clear))); 850b0855b11Sandi default: 851b0855b11Sandi msg("Unsupported crypt method $method",-1); 852b0855b11Sandi } 853b0855b11Sandi} 854b0855b11Sandi 855b0855b11Sandi/** 856b0855b11Sandi * Verifies a cleartext password against a crypted hash 857b0855b11Sandi * 858b0855b11Sandi * The method and salt used for the crypted hash is determined automatically 859b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs 860b0855b11Sandi * match true is is returned else false 861b0855b11Sandi * 862b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 863b0855b11Sandi * @return bool 864b0855b11Sandi */ 865b0855b11Sandifunction auth_verifyPassword($clear,$crypt){ 866b0855b11Sandi $method=''; 867b0855b11Sandi $salt=''; 868b0855b11Sandi 869b0855b11Sandi //determine the used method and salt 870d7be6245Sandi $len = strlen($crypt); 871b0855b11Sandi if(substr($crypt,0,3) == '$1$'){ 872b0855b11Sandi $method = 'smd5'; 873b0855b11Sandi $salt = substr($crypt,3,8); 874b0855b11Sandi }elseif(substr($crypt,0,6) == '{SSHA}'){ 875b0855b11Sandi $method = 'ssha'; 876b0855b11Sandi $salt = substr(base64_decode(substr($crypt, 6)),20); 877d7be6245Sandi }elseif($len == 32){ 878b0855b11Sandi $method = 'md5'; 879d7be6245Sandi }elseif($len == 40){ 880b0855b11Sandi $method = 'sha1'; 881d7be6245Sandi }elseif($len == 16){ 882d7be6245Sandi $method = 'mysql'; 883d7be6245Sandi }elseif($len == 41 && $crypt[0] == '*'){ 884d7be6245Sandi $method = 'my411'; 885b0855b11Sandi }else{ 886b0855b11Sandi $method = 'crypt'; 887b0855b11Sandi $salt = substr($crypt,0,2); 888b0855b11Sandi } 889b0855b11Sandi 890b0855b11Sandi //crypt and compare 891b0855b11Sandi if(auth_cryptPassword($clear,$method,$salt) === $crypt){ 892b0855b11Sandi return true; 893b0855b11Sandi } 894b0855b11Sandi return false; 895b0855b11Sandi} 896340756e4Sandi 897340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 898