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 12*fa8adffeSAndreas Gohr if(!defined('DOKU_INC')) die('meh.'); 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'] = ''; 61b2c0d874SGina Haeussge $_REQUEST['http_credentials'] = false; 6217f89d7eSMichael Klier if (!$conf['rememberme']) $_REQUEST['r'] = false; 63bbbd6568SAndreas Gohr 641e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 654a26ad85Schris if(empty($_REQUEST['u']) && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])){ 661e8c9c90SAndreas Gohr $_REQUEST['u'] = $_SERVER['PHP_AUTH_USER']; 671e8c9c90SAndreas Gohr $_REQUEST['p'] = $_SERVER['PHP_AUTH_PW']; 68b2c0d874SGina Haeussge $_REQUEST['http_credentials'] = true; 691e8c9c90SAndreas Gohr } 701e8c9c90SAndreas Gohr 71f13fa892SAndreas Gohr if($_REQUEST['authtok']){ 72f13fa892SAndreas Gohr // when an authentication token is given, trust the session 73f13fa892SAndreas Gohr auth_validateToken($_REQUEST['authtok']); 74f13fa892SAndreas Gohr }elseif(!is_null($auth) && $auth->canDo('external')){ 75f13fa892SAndreas Gohr // external trust mechanism in place 76f5cb575dSAndreas Gohr $auth->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 77f5cb575dSAndreas Gohr }else{ 78b2c0d874SGina Haeussge auth_login($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r'],$_REQUEST['http_credentials']); 79f5cb575dSAndreas Gohr } 801ec50243SAndreas Gohr } 81f5cb575dSAndreas Gohr 8215fae107Sandi //load ACL into a global array 8388e6a4f2SAndreas Gohr global $AUTH_ACL; 84e7cb32dcSAndreas Gohr if(is_readable(DOKU_CONF.'acl.auth.php')){ 85e7cb32dcSAndreas Gohr $AUTH_ACL = file(DOKU_CONF.'acl.auth.php'); 86f8cc3354SGuy Brand if(isset($_SERVER['REMOTE_USER'])){ 87a8fe108bSGuy Brand $AUTH_ACL = str_replace('@USER@',$_SERVER['REMOTE_USER'],$AUTH_ACL); 88a8fe108bSGuy Brand } 8911799630Sandi }else{ 9011799630Sandi $AUTH_ACL = array(); 9111799630Sandi } 92f3f0262cSandi } 93f3f0262cSandi 94f3f0262cSandi/** 95f3f0262cSandi * This tries to login the user based on the sent auth credentials 96f3f0262cSandi * 97f3f0262cSandi * The authentication works like this: if a username was given 9815fae107Sandi * a new login is assumed and user/password are checked. If they 9915fae107Sandi * are correct the password is encrypted with blowfish and stored 10015fae107Sandi * together with the username in a cookie - the same info is stored 10115fae107Sandi * in the session, too. Additonally a browserID is stored in the 10215fae107Sandi * session. 10315fae107Sandi * 10415fae107Sandi * If no username was given the cookie is checked: if the username, 10515fae107Sandi * crypted password and browserID match between session and cookie 10615fae107Sandi * no further testing is done and the user is accepted 10715fae107Sandi * 10815fae107Sandi * If a cookie was found but no session info was availabe the 109136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 11015fae107Sandi * together with username rechecked by calling this function again. 111f3f0262cSandi * 112f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 113f3f0262cSandi * are set. 11415fae107Sandi * 11515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 11615fae107Sandi * 11715fae107Sandi * @param string $user Username 11815fae107Sandi * @param string $pass Cleartext Password 11915fae107Sandi * @param bool $sticky Cookie should not expire 120f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 12115fae107Sandi * @return bool true on successful auth 122f3f0262cSandi*/ 123f112c2faSAndreas Gohrfunction auth_login($user,$pass,$sticky=false,$silent=false){ 124f3f0262cSandi global $USERINFO; 125f3f0262cSandi global $conf; 126f3f0262cSandi global $lang; 127cd52f92dSchris global $auth; 128132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 129f3f0262cSandi 130bbbd6568SAndreas Gohr if(!empty($user)){ 131132bdbfeSandi //usual login 132cd52f92dSchris if ($auth->checkPass($user,$pass)){ 133132bdbfeSandi // make logininfo globally available 134f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 135a0b5b007SChris Smith auth_setCookie($user,PMA_blowfish_encrypt($pass,auth_cookiesalt()),$sticky); 136132bdbfeSandi return true; 137f3f0262cSandi }else{ 138f3f0262cSandi //invalid credentials - log off 139f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'],-1); 140f3f0262cSandi auth_logoff(); 141132bdbfeSandi return false; 142f3f0262cSandi } 143f3f0262cSandi }else{ 144132bdbfeSandi // read cookie information 145e65afed4SSameer D. Sahasrabuddhe $cookie = base64_decode($_COOKIE[DOKU_COOKIE]); 146132bdbfeSandi list($user,$sticky,$pass) = split('\|',$cookie,3); 147132bdbfeSandi // get session info 148e71ce681SAndreas Gohr $session = $_SESSION[DOKU_COOKIE]['auth']; 149132bdbfeSandi if($user && $pass){ 150132bdbfeSandi // we got a cookie - see if we can trust it 151132bdbfeSandi if(isset($session) && 1527172dbc0SAndreas Gohr $auth->useSessionCache($user) && 1534c989037SChris Smith ($session['time'] >= time()-$conf['auth_security_timeout']) && 154132bdbfeSandi ($session['user'] == $user) && 155132bdbfeSandi ($session['pass'] == $pass) && //still crypted 156132bdbfeSandi ($session['buid'] == auth_browseruid()) ){ 157132bdbfeSandi // he has session, cookie and browser right - let him in 158132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 159132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 160132bdbfeSandi return true; 161132bdbfeSandi } 162f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 163132bdbfeSandi $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt()); 164f112c2faSAndreas Gohr return auth_login($user,$pass,$sticky,true); 165132bdbfeSandi } 166132bdbfeSandi } 167f3f0262cSandi //just to be sure 168f3f0262cSandi auth_logoff(); 169132bdbfeSandi return false; 170f3f0262cSandi} 171132bdbfeSandi 172132bdbfeSandi/** 173f13fa892SAndreas Gohr * Checks if a given authentication token was stored in the session 174f13fa892SAndreas Gohr * 175f13fa892SAndreas Gohr * Will setup authentication data using data from the session if the 176f13fa892SAndreas Gohr * token is correct. Will exit with a 401 Status if not. 177f13fa892SAndreas Gohr * 178f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 179f13fa892SAndreas Gohr * @param string $token The authentication token 180f13fa892SAndreas Gohr * @return boolean true (or will exit on failure) 181f13fa892SAndreas Gohr */ 182f13fa892SAndreas Gohrfunction auth_validateToken($token){ 183f13fa892SAndreas Gohr if(!$token || $token != $_SESSION[DOKU_COOKIE]['auth']['token']){ 184f13fa892SAndreas Gohr // bad token 185f13fa892SAndreas Gohr header("HTTP/1.0 401 Unauthorized"); 186f13fa892SAndreas Gohr print 'Invalid auth token - maybe the session timed out'; 187f13fa892SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['token']); // no second chance 188f13fa892SAndreas Gohr exit; 189f13fa892SAndreas Gohr } 190f13fa892SAndreas Gohr // still here? trust the session data 191f13fa892SAndreas Gohr global $USERINFO; 192f13fa892SAndreas Gohr $_SERVER['REMOTE_USER'] = $_SESSION[DOKU_COOKIE]['auth']['user']; 193f13fa892SAndreas Gohr $USERINFO = $_SESSION[DOKU_COOKIE]['auth']['info']; 194f13fa892SAndreas Gohr return true; 195f13fa892SAndreas Gohr} 196f13fa892SAndreas Gohr 197f13fa892SAndreas Gohr/** 198f13fa892SAndreas Gohr * Create an auth token and store it in the session 199f13fa892SAndreas Gohr * 200f13fa892SAndreas Gohr * NOTE: this is completely unrelated to the getSecurityToken() function 201f13fa892SAndreas Gohr * 202f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 203f13fa892SAndreas Gohr * @return string The auth token 204f13fa892SAndreas Gohr */ 205f13fa892SAndreas Gohrfunction auth_createToken(){ 206f13fa892SAndreas Gohr $token = md5(mt_rand()); 20709c2d803SAndreas Gohr @session_start(); // reopen the session if needed 208f13fa892SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['token'] = $token; 20909c2d803SAndreas Gohr session_write_close(); 210f13fa892SAndreas Gohr return $token; 211f13fa892SAndreas Gohr} 212f13fa892SAndreas Gohr 213f13fa892SAndreas Gohr/** 214136ce040Sandi * Builds a pseudo UID from browser and IP data 215132bdbfeSandi * 216132bdbfeSandi * This is neither unique nor unfakable - still it adds some 217136ce040Sandi * security. Using the first part of the IP makes sure 218136ce040Sandi * proxy farms like AOLs are stil okay. 21915fae107Sandi * 22015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 22115fae107Sandi * 22215fae107Sandi * @return string a MD5 sum of various browser headers 223132bdbfeSandi */ 224132bdbfeSandifunction auth_browseruid(){ 225132bdbfeSandi $uid = ''; 226132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 227132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 228132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 229132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 230136ce040Sandi $uid .= substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],'.')); 231132bdbfeSandi return md5($uid); 232132bdbfeSandi} 233132bdbfeSandi 234132bdbfeSandi/** 235132bdbfeSandi * Creates a random key to encrypt the password in cookies 23615fae107Sandi * 23715fae107Sandi * This function tries to read the password for encrypting 23898407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 23915fae107Sandi * if no such file is found a random key is created and 24015fae107Sandi * and stored in this file. 24115fae107Sandi * 24215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 24315fae107Sandi * 24415fae107Sandi * @return string 245132bdbfeSandi */ 246132bdbfeSandifunction auth_cookiesalt(){ 247132bdbfeSandi global $conf; 24898407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 249132bdbfeSandi $salt = io_readFile($file); 250132bdbfeSandi if(empty($salt)){ 251132bdbfeSandi $salt = uniqid(rand(),true); 252132bdbfeSandi io_saveFile($file,$salt); 253132bdbfeSandi } 254132bdbfeSandi return $salt; 255f3f0262cSandi} 256f3f0262cSandi 257f3f0262cSandi/** 258f3f0262cSandi * This clears all authenticationdata and thus log the user 259f3f0262cSandi * off 26015fae107Sandi * 26115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 262f3f0262cSandi */ 263f3f0262cSandifunction auth_logoff(){ 264f3f0262cSandi global $conf; 265f3f0262cSandi global $USERINFO; 2668b06d178Schris global $INFO, $ID; 2675298a619SAndreas Gohr global $auth; 26837065e65Sandi 269e9621d07SAndreas Gohr // reopen session 270e9621d07SAndreas Gohr @session_start(); 271e9621d07SAndreas Gohr 272e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 273e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 274e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 275e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 276e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 277e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 278e16eccb7SGuy Brand if(isset($_SESSION[DOKU_COOKIE]['bc'])) 279e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 28037065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 281f3f0262cSandi unset($_SERVER['REMOTE_USER']); 282132bdbfeSandi $USERINFO=null; //FIXME 283f5c6743cSAndreas Gohr 284f5c6743cSAndreas Gohr if (version_compare(PHP_VERSION, '5.2.0', '>')) { 285f5c6743cSAndreas Gohr setcookie(DOKU_COOKIE,'',time()-600000,DOKU_REL,($conf['securecookie'] && is_ssl()),true); 286f5c6743cSAndreas Gohr }else{ 287f5c6743cSAndreas Gohr setcookie(DOKU_COOKIE,'',time()-600000,DOKU_REL,($conf['securecookie'] && is_ssl())); 288f5c6743cSAndreas Gohr } 2895298a619SAndreas Gohr 2905298a619SAndreas Gohr if($auth && $auth->canDo('logoff')){ 2915298a619SAndreas Gohr $auth->logOff(); 2925298a619SAndreas Gohr } 293e9621d07SAndreas Gohr 294e9621d07SAndreas Gohr // close session again 295e9621d07SAndreas Gohr session_write_close(); 296f3f0262cSandi} 297f3f0262cSandi 298f3f0262cSandi/** 299f8cc712eSAndreas Gohr * Check if a user is a manager 300f8cc712eSAndreas Gohr * 301f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 302f8cc712eSAndreas Gohr * user. 303f8cc712eSAndreas Gohr * 304f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 305f8cc712eSAndreas Gohr * 306f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 307f8cc712eSAndreas Gohr * @see auth_isadmin 308f8cc712eSAndreas Gohr * @param string user - Username 309f8cc712eSAndreas Gohr * @param array groups - List of groups the user is in 310f8cc712eSAndreas Gohr * @param bool adminonly - when true checks if user is admin 311f8cc712eSAndreas Gohr */ 312f8cc712eSAndreas Gohrfunction auth_ismanager($user=null,$groups=null,$adminonly=false){ 313f8cc712eSAndreas Gohr global $conf; 314f8cc712eSAndreas Gohr global $USERINFO; 315f8cc712eSAndreas Gohr 316f8cc712eSAndreas Gohr if(!$conf['useacl']) return false; 317f8cc712eSAndreas Gohr if(is_null($user)) $user = $_SERVER['REMOTE_USER']; 31890583e9fSAndreas Gohr if(is_null($groups)) $groups = (array) $USERINFO['grps']; 319f8cc712eSAndreas Gohr $user = auth_nameencode($user); 320f8cc712eSAndreas Gohr 321f8cc712eSAndreas Gohr // check username against superuser and manager 3227651d633SGuy Brand $superusers = explode(',', $conf['superuser']); 3237651d633SGuy Brand $superusers = array_unique($superusers); 3247651d633SGuy Brand $superusers = array_map('trim', $superusers); 3257651d633SGuy Brand // prepare an array containing only true values for array_map call 3267651d633SGuy Brand $alltrue = array_fill(0, count($superusers), true); 3277651d633SGuy Brand $superusers = array_map('auth_nameencode', $superusers, $alltrue); 3287651d633SGuy Brand if(in_array($user, $superusers)) return true; 3297651d633SGuy Brand 330f8cc712eSAndreas Gohr if(!$adminonly){ 3317651d633SGuy Brand $managers = explode(',', $conf['manager']); 3327651d633SGuy Brand $managers = array_unique($managers); 3337651d633SGuy Brand $managers = array_map('trim', $managers); 3347651d633SGuy Brand // prepare an array containing only true values for array_map call 3357651d633SGuy Brand $alltrue = array_fill(0, count($managers), true); 3367651d633SGuy Brand $managers = array_map('auth_nameencode', $managers, $alltrue); 3377651d633SGuy Brand if(in_array($user, $managers)) return true; 338f8cc712eSAndreas Gohr } 339f8cc712eSAndreas Gohr 34000ce12daSChris Smith // check user's groups against superuser and manager 34100ce12daSChris Smith if (!empty($groups)) { 34200ce12daSChris Smith 343f8cc712eSAndreas Gohr //prepend groups with @ and nameencode 344f8cc712eSAndreas Gohr $cnt = count($groups); 345f8cc712eSAndreas Gohr for($i=0; $i<$cnt; $i++){ 346f8cc712eSAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 347f8cc712eSAndreas Gohr } 348f8cc712eSAndreas Gohr 349f8cc712eSAndreas Gohr // check groups against superuser and manager 3507651d633SGuy Brand foreach($superusers as $supu) 3517651d633SGuy Brand if(in_array($supu, $groups)) return true; 352f8cc712eSAndreas Gohr if(!$adminonly){ 3537651d633SGuy Brand foreach($managers as $mana) 3547651d633SGuy Brand if(in_array($mana, $groups)) return true; 355f8cc712eSAndreas Gohr } 35600ce12daSChris Smith } 35700ce12daSChris Smith 358f8cc712eSAndreas Gohr return false; 359f8cc712eSAndreas Gohr} 360f8cc712eSAndreas Gohr 361f8cc712eSAndreas Gohr/** 362f8cc712eSAndreas Gohr * Check if a user is admin 363f8cc712eSAndreas Gohr * 364f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 365f8cc712eSAndreas Gohr * 366f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 367f8cc712eSAndreas Gohr * 368f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 369f8cc712eSAndreas Gohr * @see auth_ismanager 370f8cc712eSAndreas Gohr */ 371f8cc712eSAndreas Gohrfunction auth_isadmin($user=null,$groups=null){ 372f8cc712eSAndreas Gohr return auth_ismanager($user,$groups,true); 373f8cc712eSAndreas Gohr} 374f8cc712eSAndreas Gohr 375f8cc712eSAndreas Gohr/** 37615fae107Sandi * Convinience function for auth_aclcheck() 37715fae107Sandi * 37815fae107Sandi * This checks the permissions for the current user 37915fae107Sandi * 38015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 38115fae107Sandi * 38215fae107Sandi * @param string $id page ID 38315fae107Sandi * @return int permission level 384f3f0262cSandi */ 385f3f0262cSandifunction auth_quickaclcheck($id){ 386f3f0262cSandi global $conf; 387f3f0262cSandi global $USERINFO; 388f3f0262cSandi # if no ACL is used always return upload rights 389f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 390f3f0262cSandi return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']); 391f3f0262cSandi} 392f3f0262cSandi 393f3f0262cSandi/** 394f3f0262cSandi * Returns the maximum rights a user has for 395f3f0262cSandi * the given ID or its namespace 39615fae107Sandi * 39715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 39815fae107Sandi * 39915fae107Sandi * @param string $id page ID 40015fae107Sandi * @param string $user Username 40115fae107Sandi * @param array $groups Array of groups the user is in 40215fae107Sandi * @return int permission level 403f3f0262cSandi */ 404f3f0262cSandifunction auth_aclcheck($id,$user,$groups){ 405f3f0262cSandi global $conf; 406f3f0262cSandi global $AUTH_ACL; 407f3f0262cSandi 40885d03f68SAndreas Gohr // if no ACL is used always return upload rights 409f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 410f3f0262cSandi 411074cf26bSandi //make sure groups is an array 412074cf26bSandi if(!is_array($groups)) $groups = array(); 413074cf26bSandi 41485d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 41585d03f68SAndreas Gohr if(auth_isadmin($user,$groups)) { return AUTH_ADMIN; } 41685d03f68SAndreas Gohr 41785d03f68SAndreas Gohr $user = auth_nameencode($user); 41885d03f68SAndreas Gohr 4196c2bb100SAndreas Gohr //prepend groups with @ and nameencode 4202cd2db38Sandi $cnt = count($groups); 4212cd2db38Sandi for($i=0; $i<$cnt; $i++){ 4226c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 42310a76f6fSfrank } 42410a76f6fSfrank 425f3f0262cSandi $ns = getNS($id); 426f3f0262cSandi $perm = -1; 427f3f0262cSandi 42834aeb4afSAndreas Gohr if($user || count($groups)){ 429f3f0262cSandi //add ALL group 430f3f0262cSandi $groups[] = '@ALL'; 431f3f0262cSandi //add User 43234aeb4afSAndreas Gohr if($user) $groups[] = $user; 433f3f0262cSandi //build regexp 434f3f0262cSandi $regexp = join('|',$groups); 435f3f0262cSandi }else{ 436f3f0262cSandi $regexp = '@ALL'; 437f3f0262cSandi } 438f3f0262cSandi 439f3f0262cSandi //check exact match first 44042905504SAndreas Gohr $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/',$AUTH_ACL); 441f3f0262cSandi if(count($matches)){ 442f3f0262cSandi foreach($matches as $match){ 443f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 444f3f0262cSandi $acl = preg_split('/\s+/',$match); 4458ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 446f3f0262cSandi if($acl[2] > $perm){ 447f3f0262cSandi $perm = $acl[2]; 448f3f0262cSandi } 449f3f0262cSandi } 450f3f0262cSandi if($perm > -1){ 451f3f0262cSandi //we had a match - return it 452f3f0262cSandi return $perm; 453f3f0262cSandi } 454f3f0262cSandi } 455f3f0262cSandi 456f3f0262cSandi //still here? do the namespace checks 457f3f0262cSandi if($ns){ 458f3f0262cSandi $path = $ns.':\*'; 459f3f0262cSandi }else{ 460f3f0262cSandi $path = '\*'; //root document 461f3f0262cSandi } 462f3f0262cSandi 463f3f0262cSandi do{ 464f3f0262cSandi $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/',$AUTH_ACL); 465f3f0262cSandi if(count($matches)){ 466f3f0262cSandi foreach($matches as $match){ 467f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 468f3f0262cSandi $acl = preg_split('/\s+/',$match); 4698ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 470f3f0262cSandi if($acl[2] > $perm){ 471f3f0262cSandi $perm = $acl[2]; 472f3f0262cSandi } 473f3f0262cSandi } 474f3f0262cSandi //we had a match - return it 475f3f0262cSandi return $perm; 476f3f0262cSandi } 477f3f0262cSandi 478f3f0262cSandi //get next higher namespace 479f3f0262cSandi $ns = getNS($ns); 480f3f0262cSandi 481f3f0262cSandi if($path != '\*'){ 482f3f0262cSandi $path = $ns.':\*'; 483f3f0262cSandi if($path == ':\*') $path = '\*'; 484f3f0262cSandi }else{ 485f3f0262cSandi //we did this already 486f3f0262cSandi //looks like there is something wrong with the ACL 487f3f0262cSandi //break here 488d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 489d5ce66f6SAndreas Gohr return AUTH_NONE; 490f3f0262cSandi } 491f3f0262cSandi }while(1); //this should never loop endless 49252a5af8dSandi 49352a5af8dSandi //still here? return no permissions 49452a5af8dSandi return AUTH_NONE; 495f3f0262cSandi} 496f3f0262cSandi 497f3f0262cSandi/** 4986c2bb100SAndreas Gohr * Encode ASCII special chars 4996c2bb100SAndreas Gohr * 5006c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 5016c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 5026c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 5036c2bb100SAndreas Gohr * urlencoding!). 5046c2bb100SAndreas Gohr * 5056c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 5066c2bb100SAndreas Gohr * 5076c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 5086c2bb100SAndreas Gohr * @see rawurldecode() 5096c2bb100SAndreas Gohr */ 510e838fc2eSAndreas Gohrfunction auth_nameencode($name,$skip_group=false){ 511a424cd8eSchris global $cache_authname; 512a424cd8eSchris $cache =& $cache_authname; 51331784267SAndreas Gohr $name = (string) $name; 514a424cd8eSchris 515a424cd8eSchris if (!isset($cache[$name][$skip_group])) { 516e838fc2eSAndreas Gohr if($skip_group && $name{0} =='@'){ 517a424cd8eSchris $cache[$name][$skip_group] = '@'.preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 5181a9ae8e5SAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))",substr($name,1)); 519e838fc2eSAndreas Gohr }else{ 520a424cd8eSchris $cache[$name][$skip_group] = preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 5211a9ae8e5SAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))",$name); 522e838fc2eSAndreas Gohr } 5236c2bb100SAndreas Gohr } 5246c2bb100SAndreas Gohr 525a424cd8eSchris return $cache[$name][$skip_group]; 526a424cd8eSchris} 527a424cd8eSchris 5286c2bb100SAndreas Gohr/** 529f3f0262cSandi * Create a pronouncable password 530f3f0262cSandi * 53115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 53215fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 53315fae107Sandi * 53415fae107Sandi * @return string pronouncable password 535f3f0262cSandi */ 536f3f0262cSandifunction auth_pwgen(){ 537f3f0262cSandi $pw = ''; 538f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 539f3f0262cSandi $v = 'aeiou'; //vowels 540f3f0262cSandi $a = $c.$v; //both 541f3f0262cSandi 542f3f0262cSandi //use two syllables... 543f3f0262cSandi for($i=0;$i < 2; $i++){ 544f3f0262cSandi $pw .= $c[rand(0, strlen($c)-1)]; 545f3f0262cSandi $pw .= $v[rand(0, strlen($v)-1)]; 546f3f0262cSandi $pw .= $a[rand(0, strlen($a)-1)]; 547f3f0262cSandi } 548f3f0262cSandi //... and add a nice number 549f3f0262cSandi $pw .= rand(10,99); 550f3f0262cSandi 551f3f0262cSandi return $pw; 552f3f0262cSandi} 553f3f0262cSandi 554f3f0262cSandi/** 555f3f0262cSandi * Sends a password to the given user 556f3f0262cSandi * 55715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 55815fae107Sandi * 55915fae107Sandi * @return bool true on success 560f3f0262cSandi */ 561f3f0262cSandifunction auth_sendPassword($user,$password){ 562f3f0262cSandi global $conf; 563f3f0262cSandi global $lang; 564cd52f92dSchris global $auth; 565cd52f92dSchris 566f3f0262cSandi $hdrs = ''; 567cd52f92dSchris $userinfo = $auth->getUserData($user); 568f3f0262cSandi 56987ddda95Sandi if(!$userinfo['mail']) return false; 570f3f0262cSandi 571f3f0262cSandi $text = rawLocale('password'); 572ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 57387ddda95Sandi $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 574f3f0262cSandi $text = str_replace('@LOGIN@',$user,$text); 575f3f0262cSandi $text = str_replace('@PASSWORD@',$password,$text); 576f3f0262cSandi $text = str_replace('@TITLE@',$conf['title'],$text); 577f3f0262cSandi 57844f669e9Sandi return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 57944f669e9Sandi $lang['regpwmail'], 58044f669e9Sandi $text, 58144f669e9Sandi $conf['mailfrom']); 582f3f0262cSandi} 583f3f0262cSandi 584f3f0262cSandi/** 58515fae107Sandi * Register a new user 586f3f0262cSandi * 58715fae107Sandi * This registers a new user - Data is read directly from $_POST 58815fae107Sandi * 58915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 59015fae107Sandi * 59115fae107Sandi * @return bool true on success, false on any error 592f3f0262cSandi */ 593f3f0262cSandifunction register(){ 594f3f0262cSandi global $lang; 595eb5d07e4Sjan global $conf; 596cd52f92dSchris global $auth; 597f3f0262cSandi 598f3f0262cSandi if(!$_POST['save']) return false; 59982fd59b6SAndreas Gohr if(!$auth->canDo('addUser')) return false; 600640145a5Sandi 601f3f0262cSandi //clean username 602f3f0262cSandi $_POST['login'] = preg_replace('/.*:/','',$_POST['login']); 603f3f0262cSandi $_POST['login'] = cleanID($_POST['login']); 604f3f0262cSandi //clean fullname and email 60554f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname'])); 60654f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email'])); 607f3f0262cSandi 608f3f0262cSandi if( empty($_POST['login']) || 609f3f0262cSandi empty($_POST['fullname']) || 610f3f0262cSandi empty($_POST['email']) ){ 611f3f0262cSandi msg($lang['regmissing'],-1); 612f3f0262cSandi return false; 613f3f0262cSandi } 614f3f0262cSandi 615cab2716aSmatthias.grimm if ($conf['autopasswd']) { 616cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 617cab2716aSmatthias.grimm } elseif (empty($_POST['pass']) || 618cab2716aSmatthias.grimm empty($_POST['passchk'])) { 619bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 620cab2716aSmatthias.grimm return false; 621cab2716aSmatthias.grimm } elseif ($_POST['pass'] != $_POST['passchk']) { 622bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 623cab2716aSmatthias.grimm return false; 624cab2716aSmatthias.grimm } else { 625cab2716aSmatthias.grimm $pass = $_POST['pass']; // accept checked and valid password 626cab2716aSmatthias.grimm } 627cab2716aSmatthias.grimm 628f3f0262cSandi //check mail 62944f669e9Sandi if(!mail_isvalid($_POST['email'])){ 630f3f0262cSandi msg($lang['regbadmail'],-1); 631f3f0262cSandi return false; 632f3f0262cSandi } 633f3f0262cSandi 634f3f0262cSandi //okay try to create the user 6357d3c8d42SGabriel Birke if(!$auth->triggerUserMod('create', array($_POST['login'],$pass,$_POST['fullname'],$_POST['email']))){ 636f3f0262cSandi msg($lang['reguexists'],-1); 637f3f0262cSandi return false; 638f3f0262cSandi } 639f3f0262cSandi 64002a498e7Schris // create substitutions for use in notification email 64102a498e7Schris $substitutions = array( 64202a498e7Schris 'NEWUSER' => $_POST['login'], 64302a498e7Schris 'NEWNAME' => $_POST['fullname'], 64402a498e7Schris 'NEWEMAIL' => $_POST['email'], 64502a498e7Schris ); 64602a498e7Schris 647cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 648cab2716aSmatthias.grimm msg($lang['regsuccess2'],1); 64902a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 650cab2716aSmatthias.grimm return true; 651cab2716aSmatthias.grimm } 652cab2716aSmatthias.grimm 653cab2716aSmatthias.grimm // autogenerated password? then send him the password 654f3f0262cSandi if (auth_sendPassword($_POST['login'],$pass)){ 655f3f0262cSandi msg($lang['regsuccess'],1); 65602a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 657f3f0262cSandi return true; 658f3f0262cSandi }else{ 659f3f0262cSandi msg($lang['regmailfail'],-1); 660f3f0262cSandi return false; 661f3f0262cSandi } 662f3f0262cSandi} 663f3f0262cSandi 66410a76f6fSfrank/** 6658b06d178Schris * Update user profile 6668b06d178Schris * 6678b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 6688b06d178Schris */ 6698b06d178Schrisfunction updateprofile() { 6708b06d178Schris global $conf; 6718b06d178Schris global $INFO; 6728b06d178Schris global $lang; 673cd52f92dSchris global $auth; 6748b06d178Schris 675bb4866bdSchris if(empty($_POST['save'])) return false; 6761b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 6778b06d178Schris 67882fd59b6SAndreas Gohr // should not be able to get here without Profile being possible... 67982fd59b6SAndreas Gohr if(!$auth->canDo('Profile')) { 6808b06d178Schris msg($lang['profna'],-1); 6818b06d178Schris return false; 6828b06d178Schris } 6838b06d178Schris 6848b06d178Schris if ($_POST['newpass'] != $_POST['passchk']) { 6858b06d178Schris msg($lang['regbadpass'], -1); // complain about misspelled passwords 6868b06d178Schris return false; 6878b06d178Schris } 6888b06d178Schris 6898b06d178Schris //clean fullname and email 69054f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname'])); 69154f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email'])); 6928b06d178Schris 6938b06d178Schris if (empty($_POST['fullname']) || empty($_POST['email'])) { 6948b06d178Schris msg($lang['profnoempty'],-1); 6958b06d178Schris return false; 6968b06d178Schris } 6978b06d178Schris 6988b06d178Schris if (!mail_isvalid($_POST['email'])){ 6998b06d178Schris msg($lang['regbadmail'],-1); 7008b06d178Schris return false; 7018b06d178Schris } 7028b06d178Schris 7034c21b7eeSAndreas Gohr if ($_POST['fullname'] != $INFO['userinfo']['name'] && $auth->canDo('modName')) $changes['name'] = $_POST['fullname']; 7044c21b7eeSAndreas Gohr if ($_POST['email'] != $INFO['userinfo']['mail'] && $auth->canDo('modMail')) $changes['mail'] = $_POST['email']; 705cf626a62SAndreas Gohr if (!empty($_POST['newpass']) && $auth->canDo('modPass')) $changes['pass'] = $_POST['newpass']; 7064c21b7eeSAndreas Gohr 7078b06d178Schris 7088b06d178Schris if (!count($changes)) { 7098b06d178Schris msg($lang['profnochange'], -1); 7108b06d178Schris return false; 7118b06d178Schris } 7128b06d178Schris 7138b06d178Schris if ($conf['profileconfirm']) { 714df466c7aSAndreas Gohr if (!$auth->checkPass($_SERVER['REMOTE_USER'], $_POST['oldpass'])) { 7158b06d178Schris msg($lang['badlogin'],-1); 7168b06d178Schris return false; 7178b06d178Schris } 7188b06d178Schris } 7198b06d178Schris 720a0b5b007SChris Smith if ($result = $auth->triggerUserMod('modify', array($_SERVER['REMOTE_USER'], $changes))) { 721a0b5b007SChris Smith // update cookie and session with the changed data 722a0b5b007SChris Smith $cookie = base64_decode($_COOKIE[DOKU_COOKIE]); 723a0b5b007SChris Smith list($user,$sticky,$pass) = split('\|',$cookie,3); 724a0b5b007SChris Smith if ($changes['pass']) $pass = PMA_blowfish_encrypt($changes['pass'],auth_cookiesalt()); 725a0b5b007SChris Smith 726a0b5b007SChris Smith auth_setCookie($_SERVER['REMOTE_USER'],$pass,(bool)$sticky); 72725b2a98cSMichael Klier return true; 728a0b5b007SChris Smith } 7298b06d178Schris} 7308b06d178Schris 7318b06d178Schris/** 7328b06d178Schris * Send a new password 7338b06d178Schris * 7341d5856cfSAndreas Gohr * This function handles both phases of the password reset: 7351d5856cfSAndreas Gohr * 7361d5856cfSAndreas Gohr * - handling the first request of password reset 7371d5856cfSAndreas Gohr * - validating the password reset auth token 7381d5856cfSAndreas Gohr * 7398b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 7408b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 7411d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7428b06d178Schris * 7438b06d178Schris * @return bool true on success, false on any error 7448b06d178Schris*/ 7458b06d178Schrisfunction act_resendpwd(){ 7468b06d178Schris global $lang; 7478b06d178Schris global $conf; 748cd52f92dSchris global $auth; 7498b06d178Schris 750409d7af7SAndreas Gohr if(!actionOK('resendpwd')) return false; 7518b06d178Schris 75282fd59b6SAndreas Gohr // should not be able to get here without modPass being possible... 75382fd59b6SAndreas Gohr if(!$auth->canDo('modPass')) { 7548b06d178Schris msg($lang['resendna'],-1); 7558b06d178Schris return false; 7568b06d178Schris } 7578b06d178Schris 7581d5856cfSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/','',$_REQUEST['pwauth']); 7598b06d178Schris 7601d5856cfSAndreas Gohr if($token){ 7611d5856cfSAndreas Gohr // we're in token phase 7621d5856cfSAndreas Gohr 7631d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 7641d5856cfSAndreas Gohr if(!@file_exists($tfile)){ 7651d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'],-1); 7661d5856cfSAndreas Gohr return false; 7671d5856cfSAndreas Gohr } 7681d5856cfSAndreas Gohr $user = io_readfile($tfile); 7691d5856cfSAndreas Gohr @unlink($tfile); 770cd52f92dSchris $userinfo = $auth->getUserData($user); 7718b06d178Schris if(!$userinfo['mail']) { 7728b06d178Schris msg($lang['resendpwdnouser'], -1); 7738b06d178Schris return false; 7748b06d178Schris } 7758b06d178Schris 7768b06d178Schris $pass = auth_pwgen(); 7777d3c8d42SGabriel Birke if (!$auth->triggerUserMod('modify', array($user,array('pass' => $pass)))) { 7788b06d178Schris msg('error modifying user data',-1); 7798b06d178Schris return false; 7808b06d178Schris } 7818b06d178Schris 7828b06d178Schris if (auth_sendPassword($user,$pass)) { 7838b06d178Schris msg($lang['resendpwdsuccess'],1); 7848b06d178Schris } else { 7858b06d178Schris msg($lang['regmailfail'],-1); 7868b06d178Schris } 7878b06d178Schris return true; 7881d5856cfSAndreas Gohr 7891d5856cfSAndreas Gohr } else { 7901d5856cfSAndreas Gohr // we're in request phase 7911d5856cfSAndreas Gohr 7921d5856cfSAndreas Gohr if(!$_POST['save']) return false; 7931d5856cfSAndreas Gohr 7941d5856cfSAndreas Gohr if (empty($_POST['login'])) { 7951d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 7961d5856cfSAndreas Gohr return false; 7971d5856cfSAndreas Gohr } else { 79816470b1dSchris $_POST['login'] = preg_replace('/.*:/','',$_POST['login']); 79916470b1dSchris $user = cleanID($_POST['login']); 8001d5856cfSAndreas Gohr } 8011d5856cfSAndreas Gohr 8021d5856cfSAndreas Gohr $userinfo = $auth->getUserData($user); 8031d5856cfSAndreas Gohr if(!$userinfo['mail']) { 8041d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 8051d5856cfSAndreas Gohr return false; 8061d5856cfSAndreas Gohr } 8071d5856cfSAndreas Gohr 8081d5856cfSAndreas Gohr // generate auth token 8091d5856cfSAndreas Gohr $token = md5(auth_cookiesalt().$user); //secret but user based 8101d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 8111d5856cfSAndreas Gohr $url = wl('',array('do'=>'resendpwd','pwauth'=>$token),true,'&'); 8121d5856cfSAndreas Gohr 8131d5856cfSAndreas Gohr io_saveFile($tfile,$user); 8141d5856cfSAndreas Gohr 8151d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 8161d5856cfSAndreas Gohr $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 8171d5856cfSAndreas Gohr $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 8181d5856cfSAndreas Gohr $text = str_replace('@LOGIN@',$user,$text); 8191d5856cfSAndreas Gohr $text = str_replace('@TITLE@',$conf['title'],$text); 8201d5856cfSAndreas Gohr $text = str_replace('@CONFIRM@',$url,$text); 8211d5856cfSAndreas Gohr 8221d5856cfSAndreas Gohr if(mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 8231d5856cfSAndreas Gohr $lang['regpwmail'], 8241d5856cfSAndreas Gohr $text, 8251d5856cfSAndreas Gohr $conf['mailfrom'])){ 8261d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'],1); 8271d5856cfSAndreas Gohr }else{ 8281d5856cfSAndreas Gohr msg($lang['regmailfail'],-1); 8291d5856cfSAndreas Gohr } 8301d5856cfSAndreas Gohr return true; 8311d5856cfSAndreas Gohr } 8321d5856cfSAndreas Gohr 8331d5856cfSAndreas Gohr return false; // never reached 8348b06d178Schris} 8358b06d178Schris 8368b06d178Schris/** 837b0855b11Sandi * Encrypts a password using the given method and salt 838b0855b11Sandi * 839b0855b11Sandi * If the selected method needs a salt and none was given, a random one 840b0855b11Sandi * is chosen. 841b0855b11Sandi * 842b0855b11Sandi * The following methods are understood: 843b0855b11Sandi * 844b0855b11Sandi * smd5 - Salted MD5 hashing 845577c7cdaSAndreas Gohr * apr1 - Apache salted MD5 hashing 846b0855b11Sandi * md5 - Simple MD5 hashing 847b0855b11Sandi * sha1 - SHA1 hashing 848b0855b11Sandi * ssha - Salted SHA1 hashing 849d7be6245Sandi * crypt - Unix crypt 850d7be6245Sandi * mysql - MySQL password (old method) 851d7be6245Sandi * my411 - MySQL 4.1.1 password 852b0855b11Sandi * 853b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 854b0855b11Sandi * @return string The crypted password 855b0855b11Sandi */ 856577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear,$method='',$salt=null){ 857b0855b11Sandi global $conf; 858b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 85910a76f6fSfrank 860b0855b11Sandi //prepare a salt 861577c7cdaSAndreas Gohr if(is_null($salt)) $salt = md5(uniqid(rand(), true)); 862b0855b11Sandi 863b0855b11Sandi switch(strtolower($method)){ 864b0855b11Sandi case 'smd5': 865056cb2ccSChris Smith if(defined('CRYPT_MD5') && CRYPT_MD5) return crypt($clear,'$1$'.substr($salt,0,8).'$'); 866577c7cdaSAndreas Gohr // when crypt can't handle SMD5, falls through to pure PHP implementation 867577c7cdaSAndreas Gohr $magic = '1'; 868577c7cdaSAndreas Gohr case 'apr1': 869577c7cdaSAndreas Gohr //from http://de.php.net/manual/en/function.crypt.php#73619 comment by <mikey_nich at hotmail dot com> 870577c7cdaSAndreas Gohr if(!$magic) $magic = 'apr1'; 871577c7cdaSAndreas Gohr $salt = substr($salt,0,8); 872577c7cdaSAndreas Gohr $len = strlen($clear); 873577c7cdaSAndreas Gohr $text = $clear.'$'.$magic.'$'.$salt; 874577c7cdaSAndreas Gohr $bin = pack("H32", md5($clear.$salt.$clear)); 875577c7cdaSAndreas Gohr for($i = $len; $i > 0; $i -= 16) { $text .= substr($bin, 0, min(16, $i)); } 876577c7cdaSAndreas Gohr for($i = $len; $i > 0; $i >>= 1) { $text .= ($i & 1) ? chr(0) : $clear{0}; } 877577c7cdaSAndreas Gohr $bin = pack("H32", md5($text)); 878577c7cdaSAndreas Gohr for($i = 0; $i < 1000; $i++) { 879577c7cdaSAndreas Gohr $new = ($i & 1) ? $clear : $bin; 880577c7cdaSAndreas Gohr if ($i % 3) $new .= $salt; 881577c7cdaSAndreas Gohr if ($i % 7) $new .= $clear; 882577c7cdaSAndreas Gohr $new .= ($i & 1) ? $bin : $clear; 883577c7cdaSAndreas Gohr $bin = pack("H32", md5($new)); 884577c7cdaSAndreas Gohr } 885577c7cdaSAndreas Gohr $tmp = ''; 886577c7cdaSAndreas Gohr for ($i = 0; $i < 5; $i++) { 887577c7cdaSAndreas Gohr $k = $i + 6; 888577c7cdaSAndreas Gohr $j = $i + 12; 889577c7cdaSAndreas Gohr if ($j == 16) $j = 5; 890577c7cdaSAndreas Gohr $tmp = $bin[$i].$bin[$k].$bin[$j].$tmp; 891577c7cdaSAndreas Gohr } 892577c7cdaSAndreas Gohr $tmp = chr(0).chr(0).$bin[11].$tmp; 893577c7cdaSAndreas Gohr $tmp = strtr(strrev(substr(base64_encode($tmp), 2)), 894577c7cdaSAndreas Gohr "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", 895577c7cdaSAndreas Gohr "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); 896577c7cdaSAndreas Gohr return '$'.$magic.'$'.$salt.'$'.$tmp; 897b0855b11Sandi case 'md5': 898b0855b11Sandi return md5($clear); 899b0855b11Sandi case 'sha1': 900b0855b11Sandi return sha1($clear); 901b0855b11Sandi case 'ssha': 902b0855b11Sandi $salt=substr($salt,0,4); 903d6e54e02Smatthiasgrimm return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt); 904b0855b11Sandi case 'crypt': 905b0855b11Sandi return crypt($clear,substr($salt,0,2)); 906d7be6245Sandi case 'mysql': 907d7be6245Sandi //from http://www.php.net/mysql comment by <soren at byu dot edu> 908d7be6245Sandi $nr=0x50305735; 909d7be6245Sandi $nr2=0x12345671; 910d7be6245Sandi $add=7; 911d7be6245Sandi $charArr = preg_split("//", $clear); 912d7be6245Sandi foreach ($charArr as $char) { 913d7be6245Sandi if (($char == '') || ($char == ' ') || ($char == '\t')) continue; 914d7be6245Sandi $charVal = ord($char); 915d7be6245Sandi $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8); 916d7be6245Sandi $nr2 += ($nr2 << 8) ^ $nr; 917d7be6245Sandi $add += $charVal; 918d7be6245Sandi } 919d7be6245Sandi return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff)); 920d7be6245Sandi case 'my411': 921d7be6245Sandi return '*'.sha1(pack("H*", sha1($clear))); 922b0855b11Sandi default: 923b0855b11Sandi msg("Unsupported crypt method $method",-1); 924b0855b11Sandi } 925b0855b11Sandi} 926b0855b11Sandi 927b0855b11Sandi/** 928b0855b11Sandi * Verifies a cleartext password against a crypted hash 929b0855b11Sandi * 930b0855b11Sandi * The method and salt used for the crypted hash is determined automatically 931b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs 932b0855b11Sandi * match true is is returned else false 933b0855b11Sandi * 934b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 935b0855b11Sandi * @return bool 936b0855b11Sandi */ 937b0855b11Sandifunction auth_verifyPassword($clear,$crypt){ 938b0855b11Sandi $method=''; 939b0855b11Sandi $salt=''; 940b0855b11Sandi 941b0855b11Sandi //determine the used method and salt 942d7be6245Sandi $len = strlen($crypt); 943577c7cdaSAndreas Gohr if(preg_match('/^\$1\$([^\$]{0,8})\$/',$crypt,$m)){ 944b0855b11Sandi $method = 'smd5'; 945577c7cdaSAndreas Gohr $salt = $m[1]; 946577c7cdaSAndreas Gohr }elseif(preg_match('/^\$apr1\$([^\$]{0,8})\$/',$crypt,$m)){ 947577c7cdaSAndreas Gohr $method = 'apr1'; 948577c7cdaSAndreas Gohr $salt = $m[1]; 949b0855b11Sandi }elseif(substr($crypt,0,6) == '{SSHA}'){ 950b0855b11Sandi $method = 'ssha'; 951b0855b11Sandi $salt = substr(base64_decode(substr($crypt, 6)),20); 952d7be6245Sandi }elseif($len == 32){ 953b0855b11Sandi $method = 'md5'; 954d7be6245Sandi }elseif($len == 40){ 955b0855b11Sandi $method = 'sha1'; 956d7be6245Sandi }elseif($len == 16){ 957d7be6245Sandi $method = 'mysql'; 958d7be6245Sandi }elseif($len == 41 && $crypt[0] == '*'){ 959d7be6245Sandi $method = 'my411'; 960b0855b11Sandi }else{ 961b0855b11Sandi $method = 'crypt'; 962b0855b11Sandi $salt = substr($crypt,0,2); 963b0855b11Sandi } 964b0855b11Sandi 965b0855b11Sandi //crypt and compare 966b0855b11Sandi if(auth_cryptPassword($clear,$method,$salt) === $crypt){ 967b0855b11Sandi return true; 968b0855b11Sandi } 969b0855b11Sandi return false; 970b0855b11Sandi} 971340756e4Sandi 972a0b5b007SChris Smith/** 973a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 974a0b5b007SChris Smith * 975a0b5b007SChris Smith * @param string $user username 976a0b5b007SChris Smith * @param string $pass encrypted password 977a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 978a0b5b007SChris Smith */ 979a0b5b007SChris Smithfunction auth_setCookie($user,$pass,$sticky) { 980a0b5b007SChris Smith global $conf; 981a0b5b007SChris Smith global $auth; 98279d00841SOliver Geisen global $USERINFO; 983a0b5b007SChris Smith 984a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 985a0b5b007SChris Smith 986a0b5b007SChris Smith // set cookie 987a0b5b007SChris Smith $cookie = base64_encode("$user|$sticky|$pass"); 988a0b5b007SChris Smith if($sticky) $time = time()+60*60*24*365; //one year 989a0b5b007SChris Smith if (version_compare(PHP_VERSION, '5.2.0', '>')) { 990a0b5b007SChris Smith setcookie(DOKU_COOKIE,$cookie,$time,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true); 991a0b5b007SChris Smith }else{ 992a0b5b007SChris Smith setcookie(DOKU_COOKIE,$cookie,$time,DOKU_REL,'',($conf['securecookie'] && is_ssl())); 993a0b5b007SChris Smith } 994a0b5b007SChris Smith 995a0b5b007SChris Smith // set session 996a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 997a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass; 998a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 999a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1000a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1001a0b5b007SChris Smith} 1002a0b5b007SChris Smith 1003340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 1004