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.'); 131c73890cSAndreas Gohr 14ebf97c8fSAndreas Gohr// some ACL level defines 15ebf97c8fSAndreas Gohrdefine('AUTH_NONE',0); 16ebf97c8fSAndreas Gohrdefine('AUTH_READ',1); 17ebf97c8fSAndreas Gohrdefine('AUTH_EDIT',2); 18ebf97c8fSAndreas Gohrdefine('AUTH_CREATE',4); 19ebf97c8fSAndreas Gohrdefine('AUTH_UPLOAD',8); 20ebf97c8fSAndreas Gohrdefine('AUTH_DELETE',16); 21ebf97c8fSAndreas Gohrdefine('AUTH_ADMIN',255); 22ebf97c8fSAndreas Gohr 2316905344SAndreas Gohr/** 2416905344SAndreas Gohr * Initialize the auth system. 2516905344SAndreas Gohr * 2616905344SAndreas Gohr * This function is automatically called at the end of init.php 2716905344SAndreas Gohr * 2816905344SAndreas Gohr * This used to be the main() of the auth.php 2916905344SAndreas Gohr * 3016905344SAndreas Gohr * @todo backend loading maybe should be handled by the class autoloader 3116905344SAndreas Gohr * @todo maybe split into multiple functions at the XXX marked positions 3216905344SAndreas Gohr */ 3316905344SAndreas Gohrfunction auth_setup(){ 34742c66f8Schris global $conf; 3503c4aec3Schris global $auth; 369a9714acSDominik Eckelmann global $AUTH_ACL; 379a9714acSDominik Eckelmann global $lang; 38c8f80b4eSAndreas Gohr global $config_cascade; 399c29eea5SJan Schumann global $plugin_controller; 409a9714acSDominik Eckelmann $AUTH_ACL = array(); 4103c4aec3Schris 4216905344SAndreas Gohr if(!$conf['useacl']) return false; 4316905344SAndreas Gohr 449c29eea5SJan Schumann // try to load auth backend from plugins 459c29eea5SJan Schumann $plugins = $plugin_controller->getList('auth'); 469c29eea5SJan Schumann foreach ($plugin_controller->getList('auth') as $plugin) { 479c29eea5SJan Schumann if ($conf['authtype'] === $plugin) { 48*f4476bd9SJan Schumann $auth = $plugin_controller->load('auth', $plugin); 499c29eea5SJan Schumann break; 509c29eea5SJan Schumann } 519c29eea5SJan Schumann } 529c29eea5SJan Schumann 53*f4476bd9SJan Schumann if(!$auth) return; 548b06d178Schris 55*f4476bd9SJan Schumann if ($auth && $auth->success == false) { 560f4f4adfSAndreas Gohr // degrade to unauthenticated user 57d2dde4ebSMatthias Grimm unset($auth); 580f4f4adfSAndreas Gohr auth_logoff(); 59cd52f92dSchris msg($lang['authtempfail'], -1); 60d2dde4ebSMatthias Grimm } 6116905344SAndreas Gohr 6216905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 63bbbd6568SAndreas Gohr if (!isset($_REQUEST['u'])) $_REQUEST['u'] = ''; 64bbbd6568SAndreas Gohr if (!isset($_REQUEST['p'])) $_REQUEST['p'] = ''; 65bbbd6568SAndreas Gohr if (!isset($_REQUEST['r'])) $_REQUEST['r'] = ''; 66b2c0d874SGina Haeussge $_REQUEST['http_credentials'] = false; 6717f89d7eSMichael Klier if (!$conf['rememberme']) $_REQUEST['r'] = false; 68bbbd6568SAndreas Gohr 69b2665af7SMichael Hamann // handle renamed HTTP_AUTHORIZATION variable (can happen when a fix like 70b2665af7SMichael Hamann // the one presented at 71b2665af7SMichael Hamann // http://www.besthostratings.com/articles/http-auth-php-cgi.html is used 72b2665af7SMichael Hamann // for enabling HTTP authentication with CGI/SuExec) 73b2665af7SMichael Hamann if(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) 74b2665af7SMichael Hamann $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; 75528ddc7cSAndreas Gohr // streamline HTTP auth credentials (IIS/rewrite -> mod_php) 7606156f3cSAndreas Gohr if(isset($_SERVER['HTTP_AUTHORIZATION'])){ 77528ddc7cSAndreas Gohr list($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']) = 78528ddc7cSAndreas Gohr explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); 79528ddc7cSAndreas Gohr } 80528ddc7cSAndreas Gohr 811e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 824a26ad85Schris if(empty($_REQUEST['u']) && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])){ 831e8c9c90SAndreas Gohr $_REQUEST['u'] = $_SERVER['PHP_AUTH_USER']; 841e8c9c90SAndreas Gohr $_REQUEST['p'] = $_SERVER['PHP_AUTH_PW']; 85b2c0d874SGina Haeussge $_REQUEST['http_credentials'] = true; 861e8c9c90SAndreas Gohr } 871e8c9c90SAndreas Gohr 88191bb90aSAndreas Gohr // apply cleaning 89*f4476bd9SJan Schumann if (true === $auth->success) 90*f4476bd9SJan Schumann { 91191bb90aSAndreas Gohr $_REQUEST['u'] = $auth->cleanUser($_REQUEST['u']); 92*f4476bd9SJan Schumann } 93191bb90aSAndreas Gohr 94c66972f2SAdrian Lang if(isset($_REQUEST['authtok'])){ 95f13fa892SAndreas Gohr // when an authentication token is given, trust the session 96f13fa892SAndreas Gohr auth_validateToken($_REQUEST['authtok']); 97f13fa892SAndreas Gohr }elseif(!is_null($auth) && $auth->canDo('external')){ 98f13fa892SAndreas Gohr // external trust mechanism in place 99f5cb575dSAndreas Gohr $auth->trustExternal($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['r']); 100f5cb575dSAndreas Gohr }else{ 1016080c584SRobin Gareus $evdata = array( 1026080c584SRobin Gareus 'user' => $_REQUEST['u'], 1036080c584SRobin Gareus 'password' => $_REQUEST['p'], 1046080c584SRobin Gareus 'sticky' => $_REQUEST['r'], 1056080c584SRobin Gareus 'silent' => $_REQUEST['http_credentials'], 1066080c584SRobin Gareus ); 107b5ee21aaSAdrian Lang trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 108f5cb575dSAndreas Gohr } 109f5cb575dSAndreas Gohr 11016905344SAndreas Gohr //load ACL into a global array XXX 11175c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 11275c93b77SAndreas Gohr} 11375c93b77SAndreas Gohr 11475c93b77SAndreas Gohr/** 11575c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 11675c93b77SAndreas Gohr * 11775c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11875c93b77SAndreas Gohr * @returns array 11975c93b77SAndreas Gohr */ 12075c93b77SAndreas Gohrfunction auth_loadACL(){ 12175c93b77SAndreas Gohr global $config_cascade; 12275c93b77SAndreas Gohr 12375c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 12475c93b77SAndreas Gohr 12575c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 12675c93b77SAndreas Gohr 127191bb90aSAndreas Gohr //support user wildcard 128f8cc3354SGuy Brand if(isset($_SERVER['REMOTE_USER'])){ 12975c93b77SAndreas Gohr $len = count($acl); 13075c93b77SAndreas Gohr for($i=0; $i<$len; $i++){ 13175c93b77SAndreas Gohr if($acl[$i]{0} == '#') continue; 13275c93b77SAndreas Gohr list($id,$rest) = preg_split('/\s+/',$acl[$i],2); 13375c93b77SAndreas Gohr $id = str_replace('%USER%',cleanID($_SERVER['REMOTE_USER']),$id); 13475c93b77SAndreas Gohr $rest = str_replace('%USER%',auth_nameencode($_SERVER['REMOTE_USER']),$rest); 13575c93b77SAndreas Gohr $acl[$i] = "$id\t$rest"; 136a8fe108bSGuy Brand } 13711799630Sandi } 13875c93b77SAndreas Gohr return $acl; 139f3f0262cSandi} 140f3f0262cSandi 141b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 142b5ee21aaSAdrian Lang return auth_login($evdata['user'], 143b5ee21aaSAdrian Lang $evdata['password'], 144b5ee21aaSAdrian Lang $evdata['sticky'], 145b5ee21aaSAdrian Lang $evdata['silent']); 146b5ee21aaSAdrian Lang} 147b5ee21aaSAdrian Lang 148f3f0262cSandi/** 149f3f0262cSandi * This tries to login the user based on the sent auth credentials 150f3f0262cSandi * 151f3f0262cSandi * The authentication works like this: if a username was given 15215fae107Sandi * a new login is assumed and user/password are checked. If they 15315fae107Sandi * are correct the password is encrypted with blowfish and stored 15415fae107Sandi * together with the username in a cookie - the same info is stored 15515fae107Sandi * in the session, too. Additonally a browserID is stored in the 15615fae107Sandi * session. 15715fae107Sandi * 15815fae107Sandi * If no username was given the cookie is checked: if the username, 15915fae107Sandi * crypted password and browserID match between session and cookie 16015fae107Sandi * no further testing is done and the user is accepted 16115fae107Sandi * 16215fae107Sandi * If a cookie was found but no session info was availabe the 163136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 16415fae107Sandi * together with username rechecked by calling this function again. 165f3f0262cSandi * 166f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 167f3f0262cSandi * are set. 16815fae107Sandi * 16915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 17015fae107Sandi * 17115fae107Sandi * @param string $user Username 17215fae107Sandi * @param string $pass Cleartext Password 17315fae107Sandi * @param bool $sticky Cookie should not expire 174f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 17515fae107Sandi * @return bool true on successful auth 176f3f0262cSandi */ 177f112c2faSAndreas Gohrfunction auth_login($user,$pass,$sticky=false,$silent=false){ 178f3f0262cSandi global $USERINFO; 179f3f0262cSandi global $conf; 180f3f0262cSandi global $lang; 181cd52f92dSchris global $auth; 182132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 183f3f0262cSandi 184beca106aSAdrian Lang if (!$auth) return false; 185beca106aSAdrian Lang 186bbbd6568SAndreas Gohr if(!empty($user)){ 187132bdbfeSandi //usual login 188cd52f92dSchris if ($auth->checkPass($user,$pass)){ 189132bdbfeSandi // make logininfo globally available 190f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 19132ed2b36SAndreas Gohr $secret = auth_cookiesalt(!$sticky); //bind non-sticky to session 192e940aea4SAndreas Gohr auth_setCookie($user,PMA_blowfish_encrypt($pass,$secret),$sticky); 193132bdbfeSandi return true; 194f3f0262cSandi }else{ 195f3f0262cSandi //invalid credentials - log off 196f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'],-1); 197f3f0262cSandi auth_logoff(); 198132bdbfeSandi return false; 199f3f0262cSandi } 200f3f0262cSandi }else{ 201132bdbfeSandi // read cookie information 202645c0a36SAndreas Gohr list($user,$sticky,$pass) = auth_getCookie(); 203132bdbfeSandi if($user && $pass){ 204132bdbfeSandi // we got a cookie - see if we can trust it 205fa7c70ffSAdrian Lang 206fa7c70ffSAdrian Lang // get session info 207fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 208132bdbfeSandi if(isset($session) && 2097172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2104c989037SChris Smith ($session['time'] >= time()-$conf['auth_security_timeout']) && 211132bdbfeSandi ($session['user'] == $user) && 212234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 213132bdbfeSandi ($session['buid'] == auth_browseruid()) ){ 214234ce57eSAndreas Gohr 215132bdbfeSandi // he has session, cookie and browser right - let him in 216132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 217132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 218132bdbfeSandi return true; 219132bdbfeSandi } 220f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 22132ed2b36SAndreas Gohr $secret = auth_cookiesalt(!$sticky); //bind non-sticky to session 222e940aea4SAndreas Gohr $pass = PMA_blowfish_decrypt($pass,$secret); 223f112c2faSAndreas Gohr return auth_login($user,$pass,$sticky,true); 224132bdbfeSandi } 225132bdbfeSandi } 226f3f0262cSandi //just to be sure 227883179a4SAndreas Gohr auth_logoff(true); 228132bdbfeSandi return false; 229f3f0262cSandi} 230132bdbfeSandi 231132bdbfeSandi/** 232f13fa892SAndreas Gohr * Checks if a given authentication token was stored in the session 233f13fa892SAndreas Gohr * 234f13fa892SAndreas Gohr * Will setup authentication data using data from the session if the 235f13fa892SAndreas Gohr * token is correct. Will exit with a 401 Status if not. 236f13fa892SAndreas Gohr * 237f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 238f13fa892SAndreas Gohr * @param string $token The authentication token 239f13fa892SAndreas Gohr * @return boolean true (or will exit on failure) 240f13fa892SAndreas Gohr */ 241f13fa892SAndreas Gohrfunction auth_validateToken($token){ 242f13fa892SAndreas Gohr if(!$token || $token != $_SESSION[DOKU_COOKIE]['auth']['token']){ 243f13fa892SAndreas Gohr // bad token 244f13fa892SAndreas Gohr header("HTTP/1.0 401 Unauthorized"); 245f13fa892SAndreas Gohr print 'Invalid auth token - maybe the session timed out'; 246f13fa892SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['token']); // no second chance 247f13fa892SAndreas Gohr exit; 248f13fa892SAndreas Gohr } 249f13fa892SAndreas Gohr // still here? trust the session data 250f13fa892SAndreas Gohr global $USERINFO; 251f13fa892SAndreas Gohr $_SERVER['REMOTE_USER'] = $_SESSION[DOKU_COOKIE]['auth']['user']; 252f13fa892SAndreas Gohr $USERINFO = $_SESSION[DOKU_COOKIE]['auth']['info']; 253f13fa892SAndreas Gohr return true; 254f13fa892SAndreas Gohr} 255f13fa892SAndreas Gohr 256f13fa892SAndreas Gohr/** 257f13fa892SAndreas Gohr * Create an auth token and store it in the session 258f13fa892SAndreas Gohr * 259f13fa892SAndreas Gohr * NOTE: this is completely unrelated to the getSecurityToken() function 260f13fa892SAndreas Gohr * 261f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 262f13fa892SAndreas Gohr * @return string The auth token 263f13fa892SAndreas Gohr */ 264f13fa892SAndreas Gohrfunction auth_createToken(){ 265f13fa892SAndreas Gohr $token = md5(mt_rand()); 26609c2d803SAndreas Gohr @session_start(); // reopen the session if needed 267f13fa892SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['token'] = $token; 26809c2d803SAndreas Gohr session_write_close(); 269f13fa892SAndreas Gohr return $token; 270f13fa892SAndreas Gohr} 271f13fa892SAndreas Gohr 272f13fa892SAndreas Gohr/** 273136ce040Sandi * Builds a pseudo UID from browser and IP data 274132bdbfeSandi * 275132bdbfeSandi * This is neither unique nor unfakable - still it adds some 276136ce040Sandi * security. Using the first part of the IP makes sure 277136ce040Sandi * proxy farms like AOLs are stil okay. 27815fae107Sandi * 27915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 28015fae107Sandi * 28115fae107Sandi * @return string a MD5 sum of various browser headers 282132bdbfeSandi */ 283132bdbfeSandifunction auth_browseruid(){ 2842f9daf16SAndreas Gohr $ip = clientIP(true); 285132bdbfeSandi $uid = ''; 286132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 287132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 288132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 289132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 2902f9daf16SAndreas Gohr $uid .= substr($ip,0,strpos($ip,'.')); 291132bdbfeSandi return md5($uid); 292132bdbfeSandi} 293132bdbfeSandi 294132bdbfeSandi/** 295132bdbfeSandi * Creates a random key to encrypt the password in cookies 29615fae107Sandi * 29715fae107Sandi * This function tries to read the password for encrypting 29898407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 29915fae107Sandi * if no such file is found a random key is created and 30015fae107Sandi * and stored in this file. 30115fae107Sandi * 30215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 30332ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 30415fae107Sandi * @return string 305132bdbfeSandi */ 30632ed2b36SAndreas Gohrfunction auth_cookiesalt($addsession=false){ 307132bdbfeSandi global $conf; 30898407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 309132bdbfeSandi $salt = io_readFile($file); 310132bdbfeSandi if(empty($salt)){ 311132bdbfeSandi $salt = uniqid(rand(),true); 312132bdbfeSandi io_saveFile($file,$salt); 313132bdbfeSandi } 31432ed2b36SAndreas Gohr if($addsession){ 31532ed2b36SAndreas Gohr $salt .= session_id(); 31632ed2b36SAndreas Gohr } 317132bdbfeSandi return $salt; 318f3f0262cSandi} 319f3f0262cSandi 320f3f0262cSandi/** 321883179a4SAndreas Gohr * Log out the current user 322883179a4SAndreas Gohr * 323f3f0262cSandi * This clears all authentication data and thus log the user 324883179a4SAndreas Gohr * off. It also clears session data. 32515fae107Sandi * 32615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 327883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 328f3f0262cSandi */ 329883179a4SAndreas Gohrfunction auth_logoff($keepbc=false){ 330f3f0262cSandi global $conf; 331f3f0262cSandi global $USERINFO; 3328b06d178Schris global $INFO, $ID; 3335298a619SAndreas Gohr global $auth; 33437065e65Sandi 335d4869846SAndreas Gohr // make sure the session is writable (it usually is) 336e9621d07SAndreas Gohr @session_start(); 337e9621d07SAndreas Gohr 338e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 339e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 340e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 341e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 342e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 343e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 344883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 345e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 34637065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 347f3f0262cSandi unset($_SERVER['REMOTE_USER']); 348132bdbfeSandi $USERINFO=null; //FIXME 349f5c6743cSAndreas Gohr 35073ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 351f5c6743cSAndreas Gohr if (version_compare(PHP_VERSION, '5.2.0', '>')) { 35273ab87deSGabriel Birke setcookie(DOKU_COOKIE,'',time()-600000,$cookieDir,'',($conf['securecookie'] && is_ssl()),true); 353f5c6743cSAndreas Gohr }else{ 35473ab87deSGabriel Birke setcookie(DOKU_COOKIE,'',time()-600000,$cookieDir,'',($conf['securecookie'] && is_ssl())); 355f5c6743cSAndreas Gohr } 3565298a619SAndreas Gohr 357880f62faSAndreas Gohr if($auth) $auth->logOff(); 358f3f0262cSandi} 359f3f0262cSandi 360f3f0262cSandi/** 361f8cc712eSAndreas Gohr * Check if a user is a manager 362f8cc712eSAndreas Gohr * 363f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 364f8cc712eSAndreas Gohr * user. 365f8cc712eSAndreas Gohr * 366f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 367f8cc712eSAndreas Gohr * 368f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 369f8cc712eSAndreas Gohr * @see auth_isadmin 370f8cc712eSAndreas Gohr * @param string user - Username 371f8cc712eSAndreas Gohr * @param array groups - List of groups the user is in 372f8cc712eSAndreas Gohr * @param bool adminonly - when true checks if user is admin 373f8cc712eSAndreas Gohr */ 374f8cc712eSAndreas Gohrfunction auth_ismanager($user=null,$groups=null,$adminonly=false){ 375f8cc712eSAndreas Gohr global $conf; 376f8cc712eSAndreas Gohr global $USERINFO; 377d752aedeSAndreas Gohr global $auth; 378f8cc712eSAndreas Gohr 379beca106aSAdrian Lang if (!$auth) return false; 380c66972f2SAdrian Lang if(is_null($user)) { 381c66972f2SAdrian Lang if (!isset($_SERVER['REMOTE_USER'])) { 382c66972f2SAdrian Lang return false; 383c66972f2SAdrian Lang } else { 384c66972f2SAdrian Lang $user = $_SERVER['REMOTE_USER']; 385c66972f2SAdrian Lang } 386c66972f2SAdrian Lang } 387d6dc956fSAndreas Gohr if(is_null($groups)){ 388d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 389e259aa79SAndreas Gohr } 390e259aa79SAndreas Gohr 391d6dc956fSAndreas Gohr // check superuser match 392d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'],$user, $groups)) return true; 393d6dc956fSAndreas Gohr if($adminonly) return false; 394e259aa79SAndreas Gohr // check managers 395d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'],$user, $groups)) return true; 39600ce12daSChris Smith 397f8cc712eSAndreas Gohr return false; 398f8cc712eSAndreas Gohr} 399f8cc712eSAndreas Gohr 400f8cc712eSAndreas Gohr/** 401f8cc712eSAndreas Gohr * Check if a user is admin 402f8cc712eSAndreas Gohr * 403f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 404f8cc712eSAndreas Gohr * 405f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 406f8cc712eSAndreas Gohr * 407f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 408f8cc712eSAndreas Gohr * @see auth_ismanager 409f8cc712eSAndreas Gohr */ 410f8cc712eSAndreas Gohrfunction auth_isadmin($user=null,$groups=null){ 411f8cc712eSAndreas Gohr return auth_ismanager($user,$groups,true); 412f8cc712eSAndreas Gohr} 413f8cc712eSAndreas Gohr 414d6dc956fSAndreas Gohr 415d6dc956fSAndreas Gohr/** 416d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 417d6dc956fSAndreas Gohr * users and groups to determine membership status 418d6dc956fSAndreas Gohr * 419d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 420d6dc956fSAndreas Gohr * 421d6dc956fSAndreas Gohr * @param $memberlist string commaseparated list of allowed users and groups 422d6dc956fSAndreas Gohr * @param $user string user to match against 423d6dc956fSAndreas Gohr * @param $groups array groups the user is member of 424d6dc956fSAndreas Gohr * @returns bool true for membership acknowledged 425d6dc956fSAndreas Gohr */ 426d6dc956fSAndreas Gohrfunction auth_isMember($memberlist,$user,array $groups){ 427d6dc956fSAndreas Gohr global $auth; 428d6dc956fSAndreas Gohr if (!$auth) return false; 429d6dc956fSAndreas Gohr 430d6dc956fSAndreas Gohr // clean user and groups 4314f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()){ 432d6dc956fSAndreas Gohr $user = utf8_strtolower($user); 433d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower',$groups); 434d6dc956fSAndreas Gohr } 435d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 436d6dc956fSAndreas Gohr $groups = array_map(array($auth,'cleanGroup'),$groups); 437d6dc956fSAndreas Gohr 438d6dc956fSAndreas Gohr // extract the memberlist 439d6dc956fSAndreas Gohr $members = explode(',',$memberlist); 440d6dc956fSAndreas Gohr $members = array_map('trim',$members); 441d6dc956fSAndreas Gohr $members = array_unique($members); 442d6dc956fSAndreas Gohr $members = array_filter($members); 443d6dc956fSAndreas Gohr 444d6dc956fSAndreas Gohr // compare cleaned values 445d6dc956fSAndreas Gohr foreach($members as $member){ 4464f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member); 447d6dc956fSAndreas Gohr if($member[0] == '@'){ 448d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member,1)); 449d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 450d6dc956fSAndreas Gohr }else{ 451d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 452d6dc956fSAndreas Gohr if($member == $user) return true; 453d6dc956fSAndreas Gohr } 454d6dc956fSAndreas Gohr } 455d6dc956fSAndreas Gohr 456d6dc956fSAndreas Gohr // still here? not a member! 457d6dc956fSAndreas Gohr return false; 458d6dc956fSAndreas Gohr} 459d6dc956fSAndreas Gohr 460f8cc712eSAndreas Gohr/** 46115fae107Sandi * Convinience function for auth_aclcheck() 46215fae107Sandi * 46315fae107Sandi * This checks the permissions for the current user 46415fae107Sandi * 46515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 46615fae107Sandi * 4671698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 46815fae107Sandi * @return int permission level 469f3f0262cSandi */ 470f3f0262cSandifunction auth_quickaclcheck($id){ 471f3f0262cSandi global $conf; 472f3f0262cSandi global $USERINFO; 473f3f0262cSandi # if no ACL is used always return upload rights 474f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 475f3f0262cSandi return auth_aclcheck($id,$_SERVER['REMOTE_USER'],$USERINFO['grps']); 476f3f0262cSandi} 477f3f0262cSandi 478f3f0262cSandi/** 479f3f0262cSandi * Returns the maximum rights a user has for 480f3f0262cSandi * the given ID or its namespace 48115fae107Sandi * 48215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 48315fae107Sandi * 4841698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 48515fae107Sandi * @param string $user Username 48615fae107Sandi * @param array $groups Array of groups the user is in 48715fae107Sandi * @return int permission level 488f3f0262cSandi */ 489f3f0262cSandifunction auth_aclcheck($id,$user,$groups){ 490f3f0262cSandi global $conf; 491f3f0262cSandi global $AUTH_ACL; 492d752aedeSAndreas Gohr global $auth; 493f3f0262cSandi 49485d03f68SAndreas Gohr // if no ACL is used always return upload rights 495f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 496beca106aSAdrian Lang if (!$auth) return AUTH_NONE; 497f3f0262cSandi 498074cf26bSandi //make sure groups is an array 499074cf26bSandi if(!is_array($groups)) $groups = array(); 500074cf26bSandi 50185d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 50285d03f68SAndreas Gohr if(auth_isadmin($user,$groups)) { return AUTH_ADMIN; } 50385d03f68SAndreas Gohr 504e259aa79SAndreas Gohr $ci = ''; 505e259aa79SAndreas Gohr if(!$auth->isCaseSensitive()) $ci = 'ui'; 506d752aedeSAndreas Gohr 507d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 508d752aedeSAndreas Gohr $groups = array_map(array($auth,'cleanGroup'),(array)$groups); 50985d03f68SAndreas Gohr $user = auth_nameencode($user); 51085d03f68SAndreas Gohr 5116c2bb100SAndreas Gohr //prepend groups with @ and nameencode 5122cd2db38Sandi $cnt = count($groups); 5132cd2db38Sandi for($i=0; $i<$cnt; $i++){ 5146c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 51510a76f6fSfrank } 51610a76f6fSfrank 517f3f0262cSandi $ns = getNS($id); 518f3f0262cSandi $perm = -1; 519f3f0262cSandi 52034aeb4afSAndreas Gohr if($user || count($groups)){ 521f3f0262cSandi //add ALL group 522f3f0262cSandi $groups[] = '@ALL'; 523f3f0262cSandi //add User 52434aeb4afSAndreas Gohr if($user) $groups[] = $user; 525f3f0262cSandi //build regexp 526f3f0262cSandi $regexp = join('|',$groups); 527f3f0262cSandi }else{ 528f3f0262cSandi $regexp = '@ALL'; 529f3f0262cSandi } 530f3f0262cSandi 531f3f0262cSandi //check exact match first 532e259aa79SAndreas Gohr $matches = preg_grep('/^'.preg_quote($id,'/').'\s+('.$regexp.')\s+/'.$ci,$AUTH_ACL); 533f3f0262cSandi if(count($matches)){ 534f3f0262cSandi foreach($matches as $match){ 535f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 536f3f0262cSandi $acl = preg_split('/\s+/',$match); 5378ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 538f3f0262cSandi if($acl[2] > $perm){ 539f3f0262cSandi $perm = $acl[2]; 540f3f0262cSandi } 541f3f0262cSandi } 542f3f0262cSandi if($perm > -1){ 543f3f0262cSandi //we had a match - return it 544f3f0262cSandi return $perm; 545f3f0262cSandi } 546f3f0262cSandi } 547f3f0262cSandi 548f3f0262cSandi //still here? do the namespace checks 549f3f0262cSandi if($ns){ 5503e304b55SMichael Hamann $path = $ns.':*'; 551f3f0262cSandi }else{ 5523e304b55SMichael Hamann $path = '*'; //root document 553f3f0262cSandi } 554f3f0262cSandi 555f3f0262cSandi do{ 5563e304b55SMichael Hamann $matches = preg_grep('/^'.preg_quote($path,'/').'\s+('.$regexp.')\s+/'.$ci,$AUTH_ACL); 557f3f0262cSandi if(count($matches)){ 558f3f0262cSandi foreach($matches as $match){ 559f3f0262cSandi $match = preg_replace('/#.*$/','',$match); //ignore comments 560f3f0262cSandi $acl = preg_split('/\s+/',$match); 5618ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 562f3f0262cSandi if($acl[2] > $perm){ 563f3f0262cSandi $perm = $acl[2]; 564f3f0262cSandi } 565f3f0262cSandi } 566f3f0262cSandi //we had a match - return it 567f3f0262cSandi return $perm; 568f3f0262cSandi } 569f3f0262cSandi 570f3f0262cSandi //get next higher namespace 571f3f0262cSandi $ns = getNS($ns); 572f3f0262cSandi 5733e304b55SMichael Hamann if($path != '*'){ 5743e304b55SMichael Hamann $path = $ns.':*'; 5753e304b55SMichael Hamann if($path == ':*') $path = '*'; 576f3f0262cSandi }else{ 577f3f0262cSandi //we did this already 578f3f0262cSandi //looks like there is something wrong with the ACL 579f3f0262cSandi //break here 580d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 581d5ce66f6SAndreas Gohr return AUTH_NONE; 582f3f0262cSandi } 583f3f0262cSandi }while(1); //this should never loop endless 58452a5af8dSandi 58552a5af8dSandi //still here? return no permissions 58652a5af8dSandi return AUTH_NONE; 587f3f0262cSandi} 588f3f0262cSandi 589f3f0262cSandi/** 5906c2bb100SAndreas Gohr * Encode ASCII special chars 5916c2bb100SAndreas Gohr * 5926c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 5936c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 5946c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 5956c2bb100SAndreas Gohr * urlencoding!). 5966c2bb100SAndreas Gohr * 5976c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 5986c2bb100SAndreas Gohr * 5996c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 6006c2bb100SAndreas Gohr * @see rawurldecode() 6016c2bb100SAndreas Gohr */ 602e838fc2eSAndreas Gohrfunction auth_nameencode($name,$skip_group=false){ 603a424cd8eSchris global $cache_authname; 604a424cd8eSchris $cache =& $cache_authname; 60531784267SAndreas Gohr $name = (string) $name; 606a424cd8eSchris 60780601d26SAndreas Gohr // never encode wildcard FS#1955 60880601d26SAndreas Gohr if($name == '%USER%') return $name; 60980601d26SAndreas Gohr 610a424cd8eSchris if (!isset($cache[$name][$skip_group])) { 611e838fc2eSAndreas Gohr if($skip_group && $name{0} =='@'){ 612a424cd8eSchris $cache[$name][$skip_group] = '@'.preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 6131a9ae8e5SAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))",substr($name,1)); 614e838fc2eSAndreas Gohr }else{ 615a424cd8eSchris $cache[$name][$skip_group] = preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 6161a9ae8e5SAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))",$name); 617e838fc2eSAndreas Gohr } 6186c2bb100SAndreas Gohr } 6196c2bb100SAndreas Gohr 620a424cd8eSchris return $cache[$name][$skip_group]; 621a424cd8eSchris} 622a424cd8eSchris 6236c2bb100SAndreas Gohr/** 624f3f0262cSandi * Create a pronouncable password 625f3f0262cSandi * 62615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 62715fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 62815fae107Sandi * 62915fae107Sandi * @return string pronouncable password 630f3f0262cSandi */ 631f3f0262cSandifunction auth_pwgen(){ 632f3f0262cSandi $pw = ''; 633f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 634f3f0262cSandi $v = 'aeiou'; //vowels 635f3f0262cSandi $a = $c.$v; //both 636f3f0262cSandi 637f3f0262cSandi //use two syllables... 638f3f0262cSandi for($i=0;$i < 2; $i++){ 639f3f0262cSandi $pw .= $c[rand(0, strlen($c)-1)]; 640f3f0262cSandi $pw .= $v[rand(0, strlen($v)-1)]; 641f3f0262cSandi $pw .= $a[rand(0, strlen($a)-1)]; 642f3f0262cSandi } 643f3f0262cSandi //... and add a nice number 644f3f0262cSandi $pw .= rand(10,99); 645f3f0262cSandi 646f3f0262cSandi return $pw; 647f3f0262cSandi} 648f3f0262cSandi 649f3f0262cSandi/** 650f3f0262cSandi * Sends a password to the given user 651f3f0262cSandi * 65215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 65315fae107Sandi * 65415fae107Sandi * @return bool true on success 655f3f0262cSandi */ 656f3f0262cSandifunction auth_sendPassword($user,$password){ 657f3f0262cSandi global $conf; 658f3f0262cSandi global $lang; 659cd52f92dSchris global $auth; 660beca106aSAdrian Lang if (!$auth) return false; 661cd52f92dSchris 662f3f0262cSandi $hdrs = ''; 663d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 664cd52f92dSchris $userinfo = $auth->getUserData($user); 665f3f0262cSandi 66687ddda95Sandi if(!$userinfo['mail']) return false; 667f3f0262cSandi 668f3f0262cSandi $text = rawLocale('password'); 669ed7b5f09Sandi $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 67087ddda95Sandi $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 671f3f0262cSandi $text = str_replace('@LOGIN@',$user,$text); 672f3f0262cSandi $text = str_replace('@PASSWORD@',$password,$text); 673f3f0262cSandi $text = str_replace('@TITLE@',$conf['title'],$text); 674f3f0262cSandi 6759a2d7c4eSlupo49 if(empty($conf['mailprefix'])) { 6769a2d7c4eSlupo49 $subject = $lang['regpwmail']; 6779a2d7c4eSlupo49 } else { 6789a2d7c4eSlupo49 $subject = '['.$conf['mailprefix'].'] '.$lang['regpwmail']; 6799a2d7c4eSlupo49 } 6809a2d7c4eSlupo49 68144f669e9Sandi return mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 6829a2d7c4eSlupo49 $subject, 68344f669e9Sandi $text, 68444f669e9Sandi $conf['mailfrom']); 685f3f0262cSandi} 686f3f0262cSandi 687f3f0262cSandi/** 68815fae107Sandi * Register a new user 689f3f0262cSandi * 69015fae107Sandi * This registers a new user - Data is read directly from $_POST 69115fae107Sandi * 69215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 69315fae107Sandi * 69415fae107Sandi * @return bool true on success, false on any error 695f3f0262cSandi */ 696f3f0262cSandifunction register(){ 697f3f0262cSandi global $lang; 698eb5d07e4Sjan global $conf; 699cd52f92dSchris global $auth; 700f3f0262cSandi 701f3f0262cSandi if(!$_POST['save']) return false; 7023a48618aSAnika Henke if(!actionOK('register')) return false; 703640145a5Sandi 704f3f0262cSandi //clean username 705d752aedeSAndreas Gohr $_POST['login'] = trim($auth->cleanUser($_POST['login'])); 706d752aedeSAndreas Gohr 707f3f0262cSandi //clean fullname and email 70854f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname'])); 70954f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email'])); 710f3f0262cSandi 711f3f0262cSandi if( empty($_POST['login']) || 712f3f0262cSandi empty($_POST['fullname']) || 713f3f0262cSandi empty($_POST['email']) ){ 714f3f0262cSandi msg($lang['regmissing'],-1); 715f3f0262cSandi return false; 716f3f0262cSandi } 717f3f0262cSandi 718cab2716aSmatthias.grimm if ($conf['autopasswd']) { 719cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 720cab2716aSmatthias.grimm } elseif (empty($_POST['pass']) || 721cab2716aSmatthias.grimm empty($_POST['passchk'])) { 722bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 723cab2716aSmatthias.grimm return false; 724cab2716aSmatthias.grimm } elseif ($_POST['pass'] != $_POST['passchk']) { 725bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 726cab2716aSmatthias.grimm return false; 727cab2716aSmatthias.grimm } else { 728cab2716aSmatthias.grimm $pass = $_POST['pass']; // accept checked and valid password 729cab2716aSmatthias.grimm } 730cab2716aSmatthias.grimm 731f3f0262cSandi //check mail 73244f669e9Sandi if(!mail_isvalid($_POST['email'])){ 733f3f0262cSandi msg($lang['regbadmail'],-1); 734f3f0262cSandi return false; 735f3f0262cSandi } 736f3f0262cSandi 737f3f0262cSandi //okay try to create the user 7387d3c8d42SGabriel Birke if(!$auth->triggerUserMod('create', array($_POST['login'],$pass,$_POST['fullname'],$_POST['email']))){ 739f3f0262cSandi msg($lang['reguexists'],-1); 740f3f0262cSandi return false; 741f3f0262cSandi } 742f3f0262cSandi 74302a498e7Schris // create substitutions for use in notification email 74402a498e7Schris $substitutions = array( 74502a498e7Schris 'NEWUSER' => $_POST['login'], 74602a498e7Schris 'NEWNAME' => $_POST['fullname'], 74702a498e7Schris 'NEWEMAIL' => $_POST['email'], 74802a498e7Schris ); 74902a498e7Schris 750cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 751cab2716aSmatthias.grimm msg($lang['regsuccess2'],1); 75202a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 753cab2716aSmatthias.grimm return true; 754cab2716aSmatthias.grimm } 755cab2716aSmatthias.grimm 756cab2716aSmatthias.grimm // autogenerated password? then send him the password 757f3f0262cSandi if (auth_sendPassword($_POST['login'],$pass)){ 758f3f0262cSandi msg($lang['regsuccess'],1); 75902a498e7Schris notify('', 'register', '', $_POST['login'], false, $substitutions); 760f3f0262cSandi return true; 761f3f0262cSandi }else{ 762f3f0262cSandi msg($lang['regmailfail'],-1); 763f3f0262cSandi return false; 764f3f0262cSandi } 765f3f0262cSandi} 766f3f0262cSandi 76710a76f6fSfrank/** 7688b06d178Schris * Update user profile 7698b06d178Schris * 7708b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 7718b06d178Schris */ 7728b06d178Schrisfunction updateprofile() { 7738b06d178Schris global $conf; 7748b06d178Schris global $INFO; 7758b06d178Schris global $lang; 776cd52f92dSchris global $auth; 7778b06d178Schris 778bb4866bdSchris if(empty($_POST['save'])) return false; 7791b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 7808b06d178Schris 7813a48618aSAnika Henke if(!actionOK('profile')) { 7828b06d178Schris msg($lang['profna'],-1); 7838b06d178Schris return false; 7848b06d178Schris } 7858b06d178Schris 7868b06d178Schris if ($_POST['newpass'] != $_POST['passchk']) { 7878b06d178Schris msg($lang['regbadpass'], -1); // complain about misspelled passwords 7888b06d178Schris return false; 7898b06d178Schris } 7908b06d178Schris 7918b06d178Schris //clean fullname and email 79254f0e6eaSAndreas Gohr $_POST['fullname'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['fullname'])); 79354f0e6eaSAndreas Gohr $_POST['email'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/','',$_POST['email'])); 7948b06d178Schris 7954369edafSAndy Webber if ((empty($_POST['fullname']) && $auth->canDo('modName')) || 7964369edafSAndy Webber (empty($_POST['email']) && $auth->canDo('modMail'))) { 7978b06d178Schris msg($lang['profnoempty'],-1); 7988b06d178Schris return false; 7998b06d178Schris } 8008b06d178Schris 8014369edafSAndy Webber if (!mail_isvalid($_POST['email']) && $auth->canDo('modMail')){ 8028b06d178Schris msg($lang['regbadmail'],-1); 8038b06d178Schris return false; 8048b06d178Schris } 8058b06d178Schris 8064c21b7eeSAndreas Gohr if ($_POST['fullname'] != $INFO['userinfo']['name'] && $auth->canDo('modName')) $changes['name'] = $_POST['fullname']; 8074c21b7eeSAndreas Gohr if ($_POST['email'] != $INFO['userinfo']['mail'] && $auth->canDo('modMail')) $changes['mail'] = $_POST['email']; 808cf626a62SAndreas Gohr if (!empty($_POST['newpass']) && $auth->canDo('modPass')) $changes['pass'] = $_POST['newpass']; 8094c21b7eeSAndreas Gohr 8108b06d178Schris if (!count($changes)) { 8118b06d178Schris msg($lang['profnochange'], -1); 8128b06d178Schris return false; 8138b06d178Schris } 8148b06d178Schris 8158b06d178Schris if ($conf['profileconfirm']) { 816df466c7aSAndreas Gohr if (!$auth->checkPass($_SERVER['REMOTE_USER'], $_POST['oldpass'])) { 8178b06d178Schris msg($lang['badlogin'],-1); 8188b06d178Schris return false; 8198b06d178Schris } 8208b06d178Schris } 8218b06d178Schris 822a0b5b007SChris Smith if ($result = $auth->triggerUserMod('modify', array($_SERVER['REMOTE_USER'], $changes))) { 823a0b5b007SChris Smith // update cookie and session with the changed data 82432ed2b36SAndreas Gohr if ($changes['pass']){ 82532ed2b36SAndreas Gohr list($user,$sticky,$pass) = auth_getCookie(); 82632ed2b36SAndreas Gohr $pass = PMA_blowfish_encrypt($changes['pass'],auth_cookiesalt(!$sticky)); 827a0b5b007SChris Smith auth_setCookie($_SERVER['REMOTE_USER'],$pass,(bool)$sticky); 82832ed2b36SAndreas Gohr } 82925b2a98cSMichael Klier return true; 830a0b5b007SChris Smith } 8318b06d178Schris} 8328b06d178Schris 8338b06d178Schris/** 8348b06d178Schris * Send a new password 8358b06d178Schris * 8361d5856cfSAndreas Gohr * This function handles both phases of the password reset: 8371d5856cfSAndreas Gohr * 8381d5856cfSAndreas Gohr * - handling the first request of password reset 8391d5856cfSAndreas Gohr * - validating the password reset auth token 8401d5856cfSAndreas Gohr * 8418b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 8428b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 8431d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8448b06d178Schris * 8458b06d178Schris * @return bool true on success, false on any error 8468b06d178Schris */ 8478b06d178Schrisfunction act_resendpwd(){ 8488b06d178Schris global $lang; 8498b06d178Schris global $conf; 850cd52f92dSchris global $auth; 8518b06d178Schris 8523a48618aSAnika Henke if(!actionOK('resendpwd')) { 8538b06d178Schris msg($lang['resendna'],-1); 8548b06d178Schris return false; 8558b06d178Schris } 8568b06d178Schris 8571d5856cfSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/','',$_REQUEST['pwauth']); 8588b06d178Schris 8591d5856cfSAndreas Gohr if($token){ 8601d5856cfSAndreas Gohr // we're in token phase 8611d5856cfSAndreas Gohr 8621d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 8631d5856cfSAndreas Gohr if(!@file_exists($tfile)){ 8641d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'],-1); 8651d5856cfSAndreas Gohr return false; 8661d5856cfSAndreas Gohr } 8671d5856cfSAndreas Gohr $user = io_readfile($tfile); 8681d5856cfSAndreas Gohr @unlink($tfile); 869cd52f92dSchris $userinfo = $auth->getUserData($user); 8708b06d178Schris if(!$userinfo['mail']) { 8718b06d178Schris msg($lang['resendpwdnouser'], -1); 8728b06d178Schris return false; 8738b06d178Schris } 8748b06d178Schris 8758b06d178Schris $pass = auth_pwgen(); 8767d3c8d42SGabriel Birke if (!$auth->triggerUserMod('modify', array($user,array('pass' => $pass)))) { 8778b06d178Schris msg('error modifying user data',-1); 8788b06d178Schris return false; 8798b06d178Schris } 8808b06d178Schris 8818b06d178Schris if (auth_sendPassword($user,$pass)) { 8828b06d178Schris msg($lang['resendpwdsuccess'],1); 8838b06d178Schris } else { 8848b06d178Schris msg($lang['regmailfail'],-1); 8858b06d178Schris } 8868b06d178Schris return true; 8871d5856cfSAndreas Gohr 8881d5856cfSAndreas Gohr } else { 8891d5856cfSAndreas Gohr // we're in request phase 8901d5856cfSAndreas Gohr 8911d5856cfSAndreas Gohr if(!$_POST['save']) return false; 8921d5856cfSAndreas Gohr 8931d5856cfSAndreas Gohr if (empty($_POST['login'])) { 8941d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 8951d5856cfSAndreas Gohr return false; 8961d5856cfSAndreas Gohr } else { 897d752aedeSAndreas Gohr $user = trim($auth->cleanUser($_POST['login'])); 8981d5856cfSAndreas Gohr } 8991d5856cfSAndreas Gohr 9001d5856cfSAndreas Gohr $userinfo = $auth->getUserData($user); 9011d5856cfSAndreas Gohr if(!$userinfo['mail']) { 9021d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 9031d5856cfSAndreas Gohr return false; 9041d5856cfSAndreas Gohr } 9051d5856cfSAndreas Gohr 9061d5856cfSAndreas Gohr // generate auth token 9071d5856cfSAndreas Gohr $token = md5(auth_cookiesalt().$user); //secret but user based 9081d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 9091d5856cfSAndreas Gohr $url = wl('',array('do'=>'resendpwd','pwauth'=>$token),true,'&'); 9101d5856cfSAndreas Gohr 9111d5856cfSAndreas Gohr io_saveFile($tfile,$user); 9121d5856cfSAndreas Gohr 9131d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 9141d5856cfSAndreas Gohr $text = str_replace('@DOKUWIKIURL@',DOKU_URL,$text); 9151d5856cfSAndreas Gohr $text = str_replace('@FULLNAME@',$userinfo['name'],$text); 9161d5856cfSAndreas Gohr $text = str_replace('@LOGIN@',$user,$text); 9171d5856cfSAndreas Gohr $text = str_replace('@TITLE@',$conf['title'],$text); 9181d5856cfSAndreas Gohr $text = str_replace('@CONFIRM@',$url,$text); 9191d5856cfSAndreas Gohr 9209a2d7c4eSlupo49 if(empty($conf['mailprefix'])) { 9219a2d7c4eSlupo49 $subject = $lang['regpwmail']; 9229a2d7c4eSlupo49 } else { 9239a2d7c4eSlupo49 $subject = '['.$conf['mailprefix'].'] '.$lang['regpwmail']; 9249a2d7c4eSlupo49 } 9259a2d7c4eSlupo49 9261d5856cfSAndreas Gohr if(mail_send($userinfo['name'].' <'.$userinfo['mail'].'>', 9279a2d7c4eSlupo49 $subject, 9281d5856cfSAndreas Gohr $text, 9291d5856cfSAndreas Gohr $conf['mailfrom'])){ 9301d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'],1); 9311d5856cfSAndreas Gohr }else{ 9321d5856cfSAndreas Gohr msg($lang['regmailfail'],-1); 9331d5856cfSAndreas Gohr } 9341d5856cfSAndreas Gohr return true; 9351d5856cfSAndreas Gohr } 9361d5856cfSAndreas Gohr 9371d5856cfSAndreas Gohr return false; // never reached 9388b06d178Schris} 9398b06d178Schris 9408b06d178Schris/** 941b0855b11Sandi * Encrypts a password using the given method and salt 942b0855b11Sandi * 943b0855b11Sandi * If the selected method needs a salt and none was given, a random one 944b0855b11Sandi * is chosen. 945b0855b11Sandi * 946b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 947b0855b11Sandi * @return string The crypted password 948b0855b11Sandi */ 949577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear,$method='',$salt=null){ 950b0855b11Sandi global $conf; 951b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 95210a76f6fSfrank 9533a0a2d05SAndreas Gohr $pass = new PassHash(); 9543a0a2d05SAndreas Gohr $call = 'hash_'.$method; 955b0855b11Sandi 9563a0a2d05SAndreas Gohr if(!method_exists($pass,$call)){ 957b0855b11Sandi msg("Unsupported crypt method $method",-1); 9583a0a2d05SAndreas Gohr return false; 959b0855b11Sandi } 9603a0a2d05SAndreas Gohr 9613a0a2d05SAndreas Gohr return $pass->$call($clear,$salt); 962b0855b11Sandi} 963b0855b11Sandi 964b0855b11Sandi/** 965b0855b11Sandi * Verifies a cleartext password against a crypted hash 966b0855b11Sandi * 967b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 968b0855b11Sandi * @return bool 969b0855b11Sandi */ 970b0855b11Sandifunction auth_verifyPassword($clear,$crypt){ 9713a0a2d05SAndreas Gohr $pass = new PassHash(); 9723a0a2d05SAndreas Gohr return $pass->verify_hash($clear,$crypt); 973b0855b11Sandi} 974340756e4Sandi 975a0b5b007SChris Smith/** 976a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 977a0b5b007SChris Smith * 978a0b5b007SChris Smith * @param string $user username 979a0b5b007SChris Smith * @param string $pass encrypted password 980a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 981a0b5b007SChris Smith */ 982a0b5b007SChris Smithfunction auth_setCookie($user,$pass,$sticky) { 983a0b5b007SChris Smith global $conf; 984a0b5b007SChris Smith global $auth; 98579d00841SOliver Geisen global $USERINFO; 986a0b5b007SChris Smith 987beca106aSAdrian Lang if (!$auth) return false; 988a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 989a0b5b007SChris Smith 990a0b5b007SChris Smith // set cookie 991645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 99273ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 993c66972f2SAdrian Lang $time = $sticky ? (time()+60*60*24*365) : 0; //one year 994a0b5b007SChris Smith if (version_compare(PHP_VERSION, '5.2.0', '>')) { 99573ab87deSGabriel Birke setcookie(DOKU_COOKIE,$cookie,$time,$cookieDir,'',($conf['securecookie'] && is_ssl()),true); 996a0b5b007SChris Smith }else{ 99773ab87deSGabriel Birke setcookie(DOKU_COOKIE,$cookie,$time,$cookieDir,'',($conf['securecookie'] && is_ssl())); 998a0b5b007SChris Smith } 999a0b5b007SChris Smith // set session 1000a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1001234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1002a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1003a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1004a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1005a0b5b007SChris Smith} 1006a0b5b007SChris Smith 1007645c0a36SAndreas Gohr/** 1008645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1009645c0a36SAndreas Gohr * 1010645c0a36SAndreas Gohr * @returns array 1011645c0a36SAndreas Gohr */ 1012645c0a36SAndreas Gohrfunction auth_getCookie(){ 1013c66972f2SAdrian Lang if (!isset($_COOKIE[DOKU_COOKIE])) { 1014c66972f2SAdrian Lang return array(null, null, null); 1015c66972f2SAdrian Lang } 1016645c0a36SAndreas Gohr list($user,$sticky,$pass) = explode('|',$_COOKIE[DOKU_COOKIE],3); 1017645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1018645c0a36SAndreas Gohr $pass = base64_decode($pass); 1019645c0a36SAndreas Gohr $user = base64_decode($user); 1020645c0a36SAndreas Gohr return array($user,$sticky,$pass); 1021645c0a36SAndreas Gohr} 1022645c0a36SAndreas Gohr 1023e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1024