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 32ab5d26daSAndreas Gohr * @triggers AUTH_LOGIN_CHECK 33ab5d26daSAndreas Gohr * @return bool 3416905344SAndreas Gohr */ 3516905344SAndreas Gohrfunction auth_setup() { 36742c66f8Schris global $conf; 37*93a7873eSAndreas Gohr /* @var DokuWiki_Auth_Plugin $auth */ 3803c4aec3Schris global $auth; 39bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 40bcc94b2cSAndreas Gohr global $INPUT; 419a9714acSDominik Eckelmann global $AUTH_ACL; 429a9714acSDominik Eckelmann global $lang; 43c8f80b4eSAndreas Gohr global $config_cascade; 449c29eea5SJan Schumann global $plugin_controller; 459a9714acSDominik Eckelmann $AUTH_ACL = array(); 4603c4aec3Schris 4716905344SAndreas Gohr if(!$conf['useacl']) return false; 4816905344SAndreas Gohr 499c29eea5SJan Schumann // try to load auth backend from plugins 509c29eea5SJan Schumann foreach ($plugin_controller->getList('auth') as $plugin) { 519c29eea5SJan Schumann if ($conf['authtype'] === $plugin) { 52f4476bd9SJan Schumann $auth = $plugin_controller->load('auth', $plugin); 539c29eea5SJan Schumann break; 549c29eea5SJan Schumann } 559c29eea5SJan Schumann } 568b06d178Schris 57*93a7873eSAndreas Gohr if(!$auth) return false; 588b06d178Schris 59f4476bd9SJan Schumann if ($auth && $auth->success == false) { 600f4f4adfSAndreas Gohr // degrade to unauthenticated user 61d2dde4ebSMatthias Grimm unset($auth); 620f4f4adfSAndreas Gohr auth_logoff(); 63cd52f92dSchris msg($lang['authtempfail'], -1); 64d2dde4ebSMatthias Grimm } 6516905344SAndreas Gohr 6616905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 67bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 68bcc94b2cSAndreas Gohr if(!$conf['rememberme']) $INPUT->set('r', false); 69bbbd6568SAndreas Gohr 70b2665af7SMichael Hamann // handle renamed HTTP_AUTHORIZATION variable (can happen when a fix like 71b2665af7SMichael Hamann // the one presented at 72b2665af7SMichael Hamann // http://www.besthostratings.com/articles/http-auth-php-cgi.html is used 73b2665af7SMichael Hamann // for enabling HTTP authentication with CGI/SuExec) 74b2665af7SMichael Hamann if(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) 75b2665af7SMichael Hamann $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; 76528ddc7cSAndreas Gohr // streamline HTTP auth credentials (IIS/rewrite -> mod_php) 7706156f3cSAndreas Gohr if(isset($_SERVER['HTTP_AUTHORIZATION'])) { 78528ddc7cSAndreas Gohr list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = 79528ddc7cSAndreas Gohr explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); 80528ddc7cSAndreas Gohr } 81528ddc7cSAndreas Gohr 821e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 83bcc94b2cSAndreas Gohr if(!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])) { 84bcc94b2cSAndreas Gohr $INPUT->set('u', $_SERVER['PHP_AUTH_USER']); 85bcc94b2cSAndreas Gohr $INPUT->set('p', $_SERVER['PHP_AUTH_PW']); 86bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 871e8c9c90SAndreas Gohr } 881e8c9c90SAndreas Gohr 89191bb90aSAndreas Gohr // apply cleaning 90*93a7873eSAndreas Gohr if (true === $auth->success) { 91191bb90aSAndreas Gohr $_REQUEST['u'] = $auth->cleanUser($_REQUEST['u']); 92f4476bd9SJan Schumann } 93191bb90aSAndreas Gohr 94bcc94b2cSAndreas Gohr if($INPUT->str('authtok')) { 95f13fa892SAndreas Gohr // when an authentication token is given, trust the session 96bcc94b2cSAndreas Gohr auth_validateToken($INPUT->str('authtok')); 97f13fa892SAndreas Gohr } elseif(!is_null($auth) && $auth->canDo('external')) { 98f13fa892SAndreas Gohr // external trust mechanism in place 99bcc94b2cSAndreas Gohr $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 100f5cb575dSAndreas Gohr } else { 1016080c584SRobin Gareus $evdata = array( 102bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 103bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 104bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 105bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('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(); 112ab5d26daSAndreas Gohr 113ab5d26daSAndreas Gohr return true; 11475c93b77SAndreas Gohr} 11575c93b77SAndreas Gohr 11675c93b77SAndreas Gohr/** 11775c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 11875c93b77SAndreas Gohr * 11975c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 120ab5d26daSAndreas Gohr * @return array 12175c93b77SAndreas Gohr */ 12275c93b77SAndreas Gohrfunction auth_loadACL() { 12375c93b77SAndreas Gohr global $config_cascade; 124b78bf706Sromain global $USERINFO; 12575c93b77SAndreas Gohr 12675c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 12775c93b77SAndreas Gohr 12875c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 12975c93b77SAndreas Gohr 130191bb90aSAndreas Gohr //support user wildcard 13132e82180SAndreas Gohr $out = array(); 1329ce556d2SAndreas Gohr foreach($acl as $line) { 1339ce556d2SAndreas Gohr $line = trim($line); 1349ce556d2SAndreas Gohr if($line{0} == '#') continue; 1359ce556d2SAndreas Gohr list($id,$rest) = preg_split('/\s+/',$line,2); 13632e82180SAndreas Gohr 1379ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 1389ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 139b78bf706Sromain $nid = str_replace('%GROUP%',cleanID($grp),$id); 14032e82180SAndreas Gohr $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest); 14132e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 142b78bf706Sromain } 14332e82180SAndreas Gohr } else { 14475c93b77SAndreas Gohr $id = str_replace('%USER%',cleanID($_SERVER['REMOTE_USER']),$id); 14575c93b77SAndreas Gohr $rest = str_replace('%USER%',auth_nameencode($_SERVER['REMOTE_USER']),$rest); 14632e82180SAndreas Gohr $out[] = "$id\t$rest"; 147a8fe108bSGuy Brand } 14811799630Sandi } 1499ce556d2SAndreas Gohr 15032e82180SAndreas Gohr return $out; 151f3f0262cSandi} 152f3f0262cSandi 153ab5d26daSAndreas Gohr/** 154ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 155ab5d26daSAndreas Gohr * 156ab5d26daSAndreas Gohr * @param $evdata 157ab5d26daSAndreas Gohr * @return bool 158ab5d26daSAndreas Gohr */ 159b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 160ab5d26daSAndreas Gohr return auth_login( 161ab5d26daSAndreas Gohr $evdata['user'], 162b5ee21aaSAdrian Lang $evdata['password'], 163b5ee21aaSAdrian Lang $evdata['sticky'], 164ab5d26daSAndreas Gohr $evdata['silent'] 165ab5d26daSAndreas Gohr ); 166b5ee21aaSAdrian Lang} 167b5ee21aaSAdrian Lang 168f3f0262cSandi/** 169f3f0262cSandi * This tries to login the user based on the sent auth credentials 170f3f0262cSandi * 171f3f0262cSandi * The authentication works like this: if a username was given 17215fae107Sandi * a new login is assumed and user/password are checked. If they 17315fae107Sandi * are correct the password is encrypted with blowfish and stored 17415fae107Sandi * together with the username in a cookie - the same info is stored 17515fae107Sandi * in the session, too. Additonally a browserID is stored in the 17615fae107Sandi * session. 17715fae107Sandi * 17815fae107Sandi * If no username was given the cookie is checked: if the username, 17915fae107Sandi * crypted password and browserID match between session and cookie 18015fae107Sandi * no further testing is done and the user is accepted 18115fae107Sandi * 18215fae107Sandi * If a cookie was found but no session info was availabe the 183136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 18415fae107Sandi * together with username rechecked by calling this function again. 185f3f0262cSandi * 186f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 187f3f0262cSandi * are set. 18815fae107Sandi * 18915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 19015fae107Sandi * 19115fae107Sandi * @param string $user Username 19215fae107Sandi * @param string $pass Cleartext Password 19315fae107Sandi * @param bool $sticky Cookie should not expire 194f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 19515fae107Sandi * @return bool true on successful auth 196f3f0262cSandi */ 197f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 198f3f0262cSandi global $USERINFO; 199f3f0262cSandi global $conf; 200f3f0262cSandi global $lang; 201ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 202cd52f92dSchris global $auth; 203ab5d26daSAndreas Gohr 204132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 205f3f0262cSandi 206beca106aSAdrian Lang if(!$auth) return false; 207beca106aSAdrian Lang 208bbbd6568SAndreas Gohr if(!empty($user)) { 209132bdbfeSandi //usual login 210cd52f92dSchris if($auth->checkPass($user, $pass)) { 211132bdbfeSandi // make logininfo globally available 212f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 21332ed2b36SAndreas Gohr $secret = auth_cookiesalt(!$sticky); //bind non-sticky to session 214e940aea4SAndreas Gohr auth_setCookie($user, PMA_blowfish_encrypt($pass, $secret), $sticky); 215132bdbfeSandi return true; 216f3f0262cSandi } else { 217f3f0262cSandi //invalid credentials - log off 218f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'], -1); 219f3f0262cSandi auth_logoff(); 220132bdbfeSandi return false; 221f3f0262cSandi } 222f3f0262cSandi } else { 223132bdbfeSandi // read cookie information 224645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 225132bdbfeSandi if($user && $pass) { 226132bdbfeSandi // we got a cookie - see if we can trust it 227fa7c70ffSAdrian Lang 228fa7c70ffSAdrian Lang // get session info 229fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 230132bdbfeSandi if(isset($session) && 2317172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2324c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 233132bdbfeSandi ($session['user'] == $user) && 234234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 235ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 236ab5d26daSAndreas Gohr ) { 237234ce57eSAndreas Gohr 238132bdbfeSandi // he has session, cookie and browser right - let him in 239132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 240132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 241132bdbfeSandi return true; 242132bdbfeSandi } 243f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 24432ed2b36SAndreas Gohr $secret = auth_cookiesalt(!$sticky); //bind non-sticky to session 245e940aea4SAndreas Gohr $pass = PMA_blowfish_decrypt($pass, $secret); 246f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 247132bdbfeSandi } 248132bdbfeSandi } 249f3f0262cSandi //just to be sure 250883179a4SAndreas Gohr auth_logoff(true); 251132bdbfeSandi return false; 252f3f0262cSandi} 253132bdbfeSandi 254132bdbfeSandi/** 255f13fa892SAndreas Gohr * Checks if a given authentication token was stored in the session 256f13fa892SAndreas Gohr * 257f13fa892SAndreas Gohr * Will setup authentication data using data from the session if the 258f13fa892SAndreas Gohr * token is correct. Will exit with a 401 Status if not. 259f13fa892SAndreas Gohr * 260f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 261f13fa892SAndreas Gohr * @param string $token The authentication token 262f13fa892SAndreas Gohr * @return boolean true (or will exit on failure) 263f13fa892SAndreas Gohr */ 264f13fa892SAndreas Gohrfunction auth_validateToken($token) { 265f13fa892SAndreas Gohr if(!$token || $token != $_SESSION[DOKU_COOKIE]['auth']['token']) { 266f13fa892SAndreas Gohr // bad token 267f13fa892SAndreas Gohr header("HTTP/1.0 401 Unauthorized"); 268f13fa892SAndreas Gohr print 'Invalid auth token - maybe the session timed out'; 269f13fa892SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['token']); // no second chance 270f13fa892SAndreas Gohr exit; 271f13fa892SAndreas Gohr } 272f13fa892SAndreas Gohr // still here? trust the session data 273f13fa892SAndreas Gohr global $USERINFO; 274f13fa892SAndreas Gohr $_SERVER['REMOTE_USER'] = $_SESSION[DOKU_COOKIE]['auth']['user']; 275f13fa892SAndreas Gohr $USERINFO = $_SESSION[DOKU_COOKIE]['auth']['info']; 276f13fa892SAndreas Gohr return true; 277f13fa892SAndreas Gohr} 278f13fa892SAndreas Gohr 279f13fa892SAndreas Gohr/** 280f13fa892SAndreas Gohr * Create an auth token and store it in the session 281f13fa892SAndreas Gohr * 282f13fa892SAndreas Gohr * NOTE: this is completely unrelated to the getSecurityToken() function 283f13fa892SAndreas Gohr * 284f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 285f13fa892SAndreas Gohr * @return string The auth token 286f13fa892SAndreas Gohr */ 287f13fa892SAndreas Gohrfunction auth_createToken() { 288f13fa892SAndreas Gohr $token = md5(mt_rand()); 28909c2d803SAndreas Gohr @session_start(); // reopen the session if needed 290f13fa892SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['token'] = $token; 29109c2d803SAndreas Gohr session_write_close(); 292f13fa892SAndreas Gohr return $token; 293f13fa892SAndreas Gohr} 294f13fa892SAndreas Gohr 295f13fa892SAndreas Gohr/** 296136ce040Sandi * Builds a pseudo UID from browser and IP data 297132bdbfeSandi * 298132bdbfeSandi * This is neither unique nor unfakable - still it adds some 299136ce040Sandi * security. Using the first part of the IP makes sure 300136ce040Sandi * proxy farms like AOLs are stil okay. 30115fae107Sandi * 30215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 30315fae107Sandi * 30415fae107Sandi * @return string a MD5 sum of various browser headers 305132bdbfeSandi */ 306132bdbfeSandifunction auth_browseruid() { 3072f9daf16SAndreas Gohr $ip = clientIP(true); 308132bdbfeSandi $uid = ''; 309132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 310132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 311132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 312132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 3132f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 314132bdbfeSandi return md5($uid); 315132bdbfeSandi} 316132bdbfeSandi 317132bdbfeSandi/** 318132bdbfeSandi * Creates a random key to encrypt the password in cookies 31915fae107Sandi * 32015fae107Sandi * This function tries to read the password for encrypting 32198407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 32215fae107Sandi * if no such file is found a random key is created and 32315fae107Sandi * and stored in this file. 32415fae107Sandi * 32515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 32632ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 32715fae107Sandi * @return string 328132bdbfeSandi */ 32932ed2b36SAndreas Gohrfunction auth_cookiesalt($addsession = false) { 330132bdbfeSandi global $conf; 33198407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 332132bdbfeSandi $salt = io_readFile($file); 333132bdbfeSandi if(empty($salt)) { 334132bdbfeSandi $salt = uniqid(rand(), true); 335132bdbfeSandi io_saveFile($file, $salt); 336132bdbfeSandi } 33732ed2b36SAndreas Gohr if($addsession) { 33832ed2b36SAndreas Gohr $salt .= session_id(); 33932ed2b36SAndreas Gohr } 340132bdbfeSandi return $salt; 341f3f0262cSandi} 342f3f0262cSandi 343f3f0262cSandi/** 344883179a4SAndreas Gohr * Log out the current user 345883179a4SAndreas Gohr * 346f3f0262cSandi * This clears all authentication data and thus log the user 347883179a4SAndreas Gohr * off. It also clears session data. 34815fae107Sandi * 34915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 350883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 351f3f0262cSandi */ 352883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 353f3f0262cSandi global $conf; 354f3f0262cSandi global $USERINFO; 355ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 3565298a619SAndreas Gohr global $auth; 35737065e65Sandi 358d4869846SAndreas Gohr // make sure the session is writable (it usually is) 359e9621d07SAndreas Gohr @session_start(); 360e9621d07SAndreas Gohr 361e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 362e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 363e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 364e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 365e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 366e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 367883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 368e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 36937065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 370f3f0262cSandi unset($_SERVER['REMOTE_USER']); 371132bdbfeSandi $USERINFO = null; //FIXME 372f5c6743cSAndreas Gohr 37373ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 374f5c6743cSAndreas Gohr if(version_compare(PHP_VERSION, '5.2.0', '>')) { 37573ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 376f5c6743cSAndreas Gohr } else { 37773ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 378f5c6743cSAndreas Gohr } 3795298a619SAndreas Gohr 380880f62faSAndreas Gohr if($auth) $auth->logOff(); 381f3f0262cSandi} 382f3f0262cSandi 383f3f0262cSandi/** 384f8cc712eSAndreas Gohr * Check if a user is a manager 385f8cc712eSAndreas Gohr * 386f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 387f8cc712eSAndreas Gohr * user. 388f8cc712eSAndreas Gohr * 389f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 390f8cc712eSAndreas Gohr * 391f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 392f8cc712eSAndreas Gohr * @see auth_isadmin 393ab5d26daSAndreas Gohr * @param string $user Username 394ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 395ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 396ab5d26daSAndreas Gohr * @return bool 397f8cc712eSAndreas Gohr */ 398f8cc712eSAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false) { 399f8cc712eSAndreas Gohr global $conf; 400f8cc712eSAndreas Gohr global $USERINFO; 401ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 402d752aedeSAndreas Gohr global $auth; 403f8cc712eSAndreas Gohr 404beca106aSAdrian Lang if(!$auth) return false; 405c66972f2SAdrian Lang if(is_null($user)) { 406c66972f2SAdrian Lang if(!isset($_SERVER['REMOTE_USER'])) { 407c66972f2SAdrian Lang return false; 408c66972f2SAdrian Lang } else { 409c66972f2SAdrian Lang $user = $_SERVER['REMOTE_USER']; 410c66972f2SAdrian Lang } 411c66972f2SAdrian Lang } 412d6dc956fSAndreas Gohr if(is_null($groups)) { 413d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 414e259aa79SAndreas Gohr } 415e259aa79SAndreas Gohr 416d6dc956fSAndreas Gohr // check superuser match 417d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'], $user, $groups)) return true; 418d6dc956fSAndreas Gohr if($adminonly) return false; 419e259aa79SAndreas Gohr // check managers 420d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'], $user, $groups)) return true; 42100ce12daSChris Smith 422f8cc712eSAndreas Gohr return false; 423f8cc712eSAndreas Gohr} 424f8cc712eSAndreas Gohr 425f8cc712eSAndreas Gohr/** 426f8cc712eSAndreas Gohr * Check if a user is admin 427f8cc712eSAndreas Gohr * 428f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 429f8cc712eSAndreas Gohr * 430f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 431f8cc712eSAndreas Gohr * 432f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 433ab5d26daSAndreas Gohr * @see auth_ismanager() 434ab5d26daSAndreas Gohr * @param string $user Username 435ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 436ab5d26daSAndreas Gohr * @return bool 437f8cc712eSAndreas Gohr */ 438f8cc712eSAndreas Gohrfunction auth_isadmin($user = null, $groups = null) { 439f8cc712eSAndreas Gohr return auth_ismanager($user, $groups, true); 440f8cc712eSAndreas Gohr} 441f8cc712eSAndreas Gohr 442d6dc956fSAndreas Gohr/** 443d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 444d6dc956fSAndreas Gohr * users and groups to determine membership status 445d6dc956fSAndreas Gohr * 446d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 447d6dc956fSAndreas Gohr * 448d6dc956fSAndreas Gohr * @param $memberlist string commaseparated list of allowed users and groups 449d6dc956fSAndreas Gohr * @param $user string user to match against 450d6dc956fSAndreas Gohr * @param $groups array groups the user is member of 4515446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 452d6dc956fSAndreas Gohr */ 453d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 454ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 455d6dc956fSAndreas Gohr global $auth; 456d6dc956fSAndreas Gohr if(!$auth) return false; 457d6dc956fSAndreas Gohr 458d6dc956fSAndreas Gohr // clean user and groups 4594f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 460d6dc956fSAndreas Gohr $user = utf8_strtolower($user); 461d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 462d6dc956fSAndreas Gohr } 463d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 464d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 465d6dc956fSAndreas Gohr 466d6dc956fSAndreas Gohr // extract the memberlist 467d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 468d6dc956fSAndreas Gohr $members = array_map('trim', $members); 469d6dc956fSAndreas Gohr $members = array_unique($members); 470d6dc956fSAndreas Gohr $members = array_filter($members); 471d6dc956fSAndreas Gohr 472d6dc956fSAndreas Gohr // compare cleaned values 473d6dc956fSAndreas Gohr foreach($members as $member) { 4744f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member); 475d6dc956fSAndreas Gohr if($member[0] == '@') { 476d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 477d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 478d6dc956fSAndreas Gohr } else { 479d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 480d6dc956fSAndreas Gohr if($member == $user) return true; 481d6dc956fSAndreas Gohr } 482d6dc956fSAndreas Gohr } 483d6dc956fSAndreas Gohr 484d6dc956fSAndreas Gohr // still here? not a member! 485d6dc956fSAndreas Gohr return false; 486d6dc956fSAndreas Gohr} 487d6dc956fSAndreas Gohr 488f8cc712eSAndreas Gohr/** 48915fae107Sandi * Convinience function for auth_aclcheck() 49015fae107Sandi * 49115fae107Sandi * This checks the permissions for the current user 49215fae107Sandi * 49315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 49415fae107Sandi * 4951698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 49615fae107Sandi * @return int permission level 497f3f0262cSandi */ 498f3f0262cSandifunction auth_quickaclcheck($id) { 499f3f0262cSandi global $conf; 500f3f0262cSandi global $USERINFO; 501f3f0262cSandi # if no ACL is used always return upload rights 502f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 503f3f0262cSandi return auth_aclcheck($id, $_SERVER['REMOTE_USER'], $USERINFO['grps']); 504f3f0262cSandi} 505f3f0262cSandi 506f3f0262cSandi/** 507f3f0262cSandi * Returns the maximum rights a user has for 508f3f0262cSandi * the given ID or its namespace 50915fae107Sandi * 51015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 51115fae107Sandi * 5121698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 51315fae107Sandi * @param string $user Username 5143272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 51515fae107Sandi * @return int permission level 516f3f0262cSandi */ 517f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 518f3f0262cSandi global $conf; 519f3f0262cSandi global $AUTH_ACL; 520ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 521d752aedeSAndreas Gohr global $auth; 522f3f0262cSandi 52385d03f68SAndreas Gohr // if no ACL is used always return upload rights 524f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 525beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 526f3f0262cSandi 527074cf26bSandi //make sure groups is an array 528074cf26bSandi if(!is_array($groups)) $groups = array(); 529074cf26bSandi 53085d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 531ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 532ab5d26daSAndreas Gohr return AUTH_ADMIN; 533ab5d26daSAndreas Gohr } 53485d03f68SAndreas Gohr 535e259aa79SAndreas Gohr $ci = ''; 536e259aa79SAndreas Gohr if(!$auth->isCaseSensitive()) $ci = 'ui'; 537d752aedeSAndreas Gohr 538d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 539d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 54085d03f68SAndreas Gohr $user = auth_nameencode($user); 54185d03f68SAndreas Gohr 5426c2bb100SAndreas Gohr //prepend groups with @ and nameencode 5432cd2db38Sandi $cnt = count($groups); 5442cd2db38Sandi for($i = 0; $i < $cnt; $i++) { 5456c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 54610a76f6fSfrank } 54710a76f6fSfrank 548f3f0262cSandi $ns = getNS($id); 549f3f0262cSandi $perm = -1; 550f3f0262cSandi 55134aeb4afSAndreas Gohr if($user || count($groups)) { 552f3f0262cSandi //add ALL group 553f3f0262cSandi $groups[] = '@ALL'; 554f3f0262cSandi //add User 55534aeb4afSAndreas Gohr if($user) $groups[] = $user; 556f3f0262cSandi } else { 55748d7b7a6SDominik Eckelmann $groups[] = '@ALL'; 558f3f0262cSandi } 559f3f0262cSandi 560f3f0262cSandi //check exact match first 56148d7b7a6SDominik Eckelmann $matches = preg_grep('/^'.preg_quote($id, '/').'\s+(\S+)\s+/'.$ci, $AUTH_ACL); 562f3f0262cSandi if(count($matches)) { 563f3f0262cSandi foreach($matches as $match) { 564f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 565f3f0262cSandi $acl = preg_split('/\s+/', $match); 56648d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 56748d7b7a6SDominik Eckelmann continue; 56848d7b7a6SDominik Eckelmann } 5698ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 570f3f0262cSandi if($acl[2] > $perm) { 571f3f0262cSandi $perm = $acl[2]; 572f3f0262cSandi } 573f3f0262cSandi } 574f3f0262cSandi if($perm > -1) { 575f3f0262cSandi //we had a match - return it 576f3f0262cSandi return $perm; 577f3f0262cSandi } 578f3f0262cSandi } 579f3f0262cSandi 580f3f0262cSandi //still here? do the namespace checks 581f3f0262cSandi if($ns) { 5823e304b55SMichael Hamann $path = $ns.':*'; 583f3f0262cSandi } else { 5843e304b55SMichael Hamann $path = '*'; //root document 585f3f0262cSandi } 586f3f0262cSandi 587f3f0262cSandi do { 58848d7b7a6SDominik Eckelmann $matches = preg_grep('/^'.preg_quote($path, '/').'\s+(\S+)\s+/'.$ci, $AUTH_ACL); 589f3f0262cSandi if(count($matches)) { 590f3f0262cSandi foreach($matches as $match) { 591f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 592f3f0262cSandi $acl = preg_split('/\s+/', $match); 59348d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 59448d7b7a6SDominik Eckelmann continue; 59548d7b7a6SDominik Eckelmann } 5968ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 597f3f0262cSandi if($acl[2] > $perm) { 598f3f0262cSandi $perm = $acl[2]; 599f3f0262cSandi } 600f3f0262cSandi } 601f3f0262cSandi //we had a match - return it 60248d7b7a6SDominik Eckelmann if($perm != -1) { 603f3f0262cSandi return $perm; 604f3f0262cSandi } 60548d7b7a6SDominik Eckelmann } 606f3f0262cSandi //get next higher namespace 607f3f0262cSandi $ns = getNS($ns); 608f3f0262cSandi 6093e304b55SMichael Hamann if($path != '*') { 6103e304b55SMichael Hamann $path = $ns.':*'; 6113e304b55SMichael Hamann if($path == ':*') $path = '*'; 612f3f0262cSandi } else { 613f3f0262cSandi //we did this already 614f3f0262cSandi //looks like there is something wrong with the ACL 615f3f0262cSandi //break here 616d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 617d5ce66f6SAndreas Gohr return AUTH_NONE; 618f3f0262cSandi } 619f3f0262cSandi } while(1); //this should never loop endless 620ab5d26daSAndreas Gohr return AUTH_NONE; 621f3f0262cSandi} 622f3f0262cSandi 623f3f0262cSandi/** 6246c2bb100SAndreas Gohr * Encode ASCII special chars 6256c2bb100SAndreas Gohr * 6266c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 6276c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 6286c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 6296c2bb100SAndreas Gohr * urlencoding!). 6306c2bb100SAndreas Gohr * 6316c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 6326c2bb100SAndreas Gohr * 6336c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 6346c2bb100SAndreas Gohr * @see rawurldecode() 6356c2bb100SAndreas Gohr */ 636e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 637a424cd8eSchris global $cache_authname; 638a424cd8eSchris $cache =& $cache_authname; 63931784267SAndreas Gohr $name = (string) $name; 640a424cd8eSchris 64180601d26SAndreas Gohr // never encode wildcard FS#1955 64280601d26SAndreas Gohr if($name == '%USER%') return $name; 643b78bf706Sromain if($name == '%GROUP%') return $name; 64480601d26SAndreas Gohr 645a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 646e838fc2eSAndreas Gohr if($skip_group && $name{0} == '@') { 647ab5d26daSAndreas Gohr $cache[$name][$skip_group] = '@'.preg_replace( 648ab5d26daSAndreas Gohr '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 649ab5d26daSAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))", substr($name, 1) 650ab5d26daSAndreas Gohr ); 651e838fc2eSAndreas Gohr } else { 652ab5d26daSAndreas Gohr $cache[$name][$skip_group] = preg_replace( 653ab5d26daSAndreas Gohr '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 654ab5d26daSAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))", $name 655ab5d26daSAndreas Gohr ); 656e838fc2eSAndreas Gohr } 6576c2bb100SAndreas Gohr } 6586c2bb100SAndreas Gohr 659a424cd8eSchris return $cache[$name][$skip_group]; 660a424cd8eSchris} 661a424cd8eSchris 6626c2bb100SAndreas Gohr/** 663f3f0262cSandi * Create a pronouncable password 664f3f0262cSandi * 66515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 66615fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 66715fae107Sandi * 66815fae107Sandi * @return string pronouncable password 669f3f0262cSandi */ 670f3f0262cSandifunction auth_pwgen() { 671f3f0262cSandi $pw = ''; 672f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 673f3f0262cSandi $v = 'aeiou'; //vowels 674f3f0262cSandi $a = $c.$v; //both 675f3f0262cSandi 676f3f0262cSandi //use two syllables... 677f3f0262cSandi for($i = 0; $i < 2; $i++) { 678f3f0262cSandi $pw .= $c[rand(0, strlen($c) - 1)]; 679f3f0262cSandi $pw .= $v[rand(0, strlen($v) - 1)]; 680f3f0262cSandi $pw .= $a[rand(0, strlen($a) - 1)]; 681f3f0262cSandi } 682f3f0262cSandi //... and add a nice number 683f3f0262cSandi $pw .= rand(10, 99); 684f3f0262cSandi 685f3f0262cSandi return $pw; 686f3f0262cSandi} 687f3f0262cSandi 688f3f0262cSandi/** 689f3f0262cSandi * Sends a password to the given user 690f3f0262cSandi * 69115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 692ab5d26daSAndreas Gohr * @param string $user Login name of the user 693ab5d26daSAndreas Gohr * @param string $password The new password in clear text 69415fae107Sandi * @return bool true on success 695f3f0262cSandi */ 696f3f0262cSandifunction auth_sendPassword($user, $password) { 697f3f0262cSandi global $lang; 698ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 699cd52f92dSchris global $auth; 700beca106aSAdrian Lang if(!$auth) return false; 701cd52f92dSchris 702d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 703cd52f92dSchris $userinfo = $auth->getUserData($user); 704f3f0262cSandi 70587ddda95Sandi if(!$userinfo['mail']) return false; 706f3f0262cSandi 707f3f0262cSandi $text = rawLocale('password'); 708d7169d19SAndreas Gohr $trep = array( 709d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 710d7169d19SAndreas Gohr 'LOGIN' => $user, 711d7169d19SAndreas Gohr 'PASSWORD' => $password 712d7169d19SAndreas Gohr ); 713f3f0262cSandi 714d7169d19SAndreas Gohr $mail = new Mailer(); 715d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 716d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 717d7169d19SAndreas Gohr $mail->setBody($text, $trep); 718d7169d19SAndreas Gohr return $mail->send(); 719f3f0262cSandi} 720f3f0262cSandi 721f3f0262cSandi/** 72215fae107Sandi * Register a new user 723f3f0262cSandi * 72415fae107Sandi * This registers a new user - Data is read directly from $_POST 72515fae107Sandi * 72615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 72715fae107Sandi * @return bool true on success, false on any error 728f3f0262cSandi */ 729f3f0262cSandifunction register() { 730f3f0262cSandi global $lang; 731eb5d07e4Sjan global $conf; 732ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 733cd52f92dSchris global $auth; 73464273335SAndreas Gohr global $INPUT; 735f3f0262cSandi 73664273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 7373a48618aSAnika Henke if(!actionOK('register')) return false; 738640145a5Sandi 73964273335SAndreas Gohr // gather input 74064273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 74164273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 74264273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 74364273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 74464273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 745d752aedeSAndreas Gohr 74664273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 747f3f0262cSandi msg($lang['regmissing'], -1); 748f3f0262cSandi return false; 749f3f0262cSandi } 750f3f0262cSandi 751cab2716aSmatthias.grimm if($conf['autopasswd']) { 752cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 75364273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 754bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 755cab2716aSmatthias.grimm return false; 75664273335SAndreas Gohr } elseif($pass != $passchk) { 757bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 758cab2716aSmatthias.grimm return false; 759cab2716aSmatthias.grimm } 760cab2716aSmatthias.grimm 761f3f0262cSandi //check mail 76264273335SAndreas Gohr if(!mail_isvalid($email)) { 763f3f0262cSandi msg($lang['regbadmail'], -1); 764f3f0262cSandi return false; 765f3f0262cSandi } 766f3f0262cSandi 767f3f0262cSandi //okay try to create the user 76864273335SAndreas Gohr if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email))) { 769f3f0262cSandi msg($lang['reguexists'], -1); 770f3f0262cSandi return false; 771f3f0262cSandi } 772f3f0262cSandi 77302a498e7Schris // create substitutions for use in notification email 77402a498e7Schris $substitutions = array( 77564273335SAndreas Gohr 'NEWUSER' => $login, 77664273335SAndreas Gohr 'NEWNAME' => $fullname, 77764273335SAndreas Gohr 'NEWEMAIL' => $email, 77802a498e7Schris ); 77902a498e7Schris 780cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 781cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 78264273335SAndreas Gohr notify('', 'register', '', $login, false, $substitutions); 783cab2716aSmatthias.grimm return true; 784cab2716aSmatthias.grimm } 785cab2716aSmatthias.grimm 786cab2716aSmatthias.grimm // autogenerated password? then send him the password 78764273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 788f3f0262cSandi msg($lang['regsuccess'], 1); 78964273335SAndreas Gohr notify('', 'register', '', $login, false, $substitutions); 790f3f0262cSandi return true; 791f3f0262cSandi } else { 792f3f0262cSandi msg($lang['regmailfail'], -1); 793f3f0262cSandi return false; 794f3f0262cSandi } 795f3f0262cSandi} 796f3f0262cSandi 79710a76f6fSfrank/** 7988b06d178Schris * Update user profile 7998b06d178Schris * 8008b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 8018b06d178Schris */ 8028b06d178Schrisfunction updateprofile() { 8038b06d178Schris global $conf; 8048b06d178Schris global $lang; 805ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 806cd52f92dSchris global $auth; 807bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 808bcc94b2cSAndreas Gohr global $INPUT; 8098b06d178Schris 810bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 8111b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 8128b06d178Schris 8133a48618aSAnika Henke if(!actionOK('profile')) { 8148b06d178Schris msg($lang['profna'], -1); 8158b06d178Schris return false; 8168b06d178Schris } 8178b06d178Schris 818bcc94b2cSAndreas Gohr $changes = array(); 819bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 820bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 821bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 822bcc94b2cSAndreas Gohr 823bcc94b2cSAndreas Gohr // check misspelled passwords 824bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 825bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 8268b06d178Schris return false; 8278b06d178Schris } 8288b06d178Schris 8298b06d178Schris // clean fullname and email 830bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 831bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 8328b06d178Schris 833bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 834bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 835bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 836ab5d26daSAndreas Gohr ) { 8378b06d178Schris msg($lang['profnoempty'], -1); 8388b06d178Schris return false; 8398b06d178Schris } 840bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 8418b06d178Schris msg($lang['regbadmail'], -1); 8428b06d178Schris return false; 8438b06d178Schris } 8448b06d178Schris 845bcc94b2cSAndreas Gohr $changes = array_filter($changes); 8464c21b7eeSAndreas Gohr 847bcc94b2cSAndreas Gohr // check for unavailable capabilities 848bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 849bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 850bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 851bcc94b2cSAndreas Gohr 852bcc94b2cSAndreas Gohr // anything to do? 8538b06d178Schris if(!count($changes)) { 8548b06d178Schris msg($lang['profnochange'], -1); 8558b06d178Schris return false; 8568b06d178Schris } 8578b06d178Schris 8588b06d178Schris if($conf['profileconfirm']) { 859bcc94b2cSAndreas Gohr if(!$auth->checkPass($_SERVER['REMOTE_USER'], $INPUT->post->str('oldpass'))) { 8608b06d178Schris msg($lang['badlogin'], -1); 8618b06d178Schris return false; 8628b06d178Schris } 8638b06d178Schris } 8648b06d178Schris 865a0b5b007SChris Smith if($result = $auth->triggerUserMod('modify', array($_SERVER['REMOTE_USER'], $changes))) { 866a0b5b007SChris Smith // update cookie and session with the changed data 86732ed2b36SAndreas Gohr if($changes['pass']) { 868ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 86932ed2b36SAndreas Gohr $pass = PMA_blowfish_encrypt($changes['pass'], auth_cookiesalt(!$sticky)); 870a0b5b007SChris Smith auth_setCookie($_SERVER['REMOTE_USER'], $pass, (bool) $sticky); 87132ed2b36SAndreas Gohr } 87225b2a98cSMichael Klier return true; 873a0b5b007SChris Smith } 874ab5d26daSAndreas Gohr 875ab5d26daSAndreas Gohr return false; 8768b06d178Schris} 8778b06d178Schris 8788b06d178Schris/** 8798b06d178Schris * Send a new password 8808b06d178Schris * 8811d5856cfSAndreas Gohr * This function handles both phases of the password reset: 8821d5856cfSAndreas Gohr * 8831d5856cfSAndreas Gohr * - handling the first request of password reset 8841d5856cfSAndreas Gohr * - validating the password reset auth token 8851d5856cfSAndreas Gohr * 8868b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 8878b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 8881d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8898b06d178Schris * 8908b06d178Schris * @return bool true on success, false on any error 8918b06d178Schris */ 8928b06d178Schrisfunction act_resendpwd() { 8938b06d178Schris global $lang; 8948b06d178Schris global $conf; 895ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 896cd52f92dSchris global $auth; 897bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 898bcc94b2cSAndreas Gohr global $INPUT; 8998b06d178Schris 9003a48618aSAnika Henke if(!actionOK('resendpwd')) { 9018b06d178Schris msg($lang['resendna'], -1); 9028b06d178Schris return false; 9038b06d178Schris } 9048b06d178Schris 905bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 9068b06d178Schris 9071d5856cfSAndreas Gohr if($token) { 908cc204bbdSAndreas Gohr // we're in token phase - get user info from token 9091d5856cfSAndreas Gohr 9101d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 9111d5856cfSAndreas Gohr if(!@file_exists($tfile)) { 9121d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 913bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 9141d5856cfSAndreas Gohr return false; 9151d5856cfSAndreas Gohr } 9168a9735e3SAndreas Gohr // token is only valid for 3 days 9178a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 9188a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 919bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 9201d5856cfSAndreas Gohr @unlink($tfile); 9218a9735e3SAndreas Gohr return false; 9228a9735e3SAndreas Gohr } 9238a9735e3SAndreas Gohr 9248b06d178Schris $user = io_readfile($tfile); 925cd52f92dSchris $userinfo = $auth->getUserData($user); 9268b06d178Schris if(!$userinfo['mail']) { 9278b06d178Schris msg($lang['resendpwdnouser'], -1); 9288b06d178Schris return false; 9298b06d178Schris } 9308b06d178Schris 931cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 932bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 933bcc94b2cSAndreas Gohr 934cc204bbdSAndreas Gohr // password given correctly? 935bcc94b2cSAndreas Gohr if(!$pass) return false; 936bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 937451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 938cc204bbdSAndreas Gohr return false; 939cc204bbdSAndreas Gohr } 940cc204bbdSAndreas Gohr 941bcc94b2cSAndreas Gohr // change it 942cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 943cc204bbdSAndreas Gohr msg('error modifying user data', -1); 944cc204bbdSAndreas Gohr return false; 945cc204bbdSAndreas Gohr } 946cc204bbdSAndreas Gohr 947cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 948cc204bbdSAndreas Gohr 9498b06d178Schris $pass = auth_pwgen(); 9507d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 9518b06d178Schris msg('error modifying user data', -1); 9528b06d178Schris return false; 9538b06d178Schris } 9548b06d178Schris 9558b06d178Schris if(auth_sendPassword($user, $pass)) { 9568b06d178Schris msg($lang['resendpwdsuccess'], 1); 9578b06d178Schris } else { 9588b06d178Schris msg($lang['regmailfail'], -1); 9598b06d178Schris } 960cc204bbdSAndreas Gohr } 961cc204bbdSAndreas Gohr 962cc204bbdSAndreas Gohr @unlink($tfile); 9638b06d178Schris return true; 9641d5856cfSAndreas Gohr 9651d5856cfSAndreas Gohr } else { 9661d5856cfSAndreas Gohr // we're in request phase 9671d5856cfSAndreas Gohr 968bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9691d5856cfSAndreas Gohr 970bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 9711d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 9721d5856cfSAndreas Gohr return false; 9731d5856cfSAndreas Gohr } else { 974bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 9751d5856cfSAndreas Gohr } 9761d5856cfSAndreas Gohr 9771d5856cfSAndreas Gohr $userinfo = $auth->getUserData($user); 9781d5856cfSAndreas Gohr if(!$userinfo['mail']) { 9791d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 9801d5856cfSAndreas Gohr return false; 9811d5856cfSAndreas Gohr } 9821d5856cfSAndreas Gohr 9831d5856cfSAndreas Gohr // generate auth token 9841d5856cfSAndreas Gohr $token = md5(auth_cookiesalt().$user); //secret but user based 9851d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 9861d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 9871d5856cfSAndreas Gohr 9881d5856cfSAndreas Gohr io_saveFile($tfile, $user); 9891d5856cfSAndreas Gohr 9901d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 991d7169d19SAndreas Gohr $trep = array( 992d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 993d7169d19SAndreas Gohr 'LOGIN' => $user, 994d7169d19SAndreas Gohr 'CONFIRM' => $url 995d7169d19SAndreas Gohr ); 9961d5856cfSAndreas Gohr 997d7169d19SAndreas Gohr $mail = new Mailer(); 998d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 999d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1000d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1001d7169d19SAndreas Gohr if($mail->send()) { 10021d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 10031d5856cfSAndreas Gohr } else { 10041d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 10051d5856cfSAndreas Gohr } 10061d5856cfSAndreas Gohr return true; 10071d5856cfSAndreas Gohr } 1008ab5d26daSAndreas Gohr // never reached 10098b06d178Schris} 10108b06d178Schris 10118b06d178Schris/** 1012b0855b11Sandi * Encrypts a password using the given method and salt 1013b0855b11Sandi * 1014b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1015b0855b11Sandi * is chosen. 1016b0855b11Sandi * 1017b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 1018ab5d26daSAndreas Gohr * @param string $clear The clear text password 1019ab5d26daSAndreas Gohr * @param string $method The hashing method 1020ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1021b0855b11Sandi * @return string The crypted password 1022b0855b11Sandi */ 1023577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) { 1024b0855b11Sandi global $conf; 1025b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 102610a76f6fSfrank 10273a0a2d05SAndreas Gohr $pass = new PassHash(); 10283a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1029b0855b11Sandi 10303a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1031b0855b11Sandi msg("Unsupported crypt method $method", -1); 10323a0a2d05SAndreas Gohr return false; 1033b0855b11Sandi } 10343a0a2d05SAndreas Gohr 10353a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1036b0855b11Sandi} 1037b0855b11Sandi 1038b0855b11Sandi/** 1039b0855b11Sandi * Verifies a cleartext password against a crypted hash 1040b0855b11Sandi * 1041b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 1042ab5d26daSAndreas Gohr * @param string $clear The clear text password 1043ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1044ab5d26daSAndreas Gohr * @return bool true if both match 1045b0855b11Sandi */ 1046b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 10473a0a2d05SAndreas Gohr $pass = new PassHash(); 10483a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1049b0855b11Sandi} 1050340756e4Sandi 1051a0b5b007SChris Smith/** 1052a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1053a0b5b007SChris Smith * 1054a0b5b007SChris Smith * @param string $user username 1055a0b5b007SChris Smith * @param string $pass encrypted password 1056a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1057ab5d26daSAndreas Gohr * @return bool 1058a0b5b007SChris Smith */ 1059a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1060a0b5b007SChris Smith global $conf; 1061ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 1062a0b5b007SChris Smith global $auth; 106379d00841SOliver Geisen global $USERINFO; 1064a0b5b007SChris Smith 1065beca106aSAdrian Lang if(!$auth) return false; 1066a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1067a0b5b007SChris Smith 1068a0b5b007SChris Smith // set cookie 1069645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 107073ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1071c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1072a0b5b007SChris Smith if(version_compare(PHP_VERSION, '5.2.0', '>')) { 107373ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 1074a0b5b007SChris Smith } else { 107573ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 1076a0b5b007SChris Smith } 1077a0b5b007SChris Smith // set session 1078a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1079234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1080a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1081a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1082a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1083ab5d26daSAndreas Gohr 1084ab5d26daSAndreas Gohr return true; 1085a0b5b007SChris Smith} 1086a0b5b007SChris Smith 1087645c0a36SAndreas Gohr/** 1088645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1089645c0a36SAndreas Gohr * 1090645c0a36SAndreas Gohr * @returns array 1091645c0a36SAndreas Gohr */ 1092645c0a36SAndreas Gohrfunction auth_getCookie() { 1093c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1094c66972f2SAdrian Lang return array(null, null, null); 1095c66972f2SAdrian Lang } 1096645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1097645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1098645c0a36SAndreas Gohr $pass = base64_decode($pass); 1099645c0a36SAndreas Gohr $user = base64_decode($user); 1100645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1101645c0a36SAndreas Gohr} 1102645c0a36SAndreas Gohr 1103e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1104