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; 3793a7873eSAndreas 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*3094e817SAndreas Gohr if(!$auth){ 58*3094e817SAndreas Gohr msg($lang['authtempfail'], -1); 59*3094e817SAndreas Gohr return false; 60*3094e817SAndreas Gohr } 618b06d178Schris 62f4476bd9SJan Schumann if ($auth && $auth->success == false) { 630f4f4adfSAndreas Gohr // degrade to unauthenticated user 64d2dde4ebSMatthias Grimm unset($auth); 650f4f4adfSAndreas Gohr auth_logoff(); 66cd52f92dSchris msg($lang['authtempfail'], -1); 67d2dde4ebSMatthias Grimm } 6816905344SAndreas Gohr 6916905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 70bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 71bcc94b2cSAndreas Gohr if(!$conf['rememberme']) $INPUT->set('r', false); 72bbbd6568SAndreas Gohr 73b2665af7SMichael Hamann // handle renamed HTTP_AUTHORIZATION variable (can happen when a fix like 74b2665af7SMichael Hamann // the one presented at 75b2665af7SMichael Hamann // http://www.besthostratings.com/articles/http-auth-php-cgi.html is used 76b2665af7SMichael Hamann // for enabling HTTP authentication with CGI/SuExec) 77b2665af7SMichael Hamann if(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) 78b2665af7SMichael Hamann $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; 79528ddc7cSAndreas Gohr // streamline HTTP auth credentials (IIS/rewrite -> mod_php) 8006156f3cSAndreas Gohr if(isset($_SERVER['HTTP_AUTHORIZATION'])) { 81528ddc7cSAndreas Gohr list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = 82528ddc7cSAndreas Gohr explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); 83528ddc7cSAndreas Gohr } 84528ddc7cSAndreas Gohr 851e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 86bcc94b2cSAndreas Gohr if(!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])) { 87bcc94b2cSAndreas Gohr $INPUT->set('u', $_SERVER['PHP_AUTH_USER']); 88bcc94b2cSAndreas Gohr $INPUT->set('p', $_SERVER['PHP_AUTH_PW']); 89bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 901e8c9c90SAndreas Gohr } 911e8c9c90SAndreas Gohr 92191bb90aSAndreas Gohr // apply cleaning 9393a7873eSAndreas Gohr if (true === $auth->success) { 94191bb90aSAndreas Gohr $_REQUEST['u'] = $auth->cleanUser($_REQUEST['u']); 95f4476bd9SJan Schumann } 96191bb90aSAndreas Gohr 97bcc94b2cSAndreas Gohr if($INPUT->str('authtok')) { 98f13fa892SAndreas Gohr // when an authentication token is given, trust the session 99bcc94b2cSAndreas Gohr auth_validateToken($INPUT->str('authtok')); 100f13fa892SAndreas Gohr } elseif(!is_null($auth) && $auth->canDo('external')) { 101f13fa892SAndreas Gohr // external trust mechanism in place 102bcc94b2cSAndreas Gohr $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 103f5cb575dSAndreas Gohr } else { 1046080c584SRobin Gareus $evdata = array( 105bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 106bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 107bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 108bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 1096080c584SRobin Gareus ); 110b5ee21aaSAdrian Lang trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 111f5cb575dSAndreas Gohr } 112f5cb575dSAndreas Gohr 11316905344SAndreas Gohr //load ACL into a global array XXX 11475c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 115ab5d26daSAndreas Gohr 116ab5d26daSAndreas Gohr return true; 11775c93b77SAndreas Gohr} 11875c93b77SAndreas Gohr 11975c93b77SAndreas Gohr/** 12075c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12175c93b77SAndreas Gohr * 12275c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 123ab5d26daSAndreas Gohr * @return array 12475c93b77SAndreas Gohr */ 12575c93b77SAndreas Gohrfunction auth_loadACL() { 12675c93b77SAndreas Gohr global $config_cascade; 127b78bf706Sromain global $USERINFO; 12875c93b77SAndreas Gohr 12975c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 13075c93b77SAndreas Gohr 13175c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 13275c93b77SAndreas Gohr 133191bb90aSAndreas Gohr //support user wildcard 13432e82180SAndreas Gohr $out = array(); 1359ce556d2SAndreas Gohr foreach($acl as $line) { 1369ce556d2SAndreas Gohr $line = trim($line); 1379ce556d2SAndreas Gohr if($line{0} == '#') continue; 1389ce556d2SAndreas Gohr list($id,$rest) = preg_split('/\s+/',$line,2); 13932e82180SAndreas Gohr 1409ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 1419ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 142b78bf706Sromain $nid = str_replace('%GROUP%',cleanID($grp),$id); 14332e82180SAndreas Gohr $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest); 14432e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 145b78bf706Sromain } 14632e82180SAndreas Gohr } else { 14775c93b77SAndreas Gohr $id = str_replace('%USER%',cleanID($_SERVER['REMOTE_USER']),$id); 14875c93b77SAndreas Gohr $rest = str_replace('%USER%',auth_nameencode($_SERVER['REMOTE_USER']),$rest); 14932e82180SAndreas Gohr $out[] = "$id\t$rest"; 150a8fe108bSGuy Brand } 15111799630Sandi } 1529ce556d2SAndreas Gohr 15332e82180SAndreas Gohr return $out; 154f3f0262cSandi} 155f3f0262cSandi 156ab5d26daSAndreas Gohr/** 157ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 158ab5d26daSAndreas Gohr * 159ab5d26daSAndreas Gohr * @param $evdata 160ab5d26daSAndreas Gohr * @return bool 161ab5d26daSAndreas Gohr */ 162b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 163ab5d26daSAndreas Gohr return auth_login( 164ab5d26daSAndreas Gohr $evdata['user'], 165b5ee21aaSAdrian Lang $evdata['password'], 166b5ee21aaSAdrian Lang $evdata['sticky'], 167ab5d26daSAndreas Gohr $evdata['silent'] 168ab5d26daSAndreas Gohr ); 169b5ee21aaSAdrian Lang} 170b5ee21aaSAdrian Lang 171f3f0262cSandi/** 172f3f0262cSandi * This tries to login the user based on the sent auth credentials 173f3f0262cSandi * 174f3f0262cSandi * The authentication works like this: if a username was given 17515fae107Sandi * a new login is assumed and user/password are checked. If they 17615fae107Sandi * are correct the password is encrypted with blowfish and stored 17715fae107Sandi * together with the username in a cookie - the same info is stored 17815fae107Sandi * in the session, too. Additonally a browserID is stored in the 17915fae107Sandi * session. 18015fae107Sandi * 18115fae107Sandi * If no username was given the cookie is checked: if the username, 18215fae107Sandi * crypted password and browserID match between session and cookie 18315fae107Sandi * no further testing is done and the user is accepted 18415fae107Sandi * 18515fae107Sandi * If a cookie was found but no session info was availabe the 186136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 18715fae107Sandi * together with username rechecked by calling this function again. 188f3f0262cSandi * 189f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 190f3f0262cSandi * are set. 19115fae107Sandi * 19215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 19315fae107Sandi * 19415fae107Sandi * @param string $user Username 19515fae107Sandi * @param string $pass Cleartext Password 19615fae107Sandi * @param bool $sticky Cookie should not expire 197f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 19815fae107Sandi * @return bool true on successful auth 199f3f0262cSandi */ 200f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 201f3f0262cSandi global $USERINFO; 202f3f0262cSandi global $conf; 203f3f0262cSandi global $lang; 204ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 205cd52f92dSchris global $auth; 206ab5d26daSAndreas Gohr 207132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 208f3f0262cSandi 209beca106aSAdrian Lang if(!$auth) return false; 210beca106aSAdrian Lang 211bbbd6568SAndreas Gohr if(!empty($user)) { 212132bdbfeSandi //usual login 213cd52f92dSchris if($auth->checkPass($user, $pass)) { 214132bdbfeSandi // make logininfo globally available 215f3f0262cSandi $_SERVER['REMOTE_USER'] = $user; 21632ed2b36SAndreas Gohr $secret = auth_cookiesalt(!$sticky); //bind non-sticky to session 217e940aea4SAndreas Gohr auth_setCookie($user, PMA_blowfish_encrypt($pass, $secret), $sticky); 218132bdbfeSandi return true; 219f3f0262cSandi } else { 220f3f0262cSandi //invalid credentials - log off 221f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'], -1); 222f3f0262cSandi auth_logoff(); 223132bdbfeSandi return false; 224f3f0262cSandi } 225f3f0262cSandi } else { 226132bdbfeSandi // read cookie information 227645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 228132bdbfeSandi if($user && $pass) { 229132bdbfeSandi // we got a cookie - see if we can trust it 230fa7c70ffSAdrian Lang 231fa7c70ffSAdrian Lang // get session info 232fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 233132bdbfeSandi if(isset($session) && 2347172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2354c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 236132bdbfeSandi ($session['user'] == $user) && 237234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 238ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 239ab5d26daSAndreas Gohr ) { 240234ce57eSAndreas Gohr 241132bdbfeSandi // he has session, cookie and browser right - let him in 242132bdbfeSandi $_SERVER['REMOTE_USER'] = $user; 243132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 244132bdbfeSandi return true; 245132bdbfeSandi } 246f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 24732ed2b36SAndreas Gohr $secret = auth_cookiesalt(!$sticky); //bind non-sticky to session 248e940aea4SAndreas Gohr $pass = PMA_blowfish_decrypt($pass, $secret); 249f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 250132bdbfeSandi } 251132bdbfeSandi } 252f3f0262cSandi //just to be sure 253883179a4SAndreas Gohr auth_logoff(true); 254132bdbfeSandi return false; 255f3f0262cSandi} 256132bdbfeSandi 257132bdbfeSandi/** 258f13fa892SAndreas Gohr * Checks if a given authentication token was stored in the session 259f13fa892SAndreas Gohr * 260f13fa892SAndreas Gohr * Will setup authentication data using data from the session if the 261f13fa892SAndreas Gohr * token is correct. Will exit with a 401 Status if not. 262f13fa892SAndreas Gohr * 263f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 264f13fa892SAndreas Gohr * @param string $token The authentication token 265f13fa892SAndreas Gohr * @return boolean true (or will exit on failure) 266f13fa892SAndreas Gohr */ 267f13fa892SAndreas Gohrfunction auth_validateToken($token) { 268f13fa892SAndreas Gohr if(!$token || $token != $_SESSION[DOKU_COOKIE]['auth']['token']) { 269f13fa892SAndreas Gohr // bad token 270f13fa892SAndreas Gohr header("HTTP/1.0 401 Unauthorized"); 271f13fa892SAndreas Gohr print 'Invalid auth token - maybe the session timed out'; 272f13fa892SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['token']); // no second chance 273f13fa892SAndreas Gohr exit; 274f13fa892SAndreas Gohr } 275f13fa892SAndreas Gohr // still here? trust the session data 276f13fa892SAndreas Gohr global $USERINFO; 277f13fa892SAndreas Gohr $_SERVER['REMOTE_USER'] = $_SESSION[DOKU_COOKIE]['auth']['user']; 278f13fa892SAndreas Gohr $USERINFO = $_SESSION[DOKU_COOKIE]['auth']['info']; 279f13fa892SAndreas Gohr return true; 280f13fa892SAndreas Gohr} 281f13fa892SAndreas Gohr 282f13fa892SAndreas Gohr/** 283f13fa892SAndreas Gohr * Create an auth token and store it in the session 284f13fa892SAndreas Gohr * 285f13fa892SAndreas Gohr * NOTE: this is completely unrelated to the getSecurityToken() function 286f13fa892SAndreas Gohr * 287f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 288f13fa892SAndreas Gohr * @return string The auth token 289f13fa892SAndreas Gohr */ 290f13fa892SAndreas Gohrfunction auth_createToken() { 291f13fa892SAndreas Gohr $token = md5(mt_rand()); 29209c2d803SAndreas Gohr @session_start(); // reopen the session if needed 293f13fa892SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['token'] = $token; 29409c2d803SAndreas Gohr session_write_close(); 295f13fa892SAndreas Gohr return $token; 296f13fa892SAndreas Gohr} 297f13fa892SAndreas Gohr 298f13fa892SAndreas Gohr/** 299136ce040Sandi * Builds a pseudo UID from browser and IP data 300132bdbfeSandi * 301132bdbfeSandi * This is neither unique nor unfakable - still it adds some 302136ce040Sandi * security. Using the first part of the IP makes sure 303136ce040Sandi * proxy farms like AOLs are stil okay. 30415fae107Sandi * 30515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 30615fae107Sandi * 30715fae107Sandi * @return string a MD5 sum of various browser headers 308132bdbfeSandi */ 309132bdbfeSandifunction auth_browseruid() { 3102f9daf16SAndreas Gohr $ip = clientIP(true); 311132bdbfeSandi $uid = ''; 312132bdbfeSandi $uid .= $_SERVER['HTTP_USER_AGENT']; 313132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_ENCODING']; 314132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_LANGUAGE']; 315132bdbfeSandi $uid .= $_SERVER['HTTP_ACCEPT_CHARSET']; 3162f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 317132bdbfeSandi return md5($uid); 318132bdbfeSandi} 319132bdbfeSandi 320132bdbfeSandi/** 321132bdbfeSandi * Creates a random key to encrypt the password in cookies 32215fae107Sandi * 32315fae107Sandi * This function tries to read the password for encrypting 32498407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 32515fae107Sandi * if no such file is found a random key is created and 32615fae107Sandi * and stored in this file. 32715fae107Sandi * 32815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 32932ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 33015fae107Sandi * @return string 331132bdbfeSandi */ 33232ed2b36SAndreas Gohrfunction auth_cookiesalt($addsession = false) { 333132bdbfeSandi global $conf; 33498407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 335132bdbfeSandi $salt = io_readFile($file); 336132bdbfeSandi if(empty($salt)) { 337132bdbfeSandi $salt = uniqid(rand(), true); 338132bdbfeSandi io_saveFile($file, $salt); 339132bdbfeSandi } 34032ed2b36SAndreas Gohr if($addsession) { 34132ed2b36SAndreas Gohr $salt .= session_id(); 34232ed2b36SAndreas Gohr } 343132bdbfeSandi return $salt; 344f3f0262cSandi} 345f3f0262cSandi 346f3f0262cSandi/** 347883179a4SAndreas Gohr * Log out the current user 348883179a4SAndreas Gohr * 349f3f0262cSandi * This clears all authentication data and thus log the user 350883179a4SAndreas Gohr * off. It also clears session data. 35115fae107Sandi * 35215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 353883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 354f3f0262cSandi */ 355883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 356f3f0262cSandi global $conf; 357f3f0262cSandi global $USERINFO; 358ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 3595298a619SAndreas Gohr global $auth; 36037065e65Sandi 361d4869846SAndreas Gohr // make sure the session is writable (it usually is) 362e9621d07SAndreas Gohr @session_start(); 363e9621d07SAndreas Gohr 364e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 365e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 366e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 367e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 368e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 369e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 370883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 371e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 37237065e65Sandi if(isset($_SERVER['REMOTE_USER'])) 373f3f0262cSandi unset($_SERVER['REMOTE_USER']); 374132bdbfeSandi $USERINFO = null; //FIXME 375f5c6743cSAndreas Gohr 37673ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 377f5c6743cSAndreas Gohr if(version_compare(PHP_VERSION, '5.2.0', '>')) { 37873ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 379f5c6743cSAndreas Gohr } else { 38073ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 381f5c6743cSAndreas Gohr } 3825298a619SAndreas Gohr 383880f62faSAndreas Gohr if($auth) $auth->logOff(); 384f3f0262cSandi} 385f3f0262cSandi 386f3f0262cSandi/** 387f8cc712eSAndreas Gohr * Check if a user is a manager 388f8cc712eSAndreas Gohr * 389f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 390f8cc712eSAndreas Gohr * user. 391f8cc712eSAndreas Gohr * 392f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 393f8cc712eSAndreas Gohr * 394f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 395f8cc712eSAndreas Gohr * @see auth_isadmin 396ab5d26daSAndreas Gohr * @param string $user Username 397ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 398ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 399ab5d26daSAndreas Gohr * @return bool 400f8cc712eSAndreas Gohr */ 401f8cc712eSAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false) { 402f8cc712eSAndreas Gohr global $conf; 403f8cc712eSAndreas Gohr global $USERINFO; 404ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 405d752aedeSAndreas Gohr global $auth; 406f8cc712eSAndreas Gohr 407beca106aSAdrian Lang if(!$auth) return false; 408c66972f2SAdrian Lang if(is_null($user)) { 409c66972f2SAdrian Lang if(!isset($_SERVER['REMOTE_USER'])) { 410c66972f2SAdrian Lang return false; 411c66972f2SAdrian Lang } else { 412c66972f2SAdrian Lang $user = $_SERVER['REMOTE_USER']; 413c66972f2SAdrian Lang } 414c66972f2SAdrian Lang } 415d6dc956fSAndreas Gohr if(is_null($groups)) { 416d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 417e259aa79SAndreas Gohr } 418e259aa79SAndreas Gohr 419d6dc956fSAndreas Gohr // check superuser match 420d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'], $user, $groups)) return true; 421d6dc956fSAndreas Gohr if($adminonly) return false; 422e259aa79SAndreas Gohr // check managers 423d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'], $user, $groups)) return true; 42400ce12daSChris Smith 425f8cc712eSAndreas Gohr return false; 426f8cc712eSAndreas Gohr} 427f8cc712eSAndreas Gohr 428f8cc712eSAndreas Gohr/** 429f8cc712eSAndreas Gohr * Check if a user is admin 430f8cc712eSAndreas Gohr * 431f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 432f8cc712eSAndreas Gohr * 433f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 434f8cc712eSAndreas Gohr * 435f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 436ab5d26daSAndreas Gohr * @see auth_ismanager() 437ab5d26daSAndreas Gohr * @param string $user Username 438ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 439ab5d26daSAndreas Gohr * @return bool 440f8cc712eSAndreas Gohr */ 441f8cc712eSAndreas Gohrfunction auth_isadmin($user = null, $groups = null) { 442f8cc712eSAndreas Gohr return auth_ismanager($user, $groups, true); 443f8cc712eSAndreas Gohr} 444f8cc712eSAndreas Gohr 445d6dc956fSAndreas Gohr/** 446d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 447d6dc956fSAndreas Gohr * users and groups to determine membership status 448d6dc956fSAndreas Gohr * 449d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 450d6dc956fSAndreas Gohr * 451d6dc956fSAndreas Gohr * @param $memberlist string commaseparated list of allowed users and groups 452d6dc956fSAndreas Gohr * @param $user string user to match against 453d6dc956fSAndreas Gohr * @param $groups array groups the user is member of 4545446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 455d6dc956fSAndreas Gohr */ 456d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 457ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 458d6dc956fSAndreas Gohr global $auth; 459d6dc956fSAndreas Gohr if(!$auth) return false; 460d6dc956fSAndreas Gohr 461d6dc956fSAndreas Gohr // clean user and groups 4624f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 463d6dc956fSAndreas Gohr $user = utf8_strtolower($user); 464d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 465d6dc956fSAndreas Gohr } 466d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 467d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 468d6dc956fSAndreas Gohr 469d6dc956fSAndreas Gohr // extract the memberlist 470d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 471d6dc956fSAndreas Gohr $members = array_map('trim', $members); 472d6dc956fSAndreas Gohr $members = array_unique($members); 473d6dc956fSAndreas Gohr $members = array_filter($members); 474d6dc956fSAndreas Gohr 475d6dc956fSAndreas Gohr // compare cleaned values 476d6dc956fSAndreas Gohr foreach($members as $member) { 4774f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member); 478d6dc956fSAndreas Gohr if($member[0] == '@') { 479d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 480d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 481d6dc956fSAndreas Gohr } else { 482d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 483d6dc956fSAndreas Gohr if($member == $user) return true; 484d6dc956fSAndreas Gohr } 485d6dc956fSAndreas Gohr } 486d6dc956fSAndreas Gohr 487d6dc956fSAndreas Gohr // still here? not a member! 488d6dc956fSAndreas Gohr return false; 489d6dc956fSAndreas Gohr} 490d6dc956fSAndreas Gohr 491f8cc712eSAndreas Gohr/** 49215fae107Sandi * Convinience function for auth_aclcheck() 49315fae107Sandi * 49415fae107Sandi * This checks the permissions for the current user 49515fae107Sandi * 49615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 49715fae107Sandi * 4981698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 49915fae107Sandi * @return int permission level 500f3f0262cSandi */ 501f3f0262cSandifunction auth_quickaclcheck($id) { 502f3f0262cSandi global $conf; 503f3f0262cSandi global $USERINFO; 504f3f0262cSandi # if no ACL is used always return upload rights 505f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 506f3f0262cSandi return auth_aclcheck($id, $_SERVER['REMOTE_USER'], $USERINFO['grps']); 507f3f0262cSandi} 508f3f0262cSandi 509f3f0262cSandi/** 510f3f0262cSandi * Returns the maximum rights a user has for 511f3f0262cSandi * the given ID or its namespace 51215fae107Sandi * 51315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 51415fae107Sandi * 5151698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 51615fae107Sandi * @param string $user Username 5173272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 51815fae107Sandi * @return int permission level 519f3f0262cSandi */ 520f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 521f3f0262cSandi global $conf; 522f3f0262cSandi global $AUTH_ACL; 523ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 524d752aedeSAndreas Gohr global $auth; 525f3f0262cSandi 52685d03f68SAndreas Gohr // if no ACL is used always return upload rights 527f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 528beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 529f3f0262cSandi 530074cf26bSandi //make sure groups is an array 531074cf26bSandi if(!is_array($groups)) $groups = array(); 532074cf26bSandi 53385d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 534ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 535ab5d26daSAndreas Gohr return AUTH_ADMIN; 536ab5d26daSAndreas Gohr } 53785d03f68SAndreas Gohr 538e259aa79SAndreas Gohr $ci = ''; 539e259aa79SAndreas Gohr if(!$auth->isCaseSensitive()) $ci = 'ui'; 540d752aedeSAndreas Gohr 541d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 542d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 54385d03f68SAndreas Gohr $user = auth_nameencode($user); 54485d03f68SAndreas Gohr 5456c2bb100SAndreas Gohr //prepend groups with @ and nameencode 5462cd2db38Sandi $cnt = count($groups); 5472cd2db38Sandi for($i = 0; $i < $cnt; $i++) { 5486c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 54910a76f6fSfrank } 55010a76f6fSfrank 551f3f0262cSandi $ns = getNS($id); 552f3f0262cSandi $perm = -1; 553f3f0262cSandi 55434aeb4afSAndreas Gohr if($user || count($groups)) { 555f3f0262cSandi //add ALL group 556f3f0262cSandi $groups[] = '@ALL'; 557f3f0262cSandi //add User 55834aeb4afSAndreas Gohr if($user) $groups[] = $user; 559f3f0262cSandi } else { 56048d7b7a6SDominik Eckelmann $groups[] = '@ALL'; 561f3f0262cSandi } 562f3f0262cSandi 563f3f0262cSandi //check exact match first 56448d7b7a6SDominik Eckelmann $matches = preg_grep('/^'.preg_quote($id, '/').'\s+(\S+)\s+/'.$ci, $AUTH_ACL); 565f3f0262cSandi if(count($matches)) { 566f3f0262cSandi foreach($matches as $match) { 567f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 568f3f0262cSandi $acl = preg_split('/\s+/', $match); 56948d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 57048d7b7a6SDominik Eckelmann continue; 57148d7b7a6SDominik Eckelmann } 5728ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 573f3f0262cSandi if($acl[2] > $perm) { 574f3f0262cSandi $perm = $acl[2]; 575f3f0262cSandi } 576f3f0262cSandi } 577f3f0262cSandi if($perm > -1) { 578f3f0262cSandi //we had a match - return it 579f3f0262cSandi return $perm; 580f3f0262cSandi } 581f3f0262cSandi } 582f3f0262cSandi 583f3f0262cSandi //still here? do the namespace checks 584f3f0262cSandi if($ns) { 5853e304b55SMichael Hamann $path = $ns.':*'; 586f3f0262cSandi } else { 5873e304b55SMichael Hamann $path = '*'; //root document 588f3f0262cSandi } 589f3f0262cSandi 590f3f0262cSandi do { 59148d7b7a6SDominik Eckelmann $matches = preg_grep('/^'.preg_quote($path, '/').'\s+(\S+)\s+/'.$ci, $AUTH_ACL); 592f3f0262cSandi if(count($matches)) { 593f3f0262cSandi foreach($matches as $match) { 594f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 595f3f0262cSandi $acl = preg_split('/\s+/', $match); 59648d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 59748d7b7a6SDominik Eckelmann continue; 59848d7b7a6SDominik Eckelmann } 5998ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 600f3f0262cSandi if($acl[2] > $perm) { 601f3f0262cSandi $perm = $acl[2]; 602f3f0262cSandi } 603f3f0262cSandi } 604f3f0262cSandi //we had a match - return it 60548d7b7a6SDominik Eckelmann if($perm != -1) { 606f3f0262cSandi return $perm; 607f3f0262cSandi } 60848d7b7a6SDominik Eckelmann } 609f3f0262cSandi //get next higher namespace 610f3f0262cSandi $ns = getNS($ns); 611f3f0262cSandi 6123e304b55SMichael Hamann if($path != '*') { 6133e304b55SMichael Hamann $path = $ns.':*'; 6143e304b55SMichael Hamann if($path == ':*') $path = '*'; 615f3f0262cSandi } else { 616f3f0262cSandi //we did this already 617f3f0262cSandi //looks like there is something wrong with the ACL 618f3f0262cSandi //break here 619d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 620d5ce66f6SAndreas Gohr return AUTH_NONE; 621f3f0262cSandi } 622f3f0262cSandi } while(1); //this should never loop endless 623ab5d26daSAndreas Gohr return AUTH_NONE; 624f3f0262cSandi} 625f3f0262cSandi 626f3f0262cSandi/** 6276c2bb100SAndreas Gohr * Encode ASCII special chars 6286c2bb100SAndreas Gohr * 6296c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 6306c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 6316c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 6326c2bb100SAndreas Gohr * urlencoding!). 6336c2bb100SAndreas Gohr * 6346c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 6356c2bb100SAndreas Gohr * 6366c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 6376c2bb100SAndreas Gohr * @see rawurldecode() 6386c2bb100SAndreas Gohr */ 639e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 640a424cd8eSchris global $cache_authname; 641a424cd8eSchris $cache =& $cache_authname; 64231784267SAndreas Gohr $name = (string) $name; 643a424cd8eSchris 64480601d26SAndreas Gohr // never encode wildcard FS#1955 64580601d26SAndreas Gohr if($name == '%USER%') return $name; 646b78bf706Sromain if($name == '%GROUP%') return $name; 64780601d26SAndreas Gohr 648a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 649e838fc2eSAndreas Gohr if($skip_group && $name{0} == '@') { 650ab5d26daSAndreas Gohr $cache[$name][$skip_group] = '@'.preg_replace( 651ab5d26daSAndreas Gohr '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 652ab5d26daSAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))", substr($name, 1) 653ab5d26daSAndreas Gohr ); 654e838fc2eSAndreas Gohr } else { 655ab5d26daSAndreas Gohr $cache[$name][$skip_group] = preg_replace( 656ab5d26daSAndreas Gohr '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e', 657ab5d26daSAndreas Gohr "'%'.dechex(ord(substr('\\1',-1)))", $name 658ab5d26daSAndreas Gohr ); 659e838fc2eSAndreas Gohr } 6606c2bb100SAndreas Gohr } 6616c2bb100SAndreas Gohr 662a424cd8eSchris return $cache[$name][$skip_group]; 663a424cd8eSchris} 664a424cd8eSchris 6656c2bb100SAndreas Gohr/** 666f3f0262cSandi * Create a pronouncable password 667f3f0262cSandi * 66815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 66915fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 67015fae107Sandi * 67115fae107Sandi * @return string pronouncable password 672f3f0262cSandi */ 673f3f0262cSandifunction auth_pwgen() { 674f3f0262cSandi $pw = ''; 675f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 676f3f0262cSandi $v = 'aeiou'; //vowels 677f3f0262cSandi $a = $c.$v; //both 678f3f0262cSandi 679f3f0262cSandi //use two syllables... 680f3f0262cSandi for($i = 0; $i < 2; $i++) { 681f3f0262cSandi $pw .= $c[rand(0, strlen($c) - 1)]; 682f3f0262cSandi $pw .= $v[rand(0, strlen($v) - 1)]; 683f3f0262cSandi $pw .= $a[rand(0, strlen($a) - 1)]; 684f3f0262cSandi } 685f3f0262cSandi //... and add a nice number 686f3f0262cSandi $pw .= rand(10, 99); 687f3f0262cSandi 688f3f0262cSandi return $pw; 689f3f0262cSandi} 690f3f0262cSandi 691f3f0262cSandi/** 692f3f0262cSandi * Sends a password to the given user 693f3f0262cSandi * 69415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 695ab5d26daSAndreas Gohr * @param string $user Login name of the user 696ab5d26daSAndreas Gohr * @param string $password The new password in clear text 69715fae107Sandi * @return bool true on success 698f3f0262cSandi */ 699f3f0262cSandifunction auth_sendPassword($user, $password) { 700f3f0262cSandi global $lang; 701ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 702cd52f92dSchris global $auth; 703beca106aSAdrian Lang if(!$auth) return false; 704cd52f92dSchris 705d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 706cd52f92dSchris $userinfo = $auth->getUserData($user); 707f3f0262cSandi 70887ddda95Sandi if(!$userinfo['mail']) return false; 709f3f0262cSandi 710f3f0262cSandi $text = rawLocale('password'); 711d7169d19SAndreas Gohr $trep = array( 712d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 713d7169d19SAndreas Gohr 'LOGIN' => $user, 714d7169d19SAndreas Gohr 'PASSWORD' => $password 715d7169d19SAndreas Gohr ); 716f3f0262cSandi 717d7169d19SAndreas Gohr $mail = new Mailer(); 718d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 719d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 720d7169d19SAndreas Gohr $mail->setBody($text, $trep); 721d7169d19SAndreas Gohr return $mail->send(); 722f3f0262cSandi} 723f3f0262cSandi 724f3f0262cSandi/** 72515fae107Sandi * Register a new user 726f3f0262cSandi * 72715fae107Sandi * This registers a new user - Data is read directly from $_POST 72815fae107Sandi * 72915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 73015fae107Sandi * @return bool true on success, false on any error 731f3f0262cSandi */ 732f3f0262cSandifunction register() { 733f3f0262cSandi global $lang; 734eb5d07e4Sjan global $conf; 735ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 736cd52f92dSchris global $auth; 73764273335SAndreas Gohr global $INPUT; 738f3f0262cSandi 73964273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 7403a48618aSAnika Henke if(!actionOK('register')) return false; 741640145a5Sandi 74264273335SAndreas Gohr // gather input 74364273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 74464273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 74564273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 74664273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 74764273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 748d752aedeSAndreas Gohr 74964273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 750f3f0262cSandi msg($lang['regmissing'], -1); 751f3f0262cSandi return false; 752f3f0262cSandi } 753f3f0262cSandi 754cab2716aSmatthias.grimm if($conf['autopasswd']) { 755cab2716aSmatthias.grimm $pass = auth_pwgen(); // automatically generate password 75664273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 757bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 758cab2716aSmatthias.grimm return false; 75964273335SAndreas Gohr } elseif($pass != $passchk) { 760bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 761cab2716aSmatthias.grimm return false; 762cab2716aSmatthias.grimm } 763cab2716aSmatthias.grimm 764f3f0262cSandi //check mail 76564273335SAndreas Gohr if(!mail_isvalid($email)) { 766f3f0262cSandi msg($lang['regbadmail'], -1); 767f3f0262cSandi return false; 768f3f0262cSandi } 769f3f0262cSandi 770f3f0262cSandi //okay try to create the user 77164273335SAndreas Gohr if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email))) { 772f3f0262cSandi msg($lang['reguexists'], -1); 773f3f0262cSandi return false; 774f3f0262cSandi } 775f3f0262cSandi 77602a498e7Schris // create substitutions for use in notification email 77702a498e7Schris $substitutions = array( 77864273335SAndreas Gohr 'NEWUSER' => $login, 77964273335SAndreas Gohr 'NEWNAME' => $fullname, 78064273335SAndreas Gohr 'NEWEMAIL' => $email, 78102a498e7Schris ); 78202a498e7Schris 783cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 784cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 78564273335SAndreas Gohr notify('', 'register', '', $login, false, $substitutions); 786cab2716aSmatthias.grimm return true; 787cab2716aSmatthias.grimm } 788cab2716aSmatthias.grimm 789cab2716aSmatthias.grimm // autogenerated password? then send him the password 79064273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 791f3f0262cSandi msg($lang['regsuccess'], 1); 79264273335SAndreas Gohr notify('', 'register', '', $login, false, $substitutions); 793f3f0262cSandi return true; 794f3f0262cSandi } else { 795f3f0262cSandi msg($lang['regmailfail'], -1); 796f3f0262cSandi return false; 797f3f0262cSandi } 798f3f0262cSandi} 799f3f0262cSandi 80010a76f6fSfrank/** 8018b06d178Schris * Update user profile 8028b06d178Schris * 8038b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 8048b06d178Schris */ 8058b06d178Schrisfunction updateprofile() { 8068b06d178Schris global $conf; 8078b06d178Schris global $lang; 808ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 809cd52f92dSchris global $auth; 810bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 811bcc94b2cSAndreas Gohr global $INPUT; 8128b06d178Schris 813bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 8141b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 8158b06d178Schris 8163a48618aSAnika Henke if(!actionOK('profile')) { 8178b06d178Schris msg($lang['profna'], -1); 8188b06d178Schris return false; 8198b06d178Schris } 8208b06d178Schris 821bcc94b2cSAndreas Gohr $changes = array(); 822bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 823bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 824bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 825bcc94b2cSAndreas Gohr 826bcc94b2cSAndreas Gohr // check misspelled passwords 827bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 828bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 8298b06d178Schris return false; 8308b06d178Schris } 8318b06d178Schris 8328b06d178Schris // clean fullname and email 833bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 834bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 8358b06d178Schris 836bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 837bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 838bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 839ab5d26daSAndreas Gohr ) { 8408b06d178Schris msg($lang['profnoempty'], -1); 8418b06d178Schris return false; 8428b06d178Schris } 843bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 8448b06d178Schris msg($lang['regbadmail'], -1); 8458b06d178Schris return false; 8468b06d178Schris } 8478b06d178Schris 848bcc94b2cSAndreas Gohr $changes = array_filter($changes); 8494c21b7eeSAndreas Gohr 850bcc94b2cSAndreas Gohr // check for unavailable capabilities 851bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 852bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 853bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 854bcc94b2cSAndreas Gohr 855bcc94b2cSAndreas Gohr // anything to do? 8568b06d178Schris if(!count($changes)) { 8578b06d178Schris msg($lang['profnochange'], -1); 8588b06d178Schris return false; 8598b06d178Schris } 8608b06d178Schris 8618b06d178Schris if($conf['profileconfirm']) { 862bcc94b2cSAndreas Gohr if(!$auth->checkPass($_SERVER['REMOTE_USER'], $INPUT->post->str('oldpass'))) { 8638b06d178Schris msg($lang['badlogin'], -1); 8648b06d178Schris return false; 8658b06d178Schris } 8668b06d178Schris } 8678b06d178Schris 868a0b5b007SChris Smith if($result = $auth->triggerUserMod('modify', array($_SERVER['REMOTE_USER'], $changes))) { 869a0b5b007SChris Smith // update cookie and session with the changed data 87032ed2b36SAndreas Gohr if($changes['pass']) { 871ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 87232ed2b36SAndreas Gohr $pass = PMA_blowfish_encrypt($changes['pass'], auth_cookiesalt(!$sticky)); 873a0b5b007SChris Smith auth_setCookie($_SERVER['REMOTE_USER'], $pass, (bool) $sticky); 87432ed2b36SAndreas Gohr } 87525b2a98cSMichael Klier return true; 876a0b5b007SChris Smith } 877ab5d26daSAndreas Gohr 878ab5d26daSAndreas Gohr return false; 8798b06d178Schris} 8808b06d178Schris 8818b06d178Schris/** 8828b06d178Schris * Send a new password 8838b06d178Schris * 8841d5856cfSAndreas Gohr * This function handles both phases of the password reset: 8851d5856cfSAndreas Gohr * 8861d5856cfSAndreas Gohr * - handling the first request of password reset 8871d5856cfSAndreas Gohr * - validating the password reset auth token 8881d5856cfSAndreas Gohr * 8898b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 8908b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 8911d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 8928b06d178Schris * 8938b06d178Schris * @return bool true on success, false on any error 8948b06d178Schris */ 8958b06d178Schrisfunction act_resendpwd() { 8968b06d178Schris global $lang; 8978b06d178Schris global $conf; 898ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 899cd52f92dSchris global $auth; 900bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 901bcc94b2cSAndreas Gohr global $INPUT; 9028b06d178Schris 9033a48618aSAnika Henke if(!actionOK('resendpwd')) { 9048b06d178Schris msg($lang['resendna'], -1); 9058b06d178Schris return false; 9068b06d178Schris } 9078b06d178Schris 908bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 9098b06d178Schris 9101d5856cfSAndreas Gohr if($token) { 911cc204bbdSAndreas Gohr // we're in token phase - get user info from token 9121d5856cfSAndreas Gohr 9131d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 9141d5856cfSAndreas Gohr if(!@file_exists($tfile)) { 9151d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 916bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 9171d5856cfSAndreas Gohr return false; 9181d5856cfSAndreas Gohr } 9198a9735e3SAndreas Gohr // token is only valid for 3 days 9208a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 9218a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 922bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 9231d5856cfSAndreas Gohr @unlink($tfile); 9248a9735e3SAndreas Gohr return false; 9258a9735e3SAndreas Gohr } 9268a9735e3SAndreas Gohr 9278b06d178Schris $user = io_readfile($tfile); 928cd52f92dSchris $userinfo = $auth->getUserData($user); 9298b06d178Schris if(!$userinfo['mail']) { 9308b06d178Schris msg($lang['resendpwdnouser'], -1); 9318b06d178Schris return false; 9328b06d178Schris } 9338b06d178Schris 934cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 935bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 936bcc94b2cSAndreas Gohr 937cc204bbdSAndreas Gohr // password given correctly? 938bcc94b2cSAndreas Gohr if(!$pass) return false; 939bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 940451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 941cc204bbdSAndreas Gohr return false; 942cc204bbdSAndreas Gohr } 943cc204bbdSAndreas Gohr 944bcc94b2cSAndreas Gohr // change it 945cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 946cc204bbdSAndreas Gohr msg('error modifying user data', -1); 947cc204bbdSAndreas Gohr return false; 948cc204bbdSAndreas Gohr } 949cc204bbdSAndreas Gohr 950cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 951cc204bbdSAndreas Gohr 9528b06d178Schris $pass = auth_pwgen(); 9537d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 9548b06d178Schris msg('error modifying user data', -1); 9558b06d178Schris return false; 9568b06d178Schris } 9578b06d178Schris 9588b06d178Schris if(auth_sendPassword($user, $pass)) { 9598b06d178Schris msg($lang['resendpwdsuccess'], 1); 9608b06d178Schris } else { 9618b06d178Schris msg($lang['regmailfail'], -1); 9628b06d178Schris } 963cc204bbdSAndreas Gohr } 964cc204bbdSAndreas Gohr 965cc204bbdSAndreas Gohr @unlink($tfile); 9668b06d178Schris return true; 9671d5856cfSAndreas Gohr 9681d5856cfSAndreas Gohr } else { 9691d5856cfSAndreas Gohr // we're in request phase 9701d5856cfSAndreas Gohr 971bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9721d5856cfSAndreas Gohr 973bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 9741d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 9751d5856cfSAndreas Gohr return false; 9761d5856cfSAndreas Gohr } else { 977bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 9781d5856cfSAndreas Gohr } 9791d5856cfSAndreas Gohr 9801d5856cfSAndreas Gohr $userinfo = $auth->getUserData($user); 9811d5856cfSAndreas Gohr if(!$userinfo['mail']) { 9821d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 9831d5856cfSAndreas Gohr return false; 9841d5856cfSAndreas Gohr } 9851d5856cfSAndreas Gohr 9861d5856cfSAndreas Gohr // generate auth token 9871d5856cfSAndreas Gohr $token = md5(auth_cookiesalt().$user); //secret but user based 9881d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 9891d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 9901d5856cfSAndreas Gohr 9911d5856cfSAndreas Gohr io_saveFile($tfile, $user); 9921d5856cfSAndreas Gohr 9931d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 994d7169d19SAndreas Gohr $trep = array( 995d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 996d7169d19SAndreas Gohr 'LOGIN' => $user, 997d7169d19SAndreas Gohr 'CONFIRM' => $url 998d7169d19SAndreas Gohr ); 9991d5856cfSAndreas Gohr 1000d7169d19SAndreas Gohr $mail = new Mailer(); 1001d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1002d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1003d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1004d7169d19SAndreas Gohr if($mail->send()) { 10051d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 10061d5856cfSAndreas Gohr } else { 10071d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 10081d5856cfSAndreas Gohr } 10091d5856cfSAndreas Gohr return true; 10101d5856cfSAndreas Gohr } 1011ab5d26daSAndreas Gohr // never reached 10128b06d178Schris} 10138b06d178Schris 10148b06d178Schris/** 1015b0855b11Sandi * Encrypts a password using the given method and salt 1016b0855b11Sandi * 1017b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1018b0855b11Sandi * is chosen. 1019b0855b11Sandi * 1020b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 1021ab5d26daSAndreas Gohr * @param string $clear The clear text password 1022ab5d26daSAndreas Gohr * @param string $method The hashing method 1023ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1024b0855b11Sandi * @return string The crypted password 1025b0855b11Sandi */ 1026577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) { 1027b0855b11Sandi global $conf; 1028b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 102910a76f6fSfrank 10303a0a2d05SAndreas Gohr $pass = new PassHash(); 10313a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1032b0855b11Sandi 10333a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1034b0855b11Sandi msg("Unsupported crypt method $method", -1); 10353a0a2d05SAndreas Gohr return false; 1036b0855b11Sandi } 10373a0a2d05SAndreas Gohr 10383a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1039b0855b11Sandi} 1040b0855b11Sandi 1041b0855b11Sandi/** 1042b0855b11Sandi * Verifies a cleartext password against a crypted hash 1043b0855b11Sandi * 1044b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 1045ab5d26daSAndreas Gohr * @param string $clear The clear text password 1046ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1047ab5d26daSAndreas Gohr * @return bool true if both match 1048b0855b11Sandi */ 1049b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 10503a0a2d05SAndreas Gohr $pass = new PassHash(); 10513a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1052b0855b11Sandi} 1053340756e4Sandi 1054a0b5b007SChris Smith/** 1055a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1056a0b5b007SChris Smith * 1057a0b5b007SChris Smith * @param string $user username 1058a0b5b007SChris Smith * @param string $pass encrypted password 1059a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1060ab5d26daSAndreas Gohr * @return bool 1061a0b5b007SChris Smith */ 1062a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1063a0b5b007SChris Smith global $conf; 1064ab5d26daSAndreas Gohr /* @var auth_basic $auth */ 1065a0b5b007SChris Smith global $auth; 106679d00841SOliver Geisen global $USERINFO; 1067a0b5b007SChris Smith 1068beca106aSAdrian Lang if(!$auth) return false; 1069a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1070a0b5b007SChris Smith 1071a0b5b007SChris Smith // set cookie 1072645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 107373ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1074c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1075a0b5b007SChris Smith if(version_compare(PHP_VERSION, '5.2.0', '>')) { 107673ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 1077a0b5b007SChris Smith } else { 107873ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl())); 1079a0b5b007SChris Smith } 1080a0b5b007SChris Smith // set session 1081a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1082234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1083a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1084a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1085a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1086ab5d26daSAndreas Gohr 1087ab5d26daSAndreas Gohr return true; 1088a0b5b007SChris Smith} 1089a0b5b007SChris Smith 1090645c0a36SAndreas Gohr/** 1091645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1092645c0a36SAndreas Gohr * 1093645c0a36SAndreas Gohr * @returns array 1094645c0a36SAndreas Gohr */ 1095645c0a36SAndreas Gohrfunction auth_getCookie() { 1096c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1097c66972f2SAdrian Lang return array(null, null, null); 1098c66972f2SAdrian Lang } 1099645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1100645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1101645c0a36SAndreas Gohr $pass = base64_decode($pass); 1102645c0a36SAndreas Gohr $user = base64_decode($user); 1103645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1104645c0a36SAndreas Gohr} 1105645c0a36SAndreas Gohr 1106e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1107