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 566547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) { 57*71c734a9SGerrit Uitslag 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; 956547cfc7SGerrit Uitslag if ($auth instanceof AuthPlugin && $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 1734dc42f7fSGerrit 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 2114dc42f7fSGerrit Uitslag * @throws Exception 2124dc42f7fSGerrit Uitslag * 2134dc42f7fSGerrit 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 2256547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) 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 3174dc42f7fSGerrit Uitslag * @throws Exception 3184dc42f7fSGerrit Uitslag * 3194dc42f7fSGerrit 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 3474dc42f7fSGerrit Uitslag * @throws Exception 3484dc42f7fSGerrit Uitslag * 3494dc42f7fSGerrit 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 3624dc42f7fSGerrit Uitslag * @throws Exception 3634dc42f7fSGerrit Uitslag * 3644dc42f7fSGerrit 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 3804dc42f7fSGerrit 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 4586547cfc7SGerrit Uitslag if ($auth instanceof AuthPlugin) { 4596547cfc7SGerrit Uitslag $auth->logOff(); 4606547cfc7SGerrit Uitslag } 461f3f0262cSandi} 462f3f0262cSandi 463f3f0262cSandi/** 464f8cc712eSAndreas Gohr * Check if a user is a manager 465f8cc712eSAndreas Gohr * 466f8cc712eSAndreas Gohr * Should usually be called without any parameters to check the current 467f8cc712eSAndreas Gohr * user. 468f8cc712eSAndreas Gohr * 469f8cc712eSAndreas Gohr * The info is available through $INFO['ismanager'], too 470f8cc712eSAndreas Gohr * 471ab5d26daSAndreas Gohr * @param string $user Username 472ab5d26daSAndreas Gohr * @param array $groups List of groups the user is in 473ab5d26daSAndreas Gohr * @param bool $adminonly when true checks if user is admin 47410396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 475ab5d26daSAndreas Gohr * @return bool 47696348f27SAndreas Gohr * @see auth_isadmin 47796348f27SAndreas Gohr * 47896348f27SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 479f8cc712eSAndreas Gohr */ 480d868eb89SAndreas Gohrfunction auth_ismanager($user = null, $groups = null, $adminonly = false, $recache = false) 481d868eb89SAndreas Gohr{ 482f8cc712eSAndreas Gohr global $conf; 483f8cc712eSAndreas Gohr global $USERINFO; 484e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 485d752aedeSAndreas Gohr global $auth; 486585bf44eSChristopher Smith /* @var Input $INPUT */ 487585bf44eSChristopher Smith global $INPUT; 488585bf44eSChristopher Smith 489f8cc712eSAndreas Gohr 4906547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 491c66972f2SAdrian Lang if (is_null($user)) { 492585bf44eSChristopher Smith if (!$INPUT->server->has('REMOTE_USER')) { 493c66972f2SAdrian Lang return false; 494c66972f2SAdrian Lang } else { 495585bf44eSChristopher Smith $user = $INPUT->server->str('REMOTE_USER'); 496c66972f2SAdrian Lang } 497c66972f2SAdrian Lang } 498d6dc956fSAndreas Gohr if (is_null($groups)) { 4991525c228SAnna Dabrowska // checking the logged in user, or another one? 5001525c228SAnna Dabrowska if ($USERINFO && $user === $INPUT->server->str('REMOTE_USER')) { 5011525c228SAnna Dabrowska $groups = (array) $USERINFO['grps']; 50266b108d6SAnna Dabrowska } else { 5036cf7b139SAndreas Gohr $groups = $auth->getUserData($user); 5046cf7b139SAndreas Gohr $groups = $groups ? $groups['grps'] : []; 50566b108d6SAnna Dabrowska } 506e259aa79SAndreas Gohr } 507e259aa79SAndreas Gohr 50896348f27SAndreas Gohr // prefer cached result 50996348f27SAndreas Gohr static $cache = []; 51010396f77SAndreas Gohr $cachekey = serialize([$user, $adminonly, $groups]); 51196348f27SAndreas Gohr if (!isset($cache[$cachekey]) || $recache) { 512d6dc956fSAndreas Gohr // check superuser match 51396348f27SAndreas Gohr $ok = auth_isMember($conf['superuser'], $user, $groups); 51400ce12daSChris Smith 51596348f27SAndreas Gohr // check managers 51696348f27SAndreas Gohr if (!$ok && !$adminonly) { 51796348f27SAndreas Gohr $ok = auth_isMember($conf['manager'], $user, $groups); 51896348f27SAndreas Gohr } 51996348f27SAndreas Gohr 52096348f27SAndreas Gohr $cache[$cachekey] = $ok; 52196348f27SAndreas Gohr } 52296348f27SAndreas Gohr 52396348f27SAndreas Gohr return $cache[$cachekey]; 524f8cc712eSAndreas Gohr} 525f8cc712eSAndreas Gohr 526f8cc712eSAndreas Gohr/** 527f8cc712eSAndreas Gohr * Check if a user is admin 528f8cc712eSAndreas Gohr * 529f8cc712eSAndreas Gohr * Alias to auth_ismanager with adminonly=true 530f8cc712eSAndreas Gohr * 531f8cc712eSAndreas Gohr * The info is available through $INFO['isadmin'], too 532f8cc712eSAndreas Gohr * 53396348f27SAndreas Gohr * @param string $user Username 53496348f27SAndreas Gohr * @param array $groups List of groups the user is in 53510396f77SAndreas Gohr * @param bool $recache set to true to refresh the cache 53696348f27SAndreas Gohr * @return bool 537f8cc712eSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 538ab5d26daSAndreas Gohr * @see auth_ismanager() 53942ea7f44SGerrit Uitslag * 540f8cc712eSAndreas Gohr */ 541d868eb89SAndreas Gohrfunction auth_isadmin($user = null, $groups = null, $recache = false) 542d868eb89SAndreas Gohr{ 54396348f27SAndreas Gohr return auth_ismanager($user, $groups, true, $recache); 544f8cc712eSAndreas Gohr} 545f8cc712eSAndreas Gohr 546d6dc956fSAndreas Gohr/** 547d6dc956fSAndreas Gohr * Match a user and his groups against a comma separated list of 548d6dc956fSAndreas Gohr * users and groups to determine membership status 549d6dc956fSAndreas Gohr * 550d6dc956fSAndreas Gohr * Note: all input should NOT be nameencoded. 551d6dc956fSAndreas Gohr * 55242ea7f44SGerrit Uitslag * @param string $memberlist commaseparated list of allowed users and groups 55342ea7f44SGerrit Uitslag * @param string $user user to match against 55442ea7f44SGerrit Uitslag * @param array $groups groups the user is member of 5555446f3ffSDominik Eckelmann * @return bool true for membership acknowledged 556d6dc956fSAndreas Gohr */ 557d868eb89SAndreas Gohrfunction auth_isMember($memberlist, $user, array $groups) 558d868eb89SAndreas Gohr{ 559e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 560d6dc956fSAndreas Gohr global $auth; 5616547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 562d6dc956fSAndreas Gohr 563d6dc956fSAndreas Gohr // clean user and groups 5644f56ecbfSAdrian Lang if (!$auth->isCaseSensitive()) { 56524870174SAndreas Gohr $user = PhpString::strtolower($user); 56624870174SAndreas Gohr $groups = array_map([PhpString::class, 'strtolower'], $groups); 567d6dc956fSAndreas Gohr } 568d6dc956fSAndreas Gohr $user = $auth->cleanUser($user); 56924870174SAndreas Gohr $groups = array_map([$auth, 'cleanGroup'], $groups); 570d6dc956fSAndreas Gohr 571d6dc956fSAndreas Gohr // extract the memberlist 572d6dc956fSAndreas Gohr $members = explode(',', $memberlist); 573d6dc956fSAndreas Gohr $members = array_map('trim', $members); 574d6dc956fSAndreas Gohr $members = array_unique($members); 575d6dc956fSAndreas Gohr $members = array_filter($members); 576d6dc956fSAndreas Gohr 577d6dc956fSAndreas Gohr // compare cleaned values 578d6dc956fSAndreas Gohr foreach ($members as $member) { 579e5204a12SJurgen Hart if ($member == '@ALL') return true; 58024870174SAndreas Gohr if (!$auth->isCaseSensitive()) $member = PhpString::strtolower($member); 581d6dc956fSAndreas Gohr if ($member[0] == '@') { 582d6dc956fSAndreas Gohr $member = $auth->cleanGroup(substr($member, 1)); 583d6dc956fSAndreas Gohr if (in_array($member, $groups)) return true; 584d6dc956fSAndreas Gohr } else { 585d6dc956fSAndreas Gohr $member = $auth->cleanUser($member); 586d6dc956fSAndreas Gohr if ($member == $user) return true; 587d6dc956fSAndreas Gohr } 588d6dc956fSAndreas Gohr } 589d6dc956fSAndreas Gohr 590d6dc956fSAndreas Gohr // still here? not a member! 591d6dc956fSAndreas Gohr return false; 592d6dc956fSAndreas Gohr} 593d6dc956fSAndreas Gohr 594f8cc712eSAndreas Gohr/** 59515fae107Sandi * Convinience function for auth_aclcheck() 59615fae107Sandi * 59715fae107Sandi * This checks the permissions for the current user 59815fae107Sandi * 59915fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 60015fae107Sandi * 6011698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 60215fae107Sandi * @return int permission level 603f3f0262cSandi */ 604d868eb89SAndreas Gohrfunction auth_quickaclcheck($id) 605d868eb89SAndreas Gohr{ 606f3f0262cSandi global $conf; 607f3f0262cSandi global $USERINFO; 608585bf44eSChristopher Smith /* @var Input $INPUT */ 609585bf44eSChristopher Smith global $INPUT; 610f3f0262cSandi # if no ACL is used always return upload rights 611f3f0262cSandi if (!$conf['useacl']) return AUTH_UPLOAD; 61224870174SAndreas Gohr return auth_aclcheck($id, $INPUT->server->str('REMOTE_USER'), is_array($USERINFO) ? $USERINFO['grps'] : []); 613f3f0262cSandi} 614f3f0262cSandi 615f3f0262cSandi/** 616c17acc9fSAndreas Gohr * Returns the maximum rights a user has for the given ID or its namespace 61715fae107Sandi * 61815fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 61942ea7f44SGerrit Uitslag * 620c17acc9fSAndreas Gohr * @triggers AUTH_ACL_CHECK 6211698b983Smichael * @param string $id page ID (needs to be resolved and cleaned) 62215fae107Sandi * @param string $user Username 6233272d797SAndreas Gohr * @param array|null $groups Array of groups the user is in 62415fae107Sandi * @return int permission level 625f3f0262cSandi */ 626d868eb89SAndreas Gohrfunction auth_aclcheck($id, $user, $groups) 627d868eb89SAndreas Gohr{ 62824870174SAndreas Gohr $data = [ 629bf8f8509SAndreas Gohr 'id' => $id ?? '', 630c17acc9fSAndreas Gohr 'user' => $user, 631c17acc9fSAndreas Gohr 'groups' => $groups 63224870174SAndreas Gohr ]; 633c17acc9fSAndreas Gohr 634cbb44eabSAndreas Gohr return Event::createAndTrigger('AUTH_ACL_CHECK', $data, 'auth_aclcheck_cb'); 635c17acc9fSAndreas Gohr} 636c17acc9fSAndreas Gohr 637c17acc9fSAndreas Gohr/** 638c17acc9fSAndreas Gohr * default ACL check method 639c17acc9fSAndreas Gohr * 640c17acc9fSAndreas Gohr * DO NOT CALL DIRECTLY, use auth_aclcheck() instead 641c17acc9fSAndreas Gohr * 642c17acc9fSAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 64342ea7f44SGerrit Uitslag * 644c17acc9fSAndreas Gohr * @param array $data event data 645c17acc9fSAndreas Gohr * @return int permission level 646c17acc9fSAndreas Gohr */ 647d868eb89SAndreas Gohrfunction auth_aclcheck_cb($data) 648d868eb89SAndreas Gohr{ 649c17acc9fSAndreas Gohr $id =& $data['id']; 650c17acc9fSAndreas Gohr $user =& $data['user']; 651c17acc9fSAndreas Gohr $groups =& $data['groups']; 652c17acc9fSAndreas Gohr 653f3f0262cSandi global $conf; 654f3f0262cSandi global $AUTH_ACL; 655e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 656d752aedeSAndreas Gohr global $auth; 657f3f0262cSandi 65885d03f68SAndreas Gohr // if no ACL is used always return upload rights 659f3f0262cSandi if (!$conf['useacl']) return AUTH_UPLOAD; 6606547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return AUTH_NONE; 661bf8f8509SAndreas Gohr if (!is_array($AUTH_ACL)) return AUTH_NONE; 662f3f0262cSandi 663074cf26bSandi //make sure groups is an array 66424870174SAndreas Gohr if (!is_array($groups)) $groups = []; 665074cf26bSandi 66685d03f68SAndreas Gohr //if user is superuser or in superusergroup return 255 (acl_admin) 667ab5d26daSAndreas Gohr if (auth_isadmin($user, $groups)) { 668ab5d26daSAndreas Gohr return AUTH_ADMIN; 669ab5d26daSAndreas Gohr } 67085d03f68SAndreas Gohr 671eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive()) { 67224870174SAndreas Gohr $user = PhpString::strtolower($user); 67324870174SAndreas Gohr $groups = array_map([PhpString::class, 'strtolower'], $groups); 674eb3ce0d5SKazutaka Miyasaka } 67537ff2261SSascha Klopp $user = auth_nameencode($auth->cleanUser($user)); 67624870174SAndreas Gohr $groups = array_map([$auth, 'cleanGroup'], $groups); 67785d03f68SAndreas Gohr 6786c2bb100SAndreas Gohr //prepend groups with @ and nameencode 67937ff2261SSascha Klopp foreach ($groups as &$group) { 68037ff2261SSascha Klopp $group = '@' . auth_nameencode($group); 68110a76f6fSfrank } 68210a76f6fSfrank 683f3f0262cSandi $ns = getNS($id); 684f3f0262cSandi $perm = -1; 685f3f0262cSandi 686f3f0262cSandi //add ALL group 687f3f0262cSandi $groups[] = '@ALL'; 68837ff2261SSascha Klopp 689f3f0262cSandi //add User 69034aeb4afSAndreas Gohr if ($user) $groups[] = $user; 691f3f0262cSandi 692f3f0262cSandi //check exact match first 69321c3090aSChristopher Smith $matches = preg_grep('/^' . preg_quote($id, '/') . '[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 694f3f0262cSandi if (count($matches)) { 695f3f0262cSandi foreach ($matches as $match) { 696f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 69721c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 698eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 69924870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 700eb3ce0d5SKazutaka Miyasaka } 70148d7b7a6SDominik Eckelmann if (!in_array($acl[1], $groups)) { 70248d7b7a6SDominik Eckelmann continue; 70348d7b7a6SDominik Eckelmann } 7048ef6b7caSandi if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 705f3f0262cSandi if ($acl[2] > $perm) { 706f3f0262cSandi $perm = $acl[2]; 707f3f0262cSandi } 708f3f0262cSandi } 709f3f0262cSandi if ($perm > -1) { 710f3f0262cSandi //we had a match - return it 711def492a2SGuillaume Turri return (int) $perm; 712f3f0262cSandi } 713f3f0262cSandi } 714f3f0262cSandi 715f3f0262cSandi //still here? do the namespace checks 716f3f0262cSandi if ($ns) { 7173e304b55SMichael Hamann $path = $ns . ':*'; 718f3f0262cSandi } else { 7193e304b55SMichael Hamann $path = '*'; //root document 720f3f0262cSandi } 721f3f0262cSandi 722f3f0262cSandi do { 72321c3090aSChristopher Smith $matches = preg_grep('/^' . preg_quote($path, '/') . '[ \t]+([^ \t]+)[ \t]+/', $AUTH_ACL); 724f3f0262cSandi if (count($matches)) { 725f3f0262cSandi foreach ($matches as $match) { 726f3f0262cSandi $match = preg_replace('/#.*$/', '', $match); //ignore comments 72721c3090aSChristopher Smith $acl = preg_split('/[ \t]+/', $match); 728eb3ce0d5SKazutaka Miyasaka if (!$auth->isCaseSensitive() && $acl[1] !== '@ALL') { 72924870174SAndreas Gohr $acl[1] = PhpString::strtolower($acl[1]); 730eb3ce0d5SKazutaka Miyasaka } 73148d7b7a6SDominik Eckelmann if (!in_array($acl[1], $groups)) { 73248d7b7a6SDominik Eckelmann continue; 73348d7b7a6SDominik Eckelmann } 7348ef6b7caSandi if ($acl[2] > AUTH_DELETE) $acl[2] = AUTH_DELETE; //no admins in the ACL! 735f3f0262cSandi if ($acl[2] > $perm) { 736f3f0262cSandi $perm = $acl[2]; 737f3f0262cSandi } 738f3f0262cSandi } 739f3f0262cSandi //we had a match - return it 74048d7b7a6SDominik Eckelmann if ($perm != -1) { 741def492a2SGuillaume Turri return (int) $perm; 742f3f0262cSandi } 74348d7b7a6SDominik Eckelmann } 744f3f0262cSandi //get next higher namespace 745f3f0262cSandi $ns = getNS($ns); 746f3f0262cSandi 7473e304b55SMichael Hamann if ($path != '*') { 7483e304b55SMichael Hamann $path = $ns . ':*'; 7493e304b55SMichael Hamann if ($path == ':*') $path = '*'; 750f3f0262cSandi } else { 751f3f0262cSandi //we did this already 752f3f0262cSandi //looks like there is something wrong with the ACL 753f3f0262cSandi //break here 754d5ce66f6SAndreas Gohr msg('No ACL setup yet! Denying access to everyone.'); 755d5ce66f6SAndreas Gohr return AUTH_NONE; 756f3f0262cSandi } 757f3f0262cSandi } while (1); //this should never loop endless 758ab5d26daSAndreas Gohr return AUTH_NONE; 759f3f0262cSandi} 760f3f0262cSandi 761f3f0262cSandi/** 7626c2bb100SAndreas Gohr * Encode ASCII special chars 7636c2bb100SAndreas Gohr * 7646c2bb100SAndreas Gohr * Some auth backends allow special chars in their user and groupnames 7656c2bb100SAndreas Gohr * The special chars are encoded with this function. Only ASCII chars 7666c2bb100SAndreas Gohr * are encoded UTF-8 multibyte are left as is (different from usual 7676c2bb100SAndreas Gohr * urlencoding!). 7686c2bb100SAndreas Gohr * 7696c2bb100SAndreas Gohr * Decoding can be done with rawurldecode 7706c2bb100SAndreas Gohr * 7716c2bb100SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 7726c2bb100SAndreas Gohr * @see rawurldecode() 77342ea7f44SGerrit Uitslag * 77442ea7f44SGerrit Uitslag * @param string $name 77542ea7f44SGerrit Uitslag * @param bool $skip_group 77642ea7f44SGerrit Uitslag * @return string 7776c2bb100SAndreas Gohr */ 778d868eb89SAndreas Gohrfunction auth_nameencode($name, $skip_group = false) 779d868eb89SAndreas Gohr{ 780a424cd8eSchris global $cache_authname; 781a424cd8eSchris $cache =& $cache_authname; 78231784267SAndreas Gohr $name = (string) $name; 783a424cd8eSchris 78480601d26SAndreas Gohr // never encode wildcard FS#1955 78580601d26SAndreas Gohr if ($name == '%USER%') return $name; 786b78bf706Sromain if ($name == '%GROUP%') return $name; 78780601d26SAndreas Gohr 788a424cd8eSchris if (!isset($cache[$name][$skip_group])) { 7892401f18dSSyntaxseed if ($skip_group && $name[0] == '@') { 79030f6faf0SChristopher Smith $cache[$name][$skip_group] = '@' . preg_replace_callback( 79130f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 792dccd6b2bSAndreas Gohr 'auth_nameencode_callback', 793dccd6b2bSAndreas Gohr substr($name, 1) 794ab5d26daSAndreas Gohr ); 795e838fc2eSAndreas Gohr } else { 79630f6faf0SChristopher Smith $cache[$name][$skip_group] = preg_replace_callback( 79730f6faf0SChristopher Smith '/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/', 798dccd6b2bSAndreas Gohr 'auth_nameencode_callback', 799dccd6b2bSAndreas Gohr $name 800ab5d26daSAndreas Gohr ); 801e838fc2eSAndreas Gohr } 8026c2bb100SAndreas Gohr } 8036c2bb100SAndreas Gohr 804a424cd8eSchris return $cache[$name][$skip_group]; 805a424cd8eSchris} 806a424cd8eSchris 80704d68ae4SGerrit Uitslag/** 80804d68ae4SGerrit Uitslag * callback encodes the matches 80904d68ae4SGerrit Uitslag * 81004d68ae4SGerrit Uitslag * @param array $matches first complete match, next matching subpatterms 81104d68ae4SGerrit Uitslag * @return string 81204d68ae4SGerrit Uitslag */ 813d868eb89SAndreas Gohrfunction auth_nameencode_callback($matches) 814d868eb89SAndreas Gohr{ 81530f6faf0SChristopher Smith return '%' . dechex(ord(substr($matches[1], -1))); 81630f6faf0SChristopher Smith} 81730f6faf0SChristopher Smith 8186c2bb100SAndreas Gohr/** 819f3f0262cSandi * Create a pronouncable password 820f3f0262cSandi * 8218a285f7fSAndreas Gohr * The $foruser variable might be used by plugins to run additional password 8228a285f7fSAndreas Gohr * policy checks, but is not used by the default implementation 8238a285f7fSAndreas Gohr * 8244dc42f7fSGerrit Uitslag * @param string $foruser username for which the password is generated 8254dc42f7fSGerrit Uitslag * @return string pronouncable password 8264dc42f7fSGerrit Uitslag * @throws Exception 8274dc42f7fSGerrit Uitslag * 82815fae107Sandi * @link http://www.phpbuilder.com/annotate/message.php3?id=1014451 8298a285f7fSAndreas Gohr * @triggers AUTH_PASSWORD_GENERATE 83015fae107Sandi * 8314dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 832f3f0262cSandi */ 833d868eb89SAndreas Gohrfunction auth_pwgen($foruser = '') 834d868eb89SAndreas Gohr{ 83524870174SAndreas Gohr $data = [ 836d628dcf3SAndreas Gohr 'password' => '', 837d628dcf3SAndreas Gohr 'foruser' => $foruser 83824870174SAndreas Gohr ]; 8398a285f7fSAndreas Gohr 840e1d9dcc8SAndreas Gohr $evt = new Event('AUTH_PASSWORD_GENERATE', $data); 8418a285f7fSAndreas Gohr if ($evt->advise_before(true)) { 842f3f0262cSandi $c = 'bcdfghjklmnprstvwz'; //consonants except hard to speak ones 843f3f0262cSandi $v = 'aeiou'; //vowels 844f3f0262cSandi $a = $c . $v; //both 845987c8d26SAndreas Gohr $s = '!$%&?+*~#-_:.;,'; // specials 846f3f0262cSandi 847987c8d26SAndreas Gohr //use thre syllables... 848987c8d26SAndreas Gohr for ($i = 0; $i < 3; $i++) { 849483b6238SMichael Hamann $data['password'] .= $c[auth_random(0, strlen($c) - 1)]; 850483b6238SMichael Hamann $data['password'] .= $v[auth_random(0, strlen($v) - 1)]; 851483b6238SMichael Hamann $data['password'] .= $a[auth_random(0, strlen($a) - 1)]; 852f3f0262cSandi } 853987c8d26SAndreas Gohr //... and add a nice number and special 85443f71e05Ssdavis80 $data['password'] .= $s[auth_random(0, strlen($s) - 1)] . auth_random(10, 99); 8558a285f7fSAndreas Gohr } 8568a285f7fSAndreas Gohr $evt->advise_after(); 857f3f0262cSandi 8588a285f7fSAndreas Gohr return $data['password']; 859f3f0262cSandi} 860f3f0262cSandi 861f3f0262cSandi/** 862f3f0262cSandi * Sends a password to the given user 863f3f0262cSandi * 86415fae107Sandi * @author Andreas Gohr <andi@splitbrain.org> 86542ea7f44SGerrit Uitslag * 866ab5d26daSAndreas Gohr * @param string $user Login name of the user 867ab5d26daSAndreas Gohr * @param string $password The new password in clear text 86815fae107Sandi * @return bool true on success 869f3f0262cSandi */ 870d868eb89SAndreas Gohrfunction auth_sendPassword($user, $password) 871d868eb89SAndreas Gohr{ 872f3f0262cSandi global $lang; 873e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 874cd52f92dSchris global $auth; 8756547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 876cd52f92dSchris 877d752aedeSAndreas Gohr $user = $auth->cleanUser($user); 8784dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 879f3f0262cSandi 88087ddda95Sandi if (!$userinfo['mail']) return false; 881f3f0262cSandi 882f3f0262cSandi $text = rawLocale('password'); 88324870174SAndreas Gohr $trep = [ 884d7169d19SAndreas Gohr 'FULLNAME' => $userinfo['name'], 885d7169d19SAndreas Gohr 'LOGIN' => $user, 886d7169d19SAndreas Gohr 'PASSWORD' => $password 88724870174SAndreas Gohr ]; 888f3f0262cSandi 889d7169d19SAndreas Gohr $mail = new Mailer(); 890102cdbd7SLarsGit223 $mail->to($mail->getCleanName($userinfo['name']) . ' <' . $userinfo['mail'] . '>'); 891d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 892d7169d19SAndreas Gohr $mail->setBody($text, $trep); 893d7169d19SAndreas Gohr return $mail->send(); 894f3f0262cSandi} 895f3f0262cSandi 896f3f0262cSandi/** 89715fae107Sandi * Register a new user 898f3f0262cSandi * 89915fae107Sandi * This registers a new user - Data is read directly from $_POST 90015fae107Sandi * 90115fae107Sandi * @return bool true on success, false on any error 9024dc42f7fSGerrit Uitslag * @throws Exception 9034dc42f7fSGerrit Uitslag * 9044dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 905f3f0262cSandi */ 906d868eb89SAndreas Gohrfunction register() 907d868eb89SAndreas Gohr{ 908f3f0262cSandi global $lang; 909eb5d07e4Sjan global $conf; 9104dc42f7fSGerrit Uitslag /* @var AuthPlugin $auth */ 911cd52f92dSchris global $auth; 91264273335SAndreas Gohr global $INPUT; 913f3f0262cSandi 91464273335SAndreas Gohr if (!$INPUT->post->bool('save')) return false; 9153a48618aSAnika Henke if (!actionOK('register')) return false; 916640145a5Sandi 91764273335SAndreas Gohr // gather input 91864273335SAndreas Gohr $login = trim($auth->cleanUser($INPUT->post->str('login'))); 91964273335SAndreas Gohr $fullname = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('fullname'))); 92064273335SAndreas Gohr $email = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $INPUT->post->str('email'))); 92164273335SAndreas Gohr $pass = $INPUT->post->str('pass'); 92264273335SAndreas Gohr $passchk = $INPUT->post->str('passchk'); 923d752aedeSAndreas Gohr 92464273335SAndreas Gohr if (empty($login) || empty($fullname) || empty($email)) { 925f3f0262cSandi msg($lang['regmissing'], -1); 926f3f0262cSandi return false; 927f3f0262cSandi } 928f3f0262cSandi 929cab2716aSmatthias.grimm if ($conf['autopasswd']) { 9308a285f7fSAndreas Gohr $pass = auth_pwgen($login); // automatically generate password 93164273335SAndreas Gohr } elseif (empty($pass) || empty($passchk)) { 932bf12ec81Sjan msg($lang['regmissing'], -1); // complain about missing passwords 933cab2716aSmatthias.grimm return false; 93464273335SAndreas Gohr } elseif ($pass != $passchk) { 935bf12ec81Sjan msg($lang['regbadpass'], -1); // complain about misspelled passwords 936cab2716aSmatthias.grimm return false; 937cab2716aSmatthias.grimm } 938cab2716aSmatthias.grimm 939f3f0262cSandi //check mail 94064273335SAndreas Gohr if (!mail_isvalid($email)) { 941f3f0262cSandi msg($lang['regbadmail'], -1); 942f3f0262cSandi return false; 943f3f0262cSandi } 944f3f0262cSandi 945f3f0262cSandi //okay try to create the user 94624870174SAndreas Gohr if (!$auth->triggerUserMod('create', [$login, $pass, $fullname, $email])) { 947db9faf02SPatrick Brown msg($lang['regfail'], -1); 948f3f0262cSandi return false; 949f3f0262cSandi } 950f3f0262cSandi 951790b7720SAndreas Gohr // send notification about the new user 95275d66495SMichael Große $subscription = new RegistrationSubscriptionSender(); 95375d66495SMichael Große $subscription->sendRegister($login, $fullname, $email); 95402a498e7Schris 955790b7720SAndreas Gohr // are we done? 956cab2716aSmatthias.grimm if (!$conf['autopasswd']) { 957cab2716aSmatthias.grimm msg($lang['regsuccess2'], 1); 958cab2716aSmatthias.grimm return true; 959cab2716aSmatthias.grimm } 960cab2716aSmatthias.grimm 961790b7720SAndreas Gohr // autogenerated password? then send password to user 96264273335SAndreas Gohr if (auth_sendPassword($login, $pass)) { 963f3f0262cSandi msg($lang['regsuccess'], 1); 964f3f0262cSandi return true; 965f3f0262cSandi } else { 966f3f0262cSandi msg($lang['regmailfail'], -1); 967f3f0262cSandi return false; 968f3f0262cSandi } 969f3f0262cSandi} 970f3f0262cSandi 97110a76f6fSfrank/** 9728b06d178Schris * Update user profile 9738b06d178Schris * 9744dc42f7fSGerrit Uitslag * @throws Exception 9754dc42f7fSGerrit Uitslag * 9768b06d178Schris * @author Christopher Smith <chris@jalakai.co.uk> 9778b06d178Schris */ 978d868eb89SAndreas Gohrfunction updateprofile() 979d868eb89SAndreas Gohr{ 9808b06d178Schris global $conf; 9818b06d178Schris global $lang; 982e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 983cd52f92dSchris global $auth; 984bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 985bcc94b2cSAndreas Gohr global $INPUT; 9868b06d178Schris 987bcc94b2cSAndreas Gohr if (!$INPUT->post->bool('save')) return false; 9881b2a85e8SAndreas Gohr if (!checkSecurityToken()) return false; 9898b06d178Schris 9903a48618aSAnika Henke if (!actionOK('profile')) { 9918b06d178Schris msg($lang['profna'], -1); 9928b06d178Schris return false; 9938b06d178Schris } 9948b06d178Schris 99524870174SAndreas Gohr $changes = []; 996bcc94b2cSAndreas Gohr $changes['pass'] = $INPUT->post->str('newpass'); 997bcc94b2cSAndreas Gohr $changes['name'] = $INPUT->post->str('fullname'); 998bcc94b2cSAndreas Gohr $changes['mail'] = $INPUT->post->str('email'); 999bcc94b2cSAndreas Gohr 1000bcc94b2cSAndreas Gohr // check misspelled passwords 1001bcc94b2cSAndreas Gohr if ($changes['pass'] != $INPUT->post->str('passchk')) { 1002bcc94b2cSAndreas Gohr msg($lang['regbadpass'], -1); 10038b06d178Schris return false; 10048b06d178Schris } 10058b06d178Schris 10068b06d178Schris // clean fullname and email 1007bcc94b2cSAndreas Gohr $changes['name'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['name'])); 1008bcc94b2cSAndreas Gohr $changes['mail'] = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $changes['mail'])); 10098b06d178Schris 1010bcc94b2cSAndreas Gohr // no empty name and email (except the backend doesn't support them) 10117d34963bSAndreas Gohr if ( 10127d34963bSAndreas Gohr (empty($changes['name']) && $auth->canDo('modName')) || 1013bcc94b2cSAndreas Gohr (empty($changes['mail']) && $auth->canDo('modMail')) 1014ab5d26daSAndreas Gohr ) { 10158b06d178Schris msg($lang['profnoempty'], -1); 10168b06d178Schris return false; 10178b06d178Schris } 1018bcc94b2cSAndreas Gohr if (!mail_isvalid($changes['mail']) && $auth->canDo('modMail')) { 10198b06d178Schris msg($lang['regbadmail'], -1); 10208b06d178Schris return false; 10218b06d178Schris } 10228b06d178Schris 1023bcc94b2cSAndreas Gohr $changes = array_filter($changes); 10244c21b7eeSAndreas Gohr 1025bcc94b2cSAndreas Gohr // check for unavailable capabilities 1026bcc94b2cSAndreas Gohr if (!$auth->canDo('modName')) unset($changes['name']); 1027bcc94b2cSAndreas Gohr if (!$auth->canDo('modMail')) unset($changes['mail']); 1028bcc94b2cSAndreas Gohr if (!$auth->canDo('modPass')) unset($changes['pass']); 1029bcc94b2cSAndreas Gohr 1030bcc94b2cSAndreas Gohr // anything to do? 103124870174SAndreas Gohr if ($changes === []) { 10328b06d178Schris msg($lang['profnochange'], -1); 10338b06d178Schris return false; 10348b06d178Schris } 10358b06d178Schris 10368b06d178Schris if ($conf['profileconfirm']) { 1037585bf44eSChristopher Smith if (!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 103871422fc8SChristopher Smith msg($lang['badpassconfirm'], -1); 10398b06d178Schris return false; 10408b06d178Schris } 10418b06d178Schris } 10428b06d178Schris 104324870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$INPUT->server->str('REMOTE_USER'), &$changes])) { 1044db9faf02SPatrick Brown msg($lang['proffail'], -1); 1045db9faf02SPatrick Brown return false; 1046db9faf02SPatrick Brown } 1047db9faf02SPatrick Brown 104832ed2b36SAndreas Gohr if ($changes['pass']) { 1049c276e9e8SMarcel Pennewiss // update cookie and session with the changed data 1050a19c9aa0SGerrit Uitslag [/* user */, $sticky, /* pass */] = auth_getCookie(); 105104369c3eSMichael Hamann $pass = auth_encrypt($changes['pass'], auth_cookiesalt(!$sticky, true)); 1052585bf44eSChristopher Smith auth_setCookie($INPUT->server->str('REMOTE_USER'), $pass, (bool) $sticky); 1053c276e9e8SMarcel Pennewiss } else { 1054c276e9e8SMarcel Pennewiss // make sure the session is writable 1055c276e9e8SMarcel Pennewiss @session_start(); 1056c276e9e8SMarcel Pennewiss // invalidate session cache 1057c276e9e8SMarcel Pennewiss $_SESSION[DOKU_COOKIE]['auth']['time'] = 0; 1058c276e9e8SMarcel Pennewiss session_write_close(); 105932ed2b36SAndreas Gohr } 1060c276e9e8SMarcel Pennewiss 106125b2a98cSMichael Klier return true; 1062a0b5b007SChris Smith} 1063ab5d26daSAndreas Gohr 106404d68ae4SGerrit Uitslag/** 106504d68ae4SGerrit Uitslag * Delete the current logged-in user 106604d68ae4SGerrit Uitslag * 106704d68ae4SGerrit Uitslag * @return bool true on success, false on any error 106804d68ae4SGerrit Uitslag */ 1069d868eb89SAndreas Gohrfunction auth_deleteprofile() 1070d868eb89SAndreas Gohr{ 10712a7abf2dSChristopher Smith global $conf; 10722a7abf2dSChristopher Smith global $lang; 10734dc42f7fSGerrit Uitslag /* @var AuthPlugin $auth */ 10742a7abf2dSChristopher Smith global $auth; 10752a7abf2dSChristopher Smith /* @var Input $INPUT */ 10762a7abf2dSChristopher Smith global $INPUT; 10772a7abf2dSChristopher Smith 10782a7abf2dSChristopher Smith if (!$INPUT->post->bool('delete')) return false; 10792a7abf2dSChristopher Smith if (!checkSecurityToken()) return false; 10802a7abf2dSChristopher Smith 10812a7abf2dSChristopher Smith // action prevented or auth module disallows 10822a7abf2dSChristopher Smith if (!actionOK('profile_delete') || !$auth->canDo('delUser')) { 10832a7abf2dSChristopher Smith msg($lang['profnodelete'], -1); 10842a7abf2dSChristopher Smith return false; 10852a7abf2dSChristopher Smith } 10862a7abf2dSChristopher Smith 10872a7abf2dSChristopher Smith if (!$INPUT->post->bool('confirm_delete')) { 10882a7abf2dSChristopher Smith msg($lang['profconfdeletemissing'], -1); 10892a7abf2dSChristopher Smith return false; 10902a7abf2dSChristopher Smith } 10912a7abf2dSChristopher Smith 10922a7abf2dSChristopher Smith if ($conf['profileconfirm']) { 1093585bf44eSChristopher Smith if (!$auth->checkPass($INPUT->server->str('REMOTE_USER'), $INPUT->post->str('oldpass'))) { 10942a7abf2dSChristopher Smith msg($lang['badpassconfirm'], -1); 10952a7abf2dSChristopher Smith return false; 10962a7abf2dSChristopher Smith } 10972a7abf2dSChristopher Smith } 10982a7abf2dSChristopher Smith 109924870174SAndreas Gohr $deleted = []; 1100585bf44eSChristopher Smith $deleted[] = $INPUT->server->str('REMOTE_USER'); 110124870174SAndreas Gohr if ($auth->triggerUserMod('delete', [$deleted])) { 11022a7abf2dSChristopher Smith // force and immediate logout including removing the sticky cookie 11032a7abf2dSChristopher Smith auth_logoff(); 11042a7abf2dSChristopher Smith return true; 11052a7abf2dSChristopher Smith } 11062a7abf2dSChristopher Smith 11072a7abf2dSChristopher Smith return false; 11082a7abf2dSChristopher Smith} 11092a7abf2dSChristopher Smith 11108b06d178Schris/** 11118b06d178Schris * Send a new password 11128b06d178Schris * 11131d5856cfSAndreas Gohr * This function handles both phases of the password reset: 11141d5856cfSAndreas Gohr * 11151d5856cfSAndreas Gohr * - handling the first request of password reset 11161d5856cfSAndreas Gohr * - validating the password reset auth token 11171d5856cfSAndreas Gohr * 11184dc42f7fSGerrit Uitslag * @return bool true on success, false on any error 11194dc42f7fSGerrit Uitslag * @throws Exception 11204dc42f7fSGerrit Uitslag * 11214dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 11228b06d178Schris * @author Benoit Chesneau <benoit@bchesneau.info> 11238b06d178Schris * @author Chris Smith <chris@jalakai.co.uk> 11248b06d178Schris */ 1125d868eb89SAndreas Gohrfunction act_resendpwd() 1126d868eb89SAndreas Gohr{ 11278b06d178Schris global $lang; 11288b06d178Schris global $conf; 1129e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1130cd52f92dSchris global $auth; 1131bcc94b2cSAndreas Gohr /* @var Input $INPUT */ 1132bcc94b2cSAndreas Gohr global $INPUT; 11338b06d178Schris 11343a48618aSAnika Henke if (!actionOK('resendpwd')) { 11358b06d178Schris msg($lang['resendna'], -1); 11368b06d178Schris return false; 11378b06d178Schris } 11388b06d178Schris 1139bcc94b2cSAndreas Gohr $token = preg_replace('/[^a-f0-9]+/', '', $INPUT->str('pwauth')); 11408b06d178Schris 11411d5856cfSAndreas Gohr if ($token) { 1142cc204bbdSAndreas Gohr // we're in token phase - get user info from token 11431d5856cfSAndreas Gohr 11442401f18dSSyntaxseed $tfile = $conf['cachedir'] . '/' . $token[0] . '/' . $token . '.pwauth'; 114579e79377SAndreas Gohr if (!file_exists($tfile)) { 11461d5856cfSAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1147bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11481d5856cfSAndreas Gohr return false; 11491d5856cfSAndreas Gohr } 11508a9735e3SAndreas Gohr // token is only valid for 3 days 11518a9735e3SAndreas Gohr if ((time() - filemtime($tfile)) > (3 * 60 * 60 * 24)) { 11528a9735e3SAndreas Gohr msg($lang['resendpwdbadauth'], -1); 1153bcc94b2cSAndreas Gohr $INPUT->remove('pwauth'); 11541d5856cfSAndreas Gohr @unlink($tfile); 11558a9735e3SAndreas Gohr return false; 11568a9735e3SAndreas Gohr } 11578a9735e3SAndreas Gohr 11588b06d178Schris $user = io_readfile($tfile); 11594dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 11608b06d178Schris if (!$userinfo['mail']) { 11618b06d178Schris msg($lang['resendpwdnouser'], -1); 11628b06d178Schris return false; 11638b06d178Schris } 11648b06d178Schris 1165cc204bbdSAndreas Gohr if (!$conf['autopasswd']) { // we let the user choose a password 1166bcc94b2cSAndreas Gohr $pass = $INPUT->str('pass'); 1167bcc94b2cSAndreas Gohr 1168cc204bbdSAndreas Gohr // password given correctly? 1169bcc94b2cSAndreas Gohr if (!$pass) return false; 1170bcc94b2cSAndreas Gohr if ($pass != $INPUT->str('passchk')) { 1171451e1b4dSAndreas Gohr msg($lang['regbadpass'], -1); 1172cc204bbdSAndreas Gohr return false; 1173cc204bbdSAndreas Gohr } 1174cc204bbdSAndreas Gohr 1175bcc94b2cSAndreas Gohr // change it 117624870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1177db9faf02SPatrick Brown msg($lang['proffail'], -1); 1178cc204bbdSAndreas Gohr return false; 1179cc204bbdSAndreas Gohr } 1180cc204bbdSAndreas Gohr } else { // autogenerate the password and send by mail 11818a285f7fSAndreas Gohr $pass = auth_pwgen($user); 118224870174SAndreas Gohr if (!$auth->triggerUserMod('modify', [$user, ['pass' => $pass]])) { 1183db9faf02SPatrick Brown msg($lang['proffail'], -1); 11848b06d178Schris return false; 11858b06d178Schris } 11868b06d178Schris 11878b06d178Schris if (auth_sendPassword($user, $pass)) { 11888b06d178Schris msg($lang['resendpwdsuccess'], 1); 11898b06d178Schris } else { 11908b06d178Schris msg($lang['regmailfail'], -1); 11918b06d178Schris } 1192cc204bbdSAndreas Gohr } 1193cc204bbdSAndreas Gohr 1194cc204bbdSAndreas Gohr @unlink($tfile); 11958b06d178Schris return true; 11961d5856cfSAndreas Gohr } else { 11971d5856cfSAndreas Gohr // we're in request phase 11981d5856cfSAndreas Gohr 1199bcc94b2cSAndreas Gohr if (!$INPUT->post->bool('save')) return false; 12001d5856cfSAndreas Gohr 1201bcc94b2cSAndreas Gohr if (!$INPUT->post->str('login')) { 12021d5856cfSAndreas Gohr msg($lang['resendpwdmissing'], -1); 12031d5856cfSAndreas Gohr return false; 12041d5856cfSAndreas Gohr } else { 1205bcc94b2cSAndreas Gohr $user = trim($auth->cleanUser($INPUT->post->str('login'))); 12061d5856cfSAndreas Gohr } 12071d5856cfSAndreas Gohr 12084dc42f7fSGerrit Uitslag $userinfo = $auth->getUserData($user, false); 12091d5856cfSAndreas Gohr if (!$userinfo['mail']) { 12101d5856cfSAndreas Gohr msg($lang['resendpwdnouser'], -1); 12111d5856cfSAndreas Gohr return false; 12121d5856cfSAndreas Gohr } 12131d5856cfSAndreas Gohr 12141d5856cfSAndreas Gohr // generate auth token 1215483b6238SMichael Hamann $token = md5(auth_randombytes(16)); // random secret 12162401f18dSSyntaxseed $tfile = $conf['cachedir'] . '/' . $token[0] . '/' . $token . '.pwauth'; 121724870174SAndreas Gohr $url = wl('', ['do' => 'resendpwd', 'pwauth' => $token], true, '&'); 12181d5856cfSAndreas Gohr 12191d5856cfSAndreas Gohr io_saveFile($tfile, $user); 12201d5856cfSAndreas Gohr 12211d5856cfSAndreas Gohr $text = rawLocale('pwconfirm'); 122224870174SAndreas Gohr $trep = ['FULLNAME' => $userinfo['name'], 'LOGIN' => $user, 'CONFIRM' => $url]; 12231d5856cfSAndreas Gohr 1224d7169d19SAndreas Gohr $mail = new Mailer(); 1225d7169d19SAndreas Gohr $mail->to($userinfo['name'] . ' <' . $userinfo['mail'] . '>'); 1226d7169d19SAndreas Gohr $mail->subject($lang['regpwmail']); 1227d7169d19SAndreas Gohr $mail->setBody($text, $trep); 1228d7169d19SAndreas Gohr if ($mail->send()) { 12291d5856cfSAndreas Gohr msg($lang['resendpwdconfirm'], 1); 12301d5856cfSAndreas Gohr } else { 12311d5856cfSAndreas Gohr msg($lang['regmailfail'], -1); 12321d5856cfSAndreas Gohr } 12331d5856cfSAndreas Gohr return true; 12341d5856cfSAndreas Gohr } 1235ab5d26daSAndreas Gohr // never reached 12368b06d178Schris} 12378b06d178Schris 12388b06d178Schris/** 1239b0855b11Sandi * Encrypts a password using the given method and salt 1240b0855b11Sandi * 1241b0855b11Sandi * If the selected method needs a salt and none was given, a random one 1242b0855b11Sandi * is chosen. 1243b0855b11Sandi * 1244b0855b11Sandi * @author Andreas Gohr <andi@splitbrain.org> 124542ea7f44SGerrit Uitslag * 1246ab5d26daSAndreas Gohr * @param string $clear The clear text password 1247ab5d26daSAndreas Gohr * @param string $method The hashing method 1248ab5d26daSAndreas Gohr * @param string $salt A salt, null for random 1249b0855b11Sandi * @return string The crypted password 1250b0855b11Sandi */ 1251d868eb89SAndreas Gohrfunction auth_cryptPassword($clear, $method = '', $salt = null) 1252d868eb89SAndreas Gohr{ 1253b0855b11Sandi global $conf; 1254b0855b11Sandi if (empty($method)) $method = $conf['passcrypt']; 125510a76f6fSfrank 12563a0a2d05SAndreas Gohr $pass = new PassHash(); 12573a0a2d05SAndreas Gohr $call = 'hash_' . $method; 1258b0855b11Sandi 12593a0a2d05SAndreas Gohr if (!method_exists($pass, $call)) { 1260b0855b11Sandi msg("Unsupported crypt method $method", -1); 12613a0a2d05SAndreas Gohr return false; 1262b0855b11Sandi } 12633a0a2d05SAndreas Gohr 12643a0a2d05SAndreas Gohr return $pass->$call($clear, $salt); 1265b0855b11Sandi} 1266b0855b11Sandi 1267b0855b11Sandi/** 1268b0855b11Sandi * Verifies a cleartext password against a crypted hash 1269b0855b11Sandi * 1270ab5d26daSAndreas Gohr * @param string $clear The clear text password 1271ab5d26daSAndreas Gohr * @param string $crypt The hash to compare with 1272ab5d26daSAndreas Gohr * @return bool true if both match 12734dc42f7fSGerrit Uitslag * @throws Exception 12744dc42f7fSGerrit Uitslag * 12754dc42f7fSGerrit Uitslag * @author Andreas Gohr <andi@splitbrain.org> 1276b0855b11Sandi */ 1277d868eb89SAndreas Gohrfunction auth_verifyPassword($clear, $crypt) 1278d868eb89SAndreas Gohr{ 12793a0a2d05SAndreas Gohr $pass = new PassHash(); 12803a0a2d05SAndreas Gohr return $pass->verify_hash($clear, $crypt); 1281b0855b11Sandi} 1282340756e4Sandi 1283a0b5b007SChris Smith/** 1284a0b5b007SChris Smith * Set the authentication cookie and add user identification data to the session 1285a0b5b007SChris Smith * 1286a0b5b007SChris Smith * @param string $user username 1287a0b5b007SChris Smith * @param string $pass encrypted password 1288a0b5b007SChris Smith * @param bool $sticky whether or not the cookie will last beyond the session 1289ab5d26daSAndreas Gohr * @return bool 1290a0b5b007SChris Smith */ 1291d868eb89SAndreas Gohrfunction auth_setCookie($user, $pass, $sticky) 1292d868eb89SAndreas Gohr{ 1293a0b5b007SChris Smith global $conf; 1294e1d9dcc8SAndreas Gohr /* @var AuthPlugin $auth */ 1295a0b5b007SChris Smith global $auth; 129679d00841SOliver Geisen global $USERINFO; 1297a0b5b007SChris Smith 12986547cfc7SGerrit Uitslag if (!$auth instanceof AuthPlugin) return false; 1299a0b5b007SChris Smith $USERINFO = $auth->getUserData($user); 1300a0b5b007SChris Smith 1301a0b5b007SChris Smith // set cookie 1302645c0a36SAndreas Gohr $cookie = base64_encode($user) . '|' . ((int) $sticky) . '|' . base64_encode($pass); 130373ab87deSGabriel Birke $cookieDir = empty($conf['cookiedir']) ? DOKU_REL : $conf['cookiedir']; 1304c66972f2SAdrian Lang $time = $sticky ? (time() + 60 * 60 * 24 * 365) : 0; //one year 1305bf8392ebSAndreas Gohr setcookie(DOKU_COOKIE, $cookie, [ 1306bf8392ebSAndreas Gohr 'expires' => $time, 1307bf8392ebSAndreas Gohr 'path' => $cookieDir, 1308bf8392ebSAndreas Gohr 'secure' => ($conf['securecookie'] && is_ssl()), 1309bf8392ebSAndreas Gohr 'httponly' => true, 1310486f82fcSAndreas Gohr 'samesite' => $conf['samesitecookie'] ?: null, // null means browser default 1311bf8392ebSAndreas Gohr ]); 131255a71a16SGerrit Uitslag 1313a0b5b007SChris Smith // set session 1314a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; 1315234ce57eSAndreas Gohr $_SESSION[DOKU_COOKIE]['auth']['pass'] = sha1($pass); 1316a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['buid'] = auth_browseruid(); 1317a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO; 1318a0b5b007SChris Smith $_SESSION[DOKU_COOKIE]['auth']['time'] = time(); 1319ab5d26daSAndreas Gohr 1320ab5d26daSAndreas Gohr return true; 1321a0b5b007SChris Smith} 1322a0b5b007SChris Smith 1323645c0a36SAndreas Gohr/** 1324645c0a36SAndreas Gohr * Returns the user, (encrypted) password and sticky bit from cookie 1325645c0a36SAndreas Gohr * 1326645c0a36SAndreas Gohr * @returns array 1327645c0a36SAndreas Gohr */ 1328d868eb89SAndreas Gohrfunction auth_getCookie() 1329d868eb89SAndreas Gohr{ 1330c66972f2SAdrian Lang if (!isset($_COOKIE[DOKU_COOKIE])) { 133124870174SAndreas Gohr return [null, null, null]; 1332c66972f2SAdrian Lang } 133324870174SAndreas Gohr [$user, $sticky, $pass] = sexplode('|', $_COOKIE[DOKU_COOKIE], 3, ''); 1334645c0a36SAndreas Gohr $sticky = (bool) $sticky; 1335645c0a36SAndreas Gohr $pass = base64_decode($pass); 1336645c0a36SAndreas Gohr $user = base64_decode($user); 133724870174SAndreas Gohr return [$user, $sticky, $pass]; 1338645c0a36SAndreas Gohr} 1339645c0a36SAndreas Gohr 1340e3776c06SMichael Hamann//Setup VIM: ex: et ts=2 : 1341