1ed7b5f09Sandi<?php 2d4f83172SAndreas Gohr 315fae107Sandi/** 415fae107Sandi * Authentication library 515fae107Sandi * 615fae107Sandi * Including this file will automatically try to login 715fae107Sandi * a user by calling auth_login() 815fae107Sandi * 915fae107Sandi * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 1015fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 1115fae107Sandi */ 12d4f83172SAndreas Gohr 1324870174SAndreas Gohruse phpseclib\Crypt\AES; 1424870174SAndreas Gohruse dokuwiki\Utf8\PhpString; 1596348f27SAndreas Gohruse dokuwiki\Extension\AuthPlugin; 1696348f27SAndreas Gohruse dokuwiki\Extension\Event; 1796348f27SAndreas Gohruse dokuwiki\Extension\PluginController; 18c3cc6e05SAndreas Gohruse dokuwiki\PassHash; 1975d66495SMichael Großeuse dokuwiki\Subscriptions\RegistrationSubscriptionSender; 20c3cc6e05SAndreas Gohr 2116905344SAndreas Gohr/** 2216905344SAndreas Gohr * Initialize the auth system. 2316905344SAndreas Gohr * 2416905344SAndreas Gohr * This function is automatically called at the end of init.php 2516905344SAndreas Gohr * 2616905344SAndreas Gohr * This used to be the main() of the auth.php 2716905344SAndreas Gohr * 2816905344SAndreas Gohr * @todo backend loading maybe should be handled by the class autoloader 2916905344SAndreas Gohr * @todo maybe split into multiple functions at the XXX marked positions 30ab5d26daSAndreas Gohr * @triggers AUTH_LOGIN_CHECK 31ab5d26daSAndreas Gohr * @return bool 3216905344SAndreas Gohr */ 33d868eb89SAndreas Gohrfunction auth_setup() 34d868eb89SAndreas Gohr{ 35742c66f8Schris global $conf; 36e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 3703c4aec3Schris global $auth; 38bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 39bcc94b2cSAndreas Gohr global $INPUT; 409a9714acSDominik Eckelmann global $AUTH_ACL; 419a9714acSDominik Eckelmann global $lang; 423a7140a1SAndreas Gohr /* @var PluginController $plugin_controller */ 439c29eea5SJan Schumann global $plugin_controller; 4424870174SAndreas Gohr $AUTH_ACL = []; 4503c4aec3Schris 4616905344SAndreas Gohr if (!$conf['useacl']) return false; 4716905344SAndreas Gohr 489c29eea5SJan Schumann // try to load auth backend from plugins 499c29eea5SJan Schumann foreach ($plugin_controller->getList('auth') as $plugin) { 509c29eea5SJan Schumann if ($conf['authtype'] === $plugin) { 51f4476bd9SJan Schumann $auth = $plugin_controller->load('auth', $plugin); 529c29eea5SJan Schumann break; 539c29eea5SJan Schumann } 549c29eea5SJan Schumann } 558b06d178Schris 566416b708SMichael Hamann if (!isset($auth) || !$auth) { 573094e817SAndreas Gohr msg($lang['authtempfail'], -1); 583094e817SAndreas Gohr return false; 593094e817SAndreas Gohr } 608b06d178Schris 616416b708SMichael Hamann if ($auth->success == false) { 620f4f4adfSAndreas Gohr // degrade to unauthenticated user 63ecad51ddSAndreas Gohr $auth = null; 640f4f4adfSAndreas Gohr auth_logoff(); 65cd52f92dSchris msg($lang['authtempfail'], -1); 666416b708SMichael Hamann return false; 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 7362bf3ac0SDamien Regad // Populate Basic Auth user/password from Authorization header 7462bf3ac0SDamien Regad // Note: with FastCGI, data is in REDIRECT_HTTP_AUTHORIZATION instead of HTTP_AUTHORIZATION 7562bf3ac0SDamien Regad $header = $INPUT->server->str('HTTP_AUTHORIZATION') ?: $INPUT->server->str('REDIRECT_HTTP_AUTHORIZATION'); 7662bf3ac0SDamien Regad if (preg_match('~^Basic ([a-z\d/+]*={0,2})$~i', $header, $matches)) { 7762bf3ac0SDamien Regad $userpass = explode(':', base64_decode($matches[1])); 7824870174SAndreas Gohr [$_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']] = $userpass; 79528ddc7cSAndreas Gohr } 80528ddc7cSAndreas Gohr 811e8c9c90SAndreas Gohr // if no credentials were given try to use HTTP auth (for SSO) 8203062864SAndreas Gohr if (!$INPUT->str('u') && empty($_COOKIE[DOKU_COOKIE]) && !empty($INPUT->server->str('PHP_AUTH_USER'))) { 8303062864SAndreas Gohr $INPUT->set('u', $INPUT->server->str('PHP_AUTH_USER')); 8403062864SAndreas Gohr $INPUT->set('p', $INPUT->server->str('PHP_AUTH_PW')); 85bcc94b2cSAndreas Gohr $INPUT->set('http_credentials', true); 861e8c9c90SAndreas Gohr } 871e8c9c90SAndreas Gohr 88395c2f0fSAndreas Gohr // apply cleaning (auth specific user names, remove control chars) 8993a7873eSAndreas Gohr if (true === $auth->success) { 90395c2f0fSAndreas Gohr $INPUT->set('u', $auth->cleanUser(stripctl($INPUT->str('u')))); 91395c2f0fSAndreas Gohr $INPUT->set('p', stripctl($INPUT->str('p'))); 92f4476bd9SJan Schumann } 93191bb90aSAndreas Gohr 9481e99965SPhy $ok = null; 958eca974cSAndreas Gohr if (!is_null($auth) && $auth->canDo('external')) { 9681e99965SPhy $ok = $auth->trustExternal($INPUT->str('u'), $INPUT->str('p'), $INPUT->bool('r')); 9781e99965SPhy } 9881e99965SPhy 9981e99965SPhy if ($ok === null) { 10081e99965SPhy // external trust mechanism not in place, or returns no result, 10181e99965SPhy // then attempt auth_login 10224870174SAndreas Gohr $evdata = [ 103bcc94b2cSAndreas Gohr 'user' => $INPUT->str('u'), 104bcc94b2cSAndreas Gohr 'password' => $INPUT->str('p'), 105bcc94b2cSAndreas Gohr 'sticky' => $INPUT->bool('r'), 106bcc94b2cSAndreas Gohr 'silent' => $INPUT->bool('http_credentials') 10724870174SAndreas Gohr ]; 108cbb44eabSAndreas Gohr Event::createAndTrigger('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper'); 109f5cb575dSAndreas Gohr } 110f5cb575dSAndreas Gohr 11116905344SAndreas Gohr //load ACL into a global array XXX 11275c93b77SAndreas Gohr $AUTH_ACL = auth_loadACL(); 113ab5d26daSAndreas Gohr 114ab5d26daSAndreas Gohr return true; 11575c93b77SAndreas Gohr} 11675c93b77SAndreas Gohr 11775c93b77SAndreas Gohr/** 11875c93b77SAndreas Gohr * Loads the ACL setup and handle user wildcards 11975c93b77SAndreas Gohr * 12075c93b77SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 12142ea7f44SGerrit Uitslag * 122ab5d26daSAndreas Gohr * @return array 12375c93b77SAndreas Gohr */ 124d868eb89SAndreas Gohrfunction auth_loadACL() 125d868eb89SAndreas Gohr{ 12675c93b77SAndreas Gohr global $config_cascade; 127b78bf706Sromain global $USERINFO; 128585bf44eSChristopher Smith /* @var Input $INPUT */ 129585bf44eSChristopher Smith global $INPUT; 13075c93b77SAndreas Gohr 13124870174SAndreas Gohr if (!is_readable($config_cascade['acl']['default'])) return []; 13275c93b77SAndreas Gohr 13375c93b77SAndreas Gohr $acl = file($config_cascade['acl']['default']); 13475c93b77SAndreas Gohr 13524870174SAndreas Gohr $out = []; 1369ce556d2SAndreas Gohr foreach ($acl as $line) { 1379ce556d2SAndreas Gohr $line = trim($line); 1382401f18dSSyntaxseed if (empty($line) || ($line[0] == '#')) continue; // skip blank lines & comments 13924870174SAndreas Gohr [$id, $rest] = preg_split('/[ \t]+/', $line, 2); 14032e82180SAndreas Gohr 141443e135dSChristopher Smith // substitute user wildcard first (its 1:1) 142ad3d68d7SChristopher Smith if (strstr($line, '%USER%')) { 143ad3d68d7SChristopher Smith // if user is not logged in, this ACL line is meaningless - skip it 144585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) continue; 145ad3d68d7SChristopher Smith 146585bf44eSChristopher Smith $id = str_replace('%USER%', cleanID($INPUT->server->str('REMOTE_USER')), $id); 147585bf44eSChristopher Smith $rest = str_replace('%USER%', auth_nameencode($INPUT->server->str('REMOTE_USER')), $rest); 148ad3d68d7SChristopher Smith } 149ad3d68d7SChristopher Smith 150ad3d68d7SChristopher Smith // substitute group wildcard (its 1:m) 1519ce556d2SAndreas Gohr if (strstr($line, '%GROUP%')) { 152ad3d68d7SChristopher Smith // if user is not logged in, grps is empty, no output will be added (i.e. skipped) 15306f34f54SPhy if (isset($USERINFO['grps'])) { 1549ce556d2SAndreas Gohr foreach ((array) $USERINFO['grps'] as $grp) { 155b78bf706Sromain $nid = str_replace('%GROUP%', cleanID($grp), $id); 15632e82180SAndreas Gohr $nrest = str_replace('%GROUP%', '@' . auth_nameencode($grp), $rest); 15732e82180SAndreas Gohr $out[] = "$nid\t$nrest"; 158b78bf706Sromain } 15906f34f54SPhy } 16032e82180SAndreas Gohr } else { 16132e82180SAndreas Gohr $out[] = "$id\t$rest"; 162a8fe108bSGuy Brand } 16311799630Sandi } 1649ce556d2SAndreas Gohr 16532e82180SAndreas Gohr return $out; 166f3f0262cSandi} 167f3f0262cSandi 168ab5d26daSAndreas Gohr/** 169ab5d26daSAndreas Gohr * Event hook callback for AUTH_LOGIN_CHECK 170ab5d26daSAndreas Gohr * 17142ea7f44SGerrit Uitslag * @param array $evdata 172ab5d26daSAndreas Gohr * @return bool 173*4dc42f7fSGerrit Uitslag * @throws Exception 174ab5d26daSAndreas Gohr */ 175d868eb89SAndreas Gohrfunction auth_login_wrapper($evdata) 176d868eb89SAndreas Gohr{ 177ab5d26daSAndreas Gohr return auth_login( 178ab5d26daSAndreas Gohr $evdata['user'], 179b5ee21aaSAdrian Lang $evdata['password'], 180b5ee21aaSAdrian Lang $evdata['sticky'], 181ab5d26daSAndreas Gohr $evdata['silent'] 182ab5d26daSAndreas Gohr ); 183b5ee21aaSAdrian Lang} 184b5ee21aaSAdrian Lang 185f3f0262cSandi/** 186f3f0262cSandi * This tries to login the user based on the sent auth credentials 187f3f0262cSandi * 188f3f0262cSandi * The authentication works like this: if a username was given 18915fae107Sandi * a new login is assumed and user/password are checked. If they 19015fae107Sandi * are correct the password is encrypted with blowfish and stored 19115fae107Sandi * together with the username in a cookie - the same info is stored 19215fae107Sandi * in the session, too. Additonally a browserID is stored in the 19315fae107Sandi * session. 19415fae107Sandi * 19515fae107Sandi * If no username was given the cookie is checked: if the username, 19615fae107Sandi * crypted password and browserID match between session and cookie 19715fae107Sandi * no further testing is done and the user is accepted 19815fae107Sandi * 19915fae107Sandi * If a cookie was found but no session info was availabe the 200136ce040Sandi * blowfish encrypted password from the cookie is decrypted and 20115fae107Sandi * together with username rechecked by calling this function again. 202f3f0262cSandi * 203f3f0262cSandi * On a successful login $_SERVER[REMOTE_USER] and $USERINFO 204f3f0262cSandi * are set. 20515fae107Sandi * 20615fae107Sandi * @param string $user Username 20715fae107Sandi * @param string $pass Cleartext Password 20815fae107Sandi * @param bool $sticky Cookie should not expire 209f112c2faSAndreas Gohr * @param bool $silent Don't show error on bad auth 21015fae107Sandi * @return bool true on successful auth 211*4dc42f7fSGerrit Uitslag * @throws Exception 212*4dc42f7fSGerrit Uitslag * 213*4dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 214f3f0262cSandi */ 215d868eb89SAndreas Gohrfunction auth_login($user, $pass, $sticky = false, $silent = false) 216d868eb89SAndreas Gohr{ 217f3f0262cSandi global $USERINFO; 218f3f0262cSandi global $conf; 219f3f0262cSandi global $lang; 220e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 221cd52f92dSchris global $auth; 222585bf44eSChristopher Smith /* @var Input $INPUT */ 223585bf44eSChristopher Smith global $INPUT; 224ab5d26daSAndreas Gohr 225beca106aSAdrian Lang if (!$auth) return false; 226beca106aSAdrian Lang 227bbbd6568SAndreas Gohr if (!empty($user)) { 228132bdbfeSandi //usual login 2295e9e1054SAndreas Gohr if (!empty($pass) && $auth->checkPass($user, $pass)) { 230132bdbfeSandi // make logininfo globally available 231585bf44eSChristopher Smith $INPUT->server->set('REMOTE_USER', $user); 23230d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 23304369c3eSMichael Hamann auth_setCookie($user, auth_encrypt($pass, $secret), $sticky); 234132bdbfeSandi return true; 235f3f0262cSandi } else { 236f3f0262cSandi //invalid credentials - log off 237f8b1e4e7SAndreas Gohr if (!$silent) { 238f8b1e4e7SAndreas Gohr http_status(403, 'Login failed'); 239f8b1e4e7SAndreas Gohr msg($lang['badlogin'], -1); 240f8b1e4e7SAndreas Gohr } 241f3f0262cSandi auth_logoff(); 242132bdbfeSandi return false; 243f3f0262cSandi } 244f3f0262cSandi } else { 245132bdbfeSandi // read cookie information 24624870174SAndreas Gohr [$user, $sticky, $pass] = auth_getCookie(); 247132bdbfeSandi if ($user && $pass) { 248132bdbfeSandi // we got a cookie - see if we can trust it 249fa7c70ffSAdrian Lang 250fa7c70ffSAdrian Lang // get session info 2510058ae75SDamien Regad if (isset($_SESSION[DOKU_COOKIE])) { 252fa7c70ffSAdrian Lang $session = $_SESSION[DOKU_COOKIE]['auth']; 2537d34963bSAndreas Gohr if ( 2547d34963bSAndreas Gohr isset($session) && 2557172dbc0SAndreas Gohr $auth->useSessionCache($user) && 2564c989037SChris Smith ($session['time'] >= time() - $conf['auth_security_timeout']) && 257132bdbfeSandi ($session['user'] == $user) && 258234ce57eSAndreas Gohr ($session['pass'] == sha1($pass)) && //still crypted 259ab5d26daSAndreas Gohr ($session['buid'] == auth_browseruid()) 260ab5d26daSAndreas 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 } 2660058ae75SDamien Regad } 267f112c2faSAndreas Gohr // no we don't trust it yet - recheck pass but silent 26830d544a4SMichael Hamann $secret = auth_cookiesalt(!$sticky, true); //bind non-sticky to session 26904369c3eSMichael Hamann $pass = auth_decrypt($pass, $secret); 270f112c2faSAndreas Gohr return auth_login($user, $pass, $sticky, true); 271132bdbfeSandi } 272132bdbfeSandi } 273f3f0262cSandi //just to be sure 274883179a4SAndreas Gohr auth_logoff(true); 275132bdbfeSandi return false; 276f3f0262cSandi} 277132bdbfeSandi 278132bdbfeSandi/** 279136ce040Sandi * Builds a pseudo UID from browser and IP data 280132bdbfeSandi * 281132bdbfeSandi * This is neither unique nor unfakable - still it adds some 282136ce040Sandi * security. Using the first part of the IP makes sure 28380b4f376SAndreas Gohr * proxy farms like AOLs are still okay. 28415fae107Sandi * 28515fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 28615fae107Sandi * 287b13c0e1aSAdaKaleh * @return string a SHA256 sum of various browser headers 288132bdbfeSandi */ 289d868eb89SAndreas Gohrfunction auth_browseruid() 290d868eb89SAndreas Gohr{ 291585bf44eSChristopher Smith /* @var Input $INPUT */ 292585bf44eSChristopher Smith global $INPUT; 293585bf44eSChristopher Smith 2942f9daf16SAndreas Gohr $ip = clientIP(true); 295b13c0e1aSAdaKaleh // convert IP string to packed binary representation 296b13c0e1aSAdaKaleh $pip = inet_pton($ip); 297b7c67f83SAndreas Gohr 298b7c67f83SAndreas Gohr $uid = implode("\n", [ 299b7c67f83SAndreas Gohr $INPUT->server->str('HTTP_USER_AGENT'), 300b7c67f83SAndreas Gohr $INPUT->server->str('HTTP_ACCEPT_LANGUAGE'), 301b7c67f83SAndreas Gohr substr($pip, 0, strlen($pip) / 2), // use half of the IP address (works for both IPv4 and IPv6) 302b7c67f83SAndreas Gohr ]); 303b13c0e1aSAdaKaleh return hash('sha256', $uid); 304132bdbfeSandi} 305132bdbfeSandi 306132bdbfeSandi/** 307132bdbfeSandi * Creates a random key to encrypt the password in cookies 30815fae107Sandi * 30915fae107Sandi * This function tries to read the password for encrypting 31098407a7aSandi * cookies from $conf['metadir'].'/_htcookiesalt' 31115fae107Sandi * if no such file is found a random key is created and 31215fae107Sandi * and stored in this file. 31315fae107Sandi * 31432ed2b36SAndreas Gohr * @param bool $addsession if true, the sessionid is added to the salt 31530d544a4SMichael Hamann * @param bool $secure if security is more important than keeping the old value 31615fae107Sandi * @return string 317*4dc42f7fSGerrit Uitslag * @throws Exception 318*4dc42f7fSGerrit Uitslag * 319*4dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 320132bdbfeSandi */ 321d868eb89SAndreas Gohrfunction auth_cookiesalt($addsession = false, $secure = false) 322d868eb89SAndreas Gohr{ 323a1fe3c9cSMichael Große if (defined('SIMPLE_TEST')) { 324fe745becSMichael Große return 'test'; 325a1fe3c9cSMichael Große } 326132bdbfeSandi global $conf; 32798407a7aSandi $file = $conf['metadir'] . '/_htcookiesalt'; 32830d544a4SMichael Hamann if ($secure || !file_exists($file)) { 32930d544a4SMichael Hamann $file = $conf['metadir'] . '/_htcookiesalt2'; 33030d544a4SMichael Hamann } 331132bdbfeSandi $salt = io_readFile($file); 332132bdbfeSandi if (empty($salt)) { 33330d544a4SMichael Hamann $salt = bin2hex(auth_randombytes(64)); 334132bdbfeSandi io_saveFile($file, $salt); 335132bdbfeSandi } 33632ed2b36SAndreas Gohr if ($addsession) { 33732ed2b36SAndreas Gohr $salt .= session_id(); 33832ed2b36SAndreas Gohr } 339132bdbfeSandi return $salt; 340f3f0262cSandi} 341f3f0262cSandi 342f3f0262cSandi/** 3437a33d2f8SNiklas Keller * Return cryptographically secure random bytes. 344483b6238SMichael Hamann * 3457a33d2f8SNiklas Keller * @param int $length number of bytes 3467a33d2f8SNiklas Keller * @return string cryptographically secure random bytes 347*4dc42f7fSGerrit Uitslag * @throws Exception 348*4dc42f7fSGerrit Uitslag * 349*4dc42f7fSGerrit Uitslag * @author Niklas Keller <me@kelunik.com> 350483b6238SMichael Hamann */ 351d868eb89SAndreas Gohrfunction auth_randombytes($length) 352d868eb89SAndreas Gohr{ 3537a33d2f8SNiklas Keller return random_bytes($length); 354483b6238SMichael Hamann} 355483b6238SMichael Hamann 356483b6238SMichael Hamann/** 3577a33d2f8SNiklas Keller * Cryptographically secure random number generator. 358483b6238SMichael Hamann * 359483b6238SMichael Hamann * @param int $min 360483b6238SMichael Hamann * @param int $max 361483b6238SMichael Hamann * @return int 362*4dc42f7fSGerrit Uitslag * @throws Exception 363*4dc42f7fSGerrit Uitslag * 364*4dc42f7fSGerrit Uitslag * @author Niklas Keller <me@kelunik.com> 365483b6238SMichael Hamann */ 366d868eb89SAndreas Gohrfunction auth_random($min, $max) 367d868eb89SAndreas Gohr{ 3687a33d2f8SNiklas Keller return random_int($min, $max); 369483b6238SMichael Hamann} 370483b6238SMichael Hamann 371483b6238SMichael Hamann/** 37204369c3eSMichael Hamann * Encrypt data using the given secret using AES 37304369c3eSMichael Hamann * 37404369c3eSMichael Hamann * The mode is CBC with a random initialization vector, the key is derived 37504369c3eSMichael Hamann * using pbkdf2. 37604369c3eSMichael Hamann * 37704369c3eSMichael Hamann * @param string $data The data that shall be encrypted 37804369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 37904369c3eSMichael Hamann * @return string The ciphertext 380*4dc42f7fSGerrit Uitslag * @throws Exception 38104369c3eSMichael Hamann */ 382d868eb89SAndreas Gohrfunction auth_encrypt($data, $secret) 383d868eb89SAndreas Gohr{ 38404369c3eSMichael Hamann $iv = auth_randombytes(16); 38524870174SAndreas Gohr $cipher = new AES(); 38604369c3eSMichael Hamann $cipher->setPassword($secret); 38704369c3eSMichael Hamann 3887b650cefSMichael Hamann /* 3897b650cefSMichael Hamann this uses the encrypted IV as IV as suggested in 3907b650cefSMichael Hamann http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf, Appendix C 3917b650cefSMichael Hamann for unique but necessarily random IVs. The resulting ciphertext is 3927b650cefSMichael Hamann compatible to ciphertext that was created using a "normal" IV. 3937b650cefSMichael Hamann */ 39404369c3eSMichael Hamann return $cipher->encrypt($iv . $data); 39504369c3eSMichael Hamann} 39604369c3eSMichael Hamann 39704369c3eSMichael Hamann/** 39804369c3eSMichael Hamann * Decrypt the given AES ciphertext 39904369c3eSMichael Hamann * 40004369c3eSMichael Hamann * The mode is CBC, the key is derived using pbkdf2 40104369c3eSMichael Hamann * 40204369c3eSMichael Hamann * @param string $ciphertext The encrypted data 40304369c3eSMichael Hamann * @param string $secret The secret/password that shall be used 40404369c3eSMichael Hamann * @return string The decrypted data 40504369c3eSMichael Hamann */ 406d868eb89SAndreas Gohrfunction auth_decrypt($ciphertext, $secret) 407d868eb89SAndreas Gohr{ 4087b650cefSMichael Hamann $iv = substr($ciphertext, 0, 16); 40924870174SAndreas Gohr $cipher = new AES(); 41004369c3eSMichael Hamann $cipher->setPassword($secret); 4117b650cefSMichael Hamann $cipher->setIV($iv); 41204369c3eSMichael Hamann 4137b650cefSMichael Hamann return $cipher->decrypt(substr($ciphertext, 16)); 41404369c3eSMichael Hamann} 41504369c3eSMichael Hamann 41604369c3eSMichael Hamann/** 417883179a4SAndreas Gohr * Log out the current user 418883179a4SAndreas Gohr * 419f3f0262cSandi * This clears all authentication data and thus log the user 420883179a4SAndreas Gohr * off. It also clears session data. 42115fae107Sandi * 42215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 42342ea7f44SGerrit Uitslag * 424883179a4SAndreas Gohr * @param bool $keepbc - when true, the breadcrumb data is not cleared 425f3f0262cSandi */ 426d868eb89SAndreas Gohrfunction auth_logoff($keepbc = false) 427d868eb89SAndreas Gohr{ 428f3f0262cSandi global $conf; 429f3f0262cSandi global $USERINFO; 430e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 4315298a619SAndreas Gohr global $auth; 432585bf44eSChristopher Smith /* @var Input $INPUT */ 433585bf44eSChristopher Smith global $INPUT; 43437065e65Sandi 435d4869846SAndreas Gohr // make sure the session is writable (it usually is) 436e9621d07SAndreas Gohr @session_start(); 437e9621d07SAndreas Gohr 438e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['user'])) 439e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['user']); 440e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['pass'])) 441e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['pass']); 442e71ce681SAndreas Gohr if (isset($_SESSION[DOKU_COOKIE]['auth']['info'])) 443e71ce681SAndreas Gohr unset($_SESSION[DOKU_COOKIE]['auth']['info']); 444883179a4SAndreas Gohr if (!$keepbc && isset($_SESSION[DOKU_COOKIE]['bc'])) 445e16eccb7SGuy Brand unset($_SESSION[DOKU_COOKIE]['bc']); 446585bf44eSChristopher Smith $INPUT->server->remove('REMOTE_USER'); 447132bdbfeSandi $USERINFO = null; //FIXME 448f5c6743cSAndreas Gohr 44973ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 450bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, '', [ 451bf8392ebSAndreas Gohr 'expires' => time() - 600000, 452bf8392ebSAndreas Gohr 'path' => $cookieDir, 453bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 454bf8392ebSAndreas Gohr 'httponly' => true, 455486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 456bf8392ebSAndreas Gohr ]); 4575298a619SAndreas Gohr 458880f62faSAndreas Gohr if ($auth) $auth->logOff(); 459f3f0262cSandi} 460f3f0262cSandi 461f3f0262cSandi/** 462f8cc712eSAndreas Gohr * Check if a user is a manager 463f8cc712eSAndreas Gohr * 464f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 465f8cc712eSAndreas Gohr * user. 466f8cc712eSAndreas Gohr * 467f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 468f8cc712eSAndreas Gohr * 469ab5d26daSAndreas Gohr * @param string $user Username 470ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 471ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 47210396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 473ab5d26daSAndreas Gohr * @return bool 47496348f27SAndreas Gohr * @see auth_isadmin 47596348f27SAndreas Gohr * 47696348f27SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 477f8cc712eSAndreas Gohr */ 478d868eb89SAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false, $recache = false) 479d868eb89SAndreas Gohr{ 480f8cc712eSAndreas Gohr global $conf; 481f8cc712eSAndreas Gohr global $USERINFO; 482e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 483d752aedeSAndreas Gohr global $auth; 484585bf44eSChristopher Smith /* @var Input $INPUT */ 485585bf44eSChristopher Smith global $INPUT; 486585bf44eSChristopher Smith 487f8cc712eSAndreas Gohr 488beca106aSAdrian Lang if (!$auth) return false; 489c66972f2SAdrian Lang if (is_null($user)) { 490585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) { 491c66972f2SAdrian Lang return false; 492c66972f2SAdrian Lang } else { 493585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 494c66972f2SAdrian Lang } 495c66972f2SAdrian Lang } 496d6dc956fSAndreas Gohr if (is_null($groups)) { 4971525c228SAnna Dabrowska // checking the logged in user, or another one? 4981525c228SAnna Dabrowska if ($USERINFO && $user === $INPUT->server->str('REMOTE_USER')) { 4991525c228SAnna Dabrowska $groups = (array) $USERINFO['grps']; 50066b108d6SAnna Dabrowska } else { 5016cf7b139SAndreas Gohr $groups = $auth->getUserData($user); 5026cf7b139SAndreas Gohr $groups = $groups ? $groups['grps'] : []; 50366b108d6SAnna Dabrowska } 504e259aa79SAndreas Gohr } 505e259aa79SAndreas Gohr 50696348f27SAndreas Gohr // prefer cached result 50796348f27SAndreas Gohr static $cache = []; 50810396f77SAndreas Gohr $cachekey = serialize([$user, $adminonly, $groups]); 50996348f27SAndreas Gohr if (!isset($cache[$cachekey]) || $recache) { 510d6dc956fSAndreas Gohr // check superuser match 51196348f27SAndreas Gohr $ok = auth_isMember($conf['superuser'], $user, $groups); 51200ce12daSChris Smith 51396348f27SAndreas Gohr // check managers 51496348f27SAndreas Gohr if (!$ok && !$adminonly) { 51596348f27SAndreas Gohr $ok = auth_isMember($conf['manager'], $user, $groups); 51696348f27SAndreas Gohr } 51796348f27SAndreas Gohr 51896348f27SAndreas Gohr $cache[$cachekey] = $ok; 51996348f27SAndreas Gohr } 52096348f27SAndreas Gohr 52196348f27SAndreas Gohr return $cache[$cachekey]; 522f8cc712eSAndreas Gohr} 523f8cc712eSAndreas Gohr 524f8cc712eSAndreas Gohr/** 525f8cc712eSAndreas Gohr * Check if a user is admin 526f8cc712eSAndreas Gohr * 527f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 528f8cc712eSAndreas Gohr * 529f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 530f8cc712eSAndreas Gohr * 53196348f27SAndreas Gohr * @param string $user Username 53296348f27SAndreas Gohr * @param array $groups List of groups the user is in 53310396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 53496348f27SAndreas Gohr * @return bool 535f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 536ab5d26daSAndreas Gohr * @see auth_ismanager() 53742ea7f44SGerrit Uitslag * 538f8cc712eSAndreas Gohr */ 539d868eb89SAndreas Gohrfunction auth_isadmin($user = null, $groups = null, $recache = false) 540d868eb89SAndreas Gohr{ 54196348f27SAndreas Gohr return auth_ismanager($user, $groups, true, $recache); 542f8cc712eSAndreas Gohr} 543f8cc712eSAndreas Gohr 544d6dc956fSAndreas Gohr/** 545d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 546d6dc956fSAndreas Gohr * users and groups to determine membership status 547d6dc956fSAndreas Gohr * 548d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 549d6dc956fSAndreas Gohr * 55042ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 55142ea7f44SGerrit Uitslag * @param string $user user to match against 55242ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 5535446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 554d6dc956fSAndreas Gohr */ 555d868eb89SAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) 556d868eb89SAndreas Gohr{ 557e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 558d6dc956fSAndreas Gohr global $auth; 559d6dc956fSAndreas Gohr if (!$auth) return false; 560d6dc956fSAndreas Gohr 561d6dc956fSAndreas Gohr // clean user and groups 5624f56ecbfSAdrian Lang if (!$auth->isCaseSensitive()) { 56324870174SAndreas Gohr $user = PhpString::strtolower($user); 56424870174SAndreas Gohr $groups = array_map([PhpString::class, 'strtolower'], $groups); 565d6dc956fSAndreas Gohr } 566d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 56724870174SAndreas Gohr $groups = array_map([$auth, 'cleanGroup'], $groups); 568d6dc956fSAndreas Gohr 569d6dc956fSAndreas Gohr // extract the memberlist 570d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 571d6dc956fSAndreas Gohr $members = array_map('trim', $members); 572d6dc956fSAndreas Gohr $members = array_unique($members); 573d6dc956fSAndreas Gohr $members = array_filter($members); 574d6dc956fSAndreas Gohr 575d6dc956fSAndreas Gohr // compare cleaned values 576d6dc956fSAndreas Gohr foreach ($members as $member) { 577e5204a12SJurgen Hart if ($member == '@ALL') return true; 57824870174SAndreas Gohr if (!$auth->isCaseSensitive()) $member = PhpString::strtolower($member); 579d6dc956fSAndreas Gohr if ($member[0] == '@') { 580d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 581d6dc956fSAndreas Gohr if (in_array($member, $groups)) return true; 582d6dc956fSAndreas Gohr } else { 583d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 584d6dc956fSAndreas Gohr if ($member == $user) return true; 585d6dc956fSAndreas Gohr } 586d6dc956fSAndreas Gohr } 587d6dc956fSAndreas Gohr 588d6dc956fSAndreas Gohr // still here? not a member! 589d6dc956fSAndreas Gohr return false; 590d6dc956fSAndreas Gohr} 591d6dc956fSAndreas Gohr 592f8cc712eSAndreas Gohr/** 59315fae107Sandi * Convinience function for auth_aclcheck() 59415fae107Sandi * 59515fae107Sandi * This checks the permissions for the current user 59615fae107Sandi * 59715fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 59815fae107Sandi * 5991698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 60015fae107Sandi * @return int permission level 601f3f0262cSandi */ 602d868eb89SAndreas Gohrfunction auth_quickaclcheck($id) 603d868eb89SAndreas Gohr{ 604f3f0262cSandi global $conf; 605f3f0262cSandi global $USERINFO; 606585bf44eSChristopher Smith /* @var Input $INPUT */ 607585bf44eSChristopher Smith global $INPUT; 608f3f0262cSandi # if no ACL is used always return upload rights 609f3f0262cSandi if (!$conf['useacl']) return AUTH_UPLOAD; 61024870174SAndreas Gohr return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), is_array($USERINFO) ? $USERINFO['grps'] : []); 611f3f0262cSandi} 612f3f0262cSandi 613f3f0262cSandi/** 614c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 61515fae107Sandi * 61615fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 61742ea7f44SGerrit Uitslag * 618c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 6191698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 62015fae107Sandi * @param string $user Username 6213272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 62215fae107Sandi * @return int permission level 623f3f0262cSandi */ 624d868eb89SAndreas Gohrfunction auth_aclcheck($id, $user, $groups) 625d868eb89SAndreas Gohr{ 62624870174SAndreas Gohr $data = [ 627bf8f8509SAndreas Gohr 'id' => $id ?? '', 628c17acc9fSAndreas Gohr 'user' => $user, 629c17acc9fSAndreas Gohr 'groups' => $groups 63024870174SAndreas Gohr ]; 631c17acc9fSAndreas Gohr 632cbb44eabSAndreas Gohr return Event::createAndTrigger('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 633c17acc9fSAndreas Gohr} 634c17acc9fSAndreas Gohr 635c17acc9fSAndreas Gohr/** 636c17acc9fSAndreas Gohr * default ACL check method 637c17acc9fSAndreas Gohr * 638c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 639c17acc9fSAndreas Gohr * 640c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 64142ea7f44SGerrit Uitslag * 642c17acc9fSAndreas Gohr * @param array $data event data 643c17acc9fSAndreas Gohr * @return int permission level 644c17acc9fSAndreas Gohr */ 645d868eb89SAndreas Gohrfunction auth_aclcheck_cb($data) 646d868eb89SAndreas Gohr{ 647c17acc9fSAndreas Gohr $id =& $data['id']; 648c17acc9fSAndreas Gohr $user =& $data['user']; 649c17acc9fSAndreas Gohr $groups =& $data['groups']; 650c17acc9fSAndreas Gohr 651f3f0262cSandi global $conf; 652f3f0262cSandi global $AUTH_ACL; 653e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 654d752aedeSAndreas Gohr global $auth; 655f3f0262cSandi 65685d03f68SAndreas Gohr // if no ACL is used always return upload rights 657f3f0262cSandi if (!$conf['useacl']) return AUTH_UPLOAD; 658beca106aSAdrian Lang if (!$auth) return AUTH_NONE; 659bf8f8509SAndreas Gohr if (!is_array($AUTH_ACL)) return AUTH_NONE; 660f3f0262cSandi 661074cf26bSandi //make sure groups is an array 66224870174SAndreas Gohr if (!is_array($groups)) $groups = []; 663074cf26bSandi 66485d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 665ab5d26daSAndreas Gohr if (auth_isadmin($user, $groups)) { 666ab5d26daSAndreas Gohr return AUTH_ADMIN; 667ab5d26daSAndreas Gohr } 66885d03f68SAndreas Gohr 669eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive()) { 67024870174SAndreas Gohr $user = PhpString::strtolower($user); 67124870174SAndreas Gohr $groups = array_map([PhpString::class, 'strtolower'], $groups); 672eb3ce0d5SKazutaka Miyasaka } 67337ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 67424870174SAndreas Gohr $groups = array_map([$auth, 'cleanGroup'], $groups); 67585d03f68SAndreas Gohr 6766c2bb100SAndreas Gohr //prepend groups with @ and nameencode 67737ff2261SSascha Klopp foreach ($groups as &$group) { 67837ff2261SSascha Klopp $group = '@' . auth_nameencode($group); 67910a76f6fSfrank } 68010a76f6fSfrank 681f3f0262cSandi $ns = getNS($id); 682f3f0262cSandi $perm = -1; 683f3f0262cSandi 684f3f0262cSandi //add ALL group 685f3f0262cSandi $groups[] = '@ALL'; 68637ff2261SSascha Klopp 687f3f0262cSandi //add User 68834aeb4afSAndreas Gohr if ($user) $groups[] = $user; 689f3f0262cSandi 690f3f0262cSandi //check exact match first 69121c3090aSChristopher Smith $matches = preg_grep('/^' . preg_quote($id, '/') . '[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 692f3f0262cSandi if (count($matches)) { 693f3f0262cSandi foreach ($matches as $match) { 694f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 69521c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 696eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 69724870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 698eb3ce0d5SKazutaka Miyasaka } 69948d7b7a6SDominik Eckelmann if (!in_array($acl[1], $groups)) { 70048d7b7a6SDominik Eckelmann continue; 70148d7b7a6SDominik Eckelmann } 7028ef6b7caSandi if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 703f3f0262cSandi if ($acl[2] > $perm) { 704f3f0262cSandi $perm = $acl[2]; 705f3f0262cSandi } 706f3f0262cSandi } 707f3f0262cSandi if ($perm > -1) { 708f3f0262cSandi //we had a match - return it 709def492a2SGuillaume Turri return (int) $perm; 710f3f0262cSandi } 711f3f0262cSandi } 712f3f0262cSandi 713f3f0262cSandi //still here? do the namespace checks 714f3f0262cSandi if ($ns) { 7153e304b55SMichael Hamann $path = $ns . ':*'; 716f3f0262cSandi } else { 7173e304b55SMichael Hamann $path = '*'; //root document 718f3f0262cSandi } 719f3f0262cSandi 720f3f0262cSandi do { 72121c3090aSChristopher Smith $matches = preg_grep('/^' . preg_quote($path, '/') . '[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 722f3f0262cSandi if (count($matches)) { 723f3f0262cSandi foreach ($matches as $match) { 724f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 72521c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 726eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 72724870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 728eb3ce0d5SKazutaka Miyasaka } 72948d7b7a6SDominik Eckelmann if (!in_array($acl[1], $groups)) { 73048d7b7a6SDominik Eckelmann continue; 73148d7b7a6SDominik Eckelmann } 7328ef6b7caSandi if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 733f3f0262cSandi if ($acl[2] > $perm) { 734f3f0262cSandi $perm = $acl[2]; 735f3f0262cSandi } 736f3f0262cSandi } 737f3f0262cSandi //we had a match - return it 73848d7b7a6SDominik Eckelmann if ($perm != -1) { 739def492a2SGuillaume Turri return (int) $perm; 740f3f0262cSandi } 74148d7b7a6SDominik Eckelmann } 742f3f0262cSandi //get next higher namespace 743f3f0262cSandi $ns = getNS($ns); 744f3f0262cSandi 7453e304b55SMichael Hamann if ($path != '*') { 7463e304b55SMichael Hamann $path = $ns . ':*'; 7473e304b55SMichael Hamann if ($path == ':*') $path = '*'; 748f3f0262cSandi } else { 749f3f0262cSandi //we did this already 750f3f0262cSandi //looks like there is something wrong with the ACL 751f3f0262cSandi //break here 752d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 753d5ce66f6SAndreas Gohr return AUTH_NONE; 754f3f0262cSandi } 755f3f0262cSandi } while (1); //this should never loop endless 756ab5d26daSAndreas Gohr return AUTH_NONE; 757f3f0262cSandi} 758f3f0262cSandi 759f3f0262cSandi/** 7606c2bb100SAndreas Gohr * Encode ASCII special chars 7616c2bb100SAndreas Gohr * 7626c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 7636c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 7646c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 7656c2bb100SAndreas Gohr * urlencoding!). 7666c2bb100SAndreas Gohr * 7676c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 7686c2bb100SAndreas Gohr * 7696c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 7706c2bb100SAndreas Gohr * @see rawurldecode() 77142ea7f44SGerrit Uitslag * 77242ea7f44SGerrit Uitslag * @param string $name 77342ea7f44SGerrit Uitslag * @param bool $skip_group 77442ea7f44SGerrit Uitslag * @return string 7756c2bb100SAndreas Gohr */ 776d868eb89SAndreas Gohrfunction auth_nameencode($name, $skip_group = false) 777d868eb89SAndreas Gohr{ 778a424cd8eSchris global $cache_authname; 779a424cd8eSchris $cache =& $cache_authname; 78031784267SAndreas Gohr $name = (string) $name; 781a424cd8eSchris 78280601d26SAndreas Gohr // never encode wildcard FS#1955 78380601d26SAndreas Gohr if ($name == '%USER%') return $name; 784b78bf706Sromain if ($name == '%GROUP%') return $name; 78580601d26SAndreas Gohr 786a424cd8eSchris if (!isset($cache[$name][$skip_group])) { 7872401f18dSSyntaxseed if ($skip_group && $name[0] == '@') { 78830f6faf0SChristopher Smith $cache[$name][$skip_group] = '@' . preg_replace_callback( 78930f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 790dccd6b2bSAndreas Gohr 'auth_nameencode_callback', 791dccd6b2bSAndreas Gohr substr($name, 1) 792ab5d26daSAndreas Gohr ); 793e838fc2eSAndreas Gohr } else { 79430f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 79530f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 796dccd6b2bSAndreas Gohr 'auth_nameencode_callback', 797dccd6b2bSAndreas Gohr $name 798ab5d26daSAndreas Gohr ); 799e838fc2eSAndreas Gohr } 8006c2bb100SAndreas Gohr } 8016c2bb100SAndreas Gohr 802a424cd8eSchris return $cache[$name][$skip_group]; 803a424cd8eSchris} 804a424cd8eSchris 80504d68ae4SGerrit Uitslag/** 80604d68ae4SGerrit Uitslag * callback encodes the matches 80704d68ae4SGerrit Uitslag * 80804d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 80904d68ae4SGerrit Uitslag * @return string 81004d68ae4SGerrit Uitslag */ 811d868eb89SAndreas Gohrfunction auth_nameencode_callback($matches) 812d868eb89SAndreas Gohr{ 81330f6faf0SChristopher Smith return '%' . dechex(ord(substr($matches[1], -1))); 81430f6faf0SChristopher Smith} 81530f6faf0SChristopher Smith 8166c2bb100SAndreas Gohr/** 817f3f0262cSandi * Create a pronouncable password 818f3f0262cSandi * 8198a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8208a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8218a285f7fSAndreas Gohr * 822*4dc42f7fSGerrit Uitslag * @param string $foruser username for which the password is generated 823*4dc42f7fSGerrit Uitslag * @return string pronouncable password 824*4dc42f7fSGerrit Uitslag * @throws Exception 825*4dc42f7fSGerrit Uitslag * 82615fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 8278a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 82815fae107Sandi * 829*4dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 830f3f0262cSandi */ 831d868eb89SAndreas Gohrfunction auth_pwgen($foruser = '') 832d868eb89SAndreas Gohr{ 83324870174SAndreas Gohr $data = [ 834d628dcf3SAndreas Gohr 'password' => '', 835d628dcf3SAndreas Gohr 'foruser' => $foruser 83624870174SAndreas Gohr ]; 8378a285f7fSAndreas Gohr 838e1d9dcc8SAndreas Gohr $evt = new Event('AUTH_PASSWORD_GENERATE', $data); 8398a285f7fSAndreas Gohr if ($evt->advise_before(true)) { 840f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 841f3f0262cSandi $v = 'aeiou'; //vowels 842f3f0262cSandi $a = $c . $v; //both 843987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 844f3f0262cSandi 845987c8d26SAndreas Gohr //use thre syllables... 846987c8d26SAndreas Gohr for ($i = 0; $i < 3; $i++) { 847483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 848483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 849483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 850f3f0262cSandi } 851987c8d26SAndreas Gohr //... and add a nice number and special 85243f71e05Ssdavis80 $data['password'] .= $s[auth_random(0, strlen($s) - 1)] . auth_random(10, 99); 8538a285f7fSAndreas Gohr } 8548a285f7fSAndreas Gohr $evt->advise_after(); 855f3f0262cSandi 8568a285f7fSAndreas Gohr return $data['password']; 857f3f0262cSandi} 858f3f0262cSandi 859f3f0262cSandi/** 860f3f0262cSandi * Sends a password to the given user 861f3f0262cSandi * 86215fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 86342ea7f44SGerrit Uitslag * 864ab5d26daSAndreas Gohr * @param string $user Login name of the user 865ab5d26daSAndreas Gohr * @param string $password The new password in clear text 86615fae107Sandi * @return bool true on success 867f3f0262cSandi */ 868d868eb89SAndreas Gohrfunction auth_sendPassword($user, $password) 869d868eb89SAndreas Gohr{ 870f3f0262cSandi global $lang; 871e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 872cd52f92dSchris global $auth; 873beca106aSAdrian Lang if (!$auth) return false; 874cd52f92dSchris 875d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 876*4dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 877f3f0262cSandi 87887ddda95Sandi if (!$userinfo['mail']) return false; 879f3f0262cSandi 880f3f0262cSandi $text = rawLocale('password'); 88124870174SAndreas Gohr $trep = [ 882d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 883d7169d19SAndreas Gohr 'LOGIN' => $user, 884d7169d19SAndreas Gohr 'PASSWORD' => $password 88524870174SAndreas Gohr ]; 886f3f0262cSandi 887d7169d19SAndreas Gohr $mail = new Mailer(); 888102cdbd7SLarsGit223 $mail->to($mail->getCleanName($userinfo['name']) . ' <' . $userinfo['mail'] . '>'); 889d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 890d7169d19SAndreas Gohr $mail->setBody($text, $trep); 891d7169d19SAndreas Gohr return $mail->send(); 892f3f0262cSandi} 893f3f0262cSandi 894f3f0262cSandi/** 89515fae107Sandi * Register a new user 896f3f0262cSandi * 89715fae107Sandi * This registers a new user - Data is read directly from $_POST 89815fae107Sandi * 89915fae107Sandi * @return bool true on success, false on any error 900*4dc42f7fSGerrit Uitslag * @throws Exception 901*4dc42f7fSGerrit Uitslag * 902*4dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 903f3f0262cSandi */ 904d868eb89SAndreas Gohrfunction register() 905d868eb89SAndreas Gohr{ 906f3f0262cSandi global $lang; 907eb5d07e4Sjan global $conf; 908*4dc42f7fSGerrit Uitslag /* @var AuthPlugin $auth */ 909cd52f92dSchris global $auth; 91064273335SAndreas Gohr global $INPUT; 911f3f0262cSandi 91264273335SAndreas Gohr if (!$INPUT->post->bool('save')) return false; 9133a48618aSAnika Henke if (!actionOK('register')) return false; 914640145a5Sandi 91564273335SAndreas Gohr // gather input 91664273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 91764273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 91864273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 91964273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 92064273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 921d752aedeSAndreas Gohr 92264273335SAndreas Gohr if (empty($login) || empty($fullname) || empty($email)) { 923f3f0262cSandi msg($lang['regmissing'], -1); 924f3f0262cSandi return false; 925f3f0262cSandi } 926f3f0262cSandi 927cab2716aSmatthias.grimm if ($conf['autopasswd']) { 9288a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 92964273335SAndreas Gohr } elseif (empty($pass) || empty($passchk)) { 930bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 931cab2716aSmatthias.grimm return false; 93264273335SAndreas Gohr } elseif ($pass != $passchk) { 933bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 934cab2716aSmatthias.grimm return false; 935cab2716aSmatthias.grimm } 936cab2716aSmatthias.grimm 937f3f0262cSandi //check mail 93864273335SAndreas Gohr if (!mail_isvalid($email)) { 939f3f0262cSandi msg($lang['regbadmail'], -1); 940f3f0262cSandi return false; 941f3f0262cSandi } 942f3f0262cSandi 943f3f0262cSandi //okay try to create the user 94424870174SAndreas Gohr if (!$auth->triggerUserMod('create', [$login, $pass, $fullname, $email])) { 945db9faf02SPatrick Brown msg($lang['regfail'], -1); 946f3f0262cSandi return false; 947f3f0262cSandi } 948f3f0262cSandi 949790b7720SAndreas Gohr // send notification about the new user 95075d66495SMichael Große $subscription = new RegistrationSubscriptionSender(); 95175d66495SMichael Große $subscription->sendRegister($login, $fullname, $email); 95202a498e7Schris 953790b7720SAndreas Gohr // are we done? 954cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 955cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 956cab2716aSmatthias.grimm return true; 957cab2716aSmatthias.grimm } 958cab2716aSmatthias.grimm 959790b7720SAndreas Gohr // autogenerated password? then send password to user 96064273335SAndreas Gohr if (auth_sendPassword($login, $pass)) { 961f3f0262cSandi msg($lang['regsuccess'], 1); 962f3f0262cSandi return true; 963f3f0262cSandi } else { 964f3f0262cSandi msg($lang['regmailfail'], -1); 965f3f0262cSandi return false; 966f3f0262cSandi } 967f3f0262cSandi} 968f3f0262cSandi 96910a76f6fSfrank/** 9708b06d178Schris * Update user profile 9718b06d178Schris * 972*4dc42f7fSGerrit Uitslag * @throws Exception 973*4dc42f7fSGerrit Uitslag * 9748b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 9758b06d178Schris */ 976d868eb89SAndreas Gohrfunction updateprofile() 977d868eb89SAndreas Gohr{ 9788b06d178Schris global $conf; 9798b06d178Schris global $lang; 980e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 981cd52f92dSchris global $auth; 982bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 983bcc94b2cSAndreas Gohr global $INPUT; 9848b06d178Schris 985bcc94b2cSAndreas Gohr if (!$INPUT->post->bool('save')) return false; 9861b2a85e8SAndreas Gohr if (!checkSecurityToken()) return false; 9878b06d178Schris 9883a48618aSAnika Henke if (!actionOK('profile')) { 9898b06d178Schris msg($lang['profna'], -1); 9908b06d178Schris return false; 9918b06d178Schris } 9928b06d178Schris 99324870174SAndreas Gohr $changes = []; 994bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 995bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 996bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 997bcc94b2cSAndreas Gohr 998bcc94b2cSAndreas Gohr // check misspelled passwords 999bcc94b2cSAndreas Gohr if ($changes['pass'] != $INPUT->post->str('passchk')) { 1000bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 10018b06d178Schris return false; 10028b06d178Schris } 10038b06d178Schris 10048b06d178Schris // clean fullname and email 1005bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 1006bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 10078b06d178Schris 1008bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 10097d34963bSAndreas Gohr if ( 10107d34963bSAndreas Gohr (empty($changes['name']) && $auth->canDo('modName')) || 1011bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 1012ab5d26daSAndreas Gohr ) { 10138b06d178Schris msg($lang['profnoempty'], -1); 10148b06d178Schris return false; 10158b06d178Schris } 1016bcc94b2cSAndreas Gohr if (!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10178b06d178Schris msg($lang['regbadmail'], -1); 10188b06d178Schris return false; 10198b06d178Schris } 10208b06d178Schris 1021bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10224c21b7eeSAndreas Gohr 1023bcc94b2cSAndreas Gohr // check for unavailable capabilities 1024bcc94b2cSAndreas Gohr if (!$auth->canDo('modName')) unset($changes['name']); 1025bcc94b2cSAndreas Gohr if (!$auth->canDo('modMail')) unset($changes['mail']); 1026bcc94b2cSAndreas Gohr if (!$auth->canDo('modPass')) unset($changes['pass']); 1027bcc94b2cSAndreas Gohr 1028bcc94b2cSAndreas Gohr // anything to do? 102924870174SAndreas Gohr if ($changes === []) { 10308b06d178Schris msg($lang['profnochange'], -1); 10318b06d178Schris return false; 10328b06d178Schris } 10338b06d178Schris 10348b06d178Schris if ($conf['profileconfirm']) { 1035585bf44eSChristopher Smith if (!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 103671422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 10378b06d178Schris return false; 10388b06d178Schris } 10398b06d178Schris } 10408b06d178Schris 104124870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$INPUT->server->str('REMOTE_USER'), &$changes])) { 1042db9faf02SPatrick Brown msg($lang['proffail'], -1); 1043db9faf02SPatrick Brown return false; 1044db9faf02SPatrick Brown } 1045db9faf02SPatrick Brown 104632ed2b36SAndreas Gohr if ($changes['pass']) { 1047c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 1048a19c9aa0SGerrit Uitslag [/* user */, $sticky, /* pass */] = auth_getCookie(); 104904369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1050585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 1051c276e9e8SMarcel Pennewiss } else { 1052c276e9e8SMarcel Pennewiss // make sure the session is writable 1053c276e9e8SMarcel Pennewiss @session_start(); 1054c276e9e8SMarcel Pennewiss // invalidate session cache 1055c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 1056c276e9e8SMarcel Pennewiss session_write_close(); 105732ed2b36SAndreas Gohr } 1058c276e9e8SMarcel Pennewiss 105925b2a98cSMichael Klier return true; 1060a0b5b007SChris Smith} 1061ab5d26daSAndreas Gohr 106204d68ae4SGerrit Uitslag/** 106304d68ae4SGerrit Uitslag * Delete the current logged-in user 106404d68ae4SGerrit Uitslag * 106504d68ae4SGerrit Uitslag * @return bool true on success, false on any error 106604d68ae4SGerrit Uitslag */ 1067d868eb89SAndreas Gohrfunction auth_deleteprofile() 1068d868eb89SAndreas Gohr{ 10692a7abf2dSChristopher Smith global $conf; 10702a7abf2dSChristopher Smith global $lang; 1071*4dc42f7fSGerrit Uitslag /* @var AuthPlugin $auth */ 10722a7abf2dSChristopher Smith global $auth; 10732a7abf2dSChristopher Smith /* @var Input $INPUT */ 10742a7abf2dSChristopher Smith global $INPUT; 10752a7abf2dSChristopher Smith 10762a7abf2dSChristopher Smith if (!$INPUT->post->bool('delete')) return false; 10772a7abf2dSChristopher Smith if (!checkSecurityToken()) return false; 10782a7abf2dSChristopher Smith 10792a7abf2dSChristopher Smith // action prevented or auth module disallows 10802a7abf2dSChristopher Smith if (!actionOK('profile_delete') || !$auth->canDo('delUser')) { 10812a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 10822a7abf2dSChristopher Smith return false; 10832a7abf2dSChristopher Smith } 10842a7abf2dSChristopher Smith 10852a7abf2dSChristopher Smith if (!$INPUT->post->bool('confirm_delete')) { 10862a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 10872a7abf2dSChristopher Smith return false; 10882a7abf2dSChristopher Smith } 10892a7abf2dSChristopher Smith 10902a7abf2dSChristopher Smith if ($conf['profileconfirm']) { 1091585bf44eSChristopher Smith if (!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 10922a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 10932a7abf2dSChristopher Smith return false; 10942a7abf2dSChristopher Smith } 10952a7abf2dSChristopher Smith } 10962a7abf2dSChristopher Smith 109724870174SAndreas Gohr $deleted = []; 1098585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 109924870174SAndreas Gohr if ($auth->triggerUserMod('delete', [$deleted])) { 11002a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 11012a7abf2dSChristopher Smith auth_logoff(); 11022a7abf2dSChristopher Smith return true; 11032a7abf2dSChristopher Smith } 11042a7abf2dSChristopher Smith 11052a7abf2dSChristopher Smith return false; 11062a7abf2dSChristopher Smith} 11072a7abf2dSChristopher Smith 11088b06d178Schris/** 11098b06d178Schris * Send a new password 11108b06d178Schris * 11111d5856cfSAndreas Gohr * This function handles both phases of the password reset: 11121d5856cfSAndreas Gohr * 11131d5856cfSAndreas Gohr * - handling the first request of password reset 11141d5856cfSAndreas Gohr * - validating the password reset auth token 11151d5856cfSAndreas Gohr * 1116*4dc42f7fSGerrit Uitslag * @return bool true on success, false on any error 1117*4dc42f7fSGerrit Uitslag * @throws Exception 1118*4dc42f7fSGerrit Uitslag * 1119*4dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 11208b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 11218b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 11228b06d178Schris */ 1123d868eb89SAndreas Gohrfunction act_resendpwd() 1124d868eb89SAndreas Gohr{ 11258b06d178Schris global $lang; 11268b06d178Schris global $conf; 1127e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1128cd52f92dSchris global $auth; 1129bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1130bcc94b2cSAndreas Gohr global $INPUT; 11318b06d178Schris 11323a48618aSAnika Henke if (!actionOK('resendpwd')) { 11338b06d178Schris msg($lang['resendna'], -1); 11348b06d178Schris return false; 11358b06d178Schris } 11368b06d178Schris 1137bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 11388b06d178Schris 11391d5856cfSAndreas Gohr if ($token) { 1140cc204bbdSAndreas Gohr // we're in token phase - get user info from token 11411d5856cfSAndreas Gohr 11422401f18dSSyntaxseed $tfile = $conf['cachedir'] . '/' . $token[0] . '/' . $token . '.pwauth'; 114379e79377SAndreas Gohr if (!file_exists($tfile)) { 11441d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1145bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11461d5856cfSAndreas Gohr return false; 11471d5856cfSAndreas Gohr } 11488a9735e3SAndreas Gohr // token is only valid for 3 days 11498a9735e3SAndreas Gohr if ((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 11508a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1151bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11521d5856cfSAndreas Gohr @unlink($tfile); 11538a9735e3SAndreas Gohr return false; 11548a9735e3SAndreas Gohr } 11558a9735e3SAndreas Gohr 11568b06d178Schris $user = io_readfile($tfile); 1157*4dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 11588b06d178Schris if (!$userinfo['mail']) { 11598b06d178Schris msg($lang['resendpwdnouser'], -1); 11608b06d178Schris return false; 11618b06d178Schris } 11628b06d178Schris 1163cc204bbdSAndreas Gohr if (!$conf['autopasswd']) { // we let the user choose a password 1164bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1165bcc94b2cSAndreas Gohr 1166cc204bbdSAndreas Gohr // password given correctly? 1167bcc94b2cSAndreas Gohr if (!$pass) return false; 1168bcc94b2cSAndreas Gohr if ($pass != $INPUT->str('passchk')) { 1169451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1170cc204bbdSAndreas Gohr return false; 1171cc204bbdSAndreas Gohr } 1172cc204bbdSAndreas Gohr 1173bcc94b2cSAndreas Gohr // change it 117424870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1175db9faf02SPatrick Brown msg($lang['proffail'], -1); 1176cc204bbdSAndreas Gohr return false; 1177cc204bbdSAndreas Gohr } 1178cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 11798a285f7fSAndreas Gohr $pass = auth_pwgen($user); 118024870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1181db9faf02SPatrick Brown msg($lang['proffail'], -1); 11828b06d178Schris return false; 11838b06d178Schris } 11848b06d178Schris 11858b06d178Schris if (auth_sendPassword($user, $pass)) { 11868b06d178Schris msg($lang['resendpwdsuccess'], 1); 11878b06d178Schris } else { 11888b06d178Schris msg($lang['regmailfail'], -1); 11898b06d178Schris } 1190cc204bbdSAndreas Gohr } 1191cc204bbdSAndreas Gohr 1192cc204bbdSAndreas Gohr @unlink($tfile); 11938b06d178Schris return true; 11941d5856cfSAndreas Gohr } else { 11951d5856cfSAndreas Gohr // we're in request phase 11961d5856cfSAndreas Gohr 1197bcc94b2cSAndreas Gohr if (!$INPUT->post->bool('save')) return false; 11981d5856cfSAndreas Gohr 1199bcc94b2cSAndreas Gohr if (!$INPUT->post->str('login')) { 12001d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 12011d5856cfSAndreas Gohr return false; 12021d5856cfSAndreas Gohr } else { 1203bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 12041d5856cfSAndreas Gohr } 12051d5856cfSAndreas Gohr 1206*4dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 12071d5856cfSAndreas Gohr if (!$userinfo['mail']) { 12081d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 12091d5856cfSAndreas Gohr return false; 12101d5856cfSAndreas Gohr } 12111d5856cfSAndreas Gohr 12121d5856cfSAndreas Gohr // generate auth token 1213483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 12142401f18dSSyntaxseed $tfile = $conf['cachedir'] . '/' . $token[0] . '/' . $token . '.pwauth'; 121524870174SAndreas Gohr $url = wl('', ['do' => 'resendpwd', 'pwauth' => $token], true, '&'); 12161d5856cfSAndreas Gohr 12171d5856cfSAndreas Gohr io_saveFile($tfile, $user); 12181d5856cfSAndreas Gohr 12191d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 122024870174SAndreas Gohr $trep = ['FULLNAME' => $userinfo['name'], 'LOGIN' => $user, 'CONFIRM' => $url]; 12211d5856cfSAndreas Gohr 1222d7169d19SAndreas Gohr $mail = new Mailer(); 1223d7169d19SAndreas Gohr $mail->to($userinfo['name'] . ' <' . $userinfo['mail'] . '>'); 1224d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1225d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1226d7169d19SAndreas Gohr if ($mail->send()) { 12271d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 12281d5856cfSAndreas Gohr } else { 12291d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 12301d5856cfSAndreas Gohr } 12311d5856cfSAndreas Gohr return true; 12321d5856cfSAndreas Gohr } 1233ab5d26daSAndreas Gohr // never reached 12348b06d178Schris} 12358b06d178Schris 12368b06d178Schris/** 1237b0855b11Sandi * Encrypts a password using the given method and salt 1238b0855b11Sandi * 1239b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1240b0855b11Sandi * is chosen. 1241b0855b11Sandi * 1242b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 124342ea7f44SGerrit Uitslag * 1244ab5d26daSAndreas Gohr * @param string $clear The clear text password 1245ab5d26daSAndreas Gohr * @param string $method The hashing method 1246ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1247b0855b11Sandi * @return string The crypted password 1248b0855b11Sandi */ 1249d868eb89SAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) 1250d868eb89SAndreas Gohr{ 1251b0855b11Sandi global $conf; 1252b0855b11Sandi if (empty($method)) $method = $conf['passcrypt']; 125310a76f6fSfrank 12543a0a2d05SAndreas Gohr $pass = new PassHash(); 12553a0a2d05SAndreas Gohr $call = 'hash_' . $method; 1256b0855b11Sandi 12573a0a2d05SAndreas Gohr if (!method_exists($pass, $call)) { 1258b0855b11Sandi msg("Unsupported crypt method $method", -1); 12593a0a2d05SAndreas Gohr return false; 1260b0855b11Sandi } 12613a0a2d05SAndreas Gohr 12623a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1263b0855b11Sandi} 1264b0855b11Sandi 1265b0855b11Sandi/** 1266b0855b11Sandi * Verifies a cleartext password against a crypted hash 1267b0855b11Sandi * 1268ab5d26daSAndreas Gohr * @param string $clear The clear text password 1269ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1270ab5d26daSAndreas Gohr * @return bool true if both match 1271*4dc42f7fSGerrit Uitslag * @throws Exception 1272*4dc42f7fSGerrit Uitslag * 1273*4dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 1274b0855b11Sandi */ 1275d868eb89SAndreas Gohrfunction auth_verifyPassword($clear, $crypt) 1276d868eb89SAndreas Gohr{ 12773a0a2d05SAndreas Gohr $pass = new PassHash(); 12783a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1279b0855b11Sandi} 1280340756e4Sandi 1281a0b5b007SChris Smith/** 1282a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1283a0b5b007SChris Smith * 1284a0b5b007SChris Smith * @param string $user username 1285a0b5b007SChris Smith * @param string $pass encrypted password 1286a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1287ab5d26daSAndreas Gohr * @return bool 1288a0b5b007SChris Smith */ 1289d868eb89SAndreas Gohrfunction auth_setCookie($user, $pass, $sticky) 1290d868eb89SAndreas Gohr{ 1291a0b5b007SChris Smith global $conf; 1292e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1293a0b5b007SChris Smith global $auth; 129479d00841SOliver Geisen global $USERINFO; 1295a0b5b007SChris Smith 1296beca106aSAdrian Lang if (!$auth) return false; 1297a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1298a0b5b007SChris Smith 1299a0b5b007SChris Smith // set cookie 1300645c0a36SAndreas Gohr $cookie = base64_encode($user) . '|' . ((int) $sticky) . '|' . base64_encode($pass); 130173ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1302c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1303bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, $cookie, [ 1304bf8392ebSAndreas Gohr 'expires' => $time, 1305bf8392ebSAndreas Gohr 'path' => $cookieDir, 1306bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 1307bf8392ebSAndreas Gohr 'httponly' => true, 1308486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 1309bf8392ebSAndreas Gohr ]); 131055a71a16SGerrit Uitslag 1311a0b5b007SChris Smith // set session 1312a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1313234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1314a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1315a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1316a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1317ab5d26daSAndreas Gohr 1318ab5d26daSAndreas Gohr return true; 1319a0b5b007SChris Smith} 1320a0b5b007SChris Smith 1321645c0a36SAndreas Gohr/** 1322645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1323645c0a36SAndreas Gohr * 1324645c0a36SAndreas Gohr * @returns array 1325645c0a36SAndreas Gohr */ 1326d868eb89SAndreas Gohrfunction auth_getCookie() 1327d868eb89SAndreas Gohr{ 1328c66972f2SAdrian Lang if (!isset($_COOKIE[DOKU_COOKIE])) { 132924870174SAndreas Gohr return [null, null, null]; 1330c66972f2SAdrian Lang } 133124870174SAndreas Gohr [$user, $sticky, $pass] = sexplode('|', $_COOKIE[DOKU_COOKIE], 3, ''); 1332645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1333645c0a36SAndreas Gohr $pass = base64_decode($pass); 1334645c0a36SAndreas Gohr $user = base64_decode($user); 133524870174SAndreas Gohr return [$user, $sticky, $pass]; 1336645c0a36SAndreas Gohr} 1337645c0a36SAndreas Gohr 1338e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1339