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 12fa8adffeSAndreas Gohrif(!defined('DOKU_INC')) die('meh.'); 13ed7b5f09Sandirequire_once(DOKU_INC.'inc/common.php'); 14ed7b5f09Sandirequire_once(DOKU_INC.'inc/io.php'); 151c73890cSAndreas Gohr 16ebf97c8fSAndreas Gohr// some ACL level defines 17ebf97c8fSAndreas Gohrdefine('AUTH_NONE',0); 18ebf97c8fSAndreas Gohrdefine('AUTH_READ',1); 19ebf97c8fSAndreas Gohrdefine('AUTH_EDIT',2); 20ebf97c8fSAndreas Gohrdefine('AUTH_CREATE',4); 21ebf97c8fSAndreas Gohrdefine('AUTH_UPLOAD',8); 22ebf97c8fSAndreas Gohrdefine('AUTH_DELETE',16); 23ebf97c8fSAndreas Gohrdefine('AUTH_ADMIN',255); 24ebf97c8fSAndreas Gohr 25742c66f8Schrisglobal $conf; 26742c66f8Schris 271c73890cSAndreas Gohrif($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 Gohrif($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 64528ddc7cSAndreas Gohr // streamline HTTP auth credentials (IIS/rewrite -> mod_php) 6506156f3cSAndreas Gohr if(isset($_SERVER['HTTP_AUTHORIZATION'])){ 66528ddc7cSAndreas Gohr list($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']) = 67528ddc7cSAndreas Gohr explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); 68528ddc7cSAndreas Gohr } 69528ddc7cSAndreas Gohr 701e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 714a26ad85Schris if(empty($_REQUEST['u']) && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])){ 721e8c9c90SAndreas Gohr $_REQUEST['u'] = $_SERVER['PHP_AUTH_USER']; 731e8c9c90SAndreas Gohr $_REQUEST['p'] = $_SERVER['PHP_AUTH_PW']; 74b2c0d874SGina Haeussge $_REQUEST['http_credentials'] = true; 751e8c9c90SAndreas Gohr } 761e8c9c90SAndreas Gohr 77191bb90aSAndreas Gohr // apply cleaning 78191bb90aSAndreas Gohr $_REQUEST['u'] = $auth->cleanUser($_REQUEST['u']); 79191bb90aSAndreas Gohr 80c66972f2SAdrian Lang if(isset($_REQUEST['authtok'])){ 81f13fa892SAndreas Gohr // when an authentication token is given, trust the session 82f13fa892SAndreas Gohr auth_validateToken($_REQUEST['authtok']); 83f13fa892SAndreas Gohr }elseif(!is_null($auth) && $auth->canDo('external')){ 84f13fa892SAndreas Gohr // external trust mechanism in place 85f5cb575dSAndreas Gohr $auth->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 86f5cb575dSAndreas Gohr }else{ 876080c584SRobin Gareus $evdata = array( 886080c584SRobin Gareus 'user' => $_REQUEST['u'], 896080c584SRobin Gareus 'password' => $_REQUEST['p'], 906080c584SRobin Gareus 'sticky' => $_REQUEST['r'], 916080c584SRobin Gareus 'silent' => $_REQUEST['http_credentials'], 926080c584SRobin Gareus ); 93*b5ee21aaSAdrian Lang trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 94f5cb575dSAndreas Gohr } 951ec50243SAndreas Gohr } 96f5cb575dSAndreas Gohr 9715fae107Sandi //load ACL into a global array 9888e6a4f2SAndreas Gohr global $AUTH_ACL; 99e7cb32dcSAndreas Gohr if(is_readable(DOKU_CONF.'acl.auth.php')){ 100e7cb32dcSAndreas Gohr $AUTH_ACL = file(DOKU_CONF.'acl.auth.php'); 101191bb90aSAndreas Gohr //support user wildcard 102f8cc3354SGuy Brand if(isset($_SERVER['REMOTE_USER'])){ 1035d87b2ccSAndreas Gohr $AUTH_ACL = str_replace('%USER%',$_SERVER['REMOTE_USER'],$AUTH_ACL); 1045d87b2ccSAndreas Gohr $AUTH_ACL = str_replace('@USER@',$_SERVER['REMOTE_USER'],$AUTH_ACL); //legacy 105a8fe108bSGuy Brand } 10611799630Sandi }else{ 10711799630Sandi $AUTH_ACL = array(); 10811799630Sandi } 109f3f0262cSandi} 110f3f0262cSandi 111*b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 112*b5ee21aaSAdrian Lang return auth_login($evdata['user'], 113*b5ee21aaSAdrian Lang $evdata['password'], 114*b5ee21aaSAdrian Lang $evdata['sticky'], 115*b5ee21aaSAdrian Lang $evdata['silent']); 116*b5ee21aaSAdrian Lang} 117*b5ee21aaSAdrian Lang 118f3f0262cSandi/** 119f3f0262cSandi * This tries to login the user based on the sent auth credentials 120f3f0262cSandi * 121f3f0262cSandi * The authentication works like this: if a username was given 12215fae107Sandi * a new login is assumed and user/password are checked. If they 12315fae107Sandi * are correct the password is encrypted with blowfish and stored 12415fae107Sandi * together with the username in a cookie - the same info is stored 12515fae107Sandi * in the session, too. Additonally a browserID is stored in the 12615fae107Sandi * session. 12715fae107Sandi * 12815fae107Sandi * If no username was given the cookie is checked: if the username, 12915fae107Sandi * crypted password and browserID match between session and cookie 13015fae107Sandi * no further testing is done and the user is accepted 13115fae107Sandi * 13215fae107Sandi * If a cookie was found but no session info was availabe the 133136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 13415fae107Sandi * together with username rechecked by calling this function again. 135f3f0262cSandi * 136f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 137f3f0262cSandi * are set. 13815fae107Sandi * 13915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 14015fae107Sandi * 14115fae107Sandi * @param string $user Username 14215fae107Sandi * @param string $pass Cleartext Password 14315fae107Sandi * @param bool $sticky Cookie should not expire 144f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 14515fae107Sandi * @return bool true on successful auth 146f3f0262cSandi */ 147f112c2faSAndreas Gohrfunction auth_login($user,$pass,$sticky=false,$silent=false){ 148f3f0262cSandi global $USERINFO; 149f3f0262cSandi global $conf; 150f3f0262cSandi global $lang; 151cd52f92dSchris global $auth; 152132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 153f3f0262cSandi 154beca106aSAdrian Lang if (!$auth) return false; 155beca106aSAdrian Lang 156bbbd6568SAndreas Gohr if(!empty($user)){ 157132bdbfeSandi //usual login 158cd52f92dSchris if ($auth->checkPass($user,$pass)){ 159132bdbfeSandi // make logininfo globally available 160f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 161a0b5b007SChris Smith auth_setCookie($user,PMA_blowfish_encrypt($pass,auth_cookiesalt()),$sticky); 162132bdbfeSandi return true; 163f3f0262cSandi }else{ 164f3f0262cSandi //invalid credentials - log off 165f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'],-1); 166f3f0262cSandi auth_logoff(); 167132bdbfeSandi return false; 168f3f0262cSandi } 169f3f0262cSandi }else{ 170132bdbfeSandi // read cookie information 171645c0a36SAndreas Gohr list($user,$sticky,$pass) = auth_getCookie(); 172132bdbfeSandi // get session info 173e71ce681SAndreas Gohr $session = $_SESSION[DOKU_COOKIE]['auth']; 174132bdbfeSandi if($user && $pass){ 175132bdbfeSandi // we got a cookie - see if we can trust it 176132bdbfeSandi if(isset($session) && 1777172dbc0SAndreas Gohr $auth->useSessionCache($user) && 1784c989037SChris Smith ($session['time'] >= time()-$conf['auth_security_timeout']) && 179132bdbfeSandi ($session['user'] == $user) && 180132bdbfeSandi ($session['pass'] == $pass) && //still crypted 181132bdbfeSandi ($session['buid'] == auth_browseruid()) ){ 182132bdbfeSandi // he has session, cookie and browser right - let him in 183132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 184132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 185132bdbfeSandi return true; 186132bdbfeSandi } 187f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 188132bdbfeSandi $pass = PMA_blowfish_decrypt($pass,auth_cookiesalt()); 189f112c2faSAndreas Gohr return auth_login($user,$pass,$sticky,true); 190132bdbfeSandi } 191132bdbfeSandi } 192f3f0262cSandi //just to be sure 193883179a4SAndreas Gohr auth_logoff(true); 194132bdbfeSandi return false; 195f3f0262cSandi} 196132bdbfeSandi 197132bdbfeSandi/** 198f13fa892SAndreas Gohr * Checks if a given authentication token was stored in the session 199f13fa892SAndreas Gohr * 200f13fa892SAndreas Gohr * Will setup authentication data using data from the session if the 201f13fa892SAndreas Gohr * token is correct. Will exit with a 401 Status if not. 202f13fa892SAndreas Gohr * 203f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 204f13fa892SAndreas Gohr * @param string $token The authentication token 205f13fa892SAndreas Gohr * @return boolean true (or will exit on failure) 206f13fa892SAndreas Gohr */ 207f13fa892SAndreas Gohrfunction auth_validateToken($token){ 208f13fa892SAndreas Gohr if(!$token || $token != $_SESSION[DOKU_COOKIE]['auth']['token']){ 209f13fa892SAndreas Gohr // bad token 210f13fa892SAndreas Gohr header("HTTP/1.0 401 Unauthorized"); 211f13fa892SAndreas Gohr print 'Invalid auth token - maybe the session timed out'; 212f13fa892SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['token']); // no second chance 213f13fa892SAndreas Gohr exit; 214f13fa892SAndreas Gohr } 215f13fa892SAndreas Gohr // still here? trust the session data 216f13fa892SAndreas Gohr global $USERINFO; 217f13fa892SAndreas Gohr $_SERVER['REMOTE_USER'] = $_SESSION[DOKU_COOKIE]['auth']['user']; 218f13fa892SAndreas Gohr $USERINFO = $_SESSION[DOKU_COOKIE]['auth']['info']; 219f13fa892SAndreas Gohr return true; 220f13fa892SAndreas Gohr} 221f13fa892SAndreas Gohr 222f13fa892SAndreas Gohr/** 223f13fa892SAndreas Gohr * Create an auth token and store it in the session 224f13fa892SAndreas Gohr * 225f13fa892SAndreas Gohr * NOTE: this is completely unrelated to the getSecurityToken() function 226f13fa892SAndreas Gohr * 227f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 228f13fa892SAndreas Gohr * @return string The auth token 229f13fa892SAndreas Gohr */ 230f13fa892SAndreas Gohrfunction auth_createToken(){ 231f13fa892SAndreas Gohr $token = md5(mt_rand()); 23209c2d803SAndreas Gohr @session_start(); // reopen the session if needed 233f13fa892SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['token'] = $token; 23409c2d803SAndreas Gohr session_write_close(); 235f13fa892SAndreas Gohr return $token; 236f13fa892SAndreas Gohr} 237f13fa892SAndreas Gohr 238f13fa892SAndreas Gohr/** 239136ce040Sandi * Builds a pseudo UID from browser and IP data 240132bdbfeSandi * 241132bdbfeSandi * This is neither unique nor unfakable - still it adds some 242136ce040Sandi * security. Using the first part of the IP makes sure 243136ce040Sandi * proxy farms like AOLs are stil okay. 24415fae107Sandi * 24515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 24615fae107Sandi * 24715fae107Sandi * @return string a MD5 sum of various browser headers 248132bdbfeSandi */ 249132bdbfeSandifunction auth_browseruid(){ 2502f9daf16SAndreas Gohr $ip = clientIP(true); 251132bdbfeSandi $uid = ''; 252132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 253132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 254132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 255132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 2562f9daf16SAndreas Gohr $uid .= substr($ip,0,strpos($ip,'.')); 257132bdbfeSandi return md5($uid); 258132bdbfeSandi} 259132bdbfeSandi 260132bdbfeSandi/** 261132bdbfeSandi * Creates a random key to encrypt the password in cookies 26215fae107Sandi * 26315fae107Sandi * This function tries to read the password for encrypting 26498407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 26515fae107Sandi * if no such file is found a random key is created and 26615fae107Sandi * and stored in this file. 26715fae107Sandi * 26815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 26915fae107Sandi * 27015fae107Sandi * @return string 271132bdbfeSandi */ 272132bdbfeSandifunction auth_cookiesalt(){ 273132bdbfeSandi global $conf; 27498407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 275132bdbfeSandi $salt = io_readFile($file); 276132bdbfeSandi if(empty($salt)){ 277132bdbfeSandi $salt = uniqid(rand(),true); 278132bdbfeSandi io_saveFile($file,$salt); 279132bdbfeSandi } 280132bdbfeSandi return $salt; 281f3f0262cSandi} 282f3f0262cSandi 283f3f0262cSandi/** 284883179a4SAndreas Gohr * Log out the current user 285883179a4SAndreas Gohr * 286f3f0262cSandi * This clears all authentication data and thus log the user 287883179a4SAndreas Gohr * off. It also clears session data. 28815fae107Sandi * 28915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 290883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 291f3f0262cSandi */ 292883179a4SAndreas Gohrfunction auth_logoff($keepbc=false){ 293f3f0262cSandi global $conf; 294f3f0262cSandi global $USERINFO; 2958b06d178Schris global $INFO, $ID; 2965298a619SAndreas Gohr global $auth; 29737065e65Sandi 298d4869846SAndreas Gohr // make sure the session is writable (it usually is) 299e9621d07SAndreas Gohr @session_start(); 300e9621d07SAndreas Gohr 301e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 302e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 303e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 304e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 305e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 306e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 307883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 308e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 30937065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 310f3f0262cSandi unset($_SERVER['REMOTE_USER']); 311132bdbfeSandi $USERINFO=null; //FIXME 312f5c6743cSAndreas Gohr 313f5c6743cSAndreas Gohr if (version_compare(PHP_VERSION, '5.2.0', '>')) { 31485c6f7d0SAndreas Gohr setcookie(DOKU_COOKIE,'',time()-600000,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true); 315f5c6743cSAndreas Gohr }else{ 31685c6f7d0SAndreas Gohr setcookie(DOKU_COOKIE,'',time()-600000,DOKU_REL,'',($conf['securecookie'] && is_ssl())); 317f5c6743cSAndreas Gohr } 3185298a619SAndreas Gohr 3195298a619SAndreas Gohr if($auth && $auth->canDo('logoff')){ 3205298a619SAndreas Gohr $auth->logOff(); 3215298a619SAndreas Gohr } 322f3f0262cSandi} 323f3f0262cSandi 324f3f0262cSandi/** 325f8cc712eSAndreas Gohr * Check if a user is a manager 326f8cc712eSAndreas Gohr * 327f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 328f8cc712eSAndreas Gohr * user. 329f8cc712eSAndreas Gohr * 330f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 331f8cc712eSAndreas Gohr * 332f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 333f8cc712eSAndreas Gohr * @see auth_isadmin 334f8cc712eSAndreas Gohr * @param string user - Username 335f8cc712eSAndreas Gohr * @param array groups - List of groups the user is in 336f8cc712eSAndreas Gohr * @param bool adminonly - when true checks if user is admin 337f8cc712eSAndreas Gohr */ 338f8cc712eSAndreas Gohrfunction auth_ismanager($user=null,$groups=null,$adminonly=false){ 339f8cc712eSAndreas Gohr global $conf; 340f8cc712eSAndreas Gohr global $USERINFO; 341d752aedeSAndreas Gohr global $auth; 342f8cc712eSAndreas Gohr 343beca106aSAdrian Lang if (!$auth) return false; 344c66972f2SAdrian Lang if(is_null($user)) { 345c66972f2SAdrian Lang if (!isset($_SERVER['REMOTE_USER'])) { 346c66972f2SAdrian Lang return false; 347c66972f2SAdrian Lang } else { 348c66972f2SAdrian Lang $user = $_SERVER['REMOTE_USER']; 349c66972f2SAdrian Lang } 350c66972f2SAdrian Lang } 351d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 35290583e9fSAndreas Gohr if(is_null($groups)) $groups = (array) $USERINFO['grps']; 353d752aedeSAndreas Gohr $groups = array_map(array($auth,'cleanGroup'),$groups); 354f8cc712eSAndreas Gohr $user = auth_nameencode($user); 355f8cc712eSAndreas Gohr 356f8cc712eSAndreas Gohr // check username against superuser and manager 3577651d633SGuy Brand $superusers = explode(',', $conf['superuser']); 3587651d633SGuy Brand $superusers = array_unique($superusers); 3597651d633SGuy Brand $superusers = array_map('trim', $superusers); 3607651d633SGuy Brand // prepare an array containing only true values for array_map call 3617651d633SGuy Brand $alltrue = array_fill(0, count($superusers), true); 3627651d633SGuy Brand $superusers = array_map('auth_nameencode', $superusers, $alltrue); 363e259aa79SAndreas Gohr 364e259aa79SAndreas Gohr // case insensitive? 365e259aa79SAndreas Gohr if(!$auth->isCaseSensitive()){ 366e259aa79SAndreas Gohr $superusers = array_map('utf8_strtolower',$superusers); 367e259aa79SAndreas Gohr $user = utf8_strtolower($user); 368e259aa79SAndreas Gohr } 369e259aa79SAndreas Gohr 370e259aa79SAndreas Gohr // check user match 3717651d633SGuy Brand if(in_array($user, $superusers)) return true; 3727651d633SGuy Brand 373e259aa79SAndreas Gohr // check managers 374f8cc712eSAndreas Gohr if(!$adminonly){ 3757651d633SGuy Brand $managers = explode(',', $conf['manager']); 3767651d633SGuy Brand $managers = array_unique($managers); 3777651d633SGuy Brand $managers = array_map('trim', $managers); 3787651d633SGuy Brand // prepare an array containing only true values for array_map call 3797651d633SGuy Brand $alltrue = array_fill(0, count($managers), true); 3807651d633SGuy Brand $managers = array_map('auth_nameencode', $managers, $alltrue); 381e259aa79SAndreas Gohr if(!$auth->isCaseSensitive()) $managers = array_map('utf8_strtolower',$managers); 3827651d633SGuy Brand if(in_array($user, $managers)) return true; 383f8cc712eSAndreas Gohr } 384f8cc712eSAndreas Gohr 38500ce12daSChris Smith // check user's groups against superuser and manager 38600ce12daSChris Smith if (!empty($groups)) { 38700ce12daSChris Smith 388f8cc712eSAndreas Gohr //prepend groups with @ and nameencode 389f8cc712eSAndreas Gohr $cnt = count($groups); 390f8cc712eSAndreas Gohr for($i=0; $i<$cnt; $i++){ 391f8cc712eSAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 392e259aa79SAndreas Gohr if(!$auth->isCaseSensitive()){ 393e259aa79SAndreas Gohr $groups[$i] = utf8_strtolower($groups[$i]); 394e259aa79SAndreas Gohr } 395f8cc712eSAndreas Gohr } 396f8cc712eSAndreas Gohr 397f8cc712eSAndreas Gohr // check groups against superuser and manager 3987651d633SGuy Brand foreach($superusers as $supu) 3997651d633SGuy Brand if(in_array($supu, $groups)) return true; 400f8cc712eSAndreas Gohr if(!$adminonly){ 4017651d633SGuy Brand foreach($managers as $mana) 4027651d633SGuy Brand if(in_array($mana, $groups)) return true; 403f8cc712eSAndreas Gohr } 40400ce12daSChris Smith } 40500ce12daSChris Smith 406f8cc712eSAndreas Gohr return false; 407f8cc712eSAndreas Gohr} 408f8cc712eSAndreas Gohr 409f8cc712eSAndreas Gohr/** 410f8cc712eSAndreas Gohr * Check if a user is admin 411f8cc712eSAndreas Gohr * 412f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 413f8cc712eSAndreas Gohr * 414f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 415f8cc712eSAndreas Gohr * 416f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 417f8cc712eSAndreas Gohr * @see auth_ismanager 418f8cc712eSAndreas Gohr */ 419f8cc712eSAndreas Gohrfunction auth_isadmin($user=null,$groups=null){ 420f8cc712eSAndreas Gohr return auth_ismanager($user,$groups,true); 421f8cc712eSAndreas Gohr} 422f8cc712eSAndreas Gohr 423f8cc712eSAndreas Gohr/** 42415fae107Sandi * Convinience function for auth_aclcheck() 42515fae107Sandi * 42615fae107Sandi * This checks the permissions for the current user 42715fae107Sandi * 42815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 42915fae107Sandi * 4301698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 43115fae107Sandi * @return int permission level 432f3f0262cSandi */ 433f3f0262cSandifunction auth_quickaclcheck($id){ 434f3f0262cSandi global $conf; 435f3f0262cSandi global $USERINFO; 436f3f0262cSandi # if no ACL is used always return upload rights 437f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 438f3f0262cSandi return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']); 439f3f0262cSandi} 440f3f0262cSandi 441f3f0262cSandi/** 442f3f0262cSandi * Returns the maximum rights a user has for 443f3f0262cSandi * the given ID or its namespace 44415fae107Sandi * 44515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 44615fae107Sandi * 4471698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 44815fae107Sandi * @param string $user Username 44915fae107Sandi * @param array $groups Array of groups the user is in 45015fae107Sandi * @return int permission level 451f3f0262cSandi */ 452f3f0262cSandifunction auth_aclcheck($id,$user,$groups){ 453f3f0262cSandi global $conf; 454f3f0262cSandi global $AUTH_ACL; 455d752aedeSAndreas Gohr global $auth; 456f3f0262cSandi 45785d03f68SAndreas Gohr // if no ACL is used always return upload rights 458f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 459beca106aSAdrian Lang if (!$auth) return AUTH_NONE; 460f3f0262cSandi 461074cf26bSandi //make sure groups is an array 462074cf26bSandi if(!is_array($groups)) $groups = array(); 463074cf26bSandi 46485d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 46585d03f68SAndreas Gohr if(auth_isadmin($user,$groups)) { return AUTH_ADMIN; } 46685d03f68SAndreas Gohr 467e259aa79SAndreas Gohr $ci = ''; 468e259aa79SAndreas Gohr if(!$auth->isCaseSensitive()) $ci = 'ui'; 469d752aedeSAndreas Gohr 470d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 471d752aedeSAndreas Gohr $groups = array_map(array($auth,'cleanGroup'),(array)$groups); 47285d03f68SAndreas Gohr $user = auth_nameencode($user); 47385d03f68SAndreas Gohr 4746c2bb100SAndreas Gohr //prepend groups with @ and nameencode 4752cd2db38Sandi $cnt = count($groups); 4762cd2db38Sandi for($i=0; $i<$cnt; $i++){ 4776c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 47810a76f6fSfrank } 47910a76f6fSfrank 480f3f0262cSandi $ns = getNS($id); 481f3f0262cSandi $perm = -1; 482f3f0262cSandi 48334aeb4afSAndreas Gohr if($user || count($groups)){ 484f3f0262cSandi //add ALL group 485f3f0262cSandi $groups[] = '@ALL'; 486f3f0262cSandi //add User 48734aeb4afSAndreas Gohr if($user) $groups[] = $user; 488f3f0262cSandi //build regexp 489f3f0262cSandi $regexp = join('|',$groups); 490f3f0262cSandi }else{ 491f3f0262cSandi $regexp = '@ALL'; 492f3f0262cSandi } 493f3f0262cSandi 494f3f0262cSandi //check exact match first 495e259aa79SAndreas Gohr $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/'.$ci,$AUTH_ACL); 496f3f0262cSandi if(count($matches)){ 497f3f0262cSandi foreach($matches as $match){ 498f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 499f3f0262cSandi $acl = preg_split('/\s+/',$match); 5008ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 501f3f0262cSandi if($acl[2] > $perm){ 502f3f0262cSandi $perm = $acl[2]; 503f3f0262cSandi } 504f3f0262cSandi } 505f3f0262cSandi if($perm > -1){ 506f3f0262cSandi //we had a match - return it 507f3f0262cSandi return $perm; 508f3f0262cSandi } 509f3f0262cSandi } 510f3f0262cSandi 511f3f0262cSandi //still here? do the namespace checks 512f3f0262cSandi if($ns){ 513f3f0262cSandi $path = $ns.':\*'; 514f3f0262cSandi }else{ 515f3f0262cSandi $path = '\*'; //root document 516f3f0262cSandi } 517f3f0262cSandi 518f3f0262cSandi do{ 519e259aa79SAndreas Gohr $matches = preg_grep('/^'.$path.'\s+('.$regexp.')\s+/'.$ci,$AUTH_ACL); 520f3f0262cSandi if(count($matches)){ 521f3f0262cSandi foreach($matches as $match){ 522f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 523f3f0262cSandi $acl = preg_split('/\s+/',$match); 5248ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 525f3f0262cSandi if($acl[2] > $perm){ 526f3f0262cSandi $perm = $acl[2]; 527f3f0262cSandi } 528f3f0262cSandi } 529f3f0262cSandi //we had a match - return it 530f3f0262cSandi return $perm; 531f3f0262cSandi } 532f3f0262cSandi 533f3f0262cSandi //get next higher namespace 534f3f0262cSandi $ns = getNS($ns); 535f3f0262cSandi 536f3f0262cSandi if($path != '\*'){ 537f3f0262cSandi $path = $ns.':\*'; 538f3f0262cSandi if($path == ':\*') $path = '\*'; 539f3f0262cSandi }else{ 540f3f0262cSandi //we did this already 541f3f0262cSandi //looks like there is something wrong with the ACL 542f3f0262cSandi //break here 543d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 544d5ce66f6SAndreas Gohr return AUTH_NONE; 545f3f0262cSandi } 546f3f0262cSandi }while(1); //this should never loop endless 54752a5af8dSandi 54852a5af8dSandi //still here? return no permissions 54952a5af8dSandi return AUTH_NONE; 550f3f0262cSandi} 551f3f0262cSandi 552f3f0262cSandi/** 5536c2bb100SAndreas Gohr * Encode ASCII special chars 5546c2bb100SAndreas Gohr * 5556c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 5566c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 5576c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 5586c2bb100SAndreas Gohr * urlencoding!). 5596c2bb100SAndreas Gohr * 5606c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 5616c2bb100SAndreas Gohr * 5626c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 5636c2bb100SAndreas Gohr * @see rawurldecode() 5646c2bb100SAndreas Gohr */ 565e838fc2eSAndreas Gohrfunction auth_nameencode($name,$skip_group=false){ 566a424cd8eSchris global $cache_authname; 567a424cd8eSchris $cache =& $cache_authname; 56831784267SAndreas Gohr $name = (string) $name; 569a424cd8eSchris 570a424cd8eSchris if (!isset($cache[$name][$skip_group])) { 571e838fc2eSAndreas Gohr if($skip_group && $name{0} =='@'){ 572a424cd8eSchris $cache[$name][$skip_group] = '@'.preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 5731a9ae8e5SAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))",substr($name,1)); 574e838fc2eSAndreas Gohr }else{ 575a424cd8eSchris $cache[$name][$skip_group] = preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 5761a9ae8e5SAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))",$name); 577e838fc2eSAndreas Gohr } 5786c2bb100SAndreas Gohr } 5796c2bb100SAndreas Gohr 580a424cd8eSchris return $cache[$name][$skip_group]; 581a424cd8eSchris} 582a424cd8eSchris 5836c2bb100SAndreas Gohr/** 584f3f0262cSandi * Create a pronouncable password 585f3f0262cSandi * 58615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 58715fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 58815fae107Sandi * 58915fae107Sandi * @return string pronouncable password 590f3f0262cSandi */ 591f3f0262cSandifunction auth_pwgen(){ 592f3f0262cSandi $pw = ''; 593f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 594f3f0262cSandi $v = 'aeiou'; //vowels 595f3f0262cSandi $a = $c.$v; //both 596f3f0262cSandi 597f3f0262cSandi //use two syllables... 598f3f0262cSandi for($i=0;$i < 2; $i++){ 599f3f0262cSandi $pw .= $c[rand(0, strlen($c)-1)]; 600f3f0262cSandi $pw .= $v[rand(0, strlen($v)-1)]; 601f3f0262cSandi $pw .= $a[rand(0, strlen($a)-1)]; 602f3f0262cSandi } 603f3f0262cSandi //... and add a nice number 604f3f0262cSandi $pw .= rand(10,99); 605f3f0262cSandi 606f3f0262cSandi return $pw; 607f3f0262cSandi} 608f3f0262cSandi 609f3f0262cSandi/** 610f3f0262cSandi * Sends a password to the given user 611f3f0262cSandi * 61215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 61315fae107Sandi * 61415fae107Sandi * @return bool true on success 615f3f0262cSandi */ 616f3f0262cSandifunction auth_sendPassword($user,$password){ 617f3f0262cSandi global $conf; 618f3f0262cSandi global $lang; 619cd52f92dSchris global $auth; 620beca106aSAdrian Lang if (!$auth) return false; 621cd52f92dSchris 622f3f0262cSandi $hdrs = ''; 623d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 624cd52f92dSchris $userinfo = $auth->getUserData($user); 625f3f0262cSandi 62687ddda95Sandi if(!$userinfo['mail']) return false; 627f3f0262cSandi 628f3f0262cSandi $text = rawLocale('password'); 629ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 63087ddda95Sandi $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 631f3f0262cSandi $text = str_replace('@LOGIN@',$user,$text); 632f3f0262cSandi $text = str_replace('@PASSWORD@',$password,$text); 633f3f0262cSandi $text = str_replace('@TITLE@',$conf['title'],$text); 634f3f0262cSandi 63544f669e9Sandi return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 63644f669e9Sandi $lang['regpwmail'], 63744f669e9Sandi $text, 63844f669e9Sandi $conf['mailfrom']); 639f3f0262cSandi} 640f3f0262cSandi 641f3f0262cSandi/** 64215fae107Sandi * Register a new user 643f3f0262cSandi * 64415fae107Sandi * This registers a new user - Data is read directly from $_POST 64515fae107Sandi * 64615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 64715fae107Sandi * 64815fae107Sandi * @return bool true on success, false on any error 649f3f0262cSandi */ 650f3f0262cSandifunction register(){ 651f3f0262cSandi global $lang; 652eb5d07e4Sjan global $conf; 653cd52f92dSchris global $auth; 654f3f0262cSandi 655beca106aSAdrian Lang if (!$auth) return false; 656f3f0262cSandi if(!$_POST['save']) return false; 65782fd59b6SAndreas Gohr if(!$auth->canDo('addUser')) return false; 658640145a5Sandi 659f3f0262cSandi //clean username 660d752aedeSAndreas Gohr $_POST['login'] = trim($auth->cleanUser($_POST['login'])); 661d752aedeSAndreas Gohr 662f3f0262cSandi //clean fullname and email 66354f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname'])); 66454f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email'])); 665f3f0262cSandi 666f3f0262cSandi if( empty($_POST['login']) || 667f3f0262cSandi empty($_POST['fullname']) || 668f3f0262cSandi empty($_POST['email']) ){ 669f3f0262cSandi msg($lang['regmissing'],-1); 670f3f0262cSandi return false; 671f3f0262cSandi } 672f3f0262cSandi 673cab2716aSmatthias.grimm if ($conf['autopasswd']) { 674cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 675cab2716aSmatthias.grimm } elseif (empty($_POST['pass']) || 676cab2716aSmatthias.grimm empty($_POST['passchk'])) { 677bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 678cab2716aSmatthias.grimm return false; 679cab2716aSmatthias.grimm } elseif ($_POST['pass'] != $_POST['passchk']) { 680bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 681cab2716aSmatthias.grimm return false; 682cab2716aSmatthias.grimm } else { 683cab2716aSmatthias.grimm $pass = $_POST['pass']; // accept checked and valid password 684cab2716aSmatthias.grimm } 685cab2716aSmatthias.grimm 686f3f0262cSandi //check mail 68744f669e9Sandi if(!mail_isvalid($_POST['email'])){ 688f3f0262cSandi msg($lang['regbadmail'],-1); 689f3f0262cSandi return false; 690f3f0262cSandi } 691f3f0262cSandi 692f3f0262cSandi //okay try to create the user 6937d3c8d42SGabriel Birke if(!$auth->triggerUserMod('create', array($_POST['login'],$pass,$_POST['fullname'],$_POST['email']))){ 694f3f0262cSandi msg($lang['reguexists'],-1); 695f3f0262cSandi return false; 696f3f0262cSandi } 697f3f0262cSandi 69802a498e7Schris // create substitutions for use in notification email 69902a498e7Schris $substitutions = array( 70002a498e7Schris 'NEWUSER' => $_POST['login'], 70102a498e7Schris 'NEWNAME' => $_POST['fullname'], 70202a498e7Schris 'NEWEMAIL' => $_POST['email'], 70302a498e7Schris ); 70402a498e7Schris 705cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 706cab2716aSmatthias.grimm msg($lang['regsuccess2'],1); 70702a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 708cab2716aSmatthias.grimm return true; 709cab2716aSmatthias.grimm } 710cab2716aSmatthias.grimm 711cab2716aSmatthias.grimm // autogenerated password? then send him the password 712f3f0262cSandi if (auth_sendPassword($_POST['login'],$pass)){ 713f3f0262cSandi msg($lang['regsuccess'],1); 71402a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 715f3f0262cSandi return true; 716f3f0262cSandi }else{ 717f3f0262cSandi msg($lang['regmailfail'],-1); 718f3f0262cSandi return false; 719f3f0262cSandi } 720f3f0262cSandi} 721f3f0262cSandi 72210a76f6fSfrank/** 7238b06d178Schris * Update user profile 7248b06d178Schris * 7258b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 7268b06d178Schris */ 7278b06d178Schrisfunction updateprofile() { 7288b06d178Schris global $conf; 7298b06d178Schris global $INFO; 7308b06d178Schris global $lang; 731cd52f92dSchris global $auth; 7328b06d178Schris 733beca106aSAdrian Lang if (!$auth) return false; 734bb4866bdSchris if(empty($_POST['save'])) return false; 7351b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 7368b06d178Schris 73782fd59b6SAndreas Gohr // should not be able to get here without Profile being possible... 73882fd59b6SAndreas Gohr if(!$auth->canDo('Profile')) { 7398b06d178Schris msg($lang['profna'],-1); 7408b06d178Schris return false; 7418b06d178Schris } 7428b06d178Schris 7438b06d178Schris if ($_POST['newpass'] != $_POST['passchk']) { 7448b06d178Schris msg($lang['regbadpass'], -1); // complain about misspelled passwords 7458b06d178Schris return false; 7468b06d178Schris } 7478b06d178Schris 7488b06d178Schris //clean fullname and email 74954f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname'])); 75054f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email'])); 7518b06d178Schris 7524369edafSAndy Webber if ((empty($_POST['fullname']) && $auth->canDo('modName')) || 7534369edafSAndy Webber (empty($_POST['email']) && $auth->canDo('modMail'))) { 7548b06d178Schris msg($lang['profnoempty'],-1); 7558b06d178Schris return false; 7568b06d178Schris } 7578b06d178Schris 7584369edafSAndy Webber if (!mail_isvalid($_POST['email']) && $auth->canDo('modMail')){ 7598b06d178Schris msg($lang['regbadmail'],-1); 7608b06d178Schris return false; 7618b06d178Schris } 7628b06d178Schris 7634c21b7eeSAndreas Gohr if ($_POST['fullname'] != $INFO['userinfo']['name'] && $auth->canDo('modName')) $changes['name'] = $_POST['fullname']; 7644c21b7eeSAndreas Gohr if ($_POST['email'] != $INFO['userinfo']['mail'] && $auth->canDo('modMail')) $changes['mail'] = $_POST['email']; 765cf626a62SAndreas Gohr if (!empty($_POST['newpass']) && $auth->canDo('modPass')) $changes['pass'] = $_POST['newpass']; 7664c21b7eeSAndreas Gohr 7678b06d178Schris if (!count($changes)) { 7688b06d178Schris msg($lang['profnochange'], -1); 7698b06d178Schris return false; 7708b06d178Schris } 7718b06d178Schris 7728b06d178Schris if ($conf['profileconfirm']) { 773df466c7aSAndreas Gohr if (!$auth->checkPass($_SERVER['REMOTE_USER'], $_POST['oldpass'])) { 7748b06d178Schris msg($lang['badlogin'],-1); 7758b06d178Schris return false; 7768b06d178Schris } 7778b06d178Schris } 7788b06d178Schris 779a0b5b007SChris Smith if ($result = $auth->triggerUserMod('modify', array($_SERVER['REMOTE_USER'], $changes))) { 780a0b5b007SChris Smith // update cookie and session with the changed data 781a0b5b007SChris Smith $cookie = base64_decode($_COOKIE[DOKU_COOKIE]); 7824b7f9e70STom N Harris list($user,$sticky,$pass) = explode('|',$cookie,3); 783a0b5b007SChris Smith if ($changes['pass']) $pass = PMA_blowfish_encrypt($changes['pass'],auth_cookiesalt()); 784a0b5b007SChris Smith 785a0b5b007SChris Smith auth_setCookie($_SERVER['REMOTE_USER'],$pass,(bool)$sticky); 78625b2a98cSMichael Klier return true; 787a0b5b007SChris Smith } 7888b06d178Schris} 7898b06d178Schris 7908b06d178Schris/** 7918b06d178Schris * Send a new password 7928b06d178Schris * 7931d5856cfSAndreas Gohr * This function handles both phases of the password reset: 7941d5856cfSAndreas Gohr * 7951d5856cfSAndreas Gohr * - handling the first request of password reset 7961d5856cfSAndreas Gohr * - validating the password reset auth token 7971d5856cfSAndreas Gohr * 7988b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 7998b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 8001d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8018b06d178Schris * 8028b06d178Schris * @return bool true on success, false on any error 8038b06d178Schris */ 8048b06d178Schrisfunction act_resendpwd(){ 8058b06d178Schris global $lang; 8068b06d178Schris global $conf; 807cd52f92dSchris global $auth; 8088b06d178Schris 809409d7af7SAndreas Gohr if(!actionOK('resendpwd')) return false; 810beca106aSAdrian Lang if (!$auth) return false; 8118b06d178Schris 81282fd59b6SAndreas Gohr // should not be able to get here without modPass being possible... 81382fd59b6SAndreas Gohr if(!$auth->canDo('modPass')) { 8148b06d178Schris msg($lang['resendna'],-1); 8158b06d178Schris return false; 8168b06d178Schris } 8178b06d178Schris 8181d5856cfSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/','',$_REQUEST['pwauth']); 8198b06d178Schris 8201d5856cfSAndreas Gohr if($token){ 8211d5856cfSAndreas Gohr // we're in token phase 8221d5856cfSAndreas Gohr 8231d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 8241d5856cfSAndreas Gohr if(!@file_exists($tfile)){ 8251d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'],-1); 8261d5856cfSAndreas Gohr return false; 8271d5856cfSAndreas Gohr } 8281d5856cfSAndreas Gohr $user = io_readfile($tfile); 8291d5856cfSAndreas Gohr @unlink($tfile); 830cd52f92dSchris $userinfo = $auth->getUserData($user); 8318b06d178Schris if(!$userinfo['mail']) { 8328b06d178Schris msg($lang['resendpwdnouser'], -1); 8338b06d178Schris return false; 8348b06d178Schris } 8358b06d178Schris 8368b06d178Schris $pass = auth_pwgen(); 8377d3c8d42SGabriel Birke if (!$auth->triggerUserMod('modify', array($user,array('pass' => $pass)))) { 8388b06d178Schris msg('error modifying user data',-1); 8398b06d178Schris return false; 8408b06d178Schris } 8418b06d178Schris 8428b06d178Schris if (auth_sendPassword($user,$pass)) { 8438b06d178Schris msg($lang['resendpwdsuccess'],1); 8448b06d178Schris } else { 8458b06d178Schris msg($lang['regmailfail'],-1); 8468b06d178Schris } 8478b06d178Schris return true; 8481d5856cfSAndreas Gohr 8491d5856cfSAndreas Gohr } else { 8501d5856cfSAndreas Gohr // we're in request phase 8511d5856cfSAndreas Gohr 8521d5856cfSAndreas Gohr if(!$_POST['save']) return false; 8531d5856cfSAndreas Gohr 8541d5856cfSAndreas Gohr if (empty($_POST['login'])) { 8551d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 8561d5856cfSAndreas Gohr return false; 8571d5856cfSAndreas Gohr } else { 858d752aedeSAndreas Gohr $user = trim($auth->cleanUser($_POST['login'])); 8591d5856cfSAndreas Gohr } 8601d5856cfSAndreas Gohr 8611d5856cfSAndreas Gohr $userinfo = $auth->getUserData($user); 8621d5856cfSAndreas Gohr if(!$userinfo['mail']) { 8631d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 8641d5856cfSAndreas Gohr return false; 8651d5856cfSAndreas Gohr } 8661d5856cfSAndreas Gohr 8671d5856cfSAndreas Gohr // generate auth token 8681d5856cfSAndreas Gohr $token = md5(auth_cookiesalt().$user); //secret but user based 8691d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 8701d5856cfSAndreas Gohr $url = wl('',array('do'=>'resendpwd','pwauth'=>$token),true,'&'); 8711d5856cfSAndreas Gohr 8721d5856cfSAndreas Gohr io_saveFile($tfile,$user); 8731d5856cfSAndreas Gohr 8741d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 8751d5856cfSAndreas Gohr $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 8761d5856cfSAndreas Gohr $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 8771d5856cfSAndreas Gohr $text = str_replace('@LOGIN@',$user,$text); 8781d5856cfSAndreas Gohr $text = str_replace('@TITLE@',$conf['title'],$text); 8791d5856cfSAndreas Gohr $text = str_replace('@CONFIRM@',$url,$text); 8801d5856cfSAndreas Gohr 8811d5856cfSAndreas Gohr if(mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 8821d5856cfSAndreas Gohr $lang['regpwmail'], 8831d5856cfSAndreas Gohr $text, 8841d5856cfSAndreas Gohr $conf['mailfrom'])){ 8851d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'],1); 8861d5856cfSAndreas Gohr }else{ 8871d5856cfSAndreas Gohr msg($lang['regmailfail'],-1); 8881d5856cfSAndreas Gohr } 8891d5856cfSAndreas Gohr return true; 8901d5856cfSAndreas Gohr } 8911d5856cfSAndreas Gohr 8921d5856cfSAndreas Gohr return false; // never reached 8938b06d178Schris} 8948b06d178Schris 8958b06d178Schris/** 896b0855b11Sandi * Encrypts a password using the given method and salt 897b0855b11Sandi * 898b0855b11Sandi * If the selected method needs a salt and none was given, a random one 899b0855b11Sandi * is chosen. 900b0855b11Sandi * 901b0855b11Sandi * The following methods are understood: 902b0855b11Sandi * 903b0855b11Sandi * smd5 - Salted MD5 hashing 904577c7cdaSAndreas Gohr * apr1 - Apache salted MD5 hashing 905b0855b11Sandi * md5 - Simple MD5 hashing 906b0855b11Sandi * sha1 - SHA1 hashing 907b0855b11Sandi * ssha - Salted SHA1 hashing 908d7be6245Sandi * crypt - Unix crypt 909d7be6245Sandi * mysql - MySQL password (old method) 910d7be6245Sandi * my411 - MySQL 4.1.1 password 91143ee7484SAndreas Gohr * kmd5 - Salted MD5 hashing as used by UNB 912b0855b11Sandi * 913b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 914b0855b11Sandi * @return string The crypted password 915b0855b11Sandi */ 916577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear,$method='',$salt=null){ 917b0855b11Sandi global $conf; 918b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 91910a76f6fSfrank 920b0855b11Sandi //prepare a salt 921577c7cdaSAndreas Gohr if(is_null($salt)) $salt = md5(uniqid(rand(), true)); 922b0855b11Sandi 923b0855b11Sandi switch(strtolower($method)){ 924b0855b11Sandi case 'smd5': 925056cb2ccSChris Smith if(defined('CRYPT_MD5') && CRYPT_MD5) return crypt($clear,'$1$'.substr($salt,0,8).'$'); 926577c7cdaSAndreas Gohr // when crypt can't handle SMD5, falls through to pure PHP implementation 927577c7cdaSAndreas Gohr $magic = '1'; 928577c7cdaSAndreas Gohr case 'apr1': 929577c7cdaSAndreas Gohr //from http://de.php.net/manual/en/function.crypt.php#73619 comment by <mikey_nich at hotmail dot com> 930577c7cdaSAndreas Gohr if(!$magic) $magic = 'apr1'; 931577c7cdaSAndreas Gohr $salt = substr($salt,0,8); 932577c7cdaSAndreas Gohr $len = strlen($clear); 933577c7cdaSAndreas Gohr $text = $clear.'$'.$magic.'$'.$salt; 934577c7cdaSAndreas Gohr $bin = pack("H32", md5($clear.$salt.$clear)); 935db959ae3SAndreas Gohr for($i = $len; $i > 0; $i -= 16) { 936db959ae3SAndreas Gohr $text .= substr($bin, 0, min(16, $i)); 937db959ae3SAndreas Gohr } 938db959ae3SAndreas Gohr for($i = $len; $i > 0; $i >>= 1) { 939db959ae3SAndreas Gohr $text .= ($i & 1) ? chr(0) : $clear{0}; 940db959ae3SAndreas Gohr } 941577c7cdaSAndreas Gohr $bin = pack("H32", md5($text)); 942577c7cdaSAndreas Gohr for($i = 0; $i < 1000; $i++) { 943577c7cdaSAndreas Gohr $new = ($i & 1) ? $clear : $bin; 944577c7cdaSAndreas Gohr if ($i % 3) $new .= $salt; 945577c7cdaSAndreas Gohr if ($i % 7) $new .= $clear; 946577c7cdaSAndreas Gohr $new .= ($i & 1) ? $bin : $clear; 947577c7cdaSAndreas Gohr $bin = pack("H32", md5($new)); 948577c7cdaSAndreas Gohr } 949577c7cdaSAndreas Gohr $tmp = ''; 950577c7cdaSAndreas Gohr for ($i = 0; $i < 5; $i++) { 951577c7cdaSAndreas Gohr $k = $i + 6; 952577c7cdaSAndreas Gohr $j = $i + 12; 953577c7cdaSAndreas Gohr if ($j == 16) $j = 5; 954577c7cdaSAndreas Gohr $tmp = $bin[$i].$bin[$k].$bin[$j].$tmp; 955577c7cdaSAndreas Gohr } 956577c7cdaSAndreas Gohr $tmp = chr(0).chr(0).$bin[11].$tmp; 957577c7cdaSAndreas Gohr $tmp = strtr(strrev(substr(base64_encode($tmp), 2)), 958577c7cdaSAndreas Gohr "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", 959577c7cdaSAndreas Gohr "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"); 960577c7cdaSAndreas Gohr return '$'.$magic.'$'.$salt.'$'.$tmp; 961b0855b11Sandi case 'md5': 962b0855b11Sandi return md5($clear); 963b0855b11Sandi case 'sha1': 964b0855b11Sandi return sha1($clear); 965b0855b11Sandi case 'ssha': 966b0855b11Sandi $salt=substr($salt,0,4); 967d6e54e02Smatthiasgrimm return '{SSHA}'.base64_encode(pack("H*", sha1($clear.$salt)).$salt); 968b0855b11Sandi case 'crypt': 969b0855b11Sandi return crypt($clear,substr($salt,0,2)); 970d7be6245Sandi case 'mysql': 971d7be6245Sandi //from http://www.php.net/mysql comment by <soren at byu dot edu> 972d7be6245Sandi $nr=0x50305735; 973d7be6245Sandi $nr2=0x12345671; 974d7be6245Sandi $add=7; 975d7be6245Sandi $charArr = preg_split("//", $clear); 976d7be6245Sandi foreach ($charArr as $char) { 977d7be6245Sandi if (($char == '') || ($char == ' ') || ($char == '\t')) continue; 978d7be6245Sandi $charVal = ord($char); 979d7be6245Sandi $nr ^= ((($nr & 63) + $add) * $charVal) + ($nr << 8); 980d7be6245Sandi $nr2 += ($nr2 << 8) ^ $nr; 981d7be6245Sandi $add += $charVal; 982d7be6245Sandi } 983d7be6245Sandi return sprintf("%08x%08x", ($nr & 0x7fffffff), ($nr2 & 0x7fffffff)); 984d7be6245Sandi case 'my411': 985d7be6245Sandi return '*'.sha1(pack("H*", sha1($clear))); 98643ee7484SAndreas Gohr case 'kmd5': 98743ee7484SAndreas Gohr $key = substr($salt, 16, 2); 98843ee7484SAndreas Gohr $hash1 = strtolower(md5($key . md5($clear))); 98943ee7484SAndreas Gohr $hash2 = substr($hash1, 0, 16) . $key . substr($hash1, 16); 99043ee7484SAndreas Gohr return $hash2; 991b0855b11Sandi default: 992b0855b11Sandi msg("Unsupported crypt method $method",-1); 993b0855b11Sandi } 994b0855b11Sandi} 995b0855b11Sandi 996b0855b11Sandi/** 997b0855b11Sandi * Verifies a cleartext password against a crypted hash 998b0855b11Sandi * 999b0855b11Sandi * The method and salt used for the crypted hash is determined automatically 1000b0855b11Sandi * then the clear text password is crypted using the same method. If both hashs 1001b0855b11Sandi * match true is is returned else false 1002b0855b11Sandi * 1003b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 1004b0855b11Sandi * @return bool 1005b0855b11Sandi */ 1006b0855b11Sandifunction auth_verifyPassword($clear,$crypt){ 1007b0855b11Sandi $method=''; 1008b0855b11Sandi $salt=''; 1009b0855b11Sandi 1010b0855b11Sandi //determine the used method and salt 1011d7be6245Sandi $len = strlen($crypt); 1012577c7cdaSAndreas Gohr if(preg_match('/^\$1\$([^\$]{0,8})\$/',$crypt,$m)){ 1013b0855b11Sandi $method = 'smd5'; 1014577c7cdaSAndreas Gohr $salt = $m[1]; 1015577c7cdaSAndreas Gohr }elseif(preg_match('/^\$apr1\$([^\$]{0,8})\$/',$crypt,$m)){ 1016577c7cdaSAndreas Gohr $method = 'apr1'; 1017577c7cdaSAndreas Gohr $salt = $m[1]; 1018b0855b11Sandi }elseif(substr($crypt,0,6) == '{SSHA}'){ 1019b0855b11Sandi $method = 'ssha'; 1020b0855b11Sandi $salt = substr(base64_decode(substr($crypt, 6)),20); 1021d7be6245Sandi }elseif($len == 32){ 1022b0855b11Sandi $method = 'md5'; 1023d7be6245Sandi }elseif($len == 40){ 1024b0855b11Sandi $method = 'sha1'; 1025d7be6245Sandi }elseif($len == 16){ 1026d7be6245Sandi $method = 'mysql'; 1027d7be6245Sandi }elseif($len == 41 && $crypt[0] == '*'){ 1028d7be6245Sandi $method = 'my411'; 102943ee7484SAndreas Gohr }elseif($len == 34){ 103043ee7484SAndreas Gohr $method = 'kmd5'; 103143ee7484SAndreas Gohr $salt = $crypt; 1032b0855b11Sandi }else{ 1033b0855b11Sandi $method = 'crypt'; 1034b0855b11Sandi $salt = substr($crypt,0,2); 1035b0855b11Sandi } 1036b0855b11Sandi 1037b0855b11Sandi //crypt and compare 1038b0855b11Sandi if(auth_cryptPassword($clear,$method,$salt) === $crypt){ 1039b0855b11Sandi return true; 1040b0855b11Sandi } 1041b0855b11Sandi return false; 1042b0855b11Sandi} 1043340756e4Sandi 1044a0b5b007SChris Smith/** 1045a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1046a0b5b007SChris Smith * 1047a0b5b007SChris Smith * @param string $user username 1048a0b5b007SChris Smith * @param string $pass encrypted password 1049a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1050a0b5b007SChris Smith */ 1051a0b5b007SChris Smithfunction auth_setCookie($user,$pass,$sticky) { 1052a0b5b007SChris Smith global $conf; 1053a0b5b007SChris Smith global $auth; 105479d00841SOliver Geisen global $USERINFO; 1055a0b5b007SChris Smith 1056beca106aSAdrian Lang if (!$auth) return false; 1057a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1058a0b5b007SChris Smith 1059a0b5b007SChris Smith // set cookie 1060645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 1061c66972f2SAdrian Lang $time = $sticky ? (time()+60*60*24*365) : 0; //one year 1062a0b5b007SChris Smith if (version_compare(PHP_VERSION, '5.2.0', '>')) { 1063a0b5b007SChris Smith setcookie(DOKU_COOKIE,$cookie,$time,DOKU_REL,'',($conf['securecookie'] && is_ssl()),true); 1064a0b5b007SChris Smith }else{ 1065a0b5b007SChris Smith setcookie(DOKU_COOKIE,$cookie,$time,DOKU_REL,'',($conf['securecookie'] && is_ssl())); 1066a0b5b007SChris Smith } 1067a0b5b007SChris Smith // set session 1068a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1069a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass; 1070a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1071a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1072a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1073a0b5b007SChris Smith} 1074a0b5b007SChris Smith 1075645c0a36SAndreas Gohr/** 1076645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1077645c0a36SAndreas Gohr * 1078645c0a36SAndreas Gohr * @returns array 1079645c0a36SAndreas Gohr */ 1080645c0a36SAndreas Gohrfunction auth_getCookie(){ 1081c66972f2SAdrian Lang if (!isset($_COOKIE[DOKU_COOKIE])) { 1082c66972f2SAdrian Lang return array(null, null, null); 1083c66972f2SAdrian Lang } 1084645c0a36SAndreas Gohr list($user,$sticky,$pass) = explode('|',$_COOKIE[DOKU_COOKIE],3); 1085645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1086645c0a36SAndreas Gohr $pass = base64_decode($pass); 1087645c0a36SAndreas Gohr $user = base64_decode($user); 1088645c0a36SAndreas Gohr return array($user,$sticky,$pass); 1089645c0a36SAndreas Gohr} 1090645c0a36SAndreas Gohr 1091340756e4Sandi//Setup VIM: ex: et ts=2 enc=utf-8 : 1092