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; 4327058a05SMichael Hamann /* @var Doku_Plugin_Controller $plugin_controller */ 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; 54e71b0ef7SGuy Brand } elseif ('auth' . $conf['authtype'] === $plugin) { 55e71b0ef7SGuy Brand // matches old auth backends (pre-Weatherwax) 56e71b0ef7SGuy Brand $auth = $plugin_controller->load('auth', $plugin); 57a91f1103SAnika Henke msg('Your authtype setting is deprecated. You must set $conf[\'authtype\'] = "auth' . $conf['authtype'] . '"' 5898e31f85SKlap-in . ' in your configuration (see <a href="https://www.dokuwiki.org/auth">Authentication Backends</a>)',-1,'','',MSG_ADMINS_ONLY); 599c29eea5SJan Schumann } 609c29eea5SJan Schumann } 618b06d178Schris 626416b708SMichael Hamann if(!isset($auth) || !$auth){ 633094e817SAndreas Gohr msg($lang['authtempfail'], -1); 643094e817SAndreas Gohr return false; 653094e817SAndreas Gohr } 668b06d178Schris 676416b708SMichael Hamann if ($auth->success == false) { 680f4f4adfSAndreas Gohr // degrade to unauthenticated user 69d2dde4ebSMatthias Grimm unset($auth); 700f4f4adfSAndreas Gohr auth_logoff(); 71cd52f92dSchris msg($lang['authtempfail'], -1); 726416b708SMichael Hamann return false; 73d2dde4ebSMatthias Grimm } 7416905344SAndreas Gohr 7516905344SAndreas Gohr // do the login either by cookie or provided credentials XXX 76bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', false); 77bcc94b2cSAndreas Gohr if(!$conf['rememberme']) $INPUT->set('r', false); 78bbbd6568SAndreas Gohr 79b2665af7SMichael Hamann // handle renamed HTTP_AUTHORIZATION variable (can happen when a fix like 80b2665af7SMichael Hamann // the one presented at 81b2665af7SMichael Hamann // http://www.besthostratings.com/articles/http-auth-php-cgi.html is used 82b2665af7SMichael Hamann // for enabling HTTP authentication with CGI/SuExec) 83b2665af7SMichael Hamann if(isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) 84b2665af7SMichael Hamann $_SERVER['HTTP_AUTHORIZATION'] = $_SERVER['REDIRECT_HTTP_AUTHORIZATION']; 85528ddc7cSAndreas Gohr // streamline HTTP auth credentials (IIS/rewrite -> mod_php) 8606156f3cSAndreas Gohr if(isset($_SERVER['HTTP_AUTHORIZATION'])) { 87528ddc7cSAndreas Gohr list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = 88528ddc7cSAndreas Gohr explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6))); 89528ddc7cSAndreas Gohr } 90528ddc7cSAndreas Gohr 911e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 92bcc94b2cSAndreas Gohr if(!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($_SERVER['PHP_AUTH_USER'])) { 93bcc94b2cSAndreas Gohr $INPUT->set('u', $_SERVER['PHP_AUTH_USER']); 94bcc94b2cSAndreas Gohr $INPUT->set('p', $_SERVER['PHP_AUTH_PW']); 95bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 961e8c9c90SAndreas Gohr } 971e8c9c90SAndreas Gohr 98395c2f0fSAndreas Gohr // apply cleaning (auth specific user names, remove control chars) 9993a7873eSAndreas Gohr if (true === $auth->success) { 100395c2f0fSAndreas Gohr $INPUT->set('u', $auth->cleanUser(stripctl($INPUT->str('u')))); 101395c2f0fSAndreas Gohr $INPUT->set('p', stripctl($INPUT->str('p'))); 102f4476bd9SJan Schumann } 103191bb90aSAndreas Gohr 104bcc94b2cSAndreas Gohr if($INPUT->str('authtok')) { 105f13fa892SAndreas Gohr // when an authentication token is given, trust the session 106bcc94b2cSAndreas Gohr auth_validateToken($INPUT->str('authtok')); 107f13fa892SAndreas Gohr } elseif(!is_null($auth) && $auth->canDo('external')) { 108f13fa892SAndreas Gohr // external trust mechanism in place 109bcc94b2cSAndreas Gohr $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 110f5cb575dSAndreas Gohr } else { 1116080c584SRobin Gareus $evdata = array( 112bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 113bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 114bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 115bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 1166080c584SRobin Gareus ); 117b5ee21aaSAdrian Lang trigger_event('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 118f5cb575dSAndreas Gohr } 119f5cb575dSAndreas Gohr 12016905344SAndreas Gohr //load ACL into a global array XXX 12175c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 122ab5d26daSAndreas Gohr 123ab5d26daSAndreas Gohr return true; 12475c93b77SAndreas Gohr} 12575c93b77SAndreas Gohr 12675c93b77SAndreas Gohr/** 12775c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 12875c93b77SAndreas Gohr * 12975c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 13042ea7f44SGerrit Uitslag * 131ab5d26daSAndreas Gohr * @return array 13275c93b77SAndreas Gohr */ 13375c93b77SAndreas Gohrfunction auth_loadACL() { 13475c93b77SAndreas Gohr global $config_cascade; 135b78bf706Sromain global $USERINFO; 136585bf44eSChristopher Smith /* @var Input $INPUT */ 137585bf44eSChristopher Smith global $INPUT; 13875c93b77SAndreas Gohr 13975c93b77SAndreas Gohr if(!is_readable($config_cascade['acl']['default'])) return array(); 14075c93b77SAndreas Gohr 14175c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 14275c93b77SAndreas Gohr 14332e82180SAndreas Gohr $out = array(); 1449ce556d2SAndreas Gohr foreach($acl as $line) { 1459ce556d2SAndreas Gohr $line = trim($line); 146443e135dSChristopher Smith if(empty($line) || ($line{0} == '#')) continue; // skip blank lines & comments 14721c3090aSChristopher Smith list($id,$rest) = preg_split('/[ \t]+/',$line,2); 14832e82180SAndreas Gohr 149443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 150ad3d68d7SChristopher Smith if(strstr($line, '%USER%')){ 151ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 152585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 153ad3d68d7SChristopher Smith 154585bf44eSChristopher Smith $id = str_replace('%USER%',cleanID($INPUT->server->str('REMOTE_USER')),$id); 155585bf44eSChristopher Smith $rest = str_replace('%USER%',auth_nameencode($INPUT->server->str('REMOTE_USER')),$rest); 156ad3d68d7SChristopher Smith } 157ad3d68d7SChristopher Smith 158ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1599ce556d2SAndreas Gohr if(strstr($line, '%GROUP%')){ 160ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 1619ce556d2SAndreas Gohr foreach((array) $USERINFO['grps'] as $grp){ 162b78bf706Sromain $nid = str_replace('%GROUP%',cleanID($grp),$id); 16332e82180SAndreas Gohr $nrest = str_replace('%GROUP%','@'.auth_nameencode($grp),$rest); 16432e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 165b78bf706Sromain } 16632e82180SAndreas Gohr } else { 16732e82180SAndreas Gohr $out[] = "$id\t$rest"; 168a8fe108bSGuy Brand } 16911799630Sandi } 1709ce556d2SAndreas Gohr 17132e82180SAndreas Gohr return $out; 172f3f0262cSandi} 173f3f0262cSandi 174ab5d26daSAndreas Gohr/** 175ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 176ab5d26daSAndreas Gohr * 17742ea7f44SGerrit Uitslag * @param array $evdata 178ab5d26daSAndreas Gohr * @return bool 179ab5d26daSAndreas Gohr */ 180b5ee21aaSAdrian Langfunction auth_login_wrapper($evdata) { 181ab5d26daSAndreas Gohr return auth_login( 182ab5d26daSAndreas Gohr $evdata['user'], 183b5ee21aaSAdrian Lang $evdata['password'], 184b5ee21aaSAdrian Lang $evdata['sticky'], 185ab5d26daSAndreas Gohr $evdata['silent'] 186ab5d26daSAndreas Gohr ); 187b5ee21aaSAdrian Lang} 188b5ee21aaSAdrian Lang 189f3f0262cSandi/** 190f3f0262cSandi * This tries to login the user based on the sent auth credentials 191f3f0262cSandi * 192f3f0262cSandi * The authentication works like this: if a username was given 19315fae107Sandi * a new login is assumed and user/password are checked. If they 19415fae107Sandi * are correct the password is encrypted with blowfish and stored 19515fae107Sandi * together with the username in a cookie - the same info is stored 19615fae107Sandi * in the session, too. Additonally a browserID is stored in the 19715fae107Sandi * session. 19815fae107Sandi * 19915fae107Sandi * If no username was given the cookie is checked: if the username, 20015fae107Sandi * crypted password and browserID match between session and cookie 20115fae107Sandi * no further testing is done and the user is accepted 20215fae107Sandi * 20315fae107Sandi * If a cookie was found but no session info was availabe the 204136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 20515fae107Sandi * together with username rechecked by calling this function again. 206f3f0262cSandi * 207f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 208f3f0262cSandi * are set. 20915fae107Sandi * 21015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 21115fae107Sandi * 21215fae107Sandi * @param string $user Username 21315fae107Sandi * @param string $pass Cleartext Password 21415fae107Sandi * @param bool $sticky Cookie should not expire 215f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 21615fae107Sandi * @return bool true on successful auth 217f3f0262cSandi */ 218f112c2faSAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) { 219f3f0262cSandi global $USERINFO; 220f3f0262cSandi global $conf; 221f3f0262cSandi global $lang; 22227058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 223cd52f92dSchris global $auth; 224585bf44eSChristopher Smith /* @var Input $INPUT */ 225585bf44eSChristopher Smith global $INPUT; 226ab5d26daSAndreas Gohr 227132bdbfeSandi $sticky ? $sticky = true : $sticky = false; //sanity check 228f3f0262cSandi 229beca106aSAdrian Lang if(!$auth) return false; 230beca106aSAdrian Lang 231bbbd6568SAndreas Gohr if(!empty($user)) { 232132bdbfeSandi //usual login 2335e9e1054SAndreas Gohr if(!empty($pass) && $auth->checkPass($user, $pass)) { 234132bdbfeSandi // make logininfo globally available 235585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 23630d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 23704369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 238132bdbfeSandi return true; 239f3f0262cSandi } else { 240f3f0262cSandi //invalid credentials - log off 241f112c2faSAndreas Gohr if(!$silent) msg($lang['badlogin'], -1); 242f3f0262cSandi auth_logoff(); 243132bdbfeSandi return false; 244f3f0262cSandi } 245f3f0262cSandi } else { 246132bdbfeSandi // read cookie information 247645c0a36SAndreas Gohr list($user, $sticky, $pass) = auth_getCookie(); 248132bdbfeSandi if($user && $pass) { 249132bdbfeSandi // we got a cookie - see if we can trust it 250fa7c70ffSAdrian Lang 251fa7c70ffSAdrian Lang // get session info 252fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 253132bdbfeSandi if(isset($session) && 2547172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2554c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 256132bdbfeSandi ($session['user'] == $user) && 257234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 258ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 259ab5d26daSAndreas Gohr ) { 260234ce57eSAndreas Gohr 261132bdbfeSandi // he has session, cookie and browser right - let him in 262585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 263132bdbfeSandi $USERINFO = $session['info']; //FIXME move all references to session 264132bdbfeSandi return true; 265132bdbfeSandi } 266f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 26730d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 26804369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 269f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 270132bdbfeSandi } 271132bdbfeSandi } 272f3f0262cSandi //just to be sure 273883179a4SAndreas Gohr auth_logoff(true); 274132bdbfeSandi return false; 275f3f0262cSandi} 276132bdbfeSandi 277132bdbfeSandi/** 278f13fa892SAndreas Gohr * Checks if a given authentication token was stored in the session 279f13fa892SAndreas Gohr * 280f13fa892SAndreas Gohr * Will setup authentication data using data from the session if the 281f13fa892SAndreas Gohr * token is correct. Will exit with a 401 Status if not. 282f13fa892SAndreas Gohr * 283f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 28442ea7f44SGerrit Uitslag * 285f13fa892SAndreas Gohr * @param string $token The authentication token 286*7e8500eeSGerrit Uitslag * @return boolean|null true (or will exit on failure) 287f13fa892SAndreas Gohr */ 288f13fa892SAndreas Gohrfunction auth_validateToken($token) { 289f13fa892SAndreas Gohr if(!$token || $token != $_SESSION[DOKU_COOKIE]['auth']['token']) { 290f13fa892SAndreas Gohr // bad token 2919d2e1be6SAndreas Gohr http_status(401); 292f13fa892SAndreas Gohr print 'Invalid auth token - maybe the session timed out'; 293f13fa892SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['token']); // no second chance 294f13fa892SAndreas Gohr exit; 295f13fa892SAndreas Gohr } 296f13fa892SAndreas Gohr // still here? trust the session data 297f13fa892SAndreas Gohr global $USERINFO; 298585bf44eSChristopher Smith /* @var Input $INPUT */ 299585bf44eSChristopher Smith global $INPUT; 300585bf44eSChristopher Smith 301585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER',$_SESSION[DOKU_COOKIE]['auth']['user']); 302f13fa892SAndreas Gohr $USERINFO = $_SESSION[DOKU_COOKIE]['auth']['info']; 303f13fa892SAndreas Gohr return true; 304f13fa892SAndreas Gohr} 305f13fa892SAndreas Gohr 306f13fa892SAndreas Gohr/** 307f13fa892SAndreas Gohr * Create an auth token and store it in the session 308f13fa892SAndreas Gohr * 309f13fa892SAndreas Gohr * NOTE: this is completely unrelated to the getSecurityToken() function 310f13fa892SAndreas Gohr * 311f13fa892SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 31242ea7f44SGerrit Uitslag * 313f13fa892SAndreas Gohr * @return string The auth token 314f13fa892SAndreas Gohr */ 315f13fa892SAndreas Gohrfunction auth_createToken() { 316483b6238SMichael Hamann $token = md5(auth_randombytes(16)); 31709c2d803SAndreas Gohr @session_start(); // reopen the session if needed 318f13fa892SAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['token'] = $token; 31909c2d803SAndreas Gohr session_write_close(); 320f13fa892SAndreas Gohr return $token; 321f13fa892SAndreas Gohr} 322f13fa892SAndreas Gohr 323f13fa892SAndreas Gohr/** 324136ce040Sandi * Builds a pseudo UID from browser and IP data 325132bdbfeSandi * 326132bdbfeSandi * This is neither unique nor unfakable - still it adds some 327136ce040Sandi * security. Using the first part of the IP makes sure 32880b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 32915fae107Sandi * 33015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 33115fae107Sandi * 33215fae107Sandi * @return string a MD5 sum of various browser headers 333132bdbfeSandi */ 334132bdbfeSandifunction auth_browseruid() { 335585bf44eSChristopher Smith /* @var Input $INPUT */ 336585bf44eSChristopher Smith global $INPUT; 337585bf44eSChristopher Smith 3382f9daf16SAndreas Gohr $ip = clientIP(true); 339132bdbfeSandi $uid = ''; 340585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_USER_AGENT'); 341585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_ACCEPT_ENCODING'); 342585bf44eSChristopher Smith $uid .= $INPUT->server->str('HTTP_ACCEPT_CHARSET'); 3432f9daf16SAndreas Gohr $uid .= substr($ip, 0, strpos($ip, '.')); 34480b4f376SAndreas Gohr $uid = strtolower($uid); 345132bdbfeSandi return md5($uid); 346132bdbfeSandi} 347132bdbfeSandi 348132bdbfeSandi/** 349132bdbfeSandi * Creates a random key to encrypt the password in cookies 35015fae107Sandi * 35115fae107Sandi * This function tries to read the password for encrypting 35298407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 35315fae107Sandi * if no such file is found a random key is created and 35415fae107Sandi * and stored in this file. 35515fae107Sandi * 35615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 35742ea7f44SGerrit Uitslag * 35832ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 35930d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 36015fae107Sandi * @return string 361132bdbfeSandi */ 36230d544a4SMichael Hamannfunction auth_cookiesalt($addsession = false, $secure = false) { 363132bdbfeSandi global $conf; 36498407a7aSandi $file = $conf['metadir'].'/_htcookiesalt'; 36530d544a4SMichael Hamann if ($secure || !file_exists($file)) { 36630d544a4SMichael Hamann $file = $conf['metadir'].'/_htcookiesalt2'; 36730d544a4SMichael Hamann } 368132bdbfeSandi $salt = io_readFile($file); 369132bdbfeSandi if(empty($salt)) { 37030d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 371132bdbfeSandi io_saveFile($file, $salt); 372132bdbfeSandi } 37332ed2b36SAndreas Gohr if($addsession) { 37432ed2b36SAndreas Gohr $salt .= session_id(); 37532ed2b36SAndreas Gohr } 376132bdbfeSandi return $salt; 377f3f0262cSandi} 378f3f0262cSandi 379f3f0262cSandi/** 380483b6238SMichael Hamann * Return truly (pseudo) random bytes if available, otherwise fall back to mt_rand 381483b6238SMichael Hamann * 382483b6238SMichael Hamann * @author Mark Seecof 383483b6238SMichael Hamann * @author Michael Hamann <michael@content-space.de> 384483b6238SMichael Hamann * @link http://www.php.net/manual/de/function.mt-rand.php#83655 38542ea7f44SGerrit Uitslag * 386483b6238SMichael Hamann * @param int $length number of bytes to get 387483b6238SMichael Hamann * @return string binary random strings 388483b6238SMichael Hamann */ 389483b6238SMichael Hamannfunction auth_randombytes($length) { 390483b6238SMichael Hamann $strong = false; 391483b6238SMichael Hamann $rbytes = false; 392483b6238SMichael Hamann 393483b6238SMichael Hamann if (function_exists('openssl_random_pseudo_bytes') 394483b6238SMichael Hamann && (version_compare(PHP_VERSION, '5.3.4') >= 0 395483b6238SMichael Hamann || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') 396483b6238SMichael Hamann ) { 397483b6238SMichael Hamann $rbytes = openssl_random_pseudo_bytes($length, $strong); 398483b6238SMichael Hamann } 399483b6238SMichael Hamann 400483b6238SMichael Hamann if (!$strong && function_exists('mcrypt_create_iv') 401483b6238SMichael Hamann && (version_compare(PHP_VERSION, '5.3.7') >= 0 402483b6238SMichael Hamann || strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') 403483b6238SMichael Hamann ) { 404483b6238SMichael Hamann $rbytes = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); 405483b6238SMichael Hamann if ($rbytes !== false && strlen($rbytes) === $length) { 406483b6238SMichael Hamann $strong = true; 407483b6238SMichael Hamann } 408483b6238SMichael Hamann } 409483b6238SMichael Hamann 410483b6238SMichael Hamann // If no strong randoms available, try OS the specific ways 411483b6238SMichael Hamann if(!$strong) { 412483b6238SMichael Hamann // Unix/Linux platform 413483b6238SMichael Hamann $fp = @fopen('/dev/urandom', 'rb'); 414483b6238SMichael Hamann if($fp !== false) { 415483b6238SMichael Hamann $rbytes = fread($fp, $length); 416483b6238SMichael Hamann fclose($fp); 417483b6238SMichael Hamann } 418483b6238SMichael Hamann 419483b6238SMichael Hamann // MS-Windows platform 420483b6238SMichael Hamann if(class_exists('COM')) { 421483b6238SMichael Hamann // http://msdn.microsoft.com/en-us/library/aa388176(VS.85).aspx 422483b6238SMichael Hamann try { 423483b6238SMichael Hamann $CAPI_Util = new COM('CAPICOM.Utilities.1'); 424483b6238SMichael Hamann $rbytes = $CAPI_Util->GetRandom($length, 0); 425483b6238SMichael Hamann 426483b6238SMichael Hamann // if we ask for binary data PHP munges it, so we 427483b6238SMichael Hamann // request base64 return value. 428483b6238SMichael Hamann if($rbytes) $rbytes = base64_decode($rbytes); 429483b6238SMichael Hamann } catch(Exception $ex) { 430483b6238SMichael Hamann // fail 431483b6238SMichael Hamann } 432483b6238SMichael Hamann } 433483b6238SMichael Hamann } 434483b6238SMichael Hamann if(strlen($rbytes) < $length) $rbytes = false; 435483b6238SMichael Hamann 436483b6238SMichael Hamann // still no random bytes available - fall back to mt_rand() 437483b6238SMichael Hamann if($rbytes === false) { 438483b6238SMichael Hamann $rbytes = ''; 439483b6238SMichael Hamann for ($i = 0; $i < $length; ++$i) { 440483b6238SMichael Hamann $rbytes .= chr(mt_rand(0, 255)); 441483b6238SMichael Hamann } 442483b6238SMichael Hamann } 443483b6238SMichael Hamann 444483b6238SMichael Hamann return $rbytes; 445483b6238SMichael Hamann} 446483b6238SMichael Hamann 447483b6238SMichael Hamann/** 448483b6238SMichael Hamann * Random number generator using the best available source 449483b6238SMichael Hamann * 450483b6238SMichael Hamann * @author Michael Samuel 451483b6238SMichael Hamann * @author Michael Hamann <michael@content-space.de> 45242ea7f44SGerrit Uitslag * 453483b6238SMichael Hamann * @param int $min 454483b6238SMichael Hamann * @param int $max 455483b6238SMichael Hamann * @return int 456483b6238SMichael Hamann */ 457483b6238SMichael Hamannfunction auth_random($min, $max) { 458483b6238SMichael Hamann $abs_max = $max - $min; 459483b6238SMichael Hamann 460483b6238SMichael Hamann $nbits = 0; 461483b6238SMichael Hamann for ($n = $abs_max; $n > 0; $n >>= 1) { 462483b6238SMichael Hamann ++$nbits; 463483b6238SMichael Hamann } 464483b6238SMichael Hamann 465483b6238SMichael Hamann $mask = (1 << $nbits) - 1; 466483b6238SMichael Hamann do { 467483b6238SMichael Hamann $bytes = auth_randombytes(PHP_INT_SIZE); 468483b6238SMichael Hamann $integers = unpack('Inum', $bytes); 469483b6238SMichael Hamann $integer = $integers["num"] & $mask; 470483b6238SMichael Hamann } while ($integer > $abs_max); 471483b6238SMichael Hamann 472483b6238SMichael Hamann return $min + $integer; 473483b6238SMichael Hamann} 474483b6238SMichael Hamann 475483b6238SMichael Hamann/** 47604369c3eSMichael Hamann * Encrypt data using the given secret using AES 47704369c3eSMichael Hamann * 47804369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 47904369c3eSMichael Hamann * using pbkdf2. 48004369c3eSMichael Hamann * 48104369c3eSMichael Hamann * @param string $data The data that shall be encrypted 48204369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 48304369c3eSMichael Hamann * @return string The ciphertext 48404369c3eSMichael Hamann */ 48504369c3eSMichael Hamannfunction auth_encrypt($data, $secret) { 48604369c3eSMichael Hamann $iv = auth_randombytes(16); 48704369c3eSMichael Hamann $cipher = new Crypt_AES(); 48804369c3eSMichael Hamann $cipher->setPassword($secret); 48904369c3eSMichael Hamann 4907b650cefSMichael Hamann /* 4917b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 4927b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 4937b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 4947b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 4957b650cefSMichael Hamann */ 49604369c3eSMichael Hamann return $cipher->encrypt($iv.$data); 49704369c3eSMichael Hamann} 49804369c3eSMichael Hamann 49904369c3eSMichael Hamann/** 50004369c3eSMichael Hamann * Decrypt the given AES ciphertext 50104369c3eSMichael Hamann * 50204369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 50304369c3eSMichael Hamann * 50404369c3eSMichael Hamann * @param string $ciphertext The encrypted data 50504369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 50604369c3eSMichael Hamann * @return string The decrypted data 50704369c3eSMichael Hamann */ 50804369c3eSMichael Hamannfunction auth_decrypt($ciphertext, $secret) { 5097b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 51004369c3eSMichael Hamann $cipher = new Crypt_AES(); 51104369c3eSMichael Hamann $cipher->setPassword($secret); 5127b650cefSMichael Hamann $cipher->setIV($iv); 51304369c3eSMichael Hamann 5147b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 51504369c3eSMichael Hamann} 51604369c3eSMichael Hamann 51704369c3eSMichael Hamann/** 518883179a4SAndreas Gohr * Log out the current user 519883179a4SAndreas Gohr * 520f3f0262cSandi * This clears all authentication data and thus log the user 521883179a4SAndreas Gohr * off. It also clears session data. 52215fae107Sandi * 52315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 52442ea7f44SGerrit Uitslag * 525883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 526f3f0262cSandi */ 527883179a4SAndreas Gohrfunction auth_logoff($keepbc = false) { 528f3f0262cSandi global $conf; 529f3f0262cSandi global $USERINFO; 53027058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 5315298a619SAndreas Gohr global $auth; 532585bf44eSChristopher Smith /* @var Input $INPUT */ 533585bf44eSChristopher Smith global $INPUT; 53437065e65Sandi 535d4869846SAndreas Gohr // make sure the session is writable (it usually is) 536e9621d07SAndreas Gohr @session_start(); 537e9621d07SAndreas Gohr 538e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 539e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 540e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 541e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 542e71ce681SAndreas Gohr if(isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 543e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 544883179a4SAndreas Gohr if(!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 545e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 546585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 547132bdbfeSandi $USERINFO = null; //FIXME 548f5c6743cSAndreas Gohr 54973ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 55073ab87deSGabriel Birke setcookie(DOKU_COOKIE, '', time() - 600000, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 5515298a619SAndreas Gohr 552880f62faSAndreas Gohr if($auth) $auth->logOff(); 553f3f0262cSandi} 554f3f0262cSandi 555f3f0262cSandi/** 556f8cc712eSAndreas Gohr * Check if a user is a manager 557f8cc712eSAndreas Gohr * 558f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 559f8cc712eSAndreas Gohr * user. 560f8cc712eSAndreas Gohr * 561f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 562f8cc712eSAndreas Gohr * 563f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 564f8cc712eSAndreas Gohr * @see auth_isadmin 56542ea7f44SGerrit Uitslag * 566ab5d26daSAndreas Gohr * @param string $user Username 567ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 568ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 569ab5d26daSAndreas Gohr * @return bool 570f8cc712eSAndreas Gohr */ 571f8cc712eSAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false) { 572f8cc712eSAndreas Gohr global $conf; 573f8cc712eSAndreas Gohr global $USERINFO; 57427058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 575d752aedeSAndreas Gohr global $auth; 576585bf44eSChristopher Smith /* @var Input $INPUT */ 577585bf44eSChristopher Smith global $INPUT; 578585bf44eSChristopher Smith 579f8cc712eSAndreas Gohr 580beca106aSAdrian Lang if(!$auth) return false; 581c66972f2SAdrian Lang if(is_null($user)) { 582585bf44eSChristopher Smith if(!$INPUT->server->has('REMOTE_USER')) { 583c66972f2SAdrian Lang return false; 584c66972f2SAdrian Lang } else { 585585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 586c66972f2SAdrian Lang } 587c66972f2SAdrian Lang } 588d6dc956fSAndreas Gohr if(is_null($groups)) { 589d6dc956fSAndreas Gohr $groups = (array) $USERINFO['grps']; 590e259aa79SAndreas Gohr } 591e259aa79SAndreas Gohr 592d6dc956fSAndreas Gohr // check superuser match 593d6dc956fSAndreas Gohr if(auth_isMember($conf['superuser'], $user, $groups)) return true; 594d6dc956fSAndreas Gohr if($adminonly) return false; 595e259aa79SAndreas Gohr // check managers 596d6dc956fSAndreas Gohr if(auth_isMember($conf['manager'], $user, $groups)) return true; 59700ce12daSChris Smith 598f8cc712eSAndreas Gohr return false; 599f8cc712eSAndreas Gohr} 600f8cc712eSAndreas Gohr 601f8cc712eSAndreas Gohr/** 602f8cc712eSAndreas Gohr * Check if a user is admin 603f8cc712eSAndreas Gohr * 604f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 605f8cc712eSAndreas Gohr * 606f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 607f8cc712eSAndreas Gohr * 608f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 609ab5d26daSAndreas Gohr * @see auth_ismanager() 61042ea7f44SGerrit Uitslag * 611ab5d26daSAndreas Gohr * @param string $user Username 612ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 613ab5d26daSAndreas Gohr * @return bool 614f8cc712eSAndreas Gohr */ 615f8cc712eSAndreas Gohrfunction auth_isadmin($user = null, $groups = null) { 616f8cc712eSAndreas Gohr return auth_ismanager($user, $groups, true); 617f8cc712eSAndreas Gohr} 618f8cc712eSAndreas Gohr 619d6dc956fSAndreas Gohr/** 620d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 621d6dc956fSAndreas Gohr * users and groups to determine membership status 622d6dc956fSAndreas Gohr * 623d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 624d6dc956fSAndreas Gohr * 62542ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 62642ea7f44SGerrit Uitslag * @param string $user user to match against 62742ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 6285446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 629d6dc956fSAndreas Gohr */ 630d6dc956fSAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) { 63127058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 632d6dc956fSAndreas Gohr global $auth; 633d6dc956fSAndreas Gohr if(!$auth) return false; 634d6dc956fSAndreas Gohr 635d6dc956fSAndreas Gohr // clean user and groups 6364f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) { 637d6dc956fSAndreas Gohr $user = utf8_strtolower($user); 638d6dc956fSAndreas Gohr $groups = array_map('utf8_strtolower', $groups); 639d6dc956fSAndreas Gohr } 640d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 641d6dc956fSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), $groups); 642d6dc956fSAndreas Gohr 643d6dc956fSAndreas Gohr // extract the memberlist 644d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 645d6dc956fSAndreas Gohr $members = array_map('trim', $members); 646d6dc956fSAndreas Gohr $members = array_unique($members); 647d6dc956fSAndreas Gohr $members = array_filter($members); 648d6dc956fSAndreas Gohr 649d6dc956fSAndreas Gohr // compare cleaned values 650d6dc956fSAndreas Gohr foreach($members as $member) { 651e5204a12SJurgen Hart if($member == '@ALL' ) return true; 6524f56ecbfSAdrian Lang if(!$auth->isCaseSensitive()) $member = utf8_strtolower($member); 653d6dc956fSAndreas Gohr if($member[0] == '@') { 654d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 655d6dc956fSAndreas Gohr if(in_array($member, $groups)) return true; 656d6dc956fSAndreas Gohr } else { 657d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 658d6dc956fSAndreas Gohr if($member == $user) return true; 659d6dc956fSAndreas Gohr } 660d6dc956fSAndreas Gohr } 661d6dc956fSAndreas Gohr 662d6dc956fSAndreas Gohr // still here? not a member! 663d6dc956fSAndreas Gohr return false; 664d6dc956fSAndreas Gohr} 665d6dc956fSAndreas Gohr 666f8cc712eSAndreas Gohr/** 66715fae107Sandi * Convinience function for auth_aclcheck() 66815fae107Sandi * 66915fae107Sandi * This checks the permissions for the current user 67015fae107Sandi * 67115fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 67215fae107Sandi * 6731698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 67415fae107Sandi * @return int permission level 675f3f0262cSandi */ 676f3f0262cSandifunction auth_quickaclcheck($id) { 677f3f0262cSandi global $conf; 678f3f0262cSandi global $USERINFO; 679585bf44eSChristopher Smith /* @var Input $INPUT */ 680585bf44eSChristopher Smith global $INPUT; 681f3f0262cSandi # if no ACL is used always return upload rights 682f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 683585bf44eSChristopher Smith return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']); 684f3f0262cSandi} 685f3f0262cSandi 686f3f0262cSandi/** 687c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 68815fae107Sandi * 68915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 69042ea7f44SGerrit Uitslag * 691c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 6921698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 69315fae107Sandi * @param string $user Username 6943272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 69515fae107Sandi * @return int permission level 696f3f0262cSandi */ 697f3f0262cSandifunction auth_aclcheck($id, $user, $groups) { 698c17acc9fSAndreas Gohr $data = array( 699c17acc9fSAndreas Gohr 'id' => $id, 700c17acc9fSAndreas Gohr 'user' => $user, 701c17acc9fSAndreas Gohr 'groups' => $groups 702c17acc9fSAndreas Gohr ); 703c17acc9fSAndreas Gohr 704c17acc9fSAndreas Gohr return trigger_event('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 705c17acc9fSAndreas Gohr} 706c17acc9fSAndreas Gohr 707c17acc9fSAndreas Gohr/** 708c17acc9fSAndreas Gohr * default ACL check method 709c17acc9fSAndreas Gohr * 710c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 711c17acc9fSAndreas Gohr * 712c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 71342ea7f44SGerrit Uitslag * 714c17acc9fSAndreas Gohr * @param array $data event data 715c17acc9fSAndreas Gohr * @return int permission level 716c17acc9fSAndreas Gohr */ 717c17acc9fSAndreas Gohrfunction auth_aclcheck_cb($data) { 718c17acc9fSAndreas Gohr $id =& $data['id']; 719c17acc9fSAndreas Gohr $user =& $data['user']; 720c17acc9fSAndreas Gohr $groups =& $data['groups']; 721c17acc9fSAndreas Gohr 722f3f0262cSandi global $conf; 723f3f0262cSandi global $AUTH_ACL; 72427058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 725d752aedeSAndreas Gohr global $auth; 726f3f0262cSandi 72785d03f68SAndreas Gohr // if no ACL is used always return upload rights 728f3f0262cSandi if(!$conf['useacl']) return AUTH_UPLOAD; 729beca106aSAdrian Lang if(!$auth) return AUTH_NONE; 730f3f0262cSandi 731074cf26bSandi //make sure groups is an array 732074cf26bSandi if(!is_array($groups)) $groups = array(); 733074cf26bSandi 73485d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 735ab5d26daSAndreas Gohr if(auth_isadmin($user, $groups)) { 736ab5d26daSAndreas Gohr return AUTH_ADMIN; 737ab5d26daSAndreas Gohr } 73885d03f68SAndreas Gohr 739eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive()) { 740eb3ce0d5SKazutaka Miyasaka $user = utf8_strtolower($user); 741eb3ce0d5SKazutaka Miyasaka $groups = array_map('utf8_strtolower', $groups); 742eb3ce0d5SKazutaka Miyasaka } 743d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 744d752aedeSAndreas Gohr $groups = array_map(array($auth, 'cleanGroup'), (array) $groups); 74585d03f68SAndreas Gohr $user = auth_nameencode($user); 74685d03f68SAndreas Gohr 7476c2bb100SAndreas Gohr //prepend groups with @ and nameencode 7482cd2db38Sandi $cnt = count($groups); 7492cd2db38Sandi for($i = 0; $i < $cnt; $i++) { 7506c2bb100SAndreas Gohr $groups[$i] = '@'.auth_nameencode($groups[$i]); 75110a76f6fSfrank } 75210a76f6fSfrank 753f3f0262cSandi $ns = getNS($id); 754f3f0262cSandi $perm = -1; 755f3f0262cSandi 75634aeb4afSAndreas Gohr if($user || count($groups)) { 757f3f0262cSandi //add ALL group 758f3f0262cSandi $groups[] = '@ALL'; 759f3f0262cSandi //add User 76034aeb4afSAndreas Gohr if($user) $groups[] = $user; 761f3f0262cSandi } else { 76248d7b7a6SDominik Eckelmann $groups[] = '@ALL'; 763f3f0262cSandi } 764f3f0262cSandi 765f3f0262cSandi //check exact match first 76621c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($id, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 767f3f0262cSandi if(count($matches)) { 768f3f0262cSandi foreach($matches as $match) { 769f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 77021c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 771eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 772eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 773eb3ce0d5SKazutaka Miyasaka } 77448d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 77548d7b7a6SDominik Eckelmann continue; 77648d7b7a6SDominik Eckelmann } 7778ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 778f3f0262cSandi if($acl[2] > $perm) { 779f3f0262cSandi $perm = $acl[2]; 780f3f0262cSandi } 781f3f0262cSandi } 782f3f0262cSandi if($perm > -1) { 783f3f0262cSandi //we had a match - return it 784def492a2SGuillaume Turri return (int) $perm; 785f3f0262cSandi } 786f3f0262cSandi } 787f3f0262cSandi 788f3f0262cSandi //still here? do the namespace checks 789f3f0262cSandi if($ns) { 7903e304b55SMichael Hamann $path = $ns.':*'; 791f3f0262cSandi } else { 7923e304b55SMichael Hamann $path = '*'; //root document 793f3f0262cSandi } 794f3f0262cSandi 795f3f0262cSandi do { 79621c3090aSChristopher Smith $matches = preg_grep('/^'.preg_quote($path, '/').'[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 797f3f0262cSandi if(count($matches)) { 798f3f0262cSandi foreach($matches as $match) { 799f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 80021c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 801eb3ce0d5SKazutaka Miyasaka if(!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 802eb3ce0d5SKazutaka Miyasaka $acl[1] = utf8_strtolower($acl[1]); 803eb3ce0d5SKazutaka Miyasaka } 80448d7b7a6SDominik Eckelmann if(!in_array($acl[1], $groups)) { 80548d7b7a6SDominik Eckelmann continue; 80648d7b7a6SDominik Eckelmann } 8078ef6b7caSandi if($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 808f3f0262cSandi if($acl[2] > $perm) { 809f3f0262cSandi $perm = $acl[2]; 810f3f0262cSandi } 811f3f0262cSandi } 812f3f0262cSandi //we had a match - return it 81348d7b7a6SDominik Eckelmann if($perm != -1) { 814def492a2SGuillaume Turri return (int) $perm; 815f3f0262cSandi } 81648d7b7a6SDominik Eckelmann } 817f3f0262cSandi //get next higher namespace 818f3f0262cSandi $ns = getNS($ns); 819f3f0262cSandi 8203e304b55SMichael Hamann if($path != '*') { 8213e304b55SMichael Hamann $path = $ns.':*'; 8223e304b55SMichael Hamann if($path == ':*') $path = '*'; 823f3f0262cSandi } else { 824f3f0262cSandi //we did this already 825f3f0262cSandi //looks like there is something wrong with the ACL 826f3f0262cSandi //break here 827d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 828d5ce66f6SAndreas Gohr return AUTH_NONE; 829f3f0262cSandi } 830f3f0262cSandi } while(1); //this should never loop endless 831ab5d26daSAndreas Gohr return AUTH_NONE; 832f3f0262cSandi} 833f3f0262cSandi 834f3f0262cSandi/** 8356c2bb100SAndreas Gohr * Encode ASCII special chars 8366c2bb100SAndreas Gohr * 8376c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 8386c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 8396c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 8406c2bb100SAndreas Gohr * urlencoding!). 8416c2bb100SAndreas Gohr * 8426c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 8436c2bb100SAndreas Gohr * 8446c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 8456c2bb100SAndreas Gohr * @see rawurldecode() 84642ea7f44SGerrit Uitslag * 84742ea7f44SGerrit Uitslag * @param string $name 84842ea7f44SGerrit Uitslag * @param bool $skip_group 84942ea7f44SGerrit Uitslag * @return string 8506c2bb100SAndreas Gohr */ 851e838fc2eSAndreas Gohrfunction auth_nameencode($name, $skip_group = false) { 852a424cd8eSchris global $cache_authname; 853a424cd8eSchris $cache =& $cache_authname; 85431784267SAndreas Gohr $name = (string) $name; 855a424cd8eSchris 85680601d26SAndreas Gohr // never encode wildcard FS#1955 85780601d26SAndreas Gohr if($name == '%USER%') return $name; 858b78bf706Sromain if($name == '%GROUP%') return $name; 85980601d26SAndreas Gohr 860a424cd8eSchris if(!isset($cache[$name][$skip_group])) { 861e838fc2eSAndreas Gohr if($skip_group && $name{0} == '@') { 86230f6faf0SChristopher Smith $cache[$name][$skip_group] = '@'.preg_replace_callback( 86330f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 86430f6faf0SChristopher Smith 'auth_nameencode_callback', substr($name, 1) 865ab5d26daSAndreas Gohr ); 866e838fc2eSAndreas Gohr } else { 86730f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 86830f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 86930f6faf0SChristopher Smith 'auth_nameencode_callback', $name 870ab5d26daSAndreas Gohr ); 871e838fc2eSAndreas Gohr } 8726c2bb100SAndreas Gohr } 8736c2bb100SAndreas Gohr 874a424cd8eSchris return $cache[$name][$skip_group]; 875a424cd8eSchris} 876a424cd8eSchris 87704d68ae4SGerrit Uitslag/** 87804d68ae4SGerrit Uitslag * callback encodes the matches 87904d68ae4SGerrit Uitslag * 88004d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 88104d68ae4SGerrit Uitslag * @return string 88204d68ae4SGerrit Uitslag */ 88330f6faf0SChristopher Smithfunction auth_nameencode_callback($matches) { 88430f6faf0SChristopher Smith return '%'.dechex(ord(substr($matches[1],-1))); 88530f6faf0SChristopher Smith} 88630f6faf0SChristopher Smith 8876c2bb100SAndreas Gohr/** 888f3f0262cSandi * Create a pronouncable password 889f3f0262cSandi * 8908a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8918a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8928a285f7fSAndreas Gohr * 89315fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 89415fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 8958a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 89615fae107Sandi * 8978a285f7fSAndreas Gohr * @param string $foruser username for which the password is generated 89815fae107Sandi * @return string pronouncable password 899f3f0262cSandi */ 9008a285f7fSAndreas Gohrfunction auth_pwgen($foruser = '') { 9018a285f7fSAndreas Gohr $data = array( 902d628dcf3SAndreas Gohr 'password' => '', 903d628dcf3SAndreas Gohr 'foruser' => $foruser 9048a285f7fSAndreas Gohr ); 9058a285f7fSAndreas Gohr 9068a285f7fSAndreas Gohr $evt = new Doku_Event('AUTH_PASSWORD_GENERATE', $data); 9078a285f7fSAndreas Gohr if($evt->advise_before(true)) { 908f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 909f3f0262cSandi $v = 'aeiou'; //vowels 910f3f0262cSandi $a = $c.$v; //both 911987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 912f3f0262cSandi 913987c8d26SAndreas Gohr //use thre syllables... 914987c8d26SAndreas Gohr for($i = 0; $i < 3; $i++) { 915483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 916483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 917483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 918f3f0262cSandi } 919987c8d26SAndreas Gohr //... and add a nice number and special 920483b6238SMichael Hamann $data['password'] .= auth_random(10, 99).$s[auth_random(0, strlen($s) - 1)]; 9218a285f7fSAndreas Gohr } 9228a285f7fSAndreas Gohr $evt->advise_after(); 923f3f0262cSandi 9248a285f7fSAndreas Gohr return $data['password']; 925f3f0262cSandi} 926f3f0262cSandi 927f3f0262cSandi/** 928f3f0262cSandi * Sends a password to the given user 929f3f0262cSandi * 93015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 93142ea7f44SGerrit Uitslag * 932ab5d26daSAndreas Gohr * @param string $user Login name of the user 933ab5d26daSAndreas Gohr * @param string $password The new password in clear text 93415fae107Sandi * @return bool true on success 935f3f0262cSandi */ 936f3f0262cSandifunction auth_sendPassword($user, $password) { 937f3f0262cSandi global $lang; 93827058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 939cd52f92dSchris global $auth; 940beca106aSAdrian Lang if(!$auth) return false; 941cd52f92dSchris 942d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 9432dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 944f3f0262cSandi 94587ddda95Sandi if(!$userinfo['mail']) return false; 946f3f0262cSandi 947f3f0262cSandi $text = rawLocale('password'); 948d7169d19SAndreas Gohr $trep = array( 949d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 950d7169d19SAndreas Gohr 'LOGIN' => $user, 951d7169d19SAndreas Gohr 'PASSWORD' => $password 952d7169d19SAndreas Gohr ); 953f3f0262cSandi 954d7169d19SAndreas Gohr $mail = new Mailer(); 955d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 956d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 957d7169d19SAndreas Gohr $mail->setBody($text, $trep); 958d7169d19SAndreas Gohr return $mail->send(); 959f3f0262cSandi} 960f3f0262cSandi 961f3f0262cSandi/** 96215fae107Sandi * Register a new user 963f3f0262cSandi * 96415fae107Sandi * This registers a new user - Data is read directly from $_POST 96515fae107Sandi * 96615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 96742ea7f44SGerrit Uitslag * 96815fae107Sandi * @return bool true on success, false on any error 969f3f0262cSandi */ 970f3f0262cSandifunction register() { 971f3f0262cSandi global $lang; 972eb5d07e4Sjan global $conf; 97327058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 974cd52f92dSchris global $auth; 97564273335SAndreas Gohr global $INPUT; 976f3f0262cSandi 97764273335SAndreas Gohr if(!$INPUT->post->bool('save')) return false; 9783a48618aSAnika Henke if(!actionOK('register')) return false; 979640145a5Sandi 98064273335SAndreas Gohr // gather input 98164273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 98264273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 98364273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 98464273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 98564273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 986d752aedeSAndreas Gohr 98764273335SAndreas Gohr if(empty($login) || empty($fullname) || empty($email)) { 988f3f0262cSandi msg($lang['regmissing'], -1); 989f3f0262cSandi return false; 990f3f0262cSandi } 991f3f0262cSandi 992cab2716aSmatthias.grimm if($conf['autopasswd']) { 9938a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 99464273335SAndreas Gohr } elseif(empty($pass) || empty($passchk)) { 995bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 996cab2716aSmatthias.grimm return false; 99764273335SAndreas Gohr } elseif($pass != $passchk) { 998bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 999cab2716aSmatthias.grimm return false; 1000cab2716aSmatthias.grimm } 1001cab2716aSmatthias.grimm 1002f3f0262cSandi //check mail 100364273335SAndreas Gohr if(!mail_isvalid($email)) { 1004f3f0262cSandi msg($lang['regbadmail'], -1); 1005f3f0262cSandi return false; 1006f3f0262cSandi } 1007f3f0262cSandi 1008f3f0262cSandi //okay try to create the user 100964273335SAndreas Gohr if(!$auth->triggerUserMod('create', array($login, $pass, $fullname, $email))) { 1010f3f0262cSandi msg($lang['reguexists'], -1); 1011f3f0262cSandi return false; 1012f3f0262cSandi } 1013f3f0262cSandi 1014790b7720SAndreas Gohr // send notification about the new user 1015790b7720SAndreas Gohr $subscription = new Subscription(); 1016790b7720SAndreas Gohr $subscription->send_register($login, $fullname, $email); 101702a498e7Schris 1018790b7720SAndreas Gohr // are we done? 1019cab2716aSmatthias.grimm if(!$conf['autopasswd']) { 1020cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 1021cab2716aSmatthias.grimm return true; 1022cab2716aSmatthias.grimm } 1023cab2716aSmatthias.grimm 1024790b7720SAndreas Gohr // autogenerated password? then send password to user 102564273335SAndreas Gohr if(auth_sendPassword($login, $pass)) { 1026f3f0262cSandi msg($lang['regsuccess'], 1); 1027f3f0262cSandi return true; 1028f3f0262cSandi } else { 1029f3f0262cSandi msg($lang['regmailfail'], -1); 1030f3f0262cSandi return false; 1031f3f0262cSandi } 1032f3f0262cSandi} 1033f3f0262cSandi 103410a76f6fSfrank/** 10358b06d178Schris * Update user profile 10368b06d178Schris * 10378b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 10388b06d178Schris */ 10398b06d178Schrisfunction updateprofile() { 10408b06d178Schris global $conf; 10418b06d178Schris global $lang; 104227058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1043cd52f92dSchris global $auth; 1044bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1045bcc94b2cSAndreas Gohr global $INPUT; 10468b06d178Schris 1047bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 10481b2a85e8SAndreas Gohr if(!checkSecurityToken()) return false; 10498b06d178Schris 10503a48618aSAnika Henke if(!actionOK('profile')) { 10518b06d178Schris msg($lang['profna'], -1); 10528b06d178Schris return false; 10538b06d178Schris } 10548b06d178Schris 1055bcc94b2cSAndreas Gohr $changes = array(); 1056bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 1057bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 1058bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 1059bcc94b2cSAndreas Gohr 1060bcc94b2cSAndreas Gohr // check misspelled passwords 1061bcc94b2cSAndreas Gohr if($changes['pass'] != $INPUT->post->str('passchk')) { 1062bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 10638b06d178Schris return false; 10648b06d178Schris } 10658b06d178Schris 10668b06d178Schris // clean fullname and email 1067bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 1068bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 10698b06d178Schris 1070bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 1071bcc94b2cSAndreas Gohr if((empty($changes['name']) && $auth->canDo('modName')) || 1072bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 1073ab5d26daSAndreas Gohr ) { 10748b06d178Schris msg($lang['profnoempty'], -1); 10758b06d178Schris return false; 10768b06d178Schris } 1077bcc94b2cSAndreas Gohr if(!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10788b06d178Schris msg($lang['regbadmail'], -1); 10798b06d178Schris return false; 10808b06d178Schris } 10818b06d178Schris 1082bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10834c21b7eeSAndreas Gohr 1084bcc94b2cSAndreas Gohr // check for unavailable capabilities 1085bcc94b2cSAndreas Gohr if(!$auth->canDo('modName')) unset($changes['name']); 1086bcc94b2cSAndreas Gohr if(!$auth->canDo('modMail')) unset($changes['mail']); 1087bcc94b2cSAndreas Gohr if(!$auth->canDo('modPass')) unset($changes['pass']); 1088bcc94b2cSAndreas Gohr 1089bcc94b2cSAndreas Gohr // anything to do? 10908b06d178Schris if(!count($changes)) { 10918b06d178Schris msg($lang['profnochange'], -1); 10928b06d178Schris return false; 10938b06d178Schris } 10948b06d178Schris 10958b06d178Schris if($conf['profileconfirm']) { 1096585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 109771422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 10988b06d178Schris return false; 10998b06d178Schris } 11008b06d178Schris } 11018b06d178Schris 110249cd1ed0SAndreas Gohr if($result = $auth->triggerUserMod('modify', array($INPUT->server->str('REMOTE_USER'), &$changes))) { 1103a0b5b007SChris Smith // update cookie and session with the changed data 110432ed2b36SAndreas Gohr if($changes['pass']) { 1105ab5d26daSAndreas Gohr list( /*user*/, $sticky, /*pass*/) = auth_getCookie(); 110604369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1107585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 110832ed2b36SAndreas Gohr } 110925b2a98cSMichael Klier return true; 1110a0b5b007SChris Smith } 1111ab5d26daSAndreas Gohr 1112ab5d26daSAndreas Gohr return false; 11138b06d178Schris} 11148b06d178Schris 111504d68ae4SGerrit Uitslag/** 111604d68ae4SGerrit Uitslag * Delete the current logged-in user 111704d68ae4SGerrit Uitslag * 111804d68ae4SGerrit Uitslag * @return bool true on success, false on any error 111904d68ae4SGerrit Uitslag */ 11202a7abf2dSChristopher Smithfunction auth_deleteprofile(){ 11212a7abf2dSChristopher Smith global $conf; 11222a7abf2dSChristopher Smith global $lang; 112373012efdSChristopher Smith /* @var DokuWiki_Auth_Plugin $auth */ 11242a7abf2dSChristopher Smith global $auth; 11252a7abf2dSChristopher Smith /* @var Input $INPUT */ 11262a7abf2dSChristopher Smith global $INPUT; 11272a7abf2dSChristopher Smith 11282a7abf2dSChristopher Smith if(!$INPUT->post->bool('delete')) return false; 11292a7abf2dSChristopher Smith if(!checkSecurityToken()) return false; 11302a7abf2dSChristopher Smith 11312a7abf2dSChristopher Smith // action prevented or auth module disallows 11322a7abf2dSChristopher Smith if(!actionOK('profile_delete') || !$auth->canDo('delUser')) { 11332a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 11342a7abf2dSChristopher Smith return false; 11352a7abf2dSChristopher Smith } 11362a7abf2dSChristopher Smith 11372a7abf2dSChristopher Smith if(!$INPUT->post->bool('confirm_delete')){ 11382a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 11392a7abf2dSChristopher Smith return false; 11402a7abf2dSChristopher Smith } 11412a7abf2dSChristopher Smith 11422a7abf2dSChristopher Smith if($conf['profileconfirm']) { 1143585bf44eSChristopher Smith if(!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 11442a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 11452a7abf2dSChristopher Smith return false; 11462a7abf2dSChristopher Smith } 11472a7abf2dSChristopher Smith } 11482a7abf2dSChristopher Smith 114959bc3b48SGerrit Uitslag $deleted = array(); 1150585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 115173012efdSChristopher Smith if($auth->triggerUserMod('delete', array($deleted))) { 11522a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 11532a7abf2dSChristopher Smith auth_logoff(); 11542a7abf2dSChristopher Smith return true; 11552a7abf2dSChristopher Smith } 11562a7abf2dSChristopher Smith 11572a7abf2dSChristopher Smith return false; 11582a7abf2dSChristopher Smith} 11592a7abf2dSChristopher Smith 11608b06d178Schris/** 11618b06d178Schris * Send a new password 11628b06d178Schris * 11631d5856cfSAndreas Gohr * This function handles both phases of the password reset: 11641d5856cfSAndreas Gohr * 11651d5856cfSAndreas Gohr * - handling the first request of password reset 11661d5856cfSAndreas Gohr * - validating the password reset auth token 11671d5856cfSAndreas Gohr * 11688b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 11698b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 11701d5856cfSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 11718b06d178Schris * 11728b06d178Schris * @return bool true on success, false on any error 11738b06d178Schris */ 11748b06d178Schrisfunction act_resendpwd() { 11758b06d178Schris global $lang; 11768b06d178Schris global $conf; 117727058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1178cd52f92dSchris global $auth; 1179bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1180bcc94b2cSAndreas Gohr global $INPUT; 11818b06d178Schris 11823a48618aSAnika Henke if(!actionOK('resendpwd')) { 11838b06d178Schris msg($lang['resendna'], -1); 11848b06d178Schris return false; 11858b06d178Schris } 11868b06d178Schris 1187bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 11888b06d178Schris 11891d5856cfSAndreas Gohr if($token) { 1190cc204bbdSAndreas Gohr // we're in token phase - get user info from token 11911d5856cfSAndreas Gohr 11921d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 11931d5856cfSAndreas Gohr if(!@file_exists($tfile)) { 11941d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1195bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11961d5856cfSAndreas Gohr return false; 11971d5856cfSAndreas Gohr } 11988a9735e3SAndreas Gohr // token is only valid for 3 days 11998a9735e3SAndreas Gohr if((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 12008a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1201bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 12021d5856cfSAndreas Gohr @unlink($tfile); 12038a9735e3SAndreas Gohr return false; 12048a9735e3SAndreas Gohr } 12058a9735e3SAndreas Gohr 12068b06d178Schris $user = io_readfile($tfile); 12072dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 12088b06d178Schris if(!$userinfo['mail']) { 12098b06d178Schris msg($lang['resendpwdnouser'], -1); 12108b06d178Schris return false; 12118b06d178Schris } 12128b06d178Schris 1213cc204bbdSAndreas Gohr if(!$conf['autopasswd']) { // we let the user choose a password 1214bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1215bcc94b2cSAndreas Gohr 1216cc204bbdSAndreas Gohr // password given correctly? 1217bcc94b2cSAndreas Gohr if(!$pass) return false; 1218bcc94b2cSAndreas Gohr if($pass != $INPUT->str('passchk')) { 1219451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1220cc204bbdSAndreas Gohr return false; 1221cc204bbdSAndreas Gohr } 1222cc204bbdSAndreas Gohr 1223bcc94b2cSAndreas Gohr // change it 1224cc204bbdSAndreas Gohr if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 1225cc204bbdSAndreas Gohr msg('error modifying user data', -1); 1226cc204bbdSAndreas Gohr return false; 1227cc204bbdSAndreas Gohr } 1228cc204bbdSAndreas Gohr 1229cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 1230cc204bbdSAndreas Gohr 12318a285f7fSAndreas Gohr $pass = auth_pwgen($user); 12327d3c8d42SGabriel Birke if(!$auth->triggerUserMod('modify', array($user, array('pass' => $pass)))) { 12338b06d178Schris msg('error modifying user data', -1); 12348b06d178Schris return false; 12358b06d178Schris } 12368b06d178Schris 12378b06d178Schris if(auth_sendPassword($user, $pass)) { 12388b06d178Schris msg($lang['resendpwdsuccess'], 1); 12398b06d178Schris } else { 12408b06d178Schris msg($lang['regmailfail'], -1); 12418b06d178Schris } 1242cc204bbdSAndreas Gohr } 1243cc204bbdSAndreas Gohr 1244cc204bbdSAndreas Gohr @unlink($tfile); 12458b06d178Schris return true; 12461d5856cfSAndreas Gohr 12471d5856cfSAndreas Gohr } else { 12481d5856cfSAndreas Gohr // we're in request phase 12491d5856cfSAndreas Gohr 1250bcc94b2cSAndreas Gohr if(!$INPUT->post->bool('save')) return false; 12511d5856cfSAndreas Gohr 1252bcc94b2cSAndreas Gohr if(!$INPUT->post->str('login')) { 12531d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 12541d5856cfSAndreas Gohr return false; 12551d5856cfSAndreas Gohr } else { 1256bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 12571d5856cfSAndreas Gohr } 12581d5856cfSAndreas Gohr 12592dc9e900SChristopher Smith $userinfo = $auth->getUserData($user, $requireGroups = false); 12601d5856cfSAndreas Gohr if(!$userinfo['mail']) { 12611d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 12621d5856cfSAndreas Gohr return false; 12631d5856cfSAndreas Gohr } 12641d5856cfSAndreas Gohr 12651d5856cfSAndreas Gohr // generate auth token 1266483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 12671d5856cfSAndreas Gohr $tfile = $conf['cachedir'].'/'.$token{0}.'/'.$token.'.pwauth'; 12681d5856cfSAndreas Gohr $url = wl('', array('do'=> 'resendpwd', 'pwauth'=> $token), true, '&'); 12691d5856cfSAndreas Gohr 12701d5856cfSAndreas Gohr io_saveFile($tfile, $user); 12711d5856cfSAndreas Gohr 12721d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 1273d7169d19SAndreas Gohr $trep = array( 1274d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 1275d7169d19SAndreas Gohr 'LOGIN' => $user, 1276d7169d19SAndreas Gohr 'CONFIRM' => $url 1277d7169d19SAndreas Gohr ); 12781d5856cfSAndreas Gohr 1279d7169d19SAndreas Gohr $mail = new Mailer(); 1280d7169d19SAndreas Gohr $mail->to($userinfo['name'].' <'.$userinfo['mail'].'>'); 1281d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1282d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1283d7169d19SAndreas Gohr if($mail->send()) { 12841d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 12851d5856cfSAndreas Gohr } else { 12861d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 12871d5856cfSAndreas Gohr } 12881d5856cfSAndreas Gohr return true; 12891d5856cfSAndreas Gohr } 1290ab5d26daSAndreas Gohr // never reached 12918b06d178Schris} 12928b06d178Schris 12938b06d178Schris/** 1294b0855b11Sandi * Encrypts a password using the given method and salt 1295b0855b11Sandi * 1296b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1297b0855b11Sandi * is chosen. 1298b0855b11Sandi * 1299b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 130042ea7f44SGerrit Uitslag * 1301ab5d26daSAndreas Gohr * @param string $clear The clear text password 1302ab5d26daSAndreas Gohr * @param string $method The hashing method 1303ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1304b0855b11Sandi * @return string The crypted password 1305b0855b11Sandi */ 1306577c7cdaSAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) { 1307b0855b11Sandi global $conf; 1308b0855b11Sandi if(empty($method)) $method = $conf['passcrypt']; 130910a76f6fSfrank 13103a0a2d05SAndreas Gohr $pass = new PassHash(); 13113a0a2d05SAndreas Gohr $call = 'hash_'.$method; 1312b0855b11Sandi 13133a0a2d05SAndreas Gohr if(!method_exists($pass, $call)) { 1314b0855b11Sandi msg("Unsupported crypt method $method", -1); 13153a0a2d05SAndreas Gohr return false; 1316b0855b11Sandi } 13173a0a2d05SAndreas Gohr 13183a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1319b0855b11Sandi} 1320b0855b11Sandi 1321b0855b11Sandi/** 1322b0855b11Sandi * Verifies a cleartext password against a crypted hash 1323b0855b11Sandi * 1324b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 132542ea7f44SGerrit Uitslag * 1326ab5d26daSAndreas Gohr * @param string $clear The clear text password 1327ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1328ab5d26daSAndreas Gohr * @return bool true if both match 1329b0855b11Sandi */ 1330b0855b11Sandifunction auth_verifyPassword($clear, $crypt) { 13313a0a2d05SAndreas Gohr $pass = new PassHash(); 13323a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1333b0855b11Sandi} 1334340756e4Sandi 1335a0b5b007SChris Smith/** 1336a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1337a0b5b007SChris Smith * 1338a0b5b007SChris Smith * @param string $user username 1339a0b5b007SChris Smith * @param string $pass encrypted password 1340a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1341ab5d26daSAndreas Gohr * @return bool 1342a0b5b007SChris Smith */ 1343a0b5b007SChris Smithfunction auth_setCookie($user, $pass, $sticky) { 1344a0b5b007SChris Smith global $conf; 134527058a05SMichael Hamann /* @var DokuWiki_Auth_Plugin $auth */ 1346a0b5b007SChris Smith global $auth; 134779d00841SOliver Geisen global $USERINFO; 1348a0b5b007SChris Smith 1349beca106aSAdrian Lang if(!$auth) return false; 1350a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1351a0b5b007SChris Smith 1352a0b5b007SChris Smith // set cookie 1353645c0a36SAndreas Gohr $cookie = base64_encode($user).'|'.((int) $sticky).'|'.base64_encode($pass); 135473ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1355c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 135673ab87deSGabriel Birke setcookie(DOKU_COOKIE, $cookie, $time, $cookieDir, '', ($conf['securecookie'] && is_ssl()), true); 135755a71a16SGerrit Uitslag 1358a0b5b007SChris Smith // set session 1359a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1360234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1361a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1362a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1363a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1364ab5d26daSAndreas Gohr 1365ab5d26daSAndreas Gohr return true; 1366a0b5b007SChris Smith} 1367a0b5b007SChris Smith 1368645c0a36SAndreas Gohr/** 1369645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1370645c0a36SAndreas Gohr * 1371645c0a36SAndreas Gohr * @returns array 1372645c0a36SAndreas Gohr */ 1373645c0a36SAndreas Gohrfunction auth_getCookie() { 1374c66972f2SAdrian Lang if(!isset($_COOKIE[DOKU_COOKIE])) { 1375c66972f2SAdrian Lang return array(null, null, null); 1376c66972f2SAdrian Lang } 1377645c0a36SAndreas Gohr list($user, $sticky, $pass) = explode('|', $_COOKIE[DOKU_COOKIE], 3); 1378645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1379645c0a36SAndreas Gohr $pass = base64_decode($pass); 1380645c0a36SAndreas Gohr $user = base64_decode($user); 1381645c0a36SAndreas Gohr return array($user, $sticky, $pass); 1382645c0a36SAndreas Gohr} 1383645c0a36SAndreas Gohr 1384e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1385